MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
beta_red_phase.cpp
Go to the documentation of this file.
2
3namespace mim {
4
7
9 analyzed_.clear();
10 candidates_.clear();
11}
12
14 for (auto def : old_world().externals())
15 analyze(def);
16 return false; // no fixed-point neccessary
17}
18
19void BetaRedPhase::analyze(const Def* def) {
20 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
21
22 if (auto var = def->isa<Var>()) return analyze(var->type()); // ignore Var's mut
23
24 for (auto d : def->deps()) {
25 if (auto lam = d->isa_mut<Lam>()) visit(lam);
26 analyze(d);
27 }
28}
29
30void BetaRedPhase::visit(Lam* lam) {
31 if (lam->is_external()) return;
32 if (auto [i, ins] = candidates_.emplace(lam, true); !ins) i->second = false;
33}
34
36 if (auto old_lam = app->callee()->isa_mut<Lam>(); old_lam && old_lam->is_set() && is_candidate(old_lam)) {
37 if (auto var = old_lam->has_var()) {
38 auto new_arg = rewrite(app->arg());
39 push();
40 map(var, new_arg);
41 auto res = rewrite(old_lam->body());
42 pop();
43 todo_ = true;
44 return res;
45 } else {
46 todo_ = true;
47 return rewrite(old_lam->body());
48 }
49 }
50
51 return Rewriter::rewrite_imm_App(app);
52}
53
54bool BetaRedPhase::is_candidate(Lam* lam) const {
55 if (lam->is_external()) return false;
56
57 auto i = candidates_.find(lam);
58 assert(i != candidates_.end());
59 return i->second;
60}
61
62} // namespace mim
const Def * callee() const
Definition lam.h:273
const Def * arg() const
Definition lam.h:282
const Def * rewrite_imm_App(const App *) final
Base class for all Defs.
Definition def.h:251
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:298
Defs deps() const noexcept
Definition def.cpp:464
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:482
bool is_external() const noexcept
Definition def.h:464
FPPhase(World &world, std::string name)
Definition phase.h:121
A function.
Definition lam.h:109
bool todo_
Set to true to indicate that you want to rerun all Phasees in current your fixed-point PhaseMan.
Definition phase.h:55
World & world()=delete
Hides both and forbids direct access.
World & old_world()
Get old Defs from here.
Definition phase.h:98
virtual void push()
Definition rewrite.h:40
const Def * map(const Def *old_def, const Def *new_def)
Map old_def to new_def and returns new_def.
Definition rewrite.h:45
virtual void pop()
Definition rewrite.h:41
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:14
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:36
Definition ast.h:14
bool visit(IndexSet< Indexer, Key > &set, const Key &key)
Definition indexset.h:100