blob: 4c9430a2eca07cf571ebd539e9561108e9e054c5 [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"
Eric Christopherfcd3d872015-02-13 22:48:53 +000019#include "PPCTargetMachine.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000025#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000027#include "llvm/Target/TargetOptions.h"
28
29using namespace llvm;
30
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000031/// VRRegNo - Map from a numbered VR register to its enum value.
32///
Craig Toppere5e035a32015-12-05 07:13:35 +000033static const MCPhysReg VRRegNo[] = {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000034 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
35 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
36 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
37 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
38};
39
Eric Christopherf71609b2015-02-13 00:39:27 +000040static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) {
41 if (STI.isDarwinABI())
42 return STI.isPPC64() ? 16 : 8;
43 // SVR4 ABI:
44 return STI.isPPC64() ? 16 : 4;
45}
46
Eric Christopher736d39e2015-02-13 00:39:36 +000047static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) {
48 return STI.isELFv2ABI() ? 24 : 40;
49}
50
Eric Christopherdc3a8a42015-02-13 00:39:38 +000051static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) {
52 // For the Darwin ABI:
53 // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
54 // for saving the frame pointer (if needed.) While the published ABI has
55 // not used this slot since at least MacOSX 10.2, there is older code
56 // around that does use it, and that needs to continue to work.
57 if (STI.isDarwinABI())
58 return STI.isPPC64() ? -8U : -4U;
59
60 // SVR4 ABI: First slot in the general register save area.
61 return STI.isPPC64() ? -8U : -4U;
62}
63
Eric Christophera4ae2132015-02-13 22:22:57 +000064static unsigned computeLinkageSize(const PPCSubtarget &STI) {
65 if (STI.isDarwinABI() || STI.isPPC64())
66 return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4);
67
68 // SVR4 ABI:
69 return 8;
70}
71
Eric Christopherfcd3d872015-02-13 22:48:53 +000072static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) {
73 if (STI.isDarwinABI())
74 return STI.isPPC64() ? -16U : -8U;
75
76 // SVR4 ABI: First slot in the general register save area.
77 return STI.isPPC64()
78 ? -16U
Rafael Espindola248cfb92016-06-28 12:49:12 +000079 : STI.getTargetMachine().isPositionIndependent() ? -12U : -8U;
Eric Christopherfcd3d872015-02-13 22:48:53 +000080}
81
Eric Christopherd104c312014-06-12 20:54:11 +000082PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
83 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
Hal Finkelc93a9a22015-02-25 01:06:45 +000084 STI.getPlatformStackAlignment(), 0),
Eric Christopher736d39e2015-02-13 00:39:36 +000085 Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)),
Eric Christopherdc3a8a42015-02-13 00:39:38 +000086 TOCSaveOffset(computeTOCSaveOffset(Subtarget)),
Eric Christophera4ae2132015-02-13 22:22:57 +000087 FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)),
Eric Christopherfcd3d872015-02-13 22:48:53 +000088 LinkageSize(computeLinkageSize(Subtarget)),
89 BasePointerSaveOffset(computeBasePointerSaveOffset(STI)) {}
Eric Christopherd104c312014-06-12 20:54:11 +000090
Eric Christopherd104c312014-06-12 20:54:11 +000091// With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
92const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
93 unsigned &NumEntries) const {
94 if (Subtarget.isDarwinABI()) {
95 NumEntries = 1;
96 if (Subtarget.isPPC64()) {
97 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
98 return &darwin64Offsets;
99 } else {
100 static const SpillSlot darwinOffsets = {PPC::R31, -4};
101 return &darwinOffsets;
102 }
103 }
104
105 // Early exit if not using the SVR4 ABI.
106 if (!Subtarget.isSVR4ABI()) {
107 NumEntries = 0;
108 return nullptr;
109 }
110
111 // Note that the offsets here overlap, but this is fixed up in
112 // processFunctionBeforeFrameFinalized.
113
114 static const SpillSlot Offsets[] = {
115 // Floating-point register save area offsets.
116 {PPC::F31, -8},
117 {PPC::F30, -16},
118 {PPC::F29, -24},
119 {PPC::F28, -32},
120 {PPC::F27, -40},
121 {PPC::F26, -48},
122 {PPC::F25, -56},
123 {PPC::F24, -64},
124 {PPC::F23, -72},
125 {PPC::F22, -80},
126 {PPC::F21, -88},
127 {PPC::F20, -96},
128 {PPC::F19, -104},
129 {PPC::F18, -112},
130 {PPC::F17, -120},
131 {PPC::F16, -128},
132 {PPC::F15, -136},
133 {PPC::F14, -144},
134
135 // General register save area offsets.
136 {PPC::R31, -4},
137 {PPC::R30, -8},
138 {PPC::R29, -12},
139 {PPC::R28, -16},
140 {PPC::R27, -20},
141 {PPC::R26, -24},
142 {PPC::R25, -28},
143 {PPC::R24, -32},
144 {PPC::R23, -36},
145 {PPC::R22, -40},
146 {PPC::R21, -44},
147 {PPC::R20, -48},
148 {PPC::R19, -52},
149 {PPC::R18, -56},
150 {PPC::R17, -60},
151 {PPC::R16, -64},
152 {PPC::R15, -68},
153 {PPC::R14, -72},
154
155 // CR save area offset. We map each of the nonvolatile CR fields
156 // to the slot for CR2, which is the first of the nonvolatile CR
157 // fields to be assigned, so that we only allocate one save slot.
158 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
159 {PPC::CR2, -4},
160
161 // VRSAVE save area offset.
162 {PPC::VRSAVE, -4},
163
164 // Vector register save area
165 {PPC::V31, -16},
166 {PPC::V30, -32},
167 {PPC::V29, -48},
168 {PPC::V28, -64},
169 {PPC::V27, -80},
170 {PPC::V26, -96},
171 {PPC::V25, -112},
172 {PPC::V24, -128},
173 {PPC::V23, -144},
174 {PPC::V22, -160},
175 {PPC::V21, -176},
176 {PPC::V20, -192}};
177
178 static const SpillSlot Offsets64[] = {
179 // Floating-point register save area offsets.
180 {PPC::F31, -8},
181 {PPC::F30, -16},
182 {PPC::F29, -24},
183 {PPC::F28, -32},
184 {PPC::F27, -40},
185 {PPC::F26, -48},
186 {PPC::F25, -56},
187 {PPC::F24, -64},
188 {PPC::F23, -72},
189 {PPC::F22, -80},
190 {PPC::F21, -88},
191 {PPC::F20, -96},
192 {PPC::F19, -104},
193 {PPC::F18, -112},
194 {PPC::F17, -120},
195 {PPC::F16, -128},
196 {PPC::F15, -136},
197 {PPC::F14, -144},
198
199 // General register save area offsets.
200 {PPC::X31, -8},
201 {PPC::X30, -16},
202 {PPC::X29, -24},
203 {PPC::X28, -32},
204 {PPC::X27, -40},
205 {PPC::X26, -48},
206 {PPC::X25, -56},
207 {PPC::X24, -64},
208 {PPC::X23, -72},
209 {PPC::X22, -80},
210 {PPC::X21, -88},
211 {PPC::X20, -96},
212 {PPC::X19, -104},
213 {PPC::X18, -112},
214 {PPC::X17, -120},
215 {PPC::X16, -128},
216 {PPC::X15, -136},
217 {PPC::X14, -144},
218
219 // VRSAVE save area offset.
220 {PPC::VRSAVE, -4},
221
222 // Vector register save area
223 {PPC::V31, -16},
224 {PPC::V30, -32},
225 {PPC::V29, -48},
226 {PPC::V28, -64},
227 {PPC::V27, -80},
228 {PPC::V26, -96},
229 {PPC::V25, -112},
230 {PPC::V24, -128},
231 {PPC::V23, -144},
232 {PPC::V22, -160},
233 {PPC::V21, -176},
234 {PPC::V20, -192}};
235
236 if (Subtarget.isPPC64()) {
237 NumEntries = array_lengthof(Offsets64);
238
239 return Offsets64;
240 } else {
241 NumEntries = array_lengthof(Offsets);
242
243 return Offsets;
244 }
245}
246
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000247/// RemoveVRSaveCode - We have found that this function does not need any code
248/// to manipulate the VRSAVE register, even though it uses vector registers.
249/// This can happen when the only registers used are known to be live in or out
250/// of the function. Remove all of the VRSAVE related code from the function.
Bill Schmidt38d94582012-10-10 20:54:15 +0000251/// FIXME: The removal of the code results in a compile failure at -O0 when the
252/// function contains a function call, as the GPR containing original VRSAVE
253/// contents is spilled and reloaded around the call. Without the prolog code,
254/// the spill instruction refers to an undefined register. This code needs
255/// to account for all uses of that GPR.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000256static void RemoveVRSaveCode(MachineInstr &MI) {
257 MachineBasicBlock *Entry = MI.getParent();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000258 MachineFunction *MF = Entry->getParent();
259
260 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
261 MachineBasicBlock::iterator MBBI = MI;
262 ++MBBI;
263 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
264 MBBI->eraseFromParent();
265
266 bool RemovedAllMTVRSAVEs = true;
267 // See if we can find and remove the MTVRSAVE instruction from all of the
268 // epilog blocks.
269 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
270 // If last instruction is a return instruction, add an epilogue
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000271 if (I->isReturnBlock()) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000272 bool FoundIt = false;
273 for (MBBI = I->end(); MBBI != I->begin(); ) {
274 --MBBI;
275 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
276 MBBI->eraseFromParent(); // remove it.
277 FoundIt = true;
278 break;
279 }
280 }
281 RemovedAllMTVRSAVEs &= FoundIt;
282 }
283 }
284
285 // If we found and removed all MTVRSAVE instructions, remove the read of
286 // VRSAVE as well.
287 if (RemovedAllMTVRSAVEs) {
288 MBBI = MI;
289 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
290 --MBBI;
291 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
292 MBBI->eraseFromParent();
293 }
294
295 // Finally, nuke the UPDATE_VRSAVE.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000296 MI.eraseFromParent();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000297}
298
299// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
300// instruction selector. Based on the vector registers that have been used,
301// transform this into the appropriate ORI instruction.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000302static void HandleVRSaveUpdate(MachineInstr &MI, const TargetInstrInfo &TII) {
303 MachineFunction *MF = MI.getParent()->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000304 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000305 DebugLoc dl = MI.getDebugLoc();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000306
Matthias Braun9912bb82015-07-14 17:52:07 +0000307 const MachineRegisterInfo &MRI = MF->getRegInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000308 unsigned UsedRegMask = 0;
309 for (unsigned i = 0; i != 32; ++i)
Matthias Braun9912bb82015-07-14 17:52:07 +0000310 if (MRI.isPhysRegModified(VRRegNo[i]))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000311 UsedRegMask |= 1 << (31-i);
312
313 // Live in and live out values already must be in the mask, so don't bother
314 // marking them.
315 for (MachineRegisterInfo::livein_iterator
316 I = MF->getRegInfo().livein_begin(),
317 E = MF->getRegInfo().livein_end(); I != E; ++I) {
Hal Finkelfeea6532013-03-26 20:08:20 +0000318 unsigned RegNo = TRI->getEncodingValue(I->first);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000319 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
320 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
321 }
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000322
323 // Live out registers appear as use operands on return instructions.
324 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
325 UsedRegMask != 0 && BI != BE; ++BI) {
326 const MachineBasicBlock &MBB = *BI;
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000327 if (!MBB.isReturnBlock())
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000328 continue;
329 const MachineInstr &Ret = MBB.back();
330 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
331 const MachineOperand &MO = Ret.getOperand(I);
332 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
333 continue;
Hal Finkelfeea6532013-03-26 20:08:20 +0000334 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000335 UsedRegMask &= ~(1 << (31-RegNo));
336 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000337 }
338
339 // If no registers are used, turn this into a copy.
340 if (UsedRegMask == 0) {
341 // Remove all VRSAVE code.
342 RemoveVRSaveCode(MI);
343 return;
344 }
345
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000346 unsigned SrcReg = MI.getOperand(1).getReg();
347 unsigned DstReg = MI.getOperand(0).getReg();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000348
349 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
350 if (DstReg != SrcReg)
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000351 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
352 .addReg(SrcReg)
353 .addImm(UsedRegMask);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000354 else
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000355 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
356 .addReg(SrcReg, RegState::Kill)
357 .addImm(UsedRegMask);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000358 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
359 if (DstReg != SrcReg)
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000360 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
361 .addReg(SrcReg)
362 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000363 else
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000364 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
365 .addReg(SrcReg, RegState::Kill)
366 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000367 } else {
368 if (DstReg != SrcReg)
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000369 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
370 .addReg(SrcReg)
371 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000372 else
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000373 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
374 .addReg(SrcReg, RegState::Kill)
375 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000376
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000377 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
378 .addReg(DstReg, RegState::Kill)
379 .addImm(UsedRegMask & 0xFFFF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000380 }
381
382 // Remove the old UPDATE_VRSAVE instruction.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000383 MI.eraseFromParent();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000384}
385
Roman Divackyc9e23d92012-09-12 14:47:47 +0000386static bool spillsCR(const MachineFunction &MF) {
387 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
388 return FuncInfo->isCRSpilled();
389}
390
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000391static bool spillsVRSAVE(const MachineFunction &MF) {
392 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
393 return FuncInfo->isVRSAVESpilled();
394}
395
Hal Finkelbb420f12013-03-15 05:06:04 +0000396static bool hasSpills(const MachineFunction &MF) {
397 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
398 return FuncInfo->hasSpills();
399}
400
Hal Finkelfcc51d42013-03-17 04:43:44 +0000401static bool hasNonRISpills(const MachineFunction &MF) {
402 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
403 return FuncInfo->hasNonRISpills();
404}
405
Bill Schmidt82f1c772015-02-10 19:09:05 +0000406/// MustSaveLR - Return true if this function requires that we save the LR
407/// register onto the stack in the prolog and restore it in the epilog of the
408/// function.
409static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
410 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
411
412 // We need a save/restore of LR if there is any def of LR (which is
413 // defined by calls, including the PIC setup sequence), or if there is
414 // some use of the LR stack slot (e.g. for builtin_return_address).
415 // (LR comes in 32 and 64 bit versions.)
416 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
417 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
418}
419
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000420/// determineFrameLayout - Determine the size of the frame and maximum call
421/// frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000422unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
423 bool UpdateMF,
424 bool UseEstimate) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000425 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000426
427 // Get the number of bytes to allocate from the FrameInfo
Hal Finkelbb420f12013-03-15 05:06:04 +0000428 unsigned FrameSize =
Matthias Braun941a7052016-07-28 18:40:00 +0000429 UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000430
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000431 // Get stack alignments. The frame must be aligned to the greatest of these:
432 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
Matthias Braun941a7052016-07-28 18:40:00 +0000433 unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame
Hal Finkela7c54e82013-07-17 00:45:52 +0000434 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
435
Eric Christopherb128abc2017-02-04 01:52:17 +0000436 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000437
438 // If we are a leaf function, and use up to 224 bytes of stack space,
439 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkel67369882013-04-15 02:07:05 +0000440 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000441 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
442 // stackless code if all local vars are reg-allocated.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000443 bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
Bill Schmidt82f1c772015-02-10 19:09:05 +0000444 unsigned LR = RegInfo->getRARegister();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000445 if (!DisableRedZone &&
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000446 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
447 !Subtarget.isSVR4ABI() || // allocated locals.
Eric Christopherd1737492014-04-29 00:16:40 +0000448 FrameSize == 0) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000449 FrameSize <= 224 && // Fits in red zone.
Matthias Braun941a7052016-07-28 18:40:00 +0000450 !MFI.hasVarSizedObjects() && // No dynamic alloca.
451 !MFI.adjustsStack() && // No calls.
Bill Schmidt82f1c772015-02-10 19:09:05 +0000452 !MustSaveLR(MF, LR) &&
Hal Finkela7c54e82013-07-17 00:45:52 +0000453 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000454 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000455 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000456 MFI.setStackSize(0);
Hal Finkelbb420f12013-03-15 05:06:04 +0000457 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000458 }
459
460 // Get the maximum call frame size of all the calls.
Matthias Braun941a7052016-07-28 18:40:00 +0000461 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000462
Ulrich Weigandf316e1d2014-06-23 13:47:52 +0000463 // Maximum call frame needs to be at least big enough for linkage area.
Eric Christophera4ae2132015-02-13 22:22:57 +0000464 unsigned minCallFrameSize = getLinkageSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000465 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
466
467 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
468 // that allocations will be aligned.
Matthias Braun941a7052016-07-28 18:40:00 +0000469 if (MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000470 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
471
472 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000473 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000474 MFI.setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000475
476 // Include call frame size in total.
477 FrameSize += maxCallFrameSize;
478
479 // Make sure the frame is aligned.
480 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
481
482 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000483 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000484 MFI.setStackSize(FrameSize);
Hal Finkelbb420f12013-03-15 05:06:04 +0000485
486 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000487}
488
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000489// hasFP - Return true if the specified function actually has a dedicated frame
490// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000491bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000492 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000493 // FIXME: This is pretty much broken by design: hasFP() might be called really
494 // early, before the stack layout was calculated and thus hasFP() might return
495 // true or false here depending on the time of call.
Matthias Braun941a7052016-07-28 18:40:00 +0000496 return (MFI.getStackSize()) && needsFP(MF);
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000497}
498
499// needsFP - Return true if the specified function should have a dedicated frame
500// pointer register. This is true if the function has variable sized allocas or
501// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000502bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000503 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000504
505 // Naked functions have no stack frame pushed, so we don't have a frame
506 // pointer.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000507 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000508 return false;
509
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000510 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000511 MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000512 (MF.getTarget().Options.GuaranteedTailCallOpt &&
513 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000514}
515
Hal Finkelaa03c032013-03-21 19:03:19 +0000516void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
517 bool is31 = needsFP(MF);
518 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
519 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
520
Eric Christopherb128abc2017-02-04 01:52:17 +0000521 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Hal Finkelf05d6c72013-07-17 23:50:51 +0000522 bool HasBP = RegInfo->hasBasePointer(MF);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000523 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000524 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
525
Hal Finkelaa03c032013-03-21 19:03:19 +0000526 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
527 BI != BE; ++BI)
528 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
529 --MBBI;
530 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
531 MachineOperand &MO = MBBI->getOperand(I);
532 if (!MO.isReg())
533 continue;
534
535 switch (MO.getReg()) {
536 case PPC::FP:
537 MO.setReg(FPReg);
538 break;
539 case PPC::FP8:
540 MO.setReg(FP8Reg);
541 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000542 case PPC::BP:
543 MO.setReg(BPReg);
544 break;
545 case PPC::BP8:
546 MO.setReg(BP8Reg);
547 break;
548
Hal Finkelaa03c032013-03-21 19:03:19 +0000549 }
550 }
551 }
552}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000553
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000554/* This function will do the following:
555 - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
556 respectively (defaults recommended by the ABI) and return true
557 - If MBB is not an entry block, initialize the register scavenger and look
558 for available registers.
559 - If the defaults (R0/R12) are available, return true
560 - If TwoUniqueRegsRequired is set to true, it looks for two unique
561 registers. Otherwise, look for a single available register.
562 - If the required registers are found, set SR1 and SR2 and return true.
563 - If the required registers are not found, set SR2 or both SR1 and SR2 to
564 PPC::NoRegister and return false.
565
566 Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
567 is not set, this function will attempt to find two different registers, but
568 still return true if only one register is available (and set SR1 == SR2).
569*/
570bool
571PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
572 bool UseAtEnd,
573 bool TwoUniqueRegsRequired,
574 unsigned *SR1,
575 unsigned *SR2) const {
Kit Barton9c432ae2015-11-16 20:22:15 +0000576 RegScavenger RS;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000577 unsigned R0 = Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
578 unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
Kit Barton9c432ae2015-11-16 20:22:15 +0000579
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000580 // Set the defaults for the two scratch registers.
581 if (SR1)
582 *SR1 = R0;
Kit Barton9c432ae2015-11-16 20:22:15 +0000583
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000584 if (SR2) {
585 assert (SR1 && "Asking for the second scratch register but not the first?");
586 *SR2 = R12;
587 }
588
589 // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
Kit Barton9c432ae2015-11-16 20:22:15 +0000590 if ((UseAtEnd && MBB->isReturnBlock()) ||
591 (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
592 return true;
Alexey Samsonov39b7d652015-12-02 21:25:28 +0000593
Matthias Braun7dc03f02016-04-06 02:47:09 +0000594 RS.enterBasicBlock(*MBB);
Kit Barton9c432ae2015-11-16 20:22:15 +0000595
Kit Bartonf4ce2f32015-11-30 18:59:41 +0000596 if (UseAtEnd && !MBB->empty()) {
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000597 // The scratch register will be used at the end of the block, so must
598 // consider all registers used within the block
Kit Bartonf4ce2f32015-11-30 18:59:41 +0000599
600 MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
601 // If no terminator, back iterator up to previous instruction.
602 if (MBBI == MBB->end())
603 MBBI = std::prev(MBBI);
604
605 if (MBBI != MBB->begin())
606 RS.forward(MBBI);
607 }
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000608
609 // If the two registers are available, we're all good.
610 // Note that we only return here if both R0 and R12 are available because
611 // although the function may not require two unique registers, it may benefit
612 // from having two so we should try to provide them.
613 if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12))
Kit Barton9c432ae2015-11-16 20:22:15 +0000614 return true;
615
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000616 // Get the list of callee-saved registers for the target.
Eric Christopherb128abc2017-02-04 01:52:17 +0000617 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000618 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent());
619
620 // Get all the available registers in the block.
621 BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass :
622 &PPC::GPRCRegClass);
623
624 // We shouldn't use callee-saved registers as scratch registers as they may be
625 // available when looking for a candidate block for shrink wrapping but not
626 // available when the actual prologue/epilogue is being emitted because they
627 // were added as live-in to the prologue block by PrologueEpilogueInserter.
628 for (int i = 0; CSRegs[i]; ++i)
629 BV.reset(CSRegs[i]);
630
631 // Set the first scratch register to the first available one.
632 if (SR1) {
633 int FirstScratchReg = BV.find_first();
634 *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
635 }
636
637 // If there is another one available, set the second scratch register to that.
638 // Otherwise, set it to either PPC::NoRegister if this function requires two
639 // or to whatever SR1 is set to if this function doesn't require two.
640 if (SR2) {
641 int SecondScratchReg = BV.find_next(*SR1);
642 if (SecondScratchReg != -1)
643 *SR2 = SecondScratchReg;
644 else
645 *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1;
646 }
647
648 // Now that we've done our best to provide both registers, double check
649 // whether we were unable to provide enough.
Aaron Ballman8374c1f2016-02-23 15:02:43 +0000650 if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
Kit Barton9c432ae2015-11-16 20:22:15 +0000651 return false;
652
Kit Barton9c432ae2015-11-16 20:22:15 +0000653 return true;
654}
655
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000656// We need a scratch register for spilling LR and for spilling CR. By default,
657// we use two scratch registers to hide latency. However, if only one scratch
658// register is available, we can adjust for that by not overlapping the spill
659// code. However, if we need to realign the stack (i.e. have a base pointer)
660// and the stack frame is large, we need two scratch registers.
661bool
662PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
Eric Christopherb128abc2017-02-04 01:52:17 +0000663 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000664 MachineFunction &MF = *(MBB->getParent());
665 bool HasBP = RegInfo->hasBasePointer(MF);
666 unsigned FrameSize = determineFrameLayout(MF, false);
667 int NegFrameSize = -FrameSize;
668 bool IsLargeFrame = !isInt<16>(NegFrameSize);
Matthias Braun941a7052016-07-28 18:40:00 +0000669 MachineFrameInfo &MFI = MF.getFrameInfo();
670 unsigned MaxAlign = MFI.getMaxAlignment();
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000671 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000672
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000673 return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000674}
675
Kit Barton9c432ae2015-11-16 20:22:15 +0000676bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
677 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
678
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000679 return findScratchRegister(TmpMBB, false,
680 twoUniqueScratchRegsRequired(TmpMBB));
Kit Barton9c432ae2015-11-16 20:22:15 +0000681}
682
683bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
684 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
685
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000686 return findScratchRegister(TmpMBB, true);
Kit Barton9c432ae2015-11-16 20:22:15 +0000687}
688
Quentin Colombet61b305e2015-05-05 17:38:16 +0000689void PPCFrameLowering::emitPrologue(MachineFunction &MF,
690 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000691 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000692 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherb128abc2017-02-04 01:52:17 +0000693 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
694 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000695
696 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000697 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000698 DebugLoc dl;
Jay Foad1f0a44e2014-12-01 09:42:32 +0000699 bool needsCFI = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000700 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000701
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000702 // Get processor type.
703 bool isPPC64 = Subtarget.isPPC64();
704 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000705 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000706 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chandler Carruth003ed332015-02-14 09:14:44 +0000707 assert((Subtarget.isDarwinABI() || isSVR4ABI) &&
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000708 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
709
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000710 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
711 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000712 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000713 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
714 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000715 HandleVRSaveUpdate(*MBBI, TII);
Bill Schmidt38d94582012-10-10 20:54:15 +0000716 break;
717 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000718 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000719
Kit Bartond3b904d2015-09-10 01:55:44 +0000720 // Move MBBI back to the beginning of the prologue block.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000721 MBBI = MBB.begin();
722
723 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000724 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000725 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000726 if (!isInt<32>(NegFrameSize))
727 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000728
Matthias Braun941a7052016-07-28 18:40:00 +0000729 if (MFI.isFrameAddressTaken())
Hal Finkelaa03c032013-03-21 19:03:19 +0000730 replaceFPWithRealFP(MF);
731
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000732 // Check if the link register (LR) must be saved.
733 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
734 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000735 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000736 bool MustSaveCR = !MustSaveCRs.empty();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000737 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000738 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000739 bool HasBP = RegInfo->hasBasePointer(MF);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000740 bool HasRedZone = isPPC64 || !isSVR4ABI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000741
Bill Schmidtf381afc2013-08-20 03:12:23 +0000742 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000743 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000744 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
745 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
Kit Barton9c432ae2015-11-16 20:22:15 +0000746 unsigned ScratchReg = 0;
Bill Schmidtf381afc2013-08-20 03:12:23 +0000747 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
748 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
749 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
750 : PPC::MFLR );
751 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
752 : PPC::STW );
753 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
754 : PPC::STWU );
755 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
756 : PPC::STWUX);
757 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
758 : PPC::LIS );
759 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
760 : PPC::ORI );
761 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
762 : PPC::OR );
763 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
764 : PPC::SUBFC);
765 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
766 : PPC::SUBFIC);
767
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000768 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
769 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
770 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
771 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
772 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
773 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
774
Simon Pilgrimfbd22212016-11-20 13:10:51 +0000775 // Using the same bool variable as below to suppress compiler warnings.
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +0000776 bool SingleScratchReg =
777 findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB),
778 &ScratchReg, &TempReg);
779 assert(SingleScratchReg &&
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000780 "Required number of registers not available in this block");
781
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +0000782 SingleScratchReg = ScratchReg == TempReg;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000783
Eric Christopherf71609b2015-02-13 00:39:27 +0000784 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000785
786 int FPOffset = 0;
787 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000788 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +0000789 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000790 int FPIndex = FI->getFramePointerSaveIndex();
791 assert(FPIndex && "No Frame Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000792 FPOffset = MFI.getObjectOffset(FPIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000793 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000794 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000795 }
796 }
797
Hal Finkela7c54e82013-07-17 00:45:52 +0000798 int BPOffset = 0;
799 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000800 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +0000801 MachineFrameInfo &MFI = MF.getFrameInfo();
Hal Finkela7c54e82013-07-17 00:45:52 +0000802 int BPIndex = FI->getBasePointerSaveIndex();
803 assert(BPIndex && "No Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000804 BPOffset = MFI.getObjectOffset(BPIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000805 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +0000806 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +0000807 }
808 }
809
Justin Hibbits654346e2015-01-10 01:57:21 +0000810 int PBPOffset = 0;
811 if (FI->usesPICBase()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000812 MachineFrameInfo &MFI = MF.getFrameInfo();
Justin Hibbits654346e2015-01-10 01:57:21 +0000813 int PBPIndex = FI->getPICBasePointerSaveIndex();
814 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000815 PBPOffset = MFI.getObjectOffset(PBPIndex);
Justin Hibbits654346e2015-01-10 01:57:21 +0000816 }
817
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000818 // Get stack alignments.
Matthias Braun941a7052016-07-28 18:40:00 +0000819 unsigned MaxAlign = MFI.getMaxAlignment();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000820 if (HasBP && MaxAlign > 1)
821 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
822 "Invalid alignment!");
823
824 // Frames of 32KB & larger require special handling because they cannot be
825 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
826 bool isLargeFrame = !isInt<16>(NegFrameSize);
827
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000828 assert((isPPC64 || !MustSaveCR) &&
829 "Prologue CR saving supported only in 64-bit mode");
830
831 // If we need to spill the CR and the LR but we don't have two separate
832 // registers available, we must spill them one at a time
833 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000834 // In the ELFv2 ABI, we are not required to save all CR fields.
835 // If only one or two CR fields are clobbered, it is more efficient to use
836 // mfocrf to selectively save just those fields, because mfocrf has short
837 // latency compares to mfcr.
838 unsigned MfcrOpcode = PPC::MFCR8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000839 unsigned CrState = RegState::ImplicitKill;
840 if (isELFv2ABI && MustSaveCRs.size() == 1) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000841 MfcrOpcode = PPC::MFOCRF8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000842 CrState = RegState::Kill;
843 }
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000844 MachineInstrBuilder MIB =
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000845 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000846 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000847 MIB.addReg(MustSaveCRs[i], CrState);
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000848 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
849 .addReg(TempReg, getKillRegState(true))
850 .addImm(8)
851 .addReg(SPReg);
852 }
853
Bill Schmidtf381afc2013-08-20 03:12:23 +0000854 if (MustSaveLR)
855 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000856
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000857 if (MustSaveCR &&
858 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000859 // In the ELFv2 ABI, we are not required to save all CR fields.
860 // If only one or two CR fields are clobbered, it is more efficient to use
861 // mfocrf to selectively save just those fields, because mfocrf has short
862 // latency compares to mfcr.
863 unsigned MfcrOpcode = PPC::MFCR8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000864 unsigned CrState = RegState::ImplicitKill;
865 if (isELFv2ABI && MustSaveCRs.size() == 1) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000866 MfcrOpcode = PPC::MFOCRF8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000867 CrState = RegState::Kill;
868 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000869 MachineInstrBuilder MIB =
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000870 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000871 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000872 MIB.addReg(MustSaveCRs[i], CrState);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000873 }
874
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000875 if (HasRedZone) {
876 if (HasFP)
877 BuildMI(MBB, MBBI, dl, StoreInst)
878 .addReg(FPReg)
879 .addImm(FPOffset)
880 .addReg(SPReg);
881 if (FI->usesPICBase())
882 BuildMI(MBB, MBBI, dl, StoreInst)
883 .addReg(PPC::R30)
884 .addImm(PBPOffset)
885 .addReg(SPReg);
886 if (HasBP)
887 BuildMI(MBB, MBBI, dl, StoreInst)
888 .addReg(BPReg)
889 .addImm(BPOffset)
890 .addReg(SPReg);
891 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000892
893 if (MustSaveLR)
Bill Schmidtf381afc2013-08-20 03:12:23 +0000894 BuildMI(MBB, MBBI, dl, StoreInst)
Nemanja Ivanovic62fba482016-07-15 19:56:32 +0000895 .addReg(ScratchReg, getKillRegState(true))
Bill Schmidtf381afc2013-08-20 03:12:23 +0000896 .addImm(LROffset)
897 .addReg(SPReg);
898
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000899 if (MustSaveCR &&
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000900 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
901 assert(HasRedZone && "A red zone is always available on PPC64");
Bill Schmidtf381afc2013-08-20 03:12:23 +0000902 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
903 .addReg(TempReg, getKillRegState(true))
904 .addImm(8)
905 .addReg(SPReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000906 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000907
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000908 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000909 if (!FrameSize)
910 return;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000911
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000912 // Adjust stack pointer: r1 += NegFrameSize.
913 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000914
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000915 if (HasBP && HasRedZone) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000916 // Save a copy of r1 as the base pointer.
917 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
918 .addReg(SPReg)
919 .addReg(SPReg);
920 }
921
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000922 // Have we generated a STUX instruction to claim stack frame? If so,
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +0000923 // the negated frame size will be placed in ScratchReg.
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000924 bool HasSTUX = false;
925
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000926 // This condition must be kept in sync with canUseAsPrologue.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000927 if (HasBP && MaxAlign > 1) {
928 if (isPPC64)
929 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
930 .addReg(SPReg)
931 .addImm(0)
932 .addImm(64 - Log2_32(MaxAlign));
933 else // PPC32...
934 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
935 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000936 .addImm(0)
937 .addImm(32 - Log2_32(MaxAlign))
938 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000939 if (!isLargeFrame) {
940 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
941 .addReg(ScratchReg, RegState::Kill)
942 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000943 } else {
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000944 assert(!SingleScratchReg && "Only a single scratch reg available");
Bill Schmidtf381afc2013-08-20 03:12:23 +0000945 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000946 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000947 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
948 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000949 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000950 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
951 .addReg(ScratchReg, RegState::Kill)
952 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000953 }
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000954
Bill Schmidtf381afc2013-08-20 03:12:23 +0000955 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
956 .addReg(SPReg, RegState::Kill)
957 .addReg(SPReg)
958 .addReg(ScratchReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000959 HasSTUX = true;
Hal Finkela7c54e82013-07-17 00:45:52 +0000960
Bill Schmidtf381afc2013-08-20 03:12:23 +0000961 } else if (!isLargeFrame) {
962 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
963 .addReg(SPReg)
964 .addImm(NegFrameSize)
965 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000966
Bill Schmidtf381afc2013-08-20 03:12:23 +0000967 } else {
968 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
969 .addImm(NegFrameSize >> 16);
970 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
971 .addReg(ScratchReg, RegState::Kill)
972 .addImm(NegFrameSize & 0xFFFF);
973 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
974 .addReg(SPReg, RegState::Kill)
975 .addReg(SPReg)
976 .addReg(ScratchReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000977 HasSTUX = true;
978 }
979
980 if (!HasRedZone) {
981 assert(!isPPC64 && "A red zone is always available on PPC64");
982 if (HasSTUX) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +0000983 // The negated frame size is in ScratchReg, and the SPReg has been
984 // decremented by the frame size: SPReg = old SPReg + ScratchReg.
985 // Since FPOffset, PBPOffset, etc. are relative to the beginning of
986 // the stack frame (i.e. the old SP), ideally, we would put the old
987 // SP into a register and use it as the base for the stores. The
988 // problem is that the only available register may be ScratchReg,
989 // which could be R0, and R0 cannot be used as a base address.
990
991 // First, set ScratchReg to the old SP. This may need to be modified
992 // later.
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000993 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg)
994 .addReg(ScratchReg, RegState::Kill)
995 .addReg(SPReg);
996
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +0000997 if (ScratchReg == PPC::R0) {
998 // R0 cannot be used as a base register, but it can be used as an
999 // index in a store-indexed.
1000 int LastOffset = 0;
1001 if (HasFP) {
1002 // R0 += (FPOffset-LastOffset).
1003 // Need addic, since addi treats R0 as 0.
1004 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1005 .addReg(ScratchReg)
1006 .addImm(FPOffset-LastOffset);
1007 LastOffset = FPOffset;
1008 // Store FP into *R0.
1009 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1010 .addReg(FPReg, RegState::Kill) // Save FP.
1011 .addReg(PPC::ZERO)
1012 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1013 }
1014 if (FI->usesPICBase()) {
1015 // R0 += (PBPOffset-LastOffset).
1016 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1017 .addReg(ScratchReg)
1018 .addImm(PBPOffset-LastOffset);
1019 LastOffset = PBPOffset;
1020 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1021 .addReg(PPC::R30, RegState::Kill) // Save PIC base pointer.
1022 .addReg(PPC::ZERO)
1023 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1024 }
1025 if (HasBP) {
1026 // R0 += (BPOffset-LastOffset).
1027 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1028 .addReg(ScratchReg)
1029 .addImm(BPOffset-LastOffset);
1030 LastOffset = BPOffset;
1031 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1032 .addReg(BPReg, RegState::Kill) // Save BP.
1033 .addReg(PPC::ZERO)
1034 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1035 // BP = R0-LastOffset
1036 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), BPReg)
1037 .addReg(ScratchReg, RegState::Kill)
1038 .addImm(-LastOffset);
1039 }
1040 } else {
1041 // ScratchReg is not R0, so use it as the base register. It is
1042 // already set to the old SP, so we can use the offsets directly.
1043
1044 // Now that the stack frame has been allocated, save all the necessary
1045 // registers using ScratchReg as the base address.
1046 if (HasFP)
1047 BuildMI(MBB, MBBI, dl, StoreInst)
1048 .addReg(FPReg)
1049 .addImm(FPOffset)
1050 .addReg(ScratchReg);
1051 if (FI->usesPICBase())
1052 BuildMI(MBB, MBBI, dl, StoreInst)
1053 .addReg(PPC::R30)
1054 .addImm(PBPOffset)
1055 .addReg(ScratchReg);
1056 if (HasBP) {
1057 BuildMI(MBB, MBBI, dl, StoreInst)
1058 .addReg(BPReg)
1059 .addImm(BPOffset)
1060 .addReg(ScratchReg);
1061 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
1062 .addReg(ScratchReg, RegState::Kill)
1063 .addReg(ScratchReg);
1064 }
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +00001065 }
1066 } else {
1067 // The frame size is a known 16-bit constant (fitting in the immediate
1068 // field of STWU). To be here we have to be compiling for PPC32.
1069 // Since the SPReg has been decreased by FrameSize, add it back to each
1070 // offset.
1071 if (HasFP)
1072 BuildMI(MBB, MBBI, dl, StoreInst)
1073 .addReg(FPReg)
1074 .addImm(FrameSize + FPOffset)
1075 .addReg(SPReg);
1076 if (FI->usesPICBase())
1077 BuildMI(MBB, MBBI, dl, StoreInst)
1078 .addReg(PPC::R30)
1079 .addImm(FrameSize + PBPOffset)
1080 .addReg(SPReg);
1081 if (HasBP) {
1082 BuildMI(MBB, MBBI, dl, StoreInst)
1083 .addReg(BPReg)
1084 .addImm(FrameSize + BPOffset)
1085 .addReg(SPReg);
1086 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg)
1087 .addReg(SPReg)
1088 .addImm(FrameSize);
1089 }
1090 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001091 }
1092
Jay Foad1f0a44e2014-12-01 09:42:32 +00001093 // Add Call Frame Information for the instructions we generated above.
1094 if (needsCFI) {
1095 unsigned CFIIndex;
1096
1097 if (HasBP) {
1098 // Define CFA in terms of BP. Do this in preference to using FP/SP,
1099 // because if the stack needed aligning then CFA won't be at a fixed
1100 // offset from FP/SP.
1101 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001102 CFIIndex = MF.addFrameInst(
Jay Foad1f0a44e2014-12-01 09:42:32 +00001103 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1104 } else {
1105 // Adjust the definition of CFA to account for the change in SP.
1106 assert(NegFrameSize);
Matthias Braunf23ef432016-11-30 23:48:42 +00001107 CFIIndex = MF.addFrameInst(
Jay Foad1f0a44e2014-12-01 09:42:32 +00001108 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
1109 }
Eric Christopher612bb692014-04-29 00:16:46 +00001110 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1111 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001112
1113 if (HasFP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001114 // Describe where FP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001115 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001116 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001117 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001118 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001119 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001120 }
1121
Justin Hibbits654346e2015-01-10 01:57:21 +00001122 if (FI->usesPICBase()) {
1123 // Describe where FP was saved, at a fixed offset from CFA.
1124 unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001125 CFIIndex = MF.addFrameInst(
Justin Hibbits654346e2015-01-10 01:57:21 +00001126 MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
1127 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1128 .addCFIIndex(CFIIndex);
1129 }
1130
Hal Finkela7c54e82013-07-17 00:45:52 +00001131 if (HasBP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001132 // Describe where BP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001133 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001134 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001135 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001136 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001137 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +00001138 }
1139
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001140 if (MustSaveLR) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001141 // Describe where LR was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001142 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001143 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001144 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001145 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001146 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001147 }
1148 }
1149
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001150 // If there is a frame pointer, copy R1 into R31
1151 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001152 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1153 .addReg(SPReg)
1154 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001155
Jay Foad1f0a44e2014-12-01 09:42:32 +00001156 if (!HasBP && needsCFI) {
1157 // Change the definition of CFA from SP+offset to FP+offset, because SP
1158 // will change at every alloca.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001159 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001160 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001161 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1162
Eric Christopher612bb692014-04-29 00:16:46 +00001163 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001164 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001165 }
1166 }
1167
Jay Foad1f0a44e2014-12-01 09:42:32 +00001168 if (needsCFI) {
1169 // Describe where callee saved registers were saved, at fixed offsets from
1170 // CFA.
Matthias Braun941a7052016-07-28 18:40:00 +00001171 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001172 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001173 unsigned Reg = CSI[I].getReg();
1174 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +00001175
1176 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1177 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +00001178 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +00001179 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +00001180
Roman Divackyc9e23d92012-09-12 14:47:47 +00001181 // For SVR4, don't emit a move for the CR spill slot if we haven't
1182 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001183 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001184 && !MustSaveCR)
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001185 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001186
1187 // For 64-bit SVR4 when we have spilled CRs, the spill location
1188 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001189 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Ulrich Weigandbe928cc2014-07-21 00:03:18 +00001190 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
1191 // the whole CR word. In the ELFv2 ABI, every CR that was
1192 // actually saved gets its own CFI record.
1193 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
Matthias Braunf23ef432016-11-30 23:48:42 +00001194 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Ulrich Weigandbe928cc2014-07-21 00:03:18 +00001195 nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +00001196 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001197 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001198 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001199 }
1200
Matthias Braun941a7052016-07-28 18:40:00 +00001201 int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx());
Matthias Braunf23ef432016-11-30 23:48:42 +00001202 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001203 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +00001204 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001205 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001206 }
1207 }
1208}
1209
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001210void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Kit Bartond3b904d2015-09-10 01:55:44 +00001211 MachineBasicBlock &MBB) const {
1212 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1213 DebugLoc dl;
1214
1215 if (MBBI != MBB.end())
1216 dl = MBBI->getDebugLoc();
1217
Eric Christopherb128abc2017-02-04 01:52:17 +00001218 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1219 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001220
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001221 // Get alignment info so we know how to restore the SP.
Matthias Braun941a7052016-07-28 18:40:00 +00001222 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001223
1224 // Get the number of bytes allocated from the FrameInfo.
Matthias Braun941a7052016-07-28 18:40:00 +00001225 int FrameSize = MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001226
1227 // Get processor type.
1228 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001229 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001230 bool isSVR4ABI = Subtarget.isSVR4ABI();
1231
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001232 // Check if the link register (LR) has been saved.
1233 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1234 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +00001235 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001236 bool MustSaveCR = !MustSaveCRs.empty();
Bill Schmidtf381afc2013-08-20 03:12:23 +00001237 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001238 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +00001239 bool HasBP = RegInfo->hasBasePointer(MF);
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001240 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001241
Bill Schmidtf381afc2013-08-20 03:12:23 +00001242 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +00001243 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001244 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
Kit Barton9c432ae2015-11-16 20:22:15 +00001245 unsigned ScratchReg = 0;
Bill Schmidtf381afc2013-08-20 03:12:23 +00001246 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1247 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
1248 : PPC::MTLR );
1249 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
1250 : PPC::LWZ );
1251 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
1252 : PPC::LIS );
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001253 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
1254 : PPC::OR );
Bill Schmidtf381afc2013-08-20 03:12:23 +00001255 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
1256 : PPC::ORI );
1257 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
1258 : PPC::ADDI );
1259 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
1260 : PPC::ADD4 );
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001261
Eric Christopherf71609b2015-02-13 00:39:27 +00001262 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001263
1264 int FPOffset = 0;
Kit Barton9c432ae2015-11-16 20:22:15 +00001265
Simon Pilgrimfbd22212016-11-20 13:10:51 +00001266 // Using the same bool variable as below to suppress compiler warnings.
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +00001267 bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg,
1268 &TempReg);
1269 assert(SingleScratchReg &&
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001270 "Could not find an available scratch register");
1271
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +00001272 SingleScratchReg = ScratchReg == TempReg;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001273
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001274 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001275 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001276 int FPIndex = FI->getFramePointerSaveIndex();
1277 assert(FPIndex && "No Frame Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001278 FPOffset = MFI.getObjectOffset(FPIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001279 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001280 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001281 }
1282 }
1283
Hal Finkela7c54e82013-07-17 00:45:52 +00001284 int BPOffset = 0;
1285 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001286 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +00001287 int BPIndex = FI->getBasePointerSaveIndex();
1288 assert(BPIndex && "No Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001289 BPOffset = MFI.getObjectOffset(BPIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +00001290 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001291 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001292 }
1293 }
1294
Justin Hibbits654346e2015-01-10 01:57:21 +00001295 int PBPOffset = 0;
1296 if (FI->usesPICBase()) {
Justin Hibbits654346e2015-01-10 01:57:21 +00001297 int PBPIndex = FI->getPICBasePointerSaveIndex();
1298 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001299 PBPOffset = MFI.getObjectOffset(PBPIndex);
Justin Hibbits654346e2015-01-10 01:57:21 +00001300 }
1301
NAKAMURA Takumi8061e862015-09-11 08:20:56 +00001302 bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
Kit Bartond3b904d2015-09-10 01:55:44 +00001303
1304 if (IsReturnBlock) {
1305 unsigned RetOpcode = MBBI->getOpcode();
1306 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1307 RetOpcode == PPC::TCRETURNdi ||
1308 RetOpcode == PPC::TCRETURNai ||
1309 RetOpcode == PPC::TCRETURNri8 ||
1310 RetOpcode == PPC::TCRETURNdi8 ||
1311 RetOpcode == PPC::TCRETURNai8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001312
Kit Bartond3b904d2015-09-10 01:55:44 +00001313 if (UsesTCRet) {
1314 int MaxTCRetDelta = FI->getTailCallSPDelta();
1315 MachineOperand &StackAdjust = MBBI->getOperand(1);
1316 assert(StackAdjust.isImm() && "Expecting immediate value.");
1317 // Adjust stack pointer.
1318 int StackAdj = StackAdjust.getImm();
1319 int Delta = StackAdj - MaxTCRetDelta;
1320 assert((Delta >= 0) && "Delta must be positive");
1321 if (MaxTCRetDelta>0)
1322 FrameSize += (StackAdj +Delta);
1323 else
1324 FrameSize += StackAdj;
1325 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001326 }
1327
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001328 // Frames of 32KB & larger require special handling because they cannot be
1329 // indexed into with a simple LD/LWZ immediate offset operand.
1330 bool isLargeFrame = !isInt<16>(FrameSize);
1331
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001332 // On targets without red zone, the SP needs to be restored last, so that
1333 // all live contents of the stack frame are upwards of the SP. This means
1334 // that we cannot restore SP just now, since there may be more registers
1335 // to restore from the stack frame (e.g. R31). If the frame size is not
1336 // a simple immediate value, we will need a spare register to hold the
1337 // restored SP. If the frame size is known and small, we can simply adjust
1338 // the offsets of the registers to be restored, and still use SP to restore
1339 // them. In such case, the final update of SP will be to add the frame
1340 // size to it.
1341 // To simplify the code, set RBReg to the base register used to restore
1342 // values from the stack, and set SPAdd to the value that needs to be added
1343 // to the SP at the end. The default values are as if red zone was present.
1344 unsigned RBReg = SPReg;
1345 unsigned SPAdd = 0;
1346
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001347 if (FrameSize) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001348 // In the prologue, the loaded (or persistent) stack pointer value is
1349 // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red
1350 // zone add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001351
1352 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1353 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1354 // call which invalidates the stack pointer value in SP(0). So we use the
1355 // value of R31 in this case.
1356 if (FI->hasFastCall()) {
1357 assert(HasFP && "Expecting a valid frame pointer.");
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001358 if (!HasRedZone)
1359 RBReg = FPReg;
Bill Schmidtf381afc2013-08-20 03:12:23 +00001360 if (!isLargeFrame) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001361 BuildMI(MBB, MBBI, dl, AddImmInst, RBReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001362 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001363 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001364 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1365 .addImm(FrameSize >> 16);
1366 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1367 .addReg(ScratchReg, RegState::Kill)
1368 .addImm(FrameSize & 0xFFFF);
1369 BuildMI(MBB, MBBI, dl, AddInst)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001370 .addReg(RBReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001371 .addReg(FPReg)
1372 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001373 }
Matthias Braun941a7052016-07-28 18:40:00 +00001374 } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001375 if (HasRedZone) {
1376 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1377 .addReg(SPReg)
1378 .addImm(FrameSize);
1379 } else {
1380 // Make sure that adding FrameSize will not overflow the max offset
1381 // size.
1382 assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 &&
1383 "Local offsets should be negative");
1384 SPAdd = FrameSize;
1385 FPOffset += FrameSize;
1386 BPOffset += FrameSize;
1387 PBPOffset += FrameSize;
1388 }
Bill Schmidtf381afc2013-08-20 03:12:23 +00001389 } else {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001390 // We don't want to use ScratchReg as a base register, because it
1391 // could happen to be R0. Use FP instead, but make sure to preserve it.
1392 if (!HasRedZone) {
1393 // If FP is not saved, copy it to ScratchReg.
1394 if (!HasFP)
1395 BuildMI(MBB, MBBI, dl, OrInst, ScratchReg)
1396 .addReg(FPReg)
1397 .addReg(FPReg);
1398 RBReg = FPReg;
1399 }
1400 BuildMI(MBB, MBBI, dl, LoadInst, RBReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001401 .addImm(0)
1402 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001403 }
1404 }
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001405 assert(RBReg != ScratchReg && "Should have avoided ScratchReg");
1406 // If there is no red zone, ScratchReg may be needed for holding a useful
1407 // value (although not the base register). Make sure it is not overwritten
1408 // too early.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001409
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001410 assert((isPPC64 || !MustSaveCR) &&
1411 "Epilogue CR restoring supported only in 64-bit mode");
1412
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001413 // If we need to restore both the LR and the CR and we only have one
1414 // available scratch register, we must do them one at a time.
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001415 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001416 // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg
1417 // is live here.
1418 assert(HasRedZone && "Expecting red zone");
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001419 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1420 .addImm(8)
1421 .addReg(SPReg);
1422 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1423 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1424 .addReg(TempReg, getKillRegState(i == e-1));
1425 }
1426
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001427 // Delay restoring of the LR if ScratchReg is needed. This is ok, since
1428 // LR is stored in the caller's stack frame. ScratchReg will be needed
1429 // if RBReg is anything other than SP. We shouldn't use ScratchReg as
1430 // a base register anyway, because it may happen to be R0.
1431 bool LoadedLR = false;
1432 if (MustSaveLR && RBReg == SPReg && isInt<16>(LROffset+SPAdd)) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001433 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001434 .addImm(LROffset+SPAdd)
1435 .addReg(RBReg);
1436 LoadedLR = true;
1437 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001438
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001439 if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) {
1440 // This will only occur for PPC64.
1441 assert(isPPC64 && "Expecting 64-bit mode");
1442 assert(RBReg == SPReg && "Should be using SP as a base register");
Bill Schmidtf381afc2013-08-20 03:12:23 +00001443 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1444 .addImm(8)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001445 .addReg(RBReg);
1446 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001447
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001448 if (HasFP) {
1449 // If there is red zone, restore FP directly, since SP has already been
1450 // restored. Otherwise, restore the value of FP into ScratchReg.
1451 if (HasRedZone || RBReg == SPReg)
1452 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1453 .addImm(FPOffset)
1454 .addReg(SPReg);
1455 else
1456 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1457 .addImm(FPOffset)
1458 .addReg(RBReg);
1459 }
Hal Finkela7c54e82013-07-17 00:45:52 +00001460
Justin Hibbits654346e2015-01-10 01:57:21 +00001461 if (FI->usesPICBase())
Justin Hibbits98a532d2015-01-08 15:47:19 +00001462 BuildMI(MBB, MBBI, dl, LoadInst)
1463 .addReg(PPC::R30)
Justin Hibbits654346e2015-01-10 01:57:21 +00001464 .addImm(PBPOffset)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001465 .addReg(RBReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +00001466
Bill Schmidtf381afc2013-08-20 03:12:23 +00001467 if (HasBP)
1468 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1469 .addImm(BPOffset)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001470 .addReg(RBReg);
1471
1472 // There is nothing more to be loaded from the stack, so now we can
1473 // restore SP: SP = RBReg + SPAdd.
1474 if (RBReg != SPReg || SPAdd != 0) {
1475 assert(!HasRedZone && "This should not happen with red zone");
1476 // If SPAdd is 0, generate a copy.
1477 if (SPAdd == 0)
1478 BuildMI(MBB, MBBI, dl, OrInst, SPReg)
1479 .addReg(RBReg)
1480 .addReg(RBReg);
1481 else
1482 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1483 .addReg(RBReg)
1484 .addImm(SPAdd);
1485
1486 assert(RBReg != ScratchReg && "Should be using FP or SP as base register");
1487 if (RBReg == FPReg)
1488 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1489 .addReg(ScratchReg)
1490 .addReg(ScratchReg);
1491
1492 // Now load the LR from the caller's stack frame.
1493 if (MustSaveLR && !LoadedLR)
1494 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1495 .addImm(LROffset)
1496 .addReg(SPReg);
1497 }
Hal Finkel67369882013-04-15 02:07:05 +00001498
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001499 if (MustSaveCR &&
1500 !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
Bill Schmidtf381afc2013-08-20 03:12:23 +00001501 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1502 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1503 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001504
Bill Schmidtf381afc2013-08-20 03:12:23 +00001505 if (MustSaveLR)
1506 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001507
1508 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1509 // call optimization
Kit Bartond3b904d2015-09-10 01:55:44 +00001510 if (IsReturnBlock) {
1511 unsigned RetOpcode = MBBI->getOpcode();
1512 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1513 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
1514 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1515 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1516 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001517
Kit Bartond3b904d2015-09-10 01:55:44 +00001518 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1519 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1520 .addReg(SPReg).addImm(CallerAllocatedAmt);
1521 } else {
1522 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001523 .addImm(CallerAllocatedAmt >> 16);
Kit Bartond3b904d2015-09-10 01:55:44 +00001524 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001525 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001526 .addImm(CallerAllocatedAmt & 0xFFFF);
Kit Bartond3b904d2015-09-10 01:55:44 +00001527 BuildMI(MBB, MBBI, dl, AddInst)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001528 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001529 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001530 .addReg(ScratchReg);
Kit Bartond3b904d2015-09-10 01:55:44 +00001531 }
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001532 } else {
1533 createTailCallBranchInstr(MBB);
Kit Bartond3b904d2015-09-10 01:55:44 +00001534 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001535 }
1536}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001537
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001538void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
1539 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1540 DebugLoc dl;
1541
1542 if (MBBI != MBB.end())
1543 dl = MBBI->getDebugLoc();
1544
Eric Christopherb128abc2017-02-04 01:52:17 +00001545 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001546
1547 // Create branch instruction for pseudo tail call return instruction
1548 unsigned RetOpcode = MBBI->getOpcode();
1549 if (RetOpcode == PPC::TCRETURNdi) {
1550 MBBI = MBB.getLastNonDebugInstr();
1551 MachineOperand &JumpTarget = MBBI->getOperand(0);
1552 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1553 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1554 } else if (RetOpcode == PPC::TCRETURNri) {
1555 MBBI = MBB.getLastNonDebugInstr();
1556 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1557 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1558 } else if (RetOpcode == PPC::TCRETURNai) {
1559 MBBI = MBB.getLastNonDebugInstr();
1560 MachineOperand &JumpTarget = MBBI->getOperand(0);
1561 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1562 } else if (RetOpcode == PPC::TCRETURNdi8) {
1563 MBBI = MBB.getLastNonDebugInstr();
1564 MachineOperand &JumpTarget = MBBI->getOperand(0);
1565 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1566 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1567 } else if (RetOpcode == PPC::TCRETURNri8) {
1568 MBBI = MBB.getLastNonDebugInstr();
1569 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1570 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1571 } else if (RetOpcode == PPC::TCRETURNai8) {
1572 MBBI = MBB.getLastNonDebugInstr();
1573 MachineOperand &JumpTarget = MBBI->getOperand(0);
1574 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1575 }
1576}
1577
Matthias Braun02564862015-07-14 17:17:13 +00001578void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1579 BitVector &SavedRegs,
1580 RegScavenger *RS) const {
1581 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1582
Eric Christopherb128abc2017-02-04 01:52:17 +00001583 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001584
1585 // Save and clear the LR state.
1586 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1587 unsigned LR = RegInfo->getRARegister();
1588 FI->setMustSaveLR(MustSaveLR(MF, LR));
Matthias Braun02564862015-07-14 17:17:13 +00001589 SavedRegs.reset(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001590
1591 // Save R31 if necessary
1592 int FPSI = FI->getFramePointerSaveIndex();
1593 bool isPPC64 = Subtarget.isPPC64();
1594 bool isDarwinABI = Subtarget.isDarwinABI();
Matthias Braun941a7052016-07-28 18:40:00 +00001595 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001596
1597 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001598 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001599 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001600 int FPOffset = getFramePointerSaveOffset();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001601 // Allocate the frame index for frame pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00001602 FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001603 // Save the result.
1604 FI->setFramePointerSaveIndex(FPSI);
1605 }
1606
Hal Finkela7c54e82013-07-17 00:45:52 +00001607 int BPSI = FI->getBasePointerSaveIndex();
1608 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001609 int BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001610 // Allocate the frame index for the base pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00001611 BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
Hal Finkela7c54e82013-07-17 00:45:52 +00001612 // Save the result.
1613 FI->setBasePointerSaveIndex(BPSI);
1614 }
1615
Justin Hibbits654346e2015-01-10 01:57:21 +00001616 // Reserve stack space for the PIC Base register (R30).
1617 // Only used in SVR4 32-bit.
1618 if (FI->usesPICBase()) {
Matthias Braun941a7052016-07-28 18:40:00 +00001619 int PBPSI = MFI.CreateFixedObject(4, -8, true);
Justin Hibbits654346e2015-01-10 01:57:21 +00001620 FI->setPICBasePointerSaveIndex(PBPSI);
1621 }
1622
Hal Finkel97a189c2016-08-31 00:52:03 +00001623 // Make sure we don't explicitly spill r31, because, for example, we have
1624 // some inline asm which explicity clobbers it, when we otherwise have a
1625 // frame pointer and are using r31's spill slot for the prologue/epilogue
1626 // code. Same goes for the base pointer and the PIC base register.
1627 if (needsFP(MF))
1628 SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31);
1629 if (RegInfo->hasBasePointer(MF))
1630 SavedRegs.reset(RegInfo->getBaseRegister(MF));
1631 if (FI->usesPICBase())
1632 SavedRegs.reset(PPC::R30);
1633
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001634 // Reserve stack space to move the linkage area to in case of a tail call.
1635 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001636 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1637 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Matthias Braun941a7052016-07-28 18:40:00 +00001638 MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001639 }
1640
Eric Christopherd1737492014-04-29 00:16:40 +00001641 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001642 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001643 if (!isPPC64 && !isDarwinABI &&
Matthias Braun02564862015-07-14 17:17:13 +00001644 (SavedRegs.test(PPC::CR2) ||
1645 SavedRegs.test(PPC::CR3) ||
1646 SavedRegs.test(PPC::CR4))) {
Matthias Braun941a7052016-07-28 18:40:00 +00001647 int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true);
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001648 FI->setCRSpillFrameIndex(FrameIdx);
1649 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001650}
1651
Hal Finkel5a765fd2013-03-14 20:33:40 +00001652void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001653 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001654 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001655 if (!Subtarget.isSVR4ABI()) {
1656 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001657 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001658 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001659
1660 // Get callee saved register information.
Matthias Braun941a7052016-07-28 18:40:00 +00001661 MachineFrameInfo &MFI = MF.getFrameInfo();
1662 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001663
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001664 // If the function is shrink-wrapped, and if the function has a tail call, the
1665 // tail call might not be in the new RestoreBlock, so real branch instruction
1666 // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
1667 // RestoreBlock. So we handle this case here.
Matthias Braun941a7052016-07-28 18:40:00 +00001668 if (MFI.getSavePoint() && MFI.hasTailCall()) {
1669 MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001670 for (MachineBasicBlock &MBB : MF) {
1671 if (MBB.isReturnBlock() && (&MBB) != RestoreBlock)
1672 createTailCallBranchInstr(MBB);
1673 }
1674 }
1675
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001676 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001677 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001678 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001679 return;
1680 }
1681
1682 unsigned MinGPR = PPC::R31;
1683 unsigned MinG8R = PPC::X31;
1684 unsigned MinFPR = PPC::F31;
1685 unsigned MinVR = PPC::V31;
1686
1687 bool HasGPSaveArea = false;
1688 bool HasG8SaveArea = false;
1689 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001690 bool HasVRSAVESaveArea = false;
1691 bool HasVRSaveArea = false;
1692
1693 SmallVector<CalleeSavedInfo, 18> GPRegs;
1694 SmallVector<CalleeSavedInfo, 18> G8Regs;
1695 SmallVector<CalleeSavedInfo, 18> FPRegs;
1696 SmallVector<CalleeSavedInfo, 18> VRegs;
1697
1698 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1699 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001700 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001701 HasGPSaveArea = true;
1702
1703 GPRegs.push_back(CSI[i]);
1704
1705 if (Reg < MinGPR) {
1706 MinGPR = Reg;
1707 }
Craig Topperabadc662012-04-20 06:31:50 +00001708 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001709 HasG8SaveArea = true;
1710
1711 G8Regs.push_back(CSI[i]);
1712
1713 if (Reg < MinG8R) {
1714 MinG8R = Reg;
1715 }
Craig Topperabadc662012-04-20 06:31:50 +00001716 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001717 HasFPSaveArea = true;
1718
1719 FPRegs.push_back(CSI[i]);
1720
1721 if (Reg < MinFPR) {
1722 MinFPR = Reg;
1723 }
Craig Topperabadc662012-04-20 06:31:50 +00001724 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1725 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001726 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001727 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001728 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001729 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001730 HasVRSaveArea = true;
1731
1732 VRegs.push_back(CSI[i]);
1733
1734 if (Reg < MinVR) {
1735 MinVR = Reg;
1736 }
1737 } else {
1738 llvm_unreachable("Unknown RegisterClass!");
1739 }
1740 }
1741
1742 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Eric Christopher38522b82015-01-30 02:11:26 +00001743 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001744
1745 int64_t LowerBound = 0;
1746
1747 // Take into account stack space reserved for tail calls.
1748 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001749 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1750 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001751 LowerBound = TCSPDelta;
1752 }
1753
1754 // The Floating-point register save area is right below the back chain word
1755 // of the previous stack frame.
1756 if (HasFPSaveArea) {
1757 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1758 int FI = FPRegs[i].getFrameIdx();
1759
Matthias Braun941a7052016-07-28 18:40:00 +00001760 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001761 }
1762
Hal Finkelfeea6532013-03-26 20:08:20 +00001763 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001764 }
1765
1766 // Check whether the frame pointer register is allocated. If so, make sure it
1767 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001768 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001769 HasGPSaveArea = true;
1770
1771 int FI = PFI->getFramePointerSaveIndex();
1772 assert(FI && "No Frame Pointer Save Slot!");
1773
Matthias Braun941a7052016-07-28 18:40:00 +00001774 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001775 }
1776
Justin Hibbits654346e2015-01-10 01:57:21 +00001777 if (PFI->usesPICBase()) {
1778 HasGPSaveArea = true;
1779
1780 int FI = PFI->getPICBasePointerSaveIndex();
1781 assert(FI && "No PIC Base Pointer Save Slot!");
1782
Matthias Braun941a7052016-07-28 18:40:00 +00001783 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Justin Hibbits654346e2015-01-10 01:57:21 +00001784 }
1785
Eric Christopherb128abc2017-02-04 01:52:17 +00001786 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Hal Finkela7c54e82013-07-17 00:45:52 +00001787 if (RegInfo->hasBasePointer(MF)) {
1788 HasGPSaveArea = true;
1789
1790 int FI = PFI->getBasePointerSaveIndex();
1791 assert(FI && "No Base Pointer Save Slot!");
1792
Matthias Braun941a7052016-07-28 18:40:00 +00001793 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Hal Finkela7c54e82013-07-17 00:45:52 +00001794 }
1795
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001796 // General register save area starts right below the Floating-point
1797 // register save area.
1798 if (HasGPSaveArea || HasG8SaveArea) {
1799 // Move general register save area spill slots down, taking into account
1800 // the size of the Floating-point register save area.
1801 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1802 int FI = GPRegs[i].getFrameIdx();
1803
Matthias Braun941a7052016-07-28 18:40:00 +00001804 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001805 }
1806
1807 // Move general register save area spill slots down, taking into account
1808 // the size of the Floating-point register save area.
1809 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1810 int FI = G8Regs[i].getFrameIdx();
1811
Matthias Braun941a7052016-07-28 18:40:00 +00001812 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001813 }
1814
1815 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001816 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1817 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001818
1819 if (Subtarget.isPPC64()) {
1820 LowerBound -= (31 - MinReg + 1) * 8;
1821 } else {
1822 LowerBound -= (31 - MinReg + 1) * 4;
1823 }
1824 }
1825
Roman Divackyc9e23d92012-09-12 14:47:47 +00001826 // For 32-bit only, the CR save area is below the general register
1827 // save area. For 64-bit SVR4, the CR save area is addressed relative
1828 // to the stack pointer and hence does not need an adjustment here.
1829 // Only CR2 (the first nonvolatile spilled) has an associated frame
1830 // index so that we have a single uniform save area.
1831 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001832 // Adjust the frame index of the CR spill slot.
1833 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1834 unsigned Reg = CSI[i].getReg();
1835
Roman Divackyc9e23d92012-09-12 14:47:47 +00001836 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001837 // Leave Darwin logic as-is.
1838 || (!Subtarget.isSVR4ABI() &&
1839 (PPC::CRBITRCRegClass.contains(Reg) ||
1840 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001841 int FI = CSI[i].getFrameIdx();
1842
Matthias Braun941a7052016-07-28 18:40:00 +00001843 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001844 }
1845 }
1846
1847 LowerBound -= 4; // The CR save area is always 4 bytes long.
1848 }
1849
1850 if (HasVRSAVESaveArea) {
1851 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1852 // which have the VRSAVE register class?
1853 // Adjust the frame index of the VRSAVE spill slot.
1854 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1855 unsigned Reg = CSI[i].getReg();
1856
Craig Topperabadc662012-04-20 06:31:50 +00001857 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001858 int FI = CSI[i].getFrameIdx();
1859
Matthias Braun941a7052016-07-28 18:40:00 +00001860 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001861 }
1862 }
1863
1864 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1865 }
1866
1867 if (HasVRSaveArea) {
1868 // Insert alignment padding, we need 16-byte alignment.
1869 LowerBound = (LowerBound - 15) & ~(15);
1870
1871 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1872 int FI = VRegs[i].getFrameIdx();
1873
Matthias Braun941a7052016-07-28 18:40:00 +00001874 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001875 }
1876 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001877
1878 addScavengingSpillSlot(MF, RS);
1879}
1880
1881void
1882PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1883 RegScavenger *RS) const {
1884 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1885 // a large stack, which will require scavenging a register to materialize a
1886 // large offset.
1887
1888 // We need to have a scavenger spill slot for spills if the frame size is
1889 // large. In case there is no free register for large-offset addressing,
1890 // this slot is used for the necessary emergency spill. Also, we need the
1891 // slot for dynamic stack allocations.
1892
1893 // The scavenger might be invoked if the frame offset does not fit into
1894 // the 16-bit immediate. We don't know the complete frame size here
1895 // because we've not yet computed callee-saved register spills or the
1896 // needed alignment padding.
1897 unsigned StackSize = determineFrameLayout(MF, false, true);
Matthias Braun941a7052016-07-28 18:40:00 +00001898 MachineFrameInfo &MFI = MF.getFrameInfo();
1899 if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001900 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001901 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1902 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1903 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Matthias Braun941a7052016-07-28 18:40:00 +00001904 RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(),
1905 RC->getAlignment(),
1906 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001907
Hal Finkel18607632013-07-18 04:28:21 +00001908 // Might we have over-aligned allocas?
Matthias Braun941a7052016-07-28 18:40:00 +00001909 bool HasAlVars = MFI.hasVarSizedObjects() &&
1910 MFI.getMaxAlignment() > getStackAlignment();
Hal Finkel18607632013-07-18 04:28:21 +00001911
Hal Finkel0dfbb052013-03-26 18:57:22 +00001912 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001913 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Matthias Braun941a7052016-07-28 18:40:00 +00001914 RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(),
1915 RC->getAlignment(),
1916 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001917
Hal Finkelbb420f12013-03-15 05:06:04 +00001918 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001919}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001920
Eric Christopherd1737492014-04-29 00:16:40 +00001921bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001922PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001923 MachineBasicBlock::iterator MI,
1924 const std::vector<CalleeSavedInfo> &CSI,
1925 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001926
1927 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1928 // Return false otherwise to maintain pre-existing behavior.
1929 if (!Subtarget.isSVR4ABI())
1930 return false;
1931
1932 MachineFunction *MF = MBB.getParent();
Eric Christopherb128abc2017-02-04 01:52:17 +00001933 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001934 DebugLoc DL;
1935 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001936 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001937
Roman Divackyc9e23d92012-09-12 14:47:47 +00001938 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1939 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001940 // Only Darwin actually uses the VRSAVE register, but it can still appear
1941 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1942 // Darwin, ignore it.
1943 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1944 continue;
1945
Roman Divackyc9e23d92012-09-12 14:47:47 +00001946 // CR2 through CR4 are the nonvolatile CR fields.
1947 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1948
Roman Divackyc9e23d92012-09-12 14:47:47 +00001949 // Add the callee-saved register as live-in; it's killed at the spill.
1950 MBB.addLiveIn(Reg);
1951
Hal Finkel2f293912013-04-13 23:06:15 +00001952 if (CRSpilled && IsCRField) {
1953 CRMIB.addReg(Reg, RegState::ImplicitKill);
1954 continue;
1955 }
1956
Roman Divackyc9e23d92012-09-12 14:47:47 +00001957 // Insert the spill to the stack frame.
1958 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001959 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001960 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001961 // The actual spill will happen at the start of the prologue.
1962 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001963 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001964 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001965 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001966
Eric Christopherd1737492014-04-29 00:16:40 +00001967 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1968 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1969 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001970 .addReg(Reg, RegState::ImplicitKill);
1971
Eric Christopherd1737492014-04-29 00:16:40 +00001972 MBB.insert(MI, CRMIB);
1973 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1974 .addReg(PPC::R12,
1975 getKillRegState(true)),
1976 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001977 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001978 } else {
1979 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1980 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001981 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001982 }
1983 }
1984 return true;
1985}
1986
1987static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001988restoreCRs(bool isPPC64, bool is31,
1989 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001990 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1991 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001992
1993 MachineFunction *MF = MBB.getParent();
Eric Christophercccae792015-01-30 22:02:31 +00001994 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001995 DebugLoc DL;
1996 unsigned RestoreOp, MoveReg;
1997
Hal Finkel67369882013-04-15 02:07:05 +00001998 if (isPPC64)
1999 // This is handled during epilogue generation.
2000 return;
2001 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00002002 // 32-bit: FP-relative
2003 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00002004 PPC::R12),
2005 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00002006 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00002007 MoveReg = PPC::R12;
2008 }
Eric Christopherd1737492014-04-29 00:16:40 +00002009
Roman Divackyc9e23d92012-09-12 14:47:47 +00002010 if (CR2Spilled)
2011 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00002012 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00002013
2014 if (CR3Spilled)
2015 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00002016 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00002017
2018 if (CR4Spilled)
2019 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00002020 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00002021}
2022
Hans Wennborge1a2e902016-03-31 18:33:38 +00002023MachineBasicBlock::iterator PPCFrameLowering::
Eli Bendersky8da87162013-02-21 20:05:00 +00002024eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
2025 MachineBasicBlock::iterator I) const {
Eric Christopher38522b82015-01-30 02:11:26 +00002026 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
Eli Bendersky8da87162013-02-21 20:05:00 +00002027 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2028 I->getOpcode() == PPC::ADJCALLSTACKUP) {
2029 // Add (actually subtract) back the amount the callee popped on return.
2030 if (int CalleeAmt = I->getOperand(1).getImm()) {
2031 bool is64Bit = Subtarget.isPPC64();
2032 CalleeAmt *= -1;
2033 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
2034 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
2035 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
2036 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
2037 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
2038 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +00002039 const DebugLoc &dl = I->getDebugLoc();
Eli Bendersky8da87162013-02-21 20:05:00 +00002040
2041 if (isInt<16>(CalleeAmt)) {
2042 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
2043 .addReg(StackReg, RegState::Kill)
2044 .addImm(CalleeAmt);
2045 } else {
2046 MachineBasicBlock::iterator MBBI = I;
2047 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
2048 .addImm(CalleeAmt >> 16);
2049 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
2050 .addReg(TmpReg, RegState::Kill)
2051 .addImm(CalleeAmt & 0xFFFF);
2052 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
2053 .addReg(StackReg, RegState::Kill)
2054 .addReg(TmpReg);
2055 }
2056 }
2057 }
2058 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
Hans Wennborge1a2e902016-03-31 18:33:38 +00002059 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00002060}
2061
Eric Christopherd1737492014-04-29 00:16:40 +00002062bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00002063PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00002064 MachineBasicBlock::iterator MI,
2065 const std::vector<CalleeSavedInfo> &CSI,
2066 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00002067
2068 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
2069 // Return false otherwise to maintain pre-existing behavior.
2070 if (!Subtarget.isSVR4ABI())
2071 return false;
2072
2073 MachineFunction *MF = MBB.getParent();
Eric Christopherb128abc2017-02-04 01:52:17 +00002074 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00002075 bool CR2Spilled = false;
2076 bool CR3Spilled = false;
2077 bool CR4Spilled = false;
2078 unsigned CSIIndex = 0;
2079
2080 // Initialize insertion-point logic; we will be restoring in reverse
2081 // order of spill.
2082 MachineBasicBlock::iterator I = MI, BeforeI = I;
2083 bool AtStart = I == MBB.begin();
2084
2085 if (!AtStart)
2086 --BeforeI;
2087
2088 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2089 unsigned Reg = CSI[i].getReg();
2090
Hal Finkelac1a24b2013-06-28 22:29:56 +00002091 // Only Darwin actually uses the VRSAVE register, but it can still appear
2092 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
2093 // Darwin, ignore it.
2094 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
2095 continue;
2096
Roman Divackyc9e23d92012-09-12 14:47:47 +00002097 if (Reg == PPC::CR2) {
2098 CR2Spilled = true;
2099 // The spill slot is associated only with CR2, which is the
2100 // first nonvolatile spilled. Save it here.
2101 CSIIndex = i;
2102 continue;
2103 } else if (Reg == PPC::CR3) {
2104 CR3Spilled = true;
2105 continue;
2106 } else if (Reg == PPC::CR4) {
2107 CR4Spilled = true;
2108 continue;
2109 } else {
2110 // When we first encounter a non-CR register after seeing at
2111 // least one CR register, restore all spilled CRs together.
2112 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00002113 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00002114 bool is31 = needsFP(*MF);
2115 restoreCRs(Subtarget.isPPC64(), is31,
2116 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00002117 MBB, I, CSI, CSIIndex);
2118 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00002119 }
2120
2121 // Default behavior for non-CR saves.
2122 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2123 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00002124 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00002125 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00002126 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00002127 }
2128
2129 // Insert in reverse order.
2130 if (AtStart)
2131 I = MBB.begin();
2132 else {
2133 I = BeforeI;
2134 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00002135 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00002136 }
2137
2138 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00002139 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00002140 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00002141 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00002142 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00002143 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00002144
2145 return true;
2146}
Kit Bartond3b904d2015-09-10 01:55:44 +00002147
2148bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
Kit Bartonf4ce2f32015-11-30 18:59:41 +00002149 return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() &&
2150 MF.getSubtarget<PPCSubtarget>().isPPC64());
Kit Bartond3b904d2015-09-10 01:55:44 +00002151}