13 , root_(make_node(r)) {
18 : world_(muts.front()->world())
19 , root_(make_node(nullptr)) {
20 for (
auto mut :
muts) make_node(mut, root_);
26 , root_(make_node(nullptr)) {
31void Nest::populate() {
32 std::queue<Node*> queue;
37 for (
auto [_, child] : root_->child_mut2node_) queue.push(child);
39 while (!queue.empty()) {
40 auto curr_node =
pop(queue);
41 for (
auto local_mut : curr_node->mut()->mut_local_muts()) {
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));
56 for (
auto var : local_mut->free_vars()) {
57 if (
auto node = mut2node_nonconst(var->mut()); node && node->
level() > max) {
62 queue.push(make_node(local_mut, parent));
68Nest::Node* Nest::make_node(Def* mut, Node* parent) {
69 auto node = std::unique_ptr<Node>(
new Node(*
this, mut, parent));
70 auto res = node.get();
71 mut2node_.emplace(mut, std::move(node));
73 if (
auto var = mut->has_var()) vars_ =
world().
vars().insert(vars_, var);
79 while (n->
level() < m->level()) m = m->parent();
88void Nest::deps(Node* curr)
const {
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);
104 for (
auto [_, child] : curr->child_mut2node_) {
105 curr->curr_child = child;
107 curr->curr_child =
nullptr;
111void Nest::find_SCCs(Node* curr)
const {
113 for (
auto [_, child] : curr->child_mut2node_) {
114 child->loop_depth_ = child->is_recursive() ? curr->loop_depth() + 1 : curr->loop_depth();
119void Nest::Node::find_SCCs() {
121 for (
int i = 0;
auto& [_, node] : child_mut2node_)
122 if (node->idx_ == Unvisited) i = node->tarjan(i,
this, stack);
125uint32_t Nest::Node::tarjan(uint32_t i, Node* parent, Stack& stack) {
126 this->idx_ = this->low_ = i++;
127 this->on_stack_ =
true;
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_);
135 if (this->idx_ == this->low_) {
136 parent_->topo_.emplace_front(std::make_unique<SCC>());
137 SCC* scc = parent_->topo_.front().get();
142 node->on_stack_ =
false;
143 node->recursive_ =
true;
144 node->low_ = this->idx_;
148 auto [_, ins] = parent_->SCCs_.emplace(node, scc);
150 }
while (node !=
this);
152 if (num == 1 && !this->depends_.contains(
this)) this->recursive_ =
false;
Transitively collects all closed mutables (Def::is_closed) in a World.
const Node * parent() const
static const Node * lca(const Node *n, const Node *m)
Least common ancestor of n and m.
const Node * root() const
const auto & mut2node() const
bool contains(const Def *def) const
This is a thin wrapper for std::span<T, N> with the following additional features:
The World represents the whole program and manages creation of MimIR nodes (Defs).
auto pop(S &s) -> decltype(s.top(), typename S::value_type())