[llvm] [SCEV] - Add positive-stride predicate for backedge-taken count. (PR #222261)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 29 01:19:07 PDT 2026
================
@@ -13467,11 +13467,22 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
// The positive stride case is the same as isKnownPositive(Stride) returning
// true (original behavior of the function).
//
- if (PredicatedIV || !NoWrap || !loopIsFiniteByAssumption(L) ||
- !loopHasNoAbnormalExits(L))
+ if (PredicatedIV || !NoWrap || !loopHasNoAbnormalExits(L))
return getCouldNotCompute();
- if (!isKnownNonZero(Stride)) {
+ if (!loopIsFiniteByAssumption(L)) {
+ // If the loop may be infinite, add a predicate ensuring Stride is
+ // positive, to guarantee forward progress.
+ if (!AllowPredicates || !isLoopInvariant(Stride, L))
+ return getCouldNotCompute();
+
+ const SCEV *Zero = getZero(Stride->getType());
+ auto *P = getComparePredicate(ICmpInst::ICMP_SGT, Stride, Zero);
+ Predicates.push_back(P);
+ // When the predicate holds (Stride > 0), umax(Stride, 1) == Stride,
+ // so the result is unchanged. To prevent div by zero.
+ Stride = getUMaxExpr(Stride, getOne(Stride->getType()));
----------------
fhahn wrote:
Ok, although this could have probably be done in this PR
https://github.com/llvm/llvm-project/pull/222261
More information about the llvm-commits
mailing list