MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
eta_exp_phase.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/phase.h"
4
5#include "mim/util/util.h"
6
7namespace mim {
8
9/// This phase takes care that Lam%das appear either **only** in callee position (Known) or not (Unknown).
10/// If a function `f` is both Known and Unknown,
11/// this Phase will η-expand the Unknown occurance which makes the function Known: `g f -> g (λx.f x)`
12class EtaExpPhase : public RWPhase {
13public:
16
17private:
18 enum Lattice : u8 {
19 None = 0,
20 Known = 1 << 0,
21 Unknown = 1 << 1,
22 Both = Known | Unknown,
23 };
24
25 static Lattice join(Lattice l1, Lattice l2) { return Lattice((u8)l1 | (u8)l2); }
26
27 void join(const Lam* lam, Lattice l) {
28 if (auto [i, ins] = lam2lattice_.emplace(lam, l); !ins) i->second = join(i->second, l);
29 }
30
31 Lattice lattice(const Lam* lam) {
32 if (auto i = lam2lattice_.find(lam); i != lam2lattice_.end()) return i->second;
33 return None;
34 }
35
36 bool analyze() final;
37 void analyze(const Def*);
38 void visit(const Def*, Lattice = Lattice::Unknown);
39
40 void rewrite_external(Def*) final;
41 const Def* rewrite(const Def*) final;
42 const Def* rewrite_imm_App(const App*) final;
43 const Def* rewrite_imm_Var(const Var*) final;
44 const Def* rewrite_no_eta(const Def* old_def) { return Rewriter::rewrite(old_def); }
45
46 DefSet analyzed_;
47 GIDMap<const Lam*, Lattice> lam2lattice_;
49};
50
51} // namespace mim
const Def * rewrite(const Def *) final
const Def * rewrite_imm_Var(const Var *) final
const Def * rewrite_imm_App(const App *) final
EtaExpPhase(World &world, flags_t annex)
void rewrite_external(Def *) final
bool analyze() final
You can do an optional fixed-point loop on the RWPhase::old_world before rewriting.
RWPhase(World &world, std::string name)
Definition phase.h:71
World & world()=delete
Hides both and forbids direct access.
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:27
flags_t annex() const
Definition pass.h:68
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:32
Definition ast.h:14
@ None
Definition def.h:124
u64 flags_t
Definition types.h:45
absl::flat_hash_map< K, V, GIDHash< K > > GIDMap
Definition util.h:195
GIDSet< const Def * > DefSet
Definition def.h:74
uint8_t u8
Definition types.h:34
@ Lam
Definition def.h:114
@ Var
Definition def.h:114
@ App
Definition def.h:114