[llvm] [SimplifyCFG] Bypass PHI-only successors with equal incoming values (PR #230411)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 9 01:46:44 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Nopileos Isemados (134ARG)
<details>
<summary>Changes</summary>
SimplifyCFG misses conditional branches where one successor reaches the other through a PHI-only block, even when both paths provide identical incoming values to the destination PHIs.
Compare the direct incoming values with those forwarded through the detour's PHIs. When all values match, replace the conditional branch with an unconditional branch to the common destination, removing the redundant detour edge.
Fixes #<!-- -->226929.
---
Full diff: https://github.com/llvm/llvm-project/pull/230411.diff
4 Files Affected:
- (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+67)
- (modified) llvm/test/Transforms/SimplifyCFG/EqualPHIEdgeBlockMerge.ll (+64)
- (modified) llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll (+2-3)
- (modified) llvm/test/Transforms/SimplifyCFG/switch_mask.ll (-3)
``````````diff
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index b7b442f64fda06..e49e10c18b36f2 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -8873,6 +8873,69 @@ static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
return PredPred;
}
+
+/// Bypass a PHI-only detour when both paths provide the same values.
+///
+/// Before: After:
+///
+/// BB ------> CommonDest BB ------> CommonDest
+/// \ ^ ^
+/// v | |
+/// EdgeBB ------------+ EdgeBB -------+
+/// ^ ^
+/// | |
+/// OtherPred OtherPred
+///
+/// For every CommonDest PHI, the direct value from BB must equal the
+/// value forwarded through EdgeBB's PHI on the BB -> EdgeBB edge.
+/// Only that edge is removed; EdgeBB may have other predecessors.
+static bool bypassTrivialSuccessor(CondBrInst *BI, DomTreeUpdater *DTU,
+ ArrayRef<WeakVH> LoopHeaders,
+ bool NeedCanonicalLoop) {
+ BasicBlock *BB = BI->getParent();
+
+ // Leave loop-header branch folding to the existing canonical-loop policy.
+ if (NeedCanonicalLoop && is_contained(LoopHeaders, BB))
+ return false;
+
+ for (unsigned I = 0; I != 2; ++I) {
+ BasicBlock *EdgeBB = BI->getSuccessor(I);
+ BasicBlock *CommonDest = BI->getSuccessor(I ^ 1);
+ auto *EdgeBI = dyn_cast<UncondBrInst>(EdgeBB->getTerminator());
+ if (!EdgeBI || EdgeBI->getSuccessor() != CommonDest ||
+ &*EdgeBB->getFirstNonPHIOrDbg() != EdgeBI)
+ continue;
+ if (NeedCanonicalLoop && !LoopHeaders.empty() &&
+ EdgeBB->hasNPredecessorsOrMore(2) &&
+ (is_contained(LoopHeaders, EdgeBB) ||
+ is_contained(LoopHeaders, CommonDest)))
+ continue;
+
+ bool CanBypass = all_of(CommonDest->phis(), [&](PHINode &PN) {
+ Value *DirectV = PN.getIncomingValueForBlock(BB);
+ Value *IndirectV = PN.getIncomingValueForBlock(EdgeBB);
+ if (auto *EdgePN = dyn_cast<PHINode>(IndirectV))
+ if (EdgePN->getParent() == EdgeBB)
+ IndirectV = EdgePN->getIncomingValueForBlock(BB);
+ // Preserve the PHI-based opportunity to remove undefined control flow.
+ return DirectV == IndirectV &&
+ !passingValueIsAlwaysUndefined(DirectV, &PN);
+ });
+ if (!CanBypass)
+ continue;
+
+ auto *NewBI = UncondBrInst::Create(CommonDest, BI->getIterator());
+ NewBI->setDebugLoc(BI->getDebugLoc());
+ EdgeBB->removePredecessor(BB);
+ eraseTerminatorAndDCECond(BI);
+ if (DTU)
+ DTU->applyUpdates({{DominatorTree::Delete, BB, EdgeBB}});
+ return true;
+ }
+
+ return false;
+}
+
/// Fold the following pattern:
/// bb0:
/// br i1 %cond1, label %bb1, label %bb2
@@ -9016,6 +9079,10 @@ bool SimplifyCFGOpt::simplifyCondBranch(CondBrInst *BI, IRBuilder<> &Builder) {
return requestResimplify();
}
+ if (bypassTrivialSuccessor(BI, DTU, LoopHeaders,
+ Options.NeedCanonicalLoop))
+ return requestResimplify();
+
// If this basic block is ONLY a compare and a branch, and if a predecessor
// branches to us and one of our successors, fold the comparison into the
// predecessor and use logical operations to pick the right destination.
diff --git a/llvm/test/Transforms/SimplifyCFG/EqualPHIEdgeBlockMerge.ll b/llvm/test/Transforms/SimplifyCFG/EqualPHIEdgeBlockMerge.ll
index a0a2e2b78e4bba..55b4ca86668ed0 100644
--- a/llvm/test/Transforms/SimplifyCFG/EqualPHIEdgeBlockMerge.ll
+++ b/llvm/test/Transforms/SimplifyCFG/EqualPHIEdgeBlockMerge.ll
@@ -27,6 +27,70 @@ M: ; preds = %N, %Q
ret i32 %R
}
+define i8 @bypass_trivial_successor(i1 %c, i8 %a, i8 %b, i32 %sw) {
+; CHECK-LABEL: @bypass_trivial_successor(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch i32 [[SW:%.*]], label [[TEST:%.*]] [
+; CHECK-NEXT: i32 0, label [[COMMON:%.*]]
+; CHECK-NEXT: i32 1, label [[EDGE:%.*]]
+; CHECK-NEXT: ]
+; CHECK: test:
+; CHECK-NEXT: [[S:%.*]] = select i1 [[C:%.*]], i8 [[A:%.*]], i8 [[B:%.*]]
+; CHECK-NEXT: br label [[COMMON]]
+; CHECK: edge:
+; CHECK-NEXT: br label [[COMMON]]
+; CHECK: common:
+; CHECK-NEXT: [[R:%.*]] = phi i8 [ [[S]], [[TEST]] ], [ 2, [[EDGE]] ], [ 1, [[ENTRY:%.*]] ]
+; CHECK-NEXT: ret i8 [[R]]
+;
+entry:
+ switch i32 %sw, label %test [
+ i32 0, label %common
+ i32 1, label %edge
+ ]
+
+test:
+ %s = select i1 %c, i8 %a, i8 %b
+ br i1 %c, label %common, label %edge
+
+edge:
+ %p = phi i8 [ %s, %test ], [ 2, %entry ]
+ br label %common
+
+common:
+ %r = phi i8 [ %s, %test ], [ %p, %edge ], [ 1, %entry ]
+ ret i8 %r
+}
+
+define i8 @do_not_bypass_different_values(i1 %c, i8 %a, i8 %b, i32 %sw) {
+; CHECK-LABEL: @do_not_bypass_different_values(
+; CHECK: test:
+; CHECK-NEXT: br i1 [[C:%.*]], label [[COMMON:%.*]], label [[EDGE:%.*]]
+; CHECK: edge:
+; CHECK-NEXT: [[P:%.*]] = phi i8 [ [[B:%.*]], [[TEST:%.*]] ], [ 2, [[ENTRY:%.*]] ]
+; CHECK-NEXT: br label [[COMMON]]
+; CHECK: common:
+; CHECK-NEXT: [[R:%.*]] = phi i8 [ [[A:%.*]], [[TEST]] ], [ [[P]], [[EDGE]] ], [ 1, [[ENTRY]] ]
+; CHECK-NEXT: ret i8 [[R]]
+;
+entry:
+ switch i32 %sw, label %test [
+ i32 0, label %common
+ i32 1, label %edge
+ ]
+
+test:
+ br i1 %c, label %common, label %edge
+
+edge:
+ %p = phi i8 [ %b, %test ], [ 2, %entry ]
+ br label %common
+
+common:
+ %r = phi i8 [ %a, %test ], [ %p, %edge ], [ 1, %entry ]
+ ret i8 %r
+}
+
; Test merging of blocks with phi nodes where at least one incoming value
; in the successor is undef.
define i8 @testundef(i32 %u) {
diff --git a/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll b/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
index 0f540007b242e2..1de49a4fd138e6 100644
--- a/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
+++ b/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
@@ -59,8 +59,8 @@ define void @merge_cbranches(i32 %V) {
; CHECK-NEXT: [[DIVERGENT_COND:%.*]] = icmp ne i32 [[V:%.*]], 0
; CHECK-NEXT: [[UNIFORM_COND:%.*]] = call i1 @uniform_result(i1 [[DIVERGENT_COND]])
; CHECK-NEXT: [[DIVERGENT_COND_NOT:%.*]] = xor i1 [[DIVERGENT_COND]], true
-; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[UNIFORM_COND]], i1 true, i1 [[DIVERGENT_COND_NOT]]
-; CHECK-NEXT: br i1 [[BRMERGE]], label [[EXIT:%.*]], label [[BB3:%.*]], !prof [[PROF2:![0-9]+]]
+; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[UNIFORM_COND]], i1 true, i1 [[DIVERGENT_COND_NOT]], !prof [[PROF2:![0-9]+]]
+; CHECK-NEXT: br i1 [[BRMERGE]], label [[EXIT:%.*]], label [[BB3:%.*]], !prof [[PROF3:![0-9]+]]
; CHECK: bb3:
; CHECK-NEXT: call void @bar()
; CHECK-NEXT: br label [[EXIT]]
@@ -82,7 +82,6 @@ exit:
define void @uint32_overflow_test(i1 %arg, i1 %arg1) {
; CHECK-LABEL: @uint32_overflow_test(
; CHECK-NEXT: bb:
-; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[ARG:%.*]], i1 true, i1 [[ARG1:%.*]]
; CHECK-NEXT: ret void
;
bb:
diff --git a/llvm/test/Transforms/SimplifyCFG/switch_mask.ll b/llvm/test/Transforms/SimplifyCFG/switch_mask.ll
index 70191701ed5ebd..6c6db5001dceaa 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_mask.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_mask.ll
@@ -220,9 +220,6 @@ lor.end: ; preds = %default, %for.end,
define i1 @pr88607() {
; CHECK-LABEL: @pr88607(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[COND:%.*]] = select i1 false, i32 4, i32 1
-; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 false, i32 2, i32 [[COND]]
-; CHECK-NEXT: [[COND1:%.*]] = icmp eq i32 [[SPEC_SELECT]], 1
; CHECK-NEXT: ret i1 false
;
entry:
``````````
</details>
https://github.com/llvm/llvm-project/pull/230411
More information about the llvm-commits
mailing list