@@ -12857,11 +12857,10 @@ const SCEV *ScalarEvolution::getUDivCeilSCEV(const SCEV *N, const SCEV *D) {
1285712857 return getAddExpr(MinNOne, getUDivExpr(NMinusOne, D));
1285812858}
1285912859
12860- const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
12861- const SCEV *Stride,
12862- const SCEV *End,
12863- unsigned BitWidth,
12864- bool IsSigned) {
12860+ const SCEV *
12861+ ScalarEvolution::computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride,
12862+ const SCEV *End, const Loop *L,
12863+ unsigned BitWidth, bool IsSigned) {
1286512864 // The logic in this function assumes we can represent a positive stride.
1286612865 // If we can't, the backedge-taken count must be zero.
1286712866 if (IsSigned && BitWidth == 1)
@@ -12895,8 +12894,10 @@ const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
1289512894 // the case End = RHS of the loop termination condition. This is safe because
1289612895 // in the other case (End - Start) is zero, leading to a zero maximum backedge
1289712896 // taken count.
12898- APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit)
12899- : APIntOps::umin(getUnsignedRangeMax(End), Limit);
12897+ const SCEV *GuardedEnd = applyLoopGuards(End, L);
12898+ APInt MaxEnd = IsSigned
12899+ ? APIntOps::smin(getSignedRangeMax(GuardedEnd), Limit)
12900+ : APIntOps::umin(getUnsignedRangeMax(GuardedEnd), Limit);
1290012901
1290112902 // MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride)
1290212903 MaxEnd = IsSigned ? APIntOps::smax(MaxEnd, MinStart)
@@ -13150,7 +13151,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
1315013151 // loop (RHS), and the fact that IV does not overflow (which is
1315113152 // checked above).
1315213153 const SCEV *MaxBECount = computeMaxBECountForLT(
13153- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
13154+ Start, Stride, RHS, L, getTypeSizeInBits(LHS->getType()), IsSigned);
1315413155 return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
1315513156 MaxBECount, false /*MaxOrZero*/, Predicates);
1315613157 }
@@ -13334,7 +13335,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
1333413335 MaxOrZero = true;
1333513336 } else {
1333613337 ConstantMaxBECount = computeMaxBECountForLT(
13337- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
13338+ Start, Stride, RHS, L, getTypeSizeInBits(LHS->getType()), IsSigned);
1333813339 }
1333913340
1334013341 if (isa<SCEVCouldNotCompute>(ConstantMaxBECount) &&
0 commit comments