MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
beta_red_phase.cpp
Go to the documentation of this file.
2
3namespace mim {
4
6 for (auto def : old_world().roots())
7 visit(def, false);
8 return false; // no fixed-point neccessary
9}
10
11void BetaRedPhase::analyze(const Def* def) {
12 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
13 if (def->isa<Var>()) return; // ignore Var's mut
14
15 for (auto d : def->deps())
16 visit(d, true);
17}
18
19void BetaRedPhase::visit(const Def* def, bool candidate) {
20 if (auto lam = def->isa_mut<Lam>()) {
21 if (auto [i, ins] = candidates_.emplace(lam, candidate); !ins) i->second = false;
22 }
23 analyze(def);
24}
25
27 if (auto old_lam = app->callee()->isa_mut<Lam>(); old_lam && old_lam->is_set() && is_candidate(old_lam)) {
28 DLOG("beta-reduce: `{}`", old_lam);
29 if (auto var = old_lam->has_var()) {
30 auto new_arg = rewrite(app->arg());
31 map(var, new_arg);
32 // if we want to reduce more than once, we need to push/pop
33 }
34 invalidate();
35 return rewrite(old_lam->body());
36 }
37
38 return RWPhase::rewrite_imm_App(app);
39}
40
41} // namespace mim
const Def * callee() const
Definition lam.h:276
const Def * arg() const
Definition lam.h:285
bool analyze() final
Runs the optional pre-analysis on RWPhase::old_world(), typically to a fixed point,...
const Def * rewrite_imm_App(const App *) final
Base class for all Defs.
Definition def.h:246
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:475
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:493
A function.
Definition lam.h:110
void invalidate(bool todo=true)
Signals that another round of fixed-point iteration is required, either as part of.
Definition phase.h:48
World & old_world()
Get old Defs from here.
Definition phase.h:242
virtual const Def * map(const Def *old_def, const Def *new_def)
Definition rewrite.h:45
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:56
A variable introduced by a binder (mutable).
Definition def.h:714
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:94
Definition ast.h:14
@ Lam
Definition def.h:109