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 incoming stack
25 // pointer.
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                           -SystemZMC::CallFrameSize, Align(8),
51                           false /* StackRealignable */) {
52   // Create a mapping from register number to save slot offset.
53   RegSpillOffsets.grow(SystemZ::NUM_TARGET_REGS);
54   for (unsigned I = 0, E = array_lengthof(SpillOffsetTable); I != E; ++I)
55     RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset;
56 }
57 
58 const TargetFrameLowering::SpillSlot *
59 SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const {
60   NumEntries = array_lengthof(SpillOffsetTable);
61   return SpillOffsetTable;
62 }
63 
64 void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF,
65                                                 BitVector &SavedRegs,
66                                                 RegScavenger *RS) const {
67   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
68 
69   MachineFrameInfo &MFFrame = MF.getFrameInfo();
70   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
71   bool HasFP = hasFP(MF);
72   SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
73   bool IsVarArg = MF.getFunction().isVarArg();
74 
75   // va_start stores incoming FPR varargs in the normal way, but delegates
76   // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
77   // Record these pending uses, which typically include the call-saved
78   // argument register R6D.
79   if (IsVarArg)
80     for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
81       SavedRegs.set(SystemZ::ArgGPRs[I]);
82 
83   // If there are any landing pads, entering them will modify r6/r7.
84   if (!MF.getLandingPads().empty()) {
85     SavedRegs.set(SystemZ::R6D);
86     SavedRegs.set(SystemZ::R7D);
87   }
88 
89   // If the function requires a frame pointer, record that the hard
90   // frame pointer will be clobbered.
91   if (HasFP)
92     SavedRegs.set(SystemZ::R11D);
93 
94   // If the function calls other functions, record that the return
95   // address register will be clobbered.
96   if (MFFrame.hasCalls())
97     SavedRegs.set(SystemZ::R14D);
98 
99   // If we are saving GPRs other than the stack pointer, we might as well
100   // save and restore the stack pointer at the same time, via STMG and LMG.
101   // This allows the deallocation to be done by the LMG, rather than needing
102   // a separate %r15 addition.
103   const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
104   for (unsigned I = 0; CSRegs[I]; ++I) {
105     unsigned Reg = CSRegs[I];
106     if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
107       SavedRegs.set(SystemZ::R15D);
108       break;
109     }
110   }
111 }
112 
113 // Add GPR64 to the save instruction being built by MIB, which is in basic
114 // block MBB.  IsImplicit says whether this is an explicit operand to the
115 // instruction, or an implicit one that comes between the explicit start
116 // and end registers.
117 static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
118                         unsigned GPR64, bool IsImplicit) {
119   const TargetRegisterInfo *RI =
120       MBB.getParent()->getSubtarget().getRegisterInfo();
121   Register GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
122   bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
123   if (!IsLive || !IsImplicit) {
124     MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
125     if (!IsLive)
126       MBB.addLiveIn(GPR64);
127   }
128 }
129 
130 bool SystemZFrameLowering::
131 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
132                           MachineBasicBlock::iterator MBBI,
133                           const std::vector<CalleeSavedInfo> &CSI,
134                           const TargetRegisterInfo *TRI) const {
135   if (CSI.empty())
136     return false;
137 
138   MachineFunction &MF = *MBB.getParent();
139   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
140   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
141   bool IsVarArg = MF.getFunction().isVarArg();
142   DebugLoc DL;
143 
144   // Scan the call-saved GPRs and find the bounds of the register spill area.
145   unsigned LowGPR = 0;
146   unsigned HighGPR = SystemZ::R15D;
147   unsigned StartOffset = -1U;
148   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
149     unsigned Reg = CSI[I].getReg();
150     if (SystemZ::GR64BitRegClass.contains(Reg)) {
151       unsigned Offset = RegSpillOffsets[Reg];
152       assert(Offset && "Unexpected GPR save");
153       if (StartOffset > Offset) {
154         LowGPR = Reg;
155         StartOffset = Offset;
156       }
157     }
158   }
159 
160   // Save the range of call-saved registers, for use by the epilogue inserter.
161   ZFI->setLowSavedGPR(LowGPR);
162   ZFI->setHighSavedGPR(HighGPR);
163 
164   // Include the GPR varargs, if any.  R6D is call-saved, so would
165   // be included by the loop above, but we also need to handle the
166   // call-clobbered argument registers.
167   if (IsVarArg) {
168     unsigned FirstGPR = ZFI->getVarArgsFirstGPR();
169     if (FirstGPR < SystemZ::NumArgGPRs) {
170       unsigned Reg = SystemZ::ArgGPRs[FirstGPR];
171       unsigned Offset = RegSpillOffsets[Reg];
172       if (StartOffset > Offset) {
173         LowGPR = Reg; StartOffset = Offset;
174       }
175     }
176   }
177 
178   // Save GPRs
179   if (LowGPR) {
180     assert(LowGPR != HighGPR && "Should be saving %r15 and something else");
181 
182     // Build an STMG instruction.
183     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
184 
185     // Add the explicit register operands.
186     addSavedGPR(MBB, MIB, LowGPR, false);
187     addSavedGPR(MBB, MIB, HighGPR, false);
188 
189     // Add the address.
190     MIB.addReg(SystemZ::R15D).addImm(StartOffset);
191 
192     // Make sure all call-saved GPRs are included as operands and are
193     // marked as live on entry.
194     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
195       unsigned Reg = CSI[I].getReg();
196       if (SystemZ::GR64BitRegClass.contains(Reg))
197         addSavedGPR(MBB, MIB, Reg, true);
198     }
199 
200     // ...likewise GPR varargs.
201     if (IsVarArg)
202       for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
203         addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true);
204   }
205 
206   // Save FPRs/VRs in the normal TargetInstrInfo way.
207   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
208     unsigned Reg = CSI[I].getReg();
209     if (SystemZ::FP64BitRegClass.contains(Reg)) {
210       MBB.addLiveIn(Reg);
211       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
212                                &SystemZ::FP64BitRegClass, TRI);
213     }
214     if (SystemZ::VR128BitRegClass.contains(Reg)) {
215       MBB.addLiveIn(Reg);
216       TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
217                                &SystemZ::VR128BitRegClass, TRI);
218     }
219   }
220 
221   return true;
222 }
223 
224 bool SystemZFrameLowering::
225 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
226                             MachineBasicBlock::iterator MBBI,
227                             std::vector<CalleeSavedInfo> &CSI,
228                             const TargetRegisterInfo *TRI) const {
229   if (CSI.empty())
230     return false;
231 
232   MachineFunction &MF = *MBB.getParent();
233   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
234   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
235   bool HasFP = hasFP(MF);
236   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
237 
238   // Restore FPRs/VRs in the normal TargetInstrInfo way.
239   for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
240     unsigned Reg = CSI[I].getReg();
241     if (SystemZ::FP64BitRegClass.contains(Reg))
242       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
243                                 &SystemZ::FP64BitRegClass, TRI);
244     if (SystemZ::VR128BitRegClass.contains(Reg))
245       TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
246                                 &SystemZ::VR128BitRegClass, TRI);
247   }
248 
249   // Restore call-saved GPRs (but not call-clobbered varargs, which at
250   // this point might hold return values).
251   unsigned LowGPR = ZFI->getLowSavedGPR();
252   unsigned HighGPR = ZFI->getHighSavedGPR();
253   unsigned StartOffset = RegSpillOffsets[LowGPR];
254   if (LowGPR) {
255     // If we saved any of %r2-%r5 as varargs, we should also be saving
256     // and restoring %r6.  If we're saving %r6 or above, we should be
257     // restoring it too.
258     assert(LowGPR != HighGPR && "Should be loading %r15 and something else");
259 
260     // Build an LMG instruction.
261     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
262 
263     // Add the explicit register operands.
264     MIB.addReg(LowGPR, RegState::Define);
265     MIB.addReg(HighGPR, RegState::Define);
266 
267     // Add the address.
268     MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
269     MIB.addImm(StartOffset);
270 
271     // Do a second scan adding regs as being defined by instruction
272     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
273       unsigned Reg = CSI[I].getReg();
274       if (Reg != LowGPR && Reg != HighGPR &&
275           SystemZ::GR64BitRegClass.contains(Reg))
276         MIB.addReg(Reg, RegState::ImplicitDefine);
277     }
278   }
279 
280   return true;
281 }
282 
283 void SystemZFrameLowering::
284 processFunctionBeforeFrameFinalized(MachineFunction &MF,
285                                     RegScavenger *RS) const {
286   MachineFrameInfo &MFFrame = MF.getFrameInfo();
287   // Get the size of our stack frame to be allocated ...
288   uint64_t StackSize = (MFFrame.estimateStackSize(MF) +
289                         SystemZMC::CallFrameSize);
290   // ... and the maximum offset we may need to reach into the
291   // caller's frame to access the save area or stack arguments.
292   int64_t MaxArgOffset = SystemZMC::CallFrameSize;
293   for (int I = MFFrame.getObjectIndexBegin(); I != 0; ++I)
294     if (MFFrame.getObjectOffset(I) >= 0) {
295       int64_t ArgOffset = SystemZMC::CallFrameSize +
296                           MFFrame.getObjectOffset(I) +
297                           MFFrame.getObjectSize(I);
298       MaxArgOffset = std::max(MaxArgOffset, ArgOffset);
299     }
300 
301   uint64_t MaxReach = StackSize + MaxArgOffset;
302   if (!isUInt<12>(MaxReach)) {
303     // We may need register scavenging slots if some parts of the frame
304     // are outside the reach of an unsigned 12-bit displacement.
305     // Create 2 for the case where both addresses in an MVC are
306     // out of range.
307     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
308     RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
309   }
310 }
311 
312 // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
313 static void emitIncrement(MachineBasicBlock &MBB,
314                           MachineBasicBlock::iterator &MBBI,
315                           const DebugLoc &DL,
316                           unsigned Reg, int64_t NumBytes,
317                           const TargetInstrInfo *TII) {
318   while (NumBytes) {
319     unsigned Opcode;
320     int64_t ThisVal = NumBytes;
321     if (isInt<16>(NumBytes))
322       Opcode = SystemZ::AGHI;
323     else {
324       Opcode = SystemZ::AGFI;
325       // Make sure we maintain 8-byte stack alignment.
326       int64_t MinVal = -uint64_t(1) << 31;
327       int64_t MaxVal = (int64_t(1) << 31) - 8;
328       if (ThisVal < MinVal)
329         ThisVal = MinVal;
330       else if (ThisVal > MaxVal)
331         ThisVal = MaxVal;
332     }
333     MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
334       .addReg(Reg).addImm(ThisVal);
335     // The CC implicit def is dead.
336     MI->getOperand(3).setIsDead();
337     NumBytes -= ThisVal;
338   }
339 }
340 
341 void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
342                                         MachineBasicBlock &MBB) const {
343   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
344   MachineFrameInfo &MFFrame = MF.getFrameInfo();
345   auto *ZII =
346       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
347   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
348   MachineBasicBlock::iterator MBBI = MBB.begin();
349   MachineModuleInfo &MMI = MF.getMMI();
350   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
351   const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
352   bool HasFP = hasFP(MF);
353 
354   // In GHC calling convention C stack space, including the ABI-defined
355   // 160-byte base area, is (de)allocated by GHC itself.  This stack space may
356   // be used by LLVM as spill slots for the tail recursive GHC functions.  Thus
357   // do not allocate stack space here, too.
358   if (MF.getFunction().getCallingConv() == CallingConv::GHC) {
359     if (MFFrame.getStackSize() > 2048 * sizeof(long)) {
360       report_fatal_error(
361           "Pre allocated stack space for GHC function is too small");
362     }
363     if (HasFP) {
364       report_fatal_error(
365           "In GHC calling convention a frame pointer is not supported");
366     }
367     MFFrame.setStackSize(MFFrame.getStackSize() + SystemZMC::CallFrameSize);
368     return;
369   }
370 
371   // Debug location must be unknown since the first debug location is used
372   // to determine the end of the prologue.
373   DebugLoc DL;
374 
375   // The current offset of the stack pointer from the CFA.
376   int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP;
377 
378   if (ZFI->getLowSavedGPR()) {
379     // Skip over the GPR saves.
380     if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
381       ++MBBI;
382     else
383       llvm_unreachable("Couldn't skip over GPR saves");
384 
385     // Add CFI for the GPR saves.
386     for (auto &Save : CSI) {
387       unsigned Reg = Save.getReg();
388       if (SystemZ::GR64BitRegClass.contains(Reg)) {
389         int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg];
390         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
391             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
392         BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
393             .addCFIIndex(CFIIndex);
394       }
395     }
396   }
397 
398   uint64_t StackSize = MFFrame.getStackSize();
399   // We need to allocate the ABI-defined 160-byte base area whenever
400   // we allocate stack space for our own use and whenever we call another
401   // function.
402   if (StackSize || MFFrame.hasVarSizedObjects() || MFFrame.hasCalls()) {
403     StackSize += SystemZMC::CallFrameSize;
404     MFFrame.setStackSize(StackSize);
405   }
406 
407   if (StackSize) {
408     // Determine if we want to store a backchain.
409     bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
410 
411     // If we need backchain, save current stack pointer.  R1 is free at this
412     // point.
413     if (StoreBackchain)
414       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
415         .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
416 
417     // Allocate StackSize bytes.
418     int64_t Delta = -int64_t(StackSize);
419     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
420 
421     // Add CFI for the allocation.
422     unsigned CFIIndex = MF.addFrameInst(
423         MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta));
424     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
425         .addCFIIndex(CFIIndex);
426     SPOffsetFromCFA += Delta;
427 
428     if (StoreBackchain)
429       BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
430         .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0).addReg(0);
431   }
432 
433   if (HasFP) {
434     // Copy the base of the frame to R11.
435     BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
436       .addReg(SystemZ::R15D);
437 
438     // Add CFI for the new frame location.
439     unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true);
440     unsigned CFIIndex = MF.addFrameInst(
441         MCCFIInstruction::createDefCfaRegister(nullptr, HardFP));
442     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
443         .addCFIIndex(CFIIndex);
444 
445     // Mark the FramePtr as live at the beginning of every block except
446     // the entry block.  (We'll have marked R11 as live on entry when
447     // saving the GPRs.)
448     for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
449       I->addLiveIn(SystemZ::R11D);
450   }
451 
452   // Skip over the FPR/VR saves.
453   SmallVector<unsigned, 8> CFIIndexes;
454   for (auto &Save : CSI) {
455     unsigned Reg = Save.getReg();
456     if (SystemZ::FP64BitRegClass.contains(Reg)) {
457       if (MBBI != MBB.end() &&
458           (MBBI->getOpcode() == SystemZ::STD ||
459            MBBI->getOpcode() == SystemZ::STDY))
460         ++MBBI;
461       else
462         llvm_unreachable("Couldn't skip over FPR save");
463     } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
464       if (MBBI != MBB.end() &&
465           MBBI->getOpcode() == SystemZ::VST)
466         ++MBBI;
467       else
468         llvm_unreachable("Couldn't skip over VR save");
469     } else
470       continue;
471 
472     // Add CFI for the this save.
473     unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
474     unsigned IgnoredFrameReg;
475     int64_t Offset =
476         getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg);
477 
478     unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
479           nullptr, DwarfReg, SPOffsetFromCFA + Offset));
480     CFIIndexes.push_back(CFIIndex);
481   }
482   // Complete the CFI for the FPR/VR saves, modelling them as taking effect
483   // after the last save.
484   for (auto CFIIndex : CFIIndexes) {
485     BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
486         .addCFIIndex(CFIIndex);
487   }
488 }
489 
490 void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
491                                         MachineBasicBlock &MBB) const {
492   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
493   auto *ZII =
494       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
495   SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
496   MachineFrameInfo &MFFrame = MF.getFrameInfo();
497 
498   // See SystemZFrameLowering::emitPrologue
499   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
500     return;
501 
502   // Skip the return instruction.
503   assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
504 
505   uint64_t StackSize = MFFrame.getStackSize();
506   if (ZFI->getLowSavedGPR()) {
507     --MBBI;
508     unsigned Opcode = MBBI->getOpcode();
509     if (Opcode != SystemZ::LMG)
510       llvm_unreachable("Expected to see callee-save register restore code");
511 
512     unsigned AddrOpNo = 2;
513     DebugLoc DL = MBBI->getDebugLoc();
514     uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
515     unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
516 
517     // If the offset is too large, use the largest stack-aligned offset
518     // and add the rest to the base register (the stack or frame pointer).
519     if (!NewOpcode) {
520       uint64_t NumBytes = Offset - 0x7fff8;
521       emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
522                     NumBytes, ZII);
523       Offset -= NumBytes;
524       NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
525       assert(NewOpcode && "No restore instruction available");
526     }
527 
528     MBBI->setDesc(ZII->get(NewOpcode));
529     MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
530   } else if (StackSize) {
531     DebugLoc DL = MBBI->getDebugLoc();
532     emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
533   }
534 }
535 
536 bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const {
537   return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
538           MF.getFrameInfo().hasVarSizedObjects() ||
539           MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP());
540 }
541 
542 bool
543 SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
544   // The ABI requires us to allocate 160 bytes of stack space for the callee,
545   // with any outgoing stack arguments being placed above that.  It seems
546   // better to make that area a permanent feature of the frame even if
547   // we're using a frame pointer.
548   return true;
549 }
550 
551 MachineBasicBlock::iterator SystemZFrameLowering::
552 eliminateCallFramePseudoInstr(MachineFunction &MF,
553                               MachineBasicBlock &MBB,
554                               MachineBasicBlock::iterator MI) const {
555   switch (MI->getOpcode()) {
556   case SystemZ::ADJCALLSTACKDOWN:
557   case SystemZ::ADJCALLSTACKUP:
558     assert(hasReservedCallFrame(MF) &&
559            "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
560     return MBB.erase(MI);
561     break;
562 
563   default:
564     llvm_unreachable("Unexpected call frame instruction");
565   }
566 }
567