blob: 6f660e968e8a620bb764c3418165f21a9c689b96 [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,
Hal Finkelc93a9a22015-02-25 01:06:45 +000086 STI.getPlatformStackAlignment(), 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
Matthias Braun9912bb82015-07-14 17:52:07 +0000309 const MachineRegisterInfo &MRI = MF->getRegInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000310 unsigned UsedRegMask = 0;
311 for (unsigned i = 0; i != 32; ++i)
Matthias Braun9912bb82015-07-14 17:52:07 +0000312 if (MRI.isPhysRegModified(VRRegNo[i]))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000313 UsedRegMask |= 1 << (31-i);
314
315 // Live in and live out values already must be in the mask, so don't bother
316 // marking them.
317 for (MachineRegisterInfo::livein_iterator
318 I = MF->getRegInfo().livein_begin(),
319 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelfeea6532013-03-26 20:08:20 +0000320 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000321 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
322 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
323 }
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000324
325 // Live out registers appear as use operands on return instructions.
326 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
327 UsedRegMask != 0 && BI != BE; ++BI) {
328 const MachineBasicBlock &MBB = *BI;
329 if (MBB.empty() || !MBB.back().isReturn())
330 continue;
331 const MachineInstr &Ret = MBB.back();
332 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
333 const MachineOperand &MO = Ret.getOperand(I);
334 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
335 continue;
Hal Finkelfeea6532013-03-26 20:08:20 +0000336 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000337 UsedRegMask &= ~(1 << (31-RegNo));
338 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000339 }
340
341 // If no registers are used, turn this into a copy.
342 if (UsedRegMask == 0) {
343 // Remove all VRSAVE code.
344 RemoveVRSaveCode(MI);
345 return;
346 }
347
348 unsigned SrcReg = MI->getOperand(1).getReg();
349 unsigned DstReg = MI->getOperand(0).getReg();
350
351 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
352 if (DstReg != SrcReg)
353 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
354 .addReg(SrcReg)
355 .addImm(UsedRegMask);
356 else
357 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
358 .addReg(SrcReg, RegState::Kill)
359 .addImm(UsedRegMask);
360 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
361 if (DstReg != SrcReg)
362 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
363 .addReg(SrcReg)
364 .addImm(UsedRegMask >> 16);
365 else
366 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
367 .addReg(SrcReg, RegState::Kill)
368 .addImm(UsedRegMask >> 16);
369 } else {
370 if (DstReg != SrcReg)
371 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
372 .addReg(SrcReg)
373 .addImm(UsedRegMask >> 16);
374 else
375 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
376 .addReg(SrcReg, RegState::Kill)
377 .addImm(UsedRegMask >> 16);
378
379 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
380 .addReg(DstReg, RegState::Kill)
381 .addImm(UsedRegMask & 0xFFFF);
382 }
383
384 // Remove the old UPDATE_VRSAVE instruction.
385 MI->eraseFromParent();
386}
387
Roman Divackyc9e23d92012-09-12 14:47:47 +0000388static bool spillsCR(const MachineFunction &MF) {
389 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
390 return FuncInfo->isCRSpilled();
391}
392
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000393static bool spillsVRSAVE(const MachineFunction &MF) {
394 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
395 return FuncInfo->isVRSAVESpilled();
396}
397
Hal Finkelbb420f12013-03-15 05:06:04 +0000398static bool hasSpills(const MachineFunction &MF) {
399 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
400 return FuncInfo->hasSpills();
401}
402
Hal Finkelfcc51d42013-03-17 04:43:44 +0000403static bool hasNonRISpills(const MachineFunction &MF) {
404 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
405 return FuncInfo->hasNonRISpills();
406}
407
Bill Schmidt82f1c772015-02-10 19:09:05 +0000408/// MustSaveLR - Return true if this function requires that we save the LR
409/// register onto the stack in the prolog and restore it in the epilog of the
410/// function.
411static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
412 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
413
414 // We need a save/restore of LR if there is any def of LR (which is
415 // defined by calls, including the PIC setup sequence), or if there is
416 // some use of the LR stack slot (e.g. for builtin_return_address).
417 // (LR comes in 32 and 64 bit versions.)
418 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
419 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
420}
421
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000422/// determineFrameLayout - Determine the size of the frame and maximum call
423/// frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000424unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
425 bool UpdateMF,
426 bool UseEstimate) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000427 MachineFrameInfo *MFI = MF.getFrameInfo();
428
429 // Get the number of bytes to allocate from the FrameInfo
Hal Finkelbb420f12013-03-15 05:06:04 +0000430 unsigned FrameSize =
431 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000432
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000433 // Get stack alignments. The frame must be aligned to the greatest of these:
434 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
435 unsigned MaxAlign = MFI->getMaxAlignment(); // algmt required by data in frame
Hal Finkela7c54e82013-07-17 00:45:52 +0000436 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
437
Eric Christopherfc6de422014-08-05 02:39:49 +0000438 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000439 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000440
441 // If we are a leaf function, and use up to 224 bytes of stack space,
442 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkel67369882013-04-15 02:07:05 +0000443 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000444 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
445 // stackless code if all local vars are reg-allocated.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000446 bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
Bill Schmidt82f1c772015-02-10 19:09:05 +0000447 unsigned LR = RegInfo->getRARegister();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000448 if (!DisableRedZone &&
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000449 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
450 !Subtarget.isSVR4ABI() || // allocated locals.
Eric Christopherd1737492014-04-29 00:16:40 +0000451 FrameSize == 0) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000452 FrameSize <= 224 && // Fits in red zone.
453 !MFI->hasVarSizedObjects() && // No dynamic alloca.
454 !MFI->adjustsStack() && // No calls.
Bill Schmidt82f1c772015-02-10 19:09:05 +0000455 !MustSaveLR(MF, LR) &&
Hal Finkela7c54e82013-07-17 00:45:52 +0000456 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000457 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000458 if (UpdateMF)
459 MFI->setStackSize(0);
460 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000461 }
462
463 // Get the maximum call frame size of all the calls.
464 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
465
Ulrich Weigandf316e1d2014-06-23 13:47:52 +0000466 // Maximum call frame needs to be at least big enough for linkage area.
Eric Christophera4ae2132015-02-13 22:22:57 +0000467 unsigned minCallFrameSize = getLinkageSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000468 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
469
470 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
471 // that allocations will be aligned.
472 if (MFI->hasVarSizedObjects())
473 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
474
475 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000476 if (UpdateMF)
477 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000478
479 // Include call frame size in total.
480 FrameSize += maxCallFrameSize;
481
482 // Make sure the frame is aligned.
483 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
484
485 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000486 if (UpdateMF)
487 MFI->setStackSize(FrameSize);
488
489 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000490}
491
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000492// hasFP - Return true if the specified function actually has a dedicated frame
493// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000494bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000495 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000496 // FIXME: This is pretty much broken by design: hasFP() might be called really
497 // early, before the stack layout was calculated and thus hasFP() might return
498 // true or false here depending on the time of call.
499 return (MFI->getStackSize()) && needsFP(MF);
500}
501
502// needsFP - Return true if the specified function should have a dedicated frame
503// pointer register. This is true if the function has variable sized allocas or
504// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000505bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000506 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000507
508 // Naked functions have no stack frame pushed, so we don't have a frame
509 // pointer.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000510 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000511 return false;
512
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000513 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
514 MFI->hasVarSizedObjects() ||
Hal Finkel934361a2015-01-14 01:07:51 +0000515 MFI->hasStackMap() || MFI->hasPatchPoint() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000516 (MF.getTarget().Options.GuaranteedTailCallOpt &&
517 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000518}
519
Hal Finkelaa03c032013-03-21 19:03:19 +0000520void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
521 bool is31 = needsFP(MF);
522 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
523 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
524
Eric Christopherfc6de422014-08-05 02:39:49 +0000525 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000526 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Hal Finkelf05d6c72013-07-17 23:50:51 +0000527 bool HasBP = RegInfo->hasBasePointer(MF);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000528 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000529 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
530
Hal Finkelaa03c032013-03-21 19:03:19 +0000531 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
532 BI != BE; ++BI)
533 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
534 --MBBI;
535 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
536 MachineOperand &MO = MBBI->getOperand(I);
537 if (!MO.isReg())
538 continue;
539
540 switch (MO.getReg()) {
541 case PPC::FP:
542 MO.setReg(FPReg);
543 break;
544 case PPC::FP8:
545 MO.setReg(FP8Reg);
546 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000547 case PPC::BP:
548 MO.setReg(BPReg);
549 break;
550 case PPC::BP8:
551 MO.setReg(BP8Reg);
552 break;
553
Hal Finkelaa03c032013-03-21 19:03:19 +0000554 }
555 }
556 }
557}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000558
Quentin Colombet61b305e2015-05-05 17:38:16 +0000559void PPCFrameLowering::emitPrologue(MachineFunction &MF,
560 MachineBasicBlock &MBB) const {
561 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000562 MachineBasicBlock::iterator MBBI = MBB.begin();
563 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000564 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +0000565 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Eric Christopherfc6de422014-08-05 02:39:49 +0000566 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000567 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000568
569 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000570 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000571 DebugLoc dl;
Jay Foad1f0a44e2014-12-01 09:42:32 +0000572 bool needsCFI = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000573 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000574
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000575 // Get processor type.
576 bool isPPC64 = Subtarget.isPPC64();
577 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000578 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000579 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chandler Carruth003ed332015-02-14 09:14:44 +0000580 assert((Subtarget.isDarwinABI() || isSVR4ABI) &&
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000581 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
582
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000583 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
584 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000585 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000586 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
587 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
588 HandleVRSaveUpdate(MBBI, TII);
589 break;
590 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000591 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000592
593 // Move MBBI back to the beginning of the function.
594 MBBI = MBB.begin();
595
596 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000597 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000598 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000599 if (!isInt<32>(NegFrameSize))
600 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000601
Hal Finkelaa03c032013-03-21 19:03:19 +0000602 if (MFI->isFrameAddressTaken())
603 replaceFPWithRealFP(MF);
604
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000605 // Check if the link register (LR) must be saved.
606 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
607 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000608 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000609 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000610 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000611 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000612
Bill Schmidtf381afc2013-08-20 03:12:23 +0000613 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000614 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000615 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
616 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
617 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
618 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
619 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
620 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
621 : PPC::MFLR );
622 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
623 : PPC::STW );
624 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
625 : PPC::STWU );
626 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
627 : PPC::STWUX);
628 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
629 : PPC::LIS );
630 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
631 : PPC::ORI );
632 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
633 : PPC::OR );
634 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
635 : PPC::SUBFC);
636 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
637 : PPC::SUBFIC);
638
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000639 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
640 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
641 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
642 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
643 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
644 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
645
Eric Christopherf71609b2015-02-13 00:39:27 +0000646 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000647
648 int FPOffset = 0;
649 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000650 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000651 MachineFrameInfo *FFI = MF.getFrameInfo();
652 int FPIndex = FI->getFramePointerSaveIndex();
653 assert(FPIndex && "No Frame Pointer Save Slot!");
654 FPOffset = FFI->getObjectOffset(FPIndex);
655 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000656 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000657 }
658 }
659
Hal Finkela7c54e82013-07-17 00:45:52 +0000660 int BPOffset = 0;
661 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000662 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000663 MachineFrameInfo *FFI = MF.getFrameInfo();
664 int BPIndex = FI->getBasePointerSaveIndex();
665 assert(BPIndex && "No Base Pointer Save Slot!");
666 BPOffset = FFI->getObjectOffset(BPIndex);
667 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +0000668 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +0000669 }
670 }
671
Justin Hibbits654346e2015-01-10 01:57:21 +0000672 int PBPOffset = 0;
673 if (FI->usesPICBase()) {
674 MachineFrameInfo *FFI = MF.getFrameInfo();
675 int PBPIndex = FI->getPICBasePointerSaveIndex();
676 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
677 PBPOffset = FFI->getObjectOffset(PBPIndex);
678 }
679
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000680 // Get stack alignments.
681 unsigned MaxAlign = MFI->getMaxAlignment();
682 if (HasBP && MaxAlign > 1)
683 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
684 "Invalid alignment!");
685
686 // Frames of 32KB & larger require special handling because they cannot be
687 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
688 bool isLargeFrame = !isInt<16>(NegFrameSize);
689
Bill Schmidtf381afc2013-08-20 03:12:23 +0000690 if (MustSaveLR)
691 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000692
Bill Schmidtf381afc2013-08-20 03:12:23 +0000693 assert((isPPC64 || MustSaveCRs.empty()) &&
694 "Prologue CR saving supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000695
Bill Schmidtf381afc2013-08-20 03:12:23 +0000696 if (!MustSaveCRs.empty()) { // will only occur for PPC64
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000697 // FIXME: In the ELFv2 ABI, we are not required to save all CR fields.
698 // If only one or two CR fields are clobbered, it could be more
699 // efficient to use mfocrf to selectively save just those fields.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000700 MachineInstrBuilder MIB =
701 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), TempReg);
702 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
703 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000704 }
705
Bill Schmidtf381afc2013-08-20 03:12:23 +0000706 if (HasFP)
707 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
708 BuildMI(MBB, MBBI, dl, StoreInst)
709 .addReg(FPReg)
710 .addImm(FPOffset)
711 .addReg(SPReg);
712
Justin Hibbits654346e2015-01-10 01:57:21 +0000713 if (FI->usesPICBase())
Justin Hibbits98a532d2015-01-08 15:47:19 +0000714 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
715 BuildMI(MBB, MBBI, dl, StoreInst)
716 .addReg(PPC::R30)
Justin Hibbits654346e2015-01-10 01:57:21 +0000717 .addImm(PBPOffset)
Justin Hibbits98a532d2015-01-08 15:47:19 +0000718 .addReg(SPReg);
719
Bill Schmidtf381afc2013-08-20 03:12:23 +0000720 if (HasBP)
721 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
722 BuildMI(MBB, MBBI, dl, StoreInst)
723 .addReg(BPReg)
724 .addImm(BPOffset)
725 .addReg(SPReg);
726
727 if (MustSaveLR)
728 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
729 BuildMI(MBB, MBBI, dl, StoreInst)
730 .addReg(ScratchReg)
731 .addImm(LROffset)
732 .addReg(SPReg);
733
734 if (!MustSaveCRs.empty()) // will only occur for PPC64
735 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
736 .addReg(TempReg, getKillRegState(true))
737 .addImm(8)
738 .addReg(SPReg);
739
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000740 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000741 if (!FrameSize) return;
742
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000743 // Adjust stack pointer: r1 += NegFrameSize.
744 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000745
Bill Schmidtf381afc2013-08-20 03:12:23 +0000746 if (HasBP) {
747 // Save a copy of r1 as the base pointer.
748 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
749 .addReg(SPReg)
750 .addReg(SPReg);
751 }
752
753 if (HasBP && MaxAlign > 1) {
754 if (isPPC64)
755 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
756 .addReg(SPReg)
757 .addImm(0)
758 .addImm(64 - Log2_32(MaxAlign));
759 else // PPC32...
760 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
761 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000762 .addImm(0)
763 .addImm(32 - Log2_32(MaxAlign))
764 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000765 if (!isLargeFrame) {
766 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
767 .addReg(ScratchReg, RegState::Kill)
768 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000769 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000770 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000771 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000772 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
773 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000774 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000775 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
776 .addReg(ScratchReg, RegState::Kill)
777 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000778 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000779 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
780 .addReg(SPReg, RegState::Kill)
781 .addReg(SPReg)
782 .addReg(ScratchReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000783
Bill Schmidtf381afc2013-08-20 03:12:23 +0000784 } else if (!isLargeFrame) {
785 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
786 .addReg(SPReg)
787 .addImm(NegFrameSize)
788 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000789
Bill Schmidtf381afc2013-08-20 03:12:23 +0000790 } else {
791 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
792 .addImm(NegFrameSize >> 16);
793 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
794 .addReg(ScratchReg, RegState::Kill)
795 .addImm(NegFrameSize & 0xFFFF);
796 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
797 .addReg(SPReg, RegState::Kill)
798 .addReg(SPReg)
799 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000800 }
801
Jay Foad1f0a44e2014-12-01 09:42:32 +0000802 // Add Call Frame Information for the instructions we generated above.
803 if (needsCFI) {
804 unsigned CFIIndex;
805
806 if (HasBP) {
807 // Define CFA in terms of BP. Do this in preference to using FP/SP,
808 // because if the stack needed aligning then CFA won't be at a fixed
809 // offset from FP/SP.
810 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
811 CFIIndex = MMI.addFrameInst(
812 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
813 } else {
814 // Adjust the definition of CFA to account for the change in SP.
815 assert(NegFrameSize);
816 CFIIndex = MMI.addFrameInst(
817 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
818 }
Eric Christopher612bb692014-04-29 00:16:46 +0000819 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
820 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000821
822 if (HasFP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +0000823 // Describe where FP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000824 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000825 CFIIndex = MMI.addFrameInst(
826 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000827 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000828 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000829 }
830
Justin Hibbits654346e2015-01-10 01:57:21 +0000831 if (FI->usesPICBase()) {
832 // Describe where FP was saved, at a fixed offset from CFA.
833 unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
834 CFIIndex = MMI.addFrameInst(
835 MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
836 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
837 .addCFIIndex(CFIIndex);
838 }
839
Hal Finkela7c54e82013-07-17 00:45:52 +0000840 if (HasBP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +0000841 // Describe where BP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000842 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000843 CFIIndex = MMI.addFrameInst(
844 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000845 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000846 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000847 }
848
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000849 if (MustSaveLR) {
Jay Foad1f0a44e2014-12-01 09:42:32 +0000850 // Describe where LR was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000851 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000852 CFIIndex = MMI.addFrameInst(
853 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000854 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000855 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000856 }
857 }
858
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000859 // If there is a frame pointer, copy R1 into R31
860 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000861 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
862 .addReg(SPReg)
863 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000864
Jay Foad1f0a44e2014-12-01 09:42:32 +0000865 if (!HasBP && needsCFI) {
866 // Change the definition of CFA from SP+offset to FP+offset, because SP
867 // will change at every alloca.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000868 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000869 unsigned CFIIndex = MMI.addFrameInst(
870 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
871
Eric Christopher612bb692014-04-29 00:16:46 +0000872 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000873 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000874 }
875 }
876
Jay Foad1f0a44e2014-12-01 09:42:32 +0000877 if (needsCFI) {
878 // Describe where callee saved registers were saved, at fixed offsets from
879 // CFA.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000880 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
881 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000882 unsigned Reg = CSI[I].getReg();
883 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000884
885 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
886 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +0000887 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +0000888 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000889
Roman Divackyc9e23d92012-09-12 14:47:47 +0000890 // For SVR4, don't emit a move for the CR spill slot if we haven't
891 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000892 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
893 && MustSaveCRs.empty())
894 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000895
896 // For 64-bit SVR4 when we have spilled CRs, the spill location
897 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000898 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000899 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
900 // the whole CR word. In the ELFv2 ABI, every CR that was
901 // actually saved gets its own CFI record.
902 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000903 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000904 nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +0000905 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000906 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000907 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000908 }
909
910 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000911 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
912 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +0000913 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000914 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000915 }
916 }
917}
918
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000919void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000920 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000921 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
922 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000923 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +0000924 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Eric Christopherfc6de422014-08-05 02:39:49 +0000925 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000926 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000927
928 unsigned RetOpcode = MBBI->getOpcode();
929 DebugLoc dl;
930
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000931 assert((RetOpcode == PPC::BLR ||
Hal Finkelf4a22c02015-01-13 17:47:54 +0000932 RetOpcode == PPC::BLR8 ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000933 RetOpcode == PPC::TCRETURNri ||
934 RetOpcode == PPC::TCRETURNdi ||
935 RetOpcode == PPC::TCRETURNai ||
936 RetOpcode == PPC::TCRETURNri8 ||
937 RetOpcode == PPC::TCRETURNdi8 ||
938 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000939 "Can only insert epilog into returning blocks");
940
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000941 // Get alignment info so we know how to restore the SP.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000942 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000943
944 // Get the number of bytes allocated from the FrameInfo.
945 int FrameSize = MFI->getStackSize();
946
947 // Get processor type.
948 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000949 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000950 bool isSVR4ABI = Subtarget.isSVR4ABI();
951
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000952 // Check if the link register (LR) has been saved.
953 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
954 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000955 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000956 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000957 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000958 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000959
Bill Schmidtf381afc2013-08-20 03:12:23 +0000960 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000961 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000962 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
963 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
964 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
965 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
966 : PPC::MTLR );
967 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
968 : PPC::LWZ );
969 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
970 : PPC::LIS );
971 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
972 : PPC::ORI );
973 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
974 : PPC::ADDI );
975 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
976 : PPC::ADD4 );
977
Eric Christopherf71609b2015-02-13 00:39:27 +0000978 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000979
980 int FPOffset = 0;
981 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000982 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000983 MachineFrameInfo *FFI = MF.getFrameInfo();
984 int FPIndex = FI->getFramePointerSaveIndex();
985 assert(FPIndex && "No Frame Pointer Save Slot!");
986 FPOffset = FFI->getObjectOffset(FPIndex);
987 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000988 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000989 }
990 }
991
Hal Finkela7c54e82013-07-17 00:45:52 +0000992 int BPOffset = 0;
993 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000994 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000995 MachineFrameInfo *FFI = MF.getFrameInfo();
996 int BPIndex = FI->getBasePointerSaveIndex();
997 assert(BPIndex && "No Base Pointer Save Slot!");
998 BPOffset = FFI->getObjectOffset(BPIndex);
999 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001000 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001001 }
1002 }
1003
Justin Hibbits654346e2015-01-10 01:57:21 +00001004 int PBPOffset = 0;
1005 if (FI->usesPICBase()) {
1006 MachineFrameInfo *FFI = MF.getFrameInfo();
1007 int PBPIndex = FI->getPICBasePointerSaveIndex();
1008 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
1009 PBPOffset = FFI->getObjectOffset(PBPIndex);
1010 }
1011
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001012 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1013 RetOpcode == PPC::TCRETURNdi ||
1014 RetOpcode == PPC::TCRETURNai ||
1015 RetOpcode == PPC::TCRETURNri8 ||
1016 RetOpcode == PPC::TCRETURNdi8 ||
1017 RetOpcode == PPC::TCRETURNai8;
1018
1019 if (UsesTCRet) {
1020 int MaxTCRetDelta = FI->getTailCallSPDelta();
1021 MachineOperand &StackAdjust = MBBI->getOperand(1);
1022 assert(StackAdjust.isImm() && "Expecting immediate value.");
1023 // Adjust stack pointer.
1024 int StackAdj = StackAdjust.getImm();
1025 int Delta = StackAdj - MaxTCRetDelta;
1026 assert((Delta >= 0) && "Delta must be positive");
1027 if (MaxTCRetDelta>0)
1028 FrameSize += (StackAdj +Delta);
1029 else
1030 FrameSize += StackAdj;
1031 }
1032
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001033 // Frames of 32KB & larger require special handling because they cannot be
1034 // indexed into with a simple LD/LWZ immediate offset operand.
1035 bool isLargeFrame = !isInt<16>(FrameSize);
1036
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001037 if (FrameSize) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001038 // In the prologue, the loaded (or persistent) stack pointer value is offset
1039 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001040
1041 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1042 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1043 // call which invalidates the stack pointer value in SP(0). So we use the
1044 // value of R31 in this case.
1045 if (FI->hasFastCall()) {
1046 assert(HasFP && "Expecting a valid frame pointer.");
1047 if (!isLargeFrame) {
1048 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1049 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001050 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001051 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1052 .addImm(FrameSize >> 16);
1053 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1054 .addReg(ScratchReg, RegState::Kill)
1055 .addImm(FrameSize & 0xFFFF);
1056 BuildMI(MBB, MBBI, dl, AddInst)
1057 .addReg(SPReg)
1058 .addReg(FPReg)
1059 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001060 }
Bill Schmidtf381afc2013-08-20 03:12:23 +00001061 } else if (!isLargeFrame && !HasBP && !MFI->hasVarSizedObjects()) {
1062 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1063 .addReg(SPReg)
1064 .addImm(FrameSize);
1065 } else {
1066 BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
1067 .addImm(0)
1068 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001069 }
Bill Schmidtf381afc2013-08-20 03:12:23 +00001070
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001071 }
1072
Bill Schmidtf381afc2013-08-20 03:12:23 +00001073 if (MustSaveLR)
1074 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1075 .addImm(LROffset)
1076 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001077
Bill Schmidtf381afc2013-08-20 03:12:23 +00001078 assert((isPPC64 || MustSaveCRs.empty()) &&
1079 "Epilogue CR restoring supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +00001080
Bill Schmidtf381afc2013-08-20 03:12:23 +00001081 if (!MustSaveCRs.empty()) // will only occur for PPC64
1082 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1083 .addImm(8)
1084 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001085
Bill Schmidtf381afc2013-08-20 03:12:23 +00001086 if (HasFP)
1087 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1088 .addImm(FPOffset)
1089 .addReg(SPReg);
Hal Finkela7c54e82013-07-17 00:45:52 +00001090
Justin Hibbits654346e2015-01-10 01:57:21 +00001091 if (FI->usesPICBase())
Justin Hibbits98a532d2015-01-08 15:47:19 +00001092 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
1093 BuildMI(MBB, MBBI, dl, LoadInst)
1094 .addReg(PPC::R30)
Justin Hibbits654346e2015-01-10 01:57:21 +00001095 .addImm(PBPOffset)
Justin Hibbits98a532d2015-01-08 15:47:19 +00001096 .addReg(SPReg);
1097
Bill Schmidtf381afc2013-08-20 03:12:23 +00001098 if (HasBP)
1099 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1100 .addImm(BPOffset)
1101 .addReg(SPReg);
Hal Finkel67369882013-04-15 02:07:05 +00001102
Bill Schmidtf381afc2013-08-20 03:12:23 +00001103 if (!MustSaveCRs.empty()) // will only occur for PPC64
1104 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1105 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1106 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001107
Bill Schmidtf381afc2013-08-20 03:12:23 +00001108 if (MustSaveLR)
1109 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001110
1111 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1112 // call optimization
Hal Finkelf4a22c02015-01-13 17:47:54 +00001113 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1114 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001115 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1116 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1117 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001118
1119 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001120 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1121 .addReg(SPReg).addImm(CallerAllocatedAmt);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001122 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001123 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001124 .addImm(CallerAllocatedAmt >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001125 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1126 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001127 .addImm(CallerAllocatedAmt & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001128 BuildMI(MBB, MBBI, dl, AddInst)
1129 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001130 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001131 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001132 }
1133 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001134 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001135 MachineOperand &JumpTarget = MBBI->getOperand(0);
1136 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1137 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1138 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001139 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001140 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1141 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1142 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001143 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001144 MachineOperand &JumpTarget = MBBI->getOperand(0);
1145 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1146 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001147 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001148 MachineOperand &JumpTarget = MBBI->getOperand(0);
1149 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1150 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1151 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001152 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001153 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1154 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1155 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001156 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001157 MachineOperand &JumpTarget = MBBI->getOperand(0);
1158 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1159 }
1160}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001161
Matthias Braun02564862015-07-14 17:17:13 +00001162void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1163 BitVector &SavedRegs,
1164 RegScavenger *RS) const {
1165 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1166
Eric Christopherfc6de422014-08-05 02:39:49 +00001167 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +00001168 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001169
1170 // Save and clear the LR state.
1171 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1172 unsigned LR = RegInfo->getRARegister();
1173 FI->setMustSaveLR(MustSaveLR(MF, LR));
Matthias Braun02564862015-07-14 17:17:13 +00001174 SavedRegs.reset(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001175
1176 // Save R31 if necessary
1177 int FPSI = FI->getFramePointerSaveIndex();
1178 bool isPPC64 = Subtarget.isPPC64();
1179 bool isDarwinABI = Subtarget.isDarwinABI();
1180 MachineFrameInfo *MFI = MF.getFrameInfo();
1181
1182 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001183 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001184 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001185 int FPOffset = getFramePointerSaveOffset();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001186 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001187 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001188 // Save the result.
1189 FI->setFramePointerSaveIndex(FPSI);
1190 }
1191
Hal Finkela7c54e82013-07-17 00:45:52 +00001192 int BPSI = FI->getBasePointerSaveIndex();
1193 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001194 int BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001195 // Allocate the frame index for the base pointer save area.
1196 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1197 // Save the result.
1198 FI->setBasePointerSaveIndex(BPSI);
1199 }
1200
Justin Hibbits654346e2015-01-10 01:57:21 +00001201 // Reserve stack space for the PIC Base register (R30).
1202 // Only used in SVR4 32-bit.
1203 if (FI->usesPICBase()) {
Saleem Abdulrasool3e190cb2015-08-14 03:48:35 +00001204 int PBPSI = MFI->CreateFixedObject(4, -8, true);
Justin Hibbits654346e2015-01-10 01:57:21 +00001205 FI->setPICBasePointerSaveIndex(PBPSI);
1206 }
1207
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001208 // Reserve stack space to move the linkage area to in case of a tail call.
1209 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001210 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1211 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001212 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001213 }
1214
Eric Christopherd1737492014-04-29 00:16:40 +00001215 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001216 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001217 if (!isPPC64 && !isDarwinABI &&
Matthias Braun02564862015-07-14 17:17:13 +00001218 (SavedRegs.test(PPC::CR2) ||
1219 SavedRegs.test(PPC::CR3) ||
1220 SavedRegs.test(PPC::CR4))) {
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001221 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1222 FI->setCRSpillFrameIndex(FrameIdx);
1223 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001224}
1225
Hal Finkel5a765fd2013-03-14 20:33:40 +00001226void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001227 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001228 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001229 if (!Subtarget.isSVR4ABI()) {
1230 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001231 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001232 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001233
1234 // Get callee saved register information.
1235 MachineFrameInfo *FFI = MF.getFrameInfo();
1236 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1237
1238 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001239 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001240 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001241 return;
1242 }
1243
1244 unsigned MinGPR = PPC::R31;
1245 unsigned MinG8R = PPC::X31;
1246 unsigned MinFPR = PPC::F31;
1247 unsigned MinVR = PPC::V31;
1248
1249 bool HasGPSaveArea = false;
1250 bool HasG8SaveArea = false;
1251 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001252 bool HasVRSAVESaveArea = false;
1253 bool HasVRSaveArea = false;
1254
1255 SmallVector<CalleeSavedInfo, 18> GPRegs;
1256 SmallVector<CalleeSavedInfo, 18> G8Regs;
1257 SmallVector<CalleeSavedInfo, 18> FPRegs;
1258 SmallVector<CalleeSavedInfo, 18> VRegs;
1259
1260 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1261 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001262 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001263 HasGPSaveArea = true;
1264
1265 GPRegs.push_back(CSI[i]);
1266
1267 if (Reg < MinGPR) {
1268 MinGPR = Reg;
1269 }
Craig Topperabadc662012-04-20 06:31:50 +00001270 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001271 HasG8SaveArea = true;
1272
1273 G8Regs.push_back(CSI[i]);
1274
1275 if (Reg < MinG8R) {
1276 MinG8R = Reg;
1277 }
Craig Topperabadc662012-04-20 06:31:50 +00001278 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001279 HasFPSaveArea = true;
1280
1281 FPRegs.push_back(CSI[i]);
1282
1283 if (Reg < MinFPR) {
1284 MinFPR = Reg;
1285 }
Craig Topperabadc662012-04-20 06:31:50 +00001286 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1287 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001288 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001289 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001290 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001291 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001292 HasVRSaveArea = true;
1293
1294 VRegs.push_back(CSI[i]);
1295
1296 if (Reg < MinVR) {
1297 MinVR = Reg;
1298 }
1299 } else {
1300 llvm_unreachable("Unknown RegisterClass!");
1301 }
1302 }
1303
1304 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Eric Christopher38522b82015-01-30 02:11:26 +00001305 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001306
1307 int64_t LowerBound = 0;
1308
1309 // Take into account stack space reserved for tail calls.
1310 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001311 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1312 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001313 LowerBound = TCSPDelta;
1314 }
1315
1316 // The Floating-point register save area is right below the back chain word
1317 // of the previous stack frame.
1318 if (HasFPSaveArea) {
1319 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1320 int FI = FPRegs[i].getFrameIdx();
1321
1322 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1323 }
1324
Hal Finkelfeea6532013-03-26 20:08:20 +00001325 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001326 }
1327
1328 // Check whether the frame pointer register is allocated. If so, make sure it
1329 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001330 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001331 HasGPSaveArea = true;
1332
1333 int FI = PFI->getFramePointerSaveIndex();
1334 assert(FI && "No Frame Pointer Save Slot!");
1335
1336 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1337 }
1338
Justin Hibbits654346e2015-01-10 01:57:21 +00001339 if (PFI->usesPICBase()) {
1340 HasGPSaveArea = true;
1341
1342 int FI = PFI->getPICBasePointerSaveIndex();
1343 assert(FI && "No PIC Base Pointer Save Slot!");
1344
1345 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1346 }
1347
Eric Christopherfc6de422014-08-05 02:39:49 +00001348 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +00001349 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +00001350 if (RegInfo->hasBasePointer(MF)) {
1351 HasGPSaveArea = true;
1352
1353 int FI = PFI->getBasePointerSaveIndex();
1354 assert(FI && "No Base Pointer Save Slot!");
1355
1356 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1357 }
1358
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001359 // General register save area starts right below the Floating-point
1360 // register save area.
1361 if (HasGPSaveArea || HasG8SaveArea) {
1362 // Move general register save area spill slots down, taking into account
1363 // the size of the Floating-point register save area.
1364 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1365 int FI = GPRegs[i].getFrameIdx();
1366
1367 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1368 }
1369
1370 // Move general register save area spill slots down, taking into account
1371 // the size of the Floating-point register save area.
1372 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1373 int FI = G8Regs[i].getFrameIdx();
1374
1375 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1376 }
1377
1378 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001379 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1380 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001381
1382 if (Subtarget.isPPC64()) {
1383 LowerBound -= (31 - MinReg + 1) * 8;
1384 } else {
1385 LowerBound -= (31 - MinReg + 1) * 4;
1386 }
1387 }
1388
Roman Divackyc9e23d92012-09-12 14:47:47 +00001389 // For 32-bit only, the CR save area is below the general register
1390 // save area. For 64-bit SVR4, the CR save area is addressed relative
1391 // to the stack pointer and hence does not need an adjustment here.
1392 // Only CR2 (the first nonvolatile spilled) has an associated frame
1393 // index so that we have a single uniform save area.
1394 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001395 // Adjust the frame index of the CR spill slot.
1396 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1397 unsigned Reg = CSI[i].getReg();
1398
Roman Divackyc9e23d92012-09-12 14:47:47 +00001399 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001400 // Leave Darwin logic as-is.
1401 || (!Subtarget.isSVR4ABI() &&
1402 (PPC::CRBITRCRegClass.contains(Reg) ||
1403 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001404 int FI = CSI[i].getFrameIdx();
1405
1406 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1407 }
1408 }
1409
1410 LowerBound -= 4; // The CR save area is always 4 bytes long.
1411 }
1412
1413 if (HasVRSAVESaveArea) {
1414 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1415 // which have the VRSAVE register class?
1416 // Adjust the frame index of the VRSAVE spill slot.
1417 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1418 unsigned Reg = CSI[i].getReg();
1419
Craig Topperabadc662012-04-20 06:31:50 +00001420 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001421 int FI = CSI[i].getFrameIdx();
1422
1423 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1424 }
1425 }
1426
1427 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1428 }
1429
1430 if (HasVRSaveArea) {
1431 // Insert alignment padding, we need 16-byte alignment.
1432 LowerBound = (LowerBound - 15) & ~(15);
1433
1434 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1435 int FI = VRegs[i].getFrameIdx();
1436
1437 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1438 }
1439 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001440
1441 addScavengingSpillSlot(MF, RS);
1442}
1443
1444void
1445PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1446 RegScavenger *RS) const {
1447 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1448 // a large stack, which will require scavenging a register to materialize a
1449 // large offset.
1450
1451 // We need to have a scavenger spill slot for spills if the frame size is
1452 // large. In case there is no free register for large-offset addressing,
1453 // this slot is used for the necessary emergency spill. Also, we need the
1454 // slot for dynamic stack allocations.
1455
1456 // The scavenger might be invoked if the frame offset does not fit into
1457 // the 16-bit immediate. We don't know the complete frame size here
1458 // because we've not yet computed callee-saved register spills or the
1459 // needed alignment padding.
1460 unsigned StackSize = determineFrameLayout(MF, false, true);
1461 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001462 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1463 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001464 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1465 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1466 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkel9e331c22013-03-22 23:32:27 +00001467 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkelbb420f12013-03-15 05:06:04 +00001468 RC->getAlignment(),
1469 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001470
Hal Finkel18607632013-07-18 04:28:21 +00001471 // Might we have over-aligned allocas?
1472 bool HasAlVars = MFI->hasVarSizedObjects() &&
1473 MFI->getMaxAlignment() > getStackAlignment();
1474
Hal Finkel0dfbb052013-03-26 18:57:22 +00001475 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001476 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Hal Finkel0dfbb052013-03-26 18:57:22 +00001477 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1478 RC->getAlignment(),
1479 false));
1480
Hal Finkelbb420f12013-03-15 05:06:04 +00001481 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001482}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001483
Eric Christopherd1737492014-04-29 00:16:40 +00001484bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001485PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001486 MachineBasicBlock::iterator MI,
1487 const std::vector<CalleeSavedInfo> &CSI,
1488 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001489
1490 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1491 // Return false otherwise to maintain pre-existing behavior.
1492 if (!Subtarget.isSVR4ABI())
1493 return false;
1494
1495 MachineFunction *MF = MBB.getParent();
1496 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +00001497 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001498 DebugLoc DL;
1499 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001500 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001501
Roman Divackyc9e23d92012-09-12 14:47:47 +00001502 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1503 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001504 // Only Darwin actually uses the VRSAVE register, but it can still appear
1505 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1506 // Darwin, ignore it.
1507 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1508 continue;
1509
Roman Divackyc9e23d92012-09-12 14:47:47 +00001510 // CR2 through CR4 are the nonvolatile CR fields.
1511 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1512
Roman Divackyc9e23d92012-09-12 14:47:47 +00001513 // Add the callee-saved register as live-in; it's killed at the spill.
1514 MBB.addLiveIn(Reg);
1515
Hal Finkel2f293912013-04-13 23:06:15 +00001516 if (CRSpilled && IsCRField) {
1517 CRMIB.addReg(Reg, RegState::ImplicitKill);
1518 continue;
1519 }
1520
Roman Divackyc9e23d92012-09-12 14:47:47 +00001521 // Insert the spill to the stack frame.
1522 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001523 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001524 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001525 // The actual spill will happen at the start of the prologue.
1526 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001527 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001528 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001529 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001530
Eric Christopherd1737492014-04-29 00:16:40 +00001531 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1532 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1533 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001534 .addReg(Reg, RegState::ImplicitKill);
1535
Eric Christopherd1737492014-04-29 00:16:40 +00001536 MBB.insert(MI, CRMIB);
1537 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1538 .addReg(PPC::R12,
1539 getKillRegState(true)),
1540 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001541 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001542 } else {
1543 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1544 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001545 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001546 }
1547 }
1548 return true;
1549}
1550
1551static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001552restoreCRs(bool isPPC64, bool is31,
1553 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001554 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1555 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001556
1557 MachineFunction *MF = MBB.getParent();
Eric Christophercccae792015-01-30 22:02:31 +00001558 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001559 DebugLoc DL;
1560 unsigned RestoreOp, MoveReg;
1561
Hal Finkel67369882013-04-15 02:07:05 +00001562 if (isPPC64)
1563 // This is handled during epilogue generation.
1564 return;
1565 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001566 // 32-bit: FP-relative
1567 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00001568 PPC::R12),
1569 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00001570 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001571 MoveReg = PPC::R12;
1572 }
Eric Christopherd1737492014-04-29 00:16:40 +00001573
Roman Divackyc9e23d92012-09-12 14:47:47 +00001574 if (CR2Spilled)
1575 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00001576 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001577
1578 if (CR3Spilled)
1579 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00001580 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001581
1582 if (CR4Spilled)
1583 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00001584 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001585}
1586
Eli Bendersky8da87162013-02-21 20:05:00 +00001587void PPCFrameLowering::
1588eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1589 MachineBasicBlock::iterator I) const {
Eric Christopher38522b82015-01-30 02:11:26 +00001590 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
Eli Bendersky8da87162013-02-21 20:05:00 +00001591 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1592 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1593 // Add (actually subtract) back the amount the callee popped on return.
1594 if (int CalleeAmt = I->getOperand(1).getImm()) {
1595 bool is64Bit = Subtarget.isPPC64();
1596 CalleeAmt *= -1;
1597 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1598 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1599 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1600 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1601 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1602 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1603 MachineInstr *MI = I;
1604 DebugLoc dl = MI->getDebugLoc();
1605
1606 if (isInt<16>(CalleeAmt)) {
1607 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1608 .addReg(StackReg, RegState::Kill)
1609 .addImm(CalleeAmt);
1610 } else {
1611 MachineBasicBlock::iterator MBBI = I;
1612 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1613 .addImm(CalleeAmt >> 16);
1614 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1615 .addReg(TmpReg, RegState::Kill)
1616 .addImm(CalleeAmt & 0xFFFF);
1617 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1618 .addReg(StackReg, RegState::Kill)
1619 .addReg(TmpReg);
1620 }
1621 }
1622 }
1623 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1624 MBB.erase(I);
1625}
1626
Eric Christopherd1737492014-04-29 00:16:40 +00001627bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001628PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001629 MachineBasicBlock::iterator MI,
1630 const std::vector<CalleeSavedInfo> &CSI,
1631 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001632
1633 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1634 // Return false otherwise to maintain pre-existing behavior.
1635 if (!Subtarget.isSVR4ABI())
1636 return false;
1637
1638 MachineFunction *MF = MBB.getParent();
1639 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +00001640 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001641 bool CR2Spilled = false;
1642 bool CR3Spilled = false;
1643 bool CR4Spilled = false;
1644 unsigned CSIIndex = 0;
1645
1646 // Initialize insertion-point logic; we will be restoring in reverse
1647 // order of spill.
1648 MachineBasicBlock::iterator I = MI, BeforeI = I;
1649 bool AtStart = I == MBB.begin();
1650
1651 if (!AtStart)
1652 --BeforeI;
1653
1654 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1655 unsigned Reg = CSI[i].getReg();
1656
Hal Finkelac1a24b2013-06-28 22:29:56 +00001657 // Only Darwin actually uses the VRSAVE register, but it can still appear
1658 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1659 // Darwin, ignore it.
1660 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1661 continue;
1662
Roman Divackyc9e23d92012-09-12 14:47:47 +00001663 if (Reg == PPC::CR2) {
1664 CR2Spilled = true;
1665 // The spill slot is associated only with CR2, which is the
1666 // first nonvolatile spilled. Save it here.
1667 CSIIndex = i;
1668 continue;
1669 } else if (Reg == PPC::CR3) {
1670 CR3Spilled = true;
1671 continue;
1672 } else if (Reg == PPC::CR4) {
1673 CR4Spilled = true;
1674 continue;
1675 } else {
1676 // When we first encounter a non-CR register after seeing at
1677 // least one CR register, restore all spilled CRs together.
1678 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00001679 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00001680 bool is31 = needsFP(*MF);
1681 restoreCRs(Subtarget.isPPC64(), is31,
1682 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001683 MBB, I, CSI, CSIIndex);
1684 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001685 }
1686
1687 // Default behavior for non-CR saves.
1688 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1689 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00001690 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001691 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00001692 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00001693 }
1694
1695 // Insert in reverse order.
1696 if (AtStart)
1697 I = MBB.begin();
1698 else {
1699 I = BeforeI;
1700 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00001701 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001702 }
1703
1704 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00001705 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00001706 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001707 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001708 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001709 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001710
1711 return true;
1712}