10b57cec5SDimitry Andric //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "SystemZFrameLowering.h"
100b57cec5SDimitry Andric #include "SystemZCallingConv.h"
110b57cec5SDimitry Andric #include "SystemZInstrBuilder.h"
120b57cec5SDimitry Andric #include "SystemZInstrInfo.h"
130b57cec5SDimitry Andric #include "SystemZMachineFunctionInfo.h"
140b57cec5SDimitry Andric #include "SystemZRegisterInfo.h"
150b57cec5SDimitry Andric #include "SystemZSubtarget.h"
1681ad6265SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
20e710425bSDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
210b57cec5SDimitry Andric #include "llvm/IR/Function.h"
225ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric namespace {
27480093f4SDimitry Andric // The ABI-defined register save slots, relative to the CFA (i.e.
28fe6060f1SDimitry Andric // incoming stack pointer + SystemZMC::ELFCallFrameSize).
29349cc55cSDimitry Andric static const TargetFrameLowering::SpillSlot ELFSpillOffsetTable[] = {
300b57cec5SDimitry Andric { SystemZ::R2D, 0x10 },
310b57cec5SDimitry Andric { SystemZ::R3D, 0x18 },
320b57cec5SDimitry Andric { SystemZ::R4D, 0x20 },
330b57cec5SDimitry Andric { SystemZ::R5D, 0x28 },
340b57cec5SDimitry Andric { SystemZ::R6D, 0x30 },
350b57cec5SDimitry Andric { SystemZ::R7D, 0x38 },
360b57cec5SDimitry Andric { SystemZ::R8D, 0x40 },
370b57cec5SDimitry Andric { SystemZ::R9D, 0x48 },
380b57cec5SDimitry Andric { SystemZ::R10D, 0x50 },
390b57cec5SDimitry Andric { SystemZ::R11D, 0x58 },
400b57cec5SDimitry Andric { SystemZ::R12D, 0x60 },
410b57cec5SDimitry Andric { SystemZ::R13D, 0x68 },
420b57cec5SDimitry Andric { SystemZ::R14D, 0x70 },
430b57cec5SDimitry Andric { SystemZ::R15D, 0x78 },
440b57cec5SDimitry Andric { SystemZ::F0D, 0x80 },
450b57cec5SDimitry Andric { SystemZ::F2D, 0x88 },
460b57cec5SDimitry Andric { SystemZ::F4D, 0x90 },
470b57cec5SDimitry Andric { SystemZ::F6D, 0x98 }
480b57cec5SDimitry Andric };
49349cc55cSDimitry Andric
50349cc55cSDimitry Andric static const TargetFrameLowering::SpillSlot XPLINKSpillOffsetTable[] = {
51349cc55cSDimitry Andric {SystemZ::R4D, 0x00}, {SystemZ::R5D, 0x08}, {SystemZ::R6D, 0x10},
52349cc55cSDimitry Andric {SystemZ::R7D, 0x18}, {SystemZ::R8D, 0x20}, {SystemZ::R9D, 0x28},
53349cc55cSDimitry Andric {SystemZ::R10D, 0x30}, {SystemZ::R11D, 0x38}, {SystemZ::R12D, 0x40},
54349cc55cSDimitry Andric {SystemZ::R13D, 0x48}, {SystemZ::R14D, 0x50}, {SystemZ::R15D, 0x58}};
550b57cec5SDimitry Andric } // end anonymous namespace
560b57cec5SDimitry Andric
SystemZFrameLowering(StackDirection D,Align StackAl,int LAO,Align TransAl,bool StackReal)57349cc55cSDimitry Andric SystemZFrameLowering::SystemZFrameLowering(StackDirection D, Align StackAl,
58349cc55cSDimitry Andric int LAO, Align TransAl,
59349cc55cSDimitry Andric bool StackReal)
60349cc55cSDimitry Andric : TargetFrameLowering(D, StackAl, LAO, TransAl, StackReal) {}
61480093f4SDimitry Andric
62349cc55cSDimitry Andric std::unique_ptr<SystemZFrameLowering>
create(const SystemZSubtarget & STI)63349cc55cSDimitry Andric SystemZFrameLowering::create(const SystemZSubtarget &STI) {
64349cc55cSDimitry Andric if (STI.isTargetXPLINK64())
65349cc55cSDimitry Andric return std::make_unique<SystemZXPLINKFrameLowering>();
66349cc55cSDimitry Andric return std::make_unique<SystemZELFFrameLowering>();
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator MI) const69349cc55cSDimitry Andric MachineBasicBlock::iterator SystemZFrameLowering::eliminateCallFramePseudoInstr(
70349cc55cSDimitry Andric MachineFunction &MF, MachineBasicBlock &MBB,
71349cc55cSDimitry Andric MachineBasicBlock::iterator MI) const {
72349cc55cSDimitry Andric switch (MI->getOpcode()) {
73349cc55cSDimitry Andric case SystemZ::ADJCALLSTACKDOWN:
74349cc55cSDimitry Andric case SystemZ::ADJCALLSTACKUP:
75349cc55cSDimitry Andric assert(hasReservedCallFrame(MF) &&
76349cc55cSDimitry Andric "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
77349cc55cSDimitry Andric return MBB.erase(MI);
78349cc55cSDimitry Andric break;
79349cc55cSDimitry Andric
80349cc55cSDimitry Andric default:
81349cc55cSDimitry Andric llvm_unreachable("Unexpected call frame instruction");
82349cc55cSDimitry Andric }
83349cc55cSDimitry Andric }
84349cc55cSDimitry Andric
851fd87a68SDimitry Andric namespace {
861fd87a68SDimitry Andric struct SZFrameSortingObj {
871fd87a68SDimitry Andric bool IsValid = false; // True if we care about this Object.
881fd87a68SDimitry Andric uint32_t ObjectIndex = 0; // Index of Object into MFI list.
891fd87a68SDimitry Andric uint64_t ObjectSize = 0; // Size of Object in bytes.
901fd87a68SDimitry Andric uint32_t D12Count = 0; // 12-bit displacement only.
911fd87a68SDimitry Andric uint32_t DPairCount = 0; // 12 or 20 bit displacement.
921fd87a68SDimitry Andric };
931fd87a68SDimitry Andric typedef std::vector<SZFrameSortingObj> SZFrameObjVec;
941fd87a68SDimitry Andric } // namespace
951fd87a68SDimitry Andric
961fd87a68SDimitry Andric // TODO: Move to base class.
orderFrameObjects(const MachineFunction & MF,SmallVectorImpl<int> & ObjectsToAllocate) const971fd87a68SDimitry Andric void SystemZELFFrameLowering::orderFrameObjects(
981fd87a68SDimitry Andric const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const {
991fd87a68SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
10081ad6265SDimitry Andric auto *TII = MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
1011fd87a68SDimitry Andric
1021fd87a68SDimitry Andric // Make a vector of sorting objects to track all MFI objects and mark those
1031fd87a68SDimitry Andric // to be sorted as valid.
1041fd87a68SDimitry Andric if (ObjectsToAllocate.size() <= 1)
1051fd87a68SDimitry Andric return;
1061fd87a68SDimitry Andric SZFrameObjVec SortingObjects(MFI.getObjectIndexEnd());
1071fd87a68SDimitry Andric for (auto &Obj : ObjectsToAllocate) {
1081fd87a68SDimitry Andric SortingObjects[Obj].IsValid = true;
1091fd87a68SDimitry Andric SortingObjects[Obj].ObjectIndex = Obj;
1101fd87a68SDimitry Andric SortingObjects[Obj].ObjectSize = MFI.getObjectSize(Obj);
1111fd87a68SDimitry Andric }
1121fd87a68SDimitry Andric
1131fd87a68SDimitry Andric // Examine uses for each object and record short (12-bit) and "pair"
1141fd87a68SDimitry Andric // displacement types.
1151fd87a68SDimitry Andric for (auto &MBB : MF)
1161fd87a68SDimitry Andric for (auto &MI : MBB) {
1171fd87a68SDimitry Andric if (MI.isDebugInstr())
1181fd87a68SDimitry Andric continue;
1191fd87a68SDimitry Andric for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
1201fd87a68SDimitry Andric const MachineOperand &MO = MI.getOperand(I);
1211fd87a68SDimitry Andric if (!MO.isFI())
1221fd87a68SDimitry Andric continue;
1231fd87a68SDimitry Andric int Index = MO.getIndex();
1241fd87a68SDimitry Andric if (Index >= 0 && Index < MFI.getObjectIndexEnd() &&
1251fd87a68SDimitry Andric SortingObjects[Index].IsValid) {
1261fd87a68SDimitry Andric if (TII->hasDisplacementPairInsn(MI.getOpcode()))
1271fd87a68SDimitry Andric SortingObjects[Index].DPairCount++;
1281fd87a68SDimitry Andric else if (!(MI.getDesc().TSFlags & SystemZII::Has20BitOffset))
1291fd87a68SDimitry Andric SortingObjects[Index].D12Count++;
1301fd87a68SDimitry Andric }
1311fd87a68SDimitry Andric }
1321fd87a68SDimitry Andric }
1331fd87a68SDimitry Andric
1341fd87a68SDimitry Andric // Sort all objects for short/paired displacements, which should be
1351fd87a68SDimitry Andric // sufficient as it seems like all frame objects typically are within the
1361fd87a68SDimitry Andric // long displacement range. Sorting works by computing the "density" as
1371fd87a68SDimitry Andric // Count / ObjectSize. The comparisons of two such fractions are refactored
1381fd87a68SDimitry Andric // by multiplying both sides with A.ObjectSize * B.ObjectSize, in order to
1391fd87a68SDimitry Andric // eliminate the (fp) divisions. A higher density object needs to go after
1401fd87a68SDimitry Andric // in the list in order for it to end up lower on the stack.
1411fd87a68SDimitry Andric auto CmpD12 = [](const SZFrameSortingObj &A, const SZFrameSortingObj &B) {
1421fd87a68SDimitry Andric // Put all invalid and variable sized objects at the end.
1431fd87a68SDimitry Andric if (!A.IsValid || !B.IsValid)
1441fd87a68SDimitry Andric return A.IsValid;
1451fd87a68SDimitry Andric if (!A.ObjectSize || !B.ObjectSize)
1461fd87a68SDimitry Andric return A.ObjectSize > 0;
1471fd87a68SDimitry Andric uint64_t ADensityCmp = A.D12Count * B.ObjectSize;
1481fd87a68SDimitry Andric uint64_t BDensityCmp = B.D12Count * A.ObjectSize;
1491fd87a68SDimitry Andric if (ADensityCmp != BDensityCmp)
1501fd87a68SDimitry Andric return ADensityCmp < BDensityCmp;
1511fd87a68SDimitry Andric return A.DPairCount * B.ObjectSize < B.DPairCount * A.ObjectSize;
1521fd87a68SDimitry Andric };
1531fd87a68SDimitry Andric std::stable_sort(SortingObjects.begin(), SortingObjects.end(), CmpD12);
1541fd87a68SDimitry Andric
1551fd87a68SDimitry Andric // Now modify the original list to represent the final order that
1561fd87a68SDimitry Andric // we want.
1571fd87a68SDimitry Andric unsigned Idx = 0;
1581fd87a68SDimitry Andric for (auto &Obj : SortingObjects) {
1591fd87a68SDimitry Andric // All invalid items are sorted at the end, so it's safe to stop.
1601fd87a68SDimitry Andric if (!Obj.IsValid)
1611fd87a68SDimitry Andric break;
1621fd87a68SDimitry Andric ObjectsToAllocate[Idx++] = Obj.ObjectIndex;
1631fd87a68SDimitry Andric }
1641fd87a68SDimitry Andric }
1651fd87a68SDimitry Andric
hasReservedCallFrame(const MachineFunction & MF) const166349cc55cSDimitry Andric bool SystemZFrameLowering::hasReservedCallFrame(
167349cc55cSDimitry Andric const MachineFunction &MF) const {
168349cc55cSDimitry Andric // The ELF ABI requires us to allocate 160 bytes of stack space for the
169349cc55cSDimitry Andric // callee, with any outgoing stack arguments being placed above that. It
170349cc55cSDimitry Andric // seems better to make that area a permanent feature of the frame even if
171349cc55cSDimitry Andric // we're using a frame pointer. Similarly, 64-bit XPLINK requires 96 bytes
172349cc55cSDimitry Andric // of stack space for the register save area.
173349cc55cSDimitry Andric return true;
174349cc55cSDimitry Andric }
175349cc55cSDimitry Andric
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const176349cc55cSDimitry Andric bool SystemZELFFrameLowering::assignCalleeSavedSpillSlots(
177349cc55cSDimitry Andric MachineFunction &MF, const TargetRegisterInfo *TRI,
178480093f4SDimitry Andric std::vector<CalleeSavedInfo> &CSI) const {
179480093f4SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
180480093f4SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
181480093f4SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
182480093f4SDimitry Andric if (CSI.empty())
183480093f4SDimitry Andric return true; // Early exit if no callee saved registers are modified!
184480093f4SDimitry Andric
185480093f4SDimitry Andric unsigned LowGPR = 0;
186480093f4SDimitry Andric unsigned HighGPR = SystemZ::R15D;
187fe6060f1SDimitry Andric int StartSPOffset = SystemZMC::ELFCallFrameSize;
188480093f4SDimitry Andric for (auto &CS : CSI) {
18904eeddc0SDimitry Andric Register Reg = CS.getReg();
1905ffd83dbSDimitry Andric int Offset = getRegSpillOffset(MF, Reg);
191480093f4SDimitry Andric if (Offset) {
192480093f4SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg) && StartSPOffset > Offset) {
193480093f4SDimitry Andric LowGPR = Reg;
194480093f4SDimitry Andric StartSPOffset = Offset;
195480093f4SDimitry Andric }
196fe6060f1SDimitry Andric Offset -= SystemZMC::ELFCallFrameSize;
197480093f4SDimitry Andric int FrameIdx = MFFrame.CreateFixedSpillStackObject(8, Offset);
198480093f4SDimitry Andric CS.setFrameIdx(FrameIdx);
199480093f4SDimitry Andric } else
200480093f4SDimitry Andric CS.setFrameIdx(INT32_MAX);
201480093f4SDimitry Andric }
202480093f4SDimitry Andric
203480093f4SDimitry Andric // Save the range of call-saved registers, for use by the
204480093f4SDimitry Andric // prologue/epilogue inserters.
205480093f4SDimitry Andric ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset);
206480093f4SDimitry Andric if (IsVarArg) {
207480093f4SDimitry Andric // Also save the GPR varargs, if any. R6D is call-saved, so would
208480093f4SDimitry Andric // already be included, but we also need to handle the call-clobbered
209480093f4SDimitry Andric // argument registers.
21004eeddc0SDimitry Andric Register FirstGPR = ZFI->getVarArgsFirstGPR();
211fe6060f1SDimitry Andric if (FirstGPR < SystemZ::ELFNumArgGPRs) {
212fe6060f1SDimitry Andric unsigned Reg = SystemZ::ELFArgGPRs[FirstGPR];
2135ffd83dbSDimitry Andric int Offset = getRegSpillOffset(MF, Reg);
214480093f4SDimitry Andric if (StartSPOffset > Offset) {
215480093f4SDimitry Andric LowGPR = Reg; StartSPOffset = Offset;
216480093f4SDimitry Andric }
217480093f4SDimitry Andric }
218480093f4SDimitry Andric }
219480093f4SDimitry Andric ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset);
220480093f4SDimitry Andric
221480093f4SDimitry Andric // Create fixed stack objects for the remaining registers.
222fe6060f1SDimitry Andric int CurrOffset = -SystemZMC::ELFCallFrameSize;
2235ffd83dbSDimitry Andric if (usePackedStack(MF))
2245ffd83dbSDimitry Andric CurrOffset += StartSPOffset;
2255ffd83dbSDimitry Andric
226480093f4SDimitry Andric for (auto &CS : CSI) {
227480093f4SDimitry Andric if (CS.getFrameIdx() != INT32_MAX)
228480093f4SDimitry Andric continue;
22904eeddc0SDimitry Andric Register Reg = CS.getReg();
230480093f4SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
231480093f4SDimitry Andric unsigned Size = TRI->getSpillSize(*RC);
232480093f4SDimitry Andric CurrOffset -= Size;
233480093f4SDimitry Andric assert(CurrOffset % 8 == 0 &&
234480093f4SDimitry Andric "8-byte alignment required for for all register save slots");
235480093f4SDimitry Andric int FrameIdx = MFFrame.CreateFixedSpillStackObject(Size, CurrOffset);
236480093f4SDimitry Andric CS.setFrameIdx(FrameIdx);
237480093f4SDimitry Andric }
238480093f4SDimitry Andric
239480093f4SDimitry Andric return true;
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const242349cc55cSDimitry Andric void SystemZELFFrameLowering::determineCalleeSaves(MachineFunction &MF,
2430b57cec5SDimitry Andric BitVector &SavedRegs,
2440b57cec5SDimitry Andric RegScavenger *RS) const {
2450b57cec5SDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
2460b57cec5SDimitry Andric
2470b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
2480b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2490b57cec5SDimitry Andric bool HasFP = hasFP(MF);
2500b57cec5SDimitry Andric SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
2510b57cec5SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric // va_start stores incoming FPR varargs in the normal way, but delegates
2540b57cec5SDimitry Andric // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
2550b57cec5SDimitry Andric // Record these pending uses, which typically include the call-saved
2560b57cec5SDimitry Andric // argument register R6D.
2570b57cec5SDimitry Andric if (IsVarArg)
258fe6060f1SDimitry Andric for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
259fe6060f1SDimitry Andric SavedRegs.set(SystemZ::ELFArgGPRs[I]);
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric // If there are any landing pads, entering them will modify r6/r7.
2620b57cec5SDimitry Andric if (!MF.getLandingPads().empty()) {
2630b57cec5SDimitry Andric SavedRegs.set(SystemZ::R6D);
2640b57cec5SDimitry Andric SavedRegs.set(SystemZ::R7D);
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric // If the function requires a frame pointer, record that the hard
2680b57cec5SDimitry Andric // frame pointer will be clobbered.
2690b57cec5SDimitry Andric if (HasFP)
2700b57cec5SDimitry Andric SavedRegs.set(SystemZ::R11D);
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric // If the function calls other functions, record that the return
2730b57cec5SDimitry Andric // address register will be clobbered.
2740b57cec5SDimitry Andric if (MFFrame.hasCalls())
2750b57cec5SDimitry Andric SavedRegs.set(SystemZ::R14D);
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric // If we are saving GPRs other than the stack pointer, we might as well
2780b57cec5SDimitry Andric // save and restore the stack pointer at the same time, via STMG and LMG.
2790b57cec5SDimitry Andric // This allows the deallocation to be done by the LMG, rather than needing
2800b57cec5SDimitry Andric // a separate %r15 addition.
2810b57cec5SDimitry Andric const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
2820b57cec5SDimitry Andric for (unsigned I = 0; CSRegs[I]; ++I) {
2830b57cec5SDimitry Andric unsigned Reg = CSRegs[I];
2840b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
2850b57cec5SDimitry Andric SavedRegs.set(SystemZ::R15D);
2860b57cec5SDimitry Andric break;
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric
SystemZELFFrameLowering()291349cc55cSDimitry Andric SystemZELFFrameLowering::SystemZELFFrameLowering()
292349cc55cSDimitry Andric : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8), 0,
293349cc55cSDimitry Andric Align(8), /* StackRealignable */ false),
294349cc55cSDimitry Andric RegSpillOffsets(0) {
295349cc55cSDimitry Andric
296349cc55cSDimitry Andric // Due to the SystemZ ABI, the DWARF CFA (Canonical Frame Address) is not
297349cc55cSDimitry Andric // equal to the incoming stack pointer, but to incoming stack pointer plus
298349cc55cSDimitry Andric // 160. Instead of using a Local Area Offset, the Register save area will
299349cc55cSDimitry Andric // be occupied by fixed frame objects, and all offsets are actually
300349cc55cSDimitry Andric // relative to CFA.
301349cc55cSDimitry Andric
302349cc55cSDimitry Andric // Create a mapping from register number to save slot offset.
303349cc55cSDimitry Andric // These offsets are relative to the start of the register save area.
304349cc55cSDimitry Andric RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
305bdd1243dSDimitry Andric for (const auto &Entry : ELFSpillOffsetTable)
306bdd1243dSDimitry Andric RegSpillOffsets[Entry.Reg] = Entry.Offset;
307349cc55cSDimitry Andric }
308349cc55cSDimitry Andric
3090b57cec5SDimitry Andric // Add GPR64 to the save instruction being built by MIB, which is in basic
3100b57cec5SDimitry Andric // block MBB. IsImplicit says whether this is an explicit operand to the
3110b57cec5SDimitry Andric // instruction, or an implicit one that comes between the explicit start
3120b57cec5SDimitry Andric // and end registers.
addSavedGPR(MachineBasicBlock & MBB,MachineInstrBuilder & MIB,unsigned GPR64,bool IsImplicit)3130b57cec5SDimitry Andric static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
3140b57cec5SDimitry Andric unsigned GPR64, bool IsImplicit) {
3150b57cec5SDimitry Andric const TargetRegisterInfo *RI =
3160b57cec5SDimitry Andric MBB.getParent()->getSubtarget().getRegisterInfo();
3178bcb0991SDimitry Andric Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
3180b57cec5SDimitry Andric bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
3190b57cec5SDimitry Andric if (!IsLive || !IsImplicit) {
3200b57cec5SDimitry Andric MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
3210b57cec5SDimitry Andric if (!IsLive)
3220b57cec5SDimitry Andric MBB.addLiveIn(GPR64);
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const326349cc55cSDimitry Andric bool SystemZELFFrameLowering::spillCalleeSavedRegisters(
3275ffd83dbSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
3285ffd83dbSDimitry Andric ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
3290b57cec5SDimitry Andric if (CSI.empty())
3300b57cec5SDimitry Andric return false;
3310b57cec5SDimitry Andric
3320b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent();
3330b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
3340b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
3350b57cec5SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
3360b57cec5SDimitry Andric DebugLoc DL;
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric // Save GPRs
339480093f4SDimitry Andric SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
340480093f4SDimitry Andric if (SpillGPRs.LowGPR) {
341480093f4SDimitry Andric assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
342480093f4SDimitry Andric "Should be saving %r15 and something else");
3430b57cec5SDimitry Andric
3440b57cec5SDimitry Andric // Build an STMG instruction.
3450b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric // Add the explicit register operands.
348480093f4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
349480093f4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric // Add the address.
352480093f4SDimitry Andric MIB.addReg(SystemZ::R15D).addImm(SpillGPRs.GPROffset);
3530b57cec5SDimitry Andric
3540b57cec5SDimitry Andric // Make sure all call-saved GPRs are included as operands and are
3550b57cec5SDimitry Andric // marked as live on entry.
3564824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
35704eeddc0SDimitry Andric Register Reg = I.getReg();
3580b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg))
3590b57cec5SDimitry Andric addSavedGPR(MBB, MIB, Reg, true);
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andric // ...likewise GPR varargs.
3630b57cec5SDimitry Andric if (IsVarArg)
364fe6060f1SDimitry Andric for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
365fe6060f1SDimitry Andric addSavedGPR(MBB, MIB, SystemZ::ELFArgGPRs[I], true);
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric // Save FPRs/VRs in the normal TargetInstrInfo way.
3694824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
37004eeddc0SDimitry Andric Register Reg = I.getReg();
3710b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
3720b57cec5SDimitry Andric MBB.addLiveIn(Reg);
3734824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
374bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) {
3770b57cec5SDimitry Andric MBB.addLiveIn(Reg);
3784824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
379bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric
3830b57cec5SDimitry Andric return true;
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const386349cc55cSDimitry Andric bool SystemZELFFrameLowering::restoreCalleeSavedRegisters(
3875ffd83dbSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
3885ffd83dbSDimitry Andric MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
3890b57cec5SDimitry Andric if (CSI.empty())
3900b57cec5SDimitry Andric return false;
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent();
3930b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
3940b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
3950b57cec5SDimitry Andric bool HasFP = hasFP(MF);
3960b57cec5SDimitry Andric DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric // Restore FPRs/VRs in the normal TargetInstrInfo way.
3994824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
40004eeddc0SDimitry Andric Register Reg = I.getReg();
4010b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg))
4024824e7fdSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
403bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
4040b57cec5SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg))
4054824e7fdSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
406bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andric // Restore call-saved GPRs (but not call-clobbered varargs, which at
4100b57cec5SDimitry Andric // this point might hold return values).
411480093f4SDimitry Andric SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
412480093f4SDimitry Andric if (RestoreGPRs.LowGPR) {
4130b57cec5SDimitry Andric // If we saved any of %r2-%r5 as varargs, we should also be saving
4140b57cec5SDimitry Andric // and restoring %r6. If we're saving %r6 or above, we should be
4150b57cec5SDimitry Andric // restoring it too.
416480093f4SDimitry Andric assert(RestoreGPRs.LowGPR != RestoreGPRs.HighGPR &&
417480093f4SDimitry Andric "Should be loading %r15 and something else");
4180b57cec5SDimitry Andric
4190b57cec5SDimitry Andric // Build an LMG instruction.
4200b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric // Add the explicit register operands.
423480093f4SDimitry Andric MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
424480093f4SDimitry Andric MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
4250b57cec5SDimitry Andric
4260b57cec5SDimitry Andric // Add the address.
4270b57cec5SDimitry Andric MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
428480093f4SDimitry Andric MIB.addImm(RestoreGPRs.GPROffset);
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric // Do a second scan adding regs as being defined by instruction
4314824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
43204eeddc0SDimitry Andric Register Reg = I.getReg();
433480093f4SDimitry Andric if (Reg != RestoreGPRs.LowGPR && Reg != RestoreGPRs.HighGPR &&
4340b57cec5SDimitry Andric SystemZ::GR64BitRegClass.contains(Reg))
4350b57cec5SDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine);
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric }
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andric return true;
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const442349cc55cSDimitry Andric void SystemZELFFrameLowering::processFunctionBeforeFrameFinalized(
443349cc55cSDimitry Andric MachineFunction &MF, RegScavenger *RS) const {
4440b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
445e8d8bef9SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
446e8d8bef9SDimitry Andric MachineRegisterInfo *MRI = &MF.getRegInfo();
447c9157d92SDimitry Andric bool BackChain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
448480093f4SDimitry Andric
4495ffd83dbSDimitry Andric if (!usePackedStack(MF) || BackChain)
4505ffd83dbSDimitry Andric // Create the incoming register save area.
451480093f4SDimitry Andric getOrCreateFramePointerSaveIndex(MF);
452480093f4SDimitry Andric
4530b57cec5SDimitry Andric // Get the size of our stack frame to be allocated ...
4540b57cec5SDimitry Andric uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
455fe6060f1SDimitry Andric SystemZMC::ELFCallFrameSize);
4560b57cec5SDimitry Andric // ... and the maximum offset we may need to reach into the
4570b57cec5SDimitry Andric // caller's frame to access the save area or stack arguments.
458480093f4SDimitry Andric int64_t MaxArgOffset = 0;
4590b57cec5SDimitry Andric for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
4600b57cec5SDimitry Andric if (MFFrame.getObjectOffset(I) >= 0) {
461480093f4SDimitry Andric int64_t ArgOffset = MFFrame.getObjectOffset(I) +
4620b57cec5SDimitry Andric MFFrame.getObjectSize(I);
4630b57cec5SDimitry Andric MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric uint64_t MaxReach = StackSize + MaxArgOffset;
4670b57cec5SDimitry Andric if (!isUInt<12>(MaxReach)) {
4680b57cec5SDimitry Andric // We may need register scavenging slots if some parts of the frame
4690b57cec5SDimitry Andric // are outside the reach of an unsigned 12-bit displacement.
4700b57cec5SDimitry Andric // Create 2 for the case where both addresses in an MVC are
4710b57cec5SDimitry Andric // out of range.
4725ffd83dbSDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
4735ffd83dbSDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
4740b57cec5SDimitry Andric }
475e8d8bef9SDimitry Andric
476e8d8bef9SDimitry Andric // If R6 is used as an argument register it is still callee saved. If it in
477e8d8bef9SDimitry Andric // this case is not clobbered (and restored) it should never be marked as
478e8d8bef9SDimitry Andric // killed.
479e8d8bef9SDimitry Andric if (MF.front().isLiveIn(SystemZ::R6D) &&
480e8d8bef9SDimitry Andric ZFI->getRestoreGPRRegs().LowGPR != SystemZ::R6D)
481e8d8bef9SDimitry Andric for (auto &MO : MRI->use_nodbg_operands(SystemZ::R6D))
482e8d8bef9SDimitry Andric MO.setIsKill(false);
4830b57cec5SDimitry Andric }
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
emitIncrement(MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const DebugLoc & DL,Register Reg,int64_t NumBytes,const TargetInstrInfo * TII)4860b57cec5SDimitry Andric static void emitIncrement(MachineBasicBlock &MBB,
4875ffd83dbSDimitry Andric MachineBasicBlock::iterator &MBBI, const DebugLoc &DL,
4885ffd83dbSDimitry Andric Register Reg, int64_t NumBytes,
4890b57cec5SDimitry Andric const TargetInstrInfo *TII) {
4900b57cec5SDimitry Andric while (NumBytes) {
4910b57cec5SDimitry Andric unsigned Opcode;
4920b57cec5SDimitry Andric int64_t ThisVal = NumBytes;
4930b57cec5SDimitry Andric if (isInt<16>(NumBytes))
4940b57cec5SDimitry Andric Opcode = SystemZ::AGHI;
4950b57cec5SDimitry Andric else {
4960b57cec5SDimitry Andric Opcode = SystemZ::AGFI;
4970b57cec5SDimitry Andric // Make sure we maintain 8-byte stack alignment.
4980b57cec5SDimitry Andric int64_t MinVal = -uint64_t(1) << 31;
4990b57cec5SDimitry Andric int64_t MaxVal = (int64_t(1) << 31) - 8;
5000b57cec5SDimitry Andric if (ThisVal < MinVal)
5010b57cec5SDimitry Andric ThisVal = MinVal;
5020b57cec5SDimitry Andric else if (ThisVal > MaxVal)
5030b57cec5SDimitry Andric ThisVal = MaxVal;
5040b57cec5SDimitry Andric }
5050b57cec5SDimitry Andric MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
5060b57cec5SDimitry Andric .addReg(Reg).addImm(ThisVal);
5070b57cec5SDimitry Andric // The CC implicit def is dead.
5080b57cec5SDimitry Andric MI->getOperand(3).setIsDead();
5090b57cec5SDimitry Andric NumBytes -= ThisVal;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric
5135ffd83dbSDimitry Andric // Add CFI for the new CFA offset.
buildCFAOffs(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,int Offset,const SystemZInstrInfo * ZII)5145ffd83dbSDimitry Andric static void buildCFAOffs(MachineBasicBlock &MBB,
5155ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI,
5165ffd83dbSDimitry Andric const DebugLoc &DL, int Offset,
5175ffd83dbSDimitry Andric const SystemZInstrInfo *ZII) {
5185ffd83dbSDimitry Andric unsigned CFIIndex = MBB.getParent()->addFrameInst(
5195ffd83dbSDimitry Andric MCCFIInstruction::cfiDefCfaOffset(nullptr, -Offset));
5205ffd83dbSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5215ffd83dbSDimitry Andric .addCFIIndex(CFIIndex);
5225ffd83dbSDimitry Andric }
5235ffd83dbSDimitry Andric
5245ffd83dbSDimitry Andric // Add CFI for the new frame location.
buildDefCFAReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,unsigned Reg,const SystemZInstrInfo * ZII)5255ffd83dbSDimitry Andric static void buildDefCFAReg(MachineBasicBlock &MBB,
5265ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI,
5275ffd83dbSDimitry Andric const DebugLoc &DL, unsigned Reg,
5285ffd83dbSDimitry Andric const SystemZInstrInfo *ZII) {
5295ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent();
5305ffd83dbSDimitry Andric MachineModuleInfo &MMI = MF.getMMI();
5315ffd83dbSDimitry Andric const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
5325ffd83dbSDimitry Andric unsigned RegNum = MRI->getDwarfRegNum(Reg, true);
5335ffd83dbSDimitry Andric unsigned CFIIndex = MF.addFrameInst(
5345ffd83dbSDimitry Andric MCCFIInstruction::createDefCfaRegister(nullptr, RegNum));
5355ffd83dbSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5365ffd83dbSDimitry Andric .addCFIIndex(CFIIndex);
5375ffd83dbSDimitry Andric }
5385ffd83dbSDimitry Andric
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const539349cc55cSDimitry Andric void SystemZELFFrameLowering::emitPrologue(MachineFunction &MF,
5400b57cec5SDimitry Andric MachineBasicBlock &MBB) const {
5410b57cec5SDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
5425ffd83dbSDimitry Andric const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
5435ffd83dbSDimitry Andric const SystemZTargetLowering &TLI = *STI.getTargetLowering();
5440b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
5455ffd83dbSDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(STI.getInstrInfo());
5460b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
5470b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin();
5480b57cec5SDimitry Andric MachineModuleInfo &MMI = MF.getMMI();
5490b57cec5SDimitry Andric const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
5500b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
5510b57cec5SDimitry Andric bool HasFP = hasFP(MF);
5520b57cec5SDimitry Andric
553480093f4SDimitry Andric // In GHC calling convention C stack space, including the ABI-defined
554480093f4SDimitry Andric // 160-byte base area, is (de)allocated by GHC itself. This stack space may
555480093f4SDimitry Andric // be used by LLVM as spill slots for the tail recursive GHC functions. Thus
556480093f4SDimitry Andric // do not allocate stack space here, too.
557480093f4SDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GHC) {
558480093f4SDimitry Andric if (MFFrame.getStackSize() > 2048 * sizeof(long)) {
559480093f4SDimitry Andric report_fatal_error(
560480093f4SDimitry Andric "Pre allocated stack space for GHC function is too small");
561480093f4SDimitry Andric }
562480093f4SDimitry Andric if (HasFP) {
563480093f4SDimitry Andric report_fatal_error(
564480093f4SDimitry Andric "In GHC calling convention a frame pointer is not supported");
565480093f4SDimitry Andric }
566fe6060f1SDimitry Andric MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::ELFCallFrameSize);
567480093f4SDimitry Andric return;
568480093f4SDimitry Andric }
569480093f4SDimitry Andric
5700b57cec5SDimitry Andric // Debug location must be unknown since the first debug location is used
5710b57cec5SDimitry Andric // to determine the end of the prologue.
5720b57cec5SDimitry Andric DebugLoc DL;
5730b57cec5SDimitry Andric
5740b57cec5SDimitry Andric // The current offset of the stack pointer from the CFA.
575fe6060f1SDimitry Andric int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
5760b57cec5SDimitry Andric
577480093f4SDimitry Andric if (ZFI->getSpillGPRRegs().LowGPR) {
5780b57cec5SDimitry Andric // Skip over the GPR saves.
5790b57cec5SDimitry Andric if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
5800b57cec5SDimitry Andric ++MBBI;
5810b57cec5SDimitry Andric else
5820b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over GPR saves");
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric // Add CFI for the GPR saves.
5850b57cec5SDimitry Andric for (auto &Save : CSI) {
58604eeddc0SDimitry Andric Register Reg = Save.getReg();
5870b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg)) {
588480093f4SDimitry Andric int FI = Save.getFrameIdx();
589480093f4SDimitry Andric int64_t Offset = MFFrame.getObjectOffset(FI);
5900b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
5910b57cec5SDimitry Andric nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
5920b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5930b57cec5SDimitry Andric .addCFIIndex(CFIIndex);
5940b57cec5SDimitry Andric }
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric
5980b57cec5SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
5990b57cec5SDimitry Andric // We need to allocate the ABI-defined 160-byte base area whenever
6000b57cec5SDimitry Andric // we allocate stack space for our own use and whenever we call another
6010b57cec5SDimitry Andric // function.
602480093f4SDimitry Andric bool HasStackObject = false;
603480093f4SDimitry Andric for (unsigned i = 0, e = MFFrame.getObjectIndexEnd(); i != e; ++i)
604480093f4SDimitry Andric if (!MFFrame.isDeadObjectIndex(i)) {
605480093f4SDimitry Andric HasStackObject = true;
606480093f4SDimitry Andric break;
6070b57cec5SDimitry Andric }
608480093f4SDimitry Andric if (HasStackObject || MFFrame.hasCalls())
609fe6060f1SDimitry Andric StackSize += SystemZMC::ELFCallFrameSize;
610480093f4SDimitry Andric // Don't allocate the incoming reg save area.
611fe6060f1SDimitry Andric StackSize = StackSize > SystemZMC::ELFCallFrameSize
612fe6060f1SDimitry Andric ? StackSize - SystemZMC::ELFCallFrameSize
613480093f4SDimitry Andric : 0;
614480093f4SDimitry Andric MFFrame.setStackSize(StackSize);
6150b57cec5SDimitry Andric
6160b57cec5SDimitry Andric if (StackSize) {
6170b57cec5SDimitry Andric // Allocate StackSize bytes.
6180b57cec5SDimitry Andric int64_t Delta = -int64_t(StackSize);
6195ffd83dbSDimitry Andric const unsigned ProbeSize = TLI.getStackProbeSize(MF);
6205ffd83dbSDimitry Andric bool FreeProbe = (ZFI->getSpillGPRRegs().GPROffset &&
6215ffd83dbSDimitry Andric (ZFI->getSpillGPRRegs().GPROffset + StackSize) < ProbeSize);
6225ffd83dbSDimitry Andric if (!FreeProbe &&
6235ffd83dbSDimitry Andric MF.getSubtarget().getTargetLowering()->hasInlineStackProbe(MF)) {
6245ffd83dbSDimitry Andric // Stack probing may involve looping, but splitting the prologue block
6255ffd83dbSDimitry Andric // is not possible at this point since it would invalidate the
6265ffd83dbSDimitry Andric // SaveBlocks / RestoreBlocks sets of PEI in the single block function
6275ffd83dbSDimitry Andric // case. Build a pseudo to be handled later by inlineStackProbe().
6285ffd83dbSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::PROBED_STACKALLOC))
6295ffd83dbSDimitry Andric .addImm(StackSize);
6305ffd83dbSDimitry Andric }
6315ffd83dbSDimitry Andric else {
632c9157d92SDimitry Andric bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
633e8d8bef9SDimitry Andric // If we need backchain, save current stack pointer. R1 is free at
634e8d8bef9SDimitry Andric // this point.
635e8d8bef9SDimitry Andric if (StoreBackchain)
636e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
637e8d8bef9SDimitry Andric .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
6380b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
6395ffd83dbSDimitry Andric buildCFAOffs(MBB, MBBI, DL, SPOffsetFromCFA + Delta, ZII);
640e8d8bef9SDimitry Andric if (StoreBackchain)
6410b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
6425ffd83dbSDimitry Andric .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
643e8d8bef9SDimitry Andric .addImm(getBackchainOffset(MF)).addReg(0);
6445ffd83dbSDimitry Andric }
645e8d8bef9SDimitry Andric SPOffsetFromCFA += Delta;
6460b57cec5SDimitry Andric }
6470b57cec5SDimitry Andric
6480b57cec5SDimitry Andric if (HasFP) {
6490b57cec5SDimitry Andric // Copy the base of the frame to R11.
6500b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
6510b57cec5SDimitry Andric .addReg(SystemZ::R15D);
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andric // Add CFI for the new frame location.
6545ffd83dbSDimitry Andric buildDefCFAReg(MBB, MBBI, DL, SystemZ::R11D, ZII);
6550b57cec5SDimitry Andric
6560b57cec5SDimitry Andric // Mark the FramePtr as live at the beginning of every block except
6570b57cec5SDimitry Andric // the entry block. (We'll have marked R11 as live on entry when
6580b57cec5SDimitry Andric // saving the GPRs.)
659349cc55cSDimitry Andric for (MachineBasicBlock &MBBJ : llvm::drop_begin(MF))
660349cc55cSDimitry Andric MBBJ.addLiveIn(SystemZ::R11D);
6610b57cec5SDimitry Andric }
6620b57cec5SDimitry Andric
6630b57cec5SDimitry Andric // Skip over the FPR/VR saves.
6640b57cec5SDimitry Andric SmallVector<unsigned, 8> CFIIndexes;
6650b57cec5SDimitry Andric for (auto &Save : CSI) {
66604eeddc0SDimitry Andric Register Reg = Save.getReg();
6670b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
6680b57cec5SDimitry Andric if (MBBI != MBB.end() &&
6690b57cec5SDimitry Andric (MBBI->getOpcode() == SystemZ::STD ||
6700b57cec5SDimitry Andric MBBI->getOpcode() == SystemZ::STDY))
6710b57cec5SDimitry Andric ++MBBI;
6720b57cec5SDimitry Andric else
6730b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over FPR save");
6740b57cec5SDimitry Andric } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
6750b57cec5SDimitry Andric if (MBBI != MBB.end() &&
6760b57cec5SDimitry Andric MBBI->getOpcode() == SystemZ::VST)
6770b57cec5SDimitry Andric ++MBBI;
6780b57cec5SDimitry Andric else
6790b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over VR save");
6800b57cec5SDimitry Andric } else
6810b57cec5SDimitry Andric continue;
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andric // Add CFI for the this save.
6840b57cec5SDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
6855ffd83dbSDimitry Andric Register IgnoredFrameReg;
6860b57cec5SDimitry Andric int64_t Offset =
687e8d8bef9SDimitry Andric getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg)
688e8d8bef9SDimitry Andric .getFixed();
6890b57cec5SDimitry Andric
6900b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
6910b57cec5SDimitry Andric nullptr, DwarfReg, SPOffsetFromCFA + Offset));
6920b57cec5SDimitry Andric CFIIndexes.push_back(CFIIndex);
6930b57cec5SDimitry Andric }
6940b57cec5SDimitry Andric // Complete the CFI for the FPR/VR saves, modelling them as taking effect
6950b57cec5SDimitry Andric // after the last save.
6960b57cec5SDimitry Andric for (auto CFIIndex : CFIIndexes) {
6970b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
6980b57cec5SDimitry Andric .addCFIIndex(CFIIndex);
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const702349cc55cSDimitry Andric void SystemZELFFrameLowering::emitEpilogue(MachineFunction &MF,
7030b57cec5SDimitry Andric MachineBasicBlock &MBB) const {
7040b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
7050b57cec5SDimitry Andric auto *ZII =
7060b57cec5SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
7070b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
7080b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
7090b57cec5SDimitry Andric
710349cc55cSDimitry Andric // See SystemZELFFrameLowering::emitPrologue
711480093f4SDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GHC)
712480093f4SDimitry Andric return;
713480093f4SDimitry Andric
7140b57cec5SDimitry Andric // Skip the return instruction.
7150b57cec5SDimitry Andric assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
7160b57cec5SDimitry Andric
7170b57cec5SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
718480093f4SDimitry Andric if (ZFI->getRestoreGPRRegs().LowGPR) {
7190b57cec5SDimitry Andric --MBBI;
7200b57cec5SDimitry Andric unsigned Opcode = MBBI->getOpcode();
7210b57cec5SDimitry Andric if (Opcode != SystemZ::LMG)
7220b57cec5SDimitry Andric llvm_unreachable("Expected to see callee-save register restore code");
7230b57cec5SDimitry Andric
7240b57cec5SDimitry Andric unsigned AddrOpNo = 2;
7250b57cec5SDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
7260b57cec5SDimitry Andric uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
7270b57cec5SDimitry Andric unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
7280b57cec5SDimitry Andric
7290b57cec5SDimitry Andric // If the offset is too large, use the largest stack-aligned offset
7300b57cec5SDimitry Andric // and add the rest to the base register (the stack or frame pointer).
7310b57cec5SDimitry Andric if (!NewOpcode) {
7320b57cec5SDimitry Andric uint64_t NumBytes = Offset - 0x7fff8;
7330b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
7340b57cec5SDimitry Andric NumBytes, ZII);
7350b57cec5SDimitry Andric Offset -= NumBytes;
7360b57cec5SDimitry Andric NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
7370b57cec5SDimitry Andric assert(NewOpcode && "No restore instruction available");
7380b57cec5SDimitry Andric }
7390b57cec5SDimitry Andric
7400b57cec5SDimitry Andric MBBI->setDesc(ZII->get(NewOpcode));
7410b57cec5SDimitry Andric MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
7420b57cec5SDimitry Andric } else if (StackSize) {
7430b57cec5SDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
7440b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
7450b57cec5SDimitry Andric }
7460b57cec5SDimitry Andric }
7470b57cec5SDimitry Andric
inlineStackProbe(MachineFunction & MF,MachineBasicBlock & PrologMBB) const748349cc55cSDimitry Andric void SystemZELFFrameLowering::inlineStackProbe(
749349cc55cSDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
7505ffd83dbSDimitry Andric auto *ZII =
7515ffd83dbSDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
7525ffd83dbSDimitry Andric const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
7535ffd83dbSDimitry Andric const SystemZTargetLowering &TLI = *STI.getTargetLowering();
7545ffd83dbSDimitry Andric
7555ffd83dbSDimitry Andric MachineInstr *StackAllocMI = nullptr;
7565ffd83dbSDimitry Andric for (MachineInstr &MI : PrologMBB)
7575ffd83dbSDimitry Andric if (MI.getOpcode() == SystemZ::PROBED_STACKALLOC) {
7585ffd83dbSDimitry Andric StackAllocMI = &MI;
7595ffd83dbSDimitry Andric break;
7605ffd83dbSDimitry Andric }
7615ffd83dbSDimitry Andric if (StackAllocMI == nullptr)
7625ffd83dbSDimitry Andric return;
7635ffd83dbSDimitry Andric uint64_t StackSize = StackAllocMI->getOperand(0).getImm();
7645ffd83dbSDimitry Andric const unsigned ProbeSize = TLI.getStackProbeSize(MF);
7655ffd83dbSDimitry Andric uint64_t NumFullBlocks = StackSize / ProbeSize;
7665ffd83dbSDimitry Andric uint64_t Residual = StackSize % ProbeSize;
767fe6060f1SDimitry Andric int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
7685ffd83dbSDimitry Andric MachineBasicBlock *MBB = &PrologMBB;
7695ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI = StackAllocMI;
7705ffd83dbSDimitry Andric const DebugLoc DL = StackAllocMI->getDebugLoc();
7715ffd83dbSDimitry Andric
7725ffd83dbSDimitry Andric // Allocate a block of Size bytes on the stack and probe it.
7735ffd83dbSDimitry Andric auto allocateAndProbe = [&](MachineBasicBlock &InsMBB,
7745ffd83dbSDimitry Andric MachineBasicBlock::iterator InsPt, unsigned Size,
7755ffd83dbSDimitry Andric bool EmitCFI) -> void {
7765ffd83dbSDimitry Andric emitIncrement(InsMBB, InsPt, DL, SystemZ::R15D, -int64_t(Size), ZII);
7775ffd83dbSDimitry Andric if (EmitCFI) {
7785ffd83dbSDimitry Andric SPOffsetFromCFA -= Size;
7795ffd83dbSDimitry Andric buildCFAOffs(InsMBB, InsPt, DL, SPOffsetFromCFA, ZII);
7805ffd83dbSDimitry Andric }
7815ffd83dbSDimitry Andric // Probe by means of a volatile compare.
7825ffd83dbSDimitry Andric MachineMemOperand *MMO = MF.getMachineMemOperand(MachinePointerInfo(),
7835ffd83dbSDimitry Andric MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad, 8, Align(1));
7845ffd83dbSDimitry Andric BuildMI(InsMBB, InsPt, DL, ZII->get(SystemZ::CG))
7855ffd83dbSDimitry Andric .addReg(SystemZ::R0D, RegState::Undef)
7865ffd83dbSDimitry Andric .addReg(SystemZ::R15D).addImm(Size - 8).addReg(0)
7875ffd83dbSDimitry Andric .addMemOperand(MMO);
7885ffd83dbSDimitry Andric };
7895ffd83dbSDimitry Andric
790c9157d92SDimitry Andric bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
791e8d8bef9SDimitry Andric if (StoreBackchain)
792e8d8bef9SDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR))
793e8d8bef9SDimitry Andric .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
794e8d8bef9SDimitry Andric
795e8d8bef9SDimitry Andric MachineBasicBlock *DoneMBB = nullptr;
796e8d8bef9SDimitry Andric MachineBasicBlock *LoopMBB = nullptr;
7975ffd83dbSDimitry Andric if (NumFullBlocks < 3) {
7985ffd83dbSDimitry Andric // Emit unrolled probe statements.
7995ffd83dbSDimitry Andric for (unsigned int i = 0; i < NumFullBlocks; i++)
8005ffd83dbSDimitry Andric allocateAndProbe(*MBB, MBBI, ProbeSize, true/*EmitCFI*/);
8015ffd83dbSDimitry Andric } else {
8025ffd83dbSDimitry Andric // Emit a loop probing the pages.
8035ffd83dbSDimitry Andric uint64_t LoopAlloc = ProbeSize * NumFullBlocks;
8045ffd83dbSDimitry Andric SPOffsetFromCFA -= LoopAlloc;
8055ffd83dbSDimitry Andric
806e8d8bef9SDimitry Andric // Use R0D to hold the exit value.
807e8d8bef9SDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R0D)
8085ffd83dbSDimitry Andric .addReg(SystemZ::R15D);
809e8d8bef9SDimitry Andric buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R0D, ZII);
810e8d8bef9SDimitry Andric emitIncrement(*MBB, MBBI, DL, SystemZ::R0D, -int64_t(LoopAlloc), ZII);
811fe6060f1SDimitry Andric buildCFAOffs(*MBB, MBBI, DL, -int64_t(SystemZMC::ELFCallFrameSize + LoopAlloc),
8125ffd83dbSDimitry Andric ZII);
8135ffd83dbSDimitry Andric
814e8d8bef9SDimitry Andric DoneMBB = SystemZ::splitBlockBefore(MBBI, MBB);
815e8d8bef9SDimitry Andric LoopMBB = SystemZ::emitBlockAfter(MBB);
8165ffd83dbSDimitry Andric MBB->addSuccessor(LoopMBB);
8175ffd83dbSDimitry Andric LoopMBB->addSuccessor(LoopMBB);
8185ffd83dbSDimitry Andric LoopMBB->addSuccessor(DoneMBB);
8195ffd83dbSDimitry Andric
8205ffd83dbSDimitry Andric MBB = LoopMBB;
8215ffd83dbSDimitry Andric allocateAndProbe(*MBB, MBB->end(), ProbeSize, false/*EmitCFI*/);
8225ffd83dbSDimitry Andric BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::CLGR))
823e8d8bef9SDimitry Andric .addReg(SystemZ::R15D).addReg(SystemZ::R0D);
8245ffd83dbSDimitry Andric BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::BRC))
8255ffd83dbSDimitry Andric .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_GT).addMBB(MBB);
8265ffd83dbSDimitry Andric
8275ffd83dbSDimitry Andric MBB = DoneMBB;
8285ffd83dbSDimitry Andric MBBI = DoneMBB->begin();
8295ffd83dbSDimitry Andric buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R15D, ZII);
8305ffd83dbSDimitry Andric }
8315ffd83dbSDimitry Andric
8325ffd83dbSDimitry Andric if (Residual)
8335ffd83dbSDimitry Andric allocateAndProbe(*MBB, MBBI, Residual, true/*EmitCFI*/);
8345ffd83dbSDimitry Andric
835e8d8bef9SDimitry Andric if (StoreBackchain)
836e8d8bef9SDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::STG))
837e8d8bef9SDimitry Andric .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
838e8d8bef9SDimitry Andric .addImm(getBackchainOffset(MF)).addReg(0);
839e8d8bef9SDimitry Andric
8405ffd83dbSDimitry Andric StackAllocMI->eraseFromParent();
841e8d8bef9SDimitry Andric if (DoneMBB != nullptr) {
842e8d8bef9SDimitry Andric // Compute the live-in lists for the new blocks.
843*b9d9368bSDimitry Andric bool anyChange = false;
844*b9d9368bSDimitry Andric do {
845*b9d9368bSDimitry Andric anyChange = recomputeLiveIns(*DoneMBB) || recomputeLiveIns(*LoopMBB);
846*b9d9368bSDimitry Andric } while (anyChange);
847e8d8bef9SDimitry Andric }
8485ffd83dbSDimitry Andric }
8495ffd83dbSDimitry Andric
hasFP(const MachineFunction & MF) const850349cc55cSDimitry Andric bool SystemZELFFrameLowering::hasFP(const MachineFunction &MF) const {
8510b57cec5SDimitry Andric return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
85204eeddc0SDimitry Andric MF.getFrameInfo().hasVarSizedObjects());
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const855349cc55cSDimitry Andric StackOffset SystemZELFFrameLowering::getFrameIndexReference(
856349cc55cSDimitry Andric const MachineFunction &MF, int FI, Register &FrameReg) const {
857fe6060f1SDimitry Andric // Our incoming SP is actually SystemZMC::ELFCallFrameSize below the CFA, so
858480093f4SDimitry Andric // add that difference here.
859e8d8bef9SDimitry Andric StackOffset Offset =
860480093f4SDimitry Andric TargetFrameLowering::getFrameIndexReference(MF, FI, FrameReg);
861fe6060f1SDimitry Andric return Offset + StackOffset::getFixed(SystemZMC::ELFCallFrameSize);
862480093f4SDimitry Andric }
863480093f4SDimitry Andric
getRegSpillOffset(MachineFunction & MF,Register Reg) const864349cc55cSDimitry Andric unsigned SystemZELFFrameLowering::getRegSpillOffset(MachineFunction &MF,
8655ffd83dbSDimitry Andric Register Reg) const {
8665ffd83dbSDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
867c9157d92SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
868c9157d92SDimitry Andric bool BackChain = Subtarget.hasBackChain();
869c9157d92SDimitry Andric bool SoftFloat = Subtarget.hasSoftFloat();
8705ffd83dbSDimitry Andric unsigned Offset = RegSpillOffsets[Reg];
8715ffd83dbSDimitry Andric if (usePackedStack(MF) && !(IsVarArg && !SoftFloat)) {
8725ffd83dbSDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg))
8735ffd83dbSDimitry Andric // Put all GPRs at the top of the Register save area with packed
8745ffd83dbSDimitry Andric // stack. Make room for the backchain if needed.
8755ffd83dbSDimitry Andric Offset += BackChain ? 24 : 32;
8765ffd83dbSDimitry Andric else
8775ffd83dbSDimitry Andric Offset = 0;
8785ffd83dbSDimitry Andric }
8795ffd83dbSDimitry Andric return Offset;
8805ffd83dbSDimitry Andric }
8815ffd83dbSDimitry Andric
getOrCreateFramePointerSaveIndex(MachineFunction & MF) const882349cc55cSDimitry Andric int SystemZELFFrameLowering::getOrCreateFramePointerSaveIndex(
883349cc55cSDimitry Andric MachineFunction &MF) const {
884480093f4SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
885480093f4SDimitry Andric int FI = ZFI->getFramePointerSaveIndex();
886480093f4SDimitry Andric if (!FI) {
887480093f4SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
888fe6060f1SDimitry Andric int Offset = getBackchainOffset(MF) - SystemZMC::ELFCallFrameSize;
8895ffd83dbSDimitry Andric FI = MFFrame.CreateFixedObject(8, Offset, false);
890480093f4SDimitry Andric ZFI->setFramePointerSaveIndex(FI);
891480093f4SDimitry Andric }
892480093f4SDimitry Andric return FI;
893480093f4SDimitry Andric }
8945ffd83dbSDimitry Andric
usePackedStack(MachineFunction & MF) const895349cc55cSDimitry Andric bool SystemZELFFrameLowering::usePackedStack(MachineFunction &MF) const {
8965ffd83dbSDimitry Andric bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack");
897c9157d92SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
898c9157d92SDimitry Andric bool BackChain = Subtarget.hasBackChain();
899c9157d92SDimitry Andric bool SoftFloat = Subtarget.hasSoftFloat();
9005ffd83dbSDimitry Andric if (HasPackedStackAttr && BackChain && !SoftFloat)
9015ffd83dbSDimitry Andric report_fatal_error("packed-stack + backchain + hard-float is unsupported.");
9025ffd83dbSDimitry Andric bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC;
9035ffd83dbSDimitry Andric return HasPackedStackAttr && CallConv;
9045ffd83dbSDimitry Andric }
905349cc55cSDimitry Andric
SystemZXPLINKFrameLowering()906349cc55cSDimitry Andric SystemZXPLINKFrameLowering::SystemZXPLINKFrameLowering()
9070eae32dcSDimitry Andric : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(32), 0,
908349cc55cSDimitry Andric Align(32), /* StackRealignable */ false),
909349cc55cSDimitry Andric RegSpillOffsets(-1) {
910349cc55cSDimitry Andric
911349cc55cSDimitry Andric // Create a mapping from register number to save slot offset.
912349cc55cSDimitry Andric // These offsets are relative to the start of the local are area.
913349cc55cSDimitry Andric RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
914bdd1243dSDimitry Andric for (const auto &Entry : XPLINKSpillOffsetTable)
915bdd1243dSDimitry Andric RegSpillOffsets[Entry.Reg] = Entry.Offset;
916349cc55cSDimitry Andric }
917349cc55cSDimitry Andric
918fcaf7f86SDimitry Andric // Checks if the function is a potential candidate for being a XPLeaf routine.
isXPLeafCandidate(const MachineFunction & MF)919fcaf7f86SDimitry Andric static bool isXPLeafCandidate(const MachineFunction &MF) {
920fcaf7f86SDimitry Andric const MachineFrameInfo &MFFrame = MF.getFrameInfo();
921fcaf7f86SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
922fcaf7f86SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
923fcaf7f86SDimitry Andric auto *Regs =
924fcaf7f86SDimitry Andric static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
925fcaf7f86SDimitry Andric
926fcaf7f86SDimitry Andric // If function calls other functions including alloca, then it is not a XPLeaf
927fcaf7f86SDimitry Andric // routine.
928fcaf7f86SDimitry Andric if (MFFrame.hasCalls())
929fcaf7f86SDimitry Andric return false;
930fcaf7f86SDimitry Andric
931fcaf7f86SDimitry Andric // If the function has var Sized Objects, then it is not a XPLeaf routine.
932fcaf7f86SDimitry Andric if (MFFrame.hasVarSizedObjects())
933fcaf7f86SDimitry Andric return false;
934fcaf7f86SDimitry Andric
935fcaf7f86SDimitry Andric // If the function adjusts the stack, then it is not a XPLeaf routine.
936fcaf7f86SDimitry Andric if (MFFrame.adjustsStack())
937fcaf7f86SDimitry Andric return false;
938fcaf7f86SDimitry Andric
939fcaf7f86SDimitry Andric // If function modifies the stack pointer register, then it is not a XPLeaf
940fcaf7f86SDimitry Andric // routine.
941fcaf7f86SDimitry Andric if (MRI.isPhysRegModified(Regs->getStackPointerRegister()))
942fcaf7f86SDimitry Andric return false;
943fcaf7f86SDimitry Andric
944fcaf7f86SDimitry Andric // If function modifies the ADA register, then it is not a XPLeaf routine.
945fcaf7f86SDimitry Andric if (MRI.isPhysRegModified(Regs->getAddressOfCalleeRegister()))
946fcaf7f86SDimitry Andric return false;
947fcaf7f86SDimitry Andric
948fcaf7f86SDimitry Andric // If function modifies the return address register, then it is not a XPLeaf
949fcaf7f86SDimitry Andric // routine.
950fcaf7f86SDimitry Andric if (MRI.isPhysRegModified(Regs->getReturnFunctionAddressRegister()))
951fcaf7f86SDimitry Andric return false;
952fcaf7f86SDimitry Andric
953fcaf7f86SDimitry Andric // If the backchain pointer should be stored, then it is not a XPLeaf routine.
954c9157d92SDimitry Andric if (MF.getSubtarget<SystemZSubtarget>().hasBackChain())
955fcaf7f86SDimitry Andric return false;
956fcaf7f86SDimitry Andric
957fcaf7f86SDimitry Andric // If function acquires its own stack frame, then it is not a XPLeaf routine.
958fcaf7f86SDimitry Andric // At the time this function is called, only slots for local variables are
959fcaf7f86SDimitry Andric // allocated, so this is a very rough estimate.
960fcaf7f86SDimitry Andric if (MFFrame.estimateStackSize(MF) > 0)
961fcaf7f86SDimitry Andric return false;
962fcaf7f86SDimitry Andric
963fcaf7f86SDimitry Andric return true;
964fcaf7f86SDimitry Andric }
965fcaf7f86SDimitry Andric
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const966349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::assignCalleeSavedSpillSlots(
967349cc55cSDimitry Andric MachineFunction &MF, const TargetRegisterInfo *TRI,
968349cc55cSDimitry Andric std::vector<CalleeSavedInfo> &CSI) const {
969349cc55cSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
970349cc55cSDimitry Andric SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
971349cc55cSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
972349cc55cSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
973753f127fSDimitry Andric auto &GRRegClass = SystemZ::GR64BitRegClass;
974753f127fSDimitry Andric
975fcaf7f86SDimitry Andric // At this point, the result of isXPLeafCandidate() is not accurate because
976fcaf7f86SDimitry Andric // the size of the save area has not yet been determined. If
977fcaf7f86SDimitry Andric // isXPLeafCandidate() indicates a potential leaf function, and there are no
978fcaf7f86SDimitry Andric // callee-save registers, then it is indeed a leaf function, and we can early
979fcaf7f86SDimitry Andric // exit.
980fcaf7f86SDimitry Andric // TODO: It is possible for leaf functions to use callee-saved registers.
981fcaf7f86SDimitry Andric // It can use the 0-2k range between R4 and the caller's stack frame without
982fcaf7f86SDimitry Andric // acquiring its own stack frame.
983fcaf7f86SDimitry Andric bool IsLeaf = CSI.empty() && isXPLeafCandidate(MF);
984fcaf7f86SDimitry Andric if (IsLeaf)
985fcaf7f86SDimitry Andric return true;
986fcaf7f86SDimitry Andric
987753f127fSDimitry Andric // For non-leaf functions:
988753f127fSDimitry Andric // - the address of callee (entry point) register R6 must be saved
989753f127fSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getAddressOfCalleeRegister()));
990753f127fSDimitry Andric CSI.back().setRestored(false);
991753f127fSDimitry Andric
992753f127fSDimitry Andric // The return address register R7 must be saved and restored.
993753f127fSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getReturnFunctionAddressRegister()));
994753f127fSDimitry Andric
995753f127fSDimitry Andric // If the function needs a frame pointer, or if the backchain pointer should
996753f127fSDimitry Andric // be stored, then save the stack pointer register R4.
997c9157d92SDimitry Andric if (hasFP(MF) || Subtarget.hasBackChain())
998753f127fSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getStackPointerRegister()));
999349cc55cSDimitry Andric
1000e710425bSDimitry Andric // If this function has an associated personality function then the
1001e710425bSDimitry Andric // environment register R5 must be saved in the DSA.
1002e710425bSDimitry Andric if (!MF.getLandingPads().empty())
1003e710425bSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getADARegister()));
1004e710425bSDimitry Andric
1005349cc55cSDimitry Andric // Scan the call-saved GPRs and find the bounds of the register spill area.
1006753f127fSDimitry Andric Register LowRestoreGPR = 0;
1007753f127fSDimitry Andric int LowRestoreOffset = INT32_MAX;
1008753f127fSDimitry Andric Register LowSpillGPR = 0;
1009753f127fSDimitry Andric int LowSpillOffset = INT32_MAX;
1010753f127fSDimitry Andric Register HighGPR = 0;
1011349cc55cSDimitry Andric int HighOffset = -1;
1012349cc55cSDimitry Andric
1013753f127fSDimitry Andric for (auto &CS : CSI) {
101404eeddc0SDimitry Andric Register Reg = CS.getReg();
1015349cc55cSDimitry Andric int Offset = RegSpillOffsets[Reg];
1016349cc55cSDimitry Andric if (Offset >= 0) {
1017349cc55cSDimitry Andric if (GRRegClass.contains(Reg)) {
1018753f127fSDimitry Andric if (LowSpillOffset > Offset) {
1019753f127fSDimitry Andric LowSpillOffset = Offset;
1020753f127fSDimitry Andric LowSpillGPR = Reg;
1021753f127fSDimitry Andric }
1022753f127fSDimitry Andric if (CS.isRestored() && LowRestoreOffset > Offset) {
1023753f127fSDimitry Andric LowRestoreOffset = Offset;
1024753f127fSDimitry Andric LowRestoreGPR = Reg;
1025349cc55cSDimitry Andric }
1026349cc55cSDimitry Andric
1027349cc55cSDimitry Andric if (Offset > HighOffset) {
1028349cc55cSDimitry Andric HighOffset = Offset;
1029349cc55cSDimitry Andric HighGPR = Reg;
1030349cc55cSDimitry Andric }
1031753f127fSDimitry Andric // Non-volatile GPRs are saved in the dedicated register save area at
1032753f127fSDimitry Andric // the bottom of the stack and are not truly part of the "normal" stack
1033753f127fSDimitry Andric // frame. Mark the frame index as NoAlloc to indicate it as such.
1034753f127fSDimitry Andric unsigned RegSize = 8;
1035349cc55cSDimitry Andric int FrameIdx = MFFrame.CreateFixedSpillStackObject(RegSize, Offset);
1036349cc55cSDimitry Andric CS.setFrameIdx(FrameIdx);
1037753f127fSDimitry Andric MFFrame.setStackID(FrameIdx, TargetStackID::NoAlloc);
1038349cc55cSDimitry Andric }
1039753f127fSDimitry Andric } else {
104004eeddc0SDimitry Andric Register Reg = CS.getReg();
1041349cc55cSDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1042349cc55cSDimitry Andric Align Alignment = TRI->getSpillAlign(*RC);
1043349cc55cSDimitry Andric unsigned Size = TRI->getSpillSize(*RC);
1044349cc55cSDimitry Andric Alignment = std::min(Alignment, getStackAlign());
1045349cc55cSDimitry Andric int FrameIdx = MFFrame.CreateStackObject(Size, Alignment, true);
1046349cc55cSDimitry Andric CS.setFrameIdx(FrameIdx);
1047349cc55cSDimitry Andric }
1048753f127fSDimitry Andric }
1049753f127fSDimitry Andric
1050753f127fSDimitry Andric // Save the range of call-saved registers, for use by the
1051753f127fSDimitry Andric // prologue/epilogue inserters.
1052753f127fSDimitry Andric if (LowRestoreGPR)
1053753f127fSDimitry Andric MFI->setRestoreGPRRegs(LowRestoreGPR, HighGPR, LowRestoreOffset);
1054753f127fSDimitry Andric
1055753f127fSDimitry Andric // Save the range of call-saved registers, for use by the epilogue inserter.
1056753f127fSDimitry Andric assert(LowSpillGPR && "Expected registers to spill");
1057753f127fSDimitry Andric MFI->setSpillGPRRegs(LowSpillGPR, HighGPR, LowSpillOffset);
1058349cc55cSDimitry Andric
1059349cc55cSDimitry Andric return true;
1060349cc55cSDimitry Andric }
1061349cc55cSDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const1062349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::determineCalleeSaves(MachineFunction &MF,
1063349cc55cSDimitry Andric BitVector &SavedRegs,
1064349cc55cSDimitry Andric RegScavenger *RS) const {
1065349cc55cSDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1066349cc55cSDimitry Andric
1067349cc55cSDimitry Andric bool HasFP = hasFP(MF);
1068349cc55cSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1069349cc55cSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1070349cc55cSDimitry Andric
1071349cc55cSDimitry Andric // If the function requires a frame pointer, record that the hard
1072349cc55cSDimitry Andric // frame pointer will be clobbered.
1073349cc55cSDimitry Andric if (HasFP)
1074349cc55cSDimitry Andric SavedRegs.set(Regs.getFramePointerRegister());
1075349cc55cSDimitry Andric }
1076349cc55cSDimitry Andric
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const1077349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::spillCalleeSavedRegisters(
1078349cc55cSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
1079349cc55cSDimitry Andric ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
1080349cc55cSDimitry Andric if (CSI.empty())
1081349cc55cSDimitry Andric return true;
1082349cc55cSDimitry Andric
1083349cc55cSDimitry Andric MachineFunction &MF = *MBB.getParent();
1084349cc55cSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
1085349cc55cSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1086349cc55cSDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1087349cc55cSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1088349cc55cSDimitry Andric SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
1089349cc55cSDimitry Andric DebugLoc DL;
1090349cc55cSDimitry Andric
1091349cc55cSDimitry Andric // Save GPRs
1092349cc55cSDimitry Andric if (SpillGPRs.LowGPR) {
1093349cc55cSDimitry Andric assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
1094349cc55cSDimitry Andric "Should be saving multiple registers");
1095349cc55cSDimitry Andric
1096349cc55cSDimitry Andric // Build an STM/STMG instruction.
1097349cc55cSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
1098349cc55cSDimitry Andric
1099349cc55cSDimitry Andric // Add the explicit register operands.
1100349cc55cSDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
1101349cc55cSDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
1102349cc55cSDimitry Andric
1103349cc55cSDimitry Andric // Add the address r4
1104349cc55cSDimitry Andric MIB.addReg(Regs.getStackPointerRegister());
1105349cc55cSDimitry Andric
1106349cc55cSDimitry Andric // Add the partial offset
1107349cc55cSDimitry Andric // We cannot add the actual offset as, at the stack is not finalized
1108349cc55cSDimitry Andric MIB.addImm(SpillGPRs.GPROffset);
1109349cc55cSDimitry Andric
1110349cc55cSDimitry Andric // Make sure all call-saved GPRs are included as operands and are
1111349cc55cSDimitry Andric // marked as live on entry.
1112349cc55cSDimitry Andric auto &GRRegClass = SystemZ::GR64BitRegClass;
11134824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
111404eeddc0SDimitry Andric Register Reg = I.getReg();
1115349cc55cSDimitry Andric if (GRRegClass.contains(Reg))
1116349cc55cSDimitry Andric addSavedGPR(MBB, MIB, Reg, true);
1117349cc55cSDimitry Andric }
1118349cc55cSDimitry Andric }
1119349cc55cSDimitry Andric
1120349cc55cSDimitry Andric // Spill FPRs to the stack in the normal TargetInstrInfo way
11214824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
112204eeddc0SDimitry Andric Register Reg = I.getReg();
1123349cc55cSDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
1124349cc55cSDimitry Andric MBB.addLiveIn(Reg);
11254824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1126bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
1127349cc55cSDimitry Andric }
1128349cc55cSDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) {
1129349cc55cSDimitry Andric MBB.addLiveIn(Reg);
11304824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1131bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
1132349cc55cSDimitry Andric }
1133349cc55cSDimitry Andric }
1134349cc55cSDimitry Andric
1135349cc55cSDimitry Andric return true;
1136349cc55cSDimitry Andric }
1137349cc55cSDimitry Andric
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const11380eae32dcSDimitry Andric bool SystemZXPLINKFrameLowering::restoreCalleeSavedRegisters(
11390eae32dcSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
11400eae32dcSDimitry Andric MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
11410eae32dcSDimitry Andric
11420eae32dcSDimitry Andric if (CSI.empty())
11430eae32dcSDimitry Andric return false;
11440eae32dcSDimitry Andric
11450eae32dcSDimitry Andric MachineFunction &MF = *MBB.getParent();
11460eae32dcSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
11470eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
11480eae32dcSDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11490eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
11500eae32dcSDimitry Andric
11510eae32dcSDimitry Andric DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
11520eae32dcSDimitry Andric
11530eae32dcSDimitry Andric // Restore FPRs in the normal TargetInstrInfo way.
1154bdd1243dSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
1155bdd1243dSDimitry Andric Register Reg = I.getReg();
11560eae32dcSDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg))
1157bdd1243dSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1158bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
11590eae32dcSDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg))
1160bdd1243dSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1161bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
11620eae32dcSDimitry Andric }
11630eae32dcSDimitry Andric
11640eae32dcSDimitry Andric // Restore call-saved GPRs (but not call-clobbered varargs, which at
11650eae32dcSDimitry Andric // this point might hold return values).
11660eae32dcSDimitry Andric SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
11670eae32dcSDimitry Andric if (RestoreGPRs.LowGPR) {
11680eae32dcSDimitry Andric assert(isInt<20>(Regs.getStackPointerBias() + RestoreGPRs.GPROffset));
11690eae32dcSDimitry Andric if (RestoreGPRs.LowGPR == RestoreGPRs.HighGPR)
11700eae32dcSDimitry Andric // Build an LG/L instruction.
11710eae32dcSDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LG), RestoreGPRs.LowGPR)
11720eae32dcSDimitry Andric .addReg(Regs.getStackPointerRegister())
11730eae32dcSDimitry Andric .addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset)
11740eae32dcSDimitry Andric .addReg(0);
11750eae32dcSDimitry Andric else {
11760eae32dcSDimitry Andric // Build an LMG/LM instruction.
11770eae32dcSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
11780eae32dcSDimitry Andric
11790eae32dcSDimitry Andric // Add the explicit register operands.
11800eae32dcSDimitry Andric MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
11810eae32dcSDimitry Andric MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
11820eae32dcSDimitry Andric
11830eae32dcSDimitry Andric // Add the address.
11840eae32dcSDimitry Andric MIB.addReg(Regs.getStackPointerRegister());
11850eae32dcSDimitry Andric MIB.addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset);
11860eae32dcSDimitry Andric
11870eae32dcSDimitry Andric // Do a second scan adding regs as being defined by instruction
1188bdd1243dSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
1189bdd1243dSDimitry Andric Register Reg = I.getReg();
11900eae32dcSDimitry Andric if (Reg > RestoreGPRs.LowGPR && Reg < RestoreGPRs.HighGPR)
11910eae32dcSDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine);
11920eae32dcSDimitry Andric }
11930eae32dcSDimitry Andric }
11940eae32dcSDimitry Andric }
11950eae32dcSDimitry Andric
11960eae32dcSDimitry Andric return true;
11970eae32dcSDimitry Andric }
11980eae32dcSDimitry Andric
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const1199349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitPrologue(MachineFunction &MF,
12000eae32dcSDimitry Andric MachineBasicBlock &MBB) const {
12010eae32dcSDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
12020eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
12030eae32dcSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
12040eae32dcSDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin();
12050eae32dcSDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
12060eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
12070eae32dcSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
12080eae32dcSDimitry Andric MachineInstr *StoreInstr = nullptr;
1209fcaf7f86SDimitry Andric
1210fcaf7f86SDimitry Andric determineFrameLayout(MF);
1211fcaf7f86SDimitry Andric
12120eae32dcSDimitry Andric bool HasFP = hasFP(MF);
12130eae32dcSDimitry Andric // Debug location must be unknown since the first debug location is used
12140eae32dcSDimitry Andric // to determine the end of the prologue.
12150eae32dcSDimitry Andric DebugLoc DL;
12160eae32dcSDimitry Andric uint64_t Offset = 0;
12170eae32dcSDimitry Andric
1218fcaf7f86SDimitry Andric const uint64_t StackSize = MFFrame.getStackSize();
12190eae32dcSDimitry Andric
12200eae32dcSDimitry Andric if (ZFI->getSpillGPRRegs().LowGPR) {
12210eae32dcSDimitry Andric // Skip over the GPR saves.
12220eae32dcSDimitry Andric if ((MBBI != MBB.end()) && ((MBBI->getOpcode() == SystemZ::STMG))) {
12230eae32dcSDimitry Andric const int Operand = 3;
12240eae32dcSDimitry Andric // Now we can set the offset for the operation, since now the Stack
12250eae32dcSDimitry Andric // has been finalized.
12260eae32dcSDimitry Andric Offset = Regs.getStackPointerBias() + MBBI->getOperand(Operand).getImm();
12270eae32dcSDimitry Andric // Maximum displacement for STMG instruction.
12280eae32dcSDimitry Andric if (isInt<20>(Offset - StackSize))
12290eae32dcSDimitry Andric Offset -= StackSize;
12300eae32dcSDimitry Andric else
12310eae32dcSDimitry Andric StoreInstr = &*MBBI;
12320eae32dcSDimitry Andric MBBI->getOperand(Operand).setImm(Offset);
12330eae32dcSDimitry Andric ++MBBI;
12340eae32dcSDimitry Andric } else
12350eae32dcSDimitry Andric llvm_unreachable("Couldn't skip over GPR saves");
12360eae32dcSDimitry Andric }
12370eae32dcSDimitry Andric
12380eae32dcSDimitry Andric if (StackSize) {
12390eae32dcSDimitry Andric MachineBasicBlock::iterator InsertPt = StoreInstr ? StoreInstr : MBBI;
12400eae32dcSDimitry Andric // Allocate StackSize bytes.
12410eae32dcSDimitry Andric int64_t Delta = -int64_t(StackSize);
12420eae32dcSDimitry Andric
12430eae32dcSDimitry Andric // In case the STM(G) instruction also stores SP (R4), but the displacement
12440eae32dcSDimitry Andric // is too large, the SP register is manipulated first before storing,
12450eae32dcSDimitry Andric // resulting in the wrong value stored and retrieved later. In this case, we
12460eae32dcSDimitry Andric // need to temporarily save the value of SP, and store it later to memory.
12470eae32dcSDimitry Andric if (StoreInstr && HasFP) {
12480eae32dcSDimitry Andric // Insert LR r0,r4 before STMG instruction.
12490eae32dcSDimitry Andric BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::LGR))
12500eae32dcSDimitry Andric .addReg(SystemZ::R0D, RegState::Define)
12510eae32dcSDimitry Andric .addReg(SystemZ::R4D);
12520eae32dcSDimitry Andric // Insert ST r0,xxx(,r4) after STMG instruction.
12530eae32dcSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
12540eae32dcSDimitry Andric .addReg(SystemZ::R0D, RegState::Kill)
12550eae32dcSDimitry Andric .addReg(SystemZ::R4D)
12560eae32dcSDimitry Andric .addImm(Offset)
12570eae32dcSDimitry Andric .addReg(0);
12580eae32dcSDimitry Andric }
12590eae32dcSDimitry Andric
12600eae32dcSDimitry Andric emitIncrement(MBB, InsertPt, DL, Regs.getStackPointerRegister(), Delta,
12610eae32dcSDimitry Andric ZII);
126281ad6265SDimitry Andric
126381ad6265SDimitry Andric // If the requested stack size is larger than the guard page, then we need
126481ad6265SDimitry Andric // to check if we need to call the stack extender. This requires adding a
126581ad6265SDimitry Andric // conditional branch, but splitting the prologue block is not possible at
126681ad6265SDimitry Andric // this point since it would invalidate the SaveBlocks / RestoreBlocks sets
126781ad6265SDimitry Andric // of PEI in the single block function case. Build a pseudo to be handled
126881ad6265SDimitry Andric // later by inlineStackProbe().
126981ad6265SDimitry Andric const uint64_t GuardPageSize = 1024 * 1024;
127081ad6265SDimitry Andric if (StackSize > GuardPageSize) {
127181ad6265SDimitry Andric assert(StoreInstr && "Wrong insertion point");
127281ad6265SDimitry Andric BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::XPLINK_STACKALLOC));
127381ad6265SDimitry Andric }
12740eae32dcSDimitry Andric }
12750eae32dcSDimitry Andric
12760eae32dcSDimitry Andric if (HasFP) {
12770eae32dcSDimitry Andric // Copy the base of the frame to Frame Pointer Register.
12780eae32dcSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR),
12790eae32dcSDimitry Andric Regs.getFramePointerRegister())
12800eae32dcSDimitry Andric .addReg(Regs.getStackPointerRegister());
12810eae32dcSDimitry Andric
12820eae32dcSDimitry Andric // Mark the FramePtr as live at the beginning of every block except
12830eae32dcSDimitry Andric // the entry block. (We'll have marked R8 as live on entry when
12840eae32dcSDimitry Andric // saving the GPRs.)
1285fcaf7f86SDimitry Andric for (MachineBasicBlock &B : llvm::drop_begin(MF))
1286fcaf7f86SDimitry Andric B.addLiveIn(Regs.getFramePointerRegister());
12870eae32dcSDimitry Andric }
1288c9157d92SDimitry Andric
1289c9157d92SDimitry Andric // Save GPRs used for varargs, if any.
1290c9157d92SDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1291c9157d92SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
1292c9157d92SDimitry Andric
1293c9157d92SDimitry Andric if (IsVarArg) {
1294c9157d92SDimitry Andric // FixedRegs is the number of used registers, accounting for shadow
1295c9157d92SDimitry Andric // registers.
1296c9157d92SDimitry Andric unsigned FixedRegs = ZFI->getVarArgsFirstGPR() + ZFI->getVarArgsFirstFPR();
1297c9157d92SDimitry Andric auto &GPRs = SystemZ::XPLINK64ArgGPRs;
1298c9157d92SDimitry Andric for (unsigned I = FixedRegs; I < SystemZ::XPLINK64NumArgGPRs; I++) {
1299c9157d92SDimitry Andric uint64_t StartOffset = MFFrame.getOffsetAdjustment() +
1300c9157d92SDimitry Andric MFFrame.getStackSize() + Regs.getCallFrameSize() +
1301c9157d92SDimitry Andric getOffsetOfLocalArea() + I * 8;
1302c9157d92SDimitry Andric unsigned Reg = GPRs[I];
1303c9157d92SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STG))
1304c9157d92SDimitry Andric .addReg(Reg)
1305c9157d92SDimitry Andric .addReg(Regs.getStackPointerRegister())
1306c9157d92SDimitry Andric .addImm(StartOffset)
1307c9157d92SDimitry Andric .addReg(0);
1308c9157d92SDimitry Andric if (!MBB.isLiveIn(Reg))
1309c9157d92SDimitry Andric MBB.addLiveIn(Reg);
1310c9157d92SDimitry Andric }
1311c9157d92SDimitry Andric }
13120eae32dcSDimitry Andric }
1313349cc55cSDimitry Andric
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const1314349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitEpilogue(MachineFunction &MF,
13150eae32dcSDimitry Andric MachineBasicBlock &MBB) const {
13160eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
13170eae32dcSDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
13180eae32dcSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
13190eae32dcSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
13200eae32dcSDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
13210eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
13220eae32dcSDimitry Andric
13230eae32dcSDimitry Andric // Skip the return instruction.
13240eae32dcSDimitry Andric assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
13250eae32dcSDimitry Andric
13260eae32dcSDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
13270eae32dcSDimitry Andric if (StackSize) {
13280eae32dcSDimitry Andric unsigned SPReg = Regs.getStackPointerRegister();
13290eae32dcSDimitry Andric if (ZFI->getRestoreGPRRegs().LowGPR != SPReg) {
13300eae32dcSDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
13310eae32dcSDimitry Andric emitIncrement(MBB, MBBI, DL, SPReg, StackSize, ZII);
13320eae32dcSDimitry Andric }
13330eae32dcSDimitry Andric }
13340eae32dcSDimitry Andric }
1335349cc55cSDimitry Andric
133681ad6265SDimitry Andric // Emit a compare of the stack pointer against the stack floor, and a call to
133781ad6265SDimitry Andric // the LE stack extender if needed.
inlineStackProbe(MachineFunction & MF,MachineBasicBlock & PrologMBB) const133881ad6265SDimitry Andric void SystemZXPLINKFrameLowering::inlineStackProbe(
133981ad6265SDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
134081ad6265SDimitry Andric auto *ZII =
134181ad6265SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
134281ad6265SDimitry Andric
134381ad6265SDimitry Andric MachineInstr *StackAllocMI = nullptr;
134481ad6265SDimitry Andric for (MachineInstr &MI : PrologMBB)
134581ad6265SDimitry Andric if (MI.getOpcode() == SystemZ::XPLINK_STACKALLOC) {
134681ad6265SDimitry Andric StackAllocMI = &MI;
134781ad6265SDimitry Andric break;
134881ad6265SDimitry Andric }
134981ad6265SDimitry Andric if (StackAllocMI == nullptr)
135081ad6265SDimitry Andric return;
135181ad6265SDimitry Andric
1352fe013be4SDimitry Andric bool NeedSaveSP = hasFP(MF);
1353fe013be4SDimitry Andric bool NeedSaveArg = PrologMBB.isLiveIn(SystemZ::R3D);
1354fe013be4SDimitry Andric const int64_t SaveSlotR3 = 2192;
1355fe013be4SDimitry Andric
135681ad6265SDimitry Andric MachineBasicBlock &MBB = PrologMBB;
135781ad6265SDimitry Andric const DebugLoc DL = StackAllocMI->getDebugLoc();
135881ad6265SDimitry Andric
135981ad6265SDimitry Andric // The 2nd half of block MBB after split.
136081ad6265SDimitry Andric MachineBasicBlock *NextMBB;
136181ad6265SDimitry Andric
136281ad6265SDimitry Andric // Add new basic block for the call to the stack overflow function.
136381ad6265SDimitry Andric MachineBasicBlock *StackExtMBB =
136481ad6265SDimitry Andric MF.CreateMachineBasicBlock(MBB.getBasicBlock());
136581ad6265SDimitry Andric MF.push_back(StackExtMBB);
136681ad6265SDimitry Andric
136781ad6265SDimitry Andric // LG r3,72(,r3)
136881ad6265SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::LG), SystemZ::R3D)
136981ad6265SDimitry Andric .addReg(SystemZ::R3D)
137081ad6265SDimitry Andric .addImm(72)
137181ad6265SDimitry Andric .addReg(0);
137281ad6265SDimitry Andric // BASR r3,r3
137381ad6265SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::CallBASR_STACKEXT))
137481ad6265SDimitry Andric .addReg(SystemZ::R3D);
1375fe013be4SDimitry Andric if (NeedSaveArg) {
1376fe013be4SDimitry Andric if (!NeedSaveSP) {
1377fe013be4SDimitry Andric // LGR r0,r3
1378fe013be4SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
1379fe013be4SDimitry Andric .addReg(SystemZ::R0D, RegState::Define)
1380fe013be4SDimitry Andric .addReg(SystemZ::R3D);
1381fe013be4SDimitry Andric } else {
1382fe013be4SDimitry Andric // In this case, the incoming value of r4 is saved in r0 so the
1383fe013be4SDimitry Andric // latter register is unavailable. Store r3 in its corresponding
1384fe013be4SDimitry Andric // slot in the parameter list instead. Do this at the start of
1385fe013be4SDimitry Andric // the prolog before r4 is manipulated by anything else.
1386fe013be4SDimitry Andric // STG r3, 2192(r4)
1387fe013be4SDimitry Andric BuildMI(MBB, MBB.begin(), DL, ZII->get(SystemZ::STG))
1388fe013be4SDimitry Andric .addReg(SystemZ::R3D)
1389fe013be4SDimitry Andric .addReg(SystemZ::R4D)
1390fe013be4SDimitry Andric .addImm(SaveSlotR3)
1391fe013be4SDimitry Andric .addReg(0);
1392fe013be4SDimitry Andric }
1393fe013be4SDimitry Andric }
139481ad6265SDimitry Andric // LLGT r3,1208
139581ad6265SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LLGT), SystemZ::R3D)
139681ad6265SDimitry Andric .addReg(0)
139781ad6265SDimitry Andric .addImm(1208)
139881ad6265SDimitry Andric .addReg(0);
139981ad6265SDimitry Andric // CG r4,64(,r3)
140081ad6265SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::CG))
140181ad6265SDimitry Andric .addReg(SystemZ::R4D)
140281ad6265SDimitry Andric .addReg(SystemZ::R3D)
140381ad6265SDimitry Andric .addImm(64)
140481ad6265SDimitry Andric .addReg(0);
140581ad6265SDimitry Andric // JLL b'0100',F'37'
140681ad6265SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::BRC))
140781ad6265SDimitry Andric .addImm(SystemZ::CCMASK_ICMP)
140881ad6265SDimitry Andric .addImm(SystemZ::CCMASK_CMP_LT)
140981ad6265SDimitry Andric .addMBB(StackExtMBB);
141081ad6265SDimitry Andric
141181ad6265SDimitry Andric NextMBB = SystemZ::splitBlockBefore(StackAllocMI, &MBB);
141281ad6265SDimitry Andric MBB.addSuccessor(NextMBB);
141381ad6265SDimitry Andric MBB.addSuccessor(StackExtMBB);
1414fe013be4SDimitry Andric if (NeedSaveArg) {
1415fe013be4SDimitry Andric if (!NeedSaveSP) {
1416fe013be4SDimitry Andric // LGR r3, r0
1417fe013be4SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
1418fe013be4SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
1419fe013be4SDimitry Andric .addReg(SystemZ::R0D, RegState::Kill);
1420fe013be4SDimitry Andric } else {
1421fe013be4SDimitry Andric // In this case, the incoming value of r4 is saved in r0 so the
1422fe013be4SDimitry Andric // latter register is unavailable. We stored r3 in its corresponding
1423fe013be4SDimitry Andric // slot in the parameter list instead and we now restore it from there.
1424fe013be4SDimitry Andric // LGR r3, r0
1425fe013be4SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
1426fe013be4SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
1427fe013be4SDimitry Andric .addReg(SystemZ::R0D);
1428fe013be4SDimitry Andric // LG r3, 2192(r3)
1429fe013be4SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LG))
1430fe013be4SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
1431fe013be4SDimitry Andric .addReg(SystemZ::R3D)
1432fe013be4SDimitry Andric .addImm(SaveSlotR3)
1433fe013be4SDimitry Andric .addReg(0);
1434fe013be4SDimitry Andric }
1435fe013be4SDimitry Andric }
143681ad6265SDimitry Andric
143781ad6265SDimitry Andric // Add jump back from stack extension BB.
143881ad6265SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::J)).addMBB(NextMBB);
143981ad6265SDimitry Andric StackExtMBB->addSuccessor(NextMBB);
144081ad6265SDimitry Andric
144181ad6265SDimitry Andric StackAllocMI->eraseFromParent();
144281ad6265SDimitry Andric
144381ad6265SDimitry Andric // Compute the live-in lists for the new blocks.
1444*b9d9368bSDimitry Andric bool anyChange = false;
1445*b9d9368bSDimitry Andric do {
1446*b9d9368bSDimitry Andric anyChange = recomputeLiveIns(*StackExtMBB) || recomputeLiveIns(*NextMBB);
1447*b9d9368bSDimitry Andric } while (anyChange);
144881ad6265SDimitry Andric }
144981ad6265SDimitry Andric
hasFP(const MachineFunction & MF) const1450349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {
14510eae32dcSDimitry Andric return (MF.getFrameInfo().hasVarSizedObjects());
14520eae32dcSDimitry Andric }
14530eae32dcSDimitry Andric
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const14540eae32dcSDimitry Andric void SystemZXPLINKFrameLowering::processFunctionBeforeFrameFinalized(
14550eae32dcSDimitry Andric MachineFunction &MF, RegScavenger *RS) const {
14560eae32dcSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
14570eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
14580eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
14590eae32dcSDimitry Andric
14600eae32dcSDimitry Andric // Setup stack frame offset
14610eae32dcSDimitry Andric MFFrame.setOffsetAdjustment(Regs.getStackPointerBias());
1462c9157d92SDimitry Andric
1463c9157d92SDimitry Andric // Nothing to do for leaf functions.
1464c9157d92SDimitry Andric uint64_t StackSize = MFFrame.estimateStackSize(MF);
1465c9157d92SDimitry Andric if (StackSize == 0 && MFFrame.getCalleeSavedInfo().empty())
1466c9157d92SDimitry Andric return;
1467c9157d92SDimitry Andric
1468c9157d92SDimitry Andric // Although the XPLINK specifications for AMODE64 state that minimum size
1469c9157d92SDimitry Andric // of the param area is minimum 32 bytes and no rounding is otherwise
1470c9157d92SDimitry Andric // specified, we round this area in 64 bytes increments to be compatible
1471c9157d92SDimitry Andric // with existing compilers.
1472c9157d92SDimitry Andric MFFrame.setMaxCallFrameSize(
1473c9157d92SDimitry Andric std::max(64U, (unsigned)alignTo(MFFrame.getMaxCallFrameSize(), 64)));
1474349cc55cSDimitry Andric }
1475fcaf7f86SDimitry Andric
1476fcaf7f86SDimitry Andric // Determines the size of the frame, and creates the deferred spill objects.
determineFrameLayout(MachineFunction & MF) const1477fcaf7f86SDimitry Andric void SystemZXPLINKFrameLowering::determineFrameLayout(
1478fcaf7f86SDimitry Andric MachineFunction &MF) const {
1479fcaf7f86SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
1480fcaf7f86SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1481fcaf7f86SDimitry Andric auto *Regs =
1482fcaf7f86SDimitry Andric static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
1483fcaf7f86SDimitry Andric
1484fcaf7f86SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
1485fcaf7f86SDimitry Andric if (StackSize == 0)
1486fcaf7f86SDimitry Andric return;
1487fcaf7f86SDimitry Andric
1488fcaf7f86SDimitry Andric // Add the size of the register save area and the reserved area to the size.
1489fcaf7f86SDimitry Andric StackSize += Regs->getCallFrameSize();
1490fcaf7f86SDimitry Andric MFFrame.setStackSize(StackSize);
1491fcaf7f86SDimitry Andric
1492fcaf7f86SDimitry Andric // We now know the stack size. Create the fixed spill stack objects for the
1493fcaf7f86SDimitry Andric // register save area now. This has no impact on the stack frame layout, as
1494fcaf7f86SDimitry Andric // this is already computed. However, it makes sure that all callee saved
1495fcaf7f86SDimitry Andric // registers have a valid frame index assigned.
1496fcaf7f86SDimitry Andric const unsigned RegSize = MF.getDataLayout().getPointerSize();
1497fcaf7f86SDimitry Andric for (auto &CS : MFFrame.getCalleeSavedInfo()) {
1498fcaf7f86SDimitry Andric int Offset = RegSpillOffsets[CS.getReg()];
1499fcaf7f86SDimitry Andric if (Offset >= 0)
1500fcaf7f86SDimitry Andric CS.setFrameIdx(
1501fcaf7f86SDimitry Andric MFFrame.CreateFixedSpillStackObject(RegSize, Offset - StackSize));
1502fcaf7f86SDimitry Andric }
1503fcaf7f86SDimitry Andric }
1504