MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
subst.h
Go to the documentation of this file.
1#pragma once
2
3#include <mim/rewrite.h>
4#include <mim/world.h>
5
7
8namespace mim::plug::mach {
9
10/// Rewrites a value of a block under reconstruction: memory becomes @p m; @p map and @p lams replace values and blocks.
11/// `mach.spilling` and `mach.constrain` rebuild a block in place, which neither Rewriter nor VarRewriter can do:
12/// - The keys are no Var%s: @p map holds the current version of each value, instructions and projections alike.
13/// After reloads, splits, and SSA reconstruction, a value has several definitions.
14/// - Every `mem.M` becomes @p m, the block's current chain end.
15/// - It never builds an instruction itself, since the chain places them one by one; reaching an unmapped one bails.
16/// Closed Def%s and mutables pass through as they are.
17/// - The substitution changes after every instruction, as @p map and @p m move on.
18/// A memo shared across instructions would be wrong, so each instruction gets a Subst of its own.
19class Subst : public Rewriter {
20public:
21 Subst(const Target& target, World& world, const Def2Def& map, const Def* m, const LamMap<Lam*>* lams = nullptr)
23 , target_(target)
24 , map_(map)
25 , lams_(lams)
26 , m_(m) {}
27
28 const Def* rewrite(const Def* def) final;
29
30 /// Rewrites @p term per argument, so an instruction passed as the whole argument gets the new memory.
31 const Def* rewrite_jump(const App* term) {
32 auto n = term->num_args();
33 auto args = DefVec(n, [&](size_t i) { return rewrite(term->arg(n, i)); });
34 return world().app(rewrite(term->callee()), world().tuple(args));
35 }
36
37private:
38 const Target& target_;
39 const Def2Def& map_;
40 const LamMap<Lam*>* lams_;
41 const Def* m_;
42};
43
44} // namespace mim::plug::mach
nat_t num_args() const noexcept
Definition lam.h:252
const Def * callee() const
Definition lam.h:237
const Def * arg() const
Definition lam.h:251
Base class for all Defs.
Definition def.h:285
World & world()
Definition rewrite.h:33
const Def * map(const Def *old_def, const Def *new_def)
Definition rewrite.cpp:29
Rewriter(World &world)
Definition rewrite.cpp:16
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
const Def * app(const Def *callee, const Def *arg)
Definition world.cpp:416
const Def * rewrite(const Def *def) final
Definition subst.cpp:5
const Def * rewrite_jump(const App *term)
Rewrites term per argument, so an instruction passed as the whole argument gets the new memory.
Definition subst.h:31
Subst(const Target &target, World &world, const Def2Def &map, const Def *m, const LamMap< Lam * > *lams=nullptr)
Definition subst.h:21
The machine facts of a native backend that the phases of mach need.
Definition target.h:45
The mach Plugin
Definition asm.h:15
The tuple Plugin
DefMap< const Def * > Def2Def
Definition def.h:93
GIDMap< Lam *, To > LamMap
Definition lam.h:210
fe::Vector< const Def * > DefVec
Definition def.h:96