MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
rewrite.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/world.h"
4
5namespace mim {
6
7/// Recurseivly rewrites part of a program **into** the provided World.
8/// This World may be different than the World we started with.
9class Rewriter {
10public:
12 : world_(world) {}
13
14 World& world() { return world_; }
15 /// Map @p old_def to @p new_def and returns @p new_def;
16 Ref map(Ref old_def, Ref new_def) { return old2new_[old_def] = new_def; }
17
18 /// @name rewrite
19 /// Recursively rewrite old Def%s.
20 ///@{
21 virtual Ref rewrite(Ref);
22 virtual Ref rewrite_imm(Ref);
23 virtual Ref rewrite_mut(Def*);
24 ///@}
25
26private:
27 World& world_;
28 Def2Def old2new_;
29};
30
31class VarRewriter : public Rewriter {
32public:
34 : Rewriter(var->world()) {
35 if (var) {
36 if (auto v = var->isa<Var>()) {
37 map(var, arg);
38 vars_ = world().vars(v);
39 }
40 }
41 }
42
43 Ref rewrite_imm(Ref imm) override {
44 if (imm->local_vars().empty() && imm->local_muts().empty()) return imm; // safe to skip
45 return Rewriter::rewrite_imm(imm);
46 }
47
48 Ref rewrite_mut(Def* mut) override {
49 if (world().has_intersection(mut->free_vars(), vars_)) {
50 if (auto var = mut->has_var()) vars_ = world().insert(vars_, var);
51 return Rewriter::rewrite_mut(mut);
52 }
53 return map(mut, mut);
54 }
55
56private:
57 Vars vars_;
58};
59
60/// @name rewrite
61/// Rewrites @p mut's ops by substituting @p mut's @p Var with @p arg.
62///@{
63DefVec rewrite(Def* mut, Ref arg);
64
65/// As above but only rewrites @p mut's @p i^th op.
66inline Ref rewrite(size_t i, Def* mut, Ref arg) { return VarRewriter(mut->var(), arg).rewrite(mut->op(i)); }
67
68/// As above but rewrites @p def.
69inline Ref rewrite(Ref def, Def* mut, Ref arg) { return VarRewriter(mut->var(), arg).rewrite(def); }
70///@}
71
72} // namespace mim
Base class for all Defs.
Definition def.h:223
const Def * op(size_t i) const
Definition def.h:269
Vars local_vars() const
Definition def.h:421
Ref var(nat_t a, nat_t i)
Definition def.h:401
Muts local_muts() const
Definition def.cpp:321
Vars free_vars() const
Definition def.cpp:326
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:405
constexpr bool empty() const noexcept
Definition pool.h:61
Helper class to retrieve Infer::arg if present.
Definition def.h:86
Recurseivly rewrites part of a program into the provided World.
Definition rewrite.h:9
virtual Ref rewrite_imm(Ref)
Definition rewrite.cpp:16
World & world()
Definition rewrite.h:14
Ref map(Ref old_def, Ref new_def)
Map old_def to new_def and returns new_def;.
Definition rewrite.h:16
Rewriter(World &world)
Definition rewrite.h:11
virtual Ref rewrite_mut(Def *)
Definition rewrite.cpp:32
virtual Ref rewrite(Ref)
Definition rewrite.cpp:9
VarRewriter(Ref var, Ref arg)
Definition rewrite.h:33
Ref rewrite_imm(Ref imm) override
Definition rewrite.h:43
Ref rewrite_mut(Def *mut) override
Definition rewrite.h:48
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
Vars vars(const Var *var)
Definition world.h:505
Ref insert(Ref d, Ref i, Ref val)
Definition world.cpp:352
Definition cfg.h:11
DefMap< const Def * > Def2Def
Definition def.h:60
Vector< const Def * > DefVec
Definition def.h:62
DefVec rewrite(Def *mut, Ref arg)
Definition rewrite.cpp:45