3#include <absl/container/fixed_array.h>
7const Def* SymExprOpt::Analysis::propagate(
const Def* var,
const Def* def) {
8 auto [i, ins] =
lattice_.emplace(var, def);
11 DLOG(
"propagate: {} → {}", var, def);
16 if (!cur || def->isa<
Bot>() || cur == def || cur == var || cur->isa<
Proxy>())
return cur;
19 DLOG(
"cannot propagate {}, trying GVN", var);
20 if (cur->isa<
Bot>())
return i->second = def;
21 return i->second =
nullptr;
26const Def* SymExprOpt::Analysis::rewrite_imm_App(
const App* app) {
28 auto n = app->num_targs();
29 auto abstr_args = absl::FixedArray<const Def*>(n);
30 auto abstr_vars = absl::FixedArray<const Def*>(n);
33 for (
size_t i = 0; i != n; ++i) {
34 auto abstr =
rewrite(app->targ(i));
35 abstr_vars[i] = propagate(lam->tvar(i), abstr);
36 abstr_args[i] = abstr;
41 for (
size_t i = 0; i != n; ++i) {
42 if (abstr_vars[i])
continue;
45 auto vi = lam->tvar(i);
46 auto ai = abstr_args[i];
47 vars.emplace_back(vi);
49 for (
size_t j = i + 1; j != n; ++j) {
50 auto vj = lam->tvar(j);
51 if (!abstr_vars[j] && abstr_args[j] == ai) vars.emplace_back(vj);
54 if (vars.size() == 1) {
55 lattice_[vi] = abstr_vars[i] = vi;
57 auto proxy =
world().
proxy(vi->type(), vars, 0, 0);
59 for (
auto p : proxy->ops()) {
61 auto vj = lam->tvar(j);
62 lattice_[vj] = abstr_vars[j] = proxy;
65 DLOG(
"bundle: {}", proxy);
78 for (
size_t i = 0; i != n; ++i) {
79 if (
auto proxy = abstr_vars[i]->isa<Proxy>()) {
80 auto num = proxy->num_ops();
82 auto ai = abstr_args[i];
84 for (
auto p : proxy->ops()) {
86 auto vj = lam->tvar(j);
88 if (ai == abstr_args[j]) vars.emplace_back(vj);
92 auto new_num = vars.size();
95 auto vi = lam->tvar(i);
96 lattice_[vi] = abstr_vars[i] = vi;
97 DLOG(
"single: {}", vi);
98 }
else if (new_num != num) {
100 auto new_proxy =
world().
proxy(ai->type(), vars, 0, 0);
101 DLOG(
"split: {}", new_proxy);
103 for (
auto p : new_proxy->ops()) {
105 auto vj = lam->tvar(j);
106 if (p == vj) lattice_[vj] = abstr_vars[j] = new_proxy;
115 map(lam->var(), abstr_var);
116 lattice_[lam->var()] = abstr_var;
119 for (
auto d : lam->deps())
122 return world().
app(lam, abstr_args);
125 return mim::Analysis::rewrite_imm_App(app);
129 if (old_var == abstr)
return true;
130 auto proxy = abstr->isa<
Proxy>();
131 return proxy && proxy->
op(0) == old_var;
136 if (
auto l =
lattice(old_lam->var()); l && l != old_lam->var()) {
139 size_t num_old = old_lam->num_tvars();
141 if (
auto i = lam2lam_.find(old_lam); i != lam2lam_.end())
146 for (
size_t i = 0; i != num_old; ++i) {
147 auto old_var = old_lam->var(num_old, i);
149 if (
keep(old_var, abstr)) new_doms.emplace_back(
rewrite(old_lam->dom(num_old, i)));
153 size_t num_new = new_doms.size();
154 auto new_vars = absl::FixedArray<const Def*>(num_old);
156 lam2lam_[old_lam] = new_lam;
159 for (
size_t i = 0, j = 0; i != num_old; ++i) {
160 auto old_var = old_lam->var(num_old, i);
163 if (
keep(old_var, abstr)) {
164 auto v = new_lam->
var(num_new, j++);
166 if (abstr != old_var)
map(abstr, v);
172 map(old_lam->var(), new_vars);
177 size_t num_new = new_lam->
num_vars();
178 auto new_args = absl::FixedArray<const Def*>(num_new);
179 for (
size_t i = 0, j = 0; i != num_old; ++i) {
180 auto old_var = old_lam->var(num_old, i);
182 if (
keep(old_var, abstr)) new_args[j++] =
rewrite(old_app->targ(i));
185 return map(old_app,
new_world().app(new_lam, new_args));
189 return Rewriter::rewrite_imm_App(old_app);
const Def * callee() const
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
const Def * op(size_t i) const noexcept
const Def * var(nat_t a, nat_t i) noexcept
nat_t num_vars() noexcept
Lam * set(Filter filter, const Def *body)
static T as(const Def *def)
void invalidate(bool todo=true)
Signals that another round of fixed-point iteration is required, either as part of.
World & new_world()
Create new Defs into this.
const Def * lattice(const Def *old_def)
Returns the abstract value computed by the associated Analysis for the given old-world Def,...
World & world()=delete
Hides both and forbids direct access.
virtual const Def * map(const Def *old_def, const Def *new_def)
virtual const Def * rewrite(const Def *)
virtual const Def * lookup(const Def *old_def)
Lookup old_def by searching in reverse through the stack of maps.
const Def * rewrite_imm_App(const App *) final
const Def * app(const Def *callee, const Def *arg)
const Def * tuple(Defs ops)
const Proxy * proxy(const Def *type, Defs ops, u32 index, u32 tag)
Lam * mut_lam(const Pi *pi)
#define DLOG(...)
Vaporizes to nothingness in Debug build.
static bool keep(const Def *old_var, const Def *abstr)
Vector< const Def * > DefVec
static nat_t get_index(const Def *def)
Lam * isa_optimizable(Lam *lam)
These are Lams that are.