Skip to content

Commit da84646

Browse files
committed
[AArch64] Add check that prologue insertion doesn't clobber live regs.
This patch extends AArch64FrameLowering::emitProglogue to check if the inserted prologue clobbers live registers. At the moment, llvm/test/CodeGen/AArch64/framelayout-scavengingslot.mir is failing because it has a block with the following live-in list liveins: $x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17, $x18, $x19, $x20, $x21, $x22, $x23, $x24, $x25, $x26, $x27, $x28, $lr meaning the prologue actually clobbers a live register.
1 parent ec42d54 commit da84646

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,18 @@ static void emitDefineCFAWithFP(MachineFunction &MF, MachineBasicBlock &MBB,
14231423
.setMIFlags(MachineInstr::FrameSetup);
14241424
}
14251425

1426+
/// Collect live registers from the end of \p MI's parent up to (including) \p
1427+
/// MI in \p LiveRegs.
1428+
static void getLivePhysRegsUpTo(MachineInstr &MI, const TargetRegisterInfo &TRI,
1429+
LivePhysRegs &LiveRegs) {
1430+
1431+
MachineBasicBlock &MBB = *MI.getParent();
1432+
LiveRegs.addLiveOuts(MBB);
1433+
for (const MachineInstr &MI :
1434+
reverse(make_range(MI.getIterator(), MBB.instr_end())))
1435+
LiveRegs.stepBackward(MI);
1436+
}
1437+
14261438
void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14271439
MachineBasicBlock &MBB) const {
14281440
MachineBasicBlock::iterator MBBI = MBB.begin();
@@ -1431,6 +1443,8 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14311443
const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>();
14321444
const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo();
14331445
const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1446+
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1447+
14341448
MachineModuleInfo &MMI = MF.getMMI();
14351449
AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
14361450
bool EmitCFI = AFI->needsDwarfUnwindInfo(MF);
@@ -1440,6 +1454,25 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14401454
bool HasWinCFI = false;
14411455
auto Cleanup = make_scope_exit([&]() { MF.setHasWinCFI(HasWinCFI); });
14421456

1457+
MachineBasicBlock::iterator End = MBB.end();
1458+
#ifndef NDEBUG
1459+
// Collect live register from the end of MBB up to the start of the existing
1460+
// frame setup instructions.
1461+
MachineBasicBlock::iterator NonFrameStart = MBB.begin();
1462+
while (NonFrameStart != End &&
1463+
NonFrameStart->getFlag(MachineInstr::FrameSetup))
1464+
++NonFrameStart;
1465+
LivePhysRegs LiveRegs(*TRI);
1466+
if (NonFrameStart != MBB.end()) {
1467+
getLivePhysRegsUpTo(*NonFrameStart, *TRI, LiveRegs);
1468+
// Ignore registers used for stack management for now.
1469+
LiveRegs.removeReg(AArch64::SP);
1470+
LiveRegs.removeReg(AArch64::X19);
1471+
LiveRegs.removeReg(AArch64::FP);
1472+
LiveRegs.removeReg(AArch64::LR);
1473+
}
1474+
#endif
1475+
14431476
bool IsFunclet = MBB.isEHFuncletEntry();
14441477

14451478
// At this point, we're going to decide whether or not the function uses a
@@ -1608,7 +1641,6 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
16081641
// Move past the saves of the callee-saved registers, fixing up the offsets
16091642
// and pre-inc if we decided to combine the callee-save and local stack
16101643
// pointer bump above.
1611-
MachineBasicBlock::iterator End = MBB.end();
16121644
while (MBBI != End && MBBI->getFlag(MachineInstr::FrameSetup) &&
16131645
!IsSVECalleeSave(MBBI)) {
16141646
if (CombineSPBump)
@@ -1908,6 +1940,19 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
19081940
emitCalleeSavedGPRLocations(MBB, MBBI);
19091941
emitCalleeSavedSVELocations(MBB, MBBI);
19101942
}
1943+
1944+
#ifndef NDEBUG
1945+
if (NonFrameStart != MBB.end()) {
1946+
// Check if any of the newly instructions clobber any of the live registers.
1947+
for (MachineInstr &MI :
1948+
make_range(MBB.instr_begin(), NonFrameStart->getIterator())) {
1949+
for (auto &Op : MI.operands())
1950+
if (Op.isReg() && Op.isDef())
1951+
assert(!LiveRegs.contains(Op.getReg()) &&
1952+
"live register clobbered by inserted prologue instructions");
1953+
}
1954+
}
1955+
#endif
19111956
}
19121957

19131958
static bool isFuncletReturnInstr(const MachineInstr &MI) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# RUN: not llc -mtriple=aarch64-none-linux-gnu -run-pass=prologepilog %s -o -
2+
#
3+
# REQUIRES: asserts
4+
#
5+
---
6+
# x9 is marked as live on function entry, but it will be used as scratch
7+
# register for prologue computations at the beginning of the prologue.
8+
# Use this to check we catch that the prologue clobbers $x9.
9+
name: x9_clobbered_on_fn_entry
10+
frameInfo:
11+
isFrameAddressTaken: true
12+
stack:
13+
- { id: 0, size: 16, alignment: 16 }
14+
- { id: 1, size: 32768, alignment: 32 }
15+
body: |
16+
bb.0:
17+
liveins: $x0, $x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17, $x18, $x19, $x20, $x21, $x22, $x23, $x24, $x25, $x26, $x27, $x28, $lr
18+
STRXui $x0, %stack.0, 0
19+
B %bb.1
20+
bb.1:
21+
liveins: $x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17, $x18, $x19, $x20, $x21, $x22, $x23, $x24, $x25, $x26, $x27, $x28, $lr
22+
RET_ReallyLR implicit $lr
23+
...

llvm/test/CodeGen/AArch64/framelayout-scavengingslot.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ stack:
1919
body: |
2020
bb.0:
2121
liveins: $x0, $x8
22+
$x9 = LDRXui $x0, 0 :: (load (s64))
2223
STRXui $x0, %stack.0, 0
2324
B %bb.1
2425
bb.1:

llvm/test/CodeGen/AArch64/framelayout-sve-calleesaves-fix.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
; CHECK-NEXT: ret
3131
...
3232
name: fix_restorepoint_p4
33+
tracksRegLiveness: true
3334
stack:
3435
- { id: 0, stack-id: scalable-vector, size: 16, alignment: 16 }
3536
body: |
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc -o - -mtriple=arm64e-apple-macosx %s | FileCheck %s
3+
4+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
5+
6+
define swifttailcc void @test_async_with_jumptable(ptr %src, ptr swiftasync %as) #0 {
7+
; CHECK-LABEL: test_async_with_jumptable:
8+
; CHECK: ; %bb.0: ; %entry
9+
; CHECK-NEXT: orr x29, x29, #0x1000000000000000
10+
; CHECK-NEXT: str x19, [sp, #-32]! ; 8-byte Folded Spill
11+
; CHECK-NEXT: stp x29, x30, [sp, #16] ; 16-byte Folded Spill
12+
; CHECK-NEXT: add x16, sp, #8
13+
; CHECK-NEXT: movk x16, #49946, lsl #48
14+
; CHECK-NEXT: mov x17, x22
15+
; CHECK-NEXT: pacdb x17, x16
16+
; CHECK-NEXT: str x17, [sp, #8]
17+
; CHECK-NEXT: add x29, sp, #16
18+
; CHECK-NEXT: .cfi_def_cfa w29, 16
19+
; CHECK-NEXT: .cfi_offset w30, -8
20+
; CHECK-NEXT: .cfi_offset w29, -16
21+
; CHECK-NEXT: .cfi_offset w19, -32
22+
; CHECK-NEXT: mov x20, x22
23+
; CHECK-NEXT: ldr x8, [x0]
24+
; CHECK-NEXT: cmp x8, #2
25+
; CHECK-NEXT: csel x9, xzr, x0, eq
26+
; CHECK-NEXT: cmp x8, #0
27+
; CHECK-NEXT: csel x10, x22, xzr, eq
28+
; CHECK-NEXT: cmp x8, #1
29+
; CHECK-NEXT: csel x19, x9, x10, gt
30+
; CHECK-NEXT: mov x22, x0
31+
; CHECK-NEXT: bl _foo
32+
; CHECK-NEXT: mov x2, x0
33+
; CHECK-NEXT: mov x0, x19
34+
; CHECK-NEXT: mov x1, x20
35+
; CHECK-NEXT: ldp x29, x30, [sp, #16] ; 16-byte Folded Reload
36+
; CHECK-NEXT: ldr x19, [sp], #32 ; 8-byte Folded Reload
37+
; CHECK-NEXT: and x29, x29, #0xefffffffffffffff
38+
; CHECK-NEXT: br x2
39+
entry:
40+
%l = load i64, ptr %src, align 8
41+
switch i64 %l, label %dead [
42+
i64 0, label %exit
43+
i64 1, label %then.1
44+
i64 2, label %then.2
45+
i64 3, label %then.3
46+
]
47+
48+
then.1:
49+
br label %exit
50+
51+
then.2:
52+
br label %exit
53+
54+
then.3:
55+
br label %exit
56+
57+
dead: ; preds = %entryresume.5
58+
unreachable
59+
60+
exit:
61+
%p = phi ptr [ %src, %then.3 ], [ null, %then.2 ], [ %as, %entry ], [ null, %then.1 ]
62+
%r = call i64 @foo()
63+
%fn = inttoptr i64 %r to ptr
64+
musttail call swifttailcc void %fn(ptr swiftasync %src, ptr %p, ptr %as)
65+
ret void
66+
}
67+
68+
declare i64 @foo()
69+
70+
attributes #0 = { "frame-pointer"="non-leaf" }

0 commit comments

Comments
 (0)