[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
Wed Oct 28 09:03:41 PDT 2026
https://github.com/d-makogon created https://github.com/llvm/llvm-project/pull/236461
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 widens IV's users, they can choose to use either the sext or zext of the IV.
Previously, we didn't use context to prove the non-negativity of def.
Now ask SCEV about def non-negativity at its context. This allows it to infer facts from the dominating guards and assumes.
>From 2a266aa53d4806151dbf3ed07719be2d0695f41e 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 widens IV's
users, they can use both the sext and zext of it.
Now ask SCEV about def non-negativity using its context.
This allows it to infer facts from the dominating guards and assumes.
Assisted-By: Claude
---
llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 4 +-
.../IndVarSimplify/iv-widen-elim-ext.ll | 5 +--
.../widen-iv-nonneg-from-guard.ll | 37 +++++++++----------
3 files changed, 20 insertions(+), 26 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index ccf550e45ddfe5..d3079122217bda 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -2051,8 +2051,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 fff2976d791e97..9cd6f0edfa563c 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
@@ -228,9 +227,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:
diff --git a/llvm/test/Transforms/IndVarSimplify/widen-iv-nonneg-from-guard.ll b/llvm/test/Transforms/IndVarSimplify/widen-iv-nonneg-from-guard.ll
index 0b9fb47b9cac33..84b17a6d05982f 100644
--- a/llvm/test/Transforms/IndVarSimplify/widen-iv-nonneg-from-guard.ll
+++ b/llvm/test/Transforms/IndVarSimplify/widen-iv-nonneg-from-guard.ll
@@ -8,7 +8,8 @@ target datalayout = "e-m:e-i64:64-n8:16:32:64-S128"
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: define void @offset_sge_guard(
@@ -21,8 +22,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
@@ -49,7 +50,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 the constant max
+; backedge-taken count of the loop gets worse.
; %start >=s 32, %iv steps by one with nsw, and the loop exits after %iv
; reaches 9999, so %iv - 32 >=s 0 on every iteration.
define void @offset_sge_guard_const_exit(i32 %start) {
@@ -60,16 +64,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]]
@@ -91,7 +97,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 then the zext nneg is poison.
define void @offset_ult_guard_nonneg_len(i32 %start, i32 %n, ptr %p) {
@@ -106,9 +111,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
@@ -170,7 +173,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) {
@@ -187,9 +189,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]]
@@ -292,7 +292,6 @@ exit:
ret void
}
-; TODO: zext(%iv) can be replaced with the wide IV.
; %start >=s 0 from the assume, and %iv steps by one with nsw, so %iv >=s 0 on
; every iteration.
define void @ext_assume(i32 %start, i32 %n) {
@@ -307,9 +306,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]]
More information about the llvm-branch-commits
mailing list