blob: dc87a6ca366e45670ddfc692e656a28c8b9b6c60 [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();
Eric Christopherfc6de422014-08-05 02:39:49 +0000257 const TargetRegisterInfo *TRI = MF->getSubtarget().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
Eric Christopherfc6de422014-08-05 02:39:49 +0000374 const PPCRegisterInfo *RegInfo =
375 static_cast<const PPCRegisterInfo *>(MF.getSubtarget().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(),
Ulrich Weigand8658f172014-07-20 23:43:15 +0000403 Subtarget.isDarwinABI(),
404 Subtarget.isELFv2ABI());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000405 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
406
407 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
408 // that allocations will be aligned.
409 if (MFI->hasVarSizedObjects())
410 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
411
412 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000413 if (UpdateMF)
414 MFI->setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000415
416 // Include call frame size in total.
417 FrameSize += maxCallFrameSize;
418
419 // Make sure the frame is aligned.
420 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
421
422 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000423 if (UpdateMF)
424 MFI->setStackSize(FrameSize);
425
426 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000427}
428
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000429// hasFP - Return true if the specified function actually has a dedicated frame
430// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000431bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000432 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000433 // FIXME: This is pretty much broken by design: hasFP() might be called really
434 // early, before the stack layout was calculated and thus hasFP() might return
435 // true or false here depending on the time of call.
436 return (MFI->getStackSize()) && needsFP(MF);
437}
438
439// needsFP - Return true if the specified function should have a dedicated frame
440// pointer register. This is true if the function has variable sized allocas or
441// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000442bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000443 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000444
445 // Naked functions have no stack frame pushed, so we don't have a frame
446 // pointer.
Eric Christopherd1737492014-04-29 00:16:40 +0000447 if (MF.getFunction()->getAttributes().hasAttribute(
448 AttributeSet::FunctionIndex, Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000449 return false;
450
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000451 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
452 MFI->hasVarSizedObjects() ||
453 (MF.getTarget().Options.GuaranteedTailCallOpt &&
454 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000455}
456
Hal Finkelaa03c032013-03-21 19:03:19 +0000457void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
458 bool is31 = needsFP(MF);
459 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
460 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
461
Eric Christopherfc6de422014-08-05 02:39:49 +0000462 const PPCRegisterInfo *RegInfo =
463 static_cast<const PPCRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Hal Finkelf05d6c72013-07-17 23:50:51 +0000464 bool HasBP = RegInfo->hasBasePointer(MF);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000465 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000466 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
467
Hal Finkelaa03c032013-03-21 19:03:19 +0000468 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
469 BI != BE; ++BI)
470 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
471 --MBBI;
472 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
473 MachineOperand &MO = MBBI->getOperand(I);
474 if (!MO.isReg())
475 continue;
476
477 switch (MO.getReg()) {
478 case PPC::FP:
479 MO.setReg(FPReg);
480 break;
481 case PPC::FP8:
482 MO.setReg(FP8Reg);
483 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000484 case PPC::BP:
485 MO.setReg(BPReg);
486 break;
487 case PPC::BP8:
488 MO.setReg(BP8Reg);
489 break;
490
Hal Finkelaa03c032013-03-21 19:03:19 +0000491 }
492 }
493 }
494}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000495
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000496void PPCFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000497 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
498 MachineBasicBlock::iterator MBBI = MBB.begin();
499 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000500 const PPCInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000501 *static_cast<const PPCInstrInfo *>(MF.getSubtarget().getInstrInfo());
502 const PPCRegisterInfo *RegInfo =
503 static_cast<const PPCRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000504
505 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000506 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000507 DebugLoc dl;
508 bool needsFrameMoves = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000509 MF.getFunction()->needsUnwindTableEntry();
Hal Finkel3ee2af72014-07-18 23:29:49 +0000510 bool isPIC = MF.getTarget().getRelocationModel() == Reloc::PIC_;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000511
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000512 // Get processor type.
513 bool isPPC64 = Subtarget.isPPC64();
514 // Get the ABI.
515 bool isDarwinABI = Subtarget.isDarwinABI();
516 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000517 bool isELFv2ABI = Subtarget.isELFv2ABI();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000518 assert((isDarwinABI || isSVR4ABI) &&
519 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
520
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000521 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
522 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000523 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000524 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
525 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
526 HandleVRSaveUpdate(MBBI, TII);
527 break;
528 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000529 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000530
531 // Move MBBI back to the beginning of the function.
532 MBBI = MBB.begin();
533
534 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000535 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000536 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000537 if (!isInt<32>(NegFrameSize))
538 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000539
Hal Finkelaa03c032013-03-21 19:03:19 +0000540 if (MFI->isFrameAddressTaken())
541 replaceFPWithRealFP(MF);
542
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000543 // Check if the link register (LR) must be saved.
544 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
545 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000546 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000547 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000548 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000549 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000550
Bill Schmidtf381afc2013-08-20 03:12:23 +0000551 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000552 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000553 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
554 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
555 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
556 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
557 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
558 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
559 : PPC::MFLR );
560 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
561 : PPC::STW );
562 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
563 : PPC::STWU );
564 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
565 : PPC::STWUX);
566 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
567 : PPC::LIS );
568 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
569 : PPC::ORI );
570 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
571 : PPC::OR );
572 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
573 : PPC::SUBFC);
574 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
575 : PPC::SUBFIC);
576
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000577 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
578 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
579 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
580 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
581 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
582 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
583
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000584 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000585
586 int FPOffset = 0;
587 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000588 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000589 MachineFrameInfo *FFI = MF.getFrameInfo();
590 int FPIndex = FI->getFramePointerSaveIndex();
591 assert(FPIndex && "No Frame Pointer Save Slot!");
592 FPOffset = FFI->getObjectOffset(FPIndex);
593 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000594 FPOffset =
595 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000596 }
597 }
598
Hal Finkela7c54e82013-07-17 00:45:52 +0000599 int BPOffset = 0;
600 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000601 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000602 MachineFrameInfo *FFI = MF.getFrameInfo();
603 int BPIndex = FI->getBasePointerSaveIndex();
604 assert(BPIndex && "No Base Pointer Save Slot!");
605 BPOffset = FFI->getObjectOffset(BPIndex);
606 } else {
607 BPOffset =
Hal Finkel3ee2af72014-07-18 23:29:49 +0000608 PPCFrameLowering::getBasePointerSaveOffset(isPPC64,
609 isDarwinABI,
610 isPIC);
Hal Finkela7c54e82013-07-17 00:45:52 +0000611 }
612 }
613
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000614 // Get stack alignments.
615 unsigned MaxAlign = MFI->getMaxAlignment();
616 if (HasBP && MaxAlign > 1)
617 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
618 "Invalid alignment!");
619
620 // Frames of 32KB & larger require special handling because they cannot be
621 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
622 bool isLargeFrame = !isInt<16>(NegFrameSize);
623
Bill Schmidtf381afc2013-08-20 03:12:23 +0000624 if (MustSaveLR)
625 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000626
Bill Schmidtf381afc2013-08-20 03:12:23 +0000627 assert((isPPC64 || MustSaveCRs.empty()) &&
628 "Prologue CR saving supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000629
Bill Schmidtf381afc2013-08-20 03:12:23 +0000630 if (!MustSaveCRs.empty()) { // will only occur for PPC64
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000631 // FIXME: In the ELFv2 ABI, we are not required to save all CR fields.
632 // If only one or two CR fields are clobbered, it could be more
633 // efficient to use mfocrf to selectively save just those fields.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000634 MachineInstrBuilder MIB =
635 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), TempReg);
636 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
637 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000638 }
639
Bill Schmidtf381afc2013-08-20 03:12:23 +0000640 if (HasFP)
641 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
642 BuildMI(MBB, MBBI, dl, StoreInst)
643 .addReg(FPReg)
644 .addImm(FPOffset)
645 .addReg(SPReg);
646
647 if (HasBP)
648 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
649 BuildMI(MBB, MBBI, dl, StoreInst)
650 .addReg(BPReg)
651 .addImm(BPOffset)
652 .addReg(SPReg);
653
654 if (MustSaveLR)
655 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
656 BuildMI(MBB, MBBI, dl, StoreInst)
657 .addReg(ScratchReg)
658 .addImm(LROffset)
659 .addReg(SPReg);
660
661 if (!MustSaveCRs.empty()) // will only occur for PPC64
662 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
663 .addReg(TempReg, getKillRegState(true))
664 .addImm(8)
665 .addReg(SPReg);
666
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000667 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000668 if (!FrameSize) return;
669
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000670 // Adjust stack pointer: r1 += NegFrameSize.
671 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000672
Bill Schmidtf381afc2013-08-20 03:12:23 +0000673 if (HasBP) {
674 // Save a copy of r1 as the base pointer.
675 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
676 .addReg(SPReg)
677 .addReg(SPReg);
678 }
679
680 if (HasBP && MaxAlign > 1) {
681 if (isPPC64)
682 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
683 .addReg(SPReg)
684 .addImm(0)
685 .addImm(64 - Log2_32(MaxAlign));
686 else // PPC32...
687 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
688 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000689 .addImm(0)
690 .addImm(32 - Log2_32(MaxAlign))
691 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000692 if (!isLargeFrame) {
693 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
694 .addReg(ScratchReg, RegState::Kill)
695 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000696 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000697 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000698 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000699 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
700 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000701 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000702 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
703 .addReg(ScratchReg, RegState::Kill)
704 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000705 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000706 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
707 .addReg(SPReg, RegState::Kill)
708 .addReg(SPReg)
709 .addReg(ScratchReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000710
Bill Schmidtf381afc2013-08-20 03:12:23 +0000711 } else if (!isLargeFrame) {
712 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
713 .addReg(SPReg)
714 .addImm(NegFrameSize)
715 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000716
Bill Schmidtf381afc2013-08-20 03:12:23 +0000717 } else {
718 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
719 .addImm(NegFrameSize >> 16);
720 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
721 .addReg(ScratchReg, RegState::Kill)
722 .addImm(NegFrameSize & 0xFFFF);
723 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
724 .addReg(SPReg, RegState::Kill)
725 .addReg(SPReg)
726 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000727 }
728
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000729 // Add the "machine moves" for the instructions we generated above, but in
730 // reverse order.
731 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000732 // Show update of SP.
Rafael Espindola6e8c0d92013-05-16 03:34:58 +0000733 assert(NegFrameSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000734 unsigned CFIIndex = MMI.addFrameInst(
735 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
Eric Christopher612bb692014-04-29 00:16:46 +0000736 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
737 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000738
739 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000740 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000741 CFIIndex = MMI.addFrameInst(
742 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000743 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000744 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000745 }
746
Hal Finkela7c54e82013-07-17 00:45:52 +0000747 if (HasBP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000748 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000749 CFIIndex = MMI.addFrameInst(
750 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000751 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000752 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000753 }
754
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000755 if (MustSaveLR) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000756 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000757 CFIIndex = MMI.addFrameInst(
758 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000759 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000760 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000761 }
762 }
763
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000764 // If there is a frame pointer, copy R1 into R31
765 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000766 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
767 .addReg(SPReg)
768 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000769
770 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000771 // Mark effective beginning of when frame pointer is ready.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000772 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000773 unsigned CFIIndex = MMI.addFrameInst(
774 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
775
Eric Christopher612bb692014-04-29 00:16:46 +0000776 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000777 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000778 }
779 }
780
781 if (needsFrameMoves) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000782 // Add callee saved registers to move list.
783 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
784 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000785 unsigned Reg = CSI[I].getReg();
786 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000787
788 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
789 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +0000790 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +0000791 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +0000792
Roman Divackyc9e23d92012-09-12 14:47:47 +0000793 // For SVR4, don't emit a move for the CR spill slot if we haven't
794 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000795 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
796 && MustSaveCRs.empty())
797 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000798
799 // For 64-bit SVR4 when we have spilled CRs, the spill location
800 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000801 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000802 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
803 // the whole CR word. In the ELFv2 ABI, every CR that was
804 // actually saved gets its own CFI record.
805 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000806 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000807 nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +0000808 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000809 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000810 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000811 }
812
813 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000814 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
815 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +0000816 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000817 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000818 }
819 }
820}
821
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000822void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000823 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000824 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
825 assert(MBBI != MBB.end() && "Returning block has no terminator");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000826 const PPCInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000827 *static_cast<const PPCInstrInfo *>(MF.getSubtarget().getInstrInfo());
828 const PPCRegisterInfo *RegInfo =
829 static_cast<const PPCRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000830
831 unsigned RetOpcode = MBBI->getOpcode();
832 DebugLoc dl;
833
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000834 assert((RetOpcode == PPC::BLR ||
835 RetOpcode == PPC::TCRETURNri ||
836 RetOpcode == PPC::TCRETURNdi ||
837 RetOpcode == PPC::TCRETURNai ||
838 RetOpcode == PPC::TCRETURNri8 ||
839 RetOpcode == PPC::TCRETURNdi8 ||
840 RetOpcode == PPC::TCRETURNai8) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000841 "Can only insert epilog into returning blocks");
842
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000843 // Get alignment info so we know how to restore the SP.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000844 const MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000845
846 // Get the number of bytes allocated from the FrameInfo.
847 int FrameSize = MFI->getStackSize();
848
849 // Get processor type.
850 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000851 // Get the ABI.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000852 bool isDarwinABI = Subtarget.isDarwinABI();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000853 bool isSVR4ABI = Subtarget.isSVR4ABI();
Hal Finkel3ee2af72014-07-18 23:29:49 +0000854 bool isPIC = MF.getTarget().getRelocationModel() == Reloc::PIC_;
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000855
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000856 // Check if the link register (LR) has been saved.
857 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
858 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000859 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000860 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000861 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000862 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000863
Bill Schmidtf381afc2013-08-20 03:12:23 +0000864 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000865 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000866 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
867 unsigned ScratchReg = isPPC64 ? PPC::X0 : PPC::R0;
868 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
869 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
870 : PPC::MTLR );
871 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
872 : PPC::LWZ );
873 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
874 : PPC::LIS );
875 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
876 : PPC::ORI );
877 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
878 : PPC::ADDI );
879 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
880 : PPC::ADD4 );
881
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000882 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000883
884 int FPOffset = 0;
885 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000886 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000887 MachineFrameInfo *FFI = MF.getFrameInfo();
888 int FPIndex = FI->getFramePointerSaveIndex();
889 assert(FPIndex && "No Frame Pointer Save Slot!");
890 FPOffset = FFI->getObjectOffset(FPIndex);
891 } else {
Eric Christopherd1737492014-04-29 00:16:40 +0000892 FPOffset =
893 PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000894 }
895 }
896
Hal Finkela7c54e82013-07-17 00:45:52 +0000897 int BPOffset = 0;
898 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000899 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +0000900 MachineFrameInfo *FFI = MF.getFrameInfo();
901 int BPIndex = FI->getBasePointerSaveIndex();
902 assert(BPIndex && "No Base Pointer Save Slot!");
903 BPOffset = FFI->getObjectOffset(BPIndex);
904 } else {
905 BPOffset =
Hal Finkel3ee2af72014-07-18 23:29:49 +0000906 PPCFrameLowering::getBasePointerSaveOffset(isPPC64,
907 isDarwinABI,
908 isPIC);
Hal Finkela7c54e82013-07-17 00:45:52 +0000909 }
910 }
911
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000912 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
913 RetOpcode == PPC::TCRETURNdi ||
914 RetOpcode == PPC::TCRETURNai ||
915 RetOpcode == PPC::TCRETURNri8 ||
916 RetOpcode == PPC::TCRETURNdi8 ||
917 RetOpcode == PPC::TCRETURNai8;
918
919 if (UsesTCRet) {
920 int MaxTCRetDelta = FI->getTailCallSPDelta();
921 MachineOperand &StackAdjust = MBBI->getOperand(1);
922 assert(StackAdjust.isImm() && "Expecting immediate value.");
923 // Adjust stack pointer.
924 int StackAdj = StackAdjust.getImm();
925 int Delta = StackAdj - MaxTCRetDelta;
926 assert((Delta >= 0) && "Delta must be positive");
927 if (MaxTCRetDelta>0)
928 FrameSize += (StackAdj +Delta);
929 else
930 FrameSize += StackAdj;
931 }
932
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000933 // Frames of 32KB & larger require special handling because they cannot be
934 // indexed into with a simple LD/LWZ immediate offset operand.
935 bool isLargeFrame = !isInt<16>(FrameSize);
936
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000937 if (FrameSize) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000938 // In the prologue, the loaded (or persistent) stack pointer value is offset
939 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000940
941 // If this function contained a fastcc call and GuaranteedTailCallOpt is
942 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
943 // call which invalidates the stack pointer value in SP(0). So we use the
944 // value of R31 in this case.
945 if (FI->hasFastCall()) {
946 assert(HasFP && "Expecting a valid frame pointer.");
947 if (!isLargeFrame) {
948 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
949 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000950 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000951 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
952 .addImm(FrameSize >> 16);
953 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
954 .addReg(ScratchReg, RegState::Kill)
955 .addImm(FrameSize & 0xFFFF);
956 BuildMI(MBB, MBBI, dl, AddInst)
957 .addReg(SPReg)
958 .addReg(FPReg)
959 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000960 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000961 } else if (!isLargeFrame && !HasBP && !MFI->hasVarSizedObjects()) {
962 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
963 .addReg(SPReg)
964 .addImm(FrameSize);
965 } else {
966 BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
967 .addImm(0)
968 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000969 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000970
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000971 }
972
Bill Schmidtf381afc2013-08-20 03:12:23 +0000973 if (MustSaveLR)
974 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
975 .addImm(LROffset)
976 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000977
Bill Schmidtf381afc2013-08-20 03:12:23 +0000978 assert((isPPC64 || MustSaveCRs.empty()) &&
979 "Epilogue CR restoring supported only in 64-bit mode");
Hal Finkel67369882013-04-15 02:07:05 +0000980
Bill Schmidtf381afc2013-08-20 03:12:23 +0000981 if (!MustSaveCRs.empty()) // will only occur for PPC64
982 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
983 .addImm(8)
984 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000985
Bill Schmidtf381afc2013-08-20 03:12:23 +0000986 if (HasFP)
987 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
988 .addImm(FPOffset)
989 .addReg(SPReg);
Hal Finkela7c54e82013-07-17 00:45:52 +0000990
Bill Schmidtf381afc2013-08-20 03:12:23 +0000991 if (HasBP)
992 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
993 .addImm(BPOffset)
994 .addReg(SPReg);
Hal Finkel67369882013-04-15 02:07:05 +0000995
Bill Schmidtf381afc2013-08-20 03:12:23 +0000996 if (!MustSaveCRs.empty()) // will only occur for PPC64
997 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
998 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
999 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001000
Bill Schmidtf381afc2013-08-20 03:12:23 +00001001 if (MustSaveLR)
1002 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001003
1004 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1005 // call optimization
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001006 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001007 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1008 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1009 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001010
1011 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001012 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1013 .addReg(SPReg).addImm(CallerAllocatedAmt);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001014 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001015 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001016 .addImm(CallerAllocatedAmt >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001017 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1018 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001019 .addImm(CallerAllocatedAmt & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001020 BuildMI(MBB, MBBI, dl, AddInst)
1021 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001022 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001023 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001024 }
1025 } else if (RetOpcode == PPC::TCRETURNdi) {
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::TAILB)).
1029 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1030 } else if (RetOpcode == PPC::TCRETURNri) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001031 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001032 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1033 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1034 } else if (RetOpcode == PPC::TCRETURNai) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001035 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001036 MachineOperand &JumpTarget = MBBI->getOperand(0);
1037 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1038 } else if (RetOpcode == PPC::TCRETURNdi8) {
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::TAILB8)).
1042 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1043 } else if (RetOpcode == PPC::TCRETURNri8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001044 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001045 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1046 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1047 } else if (RetOpcode == PPC::TCRETURNai8) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001048 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001049 MachineOperand &JumpTarget = MBBI->getOperand(0);
1050 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1051 }
1052}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001053
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001054/// MustSaveLR - Return true if this function requires that we save the LR
1055/// register onto the stack in the prolog and restore it in the epilog of the
1056/// function.
1057static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
1058 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
1059
1060 // We need a save/restore of LR if there is any def of LR (which is
1061 // defined by calls, including the PIC setup sequence), or if there is
1062 // some use of the LR stack slot (e.g. for builtin_return_address).
1063 // (LR comes in 32 and 64 bit versions.)
1064 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
1065 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
1066}
1067
1068void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001069PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001070 RegScavenger *) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001071 const PPCRegisterInfo *RegInfo =
1072 static_cast<const PPCRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001073
1074 // Save and clear the LR state.
1075 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1076 unsigned LR = RegInfo->getRARegister();
1077 FI->setMustSaveLR(MustSaveLR(MF, LR));
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001078 MachineRegisterInfo &MRI = MF.getRegInfo();
1079 MRI.setPhysRegUnused(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001080
1081 // Save R31 if necessary
1082 int FPSI = FI->getFramePointerSaveIndex();
1083 bool isPPC64 = Subtarget.isPPC64();
1084 bool isDarwinABI = Subtarget.isDarwinABI();
Hal Finkel3ee2af72014-07-18 23:29:49 +00001085 bool isPIC = MF.getTarget().getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001086 MachineFrameInfo *MFI = MF.getFrameInfo();
1087
1088 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001089 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001090 // Find out what the fix offset of the frame pointer save area.
1091 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI);
1092 // Allocate the frame index for frame pointer save area.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001093 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001094 // Save the result.
1095 FI->setFramePointerSaveIndex(FPSI);
1096 }
1097
Hal Finkela7c54e82013-07-17 00:45:52 +00001098 int BPSI = FI->getBasePointerSaveIndex();
1099 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Hal Finkel3ee2af72014-07-18 23:29:49 +00001100 int BPOffset = getBasePointerSaveOffset(isPPC64, isDarwinABI, isPIC);
Hal Finkela7c54e82013-07-17 00:45:52 +00001101 // Allocate the frame index for the base pointer save area.
1102 BPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1103 // Save the result.
1104 FI->setBasePointerSaveIndex(BPSI);
1105 }
1106
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001107 // Reserve stack space to move the linkage area to in case of a tail call.
1108 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001109 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1110 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001111 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001112 }
1113
Eric Christopherd1737492014-04-29 00:16:40 +00001114 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001115 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001116 if (!isPPC64 && !isDarwinABI &&
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001117 (MRI.isPhysRegUsed(PPC::CR2) ||
1118 MRI.isPhysRegUsed(PPC::CR3) ||
1119 MRI.isPhysRegUsed(PPC::CR4))) {
1120 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1121 FI->setCRSpillFrameIndex(FrameIdx);
1122 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001123}
1124
Hal Finkel5a765fd2013-03-14 20:33:40 +00001125void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001126 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001127 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001128 if (!Subtarget.isSVR4ABI()) {
1129 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001130 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001131 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001132
1133 // Get callee saved register information.
1134 MachineFrameInfo *FFI = MF.getFrameInfo();
1135 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1136
1137 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001138 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001139 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001140 return;
1141 }
1142
1143 unsigned MinGPR = PPC::R31;
1144 unsigned MinG8R = PPC::X31;
1145 unsigned MinFPR = PPC::F31;
1146 unsigned MinVR = PPC::V31;
1147
1148 bool HasGPSaveArea = false;
1149 bool HasG8SaveArea = false;
1150 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001151 bool HasVRSAVESaveArea = false;
1152 bool HasVRSaveArea = false;
1153
1154 SmallVector<CalleeSavedInfo, 18> GPRegs;
1155 SmallVector<CalleeSavedInfo, 18> G8Regs;
1156 SmallVector<CalleeSavedInfo, 18> FPRegs;
1157 SmallVector<CalleeSavedInfo, 18> VRegs;
1158
1159 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1160 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001161 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001162 HasGPSaveArea = true;
1163
1164 GPRegs.push_back(CSI[i]);
1165
1166 if (Reg < MinGPR) {
1167 MinGPR = Reg;
1168 }
Craig Topperabadc662012-04-20 06:31:50 +00001169 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001170 HasG8SaveArea = true;
1171
1172 G8Regs.push_back(CSI[i]);
1173
1174 if (Reg < MinG8R) {
1175 MinG8R = Reg;
1176 }
Craig Topperabadc662012-04-20 06:31:50 +00001177 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001178 HasFPSaveArea = true;
1179
1180 FPRegs.push_back(CSI[i]);
1181
1182 if (Reg < MinFPR) {
1183 MinFPR = Reg;
1184 }
Craig Topperabadc662012-04-20 06:31:50 +00001185 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1186 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001187 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001188 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001189 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001190 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001191 HasVRSaveArea = true;
1192
1193 VRegs.push_back(CSI[i]);
1194
1195 if (Reg < MinVR) {
1196 MinVR = Reg;
1197 }
1198 } else {
1199 llvm_unreachable("Unknown RegisterClass!");
1200 }
1201 }
1202
1203 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +00001204 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001205
1206 int64_t LowerBound = 0;
1207
1208 // Take into account stack space reserved for tail calls.
1209 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001210 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1211 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001212 LowerBound = TCSPDelta;
1213 }
1214
1215 // The Floating-point register save area is right below the back chain word
1216 // of the previous stack frame.
1217 if (HasFPSaveArea) {
1218 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1219 int FI = FPRegs[i].getFrameIdx();
1220
1221 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1222 }
1223
Hal Finkelfeea6532013-03-26 20:08:20 +00001224 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001225 }
1226
1227 // Check whether the frame pointer register is allocated. If so, make sure it
1228 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001229 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001230 HasGPSaveArea = true;
1231
1232 int FI = PFI->getFramePointerSaveIndex();
1233 assert(FI && "No Frame Pointer Save Slot!");
1234
1235 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1236 }
1237
Eric Christopherfc6de422014-08-05 02:39:49 +00001238 const PPCRegisterInfo *RegInfo =
1239 static_cast<const PPCRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +00001240 if (RegInfo->hasBasePointer(MF)) {
1241 HasGPSaveArea = true;
1242
1243 int FI = PFI->getBasePointerSaveIndex();
1244 assert(FI && "No Base Pointer Save Slot!");
1245
1246 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1247 }
1248
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001249 // General register save area starts right below the Floating-point
1250 // register save area.
1251 if (HasGPSaveArea || HasG8SaveArea) {
1252 // Move general register save area spill slots down, taking into account
1253 // the size of the Floating-point register save area.
1254 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1255 int FI = GPRegs[i].getFrameIdx();
1256
1257 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1258 }
1259
1260 // Move general register save area spill slots down, taking into account
1261 // the size of the Floating-point register save area.
1262 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1263 int FI = G8Regs[i].getFrameIdx();
1264
1265 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1266 }
1267
1268 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001269 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1270 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001271
1272 if (Subtarget.isPPC64()) {
1273 LowerBound -= (31 - MinReg + 1) * 8;
1274 } else {
1275 LowerBound -= (31 - MinReg + 1) * 4;
1276 }
1277 }
1278
Roman Divackyc9e23d92012-09-12 14:47:47 +00001279 // For 32-bit only, the CR save area is below the general register
1280 // save area. For 64-bit SVR4, the CR save area is addressed relative
1281 // to the stack pointer and hence does not need an adjustment here.
1282 // Only CR2 (the first nonvolatile spilled) has an associated frame
1283 // index so that we have a single uniform save area.
1284 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001285 // Adjust the frame index of the CR spill slot.
1286 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1287 unsigned Reg = CSI[i].getReg();
1288
Roman Divackyc9e23d92012-09-12 14:47:47 +00001289 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001290 // Leave Darwin logic as-is.
1291 || (!Subtarget.isSVR4ABI() &&
1292 (PPC::CRBITRCRegClass.contains(Reg) ||
1293 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001294 int FI = CSI[i].getFrameIdx();
1295
1296 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1297 }
1298 }
1299
1300 LowerBound -= 4; // The CR save area is always 4 bytes long.
1301 }
1302
1303 if (HasVRSAVESaveArea) {
1304 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1305 // which have the VRSAVE register class?
1306 // Adjust the frame index of the VRSAVE spill slot.
1307 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1308 unsigned Reg = CSI[i].getReg();
1309
Craig Topperabadc662012-04-20 06:31:50 +00001310 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001311 int FI = CSI[i].getFrameIdx();
1312
1313 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1314 }
1315 }
1316
1317 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1318 }
1319
1320 if (HasVRSaveArea) {
1321 // Insert alignment padding, we need 16-byte alignment.
1322 LowerBound = (LowerBound - 15) & ~(15);
1323
1324 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1325 int FI = VRegs[i].getFrameIdx();
1326
1327 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1328 }
1329 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001330
1331 addScavengingSpillSlot(MF, RS);
1332}
1333
1334void
1335PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1336 RegScavenger *RS) const {
1337 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1338 // a large stack, which will require scavenging a register to materialize a
1339 // large offset.
1340
1341 // We need to have a scavenger spill slot for spills if the frame size is
1342 // large. In case there is no free register for large-offset addressing,
1343 // this slot is used for the necessary emergency spill. Also, we need the
1344 // slot for dynamic stack allocations.
1345
1346 // The scavenger might be invoked if the frame offset does not fit into
1347 // the 16-bit immediate. We don't know the complete frame size here
1348 // because we've not yet computed callee-saved register spills or the
1349 // needed alignment padding.
1350 unsigned StackSize = determineFrameLayout(MF, false, true);
1351 MachineFrameInfo *MFI = MF.getFrameInfo();
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001352 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1353 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001354 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1355 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1356 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Hal Finkel9e331c22013-03-22 23:32:27 +00001357 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Hal Finkelbb420f12013-03-15 05:06:04 +00001358 RC->getAlignment(),
1359 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001360
Hal Finkel18607632013-07-18 04:28:21 +00001361 // Might we have over-aligned allocas?
1362 bool HasAlVars = MFI->hasVarSizedObjects() &&
1363 MFI->getMaxAlignment() > getStackAlignment();
1364
Hal Finkel0dfbb052013-03-26 18:57:22 +00001365 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001366 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Hal Finkel0dfbb052013-03-26 18:57:22 +00001367 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1368 RC->getAlignment(),
1369 false));
1370
Hal Finkelbb420f12013-03-15 05:06:04 +00001371 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001372}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001373
Eric Christopherd1737492014-04-29 00:16:40 +00001374bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001375PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001376 MachineBasicBlock::iterator MI,
1377 const std::vector<CalleeSavedInfo> &CSI,
1378 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001379
1380 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1381 // Return false otherwise to maintain pre-existing behavior.
1382 if (!Subtarget.isSVR4ABI())
1383 return false;
1384
1385 MachineFunction *MF = MBB.getParent();
1386 const PPCInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001387 *static_cast<const PPCInstrInfo *>(MF->getSubtarget().getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001388 DebugLoc DL;
1389 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001390 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001391
Roman Divackyc9e23d92012-09-12 14:47:47 +00001392 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1393 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001394 // Only Darwin actually uses the VRSAVE register, but it can still appear
1395 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1396 // Darwin, ignore it.
1397 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1398 continue;
1399
Roman Divackyc9e23d92012-09-12 14:47:47 +00001400 // CR2 through CR4 are the nonvolatile CR fields.
1401 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1402
Roman Divackyc9e23d92012-09-12 14:47:47 +00001403 // Add the callee-saved register as live-in; it's killed at the spill.
1404 MBB.addLiveIn(Reg);
1405
Hal Finkel2f293912013-04-13 23:06:15 +00001406 if (CRSpilled && IsCRField) {
1407 CRMIB.addReg(Reg, RegState::ImplicitKill);
1408 continue;
1409 }
1410
Roman Divackyc9e23d92012-09-12 14:47:47 +00001411 // Insert the spill to the stack frame.
1412 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001413 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001414 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001415 // The actual spill will happen at the start of the prologue.
1416 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001417 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001418 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001419 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001420
Eric Christopherd1737492014-04-29 00:16:40 +00001421 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1422 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1423 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001424 .addReg(Reg, RegState::ImplicitKill);
1425
Eric Christopherd1737492014-04-29 00:16:40 +00001426 MBB.insert(MI, CRMIB);
1427 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1428 .addReg(PPC::R12,
1429 getKillRegState(true)),
1430 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001431 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001432 } else {
1433 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1434 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001435 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001436 }
1437 }
1438 return true;
1439}
1440
1441static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001442restoreCRs(bool isPPC64, bool is31,
1443 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001444 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1445 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001446
1447 MachineFunction *MF = MBB.getParent();
1448 const PPCInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001449 *static_cast<const PPCInstrInfo *>(MF->getSubtarget().getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001450 DebugLoc DL;
1451 unsigned RestoreOp, MoveReg;
1452
Hal Finkel67369882013-04-15 02:07:05 +00001453 if (isPPC64)
1454 // This is handled during epilogue generation.
1455 return;
1456 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001457 // 32-bit: FP-relative
1458 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00001459 PPC::R12),
1460 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00001461 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001462 MoveReg = PPC::R12;
1463 }
Eric Christopherd1737492014-04-29 00:16:40 +00001464
Roman Divackyc9e23d92012-09-12 14:47:47 +00001465 if (CR2Spilled)
1466 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00001467 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001468
1469 if (CR3Spilled)
1470 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00001471 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001472
1473 if (CR4Spilled)
1474 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00001475 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001476}
1477
Eli Bendersky8da87162013-02-21 20:05:00 +00001478void PPCFrameLowering::
1479eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1480 MachineBasicBlock::iterator I) const {
1481 const PPCInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001482 *static_cast<const PPCInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001483 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1484 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1485 // Add (actually subtract) back the amount the callee popped on return.
1486 if (int CalleeAmt = I->getOperand(1).getImm()) {
1487 bool is64Bit = Subtarget.isPPC64();
1488 CalleeAmt *= -1;
1489 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1490 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1491 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1492 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1493 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1494 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1495 MachineInstr *MI = I;
1496 DebugLoc dl = MI->getDebugLoc();
1497
1498 if (isInt<16>(CalleeAmt)) {
1499 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1500 .addReg(StackReg, RegState::Kill)
1501 .addImm(CalleeAmt);
1502 } else {
1503 MachineBasicBlock::iterator MBBI = I;
1504 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1505 .addImm(CalleeAmt >> 16);
1506 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1507 .addReg(TmpReg, RegState::Kill)
1508 .addImm(CalleeAmt & 0xFFFF);
1509 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1510 .addReg(StackReg, RegState::Kill)
1511 .addReg(TmpReg);
1512 }
1513 }
1514 }
1515 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1516 MBB.erase(I);
1517}
1518
Eric Christopherd1737492014-04-29 00:16:40 +00001519bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001520PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001521 MachineBasicBlock::iterator MI,
1522 const std::vector<CalleeSavedInfo> &CSI,
1523 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001524
1525 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1526 // Return false otherwise to maintain pre-existing behavior.
1527 if (!Subtarget.isSVR4ABI())
1528 return false;
1529
1530 MachineFunction *MF = MBB.getParent();
1531 const PPCInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001532 *static_cast<const PPCInstrInfo *>(MF->getSubtarget().getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001533 bool CR2Spilled = false;
1534 bool CR3Spilled = false;
1535 bool CR4Spilled = false;
1536 unsigned CSIIndex = 0;
1537
1538 // Initialize insertion-point logic; we will be restoring in reverse
1539 // order of spill.
1540 MachineBasicBlock::iterator I = MI, BeforeI = I;
1541 bool AtStart = I == MBB.begin();
1542
1543 if (!AtStart)
1544 --BeforeI;
1545
1546 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1547 unsigned Reg = CSI[i].getReg();
1548
Hal Finkelac1a24b2013-06-28 22:29:56 +00001549 // Only Darwin actually uses the VRSAVE register, but it can still appear
1550 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1551 // Darwin, ignore it.
1552 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1553 continue;
1554
Roman Divackyc9e23d92012-09-12 14:47:47 +00001555 if (Reg == PPC::CR2) {
1556 CR2Spilled = true;
1557 // The spill slot is associated only with CR2, which is the
1558 // first nonvolatile spilled. Save it here.
1559 CSIIndex = i;
1560 continue;
1561 } else if (Reg == PPC::CR3) {
1562 CR3Spilled = true;
1563 continue;
1564 } else if (Reg == PPC::CR4) {
1565 CR4Spilled = true;
1566 continue;
1567 } else {
1568 // When we first encounter a non-CR register after seeing at
1569 // least one CR register, restore all spilled CRs together.
1570 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00001571 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00001572 bool is31 = needsFP(*MF);
1573 restoreCRs(Subtarget.isPPC64(), is31,
1574 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001575 MBB, I, CSI, CSIIndex);
1576 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001577 }
1578
1579 // Default behavior for non-CR saves.
1580 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1581 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00001582 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001583 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00001584 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00001585 }
1586
1587 // Insert in reverse order.
1588 if (AtStart)
1589 I = MBB.begin();
1590 else {
1591 I = BeforeI;
1592 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00001593 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001594 }
1595
1596 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00001597 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00001598 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001599 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001600 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00001601 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001602
1603 return true;
1604}