8#include <absl/container/btree_map.h>
9#include <absl/container/btree_set.h>
17struct NullSafeDefGIDLt {
18 constexpr bool operator()(
const Def* a,
const Def* b)
const noexcept {
19 return (a ?
a->gid() : 0) < (b ? b->gid() : 0);
39 const Node*
inest()
const {
return inest_; }
43 auto idom()
const {
return calc_dominance()->idom_; }
44 bool is_root()
const {
return inest_ ==
nullptr; }
50 uint32_t
level()
const {
return level_; }
51 uint32_t
loop_depth()
const {
return sccs().loop_depth_; }
54 uint32_t
gid()
const {
return mut_ ? mut_->gid() : 0; }
63 auto mut2node()
const {
return mut2node_ | std::views::transform([](
auto p) {
return std::pair{p.first,
const_cast<const Node*
>(p.second)}; }); }
64 auto muts()
const {
return mut2node_ | std::views::keys; }
65 auto nodes()
const {
return mut2node_ | std::views::transform([](
auto p) {
return const_cast<const Node*
>(p.second); }); }
67 size_t num()
const {
return mut2node_.size(); }
78 auto begin()
const {
return mut2node_.cbegin(); }
79 auto end()
const {
return mut2node_.cend(); }
83 const auto&
mut2node() {
return mut2node_; }
84 auto nodes() {
return mut2node_ | std::views::values; }
85 auto muts() {
return mut2node_ | std::views::keys; }
86 auto begin() {
return mut2node_.begin(); }
87 auto end() {
return mut2node_.end(); }
90 absl::btree_map<Def*, Node*, GIDLt<Def*>> mut2node_;
99 template<
bool Forward>
104 return nodes_ | std::views::transform([](Node* n) {
return const_cast<const Node*
>(n); });
111 bool contains(
const Node* n)
const {
return nodes_.contains(
const_cast<Node*
>(n)); }
116 auto begin()
const {
return nodes_.cbegin(); }
117 auto end()
const {
return nodes_.cend(); }
121 const auto&
nodes() {
return nodes_; }
122 auto begin() {
return nodes_.begin(); }
123 auto end() {
return nodes_.end(); }
125 absl::btree_set<Node*, GIDLt<Node*>> nodes_;
134 template<
bool Forward = true>
136 nest().calc_sibl_deps();
137 if constexpr (Forward)
140 return sibl_rev_deps_;
143 template<
bool Forward = true>
150 using SCC = absl::btree_set<const Node*, GIDLt<const Node*>>;
156 const auto&
SCCs() {
return sccs().SCCs_; }
157 const auto&
topo()
const {
return sccs().topo_; }
172 const Node& sccs()
const {
return nest().calc_SCCs(), *
this; }
174 void link(Node* other) { this->sibl_deps_.nodes_.emplace(other), other->sibl_rev_deps_.nodes_.emplace(
this); }
175 void dot(fe::Tab, std::ostream&)
const;
178 using Stack = std::stack<Node*>;
180 uint32_t tarjan(uint32_t, Node*, Stack&);
183 const Node* calc_dominance()
const;
189 uint32_t loop_depth_ : 31 = 0;
190 bool recursive_ : 1 =
false;
194 std::deque<std::unique_ptr<SCC>> topo_;
195 absl::btree_map<const Node*, const SCC*, GIDLt<const Node*>> SCCs_;
196 mutable const Node* idom_ =
nullptr;
199 mutable std::optional<size_t> postorder_number_ = std::nullopt;
202 static constexpr uint32_t Unvisited = uint32_t(-1);
203 uint32_t idx_ = Unvisited;
204 uint32_t low_ : 31 = 0;
205 bool on_stack_ : 1 =
false;
206 Node* curr_child =
nullptr;
227 bool is_recursive()
const {
return calc_SCCs().root()->is_recursive(); }
234 auto muts()
const {
return mut2node_ | std::views::keys; }
235 auto nodes()
const {
return mut2node_ | std::views::transform([](
const auto& p) {
return (
const Node*)p.second.get(); }); }
239 if (
auto i = mut2node_.find(mut); i != mut2node_.end())
return i->second.get();
246 auto begin()
const {
return mut2node_.cbegin(); }
247 auto end()
const {
return mut2node_.cend(); }
250 template<
bool bootstrapping = false>
256 void dot(std::ostream& os)
const;
257 void dot(
const char* file =
nullptr)
const;
258 void dot(std::string s)
const {
dot(s.c_str()); }
262 auto begin() {
return mut2node_.begin(); }
263 auto end() {
return mut2node_.end(); }
266 Node* make_node(Def*,
Node* inest =
nullptr);
267 void calc_sibl_deps(
Node*)
const;
268 void calc_SCCs(
Node*)
const;
269 void assign_postorder_numbers()
const;
272 void calc_sibl_deps()
const {
275 calc_sibl_deps(root_);
279 const Nest& calc_SCCs()
const {
288 absl::btree_map<Def*, std::unique_ptr<Node>, detail::NullSafeDefGIDLt> mut2node_;
291 mutable bool siblings_ =
false;
292 mutable bool sccs_ =
false;
std::string unique_name() const
name + "_" + Def::gid
bool has_free_vars_in(Vars) const
Same as vars.has_intersection(free_vars()).
const auto & sibl_deps() const
Def * mut() const
The mutable capsulated in this Node or nullptr, if it's a virtual root comprising several Nodes.
bool is_recursive() const
const Children & children() const
auto idom() const
Immediate Dominator for children in connected components.
const auto & topo() const
Topological sorting of all SCCs.
const Nest & nest() const
bool is_directly_recursive() const
bool is_mutually_recursive() const
absl::btree_set< const Node *, GIDLt< const Node * > > SCC
Strongly Connected Component.
uint32_t loop_depth() const
const Node * inest() const
Immediate nester/parent of this Node.
uint32_t gid() const
Stable id for deterministic hashing/ordering: the underlying mut's gid, or 0 for the (unique) virtual...
void dot(std::ostream &os) const
bool is_recursive() const
const Node * operator[](Def *mut) const
static const Node * lca(const Node *n, const Node *m)
Least common ancestor of n and m.
Nest & operator=(Nest)=delete
void dot(std::string s) const
const Node * root() const
Vars vars() const
All Vars occurring in this Nest.
Nest(const Nest &)=delete
bool contains(const Def *def) const
The World represents the whole program and manages creation of MimIR nodes (Defs).
fe::Patricia< const Var, DefKey >::Set Vars
size_t num() const
Number of children.
bool contains(Def *mut) const
is mut a child?
const Node * operator[](Def *mut) const
bool contains(const Node *n) const