@@ -12857,11 +12857,10 @@ const SCEV *ScalarEvolution::getUDivCeilSCEV(const SCEV *N, const SCEV *D) {
12857
12857
return getAddExpr(MinNOne, getUDivExpr(NMinusOne, D));
12858
12858
}
12859
12859
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) {
12865
12864
// The logic in this function assumes we can represent a positive stride.
12866
12865
// If we can't, the backedge-taken count must be zero.
12867
12866
if (IsSigned && BitWidth == 1)
@@ -12895,8 +12894,10 @@ const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
12895
12894
// the case End = RHS of the loop termination condition. This is safe because
12896
12895
// in the other case (End - Start) is zero, leading to a zero maximum backedge
12897
12896
// 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);
12900
12901
12901
12902
// MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride)
12902
12903
MaxEnd = IsSigned ? APIntOps::smax(MaxEnd, MinStart)
@@ -13150,7 +13151,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
13150
13151
// loop (RHS), and the fact that IV does not overflow (which is
13151
13152
// checked above).
13152
13153
const SCEV *MaxBECount = computeMaxBECountForLT(
13153
- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
13154
+ Start, Stride, RHS, L, getTypeSizeInBits(LHS->getType()), IsSigned);
13154
13155
return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
13155
13156
MaxBECount, false /*MaxOrZero*/, Predicates);
13156
13157
}
@@ -13334,7 +13335,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
13334
13335
MaxOrZero = true;
13335
13336
} else {
13336
13337
ConstantMaxBECount = computeMaxBECountForLT(
13337
- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
13338
+ Start, Stride, RHS, L, getTypeSizeInBits(LHS->getType()), IsSigned);
13338
13339
}
13339
13340
13340
13341
if (isa<SCEVCouldNotCompute>(ConstantMaxBECount) &&
0 commit comments