8void merge(
VarSet& vars,
VarSet&& other) { vars.insert(other.begin(), other.end()); }
12 for (
const auto& [_, mut] :
world().externals()) run(mut);
13 adjust_depth(root_.get(), 0);
16VarSet DepTree::run(Def* mut) {
17 auto [i, inserted] = mut2node_.emplace(mut, std::unique_ptr<DepNode>());
19 if (
auto i = def2vars_.find(mut); i != def2vars_.end())
return i->second;
23 i->second = std::make_unique<DepNode>(mut, stack_.size() + 1);
24 auto node = i->second.get();
25 stack_.push_back(node);
27 auto result = run(mut, mut);
28 auto parent = root_.get();
29 for (
auto var : result) {
30 auto n = mut2node_[var->mut()].get();
32 world().ELOG(
"var {} used before mut {} discovered, old var still around?", var, var->mut());
33 world().ELOG(
"var {} : {} [{}]", var, var->type(), var->node_name());
34 world().ELOG(
"var mut {} : {}", var->mut(), var->mut()->type());
36 assert(n &&
"Old var still around?");
37 parent = n->depth() > parent->depth() ? n : parent;
39 if (mut->is_external() && parent != root_.get()) {
40 world().WLOG(
"external {} would be hidden inside parent {}.", mut, parent->mut());
41 node->set_parent(root_.get());
43 node->set_parent(parent);
49VarSet DepTree::run(Def* curr_mut,
const Def* def) {
50 if (def->dep_const())
return {};
51 if (
auto i = def2vars_.find(def); i != def2vars_.end())
return i->second;
52 if (
auto mut = def->isa_mut(); mut && curr_mut != mut)
return run(mut);
55 if (
auto var = def->isa<
Var>()) {
58 for (
auto op : def->extended_ops())
merge(result, run(curr_mut, op));
60 if (
auto v = curr_mut->var()) {
61 if (
auto var = v->isa<
Var>(); var && curr_mut == def) result.erase(var);
65 return def2vars_[def] = result;
68void DepTree::adjust_depth(DepNode* node,
size_t depth) {
71 for (
const auto& child : node->children()) adjust_depth(child, depth + 1);
78 if (n->depth() < m->depth())
return false;
81 for (
size_t e = m->depth(); i->depth() != e; i = i->parent()) {}
const DepNode * mut2node(Def *mut) const
bool depends(Def *a, Def *b) const
Does a depend on b?
GIDSet< const Var * > VarSet