blob: 0f8da5f046b672f76897de2960870000b5191af7 [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
29// FIXME This disables some code that aligns the stack to a boundary bigger than
30// the default (16 bytes on Darwin) when there is a stack local of greater
31// alignment. This does not currently work, because the delta between old and
32// new stack pointers is added to offsets that reference incoming parameters
33// after the prolog is generated, and the code that does that doesn't handle a
34// variable delta. You don't want to do that anyway; a better approach is to
35// reserve another register that retains to the incoming stack pointer, and
36// reference parameters relative to that.
37#define ALIGN_STACK 0
38
39
40/// VRRegNo - Map from a numbered VR register to its enum value.
41///
Craig Topperb78ca422012-03-11 07:16:55 +000042static const uint16_t VRRegNo[] = {
Anton Korobeynikov33464912010-11-15 00:06:54 +000043 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
44 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
45 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
46 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
47};
48
49/// RemoveVRSaveCode - We have found that this function does not need any code
50/// to manipulate the VRSAVE register, even though it uses vector registers.
51/// This can happen when the only registers used are known to be live in or out
52/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidta5d0ab52012-10-10 20:54:15 +000053/// FIXME: The removal of the code results in a compile failure at -O0 when the
54/// function contains a function call, as the GPR containing original VRSAVE
55/// contents is spilled and reloaded around the call. Without the prolog code,
56/// the spill instruction refers to an undefined register. This code needs
57/// to account for all uses of that GPR.
Anton Korobeynikov33464912010-11-15 00:06:54 +000058static void RemoveVRSaveCode(MachineInstr *MI) {
59 MachineBasicBlock *Entry = MI->getParent();
60 MachineFunction *MF = Entry->getParent();
61
62 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
63 MachineBasicBlock::iterator MBBI = MI;
64 ++MBBI;
65 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
66 MBBI->eraseFromParent();
67
68 bool RemovedAllMTVRSAVEs = true;
69 // See if we can find and remove the MTVRSAVE instruction from all of the
70 // epilog blocks.
71 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
72 // If last instruction is a return instruction, add an epilogue
Evan Cheng5a96b3d2011-12-07 07:15:52 +000073 if (!I->empty() && I->back().isReturn()) {
Anton Korobeynikov33464912010-11-15 00:06:54 +000074 bool FoundIt = false;
75 for (MBBI = I->end(); MBBI != I->begin(); ) {
76 --MBBI;
77 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
78 MBBI->eraseFromParent(); // remove it.
79 FoundIt = true;
80 break;
81 }
82 }
83 RemovedAllMTVRSAVEs &= FoundIt;
84 }
85 }
86
87 // If we found and removed all MTVRSAVE instructions, remove the read of
88 // VRSAVE as well.
89 if (RemovedAllMTVRSAVEs) {
90 MBBI = MI;
91 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
92 --MBBI;
93 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
94 MBBI->eraseFromParent();
95 }
96
97 // Finally, nuke the UPDATE_VRSAVE.
98 MI->eraseFromParent();
99}
100
101// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
102// instruction selector. Based on the vector registers that have been used,
103// transform this into the appropriate ORI instruction.
104static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
105 MachineFunction *MF = MI->getParent()->getParent();
106 DebugLoc dl = MI->getDebugLoc();
107
108 unsigned UsedRegMask = 0;
109 for (unsigned i = 0; i != 32; ++i)
110 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
111 UsedRegMask |= 1 << (31-i);
112
113 // Live in and live out values already must be in the mask, so don't bother
114 // marking them.
115 for (MachineRegisterInfo::livein_iterator
116 I = MF->getRegInfo().livein_begin(),
117 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Evan Cheng966aeb52011-07-25 19:53:23 +0000118 unsigned RegNo = getPPCRegisterNumbering(I->first);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000119 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
120 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
121 }
Jakob Stoklund Olesen0a9d1d32013-02-05 17:40:36 +0000122
123 // Live out registers appear as use operands on return instructions.
124 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
125 UsedRegMask != 0 && BI != BE; ++BI) {
126 const MachineBasicBlock &MBB = *BI;
127 if (MBB.empty() || !MBB.back().isReturn())
128 continue;
129 const MachineInstr &Ret = MBB.back();
130 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
131 const MachineOperand &MO = Ret.getOperand(I);
132 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
133 continue;
134 unsigned RegNo = getPPCRegisterNumbering(MO.getReg());
135 UsedRegMask &= ~(1 << (31-RegNo));
136 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000137 }
138
139 // If no registers are used, turn this into a copy.
140 if (UsedRegMask == 0) {
141 // Remove all VRSAVE code.
142 RemoveVRSaveCode(MI);
143 return;
144 }
145
146 unsigned SrcReg = MI->getOperand(1).getReg();
147 unsigned DstReg = MI->getOperand(0).getReg();
148
149 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
150 if (DstReg != SrcReg)
151 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
152 .addReg(SrcReg)
153 .addImm(UsedRegMask);
154 else
155 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
156 .addReg(SrcReg, RegState::Kill)
157 .addImm(UsedRegMask);
158 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
159 if (DstReg != SrcReg)
160 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
161 .addReg(SrcReg)
162 .addImm(UsedRegMask >> 16);
163 else
164 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
165 .addReg(SrcReg, RegState::Kill)
166 .addImm(UsedRegMask >> 16);
167 } else {
168 if (DstReg != SrcReg)
169 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
170 .addReg(SrcReg)
171 .addImm(UsedRegMask >> 16);
172 else
173 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
174 .addReg(SrcReg, RegState::Kill)
175 .addImm(UsedRegMask >> 16);
176
177 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
178 .addReg(DstReg, RegState::Kill)
179 .addImm(UsedRegMask & 0xFFFF);
180 }
181
182 // Remove the old UPDATE_VRSAVE instruction.
183 MI->eraseFromParent();
184}
185
Roman Divacky9d760ae2012-09-12 14:47:47 +0000186static bool spillsCR(const MachineFunction &MF) {
187 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
188 return FuncInfo->isCRSpilled();
189}
190
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000191static bool hasSpills(const MachineFunction &MF) {
192 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
193 return FuncInfo->hasSpills();
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
207 // Get the alignments provided by the target, and the maximum alignment
208 // (if any) of the fixed frame objects.
209 unsigned MaxAlign = MFI->getMaxAlignment();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000210 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000211 unsigned AlignMask = TargetAlign - 1; //
212
213 // If we are a leaf function, and use up to 224 bytes of stack space,
214 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Roman Divacky9d760ae2012-09-12 14:47:47 +0000215 // to adjust the stack pointer (we fit in the Red Zone). For 64-bit
216 // SVR4, we also require a stack frame if we need to spill the CR,
217 // since this spill area is addressed relative to the stack pointer.
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.
Roman Divacky9d760ae2012-09-12 14:47:47 +0000229 !(Subtarget.isPPC64() && // No 64-bit SVR4 CRsave.
230 Subtarget.isSVR4ABI()
231 && spillsCR(MF)) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000232 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment.
233 // No need for frame
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000234 if (UpdateMF)
235 MFI->setStackSize(0);
236 return 0;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000237 }
238
239 // Get the maximum call frame size of all the calls.
240 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
241
242 // Maximum call frame needs to be at least big enough for linkage and 8 args.
243 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
244 Subtarget.isDarwinABI());
245 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
246
247 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
248 // that allocations will be aligned.
249 if (MFI->hasVarSizedObjects())
250 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
251
252 // Update maximum call frame size.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000253 if (UpdateMF)
254 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000255
256 // Include call frame size in total.
257 FrameSize += maxCallFrameSize;
258
259 // Make sure the frame is aligned.
260 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
261
262 // Update frame info.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000263 if (UpdateMF)
264 MFI->setStackSize(FrameSize);
265
266 return FrameSize;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000267}
268
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000269// hasFP - Return true if the specified function actually has a dedicated frame
270// pointer register.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000271bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000272 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000273 // FIXME: This is pretty much broken by design: hasFP() might be called really
274 // early, before the stack layout was calculated and thus hasFP() might return
275 // true or false here depending on the time of call.
276 return (MFI->getStackSize()) && needsFP(MF);
277}
278
279// needsFP - Return true if the specified function should have a dedicated frame
280// pointer register. This is true if the function has variable sized allocas or
281// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000282bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000283 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000284
285 // Naked functions have no stack frame pushed, so we don't have a frame
286 // pointer.
Bill Wendling831737d2012-12-30 10:32:01 +0000287 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
288 Attribute::Naked))
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000289 return false;
290
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000291 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
292 MFI->hasVarSizedObjects() ||
293 (MF.getTarget().Options.GuaranteedTailCallOpt &&
294 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000295}
296
297
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000298void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000299 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
300 MachineBasicBlock::iterator MBBI = MBB.begin();
301 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000302 const PPCInstrInfo &TII =
303 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
304
305 MachineModuleInfo &MMI = MF.getMMI();
306 DebugLoc dl;
307 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000308 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000309
310 // Prepare for frame info.
311 MCSymbol *FrameLabel = 0;
312
313 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
314 // process it.
Bill Schmidta5d0ab52012-10-10 20:54:15 +0000315 if (!Subtarget.isSVR4ABI())
316 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
317 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
318 HandleVRSaveUpdate(MBBI, TII);
319 break;
320 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000321 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000322
323 // Move MBBI back to the beginning of the function.
324 MBBI = MBB.begin();
325
326 // Work out frame sizes.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000327 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000328 int NegFrameSize = -FrameSize;
329
330 // Get processor type.
331 bool isPPC64 = Subtarget.isPPC64();
332 // Get operating system
333 bool isDarwinABI = Subtarget.isDarwinABI();
334 // Check if the link register (LR) must be saved.
335 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
336 bool MustSaveLR = FI->mustSaveLR();
337 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000338 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000339
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000340 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000341
342 int FPOffset = 0;
343 if (HasFP) {
344 if (Subtarget.isSVR4ABI()) {
345 MachineFrameInfo *FFI = MF.getFrameInfo();
346 int FPIndex = FI->getFramePointerSaveIndex();
347 assert(FPIndex && "No Frame Pointer Save Slot!");
348 FPOffset = FFI->getObjectOffset(FPIndex);
349 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000350 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000351 }
352 }
353
354 if (isPPC64) {
355 if (MustSaveLR)
356 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
357
358 if (HasFP)
359 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
360 .addReg(PPC::X31)
361 .addImm(FPOffset/4)
362 .addReg(PPC::X1);
363
364 if (MustSaveLR)
365 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
366 .addReg(PPC::X0)
367 .addImm(LROffset / 4)
368 .addReg(PPC::X1);
369 } else {
370 if (MustSaveLR)
371 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
372
373 if (HasFP)
Hal Finkelb8f2f292012-05-19 21:52:55 +0000374 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative
375 // offsets of R1 is not allowed.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000376 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
377 .addReg(PPC::R31)
378 .addImm(FPOffset)
379 .addReg(PPC::R1);
380
381 if (MustSaveLR)
382 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
383 .addReg(PPC::R0)
384 .addImm(LROffset)
385 .addReg(PPC::R1);
386 }
387
388 // Skip if a leaf routine.
389 if (!FrameSize) return;
390
391 // Get stack alignments.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000392 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000393 unsigned MaxAlign = MFI->getMaxAlignment();
394
395 // Adjust stack pointer: r1 += NegFrameSize.
396 // If there is a preferred stack alignment, align R1 now
397 if (!isPPC64) {
398 // PPC32.
399 if (ALIGN_STACK && MaxAlign > TargetAlign) {
400 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
401 "Invalid alignment!");
402 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
403
404 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
405 .addReg(PPC::R1)
406 .addImm(0)
407 .addImm(32 - Log2_32(MaxAlign))
408 .addImm(31);
409 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0)
410 .addReg(PPC::R0, RegState::Kill)
411 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000412 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000413 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000414 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000415 .addReg(PPC::R0);
416 } else if (isInt<16>(NegFrameSize)) {
417 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
418 .addReg(PPC::R1)
419 .addImm(NegFrameSize)
420 .addReg(PPC::R1);
421 } else {
422 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
423 .addImm(NegFrameSize >> 16);
424 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
425 .addReg(PPC::R0, RegState::Kill)
426 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000427 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000428 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000429 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000430 .addReg(PPC::R0);
431 }
432 } else { // PPC64.
433 if (ALIGN_STACK && MaxAlign > TargetAlign) {
434 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
435 "Invalid alignment!");
436 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
437
438 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
439 .addReg(PPC::X1)
440 .addImm(0)
441 .addImm(64 - Log2_32(MaxAlign));
442 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
443 .addReg(PPC::X0)
444 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000445 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000446 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000447 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000448 .addReg(PPC::X0);
449 } else if (isInt<16>(NegFrameSize)) {
450 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
451 .addReg(PPC::X1)
452 .addImm(NegFrameSize / 4)
453 .addReg(PPC::X1);
454 } else {
455 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
456 .addImm(NegFrameSize >> 16);
457 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
458 .addReg(PPC::X0, RegState::Kill)
459 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000460 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000461 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000462 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000463 .addReg(PPC::X0);
464 }
465 }
466
467 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
468
469 // Add the "machine moves" for the instructions we generated above, but in
470 // reverse order.
471 if (needsFrameMoves) {
472 // Mark effective beginning of when frame pointer becomes valid.
473 FrameLabel = MMI.getContext().CreateTempSymbol();
474 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
475
476 // Show update of SP.
477 if (NegFrameSize) {
478 MachineLocation SPDst(MachineLocation::VirtualFP);
479 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
480 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc));
481 } else {
482 MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31);
483 Moves.push_back(MachineMove(FrameLabel, SP, SP));
484 }
485
486 if (HasFP) {
487 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
488 MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31);
489 Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc));
490 }
491
492 if (MustSaveLR) {
493 MachineLocation LRDst(MachineLocation::VirtualFP, LROffset);
494 MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR);
495 Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc));
496 }
497 }
498
499 MCSymbol *ReadyLabel = 0;
500
501 // If there is a frame pointer, copy R1 into R31
502 if (HasFP) {
503 if (!isPPC64) {
504 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
505 .addReg(PPC::R1)
506 .addReg(PPC::R1);
507 } else {
508 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
509 .addReg(PPC::X1)
510 .addReg(PPC::X1);
511 }
512
513 if (needsFrameMoves) {
514 ReadyLabel = MMI.getContext().CreateTempSymbol();
515
516 // Mark effective beginning of when frame pointer is ready.
517 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
518
519 MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) :
520 (isPPC64 ? PPC::X1 : PPC::R1));
521 MachineLocation FPSrc(MachineLocation::VirtualFP);
522 Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc));
523 }
524 }
525
526 if (needsFrameMoves) {
527 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
528
529 // Add callee saved registers to move list.
530 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
531 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000532 unsigned Reg = CSI[I].getReg();
533 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000534
535 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
536 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperc9099502012-04-20 06:31:50 +0000537 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola6e032942011-05-30 20:20:15 +0000538 continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000539
Roman Divacky9d760ae2012-09-12 14:47:47 +0000540 // For SVR4, don't emit a move for the CR spill slot if we haven't
541 // spilled CRs.
542 if (Subtarget.isSVR4ABI()
543 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
544 && !spillsCR(MF))
545 continue;
546
547 // For 64-bit SVR4 when we have spilled CRs, the spill location
548 // is SP+8, not a frame-relative slot.
549 if (Subtarget.isSVR4ABI()
550 && Subtarget.isPPC64()
551 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
552 MachineLocation CSDst(PPC::X1, 8);
553 MachineLocation CSSrc(PPC::CR2);
554 Moves.push_back(MachineMove(Label, CSDst, CSSrc));
555 continue;
556 }
557
558 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000559 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
560 MachineLocation CSSrc(Reg);
561 Moves.push_back(MachineMove(Label, CSDst, CSSrc));
562 }
563 }
564}
565
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000566void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000567 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000568 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
569 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000570 const PPCInstrInfo &TII =
571 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
572
573 unsigned RetOpcode = MBBI->getOpcode();
574 DebugLoc dl;
575
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000576 assert((RetOpcode == PPC::BLR ||
577 RetOpcode == PPC::TCRETURNri ||
578 RetOpcode == PPC::TCRETURNdi ||
579 RetOpcode == PPC::TCRETURNai ||
580 RetOpcode == PPC::TCRETURNri8 ||
581 RetOpcode == PPC::TCRETURNdi8 ||
582 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000583 "Can only insert epilog into returning blocks");
584
585 // Get alignment info so we know how to restore r1
586 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000587 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000588 unsigned MaxAlign = MFI->getMaxAlignment();
589
590 // Get the number of bytes allocated from the FrameInfo.
591 int FrameSize = MFI->getStackSize();
592
593 // Get processor type.
594 bool isPPC64 = Subtarget.isPPC64();
595 // Get operating system
596 bool isDarwinABI = Subtarget.isDarwinABI();
597 // Check if the link register (LR) has been saved.
598 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
599 bool MustSaveLR = FI->mustSaveLR();
600 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000601 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000602
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000603 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000604
605 int FPOffset = 0;
606 if (HasFP) {
607 if (Subtarget.isSVR4ABI()) {
608 MachineFrameInfo *FFI = MF.getFrameInfo();
609 int FPIndex = FI->getFramePointerSaveIndex();
610 assert(FPIndex && "No Frame Pointer Save Slot!");
611 FPOffset = FFI->getObjectOffset(FPIndex);
612 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000613 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000614 }
615 }
616
617 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
618 RetOpcode == PPC::TCRETURNdi ||
619 RetOpcode == PPC::TCRETURNai ||
620 RetOpcode == PPC::TCRETURNri8 ||
621 RetOpcode == PPC::TCRETURNdi8 ||
622 RetOpcode == PPC::TCRETURNai8;
623
624 if (UsesTCRet) {
625 int MaxTCRetDelta = FI->getTailCallSPDelta();
626 MachineOperand &StackAdjust = MBBI->getOperand(1);
627 assert(StackAdjust.isImm() && "Expecting immediate value.");
628 // Adjust stack pointer.
629 int StackAdj = StackAdjust.getImm();
630 int Delta = StackAdj - MaxTCRetDelta;
631 assert((Delta >= 0) && "Delta must be positive");
632 if (MaxTCRetDelta>0)
633 FrameSize += (StackAdj +Delta);
634 else
635 FrameSize += StackAdj;
636 }
637
638 if (FrameSize) {
639 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
640 // on entry to the function. Add this offset back now.
641 if (!isPPC64) {
642 // If this function contained a fastcc call and GuaranteedTailCallOpt is
643 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
644 // call which invalidates the stack pointer value in SP(0). So we use the
645 // value of R31 in this case.
646 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000647 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000648 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
649 .addReg(PPC::R31).addImm(FrameSize);
650 } else if(FI->hasFastCall()) {
651 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
652 .addImm(FrameSize >> 16);
653 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
654 .addReg(PPC::R0, RegState::Kill)
655 .addImm(FrameSize & 0xFFFF);
656 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
657 .addReg(PPC::R1)
658 .addReg(PPC::R31)
659 .addReg(PPC::R0);
660 } else if (isInt<16>(FrameSize) &&
661 (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
662 !MFI->hasVarSizedObjects()) {
663 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
664 .addReg(PPC::R1).addImm(FrameSize);
665 } else {
666 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
667 .addImm(0).addReg(PPC::R1);
668 }
669 } else {
670 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000671 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000672 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
673 .addReg(PPC::X31).addImm(FrameSize);
674 } else if(FI->hasFastCall()) {
675 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
676 .addImm(FrameSize >> 16);
677 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
678 .addReg(PPC::X0, RegState::Kill)
679 .addImm(FrameSize & 0xFFFF);
680 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
681 .addReg(PPC::X1)
682 .addReg(PPC::X31)
683 .addReg(PPC::X0);
684 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign &&
685 !MFI->hasVarSizedObjects()) {
686 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
687 .addReg(PPC::X1).addImm(FrameSize);
688 } else {
689 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
690 .addImm(0).addReg(PPC::X1);
691 }
692 }
693 }
694
695 if (isPPC64) {
696 if (MustSaveLR)
697 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
698 .addImm(LROffset/4).addReg(PPC::X1);
699
700 if (HasFP)
701 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
702 .addImm(FPOffset/4).addReg(PPC::X1);
703
704 if (MustSaveLR)
705 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
706 } else {
707 if (MustSaveLR)
708 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
709 .addImm(LROffset).addReg(PPC::R1);
710
711 if (HasFP)
712 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
713 .addImm(FPOffset).addReg(PPC::R1);
714
715 if (MustSaveLR)
716 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
717 }
718
719 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
720 // call optimization
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000721 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000722 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
723 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
724 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
725 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
726 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
727 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
728 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
729 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
730 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
731 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
732
733 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
734 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
735 .addReg(StackReg).addImm(CallerAllocatedAmt);
736 } else {
737 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
738 .addImm(CallerAllocatedAmt >> 16);
739 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
740 .addReg(TmpReg, RegState::Kill)
741 .addImm(CallerAllocatedAmt & 0xFFFF);
742 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
743 .addReg(StackReg)
744 .addReg(FPReg)
745 .addReg(TmpReg);
746 }
747 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000748 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000749 MachineOperand &JumpTarget = MBBI->getOperand(0);
750 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
751 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
752 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000753 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000754 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
755 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
756 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000757 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000758 MachineOperand &JumpTarget = MBBI->getOperand(0);
759 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
760 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000761 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000762 MachineOperand &JumpTarget = MBBI->getOperand(0);
763 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
764 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
765 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000766 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000767 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
768 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
769 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000770 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000771 MachineOperand &JumpTarget = MBBI->getOperand(0);
772 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
773 }
774}
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000775
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000776/// MustSaveLR - Return true if this function requires that we save the LR
777/// register onto the stack in the prolog and restore it in the epilog of the
778/// function.
779static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
780 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
781
782 // We need a save/restore of LR if there is any def of LR (which is
783 // defined by calls, including the PIC setup sequence), or if there is
784 // some use of the LR stack slot (e.g. for builtin_return_address).
785 // (LR comes in 32 and 64 bit versions.)
786 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
787 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
788}
789
790void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000791PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000792 RegScavenger *) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000793 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
794
795 // Save and clear the LR state.
796 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
797 unsigned LR = RegInfo->getRARegister();
798 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000799 MachineRegisterInfo &MRI = MF.getRegInfo();
800 MRI.setPhysRegUnused(LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000801
802 // Save R31 if necessary
803 int FPSI = FI->getFramePointerSaveIndex();
804 bool isPPC64 = Subtarget.isPPC64();
805 bool isDarwinABI = Subtarget.isDarwinABI();
806 MachineFrameInfo *MFI = MF.getFrameInfo();
807
808 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000809 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000810 // Find out what the fix offset of the frame pointer save area.
811 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
812 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000813 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000814 // Save the result.
815 FI->setFramePointerSaveIndex(FPSI);
816 }
817
818 // Reserve stack space to move the linkage area to in case of a tail call.
819 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000820 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
821 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000822 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000823 }
824
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000825 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
826 // function uses CR 2, 3, or 4.
827 if (!isPPC64 && !isDarwinABI &&
828 (MRI.isPhysRegUsed(PPC::CR2) ||
829 MRI.isPhysRegUsed(PPC::CR3) ||
830 MRI.isPhysRegUsed(PPC::CR4))) {
831 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
832 FI->setCRSpillFrameIndex(FrameIdx);
833 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000834}
835
Hal Finkel3080d232013-03-14 20:33:40 +0000836void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000837 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000838 // Early exit if not using the SVR4 ABI.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000839 if (!Subtarget.isSVR4ABI()) {
840 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000841 return;
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000842 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000843
844 // Get callee saved register information.
845 MachineFrameInfo *FFI = MF.getFrameInfo();
846 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
847
848 // Early exit if no callee saved registers are modified!
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000849 if (CSI.empty() && !needsFP(MF)) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000850 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000851 return;
852 }
853
854 unsigned MinGPR = PPC::R31;
855 unsigned MinG8R = PPC::X31;
856 unsigned MinFPR = PPC::F31;
857 unsigned MinVR = PPC::V31;
858
859 bool HasGPSaveArea = false;
860 bool HasG8SaveArea = false;
861 bool HasFPSaveArea = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000862 bool HasVRSAVESaveArea = false;
863 bool HasVRSaveArea = false;
864
865 SmallVector<CalleeSavedInfo, 18> GPRegs;
866 SmallVector<CalleeSavedInfo, 18> G8Regs;
867 SmallVector<CalleeSavedInfo, 18> FPRegs;
868 SmallVector<CalleeSavedInfo, 18> VRegs;
869
870 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
871 unsigned Reg = CSI[i].getReg();
Craig Topperc9099502012-04-20 06:31:50 +0000872 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000873 HasGPSaveArea = true;
874
875 GPRegs.push_back(CSI[i]);
876
877 if (Reg < MinGPR) {
878 MinGPR = Reg;
879 }
Craig Topperc9099502012-04-20 06:31:50 +0000880 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000881 HasG8SaveArea = true;
882
883 G8Regs.push_back(CSI[i]);
884
885 if (Reg < MinG8R) {
886 MinG8R = Reg;
887 }
Craig Topperc9099502012-04-20 06:31:50 +0000888 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000889 HasFPSaveArea = true;
890
891 FPRegs.push_back(CSI[i]);
892
893 if (Reg < MinFPR) {
894 MinFPR = Reg;
895 }
Craig Topperc9099502012-04-20 06:31:50 +0000896 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
897 PPC::CRRCRegClass.contains(Reg)) {
Roman Divacky9d760ae2012-09-12 14:47:47 +0000898 ; // do nothing, as we already know whether CRs are spilled
Craig Topperc9099502012-04-20 06:31:50 +0000899 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000900 HasVRSAVESaveArea = true;
Craig Topperc9099502012-04-20 06:31:50 +0000901 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000902 HasVRSaveArea = true;
903
904 VRegs.push_back(CSI[i]);
905
906 if (Reg < MinVR) {
907 MinVR = Reg;
908 }
909 } else {
910 llvm_unreachable("Unknown RegisterClass!");
911 }
912 }
913
914 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
915
916 int64_t LowerBound = 0;
917
918 // Take into account stack space reserved for tail calls.
919 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000920 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
921 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000922 LowerBound = TCSPDelta;
923 }
924
925 // The Floating-point register save area is right below the back chain word
926 // of the previous stack frame.
927 if (HasFPSaveArea) {
928 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
929 int FI = FPRegs[i].getFrameIdx();
930
931 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
932 }
933
Evan Cheng966aeb52011-07-25 19:53:23 +0000934 LowerBound -= (31 - getPPCRegisterNumbering(MinFPR) + 1) * 8;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000935 }
936
937 // Check whether the frame pointer register is allocated. If so, make sure it
938 // is spilled to the correct offset.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000939 if (needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000940 HasGPSaveArea = true;
941
942 int FI = PFI->getFramePointerSaveIndex();
943 assert(FI && "No Frame Pointer Save Slot!");
944
945 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
946 }
947
948 // General register save area starts right below the Floating-point
949 // register save area.
950 if (HasGPSaveArea || HasG8SaveArea) {
951 // Move general register save area spill slots down, taking into account
952 // the size of the Floating-point register save area.
953 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
954 int FI = GPRegs[i].getFrameIdx();
955
956 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
957 }
958
959 // Move general register save area spill slots down, taking into account
960 // the size of the Floating-point register save area.
961 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
962 int FI = G8Regs[i].getFrameIdx();
963
964 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
965 }
966
967 unsigned MinReg =
Evan Cheng966aeb52011-07-25 19:53:23 +0000968 std::min<unsigned>(getPPCRegisterNumbering(MinGPR),
969 getPPCRegisterNumbering(MinG8R));
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000970
971 if (Subtarget.isPPC64()) {
972 LowerBound -= (31 - MinReg + 1) * 8;
973 } else {
974 LowerBound -= (31 - MinReg + 1) * 4;
975 }
976 }
977
Roman Divacky9d760ae2012-09-12 14:47:47 +0000978 // For 32-bit only, the CR save area is below the general register
979 // save area. For 64-bit SVR4, the CR save area is addressed relative
980 // to the stack pointer and hence does not need an adjustment here.
981 // Only CR2 (the first nonvolatile spilled) has an associated frame
982 // index so that we have a single uniform save area.
983 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000984 // Adjust the frame index of the CR spill slot.
985 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
986 unsigned Reg = CSI[i].getReg();
987
Roman Divacky9d760ae2012-09-12 14:47:47 +0000988 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
989 // Leave Darwin logic as-is.
990 || (!Subtarget.isSVR4ABI() &&
991 (PPC::CRBITRCRegClass.contains(Reg) ||
992 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000993 int FI = CSI[i].getFrameIdx();
994
995 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
996 }
997 }
998
999 LowerBound -= 4; // The CR save area is always 4 bytes long.
1000 }
1001
1002 if (HasVRSAVESaveArea) {
1003 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1004 // which have the VRSAVE register class?
1005 // Adjust the frame index of the VRSAVE spill slot.
1006 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1007 unsigned Reg = CSI[i].getReg();
1008
Craig Topperc9099502012-04-20 06:31:50 +00001009 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001010 int FI = CSI[i].getFrameIdx();
1011
1012 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1013 }
1014 }
1015
1016 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1017 }
1018
1019 if (HasVRSaveArea) {
1020 // Insert alignment padding, we need 16-byte alignment.
1021 LowerBound = (LowerBound - 15) & ~(15);
1022
1023 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1024 int FI = VRegs[i].getFrameIdx();
1025
1026 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1027 }
1028 }
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001029
1030 addScavengingSpillSlot(MF, RS);
1031}
1032
1033void
1034PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1035 RegScavenger *RS) const {
1036 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1037 // a large stack, which will require scavenging a register to materialize a
1038 // large offset.
1039
1040 // We need to have a scavenger spill slot for spills if the frame size is
1041 // large. In case there is no free register for large-offset addressing,
1042 // this slot is used for the necessary emergency spill. Also, we need the
1043 // slot for dynamic stack allocations.
1044
1045 // The scavenger might be invoked if the frame offset does not fit into
1046 // the 16-bit immediate. We don't know the complete frame size here
1047 // because we've not yet computed callee-saved register spills or the
1048 // needed alignment padding.
1049 unsigned StackSize = determineFrameLayout(MF, false, true);
1050 MachineFrameInfo *MFI = MF.getFrameInfo();
1051 if (MFI->hasVarSizedObjects() || spillsCR(MF) ||
1052 (hasSpills(MF) && !isInt<16>(StackSize))) {
1053 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1054 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1055 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
1056 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1057 RC->getAlignment(),
1058 false));
1059 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001060}
Roman Divacky9d760ae2012-09-12 14:47:47 +00001061
1062bool
1063PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1064 MachineBasicBlock::iterator MI,
1065 const std::vector<CalleeSavedInfo> &CSI,
1066 const TargetRegisterInfo *TRI) const {
1067
1068 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1069 // Return false otherwise to maintain pre-existing behavior.
1070 if (!Subtarget.isSVR4ABI())
1071 return false;
1072
1073 MachineFunction *MF = MBB.getParent();
1074 const PPCInstrInfo &TII =
1075 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1076 DebugLoc DL;
1077 bool CRSpilled = false;
1078
1079 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1080 unsigned Reg = CSI[i].getReg();
1081 // CR2 through CR4 are the nonvolatile CR fields.
1082 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1083
1084 if (CRSpilled && IsCRField)
1085 continue;
1086
1087 // Add the callee-saved register as live-in; it's killed at the spill.
1088 MBB.addLiveIn(Reg);
1089
1090 // Insert the spill to the stack frame.
1091 if (IsCRField) {
1092 CRSpilled = true;
1093 // The first time we see a CR field, store the whole CR into the
1094 // save slot via GPR12 (available in the prolog for 32- and 64-bit).
1095 if (Subtarget.isPPC64()) {
1096 // 64-bit: SP+8
1097 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::X12));
1098 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::STW))
1099 .addReg(PPC::X12,
1100 getKillRegState(true))
1101 .addImm(8)
1102 .addReg(PPC::X1));
1103 } else {
1104 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1105 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1106 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12));
1107 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1108 .addReg(PPC::R12,
1109 getKillRegState(true)),
1110 CSI[i].getFrameIdx()));
1111 }
1112
1113 // Record that we spill the CR in this function.
1114 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
1115 FuncInfo->setSpillsCR();
1116 } else {
1117 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1118 TII.storeRegToStackSlot(MBB, MI, Reg, true,
1119 CSI[i].getFrameIdx(), RC, TRI);
1120 }
1121 }
1122 return true;
1123}
1124
1125static void
1126restoreCRs(bool isPPC64, bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
1127 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1128 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1129
1130 MachineFunction *MF = MBB.getParent();
1131 const PPCInstrInfo &TII =
1132 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1133 DebugLoc DL;
1134 unsigned RestoreOp, MoveReg;
1135
1136 if (isPPC64) {
1137 // 64-bit: SP+8
1138 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::LWZ), PPC::X12)
1139 .addImm(8)
1140 .addReg(PPC::X1));
1141 RestoreOp = PPC::MTCRF8;
1142 MoveReg = PPC::X12;
1143 } else {
1144 // 32-bit: FP-relative
1145 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
1146 PPC::R12),
1147 CSI[CSIIndex].getFrameIdx()));
1148 RestoreOp = PPC::MTCRF;
1149 MoveReg = PPC::R12;
1150 }
1151
1152 if (CR2Spilled)
1153 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
1154 .addReg(MoveReg));
1155
1156 if (CR3Spilled)
1157 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
1158 .addReg(MoveReg));
1159
1160 if (CR4Spilled)
1161 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
1162 .addReg(MoveReg));
1163}
1164
Eli Bendersky700ed802013-02-21 20:05:00 +00001165void PPCFrameLowering::
1166eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1167 MachineBasicBlock::iterator I) const {
1168 const PPCInstrInfo &TII =
1169 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1170 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1171 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1172 // Add (actually subtract) back the amount the callee popped on return.
1173 if (int CalleeAmt = I->getOperand(1).getImm()) {
1174 bool is64Bit = Subtarget.isPPC64();
1175 CalleeAmt *= -1;
1176 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1177 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1178 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1179 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1180 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1181 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1182 MachineInstr *MI = I;
1183 DebugLoc dl = MI->getDebugLoc();
1184
1185 if (isInt<16>(CalleeAmt)) {
1186 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1187 .addReg(StackReg, RegState::Kill)
1188 .addImm(CalleeAmt);
1189 } else {
1190 MachineBasicBlock::iterator MBBI = I;
1191 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1192 .addImm(CalleeAmt >> 16);
1193 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1194 .addReg(TmpReg, RegState::Kill)
1195 .addImm(CalleeAmt & 0xFFFF);
1196 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1197 .addReg(StackReg, RegState::Kill)
1198 .addReg(TmpReg);
1199 }
1200 }
1201 }
1202 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1203 MBB.erase(I);
1204}
1205
Roman Divacky9d760ae2012-09-12 14:47:47 +00001206bool
1207PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1208 MachineBasicBlock::iterator MI,
1209 const std::vector<CalleeSavedInfo> &CSI,
1210 const TargetRegisterInfo *TRI) const {
1211
1212 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1213 // Return false otherwise to maintain pre-existing behavior.
1214 if (!Subtarget.isSVR4ABI())
1215 return false;
1216
1217 MachineFunction *MF = MBB.getParent();
1218 const PPCInstrInfo &TII =
1219 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1220 bool CR2Spilled = false;
1221 bool CR3Spilled = false;
1222 bool CR4Spilled = false;
1223 unsigned CSIIndex = 0;
1224
1225 // Initialize insertion-point logic; we will be restoring in reverse
1226 // order of spill.
1227 MachineBasicBlock::iterator I = MI, BeforeI = I;
1228 bool AtStart = I == MBB.begin();
1229
1230 if (!AtStart)
1231 --BeforeI;
1232
1233 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1234 unsigned Reg = CSI[i].getReg();
1235
1236 if (Reg == PPC::CR2) {
1237 CR2Spilled = true;
1238 // The spill slot is associated only with CR2, which is the
1239 // first nonvolatile spilled. Save it here.
1240 CSIIndex = i;
1241 continue;
1242 } else if (Reg == PPC::CR3) {
1243 CR3Spilled = true;
1244 continue;
1245 } else if (Reg == PPC::CR4) {
1246 CR4Spilled = true;
1247 continue;
1248 } else {
1249 // When we first encounter a non-CR register after seeing at
1250 // least one CR register, restore all spilled CRs together.
1251 if ((CR2Spilled || CR3Spilled || CR4Spilled)
1252 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
1253 restoreCRs(Subtarget.isPPC64(), CR2Spilled, CR3Spilled, CR4Spilled,
1254 MBB, I, CSI, CSIIndex);
1255 CR2Spilled = CR3Spilled = CR4Spilled = false;
1256 }
1257
1258 // Default behavior for non-CR saves.
1259 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1260 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
1261 RC, TRI);
1262 assert(I != MBB.begin() &&
1263 "loadRegFromStackSlot didn't insert any code!");
1264 }
1265
1266 // Insert in reverse order.
1267 if (AtStart)
1268 I = MBB.begin();
1269 else {
1270 I = BeforeI;
1271 ++I;
1272 }
1273 }
1274
1275 // If we haven't yet spilled the CRs, do so now.
1276 if (CR2Spilled || CR3Spilled || CR4Spilled)
1277 restoreCRs(Subtarget.isPPC64(), CR2Spilled, CR3Spilled, CR4Spilled,
1278 MBB, I, CSI, CSIIndex);
1279
1280 return true;
1281}
1282