Skip to content

Commit a634a80

Browse files
aeubankststellar
authored andcommitted
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 (cherry picked from commit fc93780)
1 parent f79cd71 commit a634a80

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
@@ -963,30 +963,32 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
963963
continue;
964964
}
965965

966-
// If one of the blocks is the entire common tail (and not the entry
967-
// block, which we can't jump to), we can treat all blocks with this same
968-
// tail at once. Use PredBB if that is one of the possibilities, as that
969-
// will not introduce any extra branches.
966+
// If one of the blocks is the entire common tail (and is not the entry
967+
// block/an EH pad, which we can't jump to), we can treat all blocks with
968+
// this same tail at once. Use PredBB if that is one of the possibilities,
969+
// as that will not introduce any extra branches.
970970
MachineBasicBlock *EntryBB =
971971
&MergePotentials.front().getBlock()->getParent()->front();
972972
unsigned commonTailIndex = SameTails.size();
973973
// If there are two blocks, check to see if one can be made to fall through
974974
// into the other.
975975
if (SameTails.size() == 2 &&
976976
SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
977-
SameTails[1].tailIsWholeBlock())
977+
SameTails[1].tailIsWholeBlock() && !SameTails[1].getBlock()->isEHPad())
978978
commonTailIndex = 1;
979979
else if (SameTails.size() == 2 &&
980980
SameTails[1].getBlock()->isLayoutSuccessor(
981-
SameTails[0].getBlock()) &&
982-
SameTails[0].tailIsWholeBlock())
981+
SameTails[0].getBlock()) &&
982+
SameTails[0].tailIsWholeBlock() &&
983+
!SameTails[0].getBlock()->isEHPad())
983984
commonTailIndex = 0;
984985
else {
985986
// Otherwise just pick one, favoring the fall-through predecessor if
986987
// there is one.
987988
for (unsigned i = 0, e = SameTails.size(); i != e; ++i) {
988989
MachineBasicBlock *MBB = SameTails[i].getBlock();
989-
if (MBB == EntryBB && SameTails[i].tailIsWholeBlock())
990+
if ((MBB == EntryBB || MBB->isEHPad()) &&
991+
SameTails[i].tailIsWholeBlock())
990992
continue;
991993
if (MBB == PredBB) {
992994
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)