@@ -357,23 +357,30 @@ bool llvm::canPeelLastIteration(const Loop &L, ScalarEvolution &SE) {
357357 m_scev_AffineAddRec (m_SCEV (), m_scev_One (), m_SpecificLoop (&L)));
358358}
359359
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.
363363static bool shouldPeelLastIteration (Loop &L, CmpPredicate Pred,
364364 const SCEVAddRecExpr *LeftAR,
365- const SCEV *RightSCEV, const SCEV *BTC,
366- ScalarEvolution &SE,
365+ const SCEV *RightSCEV, ScalarEvolution &SE,
367366 const TargetTransformInfo &TTI) {
367+ if (!canPeelLastIteration (L, SE))
368+ return false ;
369+
370+ const SCEV *BTC = SE.getBackedgeTakenCount (&L);
368371 SCEVExpander Expander (SE, L.getHeader ()->getDataLayout (), " loop-peel" );
369372 if (!SE.isKnownNonZero (BTC) &&
370373 Expander.isHighCostExpansion (BTC, &L, SCEVCheapExpansionBudget, &TTI,
371374 L.getLoopPredecessor ()->getTerminator ()))
372375 return false ;
373376
377+ auto Guards = ScalarEvolution::LoopGuards::collect (&L, SE);
378+ BTC = SE.applyLoopGuards (BTC, Guards);
379+ RightSCEV = SE.applyLoopGuards (RightSCEV, Guards);
374380 const SCEV *ValAtLastIter = LeftAR->evaluateAtIteration (BTC, SE);
375381 const SCEV *ValAtSecondToLastIter = LeftAR->evaluateAtIteration (
376382 SE.getMinusSCEV (BTC, SE.getOne (BTC->getType ())), SE);
383+
377384 return SE.isKnownPredicate (ICmpInst::getInversePredicate (Pred), ValAtLastIter,
378385 RightSCEV) &&
379386 SE.isKnownPredicate (Pred, ValAtSecondToLastIter, RightSCEV);
@@ -480,19 +487,8 @@ countToEliminateCompares(Loop &L, unsigned MaxPeelCount, ScalarEvolution &SE,
480487 const SCEV *Step = LeftAR->getStepRecurrence (SE);
481488 if (!PeelWhilePredicateIsKnown (NewPeelCount, IterVal, RightSCEV, Step,
482489 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))
491491 DesiredPeelCountLast = 1 ;
492- else
493- assert (!shouldPeelLastIteration (L, Pred, LeftAR, RightSCEV, BTC, SE,
494- TTI) &&
495- " loop guards pessimized result" );
496492 return ;
497493 }
498494
0 commit comments