@@ -2385,12 +2385,15 @@ bool IndVarSimplify::
2385
2385
linearFunctionTestReplace (Loop *L, BasicBlock *ExitingBB,
2386
2386
const SCEV *BackedgeTakenCount,
2387
2387
PHINode *IndVar, SCEVExpander &Rewriter) {
2388
+ assert (isLoopCounter (IndVar, L, SE));
2389
+ assert (L->getLoopLatch () && " Loop no longer in simplified form?" );
2390
+ Instruction * const IncVar =
2391
+ cast<Instruction>(IndVar->getIncomingValueForBlock (L->getLoopLatch ()));
2392
+
2388
2393
// Initialize CmpIndVar and IVCount to their preincremented values.
2389
2394
Value *CmpIndVar = IndVar;
2390
2395
const SCEV *IVCount = BackedgeTakenCount;
2391
2396
2392
- assert (L->getLoopLatch () && " Loop no longer in simplified form?" );
2393
-
2394
2397
// If the exiting block is the same as the backedge block, we prefer to
2395
2398
// compare against the post-incremented value, otherwise we must compare
2396
2399
// against the preincremented value.
@@ -2401,12 +2404,10 @@ linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
2401
2404
// to add a potentially UB introducing use. We need to either a) show
2402
2405
// the loop test we're modifying is already in post-inc form, or b) show
2403
2406
// that adding a use must not introduce UB.
2404
- Instruction *Inc =
2405
- cast<Instruction>(IndVar->getIncomingValueForBlock (L->getLoopLatch ()));
2406
2407
ICmpInst *LoopTest = getLoopTest (L, ExitingBB);
2407
- SafeToPostInc = LoopTest->getOperand (0 ) == Inc ||
2408
- LoopTest->getOperand (1 ) == Inc ||
2409
- mustExecuteUBIfPoisonOnPathTo (Inc , ExitingBB->getTerminator (), DT);
2408
+ SafeToPostInc = LoopTest->getOperand (0 ) == IncVar ||
2409
+ LoopTest->getOperand (1 ) == IncVar ||
2410
+ mustExecuteUBIfPoisonOnPathTo (IncVar , ExitingBB->getTerminator (), DT);
2410
2411
}
2411
2412
if (SafeToPostInc) {
2412
2413
// Add one to the "backedge-taken" count to get the trip count.
@@ -2417,7 +2418,7 @@ linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
2417
2418
// The BackedgeTaken expression contains the number of times that the
2418
2419
// backedge branches to the loop header. This is one less than the
2419
2420
// number of times the loop executes, so use the incremented indvar.
2420
- CmpIndVar = IndVar-> getIncomingValueForBlock (ExitingBB) ;
2421
+ CmpIndVar = IncVar ;
2421
2422
}
2422
2423
}
2423
2424
@@ -2433,7 +2434,6 @@ linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
2433
2434
// dynamically dead IV that wraps on the first loop iteration only, which is
2434
2435
// not covered by the post-inc addrec. (If the new IV was not dynamically
2435
2436
// dead, it could not be poison on the first iteration in the first place.)
2436
- Value *IncVar = IndVar->getIncomingValueForBlock (L->getLoopLatch ());
2437
2437
if (auto *BO = dyn_cast<BinaryOperator>(IncVar)) {
2438
2438
const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV (IncVar));
2439
2439
if (BO->hasNoUnsignedWrap ())
0 commit comments