Skip to content

Commit 3479c57

Browse files
authored
PowerPC32:PIC: Update to bcl to fix branch prediction mis-predict issue (#134140)
Update `bl` to `bcl 20, 31, .+4` for 32bit PIC code gen so the link stack is not corrupted and cause mis-predict for the branch predictor. fixes: #128644
1 parent 9965f3d commit 3479c57

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,9 @@ void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
967967
// L1$pb:
968968
MCSymbol *PICBase = MF->getPICBaseSymbol();
969969

970-
// Emit the 'bl'.
970+
// Emit 'bcl 20,31,.+4' so the link stack is not corrupted.
971971
EmitToStreamer(*OutStreamer,
972-
MCInstBuilder(PPC::BL)
972+
MCInstBuilder(PPC::BCLalways)
973973
// FIXME: We would like an efficient form for this, so we
974974
// don't have to do a lot of extra uniquing.
975975
.addExpr(MCSymbolRefExpr::create(PICBase, OutContext)));

llvm/test/CodeGen/PowerPC/mcm-5.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ sw.epilog: ; preds = %sw.bb3, %sw.default
5151
ret i32 %5
5252
}
5353
; CHECK-LABEL: test_jump_table:
54-
; CHECK-NOT: bl .L0$pb
54+
; CHECK-NOT: bcl 20, 31, .L0$pb
5555

5656
; CHECK: addis [[REG1:[0-9]+]], 2, .LC[[TOCNUM:[0-9]+]]@toc@ha
5757
; CHECK: ld [[REG2:[0-9]+]], .LC[[TOCNUM]]@toc@l([[REG1]])
@@ -64,7 +64,7 @@ sw.epilog: ; preds = %sw.bb3, %sw.default
6464
; CHECK-NEXT: .long .LBB0_{{[0-9]+}}-.LJTI0_0
6565

6666
; LARGE-LABEL: test_jump_table:
67-
; LARGE: bl .L0$pb
67+
; LARGE: bcl 20, 31, .L0$pb
6868
; LARGE-NEXT: .L0$pb:
6969
; LARGE: mflr [[REGBASE:[0-9]+]]
7070

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc < %s -mtriple=powerpc -relocation-model=pic | \
3+
; RUN: FileCheck -check-prefixes=SMALL %s
4+
5+
@val = global i8 0, align 1
6+
7+
define zeroext i8 @testbcl() nounwind {
8+
; SMALL-LABEL: testbcl:
9+
; SMALL: # %bb.0: # %entry
10+
; SMALL-NEXT: mflr 0
11+
; SMALL-NEXT: stwu 1, -16(1)
12+
; SMALL-NEXT: stw 30, 8(1)
13+
; SMALL-NEXT: stw 0, 20(1)
14+
; SMALL-NEXT: bcl 20, 31, .L0$pb
15+
; SMALL-NEXT: .L0$pb:
16+
; SMALL-NEXT: mflr 30
17+
; SMALL-NEXT: lwz 3, .L0$poff-.L0$pb(30)
18+
; SMALL-NEXT: add 30, 3, 30
19+
; SMALL-NEXT: lwz 3, .LC0-.LTOC(30)
20+
; SMALL-NEXT: lbz 3, 0(3)
21+
; SMALL-NEXT: lwz 0, 20(1)
22+
; SMALL-NEXT: lwz 30, 8(1)
23+
; SMALL-NEXT: addi 1, 1, 16
24+
; SMALL-NEXT: mtlr 0
25+
; SMALL-NEXT: blr
26+
entry:
27+
%0 = load i8, ptr @val, align 1
28+
ret i8 %0
29+
}

llvm/test/CodeGen/PowerPC/ppc32-pic-large.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ entry:
5555
; LARGE-BSS-NEXT: foo:
5656
; LARGE-BSS: stwu 1, -32(1)
5757
; LARGE-BSS: stw 30, 24(1)
58-
; LARGE-BSS: bl [[PB]]
58+
; LARGE-BSS: bcl 20, 31, [[PB]]
5959
; LARGE-BSS-NEXT: [[PB]]:
6060
; LARGE-BSS: mflr 30
6161
; LARGE-BSS: lwz [[REG:[0-9]+]], [[POFF]]-[[PB]](30)

llvm/test/CodeGen/PowerPC/ppc32-pic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ entry:
1919
; SMALL: stwu 1, -32(1)
2020
; SMALL: stw 30, 24(1)
2121
; SMALL-BSS: bl _GLOBAL_OFFSET_TABLE_@local-4
22-
; SMALL-SECURE: bl .L0$pb
22+
; SMALL-SECURE: bcl 20, 31, .L0$pb
2323
; SMALL: mflr 30
2424
; SMALL-SECURE: addis 30, 30, _GLOBAL_OFFSET_TABLE_-.L0$pb@ha
2525
; SMALL-SECURE: addi 30, 30, _GLOBAL_OFFSET_TABLE_-.L0$pb@l

0 commit comments

Comments
 (0)