@@ -1381,9 +1381,10 @@ RecurrenceDescriptor::getReductionOpChain(PHINode *Phi, Loop *L) const {
13811381 return ReductionOperations;
13821382}
13831383
1384- InductionDescriptor::InductionDescriptor (Value *Start, InductionKind K,
1385- const SCEV *Step, BinaryOperator *BOp,
1386- SmallVectorImpl<Instruction *> *Casts)
1384+ InductionDescriptor::InductionDescriptor (
1385+ Value *Start, InductionKind K, const SCEV *Step, BinaryOperator *BOp,
1386+ SmallVectorImpl<Instruction *> *Casts,
1387+ ArrayRef<const SCEVPredicate *> NoWrapPreds)
13871388 : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) {
13881389 assert (IK != IK_NoInduction && " Not an induction" );
13891390
@@ -1412,6 +1413,7 @@ InductionDescriptor::InductionDescriptor(Value *Start, InductionKind K,
14121413
14131414 if (Casts)
14141415 llvm::append_range (RedundantCasts, *Casts);
1416+ llvm::append_range (NoWrapPredicates, NoWrapPreds);
14151417}
14161418
14171419InductionDescriptor
@@ -1511,15 +1513,22 @@ bool InductionDescriptor::isFPInductionPHI(PHINode *Phi, const Loop *TheLoop,
15111513// / If we are able to find such sequence, we return the instructions
15121514// / we found, namely %casted_phi and the instructions on its use-def chain up
15131515// / to the phi (not including the phi).
1514- static bool getCastsForInductionPHI (PredicatedScalarEvolution &PSE,
1515- const SCEVUnknown *PhiScev,
1516- const SCEVAddRecExpr *AR,
1517- SmallVectorImpl<Instruction *> &CastInsts) {
1516+ static bool
1517+ getCastsForInductionPHI (PredicatedScalarEvolution &PSE,
1518+ const SCEVUnknown *PhiScev, const SCEVAddRecExpr *AR,
1519+ SmallVectorImpl<Instruction *> &CastInsts,
1520+ ArrayRef<const SCEVPredicate *> NoWrapPreds) {
15181521
15191522 assert (CastInsts.empty () && " CastInsts is expected to be empty." );
15201523 auto *PN = cast<PHINode>(PhiScev->getValue ());
1521- assert (PSE.getSCEV (PN) == AR && " Unexpected phi node SCEV expression" );
1524+
1525+ // Build a predicate to rewrite SCEVs of values in the cast chain using the
1526+ // predicates needed for this induction.
1527+ ScalarEvolution &SE = *PSE.getSE ();
1528+ SCEVUnionPredicate NoWrapUnionPred (NoWrapPreds, SE);
15221529 const Loop *L = AR->getLoop ();
1530+ assert (SE.rewriteUsingPredicate (SE.getSCEV (PN), L, NoWrapUnionPred) == AR &&
1531+ " Unexpected phi node SCEV expression" );
15231532
15241533 // Find any cast instructions that participate in the def-use chain of
15251534 // PhiScev in the loop.
@@ -1564,8 +1573,10 @@ static bool getCastsForInductionPHI(PredicatedScalarEvolution &PSE,
15641573 if (!Inst || !L->contains (Inst)) {
15651574 return false ;
15661575 }
1567- auto *AddRec = dyn_cast<SCEVAddRecExpr>(PSE.getSCEV (Val));
1568- if (AddRec && PSE.areAddRecsEqualWithPreds (AddRec, AR))
1576+ // Create AddRec with NoWrapPredicates applied.
1577+ auto *AddRec = dyn_cast<SCEVAddRecExpr>(
1578+ SE.rewriteUsingPredicate (SE.getSCEV (Val), L, NoWrapUnionPred));
1579+ if (AddRec && PSE.areAddRecsEqualWithPreds (AddRec, AR, NoWrapPreds))
15691580 InCastSequence = true ;
15701581 if (InCastSequence) {
15711582 // Only the last instruction in the cast sequence is expected to have
@@ -1603,9 +1614,12 @@ bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop,
16031614 const SCEV *PhiScev = PSE.getSCEV (Phi);
16041615 const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
16051616
1617+ // Collect predicates needed to force the SCEV into an AddRecExpr.
1618+ SmallVector<const SCEVPredicate *, 2 > Preds;
1619+
16061620 // We need this expression to be an AddRecExpr.
16071621 if (Assume && !AR)
1608- AR = PSE.getAsAddRec (Phi);
1622+ AR = PSE.getAsAddRec (Phi, &Preds );
16091623
16101624 if (!AR) {
16111625 LLVM_DEBUG (dbgs () << " LV: PHI is not a poly recurrence.\n " );
@@ -1621,17 +1635,17 @@ bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop,
16211635 // induction.
16221636 if (PhiScev != AR && SymbolicPhi) {
16231637 SmallVector<Instruction *, 2 > Casts;
1624- if (getCastsForInductionPHI (PSE, SymbolicPhi, AR, Casts))
1625- return isInductionPHI (Phi, TheLoop, PSE.getSE (), D, AR, &Casts);
1638+ if (getCastsForInductionPHI (PSE, SymbolicPhi, AR, Casts, Preds ))
1639+ return isInductionPHI (Phi, TheLoop, PSE.getSE (), D, Preds, AR, &Casts);
16261640 }
16271641
1628- return isInductionPHI (Phi, TheLoop, PSE.getSE (), D, AR);
1642+ return isInductionPHI (Phi, TheLoop, PSE.getSE (), D, Preds, AR);
16291643}
16301644
16311645bool InductionDescriptor::isInductionPHI (
16321646 PHINode *Phi, const Loop *TheLoop, ScalarEvolution *SE,
1633- InductionDescriptor &D, const SCEV *Expr ,
1634- SmallVectorImpl<Instruction *> *CastsToIgnore) {
1647+ InductionDescriptor &D, ArrayRef< const SCEVPredicate *> Preds ,
1648+ const SCEV *Expr, SmallVectorImpl<Instruction *> *CastsToIgnore) {
16351649 Type *PhiTy = Phi->getType ();
16361650 // isSCEVable returns true for integer and pointer types.
16371651 if (!SE->isSCEVable (PhiTy))
@@ -1671,13 +1685,14 @@ bool InductionDescriptor::isInductionPHI(
16711685 BinaryOperator *BOp =
16721686 dyn_cast<BinaryOperator>(Phi->getIncomingValueForBlock (Latch));
16731687 D = InductionDescriptor (StartValue, IK_IntInduction, Step, BOp,
1674- CastsToIgnore);
1688+ CastsToIgnore, Preds );
16751689 return true ;
16761690 }
16771691
16781692 assert (PhiTy->isPointerTy () && " The PHI must be a pointer" );
16791693
16801694 // This allows induction variables w/non-constant steps.
1681- D = InductionDescriptor (StartValue, IK_PtrInduction, Step);
1695+ D = InductionDescriptor (StartValue, IK_PtrInduction, Step,
1696+ /* InductionBinOp=*/ nullptr , /* Casts=*/ nullptr , Preds);
16821697 return true ;
16831698}
0 commit comments