[llvm] [FixIrreducible] Handle conditional branch with both successors as header (PR #206057)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 06:13:46 PDT 2026


https://github.com/lijinpei-amd created https://github.com/llvm/llvm-project/pull/206057

A conditional branch redirecting edges to the cycle header may have both successors equal to the header (e.g. `br i1 %c, label %h, label %h`), which the previous `Succ1 = Succ0 ? nullptr : Header` logic mishandled by dropping the second edge.

Check each successor independently against the header instead.

Fixes https://github.com/llvm/llvm-project/issues/191979.

>From 6ef2fcc5bbf07cae85312f04e5c6bff93d5a98fe Mon Sep 17 00:00:00 2001
From: Li Jinpei <jinpli at amd.com>
Date: Fri, 26 Jun 2026 21:05:29 +0800
Subject: [PATCH] [FixIrreducible] Handle conditional branch with both
 successors as header

A conditional branch redirecting edges to the cycle header may have both
successors equal to the header (e.g. `br i1 %c, label %h, label %h`),
which the previous `Succ1 = Succ0 ? nullptr : Header` logic mishandled by
dropping the second edge.

Check each successor independently against the header instead.

Fixes https://github.com/llvm/llvm-project/issues/191979.
---
 llvm/lib/Transforms/Utils/FixIrreducible.cpp  |  4 +--
 .../Transforms/FixIrreducible/pr191979.ll     | 27 +++++++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Transforms/FixIrreducible/pr191979.ll

diff --git a/llvm/lib/Transforms/Utils/FixIrreducible.cpp b/llvm/lib/Transforms/Utils/FixIrreducible.cpp
index ead39f5991081..b4f6262f7b309 100644
--- a/llvm/lib/Transforms/Utils/FixIrreducible.cpp
+++ b/llvm/lib/Transforms/Utils/FixIrreducible.cpp
@@ -298,10 +298,8 @@ static bool fixIrreducible(Cycle &C, CycleInfo &CI, DominatorTree &DT,
       LLVM_DEBUG(dbgs() << "Added internal branch: " << printBasicBlock(P)
                         << " -> " << printBasicBlock(Header) << '\n');
     } else if (CondBrInst *Branch = dyn_cast<CondBrInst>(P->getTerminator())) {
-      // Exactly one of the two successors is the header.
       BasicBlock *Succ0 = Branch->getSuccessor(0) == Header ? Header : nullptr;
-      BasicBlock *Succ1 = Succ0 ? nullptr : Header;
-      assert(Succ0 || Branch->getSuccessor(1) == Header);
+      BasicBlock *Succ1 = Branch->getSuccessor(1) == Header ? Header : nullptr;
       assert(Succ0 || Succ1);
       CHub.addBranch(P, Succ0, Succ1);
 
diff --git a/llvm/test/Transforms/FixIrreducible/pr191979.ll b/llvm/test/Transforms/FixIrreducible/pr191979.ll
new file mode 100644
index 0000000000000..1316fa453429c
--- /dev/null
+++ b/llvm/test/Transforms/FixIrreducible/pr191979.ll
@@ -0,0 +1,27 @@
+; NOTE: Do not autogenerate
+; RUN: opt < %s -passes=fix-irreducible -disable-output
+
+; Regression test for https://github.com/llvm/llvm-project/issues/191979.
+declare i1 @sideeffect()
+
+define void @foo(i1 %c1) {
+entry:
+  br label %pad0
+
+pad0:
+  br label %pad1
+
+pad1:
+  br i1 %c1, label %a, label %b
+
+a:
+  %c2.a = call i1 @sideeffect()
+  br i1 %c2.a, label %b, label %b
+
+b:
+  %c2.b = call i1 @sideeffect()
+  br i1 %c2.b, label %exit, label %a
+
+exit:
+  ret void
+}



More information about the llvm-commits mailing list