blob: 1f0c3c4b5d8b450aeb7d2e8dfe9c3d8e64e59c3b [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();
Hal Finkelaa6047d2013-03-26 20:08:20 +0000106 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000107 DebugLoc dl = MI->getDebugLoc();
108
109 unsigned UsedRegMask = 0;
110 for (unsigned i = 0; i != 32; ++i)
111 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
112 UsedRegMask |= 1 << (31-i);
113
114 // Live in and live out values already must be in the mask, so don't bother
115 // marking them.
116 for (MachineRegisterInfo::livein_iterator
117 I = MF->getRegInfo().livein_begin(),
118 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelaa6047d2013-03-26 20:08:20 +0000119 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000120 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
121 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
122 }
Jakob Stoklund Olesen0a9d1d32013-02-05 17:40:36 +0000123
124 // Live out registers appear as use operands on return instructions.
125 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
126 UsedRegMask != 0 && BI != BE; ++BI) {
127 const MachineBasicBlock &MBB = *BI;
128 if (MBB.empty() || !MBB.back().isReturn())
129 continue;
130 const MachineInstr &Ret = MBB.back();
131 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
132 const MachineOperand &MO = Ret.getOperand(I);
133 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
134 continue;
Hal Finkelaa6047d2013-03-26 20:08:20 +0000135 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesen0a9d1d32013-02-05 17:40:36 +0000136 UsedRegMask &= ~(1 << (31-RegNo));
137 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000138 }
139
140 // If no registers are used, turn this into a copy.
141 if (UsedRegMask == 0) {
142 // Remove all VRSAVE code.
143 RemoveVRSaveCode(MI);
144 return;
145 }
146
147 unsigned SrcReg = MI->getOperand(1).getReg();
148 unsigned DstReg = MI->getOperand(0).getReg();
149
150 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
151 if (DstReg != SrcReg)
152 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
153 .addReg(SrcReg)
154 .addImm(UsedRegMask);
155 else
156 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
157 .addReg(SrcReg, RegState::Kill)
158 .addImm(UsedRegMask);
159 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
160 if (DstReg != SrcReg)
161 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
162 .addReg(SrcReg)
163 .addImm(UsedRegMask >> 16);
164 else
165 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
166 .addReg(SrcReg, RegState::Kill)
167 .addImm(UsedRegMask >> 16);
168 } else {
169 if (DstReg != SrcReg)
170 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
171 .addReg(SrcReg)
172 .addImm(UsedRegMask >> 16);
173 else
174 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
175 .addReg(SrcReg, RegState::Kill)
176 .addImm(UsedRegMask >> 16);
177
178 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
179 .addReg(DstReg, RegState::Kill)
180 .addImm(UsedRegMask & 0xFFFF);
181 }
182
183 // Remove the old UPDATE_VRSAVE instruction.
184 MI->eraseFromParent();
185}
186
Roman Divacky9d760ae2012-09-12 14:47:47 +0000187static bool spillsCR(const MachineFunction &MF) {
188 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
189 return FuncInfo->isCRSpilled();
190}
191
Hal Finkel3f2c0472013-03-23 22:06:03 +0000192static bool spillsVRSAVE(const MachineFunction &MF) {
193 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
194 return FuncInfo->isVRSAVESpilled();
195}
196
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000197static bool hasSpills(const MachineFunction &MF) {
198 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
199 return FuncInfo->hasSpills();
200}
201
Hal Finkel32497292013-03-17 04:43:44 +0000202static bool hasNonRISpills(const MachineFunction &MF) {
203 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
204 return FuncInfo->hasNonRISpills();
205}
206
Anton Korobeynikov33464912010-11-15 00:06:54 +0000207/// determineFrameLayout - Determine the size of the frame and maximum call
208/// frame size.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000209unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
210 bool UpdateMF,
211 bool UseEstimate) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000212 MachineFrameInfo *MFI = MF.getFrameInfo();
213
214 // Get the number of bytes to allocate from the FrameInfo
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000215 unsigned FrameSize =
216 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000217
218 // Get the alignments provided by the target, and the maximum alignment
219 // (if any) of the fixed frame objects.
220 unsigned MaxAlign = MFI->getMaxAlignment();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000221 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000222 unsigned AlignMask = TargetAlign - 1; //
223
224 // If we are a leaf function, and use up to 224 bytes of stack space,
225 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000226 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt65396822013-02-26 21:28:57 +0000227 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
228 // stackless code if all local vars are reg-allocated.
Bill Wendling831737d2012-12-30 10:32:01 +0000229 bool DisableRedZone = MF.getFunction()->getAttributes().
230 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000231 if (!DisableRedZone &&
Bill Schmidt65396822013-02-26 21:28:57 +0000232 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
233 !Subtarget.isSVR4ABI() || // allocated locals.
234 FrameSize == 0) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000235 FrameSize <= 224 && // Fits in red zone.
236 !MFI->hasVarSizedObjects() && // No dynamic alloca.
237 !MFI->adjustsStack() && // No calls.
238 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment.
239 // No need for frame
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000240 if (UpdateMF)
241 MFI->setStackSize(0);
242 return 0;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000243 }
244
245 // Get the maximum call frame size of all the calls.
246 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
247
248 // Maximum call frame needs to be at least big enough for linkage and 8 args.
249 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
250 Subtarget.isDarwinABI());
251 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
252
253 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
254 // that allocations will be aligned.
255 if (MFI->hasVarSizedObjects())
256 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
257
258 // Update maximum call frame size.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000259 if (UpdateMF)
260 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000261
262 // Include call frame size in total.
263 FrameSize += maxCallFrameSize;
264
265 // Make sure the frame is aligned.
266 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
267
268 // Update frame info.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000269 if (UpdateMF)
270 MFI->setStackSize(FrameSize);
271
272 return FrameSize;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000273}
274
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000275// hasFP - Return true if the specified function actually has a dedicated frame
276// pointer register.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000277bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000278 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000279 // FIXME: This is pretty much broken by design: hasFP() might be called really
280 // early, before the stack layout was calculated and thus hasFP() might return
281 // true or false here depending on the time of call.
282 return (MFI->getStackSize()) && needsFP(MF);
283}
284
285// needsFP - Return true if the specified function should have a dedicated frame
286// pointer register. This is true if the function has variable sized allocas or
287// if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000288bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000289 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000290
291 // Naked functions have no stack frame pushed, so we don't have a frame
292 // pointer.
Bill Wendling831737d2012-12-30 10:32:01 +0000293 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
294 Attribute::Naked))
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000295 return false;
296
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000297 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
298 MFI->hasVarSizedObjects() ||
299 (MF.getTarget().Options.GuaranteedTailCallOpt &&
300 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000301}
302
Hal Finkele9cc0a02013-03-21 19:03:19 +0000303void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
304 bool is31 = needsFP(MF);
305 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
306 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
307
308 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
309 BI != BE; ++BI)
310 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
311 --MBBI;
312 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
313 MachineOperand &MO = MBBI->getOperand(I);
314 if (!MO.isReg())
315 continue;
316
317 switch (MO.getReg()) {
318 case PPC::FP:
319 MO.setReg(FPReg);
320 break;
321 case PPC::FP8:
322 MO.setReg(FP8Reg);
323 break;
324 }
325 }
326 }
327}
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000328
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000329void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000330 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
331 MachineBasicBlock::iterator MBBI = MBB.begin();
332 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000333 const PPCInstrInfo &TII =
334 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
335
336 MachineModuleInfo &MMI = MF.getMMI();
337 DebugLoc dl;
338 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000339 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000340
341 // Prepare for frame info.
342 MCSymbol *FrameLabel = 0;
343
344 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
345 // process it.
Bill Schmidta5d0ab52012-10-10 20:54:15 +0000346 if (!Subtarget.isSVR4ABI())
347 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
348 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
349 HandleVRSaveUpdate(MBBI, TII);
350 break;
351 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000352 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000353
354 // Move MBBI back to the beginning of the function.
355 MBBI = MBB.begin();
356
357 // Work out frame sizes.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000358 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000359 int NegFrameSize = -FrameSize;
360
Hal Finkele9cc0a02013-03-21 19:03:19 +0000361 if (MFI->isFrameAddressTaken())
362 replaceFPWithRealFP(MF);
363
Anton Korobeynikov33464912010-11-15 00:06:54 +0000364 // Get processor type.
365 bool isPPC64 = Subtarget.isPPC64();
366 // Get operating system
367 bool isDarwinABI = Subtarget.isDarwinABI();
368 // Check if the link register (LR) must be saved.
369 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
370 bool MustSaveLR = FI->mustSaveLR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000371 const SmallVector<unsigned, 3> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000372 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000373 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000374
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000375 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000376
377 int FPOffset = 0;
378 if (HasFP) {
379 if (Subtarget.isSVR4ABI()) {
380 MachineFrameInfo *FFI = MF.getFrameInfo();
381 int FPIndex = FI->getFramePointerSaveIndex();
382 assert(FPIndex && "No Frame Pointer Save Slot!");
383 FPOffset = FFI->getObjectOffset(FPIndex);
384 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000385 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000386 }
387 }
388
389 if (isPPC64) {
390 if (MustSaveLR)
391 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
392
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000393 if (!MustSaveCRs.empty()) {
394 MachineInstrBuilder MIB =
395 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), PPC::X12);
396 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
397 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
398 }
399
Anton Korobeynikov33464912010-11-15 00:06:54 +0000400 if (HasFP)
401 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
402 .addReg(PPC::X31)
403 .addImm(FPOffset/4)
404 .addReg(PPC::X1);
405
406 if (MustSaveLR)
407 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
408 .addReg(PPC::X0)
409 .addImm(LROffset / 4)
410 .addReg(PPC::X1);
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000411
412 if (!MustSaveCRs.empty())
413 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
414 .addReg(PPC::X12, getKillRegState(true))
415 .addImm(8)
416 .addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000417 } else {
418 if (MustSaveLR)
419 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
420
421 if (HasFP)
Hal Finkelb8f2f292012-05-19 21:52:55 +0000422 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative
423 // offsets of R1 is not allowed.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000424 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
425 .addReg(PPC::R31)
426 .addImm(FPOffset)
427 .addReg(PPC::R1);
428
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000429 assert(MustSaveCRs.empty() &&
430 "Prologue CR saving supported only in 64-bit mode");
431
Anton Korobeynikov33464912010-11-15 00:06:54 +0000432 if (MustSaveLR)
433 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
434 .addReg(PPC::R0)
435 .addImm(LROffset)
436 .addReg(PPC::R1);
437 }
438
439 // Skip if a leaf routine.
440 if (!FrameSize) return;
441
442 // Get stack alignments.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000443 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000444 unsigned MaxAlign = MFI->getMaxAlignment();
445
446 // Adjust stack pointer: r1 += NegFrameSize.
447 // If there is a preferred stack alignment, align R1 now
448 if (!isPPC64) {
449 // PPC32.
450 if (ALIGN_STACK && MaxAlign > TargetAlign) {
451 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
452 "Invalid alignment!");
453 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
454
455 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
456 .addReg(PPC::R1)
457 .addImm(0)
458 .addImm(32 - Log2_32(MaxAlign))
459 .addImm(31);
460 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0)
461 .addReg(PPC::R0, RegState::Kill)
462 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000463 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000464 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000465 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000466 .addReg(PPC::R0);
467 } else if (isInt<16>(NegFrameSize)) {
468 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
469 .addReg(PPC::R1)
470 .addImm(NegFrameSize)
471 .addReg(PPC::R1);
472 } else {
473 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
474 .addImm(NegFrameSize >> 16);
475 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
476 .addReg(PPC::R0, RegState::Kill)
477 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000478 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000479 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000480 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000481 .addReg(PPC::R0);
482 }
483 } else { // PPC64.
484 if (ALIGN_STACK && MaxAlign > TargetAlign) {
485 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
486 "Invalid alignment!");
487 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
488
489 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
490 .addReg(PPC::X1)
491 .addImm(0)
492 .addImm(64 - Log2_32(MaxAlign));
493 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
494 .addReg(PPC::X0)
495 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000496 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000497 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000498 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000499 .addReg(PPC::X0);
500 } else if (isInt<16>(NegFrameSize)) {
501 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
502 .addReg(PPC::X1)
503 .addImm(NegFrameSize / 4)
504 .addReg(PPC::X1);
505 } else {
506 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
507 .addImm(NegFrameSize >> 16);
508 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
509 .addReg(PPC::X0, RegState::Kill)
510 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000511 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000512 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000513 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000514 .addReg(PPC::X0);
515 }
516 }
517
Anton Korobeynikov33464912010-11-15 00:06:54 +0000518 // Add the "machine moves" for the instructions we generated above, but in
519 // reverse order.
520 if (needsFrameMoves) {
521 // Mark effective beginning of when frame pointer becomes valid.
522 FrameLabel = MMI.getContext().CreateTempSymbol();
523 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
524
525 // Show update of SP.
526 if (NegFrameSize) {
527 MachineLocation SPDst(MachineLocation::VirtualFP);
528 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +0000529 MMI.addFrameMove(FrameLabel, SPDst, SPSrc);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000530 } else {
531 MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +0000532 MMI.addFrameMove(FrameLabel, SP, SP);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000533 }
534
535 if (HasFP) {
536 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
537 MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +0000538 MMI.addFrameMove(FrameLabel, FPDst, FPSrc);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000539 }
540
541 if (MustSaveLR) {
542 MachineLocation LRDst(MachineLocation::VirtualFP, LROffset);
543 MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +0000544 MMI.addFrameMove(FrameLabel, LRDst, LRSrc);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000545 }
546 }
547
548 MCSymbol *ReadyLabel = 0;
549
550 // If there is a frame pointer, copy R1 into R31
551 if (HasFP) {
552 if (!isPPC64) {
553 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
554 .addReg(PPC::R1)
555 .addReg(PPC::R1);
556 } else {
557 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
558 .addReg(PPC::X1)
559 .addReg(PPC::X1);
560 }
561
562 if (needsFrameMoves) {
563 ReadyLabel = MMI.getContext().CreateTempSymbol();
564
565 // Mark effective beginning of when frame pointer is ready.
566 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
567
568 MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) :
569 (isPPC64 ? PPC::X1 : PPC::R1));
570 MachineLocation FPSrc(MachineLocation::VirtualFP);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +0000571 MMI.addFrameMove(ReadyLabel, FPDst, FPSrc);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000572 }
573 }
574
575 if (needsFrameMoves) {
576 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
577
578 // Add callee saved registers to move list.
579 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
580 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000581 unsigned Reg = CSI[I].getReg();
582 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000583
584 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
585 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperc9099502012-04-20 06:31:50 +0000586 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola6e032942011-05-30 20:20:15 +0000587 continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000588
Roman Divacky9d760ae2012-09-12 14:47:47 +0000589 // For SVR4, don't emit a move for the CR spill slot if we haven't
590 // spilled CRs.
591 if (Subtarget.isSVR4ABI()
592 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000593 && MustSaveCRs.empty())
Roman Divacky9d760ae2012-09-12 14:47:47 +0000594 continue;
595
596 // For 64-bit SVR4 when we have spilled CRs, the spill location
597 // is SP+8, not a frame-relative slot.
598 if (Subtarget.isSVR4ABI()
599 && Subtarget.isPPC64()
600 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
601 MachineLocation CSDst(PPC::X1, 8);
602 MachineLocation CSSrc(PPC::CR2);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +0000603 MMI.addFrameMove(Label, CSDst, CSSrc);
Roman Divacky9d760ae2012-09-12 14:47:47 +0000604 continue;
605 }
606
607 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000608 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
609 MachineLocation CSSrc(Reg);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +0000610 MMI.addFrameMove(Label, CSDst, CSSrc);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000611 }
612 }
613}
614
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000615void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000616 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000617 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
618 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000619 const PPCInstrInfo &TII =
620 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
621
622 unsigned RetOpcode = MBBI->getOpcode();
623 DebugLoc dl;
624
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000625 assert((RetOpcode == PPC::BLR ||
626 RetOpcode == PPC::TCRETURNri ||
627 RetOpcode == PPC::TCRETURNdi ||
628 RetOpcode == PPC::TCRETURNai ||
629 RetOpcode == PPC::TCRETURNri8 ||
630 RetOpcode == PPC::TCRETURNdi8 ||
631 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000632 "Can only insert epilog into returning blocks");
633
634 // Get alignment info so we know how to restore r1
635 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000636 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000637 unsigned MaxAlign = MFI->getMaxAlignment();
638
639 // Get the number of bytes allocated from the FrameInfo.
640 int FrameSize = MFI->getStackSize();
641
642 // Get processor type.
643 bool isPPC64 = Subtarget.isPPC64();
644 // Get operating system
645 bool isDarwinABI = Subtarget.isDarwinABI();
646 // Check if the link register (LR) has been saved.
647 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
648 bool MustSaveLR = FI->mustSaveLR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000649 const SmallVector<unsigned, 3> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000650 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000651 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000652
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000653 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000654
655 int FPOffset = 0;
656 if (HasFP) {
657 if (Subtarget.isSVR4ABI()) {
658 MachineFrameInfo *FFI = MF.getFrameInfo();
659 int FPIndex = FI->getFramePointerSaveIndex();
660 assert(FPIndex && "No Frame Pointer Save Slot!");
661 FPOffset = FFI->getObjectOffset(FPIndex);
662 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000663 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000664 }
665 }
666
667 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
668 RetOpcode == PPC::TCRETURNdi ||
669 RetOpcode == PPC::TCRETURNai ||
670 RetOpcode == PPC::TCRETURNri8 ||
671 RetOpcode == PPC::TCRETURNdi8 ||
672 RetOpcode == PPC::TCRETURNai8;
673
674 if (UsesTCRet) {
675 int MaxTCRetDelta = FI->getTailCallSPDelta();
676 MachineOperand &StackAdjust = MBBI->getOperand(1);
677 assert(StackAdjust.isImm() && "Expecting immediate value.");
678 // Adjust stack pointer.
679 int StackAdj = StackAdjust.getImm();
680 int Delta = StackAdj - MaxTCRetDelta;
681 assert((Delta >= 0) && "Delta must be positive");
682 if (MaxTCRetDelta>0)
683 FrameSize += (StackAdj +Delta);
684 else
685 FrameSize += StackAdj;
686 }
687
688 if (FrameSize) {
689 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
690 // on entry to the function. Add this offset back now.
691 if (!isPPC64) {
692 // If this function contained a fastcc call and GuaranteedTailCallOpt is
693 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
694 // call which invalidates the stack pointer value in SP(0). So we use the
695 // value of R31 in this case.
696 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000697 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000698 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
699 .addReg(PPC::R31).addImm(FrameSize);
700 } else if(FI->hasFastCall()) {
701 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
702 .addImm(FrameSize >> 16);
703 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
704 .addReg(PPC::R0, RegState::Kill)
705 .addImm(FrameSize & 0xFFFF);
706 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
707 .addReg(PPC::R1)
708 .addReg(PPC::R31)
709 .addReg(PPC::R0);
710 } else if (isInt<16>(FrameSize) &&
711 (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
712 !MFI->hasVarSizedObjects()) {
713 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
714 .addReg(PPC::R1).addImm(FrameSize);
715 } else {
716 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
717 .addImm(0).addReg(PPC::R1);
718 }
719 } else {
720 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000721 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000722 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
723 .addReg(PPC::X31).addImm(FrameSize);
724 } else if(FI->hasFastCall()) {
725 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
726 .addImm(FrameSize >> 16);
727 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
728 .addReg(PPC::X0, RegState::Kill)
729 .addImm(FrameSize & 0xFFFF);
730 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
731 .addReg(PPC::X1)
732 .addReg(PPC::X31)
733 .addReg(PPC::X0);
734 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign &&
735 !MFI->hasVarSizedObjects()) {
736 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
737 .addReg(PPC::X1).addImm(FrameSize);
738 } else {
739 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
740 .addImm(0).addReg(PPC::X1);
741 }
742 }
743 }
744
745 if (isPPC64) {
746 if (MustSaveLR)
747 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
748 .addImm(LROffset/4).addReg(PPC::X1);
749
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000750 if (!MustSaveCRs.empty())
751 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), PPC::X12)
752 .addImm(8).addReg(PPC::X1);
753
Anton Korobeynikov33464912010-11-15 00:06:54 +0000754 if (HasFP)
755 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
756 .addImm(FPOffset/4).addReg(PPC::X1);
757
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000758 if (!MustSaveCRs.empty())
759 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
760 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTCRF8), MustSaveCRs[i])
761 .addReg(PPC::X12, getKillRegState(i == e-1));
762
Anton Korobeynikov33464912010-11-15 00:06:54 +0000763 if (MustSaveLR)
764 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
765 } else {
766 if (MustSaveLR)
767 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
768 .addImm(LROffset).addReg(PPC::R1);
769
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000770 assert(MustSaveCRs.empty() &&
771 "Epilogue CR restoring supported only in 64-bit mode");
772
Anton Korobeynikov33464912010-11-15 00:06:54 +0000773 if (HasFP)
774 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
775 .addImm(FPOffset).addReg(PPC::R1);
776
777 if (MustSaveLR)
778 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
779 }
780
781 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
782 // call optimization
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000783 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000784 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
785 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
786 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
787 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
788 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
789 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
790 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
791 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
792 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
793 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
794
795 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
796 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
797 .addReg(StackReg).addImm(CallerAllocatedAmt);
798 } else {
799 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
800 .addImm(CallerAllocatedAmt >> 16);
801 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
802 .addReg(TmpReg, RegState::Kill)
803 .addImm(CallerAllocatedAmt & 0xFFFF);
804 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
805 .addReg(StackReg)
806 .addReg(FPReg)
807 .addReg(TmpReg);
808 }
809 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000810 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000811 MachineOperand &JumpTarget = MBBI->getOperand(0);
812 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
813 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
814 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000815 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000816 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
817 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
818 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000819 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000820 MachineOperand &JumpTarget = MBBI->getOperand(0);
821 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
822 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000823 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000824 MachineOperand &JumpTarget = MBBI->getOperand(0);
825 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
826 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
827 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000828 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000829 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
830 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
831 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000832 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000833 MachineOperand &JumpTarget = MBBI->getOperand(0);
834 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
835 }
836}
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000837
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000838/// MustSaveLR - Return true if this function requires that we save the LR
839/// register onto the stack in the prolog and restore it in the epilog of the
840/// function.
841static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
842 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
843
844 // We need a save/restore of LR if there is any def of LR (which is
845 // defined by calls, including the PIC setup sequence), or if there is
846 // some use of the LR stack slot (e.g. for builtin_return_address).
847 // (LR comes in 32 and 64 bit versions.)
848 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
849 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
850}
851
852void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000853PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000854 RegScavenger *) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000855 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
856
857 // Save and clear the LR state.
858 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
859 unsigned LR = RegInfo->getRARegister();
860 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000861 MachineRegisterInfo &MRI = MF.getRegInfo();
862 MRI.setPhysRegUnused(LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000863
864 // Save R31 if necessary
865 int FPSI = FI->getFramePointerSaveIndex();
866 bool isPPC64 = Subtarget.isPPC64();
867 bool isDarwinABI = Subtarget.isDarwinABI();
868 MachineFrameInfo *MFI = MF.getFrameInfo();
869
870 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000871 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000872 // Find out what the fix offset of the frame pointer save area.
873 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
874 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000875 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000876 // Save the result.
877 FI->setFramePointerSaveIndex(FPSI);
878 }
879
880 // Reserve stack space to move the linkage area to in case of a tail call.
881 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000882 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
883 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000884 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000885 }
886
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000887 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
888 // function uses CR 2, 3, or 4.
889 if (!isPPC64 && !isDarwinABI &&
890 (MRI.isPhysRegUsed(PPC::CR2) ||
891 MRI.isPhysRegUsed(PPC::CR3) ||
892 MRI.isPhysRegUsed(PPC::CR4))) {
893 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
894 FI->setCRSpillFrameIndex(FrameIdx);
895 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000896}
897
Hal Finkel3080d232013-03-14 20:33:40 +0000898void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000899 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000900 // Early exit if not using the SVR4 ABI.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000901 if (!Subtarget.isSVR4ABI()) {
902 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000903 return;
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000904 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000905
906 // Get callee saved register information.
907 MachineFrameInfo *FFI = MF.getFrameInfo();
908 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
909
910 // Early exit if no callee saved registers are modified!
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000911 if (CSI.empty() && !needsFP(MF)) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000912 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000913 return;
914 }
915
916 unsigned MinGPR = PPC::R31;
917 unsigned MinG8R = PPC::X31;
918 unsigned MinFPR = PPC::F31;
919 unsigned MinVR = PPC::V31;
920
921 bool HasGPSaveArea = false;
922 bool HasG8SaveArea = false;
923 bool HasFPSaveArea = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000924 bool HasVRSAVESaveArea = false;
925 bool HasVRSaveArea = false;
926
927 SmallVector<CalleeSavedInfo, 18> GPRegs;
928 SmallVector<CalleeSavedInfo, 18> G8Regs;
929 SmallVector<CalleeSavedInfo, 18> FPRegs;
930 SmallVector<CalleeSavedInfo, 18> VRegs;
931
932 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
933 unsigned Reg = CSI[i].getReg();
Craig Topperc9099502012-04-20 06:31:50 +0000934 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000935 HasGPSaveArea = true;
936
937 GPRegs.push_back(CSI[i]);
938
939 if (Reg < MinGPR) {
940 MinGPR = Reg;
941 }
Craig Topperc9099502012-04-20 06:31:50 +0000942 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000943 HasG8SaveArea = true;
944
945 G8Regs.push_back(CSI[i]);
946
947 if (Reg < MinG8R) {
948 MinG8R = Reg;
949 }
Craig Topperc9099502012-04-20 06:31:50 +0000950 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000951 HasFPSaveArea = true;
952
953 FPRegs.push_back(CSI[i]);
954
955 if (Reg < MinFPR) {
956 MinFPR = Reg;
957 }
Craig Topperc9099502012-04-20 06:31:50 +0000958 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
959 PPC::CRRCRegClass.contains(Reg)) {
Roman Divacky9d760ae2012-09-12 14:47:47 +0000960 ; // do nothing, as we already know whether CRs are spilled
Craig Topperc9099502012-04-20 06:31:50 +0000961 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000962 HasVRSAVESaveArea = true;
Craig Topperc9099502012-04-20 06:31:50 +0000963 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000964 HasVRSaveArea = true;
965
966 VRegs.push_back(CSI[i]);
967
968 if (Reg < MinVR) {
969 MinVR = Reg;
970 }
971 } else {
972 llvm_unreachable("Unknown RegisterClass!");
973 }
974 }
975
976 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Hal Finkelaa6047d2013-03-26 20:08:20 +0000977 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000978
979 int64_t LowerBound = 0;
980
981 // Take into account stack space reserved for tail calls.
982 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000983 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
984 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000985 LowerBound = TCSPDelta;
986 }
987
988 // The Floating-point register save area is right below the back chain word
989 // of the previous stack frame.
990 if (HasFPSaveArea) {
991 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
992 int FI = FPRegs[i].getFrameIdx();
993
994 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
995 }
996
Hal Finkelaa6047d2013-03-26 20:08:20 +0000997 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000998 }
999
1000 // Check whether the frame pointer register is allocated. If so, make sure it
1001 // is spilled to the correct offset.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +00001002 if (needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001003 HasGPSaveArea = true;
1004
1005 int FI = PFI->getFramePointerSaveIndex();
1006 assert(FI && "No Frame Pointer Save Slot!");
1007
1008 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1009 }
1010
1011 // General register save area starts right below the Floating-point
1012 // register save area.
1013 if (HasGPSaveArea || HasG8SaveArea) {
1014 // Move general register save area spill slots down, taking into account
1015 // the size of the Floating-point register save area.
1016 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1017 int FI = GPRegs[i].getFrameIdx();
1018
1019 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1020 }
1021
1022 // Move general register save area spill slots down, taking into account
1023 // the size of the Floating-point register save area.
1024 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1025 int FI = G8Regs[i].getFrameIdx();
1026
1027 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1028 }
1029
1030 unsigned MinReg =
Hal Finkelaa6047d2013-03-26 20:08:20 +00001031 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1032 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001033
1034 if (Subtarget.isPPC64()) {
1035 LowerBound -= (31 - MinReg + 1) * 8;
1036 } else {
1037 LowerBound -= (31 - MinReg + 1) * 4;
1038 }
1039 }
1040
Roman Divacky9d760ae2012-09-12 14:47:47 +00001041 // For 32-bit only, the CR save area is below the general register
1042 // save area. For 64-bit SVR4, the CR save area is addressed relative
1043 // to the stack pointer and hence does not need an adjustment here.
1044 // Only CR2 (the first nonvolatile spilled) has an associated frame
1045 // index so that we have a single uniform save area.
1046 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001047 // Adjust the frame index of the CR spill slot.
1048 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1049 unsigned Reg = CSI[i].getReg();
1050
Roman Divacky9d760ae2012-09-12 14:47:47 +00001051 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
1052 // Leave Darwin logic as-is.
1053 || (!Subtarget.isSVR4ABI() &&
1054 (PPC::CRBITRCRegClass.contains(Reg) ||
1055 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001056 int FI = CSI[i].getFrameIdx();
1057
1058 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1059 }
1060 }
1061
1062 LowerBound -= 4; // The CR save area is always 4 bytes long.
1063 }
1064
1065 if (HasVRSAVESaveArea) {
1066 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1067 // which have the VRSAVE register class?
1068 // Adjust the frame index of the VRSAVE spill slot.
1069 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1070 unsigned Reg = CSI[i].getReg();
1071
Craig Topperc9099502012-04-20 06:31:50 +00001072 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001073 int FI = CSI[i].getFrameIdx();
1074
1075 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1076 }
1077 }
1078
1079 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1080 }
1081
1082 if (HasVRSaveArea) {
1083 // Insert alignment padding, we need 16-byte alignment.
1084 LowerBound = (LowerBound - 15) & ~(15);
1085
1086 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1087 int FI = VRegs[i].getFrameIdx();
1088
1089 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1090 }
1091 }
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001092
1093 addScavengingSpillSlot(MF, RS);
1094}
1095
1096void
1097PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1098 RegScavenger *RS) const {
1099 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1100 // a large stack, which will require scavenging a register to materialize a
1101 // large offset.
1102
1103 // We need to have a scavenger spill slot for spills if the frame size is
1104 // large. In case there is no free register for large-offset addressing,
1105 // this slot is used for the necessary emergency spill. Also, we need the
1106 // slot for dynamic stack allocations.
1107
1108 // The scavenger might be invoked if the frame offset does not fit into
1109 // the 16-bit immediate. We don't know the complete frame size here
1110 // because we've not yet computed callee-saved register spills or the
1111 // needed alignment padding.
1112 unsigned StackSize = determineFrameLayout(MF, false, true);
1113 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkel3f2c0472013-03-23 22:06:03 +00001114 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1115 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001116 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1117 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1118 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkeldc3beb92013-03-22 23:32:27 +00001119 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001120 RC->getAlignment(),
1121 false));
Hal Finkel01f99d22013-03-26 18:57:22 +00001122
1123 // These kinds of spills might need two registers.
1124 if (spillsCR(MF) || spillsVRSAVE(MF))
1125 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1126 RC->getAlignment(),
1127 false));
1128
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001129 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001130}
Roman Divacky9d760ae2012-09-12 14:47:47 +00001131
1132bool
1133PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1134 MachineBasicBlock::iterator MI,
1135 const std::vector<CalleeSavedInfo> &CSI,
1136 const TargetRegisterInfo *TRI) const {
1137
1138 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1139 // Return false otherwise to maintain pre-existing behavior.
1140 if (!Subtarget.isSVR4ABI())
1141 return false;
1142
1143 MachineFunction *MF = MBB.getParent();
1144 const PPCInstrInfo &TII =
1145 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1146 DebugLoc DL;
1147 bool CRSpilled = false;
Hal Finkel63496f62013-04-13 23:06:15 +00001148 MachineInstrBuilder CRMIB;
Roman Divacky9d760ae2012-09-12 14:47:47 +00001149
1150 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1151 unsigned Reg = CSI[i].getReg();
1152 // CR2 through CR4 are the nonvolatile CR fields.
1153 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1154
Roman Divacky9d760ae2012-09-12 14:47:47 +00001155 // Add the callee-saved register as live-in; it's killed at the spill.
1156 MBB.addLiveIn(Reg);
1157
Hal Finkel63496f62013-04-13 23:06:15 +00001158 if (CRSpilled && IsCRField) {
1159 CRMIB.addReg(Reg, RegState::ImplicitKill);
1160 continue;
1161 }
1162
Roman Divacky9d760ae2012-09-12 14:47:47 +00001163 // Insert the spill to the stack frame.
1164 if (IsCRField) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001165 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divacky9d760ae2012-09-12 14:47:47 +00001166 if (Subtarget.isPPC64()) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001167 // The actual spill will happen at the start of the prologue.
1168 FuncInfo->addMustSaveCR(Reg);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001169 } else {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001170 CRSpilled = true;
Bill Schmidtded53bf2013-05-14 16:08:32 +00001171 FuncInfo->setSpillsCR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001172
Roman Divacky9d760ae2012-09-12 14:47:47 +00001173 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1174 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
Hal Finkel63496f62013-04-13 23:06:15 +00001175 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
1176 .addReg(Reg, RegState::ImplicitKill);
1177
1178 MBB.insert(MI, CRMIB);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001179 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1180 .addReg(PPC::R12,
1181 getKillRegState(true)),
1182 CSI[i].getFrameIdx()));
1183 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001184 } else {
1185 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1186 TII.storeRegToStackSlot(MBB, MI, Reg, true,
1187 CSI[i].getFrameIdx(), RC, TRI);
1188 }
1189 }
1190 return true;
1191}
1192
1193static void
Hal Finkelb99c9952013-04-13 08:09:20 +00001194restoreCRs(bool isPPC64, bool is31,
1195 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001196 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1197 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1198
1199 MachineFunction *MF = MBB.getParent();
1200 const PPCInstrInfo &TII =
1201 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1202 DebugLoc DL;
1203 unsigned RestoreOp, MoveReg;
1204
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001205 if (isPPC64)
1206 // This is handled during epilogue generation.
1207 return;
1208 else {
Roman Divacky9d760ae2012-09-12 14:47:47 +00001209 // 32-bit: FP-relative
1210 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
1211 PPC::R12),
1212 CSI[CSIIndex].getFrameIdx()));
1213 RestoreOp = PPC::MTCRF;
1214 MoveReg = PPC::R12;
1215 }
1216
1217 if (CR2Spilled)
1218 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkeld957f952013-03-28 03:38:16 +00001219 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001220
1221 if (CR3Spilled)
1222 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkeld957f952013-03-28 03:38:16 +00001223 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001224
1225 if (CR4Spilled)
1226 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkeld957f952013-03-28 03:38:16 +00001227 .addReg(MoveReg, getKillRegState(true)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001228}
1229
Eli Bendersky700ed802013-02-21 20:05:00 +00001230void PPCFrameLowering::
1231eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1232 MachineBasicBlock::iterator I) const {
1233 const PPCInstrInfo &TII =
1234 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1235 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1236 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1237 // Add (actually subtract) back the amount the callee popped on return.
1238 if (int CalleeAmt = I->getOperand(1).getImm()) {
1239 bool is64Bit = Subtarget.isPPC64();
1240 CalleeAmt *= -1;
1241 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1242 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1243 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1244 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1245 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1246 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1247 MachineInstr *MI = I;
1248 DebugLoc dl = MI->getDebugLoc();
1249
1250 if (isInt<16>(CalleeAmt)) {
1251 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1252 .addReg(StackReg, RegState::Kill)
1253 .addImm(CalleeAmt);
1254 } else {
1255 MachineBasicBlock::iterator MBBI = I;
1256 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1257 .addImm(CalleeAmt >> 16);
1258 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1259 .addReg(TmpReg, RegState::Kill)
1260 .addImm(CalleeAmt & 0xFFFF);
1261 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1262 .addReg(StackReg, RegState::Kill)
1263 .addReg(TmpReg);
1264 }
1265 }
1266 }
1267 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1268 MBB.erase(I);
1269}
1270
Roman Divacky9d760ae2012-09-12 14:47:47 +00001271bool
1272PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1273 MachineBasicBlock::iterator MI,
1274 const std::vector<CalleeSavedInfo> &CSI,
1275 const TargetRegisterInfo *TRI) const {
1276
1277 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1278 // Return false otherwise to maintain pre-existing behavior.
1279 if (!Subtarget.isSVR4ABI())
1280 return false;
1281
1282 MachineFunction *MF = MBB.getParent();
1283 const PPCInstrInfo &TII =
1284 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1285 bool CR2Spilled = false;
1286 bool CR3Spilled = false;
1287 bool CR4Spilled = false;
1288 unsigned CSIIndex = 0;
1289
1290 // Initialize insertion-point logic; we will be restoring in reverse
1291 // order of spill.
1292 MachineBasicBlock::iterator I = MI, BeforeI = I;
1293 bool AtStart = I == MBB.begin();
1294
1295 if (!AtStart)
1296 --BeforeI;
1297
1298 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1299 unsigned Reg = CSI[i].getReg();
1300
1301 if (Reg == PPC::CR2) {
1302 CR2Spilled = true;
1303 // The spill slot is associated only with CR2, which is the
1304 // first nonvolatile spilled. Save it here.
1305 CSIIndex = i;
1306 continue;
1307 } else if (Reg == PPC::CR3) {
1308 CR3Spilled = true;
1309 continue;
1310 } else if (Reg == PPC::CR4) {
1311 CR4Spilled = true;
1312 continue;
1313 } else {
1314 // When we first encounter a non-CR register after seeing at
1315 // least one CR register, restore all spilled CRs together.
1316 if ((CR2Spilled || CR3Spilled || CR4Spilled)
1317 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkelb99c9952013-04-13 08:09:20 +00001318 bool is31 = needsFP(*MF);
1319 restoreCRs(Subtarget.isPPC64(), is31,
1320 CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001321 MBB, I, CSI, CSIIndex);
1322 CR2Spilled = CR3Spilled = CR4Spilled = false;
1323 }
1324
1325 // Default behavior for non-CR saves.
1326 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1327 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
1328 RC, TRI);
1329 assert(I != MBB.begin() &&
1330 "loadRegFromStackSlot didn't insert any code!");
1331 }
1332
1333 // Insert in reverse order.
1334 if (AtStart)
1335 I = MBB.begin();
1336 else {
1337 I = BeforeI;
1338 ++I;
1339 }
1340 }
1341
1342 // If we haven't yet spilled the CRs, do so now.
Hal Finkelb99c9952013-04-13 08:09:20 +00001343 if (CR2Spilled || CR3Spilled || CR4Spilled) {
1344 bool is31 = needsFP(*MF);
1345 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001346 MBB, I, CSI, CSIIndex);
Hal Finkelb99c9952013-04-13 08:09:20 +00001347 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001348
1349 return true;
1350}
1351