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"
20cb14a3feSDimitry 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,unsigned PointerSize)57349cc55cSDimitry Andric SystemZFrameLowering::SystemZFrameLowering(StackDirection D, Align StackAl,
58349cc55cSDimitry Andric int LAO, Align TransAl,
59*0fca6ea1SDimitry Andric bool StackReal, unsigned PointerSize)
60*0fca6ea1SDimitry Andric : TargetFrameLowering(D, StackAl, LAO, TransAl, StackReal),
61*0fca6ea1SDimitry Andric PointerSize(PointerSize) {}
62480093f4SDimitry Andric
63349cc55cSDimitry Andric std::unique_ptr<SystemZFrameLowering>
create(const SystemZSubtarget & STI)64349cc55cSDimitry Andric SystemZFrameLowering::create(const SystemZSubtarget &STI) {
65*0fca6ea1SDimitry Andric unsigned PtrSz =
66*0fca6ea1SDimitry Andric STI.getTargetLowering()->getTargetMachine().getPointerSize(0);
67349cc55cSDimitry Andric if (STI.isTargetXPLINK64())
68*0fca6ea1SDimitry Andric return std::make_unique<SystemZXPLINKFrameLowering>(PtrSz);
69*0fca6ea1SDimitry Andric return std::make_unique<SystemZELFFrameLowering>(PtrSz);
70349cc55cSDimitry Andric }
71349cc55cSDimitry Andric
721fd87a68SDimitry Andric namespace {
731fd87a68SDimitry Andric struct SZFrameSortingObj {
741fd87a68SDimitry Andric bool IsValid = false; // True if we care about this Object.
751fd87a68SDimitry Andric uint32_t ObjectIndex = 0; // Index of Object into MFI list.
761fd87a68SDimitry Andric uint64_t ObjectSize = 0; // Size of Object in bytes.
771fd87a68SDimitry Andric uint32_t D12Count = 0; // 12-bit displacement only.
781fd87a68SDimitry Andric uint32_t DPairCount = 0; // 12 or 20 bit displacement.
791fd87a68SDimitry Andric };
801fd87a68SDimitry Andric typedef std::vector<SZFrameSortingObj> SZFrameObjVec;
811fd87a68SDimitry Andric } // namespace
821fd87a68SDimitry Andric
831fd87a68SDimitry Andric // TODO: Move to base class.
orderFrameObjects(const MachineFunction & MF,SmallVectorImpl<int> & ObjectsToAllocate) const841fd87a68SDimitry Andric void SystemZELFFrameLowering::orderFrameObjects(
851fd87a68SDimitry Andric const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const {
861fd87a68SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
8781ad6265SDimitry Andric auto *TII = MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
881fd87a68SDimitry Andric
891fd87a68SDimitry Andric // Make a vector of sorting objects to track all MFI objects and mark those
901fd87a68SDimitry Andric // to be sorted as valid.
911fd87a68SDimitry Andric if (ObjectsToAllocate.size() <= 1)
921fd87a68SDimitry Andric return;
931fd87a68SDimitry Andric SZFrameObjVec SortingObjects(MFI.getObjectIndexEnd());
941fd87a68SDimitry Andric for (auto &Obj : ObjectsToAllocate) {
951fd87a68SDimitry Andric SortingObjects[Obj].IsValid = true;
961fd87a68SDimitry Andric SortingObjects[Obj].ObjectIndex = Obj;
971fd87a68SDimitry Andric SortingObjects[Obj].ObjectSize = MFI.getObjectSize(Obj);
981fd87a68SDimitry Andric }
991fd87a68SDimitry Andric
1001fd87a68SDimitry Andric // Examine uses for each object and record short (12-bit) and "pair"
1011fd87a68SDimitry Andric // displacement types.
1021fd87a68SDimitry Andric for (auto &MBB : MF)
1031fd87a68SDimitry Andric for (auto &MI : MBB) {
1041fd87a68SDimitry Andric if (MI.isDebugInstr())
1051fd87a68SDimitry Andric continue;
1061fd87a68SDimitry Andric for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
1071fd87a68SDimitry Andric const MachineOperand &MO = MI.getOperand(I);
1081fd87a68SDimitry Andric if (!MO.isFI())
1091fd87a68SDimitry Andric continue;
1101fd87a68SDimitry Andric int Index = MO.getIndex();
1111fd87a68SDimitry Andric if (Index >= 0 && Index < MFI.getObjectIndexEnd() &&
1121fd87a68SDimitry Andric SortingObjects[Index].IsValid) {
1131fd87a68SDimitry Andric if (TII->hasDisplacementPairInsn(MI.getOpcode()))
1141fd87a68SDimitry Andric SortingObjects[Index].DPairCount++;
1151fd87a68SDimitry Andric else if (!(MI.getDesc().TSFlags & SystemZII::Has20BitOffset))
1161fd87a68SDimitry Andric SortingObjects[Index].D12Count++;
1171fd87a68SDimitry Andric }
1181fd87a68SDimitry Andric }
1191fd87a68SDimitry Andric }
1201fd87a68SDimitry Andric
1211fd87a68SDimitry Andric // Sort all objects for short/paired displacements, which should be
1221fd87a68SDimitry Andric // sufficient as it seems like all frame objects typically are within the
1231fd87a68SDimitry Andric // long displacement range. Sorting works by computing the "density" as
1241fd87a68SDimitry Andric // Count / ObjectSize. The comparisons of two such fractions are refactored
1251fd87a68SDimitry Andric // by multiplying both sides with A.ObjectSize * B.ObjectSize, in order to
1261fd87a68SDimitry Andric // eliminate the (fp) divisions. A higher density object needs to go after
1271fd87a68SDimitry Andric // in the list in order for it to end up lower on the stack.
1281fd87a68SDimitry Andric auto CmpD12 = [](const SZFrameSortingObj &A, const SZFrameSortingObj &B) {
1291fd87a68SDimitry Andric // Put all invalid and variable sized objects at the end.
1301fd87a68SDimitry Andric if (!A.IsValid || !B.IsValid)
1311fd87a68SDimitry Andric return A.IsValid;
1321fd87a68SDimitry Andric if (!A.ObjectSize || !B.ObjectSize)
1331fd87a68SDimitry Andric return A.ObjectSize > 0;
1341fd87a68SDimitry Andric uint64_t ADensityCmp = A.D12Count * B.ObjectSize;
1351fd87a68SDimitry Andric uint64_t BDensityCmp = B.D12Count * A.ObjectSize;
1361fd87a68SDimitry Andric if (ADensityCmp != BDensityCmp)
1371fd87a68SDimitry Andric return ADensityCmp < BDensityCmp;
1381fd87a68SDimitry Andric return A.DPairCount * B.ObjectSize < B.DPairCount * A.ObjectSize;
1391fd87a68SDimitry Andric };
1401fd87a68SDimitry Andric std::stable_sort(SortingObjects.begin(), SortingObjects.end(), CmpD12);
1411fd87a68SDimitry Andric
1421fd87a68SDimitry Andric // Now modify the original list to represent the final order that
1431fd87a68SDimitry Andric // we want.
1441fd87a68SDimitry Andric unsigned Idx = 0;
1451fd87a68SDimitry Andric for (auto &Obj : SortingObjects) {
1461fd87a68SDimitry Andric // All invalid items are sorted at the end, so it's safe to stop.
1471fd87a68SDimitry Andric if (!Obj.IsValid)
1481fd87a68SDimitry Andric break;
1491fd87a68SDimitry Andric ObjectsToAllocate[Idx++] = Obj.ObjectIndex;
1501fd87a68SDimitry Andric }
1511fd87a68SDimitry Andric }
1521fd87a68SDimitry Andric
hasReservedCallFrame(const MachineFunction & MF) const153349cc55cSDimitry Andric bool SystemZFrameLowering::hasReservedCallFrame(
154349cc55cSDimitry Andric const MachineFunction &MF) const {
155349cc55cSDimitry Andric // The ELF ABI requires us to allocate 160 bytes of stack space for the
156349cc55cSDimitry Andric // callee, with any outgoing stack arguments being placed above that. It
157349cc55cSDimitry Andric // seems better to make that area a permanent feature of the frame even if
158349cc55cSDimitry Andric // we're using a frame pointer. Similarly, 64-bit XPLINK requires 96 bytes
159349cc55cSDimitry Andric // of stack space for the register save area.
160349cc55cSDimitry Andric return true;
161349cc55cSDimitry Andric }
162349cc55cSDimitry Andric
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const163349cc55cSDimitry Andric bool SystemZELFFrameLowering::assignCalleeSavedSpillSlots(
164349cc55cSDimitry Andric MachineFunction &MF, const TargetRegisterInfo *TRI,
165480093f4SDimitry Andric std::vector<CalleeSavedInfo> &CSI) const {
166480093f4SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
167480093f4SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
168480093f4SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
169480093f4SDimitry Andric if (CSI.empty())
170480093f4SDimitry Andric return true; // Early exit if no callee saved registers are modified!
171480093f4SDimitry Andric
172480093f4SDimitry Andric unsigned LowGPR = 0;
173480093f4SDimitry Andric unsigned HighGPR = SystemZ::R15D;
174fe6060f1SDimitry Andric int StartSPOffset = SystemZMC::ELFCallFrameSize;
175480093f4SDimitry Andric for (auto &CS : CSI) {
17604eeddc0SDimitry Andric Register Reg = CS.getReg();
1775ffd83dbSDimitry Andric int Offset = getRegSpillOffset(MF, Reg);
178480093f4SDimitry Andric if (Offset) {
179480093f4SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg) && StartSPOffset > Offset) {
180480093f4SDimitry Andric LowGPR = Reg;
181480093f4SDimitry Andric StartSPOffset = Offset;
182480093f4SDimitry Andric }
183fe6060f1SDimitry Andric Offset -= SystemZMC::ELFCallFrameSize;
184*0fca6ea1SDimitry Andric int FrameIdx =
185*0fca6ea1SDimitry Andric MFFrame.CreateFixedSpillStackObject(getPointerSize(), Offset);
186480093f4SDimitry Andric CS.setFrameIdx(FrameIdx);
187480093f4SDimitry Andric } else
188480093f4SDimitry Andric CS.setFrameIdx(INT32_MAX);
189480093f4SDimitry Andric }
190480093f4SDimitry Andric
191480093f4SDimitry Andric // Save the range of call-saved registers, for use by the
192480093f4SDimitry Andric // prologue/epilogue inserters.
193480093f4SDimitry Andric ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset);
194480093f4SDimitry Andric if (IsVarArg) {
195480093f4SDimitry Andric // Also save the GPR varargs, if any. R6D is call-saved, so would
196480093f4SDimitry Andric // already be included, but we also need to handle the call-clobbered
197480093f4SDimitry Andric // argument registers.
19804eeddc0SDimitry Andric Register FirstGPR = ZFI->getVarArgsFirstGPR();
199fe6060f1SDimitry Andric if (FirstGPR < SystemZ::ELFNumArgGPRs) {
200fe6060f1SDimitry Andric unsigned Reg = SystemZ::ELFArgGPRs[FirstGPR];
2015ffd83dbSDimitry Andric int Offset = getRegSpillOffset(MF, Reg);
202480093f4SDimitry Andric if (StartSPOffset > Offset) {
203480093f4SDimitry Andric LowGPR = Reg; StartSPOffset = Offset;
204480093f4SDimitry Andric }
205480093f4SDimitry Andric }
206480093f4SDimitry Andric }
207480093f4SDimitry Andric ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset);
208480093f4SDimitry Andric
209480093f4SDimitry Andric // Create fixed stack objects for the remaining registers.
210fe6060f1SDimitry Andric int CurrOffset = -SystemZMC::ELFCallFrameSize;
2115ffd83dbSDimitry Andric if (usePackedStack(MF))
2125ffd83dbSDimitry Andric CurrOffset += StartSPOffset;
2135ffd83dbSDimitry Andric
214480093f4SDimitry Andric for (auto &CS : CSI) {
215480093f4SDimitry Andric if (CS.getFrameIdx() != INT32_MAX)
216480093f4SDimitry Andric continue;
21704eeddc0SDimitry Andric Register Reg = CS.getReg();
218480093f4SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
219480093f4SDimitry Andric unsigned Size = TRI->getSpillSize(*RC);
220480093f4SDimitry Andric CurrOffset -= Size;
221480093f4SDimitry Andric assert(CurrOffset % 8 == 0 &&
222480093f4SDimitry Andric "8-byte alignment required for for all register save slots");
223480093f4SDimitry Andric int FrameIdx = MFFrame.CreateFixedSpillStackObject(Size, CurrOffset);
224480093f4SDimitry Andric CS.setFrameIdx(FrameIdx);
225480093f4SDimitry Andric }
226480093f4SDimitry Andric
227480093f4SDimitry Andric return true;
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const230349cc55cSDimitry Andric void SystemZELFFrameLowering::determineCalleeSaves(MachineFunction &MF,
2310b57cec5SDimitry Andric BitVector &SavedRegs,
2320b57cec5SDimitry Andric RegScavenger *RS) const {
2330b57cec5SDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
2360b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2370b57cec5SDimitry Andric bool HasFP = hasFP(MF);
2380b57cec5SDimitry Andric SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
2390b57cec5SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric // va_start stores incoming FPR varargs in the normal way, but delegates
2420b57cec5SDimitry Andric // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
2430b57cec5SDimitry Andric // Record these pending uses, which typically include the call-saved
2440b57cec5SDimitry Andric // argument register R6D.
2450b57cec5SDimitry Andric if (IsVarArg)
246fe6060f1SDimitry Andric for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
247fe6060f1SDimitry Andric SavedRegs.set(SystemZ::ELFArgGPRs[I]);
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric // If there are any landing pads, entering them will modify r6/r7.
2500b57cec5SDimitry Andric if (!MF.getLandingPads().empty()) {
2510b57cec5SDimitry Andric SavedRegs.set(SystemZ::R6D);
2520b57cec5SDimitry Andric SavedRegs.set(SystemZ::R7D);
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric // If the function requires a frame pointer, record that the hard
2560b57cec5SDimitry Andric // frame pointer will be clobbered.
2570b57cec5SDimitry Andric if (HasFP)
2580b57cec5SDimitry Andric SavedRegs.set(SystemZ::R11D);
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric // If the function calls other functions, record that the return
2610b57cec5SDimitry Andric // address register will be clobbered.
2620b57cec5SDimitry Andric if (MFFrame.hasCalls())
2630b57cec5SDimitry Andric SavedRegs.set(SystemZ::R14D);
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric // If we are saving GPRs other than the stack pointer, we might as well
2660b57cec5SDimitry Andric // save and restore the stack pointer at the same time, via STMG and LMG.
2670b57cec5SDimitry Andric // This allows the deallocation to be done by the LMG, rather than needing
2680b57cec5SDimitry Andric // a separate %r15 addition.
2690b57cec5SDimitry Andric const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
2700b57cec5SDimitry Andric for (unsigned I = 0; CSRegs[I]; ++I) {
2710b57cec5SDimitry Andric unsigned Reg = CSRegs[I];
2720b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
2730b57cec5SDimitry Andric SavedRegs.set(SystemZ::R15D);
2740b57cec5SDimitry Andric break;
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric
SystemZELFFrameLowering(unsigned PointerSize)279*0fca6ea1SDimitry Andric SystemZELFFrameLowering::SystemZELFFrameLowering(unsigned PointerSize)
280349cc55cSDimitry Andric : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8), 0,
281*0fca6ea1SDimitry Andric Align(8), /* StackRealignable */ false, PointerSize),
282349cc55cSDimitry Andric RegSpillOffsets(0) {
283349cc55cSDimitry Andric
284349cc55cSDimitry Andric // Due to the SystemZ ABI, the DWARF CFA (Canonical Frame Address) is not
285349cc55cSDimitry Andric // equal to the incoming stack pointer, but to incoming stack pointer plus
286349cc55cSDimitry Andric // 160. Instead of using a Local Area Offset, the Register save area will
287349cc55cSDimitry Andric // be occupied by fixed frame objects, and all offsets are actually
288349cc55cSDimitry Andric // relative to CFA.
289349cc55cSDimitry Andric
290349cc55cSDimitry Andric // Create a mapping from register number to save slot offset.
291349cc55cSDimitry Andric // These offsets are relative to the start of the register save area.
292349cc55cSDimitry Andric RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
293bdd1243dSDimitry Andric for (const auto &Entry : ELFSpillOffsetTable)
294bdd1243dSDimitry Andric RegSpillOffsets[Entry.Reg] = Entry.Offset;
295349cc55cSDimitry Andric }
296349cc55cSDimitry Andric
2970b57cec5SDimitry Andric // Add GPR64 to the save instruction being built by MIB, which is in basic
2980b57cec5SDimitry Andric // block MBB. IsImplicit says whether this is an explicit operand to the
2990b57cec5SDimitry Andric // instruction, or an implicit one that comes between the explicit start
3000b57cec5SDimitry Andric // and end registers.
addSavedGPR(MachineBasicBlock & MBB,MachineInstrBuilder & MIB,unsigned GPR64,bool IsImplicit)3010b57cec5SDimitry Andric static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
3020b57cec5SDimitry Andric unsigned GPR64, bool IsImplicit) {
3030b57cec5SDimitry Andric const TargetRegisterInfo *RI =
3040b57cec5SDimitry Andric MBB.getParent()->getSubtarget().getRegisterInfo();
3058bcb0991SDimitry Andric Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
3060b57cec5SDimitry Andric bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
3070b57cec5SDimitry Andric if (!IsLive || !IsImplicit) {
3080b57cec5SDimitry Andric MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
3090b57cec5SDimitry Andric if (!IsLive)
3100b57cec5SDimitry Andric MBB.addLiveIn(GPR64);
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const314349cc55cSDimitry Andric bool SystemZELFFrameLowering::spillCalleeSavedRegisters(
3155ffd83dbSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
3165ffd83dbSDimitry Andric ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
3170b57cec5SDimitry Andric if (CSI.empty())
3180b57cec5SDimitry Andric return false;
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent();
3210b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
3220b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
3230b57cec5SDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
3240b57cec5SDimitry Andric DebugLoc DL;
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric // Save GPRs
327480093f4SDimitry Andric SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
328480093f4SDimitry Andric if (SpillGPRs.LowGPR) {
329480093f4SDimitry Andric assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
330480093f4SDimitry Andric "Should be saving %r15 and something else");
3310b57cec5SDimitry Andric
3320b57cec5SDimitry Andric // Build an STMG instruction.
3330b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
3340b57cec5SDimitry Andric
3350b57cec5SDimitry Andric // Add the explicit register operands.
336480093f4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
337480093f4SDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andric // Add the address.
340480093f4SDimitry Andric MIB.addReg(SystemZ::R15D).addImm(SpillGPRs.GPROffset);
3410b57cec5SDimitry Andric
3420b57cec5SDimitry Andric // Make sure all call-saved GPRs are included as operands and are
3430b57cec5SDimitry Andric // marked as live on entry.
3444824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
34504eeddc0SDimitry Andric Register Reg = I.getReg();
3460b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg))
3470b57cec5SDimitry Andric addSavedGPR(MBB, MIB, Reg, true);
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andric // ...likewise GPR varargs.
3510b57cec5SDimitry Andric if (IsVarArg)
352fe6060f1SDimitry Andric for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
353fe6060f1SDimitry Andric addSavedGPR(MBB, MIB, SystemZ::ELFArgGPRs[I], true);
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric // Save FPRs/VRs in the normal TargetInstrInfo way.
3574824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
35804eeddc0SDimitry Andric Register Reg = I.getReg();
3590b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
3600b57cec5SDimitry Andric MBB.addLiveIn(Reg);
3614824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
362bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) {
3650b57cec5SDimitry Andric MBB.addLiveIn(Reg);
3664824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
367bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
3680b57cec5SDimitry Andric }
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric
3710b57cec5SDimitry Andric return true;
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const374349cc55cSDimitry Andric bool SystemZELFFrameLowering::restoreCalleeSavedRegisters(
3755ffd83dbSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
3765ffd83dbSDimitry Andric MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
3770b57cec5SDimitry Andric if (CSI.empty())
3780b57cec5SDimitry Andric return false;
3790b57cec5SDimitry Andric
3800b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent();
3810b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
3820b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
3830b57cec5SDimitry Andric bool HasFP = hasFP(MF);
3840b57cec5SDimitry Andric DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric // Restore FPRs/VRs in the normal TargetInstrInfo way.
3874824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
38804eeddc0SDimitry Andric Register Reg = I.getReg();
3890b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg))
3904824e7fdSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
391bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
3920b57cec5SDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg))
3934824e7fdSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
394bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric
3970b57cec5SDimitry Andric // Restore call-saved GPRs (but not call-clobbered varargs, which at
3980b57cec5SDimitry Andric // this point might hold return values).
399480093f4SDimitry Andric SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
400480093f4SDimitry Andric if (RestoreGPRs.LowGPR) {
4010b57cec5SDimitry Andric // If we saved any of %r2-%r5 as varargs, we should also be saving
4020b57cec5SDimitry Andric // and restoring %r6. If we're saving %r6 or above, we should be
4030b57cec5SDimitry Andric // restoring it too.
404480093f4SDimitry Andric assert(RestoreGPRs.LowGPR != RestoreGPRs.HighGPR &&
405480093f4SDimitry Andric "Should be loading %r15 and something else");
4060b57cec5SDimitry Andric
4070b57cec5SDimitry Andric // Build an LMG instruction.
4080b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
4090b57cec5SDimitry Andric
4100b57cec5SDimitry Andric // Add the explicit register operands.
411480093f4SDimitry Andric MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
412480093f4SDimitry Andric MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric // Add the address.
4150b57cec5SDimitry Andric MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
416480093f4SDimitry Andric MIB.addImm(RestoreGPRs.GPROffset);
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andric // Do a second scan adding regs as being defined by instruction
4194824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
42004eeddc0SDimitry Andric Register Reg = I.getReg();
421480093f4SDimitry Andric if (Reg != RestoreGPRs.LowGPR && Reg != RestoreGPRs.HighGPR &&
4220b57cec5SDimitry Andric SystemZ::GR64BitRegClass.contains(Reg))
4230b57cec5SDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine);
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric return true;
4280b57cec5SDimitry Andric }
4290b57cec5SDimitry Andric
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const430349cc55cSDimitry Andric void SystemZELFFrameLowering::processFunctionBeforeFrameFinalized(
431349cc55cSDimitry Andric MachineFunction &MF, RegScavenger *RS) const {
4320b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
433e8d8bef9SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
434e8d8bef9SDimitry Andric MachineRegisterInfo *MRI = &MF.getRegInfo();
4355f757f3fSDimitry Andric bool BackChain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
436480093f4SDimitry Andric
4375ffd83dbSDimitry Andric if (!usePackedStack(MF) || BackChain)
4385ffd83dbSDimitry Andric // Create the incoming register save area.
439480093f4SDimitry Andric getOrCreateFramePointerSaveIndex(MF);
440480093f4SDimitry Andric
4410b57cec5SDimitry Andric // Get the size of our stack frame to be allocated ...
4420b57cec5SDimitry Andric uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
443fe6060f1SDimitry Andric SystemZMC::ELFCallFrameSize);
4440b57cec5SDimitry Andric // ... and the maximum offset we may need to reach into the
4450b57cec5SDimitry Andric // caller's frame to access the save area or stack arguments.
446480093f4SDimitry Andric int64_t MaxArgOffset = 0;
4470b57cec5SDimitry Andric for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
4480b57cec5SDimitry Andric if (MFFrame.getObjectOffset(I) >= 0) {
449480093f4SDimitry Andric int64_t ArgOffset = MFFrame.getObjectOffset(I) +
4500b57cec5SDimitry Andric MFFrame.getObjectSize(I);
4510b57cec5SDimitry Andric MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
4520b57cec5SDimitry Andric }
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andric uint64_t MaxReach = StackSize + MaxArgOffset;
4550b57cec5SDimitry Andric if (!isUInt<12>(MaxReach)) {
4560b57cec5SDimitry Andric // We may need register scavenging slots if some parts of the frame
4570b57cec5SDimitry Andric // are outside the reach of an unsigned 12-bit displacement.
4580b57cec5SDimitry Andric // Create 2 for the case where both addresses in an MVC are
4590b57cec5SDimitry Andric // out of range.
460*0fca6ea1SDimitry Andric RS->addScavengingFrameIndex(
461*0fca6ea1SDimitry Andric MFFrame.CreateStackObject(getPointerSize(), Align(8), false));
462*0fca6ea1SDimitry Andric RS->addScavengingFrameIndex(
463*0fca6ea1SDimitry Andric MFFrame.CreateStackObject(getPointerSize(), Align(8), false));
4640b57cec5SDimitry Andric }
465e8d8bef9SDimitry Andric
466e8d8bef9SDimitry Andric // If R6 is used as an argument register it is still callee saved. If it in
467e8d8bef9SDimitry Andric // this case is not clobbered (and restored) it should never be marked as
468e8d8bef9SDimitry Andric // killed.
469e8d8bef9SDimitry Andric if (MF.front().isLiveIn(SystemZ::R6D) &&
470e8d8bef9SDimitry Andric ZFI->getRestoreGPRRegs().LowGPR != SystemZ::R6D)
471e8d8bef9SDimitry Andric for (auto &MO : MRI->use_nodbg_operands(SystemZ::R6D))
472e8d8bef9SDimitry Andric MO.setIsKill(false);
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric
4750b57cec5SDimitry 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)4760b57cec5SDimitry Andric static void emitIncrement(MachineBasicBlock &MBB,
4775ffd83dbSDimitry Andric MachineBasicBlock::iterator &MBBI, const DebugLoc &DL,
4785ffd83dbSDimitry Andric Register Reg, int64_t NumBytes,
4790b57cec5SDimitry Andric const TargetInstrInfo *TII) {
4800b57cec5SDimitry Andric while (NumBytes) {
4810b57cec5SDimitry Andric unsigned Opcode;
4820b57cec5SDimitry Andric int64_t ThisVal = NumBytes;
4830b57cec5SDimitry Andric if (isInt<16>(NumBytes))
4840b57cec5SDimitry Andric Opcode = SystemZ::AGHI;
4850b57cec5SDimitry Andric else {
4860b57cec5SDimitry Andric Opcode = SystemZ::AGFI;
4870b57cec5SDimitry Andric // Make sure we maintain 8-byte stack alignment.
4880b57cec5SDimitry Andric int64_t MinVal = -uint64_t(1) << 31;
4890b57cec5SDimitry Andric int64_t MaxVal = (int64_t(1) << 31) - 8;
4900b57cec5SDimitry Andric if (ThisVal < MinVal)
4910b57cec5SDimitry Andric ThisVal = MinVal;
4920b57cec5SDimitry Andric else if (ThisVal > MaxVal)
4930b57cec5SDimitry Andric ThisVal = MaxVal;
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
4960b57cec5SDimitry Andric .addReg(Reg).addImm(ThisVal);
4970b57cec5SDimitry Andric // The CC implicit def is dead.
4980b57cec5SDimitry Andric MI->getOperand(3).setIsDead();
4990b57cec5SDimitry Andric NumBytes -= ThisVal;
5000b57cec5SDimitry Andric }
5010b57cec5SDimitry Andric }
5020b57cec5SDimitry Andric
5035ffd83dbSDimitry Andric // Add CFI for the new CFA offset.
buildCFAOffs(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,int Offset,const SystemZInstrInfo * ZII)5045ffd83dbSDimitry Andric static void buildCFAOffs(MachineBasicBlock &MBB,
5055ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI,
5065ffd83dbSDimitry Andric const DebugLoc &DL, int Offset,
5075ffd83dbSDimitry Andric const SystemZInstrInfo *ZII) {
5085ffd83dbSDimitry Andric unsigned CFIIndex = MBB.getParent()->addFrameInst(
5095ffd83dbSDimitry Andric MCCFIInstruction::cfiDefCfaOffset(nullptr, -Offset));
5105ffd83dbSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5115ffd83dbSDimitry Andric .addCFIIndex(CFIIndex);
5125ffd83dbSDimitry Andric }
5135ffd83dbSDimitry Andric
5145ffd83dbSDimitry Andric // Add CFI for the new frame location.
buildDefCFAReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,unsigned Reg,const SystemZInstrInfo * ZII)5155ffd83dbSDimitry Andric static void buildDefCFAReg(MachineBasicBlock &MBB,
5165ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI,
5175ffd83dbSDimitry Andric const DebugLoc &DL, unsigned Reg,
5185ffd83dbSDimitry Andric const SystemZInstrInfo *ZII) {
5195ffd83dbSDimitry Andric MachineFunction &MF = *MBB.getParent();
520*0fca6ea1SDimitry Andric const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
5215ffd83dbSDimitry Andric unsigned RegNum = MRI->getDwarfRegNum(Reg, true);
5225ffd83dbSDimitry Andric unsigned CFIIndex = MF.addFrameInst(
5235ffd83dbSDimitry Andric MCCFIInstruction::createDefCfaRegister(nullptr, RegNum));
5245ffd83dbSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5255ffd83dbSDimitry Andric .addCFIIndex(CFIIndex);
5265ffd83dbSDimitry Andric }
5275ffd83dbSDimitry Andric
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const528349cc55cSDimitry Andric void SystemZELFFrameLowering::emitPrologue(MachineFunction &MF,
5290b57cec5SDimitry Andric MachineBasicBlock &MBB) const {
5300b57cec5SDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
5315ffd83dbSDimitry Andric const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
5325ffd83dbSDimitry Andric const SystemZTargetLowering &TLI = *STI.getTargetLowering();
5330b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
5345ffd83dbSDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(STI.getInstrInfo());
5350b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
5360b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin();
537*0fca6ea1SDimitry Andric const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
5380b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
5390b57cec5SDimitry Andric bool HasFP = hasFP(MF);
5400b57cec5SDimitry Andric
541480093f4SDimitry Andric // In GHC calling convention C stack space, including the ABI-defined
542480093f4SDimitry Andric // 160-byte base area, is (de)allocated by GHC itself. This stack space may
543480093f4SDimitry Andric // be used by LLVM as spill slots for the tail recursive GHC functions. Thus
544480093f4SDimitry Andric // do not allocate stack space here, too.
545480093f4SDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GHC) {
546480093f4SDimitry Andric if (MFFrame.getStackSize() > 2048 * sizeof(long)) {
547480093f4SDimitry Andric report_fatal_error(
548480093f4SDimitry Andric "Pre allocated stack space for GHC function is too small");
549480093f4SDimitry Andric }
550480093f4SDimitry Andric if (HasFP) {
551480093f4SDimitry Andric report_fatal_error(
552480093f4SDimitry Andric "In GHC calling convention a frame pointer is not supported");
553480093f4SDimitry Andric }
554fe6060f1SDimitry Andric MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::ELFCallFrameSize);
555480093f4SDimitry Andric return;
556480093f4SDimitry Andric }
557480093f4SDimitry Andric
5580b57cec5SDimitry Andric // Debug location must be unknown since the first debug location is used
5590b57cec5SDimitry Andric // to determine the end of the prologue.
5600b57cec5SDimitry Andric DebugLoc DL;
5610b57cec5SDimitry Andric
5620b57cec5SDimitry Andric // The current offset of the stack pointer from the CFA.
563fe6060f1SDimitry Andric int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
5640b57cec5SDimitry Andric
565480093f4SDimitry Andric if (ZFI->getSpillGPRRegs().LowGPR) {
5660b57cec5SDimitry Andric // Skip over the GPR saves.
5670b57cec5SDimitry Andric if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
5680b57cec5SDimitry Andric ++MBBI;
5690b57cec5SDimitry Andric else
5700b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over GPR saves");
5710b57cec5SDimitry Andric
5720b57cec5SDimitry Andric // Add CFI for the GPR saves.
5730b57cec5SDimitry Andric for (auto &Save : CSI) {
57404eeddc0SDimitry Andric Register Reg = Save.getReg();
5750b57cec5SDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg)) {
576480093f4SDimitry Andric int FI = Save.getFrameIdx();
577480093f4SDimitry Andric int64_t Offset = MFFrame.getObjectOffset(FI);
5780b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
5790b57cec5SDimitry Andric nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
5800b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5810b57cec5SDimitry Andric .addCFIIndex(CFIIndex);
5820b57cec5SDimitry Andric }
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric }
5850b57cec5SDimitry Andric
5860b57cec5SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
5870b57cec5SDimitry Andric // We need to allocate the ABI-defined 160-byte base area whenever
5880b57cec5SDimitry Andric // we allocate stack space for our own use and whenever we call another
5890b57cec5SDimitry Andric // function.
590480093f4SDimitry Andric bool HasStackObject = false;
591480093f4SDimitry Andric for (unsigned i = 0, e = MFFrame.getObjectIndexEnd(); i != e; ++i)
592480093f4SDimitry Andric if (!MFFrame.isDeadObjectIndex(i)) {
593480093f4SDimitry Andric HasStackObject = true;
594480093f4SDimitry Andric break;
5950b57cec5SDimitry Andric }
596480093f4SDimitry Andric if (HasStackObject || MFFrame.hasCalls())
597fe6060f1SDimitry Andric StackSize += SystemZMC::ELFCallFrameSize;
598480093f4SDimitry Andric // Don't allocate the incoming reg save area.
599fe6060f1SDimitry Andric StackSize = StackSize > SystemZMC::ELFCallFrameSize
600fe6060f1SDimitry Andric ? StackSize - SystemZMC::ELFCallFrameSize
601480093f4SDimitry Andric : 0;
602480093f4SDimitry Andric MFFrame.setStackSize(StackSize);
6030b57cec5SDimitry Andric
6040b57cec5SDimitry Andric if (StackSize) {
6050b57cec5SDimitry Andric // Allocate StackSize bytes.
6060b57cec5SDimitry Andric int64_t Delta = -int64_t(StackSize);
6075ffd83dbSDimitry Andric const unsigned ProbeSize = TLI.getStackProbeSize(MF);
6085ffd83dbSDimitry Andric bool FreeProbe = (ZFI->getSpillGPRRegs().GPROffset &&
6095ffd83dbSDimitry Andric (ZFI->getSpillGPRRegs().GPROffset + StackSize) < ProbeSize);
6105ffd83dbSDimitry Andric if (!FreeProbe &&
6115ffd83dbSDimitry Andric MF.getSubtarget().getTargetLowering()->hasInlineStackProbe(MF)) {
6125ffd83dbSDimitry Andric // Stack probing may involve looping, but splitting the prologue block
6135ffd83dbSDimitry Andric // is not possible at this point since it would invalidate the
6145ffd83dbSDimitry Andric // SaveBlocks / RestoreBlocks sets of PEI in the single block function
6155ffd83dbSDimitry Andric // case. Build a pseudo to be handled later by inlineStackProbe().
6165ffd83dbSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::PROBED_STACKALLOC))
6175ffd83dbSDimitry Andric .addImm(StackSize);
6185ffd83dbSDimitry Andric }
6195ffd83dbSDimitry Andric else {
6205f757f3fSDimitry Andric bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
621e8d8bef9SDimitry Andric // If we need backchain, save current stack pointer. R1 is free at
622e8d8bef9SDimitry Andric // this point.
623e8d8bef9SDimitry Andric if (StoreBackchain)
624e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
625e8d8bef9SDimitry Andric .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
6260b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
6275ffd83dbSDimitry Andric buildCFAOffs(MBB, MBBI, DL, SPOffsetFromCFA + Delta, ZII);
628e8d8bef9SDimitry Andric if (StoreBackchain)
6290b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
6305ffd83dbSDimitry Andric .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
631e8d8bef9SDimitry Andric .addImm(getBackchainOffset(MF)).addReg(0);
6325ffd83dbSDimitry Andric }
633e8d8bef9SDimitry Andric SPOffsetFromCFA += Delta;
6340b57cec5SDimitry Andric }
6350b57cec5SDimitry Andric
6360b57cec5SDimitry Andric if (HasFP) {
6370b57cec5SDimitry Andric // Copy the base of the frame to R11.
6380b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
6390b57cec5SDimitry Andric .addReg(SystemZ::R15D);
6400b57cec5SDimitry Andric
6410b57cec5SDimitry Andric // Add CFI for the new frame location.
6425ffd83dbSDimitry Andric buildDefCFAReg(MBB, MBBI, DL, SystemZ::R11D, ZII);
6430b57cec5SDimitry Andric
6440b57cec5SDimitry Andric // Mark the FramePtr as live at the beginning of every block except
6450b57cec5SDimitry Andric // the entry block. (We'll have marked R11 as live on entry when
6460b57cec5SDimitry Andric // saving the GPRs.)
647349cc55cSDimitry Andric for (MachineBasicBlock &MBBJ : llvm::drop_begin(MF))
648349cc55cSDimitry Andric MBBJ.addLiveIn(SystemZ::R11D);
6490b57cec5SDimitry Andric }
6500b57cec5SDimitry Andric
6510b57cec5SDimitry Andric // Skip over the FPR/VR saves.
6520b57cec5SDimitry Andric SmallVector<unsigned, 8> CFIIndexes;
6530b57cec5SDimitry Andric for (auto &Save : CSI) {
65404eeddc0SDimitry Andric Register Reg = Save.getReg();
6550b57cec5SDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
6560b57cec5SDimitry Andric if (MBBI != MBB.end() &&
6570b57cec5SDimitry Andric (MBBI->getOpcode() == SystemZ::STD ||
6580b57cec5SDimitry Andric MBBI->getOpcode() == SystemZ::STDY))
6590b57cec5SDimitry Andric ++MBBI;
6600b57cec5SDimitry Andric else
6610b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over FPR save");
6620b57cec5SDimitry Andric } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
6630b57cec5SDimitry Andric if (MBBI != MBB.end() &&
6640b57cec5SDimitry Andric MBBI->getOpcode() == SystemZ::VST)
6650b57cec5SDimitry Andric ++MBBI;
6660b57cec5SDimitry Andric else
6670b57cec5SDimitry Andric llvm_unreachable("Couldn't skip over VR save");
6680b57cec5SDimitry Andric } else
6690b57cec5SDimitry Andric continue;
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andric // Add CFI for the this save.
6720b57cec5SDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
6735ffd83dbSDimitry Andric Register IgnoredFrameReg;
6740b57cec5SDimitry Andric int64_t Offset =
675e8d8bef9SDimitry Andric getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg)
676e8d8bef9SDimitry Andric .getFixed();
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
6790b57cec5SDimitry Andric nullptr, DwarfReg, SPOffsetFromCFA + Offset));
6800b57cec5SDimitry Andric CFIIndexes.push_back(CFIIndex);
6810b57cec5SDimitry Andric }
6820b57cec5SDimitry Andric // Complete the CFI for the FPR/VR saves, modelling them as taking effect
6830b57cec5SDimitry Andric // after the last save.
6840b57cec5SDimitry Andric for (auto CFIIndex : CFIIndexes) {
6850b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
6860b57cec5SDimitry Andric .addCFIIndex(CFIIndex);
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const690349cc55cSDimitry Andric void SystemZELFFrameLowering::emitEpilogue(MachineFunction &MF,
6910b57cec5SDimitry Andric MachineBasicBlock &MBB) const {
6920b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
6930b57cec5SDimitry Andric auto *ZII =
6940b57cec5SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
6950b57cec5SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
6960b57cec5SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
6970b57cec5SDimitry Andric
698349cc55cSDimitry Andric // See SystemZELFFrameLowering::emitPrologue
699480093f4SDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::GHC)
700480093f4SDimitry Andric return;
701480093f4SDimitry Andric
7020b57cec5SDimitry Andric // Skip the return instruction.
7030b57cec5SDimitry Andric assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
7040b57cec5SDimitry Andric
7050b57cec5SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
706480093f4SDimitry Andric if (ZFI->getRestoreGPRRegs().LowGPR) {
7070b57cec5SDimitry Andric --MBBI;
7080b57cec5SDimitry Andric unsigned Opcode = MBBI->getOpcode();
7090b57cec5SDimitry Andric if (Opcode != SystemZ::LMG)
7100b57cec5SDimitry Andric llvm_unreachable("Expected to see callee-save register restore code");
7110b57cec5SDimitry Andric
7120b57cec5SDimitry Andric unsigned AddrOpNo = 2;
7130b57cec5SDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
7140b57cec5SDimitry Andric uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
7150b57cec5SDimitry Andric unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
7160b57cec5SDimitry Andric
7170b57cec5SDimitry Andric // If the offset is too large, use the largest stack-aligned offset
7180b57cec5SDimitry Andric // and add the rest to the base register (the stack or frame pointer).
7190b57cec5SDimitry Andric if (!NewOpcode) {
7200b57cec5SDimitry Andric uint64_t NumBytes = Offset - 0x7fff8;
7210b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
7220b57cec5SDimitry Andric NumBytes, ZII);
7230b57cec5SDimitry Andric Offset -= NumBytes;
7240b57cec5SDimitry Andric NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
7250b57cec5SDimitry Andric assert(NewOpcode && "No restore instruction available");
7260b57cec5SDimitry Andric }
7270b57cec5SDimitry Andric
7280b57cec5SDimitry Andric MBBI->setDesc(ZII->get(NewOpcode));
7290b57cec5SDimitry Andric MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
7300b57cec5SDimitry Andric } else if (StackSize) {
7310b57cec5SDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
7320b57cec5SDimitry Andric emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
7330b57cec5SDimitry Andric }
7340b57cec5SDimitry Andric }
7350b57cec5SDimitry Andric
inlineStackProbe(MachineFunction & MF,MachineBasicBlock & PrologMBB) const736349cc55cSDimitry Andric void SystemZELFFrameLowering::inlineStackProbe(
737349cc55cSDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
7385ffd83dbSDimitry Andric auto *ZII =
7395ffd83dbSDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
7405ffd83dbSDimitry Andric const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
7415ffd83dbSDimitry Andric const SystemZTargetLowering &TLI = *STI.getTargetLowering();
7425ffd83dbSDimitry Andric
7435ffd83dbSDimitry Andric MachineInstr *StackAllocMI = nullptr;
7445ffd83dbSDimitry Andric for (MachineInstr &MI : PrologMBB)
7455ffd83dbSDimitry Andric if (MI.getOpcode() == SystemZ::PROBED_STACKALLOC) {
7465ffd83dbSDimitry Andric StackAllocMI = &MI;
7475ffd83dbSDimitry Andric break;
7485ffd83dbSDimitry Andric }
7495ffd83dbSDimitry Andric if (StackAllocMI == nullptr)
7505ffd83dbSDimitry Andric return;
7515ffd83dbSDimitry Andric uint64_t StackSize = StackAllocMI->getOperand(0).getImm();
7525ffd83dbSDimitry Andric const unsigned ProbeSize = TLI.getStackProbeSize(MF);
7535ffd83dbSDimitry Andric uint64_t NumFullBlocks = StackSize / ProbeSize;
7545ffd83dbSDimitry Andric uint64_t Residual = StackSize % ProbeSize;
755fe6060f1SDimitry Andric int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
7565ffd83dbSDimitry Andric MachineBasicBlock *MBB = &PrologMBB;
7575ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI = StackAllocMI;
7585ffd83dbSDimitry Andric const DebugLoc DL = StackAllocMI->getDebugLoc();
7595ffd83dbSDimitry Andric
7605ffd83dbSDimitry Andric // Allocate a block of Size bytes on the stack and probe it.
7615ffd83dbSDimitry Andric auto allocateAndProbe = [&](MachineBasicBlock &InsMBB,
7625ffd83dbSDimitry Andric MachineBasicBlock::iterator InsPt, unsigned Size,
7635ffd83dbSDimitry Andric bool EmitCFI) -> void {
7645ffd83dbSDimitry Andric emitIncrement(InsMBB, InsPt, DL, SystemZ::R15D, -int64_t(Size), ZII);
7655ffd83dbSDimitry Andric if (EmitCFI) {
7665ffd83dbSDimitry Andric SPOffsetFromCFA -= Size;
7675ffd83dbSDimitry Andric buildCFAOffs(InsMBB, InsPt, DL, SPOffsetFromCFA, ZII);
7685ffd83dbSDimitry Andric }
7695ffd83dbSDimitry Andric // Probe by means of a volatile compare.
7705ffd83dbSDimitry Andric MachineMemOperand *MMO = MF.getMachineMemOperand(MachinePointerInfo(),
7715ffd83dbSDimitry Andric MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad, 8, Align(1));
7725ffd83dbSDimitry Andric BuildMI(InsMBB, InsPt, DL, ZII->get(SystemZ::CG))
7735ffd83dbSDimitry Andric .addReg(SystemZ::R0D, RegState::Undef)
7745ffd83dbSDimitry Andric .addReg(SystemZ::R15D).addImm(Size - 8).addReg(0)
7755ffd83dbSDimitry Andric .addMemOperand(MMO);
7765ffd83dbSDimitry Andric };
7775ffd83dbSDimitry Andric
7785f757f3fSDimitry Andric bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
779e8d8bef9SDimitry Andric if (StoreBackchain)
780e8d8bef9SDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR))
781e8d8bef9SDimitry Andric .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
782e8d8bef9SDimitry Andric
783e8d8bef9SDimitry Andric MachineBasicBlock *DoneMBB = nullptr;
784e8d8bef9SDimitry Andric MachineBasicBlock *LoopMBB = nullptr;
7855ffd83dbSDimitry Andric if (NumFullBlocks < 3) {
7865ffd83dbSDimitry Andric // Emit unrolled probe statements.
7875ffd83dbSDimitry Andric for (unsigned int i = 0; i < NumFullBlocks; i++)
7885ffd83dbSDimitry Andric allocateAndProbe(*MBB, MBBI, ProbeSize, true/*EmitCFI*/);
7895ffd83dbSDimitry Andric } else {
7905ffd83dbSDimitry Andric // Emit a loop probing the pages.
7915ffd83dbSDimitry Andric uint64_t LoopAlloc = ProbeSize * NumFullBlocks;
7925ffd83dbSDimitry Andric SPOffsetFromCFA -= LoopAlloc;
7935ffd83dbSDimitry Andric
794e8d8bef9SDimitry Andric // Use R0D to hold the exit value.
795e8d8bef9SDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R0D)
7965ffd83dbSDimitry Andric .addReg(SystemZ::R15D);
797e8d8bef9SDimitry Andric buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R0D, ZII);
798e8d8bef9SDimitry Andric emitIncrement(*MBB, MBBI, DL, SystemZ::R0D, -int64_t(LoopAlloc), ZII);
799fe6060f1SDimitry Andric buildCFAOffs(*MBB, MBBI, DL, -int64_t(SystemZMC::ELFCallFrameSize + LoopAlloc),
8005ffd83dbSDimitry Andric ZII);
8015ffd83dbSDimitry Andric
802e8d8bef9SDimitry Andric DoneMBB = SystemZ::splitBlockBefore(MBBI, MBB);
803e8d8bef9SDimitry Andric LoopMBB = SystemZ::emitBlockAfter(MBB);
8045ffd83dbSDimitry Andric MBB->addSuccessor(LoopMBB);
8055ffd83dbSDimitry Andric LoopMBB->addSuccessor(LoopMBB);
8065ffd83dbSDimitry Andric LoopMBB->addSuccessor(DoneMBB);
8075ffd83dbSDimitry Andric
8085ffd83dbSDimitry Andric MBB = LoopMBB;
8095ffd83dbSDimitry Andric allocateAndProbe(*MBB, MBB->end(), ProbeSize, false/*EmitCFI*/);
8105ffd83dbSDimitry Andric BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::CLGR))
811e8d8bef9SDimitry Andric .addReg(SystemZ::R15D).addReg(SystemZ::R0D);
8125ffd83dbSDimitry Andric BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::BRC))
8135ffd83dbSDimitry Andric .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_GT).addMBB(MBB);
8145ffd83dbSDimitry Andric
8155ffd83dbSDimitry Andric MBB = DoneMBB;
8165ffd83dbSDimitry Andric MBBI = DoneMBB->begin();
8175ffd83dbSDimitry Andric buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R15D, ZII);
8185ffd83dbSDimitry Andric }
8195ffd83dbSDimitry Andric
8205ffd83dbSDimitry Andric if (Residual)
8215ffd83dbSDimitry Andric allocateAndProbe(*MBB, MBBI, Residual, true/*EmitCFI*/);
8225ffd83dbSDimitry Andric
823e8d8bef9SDimitry Andric if (StoreBackchain)
824e8d8bef9SDimitry Andric BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::STG))
825e8d8bef9SDimitry Andric .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
826e8d8bef9SDimitry Andric .addImm(getBackchainOffset(MF)).addReg(0);
827e8d8bef9SDimitry Andric
8285ffd83dbSDimitry Andric StackAllocMI->eraseFromParent();
829e8d8bef9SDimitry Andric if (DoneMBB != nullptr) {
830e8d8bef9SDimitry Andric // Compute the live-in lists for the new blocks.
831*0fca6ea1SDimitry Andric fullyRecomputeLiveIns({DoneMBB, LoopMBB});
832e8d8bef9SDimitry Andric }
8335ffd83dbSDimitry Andric }
8345ffd83dbSDimitry Andric
hasFP(const MachineFunction & MF) const835349cc55cSDimitry Andric bool SystemZELFFrameLowering::hasFP(const MachineFunction &MF) const {
8360b57cec5SDimitry Andric return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
83704eeddc0SDimitry Andric MF.getFrameInfo().hasVarSizedObjects());
8380b57cec5SDimitry Andric }
8390b57cec5SDimitry Andric
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const840349cc55cSDimitry Andric StackOffset SystemZELFFrameLowering::getFrameIndexReference(
841349cc55cSDimitry Andric const MachineFunction &MF, int FI, Register &FrameReg) const {
842fe6060f1SDimitry Andric // Our incoming SP is actually SystemZMC::ELFCallFrameSize below the CFA, so
843480093f4SDimitry Andric // add that difference here.
844e8d8bef9SDimitry Andric StackOffset Offset =
845480093f4SDimitry Andric TargetFrameLowering::getFrameIndexReference(MF, FI, FrameReg);
846fe6060f1SDimitry Andric return Offset + StackOffset::getFixed(SystemZMC::ELFCallFrameSize);
847480093f4SDimitry Andric }
848480093f4SDimitry Andric
getRegSpillOffset(MachineFunction & MF,Register Reg) const849349cc55cSDimitry Andric unsigned SystemZELFFrameLowering::getRegSpillOffset(MachineFunction &MF,
8505ffd83dbSDimitry Andric Register Reg) const {
8515ffd83dbSDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
8525f757f3fSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
8535f757f3fSDimitry Andric bool BackChain = Subtarget.hasBackChain();
8545f757f3fSDimitry Andric bool SoftFloat = Subtarget.hasSoftFloat();
8555ffd83dbSDimitry Andric unsigned Offset = RegSpillOffsets[Reg];
8565ffd83dbSDimitry Andric if (usePackedStack(MF) && !(IsVarArg && !SoftFloat)) {
8575ffd83dbSDimitry Andric if (SystemZ::GR64BitRegClass.contains(Reg))
8585ffd83dbSDimitry Andric // Put all GPRs at the top of the Register save area with packed
8595ffd83dbSDimitry Andric // stack. Make room for the backchain if needed.
8605ffd83dbSDimitry Andric Offset += BackChain ? 24 : 32;
8615ffd83dbSDimitry Andric else
8625ffd83dbSDimitry Andric Offset = 0;
8635ffd83dbSDimitry Andric }
8645ffd83dbSDimitry Andric return Offset;
8655ffd83dbSDimitry Andric }
8665ffd83dbSDimitry Andric
getOrCreateFramePointerSaveIndex(MachineFunction & MF) const867349cc55cSDimitry Andric int SystemZELFFrameLowering::getOrCreateFramePointerSaveIndex(
868349cc55cSDimitry Andric MachineFunction &MF) const {
869480093f4SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
870480093f4SDimitry Andric int FI = ZFI->getFramePointerSaveIndex();
871480093f4SDimitry Andric if (!FI) {
872480093f4SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
873fe6060f1SDimitry Andric int Offset = getBackchainOffset(MF) - SystemZMC::ELFCallFrameSize;
874*0fca6ea1SDimitry Andric FI = MFFrame.CreateFixedObject(getPointerSize(), Offset, false);
875480093f4SDimitry Andric ZFI->setFramePointerSaveIndex(FI);
876480093f4SDimitry Andric }
877480093f4SDimitry Andric return FI;
878480093f4SDimitry Andric }
8795ffd83dbSDimitry Andric
usePackedStack(MachineFunction & MF) const880349cc55cSDimitry Andric bool SystemZELFFrameLowering::usePackedStack(MachineFunction &MF) const {
8815ffd83dbSDimitry Andric bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack");
8825f757f3fSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
8835f757f3fSDimitry Andric bool BackChain = Subtarget.hasBackChain();
8845f757f3fSDimitry Andric bool SoftFloat = Subtarget.hasSoftFloat();
8855ffd83dbSDimitry Andric if (HasPackedStackAttr && BackChain && !SoftFloat)
8865ffd83dbSDimitry Andric report_fatal_error("packed-stack + backchain + hard-float is unsupported.");
8875ffd83dbSDimitry Andric bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC;
8885ffd83dbSDimitry Andric return HasPackedStackAttr && CallConv;
8895ffd83dbSDimitry Andric }
890349cc55cSDimitry Andric
SystemZXPLINKFrameLowering(unsigned PointerSize)891*0fca6ea1SDimitry Andric SystemZXPLINKFrameLowering::SystemZXPLINKFrameLowering(unsigned PointerSize)
8920eae32dcSDimitry Andric : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(32), 0,
893*0fca6ea1SDimitry Andric Align(32), /* StackRealignable */ false,
894*0fca6ea1SDimitry Andric PointerSize),
895349cc55cSDimitry Andric RegSpillOffsets(-1) {
896349cc55cSDimitry Andric
897349cc55cSDimitry Andric // Create a mapping from register number to save slot offset.
898349cc55cSDimitry Andric // These offsets are relative to the start of the local are area.
899349cc55cSDimitry Andric RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
900bdd1243dSDimitry Andric for (const auto &Entry : XPLINKSpillOffsetTable)
901bdd1243dSDimitry Andric RegSpillOffsets[Entry.Reg] = Entry.Offset;
902349cc55cSDimitry Andric }
903349cc55cSDimitry Andric
getOrCreateFramePointerSaveIndex(MachineFunction & MF) const904*0fca6ea1SDimitry Andric int SystemZXPLINKFrameLowering::getOrCreateFramePointerSaveIndex(
905*0fca6ea1SDimitry Andric MachineFunction &MF) const {
906*0fca6ea1SDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
907*0fca6ea1SDimitry Andric int FI = ZFI->getFramePointerSaveIndex();
908*0fca6ea1SDimitry Andric if (!FI) {
909*0fca6ea1SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
910*0fca6ea1SDimitry Andric FI = MFFrame.CreateFixedObject(getPointerSize(), 0, false);
911*0fca6ea1SDimitry Andric MFFrame.setStackID(FI, TargetStackID::NoAlloc);
912*0fca6ea1SDimitry Andric ZFI->setFramePointerSaveIndex(FI);
913*0fca6ea1SDimitry Andric }
914*0fca6ea1SDimitry Andric return FI;
915*0fca6ea1SDimitry Andric }
916*0fca6ea1SDimitry Andric
917fcaf7f86SDimitry Andric // Checks if the function is a potential candidate for being a XPLeaf routine.
isXPLeafCandidate(const MachineFunction & MF)918fcaf7f86SDimitry Andric static bool isXPLeafCandidate(const MachineFunction &MF) {
919fcaf7f86SDimitry Andric const MachineFrameInfo &MFFrame = MF.getFrameInfo();
920fcaf7f86SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo();
921fcaf7f86SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
922fcaf7f86SDimitry Andric auto *Regs =
923fcaf7f86SDimitry Andric static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
924fcaf7f86SDimitry Andric
925fcaf7f86SDimitry Andric // If function calls other functions including alloca, then it is not a XPLeaf
926fcaf7f86SDimitry Andric // routine.
927fcaf7f86SDimitry Andric if (MFFrame.hasCalls())
928fcaf7f86SDimitry Andric return false;
929fcaf7f86SDimitry Andric
930fcaf7f86SDimitry Andric // If the function has var Sized Objects, then it is not a XPLeaf routine.
931fcaf7f86SDimitry Andric if (MFFrame.hasVarSizedObjects())
932fcaf7f86SDimitry Andric return false;
933fcaf7f86SDimitry Andric
934fcaf7f86SDimitry Andric // If the function adjusts the stack, then it is not a XPLeaf routine.
935fcaf7f86SDimitry Andric if (MFFrame.adjustsStack())
936fcaf7f86SDimitry Andric return false;
937fcaf7f86SDimitry Andric
938fcaf7f86SDimitry Andric // If function modifies the stack pointer register, then it is not a XPLeaf
939fcaf7f86SDimitry Andric // routine.
940fcaf7f86SDimitry Andric if (MRI.isPhysRegModified(Regs->getStackPointerRegister()))
941fcaf7f86SDimitry Andric return false;
942fcaf7f86SDimitry Andric
943fcaf7f86SDimitry Andric // If function modifies the ADA register, then it is not a XPLeaf routine.
944fcaf7f86SDimitry Andric if (MRI.isPhysRegModified(Regs->getAddressOfCalleeRegister()))
945fcaf7f86SDimitry Andric return false;
946fcaf7f86SDimitry Andric
947fcaf7f86SDimitry Andric // If function modifies the return address register, then it is not a XPLeaf
948fcaf7f86SDimitry Andric // routine.
949fcaf7f86SDimitry Andric if (MRI.isPhysRegModified(Regs->getReturnFunctionAddressRegister()))
950fcaf7f86SDimitry Andric return false;
951fcaf7f86SDimitry Andric
952fcaf7f86SDimitry Andric // If the backchain pointer should be stored, then it is not a XPLeaf routine.
9535f757f3fSDimitry Andric if (MF.getSubtarget<SystemZSubtarget>().hasBackChain())
954fcaf7f86SDimitry Andric return false;
955fcaf7f86SDimitry Andric
956fcaf7f86SDimitry Andric // If function acquires its own stack frame, then it is not a XPLeaf routine.
957fcaf7f86SDimitry Andric // At the time this function is called, only slots for local variables are
958fcaf7f86SDimitry Andric // allocated, so this is a very rough estimate.
959fcaf7f86SDimitry Andric if (MFFrame.estimateStackSize(MF) > 0)
960fcaf7f86SDimitry Andric return false;
961fcaf7f86SDimitry Andric
962fcaf7f86SDimitry Andric return true;
963fcaf7f86SDimitry Andric }
964fcaf7f86SDimitry Andric
assignCalleeSavedSpillSlots(MachineFunction & MF,const TargetRegisterInfo * TRI,std::vector<CalleeSavedInfo> & CSI) const965349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::assignCalleeSavedSpillSlots(
966349cc55cSDimitry Andric MachineFunction &MF, const TargetRegisterInfo *TRI,
967349cc55cSDimitry Andric std::vector<CalleeSavedInfo> &CSI) const {
968349cc55cSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
969349cc55cSDimitry Andric SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
970349cc55cSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
971349cc55cSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
972753f127fSDimitry Andric auto &GRRegClass = SystemZ::GR64BitRegClass;
973753f127fSDimitry Andric
974fcaf7f86SDimitry Andric // At this point, the result of isXPLeafCandidate() is not accurate because
975fcaf7f86SDimitry Andric // the size of the save area has not yet been determined. If
976fcaf7f86SDimitry Andric // isXPLeafCandidate() indicates a potential leaf function, and there are no
977fcaf7f86SDimitry Andric // callee-save registers, then it is indeed a leaf function, and we can early
978fcaf7f86SDimitry Andric // exit.
979fcaf7f86SDimitry Andric // TODO: It is possible for leaf functions to use callee-saved registers.
980fcaf7f86SDimitry Andric // It can use the 0-2k range between R4 and the caller's stack frame without
981fcaf7f86SDimitry Andric // acquiring its own stack frame.
982fcaf7f86SDimitry Andric bool IsLeaf = CSI.empty() && isXPLeafCandidate(MF);
983fcaf7f86SDimitry Andric if (IsLeaf)
984fcaf7f86SDimitry Andric return true;
985fcaf7f86SDimitry Andric
986753f127fSDimitry Andric // For non-leaf functions:
987753f127fSDimitry Andric // - the address of callee (entry point) register R6 must be saved
988753f127fSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getAddressOfCalleeRegister()));
989753f127fSDimitry Andric CSI.back().setRestored(false);
990753f127fSDimitry Andric
991753f127fSDimitry Andric // The return address register R7 must be saved and restored.
992753f127fSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getReturnFunctionAddressRegister()));
993753f127fSDimitry Andric
994753f127fSDimitry Andric // If the function needs a frame pointer, or if the backchain pointer should
995753f127fSDimitry Andric // be stored, then save the stack pointer register R4.
9965f757f3fSDimitry Andric if (hasFP(MF) || Subtarget.hasBackChain())
997753f127fSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getStackPointerRegister()));
998349cc55cSDimitry Andric
999cb14a3feSDimitry Andric // If this function has an associated personality function then the
1000cb14a3feSDimitry Andric // environment register R5 must be saved in the DSA.
1001cb14a3feSDimitry Andric if (!MF.getLandingPads().empty())
1002cb14a3feSDimitry Andric CSI.push_back(CalleeSavedInfo(Regs.getADARegister()));
1003cb14a3feSDimitry Andric
1004349cc55cSDimitry Andric // Scan the call-saved GPRs and find the bounds of the register spill area.
1005753f127fSDimitry Andric Register LowRestoreGPR = 0;
1006753f127fSDimitry Andric int LowRestoreOffset = INT32_MAX;
1007753f127fSDimitry Andric Register LowSpillGPR = 0;
1008753f127fSDimitry Andric int LowSpillOffset = INT32_MAX;
1009753f127fSDimitry Andric Register HighGPR = 0;
1010349cc55cSDimitry Andric int HighOffset = -1;
1011349cc55cSDimitry Andric
1012*0fca6ea1SDimitry Andric // Query index of the saved frame pointer.
1013*0fca6ea1SDimitry Andric int FPSI = MFI->getFramePointerSaveIndex();
1014*0fca6ea1SDimitry Andric
1015753f127fSDimitry Andric for (auto &CS : CSI) {
101604eeddc0SDimitry Andric Register Reg = CS.getReg();
1017349cc55cSDimitry Andric int Offset = RegSpillOffsets[Reg];
1018349cc55cSDimitry Andric if (Offset >= 0) {
1019349cc55cSDimitry Andric if (GRRegClass.contains(Reg)) {
1020753f127fSDimitry Andric if (LowSpillOffset > Offset) {
1021753f127fSDimitry Andric LowSpillOffset = Offset;
1022753f127fSDimitry Andric LowSpillGPR = Reg;
1023753f127fSDimitry Andric }
1024753f127fSDimitry Andric if (CS.isRestored() && LowRestoreOffset > Offset) {
1025753f127fSDimitry Andric LowRestoreOffset = Offset;
1026753f127fSDimitry Andric LowRestoreGPR = Reg;
1027349cc55cSDimitry Andric }
1028349cc55cSDimitry Andric
1029349cc55cSDimitry Andric if (Offset > HighOffset) {
1030349cc55cSDimitry Andric HighOffset = Offset;
1031349cc55cSDimitry Andric HighGPR = Reg;
1032349cc55cSDimitry Andric }
1033753f127fSDimitry Andric // Non-volatile GPRs are saved in the dedicated register save area at
1034753f127fSDimitry Andric // the bottom of the stack and are not truly part of the "normal" stack
1035753f127fSDimitry Andric // frame. Mark the frame index as NoAlloc to indicate it as such.
1036*0fca6ea1SDimitry Andric unsigned RegSize = getPointerSize();
1037*0fca6ea1SDimitry Andric int FrameIdx =
1038*0fca6ea1SDimitry Andric (FPSI && Offset == 0)
1039*0fca6ea1SDimitry Andric ? FPSI
1040*0fca6ea1SDimitry Andric : MFFrame.CreateFixedSpillStackObject(RegSize, Offset);
1041349cc55cSDimitry Andric CS.setFrameIdx(FrameIdx);
1042753f127fSDimitry Andric MFFrame.setStackID(FrameIdx, TargetStackID::NoAlloc);
1043349cc55cSDimitry Andric }
1044753f127fSDimitry Andric } else {
104504eeddc0SDimitry Andric Register Reg = CS.getReg();
1046349cc55cSDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1047349cc55cSDimitry Andric Align Alignment = TRI->getSpillAlign(*RC);
1048349cc55cSDimitry Andric unsigned Size = TRI->getSpillSize(*RC);
1049349cc55cSDimitry Andric Alignment = std::min(Alignment, getStackAlign());
1050349cc55cSDimitry Andric int FrameIdx = MFFrame.CreateStackObject(Size, Alignment, true);
1051349cc55cSDimitry Andric CS.setFrameIdx(FrameIdx);
1052349cc55cSDimitry Andric }
1053753f127fSDimitry Andric }
1054753f127fSDimitry Andric
1055753f127fSDimitry Andric // Save the range of call-saved registers, for use by the
1056753f127fSDimitry Andric // prologue/epilogue inserters.
1057753f127fSDimitry Andric if (LowRestoreGPR)
1058753f127fSDimitry Andric MFI->setRestoreGPRRegs(LowRestoreGPR, HighGPR, LowRestoreOffset);
1059753f127fSDimitry Andric
1060753f127fSDimitry Andric // Save the range of call-saved registers, for use by the epilogue inserter.
1061753f127fSDimitry Andric assert(LowSpillGPR && "Expected registers to spill");
1062753f127fSDimitry Andric MFI->setSpillGPRRegs(LowSpillGPR, HighGPR, LowSpillOffset);
1063349cc55cSDimitry Andric
1064349cc55cSDimitry Andric return true;
1065349cc55cSDimitry Andric }
1066349cc55cSDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const1067349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::determineCalleeSaves(MachineFunction &MF,
1068349cc55cSDimitry Andric BitVector &SavedRegs,
1069349cc55cSDimitry Andric RegScavenger *RS) const {
1070349cc55cSDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1071349cc55cSDimitry Andric
1072349cc55cSDimitry Andric bool HasFP = hasFP(MF);
1073349cc55cSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1074349cc55cSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1075349cc55cSDimitry Andric
1076349cc55cSDimitry Andric // If the function requires a frame pointer, record that the hard
1077349cc55cSDimitry Andric // frame pointer will be clobbered.
1078349cc55cSDimitry Andric if (HasFP)
1079349cc55cSDimitry Andric SavedRegs.set(Regs.getFramePointerRegister());
1080349cc55cSDimitry Andric }
1081349cc55cSDimitry Andric
spillCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,ArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const1082349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::spillCalleeSavedRegisters(
1083349cc55cSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
1084349cc55cSDimitry Andric ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
1085349cc55cSDimitry Andric if (CSI.empty())
1086349cc55cSDimitry Andric return true;
1087349cc55cSDimitry Andric
1088349cc55cSDimitry Andric MachineFunction &MF = *MBB.getParent();
1089349cc55cSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
1090349cc55cSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1091349cc55cSDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1092349cc55cSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1093349cc55cSDimitry Andric SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
1094349cc55cSDimitry Andric DebugLoc DL;
1095349cc55cSDimitry Andric
1096349cc55cSDimitry Andric // Save GPRs
1097349cc55cSDimitry Andric if (SpillGPRs.LowGPR) {
1098349cc55cSDimitry Andric assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
1099349cc55cSDimitry Andric "Should be saving multiple registers");
1100349cc55cSDimitry Andric
1101349cc55cSDimitry Andric // Build an STM/STMG instruction.
1102349cc55cSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
1103349cc55cSDimitry Andric
1104349cc55cSDimitry Andric // Add the explicit register operands.
1105349cc55cSDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
1106349cc55cSDimitry Andric addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
1107349cc55cSDimitry Andric
1108349cc55cSDimitry Andric // Add the address r4
1109349cc55cSDimitry Andric MIB.addReg(Regs.getStackPointerRegister());
1110349cc55cSDimitry Andric
1111349cc55cSDimitry Andric // Add the partial offset
1112349cc55cSDimitry Andric // We cannot add the actual offset as, at the stack is not finalized
1113349cc55cSDimitry Andric MIB.addImm(SpillGPRs.GPROffset);
1114349cc55cSDimitry Andric
1115349cc55cSDimitry Andric // Make sure all call-saved GPRs are included as operands and are
1116349cc55cSDimitry Andric // marked as live on entry.
1117349cc55cSDimitry Andric auto &GRRegClass = SystemZ::GR64BitRegClass;
11184824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
111904eeddc0SDimitry Andric Register Reg = I.getReg();
1120349cc55cSDimitry Andric if (GRRegClass.contains(Reg))
1121349cc55cSDimitry Andric addSavedGPR(MBB, MIB, Reg, true);
1122349cc55cSDimitry Andric }
1123349cc55cSDimitry Andric }
1124349cc55cSDimitry Andric
1125349cc55cSDimitry Andric // Spill FPRs to the stack in the normal TargetInstrInfo way
11264824e7fdSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
112704eeddc0SDimitry Andric Register Reg = I.getReg();
1128349cc55cSDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg)) {
1129349cc55cSDimitry Andric MBB.addLiveIn(Reg);
11304824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1131bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
1132349cc55cSDimitry Andric }
1133349cc55cSDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg)) {
1134349cc55cSDimitry Andric MBB.addLiveIn(Reg);
11354824e7fdSDimitry Andric TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1136bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
1137349cc55cSDimitry Andric }
1138349cc55cSDimitry Andric }
1139349cc55cSDimitry Andric
1140349cc55cSDimitry Andric return true;
1141349cc55cSDimitry Andric }
1142349cc55cSDimitry Andric
restoreCalleeSavedRegisters(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MutableArrayRef<CalleeSavedInfo> CSI,const TargetRegisterInfo * TRI) const11430eae32dcSDimitry Andric bool SystemZXPLINKFrameLowering::restoreCalleeSavedRegisters(
11440eae32dcSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
11450eae32dcSDimitry Andric MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
11460eae32dcSDimitry Andric
11470eae32dcSDimitry Andric if (CSI.empty())
11480eae32dcSDimitry Andric return false;
11490eae32dcSDimitry Andric
11500eae32dcSDimitry Andric MachineFunction &MF = *MBB.getParent();
11510eae32dcSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
11520eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
11530eae32dcSDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11540eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
11550eae32dcSDimitry Andric
11560eae32dcSDimitry Andric DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
11570eae32dcSDimitry Andric
11580eae32dcSDimitry Andric // Restore FPRs in the normal TargetInstrInfo way.
1159bdd1243dSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
1160bdd1243dSDimitry Andric Register Reg = I.getReg();
11610eae32dcSDimitry Andric if (SystemZ::FP64BitRegClass.contains(Reg))
1162bdd1243dSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1163bdd1243dSDimitry Andric &SystemZ::FP64BitRegClass, TRI, Register());
11640eae32dcSDimitry Andric if (SystemZ::VR128BitRegClass.contains(Reg))
1165bdd1243dSDimitry Andric TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1166bdd1243dSDimitry Andric &SystemZ::VR128BitRegClass, TRI, Register());
11670eae32dcSDimitry Andric }
11680eae32dcSDimitry Andric
11690eae32dcSDimitry Andric // Restore call-saved GPRs (but not call-clobbered varargs, which at
11700eae32dcSDimitry Andric // this point might hold return values).
11710eae32dcSDimitry Andric SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
11720eae32dcSDimitry Andric if (RestoreGPRs.LowGPR) {
11730eae32dcSDimitry Andric assert(isInt<20>(Regs.getStackPointerBias() + RestoreGPRs.GPROffset));
11740eae32dcSDimitry Andric if (RestoreGPRs.LowGPR == RestoreGPRs.HighGPR)
11750eae32dcSDimitry Andric // Build an LG/L instruction.
11760eae32dcSDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LG), RestoreGPRs.LowGPR)
11770eae32dcSDimitry Andric .addReg(Regs.getStackPointerRegister())
11780eae32dcSDimitry Andric .addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset)
11790eae32dcSDimitry Andric .addReg(0);
11800eae32dcSDimitry Andric else {
11810eae32dcSDimitry Andric // Build an LMG/LM instruction.
11820eae32dcSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
11830eae32dcSDimitry Andric
11840eae32dcSDimitry Andric // Add the explicit register operands.
11850eae32dcSDimitry Andric MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
11860eae32dcSDimitry Andric MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
11870eae32dcSDimitry Andric
11880eae32dcSDimitry Andric // Add the address.
11890eae32dcSDimitry Andric MIB.addReg(Regs.getStackPointerRegister());
11900eae32dcSDimitry Andric MIB.addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset);
11910eae32dcSDimitry Andric
11920eae32dcSDimitry Andric // Do a second scan adding regs as being defined by instruction
1193bdd1243dSDimitry Andric for (const CalleeSavedInfo &I : CSI) {
1194bdd1243dSDimitry Andric Register Reg = I.getReg();
11950eae32dcSDimitry Andric if (Reg > RestoreGPRs.LowGPR && Reg < RestoreGPRs.HighGPR)
11960eae32dcSDimitry Andric MIB.addReg(Reg, RegState::ImplicitDefine);
11970eae32dcSDimitry Andric }
11980eae32dcSDimitry Andric }
11990eae32dcSDimitry Andric }
12000eae32dcSDimitry Andric
12010eae32dcSDimitry Andric return true;
12020eae32dcSDimitry Andric }
12030eae32dcSDimitry Andric
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const1204349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitPrologue(MachineFunction &MF,
12050eae32dcSDimitry Andric MachineBasicBlock &MBB) const {
12060eae32dcSDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
12070eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
12080eae32dcSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
12090eae32dcSDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin();
12100eae32dcSDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
12110eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
12120eae32dcSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
12130eae32dcSDimitry Andric MachineInstr *StoreInstr = nullptr;
1214fcaf7f86SDimitry Andric
1215fcaf7f86SDimitry Andric determineFrameLayout(MF);
1216fcaf7f86SDimitry Andric
12170eae32dcSDimitry Andric bool HasFP = hasFP(MF);
12180eae32dcSDimitry Andric // Debug location must be unknown since the first debug location is used
12190eae32dcSDimitry Andric // to determine the end of the prologue.
12200eae32dcSDimitry Andric DebugLoc DL;
12210eae32dcSDimitry Andric uint64_t Offset = 0;
12220eae32dcSDimitry Andric
1223fcaf7f86SDimitry Andric const uint64_t StackSize = MFFrame.getStackSize();
12240eae32dcSDimitry Andric
12250eae32dcSDimitry Andric if (ZFI->getSpillGPRRegs().LowGPR) {
12260eae32dcSDimitry Andric // Skip over the GPR saves.
12270eae32dcSDimitry Andric if ((MBBI != MBB.end()) && ((MBBI->getOpcode() == SystemZ::STMG))) {
12280eae32dcSDimitry Andric const int Operand = 3;
12290eae32dcSDimitry Andric // Now we can set the offset for the operation, since now the Stack
12300eae32dcSDimitry Andric // has been finalized.
12310eae32dcSDimitry Andric Offset = Regs.getStackPointerBias() + MBBI->getOperand(Operand).getImm();
12320eae32dcSDimitry Andric // Maximum displacement for STMG instruction.
12330eae32dcSDimitry Andric if (isInt<20>(Offset - StackSize))
12340eae32dcSDimitry Andric Offset -= StackSize;
12350eae32dcSDimitry Andric else
12360eae32dcSDimitry Andric StoreInstr = &*MBBI;
12370eae32dcSDimitry Andric MBBI->getOperand(Operand).setImm(Offset);
12380eae32dcSDimitry Andric ++MBBI;
12390eae32dcSDimitry Andric } else
12400eae32dcSDimitry Andric llvm_unreachable("Couldn't skip over GPR saves");
12410eae32dcSDimitry Andric }
12420eae32dcSDimitry Andric
12430eae32dcSDimitry Andric if (StackSize) {
12440eae32dcSDimitry Andric MachineBasicBlock::iterator InsertPt = StoreInstr ? StoreInstr : MBBI;
12450eae32dcSDimitry Andric // Allocate StackSize bytes.
12460eae32dcSDimitry Andric int64_t Delta = -int64_t(StackSize);
12470eae32dcSDimitry Andric
12480eae32dcSDimitry Andric // In case the STM(G) instruction also stores SP (R4), but the displacement
12490eae32dcSDimitry Andric // is too large, the SP register is manipulated first before storing,
12500eae32dcSDimitry Andric // resulting in the wrong value stored and retrieved later. In this case, we
12510eae32dcSDimitry Andric // need to temporarily save the value of SP, and store it later to memory.
12520eae32dcSDimitry Andric if (StoreInstr && HasFP) {
12530eae32dcSDimitry Andric // Insert LR r0,r4 before STMG instruction.
12540eae32dcSDimitry Andric BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::LGR))
12550eae32dcSDimitry Andric .addReg(SystemZ::R0D, RegState::Define)
12560eae32dcSDimitry Andric .addReg(SystemZ::R4D);
12570eae32dcSDimitry Andric // Insert ST r0,xxx(,r4) after STMG instruction.
12580eae32dcSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
12590eae32dcSDimitry Andric .addReg(SystemZ::R0D, RegState::Kill)
12600eae32dcSDimitry Andric .addReg(SystemZ::R4D)
12610eae32dcSDimitry Andric .addImm(Offset)
12620eae32dcSDimitry Andric .addReg(0);
12630eae32dcSDimitry Andric }
12640eae32dcSDimitry Andric
12650eae32dcSDimitry Andric emitIncrement(MBB, InsertPt, DL, Regs.getStackPointerRegister(), Delta,
12660eae32dcSDimitry Andric ZII);
126781ad6265SDimitry Andric
126881ad6265SDimitry Andric // If the requested stack size is larger than the guard page, then we need
126981ad6265SDimitry Andric // to check if we need to call the stack extender. This requires adding a
127081ad6265SDimitry Andric // conditional branch, but splitting the prologue block is not possible at
127181ad6265SDimitry Andric // this point since it would invalidate the SaveBlocks / RestoreBlocks sets
127281ad6265SDimitry Andric // of PEI in the single block function case. Build a pseudo to be handled
127381ad6265SDimitry Andric // later by inlineStackProbe().
127481ad6265SDimitry Andric const uint64_t GuardPageSize = 1024 * 1024;
127581ad6265SDimitry Andric if (StackSize > GuardPageSize) {
127681ad6265SDimitry Andric assert(StoreInstr && "Wrong insertion point");
127781ad6265SDimitry Andric BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::XPLINK_STACKALLOC));
127881ad6265SDimitry Andric }
12790eae32dcSDimitry Andric }
12800eae32dcSDimitry Andric
12810eae32dcSDimitry Andric if (HasFP) {
12820eae32dcSDimitry Andric // Copy the base of the frame to Frame Pointer Register.
12830eae32dcSDimitry Andric BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR),
12840eae32dcSDimitry Andric Regs.getFramePointerRegister())
12850eae32dcSDimitry Andric .addReg(Regs.getStackPointerRegister());
12860eae32dcSDimitry Andric
12870eae32dcSDimitry Andric // Mark the FramePtr as live at the beginning of every block except
12880eae32dcSDimitry Andric // the entry block. (We'll have marked R8 as live on entry when
12890eae32dcSDimitry Andric // saving the GPRs.)
1290fcaf7f86SDimitry Andric for (MachineBasicBlock &B : llvm::drop_begin(MF))
1291fcaf7f86SDimitry Andric B.addLiveIn(Regs.getFramePointerRegister());
12920eae32dcSDimitry Andric }
12935f757f3fSDimitry Andric
12945f757f3fSDimitry Andric // Save GPRs used for varargs, if any.
12955f757f3fSDimitry Andric const TargetInstrInfo *TII = Subtarget.getInstrInfo();
12965f757f3fSDimitry Andric bool IsVarArg = MF.getFunction().isVarArg();
12975f757f3fSDimitry Andric
12985f757f3fSDimitry Andric if (IsVarArg) {
12995f757f3fSDimitry Andric // FixedRegs is the number of used registers, accounting for shadow
13005f757f3fSDimitry Andric // registers.
13015f757f3fSDimitry Andric unsigned FixedRegs = ZFI->getVarArgsFirstGPR() + ZFI->getVarArgsFirstFPR();
13025f757f3fSDimitry Andric auto &GPRs = SystemZ::XPLINK64ArgGPRs;
13035f757f3fSDimitry Andric for (unsigned I = FixedRegs; I < SystemZ::XPLINK64NumArgGPRs; I++) {
13045f757f3fSDimitry Andric uint64_t StartOffset = MFFrame.getOffsetAdjustment() +
13055f757f3fSDimitry Andric MFFrame.getStackSize() + Regs.getCallFrameSize() +
1306*0fca6ea1SDimitry Andric getOffsetOfLocalArea() + I * getPointerSize();
13075f757f3fSDimitry Andric unsigned Reg = GPRs[I];
13085f757f3fSDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STG))
13095f757f3fSDimitry Andric .addReg(Reg)
13105f757f3fSDimitry Andric .addReg(Regs.getStackPointerRegister())
13115f757f3fSDimitry Andric .addImm(StartOffset)
13125f757f3fSDimitry Andric .addReg(0);
13135f757f3fSDimitry Andric if (!MBB.isLiveIn(Reg))
13145f757f3fSDimitry Andric MBB.addLiveIn(Reg);
13155f757f3fSDimitry Andric }
13165f757f3fSDimitry Andric }
13170eae32dcSDimitry Andric }
1318349cc55cSDimitry Andric
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const1319349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitEpilogue(MachineFunction &MF,
13200eae32dcSDimitry Andric MachineBasicBlock &MBB) const {
13210eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
13220eae32dcSDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
13230eae32dcSDimitry Andric SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
13240eae32dcSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
13250eae32dcSDimitry Andric auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
13260eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
13270eae32dcSDimitry Andric
13280eae32dcSDimitry Andric // Skip the return instruction.
13290eae32dcSDimitry Andric assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
13300eae32dcSDimitry Andric
13310eae32dcSDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
13320eae32dcSDimitry Andric if (StackSize) {
13330eae32dcSDimitry Andric unsigned SPReg = Regs.getStackPointerRegister();
13340eae32dcSDimitry Andric if (ZFI->getRestoreGPRRegs().LowGPR != SPReg) {
13350eae32dcSDimitry Andric DebugLoc DL = MBBI->getDebugLoc();
13360eae32dcSDimitry Andric emitIncrement(MBB, MBBI, DL, SPReg, StackSize, ZII);
13370eae32dcSDimitry Andric }
13380eae32dcSDimitry Andric }
13390eae32dcSDimitry Andric }
1340349cc55cSDimitry Andric
134181ad6265SDimitry Andric // Emit a compare of the stack pointer against the stack floor, and a call to
134281ad6265SDimitry Andric // the LE stack extender if needed.
inlineStackProbe(MachineFunction & MF,MachineBasicBlock & PrologMBB) const134381ad6265SDimitry Andric void SystemZXPLINKFrameLowering::inlineStackProbe(
134481ad6265SDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
134581ad6265SDimitry Andric auto *ZII =
134681ad6265SDimitry Andric static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
134781ad6265SDimitry Andric
134881ad6265SDimitry Andric MachineInstr *StackAllocMI = nullptr;
134981ad6265SDimitry Andric for (MachineInstr &MI : PrologMBB)
135081ad6265SDimitry Andric if (MI.getOpcode() == SystemZ::XPLINK_STACKALLOC) {
135181ad6265SDimitry Andric StackAllocMI = &MI;
135281ad6265SDimitry Andric break;
135381ad6265SDimitry Andric }
135481ad6265SDimitry Andric if (StackAllocMI == nullptr)
135581ad6265SDimitry Andric return;
135681ad6265SDimitry Andric
135706c3fb27SDimitry Andric bool NeedSaveSP = hasFP(MF);
135806c3fb27SDimitry Andric bool NeedSaveArg = PrologMBB.isLiveIn(SystemZ::R3D);
135906c3fb27SDimitry Andric const int64_t SaveSlotR3 = 2192;
136006c3fb27SDimitry Andric
136181ad6265SDimitry Andric MachineBasicBlock &MBB = PrologMBB;
136281ad6265SDimitry Andric const DebugLoc DL = StackAllocMI->getDebugLoc();
136381ad6265SDimitry Andric
136481ad6265SDimitry Andric // The 2nd half of block MBB after split.
136581ad6265SDimitry Andric MachineBasicBlock *NextMBB;
136681ad6265SDimitry Andric
136781ad6265SDimitry Andric // Add new basic block for the call to the stack overflow function.
136881ad6265SDimitry Andric MachineBasicBlock *StackExtMBB =
136981ad6265SDimitry Andric MF.CreateMachineBasicBlock(MBB.getBasicBlock());
137081ad6265SDimitry Andric MF.push_back(StackExtMBB);
137181ad6265SDimitry Andric
137281ad6265SDimitry Andric // LG r3,72(,r3)
137381ad6265SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::LG), SystemZ::R3D)
137481ad6265SDimitry Andric .addReg(SystemZ::R3D)
137581ad6265SDimitry Andric .addImm(72)
137681ad6265SDimitry Andric .addReg(0);
137781ad6265SDimitry Andric // BASR r3,r3
137881ad6265SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::CallBASR_STACKEXT))
137981ad6265SDimitry Andric .addReg(SystemZ::R3D);
138006c3fb27SDimitry Andric if (NeedSaveArg) {
138106c3fb27SDimitry Andric if (!NeedSaveSP) {
138206c3fb27SDimitry Andric // LGR r0,r3
138306c3fb27SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
138406c3fb27SDimitry Andric .addReg(SystemZ::R0D, RegState::Define)
138506c3fb27SDimitry Andric .addReg(SystemZ::R3D);
138606c3fb27SDimitry Andric } else {
138706c3fb27SDimitry Andric // In this case, the incoming value of r4 is saved in r0 so the
138806c3fb27SDimitry Andric // latter register is unavailable. Store r3 in its corresponding
138906c3fb27SDimitry Andric // slot in the parameter list instead. Do this at the start of
139006c3fb27SDimitry Andric // the prolog before r4 is manipulated by anything else.
139106c3fb27SDimitry Andric // STG r3, 2192(r4)
139206c3fb27SDimitry Andric BuildMI(MBB, MBB.begin(), DL, ZII->get(SystemZ::STG))
139306c3fb27SDimitry Andric .addReg(SystemZ::R3D)
139406c3fb27SDimitry Andric .addReg(SystemZ::R4D)
139506c3fb27SDimitry Andric .addImm(SaveSlotR3)
139606c3fb27SDimitry Andric .addReg(0);
139706c3fb27SDimitry Andric }
139806c3fb27SDimitry Andric }
139981ad6265SDimitry Andric // LLGT r3,1208
140081ad6265SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LLGT), SystemZ::R3D)
140181ad6265SDimitry Andric .addReg(0)
140281ad6265SDimitry Andric .addImm(1208)
140381ad6265SDimitry Andric .addReg(0);
140481ad6265SDimitry Andric // CG r4,64(,r3)
140581ad6265SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::CG))
140681ad6265SDimitry Andric .addReg(SystemZ::R4D)
140781ad6265SDimitry Andric .addReg(SystemZ::R3D)
140881ad6265SDimitry Andric .addImm(64)
140981ad6265SDimitry Andric .addReg(0);
141081ad6265SDimitry Andric // JLL b'0100',F'37'
141181ad6265SDimitry Andric BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::BRC))
141281ad6265SDimitry Andric .addImm(SystemZ::CCMASK_ICMP)
141381ad6265SDimitry Andric .addImm(SystemZ::CCMASK_CMP_LT)
141481ad6265SDimitry Andric .addMBB(StackExtMBB);
141581ad6265SDimitry Andric
141681ad6265SDimitry Andric NextMBB = SystemZ::splitBlockBefore(StackAllocMI, &MBB);
141781ad6265SDimitry Andric MBB.addSuccessor(NextMBB);
141881ad6265SDimitry Andric MBB.addSuccessor(StackExtMBB);
141906c3fb27SDimitry Andric if (NeedSaveArg) {
142006c3fb27SDimitry Andric if (!NeedSaveSP) {
142106c3fb27SDimitry Andric // LGR r3, r0
142206c3fb27SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
142306c3fb27SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
142406c3fb27SDimitry Andric .addReg(SystemZ::R0D, RegState::Kill);
142506c3fb27SDimitry Andric } else {
142606c3fb27SDimitry Andric // In this case, the incoming value of r4 is saved in r0 so the
142706c3fb27SDimitry Andric // latter register is unavailable. We stored r3 in its corresponding
142806c3fb27SDimitry Andric // slot in the parameter list instead and we now restore it from there.
142906c3fb27SDimitry Andric // LGR r3, r0
143006c3fb27SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LGR))
143106c3fb27SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
143206c3fb27SDimitry Andric .addReg(SystemZ::R0D);
143306c3fb27SDimitry Andric // LG r3, 2192(r3)
143406c3fb27SDimitry Andric BuildMI(*NextMBB, StackAllocMI, DL, ZII->get(SystemZ::LG))
143506c3fb27SDimitry Andric .addReg(SystemZ::R3D, RegState::Define)
143606c3fb27SDimitry Andric .addReg(SystemZ::R3D)
143706c3fb27SDimitry Andric .addImm(SaveSlotR3)
143806c3fb27SDimitry Andric .addReg(0);
143906c3fb27SDimitry Andric }
144006c3fb27SDimitry Andric }
144181ad6265SDimitry Andric
144281ad6265SDimitry Andric // Add jump back from stack extension BB.
144381ad6265SDimitry Andric BuildMI(StackExtMBB, DL, ZII->get(SystemZ::J)).addMBB(NextMBB);
144481ad6265SDimitry Andric StackExtMBB->addSuccessor(NextMBB);
144581ad6265SDimitry Andric
144681ad6265SDimitry Andric StackAllocMI->eraseFromParent();
144781ad6265SDimitry Andric
144881ad6265SDimitry Andric // Compute the live-in lists for the new blocks.
1449*0fca6ea1SDimitry Andric fullyRecomputeLiveIns({StackExtMBB, NextMBB});
145081ad6265SDimitry Andric }
145181ad6265SDimitry Andric
hasFP(const MachineFunction & MF) const1452349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {
14530eae32dcSDimitry Andric return (MF.getFrameInfo().hasVarSizedObjects());
14540eae32dcSDimitry Andric }
14550eae32dcSDimitry Andric
processFunctionBeforeFrameFinalized(MachineFunction & MF,RegScavenger * RS) const14560eae32dcSDimitry Andric void SystemZXPLINKFrameLowering::processFunctionBeforeFrameFinalized(
14570eae32dcSDimitry Andric MachineFunction &MF, RegScavenger *RS) const {
14580eae32dcSDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
14590eae32dcSDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
14600eae32dcSDimitry Andric auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
14610eae32dcSDimitry Andric
14620eae32dcSDimitry Andric // Setup stack frame offset
14630eae32dcSDimitry Andric MFFrame.setOffsetAdjustment(Regs.getStackPointerBias());
14645f757f3fSDimitry Andric
14655f757f3fSDimitry Andric // Nothing to do for leaf functions.
14665f757f3fSDimitry Andric uint64_t StackSize = MFFrame.estimateStackSize(MF);
14675f757f3fSDimitry Andric if (StackSize == 0 && MFFrame.getCalleeSavedInfo().empty())
14685f757f3fSDimitry Andric return;
14695f757f3fSDimitry Andric
14705f757f3fSDimitry Andric // Although the XPLINK specifications for AMODE64 state that minimum size
14715f757f3fSDimitry Andric // of the param area is minimum 32 bytes and no rounding is otherwise
14725f757f3fSDimitry Andric // specified, we round this area in 64 bytes increments to be compatible
14735f757f3fSDimitry Andric // with existing compilers.
14745f757f3fSDimitry Andric MFFrame.setMaxCallFrameSize(
14755f757f3fSDimitry Andric std::max(64U, (unsigned)alignTo(MFFrame.getMaxCallFrameSize(), 64)));
1476*0fca6ea1SDimitry Andric
1477*0fca6ea1SDimitry Andric // Add frame values with positive object offsets. Since the displacement from
1478*0fca6ea1SDimitry Andric // the SP/FP is calculated by ObjectOffset + StackSize + Bias, object offsets
1479*0fca6ea1SDimitry Andric // with positive values are in the caller's stack frame. We need to include
1480*0fca6ea1SDimitry Andric // that since it is accessed by displacement to SP/FP.
1481*0fca6ea1SDimitry Andric int64_t LargestArgOffset = 0;
1482*0fca6ea1SDimitry Andric for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I) {
1483*0fca6ea1SDimitry Andric if (MFFrame.getObjectOffset(I) >= 0) {
1484*0fca6ea1SDimitry Andric int64_t ObjOffset = MFFrame.getObjectOffset(I) + MFFrame.getObjectSize(I);
1485*0fca6ea1SDimitry Andric LargestArgOffset = std::max(ObjOffset, LargestArgOffset);
1486*0fca6ea1SDimitry Andric }
1487*0fca6ea1SDimitry Andric }
1488*0fca6ea1SDimitry Andric
1489*0fca6ea1SDimitry Andric uint64_t MaxReach = (StackSize + Regs.getCallFrameSize() +
1490*0fca6ea1SDimitry Andric Regs.getStackPointerBias() + LargestArgOffset);
1491*0fca6ea1SDimitry Andric
1492*0fca6ea1SDimitry Andric if (!isUInt<12>(MaxReach)) {
1493*0fca6ea1SDimitry Andric // We may need register scavenging slots if some parts of the frame
1494*0fca6ea1SDimitry Andric // are outside the reach of an unsigned 12-bit displacement.
1495*0fca6ea1SDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
1496*0fca6ea1SDimitry Andric RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
1497*0fca6ea1SDimitry Andric }
1498349cc55cSDimitry Andric }
1499fcaf7f86SDimitry Andric
1500fcaf7f86SDimitry Andric // Determines the size of the frame, and creates the deferred spill objects.
determineFrameLayout(MachineFunction & MF) const1501fcaf7f86SDimitry Andric void SystemZXPLINKFrameLowering::determineFrameLayout(
1502fcaf7f86SDimitry Andric MachineFunction &MF) const {
1503fcaf7f86SDimitry Andric MachineFrameInfo &MFFrame = MF.getFrameInfo();
1504fcaf7f86SDimitry Andric const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1505fcaf7f86SDimitry Andric auto *Regs =
1506fcaf7f86SDimitry Andric static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
1507fcaf7f86SDimitry Andric
1508fcaf7f86SDimitry Andric uint64_t StackSize = MFFrame.getStackSize();
1509fcaf7f86SDimitry Andric if (StackSize == 0)
1510fcaf7f86SDimitry Andric return;
1511fcaf7f86SDimitry Andric
1512fcaf7f86SDimitry Andric // Add the size of the register save area and the reserved area to the size.
1513fcaf7f86SDimitry Andric StackSize += Regs->getCallFrameSize();
1514fcaf7f86SDimitry Andric MFFrame.setStackSize(StackSize);
1515fcaf7f86SDimitry Andric
1516*0fca6ea1SDimitry Andric // We now know the stack size. Update the stack objects for the register save
1517*0fca6ea1SDimitry Andric // area now. This has no impact on the stack frame layout, as this is already
1518*0fca6ea1SDimitry Andric // computed. However, it makes sure that all callee saved registers have a
1519*0fca6ea1SDimitry Andric // valid offset assigned.
1520*0fca6ea1SDimitry Andric for (int FrameIdx = MFFrame.getObjectIndexBegin(); FrameIdx != 0;
1521*0fca6ea1SDimitry Andric ++FrameIdx) {
1522*0fca6ea1SDimitry Andric if (MFFrame.getStackID(FrameIdx) == TargetStackID::NoAlloc) {
1523*0fca6ea1SDimitry Andric int64_t SPOffset = MFFrame.getObjectOffset(FrameIdx);
1524*0fca6ea1SDimitry Andric SPOffset -= StackSize;
1525*0fca6ea1SDimitry Andric MFFrame.setObjectOffset(FrameIdx, SPOffset);
1526*0fca6ea1SDimitry Andric }
1527fcaf7f86SDimitry Andric }
1528fcaf7f86SDimitry Andric }
1529