blob: a28a91e834f610f17cd7dad7dc30f1cd7b3c5718 [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,
Jonas Paulssonf12b9252015-11-28 11:02:32 +000051 -SystemZMC::CallFrameSize, 8,
52 false /* StackRealignable */) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000053 // 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)
Richard Sandiforddb39b4a2013-07-03 09:11:00 +000056 RegSpillOffsets[SpillOffsetTable[I].Reg] = SpillOffsetTable[I].Offset;
57}
58
59const TargetFrameLowering::SpillSlot *
60SystemZFrameLowering::getCalleeSavedSpillSlots(unsigned &NumEntries) const {
61 NumEntries = array_lengthof(SpillOffsetTable);
62 return SpillOffsetTable;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000063}
64
Matthias Braun02564862015-07-14 17:17:13 +000065void SystemZFrameLowering::determineCalleeSaves(MachineFunction &MF,
66 BitVector &SavedRegs,
67 RegScavenger *RS) const {
68 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
69
Matthias Braun941a7052016-07-28 18:40:00 +000070 MachineFrameInfo &MFFrame = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000071 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +000072 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)
Matthias Braun02564862015-07-14 17:17:13 +000082 SavedRegs.set(SystemZ::ArgGPRs[I]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000083
Marcin Koscielnicki234e5a82016-06-28 14:13:11 +000084 // If there are any landing pads, entering them will modify r6/r7.
Matthias Braund0ee66c2016-12-01 19:32:15 +000085 if (!MF.getLandingPads().empty()) {
Marcin Koscielnicki234e5a82016-06-28 14:13:11 +000086 SavedRegs.set(SystemZ::R6D);
87 SavedRegs.set(SystemZ::R7D);
88 }
89
Ulrich Weigand5f613df2013-05-06 16:15:19 +000090 // If the function requires a frame pointer, record that the hard
91 // frame pointer will be clobbered.
92 if (HasFP)
Matthias Braun02564862015-07-14 17:17:13 +000093 SavedRegs.set(SystemZ::R11D);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000094
95 // If the function calls other functions, record that the return
96 // address register will be clobbered.
Matthias Braun941a7052016-07-28 18:40:00 +000097 if (MFFrame.hasCalls())
Matthias Braun02564862015-07-14 17:17:13 +000098 SavedRegs.set(SystemZ::R14D);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000099
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.
Craig Topper840beec2014-04-04 05:16:06 +0000104 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000105 for (unsigned I = 0; CSRegs[I]; ++I) {
106 unsigned Reg = CSRegs[I];
Matthias Braun02564862015-07-14 17:17:13 +0000107 if (SystemZ::GR64BitRegClass.contains(Reg) && SavedRegs.test(Reg)) {
108 SavedRegs.set(SystemZ::R15D);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000109 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.
118static void addSavedGPR(MachineBasicBlock &MBB, MachineInstrBuilder &MIB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000119 unsigned GPR64, bool IsImplicit) {
Eric Christopherd9134482014-08-04 21:25:23 +0000120 const TargetRegisterInfo *RI =
Eric Christopherfc6de422014-08-05 02:39:49 +0000121 MBB.getParent()->getSubtarget().getRegisterInfo();
Richard Sandiford87a44362013-09-30 10:28:35 +0000122 unsigned GPR32 = RI->getSubReg(GPR64, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000123 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
131bool SystemZFrameLowering::
132spillCalleeSavedRegisters(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();
Tim Northover775aaeb2015-11-05 21:54:58 +0000140 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;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000147 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)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000152 unsigned Offset = RegSpillOffsets[Reg];
153 assert(Offset && "Unexpected GPR save");
154 if (StartOffset > Offset) {
155 LowGPR = Reg;
156 StartOffset = Offset;
157 }
158 }
159 }
160
Richard Sandiforddb39b4a2013-07-03 09:11:00 +0000161 // Save the range of call-saved registers, for use by the epilogue inserter.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000162 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.
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000187 addSavedGPR(MBB, MIB, LowGPR, false);
188 addSavedGPR(MBB, MIB, HighGPR, false);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189
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))
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000198 addSavedGPR(MBB, MIB, Reg, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 }
200
201 // ...likewise GPR varargs.
202 if (IsVarArg)
203 for (unsigned I = ZFI->getVarArgsFirstGPR(); I < SystemZ::NumArgGPRs; ++I)
Eric Christopherf1bd22d2014-07-01 20:18:59 +0000204 addSavedGPR(MBB, MIB, SystemZ::ArgGPRs[I], true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000205 }
206
207 // Save FPRs 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 }
216
217 return true;
218}
219
220bool SystemZFrameLowering::
221restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
222 MachineBasicBlock::iterator MBBI,
223 const std::vector<CalleeSavedInfo> &CSI,
224 const TargetRegisterInfo *TRI) const {
225 if (CSI.empty())
226 return false;
227
228 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000229 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000230 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
231 bool HasFP = hasFP(MF);
232 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
233
234 // Restore FPRs in the normal TargetInstrInfo way.
235 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
236 unsigned Reg = CSI[I].getReg();
237 if (SystemZ::FP64BitRegClass.contains(Reg))
238 TII->loadRegFromStackSlot(MBB, MBBI, Reg, CSI[I].getFrameIdx(),
239 &SystemZ::FP64BitRegClass, TRI);
240 }
241
242 // Restore call-saved GPRs (but not call-clobbered varargs, which at
243 // this point might hold return values).
244 unsigned LowGPR = ZFI->getLowSavedGPR();
245 unsigned HighGPR = ZFI->getHighSavedGPR();
246 unsigned StartOffset = RegSpillOffsets[LowGPR];
247 if (LowGPR) {
248 // If we saved any of %r2-%r5 as varargs, we should also be saving
249 // and restoring %r6. If we're saving %r6 or above, we should be
250 // restoring it too.
251 assert(LowGPR != HighGPR && "Should be loading %r15 and something else");
252
253 // Build an LMG instruction.
254 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(SystemZ::LMG));
255
256 // Add the explicit register operands.
257 MIB.addReg(LowGPR, RegState::Define);
258 MIB.addReg(HighGPR, RegState::Define);
259
260 // Add the address.
261 MIB.addReg(HasFP ? SystemZ::R11D : SystemZ::R15D);
262 MIB.addImm(StartOffset);
263
264 // Do a second scan adding regs as being defined by instruction
265 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
266 unsigned Reg = CSI[I].getReg();
Jonas Paulssonf0344822016-05-02 09:37:44 +0000267 if (Reg != LowGPR && Reg != HighGPR &&
268 SystemZ::GR64BitRegClass.contains(Reg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000269 MIB.addReg(Reg, RegState::ImplicitDefine);
270 }
271 }
272
273 return true;
274}
275
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000276void SystemZFrameLowering::
277processFunctionBeforeFrameFinalized(MachineFunction &MF,
278 RegScavenger *RS) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000279 MachineFrameInfo &MFFrame = MF.getFrameInfo();
280 uint64_t MaxReach = (MFFrame.estimateStackSize(MF) +
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000281 SystemZMC::CallFrameSize * 2);
Richard Sandiford23943222013-07-05 13:11:52 +0000282 if (!isUInt<12>(MaxReach)) {
283 // We may need register scavenging slots if some parts of the frame
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000284 // are outside the reach of an unsigned 12-bit displacement.
Richard Sandiford23943222013-07-05 13:11:52 +0000285 // Create 2 for the case where both addresses in an MVC are
286 // out of range.
Matthias Braun941a7052016-07-28 18:40:00 +0000287 RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
288 RS->addScavengingFrameIndex(MFFrame.CreateStackObject(8, 8, false));
Richard Sandiford23943222013-07-05 13:11:52 +0000289 }
Richard Sandiford5dd52f82013-07-05 12:55:00 +0000290}
291
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000292// Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
293static void emitIncrement(MachineBasicBlock &MBB,
294 MachineBasicBlock::iterator &MBBI,
295 const DebugLoc &DL,
296 unsigned Reg, int64_t NumBytes,
297 const TargetInstrInfo *TII) {
298 while (NumBytes) {
299 unsigned Opcode;
300 int64_t ThisVal = NumBytes;
301 if (isInt<16>(NumBytes))
302 Opcode = SystemZ::AGHI;
303 else {
304 Opcode = SystemZ::AGFI;
305 // Make sure we maintain 8-byte stack alignment.
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +0000306 int64_t MinVal = -uint64_t(1) << 31;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000307 int64_t MaxVal = (int64_t(1) << 31) - 8;
308 if (ThisVal < MinVal)
309 ThisVal = MinVal;
310 else if (ThisVal > MaxVal)
311 ThisVal = MaxVal;
312 }
313 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII->get(Opcode), Reg)
314 .addReg(Reg).addImm(ThisVal);
Richard Sandiford14a44492013-05-22 13:38:45 +0000315 // The CC implicit def is dead.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000316 MI->getOperand(3).setIsDead();
317 NumBytes -= ThisVal;
318 }
319}
320
Quentin Colombet61b305e2015-05-05 17:38:16 +0000321void SystemZFrameLowering::emitPrologue(MachineFunction &MF,
322 MachineBasicBlock &MBB) const {
323 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Matthias Braun941a7052016-07-28 18:40:00 +0000324 MachineFrameInfo &MFFrame = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000325 auto *ZII =
326 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000327 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
328 MachineBasicBlock::iterator MBBI = MBB.begin();
329 MachineModuleInfo &MMI = MF.getMMI();
Tim Northover775aaeb2015-11-05 21:54:58 +0000330 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000331 const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +0000332 bool HasFP = hasFP(MF);
333
334 // Debug location must be unknown since the first debug location is used
335 // to determine the end of the prologue.
336 DebugLoc DL;
337
338 // The current offset of the stack pointer from the CFA.
339 int64_t SPOffsetFromCFA = -SystemZMC::CFAOffsetFromInitialSP;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000340
341 if (ZFI->getLowSavedGPR()) {
342 // Skip over the GPR saves.
343 if (MBBI != MBB.end() && MBBI->getOpcode() == SystemZ::STMG)
344 ++MBBI;
345 else
346 llvm_unreachable("Couldn't skip over GPR saves");
347
348 // Add CFI for the GPR saves.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000349 for (auto &Save : CSI) {
350 unsigned Reg = Save.getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351 if (SystemZ::GR64BitRegClass.contains(Reg)) {
352 int64_t Offset = SPOffsetFromCFA + RegSpillOffsets[Reg];
Matthias Braunf23ef432016-11-30 23:48:42 +0000353 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000354 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
355 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
356 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000357 }
358 }
359 }
360
361 uint64_t StackSize = getAllocatedStackSize(MF);
362 if (StackSize) {
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +0000363 // Determine if we want to store a backchain.
364 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
365
366 // If we need backchain, save current stack pointer. R1 is free at this
367 // point.
368 if (StoreBackchain)
369 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR))
370 .addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
371
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000372 // Allocate StackSize bytes.
373 int64_t Delta = -int64_t(StackSize);
374 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, Delta, ZII);
375
376 // Add CFI for the allocation.
Matthias Braunf23ef432016-11-30 23:48:42 +0000377 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000378 MCCFIInstruction::createDefCfaOffset(nullptr, SPOffsetFromCFA + Delta));
379 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
380 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000381 SPOffsetFromCFA += Delta;
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +0000382
383 if (StoreBackchain)
384 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::STG))
385 .addReg(SystemZ::R1D, RegState::Kill).addReg(SystemZ::R15D).addImm(0).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000386 }
387
388 if (HasFP) {
389 // Copy the base of the frame to R11.
390 BuildMI(MBB, MBBI, DL, ZII->get(SystemZ::LGR), SystemZ::R11D)
391 .addReg(SystemZ::R15D);
392
393 // Add CFI for the new frame location.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000394 unsigned HardFP = MRI->getDwarfRegNum(SystemZ::R11D, true);
Matthias Braunf23ef432016-11-30 23:48:42 +0000395 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000396 MCCFIInstruction::createDefCfaRegister(nullptr, HardFP));
397 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
398 .addCFIIndex(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000399
400 // Mark the FramePtr as live at the beginning of every block except
401 // the entry block. (We'll have marked R11 as live on entry when
402 // saving the GPRs.)
Richard Sandiford28c111e2014-03-06 11:00:15 +0000403 for (auto I = std::next(MF.begin()), E = MF.end(); I != E; ++I)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000404 I->addLiveIn(SystemZ::R11D);
405 }
406
407 // Skip over the FPR saves.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000408 SmallVector<unsigned, 8> CFIIndexes;
Richard Sandiford28c111e2014-03-06 11:00:15 +0000409 for (auto &Save : CSI) {
410 unsigned Reg = Save.getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000411 if (SystemZ::FP64BitRegClass.contains(Reg)) {
412 if (MBBI != MBB.end() &&
413 (MBBI->getOpcode() == SystemZ::STD ||
414 MBBI->getOpcode() == SystemZ::STDY))
415 ++MBBI;
416 else
417 llvm_unreachable("Couldn't skip over FPR save");
418
419 // Add CFI for the this save.
Richard Sandiford28c111e2014-03-06 11:00:15 +0000420 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
James Y Knight5567baf2015-08-15 02:32:35 +0000421 unsigned IgnoredFrameReg;
422 int64_t Offset =
423 getFrameIndexReference(MF, Save.getFrameIdx(), IgnoredFrameReg);
424
Matthias Braunf23ef432016-11-30 23:48:42 +0000425 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000426 nullptr, DwarfReg, SPOffsetFromCFA + Offset));
427 CFIIndexes.push_back(CFIIndex);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000428 }
429 }
430 // Complete the CFI for the FPR saves, modelling them as taking effect
431 // after the last save.
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000432 for (auto CFIIndex : CFIIndexes) {
433 BuildMI(MBB, MBBI, DL, ZII->get(TargetOpcode::CFI_INSTRUCTION))
434 .addCFIIndex(CFIIndex);
435 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000436}
437
438void SystemZFrameLowering::emitEpilogue(MachineFunction &MF,
439 MachineBasicBlock &MBB) const {
440 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Eric Christopherfc6de422014-08-05 02:39:49 +0000441 auto *ZII =
442 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000443 SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
444
445 // Skip the return instruction.
Richard Sandiford709bda62013-08-19 12:42:31 +0000446 assert(MBBI->isReturn() && "Can only insert epilogue into returning blocks");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000447
448 uint64_t StackSize = getAllocatedStackSize(MF);
449 if (ZFI->getLowSavedGPR()) {
450 --MBBI;
451 unsigned Opcode = MBBI->getOpcode();
452 if (Opcode != SystemZ::LMG)
453 llvm_unreachable("Expected to see callee-save register restore code");
454
455 unsigned AddrOpNo = 2;
456 DebugLoc DL = MBBI->getDebugLoc();
457 uint64_t Offset = StackSize + MBBI->getOperand(AddrOpNo + 1).getImm();
458 unsigned NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
459
460 // If the offset is too large, use the largest stack-aligned offset
461 // and add the rest to the base register (the stack or frame pointer).
462 if (!NewOpcode) {
463 uint64_t NumBytes = Offset - 0x7fff8;
464 emitIncrement(MBB, MBBI, DL, MBBI->getOperand(AddrOpNo).getReg(),
465 NumBytes, ZII);
466 Offset -= NumBytes;
467 NewOpcode = ZII->getOpcodeForOffset(Opcode, Offset);
468 assert(NewOpcode && "No restore instruction available");
469 }
470
471 MBBI->setDesc(ZII->get(NewOpcode));
472 MBBI->getOperand(AddrOpNo + 1).ChangeToImmediate(Offset);
473 } else if (StackSize) {
474 DebugLoc DL = MBBI->getDebugLoc();
475 emitIncrement(MBB, MBBI, DL, SystemZ::R15D, StackSize, ZII);
476 }
477}
478
479bool SystemZFrameLowering::hasFP(const MachineFunction &MF) const {
480 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000481 MF.getFrameInfo().hasVarSizedObjects() ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000482 MF.getInfo<SystemZMachineFunctionInfo>()->getManipulatesSP());
483}
484
James Y Knight5567baf2015-08-15 02:32:35 +0000485int SystemZFrameLowering::getFrameIndexReference(const MachineFunction &MF,
486 int FI,
487 unsigned &FrameReg) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000488 const MachineFrameInfo &MFFrame = MF.getFrameInfo();
James Y Knight5567baf2015-08-15 02:32:35 +0000489 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
490
491 // Fill in FrameReg output argument.
492 FrameReg = RI->getFrameRegister(MF);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000493
494 // Start with the offset of FI from the top of the caller-allocated frame
495 // (i.e. the top of the 160 bytes allocated by the caller). This initial
496 // offset is therefore negative.
Matthias Braun941a7052016-07-28 18:40:00 +0000497 int64_t Offset = (MFFrame.getObjectOffset(FI) +
498 MFFrame.getOffsetAdjustment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000499
500 // Make the offset relative to the incoming stack pointer.
501 Offset -= getOffsetOfLocalArea();
502
503 // Make the offset relative to the bottom of the frame.
504 Offset += getAllocatedStackSize(MF);
505
506 return Offset;
507}
508
509uint64_t SystemZFrameLowering::
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000510getAllocatedStackSize(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000511 const MachineFrameInfo &MFFrame = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000512
513 // Start with the size of the local variables and spill slots.
Matthias Braun941a7052016-07-28 18:40:00 +0000514 uint64_t StackSize = MFFrame.getStackSize();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000515
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000516 // We need to allocate the ABI-defined 160-byte base area whenever
517 // we allocate stack space for our own use and whenever we call another
518 // function.
Matthias Braun941a7052016-07-28 18:40:00 +0000519 if (StackSize || MFFrame.hasVarSizedObjects() || MFFrame.hasCalls())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000520 StackSize += SystemZMC::CallFrameSize;
521
522 return StackSize;
523}
524
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000525bool
526SystemZFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
527 // The ABI requires us to allocate 160 bytes of stack space for the callee,
528 // with any outgoing stack arguments being placed above that. It seems
529 // better to make that area a permanent feature of the frame even if
530 // we're using a frame pointer.
531 return true;
532}
533
Hans Wennborge1a2e902016-03-31 18:33:38 +0000534MachineBasicBlock::iterator SystemZFrameLowering::
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000535eliminateCallFramePseudoInstr(MachineFunction &MF,
536 MachineBasicBlock &MBB,
537 MachineBasicBlock::iterator MI) const {
538 switch (MI->getOpcode()) {
539 case SystemZ::ADJCALLSTACKDOWN:
540 case SystemZ::ADJCALLSTACKUP:
541 assert(hasReservedCallFrame(MF) &&
542 "ADJSTACKDOWN and ADJSTACKUP should be no-ops");
Hans Wennborge1a2e902016-03-31 18:33:38 +0000543 return MBB.erase(MI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000544 break;
545
546 default:
547 llvm_unreachable("Unexpected call frame instruction");
548 }
549}