@@ -1467,13 +1467,13 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1467
1467
1468
1468
// Keep a record of all the exiting blocks.
1469
1469
SmallVector<const SCEVPredicate *, 4 > Predicates;
1470
- for (BasicBlock *BB1 : ExitingBlocks) {
1470
+ for (BasicBlock *BB : ExitingBlocks) {
1471
1471
const SCEV *EC =
1472
- PSE.getSE ()->getPredicatedExitCount (TheLoop, BB1 , &Predicates);
1472
+ PSE.getSE ()->getPredicatedExitCount (TheLoop, BB , &Predicates);
1473
1473
if (isa<SCEVCouldNotCompute>(EC)) {
1474
- UncountableExitingBlocks.push_back (BB1 );
1474
+ UncountableExitingBlocks.push_back (BB );
1475
1475
1476
- SmallVector<BasicBlock *, 2 > Succs (successors (BB1 ));
1476
+ SmallVector<BasicBlock *, 2 > Succs (successors (BB ));
1477
1477
if (Succs.size () != 2 ) {
1478
1478
reportVectorizationFailure (
1479
1479
" Early exiting block does not have exactly two successors" ,
@@ -1482,17 +1482,21 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1482
1482
return false ;
1483
1483
}
1484
1484
1485
- BasicBlock *BB2 ;
1485
+ BasicBlock *ExitBlock ;
1486
1486
if (!TheLoop->contains (Succs[0 ]))
1487
- BB2 = Succs[0 ];
1487
+ ExitBlock = Succs[0 ];
1488
1488
else {
1489
1489
assert (!TheLoop->contains (Succs[1 ]));
1490
- BB2 = Succs[1 ];
1490
+ ExitBlock = Succs[1 ];
1491
1491
}
1492
- UncountableExitBlocks.push_back (BB2 );
1492
+ UncountableExitBlocks.push_back (ExitBlock );
1493
1493
} else
1494
- CountableExitingBlocks.push_back (BB1 );
1494
+ CountableExitingBlocks.push_back (BB );
1495
1495
}
1496
+ // We can safely ignore the predicates here because when vectorizing the loop
1497
+ // the PredicatatedScalarEvolution class will keep track of all predicates
1498
+ // for each exiting block anyway. This happens when calling
1499
+ // PSE.getSymbolicMaxBackedgeTakenCount() below.
1496
1500
Predicates.clear ();
1497
1501
1498
1502
// We only support one uncountable early exit.
@@ -1507,13 +1511,25 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1507
1511
// The only supported early exit loops so far are ones where the early
1508
1512
// exiting block is a unique predecessor of the latch block.
1509
1513
BasicBlock *LatchPredBB = LatchBB->getUniquePredecessor ();
1510
- if (LatchPredBB != getSpeculativeEarlyExitingBlock ()) {
1514
+ if (LatchPredBB != getUncountableEarlyExitingBlock ()) {
1511
1515
reportVectorizationFailure (" Early exit is not the latch predecessor" ,
1512
1516
" Cannot vectorize early exit loop" ,
1513
1517
" EarlyExitNotLatchPredecessor" , ORE, TheLoop);
1514
1518
return false ;
1515
1519
}
1516
1520
1521
+ // The latch block must have a countable exit.
1522
+ if (isa<SCEVCouldNotCompute>(
1523
+ PSE.getSE ()->getPredicatedExitCount (TheLoop, LatchBB, &Predicates))) {
1524
+ reportVectorizationFailure (
1525
+ " Cannot determine exact exit count for latch block" ,
1526
+ " Cannot vectorize early exit loop" ,
1527
+ " UnknownLatchExitCountEarlyExitLoop" , ORE, TheLoop);
1528
+ return false ;
1529
+ }
1530
+ assert (llvm::is_contained (CountableExitingBlocks, LatchBB) &&
1531
+ " Latch block not found in list of countable exits!" );
1532
+
1517
1533
// Check to see if there are instructions that could potentially generate
1518
1534
// exceptions or have side-effects.
1519
1535
auto IsSafeOperation = [](Instruction *I) -> bool {
@@ -1549,18 +1565,8 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1549
1565
}
1550
1566
}
1551
1567
1552
- // The latch block must have a countable exit.
1553
- if (isa<SCEVCouldNotCompute>(
1554
- PSE.getSE ()->getPredicatedExitCount (TheLoop, LatchBB, &Predicates))) {
1555
- reportVectorizationFailure (
1556
- " Cannot determine exact exit count for latch block" ,
1557
- " Cannot vectorize early exit loop" ,
1558
- " UnknownLatchExitCountEarlyExitLoop" , ORE, TheLoop);
1559
- return false ;
1560
- }
1561
-
1562
1568
// The vectoriser cannot handle loads that occur after the early exit block.
1563
- assert (LatchBB->getUniquePredecessor () == getSpeculativeEarlyExitingBlock () &&
1569
+ assert (LatchBB->getUniquePredecessor () == getUncountableEarlyExitingBlock () &&
1564
1570
" Expected latch predecessor to be the early exiting block" );
1565
1571
1566
1572
// TODO: Handle loops that may fault.
@@ -1572,16 +1578,15 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1572
1578
return false ;
1573
1579
}
1574
1580
1575
- LLVM_DEBUG (
1576
- dbgs ()
1577
- << " LV: Found an early exit. Retrying with speculative exit count.\n " );
1578
- [[maybe_unused]] const SCEV *SpecExitCount =
1581
+ [[maybe_unused]] const SCEV *SymbolicMaxBTC =
1579
1582
PSE.getSymbolicMaxBackedgeTakenCount ();
1580
- assert (!isa<SCEVCouldNotCompute>(SpecExitCount) &&
1583
+ // Since we have an exact exit count for the latch and the early exit
1584
+ // dominates the latch, then this should guarantee a computed SCEV value.
1585
+ assert (!isa<SCEVCouldNotCompute>(SymbolicMaxBTC) &&
1581
1586
" Failed to get symbolic expression for backedge taken count" );
1582
-
1583
- LLVM_DEBUG ( dbgs () << " LV: Found speculative backedge taken count: "
1584
- << *SpecExitCount << ' \n ' );
1587
+ LLVM_DEBUG ( dbgs () << " LV: Found an early exit loop with symbolic max "
1588
+ " backedge taken count: "
1589
+ << *SymbolicMaxBTC << ' \n ' );
1585
1590
return true ;
1586
1591
}
1587
1592
@@ -1645,15 +1650,15 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
1645
1650
return false ;
1646
1651
}
1647
1652
1648
- HasSpeculativeEarlyExit = false ;
1653
+ HasUncountableEarlyExit = false ;
1649
1654
if (isa<SCEVCouldNotCompute>(PSE.getBackedgeTakenCount ())) {
1650
1655
if (!isVectorizableEarlyExitLoop ()) {
1651
1656
if (DoExtraAnalysis)
1652
1657
Result = false ;
1653
1658
else
1654
1659
return false ;
1655
1660
} else
1656
- HasSpeculativeEarlyExit = true ;
1661
+ HasUncountableEarlyExit = true ;
1657
1662
}
1658
1663
1659
1664
// Go over each instruction and look at memory deps.
0 commit comments