blob: a18fc72bd7da74704e5ef9b1d43a2ae2ac1174b3 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
Anton Korobeynikovf7183ed2010-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 Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the PPC implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "PPCFrameLowering.h"
Roman Divackyc9e23d92012-09-12 14:47:47 +000015#include "PPCInstrBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "PPCInstrInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "PPCMachineFunctionInfo.h"
Anton Korobeynikovf7183ed2010-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 Korobeynikov7283b8d2010-11-27 23:05:25 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000025#include "llvm/Target/TargetOptions.h"
26
27using namespace llvm;
28
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000029/// VRRegNo - Map from a numbered VR register to its enum value.
30///
Craig Topperca658c22012-03-11 07:16:55 +000031static const uint16_t VRRegNo[] = {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000032 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
33 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
34 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
35 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
36};
37
38/// RemoveVRSaveCode - We have found that this function does not need any code
39/// to manipulate the VRSAVE register, even though it uses vector registers.
40/// This can happen when the only registers used are known to be live in or out
41/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidt38d94582012-10-10 20:54:15 +000042/// FIXME: The removal of the code results in a compile failure at -O0 when the
43/// function contains a function call, as the GPR containing original VRSAVE
44/// contents is spilled and reloaded around the call. Without the prolog code,
45/// the spill instruction refers to an undefined register. This code needs
46/// to account for all uses of that GPR.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000047static void RemoveVRSaveCode(MachineInstr *MI) {
48 MachineBasicBlock *Entry = MI->getParent();
49 MachineFunction *MF = Entry->getParent();
50
51 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
52 MachineBasicBlock::iterator MBBI = MI;
53 ++MBBI;
54 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
55 MBBI->eraseFromParent();
56
57 bool RemovedAllMTVRSAVEs = true;
58 // See if we can find and remove the MTVRSAVE instruction from all of the
59 // epilog blocks.
60 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
61 // If last instruction is a return instruction, add an epilogue
Evan Cheng7f8e5632011-12-07 07:15:52 +000062 if (!I->empty() && I->back().isReturn()) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000063 bool FoundIt = false;
64 for (MBBI = I->end(); MBBI != I->begin(); ) {
65 --MBBI;
66 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
67 MBBI->eraseFromParent(); // remove it.
68 FoundIt = true;
69 break;
70 }
71 }
72 RemovedAllMTVRSAVEs &= FoundIt;
73 }
74 }
75
76 // If we found and removed all MTVRSAVE instructions, remove the read of
77 // VRSAVE as well.
78 if (RemovedAllMTVRSAVEs) {
79 MBBI = MI;
80 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
81 --MBBI;
82 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
83 MBBI->eraseFromParent();
84 }
85
86 // Finally, nuke the UPDATE_VRSAVE.
87 MI->eraseFromParent();
88}
89
90// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
91// instruction selector. Based on the vector registers that have been used,
92// transform this into the appropriate ORI instruction.
93static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
94 MachineFunction *MF = MI->getParent()->getParent();
Hal Finkelfeea6532013-03-26 20:08:20 +000095 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000096 DebugLoc dl = MI->getDebugLoc();
97
98 unsigned UsedRegMask = 0;
99 for (unsigned i = 0; i != 32; ++i)
100 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
101 UsedRegMask |= 1 << (31-i);
102
103 // Live in and live out values already must be in the mask, so don't bother
104 // marking them.
105 for (MachineRegisterInfo::livein_iterator
106 I = MF->getRegInfo().livein_begin(),
107 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelfeea6532013-03-26 20:08:20 +0000108 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000109 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
110 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
111 }
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000112
113 // Live out registers appear as use operands on return instructions.
114 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
115 UsedRegMask != 0 && BI != BE; ++BI) {
116 const MachineBasicBlock &MBB = *BI;
117 if (MBB.empty() || !MBB.back().isReturn())
118 continue;
119 const MachineInstr &Ret = MBB.back();
120 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
121 const MachineOperand &MO = Ret.getOperand(I);
122 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
123 continue;
Hal Finkelfeea6532013-03-26 20:08:20 +0000124 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000125 UsedRegMask &= ~(1 << (31-RegNo));
126 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000127 }
128
129 // If no registers are used, turn this into a copy.
130 if (UsedRegMask == 0) {
131 // Remove all VRSAVE code.
132 RemoveVRSaveCode(MI);
133 return;
134 }
135
136 unsigned SrcReg = MI->getOperand(1).getReg();
137 unsigned DstReg = MI->getOperand(0).getReg();
138
139 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
140 if (DstReg != SrcReg)
141 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
142 .addReg(SrcReg)
143 .addImm(UsedRegMask);
144 else
145 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
146 .addReg(SrcReg, RegState::Kill)
147 .addImm(UsedRegMask);
148 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
149 if (DstReg != SrcReg)
150 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
151 .addReg(SrcReg)
152 .addImm(UsedRegMask >> 16);
153 else
154 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
155 .addReg(SrcReg, RegState::Kill)
156 .addImm(UsedRegMask >> 16);
157 } else {
158 if (DstReg != SrcReg)
159 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
160 .addReg(SrcReg)
161 .addImm(UsedRegMask >> 16);
162 else
163 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
164 .addReg(SrcReg, RegState::Kill)
165 .addImm(UsedRegMask >> 16);
166
167 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
168 .addReg(DstReg, RegState::Kill)
169 .addImm(UsedRegMask & 0xFFFF);
170 }
171
172 // Remove the old UPDATE_VRSAVE instruction.
173 MI->eraseFromParent();
174}
175
Roman Divackyc9e23d92012-09-12 14:47:47 +0000176static bool spillsCR(const MachineFunction &MF) {
177 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
178 return FuncInfo->isCRSpilled();
179}
180
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000181static bool spillsVRSAVE(const MachineFunction &MF) {
182 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
183 return FuncInfo->isVRSAVESpilled();
184}
185
Hal Finkelbb420f12013-03-15 05:06:04 +0000186static bool hasSpills(const MachineFunction &MF) {
187 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
188 return FuncInfo->hasSpills();
189}
190
Hal Finkelfcc51d42013-03-17 04:43:44 +0000191static bool hasNonRISpills(const MachineFunction &MF) {
192 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
193 return FuncInfo->hasNonRISpills();
194}
195
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000196/// determineFrameLayout - Determine the size of the frame and maximum call
197/// frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000198unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
199 bool UpdateMF,
200 bool UseEstimate) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000201 MachineFrameInfo *MFI = MF.getFrameInfo();
202
203 // Get the number of bytes to allocate from the FrameInfo
Hal Finkelbb420f12013-03-15 05:06:04 +0000204 unsigned FrameSize =
205 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000206
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000207 // Get stack alignments. The frame must be aligned to the greatest of these:
208 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
209 unsigned MaxAlign = MFI->getMaxAlignment(); // algmt required by data in frame
Hal Finkela7c54e82013-07-17 00:45:52 +0000210 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
211
212 const PPCRegisterInfo *RegInfo =
213 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000214
215 // If we are a leaf function, and use up to 224 bytes of stack space,
216 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkel67369882013-04-15 02:07:05 +0000217 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000218 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
219 // stackless code if all local vars are reg-allocated.
Bill Wendling698e84f2012-12-30 10:32:01 +0000220 bool DisableRedZone = MF.getFunction()->getAttributes().
221 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000222 if (!DisableRedZone &&
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000223 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
224 !Subtarget.isSVR4ABI() || // allocated locals.
Eric Christopherd1737492014-04-29 00:16:40 +0000225 FrameSize == 0) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000226 FrameSize <= 224 && // Fits in red zone.
227 !MFI->hasVarSizedObjects() && // No dynamic alloca.
228 !MFI->adjustsStack() && // No calls.
Hal Finkela7c54e82013-07-17 00:45:52 +0000229 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000230 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000231 if (UpdateMF)
232 MFI->setStackSize(0);
233 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000234 }
235
236 // Get the maximum call frame size of all the calls.
237 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
238
239 // Maximum call frame needs to be at least big enough for linkage and 8 args.
240 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(),
241 Subtarget.isDarwinABI());
242 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
243
244 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
245 // that allocations will be aligned.
246 if (MFI->hasVarSizedObjects())
247 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
248
249 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000250 if (UpdateMF)
251 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000252
253 // Include call frame size in total.
254 FrameSize += maxCallFrameSize;
255
256 // Make sure the frame is aligned.
257 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
258
259 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000260 if (UpdateMF)
261 MFI->setStackSize(FrameSize);
262
263 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000264}
265
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000266// hasFP - Return true if the specified function actually has a dedicated frame
267// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000268bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000269 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000270 // FIXME: This is pretty much broken by design: hasFP() might be called really
271 // early, before the stack layout was calculated and thus hasFP() might return
272 // true or false here depending on the time of call.
273 return (MFI->getStackSize()) && needsFP(MF);
274}
275
276// needsFP - Return true if the specified function should have a dedicated frame
277// pointer register. This is true if the function has variable sized allocas or
278// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000279bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000280 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000281
282 // Naked functions have no stack frame pushed, so we don't have a frame
283 // pointer.
Eric Christopherd1737492014-04-29 00:16:40 +0000284 if (MF.getFunction()->getAttributes().hasAttribute(
285 AttributeSet::FunctionIndex, Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000286 return false;
287
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000288 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
289 MFI->hasVarSizedObjects() ||
290 (MF.getTarget().Options.GuaranteedTailCallOpt &&
291 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000292}
293
Hal Finkelaa03c032013-03-21 19:03:19 +0000294void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
295 bool is31 = needsFP(MF);
296 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
297 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
298
Hal Finkelf05d6c72013-07-17 23:50:51 +0000299 const PPCRegisterInfo *RegInfo =
300 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
301 bool HasBP = RegInfo->hasBasePointer(MF);
302 unsigned BPReg = HasBP ? (unsigned) PPC::R30 : FPReg;
303 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
304
Hal Finkelaa03c032013-03-21 19:03:19 +0000305 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
306 BI != BE; ++BI)
307 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
308 --MBBI;
309 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
310 MachineOperand &MO = MBBI->getOperand(I);
311 if (!MO.isReg())
312 continue;
313
314 switch (MO.getReg()) {
315 case PPC::FP:
316 MO.setReg(FPReg);
317 break;
318 case PPC::FP8:
319 MO.setReg(FP8Reg);
320 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000321 case PPC::BP:
322 MO.setReg(BPReg);
323 break;
324 case PPC::BP8:
325 MO.setReg(BP8Reg);
326 break;
327
Hal Finkelaa03c032013-03-21 19:03:19 +0000328 }
329 }
330 }
331}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000332
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000333void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000334 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
335 MachineBasicBlock::iterator MBBI = MBB.begin();
336 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000337 const PPCInstrInfo &TII =
338 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +0000339 const PPCRegisterInfo *RegInfo =
340 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000341
342 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000343 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000344 DebugLoc dl;
345 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000346 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000347
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000348 // Get processor type.
349 bool isPPC64 = Subtarget.isPPC64();
350 // Get the ABI.
351 bool isDarwinABI = Subtarget.isDarwinABI();
352 bool isSVR4ABI = Subtarget.isSVR4ABI();
353 assert((isDarwinABI || isSVR4ABI) &&
354 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
355
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000356 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
357 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000358 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000359 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
360 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
361 HandleVRSaveUpdate(MBBI, TII);
362 break;
363 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000364 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000365
366 // Move MBBI back to the beginning of the function.
367 MBBI = MBB.begin();
368
369 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000370 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000371 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000372 if (!isInt<32>(NegFrameSize))
373 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000374
Hal Finkelaa03c032013-03-21 19:03:19 +0000375 if (MFI->isFrameAddressTaken())
376 replaceFPWithRealFP(MF);
377
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000378 // Check if the link register (LR) must be saved.
379 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
380 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000381 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000382 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000383 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000384 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000385
Bill Schmidtf381afc2013-08-20 03:12:23 +0000386 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
387 unsigned BPReg = isPPC64 ? PPC::X30 : PPC::R30;
388 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
389 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
390 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
391 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
392 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
393 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
394 : PPC::MFLR );
395 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
396 : PPC::STW );
397 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
398 : PPC::STWU );
399 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
400 : PPC::STWUX);
401 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
402 : PPC::LIS );
403 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
404 : PPC::ORI );
405 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
406 : PPC::OR );
407 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
408 : PPC::SUBFC);
409 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
410 : PPC::SUBFIC);
411
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000412 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
413 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
414 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
415 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
416 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
417 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
418
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000419 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000420
421 int FPOffset = 0;
422 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000423 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000424 MachineFrameInfo *FFI = MF.getFrameInfo();
425 int FPIndex = FI->getFramePointerSaveIndex();
426 assert(FPIndex && "No Frame Pointer Save Slot!");
427 FPOffset = FFI->getObjectOffset(FPIndex);
428 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000429 FPOffset =
430 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000431 }
432 }
433
Hal Finkela7c54e82013-07-17 00:45:52 +0000434 int BPOffset = 0;
435 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000436 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000437 MachineFrameInfo *FFI = MF.getFrameInfo();
438 int BPIndex = FI->getBasePointerSaveIndex();
439 assert(BPIndex && "No Base Pointer Save Slot!");
440 BPOffset = FFI->getObjectOffset(BPIndex);
441 } else {
442 BPOffset =
Hal Finkelf05d6c72013-07-17 23:50:51 +0000443 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkela7c54e82013-07-17 00:45:52 +0000444 }
445 }
446
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000447 // Get stack alignments.
448 unsigned MaxAlign = MFI->getMaxAlignment();
449 if (HasBP && MaxAlign > 1)
450 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
451 "Invalid alignment!");
452
453 // Frames of 32KB & larger require special handling because they cannot be
454 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
455 bool isLargeFrame = !isInt<16>(NegFrameSize);
456
Bill Schmidtf381afc2013-08-20 03:12:23 +0000457 if (MustSaveLR)
458 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000459
Bill Schmidtf381afc2013-08-20 03:12:23 +0000460 assert((isPPC64 || MustSaveCRs.empty()) &&
461 "Prologue CR saving supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000462
Bill Schmidtf381afc2013-08-20 03:12:23 +0000463 if (!MustSaveCRs.empty()) { // will only occur for PPC64
464 MachineInstrBuilder MIB =
465 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), TempReg);
466 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
467 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000468 }
469
Bill Schmidtf381afc2013-08-20 03:12:23 +0000470 if (HasFP)
471 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
472 BuildMI(MBB, MBBI, dl, StoreInst)
473 .addReg(FPReg)
474 .addImm(FPOffset)
475 .addReg(SPReg);
476
477 if (HasBP)
478 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
479 BuildMI(MBB, MBBI, dl, StoreInst)
480 .addReg(BPReg)
481 .addImm(BPOffset)
482 .addReg(SPReg);
483
484 if (MustSaveLR)
485 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
486 BuildMI(MBB, MBBI, dl, StoreInst)
487 .addReg(ScratchReg)
488 .addImm(LROffset)
489 .addReg(SPReg);
490
491 if (!MustSaveCRs.empty()) // will only occur for PPC64
492 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
493 .addReg(TempReg, getKillRegState(true))
494 .addImm(8)
495 .addReg(SPReg);
496
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000497 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000498 if (!FrameSize) return;
499
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000500 // Adjust stack pointer: r1 += NegFrameSize.
501 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000502
Bill Schmidtf381afc2013-08-20 03:12:23 +0000503 if (HasBP) {
504 // Save a copy of r1 as the base pointer.
505 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
506 .addReg(SPReg)
507 .addReg(SPReg);
508 }
509
510 if (HasBP && MaxAlign > 1) {
511 if (isPPC64)
512 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
513 .addReg(SPReg)
514 .addImm(0)
515 .addImm(64 - Log2_32(MaxAlign));
516 else // PPC32...
517 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
518 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000519 .addImm(0)
520 .addImm(32 - Log2_32(MaxAlign))
521 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000522 if (!isLargeFrame) {
523 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
524 .addReg(ScratchReg, RegState::Kill)
525 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000526 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000527 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000528 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000529 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
530 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000531 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000532 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
533 .addReg(ScratchReg, RegState::Kill)
534 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000535 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000536 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
537 .addReg(SPReg, RegState::Kill)
538 .addReg(SPReg)
539 .addReg(ScratchReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000540
Bill Schmidtf381afc2013-08-20 03:12:23 +0000541 } else if (!isLargeFrame) {
542 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
543 .addReg(SPReg)
544 .addImm(NegFrameSize)
545 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000546
Bill Schmidtf381afc2013-08-20 03:12:23 +0000547 } else {
548 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
549 .addImm(NegFrameSize >> 16);
550 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
551 .addReg(ScratchReg, RegState::Kill)
552 .addImm(NegFrameSize & 0xFFFF);
553 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
554 .addReg(SPReg, RegState::Kill)
555 .addReg(SPReg)
556 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000557 }
558
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000559 // Add the "machine moves" for the instructions we generated above, but in
560 // reverse order.
561 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000562 // Show update of SP.
Rafael Espindola6e8c0d92013-05-16 03:34:58 +0000563 assert(NegFrameSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000564 unsigned CFIIndex = MMI.addFrameInst(
565 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
566 BuildMI(MBB, MBBI, dl, TII.get(PPC::CFI_INSTRUCTION)).addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000567
568 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000569 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000570 CFIIndex = MMI.addFrameInst(
571 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
572 BuildMI(MBB, MBBI, dl, TII.get(PPC::CFI_INSTRUCTION))
573 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000574 }
575
Hal Finkela7c54e82013-07-17 00:45:52 +0000576 if (HasBP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000577 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000578 CFIIndex = MMI.addFrameInst(
579 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
580 BuildMI(MBB, MBBI, dl, TII.get(PPC::CFI_INSTRUCTION))
581 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000582 }
583
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000584 if (MustSaveLR) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000585 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000586 CFIIndex = MMI.addFrameInst(
587 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
588 BuildMI(MBB, MBBI, dl, TII.get(PPC::CFI_INSTRUCTION))
589 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000590 }
591 }
592
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000593 // If there is a frame pointer, copy R1 into R31
594 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000595 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
596 .addReg(SPReg)
597 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000598
599 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000600 // Mark effective beginning of when frame pointer is ready.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000601 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000602 unsigned CFIIndex = MMI.addFrameInst(
603 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
604
605 BuildMI(MBB, MBBI, dl, TII.get(PPC::CFI_INSTRUCTION))
606 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000607 }
608 }
609
610 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000611 // Add callee saved registers to move list.
612 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
613 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000614 unsigned Reg = CSI[I].getReg();
615 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000616
617 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
618 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +0000619 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +0000620 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000621
Roman Divackyc9e23d92012-09-12 14:47:47 +0000622 // For SVR4, don't emit a move for the CR spill slot if we haven't
623 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000624 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
625 && MustSaveCRs.empty())
626 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000627
628 // For 64-bit SVR4 when we have spilled CRs, the spill location
629 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000630 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000631 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
632 nullptr, MRI->getDwarfRegNum(PPC::CR2, true), 8));
633 BuildMI(MBB, MBBI, dl, TII.get(PPC::CFI_INSTRUCTION))
634 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000635 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000636 }
637
638 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000639 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
640 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
641 BuildMI(MBB, MBBI, dl, TII.get(PPC::CFI_INSTRUCTION))
642 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000643 }
644 }
645}
646
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000647void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000648 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000649 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
650 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000651 const PPCInstrInfo &TII =
652 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +0000653 const PPCRegisterInfo *RegInfo =
654 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000655
656 unsigned RetOpcode = MBBI->getOpcode();
657 DebugLoc dl;
658
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000659 assert((RetOpcode == PPC::BLR ||
660 RetOpcode == PPC::TCRETURNri ||
661 RetOpcode == PPC::TCRETURNdi ||
662 RetOpcode == PPC::TCRETURNai ||
663 RetOpcode == PPC::TCRETURNri8 ||
664 RetOpcode == PPC::TCRETURNdi8 ||
665 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000666 "Can only insert epilog into returning blocks");
667
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000668 // Get alignment info so we know how to restore the SP.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000669 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000670
671 // Get the number of bytes allocated from the FrameInfo.
672 int FrameSize = MFI->getStackSize();
673
674 // Get processor type.
675 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000676 // Get the ABI.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000677 bool isDarwinABI = Subtarget.isDarwinABI();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000678 bool isSVR4ABI = Subtarget.isSVR4ABI();
679
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000680 // Check if the link register (LR) has been saved.
681 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
682 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000683 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000684 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000685 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000686 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000687
Bill Schmidtf381afc2013-08-20 03:12:23 +0000688 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
689 unsigned BPReg = isPPC64 ? PPC::X30 : PPC::R30;
690 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
691 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
692 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
693 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
694 : PPC::MTLR );
695 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
696 : PPC::LWZ );
697 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
698 : PPC::LIS );
699 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
700 : PPC::ORI );
701 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
702 : PPC::ADDI );
703 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
704 : PPC::ADD4 );
705
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000706 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000707
708 int FPOffset = 0;
709 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000710 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000711 MachineFrameInfo *FFI = MF.getFrameInfo();
712 int FPIndex = FI->getFramePointerSaveIndex();
713 assert(FPIndex && "No Frame Pointer Save Slot!");
714 FPOffset = FFI->getObjectOffset(FPIndex);
715 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000716 FPOffset =
717 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000718 }
719 }
720
Hal Finkela7c54e82013-07-17 00:45:52 +0000721 int BPOffset = 0;
722 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000723 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000724 MachineFrameInfo *FFI = MF.getFrameInfo();
725 int BPIndex = FI->getBasePointerSaveIndex();
726 assert(BPIndex && "No Base Pointer Save Slot!");
727 BPOffset = FFI->getObjectOffset(BPIndex);
728 } else {
729 BPOffset =
Hal Finkelf05d6c72013-07-17 23:50:51 +0000730 PPCFrameLowering::getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkela7c54e82013-07-17 00:45:52 +0000731 }
732 }
733
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000734 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
735 RetOpcode == PPC::TCRETURNdi ||
736 RetOpcode == PPC::TCRETURNai ||
737 RetOpcode == PPC::TCRETURNri8 ||
738 RetOpcode == PPC::TCRETURNdi8 ||
739 RetOpcode == PPC::TCRETURNai8;
740
741 if (UsesTCRet) {
742 int MaxTCRetDelta = FI->getTailCallSPDelta();
743 MachineOperand &StackAdjust = MBBI->getOperand(1);
744 assert(StackAdjust.isImm() && "Expecting immediate value.");
745 // Adjust stack pointer.
746 int StackAdj = StackAdjust.getImm();
747 int Delta = StackAdj - MaxTCRetDelta;
748 assert((Delta >= 0) && "Delta must be positive");
749 if (MaxTCRetDelta>0)
750 FrameSize += (StackAdj +Delta);
751 else
752 FrameSize += StackAdj;
753 }
754
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000755 // Frames of 32KB & larger require special handling because they cannot be
756 // indexed into with a simple LD/LWZ immediate offset operand.
757 bool isLargeFrame = !isInt<16>(FrameSize);
758
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000759 if (FrameSize) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000760 // In the prologue, the loaded (or persistent) stack pointer value is offset
761 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000762
763 // If this function contained a fastcc call and GuaranteedTailCallOpt is
764 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
765 // call which invalidates the stack pointer value in SP(0). So we use the
766 // value of R31 in this case.
767 if (FI->hasFastCall()) {
768 assert(HasFP && "Expecting a valid frame pointer.");
769 if (!isLargeFrame) {
770 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
771 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000772 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000773 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
774 .addImm(FrameSize >> 16);
775 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
776 .addReg(ScratchReg, RegState::Kill)
777 .addImm(FrameSize & 0xFFFF);
778 BuildMI(MBB, MBBI, dl, AddInst)
779 .addReg(SPReg)
780 .addReg(FPReg)
781 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000782 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000783 } else if (!isLargeFrame && !HasBP && !MFI->hasVarSizedObjects()) {
784 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
785 .addReg(SPReg)
786 .addImm(FrameSize);
787 } else {
788 BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
789 .addImm(0)
790 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000791 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000792
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000793 }
794
Bill Schmidtf381afc2013-08-20 03:12:23 +0000795 if (MustSaveLR)
796 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
797 .addImm(LROffset)
798 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000799
Bill Schmidtf381afc2013-08-20 03:12:23 +0000800 assert((isPPC64 || MustSaveCRs.empty()) &&
801 "Epilogue CR restoring supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000802
Bill Schmidtf381afc2013-08-20 03:12:23 +0000803 if (!MustSaveCRs.empty()) // will only occur for PPC64
804 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
805 .addImm(8)
806 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000807
Bill Schmidtf381afc2013-08-20 03:12:23 +0000808 if (HasFP)
809 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
810 .addImm(FPOffset)
811 .addReg(SPReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000812
Bill Schmidtf381afc2013-08-20 03:12:23 +0000813 if (HasBP)
814 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
815 .addImm(BPOffset)
816 .addReg(SPReg);
Hal Finkel67369882013-04-15 02:07:05 +0000817
Bill Schmidtf381afc2013-08-20 03:12:23 +0000818 if (!MustSaveCRs.empty()) // will only occur for PPC64
819 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
820 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
821 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000822
Bill Schmidtf381afc2013-08-20 03:12:23 +0000823 if (MustSaveLR)
824 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000825
826 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
827 // call optimization
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000828 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000829 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
830 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
831 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000832
833 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000834 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
835 .addReg(SPReg).addImm(CallerAllocatedAmt);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000836 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000837 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000838 .addImm(CallerAllocatedAmt >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000839 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
840 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000841 .addImm(CallerAllocatedAmt & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000842 BuildMI(MBB, MBBI, dl, AddInst)
843 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000844 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +0000845 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000846 }
847 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000848 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000849 MachineOperand &JumpTarget = MBBI->getOperand(0);
850 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
851 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
852 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000853 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000854 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
855 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
856 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000857 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000858 MachineOperand &JumpTarget = MBBI->getOperand(0);
859 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
860 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000861 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000862 MachineOperand &JumpTarget = MBBI->getOperand(0);
863 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
864 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
865 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000866 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000867 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
868 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
869 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000870 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000871 MachineOperand &JumpTarget = MBBI->getOperand(0);
872 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
873 }
874}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +0000875
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000876/// MustSaveLR - Return true if this function requires that we save the LR
877/// register onto the stack in the prolog and restore it in the epilog of the
878/// function.
879static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
880 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
881
882 // We need a save/restore of LR if there is any def of LR (which is
883 // defined by calls, including the PIC setup sequence), or if there is
884 // some use of the LR stack slot (e.g. for builtin_return_address).
885 // (LR comes in 32 and 64 bit versions.)
886 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
887 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
888}
889
890void
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000891PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +0000892 RegScavenger *) const {
Hal Finkela7c54e82013-07-17 00:45:52 +0000893 const PPCRegisterInfo *RegInfo =
894 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000895
896 // Save and clear the LR state.
897 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
898 unsigned LR = RegInfo->getRARegister();
899 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidtc68c6df2013-02-24 17:34:50 +0000900 MachineRegisterInfo &MRI = MF.getRegInfo();
901 MRI.setPhysRegUnused(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000902
903 // Save R31 if necessary
904 int FPSI = FI->getFramePointerSaveIndex();
905 bool isPPC64 = Subtarget.isPPC64();
906 bool isDarwinABI = Subtarget.isDarwinABI();
907 MachineFrameInfo *MFI = MF.getFrameInfo();
908
909 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000910 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000911 // Find out what the fix offset of the frame pointer save area.
912 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
913 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000914 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000915 // Save the result.
916 FI->setFramePointerSaveIndex(FPSI);
917 }
918
Hal Finkela7c54e82013-07-17 00:45:52 +0000919 int BPSI = FI->getBasePointerSaveIndex();
920 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Hal Finkelf05d6c72013-07-17 23:50:51 +0000921 int BPOffset = getBasePointerSaveOffset(isPPC64, isDarwinABI);
Hal Finkela7c54e82013-07-17 00:45:52 +0000922 // Allocate the frame index for the base pointer save area.
923 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
924 // Save the result.
925 FI->setBasePointerSaveIndex(BPSI);
926 }
927
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000928 // Reserve stack space to move the linkage area to in case of a tail call.
929 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000930 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
931 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000932 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000933 }
934
Eric Christopherd1737492014-04-29 00:16:40 +0000935 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +0000936 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +0000937 if (!isPPC64 && !isDarwinABI &&
Bill Schmidtc68c6df2013-02-24 17:34:50 +0000938 (MRI.isPhysRegUsed(PPC::CR2) ||
939 MRI.isPhysRegUsed(PPC::CR3) ||
940 MRI.isPhysRegUsed(PPC::CR4))) {
941 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
942 FI->setCRSpillFrameIndex(FrameIdx);
943 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000944}
945
Hal Finkel5a765fd2013-03-14 20:33:40 +0000946void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +0000947 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000948 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +0000949 if (!Subtarget.isSVR4ABI()) {
950 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000951 return;
Hal Finkelbb420f12013-03-15 05:06:04 +0000952 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000953
954 // Get callee saved register information.
955 MachineFrameInfo *FFI = MF.getFrameInfo();
956 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
957
958 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000959 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +0000960 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000961 return;
962 }
963
964 unsigned MinGPR = PPC::R31;
965 unsigned MinG8R = PPC::X31;
966 unsigned MinFPR = PPC::F31;
967 unsigned MinVR = PPC::V31;
968
969 bool HasGPSaveArea = false;
970 bool HasG8SaveArea = false;
971 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000972 bool HasVRSAVESaveArea = false;
973 bool HasVRSaveArea = false;
974
975 SmallVector<CalleeSavedInfo, 18> GPRegs;
976 SmallVector<CalleeSavedInfo, 18> G8Regs;
977 SmallVector<CalleeSavedInfo, 18> FPRegs;
978 SmallVector<CalleeSavedInfo, 18> VRegs;
979
980 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
981 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +0000982 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000983 HasGPSaveArea = true;
984
985 GPRegs.push_back(CSI[i]);
986
987 if (Reg < MinGPR) {
988 MinGPR = Reg;
989 }
Craig Topperabadc662012-04-20 06:31:50 +0000990 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000991 HasG8SaveArea = true;
992
993 G8Regs.push_back(CSI[i]);
994
995 if (Reg < MinG8R) {
996 MinG8R = Reg;
997 }
Craig Topperabadc662012-04-20 06:31:50 +0000998 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000999 HasFPSaveArea = true;
1000
1001 FPRegs.push_back(CSI[i]);
1002
1003 if (Reg < MinFPR) {
1004 MinFPR = Reg;
1005 }
Craig Topperabadc662012-04-20 06:31:50 +00001006 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1007 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001008 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001009 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001010 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001011 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001012 HasVRSaveArea = true;
1013
1014 VRegs.push_back(CSI[i]);
1015
1016 if (Reg < MinVR) {
1017 MinVR = Reg;
1018 }
1019 } else {
1020 llvm_unreachable("Unknown RegisterClass!");
1021 }
1022 }
1023
1024 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Hal Finkelfeea6532013-03-26 20:08:20 +00001025 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001026
1027 int64_t LowerBound = 0;
1028
1029 // Take into account stack space reserved for tail calls.
1030 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001031 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1032 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001033 LowerBound = TCSPDelta;
1034 }
1035
1036 // The Floating-point register save area is right below the back chain word
1037 // of the previous stack frame.
1038 if (HasFPSaveArea) {
1039 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1040 int FI = FPRegs[i].getFrameIdx();
1041
1042 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1043 }
1044
Hal Finkelfeea6532013-03-26 20:08:20 +00001045 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001046 }
1047
1048 // Check whether the frame pointer register is allocated. If so, make sure it
1049 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001050 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001051 HasGPSaveArea = true;
1052
1053 int FI = PFI->getFramePointerSaveIndex();
1054 assert(FI && "No Frame Pointer Save Slot!");
1055
1056 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1057 }
1058
Hal Finkela7c54e82013-07-17 00:45:52 +00001059 const PPCRegisterInfo *RegInfo =
1060 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
1061 if (RegInfo->hasBasePointer(MF)) {
1062 HasGPSaveArea = true;
1063
1064 int FI = PFI->getBasePointerSaveIndex();
1065 assert(FI && "No Base Pointer Save Slot!");
1066
1067 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1068 }
1069
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001070 // General register save area starts right below the Floating-point
1071 // register save area.
1072 if (HasGPSaveArea || HasG8SaveArea) {
1073 // Move general register save area spill slots down, taking into account
1074 // the size of the Floating-point register save area.
1075 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1076 int FI = GPRegs[i].getFrameIdx();
1077
1078 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1079 }
1080
1081 // Move general register save area spill slots down, taking into account
1082 // the size of the Floating-point register save area.
1083 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1084 int FI = G8Regs[i].getFrameIdx();
1085
1086 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1087 }
1088
1089 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001090 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1091 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001092
1093 if (Subtarget.isPPC64()) {
1094 LowerBound -= (31 - MinReg + 1) * 8;
1095 } else {
1096 LowerBound -= (31 - MinReg + 1) * 4;
1097 }
1098 }
1099
Roman Divackyc9e23d92012-09-12 14:47:47 +00001100 // For 32-bit only, the CR save area is below the general register
1101 // save area. For 64-bit SVR4, the CR save area is addressed relative
1102 // to the stack pointer and hence does not need an adjustment here.
1103 // Only CR2 (the first nonvolatile spilled) has an associated frame
1104 // index so that we have a single uniform save area.
1105 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001106 // Adjust the frame index of the CR spill slot.
1107 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1108 unsigned Reg = CSI[i].getReg();
1109
Roman Divackyc9e23d92012-09-12 14:47:47 +00001110 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001111 // Leave Darwin logic as-is.
1112 || (!Subtarget.isSVR4ABI() &&
1113 (PPC::CRBITRCRegClass.contains(Reg) ||
1114 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001115 int FI = CSI[i].getFrameIdx();
1116
1117 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1118 }
1119 }
1120
1121 LowerBound -= 4; // The CR save area is always 4 bytes long.
1122 }
1123
1124 if (HasVRSAVESaveArea) {
1125 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1126 // which have the VRSAVE register class?
1127 // Adjust the frame index of the VRSAVE spill slot.
1128 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1129 unsigned Reg = CSI[i].getReg();
1130
Craig Topperabadc662012-04-20 06:31:50 +00001131 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001132 int FI = CSI[i].getFrameIdx();
1133
1134 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1135 }
1136 }
1137
1138 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1139 }
1140
1141 if (HasVRSaveArea) {
1142 // Insert alignment padding, we need 16-byte alignment.
1143 LowerBound = (LowerBound - 15) & ~(15);
1144
1145 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1146 int FI = VRegs[i].getFrameIdx();
1147
1148 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1149 }
1150 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001151
1152 addScavengingSpillSlot(MF, RS);
1153}
1154
1155void
1156PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1157 RegScavenger *RS) const {
1158 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1159 // a large stack, which will require scavenging a register to materialize a
1160 // large offset.
1161
1162 // We need to have a scavenger spill slot for spills if the frame size is
1163 // large. In case there is no free register for large-offset addressing,
1164 // this slot is used for the necessary emergency spill. Also, we need the
1165 // slot for dynamic stack allocations.
1166
1167 // The scavenger might be invoked if the frame offset does not fit into
1168 // the 16-bit immediate. We don't know the complete frame size here
1169 // because we've not yet computed callee-saved register spills or the
1170 // needed alignment padding.
1171 unsigned StackSize = determineFrameLayout(MF, false, true);
1172 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001173 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1174 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001175 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1176 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1177 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkel9e331c22013-03-22 23:32:27 +00001178 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkelbb420f12013-03-15 05:06:04 +00001179 RC->getAlignment(),
1180 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001181
Hal Finkel18607632013-07-18 04:28:21 +00001182 // Might we have over-aligned allocas?
1183 bool HasAlVars = MFI->hasVarSizedObjects() &&
1184 MFI->getMaxAlignment() > getStackAlignment();
1185
Hal Finkel0dfbb052013-03-26 18:57:22 +00001186 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001187 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Hal Finkel0dfbb052013-03-26 18:57:22 +00001188 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1189 RC->getAlignment(),
1190 false));
1191
Hal Finkelbb420f12013-03-15 05:06:04 +00001192 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001193}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001194
Eric Christopherd1737492014-04-29 00:16:40 +00001195bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001196PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001197 MachineBasicBlock::iterator MI,
1198 const std::vector<CalleeSavedInfo> &CSI,
1199 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001200
1201 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1202 // Return false otherwise to maintain pre-existing behavior.
1203 if (!Subtarget.isSVR4ABI())
1204 return false;
1205
1206 MachineFunction *MF = MBB.getParent();
1207 const PPCInstrInfo &TII =
1208 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1209 DebugLoc DL;
1210 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001211 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001212
Roman Divackyc9e23d92012-09-12 14:47:47 +00001213 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1214 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001215 // Only Darwin actually uses the VRSAVE register, but it can still appear
1216 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1217 // Darwin, ignore it.
1218 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1219 continue;
1220
Roman Divackyc9e23d92012-09-12 14:47:47 +00001221 // CR2 through CR4 are the nonvolatile CR fields.
1222 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1223
Roman Divackyc9e23d92012-09-12 14:47:47 +00001224 // Add the callee-saved register as live-in; it's killed at the spill.
1225 MBB.addLiveIn(Reg);
1226
Hal Finkel2f293912013-04-13 23:06:15 +00001227 if (CRSpilled && IsCRField) {
1228 CRMIB.addReg(Reg, RegState::ImplicitKill);
1229 continue;
1230 }
1231
Roman Divackyc9e23d92012-09-12 14:47:47 +00001232 // Insert the spill to the stack frame.
1233 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001234 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001235 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001236 // The actual spill will happen at the start of the prologue.
1237 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001238 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001239 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001240 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001241
Eric Christopherd1737492014-04-29 00:16:40 +00001242 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1243 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1244 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001245 .addReg(Reg, RegState::ImplicitKill);
1246
Eric Christopherd1737492014-04-29 00:16:40 +00001247 MBB.insert(MI, CRMIB);
1248 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1249 .addReg(PPC::R12,
1250 getKillRegState(true)),
1251 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001252 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001253 } else {
1254 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1255 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001256 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001257 }
1258 }
1259 return true;
1260}
1261
1262static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001263restoreCRs(bool isPPC64, bool is31,
1264 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001265 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1266 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001267
1268 MachineFunction *MF = MBB.getParent();
1269 const PPCInstrInfo &TII =
1270 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1271 DebugLoc DL;
1272 unsigned RestoreOp, MoveReg;
1273
Hal Finkel67369882013-04-15 02:07:05 +00001274 if (isPPC64)
1275 // This is handled during epilogue generation.
1276 return;
1277 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001278 // 32-bit: FP-relative
1279 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00001280 PPC::R12),
1281 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00001282 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001283 MoveReg = PPC::R12;
1284 }
Eric Christopherd1737492014-04-29 00:16:40 +00001285
Roman Divackyc9e23d92012-09-12 14:47:47 +00001286 if (CR2Spilled)
1287 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00001288 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001289
1290 if (CR3Spilled)
1291 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00001292 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001293
1294 if (CR4Spilled)
1295 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00001296 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001297}
1298
Eli Bendersky8da87162013-02-21 20:05:00 +00001299void PPCFrameLowering::
1300eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1301 MachineBasicBlock::iterator I) const {
1302 const PPCInstrInfo &TII =
1303 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1304 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1305 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1306 // Add (actually subtract) back the amount the callee popped on return.
1307 if (int CalleeAmt = I->getOperand(1).getImm()) {
1308 bool is64Bit = Subtarget.isPPC64();
1309 CalleeAmt *= -1;
1310 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1311 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1312 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1313 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1314 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1315 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1316 MachineInstr *MI = I;
1317 DebugLoc dl = MI->getDebugLoc();
1318
1319 if (isInt<16>(CalleeAmt)) {
1320 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1321 .addReg(StackReg, RegState::Kill)
1322 .addImm(CalleeAmt);
1323 } else {
1324 MachineBasicBlock::iterator MBBI = I;
1325 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1326 .addImm(CalleeAmt >> 16);
1327 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1328 .addReg(TmpReg, RegState::Kill)
1329 .addImm(CalleeAmt & 0xFFFF);
1330 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1331 .addReg(StackReg, RegState::Kill)
1332 .addReg(TmpReg);
1333 }
1334 }
1335 }
1336 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1337 MBB.erase(I);
1338}
1339
Eric Christopherd1737492014-04-29 00:16:40 +00001340bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001341PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001342 MachineBasicBlock::iterator MI,
1343 const std::vector<CalleeSavedInfo> &CSI,
1344 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001345
1346 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1347 // Return false otherwise to maintain pre-existing behavior.
1348 if (!Subtarget.isSVR4ABI())
1349 return false;
1350
1351 MachineFunction *MF = MBB.getParent();
1352 const PPCInstrInfo &TII =
1353 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1354 bool CR2Spilled = false;
1355 bool CR3Spilled = false;
1356 bool CR4Spilled = false;
1357 unsigned CSIIndex = 0;
1358
1359 // Initialize insertion-point logic; we will be restoring in reverse
1360 // order of spill.
1361 MachineBasicBlock::iterator I = MI, BeforeI = I;
1362 bool AtStart = I == MBB.begin();
1363
1364 if (!AtStart)
1365 --BeforeI;
1366
1367 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1368 unsigned Reg = CSI[i].getReg();
1369
Hal Finkelac1a24b2013-06-28 22:29:56 +00001370 // Only Darwin actually uses the VRSAVE register, but it can still appear
1371 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1372 // Darwin, ignore it.
1373 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1374 continue;
1375
Roman Divackyc9e23d92012-09-12 14:47:47 +00001376 if (Reg == PPC::CR2) {
1377 CR2Spilled = true;
1378 // The spill slot is associated only with CR2, which is the
1379 // first nonvolatile spilled. Save it here.
1380 CSIIndex = i;
1381 continue;
1382 } else if (Reg == PPC::CR3) {
1383 CR3Spilled = true;
1384 continue;
1385 } else if (Reg == PPC::CR4) {
1386 CR4Spilled = true;
1387 continue;
1388 } else {
1389 // When we first encounter a non-CR register after seeing at
1390 // least one CR register, restore all spilled CRs together.
1391 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00001392 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00001393 bool is31 = needsFP(*MF);
1394 restoreCRs(Subtarget.isPPC64(), is31,
1395 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001396 MBB, I, CSI, CSIIndex);
1397 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001398 }
1399
1400 // Default behavior for non-CR saves.
1401 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1402 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00001403 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001404 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00001405 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00001406 }
1407
1408 // Insert in reverse order.
1409 if (AtStart)
1410 I = MBB.begin();
1411 else {
1412 I = BeforeI;
1413 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00001414 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001415 }
1416
1417 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00001418 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00001419 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001420 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001421 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001422 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001423
1424 return true;
1425}