blob: 5161de85373b9b2fc0a415395c5b7aba35cc620b [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
Anton Korobeynikov33464912010-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 Korobeynikov16c29b52011-01-10 12:39:04 +000010// This file contains the PPC implementation of TargetFrameLowering class.
Anton Korobeynikov33464912010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "PPCFrameLowering.h"
Roman Divacky9d760ae2012-09-12 14:47:47 +000015#include "PPCInstrBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "PPCInstrInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000017#include "PPCMachineFunctionInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000025#include "llvm/Target/TargetOptions.h"
26
27using namespace llvm;
28
Anton Korobeynikov33464912010-11-15 00:06:54 +000029/// VRRegNo - Map from a numbered VR register to its enum value.
30///
Craig Topperb78ca422012-03-11 07:16:55 +000031static const uint16_t VRRegNo[] = {
Anton Korobeynikov33464912010-11-15 00:06:54 +000032 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
33 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
34 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
35 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
36};
37
38/// RemoveVRSaveCode - We have found that this function does not need any code
39/// to manipulate the VRSAVE register, even though it uses vector registers.
40/// This can happen when the only registers used are known to be live in or out
41/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidta5d0ab52012-10-10 20:54:15 +000042/// FIXME: The removal of the code results in a compile failure at -O0 when the
43/// function contains a function call, as the GPR containing original VRSAVE
44/// contents is spilled and reloaded around the call. Without the prolog code,
45/// the spill instruction refers to an undefined register. This code needs
46/// to account for all uses of that GPR.
Anton Korobeynikov33464912010-11-15 00:06:54 +000047static void RemoveVRSaveCode(MachineInstr *MI) {
48 MachineBasicBlock *Entry = MI->getParent();
49 MachineFunction *MF = Entry->getParent();
50
51 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
52 MachineBasicBlock::iterator MBBI = MI;
53 ++MBBI;
54 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
55 MBBI->eraseFromParent();
56
57 bool RemovedAllMTVRSAVEs = true;
58 // See if we can find and remove the MTVRSAVE instruction from all of the
59 // epilog blocks.
60 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
61 // If last instruction is a return instruction, add an epilogue
Evan Cheng5a96b3d2011-12-07 07:15:52 +000062 if (!I->empty() && I->back().isReturn()) {
Anton Korobeynikov33464912010-11-15 00:06:54 +000063 bool FoundIt = false;
64 for (MBBI = I->end(); MBBI != I->begin(); ) {
65 --MBBI;
66 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
67 MBBI->eraseFromParent(); // remove it.
68 FoundIt = true;
69 break;
70 }
71 }
72 RemovedAllMTVRSAVEs &= FoundIt;
73 }
74 }
75
76 // If we found and removed all MTVRSAVE instructions, remove the read of
77 // VRSAVE as well.
78 if (RemovedAllMTVRSAVEs) {
79 MBBI = MI;
80 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
81 --MBBI;
82 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
83 MBBI->eraseFromParent();
84 }
85
86 // Finally, nuke the UPDATE_VRSAVE.
87 MI->eraseFromParent();
88}
89
90// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
91// instruction selector. Based on the vector registers that have been used,
92// transform this into the appropriate ORI instruction.
93static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
94 MachineFunction *MF = MI->getParent()->getParent();
Hal Finkelaa6047d2013-03-26 20:08:20 +000095 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +000096 DebugLoc dl = MI->getDebugLoc();
97
98 unsigned UsedRegMask = 0;
99 for (unsigned i = 0; i != 32; ++i)
100 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
101 UsedRegMask |= 1 << (31-i);
102
103 // Live in and live out values already must be in the mask, so don't bother
104 // marking them.
105 for (MachineRegisterInfo::livein_iterator
106 I = MF->getRegInfo().livein_begin(),
107 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelaa6047d2013-03-26 20:08:20 +0000108 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000109 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
110 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
111 }
Jakob Stoklund Olesen0a9d1d32013-02-05 17:40:36 +0000112
113 // Live out registers appear as use operands on return instructions.
114 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
115 UsedRegMask != 0 && BI != BE; ++BI) {
116 const MachineBasicBlock &MBB = *BI;
117 if (MBB.empty() || !MBB.back().isReturn())
118 continue;
119 const MachineInstr &Ret = MBB.back();
120 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
121 const MachineOperand &MO = Ret.getOperand(I);
122 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
123 continue;
Hal Finkelaa6047d2013-03-26 20:08:20 +0000124 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesen0a9d1d32013-02-05 17:40:36 +0000125 UsedRegMask &= ~(1 << (31-RegNo));
126 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000127 }
128
129 // If no registers are used, turn this into a copy.
130 if (UsedRegMask == 0) {
131 // Remove all VRSAVE code.
132 RemoveVRSaveCode(MI);
133 return;
134 }
135
136 unsigned SrcReg = MI->getOperand(1).getReg();
137 unsigned DstReg = MI->getOperand(0).getReg();
138
139 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
140 if (DstReg != SrcReg)
141 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
142 .addReg(SrcReg)
143 .addImm(UsedRegMask);
144 else
145 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
146 .addReg(SrcReg, RegState::Kill)
147 .addImm(UsedRegMask);
148 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
149 if (DstReg != SrcReg)
150 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
151 .addReg(SrcReg)
152 .addImm(UsedRegMask >> 16);
153 else
154 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
155 .addReg(SrcReg, RegState::Kill)
156 .addImm(UsedRegMask >> 16);
157 } else {
158 if (DstReg != SrcReg)
159 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
160 .addReg(SrcReg)
161 .addImm(UsedRegMask >> 16);
162 else
163 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
164 .addReg(SrcReg, RegState::Kill)
165 .addImm(UsedRegMask >> 16);
166
167 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
168 .addReg(DstReg, RegState::Kill)
169 .addImm(UsedRegMask & 0xFFFF);
170 }
171
172 // Remove the old UPDATE_VRSAVE instruction.
173 MI->eraseFromParent();
174}
175
Roman Divacky9d760ae2012-09-12 14:47:47 +0000176static bool spillsCR(const MachineFunction &MF) {
177 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
178 return FuncInfo->isCRSpilled();
179}
180
Hal Finkel3f2c0472013-03-23 22:06:03 +0000181static bool spillsVRSAVE(const MachineFunction &MF) {
182 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
183 return FuncInfo->isVRSAVESpilled();
184}
185
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000186static bool hasSpills(const MachineFunction &MF) {
187 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
188 return FuncInfo->hasSpills();
189}
190
Hal Finkel32497292013-03-17 04:43:44 +0000191static bool hasNonRISpills(const MachineFunction &MF) {
192 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
193 return FuncInfo->hasNonRISpills();
194}
195
Anton Korobeynikov33464912010-11-15 00:06:54 +0000196/// determineFrameLayout - Determine the size of the frame and maximum call
197/// frame size.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000198unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
199 bool UpdateMF,
200 bool UseEstimate) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000201 MachineFrameInfo *MFI = MF.getFrameInfo();
202
203 // Get the number of bytes to allocate from the FrameInfo
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000204 unsigned FrameSize =
205 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000206
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000207 // Get stack alignments. The frame must be aligned to the greatest of these:
208 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
209 unsigned MaxAlign = MFI->getMaxAlignment(); // algmt required by data in frame
Hal Finkelfe47bf82013-07-17 00:45:52 +0000210 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
211
212 const PPCRegisterInfo *RegInfo =
213 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000214
215 // If we are a leaf function, and use up to 224 bytes of stack space,
216 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000217 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt65396822013-02-26 21:28:57 +0000218 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
219 // stackless code if all local vars are reg-allocated.
Bill Wendling831737d2012-12-30 10:32:01 +0000220 bool DisableRedZone = MF.getFunction()->getAttributes().
221 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000222 if (!DisableRedZone &&
Bill Schmidt65396822013-02-26 21:28:57 +0000223 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
224 !Subtarget.isSVR4ABI() || // allocated locals.
225 FrameSize == 0) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000226 FrameSize <= 224 && // Fits in red zone.
227 !MFI->hasVarSizedObjects() && // No dynamic alloca.
228 !MFI->adjustsStack() && // No calls.
Hal Finkelfe47bf82013-07-17 00:45:52 +0000229 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000230 // No need for frame
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000231 if (UpdateMF)
232 MFI->setStackSize(0);
233 return 0;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000234 }
235
236 // Get the maximum call frame size of all the calls.
237 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
238
239 // Maximum call frame needs to be at least big enough for linkage and 8 args.
240 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
241 Subtarget.isDarwinABI());
242 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
243
244 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
245 // that allocations will be aligned.
246 if (MFI->hasVarSizedObjects())
247 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
248
249 // Update maximum call frame size.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000250 if (UpdateMF)
251 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000252
253 // Include call frame size in total.
254 FrameSize += maxCallFrameSize;
255
256 // Make sure the frame is aligned.
257 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
258
259 // Update frame info.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000260 if (UpdateMF)
261 MFI->setStackSize(FrameSize);
262
263 return FrameSize;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000264}
265
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000266// hasFP - Return true if the specified function actually has a dedicated frame
267// pointer register.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000268bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000269 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000270 // FIXME: This is pretty much broken by design: hasFP() might be called really
271 // early, before the stack layout was calculated and thus hasFP() might return
272 // true or false here depending on the time of call.
273 return (MFI->getStackSize()) && needsFP(MF);
274}
275
276// needsFP - Return true if the specified function should have a dedicated frame
277// pointer register. This is true if the function has variable sized allocas or
278// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000279bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000280 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000281
282 // Naked functions have no stack frame pushed, so we don't have a frame
283 // pointer.
Bill Wendling831737d2012-12-30 10:32:01 +0000284 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
285 Attribute::Naked))
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000286 return false;
287
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000288 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
289 MFI->hasVarSizedObjects() ||
290 (MF.getTarget().Options.GuaranteedTailCallOpt &&
291 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000292}
293
Hal Finkele9cc0a02013-03-21 19:03:19 +0000294void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
295 bool is31 = needsFP(MF);
296 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
297 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
298
Hal Finkel05417222013-07-17 23:50:51 +0000299 const PPCRegisterInfo *RegInfo =
300 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
301 bool HasBP = RegInfo->hasBasePointer(MF);
302 unsigned BPReg = HasBP ? (unsigned) PPC::R30 : FPReg;
303 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
304
Hal Finkele9cc0a02013-03-21 19:03:19 +0000305 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
306 BI != BE; ++BI)
307 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
308 --MBBI;
309 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
310 MachineOperand &MO = MBBI->getOperand(I);
311 if (!MO.isReg())
312 continue;
313
314 switch (MO.getReg()) {
315 case PPC::FP:
316 MO.setReg(FPReg);
317 break;
318 case PPC::FP8:
319 MO.setReg(FP8Reg);
320 break;
Hal Finkel05417222013-07-17 23:50:51 +0000321 case PPC::BP:
322 MO.setReg(BPReg);
323 break;
324 case PPC::BP8:
325 MO.setReg(BP8Reg);
326 break;
327
Hal Finkele9cc0a02013-03-21 19:03:19 +0000328 }
329 }
330 }
331}
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000332
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000333void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000334 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
335 MachineBasicBlock::iterator MBBI = MBB.begin();
336 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000337 const PPCInstrInfo &TII =
338 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkelfe47bf82013-07-17 00:45:52 +0000339 const PPCRegisterInfo *RegInfo =
340 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000341
342 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendling99cb6222013-06-18 07:20:20 +0000343 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000344 DebugLoc dl;
345 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000346 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000347
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000348 // Get processor type.
349 bool isPPC64 = Subtarget.isPPC64();
350 // Get the ABI.
351 bool isDarwinABI = Subtarget.isDarwinABI();
352 bool isSVR4ABI = Subtarget.isSVR4ABI();
353 assert((isDarwinABI || isSVR4ABI) &&
354 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
355
Anton Korobeynikov33464912010-11-15 00:06:54 +0000356 // Prepare for frame info.
357 MCSymbol *FrameLabel = 0;
358
359 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
360 // process it.
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000361 if (!isSVR4ABI)
Bill Schmidta5d0ab52012-10-10 20:54:15 +0000362 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
363 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
364 HandleVRSaveUpdate(MBBI, TII);
365 break;
366 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000367 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000368
369 // Move MBBI back to the beginning of the function.
370 MBBI = MBB.begin();
371
372 // Work out frame sizes.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000373 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000374 int NegFrameSize = -FrameSize;
Hal Finkelfe47bf82013-07-17 00:45:52 +0000375 if (!isInt<32>(NegFrameSize))
376 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000377
Hal Finkele9cc0a02013-03-21 19:03:19 +0000378 if (MFI->isFrameAddressTaken())
379 replaceFPWithRealFP(MF);
380
Anton Korobeynikov33464912010-11-15 00:06:54 +0000381 // Check if the link register (LR) must be saved.
382 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
383 bool MustSaveLR = FI->mustSaveLR();
Craig Toppera0ec3f92013-07-14 04:42:23 +0000384 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000385 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000386 bool HasFP = hasFP(MF);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000387 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000388
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000389 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
390 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
391 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
392 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
393 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
394 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
395
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000396 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000397
398 int FPOffset = 0;
399 if (HasFP) {
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000400 if (isSVR4ABI) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000401 MachineFrameInfo *FFI = MF.getFrameInfo();
402 int FPIndex = FI->getFramePointerSaveIndex();
403 assert(FPIndex && "No Frame Pointer Save Slot!");
404 FPOffset = FFI->getObjectOffset(FPIndex);
405 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000406 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000407 }
408 }
409
Hal Finkelfe47bf82013-07-17 00:45:52 +0000410 int BPOffset = 0;
411 if (HasBP) {
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000412 if (isSVR4ABI) {
Hal Finkelfe47bf82013-07-17 00:45:52 +0000413 MachineFrameInfo *FFI = MF.getFrameInfo();
414 int BPIndex = FI->getBasePointerSaveIndex();
415 assert(BPIndex && "No Base Pointer Save Slot!");
416 BPOffset = FFI->getObjectOffset(BPIndex);
417 } else {
418 BPOffset =
Hal Finkel05417222013-07-17 23:50:51 +0000419 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000420 }
421 }
422
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000423 // Get stack alignments.
424 unsigned MaxAlign = MFI->getMaxAlignment();
425 if (HasBP && MaxAlign > 1)
426 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
427 "Invalid alignment!");
428
429 // Frames of 32KB & larger require special handling because they cannot be
430 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
431 bool isLargeFrame = !isInt<16>(NegFrameSize);
432
Anton Korobeynikov33464912010-11-15 00:06:54 +0000433 if (isPPC64) {
434 if (MustSaveLR)
435 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
436
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000437 if (!MustSaveCRs.empty()) {
438 MachineInstrBuilder MIB =
439 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), PPC::X12);
440 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
441 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
442 }
443
Anton Korobeynikov33464912010-11-15 00:06:54 +0000444 if (HasFP)
445 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
446 .addReg(PPC::X31)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000447 .addImm(FPOffset)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000448 .addReg(PPC::X1);
449
Hal Finkelfe47bf82013-07-17 00:45:52 +0000450 if (HasBP)
451 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
Hal Finkel05417222013-07-17 23:50:51 +0000452 .addReg(PPC::X30)
Hal Finkelfe47bf82013-07-17 00:45:52 +0000453 .addImm(BPOffset)
454 .addReg(PPC::X1);
455
Anton Korobeynikov33464912010-11-15 00:06:54 +0000456 if (MustSaveLR)
457 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
458 .addReg(PPC::X0)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000459 .addImm(LROffset)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000460 .addReg(PPC::X1);
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000461
462 if (!MustSaveCRs.empty())
463 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
464 .addReg(PPC::X12, getKillRegState(true))
465 .addImm(8)
466 .addReg(PPC::X1);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000467 } else { // PPC32...
Anton Korobeynikov33464912010-11-15 00:06:54 +0000468 if (MustSaveLR)
469 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
470
471 if (HasFP)
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000472 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000473 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
474 .addReg(PPC::R31)
475 .addImm(FPOffset)
476 .addReg(PPC::R1);
477
Hal Finkelfe47bf82013-07-17 00:45:52 +0000478 if (HasBP)
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000479 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
Hal Finkelfe47bf82013-07-17 00:45:52 +0000480 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
Hal Finkel05417222013-07-17 23:50:51 +0000481 .addReg(PPC::R30)
Hal Finkelfe47bf82013-07-17 00:45:52 +0000482 .addImm(BPOffset)
483 .addReg(PPC::R1);
484
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000485 assert(MustSaveCRs.empty() &&
486 "Prologue CR saving supported only in 64-bit mode");
487
Anton Korobeynikov33464912010-11-15 00:06:54 +0000488 if (MustSaveLR)
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000489 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000490 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
491 .addReg(PPC::R0)
492 .addImm(LROffset)
493 .addReg(PPC::R1);
494 }
495
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000496 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000497 if (!FrameSize) return;
498
Anton Korobeynikov33464912010-11-15 00:06:54 +0000499 // Adjust stack pointer: r1 += NegFrameSize.
500 // If there is a preferred stack alignment, align R1 now
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000501 if (!isPPC64) { // PPC32...
Hal Finkelfe47bf82013-07-17 00:45:52 +0000502 if (HasBP) {
503 // Save a copy of r1 as the base pointer.
Hal Finkel05417222013-07-17 23:50:51 +0000504 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R30)
Hal Finkelfe47bf82013-07-17 00:45:52 +0000505 .addReg(PPC::R1)
506 .addReg(PPC::R1);
507 }
508
509 if (HasBP && MaxAlign > 1) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000510 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
511 .addReg(PPC::R1)
512 .addImm(0)
513 .addImm(32 - Log2_32(MaxAlign))
514 .addImm(31);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000515 if (!isLargeFrame) {
Hal Finkelfe47bf82013-07-17 00:45:52 +0000516 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC), PPC::R0)
517 .addReg(PPC::R0, RegState::Kill)
518 .addImm(NegFrameSize);
519 } else {
520 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R12)
521 .addImm(NegFrameSize >> 16);
522 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R12)
523 .addReg(PPC::R12, RegState::Kill)
524 .addImm(NegFrameSize & 0xFFFF);
525 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFC), PPC::R0)
526 .addReg(PPC::R0, RegState::Kill)
527 .addReg(PPC::R12, RegState::Kill);
528 }
Hal Finkelac81cc32012-06-19 02:34:32 +0000529 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000530 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000531 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000532 .addReg(PPC::R0);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000533
534 } else if (!isLargeFrame) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000535 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
536 .addReg(PPC::R1)
537 .addImm(NegFrameSize)
538 .addReg(PPC::R1);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000539
Anton Korobeynikov33464912010-11-15 00:06:54 +0000540 } else {
541 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
542 .addImm(NegFrameSize >> 16);
543 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
544 .addReg(PPC::R0, RegState::Kill)
545 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000546 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000547 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000548 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000549 .addReg(PPC::R0);
550 }
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000551 } else { // PPC64...
Hal Finkelfe47bf82013-07-17 00:45:52 +0000552 if (HasBP) {
553 // Save a copy of r1 as the base pointer.
Hal Finkel05417222013-07-17 23:50:51 +0000554 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X30)
Hal Finkelfe47bf82013-07-17 00:45:52 +0000555 .addReg(PPC::X1)
556 .addReg(PPC::X1);
557 }
558
559 if (HasBP && MaxAlign > 1) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000560 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
561 .addReg(PPC::X1)
562 .addImm(0)
563 .addImm(64 - Log2_32(MaxAlign));
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000564 if (!isLargeFrame) {
Hal Finkelfe47bf82013-07-17 00:45:52 +0000565 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
566 .addReg(PPC::X0, RegState::Kill)
567 .addImm(NegFrameSize);
568 } else {
569 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X12)
570 .addImm(NegFrameSize >> 16);
571 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X12)
572 .addReg(PPC::X12, RegState::Kill)
573 .addImm(NegFrameSize & 0xFFFF);
574 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFC8), PPC::X0)
575 .addReg(PPC::X0, RegState::Kill)
576 .addReg(PPC::X12, RegState::Kill);
577 }
Hal Finkelac81cc32012-06-19 02:34:32 +0000578 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000579 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000580 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000581 .addReg(PPC::X0);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000582
583 } else if (!isLargeFrame) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000584 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
585 .addReg(PPC::X1)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000586 .addImm(NegFrameSize)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000587 .addReg(PPC::X1);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000588
Anton Korobeynikov33464912010-11-15 00:06:54 +0000589 } else {
590 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
591 .addImm(NegFrameSize >> 16);
592 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
593 .addReg(PPC::X0, RegState::Kill)
594 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000595 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000596 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000597 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000598 .addReg(PPC::X0);
599 }
600 }
601
Anton Korobeynikov33464912010-11-15 00:06:54 +0000602 // Add the "machine moves" for the instructions we generated above, but in
603 // reverse order.
604 if (needsFrameMoves) {
605 // Mark effective beginning of when frame pointer becomes valid.
606 FrameLabel = MMI.getContext().CreateTempSymbol();
607 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
608
609 // Show update of SP.
Rafael Espindolaec7f4232013-05-16 03:34:58 +0000610 assert(NegFrameSize);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000611 MMI.addFrameInst(
612 MCCFIInstruction::createDefCfaOffset(FrameLabel, NegFrameSize));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000613
614 if (HasFP) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000615 unsigned Reg = isPPC64 ? PPC::X31 : PPC::R31;
Bill Wendling99cb6222013-06-18 07:20:20 +0000616 Reg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000617 MMI.addFrameInst(
618 MCCFIInstruction::createOffset(FrameLabel, Reg, FPOffset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000619 }
620
Hal Finkelfe47bf82013-07-17 00:45:52 +0000621 if (HasBP) {
Hal Finkel05417222013-07-17 23:50:51 +0000622 unsigned Reg = isPPC64 ? PPC::X30 : PPC::R30;
Hal Finkelfe47bf82013-07-17 00:45:52 +0000623 Reg = MRI->getDwarfRegNum(Reg, true);
624 MMI.addFrameInst(
625 MCCFIInstruction::createOffset(FrameLabel, Reg, BPOffset));
626 }
627
Anton Korobeynikov33464912010-11-15 00:06:54 +0000628 if (MustSaveLR) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000629 unsigned Reg = isPPC64 ? PPC::LR8 : PPC::LR;
Bill Wendling99cb6222013-06-18 07:20:20 +0000630 Reg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000631 MMI.addFrameInst(
632 MCCFIInstruction::createOffset(FrameLabel, Reg, LROffset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000633 }
634 }
635
636 MCSymbol *ReadyLabel = 0;
637
638 // If there is a frame pointer, copy R1 into R31
639 if (HasFP) {
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000640 if (!isPPC64) { // PPC32...
Anton Korobeynikov33464912010-11-15 00:06:54 +0000641 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
642 .addReg(PPC::R1)
643 .addReg(PPC::R1);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000644 } else { // PPC64...
Anton Korobeynikov33464912010-11-15 00:06:54 +0000645 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
646 .addReg(PPC::X1)
647 .addReg(PPC::X1);
648 }
649
650 if (needsFrameMoves) {
651 ReadyLabel = MMI.getContext().CreateTempSymbol();
652
653 // Mark effective beginning of when frame pointer is ready.
654 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
655
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000656 unsigned Reg = isPPC64 ? PPC::X31 : PPC::R31;
Bill Wendling99cb6222013-06-18 07:20:20 +0000657 Reg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000658 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(ReadyLabel, Reg));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000659 }
660 }
661
662 if (needsFrameMoves) {
663 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
664
665 // Add callee saved registers to move list.
666 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
667 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000668 unsigned Reg = CSI[I].getReg();
669 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000670
671 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
672 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperc9099502012-04-20 06:31:50 +0000673 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola6e032942011-05-30 20:20:15 +0000674 continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000675
Roman Divacky9d760ae2012-09-12 14:47:47 +0000676 // For SVR4, don't emit a move for the CR spill slot if we haven't
677 // spilled CRs.
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000678 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
679 && MustSaveCRs.empty())
680 continue;
Roman Divacky9d760ae2012-09-12 14:47:47 +0000681
682 // For 64-bit SVR4 when we have spilled CRs, the spill location
683 // is SP+8, not a frame-relative slot.
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000684 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000685 MMI.addFrameInst(MCCFIInstruction::createOffset(
Bill Wendling99cb6222013-06-18 07:20:20 +0000686 Label, MRI->getDwarfRegNum(PPC::CR2, true), 8));
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000687 continue;
Roman Divacky9d760ae2012-09-12 14:47:47 +0000688 }
689
690 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000691 MMI.addFrameInst(MCCFIInstruction::createOffset(
Bill Wendling99cb6222013-06-18 07:20:20 +0000692 Label, MRI->getDwarfRegNum(Reg, true), Offset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000693 }
694 }
695}
696
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000697void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000698 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000699 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
700 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000701 const PPCInstrInfo &TII =
702 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkelfe47bf82013-07-17 00:45:52 +0000703 const PPCRegisterInfo *RegInfo =
704 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000705
706 unsigned RetOpcode = MBBI->getOpcode();
707 DebugLoc dl;
708
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000709 assert((RetOpcode == PPC::BLR ||
710 RetOpcode == PPC::TCRETURNri ||
711 RetOpcode == PPC::TCRETURNdi ||
712 RetOpcode == PPC::TCRETURNai ||
713 RetOpcode == PPC::TCRETURNri8 ||
714 RetOpcode == PPC::TCRETURNdi8 ||
715 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000716 "Can only insert epilog into returning blocks");
717
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000718 // Get alignment info so we know how to restore the SP.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000719 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000720
721 // Get the number of bytes allocated from the FrameInfo.
722 int FrameSize = MFI->getStackSize();
723
724 // Get processor type.
725 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000726 // Get the ABI.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000727 bool isDarwinABI = Subtarget.isDarwinABI();
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000728 bool isSVR4ABI = Subtarget.isSVR4ABI();
729
Anton Korobeynikov33464912010-11-15 00:06:54 +0000730 // Check if the link register (LR) has been saved.
731 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
732 bool MustSaveLR = FI->mustSaveLR();
Craig Toppera0ec3f92013-07-14 04:42:23 +0000733 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000734 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000735 bool HasFP = hasFP(MF);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000736 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000737
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000738 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000739
740 int FPOffset = 0;
741 if (HasFP) {
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000742 if (isSVR4ABI) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000743 MachineFrameInfo *FFI = MF.getFrameInfo();
744 int FPIndex = FI->getFramePointerSaveIndex();
745 assert(FPIndex && "No Frame Pointer Save Slot!");
746 FPOffset = FFI->getObjectOffset(FPIndex);
747 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000748 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000749 }
750 }
751
Hal Finkelfe47bf82013-07-17 00:45:52 +0000752 int BPOffset = 0;
753 if (HasBP) {
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000754 if (isSVR4ABI) {
Hal Finkelfe47bf82013-07-17 00:45:52 +0000755 MachineFrameInfo *FFI = MF.getFrameInfo();
756 int BPIndex = FI->getBasePointerSaveIndex();
757 assert(BPIndex && "No Base Pointer Save Slot!");
758 BPOffset = FFI->getObjectOffset(BPIndex);
759 } else {
760 BPOffset =
Hal Finkel05417222013-07-17 23:50:51 +0000761 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000762 }
763 }
764
Anton Korobeynikov33464912010-11-15 00:06:54 +0000765 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
766 RetOpcode == PPC::TCRETURNdi ||
767 RetOpcode == PPC::TCRETURNai ||
768 RetOpcode == PPC::TCRETURNri8 ||
769 RetOpcode == PPC::TCRETURNdi8 ||
770 RetOpcode == PPC::TCRETURNai8;
771
772 if (UsesTCRet) {
773 int MaxTCRetDelta = FI->getTailCallSPDelta();
774 MachineOperand &StackAdjust = MBBI->getOperand(1);
775 assert(StackAdjust.isImm() && "Expecting immediate value.");
776 // Adjust stack pointer.
777 int StackAdj = StackAdjust.getImm();
778 int Delta = StackAdj - MaxTCRetDelta;
779 assert((Delta >= 0) && "Delta must be positive");
780 if (MaxTCRetDelta>0)
781 FrameSize += (StackAdj +Delta);
782 else
783 FrameSize += StackAdj;
784 }
785
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000786 // Frames of 32KB & larger require special handling because they cannot be
787 // indexed into with a simple LD/LWZ immediate offset operand.
788 bool isLargeFrame = !isInt<16>(FrameSize);
789
Anton Korobeynikov33464912010-11-15 00:06:54 +0000790 if (FrameSize) {
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000791 // In the prologue, the loaded (or persistent) stack pointer value is offset
792 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000793 if (!isPPC64) {
794 // If this function contained a fastcc call and GuaranteedTailCallOpt is
795 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
796 // call which invalidates the stack pointer value in SP(0). So we use the
797 // value of R31 in this case.
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000798 if (FI->hasFastCall()) {
799 assert(HasFP && "Expecting a valid frame pointer.");
800 if (!isLargeFrame) {
801 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
802 .addReg(PPC::R31).addImm(FrameSize);
803 } else {
804 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
805 .addImm(FrameSize >> 16);
806 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
807 .addReg(PPC::R0, RegState::Kill)
808 .addImm(FrameSize & 0xFFFF);
809 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
810 .addReg(PPC::R1)
811 .addReg(PPC::R31)
812 .addReg(PPC::R0);
813 }
814 } else if (!isLargeFrame && !HasBP &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000815 !MFI->hasVarSizedObjects()) {
816 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
817 .addReg(PPC::R1).addImm(FrameSize);
818 } else {
819 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
820 .addImm(0).addReg(PPC::R1);
821 }
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000822 } else { // PPC64...
823 if (FI->hasFastCall()) {
824 if (!isLargeFrame) {
825 assert(HasFP && "Expecting a valid frame pointer.");
826 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
827 .addReg(PPC::X31).addImm(FrameSize);
828 } else {
829 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
830 .addImm(FrameSize >> 16);
831 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
832 .addReg(PPC::X0, RegState::Kill)
833 .addImm(FrameSize & 0xFFFF);
834 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
835 .addReg(PPC::X1)
836 .addReg(PPC::X31)
837 .addReg(PPC::X0);
838 }
839 } else if (!isLargeFrame && !HasBP &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000840 !MFI->hasVarSizedObjects()) {
841 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
842 .addReg(PPC::X1).addImm(FrameSize);
843 } else {
844 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
845 .addImm(0).addReg(PPC::X1);
846 }
847 }
848 }
849
850 if (isPPC64) {
851 if (MustSaveLR)
852 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000853 .addImm(LROffset).addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000854
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000855 if (!MustSaveCRs.empty())
856 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), PPC::X12)
857 .addImm(8).addReg(PPC::X1);
858
Anton Korobeynikov33464912010-11-15 00:06:54 +0000859 if (HasFP)
860 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000861 .addImm(FPOffset).addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000862
Hal Finkelfe47bf82013-07-17 00:45:52 +0000863 if (HasBP)
Hal Finkel05417222013-07-17 23:50:51 +0000864 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X30)
Hal Finkelfe47bf82013-07-17 00:45:52 +0000865 .addImm(BPOffset).addReg(PPC::X1);
866
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000867 if (!MustSaveCRs.empty())
868 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Ulrich Weigand33efedc2013-07-03 17:59:07 +0000869 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000870 .addReg(PPC::X12, getKillRegState(i == e-1));
871
Anton Korobeynikov33464912010-11-15 00:06:54 +0000872 if (MustSaveLR)
873 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
Bill Schmidt9bb6c812013-08-16 20:05:04 +0000874 } else { // PPC32...
Anton Korobeynikov33464912010-11-15 00:06:54 +0000875 if (MustSaveLR)
876 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
877 .addImm(LROffset).addReg(PPC::R1);
878
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000879 assert(MustSaveCRs.empty() &&
880 "Epilogue CR restoring supported only in 64-bit mode");
881
Anton Korobeynikov33464912010-11-15 00:06:54 +0000882 if (HasFP)
883 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
884 .addImm(FPOffset).addReg(PPC::R1);
885
Hal Finkelfe47bf82013-07-17 00:45:52 +0000886 if (HasBP)
Hal Finkel05417222013-07-17 23:50:51 +0000887 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R30)
Hal Finkelfe47bf82013-07-17 00:45:52 +0000888 .addImm(FPOffset).addReg(PPC::R1);
889
Anton Korobeynikov33464912010-11-15 00:06:54 +0000890 if (MustSaveLR)
891 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
892 }
893
894 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
895 // call optimization
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000896 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000897 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
898 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
899 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
900 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
901 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
902 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
903 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
904 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
905 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
906 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
907
908 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
909 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
910 .addReg(StackReg).addImm(CallerAllocatedAmt);
911 } else {
912 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
913 .addImm(CallerAllocatedAmt >> 16);
914 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
915 .addReg(TmpReg, RegState::Kill)
916 .addImm(CallerAllocatedAmt & 0xFFFF);
917 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
918 .addReg(StackReg)
919 .addReg(FPReg)
920 .addReg(TmpReg);
921 }
922 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000923 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000924 MachineOperand &JumpTarget = MBBI->getOperand(0);
925 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
926 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
927 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000928 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000929 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
930 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
931 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000932 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000933 MachineOperand &JumpTarget = MBBI->getOperand(0);
934 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
935 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000936 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000937 MachineOperand &JumpTarget = MBBI->getOperand(0);
938 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
939 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
940 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000941 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000942 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
943 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
944 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000945 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000946 MachineOperand &JumpTarget = MBBI->getOperand(0);
947 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
948 }
949}
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000950
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000951/// MustSaveLR - Return true if this function requires that we save the LR
952/// register onto the stack in the prolog and restore it in the epilog of the
953/// function.
954static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
955 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
956
957 // We need a save/restore of LR if there is any def of LR (which is
958 // defined by calls, including the PIC setup sequence), or if there is
959 // some use of the LR stack slot (e.g. for builtin_return_address).
960 // (LR comes in 32 and 64 bit versions.)
961 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
962 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
963}
964
965void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000966PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000967 RegScavenger *) const {
Hal Finkelfe47bf82013-07-17 00:45:52 +0000968 const PPCRegisterInfo *RegInfo =
969 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000970
971 // Save and clear the LR state.
972 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
973 unsigned LR = RegInfo->getRARegister();
974 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000975 MachineRegisterInfo &MRI = MF.getRegInfo();
976 MRI.setPhysRegUnused(LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000977
978 // Save R31 if necessary
979 int FPSI = FI->getFramePointerSaveIndex();
980 bool isPPC64 = Subtarget.isPPC64();
981 bool isDarwinABI = Subtarget.isDarwinABI();
982 MachineFrameInfo *MFI = MF.getFrameInfo();
983
984 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000985 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000986 // Find out what the fix offset of the frame pointer save area.
987 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
988 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000989 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000990 // Save the result.
991 FI->setFramePointerSaveIndex(FPSI);
992 }
993
Hal Finkelfe47bf82013-07-17 00:45:52 +0000994 int BPSI = FI->getBasePointerSaveIndex();
995 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Hal Finkel05417222013-07-17 23:50:51 +0000996 int BPOffset = getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000997 // Allocate the frame index for the base pointer save area.
998 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
999 // Save the result.
1000 FI->setBasePointerSaveIndex(BPSI);
1001 }
1002
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001003 // Reserve stack space to move the linkage area to in case of a tail call.
1004 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001005 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1006 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001007 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001008 }
1009
Bill Schmidt4edd84d2013-02-24 17:34:50 +00001010 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
1011 // function uses CR 2, 3, or 4.
1012 if (!isPPC64 && !isDarwinABI &&
1013 (MRI.isPhysRegUsed(PPC::CR2) ||
1014 MRI.isPhysRegUsed(PPC::CR3) ||
1015 MRI.isPhysRegUsed(PPC::CR4))) {
1016 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1017 FI->setCRSpillFrameIndex(FrameIdx);
1018 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001019}
1020
Hal Finkel3080d232013-03-14 20:33:40 +00001021void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001022 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001023 // Early exit if not using the SVR4 ABI.
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001024 if (!Subtarget.isSVR4ABI()) {
1025 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001026 return;
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001027 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001028
1029 // Get callee saved register information.
1030 MachineFrameInfo *FFI = MF.getFrameInfo();
1031 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1032
1033 // Early exit if no callee saved registers are modified!
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +00001034 if (CSI.empty() && !needsFP(MF)) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001035 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001036 return;
1037 }
1038
1039 unsigned MinGPR = PPC::R31;
1040 unsigned MinG8R = PPC::X31;
1041 unsigned MinFPR = PPC::F31;
1042 unsigned MinVR = PPC::V31;
1043
1044 bool HasGPSaveArea = false;
1045 bool HasG8SaveArea = false;
1046 bool HasFPSaveArea = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001047 bool HasVRSAVESaveArea = false;
1048 bool HasVRSaveArea = false;
1049
1050 SmallVector<CalleeSavedInfo, 18> GPRegs;
1051 SmallVector<CalleeSavedInfo, 18> G8Regs;
1052 SmallVector<CalleeSavedInfo, 18> FPRegs;
1053 SmallVector<CalleeSavedInfo, 18> VRegs;
1054
1055 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1056 unsigned Reg = CSI[i].getReg();
Craig Topperc9099502012-04-20 06:31:50 +00001057 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001058 HasGPSaveArea = true;
1059
1060 GPRegs.push_back(CSI[i]);
1061
1062 if (Reg < MinGPR) {
1063 MinGPR = Reg;
1064 }
Craig Topperc9099502012-04-20 06:31:50 +00001065 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001066 HasG8SaveArea = true;
1067
1068 G8Regs.push_back(CSI[i]);
1069
1070 if (Reg < MinG8R) {
1071 MinG8R = Reg;
1072 }
Craig Topperc9099502012-04-20 06:31:50 +00001073 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001074 HasFPSaveArea = true;
1075
1076 FPRegs.push_back(CSI[i]);
1077
1078 if (Reg < MinFPR) {
1079 MinFPR = Reg;
1080 }
Craig Topperc9099502012-04-20 06:31:50 +00001081 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1082 PPC::CRRCRegClass.contains(Reg)) {
Roman Divacky9d760ae2012-09-12 14:47:47 +00001083 ; // do nothing, as we already know whether CRs are spilled
Craig Topperc9099502012-04-20 06:31:50 +00001084 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001085 HasVRSAVESaveArea = true;
Craig Topperc9099502012-04-20 06:31:50 +00001086 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001087 HasVRSaveArea = true;
1088
1089 VRegs.push_back(CSI[i]);
1090
1091 if (Reg < MinVR) {
1092 MinVR = Reg;
1093 }
1094 } else {
1095 llvm_unreachable("Unknown RegisterClass!");
1096 }
1097 }
1098
1099 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Hal Finkelaa6047d2013-03-26 20:08:20 +00001100 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001101
1102 int64_t LowerBound = 0;
1103
1104 // Take into account stack space reserved for tail calls.
1105 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001106 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1107 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001108 LowerBound = TCSPDelta;
1109 }
1110
1111 // The Floating-point register save area is right below the back chain word
1112 // of the previous stack frame.
1113 if (HasFPSaveArea) {
1114 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1115 int FI = FPRegs[i].getFrameIdx();
1116
1117 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1118 }
1119
Hal Finkelaa6047d2013-03-26 20:08:20 +00001120 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001121 }
1122
1123 // Check whether the frame pointer register is allocated. If so, make sure it
1124 // is spilled to the correct offset.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +00001125 if (needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001126 HasGPSaveArea = true;
1127
1128 int FI = PFI->getFramePointerSaveIndex();
1129 assert(FI && "No Frame Pointer Save Slot!");
1130
1131 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1132 }
1133
Hal Finkelfe47bf82013-07-17 00:45:52 +00001134 const PPCRegisterInfo *RegInfo =
1135 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
1136 if (RegInfo->hasBasePointer(MF)) {
1137 HasGPSaveArea = true;
1138
1139 int FI = PFI->getBasePointerSaveIndex();
1140 assert(FI && "No Base Pointer Save Slot!");
1141
1142 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1143 }
1144
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001145 // General register save area starts right below the Floating-point
1146 // register save area.
1147 if (HasGPSaveArea || HasG8SaveArea) {
1148 // Move general register save area spill slots down, taking into account
1149 // the size of the Floating-point register save area.
1150 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1151 int FI = GPRegs[i].getFrameIdx();
1152
1153 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1154 }
1155
1156 // Move general register save area spill slots down, taking into account
1157 // the size of the Floating-point register save area.
1158 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1159 int FI = G8Regs[i].getFrameIdx();
1160
1161 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1162 }
1163
1164 unsigned MinReg =
Hal Finkelaa6047d2013-03-26 20:08:20 +00001165 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1166 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001167
1168 if (Subtarget.isPPC64()) {
1169 LowerBound -= (31 - MinReg + 1) * 8;
1170 } else {
1171 LowerBound -= (31 - MinReg + 1) * 4;
1172 }
1173 }
1174
Roman Divacky9d760ae2012-09-12 14:47:47 +00001175 // For 32-bit only, the CR save area is below the general register
1176 // save area. For 64-bit SVR4, the CR save area is addressed relative
1177 // to the stack pointer and hence does not need an adjustment here.
1178 // Only CR2 (the first nonvolatile spilled) has an associated frame
1179 // index so that we have a single uniform save area.
1180 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001181 // Adjust the frame index of the CR spill slot.
1182 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1183 unsigned Reg = CSI[i].getReg();
1184
Roman Divacky9d760ae2012-09-12 14:47:47 +00001185 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
1186 // Leave Darwin logic as-is.
1187 || (!Subtarget.isSVR4ABI() &&
1188 (PPC::CRBITRCRegClass.contains(Reg) ||
1189 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001190 int FI = CSI[i].getFrameIdx();
1191
1192 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1193 }
1194 }
1195
1196 LowerBound -= 4; // The CR save area is always 4 bytes long.
1197 }
1198
1199 if (HasVRSAVESaveArea) {
1200 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1201 // which have the VRSAVE register class?
1202 // Adjust the frame index of the VRSAVE spill slot.
1203 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1204 unsigned Reg = CSI[i].getReg();
1205
Craig Topperc9099502012-04-20 06:31:50 +00001206 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001207 int FI = CSI[i].getFrameIdx();
1208
1209 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1210 }
1211 }
1212
1213 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1214 }
1215
1216 if (HasVRSaveArea) {
1217 // Insert alignment padding, we need 16-byte alignment.
1218 LowerBound = (LowerBound - 15) & ~(15);
1219
1220 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1221 int FI = VRegs[i].getFrameIdx();
1222
1223 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1224 }
1225 }
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001226
1227 addScavengingSpillSlot(MF, RS);
1228}
1229
1230void
1231PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1232 RegScavenger *RS) const {
1233 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1234 // a large stack, which will require scavenging a register to materialize a
1235 // large offset.
1236
1237 // We need to have a scavenger spill slot for spills if the frame size is
1238 // large. In case there is no free register for large-offset addressing,
1239 // this slot is used for the necessary emergency spill. Also, we need the
1240 // slot for dynamic stack allocations.
1241
1242 // The scavenger might be invoked if the frame offset does not fit into
1243 // the 16-bit immediate. We don't know the complete frame size here
1244 // because we've not yet computed callee-saved register spills or the
1245 // needed alignment padding.
1246 unsigned StackSize = determineFrameLayout(MF, false, true);
1247 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkel3f2c0472013-03-23 22:06:03 +00001248 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1249 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001250 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1251 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1252 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkeldc3beb92013-03-22 23:32:27 +00001253 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001254 RC->getAlignment(),
1255 false));
Hal Finkel01f99d22013-03-26 18:57:22 +00001256
Hal Finkelaad2a722013-07-18 04:28:21 +00001257 // Might we have over-aligned allocas?
1258 bool HasAlVars = MFI->hasVarSizedObjects() &&
1259 MFI->getMaxAlignment() > getStackAlignment();
1260
Hal Finkel01f99d22013-03-26 18:57:22 +00001261 // These kinds of spills might need two registers.
Hal Finkelaad2a722013-07-18 04:28:21 +00001262 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Hal Finkel01f99d22013-03-26 18:57:22 +00001263 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1264 RC->getAlignment(),
1265 false));
1266
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001267 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001268}
Roman Divacky9d760ae2012-09-12 14:47:47 +00001269
1270bool
1271PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1272 MachineBasicBlock::iterator MI,
1273 const std::vector<CalleeSavedInfo> &CSI,
1274 const TargetRegisterInfo *TRI) const {
1275
1276 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1277 // Return false otherwise to maintain pre-existing behavior.
1278 if (!Subtarget.isSVR4ABI())
1279 return false;
1280
1281 MachineFunction *MF = MBB.getParent();
1282 const PPCInstrInfo &TII =
1283 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1284 DebugLoc DL;
1285 bool CRSpilled = false;
Hal Finkel63496f62013-04-13 23:06:15 +00001286 MachineInstrBuilder CRMIB;
Roman Divacky9d760ae2012-09-12 14:47:47 +00001287
1288 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1289 unsigned Reg = CSI[i].getReg();
Hal Finkel6a636a82013-06-28 22:29:56 +00001290 // Only Darwin actually uses the VRSAVE register, but it can still appear
1291 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1292 // Darwin, ignore it.
1293 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1294 continue;
1295
Roman Divacky9d760ae2012-09-12 14:47:47 +00001296 // CR2 through CR4 are the nonvolatile CR fields.
1297 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1298
Roman Divacky9d760ae2012-09-12 14:47:47 +00001299 // Add the callee-saved register as live-in; it's killed at the spill.
1300 MBB.addLiveIn(Reg);
1301
Hal Finkel63496f62013-04-13 23:06:15 +00001302 if (CRSpilled && IsCRField) {
1303 CRMIB.addReg(Reg, RegState::ImplicitKill);
1304 continue;
1305 }
1306
Roman Divacky9d760ae2012-09-12 14:47:47 +00001307 // Insert the spill to the stack frame.
1308 if (IsCRField) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001309 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divacky9d760ae2012-09-12 14:47:47 +00001310 if (Subtarget.isPPC64()) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001311 // The actual spill will happen at the start of the prologue.
1312 FuncInfo->addMustSaveCR(Reg);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001313 } else {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001314 CRSpilled = true;
Bill Schmidtded53bf2013-05-14 16:08:32 +00001315 FuncInfo->setSpillsCR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001316
Roman Divacky9d760ae2012-09-12 14:47:47 +00001317 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1318 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
Hal Finkel63496f62013-04-13 23:06:15 +00001319 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
1320 .addReg(Reg, RegState::ImplicitKill);
1321
1322 MBB.insert(MI, CRMIB);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001323 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1324 .addReg(PPC::R12,
1325 getKillRegState(true)),
1326 CSI[i].getFrameIdx()));
1327 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001328 } else {
1329 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1330 TII.storeRegToStackSlot(MBB, MI, Reg, true,
1331 CSI[i].getFrameIdx(), RC, TRI);
1332 }
1333 }
1334 return true;
1335}
1336
1337static void
Hal Finkelb99c9952013-04-13 08:09:20 +00001338restoreCRs(bool isPPC64, bool is31,
1339 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001340 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1341 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1342
1343 MachineFunction *MF = MBB.getParent();
1344 const PPCInstrInfo &TII =
1345 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1346 DebugLoc DL;
1347 unsigned RestoreOp, MoveReg;
1348
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001349 if (isPPC64)
1350 // This is handled during epilogue generation.
1351 return;
1352 else {
Roman Divacky9d760ae2012-09-12 14:47:47 +00001353 // 32-bit: FP-relative
1354 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
1355 PPC::R12),
1356 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand33efedc2013-07-03 17:59:07 +00001357 RestoreOp = PPC::MTOCRF;
Roman Divacky9d760ae2012-09-12 14:47:47 +00001358 MoveReg = PPC::R12;
1359 }
1360
1361 if (CR2Spilled)
1362 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkeld957f952013-03-28 03:38:16 +00001363 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001364
1365 if (CR3Spilled)
1366 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkeld957f952013-03-28 03:38:16 +00001367 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001368
1369 if (CR4Spilled)
1370 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkeld957f952013-03-28 03:38:16 +00001371 .addReg(MoveReg, getKillRegState(true)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001372}
1373
Eli Bendersky700ed802013-02-21 20:05:00 +00001374void PPCFrameLowering::
1375eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1376 MachineBasicBlock::iterator I) const {
1377 const PPCInstrInfo &TII =
1378 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1379 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1380 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1381 // Add (actually subtract) back the amount the callee popped on return.
1382 if (int CalleeAmt = I->getOperand(1).getImm()) {
1383 bool is64Bit = Subtarget.isPPC64();
1384 CalleeAmt *= -1;
1385 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1386 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1387 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1388 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1389 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1390 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1391 MachineInstr *MI = I;
1392 DebugLoc dl = MI->getDebugLoc();
1393
1394 if (isInt<16>(CalleeAmt)) {
1395 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1396 .addReg(StackReg, RegState::Kill)
1397 .addImm(CalleeAmt);
1398 } else {
1399 MachineBasicBlock::iterator MBBI = I;
1400 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1401 .addImm(CalleeAmt >> 16);
1402 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1403 .addReg(TmpReg, RegState::Kill)
1404 .addImm(CalleeAmt & 0xFFFF);
1405 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1406 .addReg(StackReg, RegState::Kill)
1407 .addReg(TmpReg);
1408 }
1409 }
1410 }
1411 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1412 MBB.erase(I);
1413}
1414
Roman Divacky9d760ae2012-09-12 14:47:47 +00001415bool
1416PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1417 MachineBasicBlock::iterator MI,
1418 const std::vector<CalleeSavedInfo> &CSI,
1419 const TargetRegisterInfo *TRI) const {
1420
1421 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1422 // Return false otherwise to maintain pre-existing behavior.
1423 if (!Subtarget.isSVR4ABI())
1424 return false;
1425
1426 MachineFunction *MF = MBB.getParent();
1427 const PPCInstrInfo &TII =
1428 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1429 bool CR2Spilled = false;
1430 bool CR3Spilled = false;
1431 bool CR4Spilled = false;
1432 unsigned CSIIndex = 0;
1433
1434 // Initialize insertion-point logic; we will be restoring in reverse
1435 // order of spill.
1436 MachineBasicBlock::iterator I = MI, BeforeI = I;
1437 bool AtStart = I == MBB.begin();
1438
1439 if (!AtStart)
1440 --BeforeI;
1441
1442 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1443 unsigned Reg = CSI[i].getReg();
1444
Hal Finkel6a636a82013-06-28 22:29:56 +00001445 // Only Darwin actually uses the VRSAVE register, but it can still appear
1446 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1447 // Darwin, ignore it.
1448 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1449 continue;
1450
Roman Divacky9d760ae2012-09-12 14:47:47 +00001451 if (Reg == PPC::CR2) {
1452 CR2Spilled = true;
1453 // The spill slot is associated only with CR2, which is the
1454 // first nonvolatile spilled. Save it here.
1455 CSIIndex = i;
1456 continue;
1457 } else if (Reg == PPC::CR3) {
1458 CR3Spilled = true;
1459 continue;
1460 } else if (Reg == PPC::CR4) {
1461 CR4Spilled = true;
1462 continue;
1463 } else {
1464 // When we first encounter a non-CR register after seeing at
1465 // least one CR register, restore all spilled CRs together.
1466 if ((CR2Spilled || CR3Spilled || CR4Spilled)
1467 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkelb99c9952013-04-13 08:09:20 +00001468 bool is31 = needsFP(*MF);
1469 restoreCRs(Subtarget.isPPC64(), is31,
1470 CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001471 MBB, I, CSI, CSIIndex);
1472 CR2Spilled = CR3Spilled = CR4Spilled = false;
1473 }
1474
1475 // Default behavior for non-CR saves.
1476 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1477 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
1478 RC, TRI);
1479 assert(I != MBB.begin() &&
1480 "loadRegFromStackSlot didn't insert any code!");
1481 }
1482
1483 // Insert in reverse order.
1484 if (AtStart)
1485 I = MBB.begin();
1486 else {
1487 I = BeforeI;
1488 ++I;
1489 }
1490 }
1491
1492 // If we haven't yet spilled the CRs, do so now.
Hal Finkelb99c9952013-04-13 08:09:20 +00001493 if (CR2Spilled || CR3Spilled || CR4Spilled) {
1494 bool is31 = needsFP(*MF);
1495 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001496 MBB, I, CSI, CSIIndex);
Hal Finkelb99c9952013-04-13 08:09:20 +00001497 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001498
1499 return true;
1500}
1501