blob: 3b57390611becb5074347645c4eda88768f633f6 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
Anton Korobeynikov33464912010-11-15 00:06:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010// This file contains the PPC implementation of TargetFrameLowering class.
Anton Korobeynikov33464912010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "PPCFrameLowering.h"
Roman Divacky9d760ae2012-09-12 14:47:47 +000015#include "PPCInstrBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "PPCInstrInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000017#include "PPCMachineFunctionInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000025#include "llvm/Target/TargetOptions.h"
26
27using namespace llvm;
28
Anton Korobeynikov33464912010-11-15 00:06:54 +000029/// VRRegNo - Map from a numbered VR register to its enum value.
30///
Craig Topperb78ca422012-03-11 07:16:55 +000031static const uint16_t VRRegNo[] = {
Anton Korobeynikov33464912010-11-15 00:06:54 +000032 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
33 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
34 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
35 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
36};
37
38/// RemoveVRSaveCode - We have found that this function does not need any code
39/// to manipulate the VRSAVE register, even though it uses vector registers.
40/// This can happen when the only registers used are known to be live in or out
41/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidta5d0ab52012-10-10 20:54:15 +000042/// FIXME: The removal of the code results in a compile failure at -O0 when the
43/// function contains a function call, as the GPR containing original VRSAVE
44/// contents is spilled and reloaded around the call. Without the prolog code,
45/// the spill instruction refers to an undefined register. This code needs
46/// to account for all uses of that GPR.
Anton Korobeynikov33464912010-11-15 00:06:54 +000047static void RemoveVRSaveCode(MachineInstr *MI) {
48 MachineBasicBlock *Entry = MI->getParent();
49 MachineFunction *MF = Entry->getParent();
50
51 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
52 MachineBasicBlock::iterator MBBI = MI;
53 ++MBBI;
54 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
55 MBBI->eraseFromParent();
56
57 bool RemovedAllMTVRSAVEs = true;
58 // See if we can find and remove the MTVRSAVE instruction from all of the
59 // epilog blocks.
60 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
61 // If last instruction is a return instruction, add an epilogue
Evan Cheng5a96b3d2011-12-07 07:15:52 +000062 if (!I->empty() && I->back().isReturn()) {
Anton Korobeynikov33464912010-11-15 00:06:54 +000063 bool FoundIt = false;
64 for (MBBI = I->end(); MBBI != I->begin(); ) {
65 --MBBI;
66 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
67 MBBI->eraseFromParent(); // remove it.
68 FoundIt = true;
69 break;
70 }
71 }
72 RemovedAllMTVRSAVEs &= FoundIt;
73 }
74 }
75
76 // If we found and removed all MTVRSAVE instructions, remove the read of
77 // VRSAVE as well.
78 if (RemovedAllMTVRSAVEs) {
79 MBBI = MI;
80 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
81 --MBBI;
82 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
83 MBBI->eraseFromParent();
84 }
85
86 // Finally, nuke the UPDATE_VRSAVE.
87 MI->eraseFromParent();
88}
89
90// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
91// instruction selector. Based on the vector registers that have been used,
92// transform this into the appropriate ORI instruction.
93static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
94 MachineFunction *MF = MI->getParent()->getParent();
Hal Finkelaa6047d2013-03-26 20:08:20 +000095 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +000096 DebugLoc dl = MI->getDebugLoc();
97
98 unsigned UsedRegMask = 0;
99 for (unsigned i = 0; i != 32; ++i)
100 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
101 UsedRegMask |= 1 << (31-i);
102
103 // Live in and live out values already must be in the mask, so don't bother
104 // marking them.
105 for (MachineRegisterInfo::livein_iterator
106 I = MF->getRegInfo().livein_begin(),
107 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelaa6047d2013-03-26 20:08:20 +0000108 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000109 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
110 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
111 }
Jakob Stoklund Olesen0a9d1d32013-02-05 17:40:36 +0000112
113 // Live out registers appear as use operands on return instructions.
114 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
115 UsedRegMask != 0 && BI != BE; ++BI) {
116 const MachineBasicBlock &MBB = *BI;
117 if (MBB.empty() || !MBB.back().isReturn())
118 continue;
119 const MachineInstr &Ret = MBB.back();
120 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
121 const MachineOperand &MO = Ret.getOperand(I);
122 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
123 continue;
Hal Finkelaa6047d2013-03-26 20:08:20 +0000124 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesen0a9d1d32013-02-05 17:40:36 +0000125 UsedRegMask &= ~(1 << (31-RegNo));
126 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000127 }
128
129 // If no registers are used, turn this into a copy.
130 if (UsedRegMask == 0) {
131 // Remove all VRSAVE code.
132 RemoveVRSaveCode(MI);
133 return;
134 }
135
136 unsigned SrcReg = MI->getOperand(1).getReg();
137 unsigned DstReg = MI->getOperand(0).getReg();
138
139 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
140 if (DstReg != SrcReg)
141 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
142 .addReg(SrcReg)
143 .addImm(UsedRegMask);
144 else
145 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
146 .addReg(SrcReg, RegState::Kill)
147 .addImm(UsedRegMask);
148 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
149 if (DstReg != SrcReg)
150 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
151 .addReg(SrcReg)
152 .addImm(UsedRegMask >> 16);
153 else
154 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
155 .addReg(SrcReg, RegState::Kill)
156 .addImm(UsedRegMask >> 16);
157 } else {
158 if (DstReg != SrcReg)
159 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
160 .addReg(SrcReg)
161 .addImm(UsedRegMask >> 16);
162 else
163 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
164 .addReg(SrcReg, RegState::Kill)
165 .addImm(UsedRegMask >> 16);
166
167 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
168 .addReg(DstReg, RegState::Kill)
169 .addImm(UsedRegMask & 0xFFFF);
170 }
171
172 // Remove the old UPDATE_VRSAVE instruction.
173 MI->eraseFromParent();
174}
175
Roman Divacky9d760ae2012-09-12 14:47:47 +0000176static bool spillsCR(const MachineFunction &MF) {
177 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
178 return FuncInfo->isCRSpilled();
179}
180
Hal Finkel3f2c0472013-03-23 22:06:03 +0000181static bool spillsVRSAVE(const MachineFunction &MF) {
182 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
183 return FuncInfo->isVRSAVESpilled();
184}
185
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000186static bool hasSpills(const MachineFunction &MF) {
187 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
188 return FuncInfo->hasSpills();
189}
190
Hal Finkel32497292013-03-17 04:43:44 +0000191static bool hasNonRISpills(const MachineFunction &MF) {
192 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
193 return FuncInfo->hasNonRISpills();
194}
195
Anton Korobeynikov33464912010-11-15 00:06:54 +0000196/// determineFrameLayout - Determine the size of the frame and maximum call
197/// frame size.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000198unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
199 bool UpdateMF,
200 bool UseEstimate) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000201 MachineFrameInfo *MFI = MF.getFrameInfo();
202
203 // Get the number of bytes to allocate from the FrameInfo
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000204 unsigned FrameSize =
205 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000206
207 // Get the alignments provided by the target, and the maximum alignment
208 // (if any) of the fixed frame objects.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000209 unsigned TargetAlign = getStackAlignment();
Hal Finkelfe47bf82013-07-17 00:45:52 +0000210 unsigned MaxAlign = MFI->getMaxAlignment();
211 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
212
213 const PPCRegisterInfo *RegInfo =
214 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000215
216 // If we are a leaf function, and use up to 224 bytes of stack space,
217 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000218 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt65396822013-02-26 21:28:57 +0000219 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
220 // stackless code if all local vars are reg-allocated.
Bill Wendling831737d2012-12-30 10:32:01 +0000221 bool DisableRedZone = MF.getFunction()->getAttributes().
222 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000223 if (!DisableRedZone &&
Bill Schmidt65396822013-02-26 21:28:57 +0000224 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
225 !Subtarget.isSVR4ABI() || // allocated locals.
226 FrameSize == 0) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000227 FrameSize <= 224 && // Fits in red zone.
228 !MFI->hasVarSizedObjects() && // No dynamic alloca.
229 !MFI->adjustsStack() && // No calls.
Hal Finkelfe47bf82013-07-17 00:45:52 +0000230 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000231 // No need for frame
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000232 if (UpdateMF)
233 MFI->setStackSize(0);
234 return 0;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000235 }
236
237 // Get the maximum call frame size of all the calls.
238 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
239
240 // Maximum call frame needs to be at least big enough for linkage and 8 args.
241 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
242 Subtarget.isDarwinABI());
243 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
244
245 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
246 // that allocations will be aligned.
247 if (MFI->hasVarSizedObjects())
248 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
249
250 // Update maximum call frame size.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000251 if (UpdateMF)
252 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000253
254 // Include call frame size in total.
255 FrameSize += maxCallFrameSize;
256
257 // Make sure the frame is aligned.
258 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
259
260 // Update frame info.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000261 if (UpdateMF)
262 MFI->setStackSize(FrameSize);
263
264 return FrameSize;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000265}
266
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000267// hasFP - Return true if the specified function actually has a dedicated frame
268// pointer register.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000269bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000270 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000271 // FIXME: This is pretty much broken by design: hasFP() might be called really
272 // early, before the stack layout was calculated and thus hasFP() might return
273 // true or false here depending on the time of call.
274 return (MFI->getStackSize()) && needsFP(MF);
275}
276
277// needsFP - Return true if the specified function should have a dedicated frame
278// pointer register. This is true if the function has variable sized allocas or
279// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000280bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000281 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000282
283 // Naked functions have no stack frame pushed, so we don't have a frame
284 // pointer.
Bill Wendling831737d2012-12-30 10:32:01 +0000285 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
286 Attribute::Naked))
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000287 return false;
288
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000289 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
290 MFI->hasVarSizedObjects() ||
291 (MF.getTarget().Options.GuaranteedTailCallOpt &&
292 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000293}
294
Hal Finkele9cc0a02013-03-21 19:03:19 +0000295void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
296 bool is31 = needsFP(MF);
297 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
298 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
299
300 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
301 BI != BE; ++BI)
302 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
303 --MBBI;
304 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
305 MachineOperand &MO = MBBI->getOperand(I);
306 if (!MO.isReg())
307 continue;
308
309 switch (MO.getReg()) {
310 case PPC::FP:
311 MO.setReg(FPReg);
312 break;
313 case PPC::FP8:
314 MO.setReg(FP8Reg);
315 break;
316 }
317 }
318 }
319}
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000320
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000321void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000322 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
323 MachineBasicBlock::iterator MBBI = MBB.begin();
324 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000325 const PPCInstrInfo &TII =
326 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkelfe47bf82013-07-17 00:45:52 +0000327 const PPCRegisterInfo *RegInfo =
328 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000329
330 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendling99cb6222013-06-18 07:20:20 +0000331 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000332 DebugLoc dl;
333 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000334 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000335
336 // Prepare for frame info.
337 MCSymbol *FrameLabel = 0;
338
339 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
340 // process it.
Bill Schmidta5d0ab52012-10-10 20:54:15 +0000341 if (!Subtarget.isSVR4ABI())
342 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
343 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
344 HandleVRSaveUpdate(MBBI, TII);
345 break;
346 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000347 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000348
349 // Move MBBI back to the beginning of the function.
350 MBBI = MBB.begin();
351
352 // Work out frame sizes.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000353 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000354 int NegFrameSize = -FrameSize;
Hal Finkelfe47bf82013-07-17 00:45:52 +0000355 if (!isInt<32>(NegFrameSize))
356 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000357
Hal Finkele9cc0a02013-03-21 19:03:19 +0000358 if (MFI->isFrameAddressTaken())
359 replaceFPWithRealFP(MF);
360
Anton Korobeynikov33464912010-11-15 00:06:54 +0000361 // Get processor type.
362 bool isPPC64 = Subtarget.isPPC64();
363 // Get operating system
364 bool isDarwinABI = Subtarget.isDarwinABI();
365 // Check if the link register (LR) must be saved.
366 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
367 bool MustSaveLR = FI->mustSaveLR();
Craig Toppera0ec3f92013-07-14 04:42:23 +0000368 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000369 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000370 bool HasFP = hasFP(MF);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000371 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000372
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000373 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000374
375 int FPOffset = 0;
376 if (HasFP) {
377 if (Subtarget.isSVR4ABI()) {
378 MachineFrameInfo *FFI = MF.getFrameInfo();
379 int FPIndex = FI->getFramePointerSaveIndex();
380 assert(FPIndex && "No Frame Pointer Save Slot!");
381 FPOffset = FFI->getObjectOffset(FPIndex);
382 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000383 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000384 }
385 }
386
Hal Finkelfe47bf82013-07-17 00:45:52 +0000387 int BPOffset = 0;
388 if (HasBP) {
389 if (Subtarget.isSVR4ABI()) {
390 MachineFrameInfo *FFI = MF.getFrameInfo();
391 int BPIndex = FI->getBasePointerSaveIndex();
392 assert(BPIndex && "No Base Pointer Save Slot!");
393 BPOffset = FFI->getObjectOffset(BPIndex);
394 } else {
395 BPOffset =
396 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI,
397 HasFP);
398 }
399 }
400
Anton Korobeynikov33464912010-11-15 00:06:54 +0000401 if (isPPC64) {
402 if (MustSaveLR)
403 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
404
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000405 if (!MustSaveCRs.empty()) {
406 MachineInstrBuilder MIB =
407 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), PPC::X12);
408 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
409 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
410 }
411
Anton Korobeynikov33464912010-11-15 00:06:54 +0000412 if (HasFP)
413 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
414 .addReg(PPC::X31)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000415 .addImm(FPOffset)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000416 .addReg(PPC::X1);
417
Hal Finkelfe47bf82013-07-17 00:45:52 +0000418 if (HasBP)
419 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
420 .addReg(HasFP ? PPC::X30 : PPC::X31)
421 .addImm(BPOffset)
422 .addReg(PPC::X1);
423
Anton Korobeynikov33464912010-11-15 00:06:54 +0000424 if (MustSaveLR)
425 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
426 .addReg(PPC::X0)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000427 .addImm(LROffset)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000428 .addReg(PPC::X1);
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000429
430 if (!MustSaveCRs.empty())
431 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
432 .addReg(PPC::X12, getKillRegState(true))
433 .addImm(8)
434 .addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000435 } else {
436 if (MustSaveLR)
437 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
438
439 if (HasFP)
Hal Finkelb8f2f292012-05-19 21:52:55 +0000440 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative
441 // offsets of R1 is not allowed.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000442 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
443 .addReg(PPC::R31)
444 .addImm(FPOffset)
445 .addReg(PPC::R1);
446
Hal Finkelfe47bf82013-07-17 00:45:52 +0000447 if (HasBP)
448 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative
449 // offsets of R1 is not allowed.
450 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
451 .addReg(HasFP ? PPC::R30 : PPC::R31)
452 .addImm(BPOffset)
453 .addReg(PPC::R1);
454
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000455 assert(MustSaveCRs.empty() &&
456 "Prologue CR saving supported only in 64-bit mode");
457
Anton Korobeynikov33464912010-11-15 00:06:54 +0000458 if (MustSaveLR)
459 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
460 .addReg(PPC::R0)
461 .addImm(LROffset)
462 .addReg(PPC::R1);
463 }
464
465 // Skip if a leaf routine.
466 if (!FrameSize) return;
467
468 // Get stack alignments.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000469 unsigned MaxAlign = MFI->getMaxAlignment();
470
471 // Adjust stack pointer: r1 += NegFrameSize.
472 // If there is a preferred stack alignment, align R1 now
473 if (!isPPC64) {
474 // PPC32.
Hal Finkelfe47bf82013-07-17 00:45:52 +0000475
476 if (HasBP) {
477 // Save a copy of r1 as the base pointer.
478 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR),
479 HasFP ? PPC::R30 : PPC::R31)
480 .addReg(PPC::R1)
481 .addReg(PPC::R1);
482 }
483
484 if (HasBP && MaxAlign > 1) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000485 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
486 "Invalid alignment!");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000487
488 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
489 .addReg(PPC::R1)
490 .addImm(0)
491 .addImm(32 - Log2_32(MaxAlign))
492 .addImm(31);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000493 if (isInt<16>(NegFrameSize)) {
494 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC), PPC::R0)
495 .addReg(PPC::R0, RegState::Kill)
496 .addImm(NegFrameSize);
497 } else {
498 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R12)
499 .addImm(NegFrameSize >> 16);
500 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R12)
501 .addReg(PPC::R12, RegState::Kill)
502 .addImm(NegFrameSize & 0xFFFF);
503 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFC), PPC::R0)
504 .addReg(PPC::R0, RegState::Kill)
505 .addReg(PPC::R12, RegState::Kill);
506 }
Hal Finkelac81cc32012-06-19 02:34:32 +0000507 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000508 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000509 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000510 .addReg(PPC::R0);
511 } else if (isInt<16>(NegFrameSize)) {
512 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
513 .addReg(PPC::R1)
514 .addImm(NegFrameSize)
515 .addReg(PPC::R1);
516 } else {
517 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
518 .addImm(NegFrameSize >> 16);
519 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
520 .addReg(PPC::R0, RegState::Kill)
521 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000522 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000523 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000524 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000525 .addReg(PPC::R0);
526 }
527 } else { // PPC64.
Hal Finkelfe47bf82013-07-17 00:45:52 +0000528 if (HasBP) {
529 // Save a copy of r1 as the base pointer.
530 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8),
531 HasFP ? PPC::X30 : PPC::X31)
532 .addReg(PPC::X1)
533 .addReg(PPC::X1);
534 }
535
536 if (HasBP && MaxAlign > 1) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000537 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
538 "Invalid alignment!");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000539
540 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
541 .addReg(PPC::X1)
542 .addImm(0)
543 .addImm(64 - Log2_32(MaxAlign));
Hal Finkelfe47bf82013-07-17 00:45:52 +0000544 if (isInt<16>(NegFrameSize)) {
545 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
546 .addReg(PPC::X0, RegState::Kill)
547 .addImm(NegFrameSize);
548 } else {
549 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X12)
550 .addImm(NegFrameSize >> 16);
551 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X12)
552 .addReg(PPC::X12, RegState::Kill)
553 .addImm(NegFrameSize & 0xFFFF);
554 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFC8), PPC::X0)
555 .addReg(PPC::X0, RegState::Kill)
556 .addReg(PPC::X12, RegState::Kill);
557 }
Hal Finkelac81cc32012-06-19 02:34:32 +0000558 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000559 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000560 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000561 .addReg(PPC::X0);
562 } else if (isInt<16>(NegFrameSize)) {
563 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
564 .addReg(PPC::X1)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000565 .addImm(NegFrameSize)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000566 .addReg(PPC::X1);
567 } else {
568 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
569 .addImm(NegFrameSize >> 16);
570 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
571 .addReg(PPC::X0, RegState::Kill)
572 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000573 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000574 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000575 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000576 .addReg(PPC::X0);
577 }
578 }
579
Anton Korobeynikov33464912010-11-15 00:06:54 +0000580 // Add the "machine moves" for the instructions we generated above, but in
581 // reverse order.
582 if (needsFrameMoves) {
583 // Mark effective beginning of when frame pointer becomes valid.
584 FrameLabel = MMI.getContext().CreateTempSymbol();
585 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
586
587 // Show update of SP.
Rafael Espindolaec7f4232013-05-16 03:34:58 +0000588 assert(NegFrameSize);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000589 MMI.addFrameInst(
590 MCCFIInstruction::createDefCfaOffset(FrameLabel, NegFrameSize));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000591
592 if (HasFP) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000593 unsigned Reg = isPPC64 ? PPC::X31 : PPC::R31;
Bill Wendling99cb6222013-06-18 07:20:20 +0000594 Reg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000595 MMI.addFrameInst(
596 MCCFIInstruction::createOffset(FrameLabel, Reg, FPOffset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000597 }
598
Hal Finkelfe47bf82013-07-17 00:45:52 +0000599 if (HasBP) {
600 unsigned Reg = isPPC64 ? (HasFP ? PPC::X30 : PPC::X31) :
601 (HasFP ? PPC::R30 : PPC::R31);
602 Reg = MRI->getDwarfRegNum(Reg, true);
603 MMI.addFrameInst(
604 MCCFIInstruction::createOffset(FrameLabel, Reg, BPOffset));
605 }
606
Anton Korobeynikov33464912010-11-15 00:06:54 +0000607 if (MustSaveLR) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000608 unsigned Reg = isPPC64 ? PPC::LR8 : PPC::LR;
Bill Wendling99cb6222013-06-18 07:20:20 +0000609 Reg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000610 MMI.addFrameInst(
611 MCCFIInstruction::createOffset(FrameLabel, Reg, LROffset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000612 }
613 }
614
615 MCSymbol *ReadyLabel = 0;
616
617 // If there is a frame pointer, copy R1 into R31
618 if (HasFP) {
619 if (!isPPC64) {
620 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
621 .addReg(PPC::R1)
622 .addReg(PPC::R1);
623 } else {
624 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
625 .addReg(PPC::X1)
626 .addReg(PPC::X1);
627 }
628
629 if (needsFrameMoves) {
630 ReadyLabel = MMI.getContext().CreateTempSymbol();
631
632 // Mark effective beginning of when frame pointer is ready.
633 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
634
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000635 unsigned Reg = HasFP ? (isPPC64 ? PPC::X31 : PPC::R31)
636 : (isPPC64 ? PPC::X1 : PPC::R1);
Bill Wendling99cb6222013-06-18 07:20:20 +0000637 Reg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000638 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(ReadyLabel, Reg));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000639 }
640 }
641
642 if (needsFrameMoves) {
643 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
644
645 // Add callee saved registers to move list.
646 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
647 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000648 unsigned Reg = CSI[I].getReg();
649 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000650
651 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
652 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperc9099502012-04-20 06:31:50 +0000653 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola6e032942011-05-30 20:20:15 +0000654 continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000655
Roman Divacky9d760ae2012-09-12 14:47:47 +0000656 // For SVR4, don't emit a move for the CR spill slot if we haven't
657 // spilled CRs.
658 if (Subtarget.isSVR4ABI()
659 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000660 && MustSaveCRs.empty())
Roman Divacky9d760ae2012-09-12 14:47:47 +0000661 continue;
662
663 // For 64-bit SVR4 when we have spilled CRs, the spill location
664 // is SP+8, not a frame-relative slot.
665 if (Subtarget.isSVR4ABI()
666 && Subtarget.isPPC64()
667 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000668 MMI.addFrameInst(MCCFIInstruction::createOffset(
Bill Wendling99cb6222013-06-18 07:20:20 +0000669 Label, MRI->getDwarfRegNum(PPC::CR2, true), 8));
Roman Divacky9d760ae2012-09-12 14:47:47 +0000670 continue;
671 }
672
673 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000674 MMI.addFrameInst(MCCFIInstruction::createOffset(
Bill Wendling99cb6222013-06-18 07:20:20 +0000675 Label, MRI->getDwarfRegNum(Reg, true), Offset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000676 }
677 }
678}
679
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000680void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000681 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000682 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
683 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000684 const PPCInstrInfo &TII =
685 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkelfe47bf82013-07-17 00:45:52 +0000686 const PPCRegisterInfo *RegInfo =
687 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000688
689 unsigned RetOpcode = MBBI->getOpcode();
690 DebugLoc dl;
691
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000692 assert((RetOpcode == PPC::BLR ||
693 RetOpcode == PPC::TCRETURNri ||
694 RetOpcode == PPC::TCRETURNdi ||
695 RetOpcode == PPC::TCRETURNai ||
696 RetOpcode == PPC::TCRETURNri8 ||
697 RetOpcode == PPC::TCRETURNdi8 ||
698 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000699 "Can only insert epilog into returning blocks");
700
701 // Get alignment info so we know how to restore r1
702 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000703
704 // Get the number of bytes allocated from the FrameInfo.
705 int FrameSize = MFI->getStackSize();
706
707 // Get processor type.
708 bool isPPC64 = Subtarget.isPPC64();
709 // Get operating system
710 bool isDarwinABI = Subtarget.isDarwinABI();
711 // Check if the link register (LR) has been saved.
712 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
713 bool MustSaveLR = FI->mustSaveLR();
Craig Toppera0ec3f92013-07-14 04:42:23 +0000714 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000715 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000716 bool HasFP = hasFP(MF);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000717 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000718
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000719 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000720
721 int FPOffset = 0;
722 if (HasFP) {
723 if (Subtarget.isSVR4ABI()) {
724 MachineFrameInfo *FFI = MF.getFrameInfo();
725 int FPIndex = FI->getFramePointerSaveIndex();
726 assert(FPIndex && "No Frame Pointer Save Slot!");
727 FPOffset = FFI->getObjectOffset(FPIndex);
728 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000729 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000730 }
731 }
732
Hal Finkelfe47bf82013-07-17 00:45:52 +0000733 int BPOffset = 0;
734 if (HasBP) {
735 if (Subtarget.isSVR4ABI()) {
736 MachineFrameInfo *FFI = MF.getFrameInfo();
737 int BPIndex = FI->getBasePointerSaveIndex();
738 assert(BPIndex && "No Base Pointer Save Slot!");
739 BPOffset = FFI->getObjectOffset(BPIndex);
740 } else {
741 BPOffset =
742 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI,
743 HasFP);
744 }
745 }
746
Anton Korobeynikov33464912010-11-15 00:06:54 +0000747 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
748 RetOpcode == PPC::TCRETURNdi ||
749 RetOpcode == PPC::TCRETURNai ||
750 RetOpcode == PPC::TCRETURNri8 ||
751 RetOpcode == PPC::TCRETURNdi8 ||
752 RetOpcode == PPC::TCRETURNai8;
753
754 if (UsesTCRet) {
755 int MaxTCRetDelta = FI->getTailCallSPDelta();
756 MachineOperand &StackAdjust = MBBI->getOperand(1);
757 assert(StackAdjust.isImm() && "Expecting immediate value.");
758 // Adjust stack pointer.
759 int StackAdj = StackAdjust.getImm();
760 int Delta = StackAdj - MaxTCRetDelta;
761 assert((Delta >= 0) && "Delta must be positive");
762 if (MaxTCRetDelta>0)
763 FrameSize += (StackAdj +Delta);
764 else
765 FrameSize += StackAdj;
766 }
767
768 if (FrameSize) {
769 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
770 // on entry to the function. Add this offset back now.
771 if (!isPPC64) {
772 // If this function contained a fastcc call and GuaranteedTailCallOpt is
773 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
774 // call which invalidates the stack pointer value in SP(0). So we use the
775 // value of R31 in this case.
776 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000777 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000778 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
779 .addReg(PPC::R31).addImm(FrameSize);
780 } else if(FI->hasFastCall()) {
781 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
782 .addImm(FrameSize >> 16);
783 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
784 .addReg(PPC::R0, RegState::Kill)
785 .addImm(FrameSize & 0xFFFF);
786 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
787 .addReg(PPC::R1)
788 .addReg(PPC::R31)
789 .addReg(PPC::R0);
790 } else if (isInt<16>(FrameSize) &&
Hal Finkelfe47bf82013-07-17 00:45:52 +0000791 !HasBP &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000792 !MFI->hasVarSizedObjects()) {
793 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
794 .addReg(PPC::R1).addImm(FrameSize);
795 } else {
796 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
797 .addImm(0).addReg(PPC::R1);
798 }
799 } else {
800 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000801 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000802 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
803 .addReg(PPC::X31).addImm(FrameSize);
804 } else if(FI->hasFastCall()) {
805 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
806 .addImm(FrameSize >> 16);
807 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
808 .addReg(PPC::X0, RegState::Kill)
809 .addImm(FrameSize & 0xFFFF);
810 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
811 .addReg(PPC::X1)
812 .addReg(PPC::X31)
813 .addReg(PPC::X0);
Hal Finkelfe47bf82013-07-17 00:45:52 +0000814 } else if (isInt<16>(FrameSize) && !HasBP &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000815 !MFI->hasVarSizedObjects()) {
816 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
817 .addReg(PPC::X1).addImm(FrameSize);
818 } else {
819 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
820 .addImm(0).addReg(PPC::X1);
821 }
822 }
823 }
824
825 if (isPPC64) {
826 if (MustSaveLR)
827 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000828 .addImm(LROffset).addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000829
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000830 if (!MustSaveCRs.empty())
831 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), PPC::X12)
832 .addImm(8).addReg(PPC::X1);
833
Anton Korobeynikov33464912010-11-15 00:06:54 +0000834 if (HasFP)
835 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000836 .addImm(FPOffset).addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000837
Hal Finkelfe47bf82013-07-17 00:45:52 +0000838 if (HasBP)
839 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), HasFP ? PPC::X30 : PPC::X31)
840 .addImm(BPOffset).addReg(PPC::X1);
841
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000842 if (!MustSaveCRs.empty())
843 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Ulrich Weigand33efedc2013-07-03 17:59:07 +0000844 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000845 .addReg(PPC::X12, getKillRegState(i == e-1));
846
Anton Korobeynikov33464912010-11-15 00:06:54 +0000847 if (MustSaveLR)
848 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
849 } else {
850 if (MustSaveLR)
851 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
852 .addImm(LROffset).addReg(PPC::R1);
853
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000854 assert(MustSaveCRs.empty() &&
855 "Epilogue CR restoring supported only in 64-bit mode");
856
Anton Korobeynikov33464912010-11-15 00:06:54 +0000857 if (HasFP)
858 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
859 .addImm(FPOffset).addReg(PPC::R1);
860
Hal Finkelfe47bf82013-07-17 00:45:52 +0000861 if (HasBP)
862 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), HasFP ? PPC::R30 : PPC::R31)
863 .addImm(FPOffset).addReg(PPC::R1);
864
Anton Korobeynikov33464912010-11-15 00:06:54 +0000865 if (MustSaveLR)
866 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
867 }
868
869 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
870 // call optimization
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000871 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000872 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
873 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
874 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
875 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
876 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
877 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
878 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
879 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
880 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
881 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
882
883 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
884 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
885 .addReg(StackReg).addImm(CallerAllocatedAmt);
886 } else {
887 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
888 .addImm(CallerAllocatedAmt >> 16);
889 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
890 .addReg(TmpReg, RegState::Kill)
891 .addImm(CallerAllocatedAmt & 0xFFFF);
892 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
893 .addReg(StackReg)
894 .addReg(FPReg)
895 .addReg(TmpReg);
896 }
897 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000898 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000899 MachineOperand &JumpTarget = MBBI->getOperand(0);
900 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
901 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
902 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000903 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000904 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
905 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
906 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000907 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000908 MachineOperand &JumpTarget = MBBI->getOperand(0);
909 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
910 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000911 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000912 MachineOperand &JumpTarget = MBBI->getOperand(0);
913 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
914 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
915 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000916 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000917 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
918 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
919 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000920 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000921 MachineOperand &JumpTarget = MBBI->getOperand(0);
922 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
923 }
924}
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000925
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000926/// MustSaveLR - Return true if this function requires that we save the LR
927/// register onto the stack in the prolog and restore it in the epilog of the
928/// function.
929static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
930 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
931
932 // We need a save/restore of LR if there is any def of LR (which is
933 // defined by calls, including the PIC setup sequence), or if there is
934 // some use of the LR stack slot (e.g. for builtin_return_address).
935 // (LR comes in 32 and 64 bit versions.)
936 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
937 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
938}
939
940void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000941PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000942 RegScavenger *) const {
Hal Finkelfe47bf82013-07-17 00:45:52 +0000943 const PPCRegisterInfo *RegInfo =
944 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000945
946 // Save and clear the LR state.
947 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
948 unsigned LR = RegInfo->getRARegister();
949 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000950 MachineRegisterInfo &MRI = MF.getRegInfo();
951 MRI.setPhysRegUnused(LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000952
953 // Save R31 if necessary
954 int FPSI = FI->getFramePointerSaveIndex();
955 bool isPPC64 = Subtarget.isPPC64();
956 bool isDarwinABI = Subtarget.isDarwinABI();
957 MachineFrameInfo *MFI = MF.getFrameInfo();
958
959 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000960 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000961 // Find out what the fix offset of the frame pointer save area.
962 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
963 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000964 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000965 // Save the result.
966 FI->setFramePointerSaveIndex(FPSI);
967 }
968
Hal Finkelfe47bf82013-07-17 00:45:52 +0000969 int BPSI = FI->getBasePointerSaveIndex();
970 if (!BPSI && RegInfo->hasBasePointer(MF)) {
971 int BPOffset = getBasePointerSaveOffset(isPPC64, isDarwinABI, needsFP(MF));
972 // Allocate the frame index for the base pointer save area.
973 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
974 // Save the result.
975 FI->setBasePointerSaveIndex(BPSI);
976 }
977
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000978 // Reserve stack space to move the linkage area to in case of a tail call.
979 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000980 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
981 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000982 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000983 }
984
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000985 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
986 // function uses CR 2, 3, or 4.
987 if (!isPPC64 && !isDarwinABI &&
988 (MRI.isPhysRegUsed(PPC::CR2) ||
989 MRI.isPhysRegUsed(PPC::CR3) ||
990 MRI.isPhysRegUsed(PPC::CR4))) {
991 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
992 FI->setCRSpillFrameIndex(FrameIdx);
993 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000994}
995
Hal Finkel3080d232013-03-14 20:33:40 +0000996void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000997 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000998 // Early exit if not using the SVR4 ABI.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000999 if (!Subtarget.isSVR4ABI()) {
1000 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001001 return;
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001002 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001003
1004 // Get callee saved register information.
1005 MachineFrameInfo *FFI = MF.getFrameInfo();
1006 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1007
1008 // Early exit if no callee saved registers are modified!
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +00001009 if (CSI.empty() && !needsFP(MF)) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001010 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001011 return;
1012 }
1013
1014 unsigned MinGPR = PPC::R31;
1015 unsigned MinG8R = PPC::X31;
1016 unsigned MinFPR = PPC::F31;
1017 unsigned MinVR = PPC::V31;
1018
1019 bool HasGPSaveArea = false;
1020 bool HasG8SaveArea = false;
1021 bool HasFPSaveArea = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001022 bool HasVRSAVESaveArea = false;
1023 bool HasVRSaveArea = false;
1024
1025 SmallVector<CalleeSavedInfo, 18> GPRegs;
1026 SmallVector<CalleeSavedInfo, 18> G8Regs;
1027 SmallVector<CalleeSavedInfo, 18> FPRegs;
1028 SmallVector<CalleeSavedInfo, 18> VRegs;
1029
1030 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1031 unsigned Reg = CSI[i].getReg();
Craig Topperc9099502012-04-20 06:31:50 +00001032 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001033 HasGPSaveArea = true;
1034
1035 GPRegs.push_back(CSI[i]);
1036
1037 if (Reg < MinGPR) {
1038 MinGPR = Reg;
1039 }
Craig Topperc9099502012-04-20 06:31:50 +00001040 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001041 HasG8SaveArea = true;
1042
1043 G8Regs.push_back(CSI[i]);
1044
1045 if (Reg < MinG8R) {
1046 MinG8R = Reg;
1047 }
Craig Topperc9099502012-04-20 06:31:50 +00001048 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001049 HasFPSaveArea = true;
1050
1051 FPRegs.push_back(CSI[i]);
1052
1053 if (Reg < MinFPR) {
1054 MinFPR = Reg;
1055 }
Craig Topperc9099502012-04-20 06:31:50 +00001056 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1057 PPC::CRRCRegClass.contains(Reg)) {
Roman Divacky9d760ae2012-09-12 14:47:47 +00001058 ; // do nothing, as we already know whether CRs are spilled
Craig Topperc9099502012-04-20 06:31:50 +00001059 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001060 HasVRSAVESaveArea = true;
Craig Topperc9099502012-04-20 06:31:50 +00001061 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001062 HasVRSaveArea = true;
1063
1064 VRegs.push_back(CSI[i]);
1065
1066 if (Reg < MinVR) {
1067 MinVR = Reg;
1068 }
1069 } else {
1070 llvm_unreachable("Unknown RegisterClass!");
1071 }
1072 }
1073
1074 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Hal Finkelaa6047d2013-03-26 20:08:20 +00001075 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001076
1077 int64_t LowerBound = 0;
1078
1079 // Take into account stack space reserved for tail calls.
1080 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001081 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1082 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001083 LowerBound = TCSPDelta;
1084 }
1085
1086 // The Floating-point register save area is right below the back chain word
1087 // of the previous stack frame.
1088 if (HasFPSaveArea) {
1089 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1090 int FI = FPRegs[i].getFrameIdx();
1091
1092 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1093 }
1094
Hal Finkelaa6047d2013-03-26 20:08:20 +00001095 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001096 }
1097
1098 // Check whether the frame pointer register is allocated. If so, make sure it
1099 // is spilled to the correct offset.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +00001100 if (needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001101 HasGPSaveArea = true;
1102
1103 int FI = PFI->getFramePointerSaveIndex();
1104 assert(FI && "No Frame Pointer Save Slot!");
1105
1106 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1107 }
1108
Hal Finkelfe47bf82013-07-17 00:45:52 +00001109 const PPCRegisterInfo *RegInfo =
1110 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
1111 if (RegInfo->hasBasePointer(MF)) {
1112 HasGPSaveArea = true;
1113
1114 int FI = PFI->getBasePointerSaveIndex();
1115 assert(FI && "No Base Pointer Save Slot!");
1116
1117 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1118 }
1119
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001120 // General register save area starts right below the Floating-point
1121 // register save area.
1122 if (HasGPSaveArea || HasG8SaveArea) {
1123 // Move general register save area spill slots down, taking into account
1124 // the size of the Floating-point register save area.
1125 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1126 int FI = GPRegs[i].getFrameIdx();
1127
1128 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1129 }
1130
1131 // Move general register save area spill slots down, taking into account
1132 // the size of the Floating-point register save area.
1133 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1134 int FI = G8Regs[i].getFrameIdx();
1135
1136 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1137 }
1138
1139 unsigned MinReg =
Hal Finkelaa6047d2013-03-26 20:08:20 +00001140 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1141 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001142
1143 if (Subtarget.isPPC64()) {
1144 LowerBound -= (31 - MinReg + 1) * 8;
1145 } else {
1146 LowerBound -= (31 - MinReg + 1) * 4;
1147 }
1148 }
1149
Roman Divacky9d760ae2012-09-12 14:47:47 +00001150 // For 32-bit only, the CR save area is below the general register
1151 // save area. For 64-bit SVR4, the CR save area is addressed relative
1152 // to the stack pointer and hence does not need an adjustment here.
1153 // Only CR2 (the first nonvolatile spilled) has an associated frame
1154 // index so that we have a single uniform save area.
1155 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001156 // Adjust the frame index of the CR spill slot.
1157 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1158 unsigned Reg = CSI[i].getReg();
1159
Roman Divacky9d760ae2012-09-12 14:47:47 +00001160 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
1161 // Leave Darwin logic as-is.
1162 || (!Subtarget.isSVR4ABI() &&
1163 (PPC::CRBITRCRegClass.contains(Reg) ||
1164 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001165 int FI = CSI[i].getFrameIdx();
1166
1167 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1168 }
1169 }
1170
1171 LowerBound -= 4; // The CR save area is always 4 bytes long.
1172 }
1173
1174 if (HasVRSAVESaveArea) {
1175 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1176 // which have the VRSAVE register class?
1177 // Adjust the frame index of the VRSAVE spill slot.
1178 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1179 unsigned Reg = CSI[i].getReg();
1180
Craig Topperc9099502012-04-20 06:31:50 +00001181 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001182 int FI = CSI[i].getFrameIdx();
1183
1184 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1185 }
1186 }
1187
1188 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1189 }
1190
1191 if (HasVRSaveArea) {
1192 // Insert alignment padding, we need 16-byte alignment.
1193 LowerBound = (LowerBound - 15) & ~(15);
1194
1195 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1196 int FI = VRegs[i].getFrameIdx();
1197
1198 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1199 }
1200 }
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001201
1202 addScavengingSpillSlot(MF, RS);
1203}
1204
1205void
1206PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1207 RegScavenger *RS) const {
1208 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1209 // a large stack, which will require scavenging a register to materialize a
1210 // large offset.
1211
1212 // We need to have a scavenger spill slot for spills if the frame size is
1213 // large. In case there is no free register for large-offset addressing,
1214 // this slot is used for the necessary emergency spill. Also, we need the
1215 // slot for dynamic stack allocations.
1216
1217 // The scavenger might be invoked if the frame offset does not fit into
1218 // the 16-bit immediate. We don't know the complete frame size here
1219 // because we've not yet computed callee-saved register spills or the
1220 // needed alignment padding.
1221 unsigned StackSize = determineFrameLayout(MF, false, true);
1222 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkel3f2c0472013-03-23 22:06:03 +00001223 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1224 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001225 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1226 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1227 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkeldc3beb92013-03-22 23:32:27 +00001228 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001229 RC->getAlignment(),
1230 false));
Hal Finkel01f99d22013-03-26 18:57:22 +00001231
1232 // These kinds of spills might need two registers.
1233 if (spillsCR(MF) || spillsVRSAVE(MF))
1234 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1235 RC->getAlignment(),
1236 false));
1237
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001238 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001239}
Roman Divacky9d760ae2012-09-12 14:47:47 +00001240
1241bool
1242PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1243 MachineBasicBlock::iterator MI,
1244 const std::vector<CalleeSavedInfo> &CSI,
1245 const TargetRegisterInfo *TRI) const {
1246
1247 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1248 // Return false otherwise to maintain pre-existing behavior.
1249 if (!Subtarget.isSVR4ABI())
1250 return false;
1251
1252 MachineFunction *MF = MBB.getParent();
1253 const PPCInstrInfo &TII =
1254 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1255 DebugLoc DL;
1256 bool CRSpilled = false;
Hal Finkel63496f62013-04-13 23:06:15 +00001257 MachineInstrBuilder CRMIB;
Roman Divacky9d760ae2012-09-12 14:47:47 +00001258
1259 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1260 unsigned Reg = CSI[i].getReg();
Hal Finkel6a636a82013-06-28 22:29:56 +00001261 // Only Darwin actually uses the VRSAVE register, but it can still appear
1262 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1263 // Darwin, ignore it.
1264 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1265 continue;
1266
Roman Divacky9d760ae2012-09-12 14:47:47 +00001267 // CR2 through CR4 are the nonvolatile CR fields.
1268 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1269
Roman Divacky9d760ae2012-09-12 14:47:47 +00001270 // Add the callee-saved register as live-in; it's killed at the spill.
1271 MBB.addLiveIn(Reg);
1272
Hal Finkel63496f62013-04-13 23:06:15 +00001273 if (CRSpilled && IsCRField) {
1274 CRMIB.addReg(Reg, RegState::ImplicitKill);
1275 continue;
1276 }
1277
Roman Divacky9d760ae2012-09-12 14:47:47 +00001278 // Insert the spill to the stack frame.
1279 if (IsCRField) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001280 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divacky9d760ae2012-09-12 14:47:47 +00001281 if (Subtarget.isPPC64()) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001282 // The actual spill will happen at the start of the prologue.
1283 FuncInfo->addMustSaveCR(Reg);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001284 } else {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001285 CRSpilled = true;
Bill Schmidtded53bf2013-05-14 16:08:32 +00001286 FuncInfo->setSpillsCR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001287
Roman Divacky9d760ae2012-09-12 14:47:47 +00001288 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1289 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
Hal Finkel63496f62013-04-13 23:06:15 +00001290 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
1291 .addReg(Reg, RegState::ImplicitKill);
1292
1293 MBB.insert(MI, CRMIB);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001294 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1295 .addReg(PPC::R12,
1296 getKillRegState(true)),
1297 CSI[i].getFrameIdx()));
1298 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001299 } else {
1300 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1301 TII.storeRegToStackSlot(MBB, MI, Reg, true,
1302 CSI[i].getFrameIdx(), RC, TRI);
1303 }
1304 }
1305 return true;
1306}
1307
1308static void
Hal Finkelb99c9952013-04-13 08:09:20 +00001309restoreCRs(bool isPPC64, bool is31,
1310 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001311 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1312 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1313
1314 MachineFunction *MF = MBB.getParent();
1315 const PPCInstrInfo &TII =
1316 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1317 DebugLoc DL;
1318 unsigned RestoreOp, MoveReg;
1319
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001320 if (isPPC64)
1321 // This is handled during epilogue generation.
1322 return;
1323 else {
Roman Divacky9d760ae2012-09-12 14:47:47 +00001324 // 32-bit: FP-relative
1325 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
1326 PPC::R12),
1327 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand33efedc2013-07-03 17:59:07 +00001328 RestoreOp = PPC::MTOCRF;
Roman Divacky9d760ae2012-09-12 14:47:47 +00001329 MoveReg = PPC::R12;
1330 }
1331
1332 if (CR2Spilled)
1333 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkeld957f952013-03-28 03:38:16 +00001334 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001335
1336 if (CR3Spilled)
1337 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkeld957f952013-03-28 03:38:16 +00001338 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001339
1340 if (CR4Spilled)
1341 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkeld957f952013-03-28 03:38:16 +00001342 .addReg(MoveReg, getKillRegState(true)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001343}
1344
Eli Bendersky700ed802013-02-21 20:05:00 +00001345void PPCFrameLowering::
1346eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1347 MachineBasicBlock::iterator I) const {
1348 const PPCInstrInfo &TII =
1349 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1350 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1351 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1352 // Add (actually subtract) back the amount the callee popped on return.
1353 if (int CalleeAmt = I->getOperand(1).getImm()) {
1354 bool is64Bit = Subtarget.isPPC64();
1355 CalleeAmt *= -1;
1356 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1357 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1358 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1359 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1360 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1361 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1362 MachineInstr *MI = I;
1363 DebugLoc dl = MI->getDebugLoc();
1364
1365 if (isInt<16>(CalleeAmt)) {
1366 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1367 .addReg(StackReg, RegState::Kill)
1368 .addImm(CalleeAmt);
1369 } else {
1370 MachineBasicBlock::iterator MBBI = I;
1371 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1372 .addImm(CalleeAmt >> 16);
1373 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1374 .addReg(TmpReg, RegState::Kill)
1375 .addImm(CalleeAmt & 0xFFFF);
1376 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1377 .addReg(StackReg, RegState::Kill)
1378 .addReg(TmpReg);
1379 }
1380 }
1381 }
1382 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1383 MBB.erase(I);
1384}
1385
Roman Divacky9d760ae2012-09-12 14:47:47 +00001386bool
1387PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1388 MachineBasicBlock::iterator MI,
1389 const std::vector<CalleeSavedInfo> &CSI,
1390 const TargetRegisterInfo *TRI) const {
1391
1392 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1393 // Return false otherwise to maintain pre-existing behavior.
1394 if (!Subtarget.isSVR4ABI())
1395 return false;
1396
1397 MachineFunction *MF = MBB.getParent();
1398 const PPCInstrInfo &TII =
1399 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1400 bool CR2Spilled = false;
1401 bool CR3Spilled = false;
1402 bool CR4Spilled = false;
1403 unsigned CSIIndex = 0;
1404
1405 // Initialize insertion-point logic; we will be restoring in reverse
1406 // order of spill.
1407 MachineBasicBlock::iterator I = MI, BeforeI = I;
1408 bool AtStart = I == MBB.begin();
1409
1410 if (!AtStart)
1411 --BeforeI;
1412
1413 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1414 unsigned Reg = CSI[i].getReg();
1415
Hal Finkel6a636a82013-06-28 22:29:56 +00001416 // Only Darwin actually uses the VRSAVE register, but it can still appear
1417 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1418 // Darwin, ignore it.
1419 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1420 continue;
1421
Roman Divacky9d760ae2012-09-12 14:47:47 +00001422 if (Reg == PPC::CR2) {
1423 CR2Spilled = true;
1424 // The spill slot is associated only with CR2, which is the
1425 // first nonvolatile spilled. Save it here.
1426 CSIIndex = i;
1427 continue;
1428 } else if (Reg == PPC::CR3) {
1429 CR3Spilled = true;
1430 continue;
1431 } else if (Reg == PPC::CR4) {
1432 CR4Spilled = true;
1433 continue;
1434 } else {
1435 // When we first encounter a non-CR register after seeing at
1436 // least one CR register, restore all spilled CRs together.
1437 if ((CR2Spilled || CR3Spilled || CR4Spilled)
1438 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkelb99c9952013-04-13 08:09:20 +00001439 bool is31 = needsFP(*MF);
1440 restoreCRs(Subtarget.isPPC64(), is31,
1441 CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001442 MBB, I, CSI, CSIIndex);
1443 CR2Spilled = CR3Spilled = CR4Spilled = false;
1444 }
1445
1446 // Default behavior for non-CR saves.
1447 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1448 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
1449 RC, TRI);
1450 assert(I != MBB.begin() &&
1451 "loadRegFromStackSlot didn't insert any code!");
1452 }
1453
1454 // Insert in reverse order.
1455 if (AtStart)
1456 I = MBB.begin();
1457 else {
1458 I = BeforeI;
1459 ++I;
1460 }
1461 }
1462
1463 // If we haven't yet spilled the CRs, do so now.
Hal Finkelb99c9952013-04-13 08:09:20 +00001464 if (CR2Spilled || CR3Spilled || CR4Spilled) {
1465 bool is31 = needsFP(*MF);
1466 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001467 MBB, I, CSI, CSIIndex);
Hal Finkelb99c9952013-04-13 08:09:20 +00001468 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001469
1470 return true;
1471}
1472