@@ -1788,28 +1788,37 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
17881788 return false ;
17891789 };
17901790
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+
17911798 // If we have any exits which can't be predicated themselves, than we can't
17921799 // predicate any exit which isn't guaranteed to execute before it. Consider
17931800 // two exits (a) and (b) which would both exit on the same iteration. If we
17941801 // can predicate (b), but not (a), and (a) preceeds (b) along some path, then
17951802 // we could convert a loop from exiting through (a) to one exiting through
17961803 // (b). Note that this problem exists only for exits with the same exit
17971804 // 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))
18121813 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" );
18131822
18141823 // Given our sorted total order, we know that exit[j] must be evaluated
18151824 // after all exit[i] such j > i.
@@ -1822,14 +1831,6 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
18221831 if (ExitingBlocks.empty ())
18231832 return false ;
18241833
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-
18331834 // At this point, ExitingBlocks consists of only those blocks which are
18341835 // predicatable. Given that, we know we have at least one exit we can
18351836 // predicate if the loop is doesn't have side effects and doesn't have any
0 commit comments