blob: ef4ea77bfca50b5a974c8de2c04c7cf8d691a557 [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"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000024#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000026#include "llvm/Target/TargetOptions.h"
27
28using namespace llvm;
29
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000030/// VRRegNo - Map from a numbered VR register to its enum value.
31///
Craig Topperca658c22012-03-11 07:16:55 +000032static const uint16_t VRRegNo[] = {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000033 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
34 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
35 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
36 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
37};
38
Eric Christopherd104c312014-06-12 20:54:11 +000039PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
40 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
41 (STI.hasQPX() || STI.isBGQ()) ? 32 : 16, 0),
42 Subtarget(STI) {}
43
44unsigned PPCFrameLowering::getMinCallArgumentsSize(bool isPPC64,
45 bool isDarwinABI) {
46 // For the Darwin ABI / 64-bit SVR4 ABI:
47 // The prolog code of the callee may store up to 8 GPR argument registers to
48 // the stack, allowing va_start to index over them in memory if its varargs.
49 // Because we cannot tell if this is needed on the caller side, we have to
50 // conservatively assume that it is needed. As such, make sure we have at
51 // least enough stack space for the caller to store the 8 GPRs.
52 if (isDarwinABI || isPPC64)
53 return 8 * (isPPC64 ? 8 : 4);
54
55 // 32-bit SVR4 ABI:
56 // There is no default stack allocated for the 8 first GPR arguments.
57 return 0;
58}
59
60/// getMinCallFrameSize - Return the minimum size a call frame can be using
61/// the PowerPC ABI.
62unsigned PPCFrameLowering::getMinCallFrameSize(bool isPPC64, bool isDarwinABI) {
63 // The call frame needs to be at least big enough for linkage and 8 args.
64 return PPCFrameLowering::getLinkageSize(isPPC64, isDarwinABI) +
65 PPCFrameLowering::getMinCallArgumentsSize(isPPC64, isDarwinABI);
66}
67
68// With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
69const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
70 unsigned &NumEntries) const {
71 if (Subtarget.isDarwinABI()) {
72 NumEntries = 1;
73 if (Subtarget.isPPC64()) {
74 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
75 return &darwin64Offsets;
76 } else {
77 static const SpillSlot darwinOffsets = {PPC::R31, -4};
78 return &darwinOffsets;
79 }
80 }
81
82 // Early exit if not using the SVR4 ABI.
83 if (!Subtarget.isSVR4ABI()) {
84 NumEntries = 0;
85 return nullptr;
86 }
87
88 // Note that the offsets here overlap, but this is fixed up in
89 // processFunctionBeforeFrameFinalized.
90
91 static const SpillSlot Offsets[] = {
92 // Floating-point register save area offsets.
93 {PPC::F31, -8},
94 {PPC::F30, -16},
95 {PPC::F29, -24},
96 {PPC::F28, -32},
97 {PPC::F27, -40},
98 {PPC::F26, -48},
99 {PPC::F25, -56},
100 {PPC::F24, -64},
101 {PPC::F23, -72},
102 {PPC::F22, -80},
103 {PPC::F21, -88},
104 {PPC::F20, -96},
105 {PPC::F19, -104},
106 {PPC::F18, -112},
107 {PPC::F17, -120},
108 {PPC::F16, -128},
109 {PPC::F15, -136},
110 {PPC::F14, -144},
111
112 // General register save area offsets.
113 {PPC::R31, -4},
114 {PPC::R30, -8},
115 {PPC::R29, -12},
116 {PPC::R28, -16},
117 {PPC::R27, -20},
118 {PPC::R26, -24},
119 {PPC::R25, -28},
120 {PPC::R24, -32},
121 {PPC::R23, -36},
122 {PPC::R22, -40},
123 {PPC::R21, -44},
124 {PPC::R20, -48},
125 {PPC::R19, -52},
126 {PPC::R18, -56},
127 {PPC::R17, -60},
128 {PPC::R16, -64},
129 {PPC::R15, -68},
130 {PPC::R14, -72},
131
132 // CR save area offset. We map each of the nonvolatile CR fields
133 // to the slot for CR2, which is the first of the nonvolatile CR
134 // fields to be assigned, so that we only allocate one save slot.
135 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
136 {PPC::CR2, -4},
137
138 // VRSAVE save area offset.
139 {PPC::VRSAVE, -4},
140
141 // Vector register save area
142 {PPC::V31, -16},
143 {PPC::V30, -32},
144 {PPC::V29, -48},
145 {PPC::V28, -64},
146 {PPC::V27, -80},
147 {PPC::V26, -96},
148 {PPC::V25, -112},
149 {PPC::V24, -128},
150 {PPC::V23, -144},
151 {PPC::V22, -160},
152 {PPC::V21, -176},
153 {PPC::V20, -192}};
154
155 static const SpillSlot Offsets64[] = {
156 // Floating-point register save area offsets.
157 {PPC::F31, -8},
158 {PPC::F30, -16},
159 {PPC::F29, -24},
160 {PPC::F28, -32},
161 {PPC::F27, -40},
162 {PPC::F26, -48},
163 {PPC::F25, -56},
164 {PPC::F24, -64},
165 {PPC::F23, -72},
166 {PPC::F22, -80},
167 {PPC::F21, -88},
168 {PPC::F20, -96},
169 {PPC::F19, -104},
170 {PPC::F18, -112},
171 {PPC::F17, -120},
172 {PPC::F16, -128},
173 {PPC::F15, -136},
174 {PPC::F14, -144},
175
176 // General register save area offsets.
177 {PPC::X31, -8},
178 {PPC::X30, -16},
179 {PPC::X29, -24},
180 {PPC::X28, -32},
181 {PPC::X27, -40},
182 {PPC::X26, -48},
183 {PPC::X25, -56},
184 {PPC::X24, -64},
185 {PPC::X23, -72},
186 {PPC::X22, -80},
187 {PPC::X21, -88},
188 {PPC::X20, -96},
189 {PPC::X19, -104},
190 {PPC::X18, -112},
191 {PPC::X17, -120},
192 {PPC::X16, -128},
193 {PPC::X15, -136},
194 {PPC::X14, -144},
195
196 // VRSAVE save area offset.
197 {PPC::VRSAVE, -4},
198
199 // Vector register save area
200 {PPC::V31, -16},
201 {PPC::V30, -32},
202 {PPC::V29, -48},
203 {PPC::V28, -64},
204 {PPC::V27, -80},
205 {PPC::V26, -96},
206 {PPC::V25, -112},
207 {PPC::V24, -128},
208 {PPC::V23, -144},
209 {PPC::V22, -160},
210 {PPC::V21, -176},
211 {PPC::V20, -192}};
212
213 if (Subtarget.isPPC64()) {
214 NumEntries = array_lengthof(Offsets64);
215
216 return Offsets64;
217 } else {
218 NumEntries = array_lengthof(Offsets);
219
220 return Offsets;
221 }
222}
223
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000224/// RemoveVRSaveCode - We have found that this function does not need any code
225/// to manipulate the VRSAVE register, even though it uses vector registers.
226/// This can happen when the only registers used are known to be live in or out
227/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidt38d94582012-10-10 20:54:15 +0000228/// FIXME: The removal of the code results in a compile failure at -O0 when the
229/// function contains a function call, as the GPR containing original VRSAVE
230/// contents is spilled and reloaded around the call. Without the prolog code,
231/// the spill instruction refers to an undefined register. This code needs
232/// to account for all uses of that GPR.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000233static void RemoveVRSaveCode(MachineInstr *MI) {
234 MachineBasicBlock *Entry = MI->getParent();
235 MachineFunction *MF = Entry->getParent();
236
237 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
238 MachineBasicBlock::iterator MBBI = MI;
239 ++MBBI;
240 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
241 MBBI->eraseFromParent();
242
243 bool RemovedAllMTVRSAVEs = true;
244 // See if we can find and remove the MTVRSAVE instruction from all of the
245 // epilog blocks.
246 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
247 // If last instruction is a return instruction, add an epilogue
Evan Cheng7f8e5632011-12-07 07:15:52 +0000248 if (!I->empty() && I->back().isReturn()) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000249 bool FoundIt = false;
250 for (MBBI = I->end(); MBBI != I->begin(); ) {
251 --MBBI;
252 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
253 MBBI->eraseFromParent(); // remove it.
254 FoundIt = true;
255 break;
256 }
257 }
258 RemovedAllMTVRSAVEs &= FoundIt;
259 }
260 }
261
262 // If we found and removed all MTVRSAVE instructions, remove the read of
263 // VRSAVE as well.
264 if (RemovedAllMTVRSAVEs) {
265 MBBI = MI;
266 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
267 --MBBI;
268 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
269 MBBI->eraseFromParent();
270 }
271
272 // Finally, nuke the UPDATE_VRSAVE.
273 MI->eraseFromParent();
274}
275
276// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
277// instruction selector. Based on the vector registers that have been used,
278// transform this into the appropriate ORI instruction.
279static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
280 MachineFunction *MF = MI->getParent()->getParent();
Hal Finkelfeea6532013-03-26 20:08:20 +0000281 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000282 DebugLoc dl = MI->getDebugLoc();
283
284 unsigned UsedRegMask = 0;
285 for (unsigned i = 0; i != 32; ++i)
286 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
287 UsedRegMask |= 1 << (31-i);
288
289 // Live in and live out values already must be in the mask, so don't bother
290 // marking them.
291 for (MachineRegisterInfo::livein_iterator
292 I = MF->getRegInfo().livein_begin(),
293 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelfeea6532013-03-26 20:08:20 +0000294 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000295 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
296 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
297 }
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000298
299 // Live out registers appear as use operands on return instructions.
300 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
301 UsedRegMask != 0 && BI != BE; ++BI) {
302 const MachineBasicBlock &MBB = *BI;
303 if (MBB.empty() || !MBB.back().isReturn())
304 continue;
305 const MachineInstr &Ret = MBB.back();
306 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
307 const MachineOperand &MO = Ret.getOperand(I);
308 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
309 continue;
Hal Finkelfeea6532013-03-26 20:08:20 +0000310 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000311 UsedRegMask &= ~(1 << (31-RegNo));
312 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000313 }
314
315 // If no registers are used, turn this into a copy.
316 if (UsedRegMask == 0) {
317 // Remove all VRSAVE code.
318 RemoveVRSaveCode(MI);
319 return;
320 }
321
322 unsigned SrcReg = MI->getOperand(1).getReg();
323 unsigned DstReg = MI->getOperand(0).getReg();
324
325 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
326 if (DstReg != SrcReg)
327 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
328 .addReg(SrcReg)
329 .addImm(UsedRegMask);
330 else
331 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
332 .addReg(SrcReg, RegState::Kill)
333 .addImm(UsedRegMask);
334 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
335 if (DstReg != SrcReg)
336 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
337 .addReg(SrcReg)
338 .addImm(UsedRegMask >> 16);
339 else
340 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
341 .addReg(SrcReg, RegState::Kill)
342 .addImm(UsedRegMask >> 16);
343 } else {
344 if (DstReg != SrcReg)
345 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
346 .addReg(SrcReg)
347 .addImm(UsedRegMask >> 16);
348 else
349 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
350 .addReg(SrcReg, RegState::Kill)
351 .addImm(UsedRegMask >> 16);
352
353 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
354 .addReg(DstReg, RegState::Kill)
355 .addImm(UsedRegMask & 0xFFFF);
356 }
357
358 // Remove the old UPDATE_VRSAVE instruction.
359 MI->eraseFromParent();
360}
361
Roman Divackyc9e23d92012-09-12 14:47:47 +0000362static bool spillsCR(const MachineFunction &MF) {
363 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
364 return FuncInfo->isCRSpilled();
365}
366
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000367static bool spillsVRSAVE(const MachineFunction &MF) {
368 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
369 return FuncInfo->isVRSAVESpilled();
370}
371
Hal Finkelbb420f12013-03-15 05:06:04 +0000372static bool hasSpills(const MachineFunction &MF) {
373 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
374 return FuncInfo->hasSpills();
375}
376
Hal Finkelfcc51d42013-03-17 04:43:44 +0000377static bool hasNonRISpills(const MachineFunction &MF) {
378 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
379 return FuncInfo->hasNonRISpills();
380}
381
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000382/// determineFrameLayout - Determine the size of the frame and maximum call
383/// frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000384unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
385 bool UpdateMF,
386 bool UseEstimate) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000387 MachineFrameInfo *MFI = MF.getFrameInfo();
388
389 // Get the number of bytes to allocate from the FrameInfo
Hal Finkelbb420f12013-03-15 05:06:04 +0000390 unsigned FrameSize =
391 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000392
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000393 // Get stack alignments. The frame must be aligned to the greatest of these:
394 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
395 unsigned MaxAlign = MFI->getMaxAlignment(); // algmt required by data in frame
Hal Finkela7c54e82013-07-17 00:45:52 +0000396 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
397
398 const PPCRegisterInfo *RegInfo =
399 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000400
401 // If we are a leaf function, and use up to 224 bytes of stack space,
402 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkel67369882013-04-15 02:07:05 +0000403 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000404 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
405 // stackless code if all local vars are reg-allocated.
Bill Wendling698e84f2012-12-30 10:32:01 +0000406 bool DisableRedZone = MF.getFunction()->getAttributes().
407 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000408 if (!DisableRedZone &&
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000409 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
410 !Subtarget.isSVR4ABI() || // allocated locals.
Eric Christopherd1737492014-04-29 00:16:40 +0000411 FrameSize == 0) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000412 FrameSize <= 224 && // Fits in red zone.
413 !MFI->hasVarSizedObjects() && // No dynamic alloca.
414 !MFI->adjustsStack() && // No calls.
Hal Finkela7c54e82013-07-17 00:45:52 +0000415 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000416 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000417 if (UpdateMF)
418 MFI->setStackSize(0);
419 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000420 }
421
422 // Get the maximum call frame size of all the calls.
423 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
424
Ulrich Weigandf316e1d2014-06-23 13:47:52 +0000425 // Maximum call frame needs to be at least big enough for linkage area.
426 unsigned minCallFrameSize = getLinkageSize(Subtarget.isPPC64(),
427 Subtarget.isDarwinABI());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000428 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
429
430 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
431 // that allocations will be aligned.
432 if (MFI->hasVarSizedObjects())
433 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
434
435 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000436 if (UpdateMF)
437 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000438
439 // Include call frame size in total.
440 FrameSize += maxCallFrameSize;
441
442 // Make sure the frame is aligned.
443 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
444
445 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000446 if (UpdateMF)
447 MFI->setStackSize(FrameSize);
448
449 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000450}
451
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000452// hasFP - Return true if the specified function actually has a dedicated frame
453// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000454bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000455 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000456 // FIXME: This is pretty much broken by design: hasFP() might be called really
457 // early, before the stack layout was calculated and thus hasFP() might return
458 // true or false here depending on the time of call.
459 return (MFI->getStackSize()) && needsFP(MF);
460}
461
462// needsFP - Return true if the specified function should have a dedicated frame
463// pointer register. This is true if the function has variable sized allocas or
464// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000465bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000466 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000467
468 // Naked functions have no stack frame pushed, so we don't have a frame
469 // pointer.
Eric Christopherd1737492014-04-29 00:16:40 +0000470 if (MF.getFunction()->getAttributes().hasAttribute(
471 AttributeSet::FunctionIndex, Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000472 return false;
473
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000474 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
475 MFI->hasVarSizedObjects() ||
476 (MF.getTarget().Options.GuaranteedTailCallOpt &&
477 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000478}
479
Hal Finkelaa03c032013-03-21 19:03:19 +0000480void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
481 bool is31 = needsFP(MF);
482 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
483 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
484
Hal Finkelf05d6c72013-07-17 23:50:51 +0000485 const PPCRegisterInfo *RegInfo =
486 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
487 bool HasBP = RegInfo->hasBasePointer(MF);
488 unsigned BPReg = HasBP ? (unsigned) PPC::R30 : FPReg;
489 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
490
Hal Finkelaa03c032013-03-21 19:03:19 +0000491 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
492 BI != BE; ++BI)
493 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
494 --MBBI;
495 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
496 MachineOperand &MO = MBBI->getOperand(I);
497 if (!MO.isReg())
498 continue;
499
500 switch (MO.getReg()) {
501 case PPC::FP:
502 MO.setReg(FPReg);
503 break;
504 case PPC::FP8:
505 MO.setReg(FP8Reg);
506 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000507 case PPC::BP:
508 MO.setReg(BPReg);
509 break;
510 case PPC::BP8:
511 MO.setReg(BP8Reg);
512 break;
513
Hal Finkelaa03c032013-03-21 19:03:19 +0000514 }
515 }
516 }
517}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000518
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000519void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000520 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
521 MachineBasicBlock::iterator MBBI = MBB.begin();
522 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000523 const PPCInstrInfo &TII =
524 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +0000525 const PPCRegisterInfo *RegInfo =
526 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000527
528 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000529 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000530 DebugLoc dl;
531 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000532 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000533
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000534 // Get processor type.
535 bool isPPC64 = Subtarget.isPPC64();
536 // Get the ABI.
537 bool isDarwinABI = Subtarget.isDarwinABI();
538 bool isSVR4ABI = Subtarget.isSVR4ABI();
539 assert((isDarwinABI || isSVR4ABI) &&
540 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
541
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000542 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
543 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000544 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000545 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
546 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
547 HandleVRSaveUpdate(MBBI, TII);
548 break;
549 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000550 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000551
552 // Move MBBI back to the beginning of the function.
553 MBBI = MBB.begin();
554
555 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000556 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000557 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000558 if (!isInt<32>(NegFrameSize))
559 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000560
Hal Finkelaa03c032013-03-21 19:03:19 +0000561 if (MFI->isFrameAddressTaken())
562 replaceFPWithRealFP(MF);
563
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000564 // Check if the link register (LR) must be saved.
565 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
566 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000567 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000568 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000569 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000570 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000571
Bill Schmidtf381afc2013-08-20 03:12:23 +0000572 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
573 unsigned BPReg = isPPC64 ? PPC::X30 : PPC::R30;
574 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
575 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
576 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
577 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
578 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
579 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
580 : PPC::MFLR );
581 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
582 : PPC::STW );
583 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
584 : PPC::STWU );
585 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
586 : PPC::STWUX);
587 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
588 : PPC::LIS );
589 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
590 : PPC::ORI );
591 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
592 : PPC::OR );
593 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
594 : PPC::SUBFC);
595 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
596 : PPC::SUBFIC);
597
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000598 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
599 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
600 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
601 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
602 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
603 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
604
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000605 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000606
607 int FPOffset = 0;
608 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000609 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000610 MachineFrameInfo *FFI = MF.getFrameInfo();
611 int FPIndex = FI->getFramePointerSaveIndex();
612 assert(FPIndex && "No Frame Pointer Save Slot!");
613 FPOffset = FFI->getObjectOffset(FPIndex);
614 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000615 FPOffset =
616 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000617 }
618 }
619
Hal Finkela7c54e82013-07-17 00:45:52 +0000620 int BPOffset = 0;
621 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000622 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000623 MachineFrameInfo *FFI = MF.getFrameInfo();
624 int BPIndex = FI->getBasePointerSaveIndex();
625 assert(BPIndex && "No Base Pointer Save Slot!");
626 BPOffset = FFI->getObjectOffset(BPIndex);
627 } else {
628 BPOffset =
Hal Finkelf05d6c72013-07-17 23:50:51 +0000629 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkela7c54e82013-07-17 00:45:52 +0000630 }
631 }
632
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000633 // Get stack alignments.
634 unsigned MaxAlign = MFI->getMaxAlignment();
635 if (HasBP && MaxAlign > 1)
636 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
637 "Invalid alignment!");
638
639 // Frames of 32KB & larger require special handling because they cannot be
640 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
641 bool isLargeFrame = !isInt<16>(NegFrameSize);
642
Bill Schmidtf381afc2013-08-20 03:12:23 +0000643 if (MustSaveLR)
644 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000645
Bill Schmidtf381afc2013-08-20 03:12:23 +0000646 assert((isPPC64 || MustSaveCRs.empty()) &&
647 "Prologue CR saving supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000648
Bill Schmidtf381afc2013-08-20 03:12:23 +0000649 if (!MustSaveCRs.empty()) { // will only occur for PPC64
650 MachineInstrBuilder MIB =
651 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), TempReg);
652 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
653 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000654 }
655
Bill Schmidtf381afc2013-08-20 03:12:23 +0000656 if (HasFP)
657 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
658 BuildMI(MBB, MBBI, dl, StoreInst)
659 .addReg(FPReg)
660 .addImm(FPOffset)
661 .addReg(SPReg);
662
663 if (HasBP)
664 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
665 BuildMI(MBB, MBBI, dl, StoreInst)
666 .addReg(BPReg)
667 .addImm(BPOffset)
668 .addReg(SPReg);
669
670 if (MustSaveLR)
671 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
672 BuildMI(MBB, MBBI, dl, StoreInst)
673 .addReg(ScratchReg)
674 .addImm(LROffset)
675 .addReg(SPReg);
676
677 if (!MustSaveCRs.empty()) // will only occur for PPC64
678 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
679 .addReg(TempReg, getKillRegState(true))
680 .addImm(8)
681 .addReg(SPReg);
682
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000683 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000684 if (!FrameSize) return;
685
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000686 // Adjust stack pointer: r1 += NegFrameSize.
687 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000688
Bill Schmidtf381afc2013-08-20 03:12:23 +0000689 if (HasBP) {
690 // Save a copy of r1 as the base pointer.
691 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
692 .addReg(SPReg)
693 .addReg(SPReg);
694 }
695
696 if (HasBP && MaxAlign > 1) {
697 if (isPPC64)
698 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
699 .addReg(SPReg)
700 .addImm(0)
701 .addImm(64 - Log2_32(MaxAlign));
702 else // PPC32...
703 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
704 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000705 .addImm(0)
706 .addImm(32 - Log2_32(MaxAlign))
707 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000708 if (!isLargeFrame) {
709 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
710 .addReg(ScratchReg, RegState::Kill)
711 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000712 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000713 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000714 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000715 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
716 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000717 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000718 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
719 .addReg(ScratchReg, RegState::Kill)
720 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000721 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000722 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
723 .addReg(SPReg, RegState::Kill)
724 .addReg(SPReg)
725 .addReg(ScratchReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000726
Bill Schmidtf381afc2013-08-20 03:12:23 +0000727 } else if (!isLargeFrame) {
728 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
729 .addReg(SPReg)
730 .addImm(NegFrameSize)
731 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000732
Bill Schmidtf381afc2013-08-20 03:12:23 +0000733 } else {
734 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
735 .addImm(NegFrameSize >> 16);
736 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
737 .addReg(ScratchReg, RegState::Kill)
738 .addImm(NegFrameSize & 0xFFFF);
739 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
740 .addReg(SPReg, RegState::Kill)
741 .addReg(SPReg)
742 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000743 }
744
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000745 // Add the "machine moves" for the instructions we generated above, but in
746 // reverse order.
747 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000748 // Show update of SP.
Rafael Espindola6e8c0d92013-05-16 03:34:58 +0000749 assert(NegFrameSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000750 unsigned CFIIndex = MMI.addFrameInst(
751 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
Eric Christopher612bb692014-04-29 00:16:46 +0000752 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
753 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000754
755 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000756 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000757 CFIIndex = MMI.addFrameInst(
758 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000759 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000760 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000761 }
762
Hal Finkela7c54e82013-07-17 00:45:52 +0000763 if (HasBP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000764 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000765 CFIIndex = MMI.addFrameInst(
766 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000767 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000768 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000769 }
770
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000771 if (MustSaveLR) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000772 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000773 CFIIndex = MMI.addFrameInst(
774 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000775 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000776 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000777 }
778 }
779
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000780 // If there is a frame pointer, copy R1 into R31
781 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000782 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
783 .addReg(SPReg)
784 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000785
786 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000787 // Mark effective beginning of when frame pointer is ready.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000788 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000789 unsigned CFIIndex = MMI.addFrameInst(
790 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
791
Eric Christopher612bb692014-04-29 00:16:46 +0000792 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000793 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000794 }
795 }
796
797 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000798 // Add callee saved registers to move list.
799 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
800 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000801 unsigned Reg = CSI[I].getReg();
802 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000803
804 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
805 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +0000806 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +0000807 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000808
Roman Divackyc9e23d92012-09-12 14:47:47 +0000809 // For SVR4, don't emit a move for the CR spill slot if we haven't
810 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000811 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
812 && MustSaveCRs.empty())
813 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000814
815 // For 64-bit SVR4 when we have spilled CRs, the spill location
816 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000817 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000818 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
819 nullptr, MRI->getDwarfRegNum(PPC::CR2, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +0000820 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000821 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000822 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000823 }
824
825 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000826 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
827 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +0000828 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000829 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000830 }
831 }
832}
833
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000834void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000835 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000836 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
837 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000838 const PPCInstrInfo &TII =
839 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +0000840 const PPCRegisterInfo *RegInfo =
841 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000842
843 unsigned RetOpcode = MBBI->getOpcode();
844 DebugLoc dl;
845
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000846 assert((RetOpcode == PPC::BLR ||
847 RetOpcode == PPC::TCRETURNri ||
848 RetOpcode == PPC::TCRETURNdi ||
849 RetOpcode == PPC::TCRETURNai ||
850 RetOpcode == PPC::TCRETURNri8 ||
851 RetOpcode == PPC::TCRETURNdi8 ||
852 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000853 "Can only insert epilog into returning blocks");
854
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000855 // Get alignment info so we know how to restore the SP.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000856 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000857
858 // Get the number of bytes allocated from the FrameInfo.
859 int FrameSize = MFI->getStackSize();
860
861 // Get processor type.
862 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000863 // Get the ABI.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000864 bool isDarwinABI = Subtarget.isDarwinABI();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000865 bool isSVR4ABI = Subtarget.isSVR4ABI();
866
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000867 // Check if the link register (LR) has been saved.
868 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
869 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000870 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000871 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000872 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000873 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000874
Bill Schmidtf381afc2013-08-20 03:12:23 +0000875 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
876 unsigned BPReg = isPPC64 ? PPC::X30 : PPC::R30;
877 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
878 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
879 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
880 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
881 : PPC::MTLR );
882 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
883 : PPC::LWZ );
884 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
885 : PPC::LIS );
886 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
887 : PPC::ORI );
888 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
889 : PPC::ADDI );
890 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
891 : PPC::ADD4 );
892
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000893 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000894
895 int FPOffset = 0;
896 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000897 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000898 MachineFrameInfo *FFI = MF.getFrameInfo();
899 int FPIndex = FI->getFramePointerSaveIndex();
900 assert(FPIndex && "No Frame Pointer Save Slot!");
901 FPOffset = FFI->getObjectOffset(FPIndex);
902 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000903 FPOffset =
904 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000905 }
906 }
907
Hal Finkela7c54e82013-07-17 00:45:52 +0000908 int BPOffset = 0;
909 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000910 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000911 MachineFrameInfo *FFI = MF.getFrameInfo();
912 int BPIndex = FI->getBasePointerSaveIndex();
913 assert(BPIndex && "No Base Pointer Save Slot!");
914 BPOffset = FFI->getObjectOffset(BPIndex);
915 } else {
916 BPOffset =
Hal Finkelf05d6c72013-07-17 23:50:51 +0000917 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkela7c54e82013-07-17 00:45:52 +0000918 }
919 }
920
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000921 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
922 RetOpcode == PPC::TCRETURNdi ||
923 RetOpcode == PPC::TCRETURNai ||
924 RetOpcode == PPC::TCRETURNri8 ||
925 RetOpcode == PPC::TCRETURNdi8 ||
926 RetOpcode == PPC::TCRETURNai8;
927
928 if (UsesTCRet) {
929 int MaxTCRetDelta = FI->getTailCallSPDelta();
930 MachineOperand &StackAdjust = MBBI->getOperand(1);
931 assert(StackAdjust.isImm() && "Expecting immediate value.");
932 // Adjust stack pointer.
933 int StackAdj = StackAdjust.getImm();
934 int Delta = StackAdj - MaxTCRetDelta;
935 assert((Delta >= 0) && "Delta must be positive");
936 if (MaxTCRetDelta>0)
937 FrameSize += (StackAdj +Delta);
938 else
939 FrameSize += StackAdj;
940 }
941
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000942 // Frames of 32KB & larger require special handling because they cannot be
943 // indexed into with a simple LD/LWZ immediate offset operand.
944 bool isLargeFrame = !isInt<16>(FrameSize);
945
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000946 if (FrameSize) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000947 // In the prologue, the loaded (or persistent) stack pointer value is offset
948 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000949
950 // If this function contained a fastcc call and GuaranteedTailCallOpt is
951 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
952 // call which invalidates the stack pointer value in SP(0). So we use the
953 // value of R31 in this case.
954 if (FI->hasFastCall()) {
955 assert(HasFP && "Expecting a valid frame pointer.");
956 if (!isLargeFrame) {
957 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
958 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000959 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000960 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
961 .addImm(FrameSize >> 16);
962 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
963 .addReg(ScratchReg, RegState::Kill)
964 .addImm(FrameSize & 0xFFFF);
965 BuildMI(MBB, MBBI, dl, AddInst)
966 .addReg(SPReg)
967 .addReg(FPReg)
968 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000969 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000970 } else if (!isLargeFrame && !HasBP && !MFI->hasVarSizedObjects()) {
971 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
972 .addReg(SPReg)
973 .addImm(FrameSize);
974 } else {
975 BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
976 .addImm(0)
977 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000978 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000979
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000980 }
981
Bill Schmidtf381afc2013-08-20 03:12:23 +0000982 if (MustSaveLR)
983 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
984 .addImm(LROffset)
985 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000986
Bill Schmidtf381afc2013-08-20 03:12:23 +0000987 assert((isPPC64 || MustSaveCRs.empty()) &&
988 "Epilogue CR restoring supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000989
Bill Schmidtf381afc2013-08-20 03:12:23 +0000990 if (!MustSaveCRs.empty()) // will only occur for PPC64
991 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
992 .addImm(8)
993 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000994
Bill Schmidtf381afc2013-08-20 03:12:23 +0000995 if (HasFP)
996 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
997 .addImm(FPOffset)
998 .addReg(SPReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000999
Bill Schmidtf381afc2013-08-20 03:12:23 +00001000 if (HasBP)
1001 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1002 .addImm(BPOffset)
1003 .addReg(SPReg);
Hal Finkel67369882013-04-15 02:07:05 +00001004
Bill Schmidtf381afc2013-08-20 03:12:23 +00001005 if (!MustSaveCRs.empty()) // will only occur for PPC64
1006 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1007 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1008 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001009
Bill Schmidtf381afc2013-08-20 03:12:23 +00001010 if (MustSaveLR)
1011 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001012
1013 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1014 // call optimization
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001015 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001016 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1017 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1018 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001019
1020 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001021 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1022 .addReg(SPReg).addImm(CallerAllocatedAmt);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001023 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001024 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001025 .addImm(CallerAllocatedAmt >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001026 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1027 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001028 .addImm(CallerAllocatedAmt & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001029 BuildMI(MBB, MBBI, dl, AddInst)
1030 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001031 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001032 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001033 }
1034 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001035 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001036 MachineOperand &JumpTarget = MBBI->getOperand(0);
1037 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1038 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1039 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001040 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001041 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1042 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1043 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001044 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001045 MachineOperand &JumpTarget = MBBI->getOperand(0);
1046 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1047 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001048 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001049 MachineOperand &JumpTarget = MBBI->getOperand(0);
1050 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1051 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1052 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001053 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001054 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1055 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1056 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001057 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001058 MachineOperand &JumpTarget = MBBI->getOperand(0);
1059 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1060 }
1061}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001062
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001063/// MustSaveLR - Return true if this function requires that we save the LR
1064/// register onto the stack in the prolog and restore it in the epilog of the
1065/// function.
1066static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
1067 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
1068
1069 // We need a save/restore of LR if there is any def of LR (which is
1070 // defined by calls, including the PIC setup sequence), or if there is
1071 // some use of the LR stack slot (e.g. for builtin_return_address).
1072 // (LR comes in 32 and 64 bit versions.)
1073 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
1074 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
1075}
1076
1077void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001078PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001079 RegScavenger *) const {
Hal Finkela7c54e82013-07-17 00:45:52 +00001080 const PPCRegisterInfo *RegInfo =
1081 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001082
1083 // Save and clear the LR state.
1084 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1085 unsigned LR = RegInfo->getRARegister();
1086 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001087 MachineRegisterInfo &MRI = MF.getRegInfo();
1088 MRI.setPhysRegUnused(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001089
1090 // Save R31 if necessary
1091 int FPSI = FI->getFramePointerSaveIndex();
1092 bool isPPC64 = Subtarget.isPPC64();
1093 bool isDarwinABI = Subtarget.isDarwinABI();
1094 MachineFrameInfo *MFI = MF.getFrameInfo();
1095
1096 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001097 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001098 // Find out what the fix offset of the frame pointer save area.
1099 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
1100 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001101 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001102 // Save the result.
1103 FI->setFramePointerSaveIndex(FPSI);
1104 }
1105
Hal Finkela7c54e82013-07-17 00:45:52 +00001106 int BPSI = FI->getBasePointerSaveIndex();
1107 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Hal Finkelf05d6c72013-07-17 23:50:51 +00001108 int BPOffset = getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkela7c54e82013-07-17 00:45:52 +00001109 // Allocate the frame index for the base pointer save area.
1110 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1111 // Save the result.
1112 FI->setBasePointerSaveIndex(BPSI);
1113 }
1114
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001115 // Reserve stack space to move the linkage area to in case of a tail call.
1116 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001117 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1118 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001119 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001120 }
1121
Eric Christopherd1737492014-04-29 00:16:40 +00001122 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001123 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001124 if (!isPPC64 && !isDarwinABI &&
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001125 (MRI.isPhysRegUsed(PPC::CR2) ||
1126 MRI.isPhysRegUsed(PPC::CR3) ||
1127 MRI.isPhysRegUsed(PPC::CR4))) {
1128 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1129 FI->setCRSpillFrameIndex(FrameIdx);
1130 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001131}
1132
Hal Finkel5a765fd2013-03-14 20:33:40 +00001133void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001134 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001135 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001136 if (!Subtarget.isSVR4ABI()) {
1137 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001138 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001139 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001140
1141 // Get callee saved register information.
1142 MachineFrameInfo *FFI = MF.getFrameInfo();
1143 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1144
1145 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001146 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001147 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001148 return;
1149 }
1150
1151 unsigned MinGPR = PPC::R31;
1152 unsigned MinG8R = PPC::X31;
1153 unsigned MinFPR = PPC::F31;
1154 unsigned MinVR = PPC::V31;
1155
1156 bool HasGPSaveArea = false;
1157 bool HasG8SaveArea = false;
1158 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001159 bool HasVRSAVESaveArea = false;
1160 bool HasVRSaveArea = false;
1161
1162 SmallVector<CalleeSavedInfo, 18> GPRegs;
1163 SmallVector<CalleeSavedInfo, 18> G8Regs;
1164 SmallVector<CalleeSavedInfo, 18> FPRegs;
1165 SmallVector<CalleeSavedInfo, 18> VRegs;
1166
1167 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1168 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001169 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001170 HasGPSaveArea = true;
1171
1172 GPRegs.push_back(CSI[i]);
1173
1174 if (Reg < MinGPR) {
1175 MinGPR = Reg;
1176 }
Craig Topperabadc662012-04-20 06:31:50 +00001177 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001178 HasG8SaveArea = true;
1179
1180 G8Regs.push_back(CSI[i]);
1181
1182 if (Reg < MinG8R) {
1183 MinG8R = Reg;
1184 }
Craig Topperabadc662012-04-20 06:31:50 +00001185 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001186 HasFPSaveArea = true;
1187
1188 FPRegs.push_back(CSI[i]);
1189
1190 if (Reg < MinFPR) {
1191 MinFPR = Reg;
1192 }
Craig Topperabadc662012-04-20 06:31:50 +00001193 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1194 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001195 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001196 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001197 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001198 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001199 HasVRSaveArea = true;
1200
1201 VRegs.push_back(CSI[i]);
1202
1203 if (Reg < MinVR) {
1204 MinVR = Reg;
1205 }
1206 } else {
1207 llvm_unreachable("Unknown RegisterClass!");
1208 }
1209 }
1210
1211 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Hal Finkelfeea6532013-03-26 20:08:20 +00001212 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001213
1214 int64_t LowerBound = 0;
1215
1216 // Take into account stack space reserved for tail calls.
1217 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001218 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1219 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001220 LowerBound = TCSPDelta;
1221 }
1222
1223 // The Floating-point register save area is right below the back chain word
1224 // of the previous stack frame.
1225 if (HasFPSaveArea) {
1226 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1227 int FI = FPRegs[i].getFrameIdx();
1228
1229 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1230 }
1231
Hal Finkelfeea6532013-03-26 20:08:20 +00001232 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001233 }
1234
1235 // Check whether the frame pointer register is allocated. If so, make sure it
1236 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001237 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001238 HasGPSaveArea = true;
1239
1240 int FI = PFI->getFramePointerSaveIndex();
1241 assert(FI && "No Frame Pointer Save Slot!");
1242
1243 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1244 }
1245
Hal Finkela7c54e82013-07-17 00:45:52 +00001246 const PPCRegisterInfo *RegInfo =
1247 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
1248 if (RegInfo->hasBasePointer(MF)) {
1249 HasGPSaveArea = true;
1250
1251 int FI = PFI->getBasePointerSaveIndex();
1252 assert(FI && "No Base Pointer Save Slot!");
1253
1254 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1255 }
1256
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001257 // General register save area starts right below the Floating-point
1258 // register save area.
1259 if (HasGPSaveArea || HasG8SaveArea) {
1260 // Move general register save area spill slots down, taking into account
1261 // the size of the Floating-point register save area.
1262 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1263 int FI = GPRegs[i].getFrameIdx();
1264
1265 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1266 }
1267
1268 // Move general register save area spill slots down, taking into account
1269 // the size of the Floating-point register save area.
1270 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1271 int FI = G8Regs[i].getFrameIdx();
1272
1273 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1274 }
1275
1276 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001277 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1278 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001279
1280 if (Subtarget.isPPC64()) {
1281 LowerBound -= (31 - MinReg + 1) * 8;
1282 } else {
1283 LowerBound -= (31 - MinReg + 1) * 4;
1284 }
1285 }
1286
Roman Divackyc9e23d92012-09-12 14:47:47 +00001287 // For 32-bit only, the CR save area is below the general register
1288 // save area. For 64-bit SVR4, the CR save area is addressed relative
1289 // to the stack pointer and hence does not need an adjustment here.
1290 // Only CR2 (the first nonvolatile spilled) has an associated frame
1291 // index so that we have a single uniform save area.
1292 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001293 // Adjust the frame index of the CR spill slot.
1294 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1295 unsigned Reg = CSI[i].getReg();
1296
Roman Divackyc9e23d92012-09-12 14:47:47 +00001297 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001298 // Leave Darwin logic as-is.
1299 || (!Subtarget.isSVR4ABI() &&
1300 (PPC::CRBITRCRegClass.contains(Reg) ||
1301 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001302 int FI = CSI[i].getFrameIdx();
1303
1304 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1305 }
1306 }
1307
1308 LowerBound -= 4; // The CR save area is always 4 bytes long.
1309 }
1310
1311 if (HasVRSAVESaveArea) {
1312 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1313 // which have the VRSAVE register class?
1314 // Adjust the frame index of the VRSAVE spill slot.
1315 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1316 unsigned Reg = CSI[i].getReg();
1317
Craig Topperabadc662012-04-20 06:31:50 +00001318 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001319 int FI = CSI[i].getFrameIdx();
1320
1321 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1322 }
1323 }
1324
1325 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1326 }
1327
1328 if (HasVRSaveArea) {
1329 // Insert alignment padding, we need 16-byte alignment.
1330 LowerBound = (LowerBound - 15) & ~(15);
1331
1332 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1333 int FI = VRegs[i].getFrameIdx();
1334
1335 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1336 }
1337 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001338
1339 addScavengingSpillSlot(MF, RS);
1340}
1341
1342void
1343PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1344 RegScavenger *RS) const {
1345 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1346 // a large stack, which will require scavenging a register to materialize a
1347 // large offset.
1348
1349 // We need to have a scavenger spill slot for spills if the frame size is
1350 // large. In case there is no free register for large-offset addressing,
1351 // this slot is used for the necessary emergency spill. Also, we need the
1352 // slot for dynamic stack allocations.
1353
1354 // The scavenger might be invoked if the frame offset does not fit into
1355 // the 16-bit immediate. We don't know the complete frame size here
1356 // because we've not yet computed callee-saved register spills or the
1357 // needed alignment padding.
1358 unsigned StackSize = determineFrameLayout(MF, false, true);
1359 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001360 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1361 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001362 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1363 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1364 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkel9e331c22013-03-22 23:32:27 +00001365 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkelbb420f12013-03-15 05:06:04 +00001366 RC->getAlignment(),
1367 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001368
Hal Finkel18607632013-07-18 04:28:21 +00001369 // Might we have over-aligned allocas?
1370 bool HasAlVars = MFI->hasVarSizedObjects() &&
1371 MFI->getMaxAlignment() > getStackAlignment();
1372
Hal Finkel0dfbb052013-03-26 18:57:22 +00001373 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001374 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Hal Finkel0dfbb052013-03-26 18:57:22 +00001375 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1376 RC->getAlignment(),
1377 false));
1378
Hal Finkelbb420f12013-03-15 05:06:04 +00001379 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001380}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001381
Eric Christopherd1737492014-04-29 00:16:40 +00001382bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001383PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001384 MachineBasicBlock::iterator MI,
1385 const std::vector<CalleeSavedInfo> &CSI,
1386 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001387
1388 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1389 // Return false otherwise to maintain pre-existing behavior.
1390 if (!Subtarget.isSVR4ABI())
1391 return false;
1392
1393 MachineFunction *MF = MBB.getParent();
1394 const PPCInstrInfo &TII =
1395 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1396 DebugLoc DL;
1397 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001398 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001399
Roman Divackyc9e23d92012-09-12 14:47:47 +00001400 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1401 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001402 // Only Darwin actually uses the VRSAVE register, but it can still appear
1403 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1404 // Darwin, ignore it.
1405 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1406 continue;
1407
Roman Divackyc9e23d92012-09-12 14:47:47 +00001408 // CR2 through CR4 are the nonvolatile CR fields.
1409 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1410
Roman Divackyc9e23d92012-09-12 14:47:47 +00001411 // Add the callee-saved register as live-in; it's killed at the spill.
1412 MBB.addLiveIn(Reg);
1413
Hal Finkel2f293912013-04-13 23:06:15 +00001414 if (CRSpilled && IsCRField) {
1415 CRMIB.addReg(Reg, RegState::ImplicitKill);
1416 continue;
1417 }
1418
Roman Divackyc9e23d92012-09-12 14:47:47 +00001419 // Insert the spill to the stack frame.
1420 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001421 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001422 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001423 // The actual spill will happen at the start of the prologue.
1424 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001425 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001426 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001427 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001428
Eric Christopherd1737492014-04-29 00:16:40 +00001429 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1430 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1431 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001432 .addReg(Reg, RegState::ImplicitKill);
1433
Eric Christopherd1737492014-04-29 00:16:40 +00001434 MBB.insert(MI, CRMIB);
1435 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1436 .addReg(PPC::R12,
1437 getKillRegState(true)),
1438 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001439 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001440 } else {
1441 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1442 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001443 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001444 }
1445 }
1446 return true;
1447}
1448
1449static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001450restoreCRs(bool isPPC64, bool is31,
1451 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001452 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1453 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001454
1455 MachineFunction *MF = MBB.getParent();
1456 const PPCInstrInfo &TII =
1457 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1458 DebugLoc DL;
1459 unsigned RestoreOp, MoveReg;
1460
Hal Finkel67369882013-04-15 02:07:05 +00001461 if (isPPC64)
1462 // This is handled during epilogue generation.
1463 return;
1464 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001465 // 32-bit: FP-relative
1466 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00001467 PPC::R12),
1468 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00001469 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001470 MoveReg = PPC::R12;
1471 }
Eric Christopherd1737492014-04-29 00:16:40 +00001472
Roman Divackyc9e23d92012-09-12 14:47:47 +00001473 if (CR2Spilled)
1474 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00001475 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001476
1477 if (CR3Spilled)
1478 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00001479 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001480
1481 if (CR4Spilled)
1482 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00001483 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001484}
1485
Eli Bendersky8da87162013-02-21 20:05:00 +00001486void PPCFrameLowering::
1487eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1488 MachineBasicBlock::iterator I) const {
1489 const PPCInstrInfo &TII =
1490 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1491 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1492 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1493 // Add (actually subtract) back the amount the callee popped on return.
1494 if (int CalleeAmt = I->getOperand(1).getImm()) {
1495 bool is64Bit = Subtarget.isPPC64();
1496 CalleeAmt *= -1;
1497 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1498 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1499 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1500 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1501 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1502 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1503 MachineInstr *MI = I;
1504 DebugLoc dl = MI->getDebugLoc();
1505
1506 if (isInt<16>(CalleeAmt)) {
1507 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1508 .addReg(StackReg, RegState::Kill)
1509 .addImm(CalleeAmt);
1510 } else {
1511 MachineBasicBlock::iterator MBBI = I;
1512 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1513 .addImm(CalleeAmt >> 16);
1514 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1515 .addReg(TmpReg, RegState::Kill)
1516 .addImm(CalleeAmt & 0xFFFF);
1517 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1518 .addReg(StackReg, RegState::Kill)
1519 .addReg(TmpReg);
1520 }
1521 }
1522 }
1523 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1524 MBB.erase(I);
1525}
1526
Eric Christopherd1737492014-04-29 00:16:40 +00001527bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001528PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001529 MachineBasicBlock::iterator MI,
1530 const std::vector<CalleeSavedInfo> &CSI,
1531 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001532
1533 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1534 // Return false otherwise to maintain pre-existing behavior.
1535 if (!Subtarget.isSVR4ABI())
1536 return false;
1537
1538 MachineFunction *MF = MBB.getParent();
1539 const PPCInstrInfo &TII =
1540 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1541 bool CR2Spilled = false;
1542 bool CR3Spilled = false;
1543 bool CR4Spilled = false;
1544 unsigned CSIIndex = 0;
1545
1546 // Initialize insertion-point logic; we will be restoring in reverse
1547 // order of spill.
1548 MachineBasicBlock::iterator I = MI, BeforeI = I;
1549 bool AtStart = I == MBB.begin();
1550
1551 if (!AtStart)
1552 --BeforeI;
1553
1554 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1555 unsigned Reg = CSI[i].getReg();
1556
Hal Finkelac1a24b2013-06-28 22:29:56 +00001557 // Only Darwin actually uses the VRSAVE register, but it can still appear
1558 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1559 // Darwin, ignore it.
1560 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1561 continue;
1562
Roman Divackyc9e23d92012-09-12 14:47:47 +00001563 if (Reg == PPC::CR2) {
1564 CR2Spilled = true;
1565 // The spill slot is associated only with CR2, which is the
1566 // first nonvolatile spilled. Save it here.
1567 CSIIndex = i;
1568 continue;
1569 } else if (Reg == PPC::CR3) {
1570 CR3Spilled = true;
1571 continue;
1572 } else if (Reg == PPC::CR4) {
1573 CR4Spilled = true;
1574 continue;
1575 } else {
1576 // When we first encounter a non-CR register after seeing at
1577 // least one CR register, restore all spilled CRs together.
1578 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00001579 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00001580 bool is31 = needsFP(*MF);
1581 restoreCRs(Subtarget.isPPC64(), is31,
1582 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001583 MBB, I, CSI, CSIIndex);
1584 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001585 }
1586
1587 // Default behavior for non-CR saves.
1588 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1589 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00001590 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001591 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00001592 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00001593 }
1594
1595 // Insert in reverse order.
1596 if (AtStart)
1597 I = MBB.begin();
1598 else {
1599 I = BeforeI;
1600 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00001601 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001602 }
1603
1604 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00001605 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00001606 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001607 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001608 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001609 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001610
1611 return true;
1612}