MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
rewrite.cpp
Go to the documentation of this file.
1#include "mim/rewrite.h"
2
3#include "mim/world.h"
4
5// Don't use fancy C++-lambdas; it's way to annoying stepping through them in a debugger.
6
7namespace mim {
8
10 if (old_def->isa<Univ>()) return world().univ();
11 if (auto i = old2new_.find(old_def); i != old2new_.end()) return i->second;
12 if (auto old_mut = old_def->isa_mut()) return rewrite_mut(old_mut);
13 return map(old_def, rewrite_imm(old_def));
14}
15
17 // Extracts are used as conditional branches: make sure that we don't rewrite unreachable stuff.
18 if (auto extract = old_def->isa<Extract>()) {
19 if (auto index = Lit::isa(rewrite(extract->index()))) {
20 if (auto tuple = extract->tuple()->isa<Tuple>()) return rewrite(tuple->op(*index));
21 if (auto pack = extract->tuple()->isa_imm<Pack>(); pack && pack->shape()->dep_const())
22 return rewrite(pack->body());
23 }
24 }
25
26 auto new_type = old_def->isa<Type>() ? nullptr : rewrite(old_def->type());
27 auto new_ops = DefVec(old_def->num_ops());
28 for (size_t i = 0, e = new_ops.size(); i != e; ++i) new_ops[i] = rewrite(old_def->op(i));
29 return old_def->rebuild(world(), new_type, new_ops);
30}
31
33 auto new_type = rewrite(old_mut->type());
34 auto new_mut = old_mut->stub(world(), new_type);
35 map(old_mut, new_mut);
36
37 if (old_mut->is_set()) {
38 for (size_t i = 0, e = old_mut->num_ops(); i != e; ++i) new_mut->set(i, rewrite(old_mut->op(i)));
39 if (auto new_imm = new_mut->immutabilize()) return map(old_mut, new_imm);
40 }
41
42 return new_mut;
43}
44
45DefVec rewrite(Def* mut, Ref arg) {
46 auto rw = VarRewriter(mut->var(), arg);
47 DefVec result(mut->num_ops());
48 for (size_t i = 0, e = result.size(); i != e; ++i) result[i] = rw.rewrite(mut->op(i));
49 return result;
50}
51
52} // namespace mim
Base class for all Defs.
Definition def.h:223
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:309
size_t num_ops() const
Definition def.h:270
const Def * op(size_t i) const
Definition def.h:269
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:447
Def * stub(World &w, Ref type)
Definition def.h:505
bool dep_const() const
Definition def.h:338
Ref var(nat_t a, nat_t i)
Definition def.h:401
const Def * type() const
Definition def.h:248
Ref rebuild(World &w, Ref type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
Definition def.h:509
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:152
static std::optional< T > isa(Ref def)
Definition def.h:763
A (possibly paramterized) Tuple.
Definition tuple.h:112
const Def * shape() const
Definition tuple.cpp:40
Helper class to retrieve Infer::arg if present.
Definition def.h:86
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
virtual Ref rewrite_mut(Def *)
Definition rewrite.cpp:32
virtual Ref rewrite(Ref)
Definition rewrite.cpp:9
Data constructor for a Sigma.
Definition tuple.h:49
const Univ * univ()
Definition world.h:204
Definition cfg.h:11
Vector< const Def * > DefVec
Definition def.h:62
DefVec rewrite(Def *mut, Ref arg)
Definition rewrite.cpp:45