1 //===-- SystemZFrameLowering.cpp - Frame lowering for SystemZ -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SystemZFrameLowering.h"
10 #include "SystemZCallingConv.h"
11 #include "SystemZInstrBuilder.h"
12 #include "SystemZInstrInfo.h"
13 #include "SystemZMachineFunctionInfo.h"
14 #include "SystemZRegisterInfo.h"
15 #include "SystemZSubtarget.h"
16 #include "llvm/CodeGen/MachineModuleInfo.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/RegisterScavenging.h"
19 #include "llvm/IR/Function.h"
20 
21 using namespace llvm;
22 
23 namespace {
24 // The ABI-defined register save slots, relative to the CFA (i.e.
25 // incoming stack pointer + SystemZMC::CallFrameSize).
26 static const TargetFrameLowering::SpillSlot SpillOffsetTable[] = {
27   { SystemZ::R2D,  0x10 },
28   { SystemZ::R3D,  0x18 },
29   { SystemZ::R4D,  0x20 },
30   { SystemZ::R5D,  0x28 },
31   { SystemZ::R6D,  0x30 },
32   { SystemZ::R7D,  0x38 },
33   { SystemZ::R8D,  0x40 },
34   { SystemZ::R9D,  0x48 },
35   { SystemZ::R10D, 0x50 },
36   { SystemZ::R11D, 0x58 },
37   { SystemZ::R12D, 0x60 },
38   { SystemZ::R13D, 0x68 },
39   { SystemZ::R14D, 0x70 },
40   { SystemZ::R15D, 0x78 },
41   { SystemZ::F0D,  0x80 },
42   { SystemZ::F2D,  0x88 },
43   { SystemZ::F4D,  0x90 },
44   { SystemZ::F6D,  0x98 }
45 };
46 } // end anonymous namespace
47 
48 SystemZFrameLowering::SystemZFrameLowering()
49     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(8),
50                           0, Align(8), false /* StackRealignable */),
51       RegSpillOffsets(0) {
52   // Due to the SystemZ ABI, the DWARF CFA (Canonical Frame Address) is not
53   // equal to the incoming stack pointer, but to incoming stack pointer plus
54   // 160.  Instead of using a Local Area Offset, the Register save area will
55   // be occupied by fixed frame objects, and all offsets are actually
56   // relative to CFA.
57 
58   // Create a mapping from register number to save slot offset.
59   // These offsets are relative to the start of the register save area.
60   RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
61   for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I)
62     RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset;
63 }
64 
65 bool SystemZFrameLowering::
66 assignCalleeSavedSpillSlots(MachineFunction &MF,
67                             const TargetRegisterInfo *TRI,
68                             std::vector<CalleeSavedInfo> &CSI) const {
69   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
70   MachineFrameInfo &MFFrame = MF.getFrameInfo();
71   bool IsVarArg = MF.getFunction().isVarArg();
72   if (CSI.empty())
73     return true; // Early exit if no callee saved registers are modified!
74 
75   unsigned LowGPR = 0;
76   unsigned HighGPR = SystemZ::R15D;
77   int StartSPOffset = SystemZMC::CallFrameSize;
78   for (auto &CS : CSI) {
79     unsigned Reg = CS.getReg();
80     int Offset = getRegSpillOffset(MF, Reg);
81     if (Offset) {
82       if (SystemZ::GR64BitRegClass.contains(Reg) && StartSPOffset > Offset) {
83         LowGPR = Reg;
84         StartSPOffset = Offset;
85       }
86       Offset -= SystemZMC::CallFrameSize;
87       int FrameIdx = MFFrame.CreateFixedSpillStackObject(8, Offset);
88       CS.setFrameIdx(FrameIdx);
89     } else
90       CS.setFrameIdx(INT32_MAX);
91   }
92 
93   // Save the range of call-saved registers, for use by the
94   // prologue/epilogue inserters.
95   ZFI->setRestoreGPRRegs(LowGPR, HighGPR, StartSPOffset);
96   if (IsVarArg) {
97     // Also save the GPR varargs, if any.  R6D is call-saved, so would
98     // already be included, but we also need to handle the call-clobbered
99     // argument registers.
100     unsigned FirstGPR = ZFI->getVarArgsFirstGPR();
101     if (FirstGPR < SystemZ::NumArgGPRs) {
102       unsigned Reg = SystemZ::ArgGPRs[FirstGPR];
103       int Offset = getRegSpillOffset(MF, Reg);
104       if (StartSPOffset > Offset) {
105         LowGPR = Reg; StartSPOffset = Offset;
106       }
107     }
108   }
109   ZFI->setSpillGPRRegs(LowGPR, HighGPR, StartSPOffset);
110 
111   // Create fixed stack objects for the remaining registers.
112   int CurrOffset = -SystemZMC::CallFrameSize;
113   if (usePackedStack(MF))
114     CurrOffset += StartSPOffset;
115 
116   for (auto &CS : CSI) {
117     if (CS.getFrameIdx() != INT32_MAX)
118       continue;
119     unsigned Reg = CS.getReg();
120     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
121     unsigned Size = TRI->getSpillSize(*RC);
122     CurrOffset -= Size;
123     assert(CurrOffset % 8 == 0 &&
124            "8-byte alignment required for for all register save slots");
125     int FrameIdx = MFFrame.CreateFixedSpillStackObject(Size, CurrOffset);
126     CS.setFrameIdx(FrameIdx);
127   }
128 
129   return true;
130 }
131 
132 void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF,
133                                                 BitVector &SavedRegs,
134                                                 RegScavenger *RS) const {
135   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
136 
137   MachineFrameInfo &MFFrame = MF.getFrameInfo();
138   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
139   bool HasFP = hasFP(MF);
140   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
141   bool IsVarArg = MF.getFunction().isVarArg();
142 
143   // va_start stores incoming FPR varargs in the normal way, but delegates
144   // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
145   // Record these pending uses, which typically include the call-saved
146   // argument register R6D.
147   if (IsVarArg)
148     for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
149       SavedRegs.set(SystemZ::ArgGPRs[I]);
150 
151   // If there are any landing pads, entering them will modify r6/r7.
152   if (!MF.getLandingPads().empty()) {
153     SavedRegs.set(SystemZ::R6D);
154     SavedRegs.set(SystemZ::R7D);
155   }
156 
157   // If the function requires a frame pointer, record that the hard
158   // frame pointer will be clobbered.
159   if (HasFP)
160     SavedRegs.set(SystemZ::R11D);
161 
162   // If the function calls other functions, record that the return
163   // address register will be clobbered.
164   if (MFFrame.hasCalls())
165     SavedRegs.set(SystemZ::R14D);
166 
167   // If we are saving GPRs other than the stack pointer, we might as well
168   // save and restore the stack pointer at the same time, via STMG and LMG.
169   // This allows the deallocation to be done by the LMG, rather than needing
170   // a separate %r15 addition.
171   const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
172   for (unsigned I = 0; CSRegs[I]; ++I) {
173     unsigned Reg = CSRegs[I];
174     if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
175       SavedRegs.set(SystemZ::R15D);
176       break;
177     }
178   }
179 }
180 
181 // Add GPR64 to the save instruction being built by MIB, which is in basic
182 // block MBB.  IsImplicit says whether this is an explicit operand to the
183 // instruction, or an implicit one that comes between the explicit start
184 // and end registers.
185 static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
186                         unsigned GPR64, bool IsImplicit) {
187   const TargetRegisterInfo *RI =
188       MBB.getParent()->getSubtarget().getRegisterInfo();
189   Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
190   bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
191   if (!IsLive || !IsImplicit) {
192     MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
193     if (!IsLive)
194       MBB.addLiveIn(GPR64);
195   }
196 }
197 
198 bool SystemZFrameLowering::spillCalleeSavedRegisters(
199     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
200     ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const {
201   if (CSI.empty())
202     return false;
203 
204   MachineFunction &MF = *MBB.getParent();
205   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
206   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
207   bool IsVarArg = MF.getFunction().isVarArg();
208   DebugLoc DL;
209 
210   // Save GPRs
211   SystemZ::GPRRegs SpillGPRs = ZFI->getSpillGPRRegs();
212   if (SpillGPRs.LowGPR) {
213     assert(SpillGPRs.LowGPR != SpillGPRs.HighGPR &&
214            "Should be saving %r15 and something else");
215 
216     // Build an STMG instruction.
217     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
218 
219     // Add the explicit register operands.
220     addSavedGPR(MBB, MIB, SpillGPRs.LowGPR, false);
221     addSavedGPR(MBB, MIB, SpillGPRs.HighGPR, false);
222 
223     // Add the address.
224     MIB.addReg(SystemZ::R15D).addImm(SpillGPRs.GPROffset);
225 
226     // Make sure all call-saved GPRs are included as operands and are
227     // marked as live on entry.
228     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
229       unsigned Reg = CSI[I].getReg();
230       if (SystemZ::GR64BitRegClass.contains(Reg))
231         addSavedGPR(MBB, MIB, Reg, true);
232     }
233 
234     // ...likewise GPR varargs.
235     if (IsVarArg)
236       for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
237         addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true);
238   }
239 
240   // Save FPRs/VRs in the normal TargetInstrInfo way.
241   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
242     unsigned Reg = CSI[I].getReg();
243     if (SystemZ::FP64BitRegClass.contains(Reg)) {
244       MBB.addLiveIn(Reg);
245       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
246                                &SystemZ::FP64BitRegClass, TRI);
247     }
248     if (SystemZ::VR128BitRegClass.contains(Reg)) {
249       MBB.addLiveIn(Reg);
250       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
251                                &SystemZ::VR128BitRegClass, TRI);
252     }
253   }
254 
255   return true;
256 }
257 
258 bool SystemZFrameLowering::
259 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
260                             MachineBasicBlock::iterator MBBI,
261                             std::vector<CalleeSavedInfo> &CSI,
262                             const TargetRegisterInfo *TRI) const {
263   if (CSI.empty())
264     return false;
265 
266   MachineFunction &MF = *MBB.getParent();
267   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
268   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
269   bool HasFP = hasFP(MF);
270   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
271 
272   // Restore FPRs/VRs in the normal TargetInstrInfo way.
273   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
274     unsigned Reg = CSI[I].getReg();
275     if (SystemZ::FP64BitRegClass.contains(Reg))
276       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
277                                 &SystemZ::FP64BitRegClass, TRI);
278     if (SystemZ::VR128BitRegClass.contains(Reg))
279       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
280                                 &SystemZ::VR128BitRegClass, TRI);
281   }
282 
283   // Restore call-saved GPRs (but not call-clobbered varargs, which at
284   // this point might hold return values).
285   SystemZ::GPRRegs RestoreGPRs = ZFI->getRestoreGPRRegs();
286   if (RestoreGPRs.LowGPR) {
287     // If we saved any of %r2-%r5 as varargs, we should also be saving
288     // and restoring %r6.  If we're saving %r6 or above, we should be
289     // restoring it too.
290     assert(RestoreGPRs.LowGPR != RestoreGPRs.HighGPR &&
291            "Should be loading %r15 and something else");
292 
293     // Build an LMG instruction.
294     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
295 
296     // Add the explicit register operands.
297     MIB.addReg(RestoreGPRs.LowGPR, RegState::Define);
298     MIB.addReg(RestoreGPRs.HighGPR, RegState::Define);
299 
300     // Add the address.
301     MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
302     MIB.addImm(RestoreGPRs.GPROffset);
303 
304     // Do a second scan adding regs as being defined by instruction
305     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
306       unsigned Reg = CSI[I].getReg();
307       if (Reg != RestoreGPRs.LowGPR && Reg != RestoreGPRs.HighGPR &&
308           SystemZ::GR64BitRegClass.contains(Reg))
309         MIB.addReg(Reg, RegState::ImplicitDefine);
310     }
311   }
312 
313   return true;
314 }
315 
316 void SystemZFrameLowering::
317 processFunctionBeforeFrameFinalized(MachineFunction &MF,
318                                     RegScavenger *RS) const {
319   MachineFrameInfo &MFFrame = MF.getFrameInfo();
320 
321   if (!usePackedStack(MF))
322     // Always create the full incoming register save area.
323     getOrCreateFramePointerSaveIndex(MF);
324 
325   // Get the size of our stack frame to be allocated ...
326   uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
327                         SystemZMC::CallFrameSize);
328   // ... and the maximum offset we may need to reach into the
329   // caller's frame to access the save area or stack arguments.
330   int64_t MaxArgOffset = 0;
331   for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
332     if (MFFrame.getObjectOffset(I) >= 0) {
333       int64_t ArgOffset = MFFrame.getObjectOffset(I) +
334                           MFFrame.getObjectSize(I);
335       MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
336     }
337 
338   uint64_t MaxReach = StackSize + MaxArgOffset;
339   if (!isUInt<12>(MaxReach)) {
340     // We may need register scavenging slots if some parts of the frame
341     // are outside the reach of an unsigned 12-bit displacement.
342     // Create 2 for the case where both addresses in an MVC are
343     // out of range.
344     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
345     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
346   }
347 }
348 
349 // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
350 static void emitIncrement(MachineBasicBlock &MBB,
351                           MachineBasicBlock::iterator &MBBI,
352                           const DebugLoc &DL,
353                           unsigned Reg, int64_t NumBytes,
354                           const TargetInstrInfo *TII) {
355   while (NumBytes) {
356     unsigned Opcode;
357     int64_t ThisVal = NumBytes;
358     if (isInt<16>(NumBytes))
359       Opcode = SystemZ::AGHI;
360     else {
361       Opcode = SystemZ::AGFI;
362       // Make sure we maintain 8-byte stack alignment.
363       int64_t MinVal = -uint64_t(1) << 31;
364       int64_t MaxVal = (int64_t(1) << 31) - 8;
365       if (ThisVal < MinVal)
366         ThisVal = MinVal;
367       else if (ThisVal > MaxVal)
368         ThisVal = MaxVal;
369     }
370     MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
371       .addReg(Reg).addImm(ThisVal);
372     // The CC implicit def is dead.
373     MI->getOperand(3).setIsDead();
374     NumBytes -= ThisVal;
375   }
376 }
377 
378 void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
379                                         MachineBasicBlock &MBB) const {
380   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
381   MachineFrameInfo &MFFrame = MF.getFrameInfo();
382   auto *ZII =
383       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
384   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
385   MachineBasicBlock::iterator MBBI = MBB.begin();
386   MachineModuleInfo &MMI = MF.getMMI();
387   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
388   const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
389   bool HasFP = hasFP(MF);
390 
391   // In GHC calling convention C stack space, including the ABI-defined
392   // 160-byte base area, is (de)allocated by GHC itself.  This stack space may
393   // be used by LLVM as spill slots for the tail recursive GHC functions.  Thus
394   // do not allocate stack space here, too.
395   if (MF.getFunction().getCallingConv() == CallingConv::GHC) {
396     if (MFFrame.getStackSize() > 2048 * sizeof(long)) {
397       report_fatal_error(
398           "Pre allocated stack space for GHC function is too small");
399     }
400     if (HasFP) {
401       report_fatal_error(
402           "In GHC calling convention a frame pointer is not supported");
403     }
404     MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::CallFrameSize);
405     return;
406   }
407 
408   // Debug location must be unknown since the first debug location is used
409   // to determine the end of the prologue.
410   DebugLoc DL;
411 
412   // The current offset of the stack pointer from the CFA.
413   int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP;
414 
415   if (ZFI->getSpillGPRRegs().LowGPR) {
416     // Skip over the GPR saves.
417     if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
418       ++MBBI;
419     else
420       llvm_unreachable("Couldn't skip over GPR saves");
421 
422     // Add CFI for the GPR saves.
423     for (auto &Save : CSI) {
424       unsigned Reg = Save.getReg();
425       if (SystemZ::GR64BitRegClass.contains(Reg)) {
426         int FI = Save.getFrameIdx();
427         int64_t Offset = MFFrame.getObjectOffset(FI);
428         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
429             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
430         BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
431             .addCFIIndex(CFIIndex);
432       }
433     }
434   }
435 
436   uint64_t StackSize = MFFrame.getStackSize();
437   // We need to allocate the ABI-defined 160-byte base area whenever
438   // we allocate stack space for our own use and whenever we call another
439   // function.
440   bool HasStackObject = false;
441   for (unsigned i = 0, e = MFFrame.getObjectIndexEnd(); i != e; ++i)
442     if (!MFFrame.isDeadObjectIndex(i)) {
443       HasStackObject = true;
444       break;
445     }
446   if (HasStackObject || MFFrame.hasCalls())
447     StackSize += SystemZMC::CallFrameSize;
448   // Don't allocate the incoming reg save area.
449   StackSize = StackSize > SystemZMC::CallFrameSize
450                   ? StackSize - SystemZMC::CallFrameSize
451                   : 0;
452   MFFrame.setStackSize(StackSize);
453 
454   if (StackSize) {
455     // Determine if we want to store a backchain.
456     bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
457 
458     // If we need backchain, save current stack pointer.  R1 is free at this
459     // point.
460     if (StoreBackchain)
461       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
462         .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
463 
464     // Allocate StackSize bytes.
465     int64_t Delta = -int64_t(StackSize);
466     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
467 
468     // Add CFI for the allocation.
469     unsigned CFIIndex = MF.addFrameInst(
470         MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta));
471     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
472         .addCFIIndex(CFIIndex);
473     SPOffsetFromCFA += Delta;
474 
475     if (StoreBackchain) {
476       // The back chain is stored topmost with packed-stack.
477       int Offset = usePackedStack(MF) ? SystemZMC::CallFrameSize - 8 : 0;
478       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
479         .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D)
480         .addImm(Offset).addReg(0);
481     }
482   }
483 
484   if (HasFP) {
485     // Copy the base of the frame to R11.
486     BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
487       .addReg(SystemZ::R15D);
488 
489     // Add CFI for the new frame location.
490     unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true);
491     unsigned CFIIndex = MF.addFrameInst(
492         MCCFIInstruction::createDefCfaRegister(nullptr, HardFP));
493     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
494         .addCFIIndex(CFIIndex);
495 
496     // Mark the FramePtr as live at the beginning of every block except
497     // the entry block.  (We'll have marked R11 as live on entry when
498     // saving the GPRs.)
499     for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
500       I->addLiveIn(SystemZ::R11D);
501   }
502 
503   // Skip over the FPR/VR saves.
504   SmallVector<unsigned, 8> CFIIndexes;
505   for (auto &Save : CSI) {
506     unsigned Reg = Save.getReg();
507     if (SystemZ::FP64BitRegClass.contains(Reg)) {
508       if (MBBI != MBB.end() &&
509           (MBBI->getOpcode() == SystemZ::STD ||
510            MBBI->getOpcode() == SystemZ::STDY))
511         ++MBBI;
512       else
513         llvm_unreachable("Couldn't skip over FPR save");
514     } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
515       if (MBBI != MBB.end() &&
516           MBBI->getOpcode() == SystemZ::VST)
517         ++MBBI;
518       else
519         llvm_unreachable("Couldn't skip over VR save");
520     } else
521       continue;
522 
523     // Add CFI for the this save.
524     unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
525     unsigned IgnoredFrameReg;
526     int64_t Offset =
527         getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg);
528 
529     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
530           nullptr, DwarfReg, SPOffsetFromCFA + Offset));
531     CFIIndexes.push_back(CFIIndex);
532   }
533   // Complete the CFI for the FPR/VR saves, modelling them as taking effect
534   // after the last save.
535   for (auto CFIIndex : CFIIndexes) {
536     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
537         .addCFIIndex(CFIIndex);
538   }
539 }
540 
541 void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
542                                         MachineBasicBlock &MBB) const {
543   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
544   auto *ZII =
545       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
546   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
547   MachineFrameInfo &MFFrame = MF.getFrameInfo();
548 
549   // See SystemZFrameLowering::emitPrologue
550   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
551     return;
552 
553   // Skip the return instruction.
554   assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
555 
556   uint64_t StackSize = MFFrame.getStackSize();
557   if (ZFI->getRestoreGPRRegs().LowGPR) {
558     --MBBI;
559     unsigned Opcode = MBBI->getOpcode();
560     if (Opcode != SystemZ::LMG)
561       llvm_unreachable("Expected to see callee-save register restore code");
562 
563     unsigned AddrOpNo = 2;
564     DebugLoc DL = MBBI->getDebugLoc();
565     uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
566     unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
567 
568     // If the offset is too large, use the largest stack-aligned offset
569     // and add the rest to the base register (the stack or frame pointer).
570     if (!NewOpcode) {
571       uint64_t NumBytes = Offset - 0x7fff8;
572       emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
573                     NumBytes, ZII);
574       Offset -= NumBytes;
575       NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
576       assert(NewOpcode && "No restore instruction available");
577     }
578 
579     MBBI->setDesc(ZII->get(NewOpcode));
580     MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
581   } else if (StackSize) {
582     DebugLoc DL = MBBI->getDebugLoc();
583     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
584   }
585 }
586 
587 bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const {
588   return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
589           MF.getFrameInfo().hasVarSizedObjects() ||
590           MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP());
591 }
592 
593 bool
594 SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
595   // The ABI requires us to allocate 160 bytes of stack space for the callee,
596   // with any outgoing stack arguments being placed above that.  It seems
597   // better to make that area a permanent feature of the frame even if
598   // we're using a frame pointer.
599   return true;
600 }
601 
602 int SystemZFrameLowering::getFrameIndexReference(const MachineFunction &MF,
603                                                  int FI,
604                                                  unsigned &FrameReg) const {
605   // Our incoming SP is actually SystemZMC::CallFrameSize below the CFA, so
606   // add that difference here.
607   int64_t Offset =
608     TargetFrameLowering::getFrameIndexReference(MF, FI, FrameReg);
609   return Offset + SystemZMC::CallFrameSize;
610 }
611 
612 MachineBasicBlock::iterator SystemZFrameLowering::
613 eliminateCallFramePseudoInstr(MachineFunction &MF,
614                               MachineBasicBlock &MBB,
615                               MachineBasicBlock::iterator MI) const {
616   switch (MI->getOpcode()) {
617   case SystemZ::ADJCALLSTACKDOWN:
618   case SystemZ::ADJCALLSTACKUP:
619     assert(hasReservedCallFrame(MF) &&
620            "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
621     return MBB.erase(MI);
622     break;
623 
624   default:
625     llvm_unreachable("Unexpected call frame instruction");
626   }
627 }
628 
629 unsigned SystemZFrameLowering::getRegSpillOffset(MachineFunction &MF,
630                                                  unsigned Reg) const {
631   bool IsVarArg = MF.getFunction().isVarArg();
632   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
633   bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
634   unsigned Offset = RegSpillOffsets[Reg];
635   if (usePackedStack(MF) && !(IsVarArg && !SoftFloat)) {
636     if (SystemZ::GR64BitRegClass.contains(Reg))
637       // Put all GPRs at the top of the Register save area with packed
638       // stack. Make room for the backchain if needed.
639       Offset += BackChain ? 24 : 32;
640     else
641       Offset = 0;
642   }
643   return Offset;
644 }
645 
646 int SystemZFrameLowering::
647 getOrCreateFramePointerSaveIndex(MachineFunction &MF) const {
648   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
649   int FI = ZFI->getFramePointerSaveIndex();
650   if (!FI) {
651     MachineFrameInfo &MFFrame = MF.getFrameInfo();
652     // The back chain is stored topmost with packed-stack.
653     int Offset = usePackedStack(MF) ? -8 : -SystemZMC::CallFrameSize;
654     FI = MFFrame.CreateFixedObject(8, Offset, false);
655     ZFI->setFramePointerSaveIndex(FI);
656   }
657   return FI;
658 }
659 
660 bool SystemZFrameLowering::usePackedStack(MachineFunction &MF) const {
661   bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack");
662   bool BackChain = MF.getFunction().hasFnAttribute("backchain");
663   bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
664   if (HasPackedStackAttr && BackChain && !SoftFloat)
665     report_fatal_error("packed-stack + backchain + hard-float is unsupported.");
666   bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC;
667   return HasPackedStackAttr && CallConv;
668 }
669