MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
adce.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/phase.h"
4
5namespace mim {
6
7/// Aggressive Dead Code Elimination.
8/// Removes Lam params nobody needs - including those that are merely forwarded to other dead params,
9/// like a loop-carried value that never leaves its loop.
10///
11/// DCE is a backward problem, but an Analysis runs forward.
12/// So we optimistically assume every param of a known Lam to be dead and propagate a Proxy for it.
13/// Afterwards, Analysis::finalize walks the abstract World and pins each param whose Proxy survived in a live position.
14/// Then, we redo.
15///
16/// Lattice per Lam::var:
17/// ```
18/// ⊤ ← Live
19/// |
20/// Dead ← Proxy
21/// |
22/// ⊥
23/// ```
24class ADCE : public RWPhase {
25private:
26 using Super = RWPhase;
27
28 class Analysis : public mim::Analysis {
29 public:
30 using Super = mim::Analysis;
31
33 : mim::Analysis(world, "ADCE::Analysis") {}
34
35 void reset() final;
36 bool is_dead(const Def* var) const;
37
38 private:
39 const Def* rewrite_imm_App(const App*) final;
40
41 void finalize() final;
42 void analyze(const Def*);
43
44 DefSet visited_;
45 };
46
47public:
49 : RWPhase(world, "ADCE", &analysis_)
50 , analysis_(world) {}
52 : RWPhase(world, annex, &analysis_)
53 , analysis_(world) {}
54
55private:
56 const Def* rewrite_imm_App(const App*) final;
57 const Def* rewrite_mut_Lam(Lam*) final;
58
59 /// Keeps @p lam's live tvars.
60 Sieve sieve(Lam* lam);
61 Lam* build_lam(Lam* old_lam);
62
63 Analysis analysis_;
64 LamMap<Sieve> lam2sieve_;
65 Lam2Lam lam_old2new_;
66 Lam2Lam lam_new2old_;
67};
68
69} // namespace mim
const Def * rewrite_imm_App(const App *) final
Definition adce.cpp:122
ADCE(World &world, flags_t annex)
Definition adce.h:51
const Def * rewrite_mut_Lam(Lam *) final
Definition adce.cpp:137
ADCE(World &world)
Definition adce.h:48
Traverses the current World using Rewriter infrastructure while staying in the same world.
Definition phase.h:130
Base class for all Defs.
Definition def.h:289
A function.
Definition lam.h:91
friend class Analysis
Definition phase.h:106
flags_t annex() const
Definition phase.h:62
virtual bool analyze()
Runs the optional pre-analysis on old_world(), typically to a fixed point, before rewriting begins.
Definition phase.cpp:202
virtual void finalize()
Run after all roots have been walked but still before the two worlds are swapped.
Definition phase.h:406
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:334
World & world()=delete
Hides both and forbids direct access.
void reset()
Definition rewrite.cpp:23
Filters the components of an aggregate and keeps track of where the survivors end up.
Definition tuple.h:116
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
Definition ast.h:16
u64 flags_t
Definition types.h:39
LamMap< Lam * > Lam2Lam
Definition lam.h:212
GIDMap< Lam *, To > LamMap
Definition lam.h:210
GIDSet< const Def * > DefSet
Definition def.h:94