@@ -1631,12 +1631,11 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
16311631
16321632 // Keep a record of all the exiting blocks.
16331633 SmallVector<const SCEVPredicate *, 4 > Predicates;
1634+ std::optional<std::pair<BasicBlock *, BasicBlock *>> SingleUncountableEdge;
16341635 for (BasicBlock *BB : ExitingBlocks) {
16351636 const SCEV *EC =
16361637 PSE.getSE ()->getPredicatedExitCount (TheLoop, BB, &Predicates);
16371638 if (isa<SCEVCouldNotCompute>(EC)) {
1638- UncountableExitingBlocks.push_back (BB);
1639-
16401639 SmallVector<BasicBlock *, 2 > Succs (successors (BB));
16411640 if (Succs.size () != 2 ) {
16421641 reportVectorizationFailure (
@@ -1653,7 +1652,16 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
16531652 assert (!TheLoop->contains (Succs[1 ]));
16541653 ExitBlock = Succs[1 ];
16551654 }
1656- UncountableExitBlocks.push_back (ExitBlock);
1655+
1656+ if (SingleUncountableEdge) {
1657+ reportVectorizationFailure (
1658+ " Loop has too many uncountable exits" ,
1659+ " Cannot vectorize early exit loop with more than one early exit" ,
1660+ " TooManyUncountableEarlyExits" , ORE, TheLoop);
1661+ return false ;
1662+ }
1663+
1664+ SingleUncountableEdge = {BB, ExitBlock};
16571665 } else
16581666 CountableExitingBlocks.push_back (BB);
16591667 }
@@ -1663,19 +1671,15 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
16631671 // PSE.getSymbolicMaxBackedgeTakenCount() below.
16641672 Predicates.clear ();
16651673
1666- // We only support one uncountable early exit.
1667- if (getUncountableExitingBlocks ().size () != 1 ) {
1668- reportVectorizationFailure (
1669- " Loop has too many uncountable exits" ,
1670- " Cannot vectorize early exit loop with more than one early exit" ,
1671- " TooManyUncountableEarlyExits" , ORE, TheLoop);
1674+ if (!SingleUncountableEdge) {
1675+ LLVM_DEBUG (dbgs () << " LV: Cound not find any uncountable exits" );
16721676 return false ;
16731677 }
16741678
16751679 // The only supported early exit loops so far are ones where the early
16761680 // exiting block is a unique predecessor of the latch block.
16771681 BasicBlock *LatchPredBB = LatchBB->getUniquePredecessor ();
1678- if (LatchPredBB != getUncountableEarlyExitingBlock () ) {
1682+ if (LatchPredBB != SingleUncountableEdge-> first ) {
16791683 reportVectorizationFailure (" Early exit is not the latch predecessor" ,
16801684 " Cannot vectorize early exit loop" ,
16811685 " EarlyExitNotLatchPredecessor" , ORE, TheLoop);
@@ -1728,7 +1732,7 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
17281732 }
17291733
17301734 // The vectoriser cannot handle loads that occur after the early exit block.
1731- assert (LatchBB->getUniquePredecessor () == getUncountableEarlyExitingBlock () &&
1735+ assert (LatchBB->getUniquePredecessor () == SingleUncountableEdge-> first &&
17321736 " Expected latch predecessor to be the early exiting block" );
17331737
17341738 // TODO: Handle loops that may fault.
@@ -1751,6 +1755,7 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
17511755 LLVM_DEBUG (dbgs () << " LV: Found an early exit loop with symbolic max "
17521756 " backedge taken count: "
17531757 << *SymbolicMaxBTC << ' \n ' );
1758+ UncountableEdge = SingleUncountableEdge;
17541759 return true ;
17551760}
17561761
@@ -1812,7 +1817,6 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
18121817 return false ;
18131818 }
18141819
1815- HasUncountableEarlyExit = false ;
18161820 if (isa<SCEVCouldNotCompute>(PSE.getBackedgeTakenCount ())) {
18171821 if (TheLoop->getExitingBlock ()) {
18181822 reportVectorizationFailure (" Cannot vectorize uncountable loop" ,
@@ -1822,10 +1826,8 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
18221826 else
18231827 return false ;
18241828 } else {
1825- HasUncountableEarlyExit = true ;
18261829 if (!isVectorizableEarlyExitLoop ()) {
1827- UncountableExitingBlocks.clear ();
1828- HasUncountableEarlyExit = false ;
1830+ UncountableEdge = std::nullopt ;
18291831 if (DoExtraAnalysis)
18301832 Result = false ;
18311833 else
0 commit comments