MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
nest.cpp
Go to the documentation of this file.
1#include "mim/nest.h"
2
3#include "mim/world.h"
4
5#include "mim/phase/phase.h"
6
7namespace mim {
8
10 : world_(r->world())
11 , root_(make_node(r)) {
12 populate();
13}
14
16 : world_(muts.front()->world())
17 , root_(make_node(nullptr)) {
18 for (auto mut : muts) make_node(mut, root_);
19 populate();
20}
21
23 : world_(world)
24 , root_(make_node(nullptr)) {
25 for (auto mut : ClosedCollector<>::collect(world)) make_node(mut, root_);
26 populate();
27}
28
29void Nest::populate() {
30 std::queue<Node*> queue;
31
32 if (root()->mut())
33 queue.push(root_);
34 else
35 for (auto [_, child] : root_->child_mut2node_) queue.push(child);
36
37 while (!queue.empty()) {
38 auto curr_node = pop(queue);
39 for (auto op : curr_node->mut()->deps()) {
40 for (auto local_mut : op->local_muts()) {
41 if (mut2node(local_mut) || !contains(local_mut)) continue;
42
43 if (curr_node->level() < local_mut->free_vars().size()) {
44 for (auto node = curr_node;; node = node->parent_) {
45 if (auto var = node->mut()->has_var()) {
46 if (local_mut->free_vars().contains(var)) {
47 queue.push(make_node(local_mut, node));
48 break;
49 }
50 }
51 }
52 } else {
53 uint32_t max = 0;
54 auto parent = root_;
55 for (auto var : local_mut->free_vars()) {
56 if (auto node = mut2node_nonconst(var->mut()); node && node->level() > max) {
57 max = node->level();
58 parent = node;
59 }
60 }
61 queue.push(make_node(local_mut, parent));
62 }
63 }
64 }
65 }
66}
67
68Nest::Node* Nest::make_node(Def* mut, Node* parent) {
69 auto node = std::unique_ptr<Node>(new Node(*this, mut, parent)); // can't use make_unique - c'tor is private
70 auto res = node.get();
71 mut2node_.emplace(mut, std::move(node));
72 if (mut) {
73 if (auto var = mut->has_var()) vars_ = world().vars().insert(vars_, var);
74 }
75 return res;
76}
77
78const Nest::Node* Nest::lca(const Node* n, const Node* m) {
79 while (n->level() < m->level()) m = m->parent();
80 while (m->level() < n->level()) n = n->parent();
81 while (n != m) {
82 n = n->parent();
83 m = m->parent();
84 }
85 return n;
86}
87
88void Nest::deps(Node* curr) const {
89 if (curr->mut()) {
90 for (auto op : curr->mut()->deps()) {
91 for (auto local_mut : op->local_muts()) {
92 if (auto local_node = mut2node_nonconst(local_mut)) {
93 if (local_node == curr)
94 local_node->link(local_node);
95 else if (auto parent = local_node->parent()) {
96 if (auto curr_child = parent->curr_child) {
97 assert(parent->child_mut2node_.contains(curr_child->mut()));
98 curr_child->link(local_node);
99 }
100 }
101 }
102 }
103 }
104 }
105
106 for (auto [_, child] : curr->child_mut2node_) {
107 curr->curr_child = child;
108 deps(child);
109 curr->curr_child = nullptr;
110 }
111}
112
113void Nest::find_SCCs(Node* curr) const {
114 curr->find_SCCs();
115 for (auto [_, child] : curr->child_mut2node_) {
116 child->loop_depth_ = child->is_recursive() ? curr->loop_depth() + 1 : curr->loop_depth();
117 find_SCCs(child);
118 }
119}
120
121void Nest::Node::find_SCCs() {
122 Stack stack;
123 for (int i = 0; auto& [_, node] : child_mut2node_)
124 if (node->idx_ == Unvisited) i = node->tarjan(i, this, stack);
125}
126
127uint32_t Nest::Node::tarjan(uint32_t i, Node* parent, Stack& stack) {
128 this->idx_ = this->low_ = i++;
129 this->on_stack_ = true;
130 stack.emplace(this);
131
132 for (auto dep : this->depends_) {
133 if (dep->idx_ == Unvisited) i = dep->tarjan(i, parent, stack);
134 if (dep->on_stack_) this->low_ = std::min(this->low_, dep->low_);
135 }
136
137 if (this->idx_ == this->low_) {
138 parent_->topo_.emplace_front(std::make_unique<SCC>());
139 SCC* scc = parent_->topo_.front().get();
140 Node* node;
141 int num = 0;
142 do {
143 node = pop(stack);
144 node->on_stack_ = false;
145 node->recursive_ = true;
146 node->low_ = this->idx_;
147 ++num;
148
149 scc->emplace(node);
150 auto [_, ins] = parent_->SCCs_.emplace(node, scc);
151 assert_unused(ins);
152 } while (node != this);
153
154 if (num == 1 && !this->depends_.contains(this)) this->recursive_ = false;
155 }
156
157 return i;
158}
159
160} // namespace mim
static Vector< M * > collect(World &world)
Wrapper to directly receive all closed mutables as Vector.
Definition phase.h:189
Base class for all Defs.
Definition def.h:198
Muts local_muts() const
Mutables reachable by following immutable deps(); mut->local_muts() is by definition the set { mut }...
Definition def.cpp:316
uint32_t level() const
Definition nest.h:26
const Node * parent() const
Definition nest.h:19
World & world() const
Definition nest.h:128
static const Node * lca(const Node *n, const Node *m)
Least common ancestor of n and m.
Definition nest.cpp:78
auto muts() const
Definition nest.h:137
const Node * root() const
Definition nest.h:129
Nest(Def *root)
Definition nest.cpp:9
const auto & mut2node() const
Definition nest.h:141
bool contains(const Def *def) const
Definition nest.h:131
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
auto & vars()
Definition world.h:514
Definition ast.h:14
auto pop(S &s) -> decltype(s.top(), typename S::value_type())
Definition util.h:80
Span< const T, N > View
Definition span.h:88
Node
Definition def.h:83