@@ -1788,28 +1788,37 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
1788
1788
return false ;
1789
1789
};
1790
1790
1791
+ // Make sure all exits dominate the latch. This means there is a linear chain
1792
+ // of exits. We check this before sorting so we have a total order.
1793
+ BasicBlock *Latch = L->getLoopLatch ();
1794
+ for (BasicBlock *ExitingBB : ExitingBlocks)
1795
+ if (!DT->dominates (ExitingBB, Latch))
1796
+ return false ;
1797
+
1791
1798
// If we have any exits which can't be predicated themselves, than we can't
1792
1799
// predicate any exit which isn't guaranteed to execute before it. Consider
1793
1800
// two exits (a) and (b) which would both exit on the same iteration. If we
1794
1801
// can predicate (b), but not (a), and (a) preceeds (b) along some path, then
1795
1802
// we could convert a loop from exiting through (a) to one exiting through
1796
1803
// (b). Note that this problem exists only for exits with the same exit
1797
1804
// count, and we could be more aggressive when exit counts are known inequal.
1798
- llvm::sort (ExitingBlocks,
1799
- [&](BasicBlock *A, BasicBlock *B) {
1800
- // std::sort sorts in ascending order, so we want the inverse of
1801
- // the normal dominance relation, plus a tie breaker for blocks
1802
- // unordered by dominance.
1803
- if (DT->properlyDominates (A, B)) return true ;
1804
- if (DT->properlyDominates (B, A)) return false ;
1805
- return A->getName () < B->getName ();
1806
- });
1807
- // Check to see if our exit blocks are a total order (i.e. a linear chain of
1808
- // exits before the backedge). If they aren't, reasoning about reachability
1809
- // is complicated and we choose not to for now.
1810
- for (unsigned i = 1 ; i < ExitingBlocks.size (); i++)
1811
- if (!DT->dominates (ExitingBlocks[i-1 ], ExitingBlocks[i]))
1805
+ llvm::sort (ExitingBlocks, [&](BasicBlock *A, BasicBlock *B) {
1806
+ // llvm::sort sorts in ascending order, so we want the inverse of
1807
+ // the normal dominance relation.
1808
+ if (A == B)
1809
+ return false ;
1810
+ if (DT->properlyDominates (A, B))
1811
+ return true ;
1812
+ if (DT->properlyDominates (B, A))
1812
1813
return false ;
1814
+ llvm_unreachable (" Should have total dominance order" );
1815
+ });
1816
+
1817
+ // Make sure our exit blocks are really a total order (i.e. a linear chain of
1818
+ // exits before the backedge).
1819
+ for (unsigned i = 1 ; i < ExitingBlocks.size (); i++)
1820
+ assert (DT->dominates (ExitingBlocks[i - 1 ], ExitingBlocks[i]) &&
1821
+ " Not sorted by dominance" );
1813
1822
1814
1823
// Given our sorted total order, we know that exit[j] must be evaluated
1815
1824
// after all exit[i] such j > i.
@@ -1822,14 +1831,6 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
1822
1831
if (ExitingBlocks.empty ())
1823
1832
return false ;
1824
1833
1825
- // We rely on not being able to reach an exiting block on a later iteration
1826
- // then it's statically compute exit count. The implementaton of
1827
- // getExitCount currently has this invariant, but assert it here so that
1828
- // breakage is obvious if this ever changes..
1829
- assert (llvm::all_of (ExitingBlocks, [&](BasicBlock *ExitingBB) {
1830
- return DT->dominates (ExitingBB, L->getLoopLatch ());
1831
- }));
1832
-
1833
1834
// At this point, ExitingBlocks consists of only those blocks which are
1834
1835
// predicatable. Given that, we know we have at least one exit we can
1835
1836
// predicate if the loop is doesn't have side effects and doesn't have any
0 commit comments