1*0b57cec5SDimitry Andric //===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file contains the PPC implementation of TargetFrameLowering class.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "PPCFrameLowering.h"
14*0b57cec5SDimitry Andric #include "PPCInstrBuilder.h"
15*0b57cec5SDimitry Andric #include "PPCInstrInfo.h"
16*0b57cec5SDimitry Andric #include "PPCMachineFunctionInfo.h"
17*0b57cec5SDimitry Andric #include "PPCSubtarget.h"
18*0b57cec5SDimitry Andric #include "PPCTargetMachine.h"
19*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
25*0b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
26*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
27*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric using namespace llvm;
30*0b57cec5SDimitry Andric 
31*0b57cec5SDimitry Andric #define DEBUG_TYPE "framelowering"
32*0b57cec5SDimitry Andric STATISTIC(NumPESpillVSR, "Number of spills to vector in prologue");
33*0b57cec5SDimitry Andric STATISTIC(NumPEReloadVSR, "Number of reloads from vector in epilogue");
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric static cl::opt<bool>
36*0b57cec5SDimitry Andric EnablePEVectorSpills("ppc-enable-pe-vector-spills",
37*0b57cec5SDimitry Andric                      cl::desc("Enable spills in prologue to vector registers."),
38*0b57cec5SDimitry Andric                      cl::init(false), cl::Hidden);
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric /// VRRegNo - Map from a numbered VR register to its enum value.
41*0b57cec5SDimitry Andric ///
42*0b57cec5SDimitry Andric static const MCPhysReg VRRegNo[] = {
43*0b57cec5SDimitry Andric  PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
44*0b57cec5SDimitry Andric  PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
45*0b57cec5SDimitry Andric  PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
46*0b57cec5SDimitry Andric  PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
47*0b57cec5SDimitry Andric };
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) {
508bcb0991SDimitry Andric   if (STI.isDarwinABI() || STI.isAIXABI())
51*0b57cec5SDimitry Andric     return STI.isPPC64() ? 16 : 8;
52*0b57cec5SDimitry Andric   // SVR4 ABI:
53*0b57cec5SDimitry Andric   return STI.isPPC64() ? 16 : 4;
54*0b57cec5SDimitry Andric }
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) {
578bcb0991SDimitry Andric   if (STI.isAIXABI())
588bcb0991SDimitry Andric     return STI.isPPC64() ? 40 : 20;
59*0b57cec5SDimitry Andric   return STI.isELFv2ABI() ? 24 : 40;
60*0b57cec5SDimitry Andric }
61*0b57cec5SDimitry Andric 
62*0b57cec5SDimitry Andric static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) {
63*0b57cec5SDimitry Andric   // For the Darwin ABI:
64*0b57cec5SDimitry Andric   // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
65*0b57cec5SDimitry Andric   // for saving the frame pointer (if needed.)  While the published ABI has
66*0b57cec5SDimitry Andric   // not used this slot since at least MacOSX 10.2, there is older code
67*0b57cec5SDimitry Andric   // around that does use it, and that needs to continue to work.
68*0b57cec5SDimitry Andric   if (STI.isDarwinABI())
69*0b57cec5SDimitry Andric     return STI.isPPC64() ? -8U : -4U;
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric   // SVR4 ABI: First slot in the general register save area.
72*0b57cec5SDimitry Andric   return STI.isPPC64() ? -8U : -4U;
73*0b57cec5SDimitry Andric }
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric static unsigned computeLinkageSize(const PPCSubtarget &STI) {
76*0b57cec5SDimitry Andric   if ((STI.isDarwinABI() || STI.isAIXABI()) || STI.isPPC64())
77*0b57cec5SDimitry Andric     return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4);
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric   // 32-bit SVR4 ABI:
80*0b57cec5SDimitry Andric   return 8;
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric 
83*0b57cec5SDimitry Andric static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) {
84*0b57cec5SDimitry Andric   if (STI.isDarwinABI())
85*0b57cec5SDimitry Andric     return STI.isPPC64() ? -16U : -8U;
86*0b57cec5SDimitry Andric 
87*0b57cec5SDimitry Andric   // SVR4 ABI: First slot in the general register save area.
88*0b57cec5SDimitry Andric   return STI.isPPC64()
89*0b57cec5SDimitry Andric              ? -16U
90*0b57cec5SDimitry Andric              : STI.getTargetMachine().isPositionIndependent() ? -12U : -8U;
91*0b57cec5SDimitry Andric }
92*0b57cec5SDimitry Andric 
938bcb0991SDimitry Andric static unsigned computeCRSaveOffset() {
948bcb0991SDimitry Andric   // The condition register save offset needs to be updated for AIX PPC32.
958bcb0991SDimitry Andric   return 8;
968bcb0991SDimitry Andric }
978bcb0991SDimitry Andric 
98*0b57cec5SDimitry Andric PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
99*0b57cec5SDimitry Andric     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
100*0b57cec5SDimitry Andric                           STI.getPlatformStackAlignment(), 0),
101*0b57cec5SDimitry Andric       Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)),
102*0b57cec5SDimitry Andric       TOCSaveOffset(computeTOCSaveOffset(Subtarget)),
103*0b57cec5SDimitry Andric       FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)),
104*0b57cec5SDimitry Andric       LinkageSize(computeLinkageSize(Subtarget)),
1058bcb0991SDimitry Andric       BasePointerSaveOffset(computeBasePointerSaveOffset(Subtarget)),
1068bcb0991SDimitry Andric       CRSaveOffset(computeCRSaveOffset()) {}
107*0b57cec5SDimitry Andric 
108*0b57cec5SDimitry Andric // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
109*0b57cec5SDimitry Andric const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
110*0b57cec5SDimitry Andric     unsigned &NumEntries) const {
111*0b57cec5SDimitry Andric   if (Subtarget.isDarwinABI()) {
112*0b57cec5SDimitry Andric     NumEntries = 1;
113*0b57cec5SDimitry Andric     if (Subtarget.isPPC64()) {
114*0b57cec5SDimitry Andric       static const SpillSlot darwin64Offsets = {PPC::X31, -8};
115*0b57cec5SDimitry Andric       return &darwin64Offsets;
116*0b57cec5SDimitry Andric     } else {
117*0b57cec5SDimitry Andric       static const SpillSlot darwinOffsets = {PPC::R31, -4};
118*0b57cec5SDimitry Andric       return &darwinOffsets;
119*0b57cec5SDimitry Andric     }
120*0b57cec5SDimitry Andric   }
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric   // Early exit if not using the SVR4 ABI.
123*0b57cec5SDimitry Andric   if (!Subtarget.isSVR4ABI()) {
124*0b57cec5SDimitry Andric     NumEntries = 0;
125*0b57cec5SDimitry Andric     return nullptr;
126*0b57cec5SDimitry Andric   }
127*0b57cec5SDimitry Andric 
128*0b57cec5SDimitry Andric   // Note that the offsets here overlap, but this is fixed up in
129*0b57cec5SDimitry Andric   // processFunctionBeforeFrameFinalized.
130*0b57cec5SDimitry Andric 
131*0b57cec5SDimitry Andric   static const SpillSlot Offsets[] = {
132*0b57cec5SDimitry Andric       // Floating-point register save area offsets.
133*0b57cec5SDimitry Andric       {PPC::F31, -8},
134*0b57cec5SDimitry Andric       {PPC::F30, -16},
135*0b57cec5SDimitry Andric       {PPC::F29, -24},
136*0b57cec5SDimitry Andric       {PPC::F28, -32},
137*0b57cec5SDimitry Andric       {PPC::F27, -40},
138*0b57cec5SDimitry Andric       {PPC::F26, -48},
139*0b57cec5SDimitry Andric       {PPC::F25, -56},
140*0b57cec5SDimitry Andric       {PPC::F24, -64},
141*0b57cec5SDimitry Andric       {PPC::F23, -72},
142*0b57cec5SDimitry Andric       {PPC::F22, -80},
143*0b57cec5SDimitry Andric       {PPC::F21, -88},
144*0b57cec5SDimitry Andric       {PPC::F20, -96},
145*0b57cec5SDimitry Andric       {PPC::F19, -104},
146*0b57cec5SDimitry Andric       {PPC::F18, -112},
147*0b57cec5SDimitry Andric       {PPC::F17, -120},
148*0b57cec5SDimitry Andric       {PPC::F16, -128},
149*0b57cec5SDimitry Andric       {PPC::F15, -136},
150*0b57cec5SDimitry Andric       {PPC::F14, -144},
151*0b57cec5SDimitry Andric 
152*0b57cec5SDimitry Andric       // General register save area offsets.
153*0b57cec5SDimitry Andric       {PPC::R31, -4},
154*0b57cec5SDimitry Andric       {PPC::R30, -8},
155*0b57cec5SDimitry Andric       {PPC::R29, -12},
156*0b57cec5SDimitry Andric       {PPC::R28, -16},
157*0b57cec5SDimitry Andric       {PPC::R27, -20},
158*0b57cec5SDimitry Andric       {PPC::R26, -24},
159*0b57cec5SDimitry Andric       {PPC::R25, -28},
160*0b57cec5SDimitry Andric       {PPC::R24, -32},
161*0b57cec5SDimitry Andric       {PPC::R23, -36},
162*0b57cec5SDimitry Andric       {PPC::R22, -40},
163*0b57cec5SDimitry Andric       {PPC::R21, -44},
164*0b57cec5SDimitry Andric       {PPC::R20, -48},
165*0b57cec5SDimitry Andric       {PPC::R19, -52},
166*0b57cec5SDimitry Andric       {PPC::R18, -56},
167*0b57cec5SDimitry Andric       {PPC::R17, -60},
168*0b57cec5SDimitry Andric       {PPC::R16, -64},
169*0b57cec5SDimitry Andric       {PPC::R15, -68},
170*0b57cec5SDimitry Andric       {PPC::R14, -72},
171*0b57cec5SDimitry Andric 
172*0b57cec5SDimitry Andric       // CR save area offset.  We map each of the nonvolatile CR fields
173*0b57cec5SDimitry Andric       // to the slot for CR2, which is the first of the nonvolatile CR
174*0b57cec5SDimitry Andric       // fields to be assigned, so that we only allocate one save slot.
175*0b57cec5SDimitry Andric       // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
176*0b57cec5SDimitry Andric       {PPC::CR2, -4},
177*0b57cec5SDimitry Andric 
178*0b57cec5SDimitry Andric       // VRSAVE save area offset.
179*0b57cec5SDimitry Andric       {PPC::VRSAVE, -4},
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric       // Vector register save area
182*0b57cec5SDimitry Andric       {PPC::V31, -16},
183*0b57cec5SDimitry Andric       {PPC::V30, -32},
184*0b57cec5SDimitry Andric       {PPC::V29, -48},
185*0b57cec5SDimitry Andric       {PPC::V28, -64},
186*0b57cec5SDimitry Andric       {PPC::V27, -80},
187*0b57cec5SDimitry Andric       {PPC::V26, -96},
188*0b57cec5SDimitry Andric       {PPC::V25, -112},
189*0b57cec5SDimitry Andric       {PPC::V24, -128},
190*0b57cec5SDimitry Andric       {PPC::V23, -144},
191*0b57cec5SDimitry Andric       {PPC::V22, -160},
192*0b57cec5SDimitry Andric       {PPC::V21, -176},
193*0b57cec5SDimitry Andric       {PPC::V20, -192},
194*0b57cec5SDimitry Andric 
195*0b57cec5SDimitry Andric       // SPE register save area (overlaps Vector save area).
196*0b57cec5SDimitry Andric       {PPC::S31, -8},
197*0b57cec5SDimitry Andric       {PPC::S30, -16},
198*0b57cec5SDimitry Andric       {PPC::S29, -24},
199*0b57cec5SDimitry Andric       {PPC::S28, -32},
200*0b57cec5SDimitry Andric       {PPC::S27, -40},
201*0b57cec5SDimitry Andric       {PPC::S26, -48},
202*0b57cec5SDimitry Andric       {PPC::S25, -56},
203*0b57cec5SDimitry Andric       {PPC::S24, -64},
204*0b57cec5SDimitry Andric       {PPC::S23, -72},
205*0b57cec5SDimitry Andric       {PPC::S22, -80},
206*0b57cec5SDimitry Andric       {PPC::S21, -88},
207*0b57cec5SDimitry Andric       {PPC::S20, -96},
208*0b57cec5SDimitry Andric       {PPC::S19, -104},
209*0b57cec5SDimitry Andric       {PPC::S18, -112},
210*0b57cec5SDimitry Andric       {PPC::S17, -120},
211*0b57cec5SDimitry Andric       {PPC::S16, -128},
212*0b57cec5SDimitry Andric       {PPC::S15, -136},
213*0b57cec5SDimitry Andric       {PPC::S14, -144}};
214*0b57cec5SDimitry Andric 
215*0b57cec5SDimitry Andric   static const SpillSlot Offsets64[] = {
216*0b57cec5SDimitry Andric       // Floating-point register save area offsets.
217*0b57cec5SDimitry Andric       {PPC::F31, -8},
218*0b57cec5SDimitry Andric       {PPC::F30, -16},
219*0b57cec5SDimitry Andric       {PPC::F29, -24},
220*0b57cec5SDimitry Andric       {PPC::F28, -32},
221*0b57cec5SDimitry Andric       {PPC::F27, -40},
222*0b57cec5SDimitry Andric       {PPC::F26, -48},
223*0b57cec5SDimitry Andric       {PPC::F25, -56},
224*0b57cec5SDimitry Andric       {PPC::F24, -64},
225*0b57cec5SDimitry Andric       {PPC::F23, -72},
226*0b57cec5SDimitry Andric       {PPC::F22, -80},
227*0b57cec5SDimitry Andric       {PPC::F21, -88},
228*0b57cec5SDimitry Andric       {PPC::F20, -96},
229*0b57cec5SDimitry Andric       {PPC::F19, -104},
230*0b57cec5SDimitry Andric       {PPC::F18, -112},
231*0b57cec5SDimitry Andric       {PPC::F17, -120},
232*0b57cec5SDimitry Andric       {PPC::F16, -128},
233*0b57cec5SDimitry Andric       {PPC::F15, -136},
234*0b57cec5SDimitry Andric       {PPC::F14, -144},
235*0b57cec5SDimitry Andric 
236*0b57cec5SDimitry Andric       // General register save area offsets.
237*0b57cec5SDimitry Andric       {PPC::X31, -8},
238*0b57cec5SDimitry Andric       {PPC::X30, -16},
239*0b57cec5SDimitry Andric       {PPC::X29, -24},
240*0b57cec5SDimitry Andric       {PPC::X28, -32},
241*0b57cec5SDimitry Andric       {PPC::X27, -40},
242*0b57cec5SDimitry Andric       {PPC::X26, -48},
243*0b57cec5SDimitry Andric       {PPC::X25, -56},
244*0b57cec5SDimitry Andric       {PPC::X24, -64},
245*0b57cec5SDimitry Andric       {PPC::X23, -72},
246*0b57cec5SDimitry Andric       {PPC::X22, -80},
247*0b57cec5SDimitry Andric       {PPC::X21, -88},
248*0b57cec5SDimitry Andric       {PPC::X20, -96},
249*0b57cec5SDimitry Andric       {PPC::X19, -104},
250*0b57cec5SDimitry Andric       {PPC::X18, -112},
251*0b57cec5SDimitry Andric       {PPC::X17, -120},
252*0b57cec5SDimitry Andric       {PPC::X16, -128},
253*0b57cec5SDimitry Andric       {PPC::X15, -136},
254*0b57cec5SDimitry Andric       {PPC::X14, -144},
255*0b57cec5SDimitry Andric 
256*0b57cec5SDimitry Andric       // VRSAVE save area offset.
257*0b57cec5SDimitry Andric       {PPC::VRSAVE, -4},
258*0b57cec5SDimitry Andric 
259*0b57cec5SDimitry Andric       // Vector register save area
260*0b57cec5SDimitry Andric       {PPC::V31, -16},
261*0b57cec5SDimitry Andric       {PPC::V30, -32},
262*0b57cec5SDimitry Andric       {PPC::V29, -48},
263*0b57cec5SDimitry Andric       {PPC::V28, -64},
264*0b57cec5SDimitry Andric       {PPC::V27, -80},
265*0b57cec5SDimitry Andric       {PPC::V26, -96},
266*0b57cec5SDimitry Andric       {PPC::V25, -112},
267*0b57cec5SDimitry Andric       {PPC::V24, -128},
268*0b57cec5SDimitry Andric       {PPC::V23, -144},
269*0b57cec5SDimitry Andric       {PPC::V22, -160},
270*0b57cec5SDimitry Andric       {PPC::V21, -176},
271*0b57cec5SDimitry Andric       {PPC::V20, -192}};
272*0b57cec5SDimitry Andric 
273*0b57cec5SDimitry Andric   if (Subtarget.isPPC64()) {
274*0b57cec5SDimitry Andric     NumEntries = array_lengthof(Offsets64);
275*0b57cec5SDimitry Andric 
276*0b57cec5SDimitry Andric     return Offsets64;
277*0b57cec5SDimitry Andric   } else {
278*0b57cec5SDimitry Andric     NumEntries = array_lengthof(Offsets);
279*0b57cec5SDimitry Andric 
280*0b57cec5SDimitry Andric     return Offsets;
281*0b57cec5SDimitry Andric   }
282*0b57cec5SDimitry Andric }
283*0b57cec5SDimitry Andric 
284*0b57cec5SDimitry Andric /// RemoveVRSaveCode - We have found that this function does not need any code
285*0b57cec5SDimitry Andric /// to manipulate the VRSAVE register, even though it uses vector registers.
286*0b57cec5SDimitry Andric /// This can happen when the only registers used are known to be live in or out
287*0b57cec5SDimitry Andric /// of the function.  Remove all of the VRSAVE related code from the function.
288*0b57cec5SDimitry Andric /// FIXME: The removal of the code results in a compile failure at -O0 when the
289*0b57cec5SDimitry Andric /// function contains a function call, as the GPR containing original VRSAVE
290*0b57cec5SDimitry Andric /// contents is spilled and reloaded around the call.  Without the prolog code,
291*0b57cec5SDimitry Andric /// the spill instruction refers to an undefined register.  This code needs
292*0b57cec5SDimitry Andric /// to account for all uses of that GPR.
293*0b57cec5SDimitry Andric static void RemoveVRSaveCode(MachineInstr &MI) {
294*0b57cec5SDimitry Andric   MachineBasicBlock *Entry = MI.getParent();
295*0b57cec5SDimitry Andric   MachineFunction *MF = Entry->getParent();
296*0b57cec5SDimitry Andric 
297*0b57cec5SDimitry Andric   // We know that the MTVRSAVE instruction immediately follows MI.  Remove it.
298*0b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MI;
299*0b57cec5SDimitry Andric   ++MBBI;
300*0b57cec5SDimitry Andric   assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
301*0b57cec5SDimitry Andric   MBBI->eraseFromParent();
302*0b57cec5SDimitry Andric 
303*0b57cec5SDimitry Andric   bool RemovedAllMTVRSAVEs = true;
304*0b57cec5SDimitry Andric   // See if we can find and remove the MTVRSAVE instruction from all of the
305*0b57cec5SDimitry Andric   // epilog blocks.
306*0b57cec5SDimitry Andric   for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
307*0b57cec5SDimitry Andric     // If last instruction is a return instruction, add an epilogue
308*0b57cec5SDimitry Andric     if (I->isReturnBlock()) {
309*0b57cec5SDimitry Andric       bool FoundIt = false;
310*0b57cec5SDimitry Andric       for (MBBI = I->end(); MBBI != I->begin(); ) {
311*0b57cec5SDimitry Andric         --MBBI;
312*0b57cec5SDimitry Andric         if (MBBI->getOpcode() == PPC::MTVRSAVE) {
313*0b57cec5SDimitry Andric           MBBI->eraseFromParent();  // remove it.
314*0b57cec5SDimitry Andric           FoundIt = true;
315*0b57cec5SDimitry Andric           break;
316*0b57cec5SDimitry Andric         }
317*0b57cec5SDimitry Andric       }
318*0b57cec5SDimitry Andric       RemovedAllMTVRSAVEs &= FoundIt;
319*0b57cec5SDimitry Andric     }
320*0b57cec5SDimitry Andric   }
321*0b57cec5SDimitry Andric 
322*0b57cec5SDimitry Andric   // If we found and removed all MTVRSAVE instructions, remove the read of
323*0b57cec5SDimitry Andric   // VRSAVE as well.
324*0b57cec5SDimitry Andric   if (RemovedAllMTVRSAVEs) {
325*0b57cec5SDimitry Andric     MBBI = MI;
326*0b57cec5SDimitry Andric     assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
327*0b57cec5SDimitry Andric     --MBBI;
328*0b57cec5SDimitry Andric     assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
329*0b57cec5SDimitry Andric     MBBI->eraseFromParent();
330*0b57cec5SDimitry Andric   }
331*0b57cec5SDimitry Andric 
332*0b57cec5SDimitry Andric   // Finally, nuke the UPDATE_VRSAVE.
333*0b57cec5SDimitry Andric   MI.eraseFromParent();
334*0b57cec5SDimitry Andric }
335*0b57cec5SDimitry Andric 
336*0b57cec5SDimitry Andric // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
337*0b57cec5SDimitry Andric // instruction selector.  Based on the vector registers that have been used,
338*0b57cec5SDimitry Andric // transform this into the appropriate ORI instruction.
339*0b57cec5SDimitry Andric static void HandleVRSaveUpdate(MachineInstr &MI, const TargetInstrInfo &TII) {
340*0b57cec5SDimitry Andric   MachineFunction *MF = MI.getParent()->getParent();
341*0b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
342*0b57cec5SDimitry Andric   DebugLoc dl = MI.getDebugLoc();
343*0b57cec5SDimitry Andric 
344*0b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF->getRegInfo();
345*0b57cec5SDimitry Andric   unsigned UsedRegMask = 0;
346*0b57cec5SDimitry Andric   for (unsigned i = 0; i != 32; ++i)
347*0b57cec5SDimitry Andric     if (MRI.isPhysRegModified(VRRegNo[i]))
348*0b57cec5SDimitry Andric       UsedRegMask |= 1 << (31-i);
349*0b57cec5SDimitry Andric 
350*0b57cec5SDimitry Andric   // Live in and live out values already must be in the mask, so don't bother
351*0b57cec5SDimitry Andric   // marking them.
352*0b57cec5SDimitry Andric   for (std::pair<unsigned, unsigned> LI : MF->getRegInfo().liveins()) {
353*0b57cec5SDimitry Andric     unsigned RegNo = TRI->getEncodingValue(LI.first);
354*0b57cec5SDimitry Andric     if (VRRegNo[RegNo] == LI.first)        // If this really is a vector reg.
355*0b57cec5SDimitry Andric       UsedRegMask &= ~(1 << (31-RegNo));   // Doesn't need to be marked.
356*0b57cec5SDimitry Andric   }
357*0b57cec5SDimitry Andric 
358*0b57cec5SDimitry Andric   // Live out registers appear as use operands on return instructions.
359*0b57cec5SDimitry Andric   for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
360*0b57cec5SDimitry Andric        UsedRegMask != 0 && BI != BE; ++BI) {
361*0b57cec5SDimitry Andric     const MachineBasicBlock &MBB = *BI;
362*0b57cec5SDimitry Andric     if (!MBB.isReturnBlock())
363*0b57cec5SDimitry Andric       continue;
364*0b57cec5SDimitry Andric     const MachineInstr &Ret = MBB.back();
365*0b57cec5SDimitry Andric     for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
366*0b57cec5SDimitry Andric       const MachineOperand &MO = Ret.getOperand(I);
367*0b57cec5SDimitry Andric       if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
368*0b57cec5SDimitry Andric         continue;
369*0b57cec5SDimitry Andric       unsigned RegNo = TRI->getEncodingValue(MO.getReg());
370*0b57cec5SDimitry Andric       UsedRegMask &= ~(1 << (31-RegNo));
371*0b57cec5SDimitry Andric     }
372*0b57cec5SDimitry Andric   }
373*0b57cec5SDimitry Andric 
374*0b57cec5SDimitry Andric   // If no registers are used, turn this into a copy.
375*0b57cec5SDimitry Andric   if (UsedRegMask == 0) {
376*0b57cec5SDimitry Andric     // Remove all VRSAVE code.
377*0b57cec5SDimitry Andric     RemoveVRSaveCode(MI);
378*0b57cec5SDimitry Andric     return;
379*0b57cec5SDimitry Andric   }
380*0b57cec5SDimitry Andric 
3818bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
3828bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
383*0b57cec5SDimitry Andric 
384*0b57cec5SDimitry Andric   if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
385*0b57cec5SDimitry Andric     if (DstReg != SrcReg)
386*0b57cec5SDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
387*0b57cec5SDimitry Andric           .addReg(SrcReg)
388*0b57cec5SDimitry Andric           .addImm(UsedRegMask);
389*0b57cec5SDimitry Andric     else
390*0b57cec5SDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
391*0b57cec5SDimitry Andric           .addReg(SrcReg, RegState::Kill)
392*0b57cec5SDimitry Andric           .addImm(UsedRegMask);
393*0b57cec5SDimitry Andric   } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
394*0b57cec5SDimitry Andric     if (DstReg != SrcReg)
395*0b57cec5SDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
396*0b57cec5SDimitry Andric           .addReg(SrcReg)
397*0b57cec5SDimitry Andric           .addImm(UsedRegMask >> 16);
398*0b57cec5SDimitry Andric     else
399*0b57cec5SDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
400*0b57cec5SDimitry Andric           .addReg(SrcReg, RegState::Kill)
401*0b57cec5SDimitry Andric           .addImm(UsedRegMask >> 16);
402*0b57cec5SDimitry Andric   } else {
403*0b57cec5SDimitry Andric     if (DstReg != SrcReg)
404*0b57cec5SDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
405*0b57cec5SDimitry Andric           .addReg(SrcReg)
406*0b57cec5SDimitry Andric           .addImm(UsedRegMask >> 16);
407*0b57cec5SDimitry Andric     else
408*0b57cec5SDimitry Andric       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
409*0b57cec5SDimitry Andric           .addReg(SrcReg, RegState::Kill)
410*0b57cec5SDimitry Andric           .addImm(UsedRegMask >> 16);
411*0b57cec5SDimitry Andric 
412*0b57cec5SDimitry Andric     BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
413*0b57cec5SDimitry Andric         .addReg(DstReg, RegState::Kill)
414*0b57cec5SDimitry Andric         .addImm(UsedRegMask & 0xFFFF);
415*0b57cec5SDimitry Andric   }
416*0b57cec5SDimitry Andric 
417*0b57cec5SDimitry Andric   // Remove the old UPDATE_VRSAVE instruction.
418*0b57cec5SDimitry Andric   MI.eraseFromParent();
419*0b57cec5SDimitry Andric }
420*0b57cec5SDimitry Andric 
421*0b57cec5SDimitry Andric static bool spillsCR(const MachineFunction &MF) {
422*0b57cec5SDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
423*0b57cec5SDimitry Andric   return FuncInfo->isCRSpilled();
424*0b57cec5SDimitry Andric }
425*0b57cec5SDimitry Andric 
426*0b57cec5SDimitry Andric static bool spillsVRSAVE(const MachineFunction &MF) {
427*0b57cec5SDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
428*0b57cec5SDimitry Andric   return FuncInfo->isVRSAVESpilled();
429*0b57cec5SDimitry Andric }
430*0b57cec5SDimitry Andric 
431*0b57cec5SDimitry Andric static bool hasSpills(const MachineFunction &MF) {
432*0b57cec5SDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
433*0b57cec5SDimitry Andric   return FuncInfo->hasSpills();
434*0b57cec5SDimitry Andric }
435*0b57cec5SDimitry Andric 
436*0b57cec5SDimitry Andric static bool hasNonRISpills(const MachineFunction &MF) {
437*0b57cec5SDimitry Andric   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
438*0b57cec5SDimitry Andric   return FuncInfo->hasNonRISpills();
439*0b57cec5SDimitry Andric }
440*0b57cec5SDimitry Andric 
441*0b57cec5SDimitry Andric /// MustSaveLR - Return true if this function requires that we save the LR
442*0b57cec5SDimitry Andric /// register onto the stack in the prolog and restore it in the epilog of the
443*0b57cec5SDimitry Andric /// function.
444*0b57cec5SDimitry Andric static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
445*0b57cec5SDimitry Andric   const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
446*0b57cec5SDimitry Andric 
447*0b57cec5SDimitry Andric   // We need a save/restore of LR if there is any def of LR (which is
448*0b57cec5SDimitry Andric   // defined by calls, including the PIC setup sequence), or if there is
449*0b57cec5SDimitry Andric   // some use of the LR stack slot (e.g. for builtin_return_address).
450*0b57cec5SDimitry Andric   // (LR comes in 32 and 64 bit versions.)
451*0b57cec5SDimitry Andric   MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
452*0b57cec5SDimitry Andric   return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
453*0b57cec5SDimitry Andric }
454*0b57cec5SDimitry Andric 
455*0b57cec5SDimitry Andric /// determineFrameLayoutAndUpdate - Determine the size of the frame and maximum
456*0b57cec5SDimitry Andric /// call frame size. Update the MachineFunction object with the stack size.
457*0b57cec5SDimitry Andric unsigned
458*0b57cec5SDimitry Andric PPCFrameLowering::determineFrameLayoutAndUpdate(MachineFunction &MF,
459*0b57cec5SDimitry Andric                                                 bool UseEstimate) const {
460*0b57cec5SDimitry Andric   unsigned NewMaxCallFrameSize = 0;
461*0b57cec5SDimitry Andric   unsigned FrameSize = determineFrameLayout(MF, UseEstimate,
462*0b57cec5SDimitry Andric                                             &NewMaxCallFrameSize);
463*0b57cec5SDimitry Andric   MF.getFrameInfo().setStackSize(FrameSize);
464*0b57cec5SDimitry Andric   MF.getFrameInfo().setMaxCallFrameSize(NewMaxCallFrameSize);
465*0b57cec5SDimitry Andric   return FrameSize;
466*0b57cec5SDimitry Andric }
467*0b57cec5SDimitry Andric 
468*0b57cec5SDimitry Andric /// determineFrameLayout - Determine the size of the frame and maximum call
469*0b57cec5SDimitry Andric /// frame size.
470*0b57cec5SDimitry Andric unsigned
471*0b57cec5SDimitry Andric PPCFrameLowering::determineFrameLayout(const MachineFunction &MF,
472*0b57cec5SDimitry Andric                                        bool UseEstimate,
473*0b57cec5SDimitry Andric                                        unsigned *NewMaxCallFrameSize) const {
474*0b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
475*0b57cec5SDimitry Andric   const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
476*0b57cec5SDimitry Andric 
477*0b57cec5SDimitry Andric   // Get the number of bytes to allocate from the FrameInfo
478*0b57cec5SDimitry Andric   unsigned FrameSize =
479*0b57cec5SDimitry Andric     UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize();
480*0b57cec5SDimitry Andric 
481*0b57cec5SDimitry Andric   // Get stack alignments. The frame must be aligned to the greatest of these:
482*0b57cec5SDimitry Andric   unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
483*0b57cec5SDimitry Andric   unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame
484*0b57cec5SDimitry Andric   unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
485*0b57cec5SDimitry Andric 
486*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
487*0b57cec5SDimitry Andric 
488*0b57cec5SDimitry Andric   unsigned LR = RegInfo->getRARegister();
489*0b57cec5SDimitry Andric   bool DisableRedZone = MF.getFunction().hasFnAttribute(Attribute::NoRedZone);
490*0b57cec5SDimitry Andric   bool CanUseRedZone = !MFI.hasVarSizedObjects() && // No dynamic alloca.
491*0b57cec5SDimitry Andric                        !MFI.adjustsStack() &&       // No calls.
492*0b57cec5SDimitry Andric                        !MustSaveLR(MF, LR) &&       // No need to save LR.
493*0b57cec5SDimitry Andric                        !FI->mustSaveTOC() &&        // No need to save TOC.
494*0b57cec5SDimitry Andric                        !RegInfo->hasBasePointer(MF); // No special alignment.
495*0b57cec5SDimitry Andric 
496*0b57cec5SDimitry Andric   // Note: for PPC32 SVR4ABI (Non-DarwinABI), we can still generate stackless
497*0b57cec5SDimitry Andric   // code if all local vars are reg-allocated.
498*0b57cec5SDimitry Andric   bool FitsInRedZone = FrameSize <= Subtarget.getRedZoneSize();
499*0b57cec5SDimitry Andric 
500*0b57cec5SDimitry Andric   // Check whether we can skip adjusting the stack pointer (by using red zone)
501*0b57cec5SDimitry Andric   if (!DisableRedZone && CanUseRedZone && FitsInRedZone) {
502*0b57cec5SDimitry Andric     // No need for frame
503*0b57cec5SDimitry Andric     return 0;
504*0b57cec5SDimitry Andric   }
505*0b57cec5SDimitry Andric 
506*0b57cec5SDimitry Andric   // Get the maximum call frame size of all the calls.
507*0b57cec5SDimitry Andric   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
508*0b57cec5SDimitry Andric 
509*0b57cec5SDimitry Andric   // Maximum call frame needs to be at least big enough for linkage area.
510*0b57cec5SDimitry Andric   unsigned minCallFrameSize = getLinkageSize();
511*0b57cec5SDimitry Andric   maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
512*0b57cec5SDimitry Andric 
513*0b57cec5SDimitry Andric   // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
514*0b57cec5SDimitry Andric   // that allocations will be aligned.
515*0b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects())
516*0b57cec5SDimitry Andric     maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
517*0b57cec5SDimitry Andric 
518*0b57cec5SDimitry Andric   // Update the new max call frame size if the caller passes in a valid pointer.
519*0b57cec5SDimitry Andric   if (NewMaxCallFrameSize)
520*0b57cec5SDimitry Andric     *NewMaxCallFrameSize = maxCallFrameSize;
521*0b57cec5SDimitry Andric 
522*0b57cec5SDimitry Andric   // Include call frame size in total.
523*0b57cec5SDimitry Andric   FrameSize += maxCallFrameSize;
524*0b57cec5SDimitry Andric 
525*0b57cec5SDimitry Andric   // Make sure the frame is aligned.
526*0b57cec5SDimitry Andric   FrameSize = (FrameSize + AlignMask) & ~AlignMask;
527*0b57cec5SDimitry Andric 
528*0b57cec5SDimitry Andric   return FrameSize;
529*0b57cec5SDimitry Andric }
530*0b57cec5SDimitry Andric 
531*0b57cec5SDimitry Andric // hasFP - Return true if the specified function actually has a dedicated frame
532*0b57cec5SDimitry Andric // pointer register.
533*0b57cec5SDimitry Andric bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
534*0b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
535*0b57cec5SDimitry Andric   // FIXME: This is pretty much broken by design: hasFP() might be called really
536*0b57cec5SDimitry Andric   // early, before the stack layout was calculated and thus hasFP() might return
537*0b57cec5SDimitry Andric   // true or false here depending on the time of call.
538*0b57cec5SDimitry Andric   return (MFI.getStackSize()) && needsFP(MF);
539*0b57cec5SDimitry Andric }
540*0b57cec5SDimitry Andric 
541*0b57cec5SDimitry Andric // needsFP - Return true if the specified function should have a dedicated frame
542*0b57cec5SDimitry Andric // pointer register.  This is true if the function has variable sized allocas or
543*0b57cec5SDimitry Andric // if frame pointer elimination is disabled.
544*0b57cec5SDimitry Andric bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
545*0b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
546*0b57cec5SDimitry Andric 
547*0b57cec5SDimitry Andric   // Naked functions have no stack frame pushed, so we don't have a frame
548*0b57cec5SDimitry Andric   // pointer.
549*0b57cec5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::Naked))
550*0b57cec5SDimitry Andric     return false;
551*0b57cec5SDimitry Andric 
552*0b57cec5SDimitry Andric   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
553*0b57cec5SDimitry Andric     MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
554*0b57cec5SDimitry Andric     (MF.getTarget().Options.GuaranteedTailCallOpt &&
555*0b57cec5SDimitry Andric      MF.getInfo<PPCFunctionInfo>()->hasFastCall());
556*0b57cec5SDimitry Andric }
557*0b57cec5SDimitry Andric 
558*0b57cec5SDimitry Andric void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
559*0b57cec5SDimitry Andric   bool is31 = needsFP(MF);
560*0b57cec5SDimitry Andric   unsigned FPReg  = is31 ? PPC::R31 : PPC::R1;
561*0b57cec5SDimitry Andric   unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
562*0b57cec5SDimitry Andric 
563*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
564*0b57cec5SDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
565*0b57cec5SDimitry Andric   unsigned BPReg  = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
566*0b57cec5SDimitry Andric   unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FP8Reg;
567*0b57cec5SDimitry Andric 
568*0b57cec5SDimitry Andric   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
569*0b57cec5SDimitry Andric        BI != BE; ++BI)
570*0b57cec5SDimitry Andric     for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
571*0b57cec5SDimitry Andric       --MBBI;
572*0b57cec5SDimitry Andric       for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
573*0b57cec5SDimitry Andric         MachineOperand &MO = MBBI->getOperand(I);
574*0b57cec5SDimitry Andric         if (!MO.isReg())
575*0b57cec5SDimitry Andric           continue;
576*0b57cec5SDimitry Andric 
577*0b57cec5SDimitry Andric         switch (MO.getReg()) {
578*0b57cec5SDimitry Andric         case PPC::FP:
579*0b57cec5SDimitry Andric           MO.setReg(FPReg);
580*0b57cec5SDimitry Andric           break;
581*0b57cec5SDimitry Andric         case PPC::FP8:
582*0b57cec5SDimitry Andric           MO.setReg(FP8Reg);
583*0b57cec5SDimitry Andric           break;
584*0b57cec5SDimitry Andric         case PPC::BP:
585*0b57cec5SDimitry Andric           MO.setReg(BPReg);
586*0b57cec5SDimitry Andric           break;
587*0b57cec5SDimitry Andric         case PPC::BP8:
588*0b57cec5SDimitry Andric           MO.setReg(BP8Reg);
589*0b57cec5SDimitry Andric           break;
590*0b57cec5SDimitry Andric 
591*0b57cec5SDimitry Andric         }
592*0b57cec5SDimitry Andric       }
593*0b57cec5SDimitry Andric     }
594*0b57cec5SDimitry Andric }
595*0b57cec5SDimitry Andric 
596*0b57cec5SDimitry Andric /*  This function will do the following:
597*0b57cec5SDimitry Andric     - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
598*0b57cec5SDimitry Andric       respectively (defaults recommended by the ABI) and return true
599*0b57cec5SDimitry Andric     - If MBB is not an entry block, initialize the register scavenger and look
600*0b57cec5SDimitry Andric       for available registers.
601*0b57cec5SDimitry Andric     - If the defaults (R0/R12) are available, return true
602*0b57cec5SDimitry Andric     - If TwoUniqueRegsRequired is set to true, it looks for two unique
603*0b57cec5SDimitry Andric       registers. Otherwise, look for a single available register.
604*0b57cec5SDimitry Andric       - If the required registers are found, set SR1 and SR2 and return true.
605*0b57cec5SDimitry Andric       - If the required registers are not found, set SR2 or both SR1 and SR2 to
606*0b57cec5SDimitry Andric         PPC::NoRegister and return false.
607*0b57cec5SDimitry Andric 
608*0b57cec5SDimitry Andric     Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
609*0b57cec5SDimitry Andric     is not set, this function will attempt to find two different registers, but
610*0b57cec5SDimitry Andric     still return true if only one register is available (and set SR1 == SR2).
611*0b57cec5SDimitry Andric */
612*0b57cec5SDimitry Andric bool
613*0b57cec5SDimitry Andric PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
614*0b57cec5SDimitry Andric                                       bool UseAtEnd,
615*0b57cec5SDimitry Andric                                       bool TwoUniqueRegsRequired,
616*0b57cec5SDimitry Andric                                       unsigned *SR1,
617*0b57cec5SDimitry Andric                                       unsigned *SR2) const {
618*0b57cec5SDimitry Andric   RegScavenger RS;
619*0b57cec5SDimitry Andric   unsigned R0 =  Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
620*0b57cec5SDimitry Andric   unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
621*0b57cec5SDimitry Andric 
622*0b57cec5SDimitry Andric   // Set the defaults for the two scratch registers.
623*0b57cec5SDimitry Andric   if (SR1)
624*0b57cec5SDimitry Andric     *SR1 = R0;
625*0b57cec5SDimitry Andric 
626*0b57cec5SDimitry Andric   if (SR2) {
627*0b57cec5SDimitry Andric     assert (SR1 && "Asking for the second scratch register but not the first?");
628*0b57cec5SDimitry Andric     *SR2 = R12;
629*0b57cec5SDimitry Andric   }
630*0b57cec5SDimitry Andric 
631*0b57cec5SDimitry Andric   // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
632*0b57cec5SDimitry Andric   if ((UseAtEnd && MBB->isReturnBlock()) ||
633*0b57cec5SDimitry Andric       (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
634*0b57cec5SDimitry Andric     return true;
635*0b57cec5SDimitry Andric 
636*0b57cec5SDimitry Andric   RS.enterBasicBlock(*MBB);
637*0b57cec5SDimitry Andric 
638*0b57cec5SDimitry Andric   if (UseAtEnd && !MBB->empty()) {
639*0b57cec5SDimitry Andric     // The scratch register will be used at the end of the block, so must
640*0b57cec5SDimitry Andric     // consider all registers used within the block
641*0b57cec5SDimitry Andric 
642*0b57cec5SDimitry Andric     MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
643*0b57cec5SDimitry Andric     // If no terminator, back iterator up to previous instruction.
644*0b57cec5SDimitry Andric     if (MBBI == MBB->end())
645*0b57cec5SDimitry Andric       MBBI = std::prev(MBBI);
646*0b57cec5SDimitry Andric 
647*0b57cec5SDimitry Andric     if (MBBI != MBB->begin())
648*0b57cec5SDimitry Andric       RS.forward(MBBI);
649*0b57cec5SDimitry Andric   }
650*0b57cec5SDimitry Andric 
651*0b57cec5SDimitry Andric   // If the two registers are available, we're all good.
652*0b57cec5SDimitry Andric   // Note that we only return here if both R0 and R12 are available because
653*0b57cec5SDimitry Andric   // although the function may not require two unique registers, it may benefit
654*0b57cec5SDimitry Andric   // from having two so we should try to provide them.
655*0b57cec5SDimitry Andric   if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12))
656*0b57cec5SDimitry Andric     return true;
657*0b57cec5SDimitry Andric 
658*0b57cec5SDimitry Andric   // Get the list of callee-saved registers for the target.
659*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
660*0b57cec5SDimitry Andric   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent());
661*0b57cec5SDimitry Andric 
662*0b57cec5SDimitry Andric   // Get all the available registers in the block.
663*0b57cec5SDimitry Andric   BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass :
664*0b57cec5SDimitry Andric                                      &PPC::GPRCRegClass);
665*0b57cec5SDimitry Andric 
666*0b57cec5SDimitry Andric   // We shouldn't use callee-saved registers as scratch registers as they may be
667*0b57cec5SDimitry Andric   // available when looking for a candidate block for shrink wrapping but not
668*0b57cec5SDimitry Andric   // available when the actual prologue/epilogue is being emitted because they
669*0b57cec5SDimitry Andric   // were added as live-in to the prologue block by PrologueEpilogueInserter.
670*0b57cec5SDimitry Andric   for (int i = 0; CSRegs[i]; ++i)
671*0b57cec5SDimitry Andric     BV.reset(CSRegs[i]);
672*0b57cec5SDimitry Andric 
673*0b57cec5SDimitry Andric   // Set the first scratch register to the first available one.
674*0b57cec5SDimitry Andric   if (SR1) {
675*0b57cec5SDimitry Andric     int FirstScratchReg = BV.find_first();
676*0b57cec5SDimitry Andric     *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
677*0b57cec5SDimitry Andric   }
678*0b57cec5SDimitry Andric 
679*0b57cec5SDimitry Andric   // If there is another one available, set the second scratch register to that.
680*0b57cec5SDimitry Andric   // Otherwise, set it to either PPC::NoRegister if this function requires two
681*0b57cec5SDimitry Andric   // or to whatever SR1 is set to if this function doesn't require two.
682*0b57cec5SDimitry Andric   if (SR2) {
683*0b57cec5SDimitry Andric     int SecondScratchReg = BV.find_next(*SR1);
684*0b57cec5SDimitry Andric     if (SecondScratchReg != -1)
685*0b57cec5SDimitry Andric       *SR2 = SecondScratchReg;
686*0b57cec5SDimitry Andric     else
687*0b57cec5SDimitry Andric       *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1;
688*0b57cec5SDimitry Andric   }
689*0b57cec5SDimitry Andric 
690*0b57cec5SDimitry Andric   // Now that we've done our best to provide both registers, double check
691*0b57cec5SDimitry Andric   // whether we were unable to provide enough.
692*0b57cec5SDimitry Andric   if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
693*0b57cec5SDimitry Andric     return false;
694*0b57cec5SDimitry Andric 
695*0b57cec5SDimitry Andric   return true;
696*0b57cec5SDimitry Andric }
697*0b57cec5SDimitry Andric 
698*0b57cec5SDimitry Andric // We need a scratch register for spilling LR and for spilling CR. By default,
699*0b57cec5SDimitry Andric // we use two scratch registers to hide latency. However, if only one scratch
700*0b57cec5SDimitry Andric // register is available, we can adjust for that by not overlapping the spill
701*0b57cec5SDimitry Andric // code. However, if we need to realign the stack (i.e. have a base pointer)
702*0b57cec5SDimitry Andric // and the stack frame is large, we need two scratch registers.
703*0b57cec5SDimitry Andric bool
704*0b57cec5SDimitry Andric PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
705*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
706*0b57cec5SDimitry Andric   MachineFunction &MF = *(MBB->getParent());
707*0b57cec5SDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
708*0b57cec5SDimitry Andric   unsigned FrameSize = determineFrameLayout(MF);
709*0b57cec5SDimitry Andric   int NegFrameSize = -FrameSize;
710*0b57cec5SDimitry Andric   bool IsLargeFrame = !isInt<16>(NegFrameSize);
711*0b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
712*0b57cec5SDimitry Andric   unsigned MaxAlign = MFI.getMaxAlignment();
713*0b57cec5SDimitry Andric   bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
714*0b57cec5SDimitry Andric 
715*0b57cec5SDimitry Andric   return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1;
716*0b57cec5SDimitry Andric }
717*0b57cec5SDimitry Andric 
718*0b57cec5SDimitry Andric bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
719*0b57cec5SDimitry Andric   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
720*0b57cec5SDimitry Andric 
721*0b57cec5SDimitry Andric   return findScratchRegister(TmpMBB, false,
722*0b57cec5SDimitry Andric                              twoUniqueScratchRegsRequired(TmpMBB));
723*0b57cec5SDimitry Andric }
724*0b57cec5SDimitry Andric 
725*0b57cec5SDimitry Andric bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
726*0b57cec5SDimitry Andric   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
727*0b57cec5SDimitry Andric 
728*0b57cec5SDimitry Andric   return findScratchRegister(TmpMBB, true);
729*0b57cec5SDimitry Andric }
730*0b57cec5SDimitry Andric 
731*0b57cec5SDimitry Andric bool PPCFrameLowering::stackUpdateCanBeMoved(MachineFunction &MF) const {
732*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
733*0b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
734*0b57cec5SDimitry Andric 
735*0b57cec5SDimitry Andric   // Abort if there is no register info or function info.
736*0b57cec5SDimitry Andric   if (!RegInfo || !FI)
737*0b57cec5SDimitry Andric     return false;
738*0b57cec5SDimitry Andric 
739*0b57cec5SDimitry Andric   // Only move the stack update on ELFv2 ABI and PPC64.
740*0b57cec5SDimitry Andric   if (!Subtarget.isELFv2ABI() || !Subtarget.isPPC64())
741*0b57cec5SDimitry Andric     return false;
742*0b57cec5SDimitry Andric 
743*0b57cec5SDimitry Andric   // Check the frame size first and return false if it does not fit the
744*0b57cec5SDimitry Andric   // requirements.
745*0b57cec5SDimitry Andric   // We need a non-zero frame size as well as a frame that will fit in the red
746*0b57cec5SDimitry Andric   // zone. This is because by moving the stack pointer update we are now storing
747*0b57cec5SDimitry Andric   // to the red zone until the stack pointer is updated. If we get an interrupt
748*0b57cec5SDimitry Andric   // inside the prologue but before the stack update we now have a number of
749*0b57cec5SDimitry Andric   // stores to the red zone and those stores must all fit.
750*0b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
751*0b57cec5SDimitry Andric   unsigned FrameSize = MFI.getStackSize();
752*0b57cec5SDimitry Andric   if (!FrameSize || FrameSize > Subtarget.getRedZoneSize())
753*0b57cec5SDimitry Andric     return false;
754*0b57cec5SDimitry Andric 
755*0b57cec5SDimitry Andric   // Frame pointers and base pointers complicate matters so don't do anything
756*0b57cec5SDimitry Andric   // if we have them. For example having a frame pointer will sometimes require
757*0b57cec5SDimitry Andric   // a copy of r1 into r31 and that makes keeping track of updates to r1 more
758*0b57cec5SDimitry Andric   // difficult.
759*0b57cec5SDimitry Andric   if (hasFP(MF) || RegInfo->hasBasePointer(MF))
760*0b57cec5SDimitry Andric     return false;
761*0b57cec5SDimitry Andric 
762*0b57cec5SDimitry Andric   // Calls to fast_cc functions use different rules for passing parameters on
763*0b57cec5SDimitry Andric   // the stack from the ABI and using PIC base in the function imposes
764*0b57cec5SDimitry Andric   // similar restrictions to using the base pointer. It is not generally safe
765*0b57cec5SDimitry Andric   // to move the stack pointer update in these situations.
766*0b57cec5SDimitry Andric   if (FI->hasFastCall() || FI->usesPICBase())
767*0b57cec5SDimitry Andric     return false;
768*0b57cec5SDimitry Andric 
769*0b57cec5SDimitry Andric   // Finally we can move the stack update if we do not require register
770*0b57cec5SDimitry Andric   // scavenging. Register scavenging can introduce more spills and so
771*0b57cec5SDimitry Andric   // may make the frame size larger than we have computed.
772*0b57cec5SDimitry Andric   return !RegInfo->requiresFrameIndexScavenging(MF);
773*0b57cec5SDimitry Andric }
774*0b57cec5SDimitry Andric 
775*0b57cec5SDimitry Andric void PPCFrameLowering::emitPrologue(MachineFunction &MF,
776*0b57cec5SDimitry Andric                                     MachineBasicBlock &MBB) const {
777*0b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.begin();
778*0b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
779*0b57cec5SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
780*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
781*0b57cec5SDimitry Andric 
782*0b57cec5SDimitry Andric   MachineModuleInfo &MMI = MF.getMMI();
783*0b57cec5SDimitry Andric   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
784*0b57cec5SDimitry Andric   DebugLoc dl;
785*0b57cec5SDimitry Andric   bool needsCFI = MMI.hasDebugInfo() ||
786*0b57cec5SDimitry Andric     MF.getFunction().needsUnwindTableEntry();
787*0b57cec5SDimitry Andric 
788*0b57cec5SDimitry Andric   // Get processor type.
789*0b57cec5SDimitry Andric   bool isPPC64 = Subtarget.isPPC64();
790*0b57cec5SDimitry Andric   // Get the ABI.
791*0b57cec5SDimitry Andric   bool isSVR4ABI = Subtarget.isSVR4ABI();
7928bcb0991SDimitry Andric   bool isAIXABI = Subtarget.isAIXABI();
793*0b57cec5SDimitry Andric   bool isELFv2ABI = Subtarget.isELFv2ABI();
7948bcb0991SDimitry Andric   assert((Subtarget.isDarwinABI() || isSVR4ABI || isAIXABI) &&
7958bcb0991SDimitry Andric          "Unsupported PPC ABI.");
796*0b57cec5SDimitry Andric 
797*0b57cec5SDimitry Andric   // Scan the prolog, looking for an UPDATE_VRSAVE instruction.  If we find it,
798*0b57cec5SDimitry Andric   // process it.
799*0b57cec5SDimitry Andric   if (!isSVR4ABI)
800*0b57cec5SDimitry Andric     for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
801*0b57cec5SDimitry Andric       if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
8028bcb0991SDimitry Andric         if (isAIXABI)
8038bcb0991SDimitry Andric           report_fatal_error("UPDATE_VRSAVE is unexpected on AIX.");
804*0b57cec5SDimitry Andric         HandleVRSaveUpdate(*MBBI, TII);
805*0b57cec5SDimitry Andric         break;
806*0b57cec5SDimitry Andric       }
807*0b57cec5SDimitry Andric     }
808*0b57cec5SDimitry Andric 
809*0b57cec5SDimitry Andric   // Move MBBI back to the beginning of the prologue block.
810*0b57cec5SDimitry Andric   MBBI = MBB.begin();
811*0b57cec5SDimitry Andric 
812*0b57cec5SDimitry Andric   // Work out frame sizes.
813*0b57cec5SDimitry Andric   unsigned FrameSize = determineFrameLayoutAndUpdate(MF);
814*0b57cec5SDimitry Andric   int NegFrameSize = -FrameSize;
815*0b57cec5SDimitry Andric   if (!isInt<32>(NegFrameSize))
816*0b57cec5SDimitry Andric     llvm_unreachable("Unhandled stack size!");
817*0b57cec5SDimitry Andric 
818*0b57cec5SDimitry Andric   if (MFI.isFrameAddressTaken())
819*0b57cec5SDimitry Andric     replaceFPWithRealFP(MF);
820*0b57cec5SDimitry Andric 
821*0b57cec5SDimitry Andric   // Check if the link register (LR) must be saved.
822*0b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
823*0b57cec5SDimitry Andric   bool MustSaveLR = FI->mustSaveLR();
824*0b57cec5SDimitry Andric   bool MustSaveTOC = FI->mustSaveTOC();
825*0b57cec5SDimitry Andric   const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
826*0b57cec5SDimitry Andric   bool MustSaveCR = !MustSaveCRs.empty();
827*0b57cec5SDimitry Andric   // Do we have a frame pointer and/or base pointer for this function?
828*0b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
829*0b57cec5SDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
830*0b57cec5SDimitry Andric   bool HasRedZone = isPPC64 || !isSVR4ABI;
831*0b57cec5SDimitry Andric 
832*0b57cec5SDimitry Andric   unsigned SPReg       = isPPC64 ? PPC::X1  : PPC::R1;
8338bcb0991SDimitry Andric   Register BPReg = RegInfo->getBaseRegister(MF);
834*0b57cec5SDimitry Andric   unsigned FPReg       = isPPC64 ? PPC::X31 : PPC::R31;
835*0b57cec5SDimitry Andric   unsigned LRReg       = isPPC64 ? PPC::LR8 : PPC::LR;
836*0b57cec5SDimitry Andric   unsigned TOCReg      = isPPC64 ? PPC::X2 :  PPC::R2;
837*0b57cec5SDimitry Andric   unsigned ScratchReg  = 0;
838*0b57cec5SDimitry Andric   unsigned TempReg     = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
839*0b57cec5SDimitry Andric   //  ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
840*0b57cec5SDimitry Andric   const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
841*0b57cec5SDimitry Andric                                                 : PPC::MFLR );
842*0b57cec5SDimitry Andric   const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
843*0b57cec5SDimitry Andric                                                  : PPC::STW );
844*0b57cec5SDimitry Andric   const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
845*0b57cec5SDimitry Andric                                                      : PPC::STWU );
846*0b57cec5SDimitry Andric   const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
847*0b57cec5SDimitry Andric                                                         : PPC::STWUX);
848*0b57cec5SDimitry Andric   const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
849*0b57cec5SDimitry Andric                                                           : PPC::LIS );
850*0b57cec5SDimitry Andric   const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
851*0b57cec5SDimitry Andric                                                  : PPC::ORI );
852*0b57cec5SDimitry Andric   const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
853*0b57cec5SDimitry Andric                                               : PPC::OR );
854*0b57cec5SDimitry Andric   const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
855*0b57cec5SDimitry Andric                                                             : PPC::SUBFC);
856*0b57cec5SDimitry Andric   const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
857*0b57cec5SDimitry Andric                                                                : PPC::SUBFIC);
858*0b57cec5SDimitry Andric 
859*0b57cec5SDimitry Andric   // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
860*0b57cec5SDimitry Andric   // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
861*0b57cec5SDimitry Andric   // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
862*0b57cec5SDimitry Andric   // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
863*0b57cec5SDimitry Andric   assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
864*0b57cec5SDimitry Andric          "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
865*0b57cec5SDimitry Andric 
866*0b57cec5SDimitry Andric   // Using the same bool variable as below to suppress compiler warnings.
867*0b57cec5SDimitry Andric   bool SingleScratchReg =
868*0b57cec5SDimitry Andric     findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB),
869*0b57cec5SDimitry Andric                         &ScratchReg, &TempReg);
870*0b57cec5SDimitry Andric   assert(SingleScratchReg &&
871*0b57cec5SDimitry Andric          "Required number of registers not available in this block");
872*0b57cec5SDimitry Andric 
873*0b57cec5SDimitry Andric   SingleScratchReg = ScratchReg == TempReg;
874*0b57cec5SDimitry Andric 
875*0b57cec5SDimitry Andric   int LROffset = getReturnSaveOffset();
876*0b57cec5SDimitry Andric 
877*0b57cec5SDimitry Andric   int FPOffset = 0;
878*0b57cec5SDimitry Andric   if (HasFP) {
879*0b57cec5SDimitry Andric     if (isSVR4ABI) {
880*0b57cec5SDimitry Andric       MachineFrameInfo &MFI = MF.getFrameInfo();
881*0b57cec5SDimitry Andric       int FPIndex = FI->getFramePointerSaveIndex();
882*0b57cec5SDimitry Andric       assert(FPIndex && "No Frame Pointer Save Slot!");
883*0b57cec5SDimitry Andric       FPOffset = MFI.getObjectOffset(FPIndex);
884*0b57cec5SDimitry Andric     } else {
885*0b57cec5SDimitry Andric       FPOffset = getFramePointerSaveOffset();
886*0b57cec5SDimitry Andric     }
887*0b57cec5SDimitry Andric   }
888*0b57cec5SDimitry Andric 
889*0b57cec5SDimitry Andric   int BPOffset = 0;
890*0b57cec5SDimitry Andric   if (HasBP) {
891*0b57cec5SDimitry Andric     if (isSVR4ABI) {
892*0b57cec5SDimitry Andric       MachineFrameInfo &MFI = MF.getFrameInfo();
893*0b57cec5SDimitry Andric       int BPIndex = FI->getBasePointerSaveIndex();
894*0b57cec5SDimitry Andric       assert(BPIndex && "No Base Pointer Save Slot!");
895*0b57cec5SDimitry Andric       BPOffset = MFI.getObjectOffset(BPIndex);
896*0b57cec5SDimitry Andric     } else {
897*0b57cec5SDimitry Andric       BPOffset = getBasePointerSaveOffset();
898*0b57cec5SDimitry Andric     }
899*0b57cec5SDimitry Andric   }
900*0b57cec5SDimitry Andric 
901*0b57cec5SDimitry Andric   int PBPOffset = 0;
902*0b57cec5SDimitry Andric   if (FI->usesPICBase()) {
903*0b57cec5SDimitry Andric     MachineFrameInfo &MFI = MF.getFrameInfo();
904*0b57cec5SDimitry Andric     int PBPIndex = FI->getPICBasePointerSaveIndex();
905*0b57cec5SDimitry Andric     assert(PBPIndex && "No PIC Base Pointer Save Slot!");
906*0b57cec5SDimitry Andric     PBPOffset = MFI.getObjectOffset(PBPIndex);
907*0b57cec5SDimitry Andric   }
908*0b57cec5SDimitry Andric 
909*0b57cec5SDimitry Andric   // Get stack alignments.
910*0b57cec5SDimitry Andric   unsigned MaxAlign = MFI.getMaxAlignment();
911*0b57cec5SDimitry Andric   if (HasBP && MaxAlign > 1)
912*0b57cec5SDimitry Andric     assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
913*0b57cec5SDimitry Andric            "Invalid alignment!");
914*0b57cec5SDimitry Andric 
915*0b57cec5SDimitry Andric   // Frames of 32KB & larger require special handling because they cannot be
916*0b57cec5SDimitry Andric   // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
917*0b57cec5SDimitry Andric   bool isLargeFrame = !isInt<16>(NegFrameSize);
918*0b57cec5SDimitry Andric 
919*0b57cec5SDimitry Andric   assert((isPPC64 || !MustSaveCR) &&
920*0b57cec5SDimitry Andric          "Prologue CR saving supported only in 64-bit mode");
921*0b57cec5SDimitry Andric 
9228bcb0991SDimitry Andric   if (MustSaveCR && isAIXABI)
9238bcb0991SDimitry Andric     report_fatal_error("Prologue CR saving is unimplemented on AIX.");
9248bcb0991SDimitry Andric 
925*0b57cec5SDimitry Andric   // Check if we can move the stack update instruction (stdu) down the prologue
926*0b57cec5SDimitry Andric   // past the callee saves. Hopefully this will avoid the situation where the
927*0b57cec5SDimitry Andric   // saves are waiting for the update on the store with update to complete.
928*0b57cec5SDimitry Andric   MachineBasicBlock::iterator StackUpdateLoc = MBBI;
929*0b57cec5SDimitry Andric   bool MovingStackUpdateDown = false;
930*0b57cec5SDimitry Andric 
931*0b57cec5SDimitry Andric   // Check if we can move the stack update.
932*0b57cec5SDimitry Andric   if (stackUpdateCanBeMoved(MF)) {
933*0b57cec5SDimitry Andric     const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
934*0b57cec5SDimitry Andric     for (CalleeSavedInfo CSI : Info) {
935*0b57cec5SDimitry Andric       int FrIdx = CSI.getFrameIdx();
936*0b57cec5SDimitry Andric       // If the frame index is not negative the callee saved info belongs to a
937*0b57cec5SDimitry Andric       // stack object that is not a fixed stack object. We ignore non-fixed
938*0b57cec5SDimitry Andric       // stack objects because we won't move the stack update pointer past them.
939*0b57cec5SDimitry Andric       if (FrIdx >= 0)
940*0b57cec5SDimitry Andric         continue;
941*0b57cec5SDimitry Andric 
942*0b57cec5SDimitry Andric       if (MFI.isFixedObjectIndex(FrIdx) && MFI.getObjectOffset(FrIdx) < 0) {
943*0b57cec5SDimitry Andric         StackUpdateLoc++;
944*0b57cec5SDimitry Andric         MovingStackUpdateDown = true;
945*0b57cec5SDimitry Andric       } else {
946*0b57cec5SDimitry Andric         // We need all of the Frame Indices to meet these conditions.
947*0b57cec5SDimitry Andric         // If they do not, abort the whole operation.
948*0b57cec5SDimitry Andric         StackUpdateLoc = MBBI;
949*0b57cec5SDimitry Andric         MovingStackUpdateDown = false;
950*0b57cec5SDimitry Andric         break;
951*0b57cec5SDimitry Andric       }
952*0b57cec5SDimitry Andric     }
953*0b57cec5SDimitry Andric 
954*0b57cec5SDimitry Andric     // If the operation was not aborted then update the object offset.
955*0b57cec5SDimitry Andric     if (MovingStackUpdateDown) {
956*0b57cec5SDimitry Andric       for (CalleeSavedInfo CSI : Info) {
957*0b57cec5SDimitry Andric         int FrIdx = CSI.getFrameIdx();
958*0b57cec5SDimitry Andric         if (FrIdx < 0)
959*0b57cec5SDimitry Andric           MFI.setObjectOffset(FrIdx, MFI.getObjectOffset(FrIdx) + NegFrameSize);
960*0b57cec5SDimitry Andric       }
961*0b57cec5SDimitry Andric     }
962*0b57cec5SDimitry Andric   }
963*0b57cec5SDimitry Andric 
964*0b57cec5SDimitry Andric   // If we need to spill the CR and the LR but we don't have two separate
965*0b57cec5SDimitry Andric   // registers available, we must spill them one at a time
966*0b57cec5SDimitry Andric   if (MustSaveCR && SingleScratchReg && MustSaveLR) {
967*0b57cec5SDimitry Andric     // In the ELFv2 ABI, we are not required to save all CR fields.
968*0b57cec5SDimitry Andric     // If only one or two CR fields are clobbered, it is more efficient to use
969*0b57cec5SDimitry Andric     // mfocrf to selectively save just those fields, because mfocrf has short
970*0b57cec5SDimitry Andric     // latency compares to mfcr.
971*0b57cec5SDimitry Andric     unsigned MfcrOpcode = PPC::MFCR8;
972*0b57cec5SDimitry Andric     unsigned CrState = RegState::ImplicitKill;
973*0b57cec5SDimitry Andric     if (isELFv2ABI && MustSaveCRs.size() == 1) {
974*0b57cec5SDimitry Andric       MfcrOpcode = PPC::MFOCRF8;
975*0b57cec5SDimitry Andric       CrState = RegState::Kill;
976*0b57cec5SDimitry Andric     }
977*0b57cec5SDimitry Andric     MachineInstrBuilder MIB =
978*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
979*0b57cec5SDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
980*0b57cec5SDimitry Andric       MIB.addReg(MustSaveCRs[i], CrState);
981*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
982*0b57cec5SDimitry Andric       .addReg(TempReg, getKillRegState(true))
9838bcb0991SDimitry Andric       .addImm(getCRSaveOffset())
984*0b57cec5SDimitry Andric       .addReg(SPReg);
985*0b57cec5SDimitry Andric   }
986*0b57cec5SDimitry Andric 
987*0b57cec5SDimitry Andric   if (MustSaveLR)
988*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
989*0b57cec5SDimitry Andric 
990*0b57cec5SDimitry Andric   if (MustSaveCR &&
991*0b57cec5SDimitry Andric       !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
992*0b57cec5SDimitry Andric     // In the ELFv2 ABI, we are not required to save all CR fields.
993*0b57cec5SDimitry Andric     // If only one or two CR fields are clobbered, it is more efficient to use
994*0b57cec5SDimitry Andric     // mfocrf to selectively save just those fields, because mfocrf has short
995*0b57cec5SDimitry Andric     // latency compares to mfcr.
996*0b57cec5SDimitry Andric     unsigned MfcrOpcode = PPC::MFCR8;
997*0b57cec5SDimitry Andric     unsigned CrState = RegState::ImplicitKill;
998*0b57cec5SDimitry Andric     if (isELFv2ABI && MustSaveCRs.size() == 1) {
999*0b57cec5SDimitry Andric       MfcrOpcode = PPC::MFOCRF8;
1000*0b57cec5SDimitry Andric       CrState = RegState::Kill;
1001*0b57cec5SDimitry Andric     }
1002*0b57cec5SDimitry Andric     MachineInstrBuilder MIB =
1003*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
1004*0b57cec5SDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1005*0b57cec5SDimitry Andric       MIB.addReg(MustSaveCRs[i], CrState);
1006*0b57cec5SDimitry Andric   }
1007*0b57cec5SDimitry Andric 
1008*0b57cec5SDimitry Andric   if (HasRedZone) {
1009*0b57cec5SDimitry Andric     if (HasFP)
1010*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, StoreInst)
1011*0b57cec5SDimitry Andric         .addReg(FPReg)
1012*0b57cec5SDimitry Andric         .addImm(FPOffset)
1013*0b57cec5SDimitry Andric         .addReg(SPReg);
1014*0b57cec5SDimitry Andric     if (FI->usesPICBase())
1015*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, StoreInst)
1016*0b57cec5SDimitry Andric         .addReg(PPC::R30)
1017*0b57cec5SDimitry Andric         .addImm(PBPOffset)
1018*0b57cec5SDimitry Andric         .addReg(SPReg);
1019*0b57cec5SDimitry Andric     if (HasBP)
1020*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, StoreInst)
1021*0b57cec5SDimitry Andric         .addReg(BPReg)
1022*0b57cec5SDimitry Andric         .addImm(BPOffset)
1023*0b57cec5SDimitry Andric         .addReg(SPReg);
1024*0b57cec5SDimitry Andric   }
1025*0b57cec5SDimitry Andric 
1026*0b57cec5SDimitry Andric   if (MustSaveLR)
1027*0b57cec5SDimitry Andric     BuildMI(MBB, StackUpdateLoc, dl, StoreInst)
1028*0b57cec5SDimitry Andric       .addReg(ScratchReg, getKillRegState(true))
1029*0b57cec5SDimitry Andric       .addImm(LROffset)
1030*0b57cec5SDimitry Andric       .addReg(SPReg);
1031*0b57cec5SDimitry Andric 
1032*0b57cec5SDimitry Andric   if (MustSaveCR &&
1033*0b57cec5SDimitry Andric       !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
1034*0b57cec5SDimitry Andric     assert(HasRedZone && "A red zone is always available on PPC64");
1035*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
1036*0b57cec5SDimitry Andric       .addReg(TempReg, getKillRegState(true))
10378bcb0991SDimitry Andric       .addImm(getCRSaveOffset())
1038*0b57cec5SDimitry Andric       .addReg(SPReg);
1039*0b57cec5SDimitry Andric   }
1040*0b57cec5SDimitry Andric 
1041*0b57cec5SDimitry Andric   // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
1042*0b57cec5SDimitry Andric   if (!FrameSize)
1043*0b57cec5SDimitry Andric     return;
1044*0b57cec5SDimitry Andric 
1045*0b57cec5SDimitry Andric   // Adjust stack pointer: r1 += NegFrameSize.
1046*0b57cec5SDimitry Andric   // If there is a preferred stack alignment, align R1 now
1047*0b57cec5SDimitry Andric 
1048*0b57cec5SDimitry Andric   if (HasBP && HasRedZone) {
1049*0b57cec5SDimitry Andric     // Save a copy of r1 as the base pointer.
1050*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, OrInst, BPReg)
1051*0b57cec5SDimitry Andric       .addReg(SPReg)
1052*0b57cec5SDimitry Andric       .addReg(SPReg);
1053*0b57cec5SDimitry Andric   }
1054*0b57cec5SDimitry Andric 
1055*0b57cec5SDimitry Andric   // Have we generated a STUX instruction to claim stack frame? If so,
1056*0b57cec5SDimitry Andric   // the negated frame size will be placed in ScratchReg.
1057*0b57cec5SDimitry Andric   bool HasSTUX = false;
1058*0b57cec5SDimitry Andric 
1059*0b57cec5SDimitry Andric   // This condition must be kept in sync with canUseAsPrologue.
1060*0b57cec5SDimitry Andric   if (HasBP && MaxAlign > 1) {
1061*0b57cec5SDimitry Andric     if (isPPC64)
1062*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
1063*0b57cec5SDimitry Andric         .addReg(SPReg)
1064*0b57cec5SDimitry Andric         .addImm(0)
1065*0b57cec5SDimitry Andric         .addImm(64 - Log2_32(MaxAlign));
1066*0b57cec5SDimitry Andric     else // PPC32...
1067*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
1068*0b57cec5SDimitry Andric         .addReg(SPReg)
1069*0b57cec5SDimitry Andric         .addImm(0)
1070*0b57cec5SDimitry Andric         .addImm(32 - Log2_32(MaxAlign))
1071*0b57cec5SDimitry Andric         .addImm(31);
1072*0b57cec5SDimitry Andric     if (!isLargeFrame) {
1073*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
1074*0b57cec5SDimitry Andric         .addReg(ScratchReg, RegState::Kill)
1075*0b57cec5SDimitry Andric         .addImm(NegFrameSize);
1076*0b57cec5SDimitry Andric     } else {
1077*0b57cec5SDimitry Andric       assert(!SingleScratchReg && "Only a single scratch reg available");
1078*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
1079*0b57cec5SDimitry Andric         .addImm(NegFrameSize >> 16);
1080*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
1081*0b57cec5SDimitry Andric         .addReg(TempReg, RegState::Kill)
1082*0b57cec5SDimitry Andric         .addImm(NegFrameSize & 0xFFFF);
1083*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
1084*0b57cec5SDimitry Andric         .addReg(ScratchReg, RegState::Kill)
1085*0b57cec5SDimitry Andric         .addReg(TempReg, RegState::Kill);
1086*0b57cec5SDimitry Andric     }
1087*0b57cec5SDimitry Andric 
1088*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
1089*0b57cec5SDimitry Andric       .addReg(SPReg, RegState::Kill)
1090*0b57cec5SDimitry Andric       .addReg(SPReg)
1091*0b57cec5SDimitry Andric       .addReg(ScratchReg);
1092*0b57cec5SDimitry Andric     HasSTUX = true;
1093*0b57cec5SDimitry Andric 
1094*0b57cec5SDimitry Andric   } else if (!isLargeFrame) {
1095*0b57cec5SDimitry Andric     BuildMI(MBB, StackUpdateLoc, dl, StoreUpdtInst, SPReg)
1096*0b57cec5SDimitry Andric       .addReg(SPReg)
1097*0b57cec5SDimitry Andric       .addImm(NegFrameSize)
1098*0b57cec5SDimitry Andric       .addReg(SPReg);
1099*0b57cec5SDimitry Andric 
1100*0b57cec5SDimitry Andric   } else {
1101*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1102*0b57cec5SDimitry Andric       .addImm(NegFrameSize >> 16);
1103*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1104*0b57cec5SDimitry Andric       .addReg(ScratchReg, RegState::Kill)
1105*0b57cec5SDimitry Andric       .addImm(NegFrameSize & 0xFFFF);
1106*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
1107*0b57cec5SDimitry Andric       .addReg(SPReg, RegState::Kill)
1108*0b57cec5SDimitry Andric       .addReg(SPReg)
1109*0b57cec5SDimitry Andric       .addReg(ScratchReg);
1110*0b57cec5SDimitry Andric     HasSTUX = true;
1111*0b57cec5SDimitry Andric   }
1112*0b57cec5SDimitry Andric 
1113*0b57cec5SDimitry Andric   // Save the TOC register after the stack pointer update if a prologue TOC
1114*0b57cec5SDimitry Andric   // save is required for the function.
1115*0b57cec5SDimitry Andric   if (MustSaveTOC) {
1116*0b57cec5SDimitry Andric     assert(isELFv2ABI && "TOC saves in the prologue only supported on ELFv2");
1117*0b57cec5SDimitry Andric     BuildMI(MBB, StackUpdateLoc, dl, TII.get(PPC::STD))
1118*0b57cec5SDimitry Andric       .addReg(TOCReg, getKillRegState(true))
1119*0b57cec5SDimitry Andric       .addImm(TOCSaveOffset)
1120*0b57cec5SDimitry Andric       .addReg(SPReg);
1121*0b57cec5SDimitry Andric   }
1122*0b57cec5SDimitry Andric 
1123*0b57cec5SDimitry Andric   if (!HasRedZone) {
1124*0b57cec5SDimitry Andric     assert(!isPPC64 && "A red zone is always available on PPC64");
1125*0b57cec5SDimitry Andric     if (HasSTUX) {
1126*0b57cec5SDimitry Andric       // The negated frame size is in ScratchReg, and the SPReg has been
1127*0b57cec5SDimitry Andric       // decremented by the frame size: SPReg = old SPReg + ScratchReg.
1128*0b57cec5SDimitry Andric       // Since FPOffset, PBPOffset, etc. are relative to the beginning of
1129*0b57cec5SDimitry Andric       // the stack frame (i.e. the old SP), ideally, we would put the old
1130*0b57cec5SDimitry Andric       // SP into a register and use it as the base for the stores. The
1131*0b57cec5SDimitry Andric       // problem is that the only available register may be ScratchReg,
1132*0b57cec5SDimitry Andric       // which could be R0, and R0 cannot be used as a base address.
1133*0b57cec5SDimitry Andric 
1134*0b57cec5SDimitry Andric       // First, set ScratchReg to the old SP. This may need to be modified
1135*0b57cec5SDimitry Andric       // later.
1136*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg)
1137*0b57cec5SDimitry Andric         .addReg(ScratchReg, RegState::Kill)
1138*0b57cec5SDimitry Andric         .addReg(SPReg);
1139*0b57cec5SDimitry Andric 
1140*0b57cec5SDimitry Andric       if (ScratchReg == PPC::R0) {
1141*0b57cec5SDimitry Andric         // R0 cannot be used as a base register, but it can be used as an
1142*0b57cec5SDimitry Andric         // index in a store-indexed.
1143*0b57cec5SDimitry Andric         int LastOffset = 0;
1144*0b57cec5SDimitry Andric         if (HasFP)  {
1145*0b57cec5SDimitry Andric           // R0 += (FPOffset-LastOffset).
1146*0b57cec5SDimitry Andric           // Need addic, since addi treats R0 as 0.
1147*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1148*0b57cec5SDimitry Andric             .addReg(ScratchReg)
1149*0b57cec5SDimitry Andric             .addImm(FPOffset-LastOffset);
1150*0b57cec5SDimitry Andric           LastOffset = FPOffset;
1151*0b57cec5SDimitry Andric           // Store FP into *R0.
1152*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1153*0b57cec5SDimitry Andric             .addReg(FPReg, RegState::Kill)  // Save FP.
1154*0b57cec5SDimitry Andric             .addReg(PPC::ZERO)
1155*0b57cec5SDimitry Andric             .addReg(ScratchReg);  // This will be the index (R0 is ok here).
1156*0b57cec5SDimitry Andric         }
1157*0b57cec5SDimitry Andric         if (FI->usesPICBase()) {
1158*0b57cec5SDimitry Andric           // R0 += (PBPOffset-LastOffset).
1159*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1160*0b57cec5SDimitry Andric             .addReg(ScratchReg)
1161*0b57cec5SDimitry Andric             .addImm(PBPOffset-LastOffset);
1162*0b57cec5SDimitry Andric           LastOffset = PBPOffset;
1163*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1164*0b57cec5SDimitry Andric             .addReg(PPC::R30, RegState::Kill)  // Save PIC base pointer.
1165*0b57cec5SDimitry Andric             .addReg(PPC::ZERO)
1166*0b57cec5SDimitry Andric             .addReg(ScratchReg);  // This will be the index (R0 is ok here).
1167*0b57cec5SDimitry Andric         }
1168*0b57cec5SDimitry Andric         if (HasBP) {
1169*0b57cec5SDimitry Andric           // R0 += (BPOffset-LastOffset).
1170*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg)
1171*0b57cec5SDimitry Andric             .addReg(ScratchReg)
1172*0b57cec5SDimitry Andric             .addImm(BPOffset-LastOffset);
1173*0b57cec5SDimitry Andric           LastOffset = BPOffset;
1174*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX))
1175*0b57cec5SDimitry Andric             .addReg(BPReg, RegState::Kill)  // Save BP.
1176*0b57cec5SDimitry Andric             .addReg(PPC::ZERO)
1177*0b57cec5SDimitry Andric             .addReg(ScratchReg);  // This will be the index (R0 is ok here).
1178*0b57cec5SDimitry Andric           // BP = R0-LastOffset
1179*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), BPReg)
1180*0b57cec5SDimitry Andric             .addReg(ScratchReg, RegState::Kill)
1181*0b57cec5SDimitry Andric             .addImm(-LastOffset);
1182*0b57cec5SDimitry Andric         }
1183*0b57cec5SDimitry Andric       } else {
1184*0b57cec5SDimitry Andric         // ScratchReg is not R0, so use it as the base register. It is
1185*0b57cec5SDimitry Andric         // already set to the old SP, so we can use the offsets directly.
1186*0b57cec5SDimitry Andric 
1187*0b57cec5SDimitry Andric         // Now that the stack frame has been allocated, save all the necessary
1188*0b57cec5SDimitry Andric         // registers using ScratchReg as the base address.
1189*0b57cec5SDimitry Andric         if (HasFP)
1190*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, StoreInst)
1191*0b57cec5SDimitry Andric             .addReg(FPReg)
1192*0b57cec5SDimitry Andric             .addImm(FPOffset)
1193*0b57cec5SDimitry Andric             .addReg(ScratchReg);
1194*0b57cec5SDimitry Andric         if (FI->usesPICBase())
1195*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, StoreInst)
1196*0b57cec5SDimitry Andric             .addReg(PPC::R30)
1197*0b57cec5SDimitry Andric             .addImm(PBPOffset)
1198*0b57cec5SDimitry Andric             .addReg(ScratchReg);
1199*0b57cec5SDimitry Andric         if (HasBP) {
1200*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, StoreInst)
1201*0b57cec5SDimitry Andric             .addReg(BPReg)
1202*0b57cec5SDimitry Andric             .addImm(BPOffset)
1203*0b57cec5SDimitry Andric             .addReg(ScratchReg);
1204*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, OrInst, BPReg)
1205*0b57cec5SDimitry Andric             .addReg(ScratchReg, RegState::Kill)
1206*0b57cec5SDimitry Andric             .addReg(ScratchReg);
1207*0b57cec5SDimitry Andric         }
1208*0b57cec5SDimitry Andric       }
1209*0b57cec5SDimitry Andric     } else {
1210*0b57cec5SDimitry Andric       // The frame size is a known 16-bit constant (fitting in the immediate
1211*0b57cec5SDimitry Andric       // field of STWU). To be here we have to be compiling for PPC32.
1212*0b57cec5SDimitry Andric       // Since the SPReg has been decreased by FrameSize, add it back to each
1213*0b57cec5SDimitry Andric       // offset.
1214*0b57cec5SDimitry Andric       if (HasFP)
1215*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, StoreInst)
1216*0b57cec5SDimitry Andric           .addReg(FPReg)
1217*0b57cec5SDimitry Andric           .addImm(FrameSize + FPOffset)
1218*0b57cec5SDimitry Andric           .addReg(SPReg);
1219*0b57cec5SDimitry Andric       if (FI->usesPICBase())
1220*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, StoreInst)
1221*0b57cec5SDimitry Andric           .addReg(PPC::R30)
1222*0b57cec5SDimitry Andric           .addImm(FrameSize + PBPOffset)
1223*0b57cec5SDimitry Andric           .addReg(SPReg);
1224*0b57cec5SDimitry Andric       if (HasBP) {
1225*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, StoreInst)
1226*0b57cec5SDimitry Andric           .addReg(BPReg)
1227*0b57cec5SDimitry Andric           .addImm(FrameSize + BPOffset)
1228*0b57cec5SDimitry Andric           .addReg(SPReg);
1229*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg)
1230*0b57cec5SDimitry Andric           .addReg(SPReg)
1231*0b57cec5SDimitry Andric           .addImm(FrameSize);
1232*0b57cec5SDimitry Andric       }
1233*0b57cec5SDimitry Andric     }
1234*0b57cec5SDimitry Andric   }
1235*0b57cec5SDimitry Andric 
1236*0b57cec5SDimitry Andric   // Add Call Frame Information for the instructions we generated above.
1237*0b57cec5SDimitry Andric   if (needsCFI) {
1238*0b57cec5SDimitry Andric     unsigned CFIIndex;
1239*0b57cec5SDimitry Andric 
1240*0b57cec5SDimitry Andric     if (HasBP) {
1241*0b57cec5SDimitry Andric       // Define CFA in terms of BP. Do this in preference to using FP/SP,
1242*0b57cec5SDimitry Andric       // because if the stack needed aligning then CFA won't be at a fixed
1243*0b57cec5SDimitry Andric       // offset from FP/SP.
1244*0b57cec5SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1245*0b57cec5SDimitry Andric       CFIIndex = MF.addFrameInst(
1246*0b57cec5SDimitry Andric           MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1247*0b57cec5SDimitry Andric     } else {
1248*0b57cec5SDimitry Andric       // Adjust the definition of CFA to account for the change in SP.
1249*0b57cec5SDimitry Andric       assert(NegFrameSize);
1250*0b57cec5SDimitry Andric       CFIIndex = MF.addFrameInst(
1251*0b57cec5SDimitry Andric           MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
1252*0b57cec5SDimitry Andric     }
1253*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1254*0b57cec5SDimitry Andric         .addCFIIndex(CFIIndex);
1255*0b57cec5SDimitry Andric 
1256*0b57cec5SDimitry Andric     if (HasFP) {
1257*0b57cec5SDimitry Andric       // Describe where FP was saved, at a fixed offset from CFA.
1258*0b57cec5SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1259*0b57cec5SDimitry Andric       CFIIndex = MF.addFrameInst(
1260*0b57cec5SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
1261*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1262*0b57cec5SDimitry Andric           .addCFIIndex(CFIIndex);
1263*0b57cec5SDimitry Andric     }
1264*0b57cec5SDimitry Andric 
1265*0b57cec5SDimitry Andric     if (FI->usesPICBase()) {
1266*0b57cec5SDimitry Andric       // Describe where FP was saved, at a fixed offset from CFA.
1267*0b57cec5SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
1268*0b57cec5SDimitry Andric       CFIIndex = MF.addFrameInst(
1269*0b57cec5SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
1270*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1271*0b57cec5SDimitry Andric           .addCFIIndex(CFIIndex);
1272*0b57cec5SDimitry Andric     }
1273*0b57cec5SDimitry Andric 
1274*0b57cec5SDimitry Andric     if (HasBP) {
1275*0b57cec5SDimitry Andric       // Describe where BP was saved, at a fixed offset from CFA.
1276*0b57cec5SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1277*0b57cec5SDimitry Andric       CFIIndex = MF.addFrameInst(
1278*0b57cec5SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
1279*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1280*0b57cec5SDimitry Andric           .addCFIIndex(CFIIndex);
1281*0b57cec5SDimitry Andric     }
1282*0b57cec5SDimitry Andric 
1283*0b57cec5SDimitry Andric     if (MustSaveLR) {
1284*0b57cec5SDimitry Andric       // Describe where LR was saved, at a fixed offset from CFA.
1285*0b57cec5SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
1286*0b57cec5SDimitry Andric       CFIIndex = MF.addFrameInst(
1287*0b57cec5SDimitry Andric           MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
1288*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1289*0b57cec5SDimitry Andric           .addCFIIndex(CFIIndex);
1290*0b57cec5SDimitry Andric     }
1291*0b57cec5SDimitry Andric   }
1292*0b57cec5SDimitry Andric 
1293*0b57cec5SDimitry Andric   // If there is a frame pointer, copy R1 into R31
1294*0b57cec5SDimitry Andric   if (HasFP) {
1295*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1296*0b57cec5SDimitry Andric       .addReg(SPReg)
1297*0b57cec5SDimitry Andric       .addReg(SPReg);
1298*0b57cec5SDimitry Andric 
1299*0b57cec5SDimitry Andric     if (!HasBP && needsCFI) {
1300*0b57cec5SDimitry Andric       // Change the definition of CFA from SP+offset to FP+offset, because SP
1301*0b57cec5SDimitry Andric       // will change at every alloca.
1302*0b57cec5SDimitry Andric       unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1303*0b57cec5SDimitry Andric       unsigned CFIIndex = MF.addFrameInst(
1304*0b57cec5SDimitry Andric           MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1305*0b57cec5SDimitry Andric 
1306*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1307*0b57cec5SDimitry Andric           .addCFIIndex(CFIIndex);
1308*0b57cec5SDimitry Andric     }
1309*0b57cec5SDimitry Andric   }
1310*0b57cec5SDimitry Andric 
1311*0b57cec5SDimitry Andric   if (needsCFI) {
1312*0b57cec5SDimitry Andric     // Describe where callee saved registers were saved, at fixed offsets from
1313*0b57cec5SDimitry Andric     // CFA.
1314*0b57cec5SDimitry Andric     const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1315*0b57cec5SDimitry Andric     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
1316*0b57cec5SDimitry Andric       unsigned Reg = CSI[I].getReg();
1317*0b57cec5SDimitry Andric       if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
1318*0b57cec5SDimitry Andric 
1319*0b57cec5SDimitry Andric       // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1320*0b57cec5SDimitry Andric       // subregisters of CR2. We just need to emit a move of CR2.
1321*0b57cec5SDimitry Andric       if (PPC::CRBITRCRegClass.contains(Reg))
1322*0b57cec5SDimitry Andric         continue;
1323*0b57cec5SDimitry Andric 
1324*0b57cec5SDimitry Andric       if ((Reg == PPC::X2 || Reg == PPC::R2) && MustSaveTOC)
1325*0b57cec5SDimitry Andric         continue;
1326*0b57cec5SDimitry Andric 
1327*0b57cec5SDimitry Andric       // For SVR4, don't emit a move for the CR spill slot if we haven't
1328*0b57cec5SDimitry Andric       // spilled CRs.
1329*0b57cec5SDimitry Andric       if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
1330*0b57cec5SDimitry Andric           && !MustSaveCR)
1331*0b57cec5SDimitry Andric         continue;
1332*0b57cec5SDimitry Andric 
1333*0b57cec5SDimitry Andric       // For 64-bit SVR4 when we have spilled CRs, the spill location
1334*0b57cec5SDimitry Andric       // is SP+8, not a frame-relative slot.
1335*0b57cec5SDimitry Andric       if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
1336*0b57cec5SDimitry Andric         // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
1337*0b57cec5SDimitry Andric         // the whole CR word.  In the ELFv2 ABI, every CR that was
1338*0b57cec5SDimitry Andric         // actually saved gets its own CFI record.
1339*0b57cec5SDimitry Andric         unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
1340*0b57cec5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
13418bcb0991SDimitry Andric             nullptr, MRI->getDwarfRegNum(CRReg, true), getCRSaveOffset()));
1342*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1343*0b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
1344*0b57cec5SDimitry Andric         continue;
1345*0b57cec5SDimitry Andric       }
1346*0b57cec5SDimitry Andric 
1347*0b57cec5SDimitry Andric       if (CSI[I].isSpilledToReg()) {
1348*0b57cec5SDimitry Andric         unsigned SpilledReg = CSI[I].getDstReg();
1349*0b57cec5SDimitry Andric         unsigned CFIRegister = MF.addFrameInst(MCCFIInstruction::createRegister(
1350*0b57cec5SDimitry Andric             nullptr, MRI->getDwarfRegNum(Reg, true),
1351*0b57cec5SDimitry Andric             MRI->getDwarfRegNum(SpilledReg, true)));
1352*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1353*0b57cec5SDimitry Andric           .addCFIIndex(CFIRegister);
1354*0b57cec5SDimitry Andric       } else {
1355*0b57cec5SDimitry Andric         int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx());
1356*0b57cec5SDimitry Andric         // We have changed the object offset above but we do not want to change
1357*0b57cec5SDimitry Andric         // the actual offsets in the CFI instruction so we have to undo the
1358*0b57cec5SDimitry Andric         // offset change here.
1359*0b57cec5SDimitry Andric         if (MovingStackUpdateDown)
1360*0b57cec5SDimitry Andric           Offset -= NegFrameSize;
1361*0b57cec5SDimitry Andric 
1362*0b57cec5SDimitry Andric         unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
1363*0b57cec5SDimitry Andric             nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
1364*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1365*0b57cec5SDimitry Andric             .addCFIIndex(CFIIndex);
1366*0b57cec5SDimitry Andric       }
1367*0b57cec5SDimitry Andric     }
1368*0b57cec5SDimitry Andric   }
1369*0b57cec5SDimitry Andric }
1370*0b57cec5SDimitry Andric 
1371*0b57cec5SDimitry Andric void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
1372*0b57cec5SDimitry Andric                                     MachineBasicBlock &MBB) const {
1373*0b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1374*0b57cec5SDimitry Andric   DebugLoc dl;
1375*0b57cec5SDimitry Andric 
1376*0b57cec5SDimitry Andric   if (MBBI != MBB.end())
1377*0b57cec5SDimitry Andric     dl = MBBI->getDebugLoc();
1378*0b57cec5SDimitry Andric 
1379*0b57cec5SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1380*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1381*0b57cec5SDimitry Andric 
1382*0b57cec5SDimitry Andric   // Get alignment info so we know how to restore the SP.
1383*0b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
1384*0b57cec5SDimitry Andric 
1385*0b57cec5SDimitry Andric   // Get the number of bytes allocated from the FrameInfo.
1386*0b57cec5SDimitry Andric   int FrameSize = MFI.getStackSize();
1387*0b57cec5SDimitry Andric 
1388*0b57cec5SDimitry Andric   // Get processor type.
1389*0b57cec5SDimitry Andric   bool isPPC64 = Subtarget.isPPC64();
1390*0b57cec5SDimitry Andric   // Get the ABI.
1391*0b57cec5SDimitry Andric   bool isSVR4ABI = Subtarget.isSVR4ABI();
1392*0b57cec5SDimitry Andric 
1393*0b57cec5SDimitry Andric   // Check if the link register (LR) has been saved.
1394*0b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1395*0b57cec5SDimitry Andric   bool MustSaveLR = FI->mustSaveLR();
1396*0b57cec5SDimitry Andric   const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
1397*0b57cec5SDimitry Andric   bool MustSaveCR = !MustSaveCRs.empty();
1398*0b57cec5SDimitry Andric   // Do we have a frame pointer and/or base pointer for this function?
1399*0b57cec5SDimitry Andric   bool HasFP = hasFP(MF);
1400*0b57cec5SDimitry Andric   bool HasBP = RegInfo->hasBasePointer(MF);
1401*0b57cec5SDimitry Andric   bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI();
1402*0b57cec5SDimitry Andric 
1403*0b57cec5SDimitry Andric   unsigned SPReg      = isPPC64 ? PPC::X1  : PPC::R1;
14048bcb0991SDimitry Andric   Register BPReg = RegInfo->getBaseRegister(MF);
1405*0b57cec5SDimitry Andric   unsigned FPReg      = isPPC64 ? PPC::X31 : PPC::R31;
1406*0b57cec5SDimitry Andric   unsigned ScratchReg = 0;
1407*0b57cec5SDimitry Andric   unsigned TempReg     = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1408*0b57cec5SDimitry Andric   const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
1409*0b57cec5SDimitry Andric                                                  : PPC::MTLR );
1410*0b57cec5SDimitry Andric   const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
1411*0b57cec5SDimitry Andric                                                  : PPC::LWZ );
1412*0b57cec5SDimitry Andric   const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
1413*0b57cec5SDimitry Andric                                                            : PPC::LIS );
1414*0b57cec5SDimitry Andric   const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
1415*0b57cec5SDimitry Andric                                               : PPC::OR );
1416*0b57cec5SDimitry Andric   const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
1417*0b57cec5SDimitry Andric                                                   : PPC::ORI );
1418*0b57cec5SDimitry Andric   const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
1419*0b57cec5SDimitry Andric                                                    : PPC::ADDI );
1420*0b57cec5SDimitry Andric   const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
1421*0b57cec5SDimitry Andric                                                 : PPC::ADD4 );
1422*0b57cec5SDimitry Andric 
1423*0b57cec5SDimitry Andric   int LROffset = getReturnSaveOffset();
1424*0b57cec5SDimitry Andric 
1425*0b57cec5SDimitry Andric   int FPOffset = 0;
1426*0b57cec5SDimitry Andric 
1427*0b57cec5SDimitry Andric   // Using the same bool variable as below to suppress compiler warnings.
1428*0b57cec5SDimitry Andric   bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg,
1429*0b57cec5SDimitry Andric                                               &TempReg);
1430*0b57cec5SDimitry Andric   assert(SingleScratchReg &&
1431*0b57cec5SDimitry Andric          "Could not find an available scratch register");
1432*0b57cec5SDimitry Andric 
1433*0b57cec5SDimitry Andric   SingleScratchReg = ScratchReg == TempReg;
1434*0b57cec5SDimitry Andric 
1435*0b57cec5SDimitry Andric   if (HasFP) {
1436*0b57cec5SDimitry Andric     if (isSVR4ABI) {
1437*0b57cec5SDimitry Andric       int FPIndex = FI->getFramePointerSaveIndex();
1438*0b57cec5SDimitry Andric       assert(FPIndex && "No Frame Pointer Save Slot!");
1439*0b57cec5SDimitry Andric       FPOffset = MFI.getObjectOffset(FPIndex);
1440*0b57cec5SDimitry Andric     } else {
1441*0b57cec5SDimitry Andric       FPOffset = getFramePointerSaveOffset();
1442*0b57cec5SDimitry Andric     }
1443*0b57cec5SDimitry Andric   }
1444*0b57cec5SDimitry Andric 
1445*0b57cec5SDimitry Andric   int BPOffset = 0;
1446*0b57cec5SDimitry Andric   if (HasBP) {
1447*0b57cec5SDimitry Andric     if (isSVR4ABI) {
1448*0b57cec5SDimitry Andric       int BPIndex = FI->getBasePointerSaveIndex();
1449*0b57cec5SDimitry Andric       assert(BPIndex && "No Base Pointer Save Slot!");
1450*0b57cec5SDimitry Andric       BPOffset = MFI.getObjectOffset(BPIndex);
1451*0b57cec5SDimitry Andric     } else {
1452*0b57cec5SDimitry Andric       BPOffset = getBasePointerSaveOffset();
1453*0b57cec5SDimitry Andric     }
1454*0b57cec5SDimitry Andric   }
1455*0b57cec5SDimitry Andric 
1456*0b57cec5SDimitry Andric   int PBPOffset = 0;
1457*0b57cec5SDimitry Andric   if (FI->usesPICBase()) {
1458*0b57cec5SDimitry Andric     int PBPIndex = FI->getPICBasePointerSaveIndex();
1459*0b57cec5SDimitry Andric     assert(PBPIndex && "No PIC Base Pointer Save Slot!");
1460*0b57cec5SDimitry Andric     PBPOffset = MFI.getObjectOffset(PBPIndex);
1461*0b57cec5SDimitry Andric   }
1462*0b57cec5SDimitry Andric 
1463*0b57cec5SDimitry Andric   bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
1464*0b57cec5SDimitry Andric 
1465*0b57cec5SDimitry Andric   if (IsReturnBlock) {
1466*0b57cec5SDimitry Andric     unsigned RetOpcode = MBBI->getOpcode();
1467*0b57cec5SDimitry Andric     bool UsesTCRet =  RetOpcode == PPC::TCRETURNri ||
1468*0b57cec5SDimitry Andric                       RetOpcode == PPC::TCRETURNdi ||
1469*0b57cec5SDimitry Andric                       RetOpcode == PPC::TCRETURNai ||
1470*0b57cec5SDimitry Andric                       RetOpcode == PPC::TCRETURNri8 ||
1471*0b57cec5SDimitry Andric                       RetOpcode == PPC::TCRETURNdi8 ||
1472*0b57cec5SDimitry Andric                       RetOpcode == PPC::TCRETURNai8;
1473*0b57cec5SDimitry Andric 
1474*0b57cec5SDimitry Andric     if (UsesTCRet) {
1475*0b57cec5SDimitry Andric       int MaxTCRetDelta = FI->getTailCallSPDelta();
1476*0b57cec5SDimitry Andric       MachineOperand &StackAdjust = MBBI->getOperand(1);
1477*0b57cec5SDimitry Andric       assert(StackAdjust.isImm() && "Expecting immediate value.");
1478*0b57cec5SDimitry Andric       // Adjust stack pointer.
1479*0b57cec5SDimitry Andric       int StackAdj = StackAdjust.getImm();
1480*0b57cec5SDimitry Andric       int Delta = StackAdj - MaxTCRetDelta;
1481*0b57cec5SDimitry Andric       assert((Delta >= 0) && "Delta must be positive");
1482*0b57cec5SDimitry Andric       if (MaxTCRetDelta>0)
1483*0b57cec5SDimitry Andric         FrameSize += (StackAdj +Delta);
1484*0b57cec5SDimitry Andric       else
1485*0b57cec5SDimitry Andric         FrameSize += StackAdj;
1486*0b57cec5SDimitry Andric     }
1487*0b57cec5SDimitry Andric   }
1488*0b57cec5SDimitry Andric 
1489*0b57cec5SDimitry Andric   // Frames of 32KB & larger require special handling because they cannot be
1490*0b57cec5SDimitry Andric   // indexed into with a simple LD/LWZ immediate offset operand.
1491*0b57cec5SDimitry Andric   bool isLargeFrame = !isInt<16>(FrameSize);
1492*0b57cec5SDimitry Andric 
1493*0b57cec5SDimitry Andric   // On targets without red zone, the SP needs to be restored last, so that
1494*0b57cec5SDimitry Andric   // all live contents of the stack frame are upwards of the SP. This means
1495*0b57cec5SDimitry Andric   // that we cannot restore SP just now, since there may be more registers
1496*0b57cec5SDimitry Andric   // to restore from the stack frame (e.g. R31). If the frame size is not
1497*0b57cec5SDimitry Andric   // a simple immediate value, we will need a spare register to hold the
1498*0b57cec5SDimitry Andric   // restored SP. If the frame size is known and small, we can simply adjust
1499*0b57cec5SDimitry Andric   // the offsets of the registers to be restored, and still use SP to restore
1500*0b57cec5SDimitry Andric   // them. In such case, the final update of SP will be to add the frame
1501*0b57cec5SDimitry Andric   // size to it.
1502*0b57cec5SDimitry Andric   // To simplify the code, set RBReg to the base register used to restore
1503*0b57cec5SDimitry Andric   // values from the stack, and set SPAdd to the value that needs to be added
1504*0b57cec5SDimitry Andric   // to the SP at the end. The default values are as if red zone was present.
1505*0b57cec5SDimitry Andric   unsigned RBReg = SPReg;
1506*0b57cec5SDimitry Andric   unsigned SPAdd = 0;
1507*0b57cec5SDimitry Andric 
1508*0b57cec5SDimitry Andric   // Check if we can move the stack update instruction up the epilogue
1509*0b57cec5SDimitry Andric   // past the callee saves. This will allow the move to LR instruction
1510*0b57cec5SDimitry Andric   // to be executed before the restores of the callee saves which means
1511*0b57cec5SDimitry Andric   // that the callee saves can hide the latency from the MTLR instrcution.
1512*0b57cec5SDimitry Andric   MachineBasicBlock::iterator StackUpdateLoc = MBBI;
1513*0b57cec5SDimitry Andric   if (stackUpdateCanBeMoved(MF)) {
1514*0b57cec5SDimitry Andric     const std::vector<CalleeSavedInfo> & Info = MFI.getCalleeSavedInfo();
1515*0b57cec5SDimitry Andric     for (CalleeSavedInfo CSI : Info) {
1516*0b57cec5SDimitry Andric       int FrIdx = CSI.getFrameIdx();
1517*0b57cec5SDimitry Andric       // If the frame index is not negative the callee saved info belongs to a
1518*0b57cec5SDimitry Andric       // stack object that is not a fixed stack object. We ignore non-fixed
1519*0b57cec5SDimitry Andric       // stack objects because we won't move the update of the stack pointer
1520*0b57cec5SDimitry Andric       // past them.
1521*0b57cec5SDimitry Andric       if (FrIdx >= 0)
1522*0b57cec5SDimitry Andric         continue;
1523*0b57cec5SDimitry Andric 
1524*0b57cec5SDimitry Andric       if (MFI.isFixedObjectIndex(FrIdx) && MFI.getObjectOffset(FrIdx) < 0)
1525*0b57cec5SDimitry Andric         StackUpdateLoc--;
1526*0b57cec5SDimitry Andric       else {
1527*0b57cec5SDimitry Andric         // Abort the operation as we can't update all CSR restores.
1528*0b57cec5SDimitry Andric         StackUpdateLoc = MBBI;
1529*0b57cec5SDimitry Andric         break;
1530*0b57cec5SDimitry Andric       }
1531*0b57cec5SDimitry Andric     }
1532*0b57cec5SDimitry Andric   }
1533*0b57cec5SDimitry Andric 
1534*0b57cec5SDimitry Andric   if (FrameSize) {
1535*0b57cec5SDimitry Andric     // In the prologue, the loaded (or persistent) stack pointer value is
1536*0b57cec5SDimitry Andric     // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red
1537*0b57cec5SDimitry Andric     // zone add this offset back now.
1538*0b57cec5SDimitry Andric 
1539*0b57cec5SDimitry Andric     // If this function contained a fastcc call and GuaranteedTailCallOpt is
1540*0b57cec5SDimitry Andric     // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1541*0b57cec5SDimitry Andric     // call which invalidates the stack pointer value in SP(0). So we use the
1542*0b57cec5SDimitry Andric     // value of R31 in this case.
1543*0b57cec5SDimitry Andric     if (FI->hasFastCall()) {
1544*0b57cec5SDimitry Andric       assert(HasFP && "Expecting a valid frame pointer.");
1545*0b57cec5SDimitry Andric       if (!HasRedZone)
1546*0b57cec5SDimitry Andric         RBReg = FPReg;
1547*0b57cec5SDimitry Andric       if (!isLargeFrame) {
1548*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, AddImmInst, RBReg)
1549*0b57cec5SDimitry Andric           .addReg(FPReg).addImm(FrameSize);
1550*0b57cec5SDimitry Andric       } else {
1551*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1552*0b57cec5SDimitry Andric           .addImm(FrameSize >> 16);
1553*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1554*0b57cec5SDimitry Andric           .addReg(ScratchReg, RegState::Kill)
1555*0b57cec5SDimitry Andric           .addImm(FrameSize & 0xFFFF);
1556*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, AddInst)
1557*0b57cec5SDimitry Andric           .addReg(RBReg)
1558*0b57cec5SDimitry Andric           .addReg(FPReg)
1559*0b57cec5SDimitry Andric           .addReg(ScratchReg);
1560*0b57cec5SDimitry Andric       }
1561*0b57cec5SDimitry Andric     } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
1562*0b57cec5SDimitry Andric       if (HasRedZone) {
1563*0b57cec5SDimitry Andric         BuildMI(MBB, StackUpdateLoc, dl, AddImmInst, SPReg)
1564*0b57cec5SDimitry Andric           .addReg(SPReg)
1565*0b57cec5SDimitry Andric           .addImm(FrameSize);
1566*0b57cec5SDimitry Andric       } else {
1567*0b57cec5SDimitry Andric         // Make sure that adding FrameSize will not overflow the max offset
1568*0b57cec5SDimitry Andric         // size.
1569*0b57cec5SDimitry Andric         assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 &&
1570*0b57cec5SDimitry Andric                "Local offsets should be negative");
1571*0b57cec5SDimitry Andric         SPAdd = FrameSize;
1572*0b57cec5SDimitry Andric         FPOffset += FrameSize;
1573*0b57cec5SDimitry Andric         BPOffset += FrameSize;
1574*0b57cec5SDimitry Andric         PBPOffset += FrameSize;
1575*0b57cec5SDimitry Andric       }
1576*0b57cec5SDimitry Andric     } else {
1577*0b57cec5SDimitry Andric       // We don't want to use ScratchReg as a base register, because it
1578*0b57cec5SDimitry Andric       // could happen to be R0. Use FP instead, but make sure to preserve it.
1579*0b57cec5SDimitry Andric       if (!HasRedZone) {
1580*0b57cec5SDimitry Andric         // If FP is not saved, copy it to ScratchReg.
1581*0b57cec5SDimitry Andric         if (!HasFP)
1582*0b57cec5SDimitry Andric           BuildMI(MBB, MBBI, dl, OrInst, ScratchReg)
1583*0b57cec5SDimitry Andric             .addReg(FPReg)
1584*0b57cec5SDimitry Andric             .addReg(FPReg);
1585*0b57cec5SDimitry Andric         RBReg = FPReg;
1586*0b57cec5SDimitry Andric       }
1587*0b57cec5SDimitry Andric       BuildMI(MBB, StackUpdateLoc, dl, LoadInst, RBReg)
1588*0b57cec5SDimitry Andric         .addImm(0)
1589*0b57cec5SDimitry Andric         .addReg(SPReg);
1590*0b57cec5SDimitry Andric     }
1591*0b57cec5SDimitry Andric   }
1592*0b57cec5SDimitry Andric   assert(RBReg != ScratchReg && "Should have avoided ScratchReg");
1593*0b57cec5SDimitry Andric   // If there is no red zone, ScratchReg may be needed for holding a useful
1594*0b57cec5SDimitry Andric   // value (although not the base register). Make sure it is not overwritten
1595*0b57cec5SDimitry Andric   // too early.
1596*0b57cec5SDimitry Andric 
1597*0b57cec5SDimitry Andric   assert((isPPC64 || !MustSaveCR) &&
1598*0b57cec5SDimitry Andric          "Epilogue CR restoring supported only in 64-bit mode");
1599*0b57cec5SDimitry Andric 
1600*0b57cec5SDimitry Andric   // If we need to restore both the LR and the CR and we only have one
1601*0b57cec5SDimitry Andric   // available scratch register, we must do them one at a time.
1602*0b57cec5SDimitry Andric   if (MustSaveCR && SingleScratchReg && MustSaveLR) {
1603*0b57cec5SDimitry Andric     // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg
1604*0b57cec5SDimitry Andric     // is live here.
1605*0b57cec5SDimitry Andric     assert(HasRedZone && "Expecting red zone");
1606*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
16078bcb0991SDimitry Andric       .addImm(getCRSaveOffset())
1608*0b57cec5SDimitry Andric       .addReg(SPReg);
1609*0b57cec5SDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1610*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1611*0b57cec5SDimitry Andric         .addReg(TempReg, getKillRegState(i == e-1));
1612*0b57cec5SDimitry Andric   }
1613*0b57cec5SDimitry Andric 
1614*0b57cec5SDimitry Andric   // Delay restoring of the LR if ScratchReg is needed. This is ok, since
1615*0b57cec5SDimitry Andric   // LR is stored in the caller's stack frame. ScratchReg will be needed
1616*0b57cec5SDimitry Andric   // if RBReg is anything other than SP. We shouldn't use ScratchReg as
1617*0b57cec5SDimitry Andric   // a base register anyway, because it may happen to be R0.
1618*0b57cec5SDimitry Andric   bool LoadedLR = false;
1619*0b57cec5SDimitry Andric   if (MustSaveLR && RBReg == SPReg && isInt<16>(LROffset+SPAdd)) {
1620*0b57cec5SDimitry Andric     BuildMI(MBB, StackUpdateLoc, dl, LoadInst, ScratchReg)
1621*0b57cec5SDimitry Andric       .addImm(LROffset+SPAdd)
1622*0b57cec5SDimitry Andric       .addReg(RBReg);
1623*0b57cec5SDimitry Andric     LoadedLR = true;
1624*0b57cec5SDimitry Andric   }
1625*0b57cec5SDimitry Andric 
1626*0b57cec5SDimitry Andric   if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) {
1627*0b57cec5SDimitry Andric     // This will only occur for PPC64.
1628*0b57cec5SDimitry Andric     assert(isPPC64 && "Expecting 64-bit mode");
1629*0b57cec5SDimitry Andric     assert(RBReg == SPReg && "Should be using SP as a base register");
1630*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
16318bcb0991SDimitry Andric       .addImm(getCRSaveOffset())
1632*0b57cec5SDimitry Andric       .addReg(RBReg);
1633*0b57cec5SDimitry Andric   }
1634*0b57cec5SDimitry Andric 
1635*0b57cec5SDimitry Andric   if (HasFP) {
1636*0b57cec5SDimitry Andric     // If there is red zone, restore FP directly, since SP has already been
1637*0b57cec5SDimitry Andric     // restored. Otherwise, restore the value of FP into ScratchReg.
1638*0b57cec5SDimitry Andric     if (HasRedZone || RBReg == SPReg)
1639*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1640*0b57cec5SDimitry Andric         .addImm(FPOffset)
1641*0b57cec5SDimitry Andric         .addReg(SPReg);
1642*0b57cec5SDimitry Andric     else
1643*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1644*0b57cec5SDimitry Andric         .addImm(FPOffset)
1645*0b57cec5SDimitry Andric         .addReg(RBReg);
1646*0b57cec5SDimitry Andric   }
1647*0b57cec5SDimitry Andric 
1648*0b57cec5SDimitry Andric   if (FI->usesPICBase())
1649*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, LoadInst, PPC::R30)
1650*0b57cec5SDimitry Andric       .addImm(PBPOffset)
1651*0b57cec5SDimitry Andric       .addReg(RBReg);
1652*0b57cec5SDimitry Andric 
1653*0b57cec5SDimitry Andric   if (HasBP)
1654*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1655*0b57cec5SDimitry Andric       .addImm(BPOffset)
1656*0b57cec5SDimitry Andric       .addReg(RBReg);
1657*0b57cec5SDimitry Andric 
1658*0b57cec5SDimitry Andric   // There is nothing more to be loaded from the stack, so now we can
1659*0b57cec5SDimitry Andric   // restore SP: SP = RBReg + SPAdd.
1660*0b57cec5SDimitry Andric   if (RBReg != SPReg || SPAdd != 0) {
1661*0b57cec5SDimitry Andric     assert(!HasRedZone && "This should not happen with red zone");
1662*0b57cec5SDimitry Andric     // If SPAdd is 0, generate a copy.
1663*0b57cec5SDimitry Andric     if (SPAdd == 0)
1664*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, OrInst, SPReg)
1665*0b57cec5SDimitry Andric         .addReg(RBReg)
1666*0b57cec5SDimitry Andric         .addReg(RBReg);
1667*0b57cec5SDimitry Andric     else
1668*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1669*0b57cec5SDimitry Andric         .addReg(RBReg)
1670*0b57cec5SDimitry Andric         .addImm(SPAdd);
1671*0b57cec5SDimitry Andric 
1672*0b57cec5SDimitry Andric     assert(RBReg != ScratchReg && "Should be using FP or SP as base register");
1673*0b57cec5SDimitry Andric     if (RBReg == FPReg)
1674*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1675*0b57cec5SDimitry Andric         .addReg(ScratchReg)
1676*0b57cec5SDimitry Andric         .addReg(ScratchReg);
1677*0b57cec5SDimitry Andric 
1678*0b57cec5SDimitry Andric     // Now load the LR from the caller's stack frame.
1679*0b57cec5SDimitry Andric     if (MustSaveLR && !LoadedLR)
1680*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1681*0b57cec5SDimitry Andric         .addImm(LROffset)
1682*0b57cec5SDimitry Andric         .addReg(SPReg);
1683*0b57cec5SDimitry Andric   }
1684*0b57cec5SDimitry Andric 
1685*0b57cec5SDimitry Andric   if (MustSaveCR &&
1686*0b57cec5SDimitry Andric       !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
1687*0b57cec5SDimitry Andric     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1688*0b57cec5SDimitry Andric       BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1689*0b57cec5SDimitry Andric         .addReg(TempReg, getKillRegState(i == e-1));
1690*0b57cec5SDimitry Andric 
1691*0b57cec5SDimitry Andric   if (MustSaveLR)
1692*0b57cec5SDimitry Andric     BuildMI(MBB, StackUpdateLoc, dl, MTLRInst).addReg(ScratchReg);
1693*0b57cec5SDimitry Andric 
1694*0b57cec5SDimitry Andric   // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1695*0b57cec5SDimitry Andric   // call optimization
1696*0b57cec5SDimitry Andric   if (IsReturnBlock) {
1697*0b57cec5SDimitry Andric     unsigned RetOpcode = MBBI->getOpcode();
1698*0b57cec5SDimitry Andric     if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1699*0b57cec5SDimitry Andric         (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
1700*0b57cec5SDimitry Andric         MF.getFunction().getCallingConv() == CallingConv::Fast) {
1701*0b57cec5SDimitry Andric       PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1702*0b57cec5SDimitry Andric       unsigned CallerAllocatedAmt = FI->getMinReservedArea();
1703*0b57cec5SDimitry Andric 
1704*0b57cec5SDimitry Andric       if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1705*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1706*0b57cec5SDimitry Andric           .addReg(SPReg).addImm(CallerAllocatedAmt);
1707*0b57cec5SDimitry Andric       } else {
1708*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1709*0b57cec5SDimitry Andric           .addImm(CallerAllocatedAmt >> 16);
1710*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1711*0b57cec5SDimitry Andric           .addReg(ScratchReg, RegState::Kill)
1712*0b57cec5SDimitry Andric           .addImm(CallerAllocatedAmt & 0xFFFF);
1713*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, AddInst)
1714*0b57cec5SDimitry Andric           .addReg(SPReg)
1715*0b57cec5SDimitry Andric           .addReg(FPReg)
1716*0b57cec5SDimitry Andric           .addReg(ScratchReg);
1717*0b57cec5SDimitry Andric       }
1718*0b57cec5SDimitry Andric     } else {
1719*0b57cec5SDimitry Andric       createTailCallBranchInstr(MBB);
1720*0b57cec5SDimitry Andric     }
1721*0b57cec5SDimitry Andric   }
1722*0b57cec5SDimitry Andric }
1723*0b57cec5SDimitry Andric 
1724*0b57cec5SDimitry Andric void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
1725*0b57cec5SDimitry Andric   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1726*0b57cec5SDimitry Andric 
1727*0b57cec5SDimitry Andric   // If we got this far a first terminator should exist.
1728*0b57cec5SDimitry Andric   assert(MBBI != MBB.end() && "Failed to find the first terminator.");
1729*0b57cec5SDimitry Andric 
1730*0b57cec5SDimitry Andric   DebugLoc dl = MBBI->getDebugLoc();
1731*0b57cec5SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
1732*0b57cec5SDimitry Andric 
1733*0b57cec5SDimitry Andric   // Create branch instruction for pseudo tail call return instruction
1734*0b57cec5SDimitry Andric   unsigned RetOpcode = MBBI->getOpcode();
1735*0b57cec5SDimitry Andric   if (RetOpcode == PPC::TCRETURNdi) {
1736*0b57cec5SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
1737*0b57cec5SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
1738*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1739*0b57cec5SDimitry Andric       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1740*0b57cec5SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNri) {
1741*0b57cec5SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
1742*0b57cec5SDimitry Andric     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1743*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1744*0b57cec5SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNai) {
1745*0b57cec5SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
1746*0b57cec5SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
1747*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1748*0b57cec5SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNdi8) {
1749*0b57cec5SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
1750*0b57cec5SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
1751*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1752*0b57cec5SDimitry Andric       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1753*0b57cec5SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNri8) {
1754*0b57cec5SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
1755*0b57cec5SDimitry Andric     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1756*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1757*0b57cec5SDimitry Andric   } else if (RetOpcode == PPC::TCRETURNai8) {
1758*0b57cec5SDimitry Andric     MBBI = MBB.getLastNonDebugInstr();
1759*0b57cec5SDimitry Andric     MachineOperand &JumpTarget = MBBI->getOperand(0);
1760*0b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1761*0b57cec5SDimitry Andric   }
1762*0b57cec5SDimitry Andric }
1763*0b57cec5SDimitry Andric 
1764*0b57cec5SDimitry Andric void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1765*0b57cec5SDimitry Andric                                             BitVector &SavedRegs,
1766*0b57cec5SDimitry Andric                                             RegScavenger *RS) const {
1767*0b57cec5SDimitry Andric   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1768*0b57cec5SDimitry Andric 
1769*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1770*0b57cec5SDimitry Andric 
1771*0b57cec5SDimitry Andric   //  Save and clear the LR state.
1772*0b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1773*0b57cec5SDimitry Andric   unsigned LR = RegInfo->getRARegister();
1774*0b57cec5SDimitry Andric   FI->setMustSaveLR(MustSaveLR(MF, LR));
1775*0b57cec5SDimitry Andric   SavedRegs.reset(LR);
1776*0b57cec5SDimitry Andric 
1777*0b57cec5SDimitry Andric   //  Save R31 if necessary
1778*0b57cec5SDimitry Andric   int FPSI = FI->getFramePointerSaveIndex();
17798bcb0991SDimitry Andric   const bool isPPC64 = Subtarget.isPPC64();
17808bcb0991SDimitry Andric   const bool IsDarwinABI  = Subtarget.isDarwinABI();
1781*0b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
1782*0b57cec5SDimitry Andric 
1783*0b57cec5SDimitry Andric   // If the frame pointer save index hasn't been defined yet.
1784*0b57cec5SDimitry Andric   if (!FPSI && needsFP(MF)) {
1785*0b57cec5SDimitry Andric     // Find out what the fix offset of the frame pointer save area.
1786*0b57cec5SDimitry Andric     int FPOffset = getFramePointerSaveOffset();
1787*0b57cec5SDimitry Andric     // Allocate the frame index for frame pointer save area.
1788*0b57cec5SDimitry Andric     FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
1789*0b57cec5SDimitry Andric     // Save the result.
1790*0b57cec5SDimitry Andric     FI->setFramePointerSaveIndex(FPSI);
1791*0b57cec5SDimitry Andric   }
1792*0b57cec5SDimitry Andric 
1793*0b57cec5SDimitry Andric   int BPSI = FI->getBasePointerSaveIndex();
1794*0b57cec5SDimitry Andric   if (!BPSI && RegInfo->hasBasePointer(MF)) {
1795*0b57cec5SDimitry Andric     int BPOffset = getBasePointerSaveOffset();
1796*0b57cec5SDimitry Andric     // Allocate the frame index for the base pointer save area.
1797*0b57cec5SDimitry Andric     BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1798*0b57cec5SDimitry Andric     // Save the result.
1799*0b57cec5SDimitry Andric     FI->setBasePointerSaveIndex(BPSI);
1800*0b57cec5SDimitry Andric   }
1801*0b57cec5SDimitry Andric 
1802*0b57cec5SDimitry Andric   // Reserve stack space for the PIC Base register (R30).
1803*0b57cec5SDimitry Andric   // Only used in SVR4 32-bit.
1804*0b57cec5SDimitry Andric   if (FI->usesPICBase()) {
1805*0b57cec5SDimitry Andric     int PBPSI = MFI.CreateFixedObject(4, -8, true);
1806*0b57cec5SDimitry Andric     FI->setPICBasePointerSaveIndex(PBPSI);
1807*0b57cec5SDimitry Andric   }
1808*0b57cec5SDimitry Andric 
1809*0b57cec5SDimitry Andric   // Make sure we don't explicitly spill r31, because, for example, we have
1810*0b57cec5SDimitry Andric   // some inline asm which explicitly clobbers it, when we otherwise have a
1811*0b57cec5SDimitry Andric   // frame pointer and are using r31's spill slot for the prologue/epilogue
1812*0b57cec5SDimitry Andric   // code. Same goes for the base pointer and the PIC base register.
1813*0b57cec5SDimitry Andric   if (needsFP(MF))
1814*0b57cec5SDimitry Andric     SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31);
1815*0b57cec5SDimitry Andric   if (RegInfo->hasBasePointer(MF))
1816*0b57cec5SDimitry Andric     SavedRegs.reset(RegInfo->getBaseRegister(MF));
1817*0b57cec5SDimitry Andric   if (FI->usesPICBase())
1818*0b57cec5SDimitry Andric     SavedRegs.reset(PPC::R30);
1819*0b57cec5SDimitry Andric 
1820*0b57cec5SDimitry Andric   // Reserve stack space to move the linkage area to in case of a tail call.
1821*0b57cec5SDimitry Andric   int TCSPDelta = 0;
1822*0b57cec5SDimitry Andric   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1823*0b57cec5SDimitry Andric       (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
1824*0b57cec5SDimitry Andric     MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
1825*0b57cec5SDimitry Andric   }
1826*0b57cec5SDimitry Andric 
1827*0b57cec5SDimitry Andric   // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
1828*0b57cec5SDimitry Andric   // function uses CR 2, 3, or 4.
18298bcb0991SDimitry Andric   if (!isPPC64 && !IsDarwinABI &&
1830*0b57cec5SDimitry Andric       (SavedRegs.test(PPC::CR2) ||
1831*0b57cec5SDimitry Andric        SavedRegs.test(PPC::CR3) ||
1832*0b57cec5SDimitry Andric        SavedRegs.test(PPC::CR4))) {
1833*0b57cec5SDimitry Andric     int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1834*0b57cec5SDimitry Andric     FI->setCRSpillFrameIndex(FrameIdx);
1835*0b57cec5SDimitry Andric   }
1836*0b57cec5SDimitry Andric }
1837*0b57cec5SDimitry Andric 
1838*0b57cec5SDimitry Andric void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
1839*0b57cec5SDimitry Andric                                                        RegScavenger *RS) const {
1840*0b57cec5SDimitry Andric   // Early exit if not using the SVR4 ABI.
1841*0b57cec5SDimitry Andric   if (!Subtarget.isSVR4ABI()) {
1842*0b57cec5SDimitry Andric     addScavengingSpillSlot(MF, RS);
1843*0b57cec5SDimitry Andric     return;
1844*0b57cec5SDimitry Andric   }
1845*0b57cec5SDimitry Andric 
1846*0b57cec5SDimitry Andric   // Get callee saved register information.
1847*0b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
1848*0b57cec5SDimitry Andric   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1849*0b57cec5SDimitry Andric 
1850*0b57cec5SDimitry Andric   // If the function is shrink-wrapped, and if the function has a tail call, the
1851*0b57cec5SDimitry Andric   // tail call might not be in the new RestoreBlock, so real branch instruction
1852*0b57cec5SDimitry Andric   // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
1853*0b57cec5SDimitry Andric   // RestoreBlock. So we handle this case here.
1854*0b57cec5SDimitry Andric   if (MFI.getSavePoint() && MFI.hasTailCall()) {
1855*0b57cec5SDimitry Andric     MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
1856*0b57cec5SDimitry Andric     for (MachineBasicBlock &MBB : MF) {
1857*0b57cec5SDimitry Andric       if (MBB.isReturnBlock() && (&MBB) != RestoreBlock)
1858*0b57cec5SDimitry Andric         createTailCallBranchInstr(MBB);
1859*0b57cec5SDimitry Andric     }
1860*0b57cec5SDimitry Andric   }
1861*0b57cec5SDimitry Andric 
1862*0b57cec5SDimitry Andric   // Early exit if no callee saved registers are modified!
1863*0b57cec5SDimitry Andric   if (CSI.empty() && !needsFP(MF)) {
1864*0b57cec5SDimitry Andric     addScavengingSpillSlot(MF, RS);
1865*0b57cec5SDimitry Andric     return;
1866*0b57cec5SDimitry Andric   }
1867*0b57cec5SDimitry Andric 
1868*0b57cec5SDimitry Andric   unsigned MinGPR = PPC::R31;
1869*0b57cec5SDimitry Andric   unsigned MinG8R = PPC::X31;
1870*0b57cec5SDimitry Andric   unsigned MinFPR = PPC::F31;
1871*0b57cec5SDimitry Andric   unsigned MinVR = Subtarget.hasSPE() ? PPC::S31 : PPC::V31;
1872*0b57cec5SDimitry Andric 
1873*0b57cec5SDimitry Andric   bool HasGPSaveArea = false;
1874*0b57cec5SDimitry Andric   bool HasG8SaveArea = false;
1875*0b57cec5SDimitry Andric   bool HasFPSaveArea = false;
1876*0b57cec5SDimitry Andric   bool HasVRSAVESaveArea = false;
1877*0b57cec5SDimitry Andric   bool HasVRSaveArea = false;
1878*0b57cec5SDimitry Andric 
1879*0b57cec5SDimitry Andric   SmallVector<CalleeSavedInfo, 18> GPRegs;
1880*0b57cec5SDimitry Andric   SmallVector<CalleeSavedInfo, 18> G8Regs;
1881*0b57cec5SDimitry Andric   SmallVector<CalleeSavedInfo, 18> FPRegs;
1882*0b57cec5SDimitry Andric   SmallVector<CalleeSavedInfo, 18> VRegs;
1883*0b57cec5SDimitry Andric 
1884*0b57cec5SDimitry Andric   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1885*0b57cec5SDimitry Andric     unsigned Reg = CSI[i].getReg();
1886*0b57cec5SDimitry Andric     assert((!MF.getInfo<PPCFunctionInfo>()->mustSaveTOC() ||
1887*0b57cec5SDimitry Andric             (Reg != PPC::X2 && Reg != PPC::R2)) &&
1888*0b57cec5SDimitry Andric            "Not expecting to try to spill R2 in a function that must save TOC");
18898bcb0991SDimitry Andric     if (PPC::GPRCRegClass.contains(Reg)) {
1890*0b57cec5SDimitry Andric       HasGPSaveArea = true;
1891*0b57cec5SDimitry Andric 
1892*0b57cec5SDimitry Andric       GPRegs.push_back(CSI[i]);
1893*0b57cec5SDimitry Andric 
1894*0b57cec5SDimitry Andric       if (Reg < MinGPR) {
1895*0b57cec5SDimitry Andric         MinGPR = Reg;
1896*0b57cec5SDimitry Andric       }
1897*0b57cec5SDimitry Andric     } else if (PPC::G8RCRegClass.contains(Reg)) {
1898*0b57cec5SDimitry Andric       HasG8SaveArea = true;
1899*0b57cec5SDimitry Andric 
1900*0b57cec5SDimitry Andric       G8Regs.push_back(CSI[i]);
1901*0b57cec5SDimitry Andric 
1902*0b57cec5SDimitry Andric       if (Reg < MinG8R) {
1903*0b57cec5SDimitry Andric         MinG8R = Reg;
1904*0b57cec5SDimitry Andric       }
1905*0b57cec5SDimitry Andric     } else if (PPC::F8RCRegClass.contains(Reg)) {
1906*0b57cec5SDimitry Andric       HasFPSaveArea = true;
1907*0b57cec5SDimitry Andric 
1908*0b57cec5SDimitry Andric       FPRegs.push_back(CSI[i]);
1909*0b57cec5SDimitry Andric 
1910*0b57cec5SDimitry Andric       if (Reg < MinFPR) {
1911*0b57cec5SDimitry Andric         MinFPR = Reg;
1912*0b57cec5SDimitry Andric       }
1913*0b57cec5SDimitry Andric     } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1914*0b57cec5SDimitry Andric                PPC::CRRCRegClass.contains(Reg)) {
1915*0b57cec5SDimitry Andric       ; // do nothing, as we already know whether CRs are spilled
1916*0b57cec5SDimitry Andric     } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
1917*0b57cec5SDimitry Andric       HasVRSAVESaveArea = true;
1918*0b57cec5SDimitry Andric     } else if (PPC::VRRCRegClass.contains(Reg) ||
1919*0b57cec5SDimitry Andric                PPC::SPERCRegClass.contains(Reg)) {
1920*0b57cec5SDimitry Andric       // Altivec and SPE are mutually exclusive, but have the same stack
1921*0b57cec5SDimitry Andric       // alignment requirements, so overload the save area for both cases.
1922*0b57cec5SDimitry Andric       HasVRSaveArea = true;
1923*0b57cec5SDimitry Andric 
1924*0b57cec5SDimitry Andric       VRegs.push_back(CSI[i]);
1925*0b57cec5SDimitry Andric 
1926*0b57cec5SDimitry Andric       if (Reg < MinVR) {
1927*0b57cec5SDimitry Andric         MinVR = Reg;
1928*0b57cec5SDimitry Andric       }
1929*0b57cec5SDimitry Andric     } else {
1930*0b57cec5SDimitry Andric       llvm_unreachable("Unknown RegisterClass!");
1931*0b57cec5SDimitry Andric     }
1932*0b57cec5SDimitry Andric   }
1933*0b57cec5SDimitry Andric 
1934*0b57cec5SDimitry Andric   PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
1935*0b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1936*0b57cec5SDimitry Andric 
1937*0b57cec5SDimitry Andric   int64_t LowerBound = 0;
1938*0b57cec5SDimitry Andric 
1939*0b57cec5SDimitry Andric   // Take into account stack space reserved for tail calls.
1940*0b57cec5SDimitry Andric   int TCSPDelta = 0;
1941*0b57cec5SDimitry Andric   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1942*0b57cec5SDimitry Andric       (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
1943*0b57cec5SDimitry Andric     LowerBound = TCSPDelta;
1944*0b57cec5SDimitry Andric   }
1945*0b57cec5SDimitry Andric 
1946*0b57cec5SDimitry Andric   // The Floating-point register save area is right below the back chain word
1947*0b57cec5SDimitry Andric   // of the previous stack frame.
1948*0b57cec5SDimitry Andric   if (HasFPSaveArea) {
1949*0b57cec5SDimitry Andric     for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1950*0b57cec5SDimitry Andric       int FI = FPRegs[i].getFrameIdx();
1951*0b57cec5SDimitry Andric 
1952*0b57cec5SDimitry Andric       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1953*0b57cec5SDimitry Andric     }
1954*0b57cec5SDimitry Andric 
1955*0b57cec5SDimitry Andric     LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
1956*0b57cec5SDimitry Andric   }
1957*0b57cec5SDimitry Andric 
1958*0b57cec5SDimitry Andric   // Check whether the frame pointer register is allocated. If so, make sure it
1959*0b57cec5SDimitry Andric   // is spilled to the correct offset.
1960*0b57cec5SDimitry Andric   if (needsFP(MF)) {
1961*0b57cec5SDimitry Andric     int FI = PFI->getFramePointerSaveIndex();
1962*0b57cec5SDimitry Andric     assert(FI && "No Frame Pointer Save Slot!");
1963*0b57cec5SDimitry Andric     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1964*0b57cec5SDimitry Andric     // FP is R31/X31, so no need to update MinGPR/MinG8R.
1965*0b57cec5SDimitry Andric     HasGPSaveArea = true;
1966*0b57cec5SDimitry Andric   }
1967*0b57cec5SDimitry Andric 
1968*0b57cec5SDimitry Andric   if (PFI->usesPICBase()) {
1969*0b57cec5SDimitry Andric     int FI = PFI->getPICBasePointerSaveIndex();
1970*0b57cec5SDimitry Andric     assert(FI && "No PIC Base Pointer Save Slot!");
1971*0b57cec5SDimitry Andric     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1972*0b57cec5SDimitry Andric 
1973*0b57cec5SDimitry Andric     MinGPR = std::min<unsigned>(MinGPR, PPC::R30);
1974*0b57cec5SDimitry Andric     HasGPSaveArea = true;
1975*0b57cec5SDimitry Andric   }
1976*0b57cec5SDimitry Andric 
1977*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
1978*0b57cec5SDimitry Andric   if (RegInfo->hasBasePointer(MF)) {
1979*0b57cec5SDimitry Andric     int FI = PFI->getBasePointerSaveIndex();
1980*0b57cec5SDimitry Andric     assert(FI && "No Base Pointer Save Slot!");
1981*0b57cec5SDimitry Andric     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1982*0b57cec5SDimitry Andric 
19838bcb0991SDimitry Andric     Register BP = RegInfo->getBaseRegister(MF);
1984*0b57cec5SDimitry Andric     if (PPC::G8RCRegClass.contains(BP)) {
1985*0b57cec5SDimitry Andric       MinG8R = std::min<unsigned>(MinG8R, BP);
1986*0b57cec5SDimitry Andric       HasG8SaveArea = true;
1987*0b57cec5SDimitry Andric     } else if (PPC::GPRCRegClass.contains(BP)) {
1988*0b57cec5SDimitry Andric       MinGPR = std::min<unsigned>(MinGPR, BP);
1989*0b57cec5SDimitry Andric       HasGPSaveArea = true;
1990*0b57cec5SDimitry Andric     }
1991*0b57cec5SDimitry Andric   }
1992*0b57cec5SDimitry Andric 
1993*0b57cec5SDimitry Andric   // General register save area starts right below the Floating-point
1994*0b57cec5SDimitry Andric   // register save area.
1995*0b57cec5SDimitry Andric   if (HasGPSaveArea || HasG8SaveArea) {
1996*0b57cec5SDimitry Andric     // Move general register save area spill slots down, taking into account
1997*0b57cec5SDimitry Andric     // the size of the Floating-point register save area.
1998*0b57cec5SDimitry Andric     for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1999*0b57cec5SDimitry Andric       if (!GPRegs[i].isSpilledToReg()) {
2000*0b57cec5SDimitry Andric         int FI = GPRegs[i].getFrameIdx();
2001*0b57cec5SDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
2002*0b57cec5SDimitry Andric       }
2003*0b57cec5SDimitry Andric     }
2004*0b57cec5SDimitry Andric 
2005*0b57cec5SDimitry Andric     // Move general register save area spill slots down, taking into account
2006*0b57cec5SDimitry Andric     // the size of the Floating-point register save area.
2007*0b57cec5SDimitry Andric     for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
2008*0b57cec5SDimitry Andric       if (!G8Regs[i].isSpilledToReg()) {
2009*0b57cec5SDimitry Andric         int FI = G8Regs[i].getFrameIdx();
2010*0b57cec5SDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
2011*0b57cec5SDimitry Andric       }
2012*0b57cec5SDimitry Andric     }
2013*0b57cec5SDimitry Andric 
2014*0b57cec5SDimitry Andric     unsigned MinReg =
2015*0b57cec5SDimitry Andric       std::min<unsigned>(TRI->getEncodingValue(MinGPR),
2016*0b57cec5SDimitry Andric                          TRI->getEncodingValue(MinG8R));
2017*0b57cec5SDimitry Andric 
2018*0b57cec5SDimitry Andric     if (Subtarget.isPPC64()) {
2019*0b57cec5SDimitry Andric       LowerBound -= (31 - MinReg + 1) * 8;
2020*0b57cec5SDimitry Andric     } else {
2021*0b57cec5SDimitry Andric       LowerBound -= (31 - MinReg + 1) * 4;
2022*0b57cec5SDimitry Andric     }
2023*0b57cec5SDimitry Andric   }
2024*0b57cec5SDimitry Andric 
2025*0b57cec5SDimitry Andric   // For 32-bit only, the CR save area is below the general register
2026*0b57cec5SDimitry Andric   // save area.  For 64-bit SVR4, the CR save area is addressed relative
2027*0b57cec5SDimitry Andric   // to the stack pointer and hence does not need an adjustment here.
2028*0b57cec5SDimitry Andric   // Only CR2 (the first nonvolatile spilled) has an associated frame
2029*0b57cec5SDimitry Andric   // index so that we have a single uniform save area.
2030*0b57cec5SDimitry Andric   if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
2031*0b57cec5SDimitry Andric     // Adjust the frame index of the CR spill slot.
2032*0b57cec5SDimitry Andric     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2033*0b57cec5SDimitry Andric       unsigned Reg = CSI[i].getReg();
2034*0b57cec5SDimitry Andric 
2035*0b57cec5SDimitry Andric       if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
2036*0b57cec5SDimitry Andric           // Leave Darwin logic as-is.
2037*0b57cec5SDimitry Andric           || (!Subtarget.isSVR4ABI() &&
2038*0b57cec5SDimitry Andric               (PPC::CRBITRCRegClass.contains(Reg) ||
2039*0b57cec5SDimitry Andric                PPC::CRRCRegClass.contains(Reg)))) {
2040*0b57cec5SDimitry Andric         int FI = CSI[i].getFrameIdx();
2041*0b57cec5SDimitry Andric 
2042*0b57cec5SDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
2043*0b57cec5SDimitry Andric       }
2044*0b57cec5SDimitry Andric     }
2045*0b57cec5SDimitry Andric 
2046*0b57cec5SDimitry Andric     LowerBound -= 4; // The CR save area is always 4 bytes long.
2047*0b57cec5SDimitry Andric   }
2048*0b57cec5SDimitry Andric 
2049*0b57cec5SDimitry Andric   if (HasVRSAVESaveArea) {
2050*0b57cec5SDimitry Andric     // FIXME SVR4: Is it actually possible to have multiple elements in CSI
2051*0b57cec5SDimitry Andric     //             which have the VRSAVE register class?
2052*0b57cec5SDimitry Andric     // Adjust the frame index of the VRSAVE spill slot.
2053*0b57cec5SDimitry Andric     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2054*0b57cec5SDimitry Andric       unsigned Reg = CSI[i].getReg();
2055*0b57cec5SDimitry Andric 
2056*0b57cec5SDimitry Andric       if (PPC::VRSAVERCRegClass.contains(Reg)) {
2057*0b57cec5SDimitry Andric         int FI = CSI[i].getFrameIdx();
2058*0b57cec5SDimitry Andric 
2059*0b57cec5SDimitry Andric         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
2060*0b57cec5SDimitry Andric       }
2061*0b57cec5SDimitry Andric     }
2062*0b57cec5SDimitry Andric 
2063*0b57cec5SDimitry Andric     LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
2064*0b57cec5SDimitry Andric   }
2065*0b57cec5SDimitry Andric 
2066*0b57cec5SDimitry Andric   // Both Altivec and SPE have the same alignment and padding requirements
2067*0b57cec5SDimitry Andric   // within the stack frame.
2068*0b57cec5SDimitry Andric   if (HasVRSaveArea) {
2069*0b57cec5SDimitry Andric     // Insert alignment padding, we need 16-byte alignment. Note: for positive
2070*0b57cec5SDimitry Andric     // number the alignment formula is : y = (x + (n-1)) & (~(n-1)). But since
2071*0b57cec5SDimitry Andric     // we are using negative number here (the stack grows downward). We should
2072*0b57cec5SDimitry Andric     // use formula : y = x & (~(n-1)). Where x is the size before aligning, n
2073*0b57cec5SDimitry Andric     // is the alignment size ( n = 16 here) and y is the size after aligning.
2074*0b57cec5SDimitry Andric     assert(LowerBound <= 0 && "Expect LowerBound have a non-positive value!");
2075*0b57cec5SDimitry Andric     LowerBound &= ~(15);
2076*0b57cec5SDimitry Andric 
2077*0b57cec5SDimitry Andric     for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
2078*0b57cec5SDimitry Andric       int FI = VRegs[i].getFrameIdx();
2079*0b57cec5SDimitry Andric 
2080*0b57cec5SDimitry Andric       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
2081*0b57cec5SDimitry Andric     }
2082*0b57cec5SDimitry Andric   }
2083*0b57cec5SDimitry Andric 
2084*0b57cec5SDimitry Andric   addScavengingSpillSlot(MF, RS);
2085*0b57cec5SDimitry Andric }
2086*0b57cec5SDimitry Andric 
2087*0b57cec5SDimitry Andric void
2088*0b57cec5SDimitry Andric PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
2089*0b57cec5SDimitry Andric                                          RegScavenger *RS) const {
2090*0b57cec5SDimitry Andric   // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
2091*0b57cec5SDimitry Andric   // a large stack, which will require scavenging a register to materialize a
2092*0b57cec5SDimitry Andric   // large offset.
2093*0b57cec5SDimitry Andric 
2094*0b57cec5SDimitry Andric   // We need to have a scavenger spill slot for spills if the frame size is
2095*0b57cec5SDimitry Andric   // large. In case there is no free register for large-offset addressing,
2096*0b57cec5SDimitry Andric   // this slot is used for the necessary emergency spill. Also, we need the
2097*0b57cec5SDimitry Andric   // slot for dynamic stack allocations.
2098*0b57cec5SDimitry Andric 
2099*0b57cec5SDimitry Andric   // The scavenger might be invoked if the frame offset does not fit into
2100*0b57cec5SDimitry Andric   // the 16-bit immediate. We don't know the complete frame size here
2101*0b57cec5SDimitry Andric   // because we've not yet computed callee-saved register spills or the
2102*0b57cec5SDimitry Andric   // needed alignment padding.
2103*0b57cec5SDimitry Andric   unsigned StackSize = determineFrameLayout(MF, true);
2104*0b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
2105*0b57cec5SDimitry Andric   if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
2106*0b57cec5SDimitry Andric       hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
2107*0b57cec5SDimitry Andric     const TargetRegisterClass &GPRC = PPC::GPRCRegClass;
2108*0b57cec5SDimitry Andric     const TargetRegisterClass &G8RC = PPC::G8RCRegClass;
2109*0b57cec5SDimitry Andric     const TargetRegisterClass &RC = Subtarget.isPPC64() ? G8RC : GPRC;
2110*0b57cec5SDimitry Andric     const TargetRegisterInfo &TRI = *Subtarget.getRegisterInfo();
2111*0b57cec5SDimitry Andric     unsigned Size = TRI.getSpillSize(RC);
2112*0b57cec5SDimitry Andric     unsigned Align = TRI.getSpillAlignment(RC);
2113*0b57cec5SDimitry Andric     RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
2114*0b57cec5SDimitry Andric 
2115*0b57cec5SDimitry Andric     // Might we have over-aligned allocas?
2116*0b57cec5SDimitry Andric     bool HasAlVars = MFI.hasVarSizedObjects() &&
2117*0b57cec5SDimitry Andric                      MFI.getMaxAlignment() > getStackAlignment();
2118*0b57cec5SDimitry Andric 
2119*0b57cec5SDimitry Andric     // These kinds of spills might need two registers.
2120*0b57cec5SDimitry Andric     if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
2121*0b57cec5SDimitry Andric       RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
2122*0b57cec5SDimitry Andric 
2123*0b57cec5SDimitry Andric   }
2124*0b57cec5SDimitry Andric }
2125*0b57cec5SDimitry Andric 
2126*0b57cec5SDimitry Andric // This function checks if a callee saved gpr can be spilled to a volatile
2127*0b57cec5SDimitry Andric // vector register. This occurs for leaf functions when the option
2128*0b57cec5SDimitry Andric // ppc-enable-pe-vector-spills is enabled. If there are any remaining registers
2129*0b57cec5SDimitry Andric // which were not spilled to vectors, return false so the target independent
2130*0b57cec5SDimitry Andric // code can handle them by assigning a FrameIdx to a stack slot.
2131*0b57cec5SDimitry Andric bool PPCFrameLowering::assignCalleeSavedSpillSlots(
2132*0b57cec5SDimitry Andric     MachineFunction &MF, const TargetRegisterInfo *TRI,
2133*0b57cec5SDimitry Andric     std::vector<CalleeSavedInfo> &CSI) const {
2134*0b57cec5SDimitry Andric 
2135*0b57cec5SDimitry Andric   if (CSI.empty())
2136*0b57cec5SDimitry Andric     return true; // Early exit if no callee saved registers are modified!
2137*0b57cec5SDimitry Andric 
2138*0b57cec5SDimitry Andric   // Early exit if cannot spill gprs to volatile vector registers.
2139*0b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
2140*0b57cec5SDimitry Andric   if (!EnablePEVectorSpills || MFI.hasCalls() || !Subtarget.hasP9Vector())
2141*0b57cec5SDimitry Andric     return false;
2142*0b57cec5SDimitry Andric 
2143*0b57cec5SDimitry Andric   // Build a BitVector of VSRs that can be used for spilling GPRs.
2144*0b57cec5SDimitry Andric   BitVector BVAllocatable = TRI->getAllocatableSet(MF);
2145*0b57cec5SDimitry Andric   BitVector BVCalleeSaved(TRI->getNumRegs());
2146*0b57cec5SDimitry Andric   const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
2147*0b57cec5SDimitry Andric   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
2148*0b57cec5SDimitry Andric   for (unsigned i = 0; CSRegs[i]; ++i)
2149*0b57cec5SDimitry Andric     BVCalleeSaved.set(CSRegs[i]);
2150*0b57cec5SDimitry Andric 
2151*0b57cec5SDimitry Andric   for (unsigned Reg : BVAllocatable.set_bits()) {
2152*0b57cec5SDimitry Andric     // Set to 0 if the register is not a volatile VF/F8 register, or if it is
2153*0b57cec5SDimitry Andric     // used in the function.
2154*0b57cec5SDimitry Andric     if (BVCalleeSaved[Reg] ||
2155*0b57cec5SDimitry Andric         (!PPC::F8RCRegClass.contains(Reg) &&
2156*0b57cec5SDimitry Andric          !PPC::VFRCRegClass.contains(Reg)) ||
2157*0b57cec5SDimitry Andric         (MF.getRegInfo().isPhysRegUsed(Reg)))
2158*0b57cec5SDimitry Andric       BVAllocatable.reset(Reg);
2159*0b57cec5SDimitry Andric   }
2160*0b57cec5SDimitry Andric 
2161*0b57cec5SDimitry Andric   bool AllSpilledToReg = true;
2162*0b57cec5SDimitry Andric   for (auto &CS : CSI) {
2163*0b57cec5SDimitry Andric     if (BVAllocatable.none())
2164*0b57cec5SDimitry Andric       return false;
2165*0b57cec5SDimitry Andric 
2166*0b57cec5SDimitry Andric     unsigned Reg = CS.getReg();
2167*0b57cec5SDimitry Andric     if (!PPC::G8RCRegClass.contains(Reg) && !PPC::GPRCRegClass.contains(Reg)) {
2168*0b57cec5SDimitry Andric       AllSpilledToReg = false;
2169*0b57cec5SDimitry Andric       continue;
2170*0b57cec5SDimitry Andric     }
2171*0b57cec5SDimitry Andric 
2172*0b57cec5SDimitry Andric     unsigned VolatileVFReg = BVAllocatable.find_first();
2173*0b57cec5SDimitry Andric     if (VolatileVFReg < BVAllocatable.size()) {
2174*0b57cec5SDimitry Andric       CS.setDstReg(VolatileVFReg);
2175*0b57cec5SDimitry Andric       BVAllocatable.reset(VolatileVFReg);
2176*0b57cec5SDimitry Andric     } else {
2177*0b57cec5SDimitry Andric       AllSpilledToReg = false;
2178*0b57cec5SDimitry Andric     }
2179*0b57cec5SDimitry Andric   }
2180*0b57cec5SDimitry Andric   return AllSpilledToReg;
2181*0b57cec5SDimitry Andric }
2182*0b57cec5SDimitry Andric 
2183*0b57cec5SDimitry Andric 
2184*0b57cec5SDimitry Andric bool
2185*0b57cec5SDimitry Andric PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
2186*0b57cec5SDimitry Andric                                      MachineBasicBlock::iterator MI,
2187*0b57cec5SDimitry Andric                                      const std::vector<CalleeSavedInfo> &CSI,
2188*0b57cec5SDimitry Andric                                      const TargetRegisterInfo *TRI) const {
2189*0b57cec5SDimitry Andric 
2190*0b57cec5SDimitry Andric   // Currently, this function only handles SVR4 32- and 64-bit ABIs.
2191*0b57cec5SDimitry Andric   // Return false otherwise to maintain pre-existing behavior.
2192*0b57cec5SDimitry Andric   if (!Subtarget.isSVR4ABI())
2193*0b57cec5SDimitry Andric     return false;
2194*0b57cec5SDimitry Andric 
2195*0b57cec5SDimitry Andric   MachineFunction *MF = MBB.getParent();
2196*0b57cec5SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
2197*0b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
2198*0b57cec5SDimitry Andric   bool MustSaveTOC = FI->mustSaveTOC();
2199*0b57cec5SDimitry Andric   DebugLoc DL;
2200*0b57cec5SDimitry Andric   bool CRSpilled = false;
2201*0b57cec5SDimitry Andric   MachineInstrBuilder CRMIB;
2202*0b57cec5SDimitry Andric 
2203*0b57cec5SDimitry Andric   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2204*0b57cec5SDimitry Andric     unsigned Reg = CSI[i].getReg();
2205*0b57cec5SDimitry Andric     // Only Darwin actually uses the VRSAVE register, but it can still appear
2206*0b57cec5SDimitry Andric     // here if, for example, @llvm.eh.unwind.init() is used.  If we're not on
2207*0b57cec5SDimitry Andric     // Darwin, ignore it.
2208*0b57cec5SDimitry Andric     if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
2209*0b57cec5SDimitry Andric       continue;
2210*0b57cec5SDimitry Andric 
2211*0b57cec5SDimitry Andric     // CR2 through CR4 are the nonvolatile CR fields.
2212*0b57cec5SDimitry Andric     bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
2213*0b57cec5SDimitry Andric 
2214*0b57cec5SDimitry Andric     // Add the callee-saved register as live-in; it's killed at the spill.
2215*0b57cec5SDimitry Andric     // Do not do this for callee-saved registers that are live-in to the
2216*0b57cec5SDimitry Andric     // function because they will already be marked live-in and this will be
2217*0b57cec5SDimitry Andric     // adding it for a second time. It is an error to add the same register
2218*0b57cec5SDimitry Andric     // to the set more than once.
2219*0b57cec5SDimitry Andric     const MachineRegisterInfo &MRI = MF->getRegInfo();
2220*0b57cec5SDimitry Andric     bool IsLiveIn = MRI.isLiveIn(Reg);
2221*0b57cec5SDimitry Andric     if (!IsLiveIn)
2222*0b57cec5SDimitry Andric        MBB.addLiveIn(Reg);
2223*0b57cec5SDimitry Andric 
2224*0b57cec5SDimitry Andric     if (CRSpilled && IsCRField) {
2225*0b57cec5SDimitry Andric       CRMIB.addReg(Reg, RegState::ImplicitKill);
2226*0b57cec5SDimitry Andric       continue;
2227*0b57cec5SDimitry Andric     }
2228*0b57cec5SDimitry Andric 
2229*0b57cec5SDimitry Andric     // The actual spill will happen in the prologue.
2230*0b57cec5SDimitry Andric     if ((Reg == PPC::X2 || Reg == PPC::R2) && MustSaveTOC)
2231*0b57cec5SDimitry Andric       continue;
2232*0b57cec5SDimitry Andric 
2233*0b57cec5SDimitry Andric     // Insert the spill to the stack frame.
2234*0b57cec5SDimitry Andric     if (IsCRField) {
2235*0b57cec5SDimitry Andric       PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
2236*0b57cec5SDimitry Andric       if (Subtarget.isPPC64()) {
2237*0b57cec5SDimitry Andric         // The actual spill will happen at the start of the prologue.
2238*0b57cec5SDimitry Andric         FuncInfo->addMustSaveCR(Reg);
2239*0b57cec5SDimitry Andric       } else {
2240*0b57cec5SDimitry Andric         CRSpilled = true;
2241*0b57cec5SDimitry Andric         FuncInfo->setSpillsCR();
2242*0b57cec5SDimitry Andric 
2243*0b57cec5SDimitry Andric         // 32-bit:  FP-relative.  Note that we made sure CR2-CR4 all have
2244*0b57cec5SDimitry Andric         // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
2245*0b57cec5SDimitry Andric         CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
2246*0b57cec5SDimitry Andric                   .addReg(Reg, RegState::ImplicitKill);
2247*0b57cec5SDimitry Andric 
2248*0b57cec5SDimitry Andric         MBB.insert(MI, CRMIB);
2249*0b57cec5SDimitry Andric         MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
2250*0b57cec5SDimitry Andric                                          .addReg(PPC::R12,
2251*0b57cec5SDimitry Andric                                                  getKillRegState(true)),
2252*0b57cec5SDimitry Andric                                          CSI[i].getFrameIdx()));
2253*0b57cec5SDimitry Andric       }
2254*0b57cec5SDimitry Andric     } else {
2255*0b57cec5SDimitry Andric       if (CSI[i].isSpilledToReg()) {
2256*0b57cec5SDimitry Andric         NumPESpillVSR++;
2257*0b57cec5SDimitry Andric         BuildMI(MBB, MI, DL, TII.get(PPC::MTVSRD), CSI[i].getDstReg())
2258*0b57cec5SDimitry Andric           .addReg(Reg, getKillRegState(true));
2259*0b57cec5SDimitry Andric       } else {
2260*0b57cec5SDimitry Andric         const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2261*0b57cec5SDimitry Andric         // Use !IsLiveIn for the kill flag.
2262*0b57cec5SDimitry Andric         // We do not want to kill registers that are live in this function
2263*0b57cec5SDimitry Andric         // before their use because they will become undefined registers.
2264*0b57cec5SDimitry Andric         TII.storeRegToStackSlot(MBB, MI, Reg, !IsLiveIn,
2265*0b57cec5SDimitry Andric                                 CSI[i].getFrameIdx(), RC, TRI);
2266*0b57cec5SDimitry Andric       }
2267*0b57cec5SDimitry Andric     }
2268*0b57cec5SDimitry Andric   }
2269*0b57cec5SDimitry Andric   return true;
2270*0b57cec5SDimitry Andric }
2271*0b57cec5SDimitry Andric 
2272*0b57cec5SDimitry Andric static void
2273*0b57cec5SDimitry Andric restoreCRs(bool isPPC64, bool is31,
2274*0b57cec5SDimitry Andric            bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
2275*0b57cec5SDimitry Andric            MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
2276*0b57cec5SDimitry Andric            const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
2277*0b57cec5SDimitry Andric 
2278*0b57cec5SDimitry Andric   MachineFunction *MF = MBB.getParent();
2279*0b57cec5SDimitry Andric   const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
2280*0b57cec5SDimitry Andric   DebugLoc DL;
2281*0b57cec5SDimitry Andric   unsigned RestoreOp, MoveReg;
2282*0b57cec5SDimitry Andric 
2283*0b57cec5SDimitry Andric   if (isPPC64)
2284*0b57cec5SDimitry Andric     // This is handled during epilogue generation.
2285*0b57cec5SDimitry Andric     return;
2286*0b57cec5SDimitry Andric   else {
2287*0b57cec5SDimitry Andric     // 32-bit:  FP-relative
2288*0b57cec5SDimitry Andric     MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
2289*0b57cec5SDimitry Andric                                              PPC::R12),
2290*0b57cec5SDimitry Andric                                      CSI[CSIIndex].getFrameIdx()));
2291*0b57cec5SDimitry Andric     RestoreOp = PPC::MTOCRF;
2292*0b57cec5SDimitry Andric     MoveReg = PPC::R12;
2293*0b57cec5SDimitry Andric   }
2294*0b57cec5SDimitry Andric 
2295*0b57cec5SDimitry Andric   if (CR2Spilled)
2296*0b57cec5SDimitry Andric     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
2297*0b57cec5SDimitry Andric                .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
2298*0b57cec5SDimitry Andric 
2299*0b57cec5SDimitry Andric   if (CR3Spilled)
2300*0b57cec5SDimitry Andric     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
2301*0b57cec5SDimitry Andric                .addReg(MoveReg, getKillRegState(!CR4Spilled)));
2302*0b57cec5SDimitry Andric 
2303*0b57cec5SDimitry Andric   if (CR4Spilled)
2304*0b57cec5SDimitry Andric     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
2305*0b57cec5SDimitry Andric                .addReg(MoveReg, getKillRegState(true)));
2306*0b57cec5SDimitry Andric }
2307*0b57cec5SDimitry Andric 
2308*0b57cec5SDimitry Andric MachineBasicBlock::iterator PPCFrameLowering::
2309*0b57cec5SDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
2310*0b57cec5SDimitry Andric                               MachineBasicBlock::iterator I) const {
2311*0b57cec5SDimitry Andric   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
2312*0b57cec5SDimitry Andric   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
2313*0b57cec5SDimitry Andric       I->getOpcode() == PPC::ADJCALLSTACKUP) {
2314*0b57cec5SDimitry Andric     // Add (actually subtract) back the amount the callee popped on return.
2315*0b57cec5SDimitry Andric     if (int CalleeAmt =  I->getOperand(1).getImm()) {
2316*0b57cec5SDimitry Andric       bool is64Bit = Subtarget.isPPC64();
2317*0b57cec5SDimitry Andric       CalleeAmt *= -1;
2318*0b57cec5SDimitry Andric       unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
2319*0b57cec5SDimitry Andric       unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
2320*0b57cec5SDimitry Andric       unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
2321*0b57cec5SDimitry Andric       unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
2322*0b57cec5SDimitry Andric       unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
2323*0b57cec5SDimitry Andric       unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
2324*0b57cec5SDimitry Andric       const DebugLoc &dl = I->getDebugLoc();
2325*0b57cec5SDimitry Andric 
2326*0b57cec5SDimitry Andric       if (isInt<16>(CalleeAmt)) {
2327*0b57cec5SDimitry Andric         BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
2328*0b57cec5SDimitry Andric           .addReg(StackReg, RegState::Kill)
2329*0b57cec5SDimitry Andric           .addImm(CalleeAmt);
2330*0b57cec5SDimitry Andric       } else {
2331*0b57cec5SDimitry Andric         MachineBasicBlock::iterator MBBI = I;
2332*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
2333*0b57cec5SDimitry Andric           .addImm(CalleeAmt >> 16);
2334*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
2335*0b57cec5SDimitry Andric           .addReg(TmpReg, RegState::Kill)
2336*0b57cec5SDimitry Andric           .addImm(CalleeAmt & 0xFFFF);
2337*0b57cec5SDimitry Andric         BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
2338*0b57cec5SDimitry Andric           .addReg(StackReg, RegState::Kill)
2339*0b57cec5SDimitry Andric           .addReg(TmpReg);
2340*0b57cec5SDimitry Andric       }
2341*0b57cec5SDimitry Andric     }
2342*0b57cec5SDimitry Andric   }
2343*0b57cec5SDimitry Andric   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
2344*0b57cec5SDimitry Andric   return MBB.erase(I);
2345*0b57cec5SDimitry Andric }
2346*0b57cec5SDimitry Andric 
2347*0b57cec5SDimitry Andric bool
2348*0b57cec5SDimitry Andric PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
2349*0b57cec5SDimitry Andric                                         MachineBasicBlock::iterator MI,
2350*0b57cec5SDimitry Andric                                         std::vector<CalleeSavedInfo> &CSI,
2351*0b57cec5SDimitry Andric                                         const TargetRegisterInfo *TRI) const {
2352*0b57cec5SDimitry Andric 
2353*0b57cec5SDimitry Andric   // Currently, this function only handles SVR4 32- and 64-bit ABIs.
2354*0b57cec5SDimitry Andric   // Return false otherwise to maintain pre-existing behavior.
2355*0b57cec5SDimitry Andric   if (!Subtarget.isSVR4ABI())
2356*0b57cec5SDimitry Andric     return false;
2357*0b57cec5SDimitry Andric 
2358*0b57cec5SDimitry Andric   MachineFunction *MF = MBB.getParent();
2359*0b57cec5SDimitry Andric   const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
2360*0b57cec5SDimitry Andric   PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
2361*0b57cec5SDimitry Andric   bool MustSaveTOC = FI->mustSaveTOC();
2362*0b57cec5SDimitry Andric   bool CR2Spilled = false;
2363*0b57cec5SDimitry Andric   bool CR3Spilled = false;
2364*0b57cec5SDimitry Andric   bool CR4Spilled = false;
2365*0b57cec5SDimitry Andric   unsigned CSIIndex = 0;
2366*0b57cec5SDimitry Andric 
2367*0b57cec5SDimitry Andric   // Initialize insertion-point logic; we will be restoring in reverse
2368*0b57cec5SDimitry Andric   // order of spill.
2369*0b57cec5SDimitry Andric   MachineBasicBlock::iterator I = MI, BeforeI = I;
2370*0b57cec5SDimitry Andric   bool AtStart = I == MBB.begin();
2371*0b57cec5SDimitry Andric 
2372*0b57cec5SDimitry Andric   if (!AtStart)
2373*0b57cec5SDimitry Andric     --BeforeI;
2374*0b57cec5SDimitry Andric 
2375*0b57cec5SDimitry Andric   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2376*0b57cec5SDimitry Andric     unsigned Reg = CSI[i].getReg();
2377*0b57cec5SDimitry Andric 
2378*0b57cec5SDimitry Andric     // Only Darwin actually uses the VRSAVE register, but it can still appear
2379*0b57cec5SDimitry Andric     // here if, for example, @llvm.eh.unwind.init() is used.  If we're not on
2380*0b57cec5SDimitry Andric     // Darwin, ignore it.
2381*0b57cec5SDimitry Andric     if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
2382*0b57cec5SDimitry Andric       continue;
2383*0b57cec5SDimitry Andric 
2384*0b57cec5SDimitry Andric     if ((Reg == PPC::X2 || Reg == PPC::R2) && MustSaveTOC)
2385*0b57cec5SDimitry Andric       continue;
2386*0b57cec5SDimitry Andric 
2387*0b57cec5SDimitry Andric     if (Reg == PPC::CR2) {
2388*0b57cec5SDimitry Andric       CR2Spilled = true;
2389*0b57cec5SDimitry Andric       // The spill slot is associated only with CR2, which is the
2390*0b57cec5SDimitry Andric       // first nonvolatile spilled.  Save it here.
2391*0b57cec5SDimitry Andric       CSIIndex = i;
2392*0b57cec5SDimitry Andric       continue;
2393*0b57cec5SDimitry Andric     } else if (Reg == PPC::CR3) {
2394*0b57cec5SDimitry Andric       CR3Spilled = true;
2395*0b57cec5SDimitry Andric       continue;
2396*0b57cec5SDimitry Andric     } else if (Reg == PPC::CR4) {
2397*0b57cec5SDimitry Andric       CR4Spilled = true;
2398*0b57cec5SDimitry Andric       continue;
2399*0b57cec5SDimitry Andric     } else {
2400*0b57cec5SDimitry Andric       // When we first encounter a non-CR register after seeing at
2401*0b57cec5SDimitry Andric       // least one CR register, restore all spilled CRs together.
2402*0b57cec5SDimitry Andric       if ((CR2Spilled || CR3Spilled || CR4Spilled)
2403*0b57cec5SDimitry Andric           && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
2404*0b57cec5SDimitry Andric         bool is31 = needsFP(*MF);
2405*0b57cec5SDimitry Andric         restoreCRs(Subtarget.isPPC64(), is31,
2406*0b57cec5SDimitry Andric                    CR2Spilled, CR3Spilled, CR4Spilled,
2407*0b57cec5SDimitry Andric                    MBB, I, CSI, CSIIndex);
2408*0b57cec5SDimitry Andric         CR2Spilled = CR3Spilled = CR4Spilled = false;
2409*0b57cec5SDimitry Andric       }
2410*0b57cec5SDimitry Andric 
2411*0b57cec5SDimitry Andric       if (CSI[i].isSpilledToReg()) {
2412*0b57cec5SDimitry Andric         DebugLoc DL;
2413*0b57cec5SDimitry Andric         NumPEReloadVSR++;
2414*0b57cec5SDimitry Andric         BuildMI(MBB, I, DL, TII.get(PPC::MFVSRD), Reg)
2415*0b57cec5SDimitry Andric             .addReg(CSI[i].getDstReg(), getKillRegState(true));
2416*0b57cec5SDimitry Andric       } else {
2417*0b57cec5SDimitry Andric        // Default behavior for non-CR saves.
2418*0b57cec5SDimitry Andric         const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
2419*0b57cec5SDimitry Andric         TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(), RC, TRI);
2420*0b57cec5SDimitry Andric         assert(I != MBB.begin() &&
2421*0b57cec5SDimitry Andric                "loadRegFromStackSlot didn't insert any code!");
2422*0b57cec5SDimitry Andric       }
2423*0b57cec5SDimitry Andric     }
2424*0b57cec5SDimitry Andric 
2425*0b57cec5SDimitry Andric     // Insert in reverse order.
2426*0b57cec5SDimitry Andric     if (AtStart)
2427*0b57cec5SDimitry Andric       I = MBB.begin();
2428*0b57cec5SDimitry Andric     else {
2429*0b57cec5SDimitry Andric       I = BeforeI;
2430*0b57cec5SDimitry Andric       ++I;
2431*0b57cec5SDimitry Andric     }
2432*0b57cec5SDimitry Andric   }
2433*0b57cec5SDimitry Andric 
2434*0b57cec5SDimitry Andric   // If we haven't yet spilled the CRs, do so now.
2435*0b57cec5SDimitry Andric   if (CR2Spilled || CR3Spilled || CR4Spilled) {
2436*0b57cec5SDimitry Andric     bool is31 = needsFP(*MF);
2437*0b57cec5SDimitry Andric     restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
2438*0b57cec5SDimitry Andric                MBB, I, CSI, CSIIndex);
2439*0b57cec5SDimitry Andric   }
2440*0b57cec5SDimitry Andric 
2441*0b57cec5SDimitry Andric   return true;
2442*0b57cec5SDimitry Andric }
2443*0b57cec5SDimitry Andric 
24448bcb0991SDimitry Andric unsigned PPCFrameLowering::getTOCSaveOffset() const {
24458bcb0991SDimitry Andric   if (Subtarget.isAIXABI())
24468bcb0991SDimitry Andric     // TOC save/restore is normally handled by the linker.
24478bcb0991SDimitry Andric     // Indirect calls should hit this limitation.
24488bcb0991SDimitry Andric     report_fatal_error("TOC save is not implemented on AIX yet.");
24498bcb0991SDimitry Andric   return TOCSaveOffset;
24508bcb0991SDimitry Andric }
24518bcb0991SDimitry Andric 
24528bcb0991SDimitry Andric unsigned PPCFrameLowering::getFramePointerSaveOffset() const {
24538bcb0991SDimitry Andric   if (Subtarget.isAIXABI())
24548bcb0991SDimitry Andric     report_fatal_error("FramePointer is not implemented on AIX yet.");
24558bcb0991SDimitry Andric   return FramePointerSaveOffset;
24568bcb0991SDimitry Andric }
24578bcb0991SDimitry Andric 
24588bcb0991SDimitry Andric unsigned PPCFrameLowering::getBasePointerSaveOffset() const {
24598bcb0991SDimitry Andric   if (Subtarget.isAIXABI())
24608bcb0991SDimitry Andric     report_fatal_error("BasePointer is not implemented on AIX yet.");
24618bcb0991SDimitry Andric   return BasePointerSaveOffset;
24628bcb0991SDimitry Andric }
24638bcb0991SDimitry Andric 
2464*0b57cec5SDimitry Andric bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
2465*0b57cec5SDimitry Andric   if (MF.getInfo<PPCFunctionInfo>()->shrinkWrapDisabled())
2466*0b57cec5SDimitry Andric     return false;
2467*0b57cec5SDimitry Andric   return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() &&
2468*0b57cec5SDimitry Andric           MF.getSubtarget<PPCSubtarget>().isPPC64());
2469*0b57cec5SDimitry Andric }
2470