blob: 9eeb046644612dd32b72e41ef2e573de73f780c8 [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
Matthias Braun02564862015-07-14 17:17:13 +000064void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF,
65 BitVector &SavedRegs,
66 RegScavenger *RS) const {
67 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
68
Ulrich Weigand5f613df2013-05-06 16:15:19 +000069 MachineFrameInfo *MFFrame = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000070 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +000071 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)
Matthias Braun02564862015-07-14 17:17:13 +000081 SavedRegs.set(SystemZ::ArgGPRs[I]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000082
83 // If the function requires a frame pointer, record that the hard
84 // frame pointer will be clobbered.
85 if (HasFP)
Matthias Braun02564862015-07-14 17:17:13 +000086 SavedRegs.set(SystemZ::R11D);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000087
88 // If the function calls other functions, record that the return
89 // address register will be clobbered.
90 if (MFFrame->hasCalls())
Matthias Braun02564862015-07-14 17:17:13 +000091 SavedRegs.set(SystemZ::R14D);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000092
93 // If we are saving GPRs other than the stack pointer, we might as well
94 // save and restore the stack pointer at the same time, via STMG and LMG.
95 // This allows the deallocation to be done by the LMG, rather than needing
96 // a separate %r15 addition.
Craig Topper840beec2014-04-04 05:16:06 +000097 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000098 for (unsigned I = 0; CSRegs[I]; ++I) {
99 unsigned Reg = CSRegs[I];
Matthias Braun02564862015-07-14 17:17:13 +0000100 if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
101 SavedRegs.set(SystemZ::R15D);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000102 break;
103 }
104 }
105}
106
107// Add GPR64 to the save instruction being built by MIB, which is in basic
108// block MBB. IsImplicit says whether this is an explicit operand to the
109// instruction, or an implicit one that comes between the explicit start
110// and end registers.
111static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000112 unsigned GPR64, bool IsImplicit) {
Eric Christopherd9134482014-08-04 21:25:23 +0000113 const TargetRegisterInfo *RI =
Eric Christopherfc6de422014-08-05 02:39:49 +0000114 MBB.getParent()->getSubtarget().getRegisterInfo();
Richard Sandiford87a44362013-09-30 10:28:35 +0000115 unsigned GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000116 bool IsLive = MBB.isLiveIn(GPR64) || MBB.isLiveIn(GPR32);
117 if (!IsLive || !IsImplicit) {
118 MIB.addReg(GPR64, getImplRegState(IsImplicit) | getKillRegState(!IsLive));
119 if (!IsLive)
120 MBB.addLiveIn(GPR64);
121 }
122}
123
124bool SystemZFrameLowering::
125spillCalleeSavedRegisters(MachineBasicBlock &MBB,
126 MachineBasicBlock::iterator MBBI,
127 const std::vector<CalleeSavedInfo> &CSI,
128 const TargetRegisterInfo *TRI) const {
129 if (CSI.empty())
130 return false;
131
132 MachineFunction &MF = *MBB.getParent();
Tim Northover775aaeb2015-11-05 21:54:58 +0000133 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
134 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
135 bool IsVarArg = MF.getFunction()->isVarArg();
136 DebugLoc DL;
137
138 // Scan the call-saved GPRs and find the bounds of the register spill area.
139 unsigned LowGPR = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000140 unsigned HighGPR = SystemZ::R15D;
141 unsigned StartOffset = -1U;
142 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
143 unsigned Reg = CSI[I].getReg();
144 if (SystemZ::GR64BitRegClass.contains(Reg)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000145 unsigned Offset = RegSpillOffsets[Reg];
146 assert(Offset && "Unexpected GPR save");
147 if (StartOffset > Offset) {
148 LowGPR = Reg;
149 StartOffset = Offset;
150 }
151 }
152 }
153
Richard Sandiforddb39b4a2013-07-03 09:11:00 +0000154 // Save the range of call-saved registers, for use by the epilogue inserter.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000155 ZFI->setLowSavedGPR(LowGPR);
156 ZFI->setHighSavedGPR(HighGPR);
157
158 // Include the GPR varargs, if any. R6D is call-saved, so would
159 // be included by the loop above, but we also need to handle the
160 // call-clobbered argument registers.
161 if (IsVarArg) {
162 unsigned FirstGPR = ZFI->getVarArgsFirstGPR();
163 if (FirstGPR < SystemZ::NumArgGPRs) {
164 unsigned Reg = SystemZ::ArgGPRs[FirstGPR];
165 unsigned Offset = RegSpillOffsets[Reg];
166 if (StartOffset > Offset) {
167 LowGPR = Reg; StartOffset = Offset;
168 }
169 }
170 }
171
172 // Save GPRs
173 if (LowGPR) {
174 assert(LowGPR != HighGPR && "Should be saving %r15 and something else");
175
176 // Build an STMG instruction.
177 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::STMG));
178
179 // Add the explicit register operands.
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000180 addSavedGPR(MBB, MIB, LowGPR, false);
181 addSavedGPR(MBB, MIB, HighGPR, false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000182
183 // Add the address.
184 MIB.addReg(SystemZ::R15D).addImm(StartOffset);
185
186 // Make sure all call-saved GPRs are included as operands and are
187 // marked as live on entry.
188 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
189 unsigned Reg = CSI[I].getReg();
190 if (SystemZ::GR64BitRegClass.contains(Reg))
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000191 addSavedGPR(MBB, MIB, Reg, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 }
193
194 // ...likewise GPR varargs.
195 if (IsVarArg)
196 for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000197 addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000198 }
199
200 // Save FPRs in the normal TargetInstrInfo way.
201 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
202 unsigned Reg = CSI[I].getReg();
203 if (SystemZ::FP64BitRegClass.contains(Reg)) {
204 MBB.addLiveIn(Reg);
205 TII->storeRegToStackSlot(MBB, MBBI, Reg, true, CSI[I].getFrameIdx(),
206 &SystemZ::FP64BitRegClass, TRI);
207 }
208 }
209
210 return true;
211}
212
213bool SystemZFrameLowering::
214restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
215 MachineBasicBlock::iterator MBBI,
216 const std::vector<CalleeSavedInfo> &CSI,
217 const TargetRegisterInfo *TRI) const {
218 if (CSI.empty())
219 return false;
220
221 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000222 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000223 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
224 bool HasFP = hasFP(MF);
225 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
226
227 // Restore FPRs in the normal TargetInstrInfo way.
228 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
229 unsigned Reg = CSI[I].getReg();
230 if (SystemZ::FP64BitRegClass.contains(Reg))
231 TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
232 &SystemZ::FP64BitRegClass, TRI);
233 }
234
235 // Restore call-saved GPRs (but not call-clobbered varargs, which at
236 // this point might hold return values).
237 unsigned LowGPR = ZFI->getLowSavedGPR();
238 unsigned HighGPR = ZFI->getHighSavedGPR();
239 unsigned StartOffset = RegSpillOffsets[LowGPR];
240 if (LowGPR) {
241 // If we saved any of %r2-%r5 as varargs, we should also be saving
242 // and restoring %r6. If we're saving %r6 or above, we should be
243 // restoring it too.
244 assert(LowGPR != HighGPR && "Should be loading %r15 and something else");
245
246 // Build an LMG instruction.
247 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
248
249 // Add the explicit register operands.
250 MIB.addReg(LowGPR, RegState::Define);
251 MIB.addReg(HighGPR, RegState::Define);
252
253 // Add the address.
254 MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
255 MIB.addImm(StartOffset);
256
257 // Do a second scan adding regs as being defined by instruction
258 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
259 unsigned Reg = CSI[I].getReg();
260 if (Reg != LowGPR && Reg != HighGPR)
261 MIB.addReg(Reg, RegState::ImplicitDefine);
262 }
263 }
264
265 return true;
266}
267
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000268void SystemZFrameLowering::
269processFunctionBeforeFrameFinalized(MachineFunction &MF,
270 RegScavenger *RS) const {
271 MachineFrameInfo *MFFrame = MF.getFrameInfo();
272 uint64_t MaxReach = (MFFrame->estimateStackSize(MF) +
273 SystemZMC::CallFrameSize * 2);
Richard Sandiford23943222013-07-05 13:11:52 +0000274 if (!isUInt<12>(MaxReach)) {
275 // We may need register scavenging slots if some parts of the frame
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000276 // are outside the reach of an unsigned 12-bit displacement.
Richard Sandiford23943222013-07-05 13:11:52 +0000277 // Create 2 for the case where both addresses in an MVC are
278 // out of range.
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000279 RS->addScavengingFrameIndex(MFFrame->CreateStackObject(8, 8, false));
Richard Sandiford23943222013-07-05 13:11:52 +0000280 RS->addScavengingFrameIndex(MFFrame->CreateStackObject(8, 8, false));
281 }
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000282}
283
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000284// Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
285static void emitIncrement(MachineBasicBlock &MBB,
286 MachineBasicBlock::iterator &MBBI,
287 const DebugLoc &DL,
288 unsigned Reg, int64_t NumBytes,
289 const TargetInstrInfo *TII) {
290 while (NumBytes) {
291 unsigned Opcode;
292 int64_t ThisVal = NumBytes;
293 if (isInt<16>(NumBytes))
294 Opcode = SystemZ::AGHI;
295 else {
296 Opcode = SystemZ::AGFI;
297 // Make sure we maintain 8-byte stack alignment.
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +0000298 int64_t MinVal = -uint64_t(1) << 31;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000299 int64_t MaxVal = (int64_t(1) << 31) - 8;
300 if (ThisVal < MinVal)
301 ThisVal = MinVal;
302 else if (ThisVal > MaxVal)
303 ThisVal = MaxVal;
304 }
305 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
306 .addReg(Reg).addImm(ThisVal);
Richard Sandiford14a44492013-05-22 13:38:45 +0000307 // The CC implicit def is dead.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000308 MI->getOperand(3).setIsDead();
309 NumBytes -= ThisVal;
310 }
311}
312
Quentin Colombet61b305e2015-05-05 17:38:16 +0000313void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
314 MachineBasicBlock &MBB) const {
315 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000316 MachineFrameInfo *MFFrame = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000317 auto *ZII =
318 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000319 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
320 MachineBasicBlock::iterator MBBI = MBB.begin();
321 MachineModuleInfo &MMI = MF.getMMI();
Tim Northover775aaeb2015-11-05 21:54:58 +0000322 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
323 const std::vector<CalleeSavedInfo> &CSI = MFFrame->getCalleeSavedInfo();
324 bool HasFP = hasFP(MF);
325
326 // Debug location must be unknown since the first debug location is used
327 // to determine the end of the prologue.
328 DebugLoc DL;
329
330 // The current offset of the stack pointer from the CFA.
331 int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000332
333 if (ZFI->getLowSavedGPR()) {
334 // Skip over the GPR saves.
335 if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
336 ++MBBI;
337 else
338 llvm_unreachable("Couldn't skip over GPR saves");
339
340 // Add CFI for the GPR saves.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000341 for (auto &Save : CSI) {
342 unsigned Reg = Save.getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000343 if (SystemZ::GR64BitRegClass.contains(Reg)) {
344 int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg];
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000345 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
346 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
347 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
348 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349 }
350 }
351 }
352
353 uint64_t StackSize = getAllocatedStackSize(MF);
354 if (StackSize) {
355 // Allocate StackSize bytes.
356 int64_t Delta = -int64_t(StackSize);
357 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
358
359 // Add CFI for the allocation.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000360 unsigned CFIIndex = MMI.addFrameInst(
361 MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta));
362 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
363 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000364 SPOffsetFromCFA += Delta;
365 }
366
367 if (HasFP) {
368 // Copy the base of the frame to R11.
369 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
370 .addReg(SystemZ::R15D);
371
372 // Add CFI for the new frame location.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000373 unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000374 unsigned CFIIndex = MMI.addFrameInst(
375 MCCFIInstruction::createDefCfaRegister(nullptr, HardFP));
376 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
377 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000378
379 // Mark the FramePtr as live at the beginning of every block except
380 // the entry block. (We'll have marked R11 as live on entry when
381 // saving the GPRs.)
Richard Sandiford28c111e2014-03-06 11:00:15 +0000382 for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000383 I->addLiveIn(SystemZ::R11D);
384 }
385
386 // Skip over the FPR saves.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000387 SmallVector<unsigned, 8> CFIIndexes;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000388 for (auto &Save : CSI) {
389 unsigned Reg = Save.getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000390 if (SystemZ::FP64BitRegClass.contains(Reg)) {
391 if (MBBI != MBB.end() &&
392 (MBBI->getOpcode() == SystemZ::STD ||
393 MBBI->getOpcode() == SystemZ::STDY))
394 ++MBBI;
395 else
396 llvm_unreachable("Couldn't skip over FPR save");
397
398 // Add CFI for the this save.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000399 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
James Y Knight5567baf2015-08-15 02:32:35 +0000400 unsigned IgnoredFrameReg;
401 int64_t Offset =
402 getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg);
403
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000404 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
405 nullptr, DwarfReg, SPOffsetFromCFA + Offset));
406 CFIIndexes.push_back(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000407 }
408 }
409 // Complete the CFI for the FPR saves, modelling them as taking effect
410 // after the last save.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000411 for (auto CFIIndex : CFIIndexes) {
412 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
413 .addCFIIndex(CFIIndex);
414 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000415}
416
417void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
418 MachineBasicBlock &MBB) const {
419 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Eric Christopherfc6de422014-08-05 02:39:49 +0000420 auto *ZII =
421 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000422 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
423
424 // Skip the return instruction.
Richard Sandiford709bda62013-08-19 12:42:31 +0000425 assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000426
427 uint64_t StackSize = getAllocatedStackSize(MF);
428 if (ZFI->getLowSavedGPR()) {
429 --MBBI;
430 unsigned Opcode = MBBI->getOpcode();
431 if (Opcode != SystemZ::LMG)
432 llvm_unreachable("Expected to see callee-save register restore code");
433
434 unsigned AddrOpNo = 2;
435 DebugLoc DL = MBBI->getDebugLoc();
436 uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
437 unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
438
439 // If the offset is too large, use the largest stack-aligned offset
440 // and add the rest to the base register (the stack or frame pointer).
441 if (!NewOpcode) {
442 uint64_t NumBytes = Offset - 0x7fff8;
443 emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
444 NumBytes, ZII);
445 Offset -= NumBytes;
446 NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
447 assert(NewOpcode && "No restore instruction available");
448 }
449
450 MBBI->setDesc(ZII->get(NewOpcode));
451 MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
452 } else if (StackSize) {
453 DebugLoc DL = MBBI->getDebugLoc();
454 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
455 }
456}
457
458bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const {
459 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
460 MF.getFrameInfo()->hasVarSizedObjects() ||
461 MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP());
462}
463
James Y Knight5567baf2015-08-15 02:32:35 +0000464int SystemZFrameLowering::getFrameIndexReference(const MachineFunction &MF,
465 int FI,
466 unsigned &FrameReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000467 const MachineFrameInfo *MFFrame = MF.getFrameInfo();
James Y Knight5567baf2015-08-15 02:32:35 +0000468 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
469
470 // Fill in FrameReg output argument.
471 FrameReg = RI->getFrameRegister(MF);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000472
473 // Start with the offset of FI from the top of the caller-allocated frame
474 // (i.e. the top of the 160 bytes allocated by the caller). This initial
475 // offset is therefore negative.
476 int64_t Offset = (MFFrame->getObjectOffset(FI) +
477 MFFrame->getOffsetAdjustment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000478
479 // Make the offset relative to the incoming stack pointer.
480 Offset -= getOffsetOfLocalArea();
481
482 // Make the offset relative to the bottom of the frame.
483 Offset += getAllocatedStackSize(MF);
484
485 return Offset;
486}
487
488uint64_t SystemZFrameLowering::
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000489getAllocatedStackSize(const MachineFunction &MF) const {
490 const MachineFrameInfo *MFFrame = MF.getFrameInfo();
491
492 // Start with the size of the local variables and spill slots.
493 uint64_t StackSize = MFFrame->getStackSize();
494
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000495 // We need to allocate the ABI-defined 160-byte base area whenever
496 // we allocate stack space for our own use and whenever we call another
497 // function.
498 if (StackSize || MFFrame->hasVarSizedObjects() || MFFrame->hasCalls())
499 StackSize += SystemZMC::CallFrameSize;
500
501 return StackSize;
502}
503
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000504bool
505SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
506 // The ABI requires us to allocate 160 bytes of stack space for the callee,
507 // with any outgoing stack arguments being placed above that. It seems
508 // better to make that area a permanent feature of the frame even if
509 // we're using a frame pointer.
510 return true;
511}
512
513void SystemZFrameLowering::
514eliminateCallFramePseudoInstr(MachineFunction &MF,
515 MachineBasicBlock &MBB,
516 MachineBasicBlock::iterator MI) const {
517 switch (MI->getOpcode()) {
518 case SystemZ::ADJCALLSTACKDOWN:
519 case SystemZ::ADJCALLSTACKUP:
520 assert(hasReservedCallFrame(MF) &&
521 "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
522 MBB.erase(MI);
523 break;
524
525 default:
526 llvm_unreachable("Unexpected call frame instruction");
527 }
528}