[llvm] [IndVarSimplify] Skip exit predication past divergent effects (#183906) (PR #194202)
Jake Xue via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 13:19:40 PDT 2026
https://github.com/Jakexxh updated https://github.com/llvm/llvm-project/pull/194202
>From 8bd6bfe9ba71f2d9ba404d6affeb138b4d541bfb Mon Sep 17 00:00:00 2001
From: Xiaohe Jake Xue <jakexxh at duck.com>
Date: Sat, 25 Apr 2026 16:16:29 -0400
Subject: [PATCH 1/2] [LoopInfo] Extract isLoopNestFinite helper for
LoopDeletion and IndVarSimplify
predicateLoopExits in IndVarSimplify folds an exiting branch to a
loop-invariant condition derived from the outer loop's BTC. When a
sub-loop or an irreducible cycle may not terminate, this drops an
observable infinite loop.
Extract LoopDeletion's check for this into llvm::isLoopNestFinite and
use it in both passes. Rather than trusting mustprogress alone, the
helper relies on ScalarEvolution::loopIsFiniteByAssumption (willreturn,
or mustprogress without side effects), which is exactly when an infinite
loop is UB: a mustprogress loop with side effects may legally run
forever.
LoopDeletion keeps its explicit mustprogress check until the next
commit, because SCEV currently counts llvm.assume as a side effect.
Fixes #183906.
Assisted-by: Claude Code
---
llvm/include/llvm/Analysis/LoopInfo.h | 10 +
llvm/lib/Analysis/LoopInfo.cpp | 28 ++
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 5 +
llvm/lib/Transforms/Scalar/LoopDeletion.cpp | 36 +--
.../skip-predication-infinite-subloop.ll | 241 ++++++++++++++++++
.../LoopDeletion/loop-nest-finite.ll | 111 ++++++++
6 files changed, 404 insertions(+), 27 deletions(-)
create mode 100644 llvm/test/Transforms/IndVarSimplify/skip-predication-infinite-subloop.ll
create mode 100644 llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll
diff --git a/llvm/include/llvm/Analysis/LoopInfo.h b/llvm/include/llvm/Analysis/LoopInfo.h
index 64caab77893e6..f19e2089bb4c0 100644
--- a/llvm/include/llvm/Analysis/LoopInfo.h
+++ b/llvm/include/llvm/Analysis/LoopInfo.h
@@ -693,6 +693,16 @@ LLVM_ABI bool isMustProgress(const Loop *L);
/// iterations.
LLVM_ABI bool isFinite(const Loop *L);
+/// Return true if every cycle in the loop nest rooted at \p L is finite.
+/// This holds if \p L is finite by assumption, or if the nest has no
+/// irreducible cycles and every loop is either finite by assumption or
+/// has a computable constant max backedge-taken count.
+///
+/// This does not guarantee termination, as a call within the nest may
+/// never return.
+LLVM_ABI bool isLoopNestFinite(Loop *L, ScalarEvolution &SE,
+ const LoopInfo &LI);
+
/// Return whether an MDNode might represent an access group.
///
/// Access group metadata nodes have to be distinct and empty. Being
diff --git a/llvm/lib/Analysis/LoopInfo.cpp b/llvm/lib/Analysis/LoopInfo.cpp
index 613e9887f3c45..f9d0dbc8c1a52 100644
--- a/llvm/lib/Analysis/LoopInfo.cpp
+++ b/llvm/lib/Analysis/LoopInfo.cpp
@@ -16,6 +16,7 @@
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/IVDescriptors.h"
#include "llvm/Analysis/LoopIterator.h"
#include "llvm/Analysis/LoopNestAnalysis.h"
@@ -1219,6 +1220,33 @@ bool llvm::isFinite(const Loop *L) {
return L->getHeader()->getParent()->willReturn();
}
+bool llvm::isLoopNestFinite(Loop *L, ScalarEvolution &SE, const LoopInfo &LI) {
+ // An assumption on L covers every cycle inside it.
+ if (SE.loopIsFiniteByAssumption(L))
+ return true;
+
+ // An irreducible cycle has no Loop to prove finite.
+ LoopBlocksRPO RPOT(L);
+ RPOT.perform(&LI);
+ if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))
+ return false;
+
+ // Likewise, a sub-loop that is finite by assumption covers its own sub-loops.
+ if (isa<SCEVCouldNotCompute>(SE.getConstantMaxBackedgeTakenCount(L)))
+ return false;
+ SmallVector<Loop *, 8> WorkList(L->begin(), L->end());
+ while (!WorkList.empty()) {
+ Loop *Current = WorkList.pop_back_val();
+ if (SE.loopIsFiniteByAssumption(Current))
+ continue;
+ if (isa<SCEVCouldNotCompute>(SE.getConstantMaxBackedgeTakenCount(Current)))
+ return false;
+ WorkList.append(Current->begin(), Current->end());
+ }
+
+ return true;
+}
+
static const char *LLVMLoopMustProgress = "llvm.loop.mustprogress";
bool llvm::hasMustProgress(const Loop *L) {
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index ab75081c7108c..888d9416dfb74 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -1915,6 +1915,11 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
if (ExitingBlocks.empty())
return false;
+ // Predication may skip iterations of L, which is only safe if none of them
+ // would run forever. Calls that may not return are rejected below.
+ if (!isLoopNestFinite(L, *SE, *LI))
+ return false;
+
// At this point, ExitingBlocks consists of only those blocks which are
// predicatable. Given that, we know we have at least one exit we can
// predicate if the loop is doesn't have side effects and doesn't have any
diff --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
index 2dc696edc5acc..b750a4deaa3d9 100644
--- a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
@@ -19,6 +19,7 @@
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/InstructionSimplify.h"
+#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopIterator.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/MemorySSA.h"
@@ -105,36 +106,17 @@ static bool isLoopDead(Loop *L, ScalarEvolution &SE,
}))
return false;
- // The loop or any of its sub-loops looping infinitely is legal. The loop can
- // only be considered dead if either
- // a. the function is mustprogress.
- // b. all (sub-)loops are mustprogress or have a known trip-count.
+ // The loop can only be considered dead if either:
+ // a. the function is mustprogress; or
+ // b. every loop in the nest is known to terminate.
+ // isLoopNestFinite also honors a mustprogress function, but only when the
+ // loop has no side effects in SCEV's sense, and SCEV counts droppable
+ // instructions (e.g. llvm.assume) as side effects. Check the attribute
+ // directly so that such loops can still be deleted.
if (L->getHeader()->getParent()->mustProgress())
return true;
- LoopBlocksRPO RPOT(L);
- RPOT.perform(&LI);
- // If the loop contains an irreducible cycle, it may loop infinitely.
- if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))
- return false;
-
- SmallVector<Loop *, 8> WorkList;
- WorkList.push_back(L);
- while (!WorkList.empty()) {
- Loop *Current = WorkList.pop_back_val();
- if (hasMustProgress(Current))
- continue;
-
- const SCEV *S = SE.getConstantMaxBackedgeTakenCount(Current);
- if (isa<SCEVCouldNotCompute>(S)) {
- LLVM_DEBUG(
- dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was "
- "not required to make progress.\n");
- return false;
- }
- WorkList.append(Current->begin(), Current->end());
- }
- return true;
+ return isLoopNestFinite(L, SE, LI);
}
/// This function returns true if there is no viable path from the
diff --git a/llvm/test/Transforms/IndVarSimplify/skip-predication-infinite-subloop.ll b/llvm/test/Transforms/IndVarSimplify/skip-predication-infinite-subloop.ll
new file mode 100644
index 0000000000000..d7a69922a06f9
--- /dev/null
+++ b/llvm/test/Transforms/IndVarSimplify/skip-predication-infinite-subloop.ll
@@ -0,0 +1,241 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=indvars -indvars-predicate-loops=1 -S | FileCheck %s
+
+; Predication must be skipped if an observable infinite loop may exist in the
+; loop nest.
+define void @infinite_subloop_skips_predication() {
+; CHECK-LABEL: @infinite_subloop_skips_predication(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
+; CHECK: outer.header:
+; CHECK-NEXT: [[IV:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[OUTER_LATCH:%.*]] ]
+; CHECK-NEXT: br label [[INNER_HEADER:%.*]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+; CHECK: inner.header:
+; CHECK-NEXT: [[INNER_COND:%.*]] = icmp eq i8 [[IV]], 1
+; CHECK-NEXT: br i1 [[INNER_COND]], label [[INNER_HEADER]], label [[OUTER_LATCH]]
+; CHECK: outer.latch:
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
+; CHECK-NEXT: [[OUTER_COND:%.*]] = icmp eq i8 [[IV_NEXT]], -1
+; CHECK-NEXT: br i1 [[OUTER_COND]], label [[EXIT:%.*]], label [[OUTER_HEADER]]
+;
+entry:
+ br label %outer.header
+
+outer.header:
+ %iv = phi i8 [ 0, %entry ], [ %iv.next, %outer.latch ]
+ br label %inner.header
+
+exit:
+ ret void
+
+inner.header:
+ %inner.cond = icmp eq i8 %iv, 1
+ br i1 %inner.cond, label %inner.header, label %outer.latch
+
+outer.latch:
+ %iv.next = add nuw i8 %iv, 1
+ %outer.cond = icmp eq i8 %iv.next, -1
+ br i1 %outer.cond, label %exit, label %outer.header
+}
+
+; !llvm.loop.mustprogress makes the side-effect-free inner loop finite.
+define void @mustprogress_subloop_still_predicated() {
+; CHECK-LABEL: @mustprogress_subloop_still_predicated(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
+; CHECK: outer.header:
+; CHECK-NEXT: [[IV:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[OUTER_LATCH:%.*]] ]
+; CHECK-NEXT: br label [[INNER_HEADER:%.*]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+; CHECK: inner.header:
+; CHECK-NEXT: [[INNER_COND:%.*]] = icmp eq i8 [[IV]], 1
+; CHECK-NEXT: br i1 [[INNER_COND]], label [[INNER_HEADER]], label [[OUTER_LATCH]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK: outer.latch:
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
+; CHECK-NEXT: br i1 true, label [[EXIT:%.*]], label [[OUTER_HEADER]]
+;
+entry:
+ br label %outer.header
+
+outer.header:
+ %iv = phi i8 [ 0, %entry ], [ %iv.next, %outer.latch ]
+ br label %inner.header
+
+exit:
+ ret void
+
+inner.header:
+ %inner.cond = icmp eq i8 %iv, 1
+ br i1 %inner.cond, label %inner.header, label %outer.latch, !llvm.loop !0
+
+outer.latch:
+ %iv.next = add nuw i8 %iv, 1
+ %outer.cond = icmp eq i8 %iv.next, -1
+ br i1 %outer.cond, label %exit, label %outer.header
+}
+
+; A mustprogress function makes the side-effect-free nest finite.
+define void @mustprogress_function_subloop_predicated() mustprogress {
+; CHECK-LABEL: @mustprogress_function_subloop_predicated(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
+; CHECK: outer.header:
+; CHECK-NEXT: [[IV:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[OUTER_LATCH:%.*]] ]
+; CHECK-NEXT: br label [[INNER_HEADER:%.*]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+; CHECK: inner.header:
+; CHECK-NEXT: [[INNER_COND:%.*]] = icmp eq i8 [[IV]], 1
+; CHECK-NEXT: br i1 [[INNER_COND]], label [[INNER_HEADER]], label [[OUTER_LATCH]]
+; CHECK: outer.latch:
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
+; CHECK-NEXT: br i1 true, label [[EXIT:%.*]], label [[OUTER_HEADER]]
+;
+entry:
+ br label %outer.header
+
+outer.header:
+ %iv = phi i8 [ 0, %entry ], [ %iv.next, %outer.latch ]
+ br label %inner.header
+
+exit:
+ ret void
+
+inner.header:
+ %inner.cond = icmp eq i8 %iv, 1
+ br i1 %inner.cond, label %inner.header, label %outer.latch
+
+outer.latch:
+ %iv.next = add nuw i8 %iv, 1
+ %outer.cond = icmp eq i8 %iv.next, -1
+ br i1 %outer.cond, label %exit, label %outer.header
+}
+
+; Likewise for willreturn.
+define void @willreturn_function_subloop_predicated() willreturn {
+; CHECK-LABEL: @willreturn_function_subloop_predicated(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
+; CHECK: outer.header:
+; CHECK-NEXT: [[IV:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[OUTER_LATCH:%.*]] ]
+; CHECK-NEXT: br label [[INNER_HEADER:%.*]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+; CHECK: inner.header:
+; CHECK-NEXT: [[INNER_COND:%.*]] = icmp eq i8 [[IV]], 1
+; CHECK-NEXT: br i1 [[INNER_COND]], label [[INNER_HEADER]], label [[OUTER_LATCH]]
+; CHECK: outer.latch:
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
+; CHECK-NEXT: br i1 true, label [[EXIT:%.*]], label [[OUTER_HEADER]]
+;
+entry:
+ br label %outer.header
+
+outer.header:
+ %iv = phi i8 [ 0, %entry ], [ %iv.next, %outer.latch ]
+ br label %inner.header
+
+exit:
+ ret void
+
+inner.header:
+ %inner.cond = icmp eq i8 %iv, 1
+ br i1 %inner.cond, label %inner.header, label %outer.latch
+
+outer.latch:
+ %iv.next = add nuw i8 %iv, 1
+ %outer.cond = icmp eq i8 %iv.next, -1
+ br i1 %outer.cond, label %exit, label %outer.header
+}
+
+; The irreducible cycle may run forever (iv == 1), so predication is skipped.
+define void @irreducible_cycle_skips_predication() {
+; CHECK-LABEL: @irreducible_cycle_skips_predication(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
+; CHECK: outer.header:
+; CHECK-NEXT: [[IV:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[OUTER_LATCH:%.*]] ]
+; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[IV]], 1
+; CHECK-NEXT: br i1 [[C]], label [[IRR_A:%.*]], label [[IRR_B:%.*]]
+; CHECK: irr.a:
+; CHECK-NEXT: br i1 [[C]], label [[IRR_B]], label [[OUTER_LATCH]]
+; CHECK: irr.b:
+; CHECK-NEXT: br i1 [[C]], label [[IRR_A]], label [[OUTER_LATCH]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+; CHECK: outer.latch:
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
+; CHECK-NEXT: [[OUTER_COND:%.*]] = icmp eq i8 [[IV_NEXT]], -1
+; CHECK-NEXT: br i1 [[OUTER_COND]], label [[EXIT:%.*]], label [[OUTER_HEADER]]
+;
+entry:
+ br label %outer.header
+
+outer.header:
+ %iv = phi i8 [ 0, %entry ], [ %iv.next, %outer.latch ]
+ %c = icmp eq i8 %iv, 1
+ br i1 %c, label %irr.a, label %irr.b
+
+irr.a:
+ br i1 %c, label %irr.b, label %outer.latch
+
+irr.b:
+ br i1 %c, label %irr.a, label %outer.latch
+
+exit:
+ ret void
+
+outer.latch:
+ %iv.next = add nuw i8 %iv, 1
+ %outer.cond = icmp eq i8 %iv.next, -1
+ br i1 %outer.cond, label %exit, label %outer.header
+}
+
+; !llvm.loop.mustprogress on the outer loop also covers the irreducible cycle.
+define void @mustprogress_outer_irreducible_cycle_predicated() {
+; CHECK-LABEL: @mustprogress_outer_irreducible_cycle_predicated(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
+; CHECK: outer.header:
+; CHECK-NEXT: [[IV:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[OUTER_LATCH:%.*]] ]
+; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[IV]], 1
+; CHECK-NEXT: br i1 [[C]], label [[IRR_A:%.*]], label [[IRR_B:%.*]]
+; CHECK: irr.a:
+; CHECK-NEXT: br i1 [[C]], label [[IRR_B]], label [[OUTER_LATCH]]
+; CHECK: irr.b:
+; CHECK-NEXT: br i1 [[C]], label [[IRR_A]], label [[OUTER_LATCH]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+; CHECK: outer.latch:
+; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
+; CHECK-NEXT: br i1 true, label [[EXIT:%.*]], label [[OUTER_HEADER]], !llvm.loop [[LOOP2:![0-9]+]]
+;
+entry:
+ br label %outer.header
+
+outer.header:
+ %iv = phi i8 [ 0, %entry ], [ %iv.next, %outer.latch ]
+ %c = icmp eq i8 %iv, 1
+ br i1 %c, label %irr.a, label %irr.b
+
+irr.a:
+ br i1 %c, label %irr.b, label %outer.latch
+
+irr.b:
+ br i1 %c, label %irr.a, label %outer.latch
+
+exit:
+ ret void
+
+outer.latch:
+ %iv.next = add nuw i8 %iv, 1
+ %outer.cond = icmp eq i8 %iv.next, -1
+ br i1 %outer.cond, label %exit, label %outer.header, !llvm.loop !2
+}
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.mustprogress"}
+!2 = distinct !{!2, !1}
diff --git a/llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll b/llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll
new file mode 100644
index 0000000000000..1ecbcba5c04b5
--- /dev/null
+++ b/llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll
@@ -0,0 +1,111 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=loop-deletion -S | FileCheck %s
+
+; Each loop has an invariant exit condition, so SCEV cannot bound its trip
+; count and deletion depends on assuming that the loop is finite.
+
+declare void @llvm.assume(i1)
+
+; Not finite by assumption, so the loop must stay.
+define void @unknown_tripcount_no_assumption(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @unknown_tripcount_no_assumption(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[FOR_COND:.*]]
+; CHECK: [[FOR_COND]]:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label %[[FOR_BODY:.*]], label %[[FOR_END:.*]]
+; CHECK: [[FOR_BODY]]:
+; CHECK-NEXT: br label %[[FOR_COND]]
+; CHECK: [[FOR_END]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %for.cond
+for.cond:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %for.body, label %for.end
+for.body:
+ br label %for.cond
+for.end:
+ ret void
+}
+
+; willreturn makes the loop finite by assumption.
+define void @unknown_tripcount_willreturn(i32 %a, i32 %b) willreturn {
+; CHECK-LABEL: define void @unknown_tripcount_willreturn(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR1:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[FOR_END:.*]]
+; CHECK: [[FOR_END]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %for.cond
+for.cond:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %for.body, label %for.end
+for.body:
+ br label %for.cond
+for.end:
+ ret void
+}
+
+; SCEV counts llvm.assume as a side effect, so isLoopNestFinite does not
+; treat this loop as finite. isLoopDead's own check of the mustprogress
+; function attribute still lets it be deleted.
+define void @unknown_tripcount_mustprogress_fn_assume(i32 %a, i32 %b) mustprogress {
+; CHECK-LABEL: define void @unknown_tripcount_mustprogress_fn_assume(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR2:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[FOR_END:.*]]
+; CHECK: [[FOR_END]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %for.cond
+for.cond:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %for.body, label %for.end
+for.body:
+ call void @llvm.assume(i1 %cmp)
+ br label %for.cond
+for.end:
+ ret void
+}
+
+; Same as above, but with the guarantee coming from loop metadata. It has no
+; function-level fallback, so the assume keeps the loop alive.
+; TODO: SCEV's side-effect check could ignore droppable instructions.
+define void @unknown_tripcount_mustprogress_md_assume(i32 %a, i32 %b) {
+; CHECK-LABEL: define void @unknown_tripcount_mustprogress_md_assume(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[FOR_COND:.*]]
+; CHECK: [[FOR_COND]]:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label %[[FOR_BODY:.*]], label %[[FOR_END:.*]]
+; CHECK: [[FOR_BODY]]:
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
+; CHECK-NEXT: br label %[[FOR_COND]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK: [[FOR_END]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %for.cond
+for.cond:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %for.body, label %for.end
+for.body:
+ call void @llvm.assume(i1 %cmp)
+ br label %for.cond, !llvm.loop !0
+for.end:
+ ret void
+}
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.mustprogress"}
+;.
+; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]]}
+; CHECK: [[META1]] = !{!"llvm.loop.mustprogress"}
+;.
>From 4d739a0f7e956727b32f28f502f9f3561d381821 Mon Sep 17 00:00:00 2001
From: Xiaohe Jake Xue <jakexxh at duck.com>
Date: Wed, 30 Sep 2026 23:08:13 -0400
Subject: [PATCH 2/2] [SCEV] Don't count droppable intrinsics as loop side
effects
getLoopProperties treats any call that may write memory as a side
effect, including droppable intrinsics such as llvm.assume, which are
only modeled as writing inaccessible memory to keep them in place. They
are not observable progress, so ignore them.
This lets mustprogress loops containing them be assumed finite, and makes
LoopDeletion's explicit mustprogress check redundant, so remove it.
Assisted-by: Claude Code
---
llvm/lib/Analysis/ScalarEvolution.cpp | 4 +
llvm/lib/Transforms/Scalar/LoopDeletion.cpp | 11 +-
.../ScalarEvolution/finite-loop-droppable.ll | 107 ++++++++++++++++++
.../LoopDeletion/loop-nest-finite.ll | 18 +--
4 files changed, 114 insertions(+), 26 deletions(-)
create mode 100644 llvm/test/Analysis/ScalarEvolution/finite-loop-droppable.ll
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 731d376818e03..448f31d536e0e 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -7516,6 +7516,10 @@ ScalarEvolution::getLoopProperties(const Loop *L) {
if (isa<MemIntrinsic>(I) && !I->isVolatile())
return false;
+ // Droppable intrinsics only model a memory write; they are not progress.
+ if (I->isDroppable())
+ return false;
+
return I->mayWriteToMemory();
};
diff --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
index b750a4deaa3d9..11912b62df52e 100644
--- a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
@@ -106,16 +106,7 @@ static bool isLoopDead(Loop *L, ScalarEvolution &SE,
}))
return false;
- // The loop can only be considered dead if either:
- // a. the function is mustprogress; or
- // b. every loop in the nest is known to terminate.
- // isLoopNestFinite also honors a mustprogress function, but only when the
- // loop has no side effects in SCEV's sense, and SCEV counts droppable
- // instructions (e.g. llvm.assume) as side effects. Check the attribute
- // directly so that such loops can still be deleted.
- if (L->getHeader()->getParent()->mustProgress())
- return true;
-
+ // Calls that may not return were rejected above.
return isLoopNestFinite(L, SE, LI);
}
diff --git a/llvm/test/Analysis/ScalarEvolution/finite-loop-droppable.ll b/llvm/test/Analysis/ScalarEvolution/finite-loop-droppable.ll
new file mode 100644
index 0000000000000..3988346351728
--- /dev/null
+++ b/llvm/test/Analysis/ScalarEvolution/finite-loop-droppable.ll
@@ -0,0 +1,107 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" -scalar-evolution-classify-expressions=0 2>&1 | FileCheck %s
+
+; Droppable intrinsics do not count as side effects, so they do not prevent
+; assuming that a mustprogress loop is finite.
+
+define void @assume_in_loop(i64 %n, i64 %s, i1 %c) mustprogress {
+; CHECK-LABEL: 'assume_in_loop'
+; CHECK-NEXT: Determining loop execution counts for: @assume_in_loop
+; CHECK-NEXT: Loop %for.body: backedge-taken count is ((((-1 * (1 umin ((-1 * %s) + (%n smax %s))))<nuw><nsw> + (-1 * %s) + (%n smax %s)) /u (1 umax %s)) + (1 umin ((-1 * %s) + (%n smax %s))))
+; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i64 -1
+; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is ((((-1 * (1 umin ((-1 * %s) + (%n smax %s))))<nuw><nsw> + (-1 * %s) + (%n smax %s)) /u (1 umax %s)) + (1 umin ((-1 * %s) + (%n smax %s))))
+; CHECK-NEXT: Loop %for.body: Trip multiple is 1
+;
+entry:
+ br label %for.body
+
+for.body:
+ %i.05 = phi i64 [ %add, %for.body ], [ 0, %entry ]
+ call void @llvm.assume(i1 %c)
+ %add = add nsw i64 %i.05, %s
+ %cmp = icmp slt i64 %add, %n
+ br i1 %cmp, label %for.body, label %for.end
+
+for.end:
+ ret void
+}
+
+define void @pseudoprobe_in_loop(i64 %n, i64 %s) mustprogress {
+; CHECK-LABEL: 'pseudoprobe_in_loop'
+; CHECK-NEXT: Determining loop execution counts for: @pseudoprobe_in_loop
+; CHECK-NEXT: Loop %for.body: backedge-taken count is ((((-1 * (1 umin ((-1 * %s) + (%n smax %s))))<nuw><nsw> + (-1 * %s) + (%n smax %s)) /u (1 umax %s)) + (1 umin ((-1 * %s) + (%n smax %s))))
+; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i64 -1
+; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is ((((-1 * (1 umin ((-1 * %s) + (%n smax %s))))<nuw><nsw> + (-1 * %s) + (%n smax %s)) /u (1 umax %s)) + (1 umin ((-1 * %s) + (%n smax %s))))
+; CHECK-NEXT: Loop %for.body: Trip multiple is 1
+;
+entry:
+ br label %for.body
+
+for.body:
+ %i.05 = phi i64 [ %add, %for.body ], [ 0, %entry ]
+ call void @llvm.pseudoprobe(i64 1234, i64 1, i32 0, i64 -1)
+ %add = add nsw i64 %i.05, %s
+ %cmp = icmp slt i64 %add, %n
+ br i1 %cmp, label %for.body, label %for.end
+
+for.end:
+ ret void
+}
+
+; Without mustprogress, the loop cannot be assumed finite.
+define void @assume_in_loop_no_mustprogress(i64 %n, i64 %s, i1 %c) {
+; CHECK-LABEL: 'assume_in_loop_no_mustprogress'
+; CHECK-NEXT: Determining loop execution counts for: @assume_in_loop_no_mustprogress
+; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
+; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
+; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
+; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is ((((-1 * (1 umin ((-1 * %s) + (%n smax %s))))<nuw><nsw> + (-1 * %s) + (%n smax %s)) /u (1 umax %s)) + (1 umin ((-1 * %s) + (%n smax %s))))
+; CHECK-NEXT: Predicates:
+; CHECK-NEXT: Compare predicate: %s sgt) 0
+; CHECK-NEXT: Loop %for.body: Predicated constant max backedge-taken count is i64 -1
+; CHECK-NEXT: Predicates:
+; CHECK-NEXT: Compare predicate: %s sgt) 0
+; CHECK-NEXT: Loop %for.body: Predicated symbolic max backedge-taken count is ((((-1 * (1 umin ((-1 * %s) + (%n smax %s))))<nuw><nsw> + (-1 * %s) + (%n smax %s)) /u (1 umax %s)) + (1 umin ((-1 * %s) + (%n smax %s))))
+; CHECK-NEXT: Predicates:
+; CHECK-NEXT: Compare predicate: %s sgt) 0
+;
+entry:
+ br label %for.body
+
+for.body:
+ %i.05 = phi i64 [ %add, %for.body ], [ 0, %entry ]
+ call void @llvm.assume(i1 %c)
+ %add = add nsw i64 %i.05, %s
+ %cmp = icmp slt i64 %add, %n
+ br i1 %cmp, label %for.body, label %for.end
+
+for.end:
+ ret void
+}
+
+; A volatile store is a side effect.
+define void @assume_and_volatile_store_in_loop(ptr %A, i64 %n, i64 %s, i1 %c) mustprogress {
+; CHECK-LABEL: 'assume_and_volatile_store_in_loop'
+; CHECK-NEXT: Determining loop execution counts for: @assume_and_volatile_store_in_loop
+; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
+; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
+; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
+;
+entry:
+ br label %for.body
+
+for.body:
+ %i.05 = phi i64 [ %add, %for.body ], [ 0, %entry ]
+ call void @llvm.assume(i1 %c)
+ %arrayidx = getelementptr inbounds i64, ptr %A, i64 %i.05
+ store volatile i64 0, ptr %arrayidx, align 8
+ %add = add nsw i64 %i.05, %s
+ %cmp = icmp slt i64 %add, %n
+ br i1 %cmp, label %for.body, label %for.end
+
+for.end:
+ ret void
+}
+
+declare void @llvm.assume(i1)
+declare void @llvm.pseudoprobe(i64, i64, i32, i64)
diff --git a/llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll b/llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll
index 1ecbcba5c04b5..172525f3fede0 100644
--- a/llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll
+++ b/llvm/test/Transforms/LoopDeletion/loop-nest-finite.ll
@@ -51,9 +51,7 @@ for.end:
ret void
}
-; SCEV counts llvm.assume as a side effect, so isLoopNestFinite does not
-; treat this loop as finite. isLoopDead's own check of the mustprogress
-; function attribute still lets it be deleted.
+; llvm.assume is droppable, so it is not a side effect.
define void @unknown_tripcount_mustprogress_fn_assume(i32 %a, i32 %b) mustprogress {
; CHECK-LABEL: define void @unknown_tripcount_mustprogress_fn_assume(
; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR2:[0-9]+]] {
@@ -74,21 +72,13 @@ for.end:
ret void
}
-; Same as above, but with the guarantee coming from loop metadata. It has no
-; function-level fallback, so the assume keeps the loop alive.
-; TODO: SCEV's side-effect check could ignore droppable instructions.
+; Same, with llvm.loop.mustprogress metadata instead.
define void @unknown_tripcount_mustprogress_md_assume(i32 %a, i32 %b) {
; CHECK-LABEL: define void @unknown_tripcount_mustprogress_md_assume(
; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: br label %[[FOR_COND:.*]]
; CHECK: [[FOR_COND]]:
-; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
-; CHECK-NEXT: br i1 [[CMP]], label %[[FOR_BODY:.*]], label %[[FOR_END:.*]]
-; CHECK: [[FOR_BODY]]:
-; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
-; CHECK-NEXT: br label %[[FOR_COND]], !llvm.loop [[LOOP0:![0-9]+]]
-; CHECK: [[FOR_END]]:
; CHECK-NEXT: ret void
;
entry:
@@ -105,7 +95,3 @@ for.end:
!0 = distinct !{!0, !1}
!1 = !{!"llvm.loop.mustprogress"}
-;.
-; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]]}
-; CHECK: [[META1]] = !{!"llvm.loop.mustprogress"}
-;.
More information about the llvm-commits
mailing list