xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
190b57cec5SDimitry Andric #include "llvm/IR/Function.h"
205ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace {
25480093f4SDimitry Andric // The ABI-defined register save slots, relative to the CFA (i.e.
26*fe6060f1SDimitry Andric // incoming stack pointer + SystemZMC::ELFCallFrameSize).
270b57cec5SDimitry Andric static const TargetFrameLowering::SpillSlot SpillOffsetTable[] = {
280b57cec5SDimitry Andric   { SystemZ::R2D,  0x10 },
290b57cec5SDimitry Andric   { SystemZ::R3D,  0x18 },
300b57cec5SDimitry Andric   { SystemZ::R4D,  0x20 },
310b57cec5SDimitry Andric   { SystemZ::R5D,  0x28 },
320b57cec5SDimitry Andric   { SystemZ::R6D,  0x30 },
330b57cec5SDimitry Andric   { SystemZ::R7D,  0x38 },
340b57cec5SDimitry Andric   { SystemZ::R8D,  0x40 },
350b57cec5SDimitry Andric   { SystemZ::R9D,  0x48 },
360b57cec5SDimitry Andric   { SystemZ::R10D, 0x50 },
370b57cec5SDimitry Andric   { SystemZ::R11D, 0x58 },
380b57cec5SDimitry Andric   { SystemZ::R12D, 0x60 },
390b57cec5SDimitry Andric   { SystemZ::R13D, 0x68 },
400b57cec5SDimitry Andric   { SystemZ::R14D, 0x70 },
410b57cec5SDimitry Andric   { SystemZ::R15D, 0x78 },
420b57cec5SDimitry Andric   { SystemZ::F0D,  0x80 },
430b57cec5SDimitry Andric   { SystemZ::F2D,  0x88 },
440b57cec5SDimitry Andric   { SystemZ::F4D,  0x90 },
450b57cec5SDimitry Andric   { SystemZ::F6D,  0x98 }
460b57cec5SDimitry Andric };
470b57cec5SDimitry Andric } // end anonymous namespace
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric SystemZFrameLowering::SystemZFrameLowering()
508bcb0991SDimitry Andric     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8),
51480093f4SDimitry Andric                           0, Align(8), false /* StackRealignable */),
52480093f4SDimitry Andric       RegSpillOffsets(0) {
53480093f4SDimitry Andric   // Due to the SystemZ ABI, the DWARF CFA (Canonical Frame Address) is not
54480093f4SDimitry Andric   // equal to the incoming stack pointer, but to incoming stack pointer plus
55480093f4SDimitry Andric   // 160.  Instead of using a Local Area Offset, the Register save area will
56480093f4SDimitry Andric   // be occupied by fixed frame objects, and all offsets are actually
57480093f4SDimitry Andric   // relative to CFA.
58480093f4SDimitry Andric 
590b57cec5SDimitry Andric   // Create a mapping from register number to save slot offset.
60480093f4SDimitry Andric   // These offsets are relative to the start of the register save area.
610b57cec5SDimitry Andric   RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
620b57cec5SDimitry Andric   for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I)
630b57cec5SDimitry Andric     RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
66480093f4SDimitry Andric bool SystemZFrameLowering::
67480093f4SDimitry Andric assignCalleeSavedSpillSlots(MachineFunction &MF,
68480093f4SDimitry Andric                             const TargetRegisterInfo *TRI,
69480093f4SDimitry Andric                             std::vector<CalleeSavedInfo> &CSI) const {
70480093f4SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
71480093f4SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
72480093f4SDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
73480093f4SDimitry Andric   if (CSI.empty())
74480093f4SDimitry Andric     return true; // Early exit if no callee saved registers are modified!
75480093f4SDimitry Andric 
76480093f4SDimitry Andric   unsigned LowGPR = 0;
77480093f4SDimitry Andric   unsigned HighGPR = SystemZ::R15D;
78*fe6060f1SDimitry Andric   int StartSPOffset = SystemZMC::ELFCallFrameSize;
79480093f4SDimitry Andric   for (auto &CS : CSI) {
80480093f4SDimitry Andric     unsigned Reg = CS.getReg();
815ffd83dbSDimitry Andric     int Offset = getRegSpillOffset(MF, Reg);
82480093f4SDimitry Andric     if (Offset) {
83480093f4SDimitry Andric       if (SystemZ::GR64BitRegClass.contains(Reg) && StartSPOffset > Offset) {
84480093f4SDimitry Andric         LowGPR = Reg;
85480093f4SDimitry Andric         StartSPOffset = Offset;
86480093f4SDimitry Andric       }
87*fe6060f1SDimitry Andric       Offset -= SystemZMC::ELFCallFrameSize;
88480093f4SDimitry Andric       int FrameIdx = MFFrame.CreateFixedSpillStackObject(8, Offset);
89480093f4SDimitry Andric       CS.setFrameIdx(FrameIdx);
90480093f4SDimitry Andric     } else
91480093f4SDimitry Andric       CS.setFrameIdx(INT32_MAX);
92480093f4SDimitry Andric   }
93480093f4SDimitry Andric 
94480093f4SDimitry Andric   // Save the range of call-saved registers, for use by the
95480093f4SDimitry Andric   // prologue/epilogue inserters.
96480093f4SDimitry Andric   ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset);
97480093f4SDimitry Andric   if (IsVarArg) {
98480093f4SDimitry Andric     // Also save the GPR varargs, if any.  R6D is call-saved, so would
99480093f4SDimitry Andric     // already be included, but we also need to handle the call-clobbered
100480093f4SDimitry Andric     // argument registers.
101480093f4SDimitry Andric     unsigned FirstGPR = ZFI->getVarArgsFirstGPR();
102*fe6060f1SDimitry Andric     if (FirstGPR < SystemZ::ELFNumArgGPRs) {
103*fe6060f1SDimitry Andric       unsigned Reg = SystemZ::ELFArgGPRs[FirstGPR];
1045ffd83dbSDimitry Andric       int Offset = getRegSpillOffset(MF, Reg);
105480093f4SDimitry Andric       if (StartSPOffset > Offset) {
106480093f4SDimitry Andric         LowGPR = Reg; StartSPOffset = Offset;
107480093f4SDimitry Andric       }
108480093f4SDimitry Andric     }
109480093f4SDimitry Andric   }
110480093f4SDimitry Andric   ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset);
111480093f4SDimitry Andric 
112480093f4SDimitry Andric   // Create fixed stack objects for the remaining registers.
113*fe6060f1SDimitry Andric   int CurrOffset = -SystemZMC::ELFCallFrameSize;
1145ffd83dbSDimitry Andric   if (usePackedStack(MF))
1155ffd83dbSDimitry Andric     CurrOffset += StartSPOffset;
1165ffd83dbSDimitry Andric 
117480093f4SDimitry Andric   for (auto &CS : CSI) {
118480093f4SDimitry Andric     if (CS.getFrameIdx() != INT32_MAX)
119480093f4SDimitry Andric       continue;
120480093f4SDimitry Andric     unsigned Reg = CS.getReg();
121480093f4SDimitry Andric     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
122480093f4SDimitry Andric     unsigned Size = TRI->getSpillSize(*RC);
123480093f4SDimitry Andric     CurrOffset -= Size;
124480093f4SDimitry Andric     assert(CurrOffset % 8 == 0 &&
125480093f4SDimitry Andric            "8-byte alignment required for for all register save slots");
126480093f4SDimitry Andric     int FrameIdx = MFFrame.CreateFixedSpillStackObject(Size, CurrOffset);
127480093f4SDimitry Andric     CS.setFrameIdx(FrameIdx);
128480093f4SDimitry Andric   }
129480093f4SDimitry Andric 
130480093f4SDimitry Andric   return true;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF,
1340b57cec5SDimitry Andric                                                 BitVector &SavedRegs,
1350b57cec5SDimitry Andric                                                 RegScavenger *RS) const {
1360b57cec5SDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
1390b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1400b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
1410b57cec5SDimitry Andric   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
1420b57cec5SDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   // va_start stores incoming FPR varargs in the normal way, but delegates
1450b57cec5SDimitry Andric   // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
1460b57cec5SDimitry Andric   // Record these pending uses, which typically include the call-saved
1470b57cec5SDimitry Andric   // argument register R6D.
1480b57cec5SDimitry Andric   if (IsVarArg)
149*fe6060f1SDimitry Andric     for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
150*fe6060f1SDimitry Andric       SavedRegs.set(SystemZ::ELFArgGPRs[I]);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   // If there are any landing pads, entering them will modify r6/r7.
1530b57cec5SDimitry Andric   if (!MF.getLandingPads().empty()) {
1540b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R6D);
1550b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R7D);
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   // If the function requires a frame pointer, record that the hard
1590b57cec5SDimitry Andric   // frame pointer will be clobbered.
1600b57cec5SDimitry Andric   if (HasFP)
1610b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R11D);
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   // If the function calls other functions, record that the return
1640b57cec5SDimitry Andric   // address register will be clobbered.
1650b57cec5SDimitry Andric   if (MFFrame.hasCalls())
1660b57cec5SDimitry Andric     SavedRegs.set(SystemZ::R14D);
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   // If we are saving GPRs other than the stack pointer, we might as well
1690b57cec5SDimitry Andric   // save and restore the stack pointer at the same time, via STMG and LMG.
1700b57cec5SDimitry Andric   // This allows the deallocation to be done by the LMG, rather than needing
1710b57cec5SDimitry Andric   // a separate %r15 addition.
1720b57cec5SDimitry Andric   const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
1730b57cec5SDimitry Andric   for (unsigned I = 0; CSRegs[I]; ++I) {
1740b57cec5SDimitry Andric     unsigned Reg = CSRegs[I];
1750b57cec5SDimitry Andric     if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
1760b57cec5SDimitry Andric       SavedRegs.set(SystemZ::R15D);
1770b57cec5SDimitry Andric       break;
1780b57cec5SDimitry Andric     }
1790b57cec5SDimitry Andric   }
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric // Add GPR64 to the save instruction being built by MIB, which is in basic
1830b57cec5SDimitry Andric // block MBB.  IsImplicit says whether this is an explicit operand to the
1840b57cec5SDimitry Andric // instruction, or an implicit one that comes between the explicit start
1850b57cec5SDimitry Andric // and end registers.
1860b57cec5SDimitry Andric static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
1870b57cec5SDimitry Andric                         unsigned GPR64, bool IsImplicit) {
1880b57cec5SDimitry Andric   const TargetRegisterInfo *RI =
1890b57cec5SDimitry Andric       MBB.getParent()->getSubtarget().getRegisterInfo();
1908bcb0991SDimitry Andric   Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
1910b57cec5SDimitry Andric   bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
1920b57cec5SDimitry Andric   if (!IsLive || !IsImplicit) {
1930b57cec5SDimitry Andric     MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
1940b57cec5SDimitry Andric     if (!IsLive)
1950b57cec5SDimitry Andric       MBB.addLiveIn(GPR64);
1960b57cec5SDimitry Andric   }
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1995ffd83dbSDimitry Andric bool SystemZFrameLowering::spillCalleeSavedRegisters(
2005ffd83dbSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
2015ffd83dbSDimitry Andric     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
2020b57cec5SDimitry Andric   if (CSI.empty())
2030b57cec5SDimitry Andric     return false;
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
2060b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2070b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
2080b57cec5SDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
2090b57cec5SDimitry Andric   DebugLoc DL;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   // Save GPRs
212480093f4SDimitry Andric   SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
213480093f4SDimitry Andric   if (SpillGPRs.LowGPR) {
214480093f4SDimitry Andric     assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
215480093f4SDimitry Andric            "Should be saving %r15 and something else");
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric     // Build an STMG instruction.
2180b57cec5SDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric     // Add the explicit register operands.
221480093f4SDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
222480093f4SDimitry Andric     addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric     // Add the address.
225480093f4SDimitry Andric     MIB.addReg(SystemZ::R15D).addImm(SpillGPRs.GPROffset);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric     // Make sure all call-saved GPRs are included as operands and are
2280b57cec5SDimitry Andric     // marked as live on entry.
2290b57cec5SDimitry Andric     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
2300b57cec5SDimitry Andric       unsigned Reg = CSI[I].getReg();
2310b57cec5SDimitry Andric       if (SystemZ::GR64BitRegClass.contains(Reg))
2320b57cec5SDimitry Andric         addSavedGPR(MBB, MIB, Reg, true);
2330b57cec5SDimitry Andric     }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric     // ...likewise GPR varargs.
2360b57cec5SDimitry Andric     if (IsVarArg)
237*fe6060f1SDimitry Andric       for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::ELFNumArgGPRs; ++I)
238*fe6060f1SDimitry Andric         addSavedGPR(MBB, MIB, SystemZ::ELFArgGPRs[I], true);
2390b57cec5SDimitry Andric   }
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric   // Save FPRs/VRs in the normal TargetInstrInfo way.
2420b57cec5SDimitry Andric   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
2430b57cec5SDimitry Andric     unsigned Reg = CSI[I].getReg();
2440b57cec5SDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg)) {
2450b57cec5SDimitry Andric       MBB.addLiveIn(Reg);
2460b57cec5SDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
2470b57cec5SDimitry Andric                                &SystemZ::FP64BitRegClass, TRI);
2480b57cec5SDimitry Andric     }
2490b57cec5SDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg)) {
2500b57cec5SDimitry Andric       MBB.addLiveIn(Reg);
2510b57cec5SDimitry Andric       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
2520b57cec5SDimitry Andric                                &SystemZ::VR128BitRegClass, TRI);
2530b57cec5SDimitry Andric     }
2540b57cec5SDimitry Andric   }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric   return true;
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric 
2595ffd83dbSDimitry Andric bool SystemZFrameLowering::restoreCalleeSavedRegisters(
2605ffd83dbSDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
2615ffd83dbSDimitry Andric     MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
2620b57cec5SDimitry Andric   if (CSI.empty())
2630b57cec5SDimitry Andric     return false;
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
2660b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2670b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
2680b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
2690b57cec5SDimitry Andric   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   // Restore FPRs/VRs in the normal TargetInstrInfo way.
2720b57cec5SDimitry Andric   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
2730b57cec5SDimitry Andric     unsigned Reg = CSI[I].getReg();
2740b57cec5SDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg))
2750b57cec5SDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
2760b57cec5SDimitry Andric                                 &SystemZ::FP64BitRegClass, TRI);
2770b57cec5SDimitry Andric     if (SystemZ::VR128BitRegClass.contains(Reg))
2780b57cec5SDimitry Andric       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
2790b57cec5SDimitry Andric                                 &SystemZ::VR128BitRegClass, TRI);
2800b57cec5SDimitry Andric   }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   // Restore call-saved GPRs (but not call-clobbered varargs, which at
2830b57cec5SDimitry Andric   // this point might hold return values).
284480093f4SDimitry Andric   SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
285480093f4SDimitry Andric   if (RestoreGPRs.LowGPR) {
2860b57cec5SDimitry Andric     // If we saved any of %r2-%r5 as varargs, we should also be saving
2870b57cec5SDimitry Andric     // and restoring %r6.  If we're saving %r6 or above, we should be
2880b57cec5SDimitry Andric     // restoring it too.
289480093f4SDimitry Andric     assert(RestoreGPRs.LowGPR != RestoreGPRs.HighGPR &&
290480093f4SDimitry Andric            "Should be loading %r15 and something else");
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric     // Build an LMG instruction.
2930b57cec5SDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric     // Add the explicit register operands.
296480093f4SDimitry Andric     MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
297480093f4SDimitry Andric     MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric     // Add the address.
3000b57cec5SDimitry Andric     MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
301480093f4SDimitry Andric     MIB.addImm(RestoreGPRs.GPROffset);
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric     // Do a second scan adding regs as being defined by instruction
3040b57cec5SDimitry Andric     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
3050b57cec5SDimitry Andric       unsigned Reg = CSI[I].getReg();
306480093f4SDimitry Andric       if (Reg != RestoreGPRs.LowGPR && Reg != RestoreGPRs.HighGPR &&
3070b57cec5SDimitry Andric           SystemZ::GR64BitRegClass.contains(Reg))
3080b57cec5SDimitry Andric         MIB.addReg(Reg, RegState::ImplicitDefine);
3090b57cec5SDimitry Andric     }
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric   return true;
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric void SystemZFrameLowering::
3160b57cec5SDimitry Andric processFunctionBeforeFrameFinalized(MachineFunction &MF,
3170b57cec5SDimitry Andric                                     RegScavenger *RS) const {
3180b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
319e8d8bef9SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
320e8d8bef9SDimitry Andric   MachineRegisterInfo *MRI = &MF.getRegInfo();
3215ffd83dbSDimitry Andric   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
322480093f4SDimitry Andric 
3235ffd83dbSDimitry Andric   if (!usePackedStack(MF) || BackChain)
3245ffd83dbSDimitry Andric     // Create the incoming register save area.
325480093f4SDimitry Andric     getOrCreateFramePointerSaveIndex(MF);
326480093f4SDimitry Andric 
3270b57cec5SDimitry Andric   // Get the size of our stack frame to be allocated ...
3280b57cec5SDimitry Andric   uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
329*fe6060f1SDimitry Andric                         SystemZMC::ELFCallFrameSize);
3300b57cec5SDimitry Andric   // ... and the maximum offset we may need to reach into the
3310b57cec5SDimitry Andric   // caller's frame to access the save area or stack arguments.
332480093f4SDimitry Andric   int64_t MaxArgOffset = 0;
3330b57cec5SDimitry Andric   for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
3340b57cec5SDimitry Andric     if (MFFrame.getObjectOffset(I) >= 0) {
335480093f4SDimitry Andric       int64_t ArgOffset = MFFrame.getObjectOffset(I) +
3360b57cec5SDimitry Andric                           MFFrame.getObjectSize(I);
3370b57cec5SDimitry Andric       MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
3380b57cec5SDimitry Andric     }
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   uint64_t MaxReach = StackSize + MaxArgOffset;
3410b57cec5SDimitry Andric   if (!isUInt<12>(MaxReach)) {
3420b57cec5SDimitry Andric     // We may need register scavenging slots if some parts of the frame
3430b57cec5SDimitry Andric     // are outside the reach of an unsigned 12-bit displacement.
3440b57cec5SDimitry Andric     // Create 2 for the case where both addresses in an MVC are
3450b57cec5SDimitry Andric     // out of range.
3465ffd83dbSDimitry Andric     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
3475ffd83dbSDimitry Andric     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, Align(8), false));
3480b57cec5SDimitry Andric   }
349e8d8bef9SDimitry Andric 
350e8d8bef9SDimitry Andric   // If R6 is used as an argument register it is still callee saved. If it in
351e8d8bef9SDimitry Andric   // this case is not clobbered (and restored) it should never be marked as
352e8d8bef9SDimitry Andric   // killed.
353e8d8bef9SDimitry Andric   if (MF.front().isLiveIn(SystemZ::R6D) &&
354e8d8bef9SDimitry Andric       ZFI->getRestoreGPRRegs().LowGPR != SystemZ::R6D)
355e8d8bef9SDimitry Andric     for (auto &MO : MRI->use_nodbg_operands(SystemZ::R6D))
356e8d8bef9SDimitry Andric       MO.setIsKill(false);
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
3600b57cec5SDimitry Andric static void emitIncrement(MachineBasicBlock &MBB,
3615ffd83dbSDimitry Andric                           MachineBasicBlock::iterator &MBBI, const DebugLoc &DL,
3625ffd83dbSDimitry Andric                           Register Reg, int64_t NumBytes,
3630b57cec5SDimitry Andric                           const TargetInstrInfo *TII) {
3640b57cec5SDimitry Andric   while (NumBytes) {
3650b57cec5SDimitry Andric     unsigned Opcode;
3660b57cec5SDimitry Andric     int64_t ThisVal = NumBytes;
3670b57cec5SDimitry Andric     if (isInt<16>(NumBytes))
3680b57cec5SDimitry Andric       Opcode = SystemZ::AGHI;
3690b57cec5SDimitry Andric     else {
3700b57cec5SDimitry Andric       Opcode = SystemZ::AGFI;
3710b57cec5SDimitry Andric       // Make sure we maintain 8-byte stack alignment.
3720b57cec5SDimitry Andric       int64_t MinVal = -uint64_t(1) << 31;
3730b57cec5SDimitry Andric       int64_t MaxVal = (int64_t(1) << 31) - 8;
3740b57cec5SDimitry Andric       if (ThisVal < MinVal)
3750b57cec5SDimitry Andric         ThisVal = MinVal;
3760b57cec5SDimitry Andric       else if (ThisVal > MaxVal)
3770b57cec5SDimitry Andric         ThisVal = MaxVal;
3780b57cec5SDimitry Andric     }
3790b57cec5SDimitry Andric     MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
3800b57cec5SDimitry Andric       .addReg(Reg).addImm(ThisVal);
3810b57cec5SDimitry Andric     // The CC implicit def is dead.
3820b57cec5SDimitry Andric     MI->getOperand(3).setIsDead();
3830b57cec5SDimitry Andric     NumBytes -= ThisVal;
3840b57cec5SDimitry Andric   }
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric 
3875ffd83dbSDimitry Andric // Add CFI for the new CFA offset.
3885ffd83dbSDimitry Andric static void buildCFAOffs(MachineBasicBlock &MBB,
3895ffd83dbSDimitry Andric                          MachineBasicBlock::iterator MBBI,
3905ffd83dbSDimitry Andric                          const DebugLoc &DL, int Offset,
3915ffd83dbSDimitry Andric                          const SystemZInstrInfo *ZII) {
3925ffd83dbSDimitry Andric   unsigned CFIIndex = MBB.getParent()->addFrameInst(
3935ffd83dbSDimitry Andric     MCCFIInstruction::cfiDefCfaOffset(nullptr, -Offset));
3945ffd83dbSDimitry Andric   BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
3955ffd83dbSDimitry Andric     .addCFIIndex(CFIIndex);
3965ffd83dbSDimitry Andric }
3975ffd83dbSDimitry Andric 
3985ffd83dbSDimitry Andric // Add CFI for the new frame location.
3995ffd83dbSDimitry Andric static void buildDefCFAReg(MachineBasicBlock &MBB,
4005ffd83dbSDimitry Andric                            MachineBasicBlock::iterator MBBI,
4015ffd83dbSDimitry Andric                            const DebugLoc &DL, unsigned Reg,
4025ffd83dbSDimitry Andric                            const SystemZInstrInfo *ZII) {
4035ffd83dbSDimitry Andric   MachineFunction &MF = *MBB.getParent();
4045ffd83dbSDimitry Andric   MachineModuleInfo &MMI = MF.getMMI();
4055ffd83dbSDimitry Andric   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
4065ffd83dbSDimitry Andric   unsigned RegNum = MRI->getDwarfRegNum(Reg, true);
4075ffd83dbSDimitry Andric   unsigned CFIIndex = MF.addFrameInst(
4085ffd83dbSDimitry Andric                         MCCFIInstruction::createDefCfaRegister(nullptr, RegNum));
4095ffd83dbSDimitry Andric   BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
4105ffd83dbSDimitry Andric     .addCFIIndex(CFIIndex);
4115ffd83dbSDimitry Andric }
4125ffd83dbSDimitry Andric 
4130b57cec5SDimitry Andric void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
4140b57cec5SDimitry Andric                                         MachineBasicBlock &MBB) const {
4150b57cec5SDimitry Andric   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
4165ffd83dbSDimitry Andric   const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
4175ffd83dbSDimitry Andric   const SystemZTargetLowering &TLI = *STI.getTargetLowering();
4180b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
4195ffd83dbSDimitry Andric   auto *ZII = static_cast<const SystemZInstrInfo *>(STI.getInstrInfo());
4200b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
4210b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
4220b57cec5SDimitry Andric   MachineModuleInfo &MMI = MF.getMMI();
4230b57cec5SDimitry Andric   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
4240b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
4250b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
4260b57cec5SDimitry Andric 
427480093f4SDimitry Andric   // In GHC calling convention C stack space, including the ABI-defined
428480093f4SDimitry Andric   // 160-byte base area, is (de)allocated by GHC itself.  This stack space may
429480093f4SDimitry Andric   // be used by LLVM as spill slots for the tail recursive GHC functions.  Thus
430480093f4SDimitry Andric   // do not allocate stack space here, too.
431480093f4SDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::GHC) {
432480093f4SDimitry Andric     if (MFFrame.getStackSize() > 2048 * sizeof(long)) {
433480093f4SDimitry Andric       report_fatal_error(
434480093f4SDimitry Andric           "Pre allocated stack space for GHC function is too small");
435480093f4SDimitry Andric     }
436480093f4SDimitry Andric     if (HasFP) {
437480093f4SDimitry Andric       report_fatal_error(
438480093f4SDimitry Andric           "In GHC calling convention a frame pointer is not supported");
439480093f4SDimitry Andric     }
440*fe6060f1SDimitry Andric     MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::ELFCallFrameSize);
441480093f4SDimitry Andric     return;
442480093f4SDimitry Andric   }
443480093f4SDimitry Andric 
4440b57cec5SDimitry Andric   // Debug location must be unknown since the first debug location is used
4450b57cec5SDimitry Andric   // to determine the end of the prologue.
4460b57cec5SDimitry Andric   DebugLoc DL;
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   // The current offset of the stack pointer from the CFA.
449*fe6060f1SDimitry Andric   int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
4500b57cec5SDimitry Andric 
451480093f4SDimitry Andric   if (ZFI->getSpillGPRRegs().LowGPR) {
4520b57cec5SDimitry Andric     // Skip over the GPR saves.
4530b57cec5SDimitry Andric     if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
4540b57cec5SDimitry Andric       ++MBBI;
4550b57cec5SDimitry Andric     else
4560b57cec5SDimitry Andric       llvm_unreachable("Couldn't skip over GPR saves");
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric     // Add CFI for the GPR saves.
4590b57cec5SDimitry Andric     for (auto &Save : CSI) {
4600b57cec5SDimitry Andric       unsigned Reg = Save.getReg();
4610b57cec5SDimitry Andric       if (SystemZ::GR64BitRegClass.contains(Reg)) {
462480093f4SDimitry Andric         int FI = Save.getFrameIdx();
463480093f4SDimitry Andric         int64_t Offset = MFFrame.getObjectOffset(FI);
4640b57cec5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
4650b57cec5SDimitry Andric             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
4660b57cec5SDimitry Andric         BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
4670b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
4680b57cec5SDimitry Andric       }
4690b57cec5SDimitry Andric     }
4700b57cec5SDimitry Andric   }
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
4730b57cec5SDimitry Andric   // We need to allocate the ABI-defined 160-byte base area whenever
4740b57cec5SDimitry Andric   // we allocate stack space for our own use and whenever we call another
4750b57cec5SDimitry Andric   // function.
476480093f4SDimitry Andric   bool HasStackObject = false;
477480093f4SDimitry Andric   for (unsigned i = 0, e = MFFrame.getObjectIndexEnd(); i != e; ++i)
478480093f4SDimitry Andric     if (!MFFrame.isDeadObjectIndex(i)) {
479480093f4SDimitry Andric       HasStackObject = true;
480480093f4SDimitry Andric       break;
4810b57cec5SDimitry Andric     }
482480093f4SDimitry Andric   if (HasStackObject || MFFrame.hasCalls())
483*fe6060f1SDimitry Andric     StackSize += SystemZMC::ELFCallFrameSize;
484480093f4SDimitry Andric   // Don't allocate the incoming reg save area.
485*fe6060f1SDimitry Andric   StackSize = StackSize > SystemZMC::ELFCallFrameSize
486*fe6060f1SDimitry Andric                   ? StackSize - SystemZMC::ELFCallFrameSize
487480093f4SDimitry Andric                   : 0;
488480093f4SDimitry Andric   MFFrame.setStackSize(StackSize);
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   if (StackSize) {
4910b57cec5SDimitry Andric     // Allocate StackSize bytes.
4920b57cec5SDimitry Andric     int64_t Delta = -int64_t(StackSize);
4935ffd83dbSDimitry Andric     const unsigned ProbeSize = TLI.getStackProbeSize(MF);
4945ffd83dbSDimitry Andric     bool FreeProbe = (ZFI->getSpillGPRRegs().GPROffset &&
4955ffd83dbSDimitry Andric            (ZFI->getSpillGPRRegs().GPROffset + StackSize) < ProbeSize);
4965ffd83dbSDimitry Andric     if (!FreeProbe &&
4975ffd83dbSDimitry Andric         MF.getSubtarget().getTargetLowering()->hasInlineStackProbe(MF)) {
4985ffd83dbSDimitry Andric       // Stack probing may involve looping, but splitting the prologue block
4995ffd83dbSDimitry Andric       // is not possible at this point since it would invalidate the
5005ffd83dbSDimitry Andric       // SaveBlocks / RestoreBlocks sets of PEI in the single block function
5015ffd83dbSDimitry Andric       // case. Build a pseudo to be handled later by inlineStackProbe().
5025ffd83dbSDimitry Andric       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::PROBED_STACKALLOC))
5035ffd83dbSDimitry Andric         .addImm(StackSize);
5045ffd83dbSDimitry Andric     }
5055ffd83dbSDimitry Andric     else {
506e8d8bef9SDimitry Andric       bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
507e8d8bef9SDimitry Andric       // If we need backchain, save current stack pointer.  R1 is free at
508e8d8bef9SDimitry Andric       // this point.
509e8d8bef9SDimitry Andric       if (StoreBackchain)
510e8d8bef9SDimitry Andric         BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
511e8d8bef9SDimitry Andric           .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
5120b57cec5SDimitry Andric       emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
5135ffd83dbSDimitry Andric       buildCFAOffs(MBB, MBBI, DL, SPOffsetFromCFA + Delta, ZII);
514e8d8bef9SDimitry Andric       if (StoreBackchain)
5150b57cec5SDimitry Andric         BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
5165ffd83dbSDimitry Andric           .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
517e8d8bef9SDimitry Andric           .addImm(getBackchainOffset(MF)).addReg(0);
5185ffd83dbSDimitry Andric     }
519e8d8bef9SDimitry Andric     SPOffsetFromCFA += Delta;
5200b57cec5SDimitry Andric   }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   if (HasFP) {
5230b57cec5SDimitry Andric     // Copy the base of the frame to R11.
5240b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
5250b57cec5SDimitry Andric       .addReg(SystemZ::R15D);
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric     // Add CFI for the new frame location.
5285ffd83dbSDimitry Andric     buildDefCFAReg(MBB, MBBI, DL, SystemZ::R11D, ZII);
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric     // Mark the FramePtr as live at the beginning of every block except
5310b57cec5SDimitry Andric     // the entry block.  (We'll have marked R11 as live on entry when
5320b57cec5SDimitry Andric     // saving the GPRs.)
5330b57cec5SDimitry Andric     for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
5340b57cec5SDimitry Andric       I->addLiveIn(SystemZ::R11D);
5350b57cec5SDimitry Andric   }
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric   // Skip over the FPR/VR saves.
5380b57cec5SDimitry Andric   SmallVector<unsigned, 8> CFIIndexes;
5390b57cec5SDimitry Andric   for (auto &Save : CSI) {
5400b57cec5SDimitry Andric     unsigned Reg = Save.getReg();
5410b57cec5SDimitry Andric     if (SystemZ::FP64BitRegClass.contains(Reg)) {
5420b57cec5SDimitry Andric       if (MBBI != MBB.end() &&
5430b57cec5SDimitry Andric           (MBBI->getOpcode() == SystemZ::STD ||
5440b57cec5SDimitry Andric            MBBI->getOpcode() == SystemZ::STDY))
5450b57cec5SDimitry Andric         ++MBBI;
5460b57cec5SDimitry Andric       else
5470b57cec5SDimitry Andric         llvm_unreachable("Couldn't skip over FPR save");
5480b57cec5SDimitry Andric     } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
5490b57cec5SDimitry Andric       if (MBBI != MBB.end() &&
5500b57cec5SDimitry Andric           MBBI->getOpcode() == SystemZ::VST)
5510b57cec5SDimitry Andric         ++MBBI;
5520b57cec5SDimitry Andric       else
5530b57cec5SDimitry Andric         llvm_unreachable("Couldn't skip over VR save");
5540b57cec5SDimitry Andric     } else
5550b57cec5SDimitry Andric       continue;
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric     // Add CFI for the this save.
5580b57cec5SDimitry Andric     unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
5595ffd83dbSDimitry Andric     Register IgnoredFrameReg;
5600b57cec5SDimitry Andric     int64_t Offset =
561e8d8bef9SDimitry Andric         getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg)
562e8d8bef9SDimitry Andric             .getFixed();
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
5650b57cec5SDimitry Andric           nullptr, DwarfReg, SPOffsetFromCFA + Offset));
5660b57cec5SDimitry Andric     CFIIndexes.push_back(CFIIndex);
5670b57cec5SDimitry Andric   }
5680b57cec5SDimitry Andric   // Complete the CFI for the FPR/VR saves, modelling them as taking effect
5690b57cec5SDimitry Andric   // after the last save.
5700b57cec5SDimitry Andric   for (auto CFIIndex : CFIIndexes) {
5710b57cec5SDimitry Andric     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
5720b57cec5SDimitry Andric         .addCFIIndex(CFIIndex);
5730b57cec5SDimitry Andric   }
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
5770b57cec5SDimitry Andric                                         MachineBasicBlock &MBB) const {
5780b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
5790b57cec5SDimitry Andric   auto *ZII =
5800b57cec5SDimitry Andric       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
5810b57cec5SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
5820b57cec5SDimitry Andric   MachineFrameInfo &MFFrame = MF.getFrameInfo();
5830b57cec5SDimitry Andric 
584480093f4SDimitry Andric   // See SystemZFrameLowering::emitPrologue
585480093f4SDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
586480093f4SDimitry Andric     return;
587480093f4SDimitry Andric 
5880b57cec5SDimitry Andric   // Skip the return instruction.
5890b57cec5SDimitry Andric   assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric   uint64_t StackSize = MFFrame.getStackSize();
592480093f4SDimitry Andric   if (ZFI->getRestoreGPRRegs().LowGPR) {
5930b57cec5SDimitry Andric     --MBBI;
5940b57cec5SDimitry Andric     unsigned Opcode = MBBI->getOpcode();
5950b57cec5SDimitry Andric     if (Opcode != SystemZ::LMG)
5960b57cec5SDimitry Andric       llvm_unreachable("Expected to see callee-save register restore code");
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric     unsigned AddrOpNo = 2;
5990b57cec5SDimitry Andric     DebugLoc DL = MBBI->getDebugLoc();
6000b57cec5SDimitry Andric     uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
6010b57cec5SDimitry Andric     unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric     // If the offset is too large, use the largest stack-aligned offset
6040b57cec5SDimitry Andric     // and add the rest to the base register (the stack or frame pointer).
6050b57cec5SDimitry Andric     if (!NewOpcode) {
6060b57cec5SDimitry Andric       uint64_t NumBytes = Offset - 0x7fff8;
6070b57cec5SDimitry Andric       emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
6080b57cec5SDimitry Andric                     NumBytes, ZII);
6090b57cec5SDimitry Andric       Offset -= NumBytes;
6100b57cec5SDimitry Andric       NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
6110b57cec5SDimitry Andric       assert(NewOpcode && "No restore instruction available");
6120b57cec5SDimitry Andric     }
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric     MBBI->setDesc(ZII->get(NewOpcode));
6150b57cec5SDimitry Andric     MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
6160b57cec5SDimitry Andric   } else if (StackSize) {
6170b57cec5SDimitry Andric     DebugLoc DL = MBBI->getDebugLoc();
6180b57cec5SDimitry Andric     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
6190b57cec5SDimitry Andric   }
6200b57cec5SDimitry Andric }
6210b57cec5SDimitry Andric 
6225ffd83dbSDimitry Andric void SystemZFrameLowering::inlineStackProbe(MachineFunction &MF,
6235ffd83dbSDimitry Andric                                             MachineBasicBlock &PrologMBB) const {
6245ffd83dbSDimitry Andric   auto *ZII =
6255ffd83dbSDimitry Andric     static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
6265ffd83dbSDimitry Andric   const SystemZSubtarget &STI = MF.getSubtarget<SystemZSubtarget>();
6275ffd83dbSDimitry Andric   const SystemZTargetLowering &TLI = *STI.getTargetLowering();
6285ffd83dbSDimitry Andric 
6295ffd83dbSDimitry Andric   MachineInstr *StackAllocMI = nullptr;
6305ffd83dbSDimitry Andric   for (MachineInstr &MI : PrologMBB)
6315ffd83dbSDimitry Andric     if (MI.getOpcode() == SystemZ::PROBED_STACKALLOC) {
6325ffd83dbSDimitry Andric       StackAllocMI = &MI;
6335ffd83dbSDimitry Andric       break;
6345ffd83dbSDimitry Andric     }
6355ffd83dbSDimitry Andric   if (StackAllocMI == nullptr)
6365ffd83dbSDimitry Andric     return;
6375ffd83dbSDimitry Andric   uint64_t StackSize = StackAllocMI->getOperand(0).getImm();
6385ffd83dbSDimitry Andric   const unsigned ProbeSize = TLI.getStackProbeSize(MF);
6395ffd83dbSDimitry Andric   uint64_t NumFullBlocks = StackSize / ProbeSize;
6405ffd83dbSDimitry Andric   uint64_t Residual = StackSize % ProbeSize;
641*fe6060f1SDimitry Andric   int64_t SPOffsetFromCFA = -SystemZMC::ELFCFAOffsetFromInitialSP;
6425ffd83dbSDimitry Andric   MachineBasicBlock *MBB = &PrologMBB;
6435ffd83dbSDimitry Andric   MachineBasicBlock::iterator MBBI = StackAllocMI;
6445ffd83dbSDimitry Andric   const DebugLoc DL = StackAllocMI->getDebugLoc();
6455ffd83dbSDimitry Andric 
6465ffd83dbSDimitry Andric   // Allocate a block of Size bytes on the stack and probe it.
6475ffd83dbSDimitry Andric   auto allocateAndProbe = [&](MachineBasicBlock &InsMBB,
6485ffd83dbSDimitry Andric                               MachineBasicBlock::iterator InsPt, unsigned Size,
6495ffd83dbSDimitry Andric                               bool EmitCFI) -> void {
6505ffd83dbSDimitry Andric     emitIncrement(InsMBB, InsPt, DL, SystemZ::R15D, -int64_t(Size), ZII);
6515ffd83dbSDimitry Andric     if (EmitCFI) {
6525ffd83dbSDimitry Andric       SPOffsetFromCFA -= Size;
6535ffd83dbSDimitry Andric       buildCFAOffs(InsMBB, InsPt, DL, SPOffsetFromCFA, ZII);
6545ffd83dbSDimitry Andric     }
6555ffd83dbSDimitry Andric     // Probe by means of a volatile compare.
6565ffd83dbSDimitry Andric     MachineMemOperand *MMO = MF.getMachineMemOperand(MachinePointerInfo(),
6575ffd83dbSDimitry Andric       MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad, 8, Align(1));
6585ffd83dbSDimitry Andric     BuildMI(InsMBB, InsPt, DL, ZII->get(SystemZ::CG))
6595ffd83dbSDimitry Andric       .addReg(SystemZ::R0D, RegState::Undef)
6605ffd83dbSDimitry Andric       .addReg(SystemZ::R15D).addImm(Size - 8).addReg(0)
6615ffd83dbSDimitry Andric       .addMemOperand(MMO);
6625ffd83dbSDimitry Andric   };
6635ffd83dbSDimitry Andric 
664e8d8bef9SDimitry Andric   bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
665e8d8bef9SDimitry Andric   if (StoreBackchain)
666e8d8bef9SDimitry Andric     BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR))
667e8d8bef9SDimitry Andric       .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
668e8d8bef9SDimitry Andric 
669e8d8bef9SDimitry Andric   MachineBasicBlock *DoneMBB = nullptr;
670e8d8bef9SDimitry Andric   MachineBasicBlock *LoopMBB = nullptr;
6715ffd83dbSDimitry Andric   if (NumFullBlocks < 3) {
6725ffd83dbSDimitry Andric     // Emit unrolled probe statements.
6735ffd83dbSDimitry Andric     for (unsigned int i = 0; i < NumFullBlocks; i++)
6745ffd83dbSDimitry Andric       allocateAndProbe(*MBB, MBBI, ProbeSize, true/*EmitCFI*/);
6755ffd83dbSDimitry Andric   } else {
6765ffd83dbSDimitry Andric     // Emit a loop probing the pages.
6775ffd83dbSDimitry Andric     uint64_t LoopAlloc = ProbeSize * NumFullBlocks;
6785ffd83dbSDimitry Andric     SPOffsetFromCFA -= LoopAlloc;
6795ffd83dbSDimitry Andric 
680e8d8bef9SDimitry Andric     // Use R0D to hold the exit value.
681e8d8bef9SDimitry Andric     BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R0D)
6825ffd83dbSDimitry Andric       .addReg(SystemZ::R15D);
683e8d8bef9SDimitry Andric     buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R0D, ZII);
684e8d8bef9SDimitry Andric     emitIncrement(*MBB, MBBI, DL, SystemZ::R0D, -int64_t(LoopAlloc), ZII);
685*fe6060f1SDimitry Andric     buildCFAOffs(*MBB, MBBI, DL, -int64_t(SystemZMC::ELFCallFrameSize + LoopAlloc),
6865ffd83dbSDimitry Andric                  ZII);
6875ffd83dbSDimitry Andric 
688e8d8bef9SDimitry Andric     DoneMBB = SystemZ::splitBlockBefore(MBBI, MBB);
689e8d8bef9SDimitry Andric     LoopMBB = SystemZ::emitBlockAfter(MBB);
6905ffd83dbSDimitry Andric     MBB->addSuccessor(LoopMBB);
6915ffd83dbSDimitry Andric     LoopMBB->addSuccessor(LoopMBB);
6925ffd83dbSDimitry Andric     LoopMBB->addSuccessor(DoneMBB);
6935ffd83dbSDimitry Andric 
6945ffd83dbSDimitry Andric     MBB = LoopMBB;
6955ffd83dbSDimitry Andric     allocateAndProbe(*MBB, MBB->end(), ProbeSize, false/*EmitCFI*/);
6965ffd83dbSDimitry Andric     BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::CLGR))
697e8d8bef9SDimitry Andric       .addReg(SystemZ::R15D).addReg(SystemZ::R0D);
6985ffd83dbSDimitry Andric     BuildMI(*MBB, MBB->end(), DL, ZII->get(SystemZ::BRC))
6995ffd83dbSDimitry Andric       .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_GT).addMBB(MBB);
7005ffd83dbSDimitry Andric 
7015ffd83dbSDimitry Andric     MBB = DoneMBB;
7025ffd83dbSDimitry Andric     MBBI = DoneMBB->begin();
7035ffd83dbSDimitry Andric     buildDefCFAReg(*MBB, MBBI, DL, SystemZ::R15D, ZII);
7045ffd83dbSDimitry Andric   }
7055ffd83dbSDimitry Andric 
7065ffd83dbSDimitry Andric   if (Residual)
7075ffd83dbSDimitry Andric     allocateAndProbe(*MBB, MBBI, Residual, true/*EmitCFI*/);
7085ffd83dbSDimitry Andric 
709e8d8bef9SDimitry Andric   if (StoreBackchain)
710e8d8bef9SDimitry Andric     BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::STG))
711e8d8bef9SDimitry Andric       .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
712e8d8bef9SDimitry Andric       .addImm(getBackchainOffset(MF)).addReg(0);
713e8d8bef9SDimitry Andric 
7145ffd83dbSDimitry Andric   StackAllocMI->eraseFromParent();
715e8d8bef9SDimitry Andric   if (DoneMBB != nullptr) {
716e8d8bef9SDimitry Andric     // Compute the live-in lists for the new blocks.
717e8d8bef9SDimitry Andric     recomputeLiveIns(*DoneMBB);
718e8d8bef9SDimitry Andric     recomputeLiveIns(*LoopMBB);
719e8d8bef9SDimitry Andric   }
7205ffd83dbSDimitry Andric }
7215ffd83dbSDimitry Andric 
7220b57cec5SDimitry Andric bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const {
7230b57cec5SDimitry Andric   return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
7240b57cec5SDimitry Andric           MF.getFrameInfo().hasVarSizedObjects() ||
7250b57cec5SDimitry Andric           MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP());
7260b57cec5SDimitry Andric }
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric bool
7290b57cec5SDimitry Andric SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
7300b57cec5SDimitry Andric   // The ABI requires us to allocate 160 bytes of stack space for the callee,
7310b57cec5SDimitry Andric   // with any outgoing stack arguments being placed above that.  It seems
7320b57cec5SDimitry Andric   // better to make that area a permanent feature of the frame even if
7330b57cec5SDimitry Andric   // we're using a frame pointer.
7340b57cec5SDimitry Andric   return true;
7350b57cec5SDimitry Andric }
7360b57cec5SDimitry Andric 
737e8d8bef9SDimitry Andric StackOffset
738e8d8bef9SDimitry Andric SystemZFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
7395ffd83dbSDimitry Andric                                              Register &FrameReg) const {
740*fe6060f1SDimitry Andric   // Our incoming SP is actually SystemZMC::ELFCallFrameSize below the CFA, so
741480093f4SDimitry Andric   // add that difference here.
742e8d8bef9SDimitry Andric   StackOffset Offset =
743480093f4SDimitry Andric       TargetFrameLowering::getFrameIndexReference(MF, FI, FrameReg);
744*fe6060f1SDimitry Andric   return Offset + StackOffset::getFixed(SystemZMC::ELFCallFrameSize);
745480093f4SDimitry Andric }
746480093f4SDimitry Andric 
7470b57cec5SDimitry Andric MachineBasicBlock::iterator SystemZFrameLowering::
7480b57cec5SDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF,
7490b57cec5SDimitry Andric                               MachineBasicBlock &MBB,
7500b57cec5SDimitry Andric                               MachineBasicBlock::iterator MI) const {
7510b57cec5SDimitry Andric   switch (MI->getOpcode()) {
7520b57cec5SDimitry Andric   case SystemZ::ADJCALLSTACKDOWN:
7530b57cec5SDimitry Andric   case SystemZ::ADJCALLSTACKUP:
7540b57cec5SDimitry Andric     assert(hasReservedCallFrame(MF) &&
7550b57cec5SDimitry Andric            "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
7560b57cec5SDimitry Andric     return MBB.erase(MI);
7570b57cec5SDimitry Andric     break;
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   default:
7600b57cec5SDimitry Andric     llvm_unreachable("Unexpected call frame instruction");
7610b57cec5SDimitry Andric   }
7620b57cec5SDimitry Andric }
763480093f4SDimitry Andric 
7645ffd83dbSDimitry Andric unsigned SystemZFrameLowering::getRegSpillOffset(MachineFunction &MF,
7655ffd83dbSDimitry Andric                                                  Register Reg) const {
7665ffd83dbSDimitry Andric   bool IsVarArg = MF.getFunction().isVarArg();
7675ffd83dbSDimitry Andric   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
7685ffd83dbSDimitry Andric   bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
7695ffd83dbSDimitry Andric   unsigned Offset = RegSpillOffsets[Reg];
7705ffd83dbSDimitry Andric   if (usePackedStack(MF) && !(IsVarArg && !SoftFloat)) {
7715ffd83dbSDimitry Andric     if (SystemZ::GR64BitRegClass.contains(Reg))
7725ffd83dbSDimitry Andric       // Put all GPRs at the top of the Register save area with packed
7735ffd83dbSDimitry Andric       // stack. Make room for the backchain if needed.
7745ffd83dbSDimitry Andric       Offset += BackChain ? 24 : 32;
7755ffd83dbSDimitry Andric     else
7765ffd83dbSDimitry Andric       Offset = 0;
7775ffd83dbSDimitry Andric   }
7785ffd83dbSDimitry Andric   return Offset;
7795ffd83dbSDimitry Andric }
7805ffd83dbSDimitry Andric 
781480093f4SDimitry Andric int SystemZFrameLowering::
782480093f4SDimitry Andric getOrCreateFramePointerSaveIndex(MachineFunction &MF) const {
783480093f4SDimitry Andric   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
784480093f4SDimitry Andric   int FI = ZFI->getFramePointerSaveIndex();
785480093f4SDimitry Andric   if (!FI) {
786480093f4SDimitry Andric     MachineFrameInfo &MFFrame = MF.getFrameInfo();
787*fe6060f1SDimitry Andric     int Offset = getBackchainOffset(MF) - SystemZMC::ELFCallFrameSize;
7885ffd83dbSDimitry Andric     FI = MFFrame.CreateFixedObject(8, Offset, false);
789480093f4SDimitry Andric     ZFI->setFramePointerSaveIndex(FI);
790480093f4SDimitry Andric   }
791480093f4SDimitry Andric   return FI;
792480093f4SDimitry Andric }
7935ffd83dbSDimitry Andric 
7945ffd83dbSDimitry Andric bool SystemZFrameLowering::usePackedStack(MachineFunction &MF) const {
7955ffd83dbSDimitry Andric   bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack");
7965ffd83dbSDimitry Andric   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
7975ffd83dbSDimitry Andric   bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
7985ffd83dbSDimitry Andric   if (HasPackedStackAttr && BackChain && !SoftFloat)
7995ffd83dbSDimitry Andric     report_fatal_error("packed-stack + backchain + hard-float is unsupported.");
8005ffd83dbSDimitry Andric   bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC;
8015ffd83dbSDimitry Andric   return HasPackedStackAttr && CallConv;
8025ffd83dbSDimitry Andric }
803