Skip to content

Commit c9dc643

Browse files
committed
fixup! [llvm][X86] Fix merging of large sp updates
[llvm][X86] Fix cfi_adjust_cfa_offset when merging large sp updates
1 parent 73ec203 commit c9dc643

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,11 @@ MachineInstrBuilder X86FrameLowering::BuildStackAdjustment(
393393
return MI;
394394
}
395395

396-
template <typename T>
396+
template <typename FoundT, typename CalcT>
397397
int64_t X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB,
398398
MachineBasicBlock::iterator &MBBI,
399-
T CalcNewOffset,
399+
FoundT FoundStackAdjust,
400+
CalcT CalcNewOffset,
400401
bool doMergeWithPrevious) const {
401402
if ((doMergeWithPrevious && MBBI == MBB.begin()) ||
402403
(!doMergeWithPrevious && MBBI == MBB.end()))
@@ -442,6 +443,7 @@ int64_t X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB,
442443
} else
443444
return CalcNewOffset(0);
444445

446+
FoundStackAdjust(PI, Offset);
445447
if (std::abs((int64_t)CalcNewOffset(Offset)) < MaxSPChunk)
446448
break;
447449

@@ -3839,13 +3841,24 @@ MachineBasicBlock::iterator X86FrameLowering::eliminateCallFramePseudoInstr(
38393841

38403842
// Add Amount to SP to destroy a frame, or subtract to setup.
38413843
int64_t StackAdjustment = isDestroy ? Amount : -Amount;
3844+
int64_t CfaAdjustment = StackAdjustment;
38423845

38433846
if (StackAdjustment) {
38443847
// Merge with any previous or following adjustment instruction. Note: the
38453848
// instructions merged with here do not have CFI, so their stack
3846-
// adjustments do not feed into CfaAdjustment.
3847-
StackAdjustment = mergeSPAdd(MBB, InsertPos, StackAdjustment, true);
3848-
StackAdjustment = mergeSPAdd(MBB, InsertPos, StackAdjustment, false);
3849+
// adjustments do not feed into CfaAdjustment
3850+
3851+
auto FoundStackAdjust = [&CfaAdjustment](MachineBasicBlock::iterator PI,
3852+
int64_t Offset) {
3853+
CfaAdjustment += Offset;
3854+
};
3855+
auto CalcNewOffset = [&StackAdjustment](int64_t Offset) {
3856+
return StackAdjustment + Offset;
3857+
};
3858+
StackAdjustment =
3859+
mergeSPUpdates(MBB, InsertPos, FoundStackAdjust, CalcNewOffset, true);
3860+
StackAdjustment = mergeSPUpdates(MBB, InsertPos, FoundStackAdjust,
3861+
CalcNewOffset, false);
38493862

38503863
if (StackAdjustment) {
38513864
if (!(F.hasMinSize() &&
@@ -3855,22 +3868,19 @@ MachineBasicBlock::iterator X86FrameLowering::eliminateCallFramePseudoInstr(
38553868
}
38563869
}
38573870

3858-
if (DwarfCFI && !hasFP(MF)) {
3871+
if (DwarfCFI && !hasFP(MF) && CfaAdjustment) {
38593872
// If we don't have FP, but need to generate unwind information,
38603873
// we need to set the correct CFA offset after the stack adjustment.
38613874
// How much we adjust the CFA offset depends on whether we're emitting
38623875
// CFI only for EH purposes or for debugging. EH only requires the CFA
38633876
// offset to be correct at each call site, while for debugging we want
38643877
// it to be more precise.
38653878

3866-
int64_t CfaAdjustment = -StackAdjustment;
38673879
// TODO: When not using precise CFA, we also need to adjust for the
38683880
// InternalAmt here.
3869-
if (CfaAdjustment) {
3870-
BuildCFI(
3871-
MBB, InsertPos, DL,
3872-
MCCFIInstruction::createAdjustCfaOffset(nullptr, CfaAdjustment));
3873-
}
3881+
BuildCFI(
3882+
MBB, InsertPos, DL,
3883+
MCCFIInstruction::createAdjustCfaOffset(nullptr, -CfaAdjustment));
38743884
}
38753885

38763886
return I;

llvm/lib/Target/X86/X86FrameLowering.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ class X86FrameLowering : public TargetFrameLowering {
139139
/// if (instruction before/after the passed instruction is ADD/SUB/LEA)
140140
/// Offset = instruction stack adjustment
141141
/// ... positive value for ADD/LEA and negative for SUB
142+
/// FoundStackAdjust(instruction, Offset)
143+
/// erase(instruction)
142144
/// return CalcNewOffset(Offset)
143145
/// else
144146
/// return CalcNewOffset(0)
@@ -147,13 +149,26 @@ class X86FrameLowering : public TargetFrameLowering {
147149
/// before/after MBBI for large adjustments that have been split into multiple
148150
/// instructions.
149151
///
152+
/// FoundStackAdjust should have the signature:
153+
/// void FoundStackAdjust(MachineBasicBlock::iterator PI, int64_t Offset)
150154
/// CalcNewOffset should have the signature:
151155
/// int64_t CalcNewOffset(int64_t Offset)
152-
template <typename T>
156+
template <typename FoundT, typename CalcT>
153157
int64_t mergeSPUpdates(MachineBasicBlock &MBB,
154-
MachineBasicBlock::iterator &MBBI, T CalcNewOffset,
158+
MachineBasicBlock::iterator &MBBI,
159+
FoundT FoundStackAdjust, CalcT CalcNewOffset,
155160
bool doMergeWithPrevious) const;
156161

162+
template <typename CalcT>
163+
int64_t mergeSPUpdates(MachineBasicBlock &MBB,
164+
MachineBasicBlock::iterator &MBBI, CalcT CalcNewOffset,
165+
bool doMergeWithPrevious) const {
166+
auto FoundStackAdjust = [](MachineBasicBlock::iterator MBBI,
167+
int64_t Offset) {};
168+
return mergeSPUpdates(MBB, MBBI, FoundStackAdjust, CalcNewOffset,
169+
doMergeWithPrevious);
170+
}
171+
157172
public:
158173
/// Equivalent to:
159174
/// mergeSPUpdates(MBB, MBBI,

llvm/test/CodeGen/X86/merge-huge-sp-updates.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc < %s -O3 -mtriple=x86_64-linux-unknown -verify-machineinstrs -o %t.s
1+
; RUN: llc < %s -mtriple=x86_64-linux-unknown -verify-machineinstrs -o %t.s
22
; RUN: FileCheck --input-file=%t.s %s
33

44
; Double-check that we are able to assemble the generated '.s'. A symptom of the
@@ -23,6 +23,7 @@ entry:
2323
; CHECK: call{{.*}}bar
2424
; CHECK: addq{{.*}}$2147483647, %rsp
2525
; CHECK: addq{{.*}}$372037585, %rsp
26+
; CHECK: .cfi_adjust_cfa_offset -2519521232
2627
ret void
2728
}
2829

0 commit comments

Comments
 (0)