@@ -6698,6 +6698,65 @@ ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
6698
6698
return true ;
6699
6699
}
6700
6700
6701
+ struct ClearWalkingBEDominatingCondsOnExit {
6702
+ ScalarEvolution &SE;
6703
+
6704
+ explicit ClearWalkingBEDominatingCondsOnExit (ScalarEvolution &SE)
6705
+ : SE(SE){};
6706
+
6707
+ ~ClearWalkingBEDominatingCondsOnExit () {
6708
+ SE.WalkingBEDominatingConds = false ;
6709
+ }
6710
+ };
6711
+
6712
+ // We don't want more than one activation of the following loop on the stack
6713
+ // -- that can lead to O(n!) time complexity.
6714
+ if (WalkingBEDominatingConds)
6715
+ return false ;
6716
+
6717
+ WalkingBEDominatingConds = true ;
6718
+ ClearWalkingBEDominatingCondsOnExit ClearOnExit (*this );
6719
+
6720
+ // If the loop is not reachable from the entry block, we risk running into an
6721
+ // infinite loop as we walk up into the dom tree. These loops do not matter
6722
+ // anyway, so we just return a conservative answer when we see them.
6723
+ if (!DT->isReachableFromEntry (L->getHeader ()))
6724
+ return false ;
6725
+
6726
+ for (DomTreeNode *DTN = (*DT)[Latch], *HeaderDTN = (*DT)[L->getHeader ()];
6727
+ DTN != HeaderDTN;
6728
+ DTN = DTN->getIDom ()) {
6729
+
6730
+ assert (DTN && " should reach the loop header before reaching the root!" );
6731
+
6732
+ BasicBlock *BB = DTN->getBlock ();
6733
+ BasicBlock *PBB = BB->getSinglePredecessor ();
6734
+ if (!PBB)
6735
+ continue ;
6736
+
6737
+ BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator ());
6738
+ if (!ContinuePredicate || !ContinuePredicate->isConditional ())
6739
+ continue ;
6740
+
6741
+ Value *Condition = ContinuePredicate->getCondition ();
6742
+
6743
+ // If we have an edge `E` within the loop body that dominates the only
6744
+ // latch, the condition guarding `E` also guards the backedge. This
6745
+ // reasoning works only for loops with a single latch.
6746
+
6747
+ BasicBlockEdge DominatingEdge (PBB, BB);
6748
+ if (DominatingEdge.isSingleEdge ()) {
6749
+ // We're constructively (and conservatively) enumerating edges within the
6750
+ // loop body that dominate the latch. The dominator tree better agree
6751
+ // with us on this:
6752
+ assert (DT->dominates (DominatingEdge, Latch) && " should be!" );
6753
+
6754
+ if (isImpliedCond (Pred, LHS, RHS, Condition,
6755
+ BB != ContinuePredicate->getSuccessor (0 )))
6756
+ return true ;
6757
+ }
6758
+ }
6759
+
6701
6760
return false ;
6702
6761
}
6703
6762
@@ -7968,8 +8027,8 @@ ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
7968
8027
// ===----------------------------------------------------------------------===//
7969
8028
7970
8029
ScalarEvolution::ScalarEvolution ()
7971
- : FunctionPass(ID), ValuesAtScopes( 64 ), LoopDispositions (64 ),
7972
- BlockDispositions(64 ), FirstUnknown(nullptr ) {
8030
+ : FunctionPass(ID), WalkingBEDominatingConds( false ), ValuesAtScopes (64 ),
8031
+ LoopDispositions( 64 ), BlockDispositions(64 ), FirstUnknown(nullptr ) {
7973
8032
initializeScalarEvolutionPass (*PassRegistry::getPassRegistry ());
7974
8033
}
7975
8034
@@ -8000,6 +8059,7 @@ void ScalarEvolution::releaseMemory() {
8000
8059
}
8001
8060
8002
8061
assert (PendingLoopPredicates.empty () && " isImpliedCond garbage" );
8062
+ assert (!WalkingBEDominatingConds && " isLoopBackedgeGuardedByCond garbage!" );
8003
8063
8004
8064
BackedgeTakenCounts.clear ();
8005
8065
ConstantEvolutionLoopExitValue.clear ();
0 commit comments