MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
eta_red.cpp
Go to the documentation of this file.
1#include "mim/pass/eta_red.h"
2
3namespace mim {
4
5static const App* eta_rule(Lam* lam) {
6 if (auto app = lam->body()->isa<App>()) {
7 if (app->arg() == lam->var()) return app;
8 }
9 return nullptr;
10}
11
13 callee_only_ = callee_only;
14 name_ += callee_only_ ? " tt" : " ff";
15}
16
17const Def* EtaRed::rewrite(const Def* def) {
18 for (size_t i = 0, e = def->num_ops(); i != e; ++i) {
19 // TODO (ClosureConv): Factor this out
20 if (auto lam = def->op(i)->isa_mut<Lam>(); (!callee_only_ || isa_callee(def, i)) && lam && lam->is_set()) {
21 if (auto app = eta_rule(lam); app && !irreducible_.contains(lam)) {
22 data().emplace(lam, Lattice::Reduce);
23 auto new_def = def->refine(i, app->callee());
24 DLOG("eta-reduction '{}' -> '{}' by eliminating '{}'", def, new_def, lam);
25 return new_def;
26 }
27 }
28 }
29
30 return def;
31}
32
34 if (auto lam = var->mut()->isa_mut<Lam>()) {
35 auto [_, l] = *data().emplace(lam, Lattice::Bot).first;
36 auto succ = irreducible_.emplace(lam).second;
37 if (l == Lattice::Reduce && succ) {
38 DLOG("irreducible: {}; found {}", lam, var);
39 return undo_visit(lam);
40 }
41 }
42
43 return No_Undo;
44}
45
46} // namespace mim
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
const Def * refine(size_t i, const Def *new_op) const
Definition def.cpp:229
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:485
const Def * op(size_t i) const noexcept
Definition def.h:308
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:429
constexpr size_t num_ops() const noexcept
Definition def.h:309
@ Reduce
η-reduction performed.
Definition eta_red.h:22
@ Bot
Never seen.
Definition eta_red.h:21
bool callee_only() const
Definition eta_red.h:18
void apply(bool callee_only)
Definition eta_red.cpp:12
const Def * rewrite(const Def *) override
Definition eta_red.cpp:17
undo_t analyze(const Var *) override
Definition eta_red.cpp:33
undo_t undo_visit(Def *mut) const
Definition pass.h:360
A function.
Definition lam.h:111
const Def * body() const
Definition lam.h:124
std::string name_
Definition pass.h:76
A variable introduced by a binder (mutable).
Definition def.h:700
Def * mut() const
Definition def.h:712
#define DLOG(...)
Vaporizes to nothingness in Debug build.
Definition log.h:95
Definition ast.h:14
static const App * eta_rule(Lam *lam)
Definition eta_red.cpp:5
const App * isa_callee(const Def *def, size_t i)
Definition lam.h:346
size_t undo_t
Definition pass.h:21
static constexpr undo_t No_Undo
Definition pass.h:22