@@ -1180,32 +1180,41 @@ bool DependenceInfo::isKnownLessThan(const SCEV *S, const SCEV *Size) const {
11801180 S = SE->getTruncateOrZeroExtend (S, MaxType);
11811181 Size = SE->getTruncateOrZeroExtend (Size, MaxType);
11821182
1183- // Special check for addrecs using BE taken count
1184- if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S))
1185- if (AddRec->isAffine () && AddRec->hasNoSignedWrap ()) {
1186- const SCEV *BECount = SE->getBackedgeTakenCount (AddRec->getLoop ());
1187- const SCEV *Start = AddRec->getStart ();
1188- const SCEV *Step = AddRec->getStepRecurrence (*SE);
1189- const SCEV *End = AddRec->evaluateAtIteration (BECount, *SE);
1190- const SCEV *Diff0 = SE->getMinusSCEV (Start, Size);
1191- const SCEV *Diff1 = SE->getMinusSCEV (End, Size);
1192-
1193- // If the value of Step is non-negative and the AddRec is non-wrap, it
1194- // reaches its maximum at the last iteration. So it's enouth to check
1195- // whether End - Size is negative.
1196- if (SE->isKnownNonNegative (Step) && SE->isKnownNegative (Diff1))
1197- return true ;
1183+ auto CheckAddRecBECount = [&]() {
1184+ const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S);
1185+ if (!AddRec || !AddRec->isAffine () || !AddRec->hasNoSignedWrap ())
1186+ return false ;
1187+ const SCEV *BECount = collectUpperBound (AddRec->getLoop (), MaxType);
1188+ // If the BTC cannot be computed, check the base case for S.
1189+ if (!BECount || isa<SCEVCouldNotCompute>(BECount))
1190+ return false ;
1191+ const SCEV *Start = AddRec->getStart ();
1192+ const SCEV *Step = AddRec->getStepRecurrence (*SE);
1193+ const SCEV *End = AddRec->evaluateAtIteration (BECount, *SE);
1194+ const SCEV *Diff0 = SE->getMinusSCEV (Start, Size);
1195+ const SCEV *Diff1 = SE->getMinusSCEV (End, Size);
1196+
1197+ // If the value of Step is non-negative and the AddRec is non-wrap, it
1198+ // reaches its maximum at the last iteration. So it's enouth to check
1199+ // whether End - Size is negative.
1200+ if (SE->isKnownNonNegative (Step) && SE->isKnownNegative (Diff1))
1201+ return true ;
11981202
1199- // If the value of Step is non-positive and the AddRec is non-wrap, the
1200- // initial value is its maximum.
1201- if (SE->isKnownNonPositive (Step) && SE->isKnownNegative (Diff0))
1202- return true ;
1203+ // If the value of Step is non-positive and the AddRec is non-wrap, the
1204+ // initial value is its maximum.
1205+ if (SE->isKnownNonPositive (Step) && SE->isKnownNegative (Diff0))
1206+ return true ;
12031207
1204- // Even if we don't know the sign of Step, either Start or End must be
1205- // the maximum value of the AddRec since it is non-wrap.
1206- if (SE->isKnownNegative (Diff0) && SE->isKnownNegative (Diff1))
1207- return true ;
1208- }
1208+ // Even if we don't know the sign of Step, either Start or End must be
1209+ // the maximum value of the AddRec since it is non-wrap.
1210+ if (SE->isKnownNegative (Diff0) && SE->isKnownNegative (Diff1))
1211+ return true ;
1212+
1213+ return false ;
1214+ };
1215+
1216+ if (CheckAddRecBECount ())
1217+ return true ;
12091218
12101219 // Check using normal isKnownNegative
12111220 const SCEV *LimitedBound = SE->getMinusSCEV (S, Size);
0 commit comments