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;
113 set(lam->var(),
world().tuple(abstr_vars));
114 return world().
app(rewrite_deps(lam), abstr_args);
117 return mim::Analysis::rewrite_imm_App(app);
121 if (old_var == abstr)
return true;
122 auto proxy = abstr->isa<
Proxy>();
123 return proxy && proxy->
op(0) == old_var;
128 if (
auto l =
lattice(old_lam->var()); l && l != old_lam->var()) {
131 size_t num_old = old_lam->num_tvars();
133 if (
auto i = lam2lam_.find(old_lam); i != lam2lam_.end())
138 for (
size_t i = 0; i != num_old; ++i) {
139 auto old_var = old_lam->var(num_old, i);
141 if (
keep(old_var, abstr)) new_doms.emplace_back(
rewrite(old_lam->dom(num_old, i)));
145 size_t num_new = new_doms.size();
146 auto new_vars = absl::FixedArray<const Def*>(num_old);
148 lam2lam_[old_lam] = new_lam;
151 for (
size_t i = 0, j = 0; i != num_old; ++i) {
152 auto old_var = old_lam->var(num_old, i);
155 if (
keep(old_var, abstr)) {
156 auto v = new_lam->
var(num_new, j++);
158 if (abstr != old_var)
map(abstr, v);
164 map(old_lam->var(), new_vars);
165 auto new_filter =
rewrite(old_lam->filter());
166 auto new_body =
rewrite(old_lam->body());
167 new_lam->
set(new_filter, new_body);
171 size_t num_new = new_lam->
num_vars();
172 auto new_args = absl::FixedArray<const Def*>(num_new);
173 for (
size_t i = 0, j = 0; i != num_old; ++i) {
174 auto old_var = old_lam->var(num_old, i);
176 if (
keep(old_var, abstr)) new_args[j++] =
rewrite(old_app->targ(i));
179 return map(old_app,
new_world().app(new_lam, new_args));
183 return RWPhase::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 *)
const Def * rewrite_imm_App(const App *) final
const Def * app(const Def *callee, const Def *arg)
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.