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