Skip to content

Commit 2e0940c

Browse files
theo25phoebewang
authored andcommitted
[X86] Fix for offsets of CFA directives
`emitPrologue` may insert stack pointer adjustment in tail call optimized functions where the callee argument stack size is bigger than the caller's. In such a case, the adjustment must be taken into account when generating CFA directives. Reviewed By: pengfei Differential Revision: https://reviews.llvm.org/D143618
1 parent bf9e0ed commit 2e0940c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,14 +1645,16 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
16451645
// Define the current CFA rule to use the provided offset.
16461646
assert(StackSize);
16471647
BuildCFI(MBB, MBBI, DL,
1648-
MCCFIInstruction::cfiDefCfaOffset(nullptr, -2 * stackGrowth),
1648+
MCCFIInstruction::cfiDefCfaOffset(
1649+
nullptr, -2 * stackGrowth + (int)TailCallArgReserveSize),
16491650
MachineInstr::FrameSetup);
16501651

16511652
// Change the rule for the FramePtr to be an "offset" rule.
16521653
unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
16531654
BuildCFI(MBB, MBBI, DL,
16541655
MCCFIInstruction::createOffset(nullptr, DwarfFramePtr,
1655-
2 * stackGrowth),
1656+
2 * stackGrowth -
1657+
(int)TailCallArgReserveSize),
16561658
MachineInstr::FrameSetup);
16571659
}
16581660

llvm/test/CodeGen/X86/tailcc-dwarf.ll

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; RUN: llc -mtriple=x86_64-unknown-linux-gnu -O0 --frame-pointer=non-leaf %s -o - | FileCheck %s
2+
3+
%block = type { %blockheader, [0 x i64*] }
4+
%blockheader = type { i64 }
5+
6+
define void @scanStackRoots(i32) {
7+
ret void
8+
}
9+
10+
define i32 @main(i32 %argc, i8** %argv) {
11+
entry:
12+
%0 = call tailcc %block* @apply_rule_6870(%block* null, %block* null)
13+
ret i32 0
14+
}
15+
16+
define internal tailcc %block* @apply_rule_6870(%block* %0, %block* %1) {
17+
entry:
18+
%2 = tail call tailcc %block* @sender12(%block* %0, %block* %1)
19+
ret %block* null
20+
}
21+
22+
define internal tailcc %block* @sender12(%block* %0, %block* %1) {
23+
; CHECK-LABEL: sender12:
24+
; CHECK: .cfi_startproc
25+
; CHECK: subq $8160, %rsp
26+
; CHECK: pushq %rbp
27+
; CHECK: .cfi_def_cfa_offset 8176
28+
; CHECK: .cfi_offset %rbp, -8176
29+
entry:
30+
%a = alloca [1024 x i32]
31+
%b = load [1024 x i32], [1024 x i32]* %a
32+
call void @scanStackRoots(i32 1)
33+
%2 = tail call tailcc %block* @apply_rule_6300(%block* %0, %block* %1, [1024 x i32] %b)
34+
ret %block* %2
35+
}
36+
37+
define internal tailcc %block* @apply_rule_6300(%block* %0, %block* %1, [1024 x i32] %2) {
38+
entry:
39+
%3 = tail call tailcc %block* @sender4(%block* %0, %block* %1)
40+
ret %block* %3
41+
}
42+
43+
define internal tailcc %block* @sender4(%block* %0, %block* %1) {
44+
entry:
45+
call void @scanStackRoots(i32 2)
46+
ret %block* null
47+
}

0 commit comments

Comments
 (0)