@@ -9149,23 +9149,21 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
9149
9149
// behaviour), and we can prove the test sequence produced must repeat
9150
9150
// the same values on self-wrap of the IV, then we can infer that IV
9151
9151
// doesn't self wrap because if it did, we'd have an infinite (undefined)
9152
- // loop.
9152
+ // loop. Note that a stride of 0 is trivially no-self-wrap by definition.
9153
9153
if (ControllingFiniteLoop && isLoopInvariant(RHS, L)) {
9154
9154
// TODO: We can peel off any functions which are invertible *in L*. Loop
9155
9155
// invariant terms are effectively constants for our purposes here.
9156
9156
auto *InnerLHS = LHS;
9157
9157
if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS))
9158
9158
InnerLHS = ZExt->getOperand();
9159
- if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS)) {
9160
- auto *StrideC = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this));
9161
- if (!AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() &&
9162
- StrideC && StrideC->getAPInt().isPowerOf2()) {
9163
- auto Flags = AR->getNoWrapFlags();
9164
- Flags = setFlags(Flags, SCEV::FlagNW);
9165
- SmallVector<const SCEV*> Operands{AR->operands()};
9166
- Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
9167
- setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
9168
- }
9159
+ if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS);
9160
+ AR && !AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() &&
9161
+ isKnownToBeAPowerOfTwo(AR->getStepRecurrence(*this), /*OrZero=*/true)) {
9162
+ auto Flags = AR->getNoWrapFlags();
9163
+ Flags = setFlags(Flags, SCEV::FlagNW);
9164
+ SmallVector<const SCEV *> Operands{AR->operands()};
9165
+ Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
9166
+ setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
9169
9167
}
9170
9168
}
9171
9169
@@ -10845,6 +10843,23 @@ bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
10845
10843
return getUnsignedRangeMin(S) != 0;
10846
10844
}
10847
10845
10846
+ bool ScalarEvolution::isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero) {
10847
+ auto NonRecursive = [this](const SCEV *S) {
10848
+ if (auto *C = dyn_cast<SCEVConstant>(S))
10849
+ return C->getAPInt().isPowerOf2();
10850
+ // The vscale_range indicates vscale is a power-of-two.
10851
+ return isa<SCEVVScale>(S) && F.hasFnAttribute(Attribute::VScaleRange);
10852
+ };
10853
+
10854
+ if (NonRecursive(S))
10855
+ return true;
10856
+
10857
+ auto *Mul = dyn_cast<SCEVMulExpr>(S);
10858
+ if (!Mul)
10859
+ return false;
10860
+ return all_of(Mul->operands(), NonRecursive) && (OrZero || isKnownNonZero(S));
10861
+ }
10862
+
10848
10863
std::pair<const SCEV *, const SCEV *>
10849
10864
ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) {
10850
10865
// Compute SCEV on entry of loop L.
@@ -12775,8 +12790,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
12775
12790
if (!isLoopInvariant(RHS, L))
12776
12791
return false;
12777
12792
12778
- auto *StrideC = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this));
12779
- if (!StrideC || !StrideC->getAPInt().isPowerOf2())
12793
+ if (!isKnownToBeAPowerOfTwo(AR->getStepRecurrence(*this), /*OrZero=*/true))
12780
12794
return false;
12781
12795
12782
12796
if (!ControlsOnlyExit || !loopHasNoAbnormalExits(L))
@@ -13132,52 +13146,50 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
13132
13146
// "(Start - End) + (Stride - 1)" has unsigned overflow.
13133
13147
const SCEV *One = getOne(Stride->getType());
13134
13148
bool MayAddOverflow = [&] {
13135
- if (auto *StrideC = dyn_cast<SCEVConstant>(Stride)) {
13136
- if (StrideC->getAPInt().isPowerOf2()) {
13137
- // Suppose Stride is a power of two, and Start/End are unsigned
13138
- // integers. Let UMAX be the largest representable unsigned
13139
- // integer.
13140
- //
13141
- // By the preconditions of this function, we know
13142
- // "(Start + Stride * N) >= End", and this doesn't overflow.
13143
- // As a formula:
13144
- //
13145
- // End <= (Start + Stride * N) <= UMAX
13146
- //
13147
- // Subtracting Start from all the terms:
13148
- //
13149
- // End - Start <= Stride * N <= UMAX - Start
13150
- //
13151
- // Since Start is unsigned, UMAX - Start <= UMAX. Therefore:
13152
- //
13153
- // End - Start <= Stride * N <= UMAX
13154
- //
13155
- // Stride * N is a multiple of Stride. Therefore,
13156
- //
13157
- // End - Start <= Stride * N <= UMAX - (UMAX mod Stride)
13158
- //
13159
- // Since Stride is a power of two, UMAX + 1 is divisible by
13160
- // Stride. Therefore, UMAX mod Stride == Stride - 1. So we can
13161
- // write:
13162
- //
13163
- // End - Start <= Stride * N <= UMAX - Stride - 1
13164
- //
13165
- // Dropping the middle term:
13166
- //
13167
- // End - Start <= UMAX - Stride - 1
13168
- //
13169
- // Adding Stride - 1 to both sides:
13170
- //
13171
- // (End - Start) + (Stride - 1) <= UMAX
13172
- //
13173
- // In other words, the addition doesn't have unsigned overflow.
13174
- //
13175
- // A similar proof works if we treat Start/End as signed values.
13176
- // Just rewrite steps before "End - Start <= Stride * N <= UMAX"
13177
- // to use signed max instead of unsigned max. Note that we're
13178
- // trying to prove a lack of unsigned overflow in either case.
13179
- return false;
13180
- }
13149
+ if (isKnownToBeAPowerOfTwo(Stride)) {
13150
+ // Suppose Stride is a power of two, and Start/End are unsigned
13151
+ // integers. Let UMAX be the largest representable unsigned
13152
+ // integer.
13153
+ //
13154
+ // By the preconditions of this function, we know
13155
+ // "(Start + Stride * N) >= End", and this doesn't overflow.
13156
+ // As a formula:
13157
+ //
13158
+ // End <= (Start + Stride * N) <= UMAX
13159
+ //
13160
+ // Subtracting Start from all the terms:
13161
+ //
13162
+ // End - Start <= Stride * N <= UMAX - Start
13163
+ //
13164
+ // Since Start is unsigned, UMAX - Start <= UMAX. Therefore:
13165
+ //
13166
+ // End - Start <= Stride * N <= UMAX
13167
+ //
13168
+ // Stride * N is a multiple of Stride. Therefore,
13169
+ //
13170
+ // End - Start <= Stride * N <= UMAX - (UMAX mod Stride)
13171
+ //
13172
+ // Since Stride is a power of two, UMAX + 1 is divisible by
13173
+ // Stride. Therefore, UMAX mod Stride == Stride - 1. So we can
13174
+ // write:
13175
+ //
13176
+ // End - Start <= Stride * N <= UMAX - Stride - 1
13177
+ //
13178
+ // Dropping the middle term:
13179
+ //
13180
+ // End - Start <= UMAX - Stride - 1
13181
+ //
13182
+ // Adding Stride - 1 to both sides:
13183
+ //
13184
+ // (End - Start) + (Stride - 1) <= UMAX
13185
+ //
13186
+ // In other words, the addition doesn't have unsigned overflow.
13187
+ //
13188
+ // A similar proof works if we treat Start/End as signed values.
13189
+ // Just rewrite steps before "End - Start <= Stride * N <= UMAX"
13190
+ // to use signed max instead of unsigned max. Note that we're
13191
+ // trying to prove a lack of unsigned overflow in either case.
13192
+ return false;
13181
13193
}
13182
13194
if (Start == Stride || Start == getMinusSCEV(Stride, One)) {
13183
13195
// If Start is equal to Stride, (End - Start) + (Stride - 1) == End
0 commit comments