blob: 3710f92612ba90910e96ca0b5481bfb69556a310 [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"
Eric Christopherd104c312014-06-12 20:54:11 +000018#include "PPCSubtarget.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000024#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000026#include "llvm/Target/TargetOptions.h"
27
28using namespace llvm;
29
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000030/// VRRegNo - Map from a numbered VR register to its enum value.
31///
Craig Topperca658c22012-03-11 07:16:55 +000032static const uint16_t VRRegNo[] = {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000033 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
34 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
35 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
36 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
37};
38
Eric Christopherd104c312014-06-12 20:54:11 +000039PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
40 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
41 (STI.hasQPX() || STI.isBGQ()) ? 32 : 16, 0),
42 Subtarget(STI) {}
43
Eric Christopherd104c312014-06-12 20:54:11 +000044// With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
45const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
46 unsigned &NumEntries) const {
47 if (Subtarget.isDarwinABI()) {
48 NumEntries = 1;
49 if (Subtarget.isPPC64()) {
50 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
51 return &darwin64Offsets;
52 } else {
53 static const SpillSlot darwinOffsets = {PPC::R31, -4};
54 return &darwinOffsets;
55 }
56 }
57
58 // Early exit if not using the SVR4 ABI.
59 if (!Subtarget.isSVR4ABI()) {
60 NumEntries = 0;
61 return nullptr;
62 }
63
64 // Note that the offsets here overlap, but this is fixed up in
65 // processFunctionBeforeFrameFinalized.
66
67 static const SpillSlot Offsets[] = {
68 // Floating-point register save area offsets.
69 {PPC::F31, -8},
70 {PPC::F30, -16},
71 {PPC::F29, -24},
72 {PPC::F28, -32},
73 {PPC::F27, -40},
74 {PPC::F26, -48},
75 {PPC::F25, -56},
76 {PPC::F24, -64},
77 {PPC::F23, -72},
78 {PPC::F22, -80},
79 {PPC::F21, -88},
80 {PPC::F20, -96},
81 {PPC::F19, -104},
82 {PPC::F18, -112},
83 {PPC::F17, -120},
84 {PPC::F16, -128},
85 {PPC::F15, -136},
86 {PPC::F14, -144},
87
88 // General register save area offsets.
89 {PPC::R31, -4},
90 {PPC::R30, -8},
91 {PPC::R29, -12},
92 {PPC::R28, -16},
93 {PPC::R27, -20},
94 {PPC::R26, -24},
95 {PPC::R25, -28},
96 {PPC::R24, -32},
97 {PPC::R23, -36},
98 {PPC::R22, -40},
99 {PPC::R21, -44},
100 {PPC::R20, -48},
101 {PPC::R19, -52},
102 {PPC::R18, -56},
103 {PPC::R17, -60},
104 {PPC::R16, -64},
105 {PPC::R15, -68},
106 {PPC::R14, -72},
107
108 // CR save area offset. We map each of the nonvolatile CR fields
109 // to the slot for CR2, which is the first of the nonvolatile CR
110 // fields to be assigned, so that we only allocate one save slot.
111 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
112 {PPC::CR2, -4},
113
114 // VRSAVE save area offset.
115 {PPC::VRSAVE, -4},
116
117 // Vector register save area
118 {PPC::V31, -16},
119 {PPC::V30, -32},
120 {PPC::V29, -48},
121 {PPC::V28, -64},
122 {PPC::V27, -80},
123 {PPC::V26, -96},
124 {PPC::V25, -112},
125 {PPC::V24, -128},
126 {PPC::V23, -144},
127 {PPC::V22, -160},
128 {PPC::V21, -176},
129 {PPC::V20, -192}};
130
131 static const SpillSlot Offsets64[] = {
132 // Floating-point register save area offsets.
133 {PPC::F31, -8},
134 {PPC::F30, -16},
135 {PPC::F29, -24},
136 {PPC::F28, -32},
137 {PPC::F27, -40},
138 {PPC::F26, -48},
139 {PPC::F25, -56},
140 {PPC::F24, -64},
141 {PPC::F23, -72},
142 {PPC::F22, -80},
143 {PPC::F21, -88},
144 {PPC::F20, -96},
145 {PPC::F19, -104},
146 {PPC::F18, -112},
147 {PPC::F17, -120},
148 {PPC::F16, -128},
149 {PPC::F15, -136},
150 {PPC::F14, -144},
151
152 // General register save area offsets.
153 {PPC::X31, -8},
154 {PPC::X30, -16},
155 {PPC::X29, -24},
156 {PPC::X28, -32},
157 {PPC::X27, -40},
158 {PPC::X26, -48},
159 {PPC::X25, -56},
160 {PPC::X24, -64},
161 {PPC::X23, -72},
162 {PPC::X22, -80},
163 {PPC::X21, -88},
164 {PPC::X20, -96},
165 {PPC::X19, -104},
166 {PPC::X18, -112},
167 {PPC::X17, -120},
168 {PPC::X16, -128},
169 {PPC::X15, -136},
170 {PPC::X14, -144},
171
172 // VRSAVE save area offset.
173 {PPC::VRSAVE, -4},
174
175 // Vector register save area
176 {PPC::V31, -16},
177 {PPC::V30, -32},
178 {PPC::V29, -48},
179 {PPC::V28, -64},
180 {PPC::V27, -80},
181 {PPC::V26, -96},
182 {PPC::V25, -112},
183 {PPC::V24, -128},
184 {PPC::V23, -144},
185 {PPC::V22, -160},
186 {PPC::V21, -176},
187 {PPC::V20, -192}};
188
189 if (Subtarget.isPPC64()) {
190 NumEntries = array_lengthof(Offsets64);
191
192 return Offsets64;
193 } else {
194 NumEntries = array_lengthof(Offsets);
195
196 return Offsets;
197 }
198}
199
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000200/// RemoveVRSaveCode - We have found that this function does not need any code
201/// to manipulate the VRSAVE register, even though it uses vector registers.
202/// This can happen when the only registers used are known to be live in or out
203/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidt38d94582012-10-10 20:54:15 +0000204/// FIXME: The removal of the code results in a compile failure at -O0 when the
205/// function contains a function call, as the GPR containing original VRSAVE
206/// contents is spilled and reloaded around the call. Without the prolog code,
207/// the spill instruction refers to an undefined register. This code needs
208/// to account for all uses of that GPR.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000209static void RemoveVRSaveCode(MachineInstr *MI) {
210 MachineBasicBlock *Entry = MI->getParent();
211 MachineFunction *MF = Entry->getParent();
212
213 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
214 MachineBasicBlock::iterator MBBI = MI;
215 ++MBBI;
216 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
217 MBBI->eraseFromParent();
218
219 bool RemovedAllMTVRSAVEs = true;
220 // See if we can find and remove the MTVRSAVE instruction from all of the
221 // epilog blocks.
222 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
223 // If last instruction is a return instruction, add an epilogue
Evan Cheng7f8e5632011-12-07 07:15:52 +0000224 if (!I->empty() && I->back().isReturn()) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000225 bool FoundIt = false;
226 for (MBBI = I->end(); MBBI != I->begin(); ) {
227 --MBBI;
228 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
229 MBBI->eraseFromParent(); // remove it.
230 FoundIt = true;
231 break;
232 }
233 }
234 RemovedAllMTVRSAVEs &= FoundIt;
235 }
236 }
237
238 // If we found and removed all MTVRSAVE instructions, remove the read of
239 // VRSAVE as well.
240 if (RemovedAllMTVRSAVEs) {
241 MBBI = MI;
242 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
243 --MBBI;
244 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
245 MBBI->eraseFromParent();
246 }
247
248 // Finally, nuke the UPDATE_VRSAVE.
249 MI->eraseFromParent();
250}
251
252// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
253// instruction selector. Based on the vector registers that have been used,
254// transform this into the appropriate ORI instruction.
255static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
256 MachineFunction *MF = MI->getParent()->getParent();
Hal Finkelfeea6532013-03-26 20:08:20 +0000257 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000258 DebugLoc dl = MI->getDebugLoc();
259
260 unsigned UsedRegMask = 0;
261 for (unsigned i = 0; i != 32; ++i)
262 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
263 UsedRegMask |= 1 << (31-i);
264
265 // Live in and live out values already must be in the mask, so don't bother
266 // marking them.
267 for (MachineRegisterInfo::livein_iterator
268 I = MF->getRegInfo().livein_begin(),
269 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelfeea6532013-03-26 20:08:20 +0000270 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000271 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
272 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
273 }
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000274
275 // Live out registers appear as use operands on return instructions.
276 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
277 UsedRegMask != 0 && BI != BE; ++BI) {
278 const MachineBasicBlock &MBB = *BI;
279 if (MBB.empty() || !MBB.back().isReturn())
280 continue;
281 const MachineInstr &Ret = MBB.back();
282 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
283 const MachineOperand &MO = Ret.getOperand(I);
284 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
285 continue;
Hal Finkelfeea6532013-03-26 20:08:20 +0000286 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000287 UsedRegMask &= ~(1 << (31-RegNo));
288 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000289 }
290
291 // If no registers are used, turn this into a copy.
292 if (UsedRegMask == 0) {
293 // Remove all VRSAVE code.
294 RemoveVRSaveCode(MI);
295 return;
296 }
297
298 unsigned SrcReg = MI->getOperand(1).getReg();
299 unsigned DstReg = MI->getOperand(0).getReg();
300
301 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
302 if (DstReg != SrcReg)
303 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
304 .addReg(SrcReg)
305 .addImm(UsedRegMask);
306 else
307 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
308 .addReg(SrcReg, RegState::Kill)
309 .addImm(UsedRegMask);
310 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
311 if (DstReg != SrcReg)
312 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
313 .addReg(SrcReg)
314 .addImm(UsedRegMask >> 16);
315 else
316 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
317 .addReg(SrcReg, RegState::Kill)
318 .addImm(UsedRegMask >> 16);
319 } else {
320 if (DstReg != SrcReg)
321 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
322 .addReg(SrcReg)
323 .addImm(UsedRegMask >> 16);
324 else
325 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
326 .addReg(SrcReg, RegState::Kill)
327 .addImm(UsedRegMask >> 16);
328
329 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
330 .addReg(DstReg, RegState::Kill)
331 .addImm(UsedRegMask & 0xFFFF);
332 }
333
334 // Remove the old UPDATE_VRSAVE instruction.
335 MI->eraseFromParent();
336}
337
Roman Divackyc9e23d92012-09-12 14:47:47 +0000338static bool spillsCR(const MachineFunction &MF) {
339 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
340 return FuncInfo->isCRSpilled();
341}
342
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000343static bool spillsVRSAVE(const MachineFunction &MF) {
344 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
345 return FuncInfo->isVRSAVESpilled();
346}
347
Hal Finkelbb420f12013-03-15 05:06:04 +0000348static bool hasSpills(const MachineFunction &MF) {
349 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
350 return FuncInfo->hasSpills();
351}
352
Hal Finkelfcc51d42013-03-17 04:43:44 +0000353static bool hasNonRISpills(const MachineFunction &MF) {
354 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
355 return FuncInfo->hasNonRISpills();
356}
357
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000358/// determineFrameLayout - Determine the size of the frame and maximum call
359/// frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000360unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
361 bool UpdateMF,
362 bool UseEstimate) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000363 MachineFrameInfo *MFI = MF.getFrameInfo();
364
365 // Get the number of bytes to allocate from the FrameInfo
Hal Finkelbb420f12013-03-15 05:06:04 +0000366 unsigned FrameSize =
367 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000368
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000369 // Get stack alignments. The frame must be aligned to the greatest of these:
370 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
371 unsigned MaxAlign = MFI->getMaxAlignment(); // algmt required by data in frame
Hal Finkela7c54e82013-07-17 00:45:52 +0000372 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
373
374 const PPCRegisterInfo *RegInfo =
375 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000376
377 // If we are a leaf function, and use up to 224 bytes of stack space,
378 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkel67369882013-04-15 02:07:05 +0000379 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000380 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
381 // stackless code if all local vars are reg-allocated.
Bill Wendling698e84f2012-12-30 10:32:01 +0000382 bool DisableRedZone = MF.getFunction()->getAttributes().
383 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000384 if (!DisableRedZone &&
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000385 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
386 !Subtarget.isSVR4ABI() || // allocated locals.
Eric Christopherd1737492014-04-29 00:16:40 +0000387 FrameSize == 0) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000388 FrameSize <= 224 && // Fits in red zone.
389 !MFI->hasVarSizedObjects() && // No dynamic alloca.
390 !MFI->adjustsStack() && // No calls.
Hal Finkela7c54e82013-07-17 00:45:52 +0000391 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000392 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000393 if (UpdateMF)
394 MFI->setStackSize(0);
395 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000396 }
397
398 // Get the maximum call frame size of all the calls.
399 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
400
Ulrich Weigandf316e1d2014-06-23 13:47:52 +0000401 // Maximum call frame needs to be at least big enough for linkage area.
402 unsigned minCallFrameSize = getLinkageSize(Subtarget.isPPC64(),
403 Subtarget.isDarwinABI());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000404 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
405
406 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
407 // that allocations will be aligned.
408 if (MFI->hasVarSizedObjects())
409 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
410
411 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000412 if (UpdateMF)
413 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000414
415 // Include call frame size in total.
416 FrameSize += maxCallFrameSize;
417
418 // Make sure the frame is aligned.
419 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
420
421 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000422 if (UpdateMF)
423 MFI->setStackSize(FrameSize);
424
425 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000426}
427
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000428// hasFP - Return true if the specified function actually has a dedicated frame
429// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000430bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000431 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000432 // FIXME: This is pretty much broken by design: hasFP() might be called really
433 // early, before the stack layout was calculated and thus hasFP() might return
434 // true or false here depending on the time of call.
435 return (MFI->getStackSize()) && needsFP(MF);
436}
437
438// needsFP - Return true if the specified function should have a dedicated frame
439// pointer register. This is true if the function has variable sized allocas or
440// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000441bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000442 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000443
444 // Naked functions have no stack frame pushed, so we don't have a frame
445 // pointer.
Eric Christopherd1737492014-04-29 00:16:40 +0000446 if (MF.getFunction()->getAttributes().hasAttribute(
447 AttributeSet::FunctionIndex, Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000448 return false;
449
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000450 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
451 MFI->hasVarSizedObjects() ||
452 (MF.getTarget().Options.GuaranteedTailCallOpt &&
453 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000454}
455
Hal Finkelaa03c032013-03-21 19:03:19 +0000456void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
457 bool is31 = needsFP(MF);
458 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
459 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
460
Hal Finkelf05d6c72013-07-17 23:50:51 +0000461 const PPCRegisterInfo *RegInfo =
462 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
463 bool HasBP = RegInfo->hasBasePointer(MF);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000464 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000465 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
466
Hal Finkelaa03c032013-03-21 19:03:19 +0000467 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
468 BI != BE; ++BI)
469 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
470 --MBBI;
471 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
472 MachineOperand &MO = MBBI->getOperand(I);
473 if (!MO.isReg())
474 continue;
475
476 switch (MO.getReg()) {
477 case PPC::FP:
478 MO.setReg(FPReg);
479 break;
480 case PPC::FP8:
481 MO.setReg(FP8Reg);
482 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000483 case PPC::BP:
484 MO.setReg(BPReg);
485 break;
486 case PPC::BP8:
487 MO.setReg(BP8Reg);
488 break;
489
Hal Finkelaa03c032013-03-21 19:03:19 +0000490 }
491 }
492 }
493}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000494
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000495void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000496 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
497 MachineBasicBlock::iterator MBBI = MBB.begin();
498 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000499 const PPCInstrInfo &TII =
500 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +0000501 const PPCRegisterInfo *RegInfo =
502 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000503
504 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000505 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000506 DebugLoc dl;
507 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000508 MF.getFunction()->needsUnwindTableEntry();
Hal Finkel3ee2af72014-07-18 23:29:49 +0000509 bool isPIC = MF.getTarget().getRelocationModel() == Reloc::PIC_;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000510
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000511 // Get processor type.
512 bool isPPC64 = Subtarget.isPPC64();
513 // Get the ABI.
514 bool isDarwinABI = Subtarget.isDarwinABI();
515 bool isSVR4ABI = Subtarget.isSVR4ABI();
516 assert((isDarwinABI || isSVR4ABI) &&
517 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
518
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000519 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
520 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000521 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000522 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
523 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
524 HandleVRSaveUpdate(MBBI, TII);
525 break;
526 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000527 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000528
529 // Move MBBI back to the beginning of the function.
530 MBBI = MBB.begin();
531
532 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000533 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000534 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000535 if (!isInt<32>(NegFrameSize))
536 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000537
Hal Finkelaa03c032013-03-21 19:03:19 +0000538 if (MFI->isFrameAddressTaken())
539 replaceFPWithRealFP(MF);
540
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000541 // Check if the link register (LR) must be saved.
542 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
543 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000544 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000545 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000546 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000547 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000548
Bill Schmidtf381afc2013-08-20 03:12:23 +0000549 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000550 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000551 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
552 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
553 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
554 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
555 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
556 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
557 : PPC::MFLR );
558 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
559 : PPC::STW );
560 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
561 : PPC::STWU );
562 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
563 : PPC::STWUX);
564 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
565 : PPC::LIS );
566 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
567 : PPC::ORI );
568 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
569 : PPC::OR );
570 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
571 : PPC::SUBFC);
572 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
573 : PPC::SUBFIC);
574
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000575 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
576 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
577 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
578 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
579 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
580 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
581
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000582 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000583
584 int FPOffset = 0;
585 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000586 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000587 MachineFrameInfo *FFI = MF.getFrameInfo();
588 int FPIndex = FI->getFramePointerSaveIndex();
589 assert(FPIndex && "No Frame Pointer Save Slot!");
590 FPOffset = FFI->getObjectOffset(FPIndex);
591 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000592 FPOffset =
593 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000594 }
595 }
596
Hal Finkela7c54e82013-07-17 00:45:52 +0000597 int BPOffset = 0;
598 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000599 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000600 MachineFrameInfo *FFI = MF.getFrameInfo();
601 int BPIndex = FI->getBasePointerSaveIndex();
602 assert(BPIndex && "No Base Pointer Save Slot!");
603 BPOffset = FFI->getObjectOffset(BPIndex);
604 } else {
605 BPOffset =
Hal Finkel3ee2af72014-07-18 23:29:49 +0000606 PPCFrameLowering::getBasePointerSaveOffset(isPPC64,
607 isDarwinABI,
608 isPIC);
Hal Finkela7c54e82013-07-17 00:45:52 +0000609 }
610 }
611
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000612 // Get stack alignments.
613 unsigned MaxAlign = MFI->getMaxAlignment();
614 if (HasBP && MaxAlign > 1)
615 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
616 "Invalid alignment!");
617
618 // Frames of 32KB & larger require special handling because they cannot be
619 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
620 bool isLargeFrame = !isInt<16>(NegFrameSize);
621
Bill Schmidtf381afc2013-08-20 03:12:23 +0000622 if (MustSaveLR)
623 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000624
Bill Schmidtf381afc2013-08-20 03:12:23 +0000625 assert((isPPC64 || MustSaveCRs.empty()) &&
626 "Prologue CR saving supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000627
Bill Schmidtf381afc2013-08-20 03:12:23 +0000628 if (!MustSaveCRs.empty()) { // will only occur for PPC64
629 MachineInstrBuilder MIB =
630 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), TempReg);
631 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
632 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000633 }
634
Bill Schmidtf381afc2013-08-20 03:12:23 +0000635 if (HasFP)
636 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
637 BuildMI(MBB, MBBI, dl, StoreInst)
638 .addReg(FPReg)
639 .addImm(FPOffset)
640 .addReg(SPReg);
641
642 if (HasBP)
643 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
644 BuildMI(MBB, MBBI, dl, StoreInst)
645 .addReg(BPReg)
646 .addImm(BPOffset)
647 .addReg(SPReg);
648
649 if (MustSaveLR)
650 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
651 BuildMI(MBB, MBBI, dl, StoreInst)
652 .addReg(ScratchReg)
653 .addImm(LROffset)
654 .addReg(SPReg);
655
656 if (!MustSaveCRs.empty()) // will only occur for PPC64
657 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
658 .addReg(TempReg, getKillRegState(true))
659 .addImm(8)
660 .addReg(SPReg);
661
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000662 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000663 if (!FrameSize) return;
664
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000665 // Adjust stack pointer: r1 += NegFrameSize.
666 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000667
Bill Schmidtf381afc2013-08-20 03:12:23 +0000668 if (HasBP) {
669 // Save a copy of r1 as the base pointer.
670 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
671 .addReg(SPReg)
672 .addReg(SPReg);
673 }
674
675 if (HasBP && MaxAlign > 1) {
676 if (isPPC64)
677 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
678 .addReg(SPReg)
679 .addImm(0)
680 .addImm(64 - Log2_32(MaxAlign));
681 else // PPC32...
682 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
683 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000684 .addImm(0)
685 .addImm(32 - Log2_32(MaxAlign))
686 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000687 if (!isLargeFrame) {
688 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
689 .addReg(ScratchReg, RegState::Kill)
690 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000691 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000692 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000693 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000694 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
695 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000696 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000697 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
698 .addReg(ScratchReg, RegState::Kill)
699 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000700 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000701 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
702 .addReg(SPReg, RegState::Kill)
703 .addReg(SPReg)
704 .addReg(ScratchReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000705
Bill Schmidtf381afc2013-08-20 03:12:23 +0000706 } else if (!isLargeFrame) {
707 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
708 .addReg(SPReg)
709 .addImm(NegFrameSize)
710 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000711
Bill Schmidtf381afc2013-08-20 03:12:23 +0000712 } else {
713 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
714 .addImm(NegFrameSize >> 16);
715 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
716 .addReg(ScratchReg, RegState::Kill)
717 .addImm(NegFrameSize & 0xFFFF);
718 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
719 .addReg(SPReg, RegState::Kill)
720 .addReg(SPReg)
721 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000722 }
723
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000724 // Add the "machine moves" for the instructions we generated above, but in
725 // reverse order.
726 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000727 // Show update of SP.
Rafael Espindola6e8c0d92013-05-16 03:34:58 +0000728 assert(NegFrameSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000729 unsigned CFIIndex = MMI.addFrameInst(
730 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
Eric Christopher612bb692014-04-29 00:16:46 +0000731 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
732 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000733
734 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000735 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000736 CFIIndex = MMI.addFrameInst(
737 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000738 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000739 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000740 }
741
Hal Finkela7c54e82013-07-17 00:45:52 +0000742 if (HasBP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000743 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000744 CFIIndex = MMI.addFrameInst(
745 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000746 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000747 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000748 }
749
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000750 if (MustSaveLR) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000751 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000752 CFIIndex = MMI.addFrameInst(
753 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000754 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000755 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000756 }
757 }
758
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000759 // If there is a frame pointer, copy R1 into R31
760 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000761 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
762 .addReg(SPReg)
763 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000764
765 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000766 // Mark effective beginning of when frame pointer is ready.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000767 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000768 unsigned CFIIndex = MMI.addFrameInst(
769 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
770
Eric Christopher612bb692014-04-29 00:16:46 +0000771 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000772 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000773 }
774 }
775
776 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000777 // Add callee saved registers to move list.
778 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
779 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000780 unsigned Reg = CSI[I].getReg();
781 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000782
783 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
784 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +0000785 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +0000786 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000787
Roman Divackyc9e23d92012-09-12 14:47:47 +0000788 // For SVR4, don't emit a move for the CR spill slot if we haven't
789 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000790 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
791 && MustSaveCRs.empty())
792 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000793
794 // For 64-bit SVR4 when we have spilled CRs, the spill location
795 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000796 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000797 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
798 nullptr, MRI->getDwarfRegNum(PPC::CR2, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +0000799 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000800 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000801 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000802 }
803
804 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000805 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
806 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +0000807 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000808 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000809 }
810 }
811}
812
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000813void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000814 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000815 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
816 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000817 const PPCInstrInfo &TII =
818 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +0000819 const PPCRegisterInfo *RegInfo =
820 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000821
822 unsigned RetOpcode = MBBI->getOpcode();
823 DebugLoc dl;
824
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000825 assert((RetOpcode == PPC::BLR ||
826 RetOpcode == PPC::TCRETURNri ||
827 RetOpcode == PPC::TCRETURNdi ||
828 RetOpcode == PPC::TCRETURNai ||
829 RetOpcode == PPC::TCRETURNri8 ||
830 RetOpcode == PPC::TCRETURNdi8 ||
831 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000832 "Can only insert epilog into returning blocks");
833
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000834 // Get alignment info so we know how to restore the SP.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000835 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000836
837 // Get the number of bytes allocated from the FrameInfo.
838 int FrameSize = MFI->getStackSize();
839
840 // Get processor type.
841 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000842 // Get the ABI.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000843 bool isDarwinABI = Subtarget.isDarwinABI();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000844 bool isSVR4ABI = Subtarget.isSVR4ABI();
Hal Finkel3ee2af72014-07-18 23:29:49 +0000845 bool isPIC = MF.getTarget().getRelocationModel() == Reloc::PIC_;
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000846
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000847 // Check if the link register (LR) has been saved.
848 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
849 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000850 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000851 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000852 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000853 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000854
Bill Schmidtf381afc2013-08-20 03:12:23 +0000855 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000856 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000857 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
858 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
859 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
860 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
861 : PPC::MTLR );
862 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
863 : PPC::LWZ );
864 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
865 : PPC::LIS );
866 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
867 : PPC::ORI );
868 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
869 : PPC::ADDI );
870 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
871 : PPC::ADD4 );
872
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000873 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000874
875 int FPOffset = 0;
876 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000877 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000878 MachineFrameInfo *FFI = MF.getFrameInfo();
879 int FPIndex = FI->getFramePointerSaveIndex();
880 assert(FPIndex && "No Frame Pointer Save Slot!");
881 FPOffset = FFI->getObjectOffset(FPIndex);
882 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000883 FPOffset =
884 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000885 }
886 }
887
Hal Finkela7c54e82013-07-17 00:45:52 +0000888 int BPOffset = 0;
889 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000890 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000891 MachineFrameInfo *FFI = MF.getFrameInfo();
892 int BPIndex = FI->getBasePointerSaveIndex();
893 assert(BPIndex && "No Base Pointer Save Slot!");
894 BPOffset = FFI->getObjectOffset(BPIndex);
895 } else {
896 BPOffset =
Hal Finkel3ee2af72014-07-18 23:29:49 +0000897 PPCFrameLowering::getBasePointerSaveOffset(isPPC64,
898 isDarwinABI,
899 isPIC);
Hal Finkela7c54e82013-07-17 00:45:52 +0000900 }
901 }
902
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000903 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
904 RetOpcode == PPC::TCRETURNdi ||
905 RetOpcode == PPC::TCRETURNai ||
906 RetOpcode == PPC::TCRETURNri8 ||
907 RetOpcode == PPC::TCRETURNdi8 ||
908 RetOpcode == PPC::TCRETURNai8;
909
910 if (UsesTCRet) {
911 int MaxTCRetDelta = FI->getTailCallSPDelta();
912 MachineOperand &StackAdjust = MBBI->getOperand(1);
913 assert(StackAdjust.isImm() && "Expecting immediate value.");
914 // Adjust stack pointer.
915 int StackAdj = StackAdjust.getImm();
916 int Delta = StackAdj - MaxTCRetDelta;
917 assert((Delta >= 0) && "Delta must be positive");
918 if (MaxTCRetDelta>0)
919 FrameSize += (StackAdj +Delta);
920 else
921 FrameSize += StackAdj;
922 }
923
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000924 // Frames of 32KB & larger require special handling because they cannot be
925 // indexed into with a simple LD/LWZ immediate offset operand.
926 bool isLargeFrame = !isInt<16>(FrameSize);
927
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000928 if (FrameSize) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000929 // In the prologue, the loaded (or persistent) stack pointer value is offset
930 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000931
932 // If this function contained a fastcc call and GuaranteedTailCallOpt is
933 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
934 // call which invalidates the stack pointer value in SP(0). So we use the
935 // value of R31 in this case.
936 if (FI->hasFastCall()) {
937 assert(HasFP && "Expecting a valid frame pointer.");
938 if (!isLargeFrame) {
939 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
940 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000941 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000942 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
943 .addImm(FrameSize >> 16);
944 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
945 .addReg(ScratchReg, RegState::Kill)
946 .addImm(FrameSize & 0xFFFF);
947 BuildMI(MBB, MBBI, dl, AddInst)
948 .addReg(SPReg)
949 .addReg(FPReg)
950 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000951 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000952 } else if (!isLargeFrame && !HasBP && !MFI->hasVarSizedObjects()) {
953 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
954 .addReg(SPReg)
955 .addImm(FrameSize);
956 } else {
957 BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
958 .addImm(0)
959 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000960 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000961
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000962 }
963
Bill Schmidtf381afc2013-08-20 03:12:23 +0000964 if (MustSaveLR)
965 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
966 .addImm(LROffset)
967 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000968
Bill Schmidtf381afc2013-08-20 03:12:23 +0000969 assert((isPPC64 || MustSaveCRs.empty()) &&
970 "Epilogue CR restoring supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000971
Bill Schmidtf381afc2013-08-20 03:12:23 +0000972 if (!MustSaveCRs.empty()) // will only occur for PPC64
973 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
974 .addImm(8)
975 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000976
Bill Schmidtf381afc2013-08-20 03:12:23 +0000977 if (HasFP)
978 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
979 .addImm(FPOffset)
980 .addReg(SPReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000981
Bill Schmidtf381afc2013-08-20 03:12:23 +0000982 if (HasBP)
983 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
984 .addImm(BPOffset)
985 .addReg(SPReg);
Hal Finkel67369882013-04-15 02:07:05 +0000986
Bill Schmidtf381afc2013-08-20 03:12:23 +0000987 if (!MustSaveCRs.empty()) // will only occur for PPC64
988 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
989 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
990 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000991
Bill Schmidtf381afc2013-08-20 03:12:23 +0000992 if (MustSaveLR)
993 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000994
995 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
996 // call optimization
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000997 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000998 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
999 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1000 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001001
1002 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001003 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1004 .addReg(SPReg).addImm(CallerAllocatedAmt);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001005 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001006 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001007 .addImm(CallerAllocatedAmt >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001008 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1009 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001010 .addImm(CallerAllocatedAmt & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001011 BuildMI(MBB, MBBI, dl, AddInst)
1012 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001013 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001014 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001015 }
1016 } else if (RetOpcode == PPC::TCRETURNdi) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001017 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001018 MachineOperand &JumpTarget = MBBI->getOperand(0);
1019 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1020 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1021 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001022 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001023 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1024 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1025 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001026 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001027 MachineOperand &JumpTarget = MBBI->getOperand(0);
1028 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1029 } else if (RetOpcode == PPC::TCRETURNdi8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001030 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001031 MachineOperand &JumpTarget = MBBI->getOperand(0);
1032 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1033 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1034 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001035 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001036 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1037 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1038 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001039 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001040 MachineOperand &JumpTarget = MBBI->getOperand(0);
1041 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1042 }
1043}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001044
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001045/// MustSaveLR - Return true if this function requires that we save the LR
1046/// register onto the stack in the prolog and restore it in the epilog of the
1047/// function.
1048static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
1049 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
1050
1051 // We need a save/restore of LR if there is any def of LR (which is
1052 // defined by calls, including the PIC setup sequence), or if there is
1053 // some use of the LR stack slot (e.g. for builtin_return_address).
1054 // (LR comes in 32 and 64 bit versions.)
1055 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
1056 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
1057}
1058
1059void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001060PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001061 RegScavenger *) const {
Hal Finkela7c54e82013-07-17 00:45:52 +00001062 const PPCRegisterInfo *RegInfo =
1063 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001064
1065 // Save and clear the LR state.
1066 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1067 unsigned LR = RegInfo->getRARegister();
1068 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001069 MachineRegisterInfo &MRI = MF.getRegInfo();
1070 MRI.setPhysRegUnused(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001071
1072 // Save R31 if necessary
1073 int FPSI = FI->getFramePointerSaveIndex();
1074 bool isPPC64 = Subtarget.isPPC64();
1075 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel3ee2af72014-07-18 23:29:49 +00001076 bool isPIC = MF.getTarget().getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001077 MachineFrameInfo *MFI = MF.getFrameInfo();
1078
1079 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001080 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001081 // Find out what the fix offset of the frame pointer save area.
1082 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
1083 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001084 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001085 // Save the result.
1086 FI->setFramePointerSaveIndex(FPSI);
1087 }
1088
Hal Finkela7c54e82013-07-17 00:45:52 +00001089 int BPSI = FI->getBasePointerSaveIndex();
1090 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00001091 int BPOffset = getBasePointerSaveOffset(isPPC64, isDarwinABI, isPIC);
Hal Finkela7c54e82013-07-17 00:45:52 +00001092 // Allocate the frame index for the base pointer save area.
1093 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1094 // Save the result.
1095 FI->setBasePointerSaveIndex(BPSI);
1096 }
1097
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001098 // Reserve stack space to move the linkage area to in case of a tail call.
1099 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001100 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1101 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001102 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001103 }
1104
Eric Christopherd1737492014-04-29 00:16:40 +00001105 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001106 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001107 if (!isPPC64 && !isDarwinABI &&
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001108 (MRI.isPhysRegUsed(PPC::CR2) ||
1109 MRI.isPhysRegUsed(PPC::CR3) ||
1110 MRI.isPhysRegUsed(PPC::CR4))) {
1111 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1112 FI->setCRSpillFrameIndex(FrameIdx);
1113 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001114}
1115
Hal Finkel5a765fd2013-03-14 20:33:40 +00001116void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001117 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001118 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001119 if (!Subtarget.isSVR4ABI()) {
1120 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001121 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001122 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001123
1124 // Get callee saved register information.
1125 MachineFrameInfo *FFI = MF.getFrameInfo();
1126 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1127
1128 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001129 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001130 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001131 return;
1132 }
1133
1134 unsigned MinGPR = PPC::R31;
1135 unsigned MinG8R = PPC::X31;
1136 unsigned MinFPR = PPC::F31;
1137 unsigned MinVR = PPC::V31;
1138
1139 bool HasGPSaveArea = false;
1140 bool HasG8SaveArea = false;
1141 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001142 bool HasVRSAVESaveArea = false;
1143 bool HasVRSaveArea = false;
1144
1145 SmallVector<CalleeSavedInfo, 18> GPRegs;
1146 SmallVector<CalleeSavedInfo, 18> G8Regs;
1147 SmallVector<CalleeSavedInfo, 18> FPRegs;
1148 SmallVector<CalleeSavedInfo, 18> VRegs;
1149
1150 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1151 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001152 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001153 HasGPSaveArea = true;
1154
1155 GPRegs.push_back(CSI[i]);
1156
1157 if (Reg < MinGPR) {
1158 MinGPR = Reg;
1159 }
Craig Topperabadc662012-04-20 06:31:50 +00001160 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001161 HasG8SaveArea = true;
1162
1163 G8Regs.push_back(CSI[i]);
1164
1165 if (Reg < MinG8R) {
1166 MinG8R = Reg;
1167 }
Craig Topperabadc662012-04-20 06:31:50 +00001168 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001169 HasFPSaveArea = true;
1170
1171 FPRegs.push_back(CSI[i]);
1172
1173 if (Reg < MinFPR) {
1174 MinFPR = Reg;
1175 }
Craig Topperabadc662012-04-20 06:31:50 +00001176 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1177 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001178 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001179 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001180 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001181 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001182 HasVRSaveArea = true;
1183
1184 VRegs.push_back(CSI[i]);
1185
1186 if (Reg < MinVR) {
1187 MinVR = Reg;
1188 }
1189 } else {
1190 llvm_unreachable("Unknown RegisterClass!");
1191 }
1192 }
1193
1194 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Hal Finkelfeea6532013-03-26 20:08:20 +00001195 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001196
1197 int64_t LowerBound = 0;
1198
1199 // Take into account stack space reserved for tail calls.
1200 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001201 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1202 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001203 LowerBound = TCSPDelta;
1204 }
1205
1206 // The Floating-point register save area is right below the back chain word
1207 // of the previous stack frame.
1208 if (HasFPSaveArea) {
1209 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1210 int FI = FPRegs[i].getFrameIdx();
1211
1212 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1213 }
1214
Hal Finkelfeea6532013-03-26 20:08:20 +00001215 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001216 }
1217
1218 // Check whether the frame pointer register is allocated. If so, make sure it
1219 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001220 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001221 HasGPSaveArea = true;
1222
1223 int FI = PFI->getFramePointerSaveIndex();
1224 assert(FI && "No Frame Pointer Save Slot!");
1225
1226 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1227 }
1228
Hal Finkela7c54e82013-07-17 00:45:52 +00001229 const PPCRegisterInfo *RegInfo =
1230 static_cast<const PPCRegisterInfo*>(MF.getTarget().getRegisterInfo());
1231 if (RegInfo->hasBasePointer(MF)) {
1232 HasGPSaveArea = true;
1233
1234 int FI = PFI->getBasePointerSaveIndex();
1235 assert(FI && "No Base Pointer Save Slot!");
1236
1237 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1238 }
1239
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001240 // General register save area starts right below the Floating-point
1241 // register save area.
1242 if (HasGPSaveArea || HasG8SaveArea) {
1243 // Move general register save area spill slots down, taking into account
1244 // the size of the Floating-point register save area.
1245 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1246 int FI = GPRegs[i].getFrameIdx();
1247
1248 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1249 }
1250
1251 // Move general register save area spill slots down, taking into account
1252 // the size of the Floating-point register save area.
1253 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1254 int FI = G8Regs[i].getFrameIdx();
1255
1256 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1257 }
1258
1259 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001260 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1261 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001262
1263 if (Subtarget.isPPC64()) {
1264 LowerBound -= (31 - MinReg + 1) * 8;
1265 } else {
1266 LowerBound -= (31 - MinReg + 1) * 4;
1267 }
1268 }
1269
Roman Divackyc9e23d92012-09-12 14:47:47 +00001270 // For 32-bit only, the CR save area is below the general register
1271 // save area. For 64-bit SVR4, the CR save area is addressed relative
1272 // to the stack pointer and hence does not need an adjustment here.
1273 // Only CR2 (the first nonvolatile spilled) has an associated frame
1274 // index so that we have a single uniform save area.
1275 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001276 // Adjust the frame index of the CR spill slot.
1277 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1278 unsigned Reg = CSI[i].getReg();
1279
Roman Divackyc9e23d92012-09-12 14:47:47 +00001280 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001281 // Leave Darwin logic as-is.
1282 || (!Subtarget.isSVR4ABI() &&
1283 (PPC::CRBITRCRegClass.contains(Reg) ||
1284 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001285 int FI = CSI[i].getFrameIdx();
1286
1287 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1288 }
1289 }
1290
1291 LowerBound -= 4; // The CR save area is always 4 bytes long.
1292 }
1293
1294 if (HasVRSAVESaveArea) {
1295 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1296 // which have the VRSAVE register class?
1297 // Adjust the frame index of the VRSAVE spill slot.
1298 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1299 unsigned Reg = CSI[i].getReg();
1300
Craig Topperabadc662012-04-20 06:31:50 +00001301 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001302 int FI = CSI[i].getFrameIdx();
1303
1304 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1305 }
1306 }
1307
1308 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1309 }
1310
1311 if (HasVRSaveArea) {
1312 // Insert alignment padding, we need 16-byte alignment.
1313 LowerBound = (LowerBound - 15) & ~(15);
1314
1315 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1316 int FI = VRegs[i].getFrameIdx();
1317
1318 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1319 }
1320 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001321
1322 addScavengingSpillSlot(MF, RS);
1323}
1324
1325void
1326PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1327 RegScavenger *RS) const {
1328 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1329 // a large stack, which will require scavenging a register to materialize a
1330 // large offset.
1331
1332 // We need to have a scavenger spill slot for spills if the frame size is
1333 // large. In case there is no free register for large-offset addressing,
1334 // this slot is used for the necessary emergency spill. Also, we need the
1335 // slot for dynamic stack allocations.
1336
1337 // The scavenger might be invoked if the frame offset does not fit into
1338 // the 16-bit immediate. We don't know the complete frame size here
1339 // because we've not yet computed callee-saved register spills or the
1340 // needed alignment padding.
1341 unsigned StackSize = determineFrameLayout(MF, false, true);
1342 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001343 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1344 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001345 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1346 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1347 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkel9e331c22013-03-22 23:32:27 +00001348 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkelbb420f12013-03-15 05:06:04 +00001349 RC->getAlignment(),
1350 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001351
Hal Finkel18607632013-07-18 04:28:21 +00001352 // Might we have over-aligned allocas?
1353 bool HasAlVars = MFI->hasVarSizedObjects() &&
1354 MFI->getMaxAlignment() > getStackAlignment();
1355
Hal Finkel0dfbb052013-03-26 18:57:22 +00001356 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001357 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Hal Finkel0dfbb052013-03-26 18:57:22 +00001358 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1359 RC->getAlignment(),
1360 false));
1361
Hal Finkelbb420f12013-03-15 05:06:04 +00001362 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001363}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001364
Eric Christopherd1737492014-04-29 00:16:40 +00001365bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001366PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001367 MachineBasicBlock::iterator MI,
1368 const std::vector<CalleeSavedInfo> &CSI,
1369 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001370
1371 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1372 // Return false otherwise to maintain pre-existing behavior.
1373 if (!Subtarget.isSVR4ABI())
1374 return false;
1375
1376 MachineFunction *MF = MBB.getParent();
1377 const PPCInstrInfo &TII =
1378 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1379 DebugLoc DL;
1380 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001381 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001382
Roman Divackyc9e23d92012-09-12 14:47:47 +00001383 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1384 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001385 // Only Darwin actually uses the VRSAVE register, but it can still appear
1386 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1387 // Darwin, ignore it.
1388 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1389 continue;
1390
Roman Divackyc9e23d92012-09-12 14:47:47 +00001391 // CR2 through CR4 are the nonvolatile CR fields.
1392 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1393
Roman Divackyc9e23d92012-09-12 14:47:47 +00001394 // Add the callee-saved register as live-in; it's killed at the spill.
1395 MBB.addLiveIn(Reg);
1396
Hal Finkel2f293912013-04-13 23:06:15 +00001397 if (CRSpilled && IsCRField) {
1398 CRMIB.addReg(Reg, RegState::ImplicitKill);
1399 continue;
1400 }
1401
Roman Divackyc9e23d92012-09-12 14:47:47 +00001402 // Insert the spill to the stack frame.
1403 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001404 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001405 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001406 // The actual spill will happen at the start of the prologue.
1407 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001408 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001409 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001410 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001411
Eric Christopherd1737492014-04-29 00:16:40 +00001412 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1413 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1414 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001415 .addReg(Reg, RegState::ImplicitKill);
1416
Eric Christopherd1737492014-04-29 00:16:40 +00001417 MBB.insert(MI, CRMIB);
1418 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1419 .addReg(PPC::R12,
1420 getKillRegState(true)),
1421 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001422 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001423 } else {
1424 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1425 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001426 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001427 }
1428 }
1429 return true;
1430}
1431
1432static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001433restoreCRs(bool isPPC64, bool is31,
1434 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001435 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1436 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001437
1438 MachineFunction *MF = MBB.getParent();
1439 const PPCInstrInfo &TII =
1440 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1441 DebugLoc DL;
1442 unsigned RestoreOp, MoveReg;
1443
Hal Finkel67369882013-04-15 02:07:05 +00001444 if (isPPC64)
1445 // This is handled during epilogue generation.
1446 return;
1447 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001448 // 32-bit: FP-relative
1449 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00001450 PPC::R12),
1451 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00001452 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001453 MoveReg = PPC::R12;
1454 }
Eric Christopherd1737492014-04-29 00:16:40 +00001455
Roman Divackyc9e23d92012-09-12 14:47:47 +00001456 if (CR2Spilled)
1457 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00001458 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001459
1460 if (CR3Spilled)
1461 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00001462 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001463
1464 if (CR4Spilled)
1465 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00001466 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001467}
1468
Eli Bendersky8da87162013-02-21 20:05:00 +00001469void PPCFrameLowering::
1470eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1471 MachineBasicBlock::iterator I) const {
1472 const PPCInstrInfo &TII =
1473 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo());
1474 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1475 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1476 // Add (actually subtract) back the amount the callee popped on return.
1477 if (int CalleeAmt = I->getOperand(1).getImm()) {
1478 bool is64Bit = Subtarget.isPPC64();
1479 CalleeAmt *= -1;
1480 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1481 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1482 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1483 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1484 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1485 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1486 MachineInstr *MI = I;
1487 DebugLoc dl = MI->getDebugLoc();
1488
1489 if (isInt<16>(CalleeAmt)) {
1490 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1491 .addReg(StackReg, RegState::Kill)
1492 .addImm(CalleeAmt);
1493 } else {
1494 MachineBasicBlock::iterator MBBI = I;
1495 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1496 .addImm(CalleeAmt >> 16);
1497 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1498 .addReg(TmpReg, RegState::Kill)
1499 .addImm(CalleeAmt & 0xFFFF);
1500 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1501 .addReg(StackReg, RegState::Kill)
1502 .addReg(TmpReg);
1503 }
1504 }
1505 }
1506 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1507 MBB.erase(I);
1508}
1509
Eric Christopherd1737492014-04-29 00:16:40 +00001510bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001511PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001512 MachineBasicBlock::iterator MI,
1513 const std::vector<CalleeSavedInfo> &CSI,
1514 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001515
1516 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1517 // Return false otherwise to maintain pre-existing behavior.
1518 if (!Subtarget.isSVR4ABI())
1519 return false;
1520
1521 MachineFunction *MF = MBB.getParent();
1522 const PPCInstrInfo &TII =
1523 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo());
1524 bool CR2Spilled = false;
1525 bool CR3Spilled = false;
1526 bool CR4Spilled = false;
1527 unsigned CSIIndex = 0;
1528
1529 // Initialize insertion-point logic; we will be restoring in reverse
1530 // order of spill.
1531 MachineBasicBlock::iterator I = MI, BeforeI = I;
1532 bool AtStart = I == MBB.begin();
1533
1534 if (!AtStart)
1535 --BeforeI;
1536
1537 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1538 unsigned Reg = CSI[i].getReg();
1539
Hal Finkelac1a24b2013-06-28 22:29:56 +00001540 // Only Darwin actually uses the VRSAVE register, but it can still appear
1541 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1542 // Darwin, ignore it.
1543 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1544 continue;
1545
Roman Divackyc9e23d92012-09-12 14:47:47 +00001546 if (Reg == PPC::CR2) {
1547 CR2Spilled = true;
1548 // The spill slot is associated only with CR2, which is the
1549 // first nonvolatile spilled. Save it here.
1550 CSIIndex = i;
1551 continue;
1552 } else if (Reg == PPC::CR3) {
1553 CR3Spilled = true;
1554 continue;
1555 } else if (Reg == PPC::CR4) {
1556 CR4Spilled = true;
1557 continue;
1558 } else {
1559 // When we first encounter a non-CR register after seeing at
1560 // least one CR register, restore all spilled CRs together.
1561 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00001562 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00001563 bool is31 = needsFP(*MF);
1564 restoreCRs(Subtarget.isPPC64(), is31,
1565 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001566 MBB, I, CSI, CSIIndex);
1567 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001568 }
1569
1570 // Default behavior for non-CR saves.
1571 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1572 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00001573 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001574 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00001575 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00001576 }
1577
1578 // Insert in reverse order.
1579 if (AtStart)
1580 I = MBB.begin();
1581 else {
1582 I = BeforeI;
1583 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00001584 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001585 }
1586
1587 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00001588 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00001589 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001590 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001591 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001592 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001593
1594 return true;
1595}