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().create(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 if (imm->has_dep(Dep::Infer) || vars_.has_intersection(imm->free_vars())) return Rewriter::rewrite_imm(imm);
46 return imm;
47 }
48
49 Ref rewrite_mut(Def* mut) override {
50 if (vars_.has_intersection(mut->free_vars())) {
51 if (auto var = mut->has_var()) vars_ = world().vars().insert(vars_, var);
52 return Rewriter::rewrite_mut(mut);
53 }
54 return map(mut, mut);
55 }
56
57private:
58 Vars vars_;
59};
60
61} // namespace mim
Base class for all Defs.
Definition def.h:212
Vars local_vars() const
Vars reachable by following immutable deps().
Definition def.h:405
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
Definition def.cpp:293
bool has_dep() const
Definition def.h:313
Vars free_vars() const
Compute a global solution, i.e., by transitively following mutables as well.
Definition def.cpp:304
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
Definition def.h:385
bool has_intersection(PooledSet< T > other)
Is ?
Definition pool.h:72
constexpr bool empty() const noexcept
Definition pool.h:64
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:49
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
auto & vars()
Definition world.h:522
Definition ast.h:14
DefMap< const Def * > Def2Def
Definition def.h:60