Skip to content

Commit

Permalink
[X86] Fix for offsets of CFA directives
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
theo25 authored and phoebewang committed Feb 28, 2023
1 parent bf9e0ed commit 2e0940c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
6 changes: 4 additions & 2 deletions llvm/lib/Target/X86/X86FrameLowering.cpp
Expand Up @@ -1645,14 +1645,16 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
// Define the current CFA rule to use the provided offset.
assert(StackSize);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::cfiDefCfaOffset(nullptr, -2 * stackGrowth),
MCCFIInstruction::cfiDefCfaOffset(
nullptr, -2 * stackGrowth + (int)TailCallArgReserveSize),
MachineInstr::FrameSetup);

// Change the rule for the FramePtr to be an "offset" rule.
unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
BuildCFI(MBB, MBBI, DL,
MCCFIInstruction::createOffset(nullptr, DwarfFramePtr,
2 * stackGrowth),
2 * stackGrowth -
(int)TailCallArgReserveSize),
MachineInstr::FrameSetup);
}

Expand Down
47 changes: 47 additions & 0 deletions llvm/test/CodeGen/X86/tailcc-dwarf.ll
@@ -0,0 +1,47 @@
; RUN: llc -mtriple=x86_64-unknown-linux-gnu -O0 --frame-pointer=non-leaf %s -o - | FileCheck %s

%block = type { %blockheader, [0 x i64*] }
%blockheader = type { i64 }

define void @scanStackRoots(i32) {
ret void
}

define i32 @main(i32 %argc, i8** %argv) {
entry:
%0 = call tailcc %block* @apply_rule_6870(%block* null, %block* null)
ret i32 0
}

define internal tailcc %block* @apply_rule_6870(%block* %0, %block* %1) {
entry:
%2 = tail call tailcc %block* @sender12(%block* %0, %block* %1)
ret %block* null
}

define internal tailcc %block* @sender12(%block* %0, %block* %1) {
; CHECK-LABEL: sender12:
; CHECK: .cfi_startproc
; CHECK: subq $8160, %rsp
; CHECK: pushq %rbp
; CHECK: .cfi_def_cfa_offset 8176
; CHECK: .cfi_offset %rbp, -8176
entry:
%a = alloca [1024 x i32]
%b = load [1024 x i32], [1024 x i32]* %a
call void @scanStackRoots(i32 1)
%2 = tail call tailcc %block* @apply_rule_6300(%block* %0, %block* %1, [1024 x i32] %b)
ret %block* %2
}

define internal tailcc %block* @apply_rule_6300(%block* %0, %block* %1, [1024 x i32] %2) {
entry:
%3 = tail call tailcc %block* @sender4(%block* %0, %block* %1)
ret %block* %3
}

define internal tailcc %block* @sender4(%block* %0, %block* %1) {
entry:
call void @scanStackRoots(i32 2)
ret %block* null
}

0 comments on commit 2e0940c

Please sign in to comment.