blob: cc1ed6967b1bfb222a69bcae6e3282d2ccbef76c [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
Anton Korobeynikov33464912010-11-15 00:06:54 +0000191/// determineFrameLayout - Determine the size of the frame and maximum call
192/// frame size.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000193void PPCFrameLowering::determineFrameLayout(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000194 MachineFrameInfo *MFI = MF.getFrameInfo();
195
196 // Get the number of bytes to allocate from the FrameInfo
197 unsigned FrameSize = MFI->getStackSize();
198
199 // Get the alignments provided by the target, and the maximum alignment
200 // (if any) of the fixed frame objects.
201 unsigned MaxAlign = MFI->getMaxAlignment();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000202 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000203 unsigned AlignMask = TargetAlign - 1; //
204
205 // If we are a leaf function, and use up to 224 bytes of stack space,
206 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Roman Divacky9d760ae2012-09-12 14:47:47 +0000207 // to adjust the stack pointer (we fit in the Red Zone). For 64-bit
208 // SVR4, we also require a stack frame if we need to spill the CR,
209 // since this spill area is addressed relative to the stack pointer.
Bill Wendling831737d2012-12-30 10:32:01 +0000210 bool DisableRedZone = MF.getFunction()->getAttributes().
211 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone);
Roman Divacky9d760ae2012-09-12 14:47:47 +0000212 // FIXME SVR4 The 32-bit SVR4 ABI has no red zone. However, it can
213 // still generate stackless code if all local vars are reg-allocated.
214 // Try: (FrameSize <= 224
215 // || (FrameSize == 0 && Subtarget.isPPC32 && Subtarget.isSVR4ABI()))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000216 if (!DisableRedZone &&
217 FrameSize <= 224 && // Fits in red zone.
218 !MFI->hasVarSizedObjects() && // No dynamic alloca.
219 !MFI->adjustsStack() && // No calls.
Roman Divacky9d760ae2012-09-12 14:47:47 +0000220 !(Subtarget.isPPC64() && // No 64-bit SVR4 CRsave.
221 Subtarget.isSVR4ABI()
222 && spillsCR(MF)) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000223 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment.
224 // No need for frame
225 MFI->setStackSize(0);
226 return;
227 }
228
229 // Get the maximum call frame size of all the calls.
230 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
231
232 // Maximum call frame needs to be at least big enough for linkage and 8 args.
233 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
234 Subtarget.isDarwinABI());
235 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
236
237 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
238 // that allocations will be aligned.
239 if (MFI->hasVarSizedObjects())
240 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
241
242 // Update maximum call frame size.
243 MFI->setMaxCallFrameSize(maxCallFrameSize);
244
245 // Include call frame size in total.
246 FrameSize += maxCallFrameSize;
247
248 // Make sure the frame is aligned.
249 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
250
251 // Update frame info.
252 MFI->setStackSize(FrameSize);
253}
254
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000255// hasFP - Return true if the specified function actually has a dedicated frame
256// pointer register.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000257bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000258 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000259 // FIXME: This is pretty much broken by design: hasFP() might be called really
260 // early, before the stack layout was calculated and thus hasFP() might return
261 // true or false here depending on the time of call.
262 return (MFI->getStackSize()) && needsFP(MF);
263}
264
265// needsFP - Return true if the specified function should have a dedicated frame
266// pointer register. This is true if the function has variable sized allocas or
267// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000268bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000269 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000270
271 // Naked functions have no stack frame pushed, so we don't have a frame
272 // pointer.
Bill Wendling831737d2012-12-30 10:32:01 +0000273 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
274 Attribute::Naked))
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000275 return false;
276
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000277 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
278 MFI->hasVarSizedObjects() ||
279 (MF.getTarget().Options.GuaranteedTailCallOpt &&
280 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000281}
282
283
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000284void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000285 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
286 MachineBasicBlock::iterator MBBI = MBB.begin();
287 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000288 const PPCInstrInfo &TII =
289 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
290
291 MachineModuleInfo &MMI = MF.getMMI();
292 DebugLoc dl;
293 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000294 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000295
296 // Prepare for frame info.
297 MCSymbol *FrameLabel = 0;
298
299 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
300 // process it.
Bill Schmidta5d0ab52012-10-10 20:54:15 +0000301 if (!Subtarget.isSVR4ABI())
302 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
303 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
304 HandleVRSaveUpdate(MBBI, TII);
305 break;
306 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000307 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000308
309 // Move MBBI back to the beginning of the function.
310 MBBI = MBB.begin();
311
312 // Work out frame sizes.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000313 // FIXME: determineFrameLayout() may change the frame size. This should be
314 // moved upper, to some hook.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000315 determineFrameLayout(MF);
316 unsigned FrameSize = MFI->getStackSize();
317
318 int NegFrameSize = -FrameSize;
319
320 // Get processor type.
321 bool isPPC64 = Subtarget.isPPC64();
322 // Get operating system
323 bool isDarwinABI = Subtarget.isDarwinABI();
324 // Check if the link register (LR) must be saved.
325 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
326 bool MustSaveLR = FI->mustSaveLR();
327 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000328 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000329
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000330 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000331
332 int FPOffset = 0;
333 if (HasFP) {
334 if (Subtarget.isSVR4ABI()) {
335 MachineFrameInfo *FFI = MF.getFrameInfo();
336 int FPIndex = FI->getFramePointerSaveIndex();
337 assert(FPIndex && "No Frame Pointer Save Slot!");
338 FPOffset = FFI->getObjectOffset(FPIndex);
339 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000340 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000341 }
342 }
343
344 if (isPPC64) {
345 if (MustSaveLR)
346 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
347
348 if (HasFP)
349 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
350 .addReg(PPC::X31)
351 .addImm(FPOffset/4)
352 .addReg(PPC::X1);
353
354 if (MustSaveLR)
355 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
356 .addReg(PPC::X0)
357 .addImm(LROffset / 4)
358 .addReg(PPC::X1);
359 } else {
360 if (MustSaveLR)
361 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
362
363 if (HasFP)
Hal Finkelb8f2f292012-05-19 21:52:55 +0000364 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative
365 // offsets of R1 is not allowed.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000366 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
367 .addReg(PPC::R31)
368 .addImm(FPOffset)
369 .addReg(PPC::R1);
370
371 if (MustSaveLR)
372 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
373 .addReg(PPC::R0)
374 .addImm(LROffset)
375 .addReg(PPC::R1);
376 }
377
378 // Skip if a leaf routine.
379 if (!FrameSize) return;
380
381 // Get stack alignments.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000382 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000383 unsigned MaxAlign = MFI->getMaxAlignment();
384
385 // Adjust stack pointer: r1 += NegFrameSize.
386 // If there is a preferred stack alignment, align R1 now
387 if (!isPPC64) {
388 // PPC32.
389 if (ALIGN_STACK && MaxAlign > TargetAlign) {
390 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
391 "Invalid alignment!");
392 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
393
394 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
395 .addReg(PPC::R1)
396 .addImm(0)
397 .addImm(32 - Log2_32(MaxAlign))
398 .addImm(31);
399 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0)
400 .addReg(PPC::R0, RegState::Kill)
401 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000402 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000403 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000404 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000405 .addReg(PPC::R0);
406 } else if (isInt<16>(NegFrameSize)) {
407 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
408 .addReg(PPC::R1)
409 .addImm(NegFrameSize)
410 .addReg(PPC::R1);
411 } else {
412 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
413 .addImm(NegFrameSize >> 16);
414 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
415 .addReg(PPC::R0, RegState::Kill)
416 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000417 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000418 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000419 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000420 .addReg(PPC::R0);
421 }
422 } else { // PPC64.
423 if (ALIGN_STACK && MaxAlign > TargetAlign) {
424 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
425 "Invalid alignment!");
426 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
427
428 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
429 .addReg(PPC::X1)
430 .addImm(0)
431 .addImm(64 - Log2_32(MaxAlign));
432 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
433 .addReg(PPC::X0)
434 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000435 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000436 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000437 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000438 .addReg(PPC::X0);
439 } else if (isInt<16>(NegFrameSize)) {
440 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
441 .addReg(PPC::X1)
442 .addImm(NegFrameSize / 4)
443 .addReg(PPC::X1);
444 } else {
445 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
446 .addImm(NegFrameSize >> 16);
447 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
448 .addReg(PPC::X0, RegState::Kill)
449 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000450 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000451 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000452 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000453 .addReg(PPC::X0);
454 }
455 }
456
457 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
458
459 // Add the "machine moves" for the instructions we generated above, but in
460 // reverse order.
461 if (needsFrameMoves) {
462 // Mark effective beginning of when frame pointer becomes valid.
463 FrameLabel = MMI.getContext().CreateTempSymbol();
464 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
465
466 // Show update of SP.
467 if (NegFrameSize) {
468 MachineLocation SPDst(MachineLocation::VirtualFP);
469 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
470 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc));
471 } else {
472 MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31);
473 Moves.push_back(MachineMove(FrameLabel, SP, SP));
474 }
475
476 if (HasFP) {
477 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
478 MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31);
479 Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc));
480 }
481
482 if (MustSaveLR) {
483 MachineLocation LRDst(MachineLocation::VirtualFP, LROffset);
484 MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR);
485 Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc));
486 }
487 }
488
489 MCSymbol *ReadyLabel = 0;
490
491 // If there is a frame pointer, copy R1 into R31
492 if (HasFP) {
493 if (!isPPC64) {
494 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
495 .addReg(PPC::R1)
496 .addReg(PPC::R1);
497 } else {
498 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
499 .addReg(PPC::X1)
500 .addReg(PPC::X1);
501 }
502
503 if (needsFrameMoves) {
504 ReadyLabel = MMI.getContext().CreateTempSymbol();
505
506 // Mark effective beginning of when frame pointer is ready.
507 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
508
509 MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) :
510 (isPPC64 ? PPC::X1 : PPC::R1));
511 MachineLocation FPSrc(MachineLocation::VirtualFP);
512 Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc));
513 }
514 }
515
516 if (needsFrameMoves) {
517 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
518
519 // Add callee saved registers to move list.
520 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
521 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000522 unsigned Reg = CSI[I].getReg();
523 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000524
525 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
526 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperc9099502012-04-20 06:31:50 +0000527 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola6e032942011-05-30 20:20:15 +0000528 continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000529
Roman Divacky9d760ae2012-09-12 14:47:47 +0000530 // For SVR4, don't emit a move for the CR spill slot if we haven't
531 // spilled CRs.
532 if (Subtarget.isSVR4ABI()
533 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
534 && !spillsCR(MF))
535 continue;
536
537 // For 64-bit SVR4 when we have spilled CRs, the spill location
538 // is SP+8, not a frame-relative slot.
539 if (Subtarget.isSVR4ABI()
540 && Subtarget.isPPC64()
541 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
542 MachineLocation CSDst(PPC::X1, 8);
543 MachineLocation CSSrc(PPC::CR2);
544 Moves.push_back(MachineMove(Label, CSDst, CSSrc));
545 continue;
546 }
547
548 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000549 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
550 MachineLocation CSSrc(Reg);
551 Moves.push_back(MachineMove(Label, CSDst, CSSrc));
552 }
553 }
554}
555
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000556void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000557 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000558 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
559 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000560 const PPCInstrInfo &TII =
561 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
562
563 unsigned RetOpcode = MBBI->getOpcode();
564 DebugLoc dl;
565
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000566 assert((RetOpcode == PPC::BLR ||
567 RetOpcode == PPC::TCRETURNri ||
568 RetOpcode == PPC::TCRETURNdi ||
569 RetOpcode == PPC::TCRETURNai ||
570 RetOpcode == PPC::TCRETURNri8 ||
571 RetOpcode == PPC::TCRETURNdi8 ||
572 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000573 "Can only insert epilog into returning blocks");
574
575 // Get alignment info so we know how to restore r1
576 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000577 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000578 unsigned MaxAlign = MFI->getMaxAlignment();
579
580 // Get the number of bytes allocated from the FrameInfo.
581 int FrameSize = MFI->getStackSize();
582
583 // Get processor type.
584 bool isPPC64 = Subtarget.isPPC64();
585 // Get operating system
586 bool isDarwinABI = Subtarget.isDarwinABI();
587 // Check if the link register (LR) has been saved.
588 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
589 bool MustSaveLR = FI->mustSaveLR();
590 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000591 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000592
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000593 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000594
595 int FPOffset = 0;
596 if (HasFP) {
597 if (Subtarget.isSVR4ABI()) {
598 MachineFrameInfo *FFI = MF.getFrameInfo();
599 int FPIndex = FI->getFramePointerSaveIndex();
600 assert(FPIndex && "No Frame Pointer Save Slot!");
601 FPOffset = FFI->getObjectOffset(FPIndex);
602 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000603 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000604 }
605 }
606
607 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
608 RetOpcode == PPC::TCRETURNdi ||
609 RetOpcode == PPC::TCRETURNai ||
610 RetOpcode == PPC::TCRETURNri8 ||
611 RetOpcode == PPC::TCRETURNdi8 ||
612 RetOpcode == PPC::TCRETURNai8;
613
614 if (UsesTCRet) {
615 int MaxTCRetDelta = FI->getTailCallSPDelta();
616 MachineOperand &StackAdjust = MBBI->getOperand(1);
617 assert(StackAdjust.isImm() && "Expecting immediate value.");
618 // Adjust stack pointer.
619 int StackAdj = StackAdjust.getImm();
620 int Delta = StackAdj - MaxTCRetDelta;
621 assert((Delta >= 0) && "Delta must be positive");
622 if (MaxTCRetDelta>0)
623 FrameSize += (StackAdj +Delta);
624 else
625 FrameSize += StackAdj;
626 }
627
628 if (FrameSize) {
629 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
630 // on entry to the function. Add this offset back now.
631 if (!isPPC64) {
632 // If this function contained a fastcc call and GuaranteedTailCallOpt is
633 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
634 // call which invalidates the stack pointer value in SP(0). So we use the
635 // value of R31 in this case.
636 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000637 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000638 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
639 .addReg(PPC::R31).addImm(FrameSize);
640 } else if(FI->hasFastCall()) {
641 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
642 .addImm(FrameSize >> 16);
643 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
644 .addReg(PPC::R0, RegState::Kill)
645 .addImm(FrameSize & 0xFFFF);
646 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
647 .addReg(PPC::R1)
648 .addReg(PPC::R31)
649 .addReg(PPC::R0);
650 } else if (isInt<16>(FrameSize) &&
651 (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
652 !MFI->hasVarSizedObjects()) {
653 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
654 .addReg(PPC::R1).addImm(FrameSize);
655 } else {
656 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
657 .addImm(0).addReg(PPC::R1);
658 }
659 } else {
660 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000661 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000662 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
663 .addReg(PPC::X31).addImm(FrameSize);
664 } else if(FI->hasFastCall()) {
665 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
666 .addImm(FrameSize >> 16);
667 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
668 .addReg(PPC::X0, RegState::Kill)
669 .addImm(FrameSize & 0xFFFF);
670 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
671 .addReg(PPC::X1)
672 .addReg(PPC::X31)
673 .addReg(PPC::X0);
674 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign &&
675 !MFI->hasVarSizedObjects()) {
676 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
677 .addReg(PPC::X1).addImm(FrameSize);
678 } else {
679 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
680 .addImm(0).addReg(PPC::X1);
681 }
682 }
683 }
684
685 if (isPPC64) {
686 if (MustSaveLR)
687 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
688 .addImm(LROffset/4).addReg(PPC::X1);
689
690 if (HasFP)
691 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
692 .addImm(FPOffset/4).addReg(PPC::X1);
693
694 if (MustSaveLR)
695 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
696 } else {
697 if (MustSaveLR)
698 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
699 .addImm(LROffset).addReg(PPC::R1);
700
701 if (HasFP)
702 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
703 .addImm(FPOffset).addReg(PPC::R1);
704
705 if (MustSaveLR)
706 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
707 }
708
709 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
710 // call optimization
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000711 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000712 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
713 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
714 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
715 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
716 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
717 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
718 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
719 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
720 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
721 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
722
723 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
724 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
725 .addReg(StackReg).addImm(CallerAllocatedAmt);
726 } else {
727 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
728 .addImm(CallerAllocatedAmt >> 16);
729 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
730 .addReg(TmpReg, RegState::Kill)
731 .addImm(CallerAllocatedAmt & 0xFFFF);
732 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
733 .addReg(StackReg)
734 .addReg(FPReg)
735 .addReg(TmpReg);
736 }
737 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000738 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000739 MachineOperand &JumpTarget = MBBI->getOperand(0);
740 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
741 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
742 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000743 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000744 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
745 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
746 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000747 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000748 MachineOperand &JumpTarget = MBBI->getOperand(0);
749 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
750 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000751 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000752 MachineOperand &JumpTarget = MBBI->getOperand(0);
753 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
754 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
755 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000756 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000757 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
758 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
759 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000760 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000761 MachineOperand &JumpTarget = MBBI->getOperand(0);
762 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
763 }
764}
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000765
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000766/// MustSaveLR - Return true if this function requires that we save the LR
767/// register onto the stack in the prolog and restore it in the epilog of the
768/// function.
769static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
770 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
771
772 // We need a save/restore of LR if there is any def of LR (which is
773 // defined by calls, including the PIC setup sequence), or if there is
774 // some use of the LR stack slot (e.g. for builtin_return_address).
775 // (LR comes in 32 and 64 bit versions.)
776 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
777 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
778}
779
780void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000781PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000782 RegScavenger *RS) const {
783 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
784
785 // Save and clear the LR state.
786 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
787 unsigned LR = RegInfo->getRARegister();
788 FI->setMustSaveLR(MustSaveLR(MF, LR));
789 MF.getRegInfo().setPhysRegUnused(LR);
790
791 // Save R31 if necessary
792 int FPSI = FI->getFramePointerSaveIndex();
793 bool isPPC64 = Subtarget.isPPC64();
794 bool isDarwinABI = Subtarget.isDarwinABI();
795 MachineFrameInfo *MFI = MF.getFrameInfo();
796
797 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000798 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000799 // Find out what the fix offset of the frame pointer save area.
800 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
801 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000802 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000803 // Save the result.
804 FI->setFramePointerSaveIndex(FPSI);
805 }
806
807 // Reserve stack space to move the linkage area to in case of a tail call.
808 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000809 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
810 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000811 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000812 }
813
814 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
815 // a large stack, which will require scavenging a register to materialize a
816 // large offset.
817 // FIXME: this doesn't actually check stack size, so is a bit pessimistic
818 // FIXME: doesn't detect whether or not we need to spill vXX, which requires
819 // r0 for now.
820
Hal Finkel3fd00182011-12-05 17:55:17 +0000821 if (RegInfo->requiresRegisterScavenging(MF))
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000822 if (needsFP(MF) || spillsCR(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000823 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
824 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
825 const TargetRegisterClass *RC = isPPC64 ? G8RC : GPRC;
826 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
827 RC->getAlignment(),
828 false));
829 }
830}
831
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000832void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF)
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000833 const {
834 // Early exit if not using the SVR4 ABI.
835 if (!Subtarget.isSVR4ABI())
836 return;
837
838 // Get callee saved register information.
839 MachineFrameInfo *FFI = MF.getFrameInfo();
840 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
841
842 // Early exit if no callee saved registers are modified!
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000843 if (CSI.empty() && !needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000844 return;
845 }
846
847 unsigned MinGPR = PPC::R31;
848 unsigned MinG8R = PPC::X31;
849 unsigned MinFPR = PPC::F31;
850 unsigned MinVR = PPC::V31;
851
852 bool HasGPSaveArea = false;
853 bool HasG8SaveArea = false;
854 bool HasFPSaveArea = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000855 bool HasVRSAVESaveArea = false;
856 bool HasVRSaveArea = false;
857
858 SmallVector<CalleeSavedInfo, 18> GPRegs;
859 SmallVector<CalleeSavedInfo, 18> G8Regs;
860 SmallVector<CalleeSavedInfo, 18> FPRegs;
861 SmallVector<CalleeSavedInfo, 18> VRegs;
862
863 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
864 unsigned Reg = CSI[i].getReg();
Craig Topperc9099502012-04-20 06:31:50 +0000865 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000866 HasGPSaveArea = true;
867
868 GPRegs.push_back(CSI[i]);
869
870 if (Reg < MinGPR) {
871 MinGPR = Reg;
872 }
Craig Topperc9099502012-04-20 06:31:50 +0000873 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000874 HasG8SaveArea = true;
875
876 G8Regs.push_back(CSI[i]);
877
878 if (Reg < MinG8R) {
879 MinG8R = Reg;
880 }
Craig Topperc9099502012-04-20 06:31:50 +0000881 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000882 HasFPSaveArea = true;
883
884 FPRegs.push_back(CSI[i]);
885
886 if (Reg < MinFPR) {
887 MinFPR = Reg;
888 }
Craig Topperc9099502012-04-20 06:31:50 +0000889 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
890 PPC::CRRCRegClass.contains(Reg)) {
Roman Divacky9d760ae2012-09-12 14:47:47 +0000891 ; // do nothing, as we already know whether CRs are spilled
Craig Topperc9099502012-04-20 06:31:50 +0000892 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000893 HasVRSAVESaveArea = true;
Craig Topperc9099502012-04-20 06:31:50 +0000894 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000895 HasVRSaveArea = true;
896
897 VRegs.push_back(CSI[i]);
898
899 if (Reg < MinVR) {
900 MinVR = Reg;
901 }
902 } else {
903 llvm_unreachable("Unknown RegisterClass!");
904 }
905 }
906
907 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
908
909 int64_t LowerBound = 0;
910
911 // Take into account stack space reserved for tail calls.
912 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000913 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
914 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000915 LowerBound = TCSPDelta;
916 }
917
918 // The Floating-point register save area is right below the back chain word
919 // of the previous stack frame.
920 if (HasFPSaveArea) {
921 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
922 int FI = FPRegs[i].getFrameIdx();
923
924 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
925 }
926
Evan Cheng966aeb52011-07-25 19:53:23 +0000927 LowerBound -= (31 - getPPCRegisterNumbering(MinFPR) + 1) * 8;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000928 }
929
930 // Check whether the frame pointer register is allocated. If so, make sure it
931 // is spilled to the correct offset.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000932 if (needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000933 HasGPSaveArea = true;
934
935 int FI = PFI->getFramePointerSaveIndex();
936 assert(FI && "No Frame Pointer Save Slot!");
937
938 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
939 }
940
941 // General register save area starts right below the Floating-point
942 // register save area.
943 if (HasGPSaveArea || HasG8SaveArea) {
944 // Move general register save area spill slots down, taking into account
945 // the size of the Floating-point register save area.
946 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
947 int FI = GPRegs[i].getFrameIdx();
948
949 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
950 }
951
952 // Move general register save area spill slots down, taking into account
953 // the size of the Floating-point register save area.
954 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
955 int FI = G8Regs[i].getFrameIdx();
956
957 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
958 }
959
960 unsigned MinReg =
Evan Cheng966aeb52011-07-25 19:53:23 +0000961 std::min<unsigned>(getPPCRegisterNumbering(MinGPR),
962 getPPCRegisterNumbering(MinG8R));
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000963
964 if (Subtarget.isPPC64()) {
965 LowerBound -= (31 - MinReg + 1) * 8;
966 } else {
967 LowerBound -= (31 - MinReg + 1) * 4;
968 }
969 }
970
Roman Divacky9d760ae2012-09-12 14:47:47 +0000971 // For 32-bit only, the CR save area is below the general register
972 // save area. For 64-bit SVR4, the CR save area is addressed relative
973 // to the stack pointer and hence does not need an adjustment here.
974 // Only CR2 (the first nonvolatile spilled) has an associated frame
975 // index so that we have a single uniform save area.
976 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000977 // Adjust the frame index of the CR spill slot.
978 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
979 unsigned Reg = CSI[i].getReg();
980
Roman Divacky9d760ae2012-09-12 14:47:47 +0000981 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
982 // Leave Darwin logic as-is.
983 || (!Subtarget.isSVR4ABI() &&
984 (PPC::CRBITRCRegClass.contains(Reg) ||
985 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000986 int FI = CSI[i].getFrameIdx();
987
988 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
989 }
990 }
991
992 LowerBound -= 4; // The CR save area is always 4 bytes long.
993 }
994
995 if (HasVRSAVESaveArea) {
996 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
997 // which have the VRSAVE register class?
998 // Adjust the frame index of the VRSAVE spill slot.
999 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1000 unsigned Reg = CSI[i].getReg();
1001
Craig Topperc9099502012-04-20 06:31:50 +00001002 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001003 int FI = CSI[i].getFrameIdx();
1004
1005 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1006 }
1007 }
1008
1009 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1010 }
1011
1012 if (HasVRSaveArea) {
1013 // Insert alignment padding, we need 16-byte alignment.
1014 LowerBound = (LowerBound - 15) & ~(15);
1015
1016 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1017 int FI = VRegs[i].getFrameIdx();
1018
1019 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1020 }
1021 }
1022}
Roman Divacky9d760ae2012-09-12 14:47:47 +00001023
1024bool
1025PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1026 MachineBasicBlock::iterator MI,
1027 const std::vector<CalleeSavedInfo> &CSI,
1028 const TargetRegisterInfo *TRI) const {
1029
1030 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1031 // Return false otherwise to maintain pre-existing behavior.
1032 if (!Subtarget.isSVR4ABI())
1033 return false;
1034
1035 MachineFunction *MF = MBB.getParent();
1036 const PPCInstrInfo &TII =
1037 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1038 DebugLoc DL;
1039 bool CRSpilled = false;
1040
1041 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1042 unsigned Reg = CSI[i].getReg();
1043 // CR2 through CR4 are the nonvolatile CR fields.
1044 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1045
1046 if (CRSpilled && IsCRField)
1047 continue;
1048
1049 // Add the callee-saved register as live-in; it's killed at the spill.
1050 MBB.addLiveIn(Reg);
1051
1052 // Insert the spill to the stack frame.
1053 if (IsCRField) {
1054 CRSpilled = true;
1055 // The first time we see a CR field, store the whole CR into the
1056 // save slot via GPR12 (available in the prolog for 32- and 64-bit).
1057 if (Subtarget.isPPC64()) {
1058 // 64-bit: SP+8
1059 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::X12));
1060 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::STW))
1061 .addReg(PPC::X12,
1062 getKillRegState(true))
1063 .addImm(8)
1064 .addReg(PPC::X1));
1065 } else {
1066 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1067 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1068 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12));
1069 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1070 .addReg(PPC::R12,
1071 getKillRegState(true)),
1072 CSI[i].getFrameIdx()));
1073 }
1074
1075 // Record that we spill the CR in this function.
1076 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
1077 FuncInfo->setSpillsCR();
1078 } else {
1079 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1080 TII.storeRegToStackSlot(MBB, MI, Reg, true,
1081 CSI[i].getFrameIdx(), RC, TRI);
1082 }
1083 }
1084 return true;
1085}
1086
1087static void
1088restoreCRs(bool isPPC64, bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
1089 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1090 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1091
1092 MachineFunction *MF = MBB.getParent();
1093 const PPCInstrInfo &TII =
1094 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1095 DebugLoc DL;
1096 unsigned RestoreOp, MoveReg;
1097
1098 if (isPPC64) {
1099 // 64-bit: SP+8
1100 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::LWZ), PPC::X12)
1101 .addImm(8)
1102 .addReg(PPC::X1));
1103 RestoreOp = PPC::MTCRF8;
1104 MoveReg = PPC::X12;
1105 } else {
1106 // 32-bit: FP-relative
1107 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
1108 PPC::R12),
1109 CSI[CSIIndex].getFrameIdx()));
1110 RestoreOp = PPC::MTCRF;
1111 MoveReg = PPC::R12;
1112 }
1113
1114 if (CR2Spilled)
1115 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
1116 .addReg(MoveReg));
1117
1118 if (CR3Spilled)
1119 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
1120 .addReg(MoveReg));
1121
1122 if (CR4Spilled)
1123 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
1124 .addReg(MoveReg));
1125}
1126
Eli Bendersky700ed802013-02-21 20:05:00 +00001127void PPCFrameLowering::
1128eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1129 MachineBasicBlock::iterator I) const {
1130 const PPCInstrInfo &TII =
1131 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1132 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1133 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1134 // Add (actually subtract) back the amount the callee popped on return.
1135 if (int CalleeAmt = I->getOperand(1).getImm()) {
1136 bool is64Bit = Subtarget.isPPC64();
1137 CalleeAmt *= -1;
1138 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1139 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1140 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1141 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1142 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1143 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1144 MachineInstr *MI = I;
1145 DebugLoc dl = MI->getDebugLoc();
1146
1147 if (isInt<16>(CalleeAmt)) {
1148 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1149 .addReg(StackReg, RegState::Kill)
1150 .addImm(CalleeAmt);
1151 } else {
1152 MachineBasicBlock::iterator MBBI = I;
1153 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1154 .addImm(CalleeAmt >> 16);
1155 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1156 .addReg(TmpReg, RegState::Kill)
1157 .addImm(CalleeAmt & 0xFFFF);
1158 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1159 .addReg(StackReg, RegState::Kill)
1160 .addReg(TmpReg);
1161 }
1162 }
1163 }
1164 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1165 MBB.erase(I);
1166}
1167
Roman Divacky9d760ae2012-09-12 14:47:47 +00001168bool
1169PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1170 MachineBasicBlock::iterator MI,
1171 const std::vector<CalleeSavedInfo> &CSI,
1172 const TargetRegisterInfo *TRI) const {
1173
1174 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1175 // Return false otherwise to maintain pre-existing behavior.
1176 if (!Subtarget.isSVR4ABI())
1177 return false;
1178
1179 MachineFunction *MF = MBB.getParent();
1180 const PPCInstrInfo &TII =
1181 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1182 bool CR2Spilled = false;
1183 bool CR3Spilled = false;
1184 bool CR4Spilled = false;
1185 unsigned CSIIndex = 0;
1186
1187 // Initialize insertion-point logic; we will be restoring in reverse
1188 // order of spill.
1189 MachineBasicBlock::iterator I = MI, BeforeI = I;
1190 bool AtStart = I == MBB.begin();
1191
1192 if (!AtStart)
1193 --BeforeI;
1194
1195 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1196 unsigned Reg = CSI[i].getReg();
1197
1198 if (Reg == PPC::CR2) {
1199 CR2Spilled = true;
1200 // The spill slot is associated only with CR2, which is the
1201 // first nonvolatile spilled. Save it here.
1202 CSIIndex = i;
1203 continue;
1204 } else if (Reg == PPC::CR3) {
1205 CR3Spilled = true;
1206 continue;
1207 } else if (Reg == PPC::CR4) {
1208 CR4Spilled = true;
1209 continue;
1210 } else {
1211 // When we first encounter a non-CR register after seeing at
1212 // least one CR register, restore all spilled CRs together.
1213 if ((CR2Spilled || CR3Spilled || CR4Spilled)
1214 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
1215 restoreCRs(Subtarget.isPPC64(), CR2Spilled, CR3Spilled, CR4Spilled,
1216 MBB, I, CSI, CSIIndex);
1217 CR2Spilled = CR3Spilled = CR4Spilled = false;
1218 }
1219
1220 // Default behavior for non-CR saves.
1221 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1222 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
1223 RC, TRI);
1224 assert(I != MBB.begin() &&
1225 "loadRegFromStackSlot didn't insert any code!");
1226 }
1227
1228 // Insert in reverse order.
1229 if (AtStart)
1230 I = MBB.begin();
1231 else {
1232 I = BeforeI;
1233 ++I;
1234 }
1235 }
1236
1237 // If we haven't yet spilled the CRs, do so now.
1238 if (CR2Spilled || CR3Spilled || CR4Spilled)
1239 restoreCRs(Subtarget.isPPC64(), CR2Spilled, CR3Spilled, CR4Spilled,
1240 MBB, I, CSI, CSIIndex);
1241
1242 return true;
1243}
1244