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().annexes())
7 visit(def, false);
8 for (auto def : old_world().externals())
9 visit(def, false);
10
11 return false; // no fixed-point neccessary
12}
13
14void BetaRedPhase::analyze(const Def* def) {
15 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
16 if (def->isa<Var>()) return; // ignore Var's mut
17
18 for (auto d : def->deps())
19 visit(d);
20}
21
22void BetaRedPhase::visit(const Def* def, bool candidate) {
23 if (auto lam = def->isa_mut<Lam>()) {
24 if (auto [i, ins] = candidates_.emplace(lam, candidate); !ins) i->second = false;
25 }
26 analyze(def);
27}
28
30 if (auto old_lam = app->callee()->isa_mut<Lam>(); old_lam && old_lam->is_set() && is_candidate(old_lam)) {
31 DLOG("beta-reduce: `{}`", old_lam);
32 if (auto var = old_lam->has_var()) {
33 auto new_arg = rewrite(app->arg());
34 map(var, new_arg);
35 // if we want to reduce more than once, we need to push/pop
36 }
37 todo_ = true;
38 return rewrite(old_lam->body());
39 }
40
41 return Rewriter::rewrite_imm_App(app);
42}
43
44} // 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:467
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