Skip to content

Commit 2913dc6

Browse files
committed
[BranchFolding] Avoid moving blocks to fall through to an indirect target
1 parent ef5e65d commit 2913dc6

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
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 checks such case by
1794+
// !FallThrough->isInlineAsmBrIndirectTarget().
1795+
// NOTE: Checking if PrevBB contains callbr is more precise, but
1796+
// much 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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
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.
66
; FIXME: The testcase hangs with -O1/2/3 enabled.
77
define i32 @loop1() nounwind {
88
; CHECK-LABEL: loop1:
99
; CHECK: # %bb.0: # %entry
10-
; CHECK-NEXT: pushl %esi
11-
; CHECK-NEXT: jmp .LBB0_1
10+
; CHECK-NEXT: .p2align 4
1211
; CHECK-NEXT: .LBB0_1: # %tailrecurse
1312
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
1413
; CHECK-NEXT: xorl %eax, %eax
1514
; CHECK-NEXT: movl $1, %edx
1615
; CHECK-NEXT: #APP
1716
; CHECK-NEXT: #NO_APP
18-
; CHECK-NEXT: movl %eax, %ecx
19-
; CHECK-NEXT: movl %edx, %esi
20-
; CHECK-NEXT: jmp .LBB0_3
17+
; CHECK-NEXT: jmp .LBB0_1
2118
; CHECK-NEXT: .LBB0_2: # Inline asm indirect target
22-
; CHECK-NEXT: # %tailrecurse.tailrecurse.backedge_crit_edge
19+
; CHECK-NEXT: # %tailrecurse.tailrecurse_crit_edge
2320
; CHECK-NEXT: # in Loop: Header=BB0_1 Depth=1
2421
; 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
2722
; CHECK-NEXT: jmp .LBB0_1
28-
; CHECK-NEXT: .LBB0_4: # Inline asm indirect target
23+
; CHECK-NEXT: .LBB0_3: # Inline asm indirect target
2924
; CHECK-NEXT: # %lab2.split
3025
; CHECK-NEXT: # Label of block must be emitted
3126
; CHECK-NEXT: movl %edx, %eax
32-
; CHECK-NEXT: popl %esi
3327
; CHECK-NEXT: retl
3428
entry:
3529
br label %tailrecurse

0 commit comments

Comments
 (0)