xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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"
16*81ad6265SDimitry 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"
200b57cec5SDimitry Andric #include "llvm/IR/Function.h"
215ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace {
26480093f4SDimitry Andric // The ABI-defined register save slots, relative to the CFA (i.e.
27fe6060f1SDimitry Andric // incoming stack pointer + SystemZMC::ELFCallFrameSize).
28349cc55cSDimitry Andric static const TargetFrameLowering::SpillSlot ELFSpillOffsetTable[] = {
290b57cec5SDimitry Andric   { SystemZ::R2D,  0x10 },
300b57cec5SDimitry Andric   { SystemZ::R3D,  0x18 },
310b57cec5SDimitry Andric   { SystemZ::R4D,  0x20 },
320b57cec5SDimitry Andric   { SystemZ::R5D,  0x28 },
330b57cec5SDimitry Andric   { SystemZ::R6D,  0x30 },
340b57cec5SDimitry Andric   { SystemZ::R7D,  0x38 },
350b57cec5SDimitry Andric   { SystemZ::R8D,  0x40 },
360b57cec5SDimitry Andric   { SystemZ::R9D,  0x48 },
370b57cec5SDimitry Andric   { SystemZ::R10D, 0x50 },
380b57cec5SDimitry Andric   { SystemZ::R11D, 0x58 },
390b57cec5SDimitry Andric   { SystemZ::R12D, 0x60 },
400b57cec5SDimitry Andric   { SystemZ::R13D, 0x68 },
410b57cec5SDimitry Andric   { SystemZ::R14D, 0x70 },
420b57cec5SDimitry Andric   { SystemZ::R15D, 0x78 },
430b57cec5SDimitry Andric   { SystemZ::F0D,  0x80 },
440b57cec5SDimitry Andric   { SystemZ::F2D,  0x88 },
450b57cec5SDimitry Andric   { SystemZ::F4D,  0x90 },
460b57cec5SDimitry Andric   { SystemZ::F6D,  0x98 }
470b57cec5SDimitry Andric };
48349cc55cSDimitry Andric 
49349cc55cSDimitry Andric static const TargetFrameLowering::SpillSlot XPLINKSpillOffsetTable[] = {
50349cc55cSDimitry Andric     {SystemZ::R4D, 0x00},  {SystemZ::R5D, 0x08},  {SystemZ::R6D, 0x10},
51349cc55cSDimitry Andric     {SystemZ::R7D, 0x18},  {SystemZ::R8D, 0x20},  {SystemZ::R9D, 0x28},
52349cc55cSDimitry Andric     {SystemZ::R10D, 0x30}, {SystemZ::R11D, 0x38}, {SystemZ::R12D, 0x40},
53349cc55cSDimitry Andric     {SystemZ::R13D, 0x48}, {SystemZ::R14D, 0x50}, {SystemZ::R15D, 0x58}};
540b57cec5SDimitry Andric } // end anonymous namespace
550b57cec5SDimitry Andric 
56349cc55cSDimitry Andric SystemZFrameLowering::SystemZFrameLowering(StackDirection D, Align StackAl,
57349cc55cSDimitry Andric                                            int LAO, Align TransAl,
58349cc55cSDimitry Andric                                            bool StackReal)
59349cc55cSDimitry Andric     : TargetFrameLowering(D, StackAl, LAO, TransAl, StackReal) {}
60480093f4SDimitry Andric 
61349cc55cSDimitry Andric std::unique_ptr<SystemZFrameLowering>
62349cc55cSDimitry Andric SystemZFrameLowering::create(const SystemZSubtarget &STI) {
63349cc55cSDimitry Andric   if (STI.isTargetXPLINK64())
64349cc55cSDimitry Andric     return std::make_unique<SystemZXPLINKFrameLowering>();
65349cc55cSDimitry Andric   return std::make_unique<SystemZELFFrameLowering>();
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric 
68349cc55cSDimitry Andric MachineBasicBlock::iterator SystemZFrameLowering::eliminateCallFramePseudoInstr(
69349cc55cSDimitry Andric     MachineFunction &MF, MachineBasicBlock &MBB,
70349cc55cSDimitry Andric     MachineBasicBlock::iterator MI) const {
71349cc55cSDimitry Andric   switch (MI->getOpcode()) {
72349cc55cSDimitry Andric   case SystemZ::ADJCALLSTACKDOWN:
73349cc55cSDimitry Andric   case SystemZ::ADJCALLSTACKUP:
74349cc55cSDimitry Andric     assert(hasReservedCallFrame(MF) &&
75349cc55cSDimitry Andric            "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
76349cc55cSDimitry Andric     return MBB.erase(MI);
77349cc55cSDimitry Andric     break;
78349cc55cSDimitry Andric 
79349cc55cSDimitry Andric   default:
80349cc55cSDimitry Andric     llvm_unreachable("Unexpected call frame instruction");
81349cc55cSDimitry Andric   }
82349cc55cSDimitry Andric }
83349cc55cSDimitry Andric 
841fd87a68SDimitry Andric namespace {
851fd87a68SDimitry Andric struct SZFrameSortingObj {
861fd87a68SDimitry Andric   bool IsValid = false;     // True if we care about this Object.
871fd87a68SDimitry Andric   uint32_t ObjectIndex = 0; // Index of Object into MFI list.
881fd87a68SDimitry Andric   uint64_t ObjectSize = 0;  // Size of Object in bytes.
891fd87a68SDimitry Andric   uint32_t D12Count = 0;    // 12-bit displacement only.
901fd87a68SDimitry Andric   uint32_t DPairCount = 0;  // 12 or 20 bit displacement.
911fd87a68SDimitry Andric };
921fd87a68SDimitry Andric typedef std::vector<SZFrameSortingObj> SZFrameObjVec;
931fd87a68SDimitry Andric } // namespace
941fd87a68SDimitry Andric 
951fd87a68SDimitry Andric // TODO: Move to base class.
961fd87a68SDimitry Andric void SystemZELFFrameLowering::orderFrameObjects(
971fd87a68SDimitry Andric     const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const {
981fd87a68SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
99*81ad6265SDimitry Andric   auto *TII = MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
1001fd87a68SDimitry Andric 
1011fd87a68SDimitry Andric   // Make a vector of sorting objects to track all MFI objects and mark those
1021fd87a68SDimitry Andric   // to be sorted as valid.
1031fd87a68SDimitry Andric   if (ObjectsToAllocate.size() <= 1)
1041fd87a68SDimitry Andric     return;
1051fd87a68SDimitry Andric   SZFrameObjVec SortingObjects(MFI.getObjectIndexEnd());
1061fd87a68SDimitry Andric   for (auto &Obj : ObjectsToAllocate) {
1071fd87a68SDimitry Andric     SortingObjects[Obj].IsValid = true;
1081fd87a68SDimitry Andric     SortingObjects[Obj].ObjectIndex = Obj;
1091fd87a68SDimitry Andric     SortingObjects[Obj].ObjectSize = MFI.getObjectSize(Obj);
1101fd87a68SDimitry Andric   }
1111fd87a68SDimitry Andric 
1121fd87a68SDimitry Andric   // Examine uses for each object and record short (12-bit) and "pair"
1131fd87a68SDimitry Andric   // displacement types.
1141fd87a68SDimitry Andric   for (auto &MBB : MF)
1151fd87a68SDimitry Andric     for (auto &MI : MBB) {
1161fd87a68SDimitry Andric       if (MI.isDebugInstr())
1171fd87a68SDimitry Andric         continue;
1181fd87a68SDimitry Andric       for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
1191fd87a68SDimitry Andric         const MachineOperand &MO = MI.getOperand(I);
1201fd87a68SDimitry Andric         if (!MO.isFI())
1211fd87a68SDimitry Andric           continue;
1221fd87a68SDimitry Andric         int Index = MO.getIndex();
1231fd87a68SDimitry Andric         if (Index >= 0 && Index < MFI.getObjectIndexEnd() &&
1241fd87a68SDimitry Andric             SortingObjects[Index].IsValid) {
1251fd87a68SDimitry Andric           if (TII->hasDisplacementPairInsn(MI.getOpcode()))
1261fd87a68SDimitry Andric             SortingObjects[Index].DPairCount++;
1271fd87a68SDimitry Andric           else if (!(MI.getDesc().TSFlags & SystemZII::Has20BitOffset))
1281fd87a68SDimitry Andric             SortingObjects[Index].D12Count++;
1291fd87a68SDimitry Andric         }
1301fd87a68SDimitry Andric       }
1311fd87a68SDimitry Andric     }
1321fd87a68SDimitry Andric 
1331fd87a68SDimitry Andric   // Sort all objects for short/paired displacements, which should be
1341fd87a68SDimitry Andric   // sufficient as it seems like all frame objects typically are within the
1351fd87a68SDimitry Andric   // long displacement range.  Sorting works by computing the "density" as
1361fd87a68SDimitry Andric   // Count / ObjectSize. The comparisons of two such fractions are refactored
1371fd87a68SDimitry Andric   // by multiplying both sides with A.ObjectSize * B.ObjectSize, in order to
1381fd87a68SDimitry Andric   // eliminate the (fp) divisions.  A higher density object needs to go after
1391fd87a68SDimitry Andric   // in the list in order for it to end up lower on the stack.
1401fd87a68SDimitry Andric   auto CmpD12 = [](const SZFrameSortingObj &A, const SZFrameSortingObj &B) {
1411fd87a68SDimitry Andric     // Put all invalid and variable sized objects at the end.
1421fd87a68SDimitry Andric     if (!A.IsValid || !B.IsValid)
1431fd87a68SDimitry Andric       return A.IsValid;
1441fd87a68SDimitry Andric     if (!A.ObjectSize || !B.ObjectSize)
1451fd87a68SDimitry Andric       return A.ObjectSize > 0;
1461fd87a68SDimitry Andric     uint64_t ADensityCmp = A.D12Count * B.ObjectSize;
1471fd87a68SDimitry Andric     uint64_t BDensityCmp = B.D12Count * A.ObjectSize;
1481fd87a68SDimitry Andric     if (ADensityCmp != BDensityCmp)
1491fd87a68SDimitry Andric       return ADensityCmp < BDensityCmp;
1501fd87a68SDimitry Andric     return A.DPairCount * B.ObjectSize < B.DPairCount * A.ObjectSize;
1511fd87a68SDimitry Andric   };
1521fd87a68SDimitry Andric   std::stable_sort(SortingObjects.begin(), SortingObjects.end(), CmpD12);
1531fd87a68SDimitry Andric 
1541fd87a68SDimitry Andric   // Now modify the original list to represent the final order that
1551fd87a68SDimitry Andric   // we want.
1561fd87a68SDimitry Andric   unsigned Idx = 0;
1571fd87a68SDimitry Andric   for (auto &Obj : SortingObjects) {
1581fd87a68SDimitry Andric     // All invalid items are sorted at the end, so it's safe to stop.
1591fd87a68SDimitry Andric     if (!Obj.IsValid)
1601fd87a68SDimitry Andric       break;
1611fd87a68SDimitry Andric     ObjectsToAllocate[Idx++] = Obj.ObjectIndex;
1621fd87a68SDimitry Andric   }
1631fd87a68SDimitry Andric }
1641fd87a68SDimitry Andric 
165349cc55cSDimitry Andric bool SystemZFrameLowering::hasReservedCallFrame(
166349cc55cSDimitry Andric     const MachineFunction &MF) const {
167349cc55cSDimitry Andric   // The ELF ABI requires us to allocate 160 bytes of stack space for the
168349cc55cSDimitry Andric   // callee, with any outgoing stack arguments being placed above that. It
169349cc55cSDimitry Andric   // seems better to make that area a permanent feature of the frame even if
170349cc55cSDimitry Andric   // we're using a frame pointer. Similarly, 64-bit XPLINK requires 96 bytes
171349cc55cSDimitry Andric   // of stack space for the register save area.
172349cc55cSDimitry Andric   return true;
173349cc55cSDimitry Andric }
174349cc55cSDimitry Andric 
175349cc55cSDimitry Andric bool SystemZELFFrameLowering::assignCalleeSavedSpillSlots(
176349cc55cSDimitry Andric     MachineFunction &MF, const TargetRegisterInfo *TRI,
177480093f4SDimitry Andric     std::vector<CalleeSavedInfo> &CSI) const {
178480093f4SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
179480093f4SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
180480093f4SDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
181480093f4SDimitry Andric   if (CSI.empty())
182480093f4SDimitry Andric     return true; // Early exit if no callee saved registers are modified!
183480093f4SDimitry Andric 
184480093f4SDimitry Andric   unsigned LowGPR = 0;
185480093f4SDimitry Andric   unsigned HighGPR = SystemZ::R15D;
186fe6060f1SDimitry Andric   int StartSPOffset = SystemZMC::ELFCallFrameSize;
187480093f4SDimitry Andric   for (auto &CS : CSI) {
18804eeddc0SDimitry Andric     Register Reg = CS.getReg();
1895ffd83dbSDimitry Andric     int Offset = getRegSpillOffset(MF, Reg);
190480093f4SDimitry Andric     if (Offset) {
191480093f4SDimitry Andric       if (SystemZ::GR64BitRegClass.contains(Reg) && StartSPOffset > Offset) {
192480093f4SDimitry Andric         LowGPR = Reg;
193480093f4SDimitry Andric         StartSPOffset = Offset;
194480093f4SDimitry Andric       }
195fe6060f1SDimitry Andric       Offset -= SystemZMC::ELFCallFrameSize;
196480093f4SDimitry Andric       int FrameIdx = MFFrame.CreateFixedSpillStackObject(8, Offset);
197480093f4SDimitry Andric       CS.setFrameIdx(FrameIdx);
198480093f4SDimitry Andric     } else
199480093f4SDimitry Andric       CS.setFrameIdx(INT32_MAX);
200480093f4SDimitry Andric   }
201480093f4SDimitry Andric 
202480093f4SDimitry Andric   // Save the range of call-saved registers, for use by the
203480093f4SDimitry Andric   // prologue/epilogue inserters.
204480093f4SDimitry Andric   ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset);
205480093f4SDimitry Andric   if (IsVarArg) {
206480093f4SDimitry Andric     // Also save the GPR varargs, if any.  R6D is call-saved, so would
207480093f4SDimitry Andric     // already be included, but we also need to handle the call-clobbered
208480093f4SDimitry Andric     // argument registers.
20904eeddc0SDimitry Andric     Register FirstGPR = ZFI->getVarArgsFirstGPR();
210fe6060f1SDimitry Andric     if (FirstGPR < SystemZ::ELFNumArgGPRs) {
211fe6060f1SDimitry Andric       unsigned Reg = SystemZ::ELFArgGPRs[FirstGPR];
2125ffd83dbSDimitry Andric       int Offset = getRegSpillOffset(MF, Reg);
213480093f4SDimitry Andric       if (StartSPOffset > Offset) {
214480093f4SDimitry Andric         LowGPR = Reg; StartSPOffset = Offset;
215480093f4SDimitry Andric       }
216480093f4SDimitry Andric     }
217480093f4SDimitry Andric   }
218480093f4SDimitry Andric   ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset);
219480093f4SDimitry Andric 
220480093f4SDimitry Andric   // Create fixed stack objects for the remaining registers.
221fe6060f1SDimitry Andric   int CurrOffset = -SystemZMC::ELFCallFrameSize;
2225ffd83dbSDimitry Andric   if (usePackedStack(MF))
2235ffd83dbSDimitry Andric     CurrOffset += StartSPOffset;
2245ffd83dbSDimitry Andric 
225480093f4SDimitry Andric   for (auto &CS : CSI) {
226480093f4SDimitry Andric     if (CS.getFrameIdx() != INT32_MAX)
227480093f4SDimitry Andric       continue;
22804eeddc0SDimitry Andric     Register Reg = CS.getReg();
229480093f4SDimitry Andric     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
230480093f4SDimitry Andric     unsigned Size = TRI->getSpillSize(*RC);
231480093f4SDimitry Andric     CurrOffset -= Size;
232480093f4SDimitry Andric     assert(CurrOffset % 8 == 0 &&
233480093f4SDimitry Andric            "8-byte alignment required for for all register save slots");
234480093f4SDimitry Andric     int FrameIdx = MFFrame.CreateFixedSpillStackObject(Size, CurrOffset);
235480093f4SDimitry Andric     CS.setFrameIdx(FrameIdx);
236480093f4SDimitry Andric   }
237480093f4SDimitry Andric 
238480093f4SDimitry Andric   return true;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
241349cc55cSDimitry Andric void SystemZELFFrameLowering::determineCalleeSaves(MachineFunction &MF,
2420b57cec5SDimitry Andric                                                    BitVector &SavedRegs,
2430b57cec5SDimitry Andric                                                    RegScavenger *RS) const {
2440b57cec5SDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
2470b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2480b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
2490b57cec5SDimitry Andric   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
2500b57cec5SDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   // va_start stores incoming FPR varargs in the normal way, but delegates
2530b57cec5SDimitry Andric   // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
2540b57cec5SDimitry Andric   // Record these pending uses, which typically include the call-saved
2550b57cec5SDimitry Andric   // argument register R6D.
2560b57cec5SDimitry Andric   if (IsVarArg)
257fe6060f1SDimitry Andric     for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
258fe6060f1SDimitry Andric       SavedRegs.set(SystemZ::ELFArgGPRs[I]);
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   // If there are any landing pads, entering them will modify r6/r7.
2610b57cec5SDimitry Andric   if (!MF.getLandingPads().empty()) {
2620b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R6D);
2630b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R7D);
2640b57cec5SDimitry Andric   }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   // If the function requires a frame pointer, record that the hard
2670b57cec5SDimitry Andric   // frame pointer will be clobbered.
2680b57cec5SDimitry Andric   if (HasFP)
2690b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R11D);
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   // If the function calls other functions, record that the return
2720b57cec5SDimitry Andric   // address register will be clobbered.
2730b57cec5SDimitry Andric   if (MFFrame.hasCalls())
2740b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R14D);
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   // If we are saving GPRs other than the stack pointer, we might as well
2770b57cec5SDimitry Andric   // save and restore the stack pointer at the same time, via STMG and LMG.
2780b57cec5SDimitry Andric   // This allows the deallocation to be done by the LMG, rather than needing
2790b57cec5SDimitry Andric   // a separate %r15 addition.
2800b57cec5SDimitry Andric   const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
2810b57cec5SDimitry Andric   for (unsigned I = 0; CSRegs[I]; ++I) {
2820b57cec5SDimitry Andric     unsigned Reg = CSRegs[I];
2830b57cec5SDimitry Andric     if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
2840b57cec5SDimitry Andric       SavedRegs.set(SystemZ::R15D);
2850b57cec5SDimitry Andric       break;
2860b57cec5SDimitry Andric     }
2870b57cec5SDimitry Andric   }
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
290349cc55cSDimitry Andric SystemZELFFrameLowering::SystemZELFFrameLowering()
291349cc55cSDimitry Andric     : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8), 0,
292349cc55cSDimitry Andric                            Align(8), /* StackRealignable */ false),
293349cc55cSDimitry Andric       RegSpillOffsets(0) {
294349cc55cSDimitry Andric 
295349cc55cSDimitry Andric   // Due to the SystemZ ABI, the DWARF CFA (Canonical Frame Address) is not
296349cc55cSDimitry Andric   // equal to the incoming stack pointer, but to incoming stack pointer plus
297349cc55cSDimitry Andric   // 160.  Instead of using a Local Area Offset, the Register save area will
298349cc55cSDimitry Andric   // be occupied by fixed frame objects, and all offsets are actually
299349cc55cSDimitry Andric   // relative to CFA.
300349cc55cSDimitry Andric 
301349cc55cSDimitry Andric   // Create a mapping from register number to save slot offset.
302349cc55cSDimitry Andric   // These offsets are relative to the start of the register save area.
303349cc55cSDimitry Andric   RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
304349cc55cSDimitry Andric   for (unsigned I = 0, E = array_lengthof(ELFSpillOffsetTable); I != E; ++I)
305349cc55cSDimitry Andric     RegSpillOffsets[ELFSpillOffsetTable[I].Reg] = ELFSpillOffsetTable[I].Offset;
306349cc55cSDimitry Andric }
307349cc55cSDimitry Andric 
3080b57cec5SDimitry Andric // Add GPR64 to the save instruction being built by MIB, which is in basic
3090b57cec5SDimitry Andric // block MBB.  IsImplicit says whether this is an explicit operand to the
3100b57cec5SDimitry Andric // instruction, or an implicit one that comes between the explicit start
3110b57cec5SDimitry Andric // and end registers.
3120b57cec5SDimitry Andric static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
3130b57cec5SDimitry Andric                         unsigned GPR64, bool IsImplicit) {
3140b57cec5SDimitry Andric   const TargetRegisterInfo *RI =
3150b57cec5SDimitry Andric       MBB.getParent()->getSubtarget().getRegisterInfo();
3168bcb0991SDimitry Andric   Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
3170b57cec5SDimitry Andric   bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
3180b57cec5SDimitry Andric   if (!IsLive || !IsImplicit) {
3190b57cec5SDimitry Andric     MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
3200b57cec5SDimitry Andric     if (!IsLive)
3210b57cec5SDimitry Andric       MBB.addLiveIn(GPR64);
3220b57cec5SDimitry Andric   }
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric 
325349cc55cSDimitry Andric bool SystemZELFFrameLowering::spillCalleeSavedRegisters(
3265ffd83dbSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
3275ffd83dbSDimitry Andric     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
3280b57cec5SDimitry Andric   if (CSI.empty())
3290b57cec5SDimitry Andric     return false;
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
3320b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
3330b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
3340b57cec5SDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
3350b57cec5SDimitry Andric   DebugLoc DL;
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   // Save GPRs
338480093f4SDimitry Andric   SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
339480093f4SDimitry Andric   if (SpillGPRs.LowGPR) {
340480093f4SDimitry Andric     assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
341480093f4SDimitry Andric            "Should be saving %r15 and something else");
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric     // Build an STMG instruction.
3440b57cec5SDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric     // Add the explicit register operands.
347480093f4SDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
348480093f4SDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     // Add the address.
351480093f4SDimitry Andric     MIB.addReg(SystemZ::R15D).addImm(SpillGPRs.GPROffset);
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric     // Make sure all call-saved GPRs are included as operands and are
3540b57cec5SDimitry Andric     // marked as live on entry.
3554824e7fdSDimitry Andric     for (const CalleeSavedInfo &I : CSI) {
35604eeddc0SDimitry Andric       Register Reg = I.getReg();
3570b57cec5SDimitry Andric       if (SystemZ::GR64BitRegClass.contains(Reg))
3580b57cec5SDimitry Andric         addSavedGPR(MBB, MIB, Reg, true);
3590b57cec5SDimitry Andric     }
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric     // ...likewise GPR varargs.
3620b57cec5SDimitry Andric     if (IsVarArg)
363fe6060f1SDimitry Andric       for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
364fe6060f1SDimitry Andric         addSavedGPR(MBB, MIB, SystemZ::ELFArgGPRs[I], true);
3650b57cec5SDimitry Andric   }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric   // Save FPRs/VRs in the normal TargetInstrInfo way.
3684824e7fdSDimitry Andric   for (const CalleeSavedInfo &I : CSI) {
36904eeddc0SDimitry Andric     Register Reg = I.getReg();
3700b57cec5SDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg)) {
3710b57cec5SDimitry Andric       MBB.addLiveIn(Reg);
3724824e7fdSDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
3730b57cec5SDimitry Andric                                &SystemZ::FP64BitRegClass, TRI);
3740b57cec5SDimitry Andric     }
3750b57cec5SDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg)) {
3760b57cec5SDimitry Andric       MBB.addLiveIn(Reg);
3774824e7fdSDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
3780b57cec5SDimitry Andric                                &SystemZ::VR128BitRegClass, TRI);
3790b57cec5SDimitry Andric     }
3800b57cec5SDimitry Andric   }
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric   return true;
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
385349cc55cSDimitry Andric bool SystemZELFFrameLowering::restoreCalleeSavedRegisters(
3865ffd83dbSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
3875ffd83dbSDimitry Andric     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
3880b57cec5SDimitry Andric   if (CSI.empty())
3890b57cec5SDimitry Andric     return false;
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
3920b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
3930b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
3940b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
3950b57cec5SDimitry Andric   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   // Restore FPRs/VRs in the normal TargetInstrInfo way.
3984824e7fdSDimitry Andric   for (const CalleeSavedInfo &I : CSI) {
39904eeddc0SDimitry Andric     Register Reg = I.getReg();
4000b57cec5SDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg))
4014824e7fdSDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
4020b57cec5SDimitry Andric                                 &SystemZ::FP64BitRegClass, TRI);
4030b57cec5SDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg))
4044824e7fdSDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
4050b57cec5SDimitry Andric                                 &SystemZ::VR128BitRegClass, TRI);
4060b57cec5SDimitry Andric   }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   // Restore call-saved GPRs (but not call-clobbered varargs, which at
4090b57cec5SDimitry Andric   // this point might hold return values).
410480093f4SDimitry Andric   SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
411480093f4SDimitry Andric   if (RestoreGPRs.LowGPR) {
4120b57cec5SDimitry Andric     // If we saved any of %r2-%r5 as varargs, we should also be saving
4130b57cec5SDimitry Andric     // and restoring %r6.  If we're saving %r6 or above, we should be
4140b57cec5SDimitry Andric     // restoring it too.
415480093f4SDimitry Andric     assert(RestoreGPRs.LowGPR != RestoreGPRs.HighGPR &&
416480093f4SDimitry Andric            "Should be loading %r15 and something else");
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric     // Build an LMG instruction.
4190b57cec5SDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric     // Add the explicit register operands.
422480093f4SDimitry Andric     MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
423480093f4SDimitry Andric     MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric     // Add the address.
4260b57cec5SDimitry Andric     MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
427480093f4SDimitry Andric     MIB.addImm(RestoreGPRs.GPROffset);
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric     // Do a second scan adding regs as being defined by instruction
4304824e7fdSDimitry Andric     for (const CalleeSavedInfo &I : CSI) {
43104eeddc0SDimitry Andric       Register Reg = I.getReg();
432480093f4SDimitry Andric       if (Reg != RestoreGPRs.LowGPR && Reg != RestoreGPRs.HighGPR &&
4330b57cec5SDimitry Andric           SystemZ::GR64BitRegClass.contains(Reg))
4340b57cec5SDimitry Andric         MIB.addReg(Reg, RegState::ImplicitDefine);
4350b57cec5SDimitry Andric     }
4360b57cec5SDimitry Andric   }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   return true;
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric 
441349cc55cSDimitry Andric void SystemZELFFrameLowering::processFunctionBeforeFrameFinalized(
442349cc55cSDimitry Andric     MachineFunction &MF, RegScavenger *RS) const {
4430b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
444e8d8bef9SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
445e8d8bef9SDimitry Andric   MachineRegisterInfo *MRI = &MF.getRegInfo();
4465ffd83dbSDimitry Andric   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
447480093f4SDimitry Andric 
4485ffd83dbSDimitry Andric   if (!usePackedStack(MF) || BackChain)
4495ffd83dbSDimitry Andric     // Create the incoming register save area.
450480093f4SDimitry Andric     getOrCreateFramePointerSaveIndex(MF);
451480093f4SDimitry Andric 
4520b57cec5SDimitry Andric   // Get the size of our stack frame to be allocated ...
4530b57cec5SDimitry Andric   uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
454fe6060f1SDimitry Andric                         SystemZMC::ELFCallFrameSize);
4550b57cec5SDimitry Andric   // ... and the maximum offset we may need to reach into the
4560b57cec5SDimitry Andric   // caller's frame to access the save area or stack arguments.
457480093f4SDimitry Andric   int64_t MaxArgOffset = 0;
4580b57cec5SDimitry Andric   for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
4590b57cec5SDimitry Andric     if (MFFrame.getObjectOffset(I) >= 0) {
460480093f4SDimitry Andric       int64_t ArgOffset = MFFrame.getObjectOffset(I) +
4610b57cec5SDimitry Andric                           MFFrame.getObjectSize(I);
4620b57cec5SDimitry Andric       MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
4630b57cec5SDimitry Andric     }
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   uint64_t MaxReach = StackSize + MaxArgOffset;
4660b57cec5SDimitry Andric   if (!isUInt<12>(MaxReach)) {
4670b57cec5SDimitry Andric     // We may need register scavenging slots if some parts of the frame
4680b57cec5SDimitry Andric     // are outside the reach of an unsigned 12-bit displacement.
4690b57cec5SDimitry Andric     // Create 2 for the case where both addresses in an MVC are
4700b57cec5SDimitry Andric     // out of range.
4715ffd83dbSDimitry Andric     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
4725ffd83dbSDimitry Andric     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
4730b57cec5SDimitry Andric   }
474e8d8bef9SDimitry Andric 
475e8d8bef9SDimitry Andric   // If R6 is used as an argument register it is still callee saved. If it in
476e8d8bef9SDimitry Andric   // this case is not clobbered (and restored) it should never be marked as
477e8d8bef9SDimitry Andric   // killed.
478e8d8bef9SDimitry Andric   if (MF.front().isLiveIn(SystemZ::R6D) &&
479e8d8bef9SDimitry Andric       ZFI->getRestoreGPRRegs().LowGPR != SystemZ::R6D)
480e8d8bef9SDimitry Andric     for (auto &MO : MRI->use_nodbg_operands(SystemZ::R6D))
481e8d8bef9SDimitry Andric       MO.setIsKill(false);
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
4850b57cec5SDimitry Andric static void emitIncrement(MachineBasicBlock &MBB,
4865ffd83dbSDimitry Andric                           MachineBasicBlock::iterator &MBBI, const DebugLoc &DL,
4875ffd83dbSDimitry Andric                           Register Reg, int64_t NumBytes,
4880b57cec5SDimitry Andric                           const TargetInstrInfo *TII) {
4890b57cec5SDimitry Andric   while (NumBytes) {
4900b57cec5SDimitry Andric     unsigned Opcode;
4910b57cec5SDimitry Andric     int64_t ThisVal = NumBytes;
4920b57cec5SDimitry Andric     if (isInt<16>(NumBytes))
4930b57cec5SDimitry Andric       Opcode = SystemZ::AGHI;
4940b57cec5SDimitry Andric     else {
4950b57cec5SDimitry Andric       Opcode = SystemZ::AGFI;
4960b57cec5SDimitry Andric       // Make sure we maintain 8-byte stack alignment.
4970b57cec5SDimitry Andric       int64_t MinVal = -uint64_t(1) << 31;
4980b57cec5SDimitry Andric       int64_t MaxVal = (int64_t(1) << 31) - 8;
4990b57cec5SDimitry Andric       if (ThisVal < MinVal)
5000b57cec5SDimitry Andric         ThisVal = MinVal;
5010b57cec5SDimitry Andric       else if (ThisVal > MaxVal)
5020b57cec5SDimitry Andric         ThisVal = MaxVal;
5030b57cec5SDimitry Andric     }
5040b57cec5SDimitry Andric     MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
5050b57cec5SDimitry Andric       .addReg(Reg).addImm(ThisVal);
5060b57cec5SDimitry Andric     // The CC implicit def is dead.
5070b57cec5SDimitry Andric     MI->getOperand(3).setIsDead();
5080b57cec5SDimitry Andric     NumBytes -= ThisVal;
5090b57cec5SDimitry Andric   }
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
5125ffd83dbSDimitry Andric // Add CFI for the new CFA offset.
5135ffd83dbSDimitry Andric static void buildCFAOffs(MachineBasicBlock &MBB,
5145ffd83dbSDimitry Andric                          MachineBasicBlock::iterator MBBI,
5155ffd83dbSDimitry Andric                          const DebugLoc &DL, int Offset,
5165ffd83dbSDimitry Andric                          const SystemZInstrInfo *ZII) {
5175ffd83dbSDimitry Andric   unsigned CFIIndex = MBB.getParent()->addFrameInst(
5185ffd83dbSDimitry Andric     MCCFIInstruction::cfiDefCfaOffset(nullptr, -Offset));
5195ffd83dbSDimitry Andric   BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5205ffd83dbSDimitry Andric     .addCFIIndex(CFIIndex);
5215ffd83dbSDimitry Andric }
5225ffd83dbSDimitry Andric 
5235ffd83dbSDimitry Andric // Add CFI for the new frame location.
5245ffd83dbSDimitry Andric static void buildDefCFAReg(MachineBasicBlock &MBB,
5255ffd83dbSDimitry Andric                            MachineBasicBlock::iterator MBBI,
5265ffd83dbSDimitry Andric                            const DebugLoc &DL, unsigned Reg,
5275ffd83dbSDimitry Andric                            const SystemZInstrInfo *ZII) {
5285ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
5295ffd83dbSDimitry Andric   MachineModuleInfo &MMI = MF.getMMI();
5305ffd83dbSDimitry Andric   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
5315ffd83dbSDimitry Andric   unsigned RegNum = MRI->getDwarfRegNum(Reg, true);
5325ffd83dbSDimitry Andric   unsigned CFIIndex = MF.addFrameInst(
5335ffd83dbSDimitry Andric                         MCCFIInstruction::createDefCfaRegister(nullptr, RegNum));
5345ffd83dbSDimitry Andric   BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5355ffd83dbSDimitry Andric     .addCFIIndex(CFIIndex);
5365ffd83dbSDimitry Andric }
5375ffd83dbSDimitry Andric 
538349cc55cSDimitry Andric void SystemZELFFrameLowering::emitPrologue(MachineFunction &MF,
5390b57cec5SDimitry Andric                                            MachineBasicBlock &MBB) const {
5400b57cec5SDimitry Andric   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
5415ffd83dbSDimitry Andric   const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
5425ffd83dbSDimitry Andric   const SystemZTargetLowering &TLI = *STI.getTargetLowering();
5430b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
5445ffd83dbSDimitry Andric   auto *ZII = static_cast<const SystemZInstrInfo *>(STI.getInstrInfo());
5450b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
5460b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
5470b57cec5SDimitry Andric   MachineModuleInfo &MMI = MF.getMMI();
5480b57cec5SDimitry Andric   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
5490b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
5500b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
5510b57cec5SDimitry Andric 
552480093f4SDimitry Andric   // In GHC calling convention C stack space, including the ABI-defined
553480093f4SDimitry Andric   // 160-byte base area, is (de)allocated by GHC itself.  This stack space may
554480093f4SDimitry Andric   // be used by LLVM as spill slots for the tail recursive GHC functions.  Thus
555480093f4SDimitry Andric   // do not allocate stack space here, too.
556480093f4SDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::GHC) {
557480093f4SDimitry Andric     if (MFFrame.getStackSize() > 2048 * sizeof(long)) {
558480093f4SDimitry Andric       report_fatal_error(
559480093f4SDimitry Andric           "Pre allocated stack space for GHC function is too small");
560480093f4SDimitry Andric     }
561480093f4SDimitry Andric     if (HasFP) {
562480093f4SDimitry Andric       report_fatal_error(
563480093f4SDimitry Andric           "In GHC calling convention a frame pointer is not supported");
564480093f4SDimitry Andric     }
565fe6060f1SDimitry Andric     MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::ELFCallFrameSize);
566480093f4SDimitry Andric     return;
567480093f4SDimitry Andric   }
568480093f4SDimitry Andric 
5690b57cec5SDimitry Andric   // Debug location must be unknown since the first debug location is used
5700b57cec5SDimitry Andric   // to determine the end of the prologue.
5710b57cec5SDimitry Andric   DebugLoc DL;
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   // The current offset of the stack pointer from the CFA.
574fe6060f1SDimitry Andric   int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
5750b57cec5SDimitry Andric 
576480093f4SDimitry Andric   if (ZFI->getSpillGPRRegs().LowGPR) {
5770b57cec5SDimitry Andric     // Skip over the GPR saves.
5780b57cec5SDimitry Andric     if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
5790b57cec5SDimitry Andric       ++MBBI;
5800b57cec5SDimitry Andric     else
5810b57cec5SDimitry Andric       llvm_unreachable("Couldn't skip over GPR saves");
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric     // Add CFI for the GPR saves.
5840b57cec5SDimitry Andric     for (auto &Save : CSI) {
58504eeddc0SDimitry Andric       Register Reg = Save.getReg();
5860b57cec5SDimitry Andric       if (SystemZ::GR64BitRegClass.contains(Reg)) {
587480093f4SDimitry Andric         int FI = Save.getFrameIdx();
588480093f4SDimitry Andric         int64_t Offset = MFFrame.getObjectOffset(FI);
5890b57cec5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
5900b57cec5SDimitry Andric             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
5910b57cec5SDimitry Andric         BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5920b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
5930b57cec5SDimitry Andric       }
5940b57cec5SDimitry Andric     }
5950b57cec5SDimitry Andric   }
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
5980b57cec5SDimitry Andric   // We need to allocate the ABI-defined 160-byte base area whenever
5990b57cec5SDimitry Andric   // we allocate stack space for our own use and whenever we call another
6000b57cec5SDimitry Andric   // function.
601480093f4SDimitry Andric   bool HasStackObject = false;
602480093f4SDimitry Andric   for (unsigned i = 0, e = MFFrame.getObjectIndexEnd(); i != e; ++i)
603480093f4SDimitry Andric     if (!MFFrame.isDeadObjectIndex(i)) {
604480093f4SDimitry Andric       HasStackObject = true;
605480093f4SDimitry Andric       break;
6060b57cec5SDimitry Andric     }
607480093f4SDimitry Andric   if (HasStackObject || MFFrame.hasCalls())
608fe6060f1SDimitry Andric     StackSize += SystemZMC::ELFCallFrameSize;
609480093f4SDimitry Andric   // Don't allocate the incoming reg save area.
610fe6060f1SDimitry Andric   StackSize = StackSize > SystemZMC::ELFCallFrameSize
611fe6060f1SDimitry Andric                   ? StackSize - SystemZMC::ELFCallFrameSize
612480093f4SDimitry Andric                   : 0;
613480093f4SDimitry Andric   MFFrame.setStackSize(StackSize);
6140b57cec5SDimitry Andric 
6150b57cec5SDimitry Andric   if (StackSize) {
6160b57cec5SDimitry Andric     // Allocate StackSize bytes.
6170b57cec5SDimitry Andric     int64_t Delta = -int64_t(StackSize);
6185ffd83dbSDimitry Andric     const unsigned ProbeSize = TLI.getStackProbeSize(MF);
6195ffd83dbSDimitry Andric     bool FreeProbe = (ZFI->getSpillGPRRegs().GPROffset &&
6205ffd83dbSDimitry Andric            (ZFI->getSpillGPRRegs().GPROffset + StackSize) < ProbeSize);
6215ffd83dbSDimitry Andric     if (!FreeProbe &&
6225ffd83dbSDimitry Andric         MF.getSubtarget().getTargetLowering()->hasInlineStackProbe(MF)) {
6235ffd83dbSDimitry Andric       // Stack probing may involve looping, but splitting the prologue block
6245ffd83dbSDimitry Andric       // is not possible at this point since it would invalidate the
6255ffd83dbSDimitry Andric       // SaveBlocks / RestoreBlocks sets of PEI in the single block function
6265ffd83dbSDimitry Andric       // case. Build a pseudo to be handled later by inlineStackProbe().
6275ffd83dbSDimitry Andric       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::PROBED_STACKALLOC))
6285ffd83dbSDimitry Andric         .addImm(StackSize);
6295ffd83dbSDimitry Andric     }
6305ffd83dbSDimitry Andric     else {
631e8d8bef9SDimitry Andric       bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
632e8d8bef9SDimitry Andric       // If we need backchain, save current stack pointer.  R1 is free at
633e8d8bef9SDimitry Andric       // this point.
634e8d8bef9SDimitry Andric       if (StoreBackchain)
635e8d8bef9SDimitry Andric         BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
636e8d8bef9SDimitry Andric           .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
6370b57cec5SDimitry Andric       emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
6385ffd83dbSDimitry Andric       buildCFAOffs(MBB, MBBI, DL, SPOffsetFromCFA + Delta, ZII);
639e8d8bef9SDimitry Andric       if (StoreBackchain)
6400b57cec5SDimitry Andric         BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
6415ffd83dbSDimitry Andric           .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
642e8d8bef9SDimitry Andric           .addImm(getBackchainOffset(MF)).addReg(0);
6435ffd83dbSDimitry Andric     }
644e8d8bef9SDimitry Andric     SPOffsetFromCFA += Delta;
6450b57cec5SDimitry Andric   }
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric   if (HasFP) {
6480b57cec5SDimitry Andric     // Copy the base of the frame to R11.
6490b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
6500b57cec5SDimitry Andric       .addReg(SystemZ::R15D);
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric     // Add CFI for the new frame location.
6535ffd83dbSDimitry Andric     buildDefCFAReg(MBB, MBBI, DL, SystemZ::R11D, ZII);
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric     // Mark the FramePtr as live at the beginning of every block except
6560b57cec5SDimitry Andric     // the entry block.  (We'll have marked R11 as live on entry when
6570b57cec5SDimitry Andric     // saving the GPRs.)
658349cc55cSDimitry Andric     for (MachineBasicBlock &MBBJ : llvm::drop_begin(MF))
659349cc55cSDimitry Andric       MBBJ.addLiveIn(SystemZ::R11D);
6600b57cec5SDimitry Andric   }
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   // Skip over the FPR/VR saves.
6630b57cec5SDimitry Andric   SmallVector<unsigned, 8> CFIIndexes;
6640b57cec5SDimitry Andric   for (auto &Save : CSI) {
66504eeddc0SDimitry Andric     Register Reg = Save.getReg();
6660b57cec5SDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg)) {
6670b57cec5SDimitry Andric       if (MBBI != MBB.end() &&
6680b57cec5SDimitry Andric           (MBBI->getOpcode() == SystemZ::STD ||
6690b57cec5SDimitry Andric            MBBI->getOpcode() == SystemZ::STDY))
6700b57cec5SDimitry Andric         ++MBBI;
6710b57cec5SDimitry Andric       else
6720b57cec5SDimitry Andric         llvm_unreachable("Couldn't skip over FPR save");
6730b57cec5SDimitry Andric     } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
6740b57cec5SDimitry Andric       if (MBBI != MBB.end() &&
6750b57cec5SDimitry Andric           MBBI->getOpcode() == SystemZ::VST)
6760b57cec5SDimitry Andric         ++MBBI;
6770b57cec5SDimitry Andric       else
6780b57cec5SDimitry Andric         llvm_unreachable("Couldn't skip over VR save");
6790b57cec5SDimitry Andric     } else
6800b57cec5SDimitry Andric       continue;
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric     // Add CFI for the this save.
6830b57cec5SDimitry Andric     unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
6845ffd83dbSDimitry Andric     Register IgnoredFrameReg;
6850b57cec5SDimitry Andric     int64_t Offset =
686e8d8bef9SDimitry Andric         getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg)
687e8d8bef9SDimitry Andric             .getFixed();
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
6900b57cec5SDimitry Andric           nullptr, DwarfReg, SPOffsetFromCFA + Offset));
6910b57cec5SDimitry Andric     CFIIndexes.push_back(CFIIndex);
6920b57cec5SDimitry Andric   }
6930b57cec5SDimitry Andric   // Complete the CFI for the FPR/VR saves, modelling them as taking effect
6940b57cec5SDimitry Andric   // after the last save.
6950b57cec5SDimitry Andric   for (auto CFIIndex : CFIIndexes) {
6960b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
6970b57cec5SDimitry Andric         .addCFIIndex(CFIIndex);
6980b57cec5SDimitry Andric   }
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric 
701349cc55cSDimitry Andric void SystemZELFFrameLowering::emitEpilogue(MachineFunction &MF,
7020b57cec5SDimitry Andric                                            MachineBasicBlock &MBB) const {
7030b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
7040b57cec5SDimitry Andric   auto *ZII =
7050b57cec5SDimitry Andric       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
7060b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
7070b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
7080b57cec5SDimitry Andric 
709349cc55cSDimitry Andric   // See SystemZELFFrameLowering::emitPrologue
710480093f4SDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
711480093f4SDimitry Andric     return;
712480093f4SDimitry Andric 
7130b57cec5SDimitry Andric   // Skip the return instruction.
7140b57cec5SDimitry Andric   assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
717480093f4SDimitry Andric   if (ZFI->getRestoreGPRRegs().LowGPR) {
7180b57cec5SDimitry Andric     --MBBI;
7190b57cec5SDimitry Andric     unsigned Opcode = MBBI->getOpcode();
7200b57cec5SDimitry Andric     if (Opcode != SystemZ::LMG)
7210b57cec5SDimitry Andric       llvm_unreachable("Expected to see callee-save register restore code");
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric     unsigned AddrOpNo = 2;
7240b57cec5SDimitry Andric     DebugLoc DL = MBBI->getDebugLoc();
7250b57cec5SDimitry Andric     uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
7260b57cec5SDimitry Andric     unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric     // If the offset is too large, use the largest stack-aligned offset
7290b57cec5SDimitry Andric     // and add the rest to the base register (the stack or frame pointer).
7300b57cec5SDimitry Andric     if (!NewOpcode) {
7310b57cec5SDimitry Andric       uint64_t NumBytes = Offset - 0x7fff8;
7320b57cec5SDimitry Andric       emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
7330b57cec5SDimitry Andric                     NumBytes, ZII);
7340b57cec5SDimitry Andric       Offset -= NumBytes;
7350b57cec5SDimitry Andric       NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
7360b57cec5SDimitry Andric       assert(NewOpcode && "No restore instruction available");
7370b57cec5SDimitry Andric     }
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric     MBBI->setDesc(ZII->get(NewOpcode));
7400b57cec5SDimitry Andric     MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
7410b57cec5SDimitry Andric   } else if (StackSize) {
7420b57cec5SDimitry Andric     DebugLoc DL = MBBI->getDebugLoc();
7430b57cec5SDimitry Andric     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
7440b57cec5SDimitry Andric   }
7450b57cec5SDimitry Andric }
7460b57cec5SDimitry Andric 
747349cc55cSDimitry Andric void SystemZELFFrameLowering::inlineStackProbe(
748349cc55cSDimitry Andric     MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
7495ffd83dbSDimitry Andric   auto *ZII =
7505ffd83dbSDimitry Andric     static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
7515ffd83dbSDimitry Andric   const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
7525ffd83dbSDimitry Andric   const SystemZTargetLowering &TLI = *STI.getTargetLowering();
7535ffd83dbSDimitry Andric 
7545ffd83dbSDimitry Andric   MachineInstr *StackAllocMI = nullptr;
7555ffd83dbSDimitry Andric   for (MachineInstr &MI : PrologMBB)
7565ffd83dbSDimitry Andric     if (MI.getOpcode() == SystemZ::PROBED_STACKALLOC) {
7575ffd83dbSDimitry Andric       StackAllocMI = &MI;
7585ffd83dbSDimitry Andric       break;
7595ffd83dbSDimitry Andric     }
7605ffd83dbSDimitry Andric   if (StackAllocMI == nullptr)
7615ffd83dbSDimitry Andric     return;
7625ffd83dbSDimitry Andric   uint64_t StackSize = StackAllocMI->getOperand(0).getImm();
7635ffd83dbSDimitry Andric   const unsigned ProbeSize = TLI.getStackProbeSize(MF);
7645ffd83dbSDimitry Andric   uint64_t NumFullBlocks = StackSize / ProbeSize;
7655ffd83dbSDimitry Andric   uint64_t Residual = StackSize % ProbeSize;
766fe6060f1SDimitry Andric   int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
7675ffd83dbSDimitry Andric   MachineBasicBlock *MBB = &PrologMBB;
7685ffd83dbSDimitry Andric   MachineBasicBlock::iterator MBBI = StackAllocMI;
7695ffd83dbSDimitry Andric   const DebugLoc DL = StackAllocMI->getDebugLoc();
7705ffd83dbSDimitry Andric 
7715ffd83dbSDimitry Andric   // Allocate a block of Size bytes on the stack and probe it.
7725ffd83dbSDimitry Andric   auto allocateAndProbe = [&](MachineBasicBlock &InsMBB,
7735ffd83dbSDimitry Andric                               MachineBasicBlock::iterator InsPt, unsigned Size,
7745ffd83dbSDimitry Andric                               bool EmitCFI) -> void {
7755ffd83dbSDimitry Andric     emitIncrement(InsMBB, InsPt, DL, SystemZ::R15D, -int64_t(Size), ZII);
7765ffd83dbSDimitry Andric     if (EmitCFI) {
7775ffd83dbSDimitry Andric       SPOffsetFromCFA -= Size;
7785ffd83dbSDimitry Andric       buildCFAOffs(InsMBB, InsPt, DL, SPOffsetFromCFA, ZII);
7795ffd83dbSDimitry Andric     }
7805ffd83dbSDimitry Andric     // Probe by means of a volatile compare.
7815ffd83dbSDimitry Andric     MachineMemOperand *MMO = MF.getMachineMemOperand(MachinePointerInfo(),
7825ffd83dbSDimitry Andric       MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad, 8, Align(1));
7835ffd83dbSDimitry Andric     BuildMI(InsMBB, InsPt, DL, ZII->get(SystemZ::CG))
7845ffd83dbSDimitry Andric       .addReg(SystemZ::R0D, RegState::Undef)
7855ffd83dbSDimitry Andric       .addReg(SystemZ::R15D).addImm(Size - 8).addReg(0)
7865ffd83dbSDimitry Andric       .addMemOperand(MMO);
7875ffd83dbSDimitry Andric   };
7885ffd83dbSDimitry Andric 
789e8d8bef9SDimitry Andric   bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
790e8d8bef9SDimitry Andric   if (StoreBackchain)
791e8d8bef9SDimitry Andric     BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR))
792e8d8bef9SDimitry Andric       .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
793e8d8bef9SDimitry Andric 
794e8d8bef9SDimitry Andric   MachineBasicBlock *DoneMBB = nullptr;
795e8d8bef9SDimitry Andric   MachineBasicBlock *LoopMBB = nullptr;
7965ffd83dbSDimitry Andric   if (NumFullBlocks < 3) {
7975ffd83dbSDimitry Andric     // Emit unrolled probe statements.
7985ffd83dbSDimitry Andric     for (unsigned int i = 0; i < NumFullBlocks; i++)
7995ffd83dbSDimitry Andric       allocateAndProbe(*MBB, MBBI, ProbeSize, true/*EmitCFI*/);
8005ffd83dbSDimitry Andric   } else {
8015ffd83dbSDimitry Andric     // Emit a loop probing the pages.
8025ffd83dbSDimitry Andric     uint64_t LoopAlloc = ProbeSize * NumFullBlocks;
8035ffd83dbSDimitry Andric     SPOffsetFromCFA -= LoopAlloc;
8045ffd83dbSDimitry Andric 
805e8d8bef9SDimitry Andric     // Use R0D to hold the exit value.
806e8d8bef9SDimitry Andric     BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R0D)
8075ffd83dbSDimitry Andric       .addReg(SystemZ::R15D);
808e8d8bef9SDimitry Andric     buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R0D, ZII);
809e8d8bef9SDimitry Andric     emitIncrement(*MBB, MBBI, DL, SystemZ::R0D, -int64_t(LoopAlloc), ZII);
810fe6060f1SDimitry Andric     buildCFAOffs(*MBB, MBBI, DL, -int64_t(SystemZMC::ELFCallFrameSize + LoopAlloc),
8115ffd83dbSDimitry Andric                  ZII);
8125ffd83dbSDimitry Andric 
813e8d8bef9SDimitry Andric     DoneMBB = SystemZ::splitBlockBefore(MBBI, MBB);
814e8d8bef9SDimitry Andric     LoopMBB = SystemZ::emitBlockAfter(MBB);
8155ffd83dbSDimitry Andric     MBB->addSuccessor(LoopMBB);
8165ffd83dbSDimitry Andric     LoopMBB->addSuccessor(LoopMBB);
8175ffd83dbSDimitry Andric     LoopMBB->addSuccessor(DoneMBB);
8185ffd83dbSDimitry Andric 
8195ffd83dbSDimitry Andric     MBB = LoopMBB;
8205ffd83dbSDimitry Andric     allocateAndProbe(*MBB, MBB->end(), ProbeSize, false/*EmitCFI*/);
8215ffd83dbSDimitry Andric     BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::CLGR))
822e8d8bef9SDimitry Andric       .addReg(SystemZ::R15D).addReg(SystemZ::R0D);
8235ffd83dbSDimitry Andric     BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::BRC))
8245ffd83dbSDimitry Andric       .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_GT).addMBB(MBB);
8255ffd83dbSDimitry Andric 
8265ffd83dbSDimitry Andric     MBB = DoneMBB;
8275ffd83dbSDimitry Andric     MBBI = DoneMBB->begin();
8285ffd83dbSDimitry Andric     buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R15D, ZII);
8295ffd83dbSDimitry Andric   }
8305ffd83dbSDimitry Andric 
8315ffd83dbSDimitry Andric   if (Residual)
8325ffd83dbSDimitry Andric     allocateAndProbe(*MBB, MBBI, Residual, true/*EmitCFI*/);
8335ffd83dbSDimitry Andric 
834e8d8bef9SDimitry Andric   if (StoreBackchain)
835e8d8bef9SDimitry Andric     BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::STG))
836e8d8bef9SDimitry Andric       .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
837e8d8bef9SDimitry Andric       .addImm(getBackchainOffset(MF)).addReg(0);
838e8d8bef9SDimitry Andric 
8395ffd83dbSDimitry Andric   StackAllocMI->eraseFromParent();
840e8d8bef9SDimitry Andric   if (DoneMBB != nullptr) {
841e8d8bef9SDimitry Andric     // Compute the live-in lists for the new blocks.
842e8d8bef9SDimitry Andric     recomputeLiveIns(*DoneMBB);
843e8d8bef9SDimitry Andric     recomputeLiveIns(*LoopMBB);
844e8d8bef9SDimitry Andric   }
8455ffd83dbSDimitry Andric }
8465ffd83dbSDimitry Andric 
847349cc55cSDimitry Andric bool SystemZELFFrameLowering::hasFP(const MachineFunction &MF) const {
8480b57cec5SDimitry Andric   return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
84904eeddc0SDimitry Andric           MF.getFrameInfo().hasVarSizedObjects());
8500b57cec5SDimitry Andric }
8510b57cec5SDimitry Andric 
852349cc55cSDimitry Andric StackOffset SystemZELFFrameLowering::getFrameIndexReference(
853349cc55cSDimitry Andric     const MachineFunction &MF, int FI, Register &FrameReg) const {
854fe6060f1SDimitry Andric   // Our incoming SP is actually SystemZMC::ELFCallFrameSize below the CFA, so
855480093f4SDimitry Andric   // add that difference here.
856e8d8bef9SDimitry Andric   StackOffset Offset =
857480093f4SDimitry Andric       TargetFrameLowering::getFrameIndexReference(MF, FI, FrameReg);
858fe6060f1SDimitry Andric   return Offset + StackOffset::getFixed(SystemZMC::ELFCallFrameSize);
859480093f4SDimitry Andric }
860480093f4SDimitry Andric 
861349cc55cSDimitry Andric unsigned SystemZELFFrameLowering::getRegSpillOffset(MachineFunction &MF,
8625ffd83dbSDimitry Andric                                                     Register Reg) const {
8635ffd83dbSDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
8645ffd83dbSDimitry Andric   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
8655ffd83dbSDimitry Andric   bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
8665ffd83dbSDimitry Andric   unsigned Offset = RegSpillOffsets[Reg];
8675ffd83dbSDimitry Andric   if (usePackedStack(MF) && !(IsVarArg && !SoftFloat)) {
8685ffd83dbSDimitry Andric     if (SystemZ::GR64BitRegClass.contains(Reg))
8695ffd83dbSDimitry Andric       // Put all GPRs at the top of the Register save area with packed
8705ffd83dbSDimitry Andric       // stack. Make room for the backchain if needed.
8715ffd83dbSDimitry Andric       Offset += BackChain ? 24 : 32;
8725ffd83dbSDimitry Andric     else
8735ffd83dbSDimitry Andric       Offset = 0;
8745ffd83dbSDimitry Andric   }
8755ffd83dbSDimitry Andric   return Offset;
8765ffd83dbSDimitry Andric }
8775ffd83dbSDimitry Andric 
878349cc55cSDimitry Andric int SystemZELFFrameLowering::getOrCreateFramePointerSaveIndex(
879349cc55cSDimitry Andric     MachineFunction &MF) const {
880480093f4SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
881480093f4SDimitry Andric   int FI = ZFI->getFramePointerSaveIndex();
882480093f4SDimitry Andric   if (!FI) {
883480093f4SDimitry Andric     MachineFrameInfo &MFFrame = MF.getFrameInfo();
884fe6060f1SDimitry Andric     int Offset = getBackchainOffset(MF) - SystemZMC::ELFCallFrameSize;
8855ffd83dbSDimitry Andric     FI = MFFrame.CreateFixedObject(8, Offset, false);
886480093f4SDimitry Andric     ZFI->setFramePointerSaveIndex(FI);
887480093f4SDimitry Andric   }
888480093f4SDimitry Andric   return FI;
889480093f4SDimitry Andric }
8905ffd83dbSDimitry Andric 
891349cc55cSDimitry Andric bool SystemZELFFrameLowering::usePackedStack(MachineFunction &MF) const {
8925ffd83dbSDimitry Andric   bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack");
8935ffd83dbSDimitry Andric   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
8945ffd83dbSDimitry Andric   bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
8955ffd83dbSDimitry Andric   if (HasPackedStackAttr && BackChain && !SoftFloat)
8965ffd83dbSDimitry Andric     report_fatal_error("packed-stack + backchain + hard-float is unsupported.");
8975ffd83dbSDimitry Andric   bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC;
8985ffd83dbSDimitry Andric   return HasPackedStackAttr && CallConv;
8995ffd83dbSDimitry Andric }
900349cc55cSDimitry Andric 
901349cc55cSDimitry Andric SystemZXPLINKFrameLowering::SystemZXPLINKFrameLowering()
9020eae32dcSDimitry Andric     : SystemZFrameLowering(TargetFrameLowering::StackGrowsDown, Align(32), 0,
903349cc55cSDimitry Andric                            Align(32), /* StackRealignable */ false),
904349cc55cSDimitry Andric       RegSpillOffsets(-1) {
905349cc55cSDimitry Andric 
906349cc55cSDimitry Andric   // Create a mapping from register number to save slot offset.
907349cc55cSDimitry Andric   // These offsets are relative to the start of the local are area.
908349cc55cSDimitry Andric   RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
909349cc55cSDimitry Andric   for (unsigned I = 0, E = array_lengthof(XPLINKSpillOffsetTable); I != E; ++I)
910349cc55cSDimitry Andric     RegSpillOffsets[XPLINKSpillOffsetTable[I].Reg] =
911349cc55cSDimitry Andric         XPLINKSpillOffsetTable[I].Offset;
912349cc55cSDimitry Andric }
913349cc55cSDimitry Andric 
914349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::assignCalleeSavedSpillSlots(
915349cc55cSDimitry Andric     MachineFunction &MF, const TargetRegisterInfo *TRI,
916349cc55cSDimitry Andric     std::vector<CalleeSavedInfo> &CSI) const {
917349cc55cSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
918349cc55cSDimitry Andric   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
919349cc55cSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
920349cc55cSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
921349cc55cSDimitry Andric 
922349cc55cSDimitry Andric   // Scan the call-saved GPRs and find the bounds of the register spill area.
923349cc55cSDimitry Andric   unsigned LowGPR = 0;
924349cc55cSDimitry Andric   int LowOffset = INT32_MAX;
925349cc55cSDimitry Andric   unsigned HighGPR = LowGPR;
926349cc55cSDimitry Andric   int HighOffset = -1;
927349cc55cSDimitry Andric 
928349cc55cSDimitry Andric   unsigned RegSP = Regs.getStackPointerRegister();
929349cc55cSDimitry Andric   auto &GRRegClass = SystemZ::GR64BitRegClass;
930349cc55cSDimitry Andric   const unsigned RegSize = 8;
931349cc55cSDimitry Andric 
932349cc55cSDimitry Andric   auto ProcessCSI = [&](std::vector<CalleeSavedInfo> &CSIList) {
933349cc55cSDimitry Andric     for (auto &CS : CSIList) {
93404eeddc0SDimitry Andric       Register Reg = CS.getReg();
935349cc55cSDimitry Andric       int Offset = RegSpillOffsets[Reg];
936349cc55cSDimitry Andric       if (Offset >= 0) {
937349cc55cSDimitry Andric         if (GRRegClass.contains(Reg)) {
938349cc55cSDimitry Andric           if (LowOffset > Offset) {
939349cc55cSDimitry Andric             LowOffset = Offset;
940349cc55cSDimitry Andric             LowGPR = Reg;
941349cc55cSDimitry Andric           }
942349cc55cSDimitry Andric 
943349cc55cSDimitry Andric           if (Offset > HighOffset) {
944349cc55cSDimitry Andric             HighOffset = Offset;
945349cc55cSDimitry Andric             HighGPR = Reg;
946349cc55cSDimitry Andric           }
947349cc55cSDimitry Andric         }
948349cc55cSDimitry Andric         int FrameIdx = MFFrame.CreateFixedSpillStackObject(RegSize, Offset);
949349cc55cSDimitry Andric         CS.setFrameIdx(FrameIdx);
950349cc55cSDimitry Andric       } else
951349cc55cSDimitry Andric         CS.setFrameIdx(INT32_MAX);
952349cc55cSDimitry Andric     }
953349cc55cSDimitry Andric   };
954349cc55cSDimitry Andric 
955349cc55cSDimitry Andric   std::vector<CalleeSavedInfo> Spills;
956349cc55cSDimitry Andric 
957349cc55cSDimitry Andric   // For non-leaf functions:
958349cc55cSDimitry Andric   // - the address of callee (entry point) register R6 must be saved
959349cc55cSDimitry Andric   Spills.push_back(CalleeSavedInfo(Regs.getAddressOfCalleeRegister()));
960349cc55cSDimitry Andric 
961349cc55cSDimitry Andric   // If the function needs a frame pointer, or if the backchain pointer should
962349cc55cSDimitry Andric   // be stored, then save the stack pointer register R4.
963349cc55cSDimitry Andric   if (hasFP(MF) || MF.getFunction().hasFnAttribute("backchain"))
964349cc55cSDimitry Andric     Spills.push_back(CalleeSavedInfo(RegSP));
965349cc55cSDimitry Andric 
966349cc55cSDimitry Andric   // Save the range of call-saved registers, for use by the
967349cc55cSDimitry Andric   // prologue/epilogue inserters.
968349cc55cSDimitry Andric   ProcessCSI(CSI);
969349cc55cSDimitry Andric   MFI->setRestoreGPRRegs(LowGPR, HighGPR, LowOffset);
970349cc55cSDimitry Andric 
971349cc55cSDimitry Andric   // Save the range of call-saved registers, for use by the epilogue inserter.
972349cc55cSDimitry Andric   ProcessCSI(Spills);
973349cc55cSDimitry Andric   MFI->setSpillGPRRegs(LowGPR, HighGPR, LowOffset);
974349cc55cSDimitry Andric 
975349cc55cSDimitry Andric   // Create spill slots for the remaining registers.
976349cc55cSDimitry Andric   for (auto &CS : CSI) {
977349cc55cSDimitry Andric     if (CS.getFrameIdx() != INT32_MAX)
978349cc55cSDimitry Andric       continue;
97904eeddc0SDimitry Andric     Register Reg = CS.getReg();
980349cc55cSDimitry Andric     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
981349cc55cSDimitry Andric     Align Alignment = TRI->getSpillAlign(*RC);
982349cc55cSDimitry Andric     unsigned Size = TRI->getSpillSize(*RC);
983349cc55cSDimitry Andric     Alignment = std::min(Alignment, getStackAlign());
984349cc55cSDimitry Andric     int FrameIdx = MFFrame.CreateStackObject(Size, Alignment, true);
985349cc55cSDimitry Andric     CS.setFrameIdx(FrameIdx);
986349cc55cSDimitry Andric   }
987349cc55cSDimitry Andric 
988349cc55cSDimitry Andric   return true;
989349cc55cSDimitry Andric }
990349cc55cSDimitry Andric 
991349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::determineCalleeSaves(MachineFunction &MF,
992349cc55cSDimitry Andric                                                       BitVector &SavedRegs,
993349cc55cSDimitry Andric                                                       RegScavenger *RS) const {
994349cc55cSDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
995349cc55cSDimitry Andric 
996349cc55cSDimitry Andric   bool HasFP = hasFP(MF);
997349cc55cSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
998349cc55cSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
999349cc55cSDimitry Andric 
1000349cc55cSDimitry Andric   // If the function requires a frame pointer, record that the hard
1001349cc55cSDimitry Andric   // frame pointer will be clobbered.
1002349cc55cSDimitry Andric   if (HasFP)
1003349cc55cSDimitry Andric     SavedRegs.set(Regs.getFramePointerRegister());
1004349cc55cSDimitry Andric 
1005349cc55cSDimitry Andric   // If the function is not an XPLeaf function, we need to save the
1006349cc55cSDimitry Andric   // return address register. We also always use that register for
1007349cc55cSDimitry Andric   // the return instruction, so it needs to be restored in the
1008349cc55cSDimitry Andric   // epilogue even though that register is considered to be volatile.
1009349cc55cSDimitry Andric   // #TODO: Implement leaf detection.
1010349cc55cSDimitry Andric   SavedRegs.set(Regs.getReturnFunctionAddressRegister());
1011349cc55cSDimitry Andric }
1012349cc55cSDimitry Andric 
1013349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::spillCalleeSavedRegisters(
1014349cc55cSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
1015349cc55cSDimitry Andric     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
1016349cc55cSDimitry Andric   if (CSI.empty())
1017349cc55cSDimitry Andric     return true;
1018349cc55cSDimitry Andric 
1019349cc55cSDimitry Andric   MachineFunction &MF = *MBB.getParent();
1020349cc55cSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
1021349cc55cSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1022349cc55cSDimitry Andric   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1023349cc55cSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1024349cc55cSDimitry Andric   SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
1025349cc55cSDimitry Andric   DebugLoc DL;
1026349cc55cSDimitry Andric 
1027349cc55cSDimitry Andric   // Save GPRs
1028349cc55cSDimitry Andric   if (SpillGPRs.LowGPR) {
1029349cc55cSDimitry Andric     assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
1030349cc55cSDimitry Andric            "Should be saving multiple registers");
1031349cc55cSDimitry Andric 
1032349cc55cSDimitry Andric     // Build an STM/STMG instruction.
1033349cc55cSDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
1034349cc55cSDimitry Andric 
1035349cc55cSDimitry Andric     // Add the explicit register operands.
1036349cc55cSDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
1037349cc55cSDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
1038349cc55cSDimitry Andric 
1039349cc55cSDimitry Andric     // Add the address r4
1040349cc55cSDimitry Andric     MIB.addReg(Regs.getStackPointerRegister());
1041349cc55cSDimitry Andric 
1042349cc55cSDimitry Andric     // Add the partial offset
1043349cc55cSDimitry Andric     // We cannot add the actual offset as, at the stack is not finalized
1044349cc55cSDimitry Andric     MIB.addImm(SpillGPRs.GPROffset);
1045349cc55cSDimitry Andric 
1046349cc55cSDimitry Andric     // Make sure all call-saved GPRs are included as operands and are
1047349cc55cSDimitry Andric     // marked as live on entry.
1048349cc55cSDimitry Andric     auto &GRRegClass = SystemZ::GR64BitRegClass;
10494824e7fdSDimitry Andric     for (const CalleeSavedInfo &I : CSI) {
105004eeddc0SDimitry Andric       Register Reg = I.getReg();
1051349cc55cSDimitry Andric       if (GRRegClass.contains(Reg))
1052349cc55cSDimitry Andric         addSavedGPR(MBB, MIB, Reg, true);
1053349cc55cSDimitry Andric     }
1054349cc55cSDimitry Andric   }
1055349cc55cSDimitry Andric 
1056349cc55cSDimitry Andric   // Spill FPRs to the stack in the normal TargetInstrInfo way
10574824e7fdSDimitry Andric   for (const CalleeSavedInfo &I : CSI) {
105804eeddc0SDimitry Andric     Register Reg = I.getReg();
1059349cc55cSDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg)) {
1060349cc55cSDimitry Andric       MBB.addLiveIn(Reg);
10614824e7fdSDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1062349cc55cSDimitry Andric                                &SystemZ::FP64BitRegClass, TRI);
1063349cc55cSDimitry Andric     }
1064349cc55cSDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg)) {
1065349cc55cSDimitry Andric       MBB.addLiveIn(Reg);
10664824e7fdSDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1067349cc55cSDimitry Andric                                &SystemZ::VR128BitRegClass, TRI);
1068349cc55cSDimitry Andric     }
1069349cc55cSDimitry Andric   }
1070349cc55cSDimitry Andric 
1071349cc55cSDimitry Andric   return true;
1072349cc55cSDimitry Andric }
1073349cc55cSDimitry Andric 
10740eae32dcSDimitry Andric bool SystemZXPLINKFrameLowering::restoreCalleeSavedRegisters(
10750eae32dcSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
10760eae32dcSDimitry Andric     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
10770eae32dcSDimitry Andric 
10780eae32dcSDimitry Andric   if (CSI.empty())
10790eae32dcSDimitry Andric     return false;
10800eae32dcSDimitry Andric 
10810eae32dcSDimitry Andric   MachineFunction &MF = *MBB.getParent();
10820eae32dcSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
10830eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
10840eae32dcSDimitry Andric   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
10850eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
10860eae32dcSDimitry Andric 
10870eae32dcSDimitry Andric   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
10880eae32dcSDimitry Andric 
10890eae32dcSDimitry Andric   // Restore FPRs in the normal TargetInstrInfo way.
10900eae32dcSDimitry Andric   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
109104eeddc0SDimitry Andric     Register Reg = CSI[I].getReg();
10920eae32dcSDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg))
10930eae32dcSDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
10940eae32dcSDimitry Andric                                 &SystemZ::FP64BitRegClass, TRI);
10950eae32dcSDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg))
10960eae32dcSDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
10970eae32dcSDimitry Andric                                 &SystemZ::VR128BitRegClass, TRI);
10980eae32dcSDimitry Andric   }
10990eae32dcSDimitry Andric 
11000eae32dcSDimitry Andric   // Restore call-saved GPRs (but not call-clobbered varargs, which at
11010eae32dcSDimitry Andric   // this point might hold return values).
11020eae32dcSDimitry Andric   SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
11030eae32dcSDimitry Andric   if (RestoreGPRs.LowGPR) {
11040eae32dcSDimitry Andric     assert(isInt<20>(Regs.getStackPointerBias() + RestoreGPRs.GPROffset));
11050eae32dcSDimitry Andric     if (RestoreGPRs.LowGPR == RestoreGPRs.HighGPR)
11060eae32dcSDimitry Andric       // Build an LG/L instruction.
11070eae32dcSDimitry Andric       BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LG), RestoreGPRs.LowGPR)
11080eae32dcSDimitry Andric           .addReg(Regs.getStackPointerRegister())
11090eae32dcSDimitry Andric           .addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset)
11100eae32dcSDimitry Andric           .addReg(0);
11110eae32dcSDimitry Andric     else {
11120eae32dcSDimitry Andric       // Build an LMG/LM instruction.
11130eae32dcSDimitry Andric       MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
11140eae32dcSDimitry Andric 
11150eae32dcSDimitry Andric       // Add the explicit register operands.
11160eae32dcSDimitry Andric       MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
11170eae32dcSDimitry Andric       MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
11180eae32dcSDimitry Andric 
11190eae32dcSDimitry Andric       // Add the address.
11200eae32dcSDimitry Andric       MIB.addReg(Regs.getStackPointerRegister());
11210eae32dcSDimitry Andric       MIB.addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset);
11220eae32dcSDimitry Andric 
11230eae32dcSDimitry Andric       // Do a second scan adding regs as being defined by instruction
11240eae32dcSDimitry Andric       for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
112504eeddc0SDimitry Andric         Register Reg = CSI[I].getReg();
11260eae32dcSDimitry Andric         if (Reg > RestoreGPRs.LowGPR && Reg < RestoreGPRs.HighGPR)
11270eae32dcSDimitry Andric           MIB.addReg(Reg, RegState::ImplicitDefine);
11280eae32dcSDimitry Andric       }
11290eae32dcSDimitry Andric     }
11300eae32dcSDimitry Andric   }
11310eae32dcSDimitry Andric 
11320eae32dcSDimitry Andric   return true;
11330eae32dcSDimitry Andric }
11340eae32dcSDimitry Andric 
1135349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitPrologue(MachineFunction &MF,
11360eae32dcSDimitry Andric                                               MachineBasicBlock &MBB) const {
11370eae32dcSDimitry Andric   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
11380eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
11390eae32dcSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
11400eae32dcSDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
11410eae32dcSDimitry Andric   auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
11420eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
11430eae32dcSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
11440eae32dcSDimitry Andric   MachineInstr *StoreInstr = nullptr;
11450eae32dcSDimitry Andric   bool HasFP = hasFP(MF);
11460eae32dcSDimitry Andric   // Debug location must be unknown since the first debug location is used
11470eae32dcSDimitry Andric   // to determine the end of the prologue.
11480eae32dcSDimitry Andric   DebugLoc DL;
11490eae32dcSDimitry Andric   uint64_t Offset = 0;
11500eae32dcSDimitry Andric 
11510eae32dcSDimitry Andric   // TODO: Support leaf functions; only add size of save+reserved area when
11520eae32dcSDimitry Andric   // function is non-leaf.
11530eae32dcSDimitry Andric   MFFrame.setStackSize(MFFrame.getStackSize() + Regs.getCallFrameSize());
11540eae32dcSDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
11550eae32dcSDimitry Andric 
11560eae32dcSDimitry Andric   if (ZFI->getSpillGPRRegs().LowGPR) {
11570eae32dcSDimitry Andric     // Skip over the GPR saves.
11580eae32dcSDimitry Andric     if ((MBBI != MBB.end()) && ((MBBI->getOpcode() == SystemZ::STMG))) {
11590eae32dcSDimitry Andric       const int Operand = 3;
11600eae32dcSDimitry Andric       // Now we can set the offset for the operation, since now the Stack
11610eae32dcSDimitry Andric       // has been finalized.
11620eae32dcSDimitry Andric       Offset = Regs.getStackPointerBias() + MBBI->getOperand(Operand).getImm();
11630eae32dcSDimitry Andric       // Maximum displacement for STMG instruction.
11640eae32dcSDimitry Andric       if (isInt<20>(Offset - StackSize))
11650eae32dcSDimitry Andric         Offset -= StackSize;
11660eae32dcSDimitry Andric       else
11670eae32dcSDimitry Andric         StoreInstr = &*MBBI;
11680eae32dcSDimitry Andric       MBBI->getOperand(Operand).setImm(Offset);
11690eae32dcSDimitry Andric       ++MBBI;
11700eae32dcSDimitry Andric     } else
11710eae32dcSDimitry Andric       llvm_unreachable("Couldn't skip over GPR saves");
11720eae32dcSDimitry Andric   }
11730eae32dcSDimitry Andric 
11740eae32dcSDimitry Andric   if (StackSize) {
11750eae32dcSDimitry Andric     MachineBasicBlock::iterator InsertPt = StoreInstr ? StoreInstr : MBBI;
11760eae32dcSDimitry Andric     // Allocate StackSize bytes.
11770eae32dcSDimitry Andric     int64_t Delta = -int64_t(StackSize);
11780eae32dcSDimitry Andric 
11790eae32dcSDimitry Andric     // In case the STM(G) instruction also stores SP (R4), but the displacement
11800eae32dcSDimitry Andric     // is too large, the SP register is manipulated first before storing,
11810eae32dcSDimitry Andric     // resulting in the wrong value stored and retrieved later. In this case, we
11820eae32dcSDimitry Andric     // need to temporarily save the value of SP, and store it later to memory.
11830eae32dcSDimitry Andric     if (StoreInstr && HasFP) {
11840eae32dcSDimitry Andric       // Insert LR r0,r4 before STMG instruction.
11850eae32dcSDimitry Andric       BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::LGR))
11860eae32dcSDimitry Andric           .addReg(SystemZ::R0D, RegState::Define)
11870eae32dcSDimitry Andric           .addReg(SystemZ::R4D);
11880eae32dcSDimitry Andric       // Insert ST r0,xxx(,r4) after STMG instruction.
11890eae32dcSDimitry Andric       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
11900eae32dcSDimitry Andric           .addReg(SystemZ::R0D, RegState::Kill)
11910eae32dcSDimitry Andric           .addReg(SystemZ::R4D)
11920eae32dcSDimitry Andric           .addImm(Offset)
11930eae32dcSDimitry Andric           .addReg(0);
11940eae32dcSDimitry Andric     }
11950eae32dcSDimitry Andric 
11960eae32dcSDimitry Andric     emitIncrement(MBB, InsertPt, DL, Regs.getStackPointerRegister(), Delta,
11970eae32dcSDimitry Andric                   ZII);
1198*81ad6265SDimitry Andric 
1199*81ad6265SDimitry Andric     // If the requested stack size is larger than the guard page, then we need
1200*81ad6265SDimitry Andric     // to check if we need to call the stack extender. This requires adding a
1201*81ad6265SDimitry Andric     // conditional branch, but splitting the prologue block is not possible at
1202*81ad6265SDimitry Andric     // this point since it would invalidate the SaveBlocks / RestoreBlocks sets
1203*81ad6265SDimitry Andric     // of PEI in the single block function case. Build a pseudo to be handled
1204*81ad6265SDimitry Andric     // later by inlineStackProbe().
1205*81ad6265SDimitry Andric     const uint64_t GuardPageSize = 1024 * 1024;
1206*81ad6265SDimitry Andric     if (StackSize > GuardPageSize) {
1207*81ad6265SDimitry Andric       assert(StoreInstr && "Wrong insertion point");
1208*81ad6265SDimitry Andric       BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::XPLINK_STACKALLOC));
1209*81ad6265SDimitry Andric     }
12100eae32dcSDimitry Andric   }
12110eae32dcSDimitry Andric 
12120eae32dcSDimitry Andric   if (HasFP) {
12130eae32dcSDimitry Andric     // Copy the base of the frame to Frame Pointer Register.
12140eae32dcSDimitry Andric     BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR),
12150eae32dcSDimitry Andric             Regs.getFramePointerRegister())
12160eae32dcSDimitry Andric         .addReg(Regs.getStackPointerRegister());
12170eae32dcSDimitry Andric 
12180eae32dcSDimitry Andric     // Mark the FramePtr as live at the beginning of every block except
12190eae32dcSDimitry Andric     // the entry block.  (We'll have marked R8 as live on entry when
12200eae32dcSDimitry Andric     // saving the GPRs.)
12210eae32dcSDimitry Andric     for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
12220eae32dcSDimitry Andric       I->addLiveIn(Regs.getFramePointerRegister());
12230eae32dcSDimitry Andric   }
12240eae32dcSDimitry Andric }
1225349cc55cSDimitry Andric 
1226349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitEpilogue(MachineFunction &MF,
12270eae32dcSDimitry Andric                                               MachineBasicBlock &MBB) const {
12280eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
12290eae32dcSDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
12300eae32dcSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
12310eae32dcSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
12320eae32dcSDimitry Andric   auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
12330eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
12340eae32dcSDimitry Andric 
12350eae32dcSDimitry Andric   // Skip the return instruction.
12360eae32dcSDimitry Andric   assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
12370eae32dcSDimitry Andric 
12380eae32dcSDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
12390eae32dcSDimitry Andric   if (StackSize) {
12400eae32dcSDimitry Andric     unsigned SPReg = Regs.getStackPointerRegister();
12410eae32dcSDimitry Andric     if (ZFI->getRestoreGPRRegs().LowGPR != SPReg) {
12420eae32dcSDimitry Andric       DebugLoc DL = MBBI->getDebugLoc();
12430eae32dcSDimitry Andric       emitIncrement(MBB, MBBI, DL, SPReg, StackSize, ZII);
12440eae32dcSDimitry Andric     }
12450eae32dcSDimitry Andric   }
12460eae32dcSDimitry Andric }
1247349cc55cSDimitry Andric 
1248*81ad6265SDimitry Andric // Emit a compare of the stack pointer against the stack floor, and a call to
1249*81ad6265SDimitry Andric // the LE stack extender if needed.
1250*81ad6265SDimitry Andric void SystemZXPLINKFrameLowering::inlineStackProbe(
1251*81ad6265SDimitry Andric     MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
1252*81ad6265SDimitry Andric   auto *ZII =
1253*81ad6265SDimitry Andric       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
1254*81ad6265SDimitry Andric 
1255*81ad6265SDimitry Andric   MachineInstr *StackAllocMI = nullptr;
1256*81ad6265SDimitry Andric   for (MachineInstr &MI : PrologMBB)
1257*81ad6265SDimitry Andric     if (MI.getOpcode() == SystemZ::XPLINK_STACKALLOC) {
1258*81ad6265SDimitry Andric       StackAllocMI = &MI;
1259*81ad6265SDimitry Andric       break;
1260*81ad6265SDimitry Andric     }
1261*81ad6265SDimitry Andric   if (StackAllocMI == nullptr)
1262*81ad6265SDimitry Andric     return;
1263*81ad6265SDimitry Andric 
1264*81ad6265SDimitry Andric   MachineBasicBlock &MBB = PrologMBB;
1265*81ad6265SDimitry Andric   const DebugLoc DL = StackAllocMI->getDebugLoc();
1266*81ad6265SDimitry Andric 
1267*81ad6265SDimitry Andric   // The 2nd half of block MBB after split.
1268*81ad6265SDimitry Andric   MachineBasicBlock *NextMBB;
1269*81ad6265SDimitry Andric 
1270*81ad6265SDimitry Andric   // Add new basic block for the call to the stack overflow function.
1271*81ad6265SDimitry Andric   MachineBasicBlock *StackExtMBB =
1272*81ad6265SDimitry Andric       MF.CreateMachineBasicBlock(MBB.getBasicBlock());
1273*81ad6265SDimitry Andric   MF.push_back(StackExtMBB);
1274*81ad6265SDimitry Andric 
1275*81ad6265SDimitry Andric   // LG r3,72(,r3)
1276*81ad6265SDimitry Andric   BuildMI(StackExtMBB, DL, ZII->get(SystemZ::LG), SystemZ::R3D)
1277*81ad6265SDimitry Andric       .addReg(SystemZ::R3D)
1278*81ad6265SDimitry Andric       .addImm(72)
1279*81ad6265SDimitry Andric       .addReg(0);
1280*81ad6265SDimitry Andric   // BASR r3,r3
1281*81ad6265SDimitry Andric   BuildMI(StackExtMBB, DL, ZII->get(SystemZ::CallBASR_STACKEXT))
1282*81ad6265SDimitry Andric       .addReg(SystemZ::R3D);
1283*81ad6265SDimitry Andric 
1284*81ad6265SDimitry Andric   // LLGT r3,1208
1285*81ad6265SDimitry Andric   BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LLGT), SystemZ::R3D)
1286*81ad6265SDimitry Andric       .addReg(0)
1287*81ad6265SDimitry Andric       .addImm(1208)
1288*81ad6265SDimitry Andric       .addReg(0);
1289*81ad6265SDimitry Andric   // CG r4,64(,r3)
1290*81ad6265SDimitry Andric   BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::CG))
1291*81ad6265SDimitry Andric       .addReg(SystemZ::R4D)
1292*81ad6265SDimitry Andric       .addReg(SystemZ::R3D)
1293*81ad6265SDimitry Andric       .addImm(64)
1294*81ad6265SDimitry Andric       .addReg(0);
1295*81ad6265SDimitry Andric   // JLL b'0100',F'37'
1296*81ad6265SDimitry Andric   BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::BRC))
1297*81ad6265SDimitry Andric       .addImm(SystemZ::CCMASK_ICMP)
1298*81ad6265SDimitry Andric       .addImm(SystemZ::CCMASK_CMP_LT)
1299*81ad6265SDimitry Andric       .addMBB(StackExtMBB);
1300*81ad6265SDimitry Andric 
1301*81ad6265SDimitry Andric   NextMBB = SystemZ::splitBlockBefore(StackAllocMI, &MBB);
1302*81ad6265SDimitry Andric   MBB.addSuccessor(NextMBB);
1303*81ad6265SDimitry Andric   MBB.addSuccessor(StackExtMBB);
1304*81ad6265SDimitry Andric 
1305*81ad6265SDimitry Andric   // Add jump back from stack extension BB.
1306*81ad6265SDimitry Andric   BuildMI(StackExtMBB, DL, ZII->get(SystemZ::J)).addMBB(NextMBB);
1307*81ad6265SDimitry Andric   StackExtMBB->addSuccessor(NextMBB);
1308*81ad6265SDimitry Andric 
1309*81ad6265SDimitry Andric   StackAllocMI->eraseFromParent();
1310*81ad6265SDimitry Andric 
1311*81ad6265SDimitry Andric   // Compute the live-in lists for the new blocks.
1312*81ad6265SDimitry Andric   recomputeLiveIns(*NextMBB);
1313*81ad6265SDimitry Andric   recomputeLiveIns(*StackExtMBB);
1314*81ad6265SDimitry Andric }
1315*81ad6265SDimitry Andric 
1316349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {
13170eae32dcSDimitry Andric   return (MF.getFrameInfo().hasVarSizedObjects());
13180eae32dcSDimitry Andric }
13190eae32dcSDimitry Andric 
13200eae32dcSDimitry Andric void SystemZXPLINKFrameLowering::processFunctionBeforeFrameFinalized(
13210eae32dcSDimitry Andric     MachineFunction &MF, RegScavenger *RS) const {
13220eae32dcSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
13230eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
13240eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
13250eae32dcSDimitry Andric 
13260eae32dcSDimitry Andric   // Setup stack frame offset
13270eae32dcSDimitry Andric   MFFrame.setOffsetAdjustment(Regs.getStackPointerBias());
1328349cc55cSDimitry Andric }
1329