[llvm-branch-commits] [llvm] [IndVarSimplify] Use context to prove a narrow IV def non-negative (PR #236461)
Dmitry Makogon via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 12 03:55:20 PST 2026
https://github.com/d-makogon updated https://github.com/llvm/llvm-project/pull/236461
>From 1adfb5eb4c5adf83ada5ec3f39f29ad0acbb7b70 Mon Sep 17 00:00:00 2001
From: Dmitrii Makogon <dmakogon at azul.com>
Date: Wed, 28 Oct 2026 13:37:32 +0000
Subject: [PATCH] [IndVarSimplify] Use context to prove a narrow IV def
non-negative
When IndVars widens an IV, it checks for each narrow def
(the IV and values computed from it) whether it is
non-negative on every iteration.
If it is, zext and sext of it are equal. Then when IndVars
idens the def's users, they can choose to use either
the sext or zext of the def.
Previously, we didn't use context to prove the non-negativity of def.
Now ask SCEV about the def non-negativity at its context -
the def instruction itself.
This allows SCEV to infer facts from the dominating guards and assumes.
This in turn allows eliminating unnecessary truncs/zexts. E.g. in
if (m > 0)
for (i32 i = 0; i < m; i++)
use(zext(i + m <nsw>));
i + m is non-negative on every iteration because of the guard
and the nsw on it.
Before, IndVars would widen i + m to i64, trunc it back to i32
and zext it again.
Now those trunc and the zext are removed.
Assisted-By: Claude
---
llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 4 +-
.../IndVarSimplify/iv-widen-elim-ext.ll | 47 ++++++++-----------
.../LoopFlatten/invalidate-scev-pr203176.ll | 2 +-
3 files changed, 22 insertions(+), 31 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index 77ec651fc286fb..20182200ead6c0 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -2053,8 +2053,8 @@ Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU,
void WidenIV::pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef) {
const SCEV *NarrowSCEV = SE->getSCEV(NarrowDef);
bool NonNegativeDef =
- SE->isKnownPredicate(ICmpInst::ICMP_SGE, NarrowSCEV,
- SE->getZero(NarrowSCEV->getType()));
+ SE->isKnownPredicateAt(ICmpInst::ICMP_SGE, NarrowSCEV,
+ SE->getZero(NarrowSCEV->getType()), NarrowDef);
for (User *U : NarrowDef->users()) {
Instruction *NarrowUser = cast<Instruction>(U);
diff --git a/llvm/test/Transforms/IndVarSimplify/iv-widen-elim-ext.ll b/llvm/test/Transforms/IndVarSimplify/iv-widen-elim-ext.ll
index 2ddb779c9843a3..8505d100bbf267 100644
--- a/llvm/test/Transforms/IndVarSimplify/iv-widen-elim-ext.ll
+++ b/llvm/test/Transforms/IndVarSimplify/iv-widen-elim-ext.ll
@@ -206,7 +206,6 @@ for.end: ; preds = %for.cond.for.end_cr
declare i32 @dummy(ptr, ptr)
-; TODO: the zext can be eliminated.
; %i.02 starts at 0 and increments with nsw. There's the guard M >s 0, so
; %add3 = %i.02 + %M (nsw) is non-negative on every iteration, and so
; zext(%add3) == sext(%add3) == the wide add.
@@ -227,9 +226,7 @@ define i32 @foo3(i32 %M) {
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
; CHECK-NEXT: [[TMP3:%.*]] = add nsw i64 [[INDVARS_IV]], [[TMP0]]
-; CHECK-NEXT: [[TMP4:%.*]] = trunc nsw i64 [[TMP3]] to i32
-; CHECK-NEXT: [[IDXPROM4:%.*]] = zext i32 [[TMP4]] to i64
-; CHECK-NEXT: [[ARRAYIDX5:%.*]] = getelementptr inbounds [100 x i32], ptr @a, i64 0, i64 [[IDXPROM4]]
+; CHECK-NEXT: [[ARRAYIDX5:%.*]] = getelementptr inbounds [100 x i32], ptr @a, i64 0, i64 [[TMP3]]
; CHECK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX5]], align 4
; CHECK-NEXT: br label [[FOR_INC]]
; CHECK: for.inc:
@@ -591,7 +588,8 @@ for.body: ; preds = %for.body.lr.ph, %fo
declare void @use(i64)
-; TODO: %iv - 32 can be computed in i64.
+; TODO: remove trunc and zext of %iv - 32. It is non-negative, so the zext
+; equals the wide add.
; %start >=s 32, %iv steps by one with nsw, so %iv - 32 >=s 0 on every iteration.
define void @offset_sge_guard(i32 %start, i32 %n) {
; CHECK-LABEL: @offset_sge_guard(
@@ -603,8 +601,8 @@ define void @offset_sge_guard(i32 %start, i32 %n) {
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[TMP0]], [[LOOP_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ]
-; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT: [[IDX:%.*]] = add nsw i32 [[TMP1]], -32
+; CHECK-NEXT: [[TMP1:%.*]] = add nsw i64 [[INDVARS_IV]], -32
+; CHECK-NEXT: [[IDX:%.*]] = trunc nsw i64 [[TMP1]] to i32
; CHECK-NEXT: [[IDX_EXT:%.*]] = zext i32 [[IDX]] to i64
; CHECK-NEXT: call void @use(i64 [[IDX_EXT]])
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
@@ -633,7 +631,10 @@ exit:
ret void
}
-; TODO: %iv - 32 can be computed in i64.
+; TODO: remove trunc and zext of %iv - 32. It is non-negative, so the zext
+; equals the wide add.
+; TODO: The exit is rewritten with smax(%start, 9999), and SCEV's constant
+; max loop counts may worsen because of this.
; %start >=s 32, %iv steps by one with nsw, so %iv - 32 >=s 0 on every iteration.
define void @offset_sge_guard_const_exit(i32 %start) {
; CHECK-LABEL: @offset_sge_guard_const_exit(
@@ -642,16 +643,18 @@ define void @offset_sge_guard_const_exit(i32 %start) {
; CHECK-NEXT: br i1 [[C]], label [[LOOP_PREHEADER:%.*]], label [[EXIT:%.*]]
; CHECK: loop.preheader:
; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[START]] to i64
+; CHECK-NEXT: [[SMAX:%.*]] = call i32 @llvm.smax.i32(i32 [[START]], i32 9999)
+; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[SMAX]], 1
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[TMP0]], [[LOOP_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ]
-; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT: [[IDX:%.*]] = add nsw i32 [[TMP1]], -32
+; CHECK-NEXT: [[TMP2:%.*]] = add nsw i64 [[INDVARS_IV]], -32
+; CHECK-NEXT: [[IDX:%.*]] = trunc nsw i64 [[TMP2]] to i32
; CHECK-NEXT: [[IDX_EXT:%.*]] = zext i32 [[IDX]] to i64
; CHECK-NEXT: call void @use(i64 [[IDX_EXT]])
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
-; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT: [[DONE:%.*]] = icmp sgt i32 [[TMP2]], 9998
+; CHECK-NEXT: [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
+; CHECK-NEXT: [[DONE:%.*]] = icmp eq i32 [[LFTR_WIDEIV]], [[TMP1]]
; CHECK-NEXT: br i1 [[DONE]], label [[EXIT_LOOPEXIT:%.*]], label [[LOOP]]
; CHECK: exit.loopexit:
; CHECK-NEXT: br label [[EXIT]]
@@ -675,7 +678,6 @@ exit:
ret void
}
-; TODO: %iv - 32 can be computed in i64.
; %start <u %len and %len >=s 0, so %start >=s 0 and so %iv >=s 0.
; %iv - 32 may be negative, but its zext nneg would be poison in such case.
define void @offset_ult_guard_nonneg_len(i32 %start, i32 %n, ptr %p) {
@@ -689,9 +691,7 @@ define void @offset_ult_guard_nonneg_len(i32 %start, i32 %n, ptr %p) {
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[TMP0]], [[LOOP_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ]
-; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT: [[IDX:%.*]] = add nsw i32 [[TMP1]], -32
-; CHECK-NEXT: [[IDX_EXT:%.*]] = zext nneg i32 [[IDX]] to i64
+; CHECK-NEXT: [[IDX_EXT:%.*]] = add nsw i64 [[INDVARS_IV]], -32
; CHECK-NEXT: call void @use(i64 [[IDX_EXT]])
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[INDVARS_IV_NEXT]] to i32
@@ -722,7 +722,6 @@ exit:
; %start <u %len and %start - 32 <u %len, with %len >=s 0, so
; %start >=s 32 and so %iv - 32 >=s 0 on every iteration.
-; TODO: %iv - 32 can be computed in i64.
define void @offset_ult_guards(ptr %a, i32 %start, i32 %n, ptr %len.p) {
; CHECK-LABEL: @offset_ult_guards(
; CHECK-NEXT: entry:
@@ -737,9 +736,7 @@ define void @offset_ult_guards(ptr %a, i32 %start, i32 %n, ptr %len.p) {
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[TMP0]], [[LOOP_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ]
-; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT: [[IDX:%.*]] = add nsw i32 [[TMP1]], -32
-; CHECK-NEXT: [[IDX_EXT:%.*]] = zext i32 [[IDX]] to i64
+; CHECK-NEXT: [[IDX_EXT:%.*]] = add nsw i64 [[INDVARS_IV]], -32
; CHECK-NEXT: [[SRC:%.*]] = getelementptr i32, ptr [[A:%.*]], i64 [[IDX_EXT]]
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[SRC]], align 4
; CHECK-NEXT: [[DST:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDVARS_IV]]
@@ -778,7 +775,6 @@ exit:
ret void
}
-; TODO: zext(%iv) can be replaced with the wide IV.
; %start <u %len and %len >=s 0, so %start >=s 0, and %iv >=s 0 on every
; iteration. So zext(%iv) equals sext(%iv), i.e. the wide IV.
define void @ext_ult_guard_nonneg_len(i32 %start, i32 %n, ptr %p) {
@@ -794,9 +790,7 @@ define void @ext_ult_guard_nonneg_len(i32 %start, i32 %n, ptr %p) {
; CHECK: loop:
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[TMP0]], [[LOOP_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ]
; CHECK-NEXT: call void @use(i64 [[INDVARS_IV]])
-; CHECK-NEXT: [[TMP2:%.*]] = trunc nsw i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT: [[IV_ZEXT:%.*]] = zext i32 [[TMP2]] to i64
-; CHECK-NEXT: call void @use(i64 [[IV_ZEXT]])
+; CHECK-NEXT: call void @use(i64 [[INDVARS_IV]])
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nsw i64 [[INDVARS_IV]], 1
; CHECK-NEXT: [[DONE:%.*]] = icmp sge i64 [[INDVARS_IV_NEXT]], [[TMP1]]
; CHECK-NEXT: br i1 [[DONE]], label [[EXIT_LOOPEXIT:%.*]], label [[LOOP]]
@@ -939,7 +933,6 @@ exit:
ret void
}
-; TODO: zext(%iv) can be replaced with the wide IV.
; %start >=s 0, %iv increments with nsw, so %iv >=s 0 on
; every iteration.
define void @ext_assume(i32 %start, i32 %n) {
@@ -953,9 +946,7 @@ define void @ext_assume(i32 %start, i32 %n) {
; CHECK: loop:
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
; CHECK-NEXT: call void @use(i64 [[INDVARS_IV]])
-; CHECK-NEXT: [[TMP2:%.*]] = trunc nsw i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT: [[IV_ZEXT:%.*]] = zext i32 [[TMP2]] to i64
-; CHECK-NEXT: call void @use(i64 [[IV_ZEXT]])
+; CHECK-NEXT: call void @use(i64 [[INDVARS_IV]])
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
; CHECK-NEXT: [[DONE:%.*]] = icmp sge i64 [[INDVARS_IV_NEXT]], [[TMP1]]
; CHECK-NEXT: br i1 [[DONE]], label [[EXIT:%.*]], label [[LOOP]]
diff --git a/llvm/test/Transforms/LoopFlatten/invalidate-scev-pr203176.ll b/llvm/test/Transforms/LoopFlatten/invalidate-scev-pr203176.ll
index e80a811d983359..9f450b6b31a95c 100644
--- a/llvm/test/Transforms/LoopFlatten/invalidate-scev-pr203176.ll
+++ b/llvm/test/Transforms/LoopFlatten/invalidate-scev-pr203176.ll
@@ -30,7 +30,7 @@ define i64 @f(i64 %n) {
; CHECK-NEXT: --> {0,+,{0,+,1}<%outer.loop>}<nuw><nsw><%mid.loop> U: [0,1) S: [0,1) Exits: {0,+,{0,+,1}<%outer.loop>}<nuw><nsw><%mid.loop> LoopDispositions: { %inner.loop: Invariant, %mid.loop: Computable, %outer.loop: Variant }
; CHECK-NEXT: %gep = getelementptr i32, ptr @g, i32 %3
; CHECK-NEXT: --> {@g,+,(4 * (sext i32 {0,+,1}<%outer.loop> to i64))<nsw>}<nw><%mid.loop> U: [4,-7) S: [-9223372036854775808,9223372036854775805) Exits: {@g,+,(4 * (sext i32 {0,+,1}<%outer.loop> to i64))<nsw>}<nuw><nsw><%mid.loop> LoopDispositions: { %inner.loop: Invariant, %mid.loop: Computable, %outer.loop: Variant }
-; CHECK-NEXT: %4 = trunc nuw i64 %indvar to i32
+; CHECK-NEXT: %4 = trunc nuw nsw i64 %indvar to i32
; CHECK-NEXT: --> {0,+,1}<nuw><%inner.loop> U: [0,-1) S: [0,-1) Exits: {-1,+,1}<%outer.loop> LoopDispositions: { %inner.loop: Computable, %mid.loop: Uniform, %outer.loop: Uniform }
; CHECK-NEXT: %arrayidx = getelementptr i32, ptr %gep, i32 %4
; CHECK-NEXT: --> ((4 * (sext i32 {0,+,1}<nuw><%inner.loop> to i64))<nsw> + {@g,+,(4 * (sext i32 {0,+,1}<%outer.loop> to i64))<nsw>}<nuw><nsw><%mid.loop>) U: [0,-3) S: [-9223372036854775808,9223372036854775805) Exits: {((4 * (sext i32 {-1,+,1}<%outer.loop> to i64))<nsw> + @g),+,(4 * (sext i32 {0,+,1}<%outer.loop> to i64))<nsw>}<nw><%mid.loop> LoopDispositions: { %inner.loop: Computable, %mid.loop: Variant, %outer.loop: Variant }
More information about the llvm-branch-commits
mailing list