Skip to content

[LoopSimplifyCFG] Add check for missing loop preheader #149743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class ConstantTerminatorFoldingImpl {
// from any other block. So this variable set to true means that loop's latch
// has become unreachable from loop header.
bool DeleteCurrentLoop = false;
// Whether or not we enter the loop through an indirectbr.
bool HasIndirectEntry = false;

// The blocks of the original loop that will still be reachable from entry
// after the constant folding.
Expand Down Expand Up @@ -216,6 +218,19 @@ class ConstantTerminatorFoldingImpl {
return;
}

// We need a loop preheader to split in handleDeadExits(). If LoopSimplify
// wasn't able to form one because the loop can be entered through an
// indirectbr we cannot continue.
if (!L.getLoopPreheader()) {
assert(any_of(predecessors(L.getHeader()),
[&](BasicBlock *Pred) {
return isa<IndirectBrInst>(Pred->getTerminator());
}) &&
"Loop should have preheader if it is not entered indirectly");
HasIndirectEntry = true;
return;
}

// Collect live and dead loop blocks and exits.
LiveLoopBlocks.insert(L.getHeader());
for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
Expand Down Expand Up @@ -546,6 +561,12 @@ class ConstantTerminatorFoldingImpl {
return false;
}

if (HasIndirectEntry) {
LLVM_DEBUG(dbgs() << "Loops which can be entered indirectly are not"
" supported!\n");
return false;
}

// Nothing to constant-fold.
if (FoldCandidates.empty()) {
LLVM_DEBUG(
Expand Down
28 changes: 28 additions & 0 deletions llvm/test/Transforms/LoopSimplifyCFG/enter-through-indirectbr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -enable-loop-simplifycfg-term-folding=true -passes='require<domtree>,loop(loop-simplifycfg)' -verify-loop-info -verify-dom-info -verify-loop-lcssa < %s | FileCheck %s

define void @test(ptr %addr) {
; CHECK-LABEL: define void @test(
; CHECK-SAME: ptr [[ADDR:%.*]]) {
; CHECK-NEXT: indirectbr ptr [[ADDR]], [label %[[A:.*]], label %C]
; CHECK: [[A]]:
; CHECK-NEXT: br i1 true, label %[[B:.*]], label %[[C_LOOPEXIT:.*]]
; CHECK: [[B]]:
; CHECK-NEXT: br i1 true, label %[[A]], label %[[C_LOOPEXIT]]
; CHECK: [[C_LOOPEXIT]]:
; CHECK-NEXT: br label %[[C:.*]]
; CHECK: [[C]]:
; CHECK-NEXT: unreachable
;

indirectbr ptr %addr, [label %A, label %C]

A:
br i1 true, label %B, label %C

B:
br i1 true, label %A, label %C

C:
unreachable
}