[llvm] d1e15b6 - [BranchFolding] Ignore debug instructions in singleton cond branch check (#228715)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 21:23:24 PDT 2026
Author: Aochang Liu
Date: 2026-10-11T21:23:19-07:00
New Revision: d1e15b695381dcfa4c695a5157e37fa22a4eb0ab
URL: https://github.com/llvm/llvm-project/commit/d1e15b695381dcfa4c695a5157e37fa22a4eb0ab
DIFF: https://github.com/llvm/llvm-project/commit/d1e15b695381dcfa4c695a5157e37fa22a4eb0ab.diff
LOG: [BranchFolding] Ignore debug instructions in singleton cond branch check (#228715)
DBG_VALUE was counted when checking for a single-branch block, so the
duplicate branch was kept only under -g.
Fixes #228713
Assisted-by: Claude Opus 5.5
Added:
Modified:
llvm/lib/CodeGen/BranchFolding.cpp
llvm/test/CodeGen/X86/branchfolding-debug-invariant.mir
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index aef63f6bdc1c5..d1d7f42caafb2 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -1586,7 +1586,8 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
// If we have a block that consists of a single conditional branch
// instruction that is exactly identical to the terminator in the previous
// block, we can remove this block.
- if (MBB->size() == 1 && PrevBB.canFallThrough() && CurTBB == PriorTBB &&
+ if (MBB->getFirstNonDebugInstr() == MBB->getLastNonDebugInstr() &&
+ PrevBB.canFallThrough() && CurTBB == PriorTBB &&
areConditionalsEqual(CurCond, PriorCond)) {
// We remove the branch from the previous basic block rather than this
// one in case there are other blocks that specifically branch to this
diff --git a/llvm/test/CodeGen/X86/branchfolding-debug-invariant.mir b/llvm/test/CodeGen/X86/branchfolding-debug-invariant.mir
index 95c96d06393a2..a4331c918ca94 100644
--- a/llvm/test/CodeGen/X86/branchfolding-debug-invariant.mir
+++ b/llvm/test/CodeGen/X86/branchfolding-debug-invariant.mir
@@ -133,3 +133,71 @@ body: |
MOV8mi $r13, 1, $noreg, 0, $noreg, 0
RET 0
...
+
+---
+name: test3a
+body: |
+ ; The conditional branch in bb.1 duplicates the one in bb.0, so bb.0 falls
+ ; through to it and the two blocks are merged.
+ ;
+ ; CHECK-LABEL: name: test3a
+ ; CHECK: bb.0:
+ ; CHECK: TEST8rr killed renamable $al, renamable $al, implicit-def $eflags
+ ; CHECK: JCC_1 %bb.2, 5, implicit $eflags
+ ; CHECK: bb.1:
+ ; CHECK-NOT: JCC_1
+ ; CHECK: MOV8mi $r12, 1, $noreg, 0, $noreg, 0
+ ; CHECK: RET 0
+ ; CHECK: bb.2:
+ ; CHECK: MOV8mi $r13, 1, $noreg, 0, $noreg, 0
+ ; CHECK: RET 0
+ bb.0:
+ TEST8rr killed renamable $al, renamable $al, implicit-def $eflags
+ JCC_1 %bb.3, 5, implicit $eflags
+
+ bb.1:
+ JCC_1 %bb.3, 5, implicit killed $eflags
+
+ bb.2:
+ MOV8mi $r12, 1, $noreg, 0, $noreg, 0
+ RET 0
+
+ bb.3:
+ MOV8mi $r13, 1, $noreg, 0, $noreg, 0
+ RET 0
+...
+
+---
+name: test3b
+body: |
+ ; Verify that we get the same rewrites as in test3a when adding a DBG_VALUE
+ ; instruction in the mix.
+ ;
+ ; CHECK-LABEL: name: test3b
+ ; CHECK: bb.0:
+ ; CHECK: TEST8rr killed renamable $al, renamable $al, implicit-def $eflags
+ ; CHECK: DBG_VALUE
+ ; CHECK: JCC_1 %bb.2, 5, implicit $eflags
+ ; CHECK: bb.1:
+ ; CHECK-NOT: JCC_1
+ ; CHECK: MOV8mi $r12, 1, $noreg, 0, $noreg, 0
+ ; CHECK: RET 0
+ ; CHECK: bb.2:
+ ; CHECK: MOV8mi $r13, 1, $noreg, 0, $noreg, 0
+ ; CHECK: RET 0
+ bb.0:
+ TEST8rr killed renamable $al, renamable $al, implicit-def $eflags
+ JCC_1 %bb.3, 5, implicit $eflags
+
+ bb.1:
+ DBG_VALUE
+ JCC_1 %bb.3, 5, implicit killed $eflags
+
+ bb.2:
+ MOV8mi $r12, 1, $noreg, 0, $noreg, 0
+ RET 0
+
+ bb.3:
+ MOV8mi $r13, 1, $noreg, 0, $noreg, 0
+ RET 0
+...
More information about the llvm-commits
mailing list