blob: dabe61348de79991eb8dde362d7c4682f8320222 [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();
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000337 const MCRegisterInfo &MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000338 DebugLoc dl;
339 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000340 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000341
342 // Prepare for frame info.
343 MCSymbol *FrameLabel = 0;
344
345 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
346 // process it.
Bill Schmidta5d0ab52012-10-10 20:54:15 +0000347 if (!Subtarget.isSVR4ABI())
348 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
349 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
350 HandleVRSaveUpdate(MBBI, TII);
351 break;
352 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000353 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000354
355 // Move MBBI back to the beginning of the function.
356 MBBI = MBB.begin();
357
358 // Work out frame sizes.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000359 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000360 int NegFrameSize = -FrameSize;
361
Hal Finkele9cc0a02013-03-21 19:03:19 +0000362 if (MFI->isFrameAddressTaken())
363 replaceFPWithRealFP(MF);
364
Anton Korobeynikov33464912010-11-15 00:06:54 +0000365 // Get processor type.
366 bool isPPC64 = Subtarget.isPPC64();
367 // Get operating system
368 bool isDarwinABI = Subtarget.isDarwinABI();
369 // Check if the link register (LR) must be saved.
370 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
371 bool MustSaveLR = FI->mustSaveLR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000372 const SmallVector<unsigned, 3> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000373 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000374 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000375
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000376 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000377
378 int FPOffset = 0;
379 if (HasFP) {
380 if (Subtarget.isSVR4ABI()) {
381 MachineFrameInfo *FFI = MF.getFrameInfo();
382 int FPIndex = FI->getFramePointerSaveIndex();
383 assert(FPIndex && "No Frame Pointer Save Slot!");
384 FPOffset = FFI->getObjectOffset(FPIndex);
385 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000386 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000387 }
388 }
389
390 if (isPPC64) {
391 if (MustSaveLR)
392 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
393
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000394 if (!MustSaveCRs.empty()) {
395 MachineInstrBuilder MIB =
396 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), PPC::X12);
397 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
398 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
399 }
400
Anton Korobeynikov33464912010-11-15 00:06:54 +0000401 if (HasFP)
402 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
403 .addReg(PPC::X31)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000404 .addImm(FPOffset)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000405 .addReg(PPC::X1);
406
407 if (MustSaveLR)
408 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
409 .addReg(PPC::X0)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000410 .addImm(LROffset)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000411 .addReg(PPC::X1);
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000412
413 if (!MustSaveCRs.empty())
414 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
415 .addReg(PPC::X12, getKillRegState(true))
416 .addImm(8)
417 .addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000418 } else {
419 if (MustSaveLR)
420 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
421
422 if (HasFP)
Hal Finkelb8f2f292012-05-19 21:52:55 +0000423 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative
424 // offsets of R1 is not allowed.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000425 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
426 .addReg(PPC::R31)
427 .addImm(FPOffset)
428 .addReg(PPC::R1);
429
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000430 assert(MustSaveCRs.empty() &&
431 "Prologue CR saving supported only in 64-bit mode");
432
Anton Korobeynikov33464912010-11-15 00:06:54 +0000433 if (MustSaveLR)
434 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
435 .addReg(PPC::R0)
436 .addImm(LROffset)
437 .addReg(PPC::R1);
438 }
439
440 // Skip if a leaf routine.
441 if (!FrameSize) return;
442
443 // Get stack alignments.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000444 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000445 unsigned MaxAlign = MFI->getMaxAlignment();
446
447 // Adjust stack pointer: r1 += NegFrameSize.
448 // If there is a preferred stack alignment, align R1 now
449 if (!isPPC64) {
450 // PPC32.
451 if (ALIGN_STACK && MaxAlign > TargetAlign) {
452 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
453 "Invalid alignment!");
454 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
455
456 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
457 .addReg(PPC::R1)
458 .addImm(0)
459 .addImm(32 - Log2_32(MaxAlign))
460 .addImm(31);
461 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0)
462 .addReg(PPC::R0, RegState::Kill)
463 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000464 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000465 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000466 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000467 .addReg(PPC::R0);
468 } else if (isInt<16>(NegFrameSize)) {
469 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
470 .addReg(PPC::R1)
471 .addImm(NegFrameSize)
472 .addReg(PPC::R1);
473 } else {
474 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
475 .addImm(NegFrameSize >> 16);
476 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
477 .addReg(PPC::R0, RegState::Kill)
478 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000479 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000480 .addReg(PPC::R1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000481 .addReg(PPC::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000482 .addReg(PPC::R0);
483 }
484 } else { // PPC64.
485 if (ALIGN_STACK && MaxAlign > TargetAlign) {
486 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
487 "Invalid alignment!");
488 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
489
490 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
491 .addReg(PPC::X1)
492 .addImm(0)
493 .addImm(64 - Log2_32(MaxAlign));
494 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
495 .addReg(PPC::X0)
496 .addImm(NegFrameSize);
Hal Finkelac81cc32012-06-19 02:34:32 +0000497 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000498 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000499 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000500 .addReg(PPC::X0);
501 } else if (isInt<16>(NegFrameSize)) {
502 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
503 .addReg(PPC::X1)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000504 .addImm(NegFrameSize)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000505 .addReg(PPC::X1);
506 } else {
507 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
508 .addImm(NegFrameSize >> 16);
509 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
510 .addReg(PPC::X0, RegState::Kill)
511 .addImm(NegFrameSize & 0xFFFF);
Hal Finkelac81cc32012-06-19 02:34:32 +0000512 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1)
Hal Finkel2e95afa2011-12-30 00:34:00 +0000513 .addReg(PPC::X1, RegState::Kill)
Hal Finkelac81cc32012-06-19 02:34:32 +0000514 .addReg(PPC::X1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000515 .addReg(PPC::X0);
516 }
517 }
518
Anton Korobeynikov33464912010-11-15 00:06:54 +0000519 // Add the "machine moves" for the instructions we generated above, but in
520 // reverse order.
521 if (needsFrameMoves) {
522 // Mark effective beginning of when frame pointer becomes valid.
523 FrameLabel = MMI.getContext().CreateTempSymbol();
524 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel);
525
526 // Show update of SP.
Rafael Espindolaec7f4232013-05-16 03:34:58 +0000527 assert(NegFrameSize);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000528 MMI.addFrameInst(
529 MCCFIInstruction::createDefCfaOffset(FrameLabel, NegFrameSize));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000530
531 if (HasFP) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000532 unsigned Reg = isPPC64 ? PPC::X31 : PPC::R31;
533 Reg = MRI.getDwarfRegNum(Reg, true);
534 MMI.addFrameInst(
535 MCCFIInstruction::createOffset(FrameLabel, Reg, FPOffset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000536 }
537
538 if (MustSaveLR) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000539 unsigned Reg = isPPC64 ? PPC::LR8 : PPC::LR;
540 Reg = MRI.getDwarfRegNum(Reg, true);
541 MMI.addFrameInst(
542 MCCFIInstruction::createOffset(FrameLabel, Reg, LROffset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000543 }
544 }
545
546 MCSymbol *ReadyLabel = 0;
547
548 // If there is a frame pointer, copy R1 into R31
549 if (HasFP) {
550 if (!isPPC64) {
551 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
552 .addReg(PPC::R1)
553 .addReg(PPC::R1);
554 } else {
555 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
556 .addReg(PPC::X1)
557 .addReg(PPC::X1);
558 }
559
560 if (needsFrameMoves) {
561 ReadyLabel = MMI.getContext().CreateTempSymbol();
562
563 // Mark effective beginning of when frame pointer is ready.
564 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel);
565
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000566 unsigned Reg = HasFP ? (isPPC64 ? PPC::X31 : PPC::R31)
567 : (isPPC64 ? PPC::X1 : PPC::R1);
568 Reg = MRI.getDwarfRegNum(Reg, true);
569 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(ReadyLabel, Reg));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000570 }
571 }
572
573 if (needsFrameMoves) {
574 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
575
576 // Add callee saved registers to move list.
577 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
578 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000579 unsigned Reg = CSI[I].getReg();
580 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000581
582 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
583 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperc9099502012-04-20 06:31:50 +0000584 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola6e032942011-05-30 20:20:15 +0000585 continue;
Rafael Espindola6e032942011-05-30 20:20:15 +0000586
Roman Divacky9d760ae2012-09-12 14:47:47 +0000587 // For SVR4, don't emit a move for the CR spill slot if we haven't
588 // spilled CRs.
589 if (Subtarget.isSVR4ABI()
590 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000591 && MustSaveCRs.empty())
Roman Divacky9d760ae2012-09-12 14:47:47 +0000592 continue;
593
594 // For 64-bit SVR4 when we have spilled CRs, the spill location
595 // is SP+8, not a frame-relative slot.
596 if (Subtarget.isSVR4ABI()
597 && Subtarget.isPPC64()
598 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000599 MMI.addFrameInst(MCCFIInstruction::createOffset(
600 Label, MRI.getDwarfRegNum(PPC::CR2, true), 8));
Roman Divacky9d760ae2012-09-12 14:47:47 +0000601 continue;
602 }
603
604 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindola6b67ffd2013-05-16 21:02:15 +0000605 MMI.addFrameInst(MCCFIInstruction::createOffset(
606 Label, MRI.getDwarfRegNum(Reg, true), Offset));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000607 }
608 }
609}
610
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000611void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000612 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000613 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
614 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000615 const PPCInstrInfo &TII =
616 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
617
618 unsigned RetOpcode = MBBI->getOpcode();
619 DebugLoc dl;
620
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000621 assert((RetOpcode == PPC::BLR ||
622 RetOpcode == PPC::TCRETURNri ||
623 RetOpcode == PPC::TCRETURNdi ||
624 RetOpcode == PPC::TCRETURNai ||
625 RetOpcode == PPC::TCRETURNri8 ||
626 RetOpcode == PPC::TCRETURNdi8 ||
627 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000628 "Can only insert epilog into returning blocks");
629
630 // Get alignment info so we know how to restore r1
631 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000632 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000633 unsigned MaxAlign = MFI->getMaxAlignment();
634
635 // Get the number of bytes allocated from the FrameInfo.
636 int FrameSize = MFI->getStackSize();
637
638 // Get processor type.
639 bool isPPC64 = Subtarget.isPPC64();
640 // Get operating system
641 bool isDarwinABI = Subtarget.isDarwinABI();
642 // Check if the link register (LR) has been saved.
643 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
644 bool MustSaveLR = FI->mustSaveLR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000645 const SmallVector<unsigned, 3> &MustSaveCRs = FI->getMustSaveCRs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000646 // Do we have a frame pointer for this function?
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000647 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000648
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000649 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000650
651 int FPOffset = 0;
652 if (HasFP) {
653 if (Subtarget.isSVR4ABI()) {
654 MachineFrameInfo *FFI = MF.getFrameInfo();
655 int FPIndex = FI->getFramePointerSaveIndex();
656 assert(FPIndex && "No Frame Pointer Save Slot!");
657 FPOffset = FFI->getObjectOffset(FPIndex);
658 } else {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000659 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000660 }
661 }
662
663 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
664 RetOpcode == PPC::TCRETURNdi ||
665 RetOpcode == PPC::TCRETURNai ||
666 RetOpcode == PPC::TCRETURNri8 ||
667 RetOpcode == PPC::TCRETURNdi8 ||
668 RetOpcode == PPC::TCRETURNai8;
669
670 if (UsesTCRet) {
671 int MaxTCRetDelta = FI->getTailCallSPDelta();
672 MachineOperand &StackAdjust = MBBI->getOperand(1);
673 assert(StackAdjust.isImm() && "Expecting immediate value.");
674 // Adjust stack pointer.
675 int StackAdj = StackAdjust.getImm();
676 int Delta = StackAdj - MaxTCRetDelta;
677 assert((Delta >= 0) && "Delta must be positive");
678 if (MaxTCRetDelta>0)
679 FrameSize += (StackAdj +Delta);
680 else
681 FrameSize += StackAdj;
682 }
683
684 if (FrameSize) {
685 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
686 // on entry to the function. Add this offset back now.
687 if (!isPPC64) {
688 // If this function contained a fastcc call and GuaranteedTailCallOpt is
689 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
690 // call which invalidates the stack pointer value in SP(0). So we use the
691 // value of R31 in this case.
692 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000693 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000694 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
695 .addReg(PPC::R31).addImm(FrameSize);
696 } else if(FI->hasFastCall()) {
697 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
698 .addImm(FrameSize >> 16);
699 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
700 .addReg(PPC::R0, RegState::Kill)
701 .addImm(FrameSize & 0xFFFF);
702 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
703 .addReg(PPC::R1)
704 .addReg(PPC::R31)
705 .addReg(PPC::R0);
706 } else if (isInt<16>(FrameSize) &&
707 (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
708 !MFI->hasVarSizedObjects()) {
709 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
710 .addReg(PPC::R1).addImm(FrameSize);
711 } else {
712 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
713 .addImm(0).addReg(PPC::R1);
714 }
715 } else {
716 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000717 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000718 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
719 .addReg(PPC::X31).addImm(FrameSize);
720 } else if(FI->hasFastCall()) {
721 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
722 .addImm(FrameSize >> 16);
723 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
724 .addReg(PPC::X0, RegState::Kill)
725 .addImm(FrameSize & 0xFFFF);
726 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
727 .addReg(PPC::X1)
728 .addReg(PPC::X31)
729 .addReg(PPC::X0);
730 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign &&
731 !MFI->hasVarSizedObjects()) {
732 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
733 .addReg(PPC::X1).addImm(FrameSize);
734 } else {
735 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
736 .addImm(0).addReg(PPC::X1);
737 }
738 }
739 }
740
741 if (isPPC64) {
742 if (MustSaveLR)
743 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000744 .addImm(LROffset).addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000745
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000746 if (!MustSaveCRs.empty())
747 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), PPC::X12)
748 .addImm(8).addReg(PPC::X1);
749
Anton Korobeynikov33464912010-11-15 00:06:54 +0000750 if (HasFP)
751 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
Ulrich Weigand347a5072013-05-16 17:58:02 +0000752 .addImm(FPOffset).addReg(PPC::X1);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000753
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000754 if (!MustSaveCRs.empty())
755 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
756 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTCRF8), MustSaveCRs[i])
757 .addReg(PPC::X12, getKillRegState(i == e-1));
758
Anton Korobeynikov33464912010-11-15 00:06:54 +0000759 if (MustSaveLR)
760 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
761 } else {
762 if (MustSaveLR)
763 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
764 .addImm(LROffset).addReg(PPC::R1);
765
Hal Finkelfb6fe0a2013-04-15 02:07:05 +0000766 assert(MustSaveCRs.empty() &&
767 "Epilogue CR restoring supported only in 64-bit mode");
768
Anton Korobeynikov33464912010-11-15 00:06:54 +0000769 if (HasFP)
770 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
771 .addImm(FPOffset).addReg(PPC::R1);
772
773 if (MustSaveLR)
774 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
775 }
776
777 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
778 // call optimization
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000779 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikov33464912010-11-15 00:06:54 +0000780 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
781 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
782 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
783 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
784 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
785 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
786 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
787 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
788 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
789 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
790
791 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
792 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
793 .addReg(StackReg).addImm(CallerAllocatedAmt);
794 } else {
795 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
796 .addImm(CallerAllocatedAmt >> 16);
797 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
798 .addReg(TmpReg, RegState::Kill)
799 .addImm(CallerAllocatedAmt & 0xFFFF);
800 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
801 .addReg(StackReg)
802 .addReg(FPReg)
803 .addReg(TmpReg);
804 }
805 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000806 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000807 MachineOperand &JumpTarget = MBBI->getOperand(0);
808 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
809 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
810 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000811 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000812 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
813 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
814 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000815 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000816 MachineOperand &JumpTarget = MBBI->getOperand(0);
817 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
818 } else if (RetOpcode == PPC::TCRETURNdi8) {
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::TAILB8)).
822 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
823 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000824 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000825 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
826 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
827 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000828 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000829 MachineOperand &JumpTarget = MBBI->getOperand(0);
830 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
831 }
832}
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000833
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000834/// MustSaveLR - Return true if this function requires that we save the LR
835/// register onto the stack in the prolog and restore it in the epilog of the
836/// function.
837static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
838 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
839
840 // We need a save/restore of LR if there is any def of LR (which is
841 // defined by calls, including the PIC setup sequence), or if there is
842 // some use of the LR stack slot (e.g. for builtin_return_address).
843 // (LR comes in 32 and 64 bit versions.)
844 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
845 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
846}
847
848void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000849PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000850 RegScavenger *) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000851 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
852
853 // Save and clear the LR state.
854 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
855 unsigned LR = RegInfo->getRARegister();
856 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000857 MachineRegisterInfo &MRI = MF.getRegInfo();
858 MRI.setPhysRegUnused(LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000859
860 // Save R31 if necessary
861 int FPSI = FI->getFramePointerSaveIndex();
862 bool isPPC64 = Subtarget.isPPC64();
863 bool isDarwinABI = Subtarget.isDarwinABI();
864 MachineFrameInfo *MFI = MF.getFrameInfo();
865
866 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000867 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000868 // Find out what the fix offset of the frame pointer save area.
869 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
870 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000871 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000872 // Save the result.
873 FI->setFramePointerSaveIndex(FPSI);
874 }
875
876 // Reserve stack space to move the linkage area to in case of a tail call.
877 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000878 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
879 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000880 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000881 }
882
Bill Schmidt4edd84d2013-02-24 17:34:50 +0000883 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
884 // function uses CR 2, 3, or 4.
885 if (!isPPC64 && !isDarwinABI &&
886 (MRI.isPhysRegUsed(PPC::CR2) ||
887 MRI.isPhysRegUsed(PPC::CR3) ||
888 MRI.isPhysRegUsed(PPC::CR4))) {
889 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
890 FI->setCRSpillFrameIndex(FrameIdx);
891 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000892}
893
Hal Finkel3080d232013-03-14 20:33:40 +0000894void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000895 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000896 // Early exit if not using the SVR4 ABI.
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000897 if (!Subtarget.isSVR4ABI()) {
898 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000899 return;
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000900 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000901
902 // Get callee saved register information.
903 MachineFrameInfo *FFI = MF.getFrameInfo();
904 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
905
906 // Early exit if no callee saved registers are modified!
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000907 if (CSI.empty() && !needsFP(MF)) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000908 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000909 return;
910 }
911
912 unsigned MinGPR = PPC::R31;
913 unsigned MinG8R = PPC::X31;
914 unsigned MinFPR = PPC::F31;
915 unsigned MinVR = PPC::V31;
916
917 bool HasGPSaveArea = false;
918 bool HasG8SaveArea = false;
919 bool HasFPSaveArea = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000920 bool HasVRSAVESaveArea = false;
921 bool HasVRSaveArea = false;
922
923 SmallVector<CalleeSavedInfo, 18> GPRegs;
924 SmallVector<CalleeSavedInfo, 18> G8Regs;
925 SmallVector<CalleeSavedInfo, 18> FPRegs;
926 SmallVector<CalleeSavedInfo, 18> VRegs;
927
928 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
929 unsigned Reg = CSI[i].getReg();
Craig Topperc9099502012-04-20 06:31:50 +0000930 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000931 HasGPSaveArea = true;
932
933 GPRegs.push_back(CSI[i]);
934
935 if (Reg < MinGPR) {
936 MinGPR = Reg;
937 }
Craig Topperc9099502012-04-20 06:31:50 +0000938 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000939 HasG8SaveArea = true;
940
941 G8Regs.push_back(CSI[i]);
942
943 if (Reg < MinG8R) {
944 MinG8R = Reg;
945 }
Craig Topperc9099502012-04-20 06:31:50 +0000946 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000947 HasFPSaveArea = true;
948
949 FPRegs.push_back(CSI[i]);
950
951 if (Reg < MinFPR) {
952 MinFPR = Reg;
953 }
Craig Topperc9099502012-04-20 06:31:50 +0000954 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
955 PPC::CRRCRegClass.contains(Reg)) {
Roman Divacky9d760ae2012-09-12 14:47:47 +0000956 ; // do nothing, as we already know whether CRs are spilled
Craig Topperc9099502012-04-20 06:31:50 +0000957 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000958 HasVRSAVESaveArea = true;
Craig Topperc9099502012-04-20 06:31:50 +0000959 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000960 HasVRSaveArea = true;
961
962 VRegs.push_back(CSI[i]);
963
964 if (Reg < MinVR) {
965 MinVR = Reg;
966 }
967 } else {
968 llvm_unreachable("Unknown RegisterClass!");
969 }
970 }
971
972 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Hal Finkelaa6047d2013-03-26 20:08:20 +0000973 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000974
975 int64_t LowerBound = 0;
976
977 // Take into account stack space reserved for tail calls.
978 int TCSPDelta = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000979 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
980 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000981 LowerBound = TCSPDelta;
982 }
983
984 // The Floating-point register save area is right below the back chain word
985 // of the previous stack frame.
986 if (HasFPSaveArea) {
987 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
988 int FI = FPRegs[i].getFrameIdx();
989
990 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
991 }
992
Hal Finkelaa6047d2013-03-26 20:08:20 +0000993 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000994 }
995
996 // Check whether the frame pointer register is allocated. If so, make sure it
997 // is spilled to the correct offset.
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +0000998 if (needsFP(MF)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000999 HasGPSaveArea = true;
1000
1001 int FI = PFI->getFramePointerSaveIndex();
1002 assert(FI && "No Frame Pointer Save Slot!");
1003
1004 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1005 }
1006
1007 // General register save area starts right below the Floating-point
1008 // register save area.
1009 if (HasGPSaveArea || HasG8SaveArea) {
1010 // Move general register save area spill slots down, taking into account
1011 // the size of the Floating-point register save area.
1012 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1013 int FI = GPRegs[i].getFrameIdx();
1014
1015 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1016 }
1017
1018 // Move general register save area spill slots down, taking into account
1019 // the size of the Floating-point register save area.
1020 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1021 int FI = G8Regs[i].getFrameIdx();
1022
1023 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1024 }
1025
1026 unsigned MinReg =
Hal Finkelaa6047d2013-03-26 20:08:20 +00001027 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1028 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001029
1030 if (Subtarget.isPPC64()) {
1031 LowerBound -= (31 - MinReg + 1) * 8;
1032 } else {
1033 LowerBound -= (31 - MinReg + 1) * 4;
1034 }
1035 }
1036
Roman Divacky9d760ae2012-09-12 14:47:47 +00001037 // For 32-bit only, the CR save area is below the general register
1038 // save area. For 64-bit SVR4, the CR save area is addressed relative
1039 // to the stack pointer and hence does not need an adjustment here.
1040 // Only CR2 (the first nonvolatile spilled) has an associated frame
1041 // index so that we have a single uniform save area.
1042 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001043 // Adjust the frame index of the CR spill slot.
1044 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1045 unsigned Reg = CSI[i].getReg();
1046
Roman Divacky9d760ae2012-09-12 14:47:47 +00001047 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
1048 // Leave Darwin logic as-is.
1049 || (!Subtarget.isSVR4ABI() &&
1050 (PPC::CRBITRCRegClass.contains(Reg) ||
1051 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001052 int FI = CSI[i].getFrameIdx();
1053
1054 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1055 }
1056 }
1057
1058 LowerBound -= 4; // The CR save area is always 4 bytes long.
1059 }
1060
1061 if (HasVRSAVESaveArea) {
1062 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1063 // which have the VRSAVE register class?
1064 // Adjust the frame index of the VRSAVE spill slot.
1065 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1066 unsigned Reg = CSI[i].getReg();
1067
Craig Topperc9099502012-04-20 06:31:50 +00001068 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001069 int FI = CSI[i].getFrameIdx();
1070
1071 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1072 }
1073 }
1074
1075 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1076 }
1077
1078 if (HasVRSaveArea) {
1079 // Insert alignment padding, we need 16-byte alignment.
1080 LowerBound = (LowerBound - 15) & ~(15);
1081
1082 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1083 int FI = VRegs[i].getFrameIdx();
1084
1085 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1086 }
1087 }
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001088
1089 addScavengingSpillSlot(MF, RS);
1090}
1091
1092void
1093PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1094 RegScavenger *RS) const {
1095 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1096 // a large stack, which will require scavenging a register to materialize a
1097 // large offset.
1098
1099 // We need to have a scavenger spill slot for spills if the frame size is
1100 // large. In case there is no free register for large-offset addressing,
1101 // this slot is used for the necessary emergency spill. Also, we need the
1102 // slot for dynamic stack allocations.
1103
1104 // The scavenger might be invoked if the frame offset does not fit into
1105 // the 16-bit immediate. We don't know the complete frame size here
1106 // because we've not yet computed callee-saved register spills or the
1107 // needed alignment padding.
1108 unsigned StackSize = determineFrameLayout(MF, false, true);
1109 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkel3f2c0472013-03-23 22:06:03 +00001110 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1111 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001112 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1113 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1114 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkeldc3beb92013-03-22 23:32:27 +00001115 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001116 RC->getAlignment(),
1117 false));
Hal Finkel01f99d22013-03-26 18:57:22 +00001118
1119 // These kinds of spills might need two registers.
1120 if (spillsCR(MF) || spillsVRSAVE(MF))
1121 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1122 RC->getAlignment(),
1123 false));
1124
Hal Finkel0cfb42a2013-03-15 05:06:04 +00001125 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001126}
Roman Divacky9d760ae2012-09-12 14:47:47 +00001127
1128bool
1129PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1130 MachineBasicBlock::iterator MI,
1131 const std::vector<CalleeSavedInfo> &CSI,
1132 const TargetRegisterInfo *TRI) const {
1133
1134 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1135 // Return false otherwise to maintain pre-existing behavior.
1136 if (!Subtarget.isSVR4ABI())
1137 return false;
1138
1139 MachineFunction *MF = MBB.getParent();
1140 const PPCInstrInfo &TII =
1141 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1142 DebugLoc DL;
1143 bool CRSpilled = false;
Hal Finkel63496f62013-04-13 23:06:15 +00001144 MachineInstrBuilder CRMIB;
Roman Divacky9d760ae2012-09-12 14:47:47 +00001145
1146 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1147 unsigned Reg = CSI[i].getReg();
1148 // CR2 through CR4 are the nonvolatile CR fields.
1149 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1150
Roman Divacky9d760ae2012-09-12 14:47:47 +00001151 // Add the callee-saved register as live-in; it's killed at the spill.
1152 MBB.addLiveIn(Reg);
1153
Hal Finkel63496f62013-04-13 23:06:15 +00001154 if (CRSpilled && IsCRField) {
1155 CRMIB.addReg(Reg, RegState::ImplicitKill);
1156 continue;
1157 }
1158
Roman Divacky9d760ae2012-09-12 14:47:47 +00001159 // Insert the spill to the stack frame.
1160 if (IsCRField) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001161 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divacky9d760ae2012-09-12 14:47:47 +00001162 if (Subtarget.isPPC64()) {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001163 // The actual spill will happen at the start of the prologue.
1164 FuncInfo->addMustSaveCR(Reg);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001165 } else {
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001166 CRSpilled = true;
Bill Schmidtded53bf2013-05-14 16:08:32 +00001167 FuncInfo->setSpillsCR();
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001168
Roman Divacky9d760ae2012-09-12 14:47:47 +00001169 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1170 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
Hal Finkel63496f62013-04-13 23:06:15 +00001171 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
1172 .addReg(Reg, RegState::ImplicitKill);
1173
1174 MBB.insert(MI, CRMIB);
Roman Divacky9d760ae2012-09-12 14:47:47 +00001175 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1176 .addReg(PPC::R12,
1177 getKillRegState(true)),
1178 CSI[i].getFrameIdx()));
1179 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001180 } else {
1181 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1182 TII.storeRegToStackSlot(MBB, MI, Reg, true,
1183 CSI[i].getFrameIdx(), RC, TRI);
1184 }
1185 }
1186 return true;
1187}
1188
1189static void
Hal Finkelb99c9952013-04-13 08:09:20 +00001190restoreCRs(bool isPPC64, bool is31,
1191 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001192 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1193 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1194
1195 MachineFunction *MF = MBB.getParent();
1196 const PPCInstrInfo &TII =
1197 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1198 DebugLoc DL;
1199 unsigned RestoreOp, MoveReg;
1200
Hal Finkelfb6fe0a2013-04-15 02:07:05 +00001201 if (isPPC64)
1202 // This is handled during epilogue generation.
1203 return;
1204 else {
Roman Divacky9d760ae2012-09-12 14:47:47 +00001205 // 32-bit: FP-relative
1206 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
1207 PPC::R12),
1208 CSI[CSIIndex].getFrameIdx()));
1209 RestoreOp = PPC::MTCRF;
1210 MoveReg = PPC::R12;
1211 }
1212
1213 if (CR2Spilled)
1214 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkeld957f952013-03-28 03:38:16 +00001215 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001216
1217 if (CR3Spilled)
1218 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkeld957f952013-03-28 03:38:16 +00001219 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001220
1221 if (CR4Spilled)
1222 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkeld957f952013-03-28 03:38:16 +00001223 .addReg(MoveReg, getKillRegState(true)));
Roman Divacky9d760ae2012-09-12 14:47:47 +00001224}
1225
Eli Bendersky700ed802013-02-21 20:05:00 +00001226void PPCFrameLowering::
1227eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1228 MachineBasicBlock::iterator I) const {
1229 const PPCInstrInfo &TII =
1230 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1231 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1232 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1233 // Add (actually subtract) back the amount the callee popped on return.
1234 if (int CalleeAmt = I->getOperand(1).getImm()) {
1235 bool is64Bit = Subtarget.isPPC64();
1236 CalleeAmt *= -1;
1237 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1238 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1239 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1240 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1241 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1242 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1243 MachineInstr *MI = I;
1244 DebugLoc dl = MI->getDebugLoc();
1245
1246 if (isInt<16>(CalleeAmt)) {
1247 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1248 .addReg(StackReg, RegState::Kill)
1249 .addImm(CalleeAmt);
1250 } else {
1251 MachineBasicBlock::iterator MBBI = I;
1252 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1253 .addImm(CalleeAmt >> 16);
1254 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1255 .addReg(TmpReg, RegState::Kill)
1256 .addImm(CalleeAmt & 0xFFFF);
1257 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1258 .addReg(StackReg, RegState::Kill)
1259 .addReg(TmpReg);
1260 }
1261 }
1262 }
1263 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1264 MBB.erase(I);
1265}
1266
Roman Divacky9d760ae2012-09-12 14:47:47 +00001267bool
1268PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1269 MachineBasicBlock::iterator MI,
1270 const std::vector<CalleeSavedInfo> &CSI,
1271 const TargetRegisterInfo *TRI) const {
1272
1273 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1274 // Return false otherwise to maintain pre-existing behavior.
1275 if (!Subtarget.isSVR4ABI())
1276 return false;
1277
1278 MachineFunction *MF = MBB.getParent();
1279 const PPCInstrInfo &TII =
1280 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1281 bool CR2Spilled = false;
1282 bool CR3Spilled = false;
1283 bool CR4Spilled = false;
1284 unsigned CSIIndex = 0;
1285
1286 // Initialize insertion-point logic; we will be restoring in reverse
1287 // order of spill.
1288 MachineBasicBlock::iterator I = MI, BeforeI = I;
1289 bool AtStart = I == MBB.begin();
1290
1291 if (!AtStart)
1292 --BeforeI;
1293
1294 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1295 unsigned Reg = CSI[i].getReg();
1296
1297 if (Reg == PPC::CR2) {
1298 CR2Spilled = true;
1299 // The spill slot is associated only with CR2, which is the
1300 // first nonvolatile spilled. Save it here.
1301 CSIIndex = i;
1302 continue;
1303 } else if (Reg == PPC::CR3) {
1304 CR3Spilled = true;
1305 continue;
1306 } else if (Reg == PPC::CR4) {
1307 CR4Spilled = true;
1308 continue;
1309 } else {
1310 // When we first encounter a non-CR register after seeing at
1311 // least one CR register, restore all spilled CRs together.
1312 if ((CR2Spilled || CR3Spilled || CR4Spilled)
1313 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkelb99c9952013-04-13 08:09:20 +00001314 bool is31 = needsFP(*MF);
1315 restoreCRs(Subtarget.isPPC64(), is31,
1316 CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001317 MBB, I, CSI, CSIIndex);
1318 CR2Spilled = CR3Spilled = CR4Spilled = false;
1319 }
1320
1321 // Default behavior for non-CR saves.
1322 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1323 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
1324 RC, TRI);
1325 assert(I != MBB.begin() &&
1326 "loadRegFromStackSlot didn't insert any code!");
1327 }
1328
1329 // Insert in reverse order.
1330 if (AtStart)
1331 I = MBB.begin();
1332 else {
1333 I = BeforeI;
1334 ++I;
1335 }
1336 }
1337
1338 // If we haven't yet spilled the CRs, do so now.
Hal Finkelb99c9952013-04-13 08:09:20 +00001339 if (CR2Spilled || CR3Spilled || CR4Spilled) {
1340 bool is31 = needsFP(*MF);
1341 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Roman Divacky9d760ae2012-09-12 14:47:47 +00001342 MBB, I, CSI, CSIIndex);
Hal Finkelb99c9952013-04-13 08:09:20 +00001343 }
Roman Divacky9d760ae2012-09-12 14:47:47 +00001344
1345 return true;
1346}
1347