[llvm] [SCEV] Use getUnsignedOverflowLimit at a site (NFC) (PR #217395)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 23 02:06:06 PDT 2026
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/217395
>From 667945371506aa91a2f94880aa6b499a62161065 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Wed, 19 Aug 2026 17:58:20 +0100
Subject: [PATCH 1/2] [SCEV] Improve code around unsigned-overflow-limit (NFC)
---
llvm/lib/Analysis/ScalarEvolution.cpp | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index a05b7e9714f01..2064766d871ae 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -1280,8 +1280,10 @@ static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
ICmpInst::Predicate *Pred,
ScalarEvolution *SE) {
unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
- *Pred = ICmpInst::ICMP_ULT;
+ if (!SE->isKnownPositive(Step))
+ return nullptr;
+ *Pred = ICmpInst::ICMP_ULT;
return SE->getConstant(APInt::getMinValue(BitWidth) -
SE->getUnsignedRangeMax(Step));
}
@@ -5153,14 +5155,14 @@ ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) {
if (AR->hasNoSignedWrap())
return Result;
- if (!AR->isAffine())
+ const SCEV *Step;
+ if (!match(AR, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step))))
return Result;
// This function can be expensive, only try to prove NSW once per AddRec.
if (!SignedWrapViaInductionTried.insert(AR).second)
return Result;
- const SCEV *Step = AR->getStepRecurrence(*this);
const Loop *L = AR->getLoop();
// Check whether the backedge-taken count is SCEVCouldNotCompute.
@@ -5206,15 +5208,14 @@ ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) {
if (AR->hasNoUnsignedWrap())
return Result;
- if (!AR->isAffine())
+ const SCEV *Step;
+ if (!match(AR, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step))))
return Result;
// This function can be expensive, only try to prove NUW once per AddRec.
if (!UnsignedWrapViaInductionTried.insert(AR).second)
return Result;
- const SCEV *Step = AR->getStepRecurrence(*this);
- unsigned BitWidth = getTypeSizeInBits(AR->getType());
const Loop *L = AR->getLoop();
// Check whether the backedge-taken count is SCEVCouldNotCompute.
@@ -5243,13 +5244,13 @@ ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) {
// addrec is safe. Also, if the entry is guarded by a comparison with the
// start value and the backedge is guarded by a comparison with the post-inc
// value, the addrec is safe.
- if (isKnownPositive(Step)) {
- const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
- getUnsignedRangeMax(Step));
- if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
- isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) {
- Result = setFlags(Result, SCEV::FlagNUW);
- }
+ ICmpInst::Predicate Pred;
+ const SCEV *OverflowLimit =
+ getUnsignedOverflowLimitForStep(Step, &Pred, this);
+ if (OverflowLimit &&
+ (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
+ isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
+ Result = setFlags(Result, SCEV::FlagNUW);
}
return Result;
>From 9b60ce479f57a77751bf4227a2aaebf4f86c0a90 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Sun, 23 Aug 2026 09:59:26 +0100
Subject: [PATCH 2/2] [SCEV] Redo patch
---
llvm/lib/Analysis/ScalarEvolution.cpp | 27 ++++++++++++---------------
1 file changed, 12 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 2064766d871ae..2be8495c164fc 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -1280,10 +1280,8 @@ static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
ICmpInst::Predicate *Pred,
ScalarEvolution *SE) {
unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
- if (!SE->isKnownPositive(Step))
- return nullptr;
-
*Pred = ICmpInst::ICMP_ULT;
+
return SE->getConstant(APInt::getMinValue(BitWidth) -
SE->getUnsignedRangeMax(Step));
}
@@ -5155,14 +5153,14 @@ ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) {
if (AR->hasNoSignedWrap())
return Result;
- const SCEV *Step;
- if (!match(AR, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step))))
+ if (!AR->isAffine())
return Result;
// This function can be expensive, only try to prove NSW once per AddRec.
if (!SignedWrapViaInductionTried.insert(AR).second)
return Result;
+ const SCEV *Step = AR->getStepRecurrence(*this);
const Loop *L = AR->getLoop();
// Check whether the backedge-taken count is SCEVCouldNotCompute.
@@ -5208,14 +5206,14 @@ ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) {
if (AR->hasNoUnsignedWrap())
return Result;
- const SCEV *Step;
- if (!match(AR, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step))))
+ if (!AR->isAffine())
return Result;
// This function can be expensive, only try to prove NUW once per AddRec.
if (!UnsignedWrapViaInductionTried.insert(AR).second)
return Result;
+ const SCEV *Step = AR->getStepRecurrence(*this);
const Loop *L = AR->getLoop();
// Check whether the backedge-taken count is SCEVCouldNotCompute.
@@ -5244,15 +5242,14 @@ ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) {
// addrec is safe. Also, if the entry is guarded by a comparison with the
// start value and the backedge is guarded by a comparison with the post-inc
// value, the addrec is safe.
- ICmpInst::Predicate Pred;
- const SCEV *OverflowLimit =
- getUnsignedOverflowLimitForStep(Step, &Pred, this);
- if (OverflowLimit &&
- (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
- isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
- Result = setFlags(Result, SCEV::FlagNUW);
+ if (isKnownPositive(Step)) {
+ ICmpInst::Predicate Pred;
+ const SCEV *OverflowLimit =
+ getUnsignedOverflowLimitForStep(Step, &Pred, this);
+ if (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
+ isKnownOnEveryIteration(Pred, AR, OverflowLimit))
+ Result = setFlags(Result, SCEV::FlagNUW);
}
-
return Result;
}
More information about the llvm-commits
mailing list