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
7#include "fe/assert.h"
8
9namespace mim {
10
12 : world_(r->world())
13 , root_(make_node(r)) {
14 populate();
15}
16
18 : world_(muts.front()->world())
19 , root_(make_node(nullptr)) {
20 for (auto mut : muts) make_node(mut, root_);
21 populate();
22}
23
25 : world_(world)
26 , root_(make_node(nullptr)) {
27 for (auto mut : ClosedCollector<>::collect(world)) make_node(mut, root_);
28 populate();
29}
30
31void Nest::populate() {
32 std::queue<Node*> queue;
33
34 if (root()->mut())
35 queue.push(root_);
36 else
37 for (auto [_, child] : root_->child_mut2node_) queue.push(child);
38
39 while (!queue.empty()) {
40 auto curr_node = pop(queue);
41 for (auto local_mut : curr_node->mut()->mut_local_muts()) {
42 if (mut2node(local_mut) || !contains(local_mut)) continue;
43
44 if (curr_node->level() < local_mut->free_vars().size()) {
45 for (auto node = curr_node;; node = node->parent_) {
46 if (auto var = node->mut()->has_var()) {
47 if (local_mut->free_vars().contains(var)) {
48 queue.push(make_node(local_mut, node));
49 break;
50 }
51 }
52 }
53 } else {
54 uint32_t max = 0;
55 auto parent = root_;
56 for (auto var : local_mut->free_vars()) {
57 if (auto node = mut2node_nonconst(var->mut()); node && node->level() > max) {
58 max = node->level();
59 parent = node;
60 }
61 }
62 queue.push(make_node(local_mut, parent));
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 local_mut : curr->mut()->mut_local_muts()) {
91 if (auto local_node = mut2node_nonconst(local_mut)) {
92 if (local_node == curr)
93 local_node->link(local_node);
94 else if (auto parent = local_node->parent()) {
95 if (auto curr_child = parent->curr_child) {
96 assert(parent->child_mut2node_.contains(curr_child->mut()));
97 curr_child->link(local_node);
98 }
99 }
100 }
101 }
102 }
103
104 for (auto [_, child] : curr->child_mut2node_) {
105 curr->curr_child = child;
106 deps(child);
107 curr->curr_child = nullptr;
108 }
109}
110
111void Nest::find_SCCs(Node* curr) const {
112 curr->find_SCCs();
113 for (auto [_, child] : curr->child_mut2node_) {
114 child->loop_depth_ = child->is_recursive() ? curr->loop_depth() + 1 : curr->loop_depth();
115 find_SCCs(child);
116 }
117}
118
119void Nest::Node::find_SCCs() {
120 Stack stack;
121 for (int i = 0; auto& [_, node] : child_mut2node_)
122 if (node->idx_ == Unvisited) i = node->tarjan(i, this, stack);
123}
124
125uint32_t Nest::Node::tarjan(uint32_t i, Node* parent, Stack& stack) {
126 this->idx_ = this->low_ = i++;
127 this->on_stack_ = true;
128 stack.emplace(this);
129
130 for (auto dep : this->depends_) {
131 if (dep->idx_ == Unvisited) i = dep->tarjan(i, parent, stack);
132 if (dep->on_stack_) this->low_ = std::min(this->low_, dep->low_);
133 }
134
135 if (this->idx_ == this->low_) {
136 parent_->topo_.emplace_front(std::make_unique<SCC>());
137 SCC* scc = parent_->topo_.front().get();
138 Node* node;
139 int num = 0;
140 do {
141 node = pop(stack);
142 node->on_stack_ = false;
143 node->recursive_ = true;
144 node->low_ = this->idx_;
145 ++num;
146
147 scc->emplace(node);
148 auto [_, ins] = parent_->SCCs_.emplace(node, scc);
149 assert_unused(ins);
150 } while (node != this);
151
152 if (num == 1 && !this->depends_.contains(this)) this->recursive_ = false;
153 }
154
155 return i;
156}
157
158} // namespace mim
Transitively collects all closed mutables (Def::is_closed) in a World.
Definition phase.h:180
Base class for all Defs.
Definition def.h:212
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:11
const auto & mut2node() const
Definition nest.h:141
bool contains(const Def *def) const
Definition nest.h:131
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
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
auto pop(S &s) -> decltype(s.top(), typename S::value_type())
Definition util.h:80