[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
Fri Oct 30 04:42:43 PDT 2026


https://github.com/d-makogon updated https://github.com/llvm/llvm-project/pull/236461

>From c0d231b44aa2a2180d0c5e23b5a857707cc20f99 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 ++++++++-----------
 2 files changed, 21 insertions(+), 30 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 c6161c89485de6..04818e70eb8175 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:
@@ -593,7 +590,8 @@ for.body:                                         ; preds = %for.body.lr.ph, %fo
 declare void @use(i64)
 declare void @llvm.assume(i1)
 
-; 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(
@@ -605,8 +603,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 the constant max
+; backedge-taken count 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]]
@@ -673,7 +676,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) {
@@ -687,9 +689,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
@@ -718,7 +718,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(i32 %start, i32 %n, ptr %len.p) {
 ; CHECK-LABEL: @offset_ult_guards(
 ; CHECK-NEXT:  entry:
@@ -733,9 +732,7 @@ define void @offset_ult_guards(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 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:    call void @use(i64 [[INDVARS_IV]])
 ; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
@@ -801,7 +798,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) {
@@ -817,9 +813,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]]
@@ -921,7 +915,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) {
@@ -935,9 +928,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