[llvm] [indvars] Update SCEV after eliminating CmpInst of loop latch (PR #207044)
Hassnaa Hamdi via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 1 13:01:31 PDT 2026
https://github.com/hassnaaHamdi updated https://github.com/llvm/llvm-project/pull/207044
>From 9a4081d1cb7def87efafbea41a6617d4e2b40c8e Mon Sep 17 00:00:00 2001
From: Hassnaa Hamdi <hassnaa.hamdi at arm.com>
Date: Wed, 1 Jul 2026 14:43:23 +0000
Subject: [PATCH 1/4] [indvars] pre-commit test case
---
.../IndVarSimplify/eliminate-comparison.ll | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll b/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll
index 08f9856ac603d..13e61431dff8d 100644
--- a/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll
+++ b/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll
@@ -1148,4 +1148,36 @@ exit:
ret i32 %iv
}
+define i64 @combined_bridge() {
+; CHECK-LABEL: @combined_bridge(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[INNER1_PH_I:%.*]]
+; CHECK: inner1.ph.i:
+; CHECK-NEXT: br i1 false, label [[INNER2_PH_I_PREHEADER:%.*]], label [[INNER1_PH_I]]
+; CHECK: inner2.ph.i.preheader:
+; CHECK-NEXT: br label [[INNER2_PH_I:%.*]]
+; CHECK: inner2.ph.i:
+; CHECK-NEXT: br i1 false, label [[OUTER2_EXIT_I:%.*]], label [[INNER2_PH_I]]
+; CHECK: outer2.exit.i:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ br label %inner1.ph.i
+
+inner1.ph.i: ; preds = %inner1.ph.i, %entry
+ %iv74.i = phi i64 [ 0, %entry ], [ %iv.next75.i, %inner1.ph.i ]
+ %iv.next75.i = add nsw i64 %iv74.i, 1
+ %exitcond77.i = icmp eq i64 %iv.next75.i, 0
+ br i1 %exitcond77.i, label %inner2.ph.i, label %inner1.ph.i
+
+inner2.ph.i: ; preds = %inner2.ph.i, %inner1.ph.i
+ %iv66.i = phi i64 [ %iv.next67.i, %inner2.ph.i ], [ 0, %inner1.ph.i ]
+ %iv.next67.i = add nsw i64 %iv66.i, 1
+ %exitcond69.i = icmp eq i64 %iv.next67.i, 0
+ br i1 %exitcond69.i, label %outer2.exit.i, label %inner2.ph.i
+
+outer2.exit.i: ; preds = %inner2.ph.i
+ ret i64 0
+}
+
!0 = !{i32 0, i32 2147483647}
>From 21d5ce265c196c4d3ce4a6e4362bc2bbc4440df4 Mon Sep 17 00:00:00 2001
From: Hassnaa Hamdi <hassnaa.hamdi at arm.com>
Date: Wed, 1 Jul 2026 14:46:42 +0000
Subject: [PATCH 2/4] running a pass after indvars causes crash
---
llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll | 1 +
1 file changed, 1 insertion(+)
diff --git a/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll b/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll
index 13e61431dff8d..ba13b9d45e343 100644
--- a/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll
+++ b/llvm/test/Transforms/IndVarSimplify/eliminate-comparison.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=indvars -S < %s | FileCheck %s
+; RUN: opt -passes="loop(indvars),loop-fusion" -disable-output -S < %s
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
>From 1453629a565ff99d41feda3a4ddac0600dea3595 Mon Sep 17 00:00:00 2001
From: Hassnaa Hamdi <hassnaa.hamdi at arm.com>
Date: Wed, 1 Jul 2026 17:56:59 +0000
Subject: [PATCH 3/4] [indvars] Update SCEV after eliminating CmpInst of loop
latch
---
llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 5 +++++
.../IndVarSimplify/invalidate-modified-lcssa-phi.ll | 2 +-
llvm/test/Transforms/IndVarSimplify/pr55689.ll | 1 -
3 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index 9e0bbe9cf3b62..88a124f43b34c 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -266,6 +266,9 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop);
const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop);
+ bool IsExitCond = false;
+ if (ICmp == L->getLatchCmpInst())
+ IsExitCond = true;
// If the condition is always true or always false in the given context,
// replace it with a constant value.
SmallVector<Instruction *, 4> Users;
@@ -276,6 +279,8 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
SE->forgetValue(ICmp);
ICmp->replaceAllUsesWith(ConstantInt::getBool(ICmp->getContext(), *Ev));
DeadInsts.emplace_back(ICmp);
+ if (IsExitCond)
+ SE->forgetLoop(L);
LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
++NumElimCmp;
Changed = true;
diff --git a/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll b/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
index 72c292a5f2bcf..e8a3dcbc22286 100644
--- a/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
+++ b/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
@@ -170,7 +170,7 @@ define i32 @pr58750(i16 %a, ptr %dst, i1 %c.0) {
; CHECK-NEXT: br label [[INNER:%.*]]
; CHECK: inner:
; CHECK-NEXT: store i16 0, ptr [[DST:%.*]], align 1
-; CHECK-NEXT: br i1 false, label [[INNER]], label [[OUTER_LATCH]]
+; CHECK-NEXT: br label [[OUTER_LATCH]]
; CHECK: outer.latch:
; CHECK-NEXT: [[LCSSA]] = phi i32 [ [[XOR]], [[INNER]] ]
; CHECK-NEXT: br i1 [[C_0:%.*]], label [[OUTER_HEADER]], label [[EXIT:%.*]]
diff --git a/llvm/test/Transforms/IndVarSimplify/pr55689.ll b/llvm/test/Transforms/IndVarSimplify/pr55689.ll
index c5565af7f1671..e77cc1748d2ae 100644
--- a/llvm/test/Transforms/IndVarSimplify/pr55689.ll
+++ b/llvm/test/Transforms/IndVarSimplify/pr55689.ll
@@ -1,7 +1,6 @@
; RUN: opt -S -passes='loop(indvars)' -verify-scev < %s | FileCheck %s
; REQUIRES: asserts
-; XFAIL: *
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
target triple = "x86_64-unknown-linux-gnu"
>From d649b13d93bd4c929cffa8e0766d5881ce20d5f2 Mon Sep 17 00:00:00 2001
From: Hassnaa Hamdi <hassnaa.hamdi at arm.com>
Date: Wed, 1 Jul 2026 19:43:48 +0000
Subject: [PATCH 4/4] update affected tests
---
llvm/test/Transforms/LoopDeletion/pr53969.ll | 42 ++++++++++++++++---
llvm/test/Transforms/LoopUnroll/scevunroll.ll | 2 +-
2 files changed, 37 insertions(+), 7 deletions(-)
diff --git a/llvm/test/Transforms/LoopDeletion/pr53969.ll b/llvm/test/Transforms/LoopDeletion/pr53969.ll
index 45c7ee2aecbbe..a6e5998355939 100644
--- a/llvm/test/Transforms/LoopDeletion/pr53969.ll
+++ b/llvm/test/Transforms/LoopDeletion/pr53969.ll
@@ -10,23 +10,48 @@ define void @test() {
; CHECK-NEXT: bb:
; CHECK-NEXT: br label [[BB1:%.*]]
; CHECK: bb1:
+; CHECK-NEXT: [[TMP:%.*]] = phi i32 [ undef, [[BB:%.*]] ]
+; CHECK-NEXT: [[TMP2:%.*]] = phi i32 [ 11, [[BB]] ]
+; CHECK-NEXT: [[TMP3:%.*]] = add nsw i32 112, -1
+; CHECK-NEXT: [[TMP4:%.*]] = add nuw nsw i32 [[TMP2]], 1
+; CHECK-NEXT: [[TMP5:%.*]] = mul i32 [[TMP3]], [[TMP3]]
+; CHECK-NEXT: [[TMP6:%.*]] = mul nsw i32 [[TMP2]], -6
+; CHECK-NEXT: [[TMP7:%.*]] = mul i32 [[TMP6]], [[TMP5]]
+; CHECK-NEXT: [[TMP8:%.*]] = add i32 [[TMP7]], [[TMP2]]
; CHECK-NEXT: [[TMP9:%.*]] = and i32 undef, 1
; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i32 [[TMP9]], 0
; CHECK-NEXT: br i1 [[TMP10]], label [[BB33_LOOPEXIT1:%.*]], label [[BB34_PREHEADER:%.*]]
; CHECK: bb34.preheader:
+; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[TMP]], 2
; CHECK-NEXT: br label [[BB34:%.*]]
; CHECK: bb11:
; CHECK-NEXT: [[TMP2_LCSSA12:%.*]] = phi i32 [ 11, [[BB34]] ]
; CHECK-NEXT: br label [[BB33_LOOPEXIT:%.*]]
; CHECK: bb12:
+; CHECK-NEXT: [[TMP41_LCSSA:%.*]] = phi i64 [ [[TMP41:%.*]], [[BB34]] ]
+; CHECK-NEXT: [[TMP13:%.*]] = icmp eq ptr addrspace(1) undef, null
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[TMP]] to i1
+; CHECK-NEXT: [[TMP2:%.*]] = add i1 [[TMP1]], true
; CHECK-NEXT: br label [[BB14:%.*]]
; CHECK: bb14:
-; CHECK-NEXT: br i1 true, label [[BB32:%.*]], label [[BB22:%.*]]
+; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 4, [[BB12:%.*]] ]
+; CHECK-NEXT: [[TMP15:%.*]] = phi i32 [ [[TMP0]], [[BB12]] ]
+; CHECK-NEXT: [[TMP16:%.*]] = phi i64 [ [[TMP41_LCSSA]], [[BB12]] ]
+; CHECK-NEXT: [[TMP18:%.*]] = add i64 [[TMP16]], undef
+; CHECK-NEXT: [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV]] to i1
+; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i1 [[LFTR_WIDEIV]], [[TMP2]]
+; CHECK-NEXT: br i1 [[EXITCOND]], label [[BB32:%.*]], label [[BB22:%.*]]
; CHECK: bb22:
-; CHECK-NEXT: br i1 false, label [[BB42:%.*]], label [[BB25:%.*]]
+; CHECK-NEXT: [[TMP3:%.*]] = trunc nuw nsw i64 [[INDVARS_IV]] to i32
+; CHECK-NEXT: [[TMP23:%.*]] = or i32 [[TMP3]], undef
+; CHECK-NEXT: [[TMP24:%.*]] = add i32 [[TMP23]], undef
+; CHECK-NEXT: br i1 [[TMP13]], label [[BB42:%.*]], label [[BB25:%.*]]
; CHECK: bb25:
+; CHECK-NEXT: [[INDVARS_IV_NEXT:%.*]] = add nuw nsw i64 [[INDVARS_IV]], 1
+; CHECK-NEXT: [[TMP29:%.*]] = add i32 [[TMP15]], 3
; CHECK-NEXT: br label [[BB31:%.*]]
; CHECK: bb31:
+; CHECK-NEXT: [[TMP29_LCSSA:%.*]] = phi i32 [ [[TMP29]], [[BB25]] ]
; CHECK-NEXT: unreachable
; CHECK: bb32:
; CHECK-NEXT: ret void
@@ -34,17 +59,22 @@ define void @test() {
; CHECK-NEXT: [[TMP2_LCSSA9:%.*]] = phi i32 [ [[TMP2_LCSSA12]], [[BB11:%.*]] ]
; CHECK-NEXT: br label [[BB33:%.*]]
; CHECK: bb33.loopexit1:
-; CHECK-NEXT: [[TMP2_LCSSA:%.*]] = phi i32 [ 11, [[BB1]] ]
+; CHECK-NEXT: [[TMP2_LCSSA:%.*]] = phi i32 [ [[TMP2]], [[BB1]] ]
; CHECK-NEXT: br label [[BB33]]
; CHECK: bb33:
; CHECK-NEXT: [[TMP210:%.*]] = phi i32 [ [[TMP2_LCSSA]], [[BB33_LOOPEXIT1]] ], [ [[TMP2_LCSSA9]], [[BB33_LOOPEXIT]] ]
; CHECK-NEXT: call void @use(i32 [[TMP210]])
; CHECK-NEXT: ret void
; CHECK: bb34:
-; CHECK-NEXT: br i1 false, label [[BB11]], label [[BB12:%.*]]
+; CHECK-NEXT: [[TMP36:%.*]] = xor i32 0, [[TMP8]]
+; CHECK-NEXT: [[TMP38:%.*]] = add i32 [[TMP36]], undef
+; CHECK-NEXT: [[TMP39:%.*]] = add i32 [[TMP38]], undef
+; CHECK-NEXT: [[TMP40:%.*]] = sext i32 [[TMP39]] to i64
+; CHECK-NEXT: [[TMP41]] = add i64 undef, [[TMP40]]
+; CHECK-NEXT: br i1 false, label [[BB11]], label [[BB12]]
; CHECK: bb42:
-; CHECK-NEXT: [[TMP24_LCSSA:%.*]] = phi i32 [ undef, [[BB22]] ]
-; CHECK-NEXT: [[TMP18_LCSSA4:%.*]] = phi i64 [ undef, [[BB22]] ]
+; CHECK-NEXT: [[TMP24_LCSSA:%.*]] = phi i32 [ [[TMP24]], [[BB22]] ]
+; CHECK-NEXT: [[TMP18_LCSSA4:%.*]] = phi i64 [ [[TMP18]], [[BB22]] ]
; CHECK-NEXT: store atomic i64 [[TMP18_LCSSA4]], ptr addrspace(1) undef unordered, align 8
; CHECK-NEXT: call void @use(i32 [[TMP24_LCSSA]])
; CHECK-NEXT: ret void
diff --git a/llvm/test/Transforms/LoopUnroll/scevunroll.ll b/llvm/test/Transforms/LoopUnroll/scevunroll.ll
index 5375524f35237..eae7f58354081 100644
--- a/llvm/test/Transforms/LoopUnroll/scevunroll.ll
+++ b/llvm/test/Transforms/LoopUnroll/scevunroll.ll
@@ -320,7 +320,7 @@ define void @nsw_latch(ptr %a) nounwind {
; CHECK: for.cond.1:
; CHECK-NEXT: br label [[RETURN]]
; CHECK: return:
-; CHECK-NEXT: [[B_03_LCSSA:%.*]] = phi i32 [ 0, [[FOR_COND_1]] ], [ 0, [[FOR_COND]] ], [ 8, [[FOR_BODY_1]] ]
+; CHECK-NEXT: [[B_03_LCSSA:%.*]] = phi i32 [ 8, [[FOR_COND_1]] ], [ 8, [[FOR_COND]] ], [ 8, [[FOR_BODY_1]] ]
; CHECK-NEXT: [[RETVAL_0:%.*]] = phi i32 [ 0, [[FOR_COND_1]] ], [ 0, [[FOR_COND]] ], [ 1, [[FOR_BODY_1]] ]
; CHECK-NEXT: store i32 [[B_03_LCSSA]], ptr [[A:%.*]], align 4
; CHECK-NEXT: ret void
More information about the llvm-commits
mailing list