xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
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();
9981ad6265SDimitry 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);
304*bdd1243dSDimitry Andric   for (const auto &Entry : ELFSpillOffsetTable)
305*bdd1243dSDimitry Andric     RegSpillOffsets[Entry.Reg] = Entry.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(),
373*bdd1243dSDimitry Andric                                &SystemZ::FP64BitRegClass, TRI, Register());
3740b57cec5SDimitry Andric     }
3750b57cec5SDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg)) {
3760b57cec5SDimitry Andric       MBB.addLiveIn(Reg);
3774824e7fdSDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
378*bdd1243dSDimitry Andric                                &SystemZ::VR128BitRegClass, TRI, Register());
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(),
402*bdd1243dSDimitry Andric                                 &SystemZ::FP64BitRegClass, TRI, Register());
4030b57cec5SDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg))
4044824e7fdSDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
405*bdd1243dSDimitry Andric                                 &SystemZ::VR128BitRegClass, TRI, Register());
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);
909*bdd1243dSDimitry Andric   for (const auto &Entry : XPLINKSpillOffsetTable)
910*bdd1243dSDimitry Andric     RegSpillOffsets[Entry.Reg] = Entry.Offset;
911349cc55cSDimitry Andric }
912349cc55cSDimitry Andric 
913fcaf7f86SDimitry Andric // Checks if the function is a potential candidate for being a XPLeaf routine.
914fcaf7f86SDimitry Andric static bool isXPLeafCandidate(const MachineFunction &MF) {
915fcaf7f86SDimitry Andric   const MachineFrameInfo &MFFrame = MF.getFrameInfo();
916fcaf7f86SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
917fcaf7f86SDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
918fcaf7f86SDimitry Andric   auto *Regs =
919fcaf7f86SDimitry Andric       static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
920fcaf7f86SDimitry Andric 
921fcaf7f86SDimitry Andric   // If function calls other functions including alloca, then it is not a XPLeaf
922fcaf7f86SDimitry Andric   // routine.
923fcaf7f86SDimitry Andric   if (MFFrame.hasCalls())
924fcaf7f86SDimitry Andric     return false;
925fcaf7f86SDimitry Andric 
926fcaf7f86SDimitry Andric   // If the function has var Sized Objects, then it is not a XPLeaf routine.
927fcaf7f86SDimitry Andric   if (MFFrame.hasVarSizedObjects())
928fcaf7f86SDimitry Andric     return false;
929fcaf7f86SDimitry Andric 
930fcaf7f86SDimitry Andric   // If the function adjusts the stack, then it is not a XPLeaf routine.
931fcaf7f86SDimitry Andric   if (MFFrame.adjustsStack())
932fcaf7f86SDimitry Andric     return false;
933fcaf7f86SDimitry Andric 
934fcaf7f86SDimitry Andric   // If function modifies the stack pointer register, then it is not a XPLeaf
935fcaf7f86SDimitry Andric   // routine.
936fcaf7f86SDimitry Andric   if (MRI.isPhysRegModified(Regs->getStackPointerRegister()))
937fcaf7f86SDimitry Andric     return false;
938fcaf7f86SDimitry Andric 
939fcaf7f86SDimitry Andric   // If function modifies the ADA register, then it is not a XPLeaf routine.
940fcaf7f86SDimitry Andric   if (MRI.isPhysRegModified(Regs->getAddressOfCalleeRegister()))
941fcaf7f86SDimitry Andric     return false;
942fcaf7f86SDimitry Andric 
943fcaf7f86SDimitry Andric   // If function modifies the return address register, then it is not a XPLeaf
944fcaf7f86SDimitry Andric   // routine.
945fcaf7f86SDimitry Andric   if (MRI.isPhysRegModified(Regs->getReturnFunctionAddressRegister()))
946fcaf7f86SDimitry Andric     return false;
947fcaf7f86SDimitry Andric 
948fcaf7f86SDimitry Andric   // If the backchain pointer should be stored, then it is not a XPLeaf routine.
949fcaf7f86SDimitry Andric   if (MF.getFunction().hasFnAttribute("backchain"))
950fcaf7f86SDimitry Andric     return false;
951fcaf7f86SDimitry Andric 
952fcaf7f86SDimitry Andric   // If function acquires its own stack frame, then it is not a XPLeaf routine.
953fcaf7f86SDimitry Andric   // At the time this function is called, only slots for local variables are
954fcaf7f86SDimitry Andric   // allocated, so this is a very rough estimate.
955fcaf7f86SDimitry Andric   if (MFFrame.estimateStackSize(MF) > 0)
956fcaf7f86SDimitry Andric     return false;
957fcaf7f86SDimitry Andric 
958fcaf7f86SDimitry Andric   return true;
959fcaf7f86SDimitry Andric }
960fcaf7f86SDimitry Andric 
961349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::assignCalleeSavedSpillSlots(
962349cc55cSDimitry Andric     MachineFunction &MF, const TargetRegisterInfo *TRI,
963349cc55cSDimitry Andric     std::vector<CalleeSavedInfo> &CSI) const {
964349cc55cSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
965349cc55cSDimitry Andric   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
966349cc55cSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
967349cc55cSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
968753f127fSDimitry Andric   auto &GRRegClass = SystemZ::GR64BitRegClass;
969753f127fSDimitry Andric 
970fcaf7f86SDimitry Andric   // At this point, the result of isXPLeafCandidate() is not accurate because
971fcaf7f86SDimitry Andric   // the size of the save area has not yet been determined. If
972fcaf7f86SDimitry Andric   // isXPLeafCandidate() indicates a potential leaf function, and there are no
973fcaf7f86SDimitry Andric   // callee-save registers, then it is indeed a leaf function, and we can early
974fcaf7f86SDimitry Andric   // exit.
975fcaf7f86SDimitry Andric   // TODO: It is possible for leaf functions to use callee-saved registers.
976fcaf7f86SDimitry Andric   // It can use the 0-2k range between R4 and the caller's stack frame without
977fcaf7f86SDimitry Andric   // acquiring its own stack frame.
978fcaf7f86SDimitry Andric   bool IsLeaf = CSI.empty() && isXPLeafCandidate(MF);
979fcaf7f86SDimitry Andric   if (IsLeaf)
980fcaf7f86SDimitry Andric     return true;
981fcaf7f86SDimitry Andric 
982753f127fSDimitry Andric   // For non-leaf functions:
983753f127fSDimitry Andric   // - the address of callee (entry point) register R6 must be saved
984753f127fSDimitry Andric   CSI.push_back(CalleeSavedInfo(Regs.getAddressOfCalleeRegister()));
985753f127fSDimitry Andric   CSI.back().setRestored(false);
986753f127fSDimitry Andric 
987753f127fSDimitry Andric   // The return address register R7 must be saved and restored.
988753f127fSDimitry Andric   CSI.push_back(CalleeSavedInfo(Regs.getReturnFunctionAddressRegister()));
989753f127fSDimitry Andric 
990753f127fSDimitry Andric   // If the function needs a frame pointer, or if the backchain pointer should
991753f127fSDimitry Andric   // be stored, then save the stack pointer register R4.
992753f127fSDimitry Andric   if (hasFP(MF) || MF.getFunction().hasFnAttribute("backchain"))
993753f127fSDimitry Andric     CSI.push_back(CalleeSavedInfo(Regs.getStackPointerRegister()));
994349cc55cSDimitry Andric 
995349cc55cSDimitry Andric   // Scan the call-saved GPRs and find the bounds of the register spill area.
996753f127fSDimitry Andric   Register LowRestoreGPR = 0;
997753f127fSDimitry Andric   int LowRestoreOffset = INT32_MAX;
998753f127fSDimitry Andric   Register LowSpillGPR = 0;
999753f127fSDimitry Andric   int LowSpillOffset = INT32_MAX;
1000753f127fSDimitry Andric   Register HighGPR = 0;
1001349cc55cSDimitry Andric   int HighOffset = -1;
1002349cc55cSDimitry Andric 
1003753f127fSDimitry Andric   for (auto &CS : CSI) {
100404eeddc0SDimitry Andric     Register Reg = CS.getReg();
1005349cc55cSDimitry Andric     int Offset = RegSpillOffsets[Reg];
1006349cc55cSDimitry Andric     if (Offset >= 0) {
1007349cc55cSDimitry Andric       if (GRRegClass.contains(Reg)) {
1008753f127fSDimitry Andric         if (LowSpillOffset > Offset) {
1009753f127fSDimitry Andric           LowSpillOffset = Offset;
1010753f127fSDimitry Andric           LowSpillGPR = Reg;
1011753f127fSDimitry Andric         }
1012753f127fSDimitry Andric         if (CS.isRestored() && LowRestoreOffset > Offset) {
1013753f127fSDimitry Andric           LowRestoreOffset = Offset;
1014753f127fSDimitry Andric           LowRestoreGPR = Reg;
1015349cc55cSDimitry Andric         }
1016349cc55cSDimitry Andric 
1017349cc55cSDimitry Andric         if (Offset > HighOffset) {
1018349cc55cSDimitry Andric           HighOffset = Offset;
1019349cc55cSDimitry Andric           HighGPR = Reg;
1020349cc55cSDimitry Andric         }
1021753f127fSDimitry Andric         // Non-volatile GPRs are saved in the dedicated register save area at
1022753f127fSDimitry Andric         // the bottom of the stack and are not truly part of the "normal" stack
1023753f127fSDimitry Andric         // frame. Mark the frame index as NoAlloc to indicate it as such.
1024753f127fSDimitry Andric         unsigned RegSize = 8;
1025349cc55cSDimitry Andric         int FrameIdx = MFFrame.CreateFixedSpillStackObject(RegSize, Offset);
1026349cc55cSDimitry Andric         CS.setFrameIdx(FrameIdx);
1027753f127fSDimitry Andric         MFFrame.setStackID(FrameIdx, TargetStackID::NoAlloc);
1028349cc55cSDimitry Andric       }
1029753f127fSDimitry Andric     } else {
103004eeddc0SDimitry Andric       Register Reg = CS.getReg();
1031349cc55cSDimitry Andric       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1032349cc55cSDimitry Andric       Align Alignment = TRI->getSpillAlign(*RC);
1033349cc55cSDimitry Andric       unsigned Size = TRI->getSpillSize(*RC);
1034349cc55cSDimitry Andric       Alignment = std::min(Alignment, getStackAlign());
1035349cc55cSDimitry Andric       int FrameIdx = MFFrame.CreateStackObject(Size, Alignment, true);
1036349cc55cSDimitry Andric       CS.setFrameIdx(FrameIdx);
1037349cc55cSDimitry Andric     }
1038753f127fSDimitry Andric   }
1039753f127fSDimitry Andric 
1040753f127fSDimitry Andric   // Save the range of call-saved registers, for use by the
1041753f127fSDimitry Andric   // prologue/epilogue inserters.
1042753f127fSDimitry Andric   if (LowRestoreGPR)
1043753f127fSDimitry Andric     MFI->setRestoreGPRRegs(LowRestoreGPR, HighGPR, LowRestoreOffset);
1044753f127fSDimitry Andric 
1045753f127fSDimitry Andric   // Save the range of call-saved registers, for use by the epilogue inserter.
1046753f127fSDimitry Andric   assert(LowSpillGPR && "Expected registers to spill");
1047753f127fSDimitry Andric   MFI->setSpillGPRRegs(LowSpillGPR, HighGPR, LowSpillOffset);
1048349cc55cSDimitry Andric 
1049349cc55cSDimitry Andric   return true;
1050349cc55cSDimitry Andric }
1051349cc55cSDimitry Andric 
1052349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::determineCalleeSaves(MachineFunction &MF,
1053349cc55cSDimitry Andric                                                       BitVector &SavedRegs,
1054349cc55cSDimitry Andric                                                       RegScavenger *RS) const {
1055349cc55cSDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1056349cc55cSDimitry Andric 
1057349cc55cSDimitry Andric   bool HasFP = hasFP(MF);
1058349cc55cSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1059349cc55cSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1060349cc55cSDimitry Andric 
1061349cc55cSDimitry Andric   // If the function requires a frame pointer, record that the hard
1062349cc55cSDimitry Andric   // frame pointer will be clobbered.
1063349cc55cSDimitry Andric   if (HasFP)
1064349cc55cSDimitry Andric     SavedRegs.set(Regs.getFramePointerRegister());
1065349cc55cSDimitry Andric }
1066349cc55cSDimitry Andric 
1067349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::spillCalleeSavedRegisters(
1068349cc55cSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
1069349cc55cSDimitry Andric     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
1070349cc55cSDimitry Andric   if (CSI.empty())
1071349cc55cSDimitry Andric     return true;
1072349cc55cSDimitry Andric 
1073349cc55cSDimitry Andric   MachineFunction &MF = *MBB.getParent();
1074349cc55cSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
1075349cc55cSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1076349cc55cSDimitry Andric   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1077349cc55cSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
1078349cc55cSDimitry Andric   SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
1079349cc55cSDimitry Andric   DebugLoc DL;
1080349cc55cSDimitry Andric 
1081349cc55cSDimitry Andric   // Save GPRs
1082349cc55cSDimitry Andric   if (SpillGPRs.LowGPR) {
1083349cc55cSDimitry Andric     assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
1084349cc55cSDimitry Andric            "Should be saving multiple registers");
1085349cc55cSDimitry Andric 
1086349cc55cSDimitry Andric     // Build an STM/STMG instruction.
1087349cc55cSDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
1088349cc55cSDimitry Andric 
1089349cc55cSDimitry Andric     // Add the explicit register operands.
1090349cc55cSDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
1091349cc55cSDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
1092349cc55cSDimitry Andric 
1093349cc55cSDimitry Andric     // Add the address r4
1094349cc55cSDimitry Andric     MIB.addReg(Regs.getStackPointerRegister());
1095349cc55cSDimitry Andric 
1096349cc55cSDimitry Andric     // Add the partial offset
1097349cc55cSDimitry Andric     // We cannot add the actual offset as, at the stack is not finalized
1098349cc55cSDimitry Andric     MIB.addImm(SpillGPRs.GPROffset);
1099349cc55cSDimitry Andric 
1100349cc55cSDimitry Andric     // Make sure all call-saved GPRs are included as operands and are
1101349cc55cSDimitry Andric     // marked as live on entry.
1102349cc55cSDimitry Andric     auto &GRRegClass = SystemZ::GR64BitRegClass;
11034824e7fdSDimitry Andric     for (const CalleeSavedInfo &I : CSI) {
110404eeddc0SDimitry Andric       Register Reg = I.getReg();
1105349cc55cSDimitry Andric       if (GRRegClass.contains(Reg))
1106349cc55cSDimitry Andric         addSavedGPR(MBB, MIB, Reg, true);
1107349cc55cSDimitry Andric     }
1108349cc55cSDimitry Andric   }
1109349cc55cSDimitry Andric 
1110349cc55cSDimitry Andric   // Spill FPRs to the stack in the normal TargetInstrInfo way
11114824e7fdSDimitry Andric   for (const CalleeSavedInfo &I : CSI) {
111204eeddc0SDimitry Andric     Register Reg = I.getReg();
1113349cc55cSDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg)) {
1114349cc55cSDimitry Andric       MBB.addLiveIn(Reg);
11154824e7fdSDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1116*bdd1243dSDimitry Andric                                &SystemZ::FP64BitRegClass, TRI, Register());
1117349cc55cSDimitry Andric     }
1118349cc55cSDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg)) {
1119349cc55cSDimitry Andric       MBB.addLiveIn(Reg);
11204824e7fdSDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, I.getFrameIdx(),
1121*bdd1243dSDimitry Andric                                &SystemZ::VR128BitRegClass, TRI, Register());
1122349cc55cSDimitry Andric     }
1123349cc55cSDimitry Andric   }
1124349cc55cSDimitry Andric 
1125349cc55cSDimitry Andric   return true;
1126349cc55cSDimitry Andric }
1127349cc55cSDimitry Andric 
11280eae32dcSDimitry Andric bool SystemZXPLINKFrameLowering::restoreCalleeSavedRegisters(
11290eae32dcSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
11300eae32dcSDimitry Andric     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
11310eae32dcSDimitry Andric 
11320eae32dcSDimitry Andric   if (CSI.empty())
11330eae32dcSDimitry Andric     return false;
11340eae32dcSDimitry Andric 
11350eae32dcSDimitry Andric   MachineFunction &MF = *MBB.getParent();
11360eae32dcSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
11370eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
11380eae32dcSDimitry Andric   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
11390eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
11400eae32dcSDimitry Andric 
11410eae32dcSDimitry Andric   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
11420eae32dcSDimitry Andric 
11430eae32dcSDimitry Andric   // Restore FPRs in the normal TargetInstrInfo way.
1144*bdd1243dSDimitry Andric   for (const CalleeSavedInfo &I : CSI) {
1145*bdd1243dSDimitry Andric     Register Reg = I.getReg();
11460eae32dcSDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg))
1147*bdd1243dSDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1148*bdd1243dSDimitry Andric                                 &SystemZ::FP64BitRegClass, TRI, Register());
11490eae32dcSDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg))
1150*bdd1243dSDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, I.getFrameIdx(),
1151*bdd1243dSDimitry Andric                                 &SystemZ::VR128BitRegClass, TRI, Register());
11520eae32dcSDimitry Andric   }
11530eae32dcSDimitry Andric 
11540eae32dcSDimitry Andric   // Restore call-saved GPRs (but not call-clobbered varargs, which at
11550eae32dcSDimitry Andric   // this point might hold return values).
11560eae32dcSDimitry Andric   SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
11570eae32dcSDimitry Andric   if (RestoreGPRs.LowGPR) {
11580eae32dcSDimitry Andric     assert(isInt<20>(Regs.getStackPointerBias() + RestoreGPRs.GPROffset));
11590eae32dcSDimitry Andric     if (RestoreGPRs.LowGPR == RestoreGPRs.HighGPR)
11600eae32dcSDimitry Andric       // Build an LG/L instruction.
11610eae32dcSDimitry Andric       BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LG), RestoreGPRs.LowGPR)
11620eae32dcSDimitry Andric           .addReg(Regs.getStackPointerRegister())
11630eae32dcSDimitry Andric           .addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset)
11640eae32dcSDimitry Andric           .addReg(0);
11650eae32dcSDimitry Andric     else {
11660eae32dcSDimitry Andric       // Build an LMG/LM instruction.
11670eae32dcSDimitry Andric       MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
11680eae32dcSDimitry Andric 
11690eae32dcSDimitry Andric       // Add the explicit register operands.
11700eae32dcSDimitry Andric       MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
11710eae32dcSDimitry Andric       MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
11720eae32dcSDimitry Andric 
11730eae32dcSDimitry Andric       // Add the address.
11740eae32dcSDimitry Andric       MIB.addReg(Regs.getStackPointerRegister());
11750eae32dcSDimitry Andric       MIB.addImm(Regs.getStackPointerBias() + RestoreGPRs.GPROffset);
11760eae32dcSDimitry Andric 
11770eae32dcSDimitry Andric       // Do a second scan adding regs as being defined by instruction
1178*bdd1243dSDimitry Andric       for (const CalleeSavedInfo &I : CSI) {
1179*bdd1243dSDimitry Andric         Register Reg = I.getReg();
11800eae32dcSDimitry Andric         if (Reg > RestoreGPRs.LowGPR && Reg < RestoreGPRs.HighGPR)
11810eae32dcSDimitry Andric           MIB.addReg(Reg, RegState::ImplicitDefine);
11820eae32dcSDimitry Andric       }
11830eae32dcSDimitry Andric     }
11840eae32dcSDimitry Andric   }
11850eae32dcSDimitry Andric 
11860eae32dcSDimitry Andric   return true;
11870eae32dcSDimitry Andric }
11880eae32dcSDimitry Andric 
1189349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitPrologue(MachineFunction &MF,
11900eae32dcSDimitry Andric                                               MachineBasicBlock &MBB) const {
11910eae32dcSDimitry Andric   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
11920eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
11930eae32dcSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
11940eae32dcSDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
11950eae32dcSDimitry Andric   auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
11960eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
11970eae32dcSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
11980eae32dcSDimitry Andric   MachineInstr *StoreInstr = nullptr;
1199fcaf7f86SDimitry Andric 
1200fcaf7f86SDimitry Andric   determineFrameLayout(MF);
1201fcaf7f86SDimitry Andric 
12020eae32dcSDimitry Andric   bool HasFP = hasFP(MF);
12030eae32dcSDimitry Andric   // Debug location must be unknown since the first debug location is used
12040eae32dcSDimitry Andric   // to determine the end of the prologue.
12050eae32dcSDimitry Andric   DebugLoc DL;
12060eae32dcSDimitry Andric   uint64_t Offset = 0;
12070eae32dcSDimitry Andric 
1208fcaf7f86SDimitry Andric   const uint64_t StackSize = MFFrame.getStackSize();
12090eae32dcSDimitry Andric 
12100eae32dcSDimitry Andric   if (ZFI->getSpillGPRRegs().LowGPR) {
12110eae32dcSDimitry Andric     // Skip over the GPR saves.
12120eae32dcSDimitry Andric     if ((MBBI != MBB.end()) && ((MBBI->getOpcode() == SystemZ::STMG))) {
12130eae32dcSDimitry Andric       const int Operand = 3;
12140eae32dcSDimitry Andric       // Now we can set the offset for the operation, since now the Stack
12150eae32dcSDimitry Andric       // has been finalized.
12160eae32dcSDimitry Andric       Offset = Regs.getStackPointerBias() + MBBI->getOperand(Operand).getImm();
12170eae32dcSDimitry Andric       // Maximum displacement for STMG instruction.
12180eae32dcSDimitry Andric       if (isInt<20>(Offset - StackSize))
12190eae32dcSDimitry Andric         Offset -= StackSize;
12200eae32dcSDimitry Andric       else
12210eae32dcSDimitry Andric         StoreInstr = &*MBBI;
12220eae32dcSDimitry Andric       MBBI->getOperand(Operand).setImm(Offset);
12230eae32dcSDimitry Andric       ++MBBI;
12240eae32dcSDimitry Andric     } else
12250eae32dcSDimitry Andric       llvm_unreachable("Couldn't skip over GPR saves");
12260eae32dcSDimitry Andric   }
12270eae32dcSDimitry Andric 
12280eae32dcSDimitry Andric   if (StackSize) {
12290eae32dcSDimitry Andric     MachineBasicBlock::iterator InsertPt = StoreInstr ? StoreInstr : MBBI;
12300eae32dcSDimitry Andric     // Allocate StackSize bytes.
12310eae32dcSDimitry Andric     int64_t Delta = -int64_t(StackSize);
12320eae32dcSDimitry Andric 
12330eae32dcSDimitry Andric     // In case the STM(G) instruction also stores SP (R4), but the displacement
12340eae32dcSDimitry Andric     // is too large, the SP register is manipulated first before storing,
12350eae32dcSDimitry Andric     // resulting in the wrong value stored and retrieved later. In this case, we
12360eae32dcSDimitry Andric     // need to temporarily save the value of SP, and store it later to memory.
12370eae32dcSDimitry Andric     if (StoreInstr && HasFP) {
12380eae32dcSDimitry Andric       // Insert LR r0,r4 before STMG instruction.
12390eae32dcSDimitry Andric       BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::LGR))
12400eae32dcSDimitry Andric           .addReg(SystemZ::R0D, RegState::Define)
12410eae32dcSDimitry Andric           .addReg(SystemZ::R4D);
12420eae32dcSDimitry Andric       // Insert ST r0,xxx(,r4) after STMG instruction.
12430eae32dcSDimitry Andric       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
12440eae32dcSDimitry Andric           .addReg(SystemZ::R0D, RegState::Kill)
12450eae32dcSDimitry Andric           .addReg(SystemZ::R4D)
12460eae32dcSDimitry Andric           .addImm(Offset)
12470eae32dcSDimitry Andric           .addReg(0);
12480eae32dcSDimitry Andric     }
12490eae32dcSDimitry Andric 
12500eae32dcSDimitry Andric     emitIncrement(MBB, InsertPt, DL, Regs.getStackPointerRegister(), Delta,
12510eae32dcSDimitry Andric                   ZII);
125281ad6265SDimitry Andric 
125381ad6265SDimitry Andric     // If the requested stack size is larger than the guard page, then we need
125481ad6265SDimitry Andric     // to check if we need to call the stack extender. This requires adding a
125581ad6265SDimitry Andric     // conditional branch, but splitting the prologue block is not possible at
125681ad6265SDimitry Andric     // this point since it would invalidate the SaveBlocks / RestoreBlocks sets
125781ad6265SDimitry Andric     // of PEI in the single block function case. Build a pseudo to be handled
125881ad6265SDimitry Andric     // later by inlineStackProbe().
125981ad6265SDimitry Andric     const uint64_t GuardPageSize = 1024 * 1024;
126081ad6265SDimitry Andric     if (StackSize > GuardPageSize) {
126181ad6265SDimitry Andric       assert(StoreInstr && "Wrong insertion point");
126281ad6265SDimitry Andric       BuildMI(MBB, InsertPt, DL, ZII->get(SystemZ::XPLINK_STACKALLOC));
126381ad6265SDimitry Andric     }
12640eae32dcSDimitry Andric   }
12650eae32dcSDimitry Andric 
12660eae32dcSDimitry Andric   if (HasFP) {
12670eae32dcSDimitry Andric     // Copy the base of the frame to Frame Pointer Register.
12680eae32dcSDimitry Andric     BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR),
12690eae32dcSDimitry Andric             Regs.getFramePointerRegister())
12700eae32dcSDimitry Andric         .addReg(Regs.getStackPointerRegister());
12710eae32dcSDimitry Andric 
12720eae32dcSDimitry Andric     // Mark the FramePtr as live at the beginning of every block except
12730eae32dcSDimitry Andric     // the entry block.  (We'll have marked R8 as live on entry when
12740eae32dcSDimitry Andric     // saving the GPRs.)
1275fcaf7f86SDimitry Andric     for (MachineBasicBlock &B : llvm::drop_begin(MF))
1276fcaf7f86SDimitry Andric       B.addLiveIn(Regs.getFramePointerRegister());
12770eae32dcSDimitry Andric   }
12780eae32dcSDimitry Andric }
1279349cc55cSDimitry Andric 
1280349cc55cSDimitry Andric void SystemZXPLINKFrameLowering::emitEpilogue(MachineFunction &MF,
12810eae32dcSDimitry Andric                                               MachineBasicBlock &MBB) const {
12820eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
12830eae32dcSDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
12840eae32dcSDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
12850eae32dcSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
12860eae32dcSDimitry Andric   auto *ZII = static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
12870eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
12880eae32dcSDimitry Andric 
12890eae32dcSDimitry Andric   // Skip the return instruction.
12900eae32dcSDimitry Andric   assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
12910eae32dcSDimitry Andric 
12920eae32dcSDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
12930eae32dcSDimitry Andric   if (StackSize) {
12940eae32dcSDimitry Andric     unsigned SPReg = Regs.getStackPointerRegister();
12950eae32dcSDimitry Andric     if (ZFI->getRestoreGPRRegs().LowGPR != SPReg) {
12960eae32dcSDimitry Andric       DebugLoc DL = MBBI->getDebugLoc();
12970eae32dcSDimitry Andric       emitIncrement(MBB, MBBI, DL, SPReg, StackSize, ZII);
12980eae32dcSDimitry Andric     }
12990eae32dcSDimitry Andric   }
13000eae32dcSDimitry Andric }
1301349cc55cSDimitry Andric 
130281ad6265SDimitry Andric // Emit a compare of the stack pointer against the stack floor, and a call to
130381ad6265SDimitry Andric // the LE stack extender if needed.
130481ad6265SDimitry Andric void SystemZXPLINKFrameLowering::inlineStackProbe(
130581ad6265SDimitry Andric     MachineFunction &MF, MachineBasicBlock &PrologMBB) const {
130681ad6265SDimitry Andric   auto *ZII =
130781ad6265SDimitry Andric       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
130881ad6265SDimitry Andric 
130981ad6265SDimitry Andric   MachineInstr *StackAllocMI = nullptr;
131081ad6265SDimitry Andric   for (MachineInstr &MI : PrologMBB)
131181ad6265SDimitry Andric     if (MI.getOpcode() == SystemZ::XPLINK_STACKALLOC) {
131281ad6265SDimitry Andric       StackAllocMI = &MI;
131381ad6265SDimitry Andric       break;
131481ad6265SDimitry Andric     }
131581ad6265SDimitry Andric   if (StackAllocMI == nullptr)
131681ad6265SDimitry Andric     return;
131781ad6265SDimitry Andric 
131881ad6265SDimitry Andric   MachineBasicBlock &MBB = PrologMBB;
131981ad6265SDimitry Andric   const DebugLoc DL = StackAllocMI->getDebugLoc();
132081ad6265SDimitry Andric 
132181ad6265SDimitry Andric   // The 2nd half of block MBB after split.
132281ad6265SDimitry Andric   MachineBasicBlock *NextMBB;
132381ad6265SDimitry Andric 
132481ad6265SDimitry Andric   // Add new basic block for the call to the stack overflow function.
132581ad6265SDimitry Andric   MachineBasicBlock *StackExtMBB =
132681ad6265SDimitry Andric       MF.CreateMachineBasicBlock(MBB.getBasicBlock());
132781ad6265SDimitry Andric   MF.push_back(StackExtMBB);
132881ad6265SDimitry Andric 
132981ad6265SDimitry Andric   // LG r3,72(,r3)
133081ad6265SDimitry Andric   BuildMI(StackExtMBB, DL, ZII->get(SystemZ::LG), SystemZ::R3D)
133181ad6265SDimitry Andric       .addReg(SystemZ::R3D)
133281ad6265SDimitry Andric       .addImm(72)
133381ad6265SDimitry Andric       .addReg(0);
133481ad6265SDimitry Andric   // BASR r3,r3
133581ad6265SDimitry Andric   BuildMI(StackExtMBB, DL, ZII->get(SystemZ::CallBASR_STACKEXT))
133681ad6265SDimitry Andric       .addReg(SystemZ::R3D);
133781ad6265SDimitry Andric 
133881ad6265SDimitry Andric   // LLGT r3,1208
133981ad6265SDimitry Andric   BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::LLGT), SystemZ::R3D)
134081ad6265SDimitry Andric       .addReg(0)
134181ad6265SDimitry Andric       .addImm(1208)
134281ad6265SDimitry Andric       .addReg(0);
134381ad6265SDimitry Andric   // CG r4,64(,r3)
134481ad6265SDimitry Andric   BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::CG))
134581ad6265SDimitry Andric       .addReg(SystemZ::R4D)
134681ad6265SDimitry Andric       .addReg(SystemZ::R3D)
134781ad6265SDimitry Andric       .addImm(64)
134881ad6265SDimitry Andric       .addReg(0);
134981ad6265SDimitry Andric   // JLL b'0100',F'37'
135081ad6265SDimitry Andric   BuildMI(MBB, StackAllocMI, DL, ZII->get(SystemZ::BRC))
135181ad6265SDimitry Andric       .addImm(SystemZ::CCMASK_ICMP)
135281ad6265SDimitry Andric       .addImm(SystemZ::CCMASK_CMP_LT)
135381ad6265SDimitry Andric       .addMBB(StackExtMBB);
135481ad6265SDimitry Andric 
135581ad6265SDimitry Andric   NextMBB = SystemZ::splitBlockBefore(StackAllocMI, &MBB);
135681ad6265SDimitry Andric   MBB.addSuccessor(NextMBB);
135781ad6265SDimitry Andric   MBB.addSuccessor(StackExtMBB);
135881ad6265SDimitry Andric 
135981ad6265SDimitry Andric   // Add jump back from stack extension BB.
136081ad6265SDimitry Andric   BuildMI(StackExtMBB, DL, ZII->get(SystemZ::J)).addMBB(NextMBB);
136181ad6265SDimitry Andric   StackExtMBB->addSuccessor(NextMBB);
136281ad6265SDimitry Andric 
136381ad6265SDimitry Andric   StackAllocMI->eraseFromParent();
136481ad6265SDimitry Andric 
136581ad6265SDimitry Andric   // Compute the live-in lists for the new blocks.
136681ad6265SDimitry Andric   recomputeLiveIns(*NextMBB);
136781ad6265SDimitry Andric   recomputeLiveIns(*StackExtMBB);
136881ad6265SDimitry Andric }
136981ad6265SDimitry Andric 
1370349cc55cSDimitry Andric bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {
13710eae32dcSDimitry Andric   return (MF.getFrameInfo().hasVarSizedObjects());
13720eae32dcSDimitry Andric }
13730eae32dcSDimitry Andric 
13740eae32dcSDimitry Andric void SystemZXPLINKFrameLowering::processFunctionBeforeFrameFinalized(
13750eae32dcSDimitry Andric     MachineFunction &MF, RegScavenger *RS) const {
13760eae32dcSDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
13770eae32dcSDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
13780eae32dcSDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
13790eae32dcSDimitry Andric 
13800eae32dcSDimitry Andric   // Setup stack frame offset
13810eae32dcSDimitry Andric   MFFrame.setOffsetAdjustment(Regs.getStackPointerBias());
1382349cc55cSDimitry Andric }
1383fcaf7f86SDimitry Andric 
1384fcaf7f86SDimitry Andric // Determines the size of the frame, and creates the deferred spill objects.
1385fcaf7f86SDimitry Andric void SystemZXPLINKFrameLowering::determineFrameLayout(
1386fcaf7f86SDimitry Andric     MachineFunction &MF) const {
1387fcaf7f86SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
1388fcaf7f86SDimitry Andric   const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
1389fcaf7f86SDimitry Andric   auto *Regs =
1390fcaf7f86SDimitry Andric       static_cast<SystemZXPLINK64Registers *>(Subtarget.getSpecialRegisters());
1391fcaf7f86SDimitry Andric 
1392fcaf7f86SDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
1393fcaf7f86SDimitry Andric   if (StackSize == 0)
1394fcaf7f86SDimitry Andric     return;
1395fcaf7f86SDimitry Andric 
1396fcaf7f86SDimitry Andric   // Add the size of the register save area and the reserved area to the size.
1397fcaf7f86SDimitry Andric   StackSize += Regs->getCallFrameSize();
1398fcaf7f86SDimitry Andric   MFFrame.setStackSize(StackSize);
1399fcaf7f86SDimitry Andric 
1400fcaf7f86SDimitry Andric   // We now know the stack size. Create the fixed spill stack objects for the
1401fcaf7f86SDimitry Andric   // register save area now. This has no impact on the stack frame layout, as
1402fcaf7f86SDimitry Andric   // this is already computed. However, it makes sure that all callee saved
1403fcaf7f86SDimitry Andric   // registers have a valid frame index assigned.
1404fcaf7f86SDimitry Andric   const unsigned RegSize = MF.getDataLayout().getPointerSize();
1405fcaf7f86SDimitry Andric   for (auto &CS : MFFrame.getCalleeSavedInfo()) {
1406fcaf7f86SDimitry Andric     int Offset = RegSpillOffsets[CS.getReg()];
1407fcaf7f86SDimitry Andric     if (Offset >= 0)
1408fcaf7f86SDimitry Andric       CS.setFrameIdx(
1409fcaf7f86SDimitry Andric           MFFrame.CreateFixedSpillStackObject(RegSize, Offset - StackSize));
1410fcaf7f86SDimitry Andric   }
1411fcaf7f86SDimitry Andric }
1412