@@ -12862,11 +12862,10 @@ const SCEV *ScalarEvolution::getUDivCeilSCEV(const SCEV *N, const SCEV *D) {
1286212862 return getAddExpr(MinNOne, getUDivExpr(NMinusOne, D));
1286312863}
1286412864
12865- const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
12866- const SCEV *Stride,
12867- const SCEV *End,
12868- unsigned BitWidth,
12869- bool IsSigned) {
12865+ const SCEV *
12866+ ScalarEvolution::computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride,
12867+ const SCEV *End, const Loop *L,
12868+ unsigned BitWidth, bool IsSigned) {
1287012869 // The logic in this function assumes we can represent a positive stride.
1287112870 // If we can't, the backedge-taken count must be zero.
1287212871 if (IsSigned && BitWidth == 1)
@@ -12900,8 +12899,10 @@ const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
1290012899 // the case End = RHS of the loop termination condition. This is safe because
1290112900 // in the other case (End - Start) is zero, leading to a zero maximum backedge
1290212901 // taken count.
12903- APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit)
12904- : APIntOps::umin(getUnsignedRangeMax(End), Limit);
12902+ const SCEV *GuardedEnd = applyLoopGuards(End, L);
12903+ APInt MaxEnd = IsSigned
12904+ ? APIntOps::smin(getSignedRangeMax(GuardedEnd), Limit)
12905+ : APIntOps::umin(getUnsignedRangeMax(GuardedEnd), Limit);
1290512906
1290612907 // MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride)
1290712908 MaxEnd = IsSigned ? APIntOps::smax(MaxEnd, MinStart)
@@ -13155,7 +13156,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
1315513156 // loop (RHS), and the fact that IV does not overflow (which is
1315613157 // checked above).
1315713158 const SCEV *MaxBECount = computeMaxBECountForLT(
13158- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
13159+ Start, Stride, RHS, L, getTypeSizeInBits(LHS->getType()), IsSigned);
1315913160 return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
1316013161 MaxBECount, false /*MaxOrZero*/, Predicates);
1316113162 }
@@ -13339,7 +13340,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
1333913340 MaxOrZero = true;
1334013341 } else {
1334113342 ConstantMaxBECount = computeMaxBECountForLT(
13342- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
13343+ Start, Stride, RHS, L, getTypeSizeInBits(LHS->getType()), IsSigned);
1334313344 }
1334413345
1334513346 if (isa<SCEVCouldNotCompute>(ConstantMaxBECount) &&
0 commit comments