blob: 66e248d069e956811c43bb4781706c3021d0cdbd [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
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//
Anton Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the PPC implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "PPCFrameLowering.h"
Roman Divackyc9e23d92012-09-12 14:47:47 +000015#include "PPCInstrBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "PPCInstrInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "PPCMachineFunctionInfo.h"
Eric Christopherd104c312014-06-12 20:54:11 +000018#include "PPCSubtarget.h"
Eric Christopherfcd3d872015-02-13 22:48:53 +000019#include "PPCTargetMachine.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000025#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000027#include "llvm/Target/TargetOptions.h"
28
29using namespace llvm;
30
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000031/// VRRegNo - Map from a numbered VR register to its enum value.
32///
Craig Topperca658c22012-03-11 07:16:55 +000033static const uint16_t VRRegNo[] = {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000034 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
35 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
36 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
37 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
38};
39
Eric Christopherf71609b2015-02-13 00:39:27 +000040static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) {
41 if (STI.isDarwinABI())
42 return STI.isPPC64() ? 16 : 8;
43 // SVR4 ABI:
44 return STI.isPPC64() ? 16 : 4;
45}
46
Eric Christopher736d39e2015-02-13 00:39:36 +000047static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) {
48 return STI.isELFv2ABI() ? 24 : 40;
49}
50
Eric Christopherdc3a8a42015-02-13 00:39:38 +000051static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) {
52 // For the Darwin ABI:
53 // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
54 // for saving the frame pointer (if needed.) While the published ABI has
55 // not used this slot since at least MacOSX 10.2, there is older code
56 // around that does use it, and that needs to continue to work.
57 if (STI.isDarwinABI())
58 return STI.isPPC64() ? -8U : -4U;
59
60 // SVR4 ABI: First slot in the general register save area.
61 return STI.isPPC64() ? -8U : -4U;
62}
63
Eric Christophera4ae2132015-02-13 22:22:57 +000064static unsigned computeLinkageSize(const PPCSubtarget &STI) {
65 if (STI.isDarwinABI() || STI.isPPC64())
66 return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4);
67
68 // SVR4 ABI:
69 return 8;
70}
71
Eric Christopherfcd3d872015-02-13 22:48:53 +000072static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) {
73 if (STI.isDarwinABI())
74 return STI.isPPC64() ? -16U : -8U;
75
76 // SVR4 ABI: First slot in the general register save area.
77 return STI.isPPC64()
78 ? -16U
79 : (STI.getTargetMachine().getRelocationModel() == Reloc::PIC_)
80 ? -12U
81 : -8U;
82}
83
Eric Christopherd104c312014-06-12 20:54:11 +000084PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
85 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
86 (STI.hasQPX() || STI.isBGQ()) ? 32 : 16, 0),
Eric Christopher736d39e2015-02-13 00:39:36 +000087 Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)),
Eric Christopherdc3a8a42015-02-13 00:39:38 +000088 TOCSaveOffset(computeTOCSaveOffset(Subtarget)),
Eric Christophera4ae2132015-02-13 22:22:57 +000089 FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)),
Eric Christopherfcd3d872015-02-13 22:48:53 +000090 LinkageSize(computeLinkageSize(Subtarget)),
91 BasePointerSaveOffset(computeBasePointerSaveOffset(STI)) {}
Eric Christopherd104c312014-06-12 20:54:11 +000092
Eric Christopherd104c312014-06-12 20:54:11 +000093// With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
94const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
95 unsigned &NumEntries) const {
96 if (Subtarget.isDarwinABI()) {
97 NumEntries = 1;
98 if (Subtarget.isPPC64()) {
99 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
100 return &darwin64Offsets;
101 } else {
102 static const SpillSlot darwinOffsets = {PPC::R31, -4};
103 return &darwinOffsets;
104 }
105 }
106
107 // Early exit if not using the SVR4 ABI.
108 if (!Subtarget.isSVR4ABI()) {
109 NumEntries = 0;
110 return nullptr;
111 }
112
113 // Note that the offsets here overlap, but this is fixed up in
114 // processFunctionBeforeFrameFinalized.
115
116 static const SpillSlot Offsets[] = {
117 // Floating-point register save area offsets.
118 {PPC::F31, -8},
119 {PPC::F30, -16},
120 {PPC::F29, -24},
121 {PPC::F28, -32},
122 {PPC::F27, -40},
123 {PPC::F26, -48},
124 {PPC::F25, -56},
125 {PPC::F24, -64},
126 {PPC::F23, -72},
127 {PPC::F22, -80},
128 {PPC::F21, -88},
129 {PPC::F20, -96},
130 {PPC::F19, -104},
131 {PPC::F18, -112},
132 {PPC::F17, -120},
133 {PPC::F16, -128},
134 {PPC::F15, -136},
135 {PPC::F14, -144},
136
137 // General register save area offsets.
138 {PPC::R31, -4},
139 {PPC::R30, -8},
140 {PPC::R29, -12},
141 {PPC::R28, -16},
142 {PPC::R27, -20},
143 {PPC::R26, -24},
144 {PPC::R25, -28},
145 {PPC::R24, -32},
146 {PPC::R23, -36},
147 {PPC::R22, -40},
148 {PPC::R21, -44},
149 {PPC::R20, -48},
150 {PPC::R19, -52},
151 {PPC::R18, -56},
152 {PPC::R17, -60},
153 {PPC::R16, -64},
154 {PPC::R15, -68},
155 {PPC::R14, -72},
156
157 // CR save area offset. We map each of the nonvolatile CR fields
158 // to the slot for CR2, which is the first of the nonvolatile CR
159 // fields to be assigned, so that we only allocate one save slot.
160 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
161 {PPC::CR2, -4},
162
163 // VRSAVE save area offset.
164 {PPC::VRSAVE, -4},
165
166 // Vector register save area
167 {PPC::V31, -16},
168 {PPC::V30, -32},
169 {PPC::V29, -48},
170 {PPC::V28, -64},
171 {PPC::V27, -80},
172 {PPC::V26, -96},
173 {PPC::V25, -112},
174 {PPC::V24, -128},
175 {PPC::V23, -144},
176 {PPC::V22, -160},
177 {PPC::V21, -176},
178 {PPC::V20, -192}};
179
180 static const SpillSlot Offsets64[] = {
181 // Floating-point register save area offsets.
182 {PPC::F31, -8},
183 {PPC::F30, -16},
184 {PPC::F29, -24},
185 {PPC::F28, -32},
186 {PPC::F27, -40},
187 {PPC::F26, -48},
188 {PPC::F25, -56},
189 {PPC::F24, -64},
190 {PPC::F23, -72},
191 {PPC::F22, -80},
192 {PPC::F21, -88},
193 {PPC::F20, -96},
194 {PPC::F19, -104},
195 {PPC::F18, -112},
196 {PPC::F17, -120},
197 {PPC::F16, -128},
198 {PPC::F15, -136},
199 {PPC::F14, -144},
200
201 // General register save area offsets.
202 {PPC::X31, -8},
203 {PPC::X30, -16},
204 {PPC::X29, -24},
205 {PPC::X28, -32},
206 {PPC::X27, -40},
207 {PPC::X26, -48},
208 {PPC::X25, -56},
209 {PPC::X24, -64},
210 {PPC::X23, -72},
211 {PPC::X22, -80},
212 {PPC::X21, -88},
213 {PPC::X20, -96},
214 {PPC::X19, -104},
215 {PPC::X18, -112},
216 {PPC::X17, -120},
217 {PPC::X16, -128},
218 {PPC::X15, -136},
219 {PPC::X14, -144},
220
221 // VRSAVE save area offset.
222 {PPC::VRSAVE, -4},
223
224 // Vector register save area
225 {PPC::V31, -16},
226 {PPC::V30, -32},
227 {PPC::V29, -48},
228 {PPC::V28, -64},
229 {PPC::V27, -80},
230 {PPC::V26, -96},
231 {PPC::V25, -112},
232 {PPC::V24, -128},
233 {PPC::V23, -144},
234 {PPC::V22, -160},
235 {PPC::V21, -176},
236 {PPC::V20, -192}};
237
238 if (Subtarget.isPPC64()) {
239 NumEntries = array_lengthof(Offsets64);
240
241 return Offsets64;
242 } else {
243 NumEntries = array_lengthof(Offsets);
244
245 return Offsets;
246 }
247}
248
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000249/// RemoveVRSaveCode - We have found that this function does not need any code
250/// to manipulate the VRSAVE register, even though it uses vector registers.
251/// This can happen when the only registers used are known to be live in or out
252/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidt38d94582012-10-10 20:54:15 +0000253/// FIXME: The removal of the code results in a compile failure at -O0 when the
254/// function contains a function call, as the GPR containing original VRSAVE
255/// contents is spilled and reloaded around the call. Without the prolog code,
256/// the spill instruction refers to an undefined register. This code needs
257/// to account for all uses of that GPR.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000258static void RemoveVRSaveCode(MachineInstr *MI) {
259 MachineBasicBlock *Entry = MI->getParent();
260 MachineFunction *MF = Entry->getParent();
261
262 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
263 MachineBasicBlock::iterator MBBI = MI;
264 ++MBBI;
265 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
266 MBBI->eraseFromParent();
267
268 bool RemovedAllMTVRSAVEs = true;
269 // See if we can find and remove the MTVRSAVE instruction from all of the
270 // epilog blocks.
271 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
272 // If last instruction is a return instruction, add an epilogue
Evan Cheng7f8e5632011-12-07 07:15:52 +0000273 if (!I->empty() && I->back().isReturn()) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000274 bool FoundIt = false;
275 for (MBBI = I->end(); MBBI != I->begin(); ) {
276 --MBBI;
277 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
278 MBBI->eraseFromParent(); // remove it.
279 FoundIt = true;
280 break;
281 }
282 }
283 RemovedAllMTVRSAVEs &= FoundIt;
284 }
285 }
286
287 // If we found and removed all MTVRSAVE instructions, remove the read of
288 // VRSAVE as well.
289 if (RemovedAllMTVRSAVEs) {
290 MBBI = MI;
291 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
292 --MBBI;
293 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
294 MBBI->eraseFromParent();
295 }
296
297 // Finally, nuke the UPDATE_VRSAVE.
298 MI->eraseFromParent();
299}
300
301// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
302// instruction selector. Based on the vector registers that have been used,
303// transform this into the appropriate ORI instruction.
304static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
305 MachineFunction *MF = MI->getParent()->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000306 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000307 DebugLoc dl = MI->getDebugLoc();
308
309 unsigned UsedRegMask = 0;
310 for (unsigned i = 0; i != 32; ++i)
311 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
312 UsedRegMask |= 1 << (31-i);
313
314 // Live in and live out values already must be in the mask, so don't bother
315 // marking them.
316 for (MachineRegisterInfo::livein_iterator
317 I = MF->getRegInfo().livein_begin(),
318 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelfeea6532013-03-26 20:08:20 +0000319 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000320 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
321 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
322 }
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000323
324 // Live out registers appear as use operands on return instructions.
325 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
326 UsedRegMask != 0 && BI != BE; ++BI) {
327 const MachineBasicBlock &MBB = *BI;
328 if (MBB.empty() || !MBB.back().isReturn())
329 continue;
330 const MachineInstr &Ret = MBB.back();
331 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
332 const MachineOperand &MO = Ret.getOperand(I);
333 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
334 continue;
Hal Finkelfeea6532013-03-26 20:08:20 +0000335 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000336 UsedRegMask &= ~(1 << (31-RegNo));
337 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000338 }
339
340 // If no registers are used, turn this into a copy.
341 if (UsedRegMask == 0) {
342 // Remove all VRSAVE code.
343 RemoveVRSaveCode(MI);
344 return;
345 }
346
347 unsigned SrcReg = MI->getOperand(1).getReg();
348 unsigned DstReg = MI->getOperand(0).getReg();
349
350 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
351 if (DstReg != SrcReg)
352 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
353 .addReg(SrcReg)
354 .addImm(UsedRegMask);
355 else
356 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
357 .addReg(SrcReg, RegState::Kill)
358 .addImm(UsedRegMask);
359 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
360 if (DstReg != SrcReg)
361 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
362 .addReg(SrcReg)
363 .addImm(UsedRegMask >> 16);
364 else
365 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
366 .addReg(SrcReg, RegState::Kill)
367 .addImm(UsedRegMask >> 16);
368 } else {
369 if (DstReg != SrcReg)
370 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
371 .addReg(SrcReg)
372 .addImm(UsedRegMask >> 16);
373 else
374 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
375 .addReg(SrcReg, RegState::Kill)
376 .addImm(UsedRegMask >> 16);
377
378 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
379 .addReg(DstReg, RegState::Kill)
380 .addImm(UsedRegMask & 0xFFFF);
381 }
382
383 // Remove the old UPDATE_VRSAVE instruction.
384 MI->eraseFromParent();
385}
386
Roman Divackyc9e23d92012-09-12 14:47:47 +0000387static bool spillsCR(const MachineFunction &MF) {
388 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
389 return FuncInfo->isCRSpilled();
390}
391
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000392static bool spillsVRSAVE(const MachineFunction &MF) {
393 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
394 return FuncInfo->isVRSAVESpilled();
395}
396
Hal Finkelbb420f12013-03-15 05:06:04 +0000397static bool hasSpills(const MachineFunction &MF) {
398 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
399 return FuncInfo->hasSpills();
400}
401
Hal Finkelfcc51d42013-03-17 04:43:44 +0000402static bool hasNonRISpills(const MachineFunction &MF) {
403 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
404 return FuncInfo->hasNonRISpills();
405}
406
Bill Schmidt82f1c772015-02-10 19:09:05 +0000407/// MustSaveLR - Return true if this function requires that we save the LR
408/// register onto the stack in the prolog and restore it in the epilog of the
409/// function.
410static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
411 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
412
413 // We need a save/restore of LR if there is any def of LR (which is
414 // defined by calls, including the PIC setup sequence), or if there is
415 // some use of the LR stack slot (e.g. for builtin_return_address).
416 // (LR comes in 32 and 64 bit versions.)
417 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
418 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
419}
420
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000421/// determineFrameLayout - Determine the size of the frame and maximum call
422/// frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000423unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
424 bool UpdateMF,
425 bool UseEstimate) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000426 MachineFrameInfo *MFI = MF.getFrameInfo();
427
428 // Get the number of bytes to allocate from the FrameInfo
Hal Finkelbb420f12013-03-15 05:06:04 +0000429 unsigned FrameSize =
430 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000431
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000432 // Get stack alignments. The frame must be aligned to the greatest of these:
433 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
434 unsigned MaxAlign = MFI->getMaxAlignment(); // algmt required by data in frame
Hal Finkela7c54e82013-07-17 00:45:52 +0000435 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
436
Eric Christopherfc6de422014-08-05 02:39:49 +0000437 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000438 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000439
440 // If we are a leaf function, and use up to 224 bytes of stack space,
441 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkel67369882013-04-15 02:07:05 +0000442 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000443 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
444 // stackless code if all local vars are reg-allocated.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000445 bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
Bill Schmidt82f1c772015-02-10 19:09:05 +0000446 unsigned LR = RegInfo->getRARegister();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000447 if (!DisableRedZone &&
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000448 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
449 !Subtarget.isSVR4ABI() || // allocated locals.
Eric Christopherd1737492014-04-29 00:16:40 +0000450 FrameSize == 0) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000451 FrameSize <= 224 && // Fits in red zone.
452 !MFI->hasVarSizedObjects() && // No dynamic alloca.
453 !MFI->adjustsStack() && // No calls.
Bill Schmidt82f1c772015-02-10 19:09:05 +0000454 !MustSaveLR(MF, LR) &&
Hal Finkela7c54e82013-07-17 00:45:52 +0000455 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000456 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000457 if (UpdateMF)
458 MFI->setStackSize(0);
459 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000460 }
461
462 // Get the maximum call frame size of all the calls.
463 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
464
Ulrich Weigandf316e1d2014-06-23 13:47:52 +0000465 // Maximum call frame needs to be at least big enough for linkage area.
Eric Christophera4ae2132015-02-13 22:22:57 +0000466 unsigned minCallFrameSize = getLinkageSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000467 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
468
469 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
470 // that allocations will be aligned.
471 if (MFI->hasVarSizedObjects())
472 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
473
474 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000475 if (UpdateMF)
476 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000477
478 // Include call frame size in total.
479 FrameSize += maxCallFrameSize;
480
481 // Make sure the frame is aligned.
482 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
483
484 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000485 if (UpdateMF)
486 MFI->setStackSize(FrameSize);
487
488 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000489}
490
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000491// hasFP - Return true if the specified function actually has a dedicated frame
492// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000493bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000494 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000495 // FIXME: This is pretty much broken by design: hasFP() might be called really
496 // early, before the stack layout was calculated and thus hasFP() might return
497 // true or false here depending on the time of call.
498 return (MFI->getStackSize()) && needsFP(MF);
499}
500
501// needsFP - Return true if the specified function should have a dedicated frame
502// pointer register. This is true if the function has variable sized allocas or
503// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000504bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000505 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000506
507 // Naked functions have no stack frame pushed, so we don't have a frame
508 // pointer.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000509 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000510 return false;
511
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000512 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
513 MFI->hasVarSizedObjects() ||
Hal Finkel934361a2015-01-14 01:07:51 +0000514 MFI->hasStackMap() || MFI->hasPatchPoint() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000515 (MF.getTarget().Options.GuaranteedTailCallOpt &&
516 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000517}
518
Hal Finkelaa03c032013-03-21 19:03:19 +0000519void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
520 bool is31 = needsFP(MF);
521 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
522 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
523
Eric Christopherfc6de422014-08-05 02:39:49 +0000524 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000525 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Hal Finkelf05d6c72013-07-17 23:50:51 +0000526 bool HasBP = RegInfo->hasBasePointer(MF);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000527 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000528 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
529
Hal Finkelaa03c032013-03-21 19:03:19 +0000530 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
531 BI != BE; ++BI)
532 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
533 --MBBI;
534 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
535 MachineOperand &MO = MBBI->getOperand(I);
536 if (!MO.isReg())
537 continue;
538
539 switch (MO.getReg()) {
540 case PPC::FP:
541 MO.setReg(FPReg);
542 break;
543 case PPC::FP8:
544 MO.setReg(FP8Reg);
545 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000546 case PPC::BP:
547 MO.setReg(BPReg);
548 break;
549 case PPC::BP8:
550 MO.setReg(BP8Reg);
551 break;
552
Hal Finkelaa03c032013-03-21 19:03:19 +0000553 }
554 }
555 }
556}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000557
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000558void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000559 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
560 MachineBasicBlock::iterator MBBI = MBB.begin();
561 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000562 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +0000563 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Eric Christopherfc6de422014-08-05 02:39:49 +0000564 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000565 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000566
567 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000568 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000569 DebugLoc dl;
Jay Foad1f0a44e2014-12-01 09:42:32 +0000570 bool needsCFI = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000571 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000572
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000573 // Get processor type.
574 bool isPPC64 = Subtarget.isPPC64();
575 // Get the ABI.
576 bool isDarwinABI = Subtarget.isDarwinABI();
577 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000578 bool isELFv2ABI = Subtarget.isELFv2ABI();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000579 assert((isDarwinABI || isSVR4ABI) &&
580 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
581
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000582 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
583 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000584 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000585 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
586 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
587 HandleVRSaveUpdate(MBBI, TII);
588 break;
589 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000590 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000591
592 // Move MBBI back to the beginning of the function.
593 MBBI = MBB.begin();
594
595 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000596 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000597 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000598 if (!isInt<32>(NegFrameSize))
599 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000600
Hal Finkelaa03c032013-03-21 19:03:19 +0000601 if (MFI->isFrameAddressTaken())
602 replaceFPWithRealFP(MF);
603
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000604 // Check if the link register (LR) must be saved.
605 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
606 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000607 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000608 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000609 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000610 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000611
Bill Schmidtf381afc2013-08-20 03:12:23 +0000612 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000613 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000614 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
615 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
616 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
617 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
618 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
619 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
620 : PPC::MFLR );
621 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
622 : PPC::STW );
623 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
624 : PPC::STWU );
625 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
626 : PPC::STWUX);
627 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
628 : PPC::LIS );
629 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
630 : PPC::ORI );
631 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
632 : PPC::OR );
633 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
634 : PPC::SUBFC);
635 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
636 : PPC::SUBFIC);
637
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000638 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
639 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
640 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
641 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
642 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
643 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
644
Eric Christopherf71609b2015-02-13 00:39:27 +0000645 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000646
647 int FPOffset = 0;
648 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000649 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000650 MachineFrameInfo *FFI = MF.getFrameInfo();
651 int FPIndex = FI->getFramePointerSaveIndex();
652 assert(FPIndex && "No Frame Pointer Save Slot!");
653 FPOffset = FFI->getObjectOffset(FPIndex);
654 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000655 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000656 }
657 }
658
Hal Finkela7c54e82013-07-17 00:45:52 +0000659 int BPOffset = 0;
660 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000661 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000662 MachineFrameInfo *FFI = MF.getFrameInfo();
663 int BPIndex = FI->getBasePointerSaveIndex();
664 assert(BPIndex && "No Base Pointer Save Slot!");
665 BPOffset = FFI->getObjectOffset(BPIndex);
666 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +0000667 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +0000668 }
669 }
670
Justin Hibbits654346e2015-01-10 01:57:21 +0000671 int PBPOffset = 0;
672 if (FI->usesPICBase()) {
673 MachineFrameInfo *FFI = MF.getFrameInfo();
674 int PBPIndex = FI->getPICBasePointerSaveIndex();
675 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
676 PBPOffset = FFI->getObjectOffset(PBPIndex);
677 }
678
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000679 // Get stack alignments.
680 unsigned MaxAlign = MFI->getMaxAlignment();
681 if (HasBP && MaxAlign > 1)
682 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
683 "Invalid alignment!");
684
685 // Frames of 32KB & larger require special handling because they cannot be
686 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
687 bool isLargeFrame = !isInt<16>(NegFrameSize);
688
Bill Schmidtf381afc2013-08-20 03:12:23 +0000689 if (MustSaveLR)
690 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000691
Bill Schmidtf381afc2013-08-20 03:12:23 +0000692 assert((isPPC64 || MustSaveCRs.empty()) &&
693 "Prologue CR saving supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000694
Bill Schmidtf381afc2013-08-20 03:12:23 +0000695 if (!MustSaveCRs.empty()) { // will only occur for PPC64
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000696 // FIXME: In the ELFv2 ABI, we are not required to save all CR fields.
697 // If only one or two CR fields are clobbered, it could be more
698 // efficient to use mfocrf to selectively save just those fields.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000699 MachineInstrBuilder MIB =
700 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), TempReg);
701 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
702 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000703 }
704
Bill Schmidtf381afc2013-08-20 03:12:23 +0000705 if (HasFP)
706 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
707 BuildMI(MBB, MBBI, dl, StoreInst)
708 .addReg(FPReg)
709 .addImm(FPOffset)
710 .addReg(SPReg);
711
Justin Hibbits654346e2015-01-10 01:57:21 +0000712 if (FI->usesPICBase())
Justin Hibbits98a532d2015-01-08 15:47:19 +0000713 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
714 BuildMI(MBB, MBBI, dl, StoreInst)
715 .addReg(PPC::R30)
Justin Hibbits654346e2015-01-10 01:57:21 +0000716 .addImm(PBPOffset)
Justin Hibbits98a532d2015-01-08 15:47:19 +0000717 .addReg(SPReg);
718
Bill Schmidtf381afc2013-08-20 03:12:23 +0000719 if (HasBP)
720 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
721 BuildMI(MBB, MBBI, dl, StoreInst)
722 .addReg(BPReg)
723 .addImm(BPOffset)
724 .addReg(SPReg);
725
726 if (MustSaveLR)
727 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
728 BuildMI(MBB, MBBI, dl, StoreInst)
729 .addReg(ScratchReg)
730 .addImm(LROffset)
731 .addReg(SPReg);
732
733 if (!MustSaveCRs.empty()) // will only occur for PPC64
734 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
735 .addReg(TempReg, getKillRegState(true))
736 .addImm(8)
737 .addReg(SPReg);
738
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000739 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000740 if (!FrameSize) return;
741
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000742 // Adjust stack pointer: r1 += NegFrameSize.
743 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000744
Bill Schmidtf381afc2013-08-20 03:12:23 +0000745 if (HasBP) {
746 // Save a copy of r1 as the base pointer.
747 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
748 .addReg(SPReg)
749 .addReg(SPReg);
750 }
751
752 if (HasBP && MaxAlign > 1) {
753 if (isPPC64)
754 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
755 .addReg(SPReg)
756 .addImm(0)
757 .addImm(64 - Log2_32(MaxAlign));
758 else // PPC32...
759 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
760 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000761 .addImm(0)
762 .addImm(32 - Log2_32(MaxAlign))
763 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000764 if (!isLargeFrame) {
765 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
766 .addReg(ScratchReg, RegState::Kill)
767 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000768 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000769 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000770 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000771 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
772 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000773 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000774 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
775 .addReg(ScratchReg, RegState::Kill)
776 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000777 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000778 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
779 .addReg(SPReg, RegState::Kill)
780 .addReg(SPReg)
781 .addReg(ScratchReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000782
Bill Schmidtf381afc2013-08-20 03:12:23 +0000783 } else if (!isLargeFrame) {
784 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
785 .addReg(SPReg)
786 .addImm(NegFrameSize)
787 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000788
Bill Schmidtf381afc2013-08-20 03:12:23 +0000789 } else {
790 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
791 .addImm(NegFrameSize >> 16);
792 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
793 .addReg(ScratchReg, RegState::Kill)
794 .addImm(NegFrameSize & 0xFFFF);
795 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
796 .addReg(SPReg, RegState::Kill)
797 .addReg(SPReg)
798 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000799 }
800
Jay Foad1f0a44e2014-12-01 09:42:32 +0000801 // Add Call Frame Information for the instructions we generated above.
802 if (needsCFI) {
803 unsigned CFIIndex;
804
805 if (HasBP) {
806 // Define CFA in terms of BP. Do this in preference to using FP/SP,
807 // because if the stack needed aligning then CFA won't be at a fixed
808 // offset from FP/SP.
809 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
810 CFIIndex = MMI.addFrameInst(
811 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
812 } else {
813 // Adjust the definition of CFA to account for the change in SP.
814 assert(NegFrameSize);
815 CFIIndex = MMI.addFrameInst(
816 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
817 }
Eric Christopher612bb692014-04-29 00:16:46 +0000818 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
819 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000820
821 if (HasFP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +0000822 // Describe where FP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000823 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000824 CFIIndex = MMI.addFrameInst(
825 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000826 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000827 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000828 }
829
Justin Hibbits654346e2015-01-10 01:57:21 +0000830 if (FI->usesPICBase()) {
831 // Describe where FP was saved, at a fixed offset from CFA.
832 unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
833 CFIIndex = MMI.addFrameInst(
834 MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
835 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
836 .addCFIIndex(CFIIndex);
837 }
838
Hal Finkela7c54e82013-07-17 00:45:52 +0000839 if (HasBP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +0000840 // Describe where BP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000841 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000842 CFIIndex = MMI.addFrameInst(
843 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000844 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000845 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000846 }
847
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000848 if (MustSaveLR) {
Jay Foad1f0a44e2014-12-01 09:42:32 +0000849 // Describe where LR was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000850 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000851 CFIIndex = MMI.addFrameInst(
852 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000853 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000854 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000855 }
856 }
857
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000858 // If there is a frame pointer, copy R1 into R31
859 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000860 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
861 .addReg(SPReg)
862 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000863
Jay Foad1f0a44e2014-12-01 09:42:32 +0000864 if (!HasBP && needsCFI) {
865 // Change the definition of CFA from SP+offset to FP+offset, because SP
866 // will change at every alloca.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000867 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000868 unsigned CFIIndex = MMI.addFrameInst(
869 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
870
Eric Christopher612bb692014-04-29 00:16:46 +0000871 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000872 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000873 }
874 }
875
Jay Foad1f0a44e2014-12-01 09:42:32 +0000876 if (needsCFI) {
877 // Describe where callee saved registers were saved, at fixed offsets from
878 // CFA.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000879 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
880 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000881 unsigned Reg = CSI[I].getReg();
882 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000883
884 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
885 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +0000886 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +0000887 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000888
Roman Divackyc9e23d92012-09-12 14:47:47 +0000889 // For SVR4, don't emit a move for the CR spill slot if we haven't
890 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000891 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
892 && MustSaveCRs.empty())
893 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000894
895 // For 64-bit SVR4 when we have spilled CRs, the spill location
896 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000897 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000898 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
899 // the whole CR word. In the ELFv2 ABI, every CR that was
900 // actually saved gets its own CFI record.
901 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000902 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000903 nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +0000904 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000905 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000906 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000907 }
908
909 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000910 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
911 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +0000912 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000913 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000914 }
915 }
916}
917
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000918void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000919 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000920 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
921 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000922 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +0000923 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Eric Christopherfc6de422014-08-05 02:39:49 +0000924 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000925 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000926
927 unsigned RetOpcode = MBBI->getOpcode();
928 DebugLoc dl;
929
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000930 assert((RetOpcode == PPC::BLR ||
Hal Finkelf4a22c02015-01-13 17:47:54 +0000931 RetOpcode == PPC::BLR8 ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000932 RetOpcode == PPC::TCRETURNri ||
933 RetOpcode == PPC::TCRETURNdi ||
934 RetOpcode == PPC::TCRETURNai ||
935 RetOpcode == PPC::TCRETURNri8 ||
936 RetOpcode == PPC::TCRETURNdi8 ||
937 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000938 "Can only insert epilog into returning blocks");
939
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000940 // Get alignment info so we know how to restore the SP.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000941 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000942
943 // Get the number of bytes allocated from the FrameInfo.
944 int FrameSize = MFI->getStackSize();
945
946 // Get processor type.
947 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000948 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000949 bool isSVR4ABI = Subtarget.isSVR4ABI();
950
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000951 // Check if the link register (LR) has been saved.
952 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
953 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000954 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000955 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000956 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000957 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000958
Bill Schmidtf381afc2013-08-20 03:12:23 +0000959 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000960 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000961 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
962 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
963 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
964 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
965 : PPC::MTLR );
966 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
967 : PPC::LWZ );
968 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
969 : PPC::LIS );
970 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
971 : PPC::ORI );
972 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
973 : PPC::ADDI );
974 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
975 : PPC::ADD4 );
976
Eric Christopherf71609b2015-02-13 00:39:27 +0000977 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000978
979 int FPOffset = 0;
980 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000981 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000982 MachineFrameInfo *FFI = MF.getFrameInfo();
983 int FPIndex = FI->getFramePointerSaveIndex();
984 assert(FPIndex && "No Frame Pointer Save Slot!");
985 FPOffset = FFI->getObjectOffset(FPIndex);
986 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000987 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000988 }
989 }
990
Hal Finkela7c54e82013-07-17 00:45:52 +0000991 int BPOffset = 0;
992 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000993 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000994 MachineFrameInfo *FFI = MF.getFrameInfo();
995 int BPIndex = FI->getBasePointerSaveIndex();
996 assert(BPIndex && "No Base Pointer Save Slot!");
997 BPOffset = FFI->getObjectOffset(BPIndex);
998 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +0000999 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001000 }
1001 }
1002
Justin Hibbits654346e2015-01-10 01:57:21 +00001003 int PBPOffset = 0;
1004 if (FI->usesPICBase()) {
1005 MachineFrameInfo *FFI = MF.getFrameInfo();
1006 int PBPIndex = FI->getPICBasePointerSaveIndex();
1007 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
1008 PBPOffset = FFI->getObjectOffset(PBPIndex);
1009 }
1010
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001011 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1012 RetOpcode == PPC::TCRETURNdi ||
1013 RetOpcode == PPC::TCRETURNai ||
1014 RetOpcode == PPC::TCRETURNri8 ||
1015 RetOpcode == PPC::TCRETURNdi8 ||
1016 RetOpcode == PPC::TCRETURNai8;
1017
1018 if (UsesTCRet) {
1019 int MaxTCRetDelta = FI->getTailCallSPDelta();
1020 MachineOperand &StackAdjust = MBBI->getOperand(1);
1021 assert(StackAdjust.isImm() && "Expecting immediate value.");
1022 // Adjust stack pointer.
1023 int StackAdj = StackAdjust.getImm();
1024 int Delta = StackAdj - MaxTCRetDelta;
1025 assert((Delta >= 0) && "Delta must be positive");
1026 if (MaxTCRetDelta>0)
1027 FrameSize += (StackAdj +Delta);
1028 else
1029 FrameSize += StackAdj;
1030 }
1031
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001032 // Frames of 32KB & larger require special handling because they cannot be
1033 // indexed into with a simple LD/LWZ immediate offset operand.
1034 bool isLargeFrame = !isInt<16>(FrameSize);
1035
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001036 if (FrameSize) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001037 // In the prologue, the loaded (or persistent) stack pointer value is offset
1038 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001039
1040 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1041 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1042 // call which invalidates the stack pointer value in SP(0). So we use the
1043 // value of R31 in this case.
1044 if (FI->hasFastCall()) {
1045 assert(HasFP && "Expecting a valid frame pointer.");
1046 if (!isLargeFrame) {
1047 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1048 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001049 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001050 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1051 .addImm(FrameSize >> 16);
1052 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1053 .addReg(ScratchReg, RegState::Kill)
1054 .addImm(FrameSize & 0xFFFF);
1055 BuildMI(MBB, MBBI, dl, AddInst)
1056 .addReg(SPReg)
1057 .addReg(FPReg)
1058 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001059 }
Bill Schmidtf381afc2013-08-20 03:12:23 +00001060 } else if (!isLargeFrame && !HasBP && !MFI->hasVarSizedObjects()) {
1061 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1062 .addReg(SPReg)
1063 .addImm(FrameSize);
1064 } else {
1065 BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
1066 .addImm(0)
1067 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001068 }
Bill Schmidtf381afc2013-08-20 03:12:23 +00001069
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001070 }
1071
Bill Schmidtf381afc2013-08-20 03:12:23 +00001072 if (MustSaveLR)
1073 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1074 .addImm(LROffset)
1075 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001076
Bill Schmidtf381afc2013-08-20 03:12:23 +00001077 assert((isPPC64 || MustSaveCRs.empty()) &&
1078 "Epilogue CR restoring supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +00001079
Bill Schmidtf381afc2013-08-20 03:12:23 +00001080 if (!MustSaveCRs.empty()) // will only occur for PPC64
1081 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1082 .addImm(8)
1083 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001084
Bill Schmidtf381afc2013-08-20 03:12:23 +00001085 if (HasFP)
1086 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1087 .addImm(FPOffset)
1088 .addReg(SPReg);
Hal Finkela7c54e82013-07-17 00:45:52 +00001089
Justin Hibbits654346e2015-01-10 01:57:21 +00001090 if (FI->usesPICBase())
Justin Hibbits98a532d2015-01-08 15:47:19 +00001091 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
1092 BuildMI(MBB, MBBI, dl, LoadInst)
1093 .addReg(PPC::R30)
Justin Hibbits654346e2015-01-10 01:57:21 +00001094 .addImm(PBPOffset)
Justin Hibbits98a532d2015-01-08 15:47:19 +00001095 .addReg(SPReg);
1096
Bill Schmidtf381afc2013-08-20 03:12:23 +00001097 if (HasBP)
1098 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1099 .addImm(BPOffset)
1100 .addReg(SPReg);
Hal Finkel67369882013-04-15 02:07:05 +00001101
Bill Schmidtf381afc2013-08-20 03:12:23 +00001102 if (!MustSaveCRs.empty()) // will only occur for PPC64
1103 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1104 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1105 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001106
Bill Schmidtf381afc2013-08-20 03:12:23 +00001107 if (MustSaveLR)
1108 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001109
1110 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1111 // call optimization
Hal Finkelf4a22c02015-01-13 17:47:54 +00001112 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1113 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001114 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1115 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1116 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001117
1118 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001119 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1120 .addReg(SPReg).addImm(CallerAllocatedAmt);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001121 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001122 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001123 .addImm(CallerAllocatedAmt >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001124 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1125 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001126 .addImm(CallerAllocatedAmt & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001127 BuildMI(MBB, MBBI, dl, AddInst)
1128 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001129 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001130 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001131 }
1132 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001133 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001134 MachineOperand &JumpTarget = MBBI->getOperand(0);
1135 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1136 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1137 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001138 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001139 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1140 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1141 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001142 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001143 MachineOperand &JumpTarget = MBBI->getOperand(0);
1144 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1145 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001146 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001147 MachineOperand &JumpTarget = MBBI->getOperand(0);
1148 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1149 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1150 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001151 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001152 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1153 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1154 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001155 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001156 MachineOperand &JumpTarget = MBBI->getOperand(0);
1157 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1158 }
1159}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001160
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001161void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001162PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001163 RegScavenger *) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001164 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +00001165 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001166
1167 // Save and clear the LR state.
1168 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1169 unsigned LR = RegInfo->getRARegister();
1170 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001171 MachineRegisterInfo &MRI = MF.getRegInfo();
1172 MRI.setPhysRegUnused(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001173
1174 // Save R31 if necessary
1175 int FPSI = FI->getFramePointerSaveIndex();
1176 bool isPPC64 = Subtarget.isPPC64();
1177 bool isDarwinABI = Subtarget.isDarwinABI();
1178 MachineFrameInfo *MFI = MF.getFrameInfo();
1179
1180 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001181 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001182 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001183 int FPOffset = getFramePointerSaveOffset();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001184 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001185 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001186 // Save the result.
1187 FI->setFramePointerSaveIndex(FPSI);
1188 }
1189
Hal Finkela7c54e82013-07-17 00:45:52 +00001190 int BPSI = FI->getBasePointerSaveIndex();
1191 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001192 int BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001193 // Allocate the frame index for the base pointer save area.
1194 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1195 // Save the result.
1196 FI->setBasePointerSaveIndex(BPSI);
1197 }
1198
Justin Hibbits654346e2015-01-10 01:57:21 +00001199 // Reserve stack space for the PIC Base register (R30).
1200 // Only used in SVR4 32-bit.
1201 if (FI->usesPICBase()) {
1202 int PBPSI = FI->getPICBasePointerSaveIndex();
1203 PBPSI = MFI->CreateFixedObject(4, -8, true);
1204 FI->setPICBasePointerSaveIndex(PBPSI);
1205 }
1206
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001207 // Reserve stack space to move the linkage area to in case of a tail call.
1208 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001209 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1210 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001211 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001212 }
1213
Eric Christopherd1737492014-04-29 00:16:40 +00001214 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001215 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001216 if (!isPPC64 && !isDarwinABI &&
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001217 (MRI.isPhysRegUsed(PPC::CR2) ||
1218 MRI.isPhysRegUsed(PPC::CR3) ||
1219 MRI.isPhysRegUsed(PPC::CR4))) {
1220 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1221 FI->setCRSpillFrameIndex(FrameIdx);
1222 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001223}
1224
Hal Finkel5a765fd2013-03-14 20:33:40 +00001225void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001226 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001227 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001228 if (!Subtarget.isSVR4ABI()) {
1229 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001230 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001231 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001232
1233 // Get callee saved register information.
1234 MachineFrameInfo *FFI = MF.getFrameInfo();
1235 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1236
1237 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001238 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001239 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001240 return;
1241 }
1242
1243 unsigned MinGPR = PPC::R31;
1244 unsigned MinG8R = PPC::X31;
1245 unsigned MinFPR = PPC::F31;
1246 unsigned MinVR = PPC::V31;
1247
1248 bool HasGPSaveArea = false;
1249 bool HasG8SaveArea = false;
1250 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001251 bool HasVRSAVESaveArea = false;
1252 bool HasVRSaveArea = false;
1253
1254 SmallVector<CalleeSavedInfo, 18> GPRegs;
1255 SmallVector<CalleeSavedInfo, 18> G8Regs;
1256 SmallVector<CalleeSavedInfo, 18> FPRegs;
1257 SmallVector<CalleeSavedInfo, 18> VRegs;
1258
1259 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1260 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001261 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001262 HasGPSaveArea = true;
1263
1264 GPRegs.push_back(CSI[i]);
1265
1266 if (Reg < MinGPR) {
1267 MinGPR = Reg;
1268 }
Craig Topperabadc662012-04-20 06:31:50 +00001269 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001270 HasG8SaveArea = true;
1271
1272 G8Regs.push_back(CSI[i]);
1273
1274 if (Reg < MinG8R) {
1275 MinG8R = Reg;
1276 }
Craig Topperabadc662012-04-20 06:31:50 +00001277 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001278 HasFPSaveArea = true;
1279
1280 FPRegs.push_back(CSI[i]);
1281
1282 if (Reg < MinFPR) {
1283 MinFPR = Reg;
1284 }
Craig Topperabadc662012-04-20 06:31:50 +00001285 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1286 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001287 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001288 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001289 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001290 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001291 HasVRSaveArea = true;
1292
1293 VRegs.push_back(CSI[i]);
1294
1295 if (Reg < MinVR) {
1296 MinVR = Reg;
1297 }
1298 } else {
1299 llvm_unreachable("Unknown RegisterClass!");
1300 }
1301 }
1302
1303 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Eric Christopher38522b82015-01-30 02:11:26 +00001304 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001305
1306 int64_t LowerBound = 0;
1307
1308 // Take into account stack space reserved for tail calls.
1309 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001310 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1311 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001312 LowerBound = TCSPDelta;
1313 }
1314
1315 // The Floating-point register save area is right below the back chain word
1316 // of the previous stack frame.
1317 if (HasFPSaveArea) {
1318 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1319 int FI = FPRegs[i].getFrameIdx();
1320
1321 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1322 }
1323
Hal Finkelfeea6532013-03-26 20:08:20 +00001324 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001325 }
1326
1327 // Check whether the frame pointer register is allocated. If so, make sure it
1328 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001329 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001330 HasGPSaveArea = true;
1331
1332 int FI = PFI->getFramePointerSaveIndex();
1333 assert(FI && "No Frame Pointer Save Slot!");
1334
1335 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1336 }
1337
Justin Hibbits654346e2015-01-10 01:57:21 +00001338 if (PFI->usesPICBase()) {
1339 HasGPSaveArea = true;
1340
1341 int FI = PFI->getPICBasePointerSaveIndex();
1342 assert(FI && "No PIC Base Pointer Save Slot!");
1343
1344 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1345 }
1346
Eric Christopherfc6de422014-08-05 02:39:49 +00001347 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +00001348 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +00001349 if (RegInfo->hasBasePointer(MF)) {
1350 HasGPSaveArea = true;
1351
1352 int FI = PFI->getBasePointerSaveIndex();
1353 assert(FI && "No Base Pointer Save Slot!");
1354
1355 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1356 }
1357
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001358 // General register save area starts right below the Floating-point
1359 // register save area.
1360 if (HasGPSaveArea || HasG8SaveArea) {
1361 // Move general register save area spill slots down, taking into account
1362 // the size of the Floating-point register save area.
1363 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1364 int FI = GPRegs[i].getFrameIdx();
1365
1366 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1367 }
1368
1369 // Move general register save area spill slots down, taking into account
1370 // the size of the Floating-point register save area.
1371 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1372 int FI = G8Regs[i].getFrameIdx();
1373
1374 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1375 }
1376
1377 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001378 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1379 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001380
1381 if (Subtarget.isPPC64()) {
1382 LowerBound -= (31 - MinReg + 1) * 8;
1383 } else {
1384 LowerBound -= (31 - MinReg + 1) * 4;
1385 }
1386 }
1387
Roman Divackyc9e23d92012-09-12 14:47:47 +00001388 // For 32-bit only, the CR save area is below the general register
1389 // save area. For 64-bit SVR4, the CR save area is addressed relative
1390 // to the stack pointer and hence does not need an adjustment here.
1391 // Only CR2 (the first nonvolatile spilled) has an associated frame
1392 // index so that we have a single uniform save area.
1393 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001394 // Adjust the frame index of the CR spill slot.
1395 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1396 unsigned Reg = CSI[i].getReg();
1397
Roman Divackyc9e23d92012-09-12 14:47:47 +00001398 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001399 // Leave Darwin logic as-is.
1400 || (!Subtarget.isSVR4ABI() &&
1401 (PPC::CRBITRCRegClass.contains(Reg) ||
1402 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001403 int FI = CSI[i].getFrameIdx();
1404
1405 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1406 }
1407 }
1408
1409 LowerBound -= 4; // The CR save area is always 4 bytes long.
1410 }
1411
1412 if (HasVRSAVESaveArea) {
1413 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1414 // which have the VRSAVE register class?
1415 // Adjust the frame index of the VRSAVE spill slot.
1416 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1417 unsigned Reg = CSI[i].getReg();
1418
Craig Topperabadc662012-04-20 06:31:50 +00001419 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001420 int FI = CSI[i].getFrameIdx();
1421
1422 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1423 }
1424 }
1425
1426 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1427 }
1428
1429 if (HasVRSaveArea) {
1430 // Insert alignment padding, we need 16-byte alignment.
1431 LowerBound = (LowerBound - 15) & ~(15);
1432
1433 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1434 int FI = VRegs[i].getFrameIdx();
1435
1436 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1437 }
1438 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001439
1440 addScavengingSpillSlot(MF, RS);
1441}
1442
1443void
1444PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1445 RegScavenger *RS) const {
1446 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1447 // a large stack, which will require scavenging a register to materialize a
1448 // large offset.
1449
1450 // We need to have a scavenger spill slot for spills if the frame size is
1451 // large. In case there is no free register for large-offset addressing,
1452 // this slot is used for the necessary emergency spill. Also, we need the
1453 // slot for dynamic stack allocations.
1454
1455 // The scavenger might be invoked if the frame offset does not fit into
1456 // the 16-bit immediate. We don't know the complete frame size here
1457 // because we've not yet computed callee-saved register spills or the
1458 // needed alignment padding.
1459 unsigned StackSize = determineFrameLayout(MF, false, true);
1460 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001461 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1462 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001463 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1464 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1465 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkel9e331c22013-03-22 23:32:27 +00001466 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkelbb420f12013-03-15 05:06:04 +00001467 RC->getAlignment(),
1468 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001469
Hal Finkel18607632013-07-18 04:28:21 +00001470 // Might we have over-aligned allocas?
1471 bool HasAlVars = MFI->hasVarSizedObjects() &&
1472 MFI->getMaxAlignment() > getStackAlignment();
1473
Hal Finkel0dfbb052013-03-26 18:57:22 +00001474 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001475 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Hal Finkel0dfbb052013-03-26 18:57:22 +00001476 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1477 RC->getAlignment(),
1478 false));
1479
Hal Finkelbb420f12013-03-15 05:06:04 +00001480 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001481}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001482
Eric Christopherd1737492014-04-29 00:16:40 +00001483bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001484PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001485 MachineBasicBlock::iterator MI,
1486 const std::vector<CalleeSavedInfo> &CSI,
1487 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001488
1489 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1490 // Return false otherwise to maintain pre-existing behavior.
1491 if (!Subtarget.isSVR4ABI())
1492 return false;
1493
1494 MachineFunction *MF = MBB.getParent();
1495 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +00001496 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001497 DebugLoc DL;
1498 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001499 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001500
Roman Divackyc9e23d92012-09-12 14:47:47 +00001501 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1502 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001503 // Only Darwin actually uses the VRSAVE register, but it can still appear
1504 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1505 // Darwin, ignore it.
1506 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1507 continue;
1508
Roman Divackyc9e23d92012-09-12 14:47:47 +00001509 // CR2 through CR4 are the nonvolatile CR fields.
1510 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1511
Roman Divackyc9e23d92012-09-12 14:47:47 +00001512 // Add the callee-saved register as live-in; it's killed at the spill.
1513 MBB.addLiveIn(Reg);
1514
Hal Finkel2f293912013-04-13 23:06:15 +00001515 if (CRSpilled && IsCRField) {
1516 CRMIB.addReg(Reg, RegState::ImplicitKill);
1517 continue;
1518 }
1519
Roman Divackyc9e23d92012-09-12 14:47:47 +00001520 // Insert the spill to the stack frame.
1521 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001522 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001523 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001524 // The actual spill will happen at the start of the prologue.
1525 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001526 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001527 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001528 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001529
Eric Christopherd1737492014-04-29 00:16:40 +00001530 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1531 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1532 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001533 .addReg(Reg, RegState::ImplicitKill);
1534
Eric Christopherd1737492014-04-29 00:16:40 +00001535 MBB.insert(MI, CRMIB);
1536 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1537 .addReg(PPC::R12,
1538 getKillRegState(true)),
1539 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001540 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001541 } else {
1542 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1543 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001544 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001545 }
1546 }
1547 return true;
1548}
1549
1550static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001551restoreCRs(bool isPPC64, bool is31,
1552 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001553 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1554 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001555
1556 MachineFunction *MF = MBB.getParent();
Eric Christophercccae792015-01-30 22:02:31 +00001557 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001558 DebugLoc DL;
1559 unsigned RestoreOp, MoveReg;
1560
Hal Finkel67369882013-04-15 02:07:05 +00001561 if (isPPC64)
1562 // This is handled during epilogue generation.
1563 return;
1564 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001565 // 32-bit: FP-relative
1566 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00001567 PPC::R12),
1568 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00001569 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001570 MoveReg = PPC::R12;
1571 }
Eric Christopherd1737492014-04-29 00:16:40 +00001572
Roman Divackyc9e23d92012-09-12 14:47:47 +00001573 if (CR2Spilled)
1574 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00001575 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001576
1577 if (CR3Spilled)
1578 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00001579 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001580
1581 if (CR4Spilled)
1582 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00001583 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001584}
1585
Eli Bendersky8da87162013-02-21 20:05:00 +00001586void PPCFrameLowering::
1587eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1588 MachineBasicBlock::iterator I) const {
Eric Christopher38522b82015-01-30 02:11:26 +00001589 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
Eli Bendersky8da87162013-02-21 20:05:00 +00001590 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1591 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1592 // Add (actually subtract) back the amount the callee popped on return.
1593 if (int CalleeAmt = I->getOperand(1).getImm()) {
1594 bool is64Bit = Subtarget.isPPC64();
1595 CalleeAmt *= -1;
1596 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1597 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1598 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1599 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1600 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1601 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1602 MachineInstr *MI = I;
1603 DebugLoc dl = MI->getDebugLoc();
1604
1605 if (isInt<16>(CalleeAmt)) {
1606 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1607 .addReg(StackReg, RegState::Kill)
1608 .addImm(CalleeAmt);
1609 } else {
1610 MachineBasicBlock::iterator MBBI = I;
1611 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1612 .addImm(CalleeAmt >> 16);
1613 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1614 .addReg(TmpReg, RegState::Kill)
1615 .addImm(CalleeAmt & 0xFFFF);
1616 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1617 .addReg(StackReg, RegState::Kill)
1618 .addReg(TmpReg);
1619 }
1620 }
1621 }
1622 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1623 MBB.erase(I);
1624}
1625
Eric Christopherd1737492014-04-29 00:16:40 +00001626bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001627PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001628 MachineBasicBlock::iterator MI,
1629 const std::vector<CalleeSavedInfo> &CSI,
1630 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001631
1632 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1633 // Return false otherwise to maintain pre-existing behavior.
1634 if (!Subtarget.isSVR4ABI())
1635 return false;
1636
1637 MachineFunction *MF = MBB.getParent();
1638 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +00001639 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001640 bool CR2Spilled = false;
1641 bool CR3Spilled = false;
1642 bool CR4Spilled = false;
1643 unsigned CSIIndex = 0;
1644
1645 // Initialize insertion-point logic; we will be restoring in reverse
1646 // order of spill.
1647 MachineBasicBlock::iterator I = MI, BeforeI = I;
1648 bool AtStart = I == MBB.begin();
1649
1650 if (!AtStart)
1651 --BeforeI;
1652
1653 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1654 unsigned Reg = CSI[i].getReg();
1655
Hal Finkelac1a24b2013-06-28 22:29:56 +00001656 // Only Darwin actually uses the VRSAVE register, but it can still appear
1657 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1658 // Darwin, ignore it.
1659 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1660 continue;
1661
Roman Divackyc9e23d92012-09-12 14:47:47 +00001662 if (Reg == PPC::CR2) {
1663 CR2Spilled = true;
1664 // The spill slot is associated only with CR2, which is the
1665 // first nonvolatile spilled. Save it here.
1666 CSIIndex = i;
1667 continue;
1668 } else if (Reg == PPC::CR3) {
1669 CR3Spilled = true;
1670 continue;
1671 } else if (Reg == PPC::CR4) {
1672 CR4Spilled = true;
1673 continue;
1674 } else {
1675 // When we first encounter a non-CR register after seeing at
1676 // least one CR register, restore all spilled CRs together.
1677 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00001678 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00001679 bool is31 = needsFP(*MF);
1680 restoreCRs(Subtarget.isPPC64(), is31,
1681 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001682 MBB, I, CSI, CSIIndex);
1683 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001684 }
1685
1686 // Default behavior for non-CR saves.
1687 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1688 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00001689 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001690 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00001691 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00001692 }
1693
1694 // Insert in reverse order.
1695 if (AtStart)
1696 I = MBB.begin();
1697 else {
1698 I = BeforeI;
1699 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00001700 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001701 }
1702
1703 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00001704 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00001705 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001706 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001707 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001708 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001709
1710 return true;
1711}