@@ -357,23 +357,30 @@ bool llvm::canPeelLastIteration(const Loop &L, ScalarEvolution &SE) {
357
357
m_scev_AffineAddRec (m_SCEV (), m_scev_One (), m_SpecificLoop (&L)));
358
358
}
359
359
360
- // / Returns true if the last iteration should be peeled off, i.e. the condition
361
- // / (Pred LeftAR, RightSCEV) is known at the last iteration and the inverse
362
- // / condition is known at the second-to-last.
360
+ // / Returns true if the last iteration can be peeled off and the condition (Pred
361
+ // / LeftAR, RightSCEV) is known at the last iteration and the inverse condition
362
+ // / is known at the second-to-last.
363
363
static bool shouldPeelLastIteration (Loop &L, CmpPredicate Pred,
364
364
const SCEVAddRecExpr *LeftAR,
365
- const SCEV *RightSCEV, const SCEV *BTC,
366
- ScalarEvolution &SE,
365
+ const SCEV *RightSCEV, ScalarEvolution &SE,
367
366
const TargetTransformInfo &TTI) {
367
+ if (!canPeelLastIteration (L, SE))
368
+ return false ;
369
+
370
+ const SCEV *BTC = SE.getBackedgeTakenCount (&L);
368
371
SCEVExpander Expander (SE, L.getHeader ()->getDataLayout (), " loop-peel" );
369
372
if (!SE.isKnownNonZero (BTC) &&
370
373
Expander.isHighCostExpansion (BTC, &L, SCEVCheapExpansionBudget, &TTI,
371
374
L.getLoopPredecessor ()->getTerminator ()))
372
375
return false ;
373
376
377
+ auto Guards = ScalarEvolution::LoopGuards::collect (&L, SE);
378
+ BTC = SE.applyLoopGuards (BTC, Guards);
379
+ RightSCEV = SE.applyLoopGuards (RightSCEV, Guards);
374
380
const SCEV *ValAtLastIter = LeftAR->evaluateAtIteration (BTC, SE);
375
381
const SCEV *ValAtSecondToLastIter = LeftAR->evaluateAtIteration (
376
382
SE.getMinusSCEV (BTC, SE.getOne (BTC->getType ())), SE);
383
+
377
384
return SE.isKnownPredicate (ICmpInst::getInversePredicate (Pred), ValAtLastIter,
378
385
RightSCEV) &&
379
386
SE.isKnownPredicate (Pred, ValAtSecondToLastIter, RightSCEV);
@@ -480,19 +487,8 @@ countToEliminateCompares(Loop &L, unsigned MaxPeelCount, ScalarEvolution &SE,
480
487
const SCEV *Step = LeftAR->getStepRecurrence (SE);
481
488
if (!PeelWhilePredicateIsKnown (NewPeelCount, IterVal, RightSCEV, Step,
482
489
Pred)) {
483
- if (!canPeelLastIteration (L, SE))
484
- return ;
485
-
486
- const SCEV *BTC = SE.getBackedgeTakenCount (&L);
487
- auto Guards = ScalarEvolution::LoopGuards::collect (&L, SE);
488
- if (shouldPeelLastIteration (L, Pred, LeftAR,
489
- SE.applyLoopGuards (RightSCEV, Guards),
490
- SE.applyLoopGuards (BTC, Guards), SE, TTI))
490
+ if (shouldPeelLastIteration (L, Pred, LeftAR, RightSCEV, SE, TTI))
491
491
DesiredPeelCountLast = 1 ;
492
- else
493
- assert (!shouldPeelLastIteration (L, Pred, LeftAR, RightSCEV, BTC, SE,
494
- TTI) &&
495
- " loop guards pessimized result" );
496
492
return ;
497
493
}
498
494
0 commit comments