15f613dfdSUlrich Weigand //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===//
25f613dfdSUlrich Weigand //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f613dfdSUlrich Weigand //
75f613dfdSUlrich Weigand //===----------------------------------------------------------------------===//
85f613dfdSUlrich Weigand 
95f613dfdSUlrich Weigand #include "SystemZFrameLowering.h"
105f613dfdSUlrich Weigand #include "SystemZCallingConv.h"
115f613dfdSUlrich Weigand #include "SystemZInstrBuilder.h"
12f1bd22dfSEric Christopher #include "SystemZInstrInfo.h"
135f613dfdSUlrich Weigand #include "SystemZMachineFunctionInfo.h"
14f1bd22dfSEric Christopher #include "SystemZRegisterInfo.h"
15d913448bSEric Christopher #include "SystemZSubtarget.h"
165f613dfdSUlrich Weigand #include "llvm/CodeGen/MachineModuleInfo.h"
175f613dfdSUlrich Weigand #include "llvm/CodeGen/MachineRegisterInfo.h"
185dd52f8cSRichard Sandiford #include "llvm/CodeGen/RegisterScavenging.h"
195f613dfdSUlrich Weigand #include "llvm/IR/Function.h"
205f613dfdSUlrich Weigand 
215f613dfdSUlrich Weigand using namespace llvm;
225f613dfdSUlrich Weigand 
23db39b4a2SRichard Sandiford namespace {
245f613dfdSUlrich Weigand // The ABI-defined register save slots, relative to the incoming stack
255f613dfdSUlrich Weigand // pointer.
26db39b4a2SRichard Sandiford static const TargetFrameLowering::SpillSlot SpillOffsetTable[] = {
275f613dfdSUlrich Weigand   { SystemZ::R2D,  0x10 },
285f613dfdSUlrich Weigand   { SystemZ::R3D,  0x18 },
295f613dfdSUlrich Weigand   { SystemZ::R4D,  0x20 },
305f613dfdSUlrich Weigand   { SystemZ::R5D,  0x28 },
315f613dfdSUlrich Weigand   { SystemZ::R6D,  0x30 },
325f613dfdSUlrich Weigand   { SystemZ::R7D,  0x38 },
335f613dfdSUlrich Weigand   { SystemZ::R8D,  0x40 },
345f613dfdSUlrich Weigand   { SystemZ::R9D,  0x48 },
355f613dfdSUlrich Weigand   { SystemZ::R10D, 0x50 },
365f613dfdSUlrich Weigand   { SystemZ::R11D, 0x58 },
375f613dfdSUlrich Weigand   { SystemZ::R12D, 0x60 },
385f613dfdSUlrich Weigand   { SystemZ::R13D, 0x68 },
395f613dfdSUlrich Weigand   { SystemZ::R14D, 0x70 },
405f613dfdSUlrich Weigand   { SystemZ::R15D, 0x78 },
415f613dfdSUlrich Weigand   { SystemZ::F0D,  0x80 },
425f613dfdSUlrich Weigand   { SystemZ::F2D,  0x88 },
435f613dfdSUlrich Weigand   { SystemZ::F4D,  0x90 },
445f613dfdSUlrich Weigand   { SystemZ::F6D,  0x98 }
455f613dfdSUlrich Weigand };
46c231269fSRichard Sandiford } // end anonymous namespace
475f613dfdSUlrich Weigand 
48f1bd22dfSEric Christopher SystemZFrameLowering::SystemZFrameLowering()
49*882c43d7SGuillaume Chatelet     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8),
50f12b925bSJonas Paulsson                           -SystemZMC::CallFrameSize, 8,
51f12b925bSJonas Paulsson                           false /* StackRealignable */) {
525f613dfdSUlrich Weigand   // Create a mapping from register number to save slot offset.
535f613dfdSUlrich Weigand   RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
545f613dfdSUlrich Weigand   for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I)
55db39b4a2SRichard Sandiford     RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset;
56db39b4a2SRichard Sandiford }
57db39b4a2SRichard Sandiford 
58db39b4a2SRichard Sandiford const TargetFrameLowering::SpillSlot *
59db39b4a2SRichard Sandiford SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const {
60db39b4a2SRichard Sandiford   NumEntries = array_lengthof(SpillOffsetTable);
61db39b4a2SRichard Sandiford   return SpillOffsetTable;
625f613dfdSUlrich Weigand }
635f613dfdSUlrich Weigand 
6402564865SMatthias Braun void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF,
6502564865SMatthias Braun                                                 BitVector &SavedRegs,
665f613dfdSUlrich Weigand                                                 RegScavenger *RS) const {
6702564865SMatthias Braun   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
6802564865SMatthias Braun 
69941a705bSMatthias Braun   MachineFrameInfo &MFFrame = MF.getFrameInfo();
70fc6de428SEric Christopher   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
715f613dfdSUlrich Weigand   bool HasFP = hasFP(MF);
725f613dfdSUlrich Weigand   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
73f1caa283SMatthias Braun   bool IsVarArg = MF.getFunction().isVarArg();
745f613dfdSUlrich Weigand 
755f613dfdSUlrich Weigand   // va_start stores incoming FPR varargs in the normal way, but delegates
765f613dfdSUlrich Weigand   // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
775f613dfdSUlrich Weigand   // Record these pending uses, which typically include the call-saved
785f613dfdSUlrich Weigand   // argument register R6D.
795f613dfdSUlrich Weigand   if (IsVarArg)
805f613dfdSUlrich Weigand     for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
8102564865SMatthias Braun       SavedRegs.set(SystemZ::ArgGPRs[I]);
825f613dfdSUlrich Weigand 
83234e5a80SMarcin Koscielnicki   // If there are any landing pads, entering them will modify r6/r7.
84d0ee66c2SMatthias Braun   if (!MF.getLandingPads().empty()) {
85234e5a80SMarcin Koscielnicki     SavedRegs.set(SystemZ::R6D);
86234e5a80SMarcin Koscielnicki     SavedRegs.set(SystemZ::R7D);
87234e5a80SMarcin Koscielnicki   }
88234e5a80SMarcin Koscielnicki 
895f613dfdSUlrich Weigand   // If the function requires a frame pointer, record that the hard
905f613dfdSUlrich Weigand   // frame pointer will be clobbered.
915f613dfdSUlrich Weigand   if (HasFP)
9202564865SMatthias Braun     SavedRegs.set(SystemZ::R11D);
935f613dfdSUlrich Weigand 
945f613dfdSUlrich Weigand   // If the function calls other functions, record that the return
955f613dfdSUlrich Weigand   // address register will be clobbered.
96941a705bSMatthias Braun   if (MFFrame.hasCalls())
9702564865SMatthias Braun     SavedRegs.set(SystemZ::R14D);
985f613dfdSUlrich Weigand 
995f613dfdSUlrich Weigand   // If we are saving GPRs other than the stack pointer, we might as well
1005f613dfdSUlrich Weigand   // save and restore the stack pointer at the same time, via STMG and LMG.
1015f613dfdSUlrich Weigand   // This allows the deallocation to be done by the LMG, rather than needing
1025f613dfdSUlrich Weigand   // a separate %r15 addition.
103840beec2SCraig Topper   const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
1045f613dfdSUlrich Weigand   for (unsigned I = 0; CSRegs[I]; ++I) {
1055f613dfdSUlrich Weigand     unsigned Reg = CSRegs[I];
10602564865SMatthias Braun     if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
10702564865SMatthias Braun       SavedRegs.set(SystemZ::R15D);
1085f613dfdSUlrich Weigand       break;
1095f613dfdSUlrich Weigand     }
1105f613dfdSUlrich Weigand   }
1115f613dfdSUlrich Weigand }
1125f613dfdSUlrich Weigand 
1135f613dfdSUlrich Weigand // Add GPR64 to the save instruction being built by MIB, which is in basic
1145f613dfdSUlrich Weigand // block MBB.  IsImplicit says whether this is an explicit operand to the
1155f613dfdSUlrich Weigand // instruction, or an implicit one that comes between the explicit start
1165f613dfdSUlrich Weigand // and end registers.
1175f613dfdSUlrich Weigand static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
1185f613dfdSUlrich Weigand                         unsigned GPR64, bool IsImplicit) {
119d913448bSEric Christopher   const TargetRegisterInfo *RI =
120fc6de428SEric Christopher       MBB.getParent()->getSubtarget().getRegisterInfo();
1210c476111SDaniel Sanders   Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
1225f613dfdSUlrich Weigand   bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
1235f613dfdSUlrich Weigand   if (!IsLive || !IsImplicit) {
1245f613dfdSUlrich Weigand     MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
1255f613dfdSUlrich Weigand     if (!IsLive)
1265f613dfdSUlrich Weigand       MBB.addLiveIn(GPR64);
1275f613dfdSUlrich Weigand   }
1285f613dfdSUlrich Weigand }
1295f613dfdSUlrich Weigand 
1305f613dfdSUlrich Weigand bool SystemZFrameLowering::
1315f613dfdSUlrich Weigand spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1325f613dfdSUlrich Weigand                           MachineBasicBlock::iterator MBBI,
1335f613dfdSUlrich Weigand                           const std::vector<CalleeSavedInfo> &CSI,
1345f613dfdSUlrich Weigand                           const TargetRegisterInfo *TRI) const {
1355f613dfdSUlrich Weigand   if (CSI.empty())
1365f613dfdSUlrich Weigand     return false;
1375f613dfdSUlrich Weigand 
1385f613dfdSUlrich Weigand   MachineFunction &MF = *MBB.getParent();
139fc6de428SEric Christopher   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
1405f613dfdSUlrich Weigand   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
141f1caa283SMatthias Braun   bool IsVarArg = MF.getFunction().isVarArg();
142057c5a6bSOleg Ranevskyy   DebugLoc DL;
1435f613dfdSUlrich Weigand 
1445f613dfdSUlrich Weigand   // Scan the call-saved GPRs and find the bounds of the register spill area.
1455f613dfdSUlrich Weigand   unsigned LowGPR = 0;
1465f613dfdSUlrich Weigand   unsigned HighGPR = SystemZ::R15D;
1475f613dfdSUlrich Weigand   unsigned StartOffset = -1U;
1485f613dfdSUlrich Weigand   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
1495f613dfdSUlrich Weigand     unsigned Reg = CSI[I].getReg();
1505f613dfdSUlrich Weigand     if (SystemZ::GR64BitRegClass.contains(Reg)) {
1515f613dfdSUlrich Weigand       unsigned Offset = RegSpillOffsets[Reg];
1525f613dfdSUlrich Weigand       assert(Offset && "Unexpected GPR save");
1535f613dfdSUlrich Weigand       if (StartOffset > Offset) {
1545f613dfdSUlrich Weigand         LowGPR = Reg;
1555f613dfdSUlrich Weigand         StartOffset = Offset;
1565f613dfdSUlrich Weigand       }
1575f613dfdSUlrich Weigand     }
1585f613dfdSUlrich Weigand   }
1595f613dfdSUlrich Weigand 
160db39b4a2SRichard Sandiford   // Save the range of call-saved registers, for use by the epilogue inserter.
1615f613dfdSUlrich Weigand   ZFI->setLowSavedGPR(LowGPR);
1625f613dfdSUlrich Weigand   ZFI->setHighSavedGPR(HighGPR);
1635f613dfdSUlrich Weigand 
1645f613dfdSUlrich Weigand   // Include the GPR varargs, if any.  R6D is call-saved, so would
1655f613dfdSUlrich Weigand   // be included by the loop above, but we also need to handle the
1665f613dfdSUlrich Weigand   // call-clobbered argument registers.
1675f613dfdSUlrich Weigand   if (IsVarArg) {
1685f613dfdSUlrich Weigand     unsigned FirstGPR = ZFI->getVarArgsFirstGPR();
1695f613dfdSUlrich Weigand     if (FirstGPR < SystemZ::NumArgGPRs) {
1705f613dfdSUlrich Weigand       unsigned Reg = SystemZ::ArgGPRs[FirstGPR];
1715f613dfdSUlrich Weigand       unsigned Offset = RegSpillOffsets[Reg];
1725f613dfdSUlrich Weigand       if (StartOffset > Offset) {
1735f613dfdSUlrich Weigand         LowGPR = Reg; StartOffset = Offset;
1745f613dfdSUlrich Weigand       }
1755f613dfdSUlrich Weigand     }
1765f613dfdSUlrich Weigand   }
1775f613dfdSUlrich Weigand 
1785f613dfdSUlrich Weigand   // Save GPRs
1795f613dfdSUlrich Weigand   if (LowGPR) {
1805f613dfdSUlrich Weigand     assert(LowGPR != HighGPR && "Should be saving %r15 and something else");
1815f613dfdSUlrich Weigand 
1825f613dfdSUlrich Weigand     // Build an STMG instruction.
1835f613dfdSUlrich Weigand     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
1845f613dfdSUlrich Weigand 
1855f613dfdSUlrich Weigand     // Add the explicit register operands.
186f1bd22dfSEric Christopher     addSavedGPR(MBB, MIB, LowGPR, false);
187f1bd22dfSEric Christopher     addSavedGPR(MBB, MIB, HighGPR, false);
1885f613dfdSUlrich Weigand 
1895f613dfdSUlrich Weigand     // Add the address.
1905f613dfdSUlrich Weigand     MIB.addReg(SystemZ::R15D).addImm(StartOffset);
1915f613dfdSUlrich Weigand 
1925f613dfdSUlrich Weigand     // Make sure all call-saved GPRs are included as operands and are
1935f613dfdSUlrich Weigand     // marked as live on entry.
1945f613dfdSUlrich Weigand     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
1955f613dfdSUlrich Weigand       unsigned Reg = CSI[I].getReg();
1965f613dfdSUlrich Weigand       if (SystemZ::GR64BitRegClass.contains(Reg))
197f1bd22dfSEric Christopher         addSavedGPR(MBB, MIB, Reg, true);
1985f613dfdSUlrich Weigand     }
1995f613dfdSUlrich Weigand 
2005f613dfdSUlrich Weigand     // ...likewise GPR varargs.
2015f613dfdSUlrich Weigand     if (IsVarArg)
2025f613dfdSUlrich Weigand       for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
203f1bd22dfSEric Christopher         addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true);
2045f613dfdSUlrich Weigand   }
2055f613dfdSUlrich Weigand 
2068b19be46SUlrich Weigand   // Save FPRs/VRs in the normal TargetInstrInfo way.
2075f613dfdSUlrich Weigand   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
2085f613dfdSUlrich Weigand     unsigned Reg = CSI[I].getReg();
2095f613dfdSUlrich Weigand     if (SystemZ::FP64BitRegClass.contains(Reg)) {
2105f613dfdSUlrich Weigand       MBB.addLiveIn(Reg);
2115f613dfdSUlrich Weigand       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
2125f613dfdSUlrich Weigand                                &SystemZ::FP64BitRegClass, TRI);
2135f613dfdSUlrich Weigand     }
2148b19be46SUlrich Weigand     if (SystemZ::VR128BitRegClass.contains(Reg)) {
2158b19be46SUlrich Weigand       MBB.addLiveIn(Reg);
2168b19be46SUlrich Weigand       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
2178b19be46SUlrich Weigand                                &SystemZ::VR128BitRegClass, TRI);
2188b19be46SUlrich Weigand     }
2195f613dfdSUlrich Weigand   }
2205f613dfdSUlrich Weigand 
2215f613dfdSUlrich Weigand   return true;
2225f613dfdSUlrich Weigand }
2235f613dfdSUlrich Weigand 
2245f613dfdSUlrich Weigand bool SystemZFrameLowering::
2255f613dfdSUlrich Weigand restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
2265f613dfdSUlrich Weigand                             MachineBasicBlock::iterator MBBI,
227bea30c62SKrzysztof Parzyszek                             std::vector<CalleeSavedInfo> &CSI,
2285f613dfdSUlrich Weigand                             const TargetRegisterInfo *TRI) const {
2295f613dfdSUlrich Weigand   if (CSI.empty())
2305f613dfdSUlrich Weigand     return false;
2315f613dfdSUlrich Weigand 
2325f613dfdSUlrich Weigand   MachineFunction &MF = *MBB.getParent();
233fc6de428SEric Christopher   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2345f613dfdSUlrich Weigand   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
2355f613dfdSUlrich Weigand   bool HasFP = hasFP(MF);
2365f613dfdSUlrich Weigand   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
2375f613dfdSUlrich Weigand 
2388b19be46SUlrich Weigand   // Restore FPRs/VRs in the normal TargetInstrInfo way.
2395f613dfdSUlrich Weigand   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
2405f613dfdSUlrich Weigand     unsigned Reg = CSI[I].getReg();
2415f613dfdSUlrich Weigand     if (SystemZ::FP64BitRegClass.contains(Reg))
2425f613dfdSUlrich Weigand       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
2435f613dfdSUlrich Weigand                                 &SystemZ::FP64BitRegClass, TRI);
2448b19be46SUlrich Weigand     if (SystemZ::VR128BitRegClass.contains(Reg))
2458b19be46SUlrich Weigand       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
2468b19be46SUlrich Weigand                                 &SystemZ::VR128BitRegClass, TRI);
2475f613dfdSUlrich Weigand   }
2485f613dfdSUlrich Weigand 
2495f613dfdSUlrich Weigand   // Restore call-saved GPRs (but not call-clobbered varargs, which at
2505f613dfdSUlrich Weigand   // this point might hold return values).
2515f613dfdSUlrich Weigand   unsigned LowGPR = ZFI->getLowSavedGPR();
2525f613dfdSUlrich Weigand   unsigned HighGPR = ZFI->getHighSavedGPR();
2535f613dfdSUlrich Weigand   unsigned StartOffset = RegSpillOffsets[LowGPR];
2545f613dfdSUlrich Weigand   if (LowGPR) {
2555f613dfdSUlrich Weigand     // If we saved any of %r2-%r5 as varargs, we should also be saving
2565f613dfdSUlrich Weigand     // and restoring %r6.  If we're saving %r6 or above, we should be
2575f613dfdSUlrich Weigand     // restoring it too.
2585f613dfdSUlrich Weigand     assert(LowGPR != HighGPR && "Should be loading %r15 and something else");
2595f613dfdSUlrich Weigand 
2605f613dfdSUlrich Weigand     // Build an LMG instruction.
2615f613dfdSUlrich Weigand     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
2625f613dfdSUlrich Weigand 
2635f613dfdSUlrich Weigand     // Add the explicit register operands.
2645f613dfdSUlrich Weigand     MIB.addReg(LowGPR, RegState::Define);
2655f613dfdSUlrich Weigand     MIB.addReg(HighGPR, RegState::Define);
2665f613dfdSUlrich Weigand 
2675f613dfdSUlrich Weigand     // Add the address.
2685f613dfdSUlrich Weigand     MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
2695f613dfdSUlrich Weigand     MIB.addImm(StartOffset);
2705f613dfdSUlrich Weigand 
2715f613dfdSUlrich Weigand     // Do a second scan adding regs as being defined by instruction
2725f613dfdSUlrich Weigand     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
2735f613dfdSUlrich Weigand       unsigned Reg = CSI[I].getReg();
274f0344826SJonas Paulsson       if (Reg != LowGPR && Reg != HighGPR &&
275f0344826SJonas Paulsson           SystemZ::GR64BitRegClass.contains(Reg))
2765f613dfdSUlrich Weigand         MIB.addReg(Reg, RegState::ImplicitDefine);
2775f613dfdSUlrich Weigand     }
2785f613dfdSUlrich Weigand   }
2795f613dfdSUlrich Weigand 
2805f613dfdSUlrich Weigand   return true;
2815f613dfdSUlrich Weigand }
2825f613dfdSUlrich Weigand 
2835dd52f8cSRichard Sandiford void SystemZFrameLowering::
2845dd52f8cSRichard Sandiford processFunctionBeforeFrameFinalized(MachineFunction &MF,
2855dd52f8cSRichard Sandiford                                     RegScavenger *RS) const {
286941a705bSMatthias Braun   MachineFrameInfo &MFFrame = MF.getFrameInfo();
287af98b748SUlrich Weigand   // Get the size of our stack frame to be allocated ...
288af98b748SUlrich Weigand   uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
289af98b748SUlrich Weigand                         SystemZMC::CallFrameSize);
290af98b748SUlrich Weigand   // ... and the maximum offset we may need to reach into the
291af98b748SUlrich Weigand   // caller's frame to access the save area or stack arguments.
292af98b748SUlrich Weigand   int64_t MaxArgOffset = SystemZMC::CallFrameSize;
293af98b748SUlrich Weigand   for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
294af98b748SUlrich Weigand     if (MFFrame.getObjectOffset(I) >= 0) {
295af98b748SUlrich Weigand       int64_t ArgOffset = SystemZMC::CallFrameSize +
296af98b748SUlrich Weigand                           MFFrame.getObjectOffset(I) +
297af98b748SUlrich Weigand                           MFFrame.getObjectSize(I);
298af98b748SUlrich Weigand       MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
299af98b748SUlrich Weigand     }
300af98b748SUlrich Weigand 
301af98b748SUlrich Weigand   uint64_t MaxReach = StackSize + MaxArgOffset;
30223943229SRichard Sandiford   if (!isUInt<12>(MaxReach)) {
30323943229SRichard Sandiford     // We may need register scavenging slots if some parts of the frame
3045dd52f8cSRichard Sandiford     // are outside the reach of an unsigned 12-bit displacement.
30523943229SRichard Sandiford     // Create 2 for the case where both addresses in an MVC are
30623943229SRichard Sandiford     // out of range.
307941a705bSMatthias Braun     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
308941a705bSMatthias Braun     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
30923943229SRichard Sandiford   }
3105dd52f8cSRichard Sandiford }
3115dd52f8cSRichard Sandiford 
3125f613dfdSUlrich Weigand // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
3135f613dfdSUlrich Weigand static void emitIncrement(MachineBasicBlock &MBB,
3145f613dfdSUlrich Weigand                           MachineBasicBlock::iterator &MBBI,
3155f613dfdSUlrich Weigand                           const DebugLoc &DL,
3165f613dfdSUlrich Weigand                           unsigned Reg, int64_t NumBytes,
3175f613dfdSUlrich Weigand                           const TargetInstrInfo *TII) {
3185f613dfdSUlrich Weigand   while (NumBytes) {
3195f613dfdSUlrich Weigand     unsigned Opcode;
3205f613dfdSUlrich Weigand     int64_t ThisVal = NumBytes;
3215f613dfdSUlrich Weigand     if (isInt<16>(NumBytes))
3225f613dfdSUlrich Weigand       Opcode = SystemZ::AGHI;
3235f613dfdSUlrich Weigand     else {
3245f613dfdSUlrich Weigand       Opcode = SystemZ::AGFI;
3255f613dfdSUlrich Weigand       // Make sure we maintain 8-byte stack alignment.
326fffd56ecSAlexey Samsonov       int64_t MinVal = -uint64_t(1) << 31;
3275f613dfdSUlrich Weigand       int64_t MaxVal = (int64_t(1) << 31) - 8;
3285f613dfdSUlrich Weigand       if (ThisVal < MinVal)
3295f613dfdSUlrich Weigand         ThisVal = MinVal;
3305f613dfdSUlrich Weigand       else if (ThisVal > MaxVal)
3315f613dfdSUlrich Weigand         ThisVal = MaxVal;
3325f613dfdSUlrich Weigand     }
3335f613dfdSUlrich Weigand     MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
3345f613dfdSUlrich Weigand       .addReg(Reg).addImm(ThisVal);
33514a44495SRichard Sandiford     // The CC implicit def is dead.
3365f613dfdSUlrich Weigand     MI->getOperand(3).setIsDead();
3375f613dfdSUlrich Weigand     NumBytes -= ThisVal;
3385f613dfdSUlrich Weigand   }
3395f613dfdSUlrich Weigand }
3405f613dfdSUlrich Weigand 
34161b305edSQuentin Colombet void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
34261b305edSQuentin Colombet                                         MachineBasicBlock &MBB) const {
34361b305edSQuentin Colombet   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
344941a705bSMatthias Braun   MachineFrameInfo &MFFrame = MF.getFrameInfo();
345fc6de428SEric Christopher   auto *ZII =
346fc6de428SEric Christopher       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
3475f613dfdSUlrich Weigand   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
3485f613dfdSUlrich Weigand   MachineBasicBlock::iterator MBBI = MBB.begin();
3495f613dfdSUlrich Weigand   MachineModuleInfo &MMI = MF.getMMI();
350bc07a890SBill Wendling   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
351941a705bSMatthias Braun   const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
3525f613dfdSUlrich Weigand   bool HasFP = hasFP(MF);
353057c5a6bSOleg Ranevskyy 
354057c5a6bSOleg Ranevskyy   // Debug location must be unknown since the first debug location is used
355057c5a6bSOleg Ranevskyy   // to determine the end of the prologue.
356057c5a6bSOleg Ranevskyy   DebugLoc DL;
3575f613dfdSUlrich Weigand 
3585f613dfdSUlrich Weigand   // The current offset of the stack pointer from the CFA.
3595f613dfdSUlrich Weigand   int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP;
3605f613dfdSUlrich Weigand 
3615f613dfdSUlrich Weigand   if (ZFI->getLowSavedGPR()) {
3625f613dfdSUlrich Weigand     // Skip over the GPR saves.
3635f613dfdSUlrich Weigand     if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
3645f613dfdSUlrich Weigand       ++MBBI;
3655f613dfdSUlrich Weigand     else
3665f613dfdSUlrich Weigand       llvm_unreachable("Couldn't skip over GPR saves");
3675f613dfdSUlrich Weigand 
3685f613dfdSUlrich Weigand     // Add CFI for the GPR saves.
36928c111ecSRichard Sandiford     for (auto &Save : CSI) {
37028c111ecSRichard Sandiford       unsigned Reg = Save.getReg();
3715f613dfdSUlrich Weigand       if (SystemZ::GR64BitRegClass.contains(Reg)) {
3725f613dfdSUlrich Weigand         int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg];
373f23ef437SMatthias Braun         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
374b1f25f1bSRafael Espindola             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
375b1f25f1bSRafael Espindola         BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
376b1f25f1bSRafael Espindola             .addCFIIndex(CFIIndex);
3775f613dfdSUlrich Weigand       }
3785f613dfdSUlrich Weigand     }
3795f613dfdSUlrich Weigand   }
3805f613dfdSUlrich Weigand 
38132063888SUlrich Weigand   uint64_t StackSize = MFFrame.getStackSize();
38232063888SUlrich Weigand   // We need to allocate the ABI-defined 160-byte base area whenever
38332063888SUlrich Weigand   // we allocate stack space for our own use and whenever we call another
38432063888SUlrich Weigand   // function.
38532063888SUlrich Weigand   if (StackSize || MFFrame.hasVarSizedObjects() || MFFrame.hasCalls()) {
38632063888SUlrich Weigand     StackSize += SystemZMC::CallFrameSize;
38732063888SUlrich Weigand     MFFrame.setStackSize(StackSize);
38832063888SUlrich Weigand   }
38932063888SUlrich Weigand 
3905f613dfdSUlrich Weigand   if (StackSize) {
391ad1482c6SMarcin Koscielnicki     // Determine if we want to store a backchain.
392f1caa283SMatthias Braun     bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
393ad1482c6SMarcin Koscielnicki 
394ad1482c6SMarcin Koscielnicki     // If we need backchain, save current stack pointer.  R1 is free at this
395ad1482c6SMarcin Koscielnicki     // point.
396ad1482c6SMarcin Koscielnicki     if (StoreBackchain)
397ad1482c6SMarcin Koscielnicki       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
398ad1482c6SMarcin Koscielnicki         .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
399ad1482c6SMarcin Koscielnicki 
4005f613dfdSUlrich Weigand     // Allocate StackSize bytes.
4015f613dfdSUlrich Weigand     int64_t Delta = -int64_t(StackSize);
4025f613dfdSUlrich Weigand     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
4035f613dfdSUlrich Weigand 
4045f613dfdSUlrich Weigand     // Add CFI for the allocation.
405f23ef437SMatthias Braun     unsigned CFIIndex = MF.addFrameInst(
406b1f25f1bSRafael Espindola         MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta));
407b1f25f1bSRafael Espindola     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
408b1f25f1bSRafael Espindola         .addCFIIndex(CFIIndex);
4095f613dfdSUlrich Weigand     SPOffsetFromCFA += Delta;
410ad1482c6SMarcin Koscielnicki 
411ad1482c6SMarcin Koscielnicki     if (StoreBackchain)
412ad1482c6SMarcin Koscielnicki       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
413ad1482c6SMarcin Koscielnicki         .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0).addReg(0);
4145f613dfdSUlrich Weigand   }
4155f613dfdSUlrich Weigand 
4165f613dfdSUlrich Weigand   if (HasFP) {
4175f613dfdSUlrich Weigand     // Copy the base of the frame to R11.
4185f613dfdSUlrich Weigand     BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
4195f613dfdSUlrich Weigand       .addReg(SystemZ::R15D);
4205f613dfdSUlrich Weigand 
4215f613dfdSUlrich Weigand     // Add CFI for the new frame location.
422bc07a890SBill Wendling     unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true);
423f23ef437SMatthias Braun     unsigned CFIIndex = MF.addFrameInst(
424b1f25f1bSRafael Espindola         MCCFIInstruction::createDefCfaRegister(nullptr, HardFP));
425b1f25f1bSRafael Espindola     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
426b1f25f1bSRafael Espindola         .addCFIIndex(CFIIndex);
4275f613dfdSUlrich Weigand 
4285f613dfdSUlrich Weigand     // Mark the FramePtr as live at the beginning of every block except
4295f613dfdSUlrich Weigand     // the entry block.  (We'll have marked R11 as live on entry when
4305f613dfdSUlrich Weigand     // saving the GPRs.)
43128c111ecSRichard Sandiford     for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
4325f613dfdSUlrich Weigand       I->addLiveIn(SystemZ::R11D);
4335f613dfdSUlrich Weigand   }
4345f613dfdSUlrich Weigand 
4358b19be46SUlrich Weigand   // Skip over the FPR/VR saves.
436b1f25f1bSRafael Espindola   SmallVector<unsigned, 8> CFIIndexes;
43728c111ecSRichard Sandiford   for (auto &Save : CSI) {
43828c111ecSRichard Sandiford     unsigned Reg = Save.getReg();
4395f613dfdSUlrich Weigand     if (SystemZ::FP64BitRegClass.contains(Reg)) {
4405f613dfdSUlrich Weigand       if (MBBI != MBB.end() &&
4415f613dfdSUlrich Weigand           (MBBI->getOpcode() == SystemZ::STD ||
4425f613dfdSUlrich Weigand            MBBI->getOpcode() == SystemZ::STDY))
4435f613dfdSUlrich Weigand         ++MBBI;
4445f613dfdSUlrich Weigand       else
4455f613dfdSUlrich Weigand         llvm_unreachable("Couldn't skip over FPR save");
4468b19be46SUlrich Weigand     } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
4478b19be46SUlrich Weigand       if (MBBI != MBB.end() &&
4488b19be46SUlrich Weigand           MBBI->getOpcode() == SystemZ::VST)
4498b19be46SUlrich Weigand         ++MBBI;
4508b19be46SUlrich Weigand       else
4518b19be46SUlrich Weigand         llvm_unreachable("Couldn't skip over VR save");
4528b19be46SUlrich Weigand     } else
4538b19be46SUlrich Weigand       continue;
4545f613dfdSUlrich Weigand 
4555f613dfdSUlrich Weigand     // Add CFI for the this save.
45628c111ecSRichard Sandiford     unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
4575567bafeSJames Y Knight     unsigned IgnoredFrameReg;
4585567bafeSJames Y Knight     int64_t Offset =
4595567bafeSJames Y Knight         getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg);
4605567bafeSJames Y Knight 
461f23ef437SMatthias Braun     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
462b1f25f1bSRafael Espindola           nullptr, DwarfReg, SPOffsetFromCFA + Offset));
463b1f25f1bSRafael Espindola     CFIIndexes.push_back(CFIIndex);
4645f613dfdSUlrich Weigand   }
4658b19be46SUlrich Weigand   // Complete the CFI for the FPR/VR saves, modelling them as taking effect
4665f613dfdSUlrich Weigand   // after the last save.
467b1f25f1bSRafael Espindola   for (auto CFIIndex : CFIIndexes) {
468b1f25f1bSRafael Espindola     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
469b1f25f1bSRafael Espindola         .addCFIIndex(CFIIndex);
470b1f25f1bSRafael Espindola   }
4715f613dfdSUlrich Weigand }
4725f613dfdSUlrich Weigand 
4735f613dfdSUlrich Weigand void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
4745f613dfdSUlrich Weigand                                         MachineBasicBlock &MBB) const {
4755f613dfdSUlrich Weigand   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
476fc6de428SEric Christopher   auto *ZII =
477fc6de428SEric Christopher       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
4785f613dfdSUlrich Weigand   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
47932063888SUlrich Weigand   MachineFrameInfo &MFFrame = MF.getFrameInfo();
4805f613dfdSUlrich Weigand 
4815f613dfdSUlrich Weigand   // Skip the return instruction.
482709bda66SRichard Sandiford   assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
4835f613dfdSUlrich Weigand 
48432063888SUlrich Weigand   uint64_t StackSize = MFFrame.getStackSize();
4855f613dfdSUlrich Weigand   if (ZFI->getLowSavedGPR()) {
4865f613dfdSUlrich Weigand     --MBBI;
4875f613dfdSUlrich Weigand     unsigned Opcode = MBBI->getOpcode();
4885f613dfdSUlrich Weigand     if (Opcode != SystemZ::LMG)
4895f613dfdSUlrich Weigand       llvm_unreachable("Expected to see callee-save register restore code");
4905f613dfdSUlrich Weigand 
4915f613dfdSUlrich Weigand     unsigned AddrOpNo = 2;
4925f613dfdSUlrich Weigand     DebugLoc DL = MBBI->getDebugLoc();
4935f613dfdSUlrich Weigand     uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
4945f613dfdSUlrich Weigand     unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
4955f613dfdSUlrich Weigand 
4965f613dfdSUlrich Weigand     // If the offset is too large, use the largest stack-aligned offset
4975f613dfdSUlrich Weigand     // and add the rest to the base register (the stack or frame pointer).
4985f613dfdSUlrich Weigand     if (!NewOpcode) {
4995f613dfdSUlrich Weigand       uint64_t NumBytes = Offset - 0x7fff8;
5005f613dfdSUlrich Weigand       emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
5015f613dfdSUlrich Weigand                     NumBytes, ZII);
5025f613dfdSUlrich Weigand       Offset -= NumBytes;
5035f613dfdSUlrich Weigand       NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
5045f613dfdSUlrich Weigand       assert(NewOpcode && "No restore instruction available");
5055f613dfdSUlrich Weigand     }
5065f613dfdSUlrich Weigand 
5075f613dfdSUlrich Weigand     MBBI->setDesc(ZII->get(NewOpcode));
5085f613dfdSUlrich Weigand     MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
5095f613dfdSUlrich Weigand   } else if (StackSize) {
5105f613dfdSUlrich Weigand     DebugLoc DL = MBBI->getDebugLoc();
5115f613dfdSUlrich Weigand     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
5125f613dfdSUlrich Weigand   }
5135f613dfdSUlrich Weigand }
5145f613dfdSUlrich Weigand 
5155f613dfdSUlrich Weigand bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const {
5165f613dfdSUlrich Weigand   return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
517941a705bSMatthias Braun           MF.getFrameInfo().hasVarSizedObjects() ||
5185f613dfdSUlrich Weigand           MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP());
5195f613dfdSUlrich Weigand }
5205f613dfdSUlrich Weigand 
5215f613dfdSUlrich Weigand bool
5225f613dfdSUlrich Weigand SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
5235f613dfdSUlrich Weigand   // The ABI requires us to allocate 160 bytes of stack space for the callee,
5245f613dfdSUlrich Weigand   // with any outgoing stack arguments being placed above that.  It seems
5255f613dfdSUlrich Weigand   // better to make that area a permanent feature of the frame even if
5265f613dfdSUlrich Weigand   // we're using a frame pointer.
5275f613dfdSUlrich Weigand   return true;
5285f613dfdSUlrich Weigand }
5295f613dfdSUlrich Weigand 
530e1a2e90fSHans Wennborg MachineBasicBlock::iterator SystemZFrameLowering::
5315f613dfdSUlrich Weigand eliminateCallFramePseudoInstr(MachineFunction &MF,
5325f613dfdSUlrich Weigand                               MachineBasicBlock &MBB,
5335f613dfdSUlrich Weigand                               MachineBasicBlock::iterator MI) const {
5345f613dfdSUlrich Weigand   switch (MI->getOpcode()) {
5355f613dfdSUlrich Weigand   case SystemZ::ADJCALLSTACKDOWN:
5365f613dfdSUlrich Weigand   case SystemZ::ADJCALLSTACKUP:
5375f613dfdSUlrich Weigand     assert(hasReservedCallFrame(MF) &&
5385f613dfdSUlrich Weigand            "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
539e1a2e90fSHans Wennborg     return MBB.erase(MI);
5405f613dfdSUlrich Weigand     break;
5415f613dfdSUlrich Weigand 
5425f613dfdSUlrich Weigand   default:
5435f613dfdSUlrich Weigand     llvm_unreachable("Unexpected call frame instruction");
5445f613dfdSUlrich Weigand   }
5455f613dfdSUlrich Weigand }
546