Skip to content

Commit f393f2a

Browse files
authored
[BranchFolding] Avoid moving blocks to fall through to an indirect target (#152916)
Depend on #152591 to fix #149023. Similar to an EH pad, there is no real advantage in "falling through" to an indirect target of an INLINEASM_BR. And multiple indirect targets of inline asm at the end of a function may be rotated infinitely. Therefore, this patch avoids such optimization on indirect target of inline asm as fall through.
1 parent 4c28bbf commit f393f2a

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,10 +1787,18 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
17871787
// below were performed for EH "FallThrough" blocks. Therefore, even if
17881788
// that appears not to be happening anymore, we should assume that it is
17891789
// possible and not remove the "!FallThrough()->isEHPad" condition below.
1790+
//
1791+
// Similarly, the analyzeBranch call does not consider callbr, which also
1792+
// introduces the possibility of infinite rotation, as there may be
1793+
// multiple successors of PrevBB. Thus we check such case by
1794+
// FallThrough->isInlineAsmBrIndirectTarget().
1795+
// NOTE: Checking if PrevBB contains callbr is more precise, but much
1796+
// more expensive.
17901797
MachineBasicBlock *PrevTBB = nullptr, *PrevFBB = nullptr;
17911798
SmallVector<MachineOperand, 4> PrevCond;
1792-
if (FallThrough != MF.end() &&
1793-
!FallThrough->isEHPad() &&
1799+
1800+
if (FallThrough != MF.end() && !FallThrough->isEHPad() &&
1801+
!FallThrough->isInlineAsmBrIndirectTarget() &&
17941802
!TII->analyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond, true) &&
17951803
PrevBB.isSuccessor(&*FallThrough)) {
17961804
MBB->moveAfter(&MF.back());

llvm/test/CodeGen/X86/callbr-asm-loop.ll

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
22

3-
; RUN: llc -O0 -mtriple=i686-- < %s | FileCheck %s
3+
; RUN: llc -O1 -mtriple=i686-- < %s | FileCheck %s
44

55
; Test that causes multiple defs of %eax.
6-
; FIXME: The testcase hangs with -O1/2/3 enabled.
76
define i32 @loop1() nounwind {
87
; CHECK-LABEL: loop1:
98
; CHECK: # %bb.0: # %entry
10-
; CHECK-NEXT: pushl %esi
11-
; CHECK-NEXT: jmp .LBB0_1
9+
; CHECK-NEXT: .p2align 4
1210
; CHECK-NEXT: .LBB0_1: # %tailrecurse
1311
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
1412
; CHECK-NEXT: xorl %eax, %eax
1513
; CHECK-NEXT: movl $1, %edx
1614
; CHECK-NEXT: #APP
1715
; CHECK-NEXT: #NO_APP
18-
; CHECK-NEXT: movl %eax, %ecx
19-
; CHECK-NEXT: movl %edx, %esi
20-
; CHECK-NEXT: jmp .LBB0_3
16+
; CHECK-NEXT: jmp .LBB0_1
2117
; CHECK-NEXT: .LBB0_2: # Inline asm indirect target
22-
; CHECK-NEXT: # %tailrecurse.tailrecurse.backedge_crit_edge
18+
; CHECK-NEXT: # %tailrecurse.tailrecurse_crit_edge
2319
; CHECK-NEXT: # in Loop: Header=BB0_1 Depth=1
2420
; CHECK-NEXT: # Label of block must be emitted
25-
; CHECK-NEXT: .LBB0_3: # %tailrecurse.backedge
26-
; CHECK-NEXT: # in Loop: Header=BB0_1 Depth=1
2721
; CHECK-NEXT: jmp .LBB0_1
28-
; CHECK-NEXT: .LBB0_4: # Inline asm indirect target
22+
; CHECK-NEXT: .LBB0_3: # Inline asm indirect target
2923
; CHECK-NEXT: # %lab2.split
3024
; CHECK-NEXT: # Label of block must be emitted
3125
; CHECK-NEXT: movl %edx, %eax
32-
; CHECK-NEXT: popl %esi
3326
; CHECK-NEXT: retl
3427
entry:
3528
br label %tailrecurse

0 commit comments

Comments
 (0)