[llvm] 3b8ee89 - [MustExecute] Use the loop predecessor instead of the preheader (#222096)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 00:25:44 PDT 2026
Author: Timur Baidusenov
Date: 2026-09-16T09:25:39+02:00
New Revision: 3b8ee89006f5a228fce75528f8dd80b4229abcc1
URL: https://github.com/llvm/llvm-project/commit/3b8ee89006f5a228fce75528f8dd80b4229abcc1
DIFF: https://github.com/llvm/llvm-project/commit/3b8ee89006f5a228fce75528f8dd80b4229abcc1.diff
LOG: [MustExecute] Use the loop predecessor instead of the preheader (#222096)
CanProveNotTakenFirstIteration() gets the IV start value via
LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader()). If the loop
is not in simplified form, getLoopPreheader() returns null and the
lookup asserts. This is reachable from print<must-execute> on IR that is
not in LoopSimplify form (see the reproducer in #166488).
Fixed: use getLoopPredecessor() instead: it returns the unique
out-of-loop predecessor of the header even when it is not a dedicated
preheader, consequently the start value is still known. If there is no
such predecessor, bail out.
Fixes #166488.
Added:
llvm/test/Analysis/MustExecute/no-preheader.ll
Modified:
llvm/lib/Analysis/MustExecute.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/MustExecute.cpp b/llvm/lib/Analysis/MustExecute.cpp
index 463477ce4aef1..af929c5070b21 100644
--- a/llvm/lib/Analysis/MustExecute.cpp
+++ b/llvm/lib/Analysis/MustExecute.cpp
@@ -150,8 +150,15 @@ static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
return false;
}
+ // The induction variable starts at the value coming into the header from
+ // outside the loop. A loop that is not in simplified form has no preheader,
+ // but the header can still have a single predecessor outside the loop.
+ BasicBlock *Predecessor = CurLoop->getLoopPredecessor();
+ if (!Predecessor)
+ return false;
+
auto DL = ExitBlock->getModule()->getDataLayout();
- auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader());
+ auto *IVStart = LHS->getIncomingValueForBlock(Predecessor);
auto *SimpleValOrNull = simplifyCmpInst(
Pred, IVStart, RHS, {DL, /*TLI*/ nullptr, DT, /*AC*/ nullptr, BI});
auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull);
diff --git a/llvm/test/Analysis/MustExecute/no-preheader.ll b/llvm/test/Analysis/MustExecute/no-preheader.ll
new file mode 100644
index 0000000000000..57c92cad57eb4
--- /dev/null
+++ b/llvm/test/Analysis/MustExecute/no-preheader.ll
@@ -0,0 +1,33 @@
+; RUN: opt -disable-output -passes='print<must-execute>' %s 2>&1 | FileCheck %s
+
+; The loop is not in simplified form: %header is entered from %entry, which
+; ends in a conditional branch, so the loop has no preheader. %entry is still
+; the only predecessor of the header from outside the loop, so the value %iv
+; starts at is known, and %latch is proven to execute on the first iteration.
+
+; CHECK-LABEL: @no_preheader(
+; CHECK: header:
+; CHECK-NEXT: %iv = phi i32 [ 0, %latch ], [ 0, %entry ] ; (mustexec in: header)
+; CHECK-NEXT: %cmp = icmp sgt i32 %iv, 0 ; (mustexec in: header)
+; CHECK-NEXT: br i1 %cmp, label %loop.exit, label %latch ; (mustexec in: header)
+; CHECK: latch:
+; CHECK-NEXT: br label %header ; (mustexec in: header)
+
+define i32 @no_preheader(i1 %c) {
+entry:
+ br i1 %c, label %header, label %exit
+
+header:
+ %iv = phi i32 [ 0, %latch ], [ 0, %entry ]
+ %cmp = icmp sgt i32 %iv, 0
+ br i1 %cmp, label %loop.exit, label %latch
+
+latch:
+ br label %header
+
+loop.exit:
+ ret i32 0
+
+exit:
+ ret i32 0
+}
More information about the llvm-commits
mailing list