MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
beta_red.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 BetaRed::analyze(const Def* def) {
12 if (auto [_, ins] = analyzed_.emplace(def); !ins) return;
13
14 for (auto d : def->deps())
15 visit(d, true);
16}
17
18void BetaRed::visit(const Def* def, bool candidate) {
19 if (auto lam = def->isa_mut<Lam>()) {
20 if (auto [i, ins] = candidates_.emplace(lam, candidate); !ins) i->second = false;
21 }
22 analyze(def);
23}
24
25const Def* BetaRed::rewrite_imm_App(const App* app) {
26 if (auto old_lam = app->callee()->isa_mut<Lam>(); old_lam && old_lam->is_set() && is_candidate(old_lam)) {
27 profile_count("β-reduction");
28 if (auto var = old_lam->has_var()) {
29 auto new_arg = rewrite(app->arg());
30 map(var, new_arg);
31 // if we want to reduce more than once, we need to push/pop
32 }
33 invalidate();
34 return rewrite(old_lam->body());
35 }
36
37 return RWPhase::rewrite_imm_App(app);
38}
39
40} // namespace mim
const Def * callee() const
Definition lam.h:276
const Def * arg() const
Definition lam.h:285
const Def * rewrite_imm_App(const App *) final
Definition beta_red.cpp:25
bool analyze() final
Runs the optional pre-analysis on RWPhase::old_world(), typically to a fixed point,...
Definition beta_red.cpp:5
Base class for all Defs.
Definition def.h:261
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:308
Defs deps() const noexcept
Definition def.cpp:514
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:527
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:98
void profile_count(std::string_view key, uint64_t n=1)
Adds n to the custom Profiler counter key of the current run; no-op unless profiling is enabled.
Definition phase.cpp:43
World & old_world()
Get old Defs from here.
Definition phase.h:367
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
Definition ast.h:14
@ Lam
Definition def.h:109