Skip to content

Commit 53be148

Browse files
committed
[llvm][AArch64] Preserve regmask when expanding the BLR_BTI pseudo instruction
Fixes #73787 Not doing so lead to us making use of a register after the call, which has been clobbered by the call. Added an MIR test that runs only the pseudo expasion pass.
1 parent e9869b5 commit 53be148

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ bool AArch64ExpandPseudo::expandCALL_BTI(MachineBasicBlock &MBB,
837837
BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)).getInstr();
838838
Call->addOperand(CallTarget);
839839
Call->setCFIType(*MBB.getParent(), MI.getCFIType());
840+
Call->copyImplicitOps(*MBB.getParent(), MI);
840841

841842
MachineInstr *BTI =
842843
BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::HINT))
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass=aarch64-expand-pseudo -o - %s | FileCheck %s
2+
3+
# When expanding a BLR_BTI, we should keep the regmask that was attached to it.
4+
# Otherwise we could end up using a register after the BL which was clobbered by
5+
# the function that was called.
6+
# CHECK: BUNDLE implicit-def $lr, implicit-def $w30, implicit-def $sp, implicit-def $wsp, implicit $sp {
7+
# CHECK: BL @_setjmp, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def dead $lr, implicit $sp, implicit-def $sp
8+
# CHECK: HINT 36
9+
# CHECK: }
10+
11+
# Generated from C, then simplified:
12+
# void _setjmp();
13+
# void a(int b) {
14+
# _setjmp();
15+
# for (; b;)
16+
# ;
17+
# }
18+
19+
--- |
20+
define void @a() {
21+
ret void
22+
}
23+
24+
declare void @_setjmp(...)
25+
...
26+
---
27+
name: a
28+
stack:
29+
- { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 16, callee-saved-register: '$x19' }
30+
- { id: 1, type: spill-slot, offset: -24, size: 8, alignment: 8, callee-saved-register: '$lr' }
31+
- { id: 2, type: spill-slot, offset: -32, size: 8, alignment: 8, callee-saved-register: '$fp' }
32+
body: |
33+
bb.0:
34+
successors: %bb.2, %bb.1
35+
liveins: $w0, $lr, $x19
36+
37+
frame-setup PAUTH_PROLOGUE implicit-def $lr, implicit $lr, implicit $sp
38+
early-clobber $sp = frame-setup STPXpre killed $fp, killed $lr, $sp, -4 :: (store (s64) into %stack.2), (store (s64) into %stack.1)
39+
frame-setup CFI_INSTRUCTION def_cfa_offset 32
40+
frame-setup STRXui killed $x19, $sp, 2 :: (store (s64) into %stack.0)
41+
$fp = frame-setup ADDXri $sp, 0, 0
42+
frame-setup CFI_INSTRUCTION def_cfa $w29, 32
43+
frame-setup CFI_INSTRUCTION offset $w19, -16
44+
frame-setup CFI_INSTRUCTION offset $w30, -24
45+
frame-setup CFI_INSTRUCTION offset $w29, -32
46+
$w19 = ORRWrr $wzr, $w0
47+
BLR_BTI @_setjmp, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
48+
CBZW killed renamable $w19, %bb.2
49+
50+
bb.1:
51+
B %bb.1
52+
53+
bb.2:
54+
frame-destroy CFI_INSTRUCTION def_cfa $wsp, 32
55+
$x19 = frame-destroy LDRXui $sp, 2 :: (load (s64) from %stack.0)
56+
early-clobber $sp, $fp, $lr = frame-destroy LDPXpost $sp, 4 :: (load (s64) from %stack.2), (load (s64) from %stack.1)
57+
frame-destroy CFI_INSTRUCTION def_cfa_offset 0
58+
frame-destroy PAUTH_EPILOGUE implicit-def $lr, implicit $lr, implicit $sp
59+
frame-destroy CFI_INSTRUCTION restore $w19
60+
frame-destroy CFI_INSTRUCTION restore $w30
61+
frame-destroy CFI_INSTRUCTION restore $w29
62+
RET_ReallyLR
63+
...

llvm/test/CodeGen/AArch64/kcfi-bti.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ define void @f2(ptr noundef %x) !kcfi_type !2 {
4949

5050
; KCFI: BUNDLE{{.*}} {
5151
; KCFI-NEXT: KCFI_CHECK $x0, 12345678, implicit-def $x9, implicit-def $x16, implicit-def $x17, implicit-def $nzcv
52-
; KCFI-NEXT: BLR killed $x0, implicit-def $lr, implicit $sp
52+
; KCFI-NEXT: BLR killed $x0, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit-def dead $lr, implicit $sp, implicit-def $sp
5353
; KCFI-NEXT: HINT 36
5454
; KCFI-NEXT: }
5555

0 commit comments

Comments
 (0)