@@ -1051,7 +1051,7 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
1051
1051
return true ;
1052
1052
}
1053
1053
1054
- bool LoopVectorizationLegality::canVectorizeMemory (bool IsEarlyExitLoop ) {
1054
+ bool LoopVectorizationLegality::canVectorizeMemory () {
1055
1055
LAI = &LAIs.getInfo (*TheLoop);
1056
1056
const OptimizationRemarkAnalysis *LAR = LAI->getReport ();
1057
1057
if (LAR) {
@@ -1073,51 +1073,6 @@ bool LoopVectorizationLegality::canVectorizeMemory(bool IsEarlyExitLoop) {
1073
1073
return false ;
1074
1074
}
1075
1075
1076
- // For loops with uncountable early exiting blocks that are not the latch
1077
- // it's necessary to perform extra checks, since the vectoriser is currently
1078
- // only capable of handling simple search loops.
1079
- if (IsEarlyExitLoop) {
1080
- // We don't support calls or any memory accesses that write to memory.
1081
- if (LAI->getNumStores ()) {
1082
- reportVectorizationFailure (
1083
- " Writes to memory unsupported in early exit loops" ,
1084
- " Cannot vectorize early exit loop with writes to memory" ,
1085
- " WritesInEarlyExitLoop" , ORE, TheLoop);
1086
- return false ;
1087
- }
1088
-
1089
- // The vectoriser cannot handle loads that occur after the early exit block.
1090
- BasicBlock *LatchBB = TheLoop->getLoopLatch ();
1091
- assert (LatchBB->getUniquePredecessor () ==
1092
- getUncountableExitingBlocks ()[0 ] &&
1093
- " Expected latch predecessor to be the early exiting block" );
1094
-
1095
- for (Instruction &I : *LatchBB) {
1096
- if (I.mayReadFromMemory ()) {
1097
- reportVectorizationFailure (
1098
- " Loads not permitted after early exit" ,
1099
- " Cannot vectorize early exit loop with loads after early exit" ,
1100
- " LoadsAfterEarlyExit" , ORE, TheLoop);
1101
- return false ;
1102
- }
1103
- // Any other problematic instructions should have been caught earlier.
1104
- assert (!I.mayWriteToMemory () && !I.mayThrow () &&
1105
- !I.mayHaveSideEffects () &&
1106
- " Unexpected instructions in latch block of early exit loop" );
1107
- }
1108
-
1109
- // The vectoriser does not yet handle loops that may fault, but this will
1110
- // be improved in a follow-on patch.
1111
- // TODO: Handle loops that may fault.
1112
- if (!isDereferenceableReadOnlyLoop (TheLoop, PSE.getSE (), DT, AC)) {
1113
- reportVectorizationFailure (
1114
- " Loop may fault" ,
1115
- " Cannot vectorize potentially faulting early exit loop" ,
1116
- " PotentiallyFaultingEarlyExitLoop" , ORE, TheLoop);
1117
- return false ;
1118
- }
1119
- }
1120
-
1121
1076
// We can vectorize stores to invariant address when final reduction value is
1122
1077
// guaranteed to be stored at the end of the loop. Also, if decision to
1123
1078
// vectorize loop is made, runtime checks are added so as to make sure that
@@ -1491,7 +1446,6 @@ bool LoopVectorizationLegality::canVectorizeLoopNestCFG(
1491
1446
}
1492
1447
1493
1448
bool LoopVectorizationLegality::isVectorizableEarlyExitLoop () {
1494
- // At least one of the exiting blocks must be the latch.
1495
1449
BasicBlock *LatchBB = TheLoop->getLoopLatch ();
1496
1450
if (!LatchBB) {
1497
1451
reportVectorizationFailure (" Loop does not have a latch" ,
@@ -1553,33 +1507,38 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1553
1507
// The only supported early exit loops so far are ones where the early
1554
1508
// exiting block is a unique predecessor of the latch block.
1555
1509
BasicBlock *LatchPredBB = LatchBB->getUniquePredecessor ();
1556
- if (! LatchPredBB || LatchPredBB != getUncountableExitingBlocks ()[ 0 ] ) {
1510
+ if (LatchPredBB != getSpeculativeEarlyExitingBlock () ) {
1557
1511
reportVectorizationFailure (" Early exit is not the latch predecessor" ,
1558
1512
" Cannot vectorize early exit loop" ,
1559
1513
" EarlyExitNotLatchPredecessor" , ORE, TheLoop);
1560
1514
return false ;
1561
1515
}
1562
1516
1563
- // Check all instructions in the loop to see if they could potentially
1564
- // generate exceptions or have side-effects.
1517
+ // Check to see if there are instructions that could potentially generate
1518
+ // exceptions or have side-effects.
1565
1519
auto IsSafeOperation = [](Instruction *I) -> bool {
1566
- // Is this a divide?
1567
1520
switch (I->getOpcode ()) {
1568
1521
case Instruction::Load:
1569
1522
case Instruction::Store:
1570
1523
case Instruction::PHI:
1571
1524
case Instruction::Br:
1572
- // These are checked separately. For example, canVectorizeMemory will
1573
- // analyze the loads and stores in the loop.
1525
+ // These are checked separately.
1574
1526
return true ;
1575
1527
default :
1576
1528
return isSafeToSpeculativelyExecute (I);
1577
1529
}
1578
1530
};
1579
1531
1580
1532
for (auto *BB : TheLoop->blocks ())
1581
- for (auto &I : *BB)
1582
- if (!IsSafeOperation (&I)) {
1533
+ for (auto &I : *BB) {
1534
+ if (I.mayWriteToMemory ()) {
1535
+ // We don't support writes to memory.
1536
+ reportVectorizationFailure (
1537
+ " Writes to memory unsupported in early exit loops" ,
1538
+ " Cannot vectorize early exit loop with writes to memory" ,
1539
+ " WritesInEarlyExitLoop" , ORE, TheLoop);
1540
+ return false ;
1541
+ } else if (!IsSafeOperation (&I)) {
1583
1542
reportVectorizationFailure (" Early exit loop contains operations that "
1584
1543
" cannot be speculatively executed" ,
1585
1544
" Early exit loop contains operations that "
@@ -1588,10 +1547,9 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1588
1547
TheLoop);
1589
1548
return false ;
1590
1549
}
1550
+ }
1591
1551
1592
- LLVM_DEBUG (
1593
- dbgs ()
1594
- << " LV: Found an early exit. Retrying with speculative exit count.\n " );
1552
+ // At least one of the exiting blocks must be the latch.
1595
1553
if (isa<SCEVCouldNotCompute>(
1596
1554
PSE.getSE ()->getPredicatedExitCount (TheLoop, LatchBB, &Predicates))) {
1597
1555
reportVectorizationFailure (
@@ -1601,6 +1559,34 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1601
1559
return false ;
1602
1560
}
1603
1561
1562
+ // The vectoriser cannot handle loads that occur after the early exit block.
1563
+ assert (LatchBB->getUniquePredecessor () == getSpeculativeEarlyExitingBlock () &&
1564
+ " Expected latch predecessor to be the early exiting block" );
1565
+ for (Instruction &I : *LatchBB) {
1566
+ if (I.mayReadFromMemory ()) {
1567
+ reportVectorizationFailure (
1568
+ " Loads not permitted after early exit" ,
1569
+ " Cannot vectorize early exit loop with loads after early exit" ,
1570
+ " LoadsAfterEarlyExit" , ORE, TheLoop);
1571
+ return false ;
1572
+ }
1573
+ // Any other problematic instructions should have been caught earlier.
1574
+ assert (!I.mayWriteToMemory () && !I.mayThrow () && !I.mayHaveSideEffects () &&
1575
+ " Unexpected instructions in latch block of early exit loop" );
1576
+ }
1577
+
1578
+ // TODO: Handle loops that may fault.
1579
+ if (!isDereferenceableReadOnlyLoop (TheLoop, PSE.getSE (), DT, AC)) {
1580
+ reportVectorizationFailure (
1581
+ " Loop may fault" ,
1582
+ " Cannot vectorize potentially faulting early exit loop" ,
1583
+ " PotentiallyFaultingEarlyExitLoop" , ORE, TheLoop);
1584
+ return false ;
1585
+ }
1586
+
1587
+ LLVM_DEBUG (
1588
+ dbgs ()
1589
+ << " LV: Found an early exit. Retrying with speculative exit count.\n " );
1604
1590
const SCEV *SpecExitCount = PSE.getSymbolicMaxBackedgeTakenCount ();
1605
1591
assert (!isa<SCEVCouldNotCompute>(SpecExitCount) &&
1606
1592
" Failed to get symbolic expression for backedge taken count" );
@@ -1682,7 +1668,7 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
1682
1668
}
1683
1669
1684
1670
// Go over each instruction and look at memory deps.
1685
- if (!canVectorizeMemory (HasSpeculativeEarlyExit )) {
1671
+ if (!canVectorizeMemory ()) {
1686
1672
LLVM_DEBUG (dbgs () << " LV: Can't vectorize due to memory conflicts\n " );
1687
1673
if (DoExtraAnalysis)
1688
1674
Result = false ;
0 commit comments