blob: 55d740821d370d35927b61d9d82f99e32b1157ce [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.
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000315 for (std::pair<unsigned, unsigned> LI : MF->getRegInfo().liveins()) {
316 unsigned RegNo = TRI->getEncodingValue(LI.first);
317 if (VRRegNo[RegNo] == LI.first) // If this really is a vector reg.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000318 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
319 }
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000320
321 // Live out registers appear as use operands on return instructions.
322 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
323 UsedRegMask != 0 && BI != BE; ++BI) {
324 const MachineBasicBlock &MBB = *BI;
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000325 if (!MBB.isReturnBlock())
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000326 continue;
327 const MachineInstr &Ret = MBB.back();
328 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
329 const MachineOperand &MO = Ret.getOperand(I);
330 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
331 continue;
Hal Finkelfeea6532013-03-26 20:08:20 +0000332 unsigned RegNo = TRI->getEncodingValue(MO.getReg());
Jakob Stoklund Olesenbf034db2013-02-05 17:40:36 +0000333 UsedRegMask &= ~(1 << (31-RegNo));
334 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000335 }
336
337 // If no registers are used, turn this into a copy.
338 if (UsedRegMask == 0) {
339 // Remove all VRSAVE code.
340 RemoveVRSaveCode(MI);
341 return;
342 }
343
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000344 unsigned SrcReg = MI.getOperand(1).getReg();
345 unsigned DstReg = MI.getOperand(0).getReg();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000346
347 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
348 if (DstReg != SrcReg)
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000349 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
350 .addReg(SrcReg)
351 .addImm(UsedRegMask);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000352 else
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000353 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
354 .addReg(SrcReg, RegState::Kill)
355 .addImm(UsedRegMask);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000356 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
357 if (DstReg != SrcReg)
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000358 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
359 .addReg(SrcReg)
360 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000361 else
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000362 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
363 .addReg(SrcReg, RegState::Kill)
364 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000365 } else {
366 if (DstReg != SrcReg)
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000367 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
368 .addReg(SrcReg)
369 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000370 else
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000371 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
372 .addReg(SrcReg, RegState::Kill)
373 .addImm(UsedRegMask >> 16);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000374
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000375 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
376 .addReg(DstReg, RegState::Kill)
377 .addImm(UsedRegMask & 0xFFFF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000378 }
379
380 // Remove the old UPDATE_VRSAVE instruction.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000381 MI.eraseFromParent();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000382}
383
Roman Divackyc9e23d92012-09-12 14:47:47 +0000384static bool spillsCR(const MachineFunction &MF) {
385 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
386 return FuncInfo->isCRSpilled();
387}
388
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000389static bool spillsVRSAVE(const MachineFunction &MF) {
390 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
391 return FuncInfo->isVRSAVESpilled();
392}
393
Hal Finkelbb420f12013-03-15 05:06:04 +0000394static bool hasSpills(const MachineFunction &MF) {
395 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
396 return FuncInfo->hasSpills();
397}
398
Hal Finkelfcc51d42013-03-17 04:43:44 +0000399static bool hasNonRISpills(const MachineFunction &MF) {
400 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
401 return FuncInfo->hasNonRISpills();
402}
403
Bill Schmidt82f1c772015-02-10 19:09:05 +0000404/// MustSaveLR - Return true if this function requires that we save the LR
405/// register onto the stack in the prolog and restore it in the epilog of the
406/// function.
407static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
408 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
409
410 // We need a save/restore of LR if there is any def of LR (which is
411 // defined by calls, including the PIC setup sequence), or if there is
412 // some use of the LR stack slot (e.g. for builtin_return_address).
413 // (LR comes in 32 and 64 bit versions.)
414 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
415 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
416}
417
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000418/// determineFrameLayout - Determine the size of the frame and maximum call
419/// frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000420unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
421 bool UpdateMF,
422 bool UseEstimate) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000423 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000424
425 // Get the number of bytes to allocate from the FrameInfo
Hal Finkelbb420f12013-03-15 05:06:04 +0000426 unsigned FrameSize =
Matthias Braun941a7052016-07-28 18:40:00 +0000427 UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000428
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000429 // Get stack alignments. The frame must be aligned to the greatest of these:
430 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
Matthias Braun941a7052016-07-28 18:40:00 +0000431 unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame
Hal Finkela7c54e82013-07-17 00:45:52 +0000432 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
433
Eric Christopherb128abc2017-02-04 01:52:17 +0000434 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000435
Bill Schmidt82f1c772015-02-10 19:09:05 +0000436 unsigned LR = RegInfo->getRARegister();
Matthias Braunf1caa282017-12-15 22:22:58 +0000437 bool DisableRedZone = MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
Tony Jiangd5acad02017-07-11 16:42:20 +0000438 bool CanUseRedZone = !MFI.hasVarSizedObjects() && // No dynamic alloca.
439 !MFI.adjustsStack() && // No calls.
440 !MustSaveLR(MF, LR) && // No need to save LR.
441 !RegInfo->hasBasePointer(MF); // No special alignment.
442
443 // Note: for PPC32 SVR4ABI (Non-DarwinABI), we can still generate stackless
444 // code if all local vars are reg-allocated.
445 bool FitsInRedZone = FrameSize <= Subtarget.getRedZoneSize();
446
447 // Check whether we can skip adjusting the stack pointer (by using red zone)
448 if (!DisableRedZone && CanUseRedZone && FitsInRedZone) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000449 // No need for frame
Hal Finkelbb420f12013-03-15 05:06:04 +0000450 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000451 MFI.setStackSize(0);
Hal Finkelbb420f12013-03-15 05:06:04 +0000452 return 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000453 }
454
455 // Get the maximum call frame size of all the calls.
Matthias Braun941a7052016-07-28 18:40:00 +0000456 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000457
Ulrich Weigandf316e1d2014-06-23 13:47:52 +0000458 // Maximum call frame needs to be at least big enough for linkage area.
Eric Christophera4ae2132015-02-13 22:22:57 +0000459 unsigned minCallFrameSize = getLinkageSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000460 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
461
462 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
463 // that allocations will be aligned.
Matthias Braun941a7052016-07-28 18:40:00 +0000464 if (MFI.hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000465 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
466
467 // Update maximum call frame size.
Hal Finkelbb420f12013-03-15 05:06:04 +0000468 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000469 MFI.setMaxCallFrameSize(maxCallFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000470
471 // Include call frame size in total.
472 FrameSize += maxCallFrameSize;
473
474 // Make sure the frame is aligned.
475 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
476
477 // Update frame info.
Hal Finkelbb420f12013-03-15 05:06:04 +0000478 if (UpdateMF)
Matthias Braun941a7052016-07-28 18:40:00 +0000479 MFI.setStackSize(FrameSize);
Hal Finkelbb420f12013-03-15 05:06:04 +0000480
481 return FrameSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000482}
483
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000484// hasFP - Return true if the specified function actually has a dedicated frame
485// pointer register.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000486bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000487 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000488 // FIXME: This is pretty much broken by design: hasFP() might be called really
489 // early, before the stack layout was calculated and thus hasFP() might return
490 // true or false here depending on the time of call.
Matthias Braun941a7052016-07-28 18:40:00 +0000491 return (MFI.getStackSize()) && needsFP(MF);
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000492}
493
494// needsFP - Return true if the specified function should have a dedicated frame
495// pointer register. This is true if the function has variable sized allocas or
496// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000497bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000498 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000499
500 // Naked functions have no stack frame pushed, so we don't have a frame
501 // pointer.
Matthias Braunf1caa282017-12-15 22:22:58 +0000502 if (MF.getFunction().hasFnAttribute(Attribute::Naked))
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000503 return false;
504
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000505 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000506 MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000507 (MF.getTarget().Options.GuaranteedTailCallOpt &&
508 MF.getInfo<PPCFunctionInfo>()->hasFastCall());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000509}
510
Hal Finkelaa03c032013-03-21 19:03:19 +0000511void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
512 bool is31 = needsFP(MF);
513 unsigned FPReg = is31 ? PPC::R31 : PPC::R1;
514 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
515
Eric Christopherb128abc2017-02-04 01:52:17 +0000516 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Hal Finkelf05d6c72013-07-17 23:50:51 +0000517 bool HasBP = RegInfo->hasBasePointer(MF);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000518 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
Hiroshi Inoue1d5693c2017-06-22 04:33:44 +0000519 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FP8Reg;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000520
Hal Finkelaa03c032013-03-21 19:03:19 +0000521 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
522 BI != BE; ++BI)
523 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
524 --MBBI;
525 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
526 MachineOperand &MO = MBBI->getOperand(I);
527 if (!MO.isReg())
528 continue;
529
530 switch (MO.getReg()) {
531 case PPC::FP:
532 MO.setReg(FPReg);
533 break;
534 case PPC::FP8:
535 MO.setReg(FP8Reg);
536 break;
Hal Finkelf05d6c72013-07-17 23:50:51 +0000537 case PPC::BP:
538 MO.setReg(BPReg);
539 break;
540 case PPC::BP8:
541 MO.setReg(BP8Reg);
542 break;
543
Hal Finkelaa03c032013-03-21 19:03:19 +0000544 }
545 }
546 }
547}
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000548
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000549/* This function will do the following:
550 - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
551 respectively (defaults recommended by the ABI) and return true
552 - If MBB is not an entry block, initialize the register scavenger and look
553 for available registers.
554 - If the defaults (R0/R12) are available, return true
555 - If TwoUniqueRegsRequired is set to true, it looks for two unique
556 registers. Otherwise, look for a single available register.
557 - If the required registers are found, set SR1 and SR2 and return true.
558 - If the required registers are not found, set SR2 or both SR1 and SR2 to
559 PPC::NoRegister and return false.
560
561 Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
562 is not set, this function will attempt to find two different registers, but
563 still return true if only one register is available (and set SR1 == SR2).
564*/
565bool
566PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
567 bool UseAtEnd,
568 bool TwoUniqueRegsRequired,
569 unsigned *SR1,
570 unsigned *SR2) const {
Kit Barton9c432ae2015-11-16 20:22:15 +0000571 RegScavenger RS;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000572 unsigned R0 = Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
573 unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
Kit Barton9c432ae2015-11-16 20:22:15 +0000574
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000575 // Set the defaults for the two scratch registers.
576 if (SR1)
577 *SR1 = R0;
Kit Barton9c432ae2015-11-16 20:22:15 +0000578
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000579 if (SR2) {
580 assert (SR1 && "Asking for the second scratch register but not the first?");
581 *SR2 = R12;
582 }
583
584 // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
Kit Barton9c432ae2015-11-16 20:22:15 +0000585 if ((UseAtEnd && MBB->isReturnBlock()) ||
586 (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
587 return true;
Alexey Samsonov39b7d652015-12-02 21:25:28 +0000588
Matthias Braun7dc03f02016-04-06 02:47:09 +0000589 RS.enterBasicBlock(*MBB);
Kit Barton9c432ae2015-11-16 20:22:15 +0000590
Kit Bartonf4ce2f32015-11-30 18:59:41 +0000591 if (UseAtEnd && !MBB->empty()) {
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000592 // The scratch register will be used at the end of the block, so must
593 // consider all registers used within the block
Kit Bartonf4ce2f32015-11-30 18:59:41 +0000594
595 MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
596 // If no terminator, back iterator up to previous instruction.
597 if (MBBI == MBB->end())
598 MBBI = std::prev(MBBI);
599
600 if (MBBI != MBB->begin())
601 RS.forward(MBBI);
602 }
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000603
604 // If the two registers are available, we're all good.
605 // Note that we only return here if both R0 and R12 are available because
606 // although the function may not require two unique registers, it may benefit
607 // from having two so we should try to provide them.
608 if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12))
Kit Barton9c432ae2015-11-16 20:22:15 +0000609 return true;
610
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000611 // Get the list of callee-saved registers for the target.
Eric Christopherb128abc2017-02-04 01:52:17 +0000612 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000613 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent());
614
615 // Get all the available registers in the block.
616 BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass :
617 &PPC::GPRCRegClass);
618
619 // We shouldn't use callee-saved registers as scratch registers as they may be
620 // available when looking for a candidate block for shrink wrapping but not
621 // available when the actual prologue/epilogue is being emitted because they
622 // were added as live-in to the prologue block by PrologueEpilogueInserter.
623 for (int i = 0; CSRegs[i]; ++i)
624 BV.reset(CSRegs[i]);
625
626 // Set the first scratch register to the first available one.
627 if (SR1) {
628 int FirstScratchReg = BV.find_first();
629 *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
630 }
631
632 // If there is another one available, set the second scratch register to that.
633 // Otherwise, set it to either PPC::NoRegister if this function requires two
634 // or to whatever SR1 is set to if this function doesn't require two.
635 if (SR2) {
636 int SecondScratchReg = BV.find_next(*SR1);
637 if (SecondScratchReg != -1)
638 *SR2 = SecondScratchReg;
639 else
640 *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1;
641 }
642
643 // Now that we've done our best to provide both registers, double check
644 // whether we were unable to provide enough.
Aaron Ballman8374c1f2016-02-23 15:02:43 +0000645 if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
Kit Barton9c432ae2015-11-16 20:22:15 +0000646 return false;
647
Kit Barton9c432ae2015-11-16 20:22:15 +0000648 return true;
649}
650
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000651// We need a scratch register for spilling LR and for spilling CR. By default,
652// we use two scratch registers to hide latency. However, if only one scratch
653// register is available, we can adjust for that by not overlapping the spill
654// code. However, if we need to realign the stack (i.e. have a base pointer)
655// and the stack frame is large, we need two scratch registers.
656bool
657PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
Eric Christopherb128abc2017-02-04 01:52:17 +0000658 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000659 MachineFunction &MF = *(MBB->getParent());
660 bool HasBP = RegInfo->hasBasePointer(MF);
661 unsigned FrameSize = determineFrameLayout(MF, false);
662 int NegFrameSize = -FrameSize;
663 bool IsLargeFrame = !isInt<16>(NegFrameSize);
Matthias Braun941a7052016-07-28 18:40:00 +0000664 MachineFrameInfo &MFI = MF.getFrameInfo();
665 unsigned MaxAlign = MFI.getMaxAlignment();
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000666 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000667
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000668 return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000669}
670
Kit Barton9c432ae2015-11-16 20:22:15 +0000671bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
672 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
673
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000674 return findScratchRegister(TmpMBB, false,
675 twoUniqueScratchRegsRequired(TmpMBB));
Kit Barton9c432ae2015-11-16 20:22:15 +0000676}
677
678bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
679 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
680
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000681 return findScratchRegister(TmpMBB, true);
Kit Barton9c432ae2015-11-16 20:22:15 +0000682}
683
Quentin Colombet61b305e2015-05-05 17:38:16 +0000684void PPCFrameLowering::emitPrologue(MachineFunction &MF,
685 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000686 MachineBasicBlock::iterator MBBI = MBB.begin();
Matthias Braun941a7052016-07-28 18:40:00 +0000687 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherb128abc2017-02-04 01:52:17 +0000688 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
689 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000690
691 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000692 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000693 DebugLoc dl;
Jay Foad1f0a44e2014-12-01 09:42:32 +0000694 bool needsCFI = MMI.hasDebugInfo() ||
Matthias Braunf1caa282017-12-15 22:22:58 +0000695 MF.getFunction().needsUnwindTableEntry();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000696
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000697 // Get processor type.
698 bool isPPC64 = Subtarget.isPPC64();
699 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000700 bool isSVR4ABI = Subtarget.isSVR4ABI();
Ulrich Weigandbe928cc2014-07-21 00:03:18 +0000701 bool isELFv2ABI = Subtarget.isELFv2ABI();
Chandler Carruth003ed332015-02-14 09:14:44 +0000702 assert((Subtarget.isDarwinABI() || isSVR4ABI) &&
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000703 "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
704
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000705 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
706 // process it.
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000707 if (!isSVR4ABI)
Bill Schmidt38d94582012-10-10 20:54:15 +0000708 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
709 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000710 HandleVRSaveUpdate(*MBBI, TII);
Bill Schmidt38d94582012-10-10 20:54:15 +0000711 break;
712 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000713 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000714
Kit Bartond3b904d2015-09-10 01:55:44 +0000715 // Move MBBI back to the beginning of the prologue block.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000716 MBBI = MBB.begin();
717
718 // Work out frame sizes.
Hal Finkelbb420f12013-03-15 05:06:04 +0000719 unsigned FrameSize = determineFrameLayout(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000720 int NegFrameSize = -FrameSize;
Hal Finkela7c54e82013-07-17 00:45:52 +0000721 if (!isInt<32>(NegFrameSize))
722 llvm_unreachable("Unhandled stack size!");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000723
Matthias Braun941a7052016-07-28 18:40:00 +0000724 if (MFI.isFrameAddressTaken())
Hal Finkelaa03c032013-03-21 19:03:19 +0000725 replaceFPWithRealFP(MF);
726
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000727 // Check if the link register (LR) must be saved.
728 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
729 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +0000730 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000731 bool MustSaveCR = !MustSaveCRs.empty();
Bill Schmidtf381afc2013-08-20 03:12:23 +0000732 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000733 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +0000734 bool HasBP = RegInfo->hasBasePointer(MF);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000735 bool HasRedZone = isPPC64 || !isSVR4ABI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000736
Bill Schmidtf381afc2013-08-20 03:12:23 +0000737 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +0000738 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000739 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
740 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR;
Kit Barton9c432ae2015-11-16 20:22:15 +0000741 unsigned ScratchReg = 0;
Bill Schmidtf381afc2013-08-20 03:12:23 +0000742 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
743 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
744 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
745 : PPC::MFLR );
746 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
747 : PPC::STW );
748 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
749 : PPC::STWU );
750 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
751 : PPC::STWUX);
752 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
753 : PPC::LIS );
754 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
755 : PPC::ORI );
756 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
757 : PPC::OR );
758 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
759 : PPC::SUBFC);
760 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
761 : PPC::SUBFIC);
762
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000763 // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
764 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
765 // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
766 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
767 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
768 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
769
Simon Pilgrimfbd22212016-11-20 13:10:51 +0000770 // Using the same bool variable as below to suppress compiler warnings.
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +0000771 bool SingleScratchReg =
772 findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB),
773 &ScratchReg, &TempReg);
774 assert(SingleScratchReg &&
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000775 "Required number of registers not available in this block");
776
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +0000777 SingleScratchReg = ScratchReg == TempReg;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000778
Eric Christopherf71609b2015-02-13 00:39:27 +0000779 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000780
781 int FPOffset = 0;
782 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000783 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +0000784 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000785 int FPIndex = FI->getFramePointerSaveIndex();
786 assert(FPIndex && "No Frame Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000787 FPOffset = MFI.getObjectOffset(FPIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000788 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000789 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000790 }
791 }
792
Hal Finkela7c54e82013-07-17 00:45:52 +0000793 int BPOffset = 0;
794 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000795 if (isSVR4ABI) {
Matthias Braun941a7052016-07-28 18:40:00 +0000796 MachineFrameInfo &MFI = MF.getFrameInfo();
Hal Finkela7c54e82013-07-17 00:45:52 +0000797 int BPIndex = FI->getBasePointerSaveIndex();
798 assert(BPIndex && "No Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000799 BPOffset = MFI.getObjectOffset(BPIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +0000800 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +0000801 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +0000802 }
803 }
804
Justin Hibbits654346e2015-01-10 01:57:21 +0000805 int PBPOffset = 0;
806 if (FI->usesPICBase()) {
Matthias Braun941a7052016-07-28 18:40:00 +0000807 MachineFrameInfo &MFI = MF.getFrameInfo();
Justin Hibbits654346e2015-01-10 01:57:21 +0000808 int PBPIndex = FI->getPICBasePointerSaveIndex();
809 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +0000810 PBPOffset = MFI.getObjectOffset(PBPIndex);
Justin Hibbits654346e2015-01-10 01:57:21 +0000811 }
812
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000813 // Get stack alignments.
Matthias Braun941a7052016-07-28 18:40:00 +0000814 unsigned MaxAlign = MFI.getMaxAlignment();
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000815 if (HasBP && MaxAlign > 1)
816 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
817 "Invalid alignment!");
818
819 // Frames of 32KB & larger require special handling because they cannot be
820 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
821 bool isLargeFrame = !isInt<16>(NegFrameSize);
822
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000823 assert((isPPC64 || !MustSaveCR) &&
824 "Prologue CR saving supported only in 64-bit mode");
825
826 // If we need to spill the CR and the LR but we don't have two separate
827 // registers available, we must spill them one at a time
828 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000829 // In the ELFv2 ABI, we are not required to save all CR fields.
830 // If only one or two CR fields are clobbered, it is more efficient to use
831 // mfocrf to selectively save just those fields, because mfocrf has short
832 // latency compares to mfcr.
833 unsigned MfcrOpcode = PPC::MFCR8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000834 unsigned CrState = RegState::ImplicitKill;
835 if (isELFv2ABI && MustSaveCRs.size() == 1) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000836 MfcrOpcode = PPC::MFOCRF8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000837 CrState = RegState::Kill;
838 }
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000839 MachineInstrBuilder MIB =
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000840 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000841 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000842 MIB.addReg(MustSaveCRs[i], CrState);
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000843 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
844 .addReg(TempReg, getKillRegState(true))
845 .addImm(8)
846 .addReg(SPReg);
847 }
848
Bill Schmidtf381afc2013-08-20 03:12:23 +0000849 if (MustSaveLR)
850 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000851
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000852 if (MustSaveCR &&
853 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000854 // In the ELFv2 ABI, we are not required to save all CR fields.
855 // If only one or two CR fields are clobbered, it is more efficient to use
856 // mfocrf to selectively save just those fields, because mfocrf has short
857 // latency compares to mfcr.
858 unsigned MfcrOpcode = PPC::MFCR8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000859 unsigned CrState = RegState::ImplicitKill;
860 if (isELFv2ABI && MustSaveCRs.size() == 1) {
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000861 MfcrOpcode = PPC::MFOCRF8;
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000862 CrState = RegState::Kill;
863 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000864 MachineInstrBuilder MIB =
Chuang-Yu Cheng6efde2f2016-04-12 03:04:44 +0000865 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000866 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
Chuang-Yu Cheng8676c3d2016-04-27 02:59:28 +0000867 MIB.addReg(MustSaveCRs[i], CrState);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000868 }
869
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000870 if (HasRedZone) {
871 if (HasFP)
872 BuildMI(MBB, MBBI, dl, StoreInst)
873 .addReg(FPReg)
874 .addImm(FPOffset)
875 .addReg(SPReg);
876 if (FI->usesPICBase())
877 BuildMI(MBB, MBBI, dl, StoreInst)
878 .addReg(PPC::R30)
879 .addImm(PBPOffset)
880 .addReg(SPReg);
881 if (HasBP)
882 BuildMI(MBB, MBBI, dl, StoreInst)
883 .addReg(BPReg)
884 .addImm(BPOffset)
885 .addReg(SPReg);
886 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000887
888 if (MustSaveLR)
Stefan Pintilie70bfe662018-01-12 13:12:49 +0000889 BuildMI(MBB, MBBI, dl, StoreInst)
Nemanja Ivanovic62fba482016-07-15 19:56:32 +0000890 .addReg(ScratchReg, getKillRegState(true))
Bill Schmidtf381afc2013-08-20 03:12:23 +0000891 .addImm(LROffset)
892 .addReg(SPReg);
893
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000894 if (MustSaveCR &&
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000895 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
896 assert(HasRedZone && "A red zone is always available on PPC64");
Bill Schmidtf381afc2013-08-20 03:12:23 +0000897 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
898 .addReg(TempReg, getKillRegState(true))
899 .addImm(8)
900 .addReg(SPReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000901 }
Bill Schmidtf381afc2013-08-20 03:12:23 +0000902
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000903 // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000904 if (!FrameSize)
905 return;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000906
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000907 // Adjust stack pointer: r1 += NegFrameSize.
908 // If there is a preferred stack alignment, align R1 now
Hal Finkela7c54e82013-07-17 00:45:52 +0000909
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000910 if (HasBP && HasRedZone) {
Bill Schmidtf381afc2013-08-20 03:12:23 +0000911 // Save a copy of r1 as the base pointer.
912 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
913 .addReg(SPReg)
914 .addReg(SPReg);
915 }
916
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000917 // Have we generated a STUX instruction to claim stack frame? If so,
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +0000918 // the negated frame size will be placed in ScratchReg.
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000919 bool HasSTUX = false;
920
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000921 // This condition must be kept in sync with canUseAsPrologue.
Bill Schmidtf381afc2013-08-20 03:12:23 +0000922 if (HasBP && MaxAlign > 1) {
923 if (isPPC64)
924 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
925 .addReg(SPReg)
926 .addImm(0)
927 .addImm(64 - Log2_32(MaxAlign));
928 else // PPC32...
929 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
930 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000931 .addImm(0)
932 .addImm(32 - Log2_32(MaxAlign))
933 .addImm(31);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000934 if (!isLargeFrame) {
935 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
936 .addReg(ScratchReg, RegState::Kill)
937 .addImm(NegFrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000938 } else {
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +0000939 assert(!SingleScratchReg && "Only a single scratch reg available");
Bill Schmidtf381afc2013-08-20 03:12:23 +0000940 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000941 .addImm(NegFrameSize >> 16);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000942 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
943 .addReg(TempReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000944 .addImm(NegFrameSize & 0xFFFF);
Bill Schmidtf381afc2013-08-20 03:12:23 +0000945 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
946 .addReg(ScratchReg, RegState::Kill)
947 .addReg(TempReg, RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000948 }
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000949
Bill Schmidtf381afc2013-08-20 03:12:23 +0000950 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
951 .addReg(SPReg, RegState::Kill)
952 .addReg(SPReg)
953 .addReg(ScratchReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000954 HasSTUX = true;
Hal Finkela7c54e82013-07-17 00:45:52 +0000955
Bill Schmidtf381afc2013-08-20 03:12:23 +0000956 } else if (!isLargeFrame) {
Stefan Pintilie70bfe662018-01-12 13:12:49 +0000957 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +0000958 .addReg(SPReg)
959 .addImm(NegFrameSize)
960 .addReg(SPReg);
Bill Schmidt8893a3d2013-08-16 20:05:04 +0000961
Bill Schmidtf381afc2013-08-20 03:12:23 +0000962 } else {
963 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
964 .addImm(NegFrameSize >> 16);
965 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
966 .addReg(ScratchReg, RegState::Kill)
967 .addImm(NegFrameSize & 0xFFFF);
968 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
969 .addReg(SPReg, RegState::Kill)
970 .addReg(SPReg)
971 .addReg(ScratchReg);
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000972 HasSTUX = true;
973 }
974
975 if (!HasRedZone) {
976 assert(!isPPC64 && "A red zone is always available on PPC64");
977 if (HasSTUX) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +0000978 // The negated frame size is in ScratchReg, and the SPReg has been
979 // decremented by the frame size: SPReg = old SPReg + ScratchReg.
980 // Since FPOffset, PBPOffset, etc. are relative to the beginning of
981 // the stack frame (i.e. the old SP), ideally, we would put the old
982 // SP into a register and use it as the base for the stores. The
983 // problem is that the only available register may be ScratchReg,
984 // which could be R0, and R0 cannot be used as a base address.
985
986 // First, set ScratchReg to the old SP. This may need to be modified
987 // later.
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +0000988 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg)
989 .addReg(ScratchReg, RegState::Kill)
990 .addReg(SPReg);
991
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +0000992 if (ScratchReg == PPC::R0) {
993 // R0 cannot be used as a base register, but it can be used as an
994 // index in a store-indexed.
995 int LastOffset = 0;
996 if (HasFP) {
997 // R0 += (FPOffset-LastOffset).
998 // Need addic, since addi treats R0 as 0.
999 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1000 .addReg(ScratchReg)
1001 .addImm(FPOffset-LastOffset);
1002 LastOffset = FPOffset;
1003 // Store FP into *R0.
1004 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1005 .addReg(FPReg, RegState::Kill) // Save FP.
1006 .addReg(PPC::ZERO)
1007 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1008 }
1009 if (FI->usesPICBase()) {
1010 // R0 += (PBPOffset-LastOffset).
1011 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1012 .addReg(ScratchReg)
1013 .addImm(PBPOffset-LastOffset);
1014 LastOffset = PBPOffset;
1015 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1016 .addReg(PPC::R30, RegState::Kill) // Save PIC base pointer.
1017 .addReg(PPC::ZERO)
1018 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1019 }
1020 if (HasBP) {
1021 // R0 += (BPOffset-LastOffset).
1022 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1023 .addReg(ScratchReg)
1024 .addImm(BPOffset-LastOffset);
1025 LastOffset = BPOffset;
1026 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1027 .addReg(BPReg, RegState::Kill) // Save BP.
1028 .addReg(PPC::ZERO)
1029 .addReg(ScratchReg); // This will be the index (R0 is ok here).
1030 // BP = R0-LastOffset
1031 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), BPReg)
1032 .addReg(ScratchReg, RegState::Kill)
1033 .addImm(-LastOffset);
1034 }
1035 } else {
1036 // ScratchReg is not R0, so use it as the base register. It is
1037 // already set to the old SP, so we can use the offsets directly.
1038
1039 // Now that the stack frame has been allocated, save all the necessary
1040 // registers using ScratchReg as the base address.
1041 if (HasFP)
1042 BuildMI(MBB, MBBI, dl, StoreInst)
1043 .addReg(FPReg)
1044 .addImm(FPOffset)
1045 .addReg(ScratchReg);
1046 if (FI->usesPICBase())
1047 BuildMI(MBB, MBBI, dl, StoreInst)
1048 .addReg(PPC::R30)
1049 .addImm(PBPOffset)
1050 .addReg(ScratchReg);
1051 if (HasBP) {
1052 BuildMI(MBB, MBBI, dl, StoreInst)
1053 .addReg(BPReg)
1054 .addImm(BPOffset)
1055 .addReg(ScratchReg);
1056 BuildMI(MBB, MBBI, dl, OrInst, BPReg)
1057 .addReg(ScratchReg, RegState::Kill)
1058 .addReg(ScratchReg);
1059 }
Krzysztof Parzyszek020ec292016-09-06 12:30:00 +00001060 }
1061 } else {
1062 // The frame size is a known 16-bit constant (fitting in the immediate
1063 // field of STWU). To be here we have to be compiling for PPC32.
1064 // Since the SPReg has been decreased by FrameSize, add it back to each
1065 // offset.
1066 if (HasFP)
1067 BuildMI(MBB, MBBI, dl, StoreInst)
1068 .addReg(FPReg)
1069 .addImm(FrameSize + FPOffset)
1070 .addReg(SPReg);
1071 if (FI->usesPICBase())
1072 BuildMI(MBB, MBBI, dl, StoreInst)
1073 .addReg(PPC::R30)
1074 .addImm(FrameSize + PBPOffset)
1075 .addReg(SPReg);
1076 if (HasBP) {
1077 BuildMI(MBB, MBBI, dl, StoreInst)
1078 .addReg(BPReg)
1079 .addImm(FrameSize + BPOffset)
1080 .addReg(SPReg);
1081 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg)
1082 .addReg(SPReg)
1083 .addImm(FrameSize);
1084 }
1085 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001086 }
1087
Jay Foad1f0a44e2014-12-01 09:42:32 +00001088 // Add Call Frame Information for the instructions we generated above.
1089 if (needsCFI) {
1090 unsigned CFIIndex;
1091
1092 if (HasBP) {
1093 // Define CFA in terms of BP. Do this in preference to using FP/SP,
1094 // because if the stack needed aligning then CFA won't be at a fixed
1095 // offset from FP/SP.
1096 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001097 CFIIndex = MF.addFrameInst(
Jay Foad1f0a44e2014-12-01 09:42:32 +00001098 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1099 } else {
1100 // Adjust the definition of CFA to account for the change in SP.
1101 assert(NegFrameSize);
Matthias Braunf23ef432016-11-30 23:48:42 +00001102 CFIIndex = MF.addFrameInst(
Jay Foad1f0a44e2014-12-01 09:42:32 +00001103 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
1104 }
Eric Christopher612bb692014-04-29 00:16:46 +00001105 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1106 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001107
1108 if (HasFP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001109 // Describe where FP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001110 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001111 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001112 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001113 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001114 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001115 }
1116
Justin Hibbits654346e2015-01-10 01:57:21 +00001117 if (FI->usesPICBase()) {
1118 // Describe where FP was saved, at a fixed offset from CFA.
1119 unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001120 CFIIndex = MF.addFrameInst(
Justin Hibbits654346e2015-01-10 01:57:21 +00001121 MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
1122 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1123 .addCFIIndex(CFIIndex);
1124 }
1125
Hal Finkela7c54e82013-07-17 00:45:52 +00001126 if (HasBP) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001127 // Describe where BP was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001128 unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001129 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001130 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001131 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001132 .addCFIIndex(CFIIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +00001133 }
1134
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001135 if (MustSaveLR) {
Jay Foad1f0a44e2014-12-01 09:42:32 +00001136 // Describe where LR was saved, at a fixed offset from CFA.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001137 unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001138 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001139 MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
Eric Christopher612bb692014-04-29 00:16:46 +00001140 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001141 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001142 }
1143 }
1144
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001145 // If there is a frame pointer, copy R1 into R31
1146 if (HasFP) {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001147 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1148 .addReg(SPReg)
1149 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001150
Jay Foad1f0a44e2014-12-01 09:42:32 +00001151 if (!HasBP && needsCFI) {
1152 // Change the definition of CFA from SP+offset to FP+offset, because SP
1153 // will change at every alloca.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001154 unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +00001155 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001156 MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1157
Eric Christopher612bb692014-04-29 00:16:46 +00001158 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001159 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001160 }
1161 }
1162
Jay Foad1f0a44e2014-12-01 09:42:32 +00001163 if (needsCFI) {
1164 // Describe where callee saved registers were saved, at fixed offsets from
1165 // CFA.
Matthias Braun941a7052016-07-28 18:40:00 +00001166 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001167 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001168 unsigned Reg = CSI[I].getReg();
1169 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +00001170
1171 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1172 // subregisters of CR2. We just need to emit a move of CR2.
Craig Topperabadc662012-04-20 06:31:50 +00001173 if (PPC::CRBITRCRegClass.contains(Reg))
Rafael Espindola08600bc2011-05-30 20:20:15 +00001174 continue;
Rafael Espindola08600bc2011-05-30 20:20:15 +00001175
Roman Divackyc9e23d92012-09-12 14:47:47 +00001176 // For SVR4, don't emit a move for the CR spill slot if we haven't
1177 // spilled CRs.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001178 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001179 && !MustSaveCR)
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001180 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001181
1182 // For 64-bit SVR4 when we have spilled CRs, the spill location
1183 // is SP+8, not a frame-relative slot.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001184 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Ulrich Weigandbe928cc2014-07-21 00:03:18 +00001185 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
1186 // the whole CR word. In the ELFv2 ABI, every CR that was
1187 // actually saved gets its own CFI record.
1188 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
Matthias Braunf23ef432016-11-30 23:48:42 +00001189 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Ulrich Weigandbe928cc2014-07-21 00:03:18 +00001190 nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
Eric Christopher612bb692014-04-29 00:16:46 +00001191 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001192 .addCFIIndex(CFIIndex);
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001193 continue;
Roman Divackyc9e23d92012-09-12 14:47:47 +00001194 }
1195
Matthias Braun941a7052016-07-28 18:40:00 +00001196 int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx());
Matthias Braunf23ef432016-11-30 23:48:42 +00001197 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001198 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
Eric Christopher612bb692014-04-29 00:16:46 +00001199 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001200 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001201 }
1202 }
1203}
1204
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001205void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
Kit Bartond3b904d2015-09-10 01:55:44 +00001206 MachineBasicBlock &MBB) const {
1207 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1208 DebugLoc dl;
1209
1210 if (MBBI != MBB.end())
1211 dl = MBBI->getDebugLoc();
1212
Eric Christopherb128abc2017-02-04 01:52:17 +00001213 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1214 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001215
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001216 // Get alignment info so we know how to restore the SP.
Matthias Braun941a7052016-07-28 18:40:00 +00001217 const MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001218
1219 // Get the number of bytes allocated from the FrameInfo.
Matthias Braun941a7052016-07-28 18:40:00 +00001220 int FrameSize = MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001221
1222 // Get processor type.
1223 bool isPPC64 = Subtarget.isPPC64();
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001224 // Get the ABI.
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001225 bool isSVR4ABI = Subtarget.isSVR4ABI();
1226
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001227 // Check if the link register (LR) has been saved.
1228 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1229 bool MustSaveLR = FI->mustSaveLR();
Craig Topperb94011f2013-07-14 04:42:23 +00001230 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001231 bool MustSaveCR = !MustSaveCRs.empty();
Bill Schmidtf381afc2013-08-20 03:12:23 +00001232 // Do we have a frame pointer and/or base pointer for this function?
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001233 bool HasFP = hasFP(MF);
Hal Finkela7c54e82013-07-17 00:45:52 +00001234 bool HasBP = RegInfo->hasBasePointer(MF);
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001235 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001236
Bill Schmidtf381afc2013-08-20 03:12:23 +00001237 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1;
Hal Finkel3ee2af72014-07-18 23:29:49 +00001238 unsigned BPReg = RegInfo->getBaseRegister(MF);
Bill Schmidtf381afc2013-08-20 03:12:23 +00001239 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
Kit Barton9c432ae2015-11-16 20:22:15 +00001240 unsigned ScratchReg = 0;
Bill Schmidtf381afc2013-08-20 03:12:23 +00001241 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1242 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
1243 : PPC::MTLR );
1244 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
1245 : PPC::LWZ );
1246 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
1247 : PPC::LIS );
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001248 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
1249 : PPC::OR );
Bill Schmidtf381afc2013-08-20 03:12:23 +00001250 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
1251 : PPC::ORI );
1252 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
1253 : PPC::ADDI );
1254 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
1255 : PPC::ADD4 );
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001256
Eric Christopherf71609b2015-02-13 00:39:27 +00001257 int LROffset = getReturnSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001258
1259 int FPOffset = 0;
Kit Barton9c432ae2015-11-16 20:22:15 +00001260
Simon Pilgrimfbd22212016-11-20 13:10:51 +00001261 // Using the same bool variable as below to suppress compiler warnings.
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +00001262 bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg,
1263 &TempReg);
1264 assert(SingleScratchReg &&
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001265 "Could not find an available scratch register");
1266
Nemanja Ivanovicdaf0ca22016-02-20 20:45:37 +00001267 SingleScratchReg = ScratchReg == TempReg;
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001268
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001269 if (HasFP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001270 if (isSVR4ABI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001271 int FPIndex = FI->getFramePointerSaveIndex();
1272 assert(FPIndex && "No Frame Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001273 FPOffset = MFI.getObjectOffset(FPIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001274 } else {
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001275 FPOffset = getFramePointerSaveOffset();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001276 }
1277 }
1278
Hal Finkela7c54e82013-07-17 00:45:52 +00001279 int BPOffset = 0;
1280 if (HasBP) {
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001281 if (isSVR4ABI) {
Hal Finkela7c54e82013-07-17 00:45:52 +00001282 int BPIndex = FI->getBasePointerSaveIndex();
1283 assert(BPIndex && "No Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001284 BPOffset = MFI.getObjectOffset(BPIndex);
Hal Finkela7c54e82013-07-17 00:45:52 +00001285 } else {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001286 BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001287 }
1288 }
1289
Justin Hibbits654346e2015-01-10 01:57:21 +00001290 int PBPOffset = 0;
1291 if (FI->usesPICBase()) {
Justin Hibbits654346e2015-01-10 01:57:21 +00001292 int PBPIndex = FI->getPICBasePointerSaveIndex();
1293 assert(PBPIndex && "No PIC Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001294 PBPOffset = MFI.getObjectOffset(PBPIndex);
Justin Hibbits654346e2015-01-10 01:57:21 +00001295 }
1296
NAKAMURA Takumi8061e862015-09-11 08:20:56 +00001297 bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
Kit Bartond3b904d2015-09-10 01:55:44 +00001298
1299 if (IsReturnBlock) {
1300 unsigned RetOpcode = MBBI->getOpcode();
1301 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1302 RetOpcode == PPC::TCRETURNdi ||
1303 RetOpcode == PPC::TCRETURNai ||
1304 RetOpcode == PPC::TCRETURNri8 ||
1305 RetOpcode == PPC::TCRETURNdi8 ||
1306 RetOpcode == PPC::TCRETURNai8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001307
Kit Bartond3b904d2015-09-10 01:55:44 +00001308 if (UsesTCRet) {
1309 int MaxTCRetDelta = FI->getTailCallSPDelta();
1310 MachineOperand &StackAdjust = MBBI->getOperand(1);
1311 assert(StackAdjust.isImm() && "Expecting immediate value.");
1312 // Adjust stack pointer.
1313 int StackAdj = StackAdjust.getImm();
1314 int Delta = StackAdj - MaxTCRetDelta;
1315 assert((Delta >= 0) && "Delta must be positive");
1316 if (MaxTCRetDelta>0)
1317 FrameSize += (StackAdj +Delta);
1318 else
1319 FrameSize += StackAdj;
1320 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001321 }
1322
Bill Schmidt8893a3d2013-08-16 20:05:04 +00001323 // Frames of 32KB & larger require special handling because they cannot be
1324 // indexed into with a simple LD/LWZ immediate offset operand.
1325 bool isLargeFrame = !isInt<16>(FrameSize);
1326
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001327 // On targets without red zone, the SP needs to be restored last, so that
1328 // all live contents of the stack frame are upwards of the SP. This means
1329 // that we cannot restore SP just now, since there may be more registers
1330 // to restore from the stack frame (e.g. R31). If the frame size is not
1331 // a simple immediate value, we will need a spare register to hold the
1332 // restored SP. If the frame size is known and small, we can simply adjust
1333 // the offsets of the registers to be restored, and still use SP to restore
1334 // them. In such case, the final update of SP will be to add the frame
1335 // size to it.
1336 // To simplify the code, set RBReg to the base register used to restore
1337 // values from the stack, and set SPAdd to the value that needs to be added
1338 // to the SP at the end. The default values are as if red zone was present.
1339 unsigned RBReg = SPReg;
1340 unsigned SPAdd = 0;
1341
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001342 if (FrameSize) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001343 // In the prologue, the loaded (or persistent) stack pointer value is
1344 // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red
1345 // zone add this offset back now.
Bill Schmidtf381afc2013-08-20 03:12:23 +00001346
1347 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1348 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1349 // call which invalidates the stack pointer value in SP(0). So we use the
1350 // value of R31 in this case.
1351 if (FI->hasFastCall()) {
1352 assert(HasFP && "Expecting a valid frame pointer.");
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001353 if (!HasRedZone)
1354 RBReg = FPReg;
Bill Schmidtf381afc2013-08-20 03:12:23 +00001355 if (!isLargeFrame) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001356 BuildMI(MBB, MBBI, dl, AddImmInst, RBReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001357 .addReg(FPReg).addImm(FrameSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001358 } else {
Bill Schmidtf381afc2013-08-20 03:12:23 +00001359 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1360 .addImm(FrameSize >> 16);
1361 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1362 .addReg(ScratchReg, RegState::Kill)
1363 .addImm(FrameSize & 0xFFFF);
1364 BuildMI(MBB, MBBI, dl, AddInst)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001365 .addReg(RBReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001366 .addReg(FPReg)
1367 .addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001368 }
Matthias Braun941a7052016-07-28 18:40:00 +00001369 } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001370 if (HasRedZone) {
Stefan Pintilie70bfe662018-01-12 13:12:49 +00001371 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001372 .addReg(SPReg)
1373 .addImm(FrameSize);
1374 } else {
1375 // Make sure that adding FrameSize will not overflow the max offset
1376 // size.
1377 assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 &&
1378 "Local offsets should be negative");
1379 SPAdd = FrameSize;
1380 FPOffset += FrameSize;
1381 BPOffset += FrameSize;
1382 PBPOffset += FrameSize;
1383 }
Bill Schmidtf381afc2013-08-20 03:12:23 +00001384 } else {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001385 // We don't want to use ScratchReg as a base register, because it
1386 // could happen to be R0. Use FP instead, but make sure to preserve it.
1387 if (!HasRedZone) {
1388 // If FP is not saved, copy it to ScratchReg.
1389 if (!HasFP)
1390 BuildMI(MBB, MBBI, dl, OrInst, ScratchReg)
1391 .addReg(FPReg)
1392 .addReg(FPReg);
1393 RBReg = FPReg;
1394 }
Stefan Pintilie70bfe662018-01-12 13:12:49 +00001395 BuildMI(MBB, MBBI, dl, LoadInst, RBReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001396 .addImm(0)
1397 .addReg(SPReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001398 }
1399 }
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001400 assert(RBReg != ScratchReg && "Should have avoided ScratchReg");
1401 // If there is no red zone, ScratchReg may be needed for holding a useful
1402 // value (although not the base register). Make sure it is not overwritten
1403 // too early.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001404
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001405 assert((isPPC64 || !MustSaveCR) &&
1406 "Epilogue CR restoring supported only in 64-bit mode");
1407
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001408 // If we need to restore both the LR and the CR and we only have one
1409 // available scratch register, we must do them one at a time.
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001410 if (MustSaveCR && SingleScratchReg && MustSaveLR) {
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001411 // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg
1412 // is live here.
1413 assert(HasRedZone && "Expecting red zone");
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001414 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1415 .addImm(8)
1416 .addReg(SPReg);
1417 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1418 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1419 .addReg(TempReg, getKillRegState(i == e-1));
1420 }
1421
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001422 // Delay restoring of the LR if ScratchReg is needed. This is ok, since
1423 // LR is stored in the caller's stack frame. ScratchReg will be needed
1424 // if RBReg is anything other than SP. We shouldn't use ScratchReg as
1425 // a base register anyway, because it may happen to be R0.
1426 bool LoadedLR = false;
1427 if (MustSaveLR && RBReg == SPReg && isInt<16>(LROffset+SPAdd)) {
Stefan Pintilie70bfe662018-01-12 13:12:49 +00001428 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001429 .addImm(LROffset+SPAdd)
1430 .addReg(RBReg);
1431 LoadedLR = true;
1432 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001433
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001434 if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) {
1435 // This will only occur for PPC64.
1436 assert(isPPC64 && "Expecting 64-bit mode");
1437 assert(RBReg == SPReg && "Should be using SP as a base register");
Bill Schmidtf381afc2013-08-20 03:12:23 +00001438 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1439 .addImm(8)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001440 .addReg(RBReg);
1441 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001442
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001443 if (HasFP) {
1444 // If there is red zone, restore FP directly, since SP has already been
1445 // restored. Otherwise, restore the value of FP into ScratchReg.
1446 if (HasRedZone || RBReg == SPReg)
1447 BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1448 .addImm(FPOffset)
1449 .addReg(SPReg);
1450 else
1451 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1452 .addImm(FPOffset)
1453 .addReg(RBReg);
1454 }
Hal Finkela7c54e82013-07-17 00:45:52 +00001455
Justin Hibbits654346e2015-01-10 01:57:21 +00001456 if (FI->usesPICBase())
Krzysztof Parzyszek038a0542017-05-04 19:14:54 +00001457 BuildMI(MBB, MBBI, dl, LoadInst, PPC::R30)
Justin Hibbits654346e2015-01-10 01:57:21 +00001458 .addImm(PBPOffset)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001459 .addReg(RBReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +00001460
Bill Schmidtf381afc2013-08-20 03:12:23 +00001461 if (HasBP)
1462 BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1463 .addImm(BPOffset)
Krzysztof Parzyszekb66efb82016-09-22 17:22:43 +00001464 .addReg(RBReg);
1465
1466 // There is nothing more to be loaded from the stack, so now we can
1467 // restore SP: SP = RBReg + SPAdd.
1468 if (RBReg != SPReg || SPAdd != 0) {
1469 assert(!HasRedZone && "This should not happen with red zone");
1470 // If SPAdd is 0, generate a copy.
1471 if (SPAdd == 0)
1472 BuildMI(MBB, MBBI, dl, OrInst, SPReg)
1473 .addReg(RBReg)
1474 .addReg(RBReg);
1475 else
1476 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1477 .addReg(RBReg)
1478 .addImm(SPAdd);
1479
1480 assert(RBReg != ScratchReg && "Should be using FP or SP as base register");
1481 if (RBReg == FPReg)
1482 BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1483 .addReg(ScratchReg)
1484 .addReg(ScratchReg);
1485
1486 // Now load the LR from the caller's stack frame.
1487 if (MustSaveLR && !LoadedLR)
1488 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1489 .addImm(LROffset)
1490 .addReg(SPReg);
1491 }
Hal Finkel67369882013-04-15 02:07:05 +00001492
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +00001493 if (MustSaveCR &&
1494 !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
Bill Schmidtf381afc2013-08-20 03:12:23 +00001495 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1496 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1497 .addReg(TempReg, getKillRegState(i == e-1));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001498
Bill Schmidtf381afc2013-08-20 03:12:23 +00001499 if (MustSaveLR)
Stefan Pintilie70bfe662018-01-12 13:12:49 +00001500 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001501
1502 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1503 // call optimization
Kit Bartond3b904d2015-09-10 01:55:44 +00001504 if (IsReturnBlock) {
1505 unsigned RetOpcode = MBBI->getOpcode();
1506 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1507 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
Matthias Braunf1caa282017-12-15 22:22:58 +00001508 MF.getFunction().getCallingConv() == CallingConv::Fast) {
Kit Bartond3b904d2015-09-10 01:55:44 +00001509 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1510 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001511
Kit Bartond3b904d2015-09-10 01:55:44 +00001512 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1513 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1514 .addReg(SPReg).addImm(CallerAllocatedAmt);
1515 } else {
1516 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001517 .addImm(CallerAllocatedAmt >> 16);
Kit Bartond3b904d2015-09-10 01:55:44 +00001518 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001519 .addReg(ScratchReg, RegState::Kill)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001520 .addImm(CallerAllocatedAmt & 0xFFFF);
Kit Bartond3b904d2015-09-10 01:55:44 +00001521 BuildMI(MBB, MBBI, dl, AddInst)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001522 .addReg(SPReg)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001523 .addReg(FPReg)
Bill Schmidtf381afc2013-08-20 03:12:23 +00001524 .addReg(ScratchReg);
Kit Bartond3b904d2015-09-10 01:55:44 +00001525 }
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001526 } else {
1527 createTailCallBranchInstr(MBB);
Kit Bartond3b904d2015-09-10 01:55:44 +00001528 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001529 }
1530}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001531
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001532void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
1533 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001534
Stefan Pintilie42418212017-12-20 19:07:44 +00001535 // If we got this far a first terminator should exist.
1536 assert(MBBI != MBB.end() && "Failed to find the first terminator.");
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001537
Stefan Pintilie42418212017-12-20 19:07:44 +00001538 DebugLoc dl = MBBI->getDebugLoc();
Eric Christopherb128abc2017-02-04 01:52:17 +00001539 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001540
1541 // Create branch instruction for pseudo tail call return instruction
1542 unsigned RetOpcode = MBBI->getOpcode();
1543 if (RetOpcode == PPC::TCRETURNdi) {
1544 MBBI = MBB.getLastNonDebugInstr();
1545 MachineOperand &JumpTarget = MBBI->getOperand(0);
1546 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1547 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1548 } else if (RetOpcode == PPC::TCRETURNri) {
1549 MBBI = MBB.getLastNonDebugInstr();
1550 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1551 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1552 } else if (RetOpcode == PPC::TCRETURNai) {
1553 MBBI = MBB.getLastNonDebugInstr();
1554 MachineOperand &JumpTarget = MBBI->getOperand(0);
1555 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1556 } else if (RetOpcode == PPC::TCRETURNdi8) {
1557 MBBI = MBB.getLastNonDebugInstr();
1558 MachineOperand &JumpTarget = MBBI->getOperand(0);
1559 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1560 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1561 } else if (RetOpcode == PPC::TCRETURNri8) {
1562 MBBI = MBB.getLastNonDebugInstr();
1563 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1564 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1565 } else if (RetOpcode == PPC::TCRETURNai8) {
1566 MBBI = MBB.getLastNonDebugInstr();
1567 MachineOperand &JumpTarget = MBBI->getOperand(0);
1568 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1569 }
1570}
1571
Matthias Braun02564862015-07-14 17:17:13 +00001572void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1573 BitVector &SavedRegs,
1574 RegScavenger *RS) const {
1575 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1576
Eric Christopherb128abc2017-02-04 01:52:17 +00001577 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001578
1579 // Save and clear the LR state.
1580 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1581 unsigned LR = RegInfo->getRARegister();
1582 FI->setMustSaveLR(MustSaveLR(MF, LR));
Matthias Braun02564862015-07-14 17:17:13 +00001583 SavedRegs.reset(LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001584
1585 // Save R31 if necessary
1586 int FPSI = FI->getFramePointerSaveIndex();
1587 bool isPPC64 = Subtarget.isPPC64();
1588 bool isDarwinABI = Subtarget.isDarwinABI();
Matthias Braun941a7052016-07-28 18:40:00 +00001589 MachineFrameInfo &MFI = MF.getFrameInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001590
1591 // If the frame pointer save index hasn't been defined yet.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001592 if (!FPSI && needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001593 // Find out what the fix offset of the frame pointer save area.
Eric Christopherdc3a8a42015-02-13 00:39:38 +00001594 int FPOffset = getFramePointerSaveOffset();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001595 // Allocate the frame index for frame pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00001596 FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001597 // Save the result.
1598 FI->setFramePointerSaveIndex(FPSI);
1599 }
1600
Hal Finkela7c54e82013-07-17 00:45:52 +00001601 int BPSI = FI->getBasePointerSaveIndex();
1602 if (!BPSI && RegInfo->hasBasePointer(MF)) {
Eric Christopherfcd3d872015-02-13 22:48:53 +00001603 int BPOffset = getBasePointerSaveOffset();
Hal Finkela7c54e82013-07-17 00:45:52 +00001604 // Allocate the frame index for the base pointer save area.
Matthias Braun941a7052016-07-28 18:40:00 +00001605 BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
Hal Finkela7c54e82013-07-17 00:45:52 +00001606 // Save the result.
1607 FI->setBasePointerSaveIndex(BPSI);
1608 }
1609
Justin Hibbits654346e2015-01-10 01:57:21 +00001610 // Reserve stack space for the PIC Base register (R30).
1611 // Only used in SVR4 32-bit.
1612 if (FI->usesPICBase()) {
Matthias Braun941a7052016-07-28 18:40:00 +00001613 int PBPSI = MFI.CreateFixedObject(4, -8, true);
Justin Hibbits654346e2015-01-10 01:57:21 +00001614 FI->setPICBasePointerSaveIndex(PBPSI);
1615 }
1616
Hal Finkel97a189c2016-08-31 00:52:03 +00001617 // Make sure we don't explicitly spill r31, because, for example, we have
1618 // some inline asm which explicity clobbers it, when we otherwise have a
1619 // frame pointer and are using r31's spill slot for the prologue/epilogue
1620 // code. Same goes for the base pointer and the PIC base register.
1621 if (needsFP(MF))
1622 SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31);
1623 if (RegInfo->hasBasePointer(MF))
1624 SavedRegs.reset(RegInfo->getBaseRegister(MF));
1625 if (FI->usesPICBase())
1626 SavedRegs.reset(PPC::R30);
1627
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001628 // Reserve stack space to move the linkage area to in case of a tail call.
1629 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001630 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1631 (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
Matthias Braun941a7052016-07-28 18:40:00 +00001632 MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001633 }
1634
Eric Christopherd1737492014-04-29 00:16:40 +00001635 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001636 // function uses CR 2, 3, or 4.
Eric Christopherd1737492014-04-29 00:16:40 +00001637 if (!isPPC64 && !isDarwinABI &&
Matthias Braun02564862015-07-14 17:17:13 +00001638 (SavedRegs.test(PPC::CR2) ||
1639 SavedRegs.test(PPC::CR3) ||
1640 SavedRegs.test(PPC::CR4))) {
Matthias Braun941a7052016-07-28 18:40:00 +00001641 int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true);
Bill Schmidtc68c6df2013-02-24 17:34:50 +00001642 FI->setCRSpillFrameIndex(FrameIdx);
1643 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001644}
1645
Hal Finkel5a765fd2013-03-14 20:33:40 +00001646void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
Hal Finkelbb420f12013-03-15 05:06:04 +00001647 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001648 // Early exit if not using the SVR4 ABI.
Hal Finkelbb420f12013-03-15 05:06:04 +00001649 if (!Subtarget.isSVR4ABI()) {
1650 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001651 return;
Hal Finkelbb420f12013-03-15 05:06:04 +00001652 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001653
1654 // Get callee saved register information.
Matthias Braun941a7052016-07-28 18:40:00 +00001655 MachineFrameInfo &MFI = MF.getFrameInfo();
1656 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001657
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001658 // If the function is shrink-wrapped, and if the function has a tail call, the
1659 // tail call might not be in the new RestoreBlock, so real branch instruction
1660 // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
1661 // RestoreBlock. So we handle this case here.
Matthias Braun941a7052016-07-28 18:40:00 +00001662 if (MFI.getSavePoint() && MFI.hasTailCall()) {
1663 MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +00001664 for (MachineBasicBlock &MBB : MF) {
1665 if (MBB.isReturnBlock() && (&MBB) != RestoreBlock)
1666 createTailCallBranchInstr(MBB);
1667 }
1668 }
1669
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001670 // Early exit if no callee saved registers are modified!
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001671 if (CSI.empty() && !needsFP(MF)) {
Hal Finkelbb420f12013-03-15 05:06:04 +00001672 addScavengingSpillSlot(MF, RS);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001673 return;
1674 }
1675
1676 unsigned MinGPR = PPC::R31;
1677 unsigned MinG8R = PPC::X31;
1678 unsigned MinFPR = PPC::F31;
1679 unsigned MinVR = PPC::V31;
1680
1681 bool HasGPSaveArea = false;
1682 bool HasG8SaveArea = false;
1683 bool HasFPSaveArea = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001684 bool HasVRSAVESaveArea = false;
1685 bool HasVRSaveArea = false;
1686
1687 SmallVector<CalleeSavedInfo, 18> GPRegs;
1688 SmallVector<CalleeSavedInfo, 18> G8Regs;
1689 SmallVector<CalleeSavedInfo, 18> FPRegs;
1690 SmallVector<CalleeSavedInfo, 18> VRegs;
1691
1692 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1693 unsigned Reg = CSI[i].getReg();
Craig Topperabadc662012-04-20 06:31:50 +00001694 if (PPC::GPRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001695 HasGPSaveArea = true;
1696
1697 GPRegs.push_back(CSI[i]);
1698
1699 if (Reg < MinGPR) {
1700 MinGPR = Reg;
1701 }
Craig Topperabadc662012-04-20 06:31:50 +00001702 } else if (PPC::G8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001703 HasG8SaveArea = true;
1704
1705 G8Regs.push_back(CSI[i]);
1706
1707 if (Reg < MinG8R) {
1708 MinG8R = Reg;
1709 }
Craig Topperabadc662012-04-20 06:31:50 +00001710 } else if (PPC::F8RCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001711 HasFPSaveArea = true;
1712
1713 FPRegs.push_back(CSI[i]);
1714
1715 if (Reg < MinFPR) {
1716 MinFPR = Reg;
1717 }
Craig Topperabadc662012-04-20 06:31:50 +00001718 } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1719 PPC::CRRCRegClass.contains(Reg)) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001720 ; // do nothing, as we already know whether CRs are spilled
Craig Topperabadc662012-04-20 06:31:50 +00001721 } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001722 HasVRSAVESaveArea = true;
Craig Topperabadc662012-04-20 06:31:50 +00001723 } else if (PPC::VRRCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001724 HasVRSaveArea = true;
1725
1726 VRegs.push_back(CSI[i]);
1727
1728 if (Reg < MinVR) {
1729 MinVR = Reg;
1730 }
1731 } else {
1732 llvm_unreachable("Unknown RegisterClass!");
1733 }
1734 }
1735
1736 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
Eric Christopher38522b82015-01-30 02:11:26 +00001737 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001738
1739 int64_t LowerBound = 0;
1740
1741 // Take into account stack space reserved for tail calls.
1742 int TCSPDelta = 0;
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001743 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1744 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001745 LowerBound = TCSPDelta;
1746 }
1747
1748 // The Floating-point register save area is right below the back chain word
1749 // of the previous stack frame.
1750 if (HasFPSaveArea) {
1751 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1752 int FI = FPRegs[i].getFrameIdx();
1753
Matthias Braun941a7052016-07-28 18:40:00 +00001754 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001755 }
1756
Hal Finkelfeea6532013-03-26 20:08:20 +00001757 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001758 }
1759
1760 // Check whether the frame pointer register is allocated. If so, make sure it
1761 // is spilled to the correct offset.
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +00001762 if (needsFP(MF)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001763 int FI = PFI->getFramePointerSaveIndex();
1764 assert(FI && "No Frame Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001765 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Krzysztof Parzyszek2b053312017-05-17 13:25:09 +00001766 // FP is R31/X31, so no need to update MinGPR/MinG8R.
1767 HasGPSaveArea = true;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001768 }
1769
Justin Hibbits654346e2015-01-10 01:57:21 +00001770 if (PFI->usesPICBase()) {
Justin Hibbits654346e2015-01-10 01:57:21 +00001771 int FI = PFI->getPICBasePointerSaveIndex();
1772 assert(FI && "No PIC Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001773 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Krzysztof Parzyszek2b053312017-05-17 13:25:09 +00001774
1775 MinGPR = std::min<unsigned>(MinGPR, PPC::R30);
1776 HasGPSaveArea = true;
Justin Hibbits654346e2015-01-10 01:57:21 +00001777 }
1778
Eric Christopherb128abc2017-02-04 01:52:17 +00001779 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
Hal Finkela7c54e82013-07-17 00:45:52 +00001780 if (RegInfo->hasBasePointer(MF)) {
Hal Finkela7c54e82013-07-17 00:45:52 +00001781 int FI = PFI->getBasePointerSaveIndex();
1782 assert(FI && "No Base Pointer Save Slot!");
Matthias Braun941a7052016-07-28 18:40:00 +00001783 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Krzysztof Parzyszek2b053312017-05-17 13:25:09 +00001784
1785 unsigned BP = RegInfo->getBaseRegister(MF);
1786 if (PPC::G8RCRegClass.contains(BP)) {
1787 MinG8R = std::min<unsigned>(MinG8R, BP);
1788 HasG8SaveArea = true;
1789 } else if (PPC::GPRCRegClass.contains(BP)) {
1790 MinGPR = std::min<unsigned>(MinGPR, BP);
1791 HasGPSaveArea = true;
1792 }
Hal Finkela7c54e82013-07-17 00:45:52 +00001793 }
1794
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001795 // General register save area starts right below the Floating-point
1796 // register save area.
1797 if (HasGPSaveArea || HasG8SaveArea) {
1798 // Move general register save area spill slots down, taking into account
1799 // the size of the Floating-point register save area.
1800 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1801 int FI = GPRegs[i].getFrameIdx();
1802
Matthias Braun941a7052016-07-28 18:40:00 +00001803 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001804 }
1805
1806 // Move general register save area spill slots down, taking into account
1807 // the size of the Floating-point register save area.
1808 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1809 int FI = G8Regs[i].getFrameIdx();
1810
Matthias Braun941a7052016-07-28 18:40:00 +00001811 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001812 }
1813
1814 unsigned MinReg =
Hal Finkelfeea6532013-03-26 20:08:20 +00001815 std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1816 TRI->getEncodingValue(MinG8R));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001817
1818 if (Subtarget.isPPC64()) {
1819 LowerBound -= (31 - MinReg + 1) * 8;
1820 } else {
1821 LowerBound -= (31 - MinReg + 1) * 4;
1822 }
1823 }
1824
Roman Divackyc9e23d92012-09-12 14:47:47 +00001825 // For 32-bit only, the CR save area is below the general register
1826 // save area. For 64-bit SVR4, the CR save area is addressed relative
1827 // to the stack pointer and hence does not need an adjustment here.
1828 // Only CR2 (the first nonvolatile spilled) has an associated frame
1829 // index so that we have a single uniform save area.
1830 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001831 // Adjust the frame index of the CR spill slot.
1832 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1833 unsigned Reg = CSI[i].getReg();
1834
Roman Divackyc9e23d92012-09-12 14:47:47 +00001835 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
Eric Christopherd1737492014-04-29 00:16:40 +00001836 // Leave Darwin logic as-is.
1837 || (!Subtarget.isSVR4ABI() &&
1838 (PPC::CRBITRCRegClass.contains(Reg) ||
1839 PPC::CRRCRegClass.contains(Reg)))) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001840 int FI = CSI[i].getFrameIdx();
1841
Matthias Braun941a7052016-07-28 18:40:00 +00001842 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001843 }
1844 }
1845
1846 LowerBound -= 4; // The CR save area is always 4 bytes long.
1847 }
1848
1849 if (HasVRSAVESaveArea) {
1850 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1851 // which have the VRSAVE register class?
1852 // Adjust the frame index of the VRSAVE spill slot.
1853 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1854 unsigned Reg = CSI[i].getReg();
1855
Craig Topperabadc662012-04-20 06:31:50 +00001856 if (PPC::VRSAVERCRegClass.contains(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001857 int FI = CSI[i].getFrameIdx();
1858
Matthias Braun941a7052016-07-28 18:40:00 +00001859 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001860 }
1861 }
1862
1863 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1864 }
1865
1866 if (HasVRSaveArea) {
Tony Jiangd5acad02017-07-11 16:42:20 +00001867 // Insert alignment padding, we need 16-byte alignment. Note: for postive
1868 // number the alignment formula is : y = (x + (n-1)) & (~(n-1)). But since
1869 // we are using negative number here (the stack grows downward). We should
1870 // use formula : y = x & (~(n-1)). Where x is the size before aligning, n
1871 // is the alignment size ( n = 16 here) and y is the size after aligning.
1872 assert(LowerBound <= 0 && "Expect LowerBound have a non-positive value!");
1873 LowerBound &= ~(15);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001874
1875 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1876 int FI = VRegs[i].getFrameIdx();
1877
Matthias Braun941a7052016-07-28 18:40:00 +00001878 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001879 }
1880 }
Hal Finkelbb420f12013-03-15 05:06:04 +00001881
1882 addScavengingSpillSlot(MF, RS);
1883}
1884
1885void
1886PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1887 RegScavenger *RS) const {
1888 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1889 // a large stack, which will require scavenging a register to materialize a
1890 // large offset.
1891
1892 // We need to have a scavenger spill slot for spills if the frame size is
1893 // large. In case there is no free register for large-offset addressing,
1894 // this slot is used for the necessary emergency spill. Also, we need the
1895 // slot for dynamic stack allocations.
1896
1897 // The scavenger might be invoked if the frame offset does not fit into
1898 // the 16-bit immediate. We don't know the complete frame size here
1899 // because we've not yet computed callee-saved register spills or the
1900 // needed alignment padding.
1901 unsigned StackSize = determineFrameLayout(MF, false, true);
Matthias Braun941a7052016-07-28 18:40:00 +00001902 MachineFrameInfo &MFI = MF.getFrameInfo();
1903 if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001904 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001905 const TargetRegisterClass &GPRC = PPC::GPRCRegClass;
1906 const TargetRegisterClass &G8RC = PPC::G8RCRegClass;
1907 const TargetRegisterClass &RC = Subtarget.isPPC64() ? G8RC : GPRC;
1908 const TargetRegisterInfo &TRI = *Subtarget.getRegisterInfo();
1909 unsigned Size = TRI.getSpillSize(RC);
1910 unsigned Align = TRI.getSpillAlignment(RC);
1911 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001912
Hal Finkel18607632013-07-18 04:28:21 +00001913 // Might we have over-aligned allocas?
Matthias Braun941a7052016-07-28 18:40:00 +00001914 bool HasAlVars = MFI.hasVarSizedObjects() &&
1915 MFI.getMaxAlignment() > getStackAlignment();
Hal Finkel18607632013-07-18 04:28:21 +00001916
Hal Finkel0dfbb052013-03-26 18:57:22 +00001917 // These kinds of spills might need two registers.
Hal Finkel18607632013-07-18 04:28:21 +00001918 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001919 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
Hal Finkel0dfbb052013-03-26 18:57:22 +00001920
Hal Finkelbb420f12013-03-15 05:06:04 +00001921 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001922}
Roman Divackyc9e23d92012-09-12 14:47:47 +00001923
Eric Christopherd1737492014-04-29 00:16:40 +00001924bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00001925PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00001926 MachineBasicBlock::iterator MI,
1927 const std::vector<CalleeSavedInfo> &CSI,
1928 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00001929
1930 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1931 // Return false otherwise to maintain pre-existing behavior.
1932 if (!Subtarget.isSVR4ABI())
1933 return false;
1934
1935 MachineFunction *MF = MBB.getParent();
Eric Christopherb128abc2017-02-04 01:52:17 +00001936 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001937 DebugLoc DL;
1938 bool CRSpilled = false;
Hal Finkel2f293912013-04-13 23:06:15 +00001939 MachineInstrBuilder CRMIB;
Eric Christopherd1737492014-04-29 00:16:40 +00001940
Roman Divackyc9e23d92012-09-12 14:47:47 +00001941 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1942 unsigned Reg = CSI[i].getReg();
Hal Finkelac1a24b2013-06-28 22:29:56 +00001943 // Only Darwin actually uses the VRSAVE register, but it can still appear
1944 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
1945 // Darwin, ignore it.
1946 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1947 continue;
1948
Roman Divackyc9e23d92012-09-12 14:47:47 +00001949 // CR2 through CR4 are the nonvolatile CR fields.
1950 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1951
Roman Divackyc9e23d92012-09-12 14:47:47 +00001952 // Add the callee-saved register as live-in; it's killed at the spill.
Zaara Syeda1f59ae32018-01-30 16:17:22 +00001953 // Do not do this for callee-saved registers that are live-in to the
1954 // function because they will already be marked live-in and this will be
1955 // adding it for a second time. It is an error to add the same register
1956 // to the set more than once.
1957 const MachineRegisterInfo &MRI = MF->getRegInfo();
1958 bool IsLiveIn = MRI.isLiveIn(Reg);
1959 if (!IsLiveIn)
1960 MBB.addLiveIn(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001961
Hal Finkel2f293912013-04-13 23:06:15 +00001962 if (CRSpilled && IsCRField) {
1963 CRMIB.addReg(Reg, RegState::ImplicitKill);
1964 continue;
1965 }
1966
Roman Divackyc9e23d92012-09-12 14:47:47 +00001967 // Insert the spill to the stack frame.
1968 if (IsCRField) {
Hal Finkel67369882013-04-15 02:07:05 +00001969 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
Roman Divackyc9e23d92012-09-12 14:47:47 +00001970 if (Subtarget.isPPC64()) {
Hal Finkel67369882013-04-15 02:07:05 +00001971 // The actual spill will happen at the start of the prologue.
1972 FuncInfo->addMustSaveCR(Reg);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001973 } else {
Hal Finkel67369882013-04-15 02:07:05 +00001974 CRSpilled = true;
Bill Schmidtef3d1a22013-05-14 16:08:32 +00001975 FuncInfo->setSpillsCR();
Hal Finkel67369882013-04-15 02:07:05 +00001976
Eric Christopherd1737492014-04-29 00:16:40 +00001977 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have
1978 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1979 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
Hal Finkel2f293912013-04-13 23:06:15 +00001980 .addReg(Reg, RegState::ImplicitKill);
1981
Eric Christopherd1737492014-04-29 00:16:40 +00001982 MBB.insert(MI, CRMIB);
1983 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1984 .addReg(PPC::R12,
1985 getKillRegState(true)),
1986 CSI[i].getFrameIdx()));
Roman Divackyc9e23d92012-09-12 14:47:47 +00001987 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00001988 } else {
1989 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
Zaara Syeda1f59ae32018-01-30 16:17:22 +00001990 // Use !IsLiveIn for the kill flag.
1991 // We do not want to kill registers that are live in this function
1992 // before their use because they will become undefined registers.
1993 TII.storeRegToStackSlot(MBB, MI, Reg, !IsLiveIn,
Eric Christopherd1737492014-04-29 00:16:40 +00001994 CSI[i].getFrameIdx(), RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00001995 }
1996 }
1997 return true;
1998}
1999
2000static void
Hal Finkeld85a04b2013-04-13 08:09:20 +00002001restoreCRs(bool isPPC64, bool is31,
2002 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00002003 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
2004 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
Roman Divackyc9e23d92012-09-12 14:47:47 +00002005
2006 MachineFunction *MF = MBB.getParent();
Eric Christophercccae792015-01-30 22:02:31 +00002007 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00002008 DebugLoc DL;
2009 unsigned RestoreOp, MoveReg;
2010
Hal Finkel67369882013-04-15 02:07:05 +00002011 if (isPPC64)
2012 // This is handled during epilogue generation.
2013 return;
2014 else {
Roman Divackyc9e23d92012-09-12 14:47:47 +00002015 // 32-bit: FP-relative
2016 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
Eric Christopherd1737492014-04-29 00:16:40 +00002017 PPC::R12),
2018 CSI[CSIIndex].getFrameIdx()));
Ulrich Weigand49f487e2013-07-03 17:59:07 +00002019 RestoreOp = PPC::MTOCRF;
Roman Divackyc9e23d92012-09-12 14:47:47 +00002020 MoveReg = PPC::R12;
2021 }
Eric Christopherd1737492014-04-29 00:16:40 +00002022
Roman Divackyc9e23d92012-09-12 14:47:47 +00002023 if (CR2Spilled)
2024 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
Hal Finkel035b4822013-03-28 03:38:16 +00002025 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00002026
2027 if (CR3Spilled)
2028 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
Hal Finkel035b4822013-03-28 03:38:16 +00002029 .addReg(MoveReg, getKillRegState(!CR4Spilled)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00002030
2031 if (CR4Spilled)
2032 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
Hal Finkel035b4822013-03-28 03:38:16 +00002033 .addReg(MoveReg, getKillRegState(true)));
Roman Divackyc9e23d92012-09-12 14:47:47 +00002034}
2035
Hans Wennborge1a2e902016-03-31 18:33:38 +00002036MachineBasicBlock::iterator PPCFrameLowering::
Eli Bendersky8da87162013-02-21 20:05:00 +00002037eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
2038 MachineBasicBlock::iterator I) const {
Eric Christopher38522b82015-01-30 02:11:26 +00002039 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
Eli Bendersky8da87162013-02-21 20:05:00 +00002040 if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2041 I->getOpcode() == PPC::ADJCALLSTACKUP) {
2042 // Add (actually subtract) back the amount the callee popped on return.
2043 if (int CalleeAmt = I->getOperand(1).getImm()) {
2044 bool is64Bit = Subtarget.isPPC64();
2045 CalleeAmt *= -1;
2046 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
2047 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
2048 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
2049 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
2050 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
2051 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +00002052 const DebugLoc &dl = I->getDebugLoc();
Eli Bendersky8da87162013-02-21 20:05:00 +00002053
2054 if (isInt<16>(CalleeAmt)) {
2055 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
2056 .addReg(StackReg, RegState::Kill)
2057 .addImm(CalleeAmt);
2058 } else {
2059 MachineBasicBlock::iterator MBBI = I;
2060 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
2061 .addImm(CalleeAmt >> 16);
2062 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
2063 .addReg(TmpReg, RegState::Kill)
2064 .addImm(CalleeAmt & 0xFFFF);
2065 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
2066 .addReg(StackReg, RegState::Kill)
2067 .addReg(TmpReg);
2068 }
2069 }
2070 }
2071 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
Hans Wennborge1a2e902016-03-31 18:33:38 +00002072 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +00002073}
2074
Eric Christopherd1737492014-04-29 00:16:40 +00002075bool
Roman Divackyc9e23d92012-09-12 14:47:47 +00002076PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Eric Christopherd1737492014-04-29 00:16:40 +00002077 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +00002078 std::vector<CalleeSavedInfo> &CSI,
Eric Christopherd1737492014-04-29 00:16:40 +00002079 const TargetRegisterInfo *TRI) const {
Roman Divackyc9e23d92012-09-12 14:47:47 +00002080
2081 // Currently, this function only handles SVR4 32- and 64-bit ABIs.
2082 // Return false otherwise to maintain pre-existing behavior.
2083 if (!Subtarget.isSVR4ABI())
2084 return false;
2085
2086 MachineFunction *MF = MBB.getParent();
Eric Christopherb128abc2017-02-04 01:52:17 +00002087 const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
Roman Divackyc9e23d92012-09-12 14:47:47 +00002088 bool CR2Spilled = false;
2089 bool CR3Spilled = false;
2090 bool CR4Spilled = false;
2091 unsigned CSIIndex = 0;
2092
2093 // Initialize insertion-point logic; we will be restoring in reverse
2094 // order of spill.
2095 MachineBasicBlock::iterator I = MI, BeforeI = I;
2096 bool AtStart = I == MBB.begin();
2097
2098 if (!AtStart)
2099 --BeforeI;
2100
2101 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2102 unsigned Reg = CSI[i].getReg();
2103
Hal Finkelac1a24b2013-06-28 22:29:56 +00002104 // Only Darwin actually uses the VRSAVE register, but it can still appear
2105 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on
2106 // Darwin, ignore it.
2107 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
2108 continue;
2109
Roman Divackyc9e23d92012-09-12 14:47:47 +00002110 if (Reg == PPC::CR2) {
2111 CR2Spilled = true;
2112 // The spill slot is associated only with CR2, which is the
2113 // first nonvolatile spilled. Save it here.
2114 CSIIndex = i;
2115 continue;
2116 } else if (Reg == PPC::CR3) {
2117 CR3Spilled = true;
2118 continue;
2119 } else if (Reg == PPC::CR4) {
2120 CR4Spilled = true;
2121 continue;
2122 } else {
2123 // When we first encounter a non-CR register after seeing at
2124 // least one CR register, restore all spilled CRs together.
2125 if ((CR2Spilled || CR3Spilled || CR4Spilled)
Eric Christopherd1737492014-04-29 00:16:40 +00002126 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
Hal Finkeld85a04b2013-04-13 08:09:20 +00002127 bool is31 = needsFP(*MF);
2128 restoreCRs(Subtarget.isPPC64(), is31,
2129 CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00002130 MBB, I, CSI, CSIIndex);
2131 CR2Spilled = CR3Spilled = CR4Spilled = false;
Roman Divackyc9e23d92012-09-12 14:47:47 +00002132 }
2133
2134 // Default behavior for non-CR saves.
2135 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2136 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
Eric Christopherd1737492014-04-29 00:16:40 +00002137 RC, TRI);
Roman Divackyc9e23d92012-09-12 14:47:47 +00002138 assert(I != MBB.begin() &&
Eric Christopherd1737492014-04-29 00:16:40 +00002139 "loadRegFromStackSlot didn't insert any code!");
Roman Divackyc9e23d92012-09-12 14:47:47 +00002140 }
2141
2142 // Insert in reverse order.
2143 if (AtStart)
2144 I = MBB.begin();
2145 else {
2146 I = BeforeI;
2147 ++I;
Eric Christopherd1737492014-04-29 00:16:40 +00002148 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00002149 }
2150
2151 // If we haven't yet spilled the CRs, do so now.
Hal Finkeld85a04b2013-04-13 08:09:20 +00002152 if (CR2Spilled || CR3Spilled || CR4Spilled) {
Eric Christopherd1737492014-04-29 00:16:40 +00002153 bool is31 = needsFP(*MF);
Hal Finkeld85a04b2013-04-13 08:09:20 +00002154 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
Eric Christopherd1737492014-04-29 00:16:40 +00002155 MBB, I, CSI, CSIIndex);
Hal Finkeld85a04b2013-04-13 08:09:20 +00002156 }
Roman Divackyc9e23d92012-09-12 14:47:47 +00002157
2158 return true;
2159}
Kit Bartond3b904d2015-09-10 01:55:44 +00002160
2161bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
Nemanja Ivanovicbcc82c92018-02-23 23:08:34 +00002162 if (MF.getInfo<PPCFunctionInfo>()->shrinkWrapDisabled())
2163 return false;
Kit Bartonf4ce2f32015-11-30 18:59:41 +00002164 return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() &&
2165 MF.getSubtarget<PPCSubtarget>().isPPC64());
Kit Bartond3b904d2015-09-10 01:55:44 +00002166}