Skip to content

Commit fc93780

Browse files
committed
Don't jump to landing pads in Control Flow Optimizer
Summary: Likely fixes https://bugs.llvm.org/show_bug.cgi?id=45858. Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D80047
1 parent 628f008 commit fc93780

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,30 +921,32 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
921921
continue;
922922
}
923923

924-
// If one of the blocks is the entire common tail (and not the entry
925-
// block, which we can't jump to), we can treat all blocks with this same
926-
// tail at once. Use PredBB if that is one of the possibilities, as that
927-
// will not introduce any extra branches.
924+
// If one of the blocks is the entire common tail (and is not the entry
925+
// block/an EH pad, which we can't jump to), we can treat all blocks with
926+
// this same tail at once. Use PredBB if that is one of the possibilities,
927+
// as that will not introduce any extra branches.
928928
MachineBasicBlock *EntryBB =
929929
&MergePotentials.front().getBlock()->getParent()->front();
930930
unsigned commonTailIndex = SameTails.size();
931931
// If there are two blocks, check to see if one can be made to fall through
932932
// into the other.
933933
if (SameTails.size() == 2 &&
934934
SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
935-
SameTails[1].tailIsWholeBlock())
935+
SameTails[1].tailIsWholeBlock() && !SameTails[1].getBlock()->isEHPad())
936936
commonTailIndex = 1;
937937
else if (SameTails.size() == 2 &&
938938
SameTails[1].getBlock()->isLayoutSuccessor(
939-
SameTails[0].getBlock()) &&
940-
SameTails[0].tailIsWholeBlock())
939+
SameTails[0].getBlock()) &&
940+
SameTails[0].tailIsWholeBlock() &&
941+
!SameTails[0].getBlock()->isEHPad())
941942
commonTailIndex = 0;
942943
else {
943944
// Otherwise just pick one, favoring the fall-through predecessor if
944945
// there is one.
945946
for (unsigned i = 0, e = SameTails.size(); i != e; ++i) {
946947
MachineBasicBlock *MBB = SameTails[i].getBlock();
947-
if (MBB == EntryBB && SameTails[i].tailIsWholeBlock())
948+
if ((MBB == EntryBB || MBB->isEHPad()) &&
949+
SameTails[i].tailIsWholeBlock())
948950
continue;
949951
if (MBB == PredBB) {
950952
commonTailIndex = i;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# RUN: llc -mtriple=x86_64-windows-msvc -verify-machineinstrs -run-pass branch-folder -o - %s | FileCheck %s
2+
3+
# Check that branch-folder does not create a fallthrough to a landing pad.
4+
# Also make sure that the landing pad still can be tail merged.
5+
---
6+
name: foo
7+
body: |
8+
; CHECK-LABEL: name: foo
9+
bb.0:
10+
successors: %bb.1, %bb.3
11+
bb.1:
12+
JCC_1 %bb.4, 5, implicit killed $eflags
13+
bb.2:
14+
MOV8mi $r13, 1, $noreg, 0, $noreg, 0
15+
JMP_1 %bb.5
16+
; CHECK: bb.2:
17+
; CHECK-NOT: successors: {{.*}}bb.3
18+
; CHECK: bb.3 (landing-pad):
19+
; CHECK-NOT: MOV8mi
20+
bb.3(landing-pad):
21+
MOV8mi $r13, 1, $noreg, 0, $noreg, 0
22+
JMP_1 %bb.5
23+
; CHECK: bb.4:
24+
bb.4:
25+
MOV8mi $r13, 2, $noreg, 0, $noreg, 0
26+
bb.5:
27+
RET 0
28+
...

0 commit comments

Comments
 (0)