blob: a636b35635ce3041b5e0c5e6e024a9f8d2567176 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- 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"
Eric Christopherf1bd22d2014-07-01 20:18:59 +000013#include "SystemZInstrInfo.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000014#include "SystemZMachineFunctionInfo.h"
Eric Christopherf1bd22d2014-07-01 20:18:59 +000015#include "SystemZRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000016#include "SystemZSubtarget.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Richard Sandiford5dd52f82013-07-05 12:55:00 +000019#include "llvm/CodeGen/RegisterScavenging.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000020#include "llvm/IR/Function.h"
21
22using namespace llvm;
23
Richard Sandiforddb39b4a2013-07-03 09:11:00 +000024namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +000025// The ABI-defined register save slots, relative to the incoming stack
26// pointer.
27static 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
Ulrich Weigand5f613df2013-05-06 16:15:19 +000048
Eric Christopherf1bd22d2014-07-01 20:18:59 +000049SystemZFrameLowering::SystemZFrameLowering()
50 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 8,
51 -SystemZMC::CallFrameSize, 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000052 // 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)
Richard Sandiforddb39b4a2013-07-03 09:11:00 +000055 RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset;
56}
57
58const TargetFrameLowering::SpillSlot *
59SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const {
60 NumEntries = array_lengthof(SpillOffsetTable);
61 return SpillOffsetTable;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000062}
63
64void SystemZFrameLowering::
65processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
66 RegScavenger *RS) const {
67 MachineFrameInfo *MFFrame = MF.getFrameInfo();
68 MachineRegisterInfo &MRI = MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000069 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +000070 bool HasFP = hasFP(MF);
71 SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
72 bool IsVarArg = MF.getFunction()->isVarArg();
73
74 // va_start stores incoming FPR varargs in the normal way, but delegates
75 // the saving of incoming GPR varargs to spillCalleeSavedRegisters().
76 // Record these pending uses, which typically include the call-saved
77 // argument register R6D.
78 if (IsVarArg)
79 for (unsigned I = MFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
80 MRI.setPhysRegUsed(SystemZ::ArgGPRs[I]);
81
82 // If the function requires a frame pointer, record that the hard
83 // frame pointer will be clobbered.
84 if (HasFP)
85 MRI.setPhysRegUsed(SystemZ::R11D);
86
87 // If the function calls other functions, record that the return
88 // address register will be clobbered.
89 if (MFFrame->hasCalls())
90 MRI.setPhysRegUsed(SystemZ::R14D);
91
92 // If we are saving GPRs other than the stack pointer, we might as well
93 // save and restore the stack pointer at the same time, via STMG and LMG.
94 // This allows the deallocation to be done by the LMG, rather than needing
95 // a separate %r15 addition.
Craig Topper840beec2014-04-04 05:16:06 +000096 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000097 for (unsigned I = 0; CSRegs[I]; ++I) {
98 unsigned Reg = CSRegs[I];
99 if (SystemZ::GR64BitRegClass.contains(Reg) && MRI.isPhysRegUsed(Reg)) {
100 MRI.setPhysRegUsed(SystemZ::R15D);
101 break;
102 }
103 }
104}
105
106// Add GPR64 to the save instruction being built by MIB, which is in basic
107// block MBB. IsImplicit says whether this is an explicit operand to the
108// instruction, or an implicit one that comes between the explicit start
109// and end registers.
110static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000111 unsigned GPR64, bool IsImplicit) {
Eric Christopherd9134482014-08-04 21:25:23 +0000112 const TargetRegisterInfo *RI =
Eric Christopherfc6de422014-08-05 02:39:49 +0000113 MBB.getParent()->getSubtarget().getRegisterInfo();
Richard Sandiford87a44362013-09-30 10:28:35 +0000114 unsigned GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000115 bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
116 if (!IsLive || !IsImplicit) {
117 MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
118 if (!IsLive)
119 MBB.addLiveIn(GPR64);
120 }
121}
122
123bool SystemZFrameLowering::
124spillCalleeSavedRegisters(MachineBasicBlock &MBB,
125 MachineBasicBlock::iterator MBBI,
126 const std::vector<CalleeSavedInfo> &CSI,
127 const TargetRegisterInfo *TRI) const {
128 if (CSI.empty())
129 return false;
130
131 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000132 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
134 bool IsVarArg = MF.getFunction()->isVarArg();
135 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
136
137 // Scan the call-saved GPRs and find the bounds of the register spill area.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000138 unsigned LowGPR = 0;
139 unsigned HighGPR = SystemZ::R15D;
140 unsigned StartOffset = -1U;
141 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
142 unsigned Reg = CSI[I].getReg();
143 if (SystemZ::GR64BitRegClass.contains(Reg)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000144 unsigned Offset = RegSpillOffsets[Reg];
145 assert(Offset && "Unexpected GPR save");
146 if (StartOffset > Offset) {
147 LowGPR = Reg;
148 StartOffset = Offset;
149 }
150 }
151 }
152
Richard Sandiforddb39b4a2013-07-03 09:11:00 +0000153 // Save the range of call-saved registers, for use by the epilogue inserter.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000154 ZFI->setLowSavedGPR(LowGPR);
155 ZFI->setHighSavedGPR(HighGPR);
156
157 // Include the GPR varargs, if any. R6D is call-saved, so would
158 // be included by the loop above, but we also need to handle the
159 // call-clobbered argument registers.
160 if (IsVarArg) {
161 unsigned FirstGPR = ZFI->getVarArgsFirstGPR();
162 if (FirstGPR < SystemZ::NumArgGPRs) {
163 unsigned Reg = SystemZ::ArgGPRs[FirstGPR];
164 unsigned Offset = RegSpillOffsets[Reg];
165 if (StartOffset > Offset) {
166 LowGPR = Reg; StartOffset = Offset;
167 }
168 }
169 }
170
171 // Save GPRs
172 if (LowGPR) {
173 assert(LowGPR != HighGPR && "Should be saving %r15 and something else");
174
175 // Build an STMG instruction.
176 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
177
178 // Add the explicit register operands.
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000179 addSavedGPR(MBB, MIB, LowGPR, false);
180 addSavedGPR(MBB, MIB, HighGPR, false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181
182 // Add the address.
183 MIB.addReg(SystemZ::R15D).addImm(StartOffset);
184
185 // Make sure all call-saved GPRs are included as operands and are
186 // marked as live on entry.
187 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
188 unsigned Reg = CSI[I].getReg();
189 if (SystemZ::GR64BitRegClass.contains(Reg))
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000190 addSavedGPR(MBB, MIB, Reg, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 }
192
193 // ...likewise GPR varargs.
194 if (IsVarArg)
195 for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000196 addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000197 }
198
199 // Save FPRs in the normal TargetInstrInfo way.
200 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
201 unsigned Reg = CSI[I].getReg();
202 if (SystemZ::FP64BitRegClass.contains(Reg)) {
203 MBB.addLiveIn(Reg);
204 TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
205 &SystemZ::FP64BitRegClass, TRI);
206 }
207 }
208
209 return true;
210}
211
212bool SystemZFrameLowering::
213restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
214 MachineBasicBlock::iterator MBBI,
215 const std::vector<CalleeSavedInfo> &CSI,
216 const TargetRegisterInfo *TRI) const {
217 if (CSI.empty())
218 return false;
219
220 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000221 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000222 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
223 bool HasFP = hasFP(MF);
224 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
225
226 // Restore FPRs in the normal TargetInstrInfo way.
227 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
228 unsigned Reg = CSI[I].getReg();
229 if (SystemZ::FP64BitRegClass.contains(Reg))
230 TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
231 &SystemZ::FP64BitRegClass, TRI);
232 }
233
234 // Restore call-saved GPRs (but not call-clobbered varargs, which at
235 // this point might hold return values).
236 unsigned LowGPR = ZFI->getLowSavedGPR();
237 unsigned HighGPR = ZFI->getHighSavedGPR();
238 unsigned StartOffset = RegSpillOffsets[LowGPR];
239 if (LowGPR) {
240 // If we saved any of %r2-%r5 as varargs, we should also be saving
241 // and restoring %r6. If we're saving %r6 or above, we should be
242 // restoring it too.
243 assert(LowGPR != HighGPR && "Should be loading %r15 and something else");
244
245 // Build an LMG instruction.
246 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
247
248 // Add the explicit register operands.
249 MIB.addReg(LowGPR, RegState::Define);
250 MIB.addReg(HighGPR, RegState::Define);
251
252 // Add the address.
253 MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
254 MIB.addImm(StartOffset);
255
256 // Do a second scan adding regs as being defined by instruction
257 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
258 unsigned Reg = CSI[I].getReg();
259 if (Reg != LowGPR && Reg != HighGPR)
260 MIB.addReg(Reg, RegState::ImplicitDefine);
261 }
262 }
263
264 return true;
265}
266
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000267void SystemZFrameLowering::
268processFunctionBeforeFrameFinalized(MachineFunction &MF,
269 RegScavenger *RS) const {
270 MachineFrameInfo *MFFrame = MF.getFrameInfo();
271 uint64_t MaxReach = (MFFrame->estimateStackSize(MF) +
272 SystemZMC::CallFrameSize * 2);
Richard Sandiford23943222013-07-05 13:11:52 +0000273 if (!isUInt<12>(MaxReach)) {
274 // We may need register scavenging slots if some parts of the frame
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000275 // are outside the reach of an unsigned 12-bit displacement.
Richard Sandiford23943222013-07-05 13:11:52 +0000276 // Create 2 for the case where both addresses in an MVC are
277 // out of range.
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000278 RS->addScavengingFrameIndex(MFFrame->CreateStackObject(8, 8, false));
Richard Sandiford23943222013-07-05 13:11:52 +0000279 RS->addScavengingFrameIndex(MFFrame->CreateStackObject(8, 8, false));
280 }
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000281}
282
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000283// Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
284static void emitIncrement(MachineBasicBlock &MBB,
285 MachineBasicBlock::iterator &MBBI,
286 const DebugLoc &DL,
287 unsigned Reg, int64_t NumBytes,
288 const TargetInstrInfo *TII) {
289 while (NumBytes) {
290 unsigned Opcode;
291 int64_t ThisVal = NumBytes;
292 if (isInt<16>(NumBytes))
293 Opcode = SystemZ::AGHI;
294 else {
295 Opcode = SystemZ::AGFI;
296 // Make sure we maintain 8-byte stack alignment.
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +0000297 int64_t MinVal = -uint64_t(1) << 31;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000298 int64_t MaxVal = (int64_t(1) << 31) - 8;
299 if (ThisVal < MinVal)
300 ThisVal = MinVal;
301 else if (ThisVal > MaxVal)
302 ThisVal = MaxVal;
303 }
304 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
305 .addReg(Reg).addImm(ThisVal);
Richard Sandiford14a44492013-05-22 13:38:45 +0000306 // The CC implicit def is dead.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000307 MI->getOperand(3).setIsDead();
308 NumBytes -= ThisVal;
309 }
310}
311
Quentin Colombet61b305e2015-05-05 17:38:16 +0000312void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
313 MachineBasicBlock &MBB) const {
314 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000315 MachineFrameInfo *MFFrame = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000316 auto *ZII =
317 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000318 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
319 MachineBasicBlock::iterator MBBI = MBB.begin();
320 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000321 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000322 const std::vector<CalleeSavedInfo> &CSI = MFFrame->getCalleeSavedInfo();
323 bool HasFP = hasFP(MF);
324 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
325
326 // The current offset of the stack pointer from the CFA.
327 int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP;
328
329 if (ZFI->getLowSavedGPR()) {
330 // Skip over the GPR saves.
331 if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
332 ++MBBI;
333 else
334 llvm_unreachable("Couldn't skip over GPR saves");
335
336 // Add CFI for the GPR saves.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000337 for (auto &Save : CSI) {
338 unsigned Reg = Save.getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000339 if (SystemZ::GR64BitRegClass.contains(Reg)) {
340 int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg];
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000341 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
342 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
343 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
344 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000345 }
346 }
347 }
348
349 uint64_t StackSize = getAllocatedStackSize(MF);
350 if (StackSize) {
351 // Allocate StackSize bytes.
352 int64_t Delta = -int64_t(StackSize);
353 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
354
355 // Add CFI for the allocation.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000356 unsigned CFIIndex = MMI.addFrameInst(
357 MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta));
358 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
359 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000360 SPOffsetFromCFA += Delta;
361 }
362
363 if (HasFP) {
364 // Copy the base of the frame to R11.
365 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
366 .addReg(SystemZ::R15D);
367
368 // Add CFI for the new frame location.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000369 unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000370 unsigned CFIIndex = MMI.addFrameInst(
371 MCCFIInstruction::createDefCfaRegister(nullptr, HardFP));
372 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
373 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000374
375 // Mark the FramePtr as live at the beginning of every block except
376 // the entry block. (We'll have marked R11 as live on entry when
377 // saving the GPRs.)
Richard Sandiford28c111e2014-03-06 11:00:15 +0000378 for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000379 I->addLiveIn(SystemZ::R11D);
380 }
381
382 // Skip over the FPR saves.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000383 SmallVector<unsigned, 8> CFIIndexes;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000384 for (auto &Save : CSI) {
385 unsigned Reg = Save.getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000386 if (SystemZ::FP64BitRegClass.contains(Reg)) {
387 if (MBBI != MBB.end() &&
388 (MBBI->getOpcode() == SystemZ::STD ||
389 MBBI->getOpcode() == SystemZ::STDY))
390 ++MBBI;
391 else
392 llvm_unreachable("Couldn't skip over FPR save");
393
394 // Add CFI for the this save.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000395 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
396 int64_t Offset = getFrameIndexOffset(MF, Save.getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000397 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
398 nullptr, DwarfReg, SPOffsetFromCFA + Offset));
399 CFIIndexes.push_back(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000400 }
401 }
402 // Complete the CFI for the FPR saves, modelling them as taking effect
403 // after the last save.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000404 for (auto CFIIndex : CFIIndexes) {
405 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
406 .addCFIIndex(CFIIndex);
407 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000408}
409
410void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
411 MachineBasicBlock &MBB) const {
412 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Eric Christopherfc6de422014-08-05 02:39:49 +0000413 auto *ZII =
414 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000415 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
416
417 // Skip the return instruction.
Richard Sandiford709bda62013-08-19 12:42:31 +0000418 assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000419
420 uint64_t StackSize = getAllocatedStackSize(MF);
421 if (ZFI->getLowSavedGPR()) {
422 --MBBI;
423 unsigned Opcode = MBBI->getOpcode();
424 if (Opcode != SystemZ::LMG)
425 llvm_unreachable("Expected to see callee-save register restore code");
426
427 unsigned AddrOpNo = 2;
428 DebugLoc DL = MBBI->getDebugLoc();
429 uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
430 unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
431
432 // If the offset is too large, use the largest stack-aligned offset
433 // and add the rest to the base register (the stack or frame pointer).
434 if (!NewOpcode) {
435 uint64_t NumBytes = Offset - 0x7fff8;
436 emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
437 NumBytes, ZII);
438 Offset -= NumBytes;
439 NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
440 assert(NewOpcode && "No restore instruction available");
441 }
442
443 MBBI->setDesc(ZII->get(NewOpcode));
444 MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
445 } else if (StackSize) {
446 DebugLoc DL = MBBI->getDebugLoc();
447 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
448 }
449}
450
451bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const {
452 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
453 MF.getFrameInfo()->hasVarSizedObjects() ||
454 MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP());
455}
456
457int SystemZFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
458 int FI) const {
459 const MachineFrameInfo *MFFrame = MF.getFrameInfo();
460
461 // Start with the offset of FI from the top of the caller-allocated frame
462 // (i.e. the top of the 160 bytes allocated by the caller). This initial
463 // offset is therefore negative.
464 int64_t Offset = (MFFrame->getObjectOffset(FI) +
465 MFFrame->getOffsetAdjustment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000466
467 // Make the offset relative to the incoming stack pointer.
468 Offset -= getOffsetOfLocalArea();
469
470 // Make the offset relative to the bottom of the frame.
471 Offset += getAllocatedStackSize(MF);
472
473 return Offset;
474}
475
476uint64_t SystemZFrameLowering::
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000477getAllocatedStackSize(const MachineFunction &MF) const {
478 const MachineFrameInfo *MFFrame = MF.getFrameInfo();
479
480 // Start with the size of the local variables and spill slots.
481 uint64_t StackSize = MFFrame->getStackSize();
482
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000483 // We need to allocate the ABI-defined 160-byte base area whenever
484 // we allocate stack space for our own use and whenever we call another
485 // function.
486 if (StackSize || MFFrame->hasVarSizedObjects() || MFFrame->hasCalls())
487 StackSize += SystemZMC::CallFrameSize;
488
489 return StackSize;
490}
491
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000492bool
493SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
494 // The ABI requires us to allocate 160 bytes of stack space for the callee,
495 // with any outgoing stack arguments being placed above that. It seems
496 // better to make that area a permanent feature of the frame even if
497 // we're using a frame pointer.
498 return true;
499}
500
501void SystemZFrameLowering::
502eliminateCallFramePseudoInstr(MachineFunction &MF,
503 MachineBasicBlock &MBB,
504 MachineBasicBlock::iterator MI) const {
505 switch (MI->getOpcode()) {
506 case SystemZ::ADJCALLSTACKDOWN:
507 case SystemZ::ADJCALLSTACKUP:
508 assert(hasReservedCallFrame(MF) &&
509 "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
510 MBB.erase(MI);
511 break;
512
513 default:
514 llvm_unreachable("Unexpected call frame instruction");
515 }
516}