Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 56b1ef7

Browse files
committed
Merging r309422:
------------------------------------------------------------------------ r309422 | rnk | 2017-07-28 12:48:40 -0700 (Fri, 28 Jul 2017) | 25 lines Fix conditional tail call branch folding when both edges are the same The conditional tail call logic did the wrong thing when both destinations of a conditional branch were the same: BB#1: derived from LLVM BB %entry Live Ins: %EFLAGS Predecessors according to CFG: BB#0 JE_1 <BB#5>, %EFLAGS<imp-use,kill> JMP_1 <BB#5> BB#5: derived from LLVM BB %sw.epilog Predecessors according to CFG: BB#1 TCRETURNdi64 <ga:@mergeable_conditional_tailcall>, 0, ... We would fold the JE_1 to a TCRETURNdi64cc, and then remove our BB#5 successor. Then BB#5 would be deleted as it had no predecessors, leaving a dangling "JMP_1 <BB#5>" reference behind to cause assertions later. This patch checks that both conditional branch destinations are different before doing the transform. The standard branch folding logic is able to remove both the JMP_1 and the JE_1, and for my test case we end up forming a better conditional tail call later. Fixes PR33980 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@309574 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 079c1f3 commit 56b1ef7

File tree

2 files changed

+142
-2
lines changed

2 files changed

+142
-2
lines changed

lib/CodeGen/BranchFolding.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,13 +1475,14 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
14751475
bool PredAnalyzable =
14761476
!TII->analyzeBranch(*Pred, PredTBB, PredFBB, PredCond, true);
14771477

1478-
if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB) {
1478+
if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB &&
1479+
PredTBB != PredFBB) {
14791480
// The predecessor has a conditional branch to this block which consists
14801481
// of only a tail call. Try to fold the tail call into the conditional
14811482
// branch.
14821483
if (TII->canMakeTailCallConditional(PredCond, TailCall)) {
14831484
// TODO: It would be nice if analyzeBranch() could provide a pointer
1484-
// to the branch insturction so replaceBranchWithTailCall() doesn't
1485+
// to the branch instruction so replaceBranchWithTailCall() doesn't
14851486
// have to search for it.
14861487
TII->replaceBranchWithTailCall(*Pred, PredCond, TailCall);
14871488
++NumTailCalls;
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# RUN: llc -run-pass=branch-folder %s -o - | FileCheck %s
2+
3+
# PR33980
4+
5+
# Don't form conditional tail calls when the original conditional branch has
6+
# the same true and false destination. Otherwise, when we remove the tail call
7+
# successor we will also remove the fallthrough successor from the CFG.
8+
9+
# CHECK: body: |
10+
# CHECK: bb.0.entry:
11+
# CHECK: successors: %bb.1.sw.bb(0x40000000)
12+
# CHECK: liveins: %edi
13+
# CHECK: CMP32ri8 killed %edi, 2, implicit-def %eflags
14+
# CHECK: TCRETURNdi64cc @mergeable_conditional_tailcall
15+
16+
# This was the unconditional branch to a dead MBB that we left behind before
17+
# this bug was fixed.
18+
# CHECK-NOT: JMP_1 %bb.-1
19+
20+
--- |
21+
; ModuleID = 't.ll'
22+
source_filename = "t.ll"
23+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
24+
target triple = "x86_64--linux"
25+
26+
@static_local_guard = external global i64, align 8
27+
28+
; Function Attrs: optsize
29+
define void @f(i32 %arg) #0 {
30+
entry:
31+
switch i32 %arg, label %sw.epilog [
32+
i32 0, label %sw.bb
33+
i32 1, label %sw.bb
34+
i32 2, label %sw.bb2
35+
]
36+
37+
sw.bb: ; preds = %entry, %entry
38+
%tmp = load atomic i8, i8* bitcast (i64* @static_local_guard to i8*) acquire, align 8
39+
%guard.uninitialized.i = icmp eq i8 %tmp, 0
40+
br i1 %guard.uninitialized.i, label %init.check.i, label %return, !prof !0
41+
42+
init.check.i: ; preds = %sw.bb
43+
tail call void @initialize_static_local(i64* nonnull @static_local_guard)
44+
ret void
45+
46+
sw.bb2: ; preds = %entry
47+
tail call void @mergeable_conditional_tailcall()
48+
ret void
49+
50+
sw.epilog: ; preds = %entry
51+
tail call void @mergeable_conditional_tailcall()
52+
ret void
53+
54+
return: ; preds = %sw.bb
55+
ret void
56+
}
57+
58+
declare void @mergeable_conditional_tailcall()
59+
60+
declare void @initialize_static_local(i64*)
61+
62+
; Function Attrs: nounwind
63+
declare void @llvm.stackprotector(i8*, i8**) #1
64+
65+
attributes #0 = { optsize }
66+
attributes #1 = { nounwind }
67+
68+
!0 = !{!"branch_weights", i32 1, i32 1048575}
69+
70+
...
71+
---
72+
name: f
73+
alignment: 0
74+
exposesReturnsTwice: false
75+
legalized: false
76+
regBankSelected: false
77+
selected: false
78+
tracksRegLiveness: true
79+
registers:
80+
liveins:
81+
- { reg: '%edi', virtual-reg: '' }
82+
frameInfo:
83+
isFrameAddressTaken: false
84+
isReturnAddressTaken: false
85+
hasStackMap: false
86+
hasPatchPoint: false
87+
stackSize: 0
88+
offsetAdjustment: 0
89+
maxAlignment: 0
90+
adjustsStack: false
91+
hasCalls: false
92+
stackProtector: ''
93+
maxCallFrameSize: 0
94+
hasOpaqueSPAdjustment: false
95+
hasVAStart: false
96+
hasMustTailInVarArgFunc: false
97+
savePoint: ''
98+
restorePoint: ''
99+
fixedStack:
100+
stack:
101+
constants:
102+
body: |
103+
bb.0.entry:
104+
successors: %bb.2.sw.bb(0x40000000), %bb.1.entry(0x40000000)
105+
liveins: %edi
106+
107+
CMP32ri8 killed %edi, 2, implicit-def %eflags
108+
JB_1 %bb.2.sw.bb, implicit %eflags
109+
JMP_1 %bb.1.entry
110+
111+
bb.1.entry:
112+
successors: %bb.4.sw.bb2(0x40000000), %bb.5.sw.epilog(0x40000000)
113+
liveins: %eflags
114+
115+
JE_1 %bb.4.sw.bb2, implicit killed %eflags
116+
JMP_1 %bb.5.sw.epilog
117+
118+
bb.2.sw.bb:
119+
successors: %bb.3.init.check.i(0x00000800), %bb.6.return(0x7ffff800)
120+
121+
%al = ACQUIRE_MOV8rm %rip, 1, _, @static_local_guard, _ :: (volatile load acquire 1 from `i8* bitcast (i64* @static_local_guard to i8*)`, align 8)
122+
TEST8rr killed %al, %al, implicit-def %eflags
123+
JNE_1 %bb.6.return, implicit killed %eflags
124+
JMP_1 %bb.3.init.check.i
125+
126+
bb.3.init.check.i:
127+
dead %edi = MOV32ri64 @static_local_guard, implicit-def %rdi
128+
TCRETURNdi64 @initialize_static_local, 0, csr_64, implicit %rsp, implicit %rdi
129+
130+
bb.4.sw.bb2:
131+
TCRETURNdi64 @mergeable_conditional_tailcall, 0, csr_64, implicit %rsp
132+
133+
bb.5.sw.epilog:
134+
TCRETURNdi64 @mergeable_conditional_tailcall, 0, csr_64, implicit %rsp
135+
136+
bb.6.return:
137+
RET 0
138+
139+
...

0 commit comments

Comments
 (0)