Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
rewrite.cpp
Go to the documentation of this file.
1#include "thorin/rewrite.h"
2
3#include "thorin/world.h"
4
5// Don't use fancy C++-lambdas; it's way to annoying stepping through them in a debugger.
6
7namespace thorin {
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 thorin
Base class for all Defs.
Definition def.h:222
Ref var(nat_t a, nat_t i)
Definition def.h:403
bool is_set() const
Yields true if empty or the last op is set.
Definition def.cpp:315
const Def * op(size_t i) const
Definition def.h:269
Def * stub(World &w, Ref type)
Definition def.h:494
size_t num_ops() const
Definition def.h:270
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:498
const Def * type() const
Yields the raw type of this Def, i.e. maybe nullptr.
Definition def.h:248
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:449
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:118
static std::optional< T > isa(Ref def)
Definition def.h:726
A (possibly paramterized) Tuple.
Definition tuple.h:87
Helper class to retrieve Infer::arg if present.
Definition def.h:87
World & world()
Definition rewrite.h:14
virtual Ref rewrite_mut(Def *)
Definition rewrite.cpp:32
virtual Ref rewrite(Ref)
Definition rewrite.cpp:9
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_imm(Ref)
Definition rewrite.cpp:16
Data constructor for a Sigma.
Definition tuple.h:39
const Univ * univ()
Definition world.h:183
Definition cfg.h:11
DefVec rewrite(Def *mut, Ref arg)
Definition rewrite.cpp:45
Vector< const Def * > DefVec
Definition def.h:63