blob: 614a7f81ecd1350d4eeabe284b23644dbeb9b2c2 [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 Christopherfc6de422014-08-05 02:39:49 +0000436 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000437 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000438
439 // If we are a leaf function, and use up to 224 bytes of stack space,
440 // don't have a frame pointer, calls, or dynamic alloca then we do not need
Hal Finkel67369882013-04-15 02:07:05 +0000441 // to adjust the stack pointer (we fit in the Red Zone).
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000442 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
443 // stackless code if all local vars are reg-allocated.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000444 bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
Bill Schmidt82f1c772015-02-10 19:09:05 +0000445 unsigned LR = RegInfo->getRARegister();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000446 if (!DisableRedZone &&
Bill Schmidt8ea7af82013-02-26 21:28:57 +0000447 (Subtarget.isPPC64() || // 32-bit SVR4, no stack-
448 !Subtarget.isSVR4ABI() || // allocated locals.
Eric Christopherd1737492014-04-29 00:16:40 +0000449 FrameSize == 0) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000450 FrameSize <= 224 && // Fits in red zone.
Matthias Braun941a7052016-07-28 18:40:00 +0000451 !MFI.hasVarSizedObjects() && // No dynamic alloca.
452 !MFI.adjustsStack() && // No calls.
Bill Schmidt82f1c772015-02-10 19:09:05 +0000453 !MustSaveLR(MF, LR) &&
Hal Finkela7c54e82013-07-17 00:45:52 +0000454 !RegInfo->hasBasePointer(MF)) { // No special alignment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000455 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000456 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000457 MFI.setStackSize(0);
Hal Finkelbb420f12013-03-15 05:06:04 +0000458 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000459 }
460
461 // Get the maximum call frame size of all the calls.
Matthias Braun941a7052016-07-28 18:40:00 +0000462 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000463
Ulrich Weigandf316e1d2014-06-23 13:47:52 +0000464 // Maximum call frame needs to be at least big enough for linkage area.
Eric Christophera4ae2132015-02-13 22:22:57 +0000465 unsigned minCallFrameSize = getLinkageSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000466 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
467
468 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
469 // that allocations will be aligned.
Matthias Braun941a7052016-07-28 18:40:00 +0000470 if (MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000471 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
472
473 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000474 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000475 MFI.setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000476
477 // Include call frame size in total.
478 FrameSize += maxCallFrameSize;
479
480 // Make sure the frame is aligned.
481 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
482
483 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000484 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000485 MFI.setStackSize(FrameSize);
Hal Finkelbb420f12013-03-15 05:06:04 +0000486
487 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000488}
489
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000490// hasFP - Return true if the specified function actually has a dedicated frame
491// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000492bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000493 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000494 // FIXME: This is pretty much broken by design: hasFP() might be called really
495 // early, before the stack layout was calculated and thus hasFP() might return
496 // true or false here depending on the time of call.
Matthias Braun941a7052016-07-28 18:40:00 +0000497 return (MFI.getStackSize()) && needsFP(MF);
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000498}
499
500// needsFP - Return true if the specified function should have a dedicated frame
501// pointer register. This is true if the function has variable sized allocas or
502// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000503bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000504 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000505
506 // Naked functions have no stack frame pushed, so we don't have a frame
507 // pointer.
Duncan P. N. Exon Smith5bedaf932015-02-14 02:54:07 +0000508 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000509 return false;
510
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000511 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000512 MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000513 (MF.getTarget().Options.GuaranteedTailCallOpt &&
514 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000515}
516
Hal Finkelaa03c032013-03-21 19:03:19 +0000517void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
518 bool is31 = needsFP(MF);
519 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
520 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
521
Eric Christopherfc6de422014-08-05 02:39:49 +0000522 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000523 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Hal Finkelf05d6c72013-07-17 23:50:51 +0000524 bool HasBP = RegInfo->hasBasePointer(MF);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000525 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000526 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
527
Hal Finkelaa03c032013-03-21 19:03:19 +0000528 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
529 BI != BE; ++BI)
530 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
531 --MBBI;
532 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
533 MachineOperand &MO = MBBI->getOperand(I);
534 if (!MO.isReg())
535 continue;
536
537 switch (MO.getReg()) {
538 case PPC::FP:
539 MO.setReg(FPReg);
540 break;
541 case PPC::FP8:
542 MO.setReg(FP8Reg);
543 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000544 case PPC::BP:
545 MO.setReg(BPReg);
546 break;
547 case PPC::BP8:
548 MO.setReg(BP8Reg);
549 break;
550
Hal Finkelaa03c032013-03-21 19:03:19 +0000551 }
552 }
553 }
554}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000555
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000556/* This function will do the following:
557 - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
558 respectively (defaults recommended by the ABI) and return true
559 - If MBB is not an entry block, initialize the register scavenger and look
560 for available registers.
561 - If the defaults (R0/R12) are available, return true
562 - If TwoUniqueRegsRequired is set to true, it looks for two unique
563 registers. Otherwise, look for a single available register.
564 - If the required registers are found, set SR1 and SR2 and return true.
565 - If the required registers are not found, set SR2 or both SR1 and SR2 to
566 PPC::NoRegister and return false.
567
568 Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
569 is not set, this function will attempt to find two different registers, but
570 still return true if only one register is available (and set SR1 == SR2).
571*/
572bool
573PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
574 bool UseAtEnd,
575 bool TwoUniqueRegsRequired,
576 unsigned *SR1,
577 unsigned *SR2) const {
Kit Barton9c432ae2015-11-16 20:22:15 +0000578 RegScavenger RS;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000579 unsigned R0 = Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
580 unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
Kit Barton9c432ae2015-11-16 20:22:15 +0000581
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000582 // Set the defaults for the two scratch registers.
583 if (SR1)
584 *SR1 = R0;
Kit Barton9c432ae2015-11-16 20:22:15 +0000585
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000586 if (SR2) {
587 assert (SR1 && "Asking for the second scratch register but not the first?");
588 *SR2 = R12;
589 }
590
591 // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
Kit Barton9c432ae2015-11-16 20:22:15 +0000592 if ((UseAtEnd && MBB->isReturnBlock()) ||
593 (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
594 return true;
Alexey Samsonov39b7d652015-12-02 21:25:28 +0000595
Matthias Braun7dc03f02016-04-06 02:47:09 +0000596 RS.enterBasicBlock(*MBB);
Kit Barton9c432ae2015-11-16 20:22:15 +0000597
Kit Bartonf4ce2f32015-11-30 18:59:41 +0000598 if (UseAtEnd && !MBB->empty()) {
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000599 // The scratch register will be used at the end of the block, so must
600 // consider all registers used within the block
Kit Bartonf4ce2f32015-11-30 18:59:41 +0000601
602 MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
603 // If no terminator, back iterator up to previous instruction.
604 if (MBBI == MBB->end())
605 MBBI = std::prev(MBBI);
606
607 if (MBBI != MBB->begin())
608 RS.forward(MBBI);
609 }
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000610
611 // If the two registers are available, we're all good.
612 // Note that we only return here if both R0 and R12 are available because
613 // although the function may not require two unique registers, it may benefit
614 // from having two so we should try to provide them.
615 if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12))
Kit Barton9c432ae2015-11-16 20:22:15 +0000616 return true;
617
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000618 // Get the list of callee-saved registers for the target.
619 const PPCRegisterInfo *RegInfo =
620 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
621 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent());
622
623 // Get all the available registers in the block.
624 BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass :
625 &PPC::GPRCRegClass);
626
627 // We shouldn't use callee-saved registers as scratch registers as they may be
628 // available when looking for a candidate block for shrink wrapping but not
629 // available when the actual prologue/epilogue is being emitted because they
630 // were added as live-in to the prologue block by PrologueEpilogueInserter.
631 for (int i = 0; CSRegs[i]; ++i)
632 BV.reset(CSRegs[i]);
633
634 // Set the first scratch register to the first available one.
635 if (SR1) {
636 int FirstScratchReg = BV.find_first();
637 *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
638 }
639
640 // If there is another one available, set the second scratch register to that.
641 // Otherwise, set it to either PPC::NoRegister if this function requires two
642 // or to whatever SR1 is set to if this function doesn't require two.
643 if (SR2) {
644 int SecondScratchReg = BV.find_next(*SR1);
645 if (SecondScratchReg != -1)
646 *SR2 = SecondScratchReg;
647 else
648 *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1;
649 }
650
651 // Now that we've done our best to provide both registers, double check
652 // whether we were unable to provide enough.
Aaron Ballman8374c1f2016-02-23 15:02:43 +0000653 if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
Kit Barton9c432ae2015-11-16 20:22:15 +0000654 return false;
655
Kit Barton9c432ae2015-11-16 20:22:15 +0000656 return true;
657}
658
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000659// We need a scratch register for spilling LR and for spilling CR. By default,
660// we use two scratch registers to hide latency. However, if only one scratch
661// register is available, we can adjust for that by not overlapping the spill
662// code. However, if we need to realign the stack (i.e. have a base pointer)
663// and the stack frame is large, we need two scratch registers.
664bool
665PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
666 const PPCRegisterInfo *RegInfo =
667 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
668 MachineFunction &MF = *(MBB->getParent());
669 bool HasBP = RegInfo->hasBasePointer(MF);
670 unsigned FrameSize = determineFrameLayout(MF, false);
671 int NegFrameSize = -FrameSize;
672 bool IsLargeFrame = !isInt<16>(NegFrameSize);
Matthias Braun941a7052016-07-28 18:40:00 +0000673 MachineFrameInfo &MFI = MF.getFrameInfo();
674 unsigned MaxAlign = MFI.getMaxAlignment();
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000675 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000676
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000677 return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000678}
679
Kit Barton9c432ae2015-11-16 20:22:15 +0000680bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
681 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
682
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000683 return findScratchRegister(TmpMBB, false,
684 twoUniqueScratchRegsRequired(TmpMBB));
Kit Barton9c432ae2015-11-16 20:22:15 +0000685}
686
687bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
688 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
689
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000690 return findScratchRegister(TmpMBB, true);
Kit Barton9c432ae2015-11-16 20:22:15 +0000691}
692
Quentin Colombet61b305e2015-05-05 17:38:16 +0000693void PPCFrameLowering::emitPrologue(MachineFunction &MF,
694 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000695 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000696 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000697 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +0000698 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Eric Christopherfc6de422014-08-05 02:39:49 +0000699 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +0000700 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000701
702 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000703 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000704 DebugLoc dl;
Jay Foad1f0a44e2014-12-01 09:42:32 +0000705 bool needsCFI = MMI.hasDebugInfo() ||
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000706 MF.getFunction()->needsUnwindTableEntry();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000707
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000708 // Get processor type.
709 bool isPPC64 = Subtarget.isPPC64();
710 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000711 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000712 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chandler Carruth003ed332015-02-14 09:14:44 +0000713 assert((Subtarget.isDarwinABI() || isSVR4ABI) &&
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000714 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
715
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000716 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
717 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000718 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000719 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
720 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000721 HandleVRSaveUpdate(*MBBI, TII);
Bill Schmidt38d94582012-10-10 20:54:15 +0000722 break;
723 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000724 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000725
Kit Bartond3b904d2015-09-10 01:55:44 +0000726 // Move MBBI back to the beginning of the prologue block.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000727 MBBI = MBB.begin();
728
729 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000730 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000731 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000732 if (!isInt<32>(NegFrameSize))
733 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000734
Matthias Braun941a7052016-07-28 18:40:00 +0000735 if (MFI.isFrameAddressTaken())
Hal Finkelaa03c032013-03-21 19:03:19 +0000736 replaceFPWithRealFP(MF);
737
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000738 // Check if the link register (LR) must be saved.
739 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
740 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000741 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000742 bool MustSaveCR = !MustSaveCRs.empty();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000743 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000744 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000745 bool HasBP = RegInfo->hasBasePointer(MF);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000746 bool HasRedZone = isPPC64 || !isSVR4ABI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000747
Bill Schmidtf381afc2013-08-20 03:12:23 +0000748 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000749 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000750 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
751 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
Kit Barton9c432ae2015-11-16 20:22:15 +0000752 unsigned ScratchReg = 0;
Bill Schmidtf381afc2013-08-20 03:12:23 +0000753 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
754 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
755 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
756 : PPC::MFLR );
757 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
758 : PPC::STW );
759 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
760 : PPC::STWU );
761 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
762 : PPC::STWUX);
763 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
764 : PPC::LIS );
765 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
766 : PPC::ORI );
767 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
768 : PPC::OR );
769 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
770 : PPC::SUBFC);
771 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
772 : PPC::SUBFIC);
773
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000774 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
775 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
776 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
777 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
778 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
779 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
780
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +0000781 // Using the same bool variable as below to supress compiler warnings.
782 bool SingleScratchReg =
783 findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB),
784 &ScratchReg, &TempReg);
785 assert(SingleScratchReg &&
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000786 "Required number of registers not available in this block");
787
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +0000788 SingleScratchReg = ScratchReg == TempReg;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000789
Eric Christopherf71609b2015-02-13 00:39:27 +0000790 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000791
792 int FPOffset = 0;
793 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000794 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +0000795 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000796 int FPIndex = FI->getFramePointerSaveIndex();
797 assert(FPIndex && "No Frame Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000798 FPOffset = MFI.getObjectOffset(FPIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000799 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000800 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000801 }
802 }
803
Hal Finkela7c54e82013-07-17 00:45:52 +0000804 int BPOffset = 0;
805 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000806 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +0000807 MachineFrameInfo &MFI = MF.getFrameInfo();
Hal Finkela7c54e82013-07-17 00:45:52 +0000808 int BPIndex = FI->getBasePointerSaveIndex();
809 assert(BPIndex && "No Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000810 BPOffset = MFI.getObjectOffset(BPIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000811 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +0000812 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +0000813 }
814 }
815
Justin Hibbits654346e2015-01-10 01:57:21 +0000816 int PBPOffset = 0;
817 if (FI->usesPICBase()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000818 MachineFrameInfo &MFI = MF.getFrameInfo();
Justin Hibbits654346e2015-01-10 01:57:21 +0000819 int PBPIndex = FI->getPICBasePointerSaveIndex();
820 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000821 PBPOffset = MFI.getObjectOffset(PBPIndex);
Justin Hibbits654346e2015-01-10 01:57:21 +0000822 }
823
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000824 // Get stack alignments.
Matthias Braun941a7052016-07-28 18:40:00 +0000825 unsigned MaxAlign = MFI.getMaxAlignment();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000826 if (HasBP && MaxAlign > 1)
827 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
828 "Invalid alignment!");
829
830 // Frames of 32KB & larger require special handling because they cannot be
831 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
832 bool isLargeFrame = !isInt<16>(NegFrameSize);
833
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000834 assert((isPPC64 || !MustSaveCR) &&
835 "Prologue CR saving supported only in 64-bit mode");
836
837 // If we need to spill the CR and the LR but we don't have two separate
838 // registers available, we must spill them one at a time
839 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000840 // In the ELFv2 ABI, we are not required to save all CR fields.
841 // If only one or two CR fields are clobbered, it is more efficient to use
842 // mfocrf to selectively save just those fields, because mfocrf has short
843 // latency compares to mfcr.
844 unsigned MfcrOpcode = PPC::MFCR8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000845 unsigned CrState = RegState::ImplicitKill;
846 if (isELFv2ABI && MustSaveCRs.size() == 1) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000847 MfcrOpcode = PPC::MFOCRF8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000848 CrState = RegState::Kill;
849 }
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000850 MachineInstrBuilder MIB =
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000851 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000852 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000853 MIB.addReg(MustSaveCRs[i], CrState);
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000854 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
855 .addReg(TempReg, getKillRegState(true))
856 .addImm(8)
857 .addReg(SPReg);
858 }
859
Bill Schmidtf381afc2013-08-20 03:12:23 +0000860 if (MustSaveLR)
861 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000862
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000863 if (MustSaveCR &&
864 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000865 // In the ELFv2 ABI, we are not required to save all CR fields.
866 // If only one or two CR fields are clobbered, it is more efficient to use
867 // mfocrf to selectively save just those fields, because mfocrf has short
868 // latency compares to mfcr.
869 unsigned MfcrOpcode = PPC::MFCR8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000870 unsigned CrState = RegState::ImplicitKill;
871 if (isELFv2ABI && MustSaveCRs.size() == 1) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000872 MfcrOpcode = PPC::MFOCRF8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000873 CrState = RegState::Kill;
874 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000875 MachineInstrBuilder MIB =
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000876 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000877 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000878 MIB.addReg(MustSaveCRs[i], CrState);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000879 }
880
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000881 if (HasRedZone) {
882 if (HasFP)
883 BuildMI(MBB, MBBI, dl, StoreInst)
884 .addReg(FPReg)
885 .addImm(FPOffset)
886 .addReg(SPReg);
887 if (FI->usesPICBase())
888 BuildMI(MBB, MBBI, dl, StoreInst)
889 .addReg(PPC::R30)
890 .addImm(PBPOffset)
891 .addReg(SPReg);
892 if (HasBP)
893 BuildMI(MBB, MBBI, dl, StoreInst)
894 .addReg(BPReg)
895 .addImm(BPOffset)
896 .addReg(SPReg);
897 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000898
899 if (MustSaveLR)
Bill Schmidtf381afc2013-08-20 03:12:23 +0000900 BuildMI(MBB, MBBI, dl, StoreInst)
Nemanja Ivanovic62fba482016-07-15 19:56:32 +0000901 .addReg(ScratchReg, getKillRegState(true))
Bill Schmidtf381afc2013-08-20 03:12:23 +0000902 .addImm(LROffset)
903 .addReg(SPReg);
904
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000905 if (MustSaveCR &&
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000906 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
907 assert(HasRedZone && "A red zone is always available on PPC64");
Bill Schmidtf381afc2013-08-20 03:12:23 +0000908 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
909 .addReg(TempReg, getKillRegState(true))
910 .addImm(8)
911 .addReg(SPReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000912 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000913
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000914 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000915 if (!FrameSize)
916 return;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000917
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000918 // Adjust stack pointer: r1 += NegFrameSize.
919 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000920
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000921 if (HasBP && HasRedZone) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000922 // Save a copy of r1 as the base pointer.
923 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
924 .addReg(SPReg)
925 .addReg(SPReg);
926 }
927
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000928 // Have we generated a STUX instruction to claim stack frame? If so,
929 // the frame size will be placed in ScratchReg.
930 bool HasSTUX = false;
931
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000932 // This condition must be kept in sync with canUseAsPrologue.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000933 if (HasBP && MaxAlign > 1) {
934 if (isPPC64)
935 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
936 .addReg(SPReg)
937 .addImm(0)
938 .addImm(64 - Log2_32(MaxAlign));
939 else // PPC32...
940 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
941 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000942 .addImm(0)
943 .addImm(32 - Log2_32(MaxAlign))
944 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000945 if (!isLargeFrame) {
946 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
947 .addReg(ScratchReg, RegState::Kill)
948 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000949 } else {
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000950 assert(!SingleScratchReg && "Only a single scratch reg available");
Bill Schmidtf381afc2013-08-20 03:12:23 +0000951 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000952 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000953 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
954 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000955 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000956 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
957 .addReg(ScratchReg, RegState::Kill)
958 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000959 }
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000960
Bill Schmidtf381afc2013-08-20 03:12:23 +0000961 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
962 .addReg(SPReg, RegState::Kill)
963 .addReg(SPReg)
964 .addReg(ScratchReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000965 HasSTUX = true;
Hal Finkela7c54e82013-07-17 00:45:52 +0000966
Bill Schmidtf381afc2013-08-20 03:12:23 +0000967 } else if (!isLargeFrame) {
968 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
969 .addReg(SPReg)
970 .addImm(NegFrameSize)
971 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000972
Bill Schmidtf381afc2013-08-20 03:12:23 +0000973 } else {
974 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
975 .addImm(NegFrameSize >> 16);
976 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
977 .addReg(ScratchReg, RegState::Kill)
978 .addImm(NegFrameSize & 0xFFFF);
979 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
980 .addReg(SPReg, RegState::Kill)
981 .addReg(SPReg)
982 .addReg(ScratchReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000983 HasSTUX = true;
984 }
985
986 if (!HasRedZone) {
987 assert(!isPPC64 && "A red zone is always available on PPC64");
988 if (HasSTUX) {
989 // The frame size is in ScratchReg, and the SPReg has been advanced
990 // (downwards) by the frame size: SPReg = old SPReg + ScratchReg.
991 // Set ScratchReg to the original SPReg: ScratchReg = SPReg - ScratchReg.
992 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg)
993 .addReg(ScratchReg, RegState::Kill)
994 .addReg(SPReg);
995
996 // Now that the stack frame has been allocated, save all the necessary
997 // registers using ScratchReg as the base address.
998 if (HasFP)
999 BuildMI(MBB, MBBI, dl, StoreInst)
1000 .addReg(FPReg)
1001 .addImm(FPOffset)
1002 .addReg(ScratchReg);
1003 if (FI->usesPICBase())
1004 BuildMI(MBB, MBBI, dl, StoreInst)
1005 .addReg(PPC::R30)
1006 .addImm(PBPOffset)
1007 .addReg(ScratchReg);
1008 if (HasBP) {
1009 BuildMI(MBB, MBBI, dl, StoreInst)
1010 .addReg(BPReg)
1011 .addImm(BPOffset)
1012 .addReg(ScratchReg);
1013 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
1014 .addReg(ScratchReg, RegState::Kill)
1015 .addReg(ScratchReg);
1016 }
1017 } else {
1018 // The frame size is a known 16-bit constant (fitting in the immediate
1019 // field of STWU). To be here we have to be compiling for PPC32.
1020 // Since the SPReg has been decreased by FrameSize, add it back to each
1021 // offset.
1022 if (HasFP)
1023 BuildMI(MBB, MBBI, dl, StoreInst)
1024 .addReg(FPReg)
1025 .addImm(FrameSize + FPOffset)
1026 .addReg(SPReg);
1027 if (FI->usesPICBase())
1028 BuildMI(MBB, MBBI, dl, StoreInst)
1029 .addReg(PPC::R30)
1030 .addImm(FrameSize + PBPOffset)
1031 .addReg(SPReg);
1032 if (HasBP) {
1033 BuildMI(MBB, MBBI, dl, StoreInst)
1034 .addReg(BPReg)
1035 .addImm(FrameSize + BPOffset)
1036 .addReg(SPReg);
1037 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg)
1038 .addReg(SPReg)
1039 .addImm(FrameSize);
1040 }
1041 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001042 }
1043
Jay Foad1f0a44e2014-12-01 09:42:32 +00001044 // Add Call Frame Information for the instructions we generated above.
1045 if (needsCFI) {
1046 unsigned CFIIndex;
1047
1048 if (HasBP) {
1049 // Define CFA in terms of BP. Do this in preference to using FP/SP,
1050 // because if the stack needed aligning then CFA won't be at a fixed
1051 // offset from FP/SP.
1052 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1053 CFIIndex = MMI.addFrameInst(
1054 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1055 } else {
1056 // Adjust the definition of CFA to account for the change in SP.
1057 assert(NegFrameSize);
1058 CFIIndex = MMI.addFrameInst(
1059 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
1060 }
Eric Christopher612bb692014-04-29 00:16:46 +00001061 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1062 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001063
1064 if (HasFP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001065 // Describe where FP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001066 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001067 CFIIndex = MMI.addFrameInst(
1068 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001069 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001070 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001071 }
1072
Justin Hibbits654346e2015-01-10 01:57:21 +00001073 if (FI->usesPICBase()) {
1074 // Describe where FP was saved, at a fixed offset from CFA.
1075 unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
1076 CFIIndex = MMI.addFrameInst(
1077 MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
1078 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1079 .addCFIIndex(CFIIndex);
1080 }
1081
Hal Finkela7c54e82013-07-17 00:45:52 +00001082 if (HasBP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001083 // Describe where BP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001084 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001085 CFIIndex = MMI.addFrameInst(
1086 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001087 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001088 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +00001089 }
1090
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001091 if (MustSaveLR) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001092 // Describe where LR was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001093 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001094 CFIIndex = MMI.addFrameInst(
1095 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001096 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001097 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001098 }
1099 }
1100
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001101 // If there is a frame pointer, copy R1 into R31
1102 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001103 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1104 .addReg(SPReg)
1105 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001106
Jay Foad1f0a44e2014-12-01 09:42:32 +00001107 if (!HasBP && needsCFI) {
1108 // Change the definition of CFA from SP+offset to FP+offset, because SP
1109 // will change at every alloca.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001110 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001111 unsigned CFIIndex = MMI.addFrameInst(
1112 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1113
Eric Christopher612bb692014-04-29 00:16:46 +00001114 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001115 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001116 }
1117 }
1118
Jay Foad1f0a44e2014-12-01 09:42:32 +00001119 if (needsCFI) {
1120 // Describe where callee saved registers were saved, at fixed offsets from
1121 // CFA.
Matthias Braun941a7052016-07-28 18:40:00 +00001122 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001123 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001124 unsigned Reg = CSI[I].getReg();
1125 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +00001126
1127 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1128 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +00001129 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +00001130 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +00001131
Roman Divackyc9e23d92012-09-12 14:47:47 +00001132 // For SVR4, don't emit a move for the CR spill slot if we haven't
1133 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001134 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001135 && !MustSaveCR)
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001136 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001137
1138 // For 64-bit SVR4 when we have spilled CRs, the spill location
1139 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001140 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Ulrich Weigandbe928cc2014-07-21 00:03:18 +00001141 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
1142 // the whole CR word. In the ELFv2 ABI, every CR that was
1143 // actually saved gets its own CFI record.
1144 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001145 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
Ulrich Weigandbe928cc2014-07-21 00:03:18 +00001146 nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +00001147 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001148 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001149 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001150 }
1151
Matthias Braun941a7052016-07-28 18:40:00 +00001152 int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx());
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001153 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1154 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +00001155 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001156 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001157 }
1158 }
1159}
1160
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001161void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Kit Bartond3b904d2015-09-10 01:55:44 +00001162 MachineBasicBlock &MBB) const {
1163 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1164 DebugLoc dl;
1165
1166 if (MBBI != MBB.end())
1167 dl = MBBI->getDebugLoc();
1168
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001169 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +00001170 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Eric Christopherfc6de422014-08-05 02:39:49 +00001171 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +00001172 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001173
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001174 // Get alignment info so we know how to restore the SP.
Matthias Braun941a7052016-07-28 18:40:00 +00001175 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001176
1177 // Get the number of bytes allocated from the FrameInfo.
Matthias Braun941a7052016-07-28 18:40:00 +00001178 int FrameSize = MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001179
1180 // Get processor type.
1181 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001182 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001183 bool isSVR4ABI = Subtarget.isSVR4ABI();
1184
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001185 // Check if the link register (LR) has been saved.
1186 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1187 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +00001188 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001189 bool MustSaveCR = !MustSaveCRs.empty();
Bill Schmidtf381afc2013-08-20 03:12:23 +00001190 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001191 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +00001192 bool HasBP = RegInfo->hasBasePointer(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001193
Bill Schmidtf381afc2013-08-20 03:12:23 +00001194 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +00001195 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001196 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
Kit Barton9c432ae2015-11-16 20:22:15 +00001197 unsigned ScratchReg = 0;
Bill Schmidtf381afc2013-08-20 03:12:23 +00001198 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1199 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
1200 : PPC::MTLR );
1201 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
1202 : PPC::LWZ );
1203 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
1204 : PPC::LIS );
1205 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
1206 : PPC::ORI );
1207 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
1208 : PPC::ADDI );
1209 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
1210 : PPC::ADD4 );
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001211
Eric Christopherf71609b2015-02-13 00:39:27 +00001212 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001213
1214 int FPOffset = 0;
Kit Barton9c432ae2015-11-16 20:22:15 +00001215
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +00001216 // Using the same bool variable as below to supress compiler warnings.
1217 bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg,
1218 &TempReg);
1219 assert(SingleScratchReg &&
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001220 "Could not find an available scratch register");
1221
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +00001222 SingleScratchReg = ScratchReg == TempReg;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001223
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001224 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001225 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +00001226 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001227 int FPIndex = FI->getFramePointerSaveIndex();
1228 assert(FPIndex && "No Frame Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001229 FPOffset = MFI.getObjectOffset(FPIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001230 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001231 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001232 }
1233 }
1234
Hal Finkela7c54e82013-07-17 00:45:52 +00001235 int BPOffset = 0;
1236 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001237 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +00001238 MachineFrameInfo &MFI = MF.getFrameInfo();
Hal Finkela7c54e82013-07-17 00:45:52 +00001239 int BPIndex = FI->getBasePointerSaveIndex();
1240 assert(BPIndex && "No Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001241 BPOffset = MFI.getObjectOffset(BPIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +00001242 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001243 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001244 }
1245 }
1246
Justin Hibbits654346e2015-01-10 01:57:21 +00001247 int PBPOffset = 0;
1248 if (FI->usesPICBase()) {
Matthias Braun941a7052016-07-28 18:40:00 +00001249 MachineFrameInfo &MFI = MF.getFrameInfo();
Justin Hibbits654346e2015-01-10 01:57:21 +00001250 int PBPIndex = FI->getPICBasePointerSaveIndex();
1251 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001252 PBPOffset = MFI.getObjectOffset(PBPIndex);
Justin Hibbits654346e2015-01-10 01:57:21 +00001253 }
1254
NAKAMURA Takumi8061e862015-09-11 08:20:56 +00001255 bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
Kit Bartond3b904d2015-09-10 01:55:44 +00001256
1257 if (IsReturnBlock) {
1258 unsigned RetOpcode = MBBI->getOpcode();
1259 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1260 RetOpcode == PPC::TCRETURNdi ||
1261 RetOpcode == PPC::TCRETURNai ||
1262 RetOpcode == PPC::TCRETURNri8 ||
1263 RetOpcode == PPC::TCRETURNdi8 ||
1264 RetOpcode == PPC::TCRETURNai8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001265
Kit Bartond3b904d2015-09-10 01:55:44 +00001266 if (UsesTCRet) {
1267 int MaxTCRetDelta = FI->getTailCallSPDelta();
1268 MachineOperand &StackAdjust = MBBI->getOperand(1);
1269 assert(StackAdjust.isImm() && "Expecting immediate value.");
1270 // Adjust stack pointer.
1271 int StackAdj = StackAdjust.getImm();
1272 int Delta = StackAdj - MaxTCRetDelta;
1273 assert((Delta >= 0) && "Delta must be positive");
1274 if (MaxTCRetDelta>0)
1275 FrameSize += (StackAdj +Delta);
1276 else
1277 FrameSize += StackAdj;
1278 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001279 }
1280
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001281 // Frames of 32KB & larger require special handling because they cannot be
1282 // indexed into with a simple LD/LWZ immediate offset operand.
1283 bool isLargeFrame = !isInt<16>(FrameSize);
1284
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001285 if (FrameSize) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001286 // In the prologue, the loaded (or persistent) stack pointer value is offset
1287 // by the STDU/STDUX/STWU/STWUX instruction. Add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001288
1289 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1290 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1291 // call which invalidates the stack pointer value in SP(0). So we use the
1292 // value of R31 in this case.
1293 if (FI->hasFastCall()) {
1294 assert(HasFP && "Expecting a valid frame pointer.");
1295 if (!isLargeFrame) {
1296 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1297 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001298 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001299 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1300 .addImm(FrameSize >> 16);
1301 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1302 .addReg(ScratchReg, RegState::Kill)
1303 .addImm(FrameSize & 0xFFFF);
1304 BuildMI(MBB, MBBI, dl, AddInst)
1305 .addReg(SPReg)
1306 .addReg(FPReg)
1307 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001308 }
Matthias Braun941a7052016-07-28 18:40:00 +00001309 } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001310 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1311 .addReg(SPReg)
1312 .addImm(FrameSize);
1313 } else {
1314 BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
1315 .addImm(0)
1316 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001317 }
1318 }
1319
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001320 assert((isPPC64 || !MustSaveCR) &&
1321 "Epilogue CR restoring supported only in 64-bit mode");
1322
1323 // If we need to save both the LR and the CR and we only have one available
1324 // scratch register, we must do them one at a time.
1325 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
1326 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1327 .addImm(8)
1328 .addReg(SPReg);
1329 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1330 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1331 .addReg(TempReg, getKillRegState(i == e-1));
1332 }
1333
Bill Schmidtf381afc2013-08-20 03:12:23 +00001334 if (MustSaveLR)
1335 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1336 .addImm(LROffset)
1337 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001338
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001339 if (MustSaveCR &&
1340 !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
Bill Schmidtf381afc2013-08-20 03:12:23 +00001341 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1342 .addImm(8)
1343 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001344
Bill Schmidtf381afc2013-08-20 03:12:23 +00001345 if (HasFP)
1346 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1347 .addImm(FPOffset)
1348 .addReg(SPReg);
Hal Finkela7c54e82013-07-17 00:45:52 +00001349
Justin Hibbits654346e2015-01-10 01:57:21 +00001350 if (FI->usesPICBase())
Justin Hibbits98a532d2015-01-08 15:47:19 +00001351 // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
1352 BuildMI(MBB, MBBI, dl, LoadInst)
1353 .addReg(PPC::R30)
Justin Hibbits654346e2015-01-10 01:57:21 +00001354 .addImm(PBPOffset)
Justin Hibbits98a532d2015-01-08 15:47:19 +00001355 .addReg(SPReg);
1356
Bill Schmidtf381afc2013-08-20 03:12:23 +00001357 if (HasBP)
1358 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1359 .addImm(BPOffset)
1360 .addReg(SPReg);
Hal Finkel67369882013-04-15 02:07:05 +00001361
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001362 if (MustSaveCR &&
1363 !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
Bill Schmidtf381afc2013-08-20 03:12:23 +00001364 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1365 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1366 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001367
Bill Schmidtf381afc2013-08-20 03:12:23 +00001368 if (MustSaveLR)
1369 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001370
1371 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1372 // call optimization
Kit Bartond3b904d2015-09-10 01:55:44 +00001373 if (IsReturnBlock) {
1374 unsigned RetOpcode = MBBI->getOpcode();
1375 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1376 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
1377 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1378 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1379 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001380
Kit Bartond3b904d2015-09-10 01:55:44 +00001381 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1382 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1383 .addReg(SPReg).addImm(CallerAllocatedAmt);
1384 } else {
1385 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001386 .addImm(CallerAllocatedAmt >> 16);
Kit Bartond3b904d2015-09-10 01:55:44 +00001387 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001388 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001389 .addImm(CallerAllocatedAmt & 0xFFFF);
Kit Bartond3b904d2015-09-10 01:55:44 +00001390 BuildMI(MBB, MBBI, dl, AddInst)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001391 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001392 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001393 .addReg(ScratchReg);
Kit Bartond3b904d2015-09-10 01:55:44 +00001394 }
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001395 } else {
1396 createTailCallBranchInstr(MBB);
Kit Bartond3b904d2015-09-10 01:55:44 +00001397 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001398 }
1399}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001400
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001401void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
1402 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1403 DebugLoc dl;
1404
1405 if (MBBI != MBB.end())
1406 dl = MBBI->getDebugLoc();
1407
1408 const PPCInstrInfo &TII =
1409 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
1410
1411 // Create branch instruction for pseudo tail call return instruction
1412 unsigned RetOpcode = MBBI->getOpcode();
1413 if (RetOpcode == PPC::TCRETURNdi) {
1414 MBBI = MBB.getLastNonDebugInstr();
1415 MachineOperand &JumpTarget = MBBI->getOperand(0);
1416 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1417 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1418 } else if (RetOpcode == PPC::TCRETURNri) {
1419 MBBI = MBB.getLastNonDebugInstr();
1420 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1421 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1422 } else if (RetOpcode == PPC::TCRETURNai) {
1423 MBBI = MBB.getLastNonDebugInstr();
1424 MachineOperand &JumpTarget = MBBI->getOperand(0);
1425 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1426 } else if (RetOpcode == PPC::TCRETURNdi8) {
1427 MBBI = MBB.getLastNonDebugInstr();
1428 MachineOperand &JumpTarget = MBBI->getOperand(0);
1429 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1430 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1431 } else if (RetOpcode == PPC::TCRETURNri8) {
1432 MBBI = MBB.getLastNonDebugInstr();
1433 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1434 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1435 } else if (RetOpcode == PPC::TCRETURNai8) {
1436 MBBI = MBB.getLastNonDebugInstr();
1437 MachineOperand &JumpTarget = MBBI->getOperand(0);
1438 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1439 }
1440}
1441
Matthias Braun02564862015-07-14 17:17:13 +00001442void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1443 BitVector &SavedRegs,
1444 RegScavenger *RS) const {
1445 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1446
Eric Christopherfc6de422014-08-05 02:39:49 +00001447 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +00001448 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001449
1450 // Save and clear the LR state.
1451 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1452 unsigned LR = RegInfo->getRARegister();
1453 FI->setMustSaveLR(MustSaveLR(MF, LR));
Matthias Braun02564862015-07-14 17:17:13 +00001454 SavedRegs.reset(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001455
1456 // Save R31 if necessary
1457 int FPSI = FI->getFramePointerSaveIndex();
1458 bool isPPC64 = Subtarget.isPPC64();
1459 bool isDarwinABI = Subtarget.isDarwinABI();
Matthias Braun941a7052016-07-28 18:40:00 +00001460 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001461
1462 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001463 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001464 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001465 int FPOffset = getFramePointerSaveOffset();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001466 // Allocate the frame index for frame pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00001467 FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001468 // Save the result.
1469 FI->setFramePointerSaveIndex(FPSI);
1470 }
1471
Hal Finkela7c54e82013-07-17 00:45:52 +00001472 int BPSI = FI->getBasePointerSaveIndex();
1473 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001474 int BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001475 // Allocate the frame index for the base pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00001476 BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
Hal Finkela7c54e82013-07-17 00:45:52 +00001477 // Save the result.
1478 FI->setBasePointerSaveIndex(BPSI);
1479 }
1480
Justin Hibbits654346e2015-01-10 01:57:21 +00001481 // Reserve stack space for the PIC Base register (R30).
1482 // Only used in SVR4 32-bit.
1483 if (FI->usesPICBase()) {
Matthias Braun941a7052016-07-28 18:40:00 +00001484 int PBPSI = MFI.CreateFixedObject(4, -8, true);
Justin Hibbits654346e2015-01-10 01:57:21 +00001485 FI->setPICBasePointerSaveIndex(PBPSI);
1486 }
1487
Hal Finkel97a189c2016-08-31 00:52:03 +00001488 // Make sure we don't explicitly spill r31, because, for example, we have
1489 // some inline asm which explicity clobbers it, when we otherwise have a
1490 // frame pointer and are using r31's spill slot for the prologue/epilogue
1491 // code. Same goes for the base pointer and the PIC base register.
1492 if (needsFP(MF))
1493 SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31);
1494 if (RegInfo->hasBasePointer(MF))
1495 SavedRegs.reset(RegInfo->getBaseRegister(MF));
1496 if (FI->usesPICBase())
1497 SavedRegs.reset(PPC::R30);
1498
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001499 // Reserve stack space to move the linkage area to in case of a tail call.
1500 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001501 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1502 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Matthias Braun941a7052016-07-28 18:40:00 +00001503 MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001504 }
1505
Eric Christopherd1737492014-04-29 00:16:40 +00001506 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001507 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001508 if (!isPPC64 && !isDarwinABI &&
Matthias Braun02564862015-07-14 17:17:13 +00001509 (SavedRegs.test(PPC::CR2) ||
1510 SavedRegs.test(PPC::CR3) ||
1511 SavedRegs.test(PPC::CR4))) {
Matthias Braun941a7052016-07-28 18:40:00 +00001512 int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true);
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001513 FI->setCRSpillFrameIndex(FrameIdx);
1514 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001515}
1516
Hal Finkel5a765fd2013-03-14 20:33:40 +00001517void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001518 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001519 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001520 if (!Subtarget.isSVR4ABI()) {
1521 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001522 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001523 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001524
1525 // Get callee saved register information.
Matthias Braun941a7052016-07-28 18:40:00 +00001526 MachineFrameInfo &MFI = MF.getFrameInfo();
1527 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001528
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001529 // If the function is shrink-wrapped, and if the function has a tail call, the
1530 // tail call might not be in the new RestoreBlock, so real branch instruction
1531 // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
1532 // RestoreBlock. So we handle this case here.
Matthias Braun941a7052016-07-28 18:40:00 +00001533 if (MFI.getSavePoint() && MFI.hasTailCall()) {
1534 MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001535 for (MachineBasicBlock &MBB : MF) {
1536 if (MBB.isReturnBlock() && (&MBB) != RestoreBlock)
1537 createTailCallBranchInstr(MBB);
1538 }
1539 }
1540
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001541 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001542 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001543 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001544 return;
1545 }
1546
1547 unsigned MinGPR = PPC::R31;
1548 unsigned MinG8R = PPC::X31;
1549 unsigned MinFPR = PPC::F31;
1550 unsigned MinVR = PPC::V31;
1551
1552 bool HasGPSaveArea = false;
1553 bool HasG8SaveArea = false;
1554 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001555 bool HasVRSAVESaveArea = false;
1556 bool HasVRSaveArea = false;
1557
1558 SmallVector<CalleeSavedInfo, 18> GPRegs;
1559 SmallVector<CalleeSavedInfo, 18> G8Regs;
1560 SmallVector<CalleeSavedInfo, 18> FPRegs;
1561 SmallVector<CalleeSavedInfo, 18> VRegs;
1562
1563 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1564 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001565 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001566 HasGPSaveArea = true;
1567
1568 GPRegs.push_back(CSI[i]);
1569
1570 if (Reg < MinGPR) {
1571 MinGPR = Reg;
1572 }
Craig Topperabadc662012-04-20 06:31:50 +00001573 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001574 HasG8SaveArea = true;
1575
1576 G8Regs.push_back(CSI[i]);
1577
1578 if (Reg < MinG8R) {
1579 MinG8R = Reg;
1580 }
Craig Topperabadc662012-04-20 06:31:50 +00001581 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001582 HasFPSaveArea = true;
1583
1584 FPRegs.push_back(CSI[i]);
1585
1586 if (Reg < MinFPR) {
1587 MinFPR = Reg;
1588 }
Craig Topperabadc662012-04-20 06:31:50 +00001589 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1590 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001591 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001592 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001593 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001594 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001595 HasVRSaveArea = true;
1596
1597 VRegs.push_back(CSI[i]);
1598
1599 if (Reg < MinVR) {
1600 MinVR = Reg;
1601 }
1602 } else {
1603 llvm_unreachable("Unknown RegisterClass!");
1604 }
1605 }
1606
1607 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Eric Christopher38522b82015-01-30 02:11:26 +00001608 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001609
1610 int64_t LowerBound = 0;
1611
1612 // Take into account stack space reserved for tail calls.
1613 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001614 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1615 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001616 LowerBound = TCSPDelta;
1617 }
1618
1619 // The Floating-point register save area is right below the back chain word
1620 // of the previous stack frame.
1621 if (HasFPSaveArea) {
1622 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1623 int FI = FPRegs[i].getFrameIdx();
1624
Matthias Braun941a7052016-07-28 18:40:00 +00001625 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001626 }
1627
Hal Finkelfeea6532013-03-26 20:08:20 +00001628 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001629 }
1630
1631 // Check whether the frame pointer register is allocated. If so, make sure it
1632 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001633 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001634 HasGPSaveArea = true;
1635
1636 int FI = PFI->getFramePointerSaveIndex();
1637 assert(FI && "No Frame Pointer Save Slot!");
1638
Matthias Braun941a7052016-07-28 18:40:00 +00001639 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001640 }
1641
Justin Hibbits654346e2015-01-10 01:57:21 +00001642 if (PFI->usesPICBase()) {
1643 HasGPSaveArea = true;
1644
1645 int FI = PFI->getPICBasePointerSaveIndex();
1646 assert(FI && "No PIC Base Pointer Save Slot!");
1647
Matthias Braun941a7052016-07-28 18:40:00 +00001648 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Justin Hibbits654346e2015-01-10 01:57:21 +00001649 }
1650
Eric Christopherfc6de422014-08-05 02:39:49 +00001651 const PPCRegisterInfo *RegInfo =
Eric Christopher38522b82015-01-30 02:11:26 +00001652 static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
Hal Finkela7c54e82013-07-17 00:45:52 +00001653 if (RegInfo->hasBasePointer(MF)) {
1654 HasGPSaveArea = true;
1655
1656 int FI = PFI->getBasePointerSaveIndex();
1657 assert(FI && "No Base Pointer Save Slot!");
1658
Matthias Braun941a7052016-07-28 18:40:00 +00001659 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Hal Finkela7c54e82013-07-17 00:45:52 +00001660 }
1661
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001662 // General register save area starts right below the Floating-point
1663 // register save area.
1664 if (HasGPSaveArea || HasG8SaveArea) {
1665 // Move general register save area spill slots down, taking into account
1666 // the size of the Floating-point register save area.
1667 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1668 int FI = GPRegs[i].getFrameIdx();
1669
Matthias Braun941a7052016-07-28 18:40:00 +00001670 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001671 }
1672
1673 // Move general register save area spill slots down, taking into account
1674 // the size of the Floating-point register save area.
1675 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1676 int FI = G8Regs[i].getFrameIdx();
1677
Matthias Braun941a7052016-07-28 18:40:00 +00001678 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001679 }
1680
1681 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001682 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1683 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001684
1685 if (Subtarget.isPPC64()) {
1686 LowerBound -= (31 - MinReg + 1) * 8;
1687 } else {
1688 LowerBound -= (31 - MinReg + 1) * 4;
1689 }
1690 }
1691
Roman Divackyc9e23d92012-09-12 14:47:47 +00001692 // For 32-bit only, the CR save area is below the general register
1693 // save area. For 64-bit SVR4, the CR save area is addressed relative
1694 // to the stack pointer and hence does not need an adjustment here.
1695 // Only CR2 (the first nonvolatile spilled) has an associated frame
1696 // index so that we have a single uniform save area.
1697 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001698 // Adjust the frame index of the CR spill slot.
1699 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1700 unsigned Reg = CSI[i].getReg();
1701
Roman Divackyc9e23d92012-09-12 14:47:47 +00001702 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001703 // Leave Darwin logic as-is.
1704 || (!Subtarget.isSVR4ABI() &&
1705 (PPC::CRBITRCRegClass.contains(Reg) ||
1706 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001707 int FI = CSI[i].getFrameIdx();
1708
Matthias Braun941a7052016-07-28 18:40:00 +00001709 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001710 }
1711 }
1712
1713 LowerBound -= 4; // The CR save area is always 4 bytes long.
1714 }
1715
1716 if (HasVRSAVESaveArea) {
1717 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1718 // which have the VRSAVE register class?
1719 // Adjust the frame index of the VRSAVE spill slot.
1720 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1721 unsigned Reg = CSI[i].getReg();
1722
Craig Topperabadc662012-04-20 06:31:50 +00001723 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001724 int FI = CSI[i].getFrameIdx();
1725
Matthias Braun941a7052016-07-28 18:40:00 +00001726 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001727 }
1728 }
1729
1730 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1731 }
1732
1733 if (HasVRSaveArea) {
1734 // Insert alignment padding, we need 16-byte alignment.
1735 LowerBound = (LowerBound - 15) & ~(15);
1736
1737 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1738 int FI = VRegs[i].getFrameIdx();
1739
Matthias Braun941a7052016-07-28 18:40:00 +00001740 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001741 }
1742 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001743
1744 addScavengingSpillSlot(MF, RS);
1745}
1746
1747void
1748PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1749 RegScavenger *RS) const {
1750 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1751 // a large stack, which will require scavenging a register to materialize a
1752 // large offset.
1753
1754 // We need to have a scavenger spill slot for spills if the frame size is
1755 // large. In case there is no free register for large-offset addressing,
1756 // this slot is used for the necessary emergency spill. Also, we need the
1757 // slot for dynamic stack allocations.
1758
1759 // The scavenger might be invoked if the frame offset does not fit into
1760 // the 16-bit immediate. We don't know the complete frame size here
1761 // because we've not yet computed callee-saved register spills or the
1762 // needed alignment padding.
1763 unsigned StackSize = determineFrameLayout(MF, false, true);
Matthias Braun941a7052016-07-28 18:40:00 +00001764 MachineFrameInfo &MFI = MF.getFrameInfo();
1765 if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001766 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001767 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1768 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1769 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
Matthias Braun941a7052016-07-28 18:40:00 +00001770 RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(),
1771 RC->getAlignment(),
1772 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001773
Hal Finkel18607632013-07-18 04:28:21 +00001774 // Might we have over-aligned allocas?
Matthias Braun941a7052016-07-28 18:40:00 +00001775 bool HasAlVars = MFI.hasVarSizedObjects() &&
1776 MFI.getMaxAlignment() > getStackAlignment();
Hal Finkel18607632013-07-18 04:28:21 +00001777
Hal Finkel0dfbb052013-03-26 18:57:22 +00001778 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001779 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Matthias Braun941a7052016-07-28 18:40:00 +00001780 RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(),
1781 RC->getAlignment(),
1782 false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001783
Hal Finkelbb420f12013-03-15 05:06:04 +00001784 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001785}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001786
Eric Christopherd1737492014-04-29 00:16:40 +00001787bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001788PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001789 MachineBasicBlock::iterator MI,
1790 const std::vector<CalleeSavedInfo> &CSI,
1791 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001792
1793 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1794 // Return false otherwise to maintain pre-existing behavior.
1795 if (!Subtarget.isSVR4ABI())
1796 return false;
1797
1798 MachineFunction *MF = MBB.getParent();
1799 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +00001800 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001801 DebugLoc DL;
1802 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001803 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001804
Roman Divackyc9e23d92012-09-12 14:47:47 +00001805 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1806 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001807 // Only Darwin actually uses the VRSAVE register, but it can still appear
1808 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1809 // Darwin, ignore it.
1810 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1811 continue;
1812
Roman Divackyc9e23d92012-09-12 14:47:47 +00001813 // CR2 through CR4 are the nonvolatile CR fields.
1814 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1815
Roman Divackyc9e23d92012-09-12 14:47:47 +00001816 // Add the callee-saved register as live-in; it's killed at the spill.
1817 MBB.addLiveIn(Reg);
1818
Hal Finkel2f293912013-04-13 23:06:15 +00001819 if (CRSpilled && IsCRField) {
1820 CRMIB.addReg(Reg, RegState::ImplicitKill);
1821 continue;
1822 }
1823
Roman Divackyc9e23d92012-09-12 14:47:47 +00001824 // Insert the spill to the stack frame.
1825 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001826 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001827 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001828 // The actual spill will happen at the start of the prologue.
1829 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001830 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001831 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001832 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001833
Eric Christopherd1737492014-04-29 00:16:40 +00001834 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1835 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1836 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001837 .addReg(Reg, RegState::ImplicitKill);
1838
Eric Christopherd1737492014-04-29 00:16:40 +00001839 MBB.insert(MI, CRMIB);
1840 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1841 .addReg(PPC::R12,
1842 getKillRegState(true)),
1843 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001844 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001845 } else {
1846 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1847 TII.storeRegToStackSlot(MBB, MI, Reg, true,
Eric Christopherd1737492014-04-29 00:16:40 +00001848 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001849 }
1850 }
1851 return true;
1852}
1853
1854static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00001855restoreCRs(bool isPPC64, bool is31,
1856 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001857 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1858 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001859
1860 MachineFunction *MF = MBB.getParent();
Eric Christophercccae792015-01-30 22:02:31 +00001861 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001862 DebugLoc DL;
1863 unsigned RestoreOp, MoveReg;
1864
Hal Finkel67369882013-04-15 02:07:05 +00001865 if (isPPC64)
1866 // This is handled during epilogue generation.
1867 return;
1868 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001869 // 32-bit: FP-relative
1870 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00001871 PPC::R12),
1872 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00001873 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001874 MoveReg = PPC::R12;
1875 }
Eric Christopherd1737492014-04-29 00:16:40 +00001876
Roman Divackyc9e23d92012-09-12 14:47:47 +00001877 if (CR2Spilled)
1878 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00001879 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001880
1881 if (CR3Spilled)
1882 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00001883 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001884
1885 if (CR4Spilled)
1886 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00001887 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001888}
1889
Hans Wennborge1a2e902016-03-31 18:33:38 +00001890MachineBasicBlock::iterator PPCFrameLowering::
Eli Bendersky8da87162013-02-21 20:05:00 +00001891eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1892 MachineBasicBlock::iterator I) const {
Eric Christopher38522b82015-01-30 02:11:26 +00001893 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
Eli Bendersky8da87162013-02-21 20:05:00 +00001894 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1895 I->getOpcode() == PPC::ADJCALLSTACKUP) {
1896 // Add (actually subtract) back the amount the callee popped on return.
1897 if (int CalleeAmt = I->getOperand(1).getImm()) {
1898 bool is64Bit = Subtarget.isPPC64();
1899 CalleeAmt *= -1;
1900 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1901 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1902 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1903 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1904 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1905 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +00001906 const DebugLoc &dl = I->getDebugLoc();
Eli Bendersky8da87162013-02-21 20:05:00 +00001907
1908 if (isInt<16>(CalleeAmt)) {
1909 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1910 .addReg(StackReg, RegState::Kill)
1911 .addImm(CalleeAmt);
1912 } else {
1913 MachineBasicBlock::iterator MBBI = I;
1914 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1915 .addImm(CalleeAmt >> 16);
1916 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1917 .addReg(TmpReg, RegState::Kill)
1918 .addImm(CalleeAmt & 0xFFFF);
1919 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1920 .addReg(StackReg, RegState::Kill)
1921 .addReg(TmpReg);
1922 }
1923 }
1924 }
1925 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
Hans Wennborge1a2e902016-03-31 18:33:38 +00001926 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00001927}
1928
Eric Christopherd1737492014-04-29 00:16:40 +00001929bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001930PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001931 MachineBasicBlock::iterator MI,
1932 const std::vector<CalleeSavedInfo> &CSI,
1933 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001934
1935 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1936 // Return false otherwise to maintain pre-existing behavior.
1937 if (!Subtarget.isSVR4ABI())
1938 return false;
1939
1940 MachineFunction *MF = MBB.getParent();
1941 const PPCInstrInfo &TII =
Eric Christopher38522b82015-01-30 02:11:26 +00001942 *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
Roman Divackyc9e23d92012-09-12 14:47:47 +00001943 bool CR2Spilled = false;
1944 bool CR3Spilled = false;
1945 bool CR4Spilled = false;
1946 unsigned CSIIndex = 0;
1947
1948 // Initialize insertion-point logic; we will be restoring in reverse
1949 // order of spill.
1950 MachineBasicBlock::iterator I = MI, BeforeI = I;
1951 bool AtStart = I == MBB.begin();
1952
1953 if (!AtStart)
1954 --BeforeI;
1955
1956 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1957 unsigned Reg = CSI[i].getReg();
1958
Hal Finkelac1a24b2013-06-28 22:29:56 +00001959 // Only Darwin actually uses the VRSAVE register, but it can still appear
1960 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1961 // Darwin, ignore it.
1962 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1963 continue;
1964
Roman Divackyc9e23d92012-09-12 14:47:47 +00001965 if (Reg == PPC::CR2) {
1966 CR2Spilled = true;
1967 // The spill slot is associated only with CR2, which is the
1968 // first nonvolatile spilled. Save it here.
1969 CSIIndex = i;
1970 continue;
1971 } else if (Reg == PPC::CR3) {
1972 CR3Spilled = true;
1973 continue;
1974 } else if (Reg == PPC::CR4) {
1975 CR4Spilled = true;
1976 continue;
1977 } else {
1978 // When we first encounter a non-CR register after seeing at
1979 // least one CR register, restore all spilled CRs together.
1980 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00001981 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00001982 bool is31 = needsFP(*MF);
1983 restoreCRs(Subtarget.isPPC64(), is31,
1984 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00001985 MBB, I, CSI, CSIIndex);
1986 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001987 }
1988
1989 // Default behavior for non-CR saves.
1990 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1991 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00001992 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001993 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00001994 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00001995 }
1996
1997 // Insert in reverse order.
1998 if (AtStart)
1999 I = MBB.begin();
2000 else {
2001 I = BeforeI;
2002 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00002003 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00002004 }
2005
2006 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00002007 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00002008 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00002009 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00002010 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00002011 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00002012
2013 return true;
2014}
Kit Bartond3b904d2015-09-10 01:55:44 +00002015
2016bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
Kit Bartonf4ce2f32015-11-30 18:59:41 +00002017 return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() &&
2018 MF.getSubtarget<PPCSubtarget>().isPPC64());
Kit Bartond3b904d2015-09-10 01:55:44 +00002019}