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
6 for (auto def : old_world().externals())
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);
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 todo_ = true;
35 return rewrite(old_lam->body());
36 }
37
38 return Rewriter::rewrite_imm_App(app);
39}
40
41} // namespace mim
const Def * callee() const
Definition lam.h:277
const Def * arg() const
Definition lam.h:286
bool analyze() final
You can do an optional fixed-point loop on the RWPhase::old_world before rewriting.
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:295
Defs deps() const noexcept
Definition def.cpp:470
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:485
A function.
Definition lam.h:111
bool todo_
Set to true to indicate that you want to rerun all Phasees in current your fixed-point PhaseMan.
Definition phase.h:57
World & old_world()
Get old Defs from here.
Definition phase.h:99
virtual const Def * map(const Def *old_def, const Def *new_def)
Definition rewrite.h:59
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:27
A variable introduced by a binder (mutable).
Definition def.h:700
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:95
Definition ast.h:14
bool visit(IndexSet< Indexer, Key > &set, const Key &key)
Definition indexset.h:100
@ Lam
Definition def.h:114