[llvm] [ControlFlowHub] Fix duplicate DomTree updates when branch successors are identical (PR #176620)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 17 19:33:21 PST 2026
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/176620
>From 2e57b8d495342ac33e8f0846d14ef0e0ed254951 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Sat, 17 Jan 2026 21:55:27 -0500
Subject: [PATCH] [ControlFlowHub] Fix duplicate DomTree updates when branch
successors are identical
When a conditional branch has both successors pointing to the same block (e.g., `br i1 %cond, label %bb, label %bb`), `ControlFlowHub::finalize` generates duplicate `Delete` updates for the same CFG edge. This can cause assertion in `fix-irreducible` pass.
Fixes #176553.
---
.../lib/Transforms/Utils/ControlFlowUtils.cpp | 13 ++++---
...ntrol-flow-hub-finalize-same-succ-crash.ll | 37 +++++++++++++++++++
2 files changed, 45 insertions(+), 5 deletions(-)
create mode 100644 llvm/test/Transforms/Util/control-flow-hub-finalize-same-succ-crash.ll
diff --git a/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp b/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp
index 287a177371c80..e9f74643297ac 100644
--- a/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ControlFlowUtils.cpp
@@ -97,7 +97,7 @@ static void calcPredicateUsingInteger(ArrayRef<EdgeDescriptor> Branches,
for (auto [BB, Succ0, Succ1] : Branches) {
Value *Condition = redirectToHub(BB, Succ0, Succ1, FirstGuardBlock);
Value *IncomingId = nullptr;
- if (Succ0 && Succ1) {
+ if (Succ0 && Succ1 && Succ0 != Succ1) {
auto Succ0Iter = find(Outgoing, Succ0);
auto Succ1Iter = find(Outgoing, Succ1);
Value *Id0 =
@@ -107,7 +107,8 @@ static void calcPredicateUsingInteger(ArrayRef<EdgeDescriptor> Branches,
IncomingId = SelectInst::Create(Condition, Id0, Id1, "target.bb.idx",
BB->getTerminator()->getIterator());
} else {
- // Get the index of the non-null successor.
+ // Get the index of the non-null successor, or when both successors
+ // are the same block, use that block's index directly.
auto SuccIter = Succ0 ? find(Outgoing, Succ0) : find(Outgoing, Succ1);
IncomingId =
ConstantInt::get(Int32Ty, std::distance(Outgoing.begin(), SuccIter));
@@ -166,9 +167,10 @@ static void calcPredicateUsingBooleans(
PHINode *Phi = cast<PHINode>(GuardPredicates[Out]);
if (Out != Succ0 && Out != Succ1) {
Phi->addIncoming(BoolFalse, BB);
- } else if (!Succ0 || !Succ1 || OneSuccessorDone) {
+ } else if (!Succ0 || !Succ1 || Succ0 == Succ1 || OneSuccessorDone) {
// Optimization: When only one successor is an outgoing block,
- // the incoming predicate from `BB` is always true.
+ // or both successors are the same block, the incoming predicate
+ // from `BB` is always true.
Phi->addIncoming(BoolTrue, BB);
} else {
assert(Succ0 && Succ1);
@@ -300,7 +302,8 @@ std::pair<BasicBlock *, bool> ControlFlowHub::finalize(
for (auto [BB, Succ0, Succ1] : Branches) {
if (Succ0)
Updates.push_back({DominatorTree::Delete, BB, Succ0});
- if (Succ1)
+ // Only add Succ1 if it's different from Succ0 to avoid duplicate updates
+ if (Succ1 && Succ1 != Succ0)
Updates.push_back({DominatorTree::Delete, BB, Succ1});
}
}
diff --git a/llvm/test/Transforms/Util/control-flow-hub-finalize-same-succ-crash.ll b/llvm/test/Transforms/Util/control-flow-hub-finalize-same-succ-crash.ll
new file mode 100644
index 0000000000000..137331c4c268b
--- /dev/null
+++ b/llvm/test/Transforms/Util/control-flow-hub-finalize-same-succ-crash.ll
@@ -0,0 +1,37 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=fix-irreducible %s | FileCheck %s
+
+define void @func(i1 %v.0, i1 %v.1) {
+; CHECK-LABEL: define void @func(
+; CHECK-SAME: i1 [[V_0:%.*]], i1 [[V_1:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br i1 [[V_0]], label %[[BB_1:.*]], label %[[BB_2:.*]]
+; CHECK: [[BB_1]]:
+; CHECK-NEXT: br label %[[IRR_GUARD:.*]]
+; CHECK: [[BB_2]]:
+; CHECK-NEXT: br label %[[IRR_GUARD]]
+; CHECK: [[BB_3:.*]]:
+; CHECK-NEXT: br label %[[BB_4:.*]]
+; CHECK: [[BB_4]]:
+; CHECK-NEXT: br label %[[IRR_GUARD]]
+; CHECK: [[IRR_GUARD]]:
+; CHECK-NEXT: [[GUARD_BB_3:%.*]] = phi i1 [ true, %[[BB_4]] ], [ true, %[[BB_2]] ], [ false, %[[BB_1]] ]
+; CHECK-NEXT: br i1 [[GUARD_BB_3]], label %[[BB_3]], label %[[BB_4]]
+;
+entry:
+ br i1 %v.0, label %bb.1, label %bb.2
+
+bb.1:
+ br label %bb.4
+
+bb.2:
+ br i1 %v.1, label %bb.3, label %bb.3
+
+bb.3:
+ br label %bb.4
+
+bb.4:
+ br label %bb.3
+
+ uselistorder label %bb.4, { 1, 0 }
+}
More information about the llvm-commits
mailing list