@@ -1473,13 +1473,13 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1473
1473
1474
1474
// Keep a record of all the exiting blocks.
1475
1475
SmallVector<const SCEVPredicate *, 4 > Predicates;
1476
- for (BasicBlock *BB1 : ExitingBlocks) {
1476
+ for (BasicBlock *BB : ExitingBlocks) {
1477
1477
const SCEV *EC =
1478
- PSE.getSE ()->getPredicatedExitCount (TheLoop, BB1 , &Predicates);
1478
+ PSE.getSE ()->getPredicatedExitCount (TheLoop, BB , &Predicates);
1479
1479
if (isa<SCEVCouldNotCompute>(EC)) {
1480
- UncountableExitingBlocks.push_back (BB1 );
1480
+ UncountableExitingBlocks.push_back (BB );
1481
1481
1482
- SmallVector<BasicBlock *, 2 > Succs (successors (BB1 ));
1482
+ SmallVector<BasicBlock *, 2 > Succs (successors (BB ));
1483
1483
if (Succs.size () != 2 ) {
1484
1484
reportVectorizationFailure (
1485
1485
" Early exiting block does not have exactly two successors" ,
@@ -1488,17 +1488,21 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1488
1488
return false ;
1489
1489
}
1490
1490
1491
- BasicBlock *BB2 ;
1491
+ BasicBlock *ExitBlock ;
1492
1492
if (!TheLoop->contains (Succs[0 ]))
1493
- BB2 = Succs[0 ];
1493
+ ExitBlock = Succs[0 ];
1494
1494
else {
1495
1495
assert (!TheLoop->contains (Succs[1 ]));
1496
- BB2 = Succs[1 ];
1496
+ ExitBlock = Succs[1 ];
1497
1497
}
1498
- UncountableExitBlocks.push_back (BB2 );
1498
+ UncountableExitBlocks.push_back (ExitBlock );
1499
1499
} else
1500
- CountableExitingBlocks.push_back (BB1 );
1500
+ CountableExitingBlocks.push_back (BB );
1501
1501
}
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.
1502
1506
Predicates.clear ();
1503
1507
1504
1508
// We only support one uncountable early exit.
@@ -1513,13 +1517,25 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1513
1517
// The only supported early exit loops so far are ones where the early
1514
1518
// exiting block is a unique predecessor of the latch block.
1515
1519
BasicBlock *LatchPredBB = LatchBB->getUniquePredecessor ();
1516
- if (LatchPredBB != getSpeculativeEarlyExitingBlock ()) {
1520
+ if (LatchPredBB != getUncountableEarlyExitingBlock ()) {
1517
1521
reportVectorizationFailure (" Early exit is not the latch predecessor" ,
1518
1522
" Cannot vectorize early exit loop" ,
1519
1523
" EarlyExitNotLatchPredecessor" , ORE, TheLoop);
1520
1524
return false ;
1521
1525
}
1522
1526
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
+
1523
1539
// Check to see if there are instructions that could potentially generate
1524
1540
// exceptions or have side-effects.
1525
1541
auto IsSafeOperation = [](Instruction *I) -> bool {
@@ -1555,18 +1571,8 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1555
1571
}
1556
1572
}
1557
1573
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
-
1568
1574
// The vectoriser cannot handle loads that occur after the early exit block.
1569
- assert (LatchBB->getUniquePredecessor () == getSpeculativeEarlyExitingBlock () &&
1575
+ assert (LatchBB->getUniquePredecessor () == getUncountableEarlyExitingBlock () &&
1570
1576
" Expected latch predecessor to be the early exiting block" );
1571
1577
1572
1578
// TODO: Handle loops that may fault.
@@ -1580,16 +1586,15 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1580
1586
return false ;
1581
1587
}
1582
1588
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 =
1587
1590
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) &&
1589
1594
" 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 ' );
1593
1598
return true ;
1594
1599
}
1595
1600
@@ -1653,15 +1658,15 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
1653
1658
return false ;
1654
1659
}
1655
1660
1656
- HasSpeculativeEarlyExit = false ;
1661
+ HasUncountableEarlyExit = false ;
1657
1662
if (isa<SCEVCouldNotCompute>(PSE.getBackedgeTakenCount ())) {
1658
1663
if (!isVectorizableEarlyExitLoop ()) {
1659
1664
if (DoExtraAnalysis)
1660
1665
Result = false ;
1661
1666
else
1662
1667
return false ;
1663
1668
} else
1664
- HasSpeculativeEarlyExit = true ;
1669
+ HasUncountableEarlyExit = true ;
1665
1670
}
1666
1671
1667
1672
// Go over each instruction and look at memory deps.
0 commit comments