@@ -393,10 +393,11 @@ MachineInstrBuilder X86FrameLowering::BuildStackAdjustment(
393393 return MI;
394394}
395395
396- template <typename T >
396+ template <typename FoundT, typename CalcT >
397397int64_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;
0 commit comments