Skip to content

Commit ab2025b

Browse files
committed
[BranchFolding] Avoid moving blocks to fall through to an indirect target of inline asm
1 parent efeb4ae commit ab2025b

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,8 +1776,9 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
17761776
// Okay, there is no really great place to put this block. If, however,
17771777
// the block before this one would be a fall-through if this block were
17781778
// removed, move this block to the end of the function. There is no real
1779-
// advantage in "falling through" to an EH block, so we don't want to
1780-
// perform this transformation for that case.
1779+
// advantage in "falling through" to an EH block or an indirect target of
1780+
// an INLINEASM_BR, so we don't want to perform this transformation for
1781+
// that case.
17811782
//
17821783
// Also, Windows EH introduced the possibility of an arbitrary number of
17831784
// successors to a given block. The analyzeBranch call does not consider
@@ -1787,10 +1788,15 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
17871788
// below were performed for EH "FallThrough" blocks. Therefore, even if
17881789
// that appears not to be happening anymore, we should assume that it is
17891790
// possible and not remove the "!FallThrough()->isEHPad" condition below.
1791+
//
1792+
// And inline asm branches also introduced the possibility of infinite
1793+
// rotation, as there are an arbitrary number of successors to a given
1794+
// block.
17901795
MachineBasicBlock *PrevTBB = nullptr, *PrevFBB = nullptr;
17911796
SmallVector<MachineOperand, 4> PrevCond;
17921797
if (FallThrough != MF.end() &&
17931798
!FallThrough->isEHPad() &&
1799+
!FallThrough->isInlineAsmBrIndirectTarget() &&
17941800
!TII->analyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond, true) &&
17951801
PrevBB.isSuccessor(&*FallThrough)) {
17961802
MBB->moveAfter(&MF.back());

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +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() {
88
; CHECK-LABEL: loop1:
99
; CHECK: # %bb.0: # %entry
10-
; CHECK-NEXT: pushl %esi
11-
; CHECK-NEXT: .cfi_def_cfa_offset 8
12-
; CHECK-NEXT: .cfi_offset %esi, -8
13-
; CHECK-NEXT: jmp .LBB0_1
10+
; CHECK-NEXT: .p2align 4
1411
; CHECK-NEXT: .LBB0_1: # %tailrecurse
1512
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
1613
; CHECK-NEXT: xorl %eax, %eax
1714
; CHECK-NEXT: movl $1, %edx
1815
; CHECK-NEXT: #APP
1916
; CHECK-NEXT: #NO_APP
20-
; CHECK-NEXT: movl %eax, %ecx
21-
; CHECK-NEXT: movl %edx, %esi
22-
; CHECK-NEXT: jmp .LBB0_3
17+
; CHECK-NEXT: jmp .LBB0_1
2318
; CHECK-NEXT: .LBB0_2: # Inline asm indirect target
24-
; CHECK-NEXT: # %tailrecurse.tailrecurse.backedge_crit_edge
19+
; CHECK-NEXT: # %tailrecurse.tailrecurse_crit_edge
2520
; CHECK-NEXT: # in Loop: Header=BB0_1 Depth=1
2621
; CHECK-NEXT: # Label of block must be emitted
27-
; CHECK-NEXT: .LBB0_3: # %tailrecurse.backedge
28-
; CHECK-NEXT: # in Loop: Header=BB0_1 Depth=1
2922
; CHECK-NEXT: jmp .LBB0_1
30-
; CHECK-NEXT: .LBB0_4: # Inline asm indirect target
23+
; CHECK-NEXT: .LBB0_3: # Inline asm indirect target
3124
; CHECK-NEXT: # %lab2.split
3225
; CHECK-NEXT: # Label of block must be emitted
3326
; CHECK-NEXT: movl %edx, %eax
34-
; CHECK-NEXT: popl %esi
35-
; CHECK-NEXT: .cfi_def_cfa_offset 4
3627
; CHECK-NEXT: retl
3728
entry:
3829
br label %tailrecurse

0 commit comments

Comments
 (0)