Skip to content

Commit 638c085

Browse files
committed
[Dominators] Include infinite loops in PostDominatorTree
Summary: This patch teaches PostDominatorTree about infinite loops. It is built on top of D29705 by @dberlin which includes a very detailed motivation for this change. What's new is that the patch also teaches the incremental updater how to deal with reverse-unreachable regions and how to properly maintain and verify tree roots. Before that, the incremental algorithm sometimes ended up preserving reverse-unreachable regions after updates that wouldn't appear in the tree if it was constructed from scratch on the same CFG. This patch makes the following assumptions: - A sequence of updates should produce the same tree as a recalculating it. - Any sequence of the same updates should lead to the same tree. - Siblings and roots are unordered. The last two properties are essential to efficiently perform batch updates in the future. When it comes to the first one, we can decide later that the consistency between freshly built tree and an updated one doesn't matter match, as there are many correct ways to pick roots in infinite loops, and to relax this assumption. That should enable us to recalculate postdominators less frequently. This patch is pretty conservative when it comes to incremental updates on reverse-unreachable regions and ends up recalculating the whole tree in many cases. It should be possible to improve the performance in many cases, if we decide that it's important enough. That being said, my experiments showed that reverse-unreachable are very rare in the IR emitted by clang when bootstrapping clang. Here are the statistics I collected by analyzing IR between passes and after each removePredecessor call: ``` # functions: 52283 # samples: 337609 # reverse unreachable BBs: 216022 # BBs: 247840796 Percent reverse-unreachable: 0.08716159869015269 % Max(PercRevUnreachable) in a function: 87.58620689655172 % # > 25 % samples: 471 ( 0.1395104988314885 % samples ) ... in 145 ( 0.27733680163724345 % functions ) ``` Most of the reverse-unreachable regions come from invalid IR where it wouldn't be possible to construct a PostDomTree anyway. I would like to commit this patch in the next week in order to be able to complete the work that depends on it before the end of my internship, so please don't wait long to voice your concerns :). Reviewers: dberlin, sanjoy, grosser, brzycki, davide, chandlerc, hfinkel Reviewed By: dberlin Subscribers: nhaehnle, javed.absar, kparzysz, uabelho, jlebar, hiraditya, llvm-commits, dberlin, david2050 Differential Revision: https://reviews.llvm.org/D35851 llvm-svn: 310940
1 parent 590a974 commit 638c085

23 files changed

+731
-226
lines changed

llvm/include/llvm/Support/GenericDomTree.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,15 @@ class DominatorTreeBase {
417417
}
418418

419419
/// findNearestCommonDominator - Find nearest common dominator basic block
420-
/// for basic block A and B. If there is no such block then return NULL.
420+
/// for basic block A and B. If there is no such block then return nullptr.
421421
NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) const {
422+
assert(A && B && "Pointers are not valid");
422423
assert(A->getParent() == B->getParent() &&
423424
"Two blocks are not in same function");
424425

425426
// If either A or B is a entry block then it is nearest common dominator
426427
// (for forward-dominators).
427-
if (!this->isPostDominator()) {
428+
if (!isPostDominator()) {
428429
NodeT &Entry = A->getParent()->front();
429430
if (A == &Entry || B == &Entry)
430431
return &Entry;
@@ -580,6 +581,15 @@ class DominatorTreeBase {
580581
}
581582

582583
DomTreeNodes.erase(BB);
584+
585+
if (!IsPostDom) return;
586+
587+
// Remember to update PostDominatorTree roots.
588+
auto RIt = llvm::find(Roots, BB);
589+
if (RIt != Roots.end()) {
590+
std::swap(*RIt, Roots.back());
591+
Roots.pop_back();
592+
}
583593
}
584594

585595
/// splitBlock - BB is split and now it has one successor. Update dominator
@@ -595,7 +605,7 @@ class DominatorTreeBase {
595605
///
596606
void print(raw_ostream &O) const {
597607
O << "=============================--------------------------------\n";
598-
if (this->isPostDominator())
608+
if (IsPostDominator)
599609
O << "Inorder PostDominator Tree: ";
600610
else
601611
O << "Inorder Dominator Tree: ";
@@ -605,6 +615,14 @@ class DominatorTreeBase {
605615

606616
// The postdom tree can have a null root if there are no returns.
607617
if (getRootNode()) PrintDomTree<NodeT>(getRootNode(), O, 1);
618+
if (IsPostDominator) {
619+
O << "Roots: ";
620+
for (const NodePtr Block : Roots) {
621+
Block->printAsOperand(O, false);
622+
O << " ";
623+
}
624+
O << "\n";
625+
}
608626
}
609627

610628
public:

0 commit comments

Comments
 (0)