aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaohai Wen <haohai.wen@intel.com>2024-01-18 23:26:22 +0800
committerGitHub <noreply@github.com>2024-01-18 23:26:22 +0800
commitfb2c6bbf42b09c0d3d31df7cac3f5a6cc2ee929e (patch)
tree9ae1e72d0fccecf0c12a490004bca59d9cefd81c
parent819bd9e39b8c255600b7ec13ac195f726aa9a082 (diff)
downloadllvm-fb2c6bbf42b09c0d3d31df7cac3f5a6cc2ee929e.zip
llvm-fb2c6bbf42b09c0d3d31df7cac3f5a6cc2ee929e.tar.gz
llvm-fb2c6bbf42b09c0d3d31df7cac3f5a6cc2ee929e.tar.bz2
[BranchFolding] Use isSuccessor to confirm fall through (#77923)
When merging blocks, if the previous block has no any branch instruction and has one successor, the successor may be SEH landing pad and the block will always raise exception and nerver fall through to next block. We can not merge them in such case. isSuccessor should be used to confirm it can fall through to next block.
-rw-r--r--llvm/lib/CodeGen/BranchFolding.cpp2
-rw-r--r--llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir41
2 files changed, 42 insertions, 1 deletions
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 599b7c7..a9f7835 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -1411,7 +1411,7 @@ ReoptimizeBlock:
// This has to check PrevBB->succ_size() because EH edges are ignored by
// analyzeBranch.
if (PriorCond.empty() && !PriorTBB && MBB->pred_size() == 1 &&
- PrevBB.succ_size() == 1 &&
+ PrevBB.succ_size() == 1 && PrevBB.isSuccessor(MBB) &&
!MBB->hasAddressTaken() && !MBB->isEHPad()) {
LLVM_DEBUG(dbgs() << "\nMerging into block: " << PrevBB
<< "From MBB: " << *MBB);
diff --git a/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir b/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
index 8eef545..98dadbf 100644
--- a/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
+++ b/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
@@ -49,3 +49,44 @@ body: |
bb.6:
RET 0
...
+---
+name: foo
+body: |
+ ; CHECK-LABEL: name: foo
+ ; CHECK: bb.0:
+ ; CHECK-NEXT: successors: %bb.1(0x7ffff800), %bb.2(0x00000800)
+ ; CHECK-NEXT: liveins: $rcx
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: renamable $eax = MOV32rm renamable $rcx, 1, $noreg, 0, $noreg
+ ; CHECK-NEXT: TEST32rr renamable $eax, renamable $eax, implicit-def $eflags
+ ; CHECK-NEXT: JCC_1 %bb.2, 14, implicit killed $eflags
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.1:
+ ; CHECK-NEXT: successors: %bb.3(0x80000000)
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: INT 3
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.2:
+ ; CHECK-NEXT: RET 0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.3 (machine-block-address-taken, landing-pad, ehfunclet-entry):
+ ; CHECK-NEXT: CLEANUPRET
+ bb.0:
+ successors: %bb.1(0x7ffff800), %bb.2(0x00000800)
+ liveins: $rcx
+
+ renamable $eax = MOV32rm renamable $rcx, 1, $noreg, 0, $noreg
+ TEST32rr renamable $eax, renamable $eax, implicit-def $eflags
+ JCC_1 %bb.2, 14, implicit killed $eflags
+ JMP_1 %bb.1
+
+ bb.1:
+ successors: %bb.3(0x80000000)
+ INT 3
+
+ bb.2:
+ RET 0
+
+ bb.3 (machine-block-address-taken, landing-pad, ehfunclet-entry):
+ CLEANUPRET
+...