[llvm] [IndVarSimplify] Kept additional nuw and nsw flags during LinearFunctionTestReplace (PR #177433)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 20 15:00:17 PST 2026
https://github.com/yasmincs updated https://github.com/llvm/llvm-project/pull/177433
>From 8fdbef5107d19a6b62c8db8a94c4c80fd62071ca Mon Sep 17 00:00:00 2001
From: ysarita <ysarita at nvidia.com>
Date: Thu, 22 Jan 2026 18:50:02 +0000
Subject: [PATCH] Kept additional nsw nuw flags during
linearfunctiontestreplace
---
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 39 ++++++++++++++++++-
.../IndVarSimplify/lftr-multi-exit.ll | 6 +--
...ple-unreachable-exits-for-vectorization.ll | 4 +-
.../loop-rotation-vs-common-code-hoisting.ll | 2 +-
4 files changed, 43 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index f377c192371ea..b049eab5ffcea 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -1095,10 +1095,45 @@ linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
// dead, it could not be poison on the first iteration in the first place.)
if (auto *BO = dyn_cast<BinaryOperator>(IncVar)) {
const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IncVar));
+
+ // However, if we successfully computed an ExitCount, we can infer nowrap
+ // properties from the original loop exit condition. If the original
+ // comparison was signed and SCEV computed an exit count, then NSW is safe.
+ // Similarly, if it was unsigned, NUW is safe. This is because SCEV's exit
+ // count analysis proves the loop exits before overflow occurs for that
+ // signedness.
+ bool KeepNSW = false;
+ bool KeepNUW = false;
+
+ BranchInst *BI = cast<BranchInst>(ExitingBB->getTerminator());
+ if (auto *OrigCmp = dyn_cast<ICmpInst>(BI->getCondition())) {
+ // Check if the chosen IV is actually used in the original comparison.
+ bool IVUsedInOriginalCmp = (OrigCmp->getOperand(0) == IndVar ||
+ OrigCmp->getOperand(0) == IncVar ||
+ OrigCmp->getOperand(1) == IndVar ||
+ OrigCmp->getOperand(1) == IncVar);
+
+ if (IVUsedInOriginalCmp) {
+ ICmpInst::Predicate Pred = OrigCmp->getPredicate();
+
+ // Don't keep flags if the predicate sign was changed by SimplifyIndvar.
+ // The samesign flag indicates the predicate was originally signed but
+ // was canonicalized to unsigned (e.g., slt -> samesign ult).
+ if (!OrigCmp->hasSameSign()) {
+ // If the original comparison was signed, SCEV proved NSW is safe.
+ KeepNSW = ICmpInst::isSigned(Pred);
+ // If the original comparison was unsigned, SCEV proved NUW is safe.
+ KeepNUW = ICmpInst::isUnsigned(Pred);
+ }
+ }
+ }
+
+ // Use SCEV's analysis, but also keep flags if the original comparison
+ // guarantees safety for that signedness.
if (BO->hasNoUnsignedWrap())
- BO->setHasNoUnsignedWrap(AR->hasNoUnsignedWrap());
+ BO->setHasNoUnsignedWrap(AR->hasNoUnsignedWrap() || KeepNUW);
if (BO->hasNoSignedWrap())
- BO->setHasNoSignedWrap(AR->hasNoSignedWrap());
+ BO->setHasNoSignedWrap(AR->hasNoSignedWrap() || KeepNSW);
}
Value *ExitCnt = genLoopLimit(
diff --git a/llvm/test/Transforms/IndVarSimplify/lftr-multi-exit.ll b/llvm/test/Transforms/IndVarSimplify/lftr-multi-exit.ll
index 0898c6c2dae34..a2cdcaaff59db 100644
--- a/llvm/test/Transforms/IndVarSimplify/lftr-multi-exit.ll
+++ b/llvm/test/Transforms/IndVarSimplify/lftr-multi-exit.ll
@@ -172,7 +172,7 @@ define void @unanalyzeable_latch(i32 %n) {
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV]], [[N:%.*]]
; CHECK-NEXT: br i1 [[EXITCOND]], label [[LATCH]], label [[EXIT:%.*]]
; CHECK: latch:
-; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
; CHECK-NEXT: store i32 [[IV]], ptr @A, align 4
; CHECK-NEXT: [[VOL:%.*]] = load volatile i32, ptr @A, align 4
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[VOL]], 1000
@@ -208,7 +208,7 @@ define void @single_exit_no_latch(i32 %n) {
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i32 [[IV]], [[N:%.*]]
; CHECK-NEXT: br i1 [[EXITCOND]], label [[LATCH]], label [[EXIT:%.*]]
; CHECK: latch:
-; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
; CHECK-NEXT: store i32 [[IV]], ptr @A, align 4
; CHECK-NEXT: br label [[LOOP]]
; CHECK: exit:
@@ -247,7 +247,7 @@ define void @no_latch_exit(i32 %n, i32 %m) {
; CHECK-NEXT: br i1 [[EXITCOND1]], label [[LATCH]], label [[EXIT]]
; CHECK: latch:
; CHECK-NEXT: store volatile i32 [[IV]], ptr @A, align 4
-; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
; CHECK-NEXT: br label [[LOOP]]
; CHECK: exit:
; CHECK-NEXT: ret void
diff --git a/llvm/test/Transforms/PhaseOrdering/AArch64/peel-multiple-unreachable-exits-for-vectorization.ll b/llvm/test/Transforms/PhaseOrdering/AArch64/peel-multiple-unreachable-exits-for-vectorization.ll
index 2fe49a31b7722..4a0d81bb4fc4a 100644
--- a/llvm/test/Transforms/PhaseOrdering/AArch64/peel-multiple-unreachable-exits-for-vectorization.ll
+++ b/llvm/test/Transforms/PhaseOrdering/AArch64/peel-multiple-unreachable-exits-for-vectorization.ll
@@ -79,7 +79,7 @@ define i64 @sum_2_at_with_int_conversion(ptr %A, ptr %B, i64 %N) {
; CHECK-NEXT: [[LV_I9:%.*]] = load i64, ptr [[GEP_IDX_I8]], align 8
; CHECK-NEXT: [[ADD:%.*]] = add i64 [[LV_I]], [[SUM]]
; CHECK-NEXT: [[SUM_NEXT]] = add i64 [[ADD]], [[LV_I9]]
-; CHECK-NEXT: [[IV_NEXT]] = add nuw i64 [[IV]], 1
+; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV]], [[SMAX]]
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[EXIT]], label [[LOOP]], !llvm.loop [[LOOP3:![0-9]+]]
; CHECK: error.i:
@@ -203,7 +203,7 @@ define i64 @sum_3_at_with_int_conversion(ptr %A, ptr %B, ptr %C, i64 %N) {
; CHECK-NEXT: [[ADD_1:%.*]] = add i64 [[LV_I]], [[SUM]]
; CHECK-NEXT: [[ADD_2:%.*]] = add i64 [[ADD_1]], [[LV_I9]]
; CHECK-NEXT: [[SUM_NEXT]] = add i64 [[ADD_2]], [[LV_I20]]
-; CHECK-NEXT: [[IV_NEXT]] = add nuw i64 [[IV]], 1
+; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV]], [[SMAX]]
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[EXIT]], label [[LOOP]], !llvm.loop [[LOOP5:![0-9]+]]
; CHECK: error.i:
diff --git a/llvm/test/Transforms/PhaseOrdering/loop-rotation-vs-common-code-hoisting.ll b/llvm/test/Transforms/PhaseOrdering/loop-rotation-vs-common-code-hoisting.ll
index 5ff57eae3a773..9333be4e6c4c7 100644
--- a/llvm/test/Transforms/PhaseOrdering/loop-rotation-vs-common-code-hoisting.ll
+++ b/llvm/test/Transforms/PhaseOrdering/loop-rotation-vs-common-code-hoisting.ll
@@ -59,7 +59,7 @@ define void @_Z4loopi(i32 %width) {
; HOIST-NEXT: br label [[RETURN]]
; HOIST: for.body:
; HOIST-NEXT: tail call void @f1()
-; HOIST-NEXT: [[INC]] = add nuw i32 [[I_0]], 1
+; HOIST-NEXT: [[INC]] = add nuw nsw i32 [[I_0]], 1
; HOIST-NEXT: br label [[FOR_COND]]
; HOIST: return:
; HOIST-NEXT: ret void
More information about the llvm-commits
mailing list