@@ -1473,13 +1473,13 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
14731473
14741474 // Keep a record of all the exiting blocks.
14751475 SmallVector<const SCEVPredicate *, 4 > Predicates;
1476- for (BasicBlock *BB1 : ExitingBlocks) {
1476+ for (BasicBlock *BB : ExitingBlocks) {
14771477 const SCEV *EC =
1478- PSE.getSE ()->getPredicatedExitCount (TheLoop, BB1 , &Predicates);
1478+ PSE.getSE ()->getPredicatedExitCount (TheLoop, BB , &Predicates);
14791479 if (isa<SCEVCouldNotCompute>(EC)) {
1480- UncountableExitingBlocks.push_back (BB1 );
1480+ UncountableExitingBlocks.push_back (BB );
14811481
1482- SmallVector<BasicBlock *, 2 > Succs (successors (BB1 ));
1482+ SmallVector<BasicBlock *, 2 > Succs (successors (BB ));
14831483 if (Succs.size () != 2 ) {
14841484 reportVectorizationFailure (
14851485 " Early exiting block does not have exactly two successors" ,
@@ -1488,17 +1488,21 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
14881488 return false ;
14891489 }
14901490
1491- BasicBlock *BB2 ;
1491+ BasicBlock *ExitBlock ;
14921492 if (!TheLoop->contains (Succs[0 ]))
1493- BB2 = Succs[0 ];
1493+ ExitBlock = Succs[0 ];
14941494 else {
14951495 assert (!TheLoop->contains (Succs[1 ]));
1496- BB2 = Succs[1 ];
1496+ ExitBlock = Succs[1 ];
14971497 }
1498- UncountableExitBlocks.push_back (BB2 );
1498+ UncountableExitBlocks.push_back (ExitBlock );
14991499 } else
1500- CountableExitingBlocks.push_back (BB1 );
1500+ CountableExitingBlocks.push_back (BB );
15011501 }
1502+ // We can safely ignore the predicates here because when vectorizing the loop
1503+ // the PredicatatedScalarEvolution class will keep track of all predicates
1504+ // for each exiting block anyway. This happens when calling
1505+ // PSE.getSymbolicMaxBackedgeTakenCount() below.
15021506 Predicates.clear ();
15031507
15041508 // We only support one uncountable early exit.
@@ -1513,13 +1517,25 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
15131517 // The only supported early exit loops so far are ones where the early
15141518 // exiting block is a unique predecessor of the latch block.
15151519 BasicBlock *LatchPredBB = LatchBB->getUniquePredecessor ();
1516- if (LatchPredBB != getSpeculativeEarlyExitingBlock ()) {
1520+ if (LatchPredBB != getUncountableEarlyExitingBlock ()) {
15171521 reportVectorizationFailure (" Early exit is not the latch predecessor" ,
15181522 " Cannot vectorize early exit loop" ,
15191523 " EarlyExitNotLatchPredecessor" , ORE, TheLoop);
15201524 return false ;
15211525 }
15221526
1527+ // The latch block must have a countable exit.
1528+ if (isa<SCEVCouldNotCompute>(
1529+ PSE.getSE ()->getPredicatedExitCount (TheLoop, LatchBB, &Predicates))) {
1530+ reportVectorizationFailure (
1531+ " Cannot determine exact exit count for latch block" ,
1532+ " Cannot vectorize early exit loop" ,
1533+ " UnknownLatchExitCountEarlyExitLoop" , ORE, TheLoop);
1534+ return false ;
1535+ }
1536+ assert (llvm::is_contained (CountableExitingBlocks, LatchBB) &&
1537+ " Latch block not found in list of countable exits!" );
1538+
15231539 // Check to see if there are instructions that could potentially generate
15241540 // exceptions or have side-effects.
15251541 auto IsSafeOperation = [](Instruction *I) -> bool {
@@ -1555,18 +1571,8 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
15551571 }
15561572 }
15571573
1558- // The latch block must have a countable exit.
1559- if (isa<SCEVCouldNotCompute>(
1560- PSE.getSE ()->getPredicatedExitCount (TheLoop, LatchBB, &Predicates))) {
1561- reportVectorizationFailure (
1562- " Cannot determine exact exit count for latch block" ,
1563- " Cannot vectorize early exit loop" ,
1564- " UnknownLatchExitCountEarlyExitLoop" , ORE, TheLoop);
1565- return false ;
1566- }
1567-
15681574 // The vectoriser cannot handle loads that occur after the early exit block.
1569- assert (LatchBB->getUniquePredecessor () == getSpeculativeEarlyExitingBlock () &&
1575+ assert (LatchBB->getUniquePredecessor () == getUncountableEarlyExitingBlock () &&
15701576 " Expected latch predecessor to be the early exiting block" );
15711577
15721578 // TODO: Handle loops that may fault.
@@ -1580,16 +1586,15 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
15801586 return false ;
15811587 }
15821588
1583- LLVM_DEBUG (
1584- dbgs ()
1585- << " LV: Found an early exit. Retrying with speculative exit count.\n " );
1586- [[maybe_unused]] const SCEV *SpecExitCount =
1589+ [[maybe_unused]] const SCEV *SymbolicMaxBTC =
15871590 PSE.getSymbolicMaxBackedgeTakenCount ();
1588- assert (!isa<SCEVCouldNotCompute>(SpecExitCount) &&
1591+ // Since we have an exact exit count for the latch and the early exit
1592+ // dominates the latch, then this should guarantee a computed SCEV value.
1593+ assert (!isa<SCEVCouldNotCompute>(SymbolicMaxBTC) &&
15891594 " Failed to get symbolic expression for backedge taken count" );
1590-
1591- LLVM_DEBUG ( dbgs () << " LV: Found speculative backedge taken count: "
1592- << *SpecExitCount << ' \n ' );
1595+ LLVM_DEBUG ( dbgs () << " LV: Found an early exit loop with symbolic max "
1596+ " backedge taken count: "
1597+ << *SymbolicMaxBTC << ' \n ' );
15931598 return true ;
15941599}
15951600
@@ -1653,15 +1658,15 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
16531658 return false ;
16541659 }
16551660
1656- HasSpeculativeEarlyExit = false ;
1661+ HasUncountableEarlyExit = false ;
16571662 if (isa<SCEVCouldNotCompute>(PSE.getBackedgeTakenCount ())) {
16581663 if (!isVectorizableEarlyExitLoop ()) {
16591664 if (DoExtraAnalysis)
16601665 Result = false ;
16611666 else
16621667 return false ;
16631668 } else
1664- HasSpeculativeEarlyExit = true ;
1669+ HasUncountableEarlyExit = true ;
16651670 }
16661671
16671672 // Go over each instruction and look at memory deps.
0 commit comments