MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
beta_red.cpp
Go to the documentation of this file.
1#include "mim/pass/beta_red.h"
2
3namespace mim {
4
5const Def* BetaRed::rewrite(const Def* def) {
6 if (auto [app, lam] = isa_apped_mut_lam(def); isa_workable(lam) && !keep_.contains(lam)) {
7 if (auto [_, ins] = data().emplace(lam); ins) {
8 world().DLOG("beta-reduction {}", lam);
9 return lam->reduce_body(app->arg());
10 } else {
11 return proxy(app->type(), {lam, app->arg()}, 0);
12 }
13 }
14
15 return def;
16}
17
19 auto lam = proxy->op(0)->as_mut<Lam>();
20 if (keep_.emplace(lam).second) {
21 world().DLOG("found proxy app of '{}' within '{}'", lam, curr_mut());
22 return undo_visit(lam);
23 }
24
25 return No_Undo;
26}
27
29 auto undo = No_Undo;
30 for (auto op : def->ops()) {
31 if (auto lam = isa_workable(op->isa_mut<Lam>()); lam && keep_.emplace(lam).second) {
32 auto [_, ins] = data().emplace(lam);
33 if (!ins) {
34 world().DLOG("non-callee-position of '{}'; undo inlining of {} within {}", lam, lam, curr_mut());
35 undo = std::min(undo, undo_visit(lam));
36 }
37 }
38 }
39
40 return undo;
41}
42
43} // namespace mim
const Def * rewrite(const Def *) override
Definition beta_red.cpp:5
undo_t analyze(const Proxy *) override
Definition beta_red.cpp:18
Base class for all Defs.
Definition def.h:197
constexpr auto ops() const noexcept
Definition def.h:260
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:429
undo_t undo_visit(Def *mut) const
Definition pass.h:273
A function.
Definition lam.h:106
World & world()
Definition pass.h:296
const Proxy * proxy(const Def *type, Defs ops, u32 tag=0)
Definition pass.h:75
Def * curr_mut() const
Definition pass.h:232
Definition ast.h:14
std::pair< const App *, Lam * > isa_apped_mut_lam(const Def *def)
Definition lam.h:259
Lam * isa_workable(Lam *lam)
These are Lams that are neither nullptr, nor Lam::is_external, nor Lam::is_unset.
Definition lam.h:254
size_t undo_t
Definition pass.h:14
static constexpr undo_t No_Undo
Definition pass.h:15