xref: /llvm-project/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp (revision 97a189c7169cfaf927c32b3870675ae161970e09)
1 //===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the PPC implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PPCFrameLowering.h"
15 #include "PPCInstrBuilder.h"
16 #include "PPCInstrInfo.h"
17 #include "PPCMachineFunctionInfo.h"
18 #include "PPCSubtarget.h"
19 #include "PPCTargetMachine.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/RegisterScavenging.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Target/TargetOptions.h"
28 
29 using namespace llvm;
30 
31 /// VRRegNo - Map from a numbered VR register to its enum value.
32 ///
33 static const MCPhysReg VRRegNo[] = {
34  PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
35  PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
36  PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
37  PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
38 };
39 
40 static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) {
41   if (STI.isDarwinABI())
42     return STI.isPPC64() ? 16 : 8;
43   // SVR4 ABI:
44   return STI.isPPC64() ? 16 : 4;
45 }
46 
47 static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) {
48   return STI.isELFv2ABI() ? 24 : 40;
49 }
50 
51 static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) {
52   // For the Darwin ABI:
53   // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
54   // for saving the frame pointer (if needed.)  While the published ABI has
55   // not used this slot since at least MacOSX 10.2, there is older code
56   // around that does use it, and that needs to continue to work.
57   if (STI.isDarwinABI())
58     return STI.isPPC64() ? -8U : -4U;
59 
60   // SVR4 ABI: First slot in the general register save area.
61   return STI.isPPC64() ? -8U : -4U;
62 }
63 
64 static unsigned computeLinkageSize(const PPCSubtarget &STI) {
65   if (STI.isDarwinABI() || STI.isPPC64())
66     return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4);
67 
68   // SVR4 ABI:
69   return 8;
70 }
71 
72 static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) {
73   if (STI.isDarwinABI())
74     return STI.isPPC64() ? -16U : -8U;
75 
76   // SVR4 ABI: First slot in the general register save area.
77   return STI.isPPC64()
78              ? -16U
79              : STI.getTargetMachine().isPositionIndependent() ? -12U : -8U;
80 }
81 
82 PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI)
83     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
84                           STI.getPlatformStackAlignment(), 0),
85       Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)),
86       TOCSaveOffset(computeTOCSaveOffset(Subtarget)),
87       FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)),
88       LinkageSize(computeLinkageSize(Subtarget)),
89       BasePointerSaveOffset(computeBasePointerSaveOffset(STI)) {}
90 
91 // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
92 const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots(
93     unsigned &NumEntries) const {
94   if (Subtarget.isDarwinABI()) {
95     NumEntries = 1;
96     if (Subtarget.isPPC64()) {
97       static const SpillSlot darwin64Offsets = {PPC::X31, -8};
98       return &darwin64Offsets;
99     } else {
100       static const SpillSlot darwinOffsets = {PPC::R31, -4};
101       return &darwinOffsets;
102     }
103   }
104 
105   // Early exit if not using the SVR4 ABI.
106   if (!Subtarget.isSVR4ABI()) {
107     NumEntries = 0;
108     return nullptr;
109   }
110 
111   // Note that the offsets here overlap, but this is fixed up in
112   // processFunctionBeforeFrameFinalized.
113 
114   static const SpillSlot Offsets[] = {
115       // Floating-point register save area offsets.
116       {PPC::F31, -8},
117       {PPC::F30, -16},
118       {PPC::F29, -24},
119       {PPC::F28, -32},
120       {PPC::F27, -40},
121       {PPC::F26, -48},
122       {PPC::F25, -56},
123       {PPC::F24, -64},
124       {PPC::F23, -72},
125       {PPC::F22, -80},
126       {PPC::F21, -88},
127       {PPC::F20, -96},
128       {PPC::F19, -104},
129       {PPC::F18, -112},
130       {PPC::F17, -120},
131       {PPC::F16, -128},
132       {PPC::F15, -136},
133       {PPC::F14, -144},
134 
135       // General register save area offsets.
136       {PPC::R31, -4},
137       {PPC::R30, -8},
138       {PPC::R29, -12},
139       {PPC::R28, -16},
140       {PPC::R27, -20},
141       {PPC::R26, -24},
142       {PPC::R25, -28},
143       {PPC::R24, -32},
144       {PPC::R23, -36},
145       {PPC::R22, -40},
146       {PPC::R21, -44},
147       {PPC::R20, -48},
148       {PPC::R19, -52},
149       {PPC::R18, -56},
150       {PPC::R17, -60},
151       {PPC::R16, -64},
152       {PPC::R15, -68},
153       {PPC::R14, -72},
154 
155       // CR save area offset.  We map each of the nonvolatile CR fields
156       // to the slot for CR2, which is the first of the nonvolatile CR
157       // fields to be assigned, so that we only allocate one save slot.
158       // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
159       {PPC::CR2, -4},
160 
161       // VRSAVE save area offset.
162       {PPC::VRSAVE, -4},
163 
164       // Vector register save area
165       {PPC::V31, -16},
166       {PPC::V30, -32},
167       {PPC::V29, -48},
168       {PPC::V28, -64},
169       {PPC::V27, -80},
170       {PPC::V26, -96},
171       {PPC::V25, -112},
172       {PPC::V24, -128},
173       {PPC::V23, -144},
174       {PPC::V22, -160},
175       {PPC::V21, -176},
176       {PPC::V20, -192}};
177 
178   static const SpillSlot Offsets64[] = {
179       // Floating-point register save area offsets.
180       {PPC::F31, -8},
181       {PPC::F30, -16},
182       {PPC::F29, -24},
183       {PPC::F28, -32},
184       {PPC::F27, -40},
185       {PPC::F26, -48},
186       {PPC::F25, -56},
187       {PPC::F24, -64},
188       {PPC::F23, -72},
189       {PPC::F22, -80},
190       {PPC::F21, -88},
191       {PPC::F20, -96},
192       {PPC::F19, -104},
193       {PPC::F18, -112},
194       {PPC::F17, -120},
195       {PPC::F16, -128},
196       {PPC::F15, -136},
197       {PPC::F14, -144},
198 
199       // General register save area offsets.
200       {PPC::X31, -8},
201       {PPC::X30, -16},
202       {PPC::X29, -24},
203       {PPC::X28, -32},
204       {PPC::X27, -40},
205       {PPC::X26, -48},
206       {PPC::X25, -56},
207       {PPC::X24, -64},
208       {PPC::X23, -72},
209       {PPC::X22, -80},
210       {PPC::X21, -88},
211       {PPC::X20, -96},
212       {PPC::X19, -104},
213       {PPC::X18, -112},
214       {PPC::X17, -120},
215       {PPC::X16, -128},
216       {PPC::X15, -136},
217       {PPC::X14, -144},
218 
219       // VRSAVE save area offset.
220       {PPC::VRSAVE, -4},
221 
222       // Vector register save area
223       {PPC::V31, -16},
224       {PPC::V30, -32},
225       {PPC::V29, -48},
226       {PPC::V28, -64},
227       {PPC::V27, -80},
228       {PPC::V26, -96},
229       {PPC::V25, -112},
230       {PPC::V24, -128},
231       {PPC::V23, -144},
232       {PPC::V22, -160},
233       {PPC::V21, -176},
234       {PPC::V20, -192}};
235 
236   if (Subtarget.isPPC64()) {
237     NumEntries = array_lengthof(Offsets64);
238 
239     return Offsets64;
240   } else {
241     NumEntries = array_lengthof(Offsets);
242 
243     return Offsets;
244   }
245 }
246 
247 /// RemoveVRSaveCode - We have found that this function does not need any code
248 /// to manipulate the VRSAVE register, even though it uses vector registers.
249 /// This can happen when the only registers used are known to be live in or out
250 /// of the function.  Remove all of the VRSAVE related code from the function.
251 /// FIXME: The removal of the code results in a compile failure at -O0 when the
252 /// function contains a function call, as the GPR containing original VRSAVE
253 /// contents is spilled and reloaded around the call.  Without the prolog code,
254 /// the spill instruction refers to an undefined register.  This code needs
255 /// to account for all uses of that GPR.
256 static void RemoveVRSaveCode(MachineInstr &MI) {
257   MachineBasicBlock *Entry = MI.getParent();
258   MachineFunction *MF = Entry->getParent();
259 
260   // We know that the MTVRSAVE instruction immediately follows MI.  Remove it.
261   MachineBasicBlock::iterator MBBI = MI;
262   ++MBBI;
263   assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
264   MBBI->eraseFromParent();
265 
266   bool RemovedAllMTVRSAVEs = true;
267   // See if we can find and remove the MTVRSAVE instruction from all of the
268   // epilog blocks.
269   for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
270     // If last instruction is a return instruction, add an epilogue
271     if (I->isReturnBlock()) {
272       bool FoundIt = false;
273       for (MBBI = I->end(); MBBI != I->begin(); ) {
274         --MBBI;
275         if (MBBI->getOpcode() == PPC::MTVRSAVE) {
276           MBBI->eraseFromParent();  // remove it.
277           FoundIt = true;
278           break;
279         }
280       }
281       RemovedAllMTVRSAVEs &= FoundIt;
282     }
283   }
284 
285   // If we found and removed all MTVRSAVE instructions, remove the read of
286   // VRSAVE as well.
287   if (RemovedAllMTVRSAVEs) {
288     MBBI = MI;
289     assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
290     --MBBI;
291     assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
292     MBBI->eraseFromParent();
293   }
294 
295   // Finally, nuke the UPDATE_VRSAVE.
296   MI.eraseFromParent();
297 }
298 
299 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
300 // instruction selector.  Based on the vector registers that have been used,
301 // transform this into the appropriate ORI instruction.
302 static void HandleVRSaveUpdate(MachineInstr &MI, const TargetInstrInfo &TII) {
303   MachineFunction *MF = MI.getParent()->getParent();
304   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
305   DebugLoc dl = MI.getDebugLoc();
306 
307   const MachineRegisterInfo &MRI = MF->getRegInfo();
308   unsigned UsedRegMask = 0;
309   for (unsigned i = 0; i != 32; ++i)
310     if (MRI.isPhysRegModified(VRRegNo[i]))
311       UsedRegMask |= 1 << (31-i);
312 
313   // Live in and live out values already must be in the mask, so don't bother
314   // marking them.
315   for (MachineRegisterInfo::livein_iterator
316        I = MF->getRegInfo().livein_begin(),
317        E = MF->getRegInfo().livein_end(); I != E; ++I) {
318     unsigned RegNo = TRI->getEncodingValue(I->first);
319     if (VRRegNo[RegNo] == I->first)        // If this really is a vector reg.
320       UsedRegMask &= ~(1 << (31-RegNo));   // Doesn't need to be marked.
321   }
322 
323   // Live out registers appear as use operands on return instructions.
324   for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end();
325        UsedRegMask != 0 && BI != BE; ++BI) {
326     const MachineBasicBlock &MBB = *BI;
327     if (!MBB.isReturnBlock())
328       continue;
329     const MachineInstr &Ret = MBB.back();
330     for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) {
331       const MachineOperand &MO = Ret.getOperand(I);
332       if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg()))
333         continue;
334       unsigned RegNo = TRI->getEncodingValue(MO.getReg());
335       UsedRegMask &= ~(1 << (31-RegNo));
336     }
337   }
338 
339   // If no registers are used, turn this into a copy.
340   if (UsedRegMask == 0) {
341     // Remove all VRSAVE code.
342     RemoveVRSaveCode(MI);
343     return;
344   }
345 
346   unsigned SrcReg = MI.getOperand(1).getReg();
347   unsigned DstReg = MI.getOperand(0).getReg();
348 
349   if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
350     if (DstReg != SrcReg)
351       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
352           .addReg(SrcReg)
353           .addImm(UsedRegMask);
354     else
355       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
356           .addReg(SrcReg, RegState::Kill)
357           .addImm(UsedRegMask);
358   } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
359     if (DstReg != SrcReg)
360       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
361           .addReg(SrcReg)
362           .addImm(UsedRegMask >> 16);
363     else
364       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
365           .addReg(SrcReg, RegState::Kill)
366           .addImm(UsedRegMask >> 16);
367   } else {
368     if (DstReg != SrcReg)
369       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
370           .addReg(SrcReg)
371           .addImm(UsedRegMask >> 16);
372     else
373       BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
374           .addReg(SrcReg, RegState::Kill)
375           .addImm(UsedRegMask >> 16);
376 
377     BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
378         .addReg(DstReg, RegState::Kill)
379         .addImm(UsedRegMask & 0xFFFF);
380   }
381 
382   // Remove the old UPDATE_VRSAVE instruction.
383   MI.eraseFromParent();
384 }
385 
386 static bool spillsCR(const MachineFunction &MF) {
387   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
388   return FuncInfo->isCRSpilled();
389 }
390 
391 static bool spillsVRSAVE(const MachineFunction &MF) {
392   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
393   return FuncInfo->isVRSAVESpilled();
394 }
395 
396 static bool hasSpills(const MachineFunction &MF) {
397   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
398   return FuncInfo->hasSpills();
399 }
400 
401 static bool hasNonRISpills(const MachineFunction &MF) {
402   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
403   return FuncInfo->hasNonRISpills();
404 }
405 
406 /// MustSaveLR - Return true if this function requires that we save the LR
407 /// register onto the stack in the prolog and restore it in the epilog of the
408 /// function.
409 static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
410   const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
411 
412   // We need a save/restore of LR if there is any def of LR (which is
413   // defined by calls, including the PIC setup sequence), or if there is
414   // some use of the LR stack slot (e.g. for builtin_return_address).
415   // (LR comes in 32 and 64 bit versions.)
416   MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
417   return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
418 }
419 
420 /// determineFrameLayout - Determine the size of the frame and maximum call
421 /// frame size.
422 unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF,
423                                                 bool UpdateMF,
424                                                 bool UseEstimate) const {
425   MachineFrameInfo &MFI = MF.getFrameInfo();
426 
427   // Get the number of bytes to allocate from the FrameInfo
428   unsigned FrameSize =
429     UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize();
430 
431   // Get stack alignments. The frame must be aligned to the greatest of these:
432   unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI
433   unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame
434   unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1;
435 
436   const PPCRegisterInfo *RegInfo =
437       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
438 
439   // If we are a leaf function, and use up to 224 bytes of stack space,
440   // don't have a frame pointer, calls, or dynamic alloca then we do not need
441   // to adjust the stack pointer (we fit in the Red Zone).
442   // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate
443   // stackless code if all local vars are reg-allocated.
444   bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone);
445   unsigned LR = RegInfo->getRARegister();
446   if (!DisableRedZone &&
447       (Subtarget.isPPC64() ||                      // 32-bit SVR4, no stack-
448        !Subtarget.isSVR4ABI() ||                   //   allocated locals.
449         FrameSize == 0) &&
450       FrameSize <= 224 &&                          // Fits in red zone.
451       !MFI.hasVarSizedObjects() &&                 // No dynamic alloca.
452       !MFI.adjustsStack() &&                       // No calls.
453       !MustSaveLR(MF, LR) &&
454       !RegInfo->hasBasePointer(MF)) { // No special alignment.
455     // No need for frame
456     if (UpdateMF)
457       MFI.setStackSize(0);
458     return 0;
459   }
460 
461   // Get the maximum call frame size of all the calls.
462   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
463 
464   // Maximum call frame needs to be at least big enough for linkage area.
465   unsigned minCallFrameSize = getLinkageSize();
466   maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
467 
468   // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
469   // that allocations will be aligned.
470   if (MFI.hasVarSizedObjects())
471     maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
472 
473   // Update maximum call frame size.
474   if (UpdateMF)
475     MFI.setMaxCallFrameSize(maxCallFrameSize);
476 
477   // Include call frame size in total.
478   FrameSize += maxCallFrameSize;
479 
480   // Make sure the frame is aligned.
481   FrameSize = (FrameSize + AlignMask) & ~AlignMask;
482 
483   // Update frame info.
484   if (UpdateMF)
485     MFI.setStackSize(FrameSize);
486 
487   return FrameSize;
488 }
489 
490 // hasFP - Return true if the specified function actually has a dedicated frame
491 // pointer register.
492 bool PPCFrameLowering::hasFP(const MachineFunction &MF) const {
493   const MachineFrameInfo &MFI = MF.getFrameInfo();
494   // FIXME: This is pretty much broken by design: hasFP() might be called really
495   // early, before the stack layout was calculated and thus hasFP() might return
496   // true or false here depending on the time of call.
497   return (MFI.getStackSize()) && needsFP(MF);
498 }
499 
500 // needsFP - Return true if the specified function should have a dedicated frame
501 // pointer register.  This is true if the function has variable sized allocas or
502 // if frame pointer elimination is disabled.
503 bool PPCFrameLowering::needsFP(const MachineFunction &MF) const {
504   const MachineFrameInfo &MFI = MF.getFrameInfo();
505 
506   // Naked functions have no stack frame pushed, so we don't have a frame
507   // pointer.
508   if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
509     return false;
510 
511   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
512     MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() ||
513     (MF.getTarget().Options.GuaranteedTailCallOpt &&
514      MF.getInfo<PPCFunctionInfo>()->hasFastCall());
515 }
516 
517 void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const {
518   bool is31 = needsFP(MF);
519   unsigned FPReg  = is31 ? PPC::R31 : PPC::R1;
520   unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1;
521 
522   const PPCRegisterInfo *RegInfo =
523       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
524   bool HasBP = RegInfo->hasBasePointer(MF);
525   unsigned BPReg  = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg;
526   unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg;
527 
528   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
529        BI != BE; ++BI)
530     for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) {
531       --MBBI;
532       for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
533         MachineOperand &MO = MBBI->getOperand(I);
534         if (!MO.isReg())
535           continue;
536 
537         switch (MO.getReg()) {
538         case PPC::FP:
539           MO.setReg(FPReg);
540           break;
541         case PPC::FP8:
542           MO.setReg(FP8Reg);
543           break;
544         case PPC::BP:
545           MO.setReg(BPReg);
546           break;
547         case PPC::BP8:
548           MO.setReg(BP8Reg);
549           break;
550 
551         }
552       }
553     }
554 }
555 
556 /*  This function will do the following:
557     - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12
558       respectively (defaults recommended by the ABI) and return true
559     - If MBB is not an entry block, initialize the register scavenger and look
560       for available registers.
561     - If the defaults (R0/R12) are available, return true
562     - If TwoUniqueRegsRequired is set to true, it looks for two unique
563       registers. Otherwise, look for a single available register.
564       - If the required registers are found, set SR1 and SR2 and return true.
565       - If the required registers are not found, set SR2 or both SR1 and SR2 to
566         PPC::NoRegister and return false.
567 
568     Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired
569     is not set, this function will attempt to find two different registers, but
570     still return true if only one register is available (and set SR1 == SR2).
571 */
572 bool
573 PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB,
574                                       bool UseAtEnd,
575                                       bool TwoUniqueRegsRequired,
576                                       unsigned *SR1,
577                                       unsigned *SR2) const {
578   RegScavenger RS;
579   unsigned R0 =  Subtarget.isPPC64() ? PPC::X0 : PPC::R0;
580   unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12;
581 
582   // Set the defaults for the two scratch registers.
583   if (SR1)
584     *SR1 = R0;
585 
586   if (SR2) {
587     assert (SR1 && "Asking for the second scratch register but not the first?");
588     *SR2 = R12;
589   }
590 
591   // If MBB is an entry or exit block, use R0 and R12 as the scratch registers.
592   if ((UseAtEnd && MBB->isReturnBlock()) ||
593       (!UseAtEnd && (&MBB->getParent()->front() == MBB)))
594     return true;
595 
596   RS.enterBasicBlock(*MBB);
597 
598   if (UseAtEnd && !MBB->empty()) {
599     // The scratch register will be used at the end of the block, so must
600     // consider all registers used within the block
601 
602     MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator();
603     // If no terminator, back iterator up to previous instruction.
604     if (MBBI == MBB->end())
605       MBBI = std::prev(MBBI);
606 
607     if (MBBI != MBB->begin())
608       RS.forward(MBBI);
609   }
610 
611   // If the two registers are available, we're all good.
612   // Note that we only return here if both R0 and R12 are available because
613   // although the function may not require two unique registers, it may benefit
614   // from having two so we should try to provide them.
615   if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12))
616     return true;
617 
618   // Get the list of callee-saved registers for the target.
619   const PPCRegisterInfo *RegInfo =
620       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
621   const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent());
622 
623   // Get all the available registers in the block.
624   BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass :
625                                      &PPC::GPRCRegClass);
626 
627   // We shouldn't use callee-saved registers as scratch registers as they may be
628   // available when looking for a candidate block for shrink wrapping but not
629   // available when the actual prologue/epilogue is being emitted because they
630   // were added as live-in to the prologue block by PrologueEpilogueInserter.
631   for (int i = 0; CSRegs[i]; ++i)
632     BV.reset(CSRegs[i]);
633 
634   // Set the first scratch register to the first available one.
635   if (SR1) {
636     int FirstScratchReg = BV.find_first();
637     *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg;
638   }
639 
640   // If there is another one available, set the second scratch register to that.
641   // Otherwise, set it to either PPC::NoRegister if this function requires two
642   // or to whatever SR1 is set to if this function doesn't require two.
643   if (SR2) {
644     int SecondScratchReg = BV.find_next(*SR1);
645     if (SecondScratchReg != -1)
646       *SR2 = SecondScratchReg;
647     else
648       *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1;
649   }
650 
651   // Now that we've done our best to provide both registers, double check
652   // whether we were unable to provide enough.
653   if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U))
654     return false;
655 
656   return true;
657 }
658 
659 // We need a scratch register for spilling LR and for spilling CR. By default,
660 // we use two scratch registers to hide latency. However, if only one scratch
661 // register is available, we can adjust for that by not overlapping the spill
662 // code. However, if we need to realign the stack (i.e. have a base pointer)
663 // and the stack frame is large, we need two scratch registers.
664 bool
665 PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const {
666   const PPCRegisterInfo *RegInfo =
667       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
668   MachineFunction &MF = *(MBB->getParent());
669   bool HasBP = RegInfo->hasBasePointer(MF);
670   unsigned FrameSize = determineFrameLayout(MF, false);
671   int NegFrameSize = -FrameSize;
672   bool IsLargeFrame = !isInt<16>(NegFrameSize);
673   MachineFrameInfo &MFI = MF.getFrameInfo();
674   unsigned MaxAlign = MFI.getMaxAlignment();
675 
676   return IsLargeFrame && HasBP && MaxAlign > 1;
677 }
678 
679 bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
680   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
681 
682   return findScratchRegister(TmpMBB, false,
683                              twoUniqueScratchRegsRequired(TmpMBB));
684 }
685 
686 bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
687   MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB);
688 
689   return findScratchRegister(TmpMBB, true);
690 }
691 
692 void PPCFrameLowering::emitPrologue(MachineFunction &MF,
693                                     MachineBasicBlock &MBB) const {
694   MachineBasicBlock::iterator MBBI = MBB.begin();
695   MachineFrameInfo &MFI = MF.getFrameInfo();
696   const PPCInstrInfo &TII =
697       *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
698   const PPCRegisterInfo *RegInfo =
699       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
700 
701   MachineModuleInfo &MMI = MF.getMMI();
702   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
703   DebugLoc dl;
704   bool needsCFI = MMI.hasDebugInfo() ||
705     MF.getFunction()->needsUnwindTableEntry();
706 
707   // Get processor type.
708   bool isPPC64 = Subtarget.isPPC64();
709   // Get the ABI.
710   bool isSVR4ABI = Subtarget.isSVR4ABI();
711   bool isELFv2ABI = Subtarget.isELFv2ABI();
712   assert((Subtarget.isDarwinABI() || isSVR4ABI) &&
713          "Currently only Darwin and SVR4 ABIs are supported for PowerPC.");
714 
715   // Scan the prolog, looking for an UPDATE_VRSAVE instruction.  If we find it,
716   // process it.
717   if (!isSVR4ABI)
718     for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
719       if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
720         HandleVRSaveUpdate(*MBBI, TII);
721         break;
722       }
723     }
724 
725   // Move MBBI back to the beginning of the prologue block.
726   MBBI = MBB.begin();
727 
728   // Work out frame sizes.
729   unsigned FrameSize = determineFrameLayout(MF);
730   int NegFrameSize = -FrameSize;
731   if (!isInt<32>(NegFrameSize))
732     llvm_unreachable("Unhandled stack size!");
733 
734   if (MFI.isFrameAddressTaken())
735     replaceFPWithRealFP(MF);
736 
737   // Check if the link register (LR) must be saved.
738   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
739   bool MustSaveLR = FI->mustSaveLR();
740   const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
741   bool MustSaveCR = !MustSaveCRs.empty();
742   // Do we have a frame pointer and/or base pointer for this function?
743   bool HasFP = hasFP(MF);
744   bool HasBP = RegInfo->hasBasePointer(MF);
745 
746   unsigned SPReg       = isPPC64 ? PPC::X1  : PPC::R1;
747   unsigned BPReg       = RegInfo->getBaseRegister(MF);
748   unsigned FPReg       = isPPC64 ? PPC::X31 : PPC::R31;
749   unsigned LRReg       = isPPC64 ? PPC::LR8 : PPC::LR;
750   unsigned ScratchReg  = 0;
751   unsigned TempReg     = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
752   //  ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.)
753   const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8
754                                                 : PPC::MFLR );
755   const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD
756                                                  : PPC::STW );
757   const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU
758                                                      : PPC::STWU );
759   const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX
760                                                         : PPC::STWUX);
761   const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8
762                                                           : PPC::LIS );
763   const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8
764                                                  : PPC::ORI );
765   const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8
766                                               : PPC::OR );
767   const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8
768                                                             : PPC::SUBFC);
769   const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8
770                                                                : PPC::SUBFIC);
771 
772   // Regarding this assert: Even though LR is saved in the caller's frame (i.e.,
773   // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no
774   // Red Zone, an asynchronous event (a form of "callee") could claim a frame &
775   // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR.
776   assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) &&
777          "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4.");
778 
779   // Using the same bool variable as below to supress compiler warnings.
780   bool SingleScratchReg =
781     findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB),
782                         &ScratchReg, &TempReg);
783   assert(SingleScratchReg &&
784          "Required number of registers not available in this block");
785 
786   SingleScratchReg = ScratchReg == TempReg;
787 
788   int LROffset = getReturnSaveOffset();
789 
790   int FPOffset = 0;
791   if (HasFP) {
792     if (isSVR4ABI) {
793       MachineFrameInfo &MFI = MF.getFrameInfo();
794       int FPIndex = FI->getFramePointerSaveIndex();
795       assert(FPIndex && "No Frame Pointer Save Slot!");
796       FPOffset = MFI.getObjectOffset(FPIndex);
797     } else {
798       FPOffset = getFramePointerSaveOffset();
799     }
800   }
801 
802   int BPOffset = 0;
803   if (HasBP) {
804     if (isSVR4ABI) {
805       MachineFrameInfo &MFI = MF.getFrameInfo();
806       int BPIndex = FI->getBasePointerSaveIndex();
807       assert(BPIndex && "No Base Pointer Save Slot!");
808       BPOffset = MFI.getObjectOffset(BPIndex);
809     } else {
810       BPOffset = getBasePointerSaveOffset();
811     }
812   }
813 
814   int PBPOffset = 0;
815   if (FI->usesPICBase()) {
816     MachineFrameInfo &MFI = MF.getFrameInfo();
817     int PBPIndex = FI->getPICBasePointerSaveIndex();
818     assert(PBPIndex && "No PIC Base Pointer Save Slot!");
819     PBPOffset = MFI.getObjectOffset(PBPIndex);
820   }
821 
822   // Get stack alignments.
823   unsigned MaxAlign = MFI.getMaxAlignment();
824   if (HasBP && MaxAlign > 1)
825     assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
826            "Invalid alignment!");
827 
828   // Frames of 32KB & larger require special handling because they cannot be
829   // indexed into with a simple STDU/STWU/STD/STW immediate offset operand.
830   bool isLargeFrame = !isInt<16>(NegFrameSize);
831 
832   assert((isPPC64 || !MustSaveCR) &&
833          "Prologue CR saving supported only in 64-bit mode");
834 
835   // If we need to spill the CR and the LR but we don't have two separate
836   // registers available, we must spill them one at a time
837   if (MustSaveCR && SingleScratchReg && MustSaveLR) {
838     // In the ELFv2 ABI, we are not required to save all CR fields.
839     // If only one or two CR fields are clobbered, it is more efficient to use
840     // mfocrf to selectively save just those fields, because mfocrf has short
841     // latency compares to mfcr.
842     unsigned MfcrOpcode = PPC::MFCR8;
843     unsigned CrState = RegState::ImplicitKill;
844     if (isELFv2ABI && MustSaveCRs.size() == 1) {
845       MfcrOpcode = PPC::MFOCRF8;
846       CrState = RegState::Kill;
847     }
848     MachineInstrBuilder MIB =
849       BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
850     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
851       MIB.addReg(MustSaveCRs[i], CrState);
852     BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
853       .addReg(TempReg, getKillRegState(true))
854       .addImm(8)
855       .addReg(SPReg);
856   }
857 
858   if (MustSaveLR)
859     BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg);
860 
861   if (MustSaveCR &&
862       !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64
863     // In the ELFv2 ABI, we are not required to save all CR fields.
864     // If only one or two CR fields are clobbered, it is more efficient to use
865     // mfocrf to selectively save just those fields, because mfocrf has short
866     // latency compares to mfcr.
867     unsigned MfcrOpcode = PPC::MFCR8;
868     unsigned CrState = RegState::ImplicitKill;
869     if (isELFv2ABI && MustSaveCRs.size() == 1) {
870       MfcrOpcode = PPC::MFOCRF8;
871       CrState = RegState::Kill;
872     }
873     MachineInstrBuilder MIB =
874       BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg);
875     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
876       MIB.addReg(MustSaveCRs[i], CrState);
877   }
878 
879   if (HasFP)
880     // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
881     BuildMI(MBB, MBBI, dl, StoreInst)
882       .addReg(FPReg)
883       .addImm(FPOffset)
884       .addReg(SPReg);
885 
886   if (FI->usesPICBase())
887     // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
888     BuildMI(MBB, MBBI, dl, StoreInst)
889       .addReg(PPC::R30)
890       .addImm(PBPOffset)
891       .addReg(SPReg);
892 
893   if (HasBP)
894     // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
895     BuildMI(MBB, MBBI, dl, StoreInst)
896       .addReg(BPReg)
897       .addImm(BPOffset)
898       .addReg(SPReg);
899 
900   if (MustSaveLR)
901     // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
902     BuildMI(MBB, MBBI, dl, StoreInst)
903       .addReg(ScratchReg, getKillRegState(true))
904       .addImm(LROffset)
905       .addReg(SPReg);
906 
907   if (MustSaveCR &&
908       !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
909     BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8))
910       .addReg(TempReg, getKillRegState(true))
911       .addImm(8)
912       .addReg(SPReg);
913 
914   // Skip the rest if this is a leaf function & all spills fit in the Red Zone.
915   if (!FrameSize) return;
916 
917   // Adjust stack pointer: r1 += NegFrameSize.
918   // If there is a preferred stack alignment, align R1 now
919 
920   if (HasBP) {
921     // Save a copy of r1 as the base pointer.
922     BuildMI(MBB, MBBI, dl, OrInst, BPReg)
923       .addReg(SPReg)
924       .addReg(SPReg);
925   }
926 
927   // This condition must be kept in sync with canUseAsPrologue.
928   if (HasBP && MaxAlign > 1) {
929     if (isPPC64)
930       BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg)
931         .addReg(SPReg)
932         .addImm(0)
933         .addImm(64 - Log2_32(MaxAlign));
934     else // PPC32...
935       BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg)
936         .addReg(SPReg)
937         .addImm(0)
938         .addImm(32 - Log2_32(MaxAlign))
939         .addImm(31);
940     if (!isLargeFrame) {
941       BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg)
942         .addReg(ScratchReg, RegState::Kill)
943         .addImm(NegFrameSize);
944     } else {
945       assert(!SingleScratchReg && "Only a single scratch reg available");
946       BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg)
947         .addImm(NegFrameSize >> 16);
948       BuildMI(MBB, MBBI, dl, OrImmInst, TempReg)
949         .addReg(TempReg, RegState::Kill)
950         .addImm(NegFrameSize & 0xFFFF);
951       BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg)
952         .addReg(ScratchReg, RegState::Kill)
953         .addReg(TempReg, RegState::Kill);
954     }
955     BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
956       .addReg(SPReg, RegState::Kill)
957       .addReg(SPReg)
958       .addReg(ScratchReg);
959 
960   } else if (!isLargeFrame) {
961     BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg)
962       .addReg(SPReg)
963       .addImm(NegFrameSize)
964       .addReg(SPReg);
965 
966   } else {
967     BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
968       .addImm(NegFrameSize >> 16);
969     BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
970       .addReg(ScratchReg, RegState::Kill)
971       .addImm(NegFrameSize & 0xFFFF);
972     BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg)
973       .addReg(SPReg, RegState::Kill)
974       .addReg(SPReg)
975       .addReg(ScratchReg);
976   }
977 
978   // Add Call Frame Information for the instructions we generated above.
979   if (needsCFI) {
980     unsigned CFIIndex;
981 
982     if (HasBP) {
983       // Define CFA in terms of BP. Do this in preference to using FP/SP,
984       // because if the stack needed aligning then CFA won't be at a fixed
985       // offset from FP/SP.
986       unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
987       CFIIndex = MMI.addFrameInst(
988           MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
989     } else {
990       // Adjust the definition of CFA to account for the change in SP.
991       assert(NegFrameSize);
992       CFIIndex = MMI.addFrameInst(
993           MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize));
994     }
995     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
996         .addCFIIndex(CFIIndex);
997 
998     if (HasFP) {
999       // Describe where FP was saved, at a fixed offset from CFA.
1000       unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1001       CFIIndex = MMI.addFrameInst(
1002           MCCFIInstruction::createOffset(nullptr, Reg, FPOffset));
1003       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1004           .addCFIIndex(CFIIndex);
1005     }
1006 
1007     if (FI->usesPICBase()) {
1008       // Describe where FP was saved, at a fixed offset from CFA.
1009       unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true);
1010       CFIIndex = MMI.addFrameInst(
1011           MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset));
1012       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1013           .addCFIIndex(CFIIndex);
1014     }
1015 
1016     if (HasBP) {
1017       // Describe where BP was saved, at a fixed offset from CFA.
1018       unsigned Reg = MRI->getDwarfRegNum(BPReg, true);
1019       CFIIndex = MMI.addFrameInst(
1020           MCCFIInstruction::createOffset(nullptr, Reg, BPOffset));
1021       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1022           .addCFIIndex(CFIIndex);
1023     }
1024 
1025     if (MustSaveLR) {
1026       // Describe where LR was saved, at a fixed offset from CFA.
1027       unsigned Reg = MRI->getDwarfRegNum(LRReg, true);
1028       CFIIndex = MMI.addFrameInst(
1029           MCCFIInstruction::createOffset(nullptr, Reg, LROffset));
1030       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1031           .addCFIIndex(CFIIndex);
1032     }
1033   }
1034 
1035   // If there is a frame pointer, copy R1 into R31
1036   if (HasFP) {
1037     BuildMI(MBB, MBBI, dl, OrInst, FPReg)
1038       .addReg(SPReg)
1039       .addReg(SPReg);
1040 
1041     if (!HasBP && needsCFI) {
1042       // Change the definition of CFA from SP+offset to FP+offset, because SP
1043       // will change at every alloca.
1044       unsigned Reg = MRI->getDwarfRegNum(FPReg, true);
1045       unsigned CFIIndex = MMI.addFrameInst(
1046           MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1047 
1048       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1049           .addCFIIndex(CFIIndex);
1050     }
1051   }
1052 
1053   if (needsCFI) {
1054     // Describe where callee saved registers were saved, at fixed offsets from
1055     // CFA.
1056     const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1057     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
1058       unsigned Reg = CSI[I].getReg();
1059       if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
1060 
1061       // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just
1062       // subregisters of CR2. We just need to emit a move of CR2.
1063       if (PPC::CRBITRCRegClass.contains(Reg))
1064         continue;
1065 
1066       // For SVR4, don't emit a move for the CR spill slot if we haven't
1067       // spilled CRs.
1068       if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4)
1069           && !MustSaveCR)
1070         continue;
1071 
1072       // For 64-bit SVR4 when we have spilled CRs, the spill location
1073       // is SP+8, not a frame-relative slot.
1074       if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
1075         // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for
1076         // the whole CR word.  In the ELFv2 ABI, every CR that was
1077         // actually saved gets its own CFI record.
1078         unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2;
1079         unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1080             nullptr, MRI->getDwarfRegNum(CRReg, true), 8));
1081         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1082             .addCFIIndex(CFIIndex);
1083         continue;
1084       }
1085 
1086       int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx());
1087       unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1088           nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
1089       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
1090           .addCFIIndex(CFIIndex);
1091     }
1092   }
1093 }
1094 
1095 void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
1096                                     MachineBasicBlock &MBB) const {
1097   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1098   DebugLoc dl;
1099 
1100   if (MBBI != MBB.end())
1101     dl = MBBI->getDebugLoc();
1102 
1103   const PPCInstrInfo &TII =
1104       *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
1105   const PPCRegisterInfo *RegInfo =
1106       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
1107 
1108   // Get alignment info so we know how to restore the SP.
1109   const MachineFrameInfo &MFI = MF.getFrameInfo();
1110 
1111   // Get the number of bytes allocated from the FrameInfo.
1112   int FrameSize = MFI.getStackSize();
1113 
1114   // Get processor type.
1115   bool isPPC64 = Subtarget.isPPC64();
1116   // Get the ABI.
1117   bool isSVR4ABI = Subtarget.isSVR4ABI();
1118 
1119   // Check if the link register (LR) has been saved.
1120   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1121   bool MustSaveLR = FI->mustSaveLR();
1122   const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs();
1123   bool MustSaveCR = !MustSaveCRs.empty();
1124   // Do we have a frame pointer and/or base pointer for this function?
1125   bool HasFP = hasFP(MF);
1126   bool HasBP = RegInfo->hasBasePointer(MF);
1127 
1128   unsigned SPReg      = isPPC64 ? PPC::X1  : PPC::R1;
1129   unsigned BPReg      = RegInfo->getBaseRegister(MF);
1130   unsigned FPReg      = isPPC64 ? PPC::X31 : PPC::R31;
1131   unsigned ScratchReg = 0;
1132   unsigned TempReg     = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg
1133   const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8
1134                                                  : PPC::MTLR );
1135   const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD
1136                                                  : PPC::LWZ );
1137   const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8
1138                                                            : PPC::LIS );
1139   const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8
1140                                                   : PPC::ORI );
1141   const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8
1142                                                    : PPC::ADDI );
1143   const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8
1144                                                 : PPC::ADD4 );
1145 
1146   int LROffset = getReturnSaveOffset();
1147 
1148   int FPOffset = 0;
1149 
1150   // Using the same bool variable as below to supress compiler warnings.
1151   bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg,
1152                                               &TempReg);
1153   assert(SingleScratchReg &&
1154          "Could not find an available scratch register");
1155 
1156   SingleScratchReg = ScratchReg == TempReg;
1157 
1158   if (HasFP) {
1159     if (isSVR4ABI) {
1160       MachineFrameInfo &MFI = MF.getFrameInfo();
1161       int FPIndex = FI->getFramePointerSaveIndex();
1162       assert(FPIndex && "No Frame Pointer Save Slot!");
1163       FPOffset = MFI.getObjectOffset(FPIndex);
1164     } else {
1165       FPOffset = getFramePointerSaveOffset();
1166     }
1167   }
1168 
1169   int BPOffset = 0;
1170   if (HasBP) {
1171     if (isSVR4ABI) {
1172       MachineFrameInfo &MFI = MF.getFrameInfo();
1173       int BPIndex = FI->getBasePointerSaveIndex();
1174       assert(BPIndex && "No Base Pointer Save Slot!");
1175       BPOffset = MFI.getObjectOffset(BPIndex);
1176     } else {
1177       BPOffset = getBasePointerSaveOffset();
1178     }
1179   }
1180 
1181   int PBPOffset = 0;
1182   if (FI->usesPICBase()) {
1183     MachineFrameInfo &MFI = MF.getFrameInfo();
1184     int PBPIndex = FI->getPICBasePointerSaveIndex();
1185     assert(PBPIndex && "No PIC Base Pointer Save Slot!");
1186     PBPOffset = MFI.getObjectOffset(PBPIndex);
1187   }
1188 
1189   bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn());
1190 
1191   if (IsReturnBlock) {
1192     unsigned RetOpcode = MBBI->getOpcode();
1193     bool UsesTCRet =  RetOpcode == PPC::TCRETURNri ||
1194                       RetOpcode == PPC::TCRETURNdi ||
1195                       RetOpcode == PPC::TCRETURNai ||
1196                       RetOpcode == PPC::TCRETURNri8 ||
1197                       RetOpcode == PPC::TCRETURNdi8 ||
1198                       RetOpcode == PPC::TCRETURNai8;
1199 
1200     if (UsesTCRet) {
1201       int MaxTCRetDelta = FI->getTailCallSPDelta();
1202       MachineOperand &StackAdjust = MBBI->getOperand(1);
1203       assert(StackAdjust.isImm() && "Expecting immediate value.");
1204       // Adjust stack pointer.
1205       int StackAdj = StackAdjust.getImm();
1206       int Delta = StackAdj - MaxTCRetDelta;
1207       assert((Delta >= 0) && "Delta must be positive");
1208       if (MaxTCRetDelta>0)
1209         FrameSize += (StackAdj +Delta);
1210       else
1211         FrameSize += StackAdj;
1212     }
1213   }
1214 
1215   // Frames of 32KB & larger require special handling because they cannot be
1216   // indexed into with a simple LD/LWZ immediate offset operand.
1217   bool isLargeFrame = !isInt<16>(FrameSize);
1218 
1219   if (FrameSize) {
1220     // In the prologue, the loaded (or persistent) stack pointer value is offset
1221     // by the STDU/STDUX/STWU/STWUX instruction.  Add this offset back now.
1222 
1223     // If this function contained a fastcc call and GuaranteedTailCallOpt is
1224     // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1225     // call which invalidates the stack pointer value in SP(0). So we use the
1226     // value of R31 in this case.
1227     if (FI->hasFastCall()) {
1228       assert(HasFP && "Expecting a valid frame pointer.");
1229       if (!isLargeFrame) {
1230         BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1231           .addReg(FPReg).addImm(FrameSize);
1232       } else {
1233         BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1234           .addImm(FrameSize >> 16);
1235         BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1236           .addReg(ScratchReg, RegState::Kill)
1237           .addImm(FrameSize & 0xFFFF);
1238         BuildMI(MBB, MBBI, dl, AddInst)
1239           .addReg(SPReg)
1240           .addReg(FPReg)
1241           .addReg(ScratchReg);
1242       }
1243     } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) {
1244       BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1245         .addReg(SPReg)
1246         .addImm(FrameSize);
1247     } else {
1248       BuildMI(MBB, MBBI, dl, LoadInst, SPReg)
1249         .addImm(0)
1250         .addReg(SPReg);
1251     }
1252   }
1253 
1254   assert((isPPC64 || !MustSaveCR) &&
1255          "Epilogue CR restoring supported only in 64-bit mode");
1256 
1257   // If we need to save both the LR and the CR and we only have one available
1258   // scratch register, we must do them one at a time.
1259   if (MustSaveCR && SingleScratchReg && MustSaveLR) {
1260     BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1261       .addImm(8)
1262       .addReg(SPReg);
1263     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1264       BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1265         .addReg(TempReg, getKillRegState(i == e-1));
1266   }
1267 
1268   if (MustSaveLR)
1269     BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg)
1270       .addImm(LROffset)
1271       .addReg(SPReg);
1272 
1273   if (MustSaveCR &&
1274       !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
1275     BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg)
1276       .addImm(8)
1277       .addReg(SPReg);
1278 
1279   if (HasFP)
1280     BuildMI(MBB, MBBI, dl, LoadInst, FPReg)
1281       .addImm(FPOffset)
1282       .addReg(SPReg);
1283 
1284   if (FI->usesPICBase())
1285     // FIXME: On PPC32 SVR4, we must not spill before claiming the stackframe.
1286     BuildMI(MBB, MBBI, dl, LoadInst)
1287       .addReg(PPC::R30)
1288       .addImm(PBPOffset)
1289       .addReg(SPReg);
1290 
1291   if (HasBP)
1292     BuildMI(MBB, MBBI, dl, LoadInst, BPReg)
1293       .addImm(BPOffset)
1294       .addReg(SPReg);
1295 
1296   if (MustSaveCR &&
1297       !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64
1298     for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i)
1299       BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i])
1300         .addReg(TempReg, getKillRegState(i == e-1));
1301 
1302   if (MustSaveLR)
1303     BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg);
1304 
1305   // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1306   // call optimization
1307   if (IsReturnBlock) {
1308     unsigned RetOpcode = MBBI->getOpcode();
1309     if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1310         (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) &&
1311         MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1312       PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1313       unsigned CallerAllocatedAmt = FI->getMinReservedArea();
1314 
1315       if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1316         BuildMI(MBB, MBBI, dl, AddImmInst, SPReg)
1317           .addReg(SPReg).addImm(CallerAllocatedAmt);
1318       } else {
1319         BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg)
1320           .addImm(CallerAllocatedAmt >> 16);
1321         BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg)
1322           .addReg(ScratchReg, RegState::Kill)
1323           .addImm(CallerAllocatedAmt & 0xFFFF);
1324         BuildMI(MBB, MBBI, dl, AddInst)
1325           .addReg(SPReg)
1326           .addReg(FPReg)
1327           .addReg(ScratchReg);
1328       }
1329     } else {
1330       createTailCallBranchInstr(MBB);
1331     }
1332   }
1333 }
1334 
1335 void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const {
1336   MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
1337   DebugLoc dl;
1338 
1339   if (MBBI != MBB.end())
1340     dl = MBBI->getDebugLoc();
1341 
1342   const PPCInstrInfo &TII =
1343       *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
1344 
1345   // Create branch instruction for pseudo tail call return instruction
1346   unsigned RetOpcode = MBBI->getOpcode();
1347   if (RetOpcode == PPC::TCRETURNdi) {
1348     MBBI = MBB.getLastNonDebugInstr();
1349     MachineOperand &JumpTarget = MBBI->getOperand(0);
1350     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1351       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1352   } else if (RetOpcode == PPC::TCRETURNri) {
1353     MBBI = MBB.getLastNonDebugInstr();
1354     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1355     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1356   } else if (RetOpcode == PPC::TCRETURNai) {
1357     MBBI = MBB.getLastNonDebugInstr();
1358     MachineOperand &JumpTarget = MBBI->getOperand(0);
1359     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1360   } else if (RetOpcode == PPC::TCRETURNdi8) {
1361     MBBI = MBB.getLastNonDebugInstr();
1362     MachineOperand &JumpTarget = MBBI->getOperand(0);
1363     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1364       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1365   } else if (RetOpcode == PPC::TCRETURNri8) {
1366     MBBI = MBB.getLastNonDebugInstr();
1367     assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1368     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1369   } else if (RetOpcode == PPC::TCRETURNai8) {
1370     MBBI = MBB.getLastNonDebugInstr();
1371     MachineOperand &JumpTarget = MBBI->getOperand(0);
1372     BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1373   }
1374 }
1375 
1376 void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF,
1377                                             BitVector &SavedRegs,
1378                                             RegScavenger *RS) const {
1379   TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1380 
1381   const PPCRegisterInfo *RegInfo =
1382       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
1383 
1384   //  Save and clear the LR state.
1385   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1386   unsigned LR = RegInfo->getRARegister();
1387   FI->setMustSaveLR(MustSaveLR(MF, LR));
1388   SavedRegs.reset(LR);
1389 
1390   //  Save R31 if necessary
1391   int FPSI = FI->getFramePointerSaveIndex();
1392   bool isPPC64 = Subtarget.isPPC64();
1393   bool isDarwinABI  = Subtarget.isDarwinABI();
1394   MachineFrameInfo &MFI = MF.getFrameInfo();
1395 
1396   // If the frame pointer save index hasn't been defined yet.
1397   if (!FPSI && needsFP(MF)) {
1398     // Find out what the fix offset of the frame pointer save area.
1399     int FPOffset = getFramePointerSaveOffset();
1400     // Allocate the frame index for frame pointer save area.
1401     FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
1402     // Save the result.
1403     FI->setFramePointerSaveIndex(FPSI);
1404   }
1405 
1406   int BPSI = FI->getBasePointerSaveIndex();
1407   if (!BPSI && RegInfo->hasBasePointer(MF)) {
1408     int BPOffset = getBasePointerSaveOffset();
1409     // Allocate the frame index for the base pointer save area.
1410     BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true);
1411     // Save the result.
1412     FI->setBasePointerSaveIndex(BPSI);
1413   }
1414 
1415   // Reserve stack space for the PIC Base register (R30).
1416   // Only used in SVR4 32-bit.
1417   if (FI->usesPICBase()) {
1418     int PBPSI = MFI.CreateFixedObject(4, -8, true);
1419     FI->setPICBasePointerSaveIndex(PBPSI);
1420   }
1421 
1422   // Make sure we don't explicitly spill r31, because, for example, we have
1423   // some inline asm which explicity clobbers it, when we otherwise have a
1424   // frame pointer and are using r31's spill slot for the prologue/epilogue
1425   // code. Same goes for the base pointer and the PIC base register.
1426   if (needsFP(MF))
1427     SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31);
1428   if (RegInfo->hasBasePointer(MF))
1429     SavedRegs.reset(RegInfo->getBaseRegister(MF));
1430   if (FI->usesPICBase())
1431     SavedRegs.reset(PPC::R30);
1432 
1433   // Reserve stack space to move the linkage area to in case of a tail call.
1434   int TCSPDelta = 0;
1435   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1436       (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
1437     MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true);
1438   }
1439 
1440   // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the
1441   // function uses CR 2, 3, or 4.
1442   if (!isPPC64 && !isDarwinABI &&
1443       (SavedRegs.test(PPC::CR2) ||
1444        SavedRegs.test(PPC::CR3) ||
1445        SavedRegs.test(PPC::CR4))) {
1446     int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true);
1447     FI->setCRSpillFrameIndex(FrameIdx);
1448   }
1449 }
1450 
1451 void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
1452                                                        RegScavenger *RS) const {
1453   // Early exit if not using the SVR4 ABI.
1454   if (!Subtarget.isSVR4ABI()) {
1455     addScavengingSpillSlot(MF, RS);
1456     return;
1457   }
1458 
1459   // Get callee saved register information.
1460   MachineFrameInfo &MFI = MF.getFrameInfo();
1461   const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
1462 
1463   // If the function is shrink-wrapped, and if the function has a tail call, the
1464   // tail call might not be in the new RestoreBlock, so real branch instruction
1465   // won't be generated by emitEpilogue(), because shrink-wrap has chosen new
1466   // RestoreBlock. So we handle this case here.
1467   if (MFI.getSavePoint() && MFI.hasTailCall()) {
1468     MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
1469     for (MachineBasicBlock &MBB : MF) {
1470       if (MBB.isReturnBlock() && (&MBB) != RestoreBlock)
1471         createTailCallBranchInstr(MBB);
1472     }
1473   }
1474 
1475   // Early exit if no callee saved registers are modified!
1476   if (CSI.empty() && !needsFP(MF)) {
1477     addScavengingSpillSlot(MF, RS);
1478     return;
1479   }
1480 
1481   unsigned MinGPR = PPC::R31;
1482   unsigned MinG8R = PPC::X31;
1483   unsigned MinFPR = PPC::F31;
1484   unsigned MinVR = PPC::V31;
1485 
1486   bool HasGPSaveArea = false;
1487   bool HasG8SaveArea = false;
1488   bool HasFPSaveArea = false;
1489   bool HasVRSAVESaveArea = false;
1490   bool HasVRSaveArea = false;
1491 
1492   SmallVector<CalleeSavedInfo, 18> GPRegs;
1493   SmallVector<CalleeSavedInfo, 18> G8Regs;
1494   SmallVector<CalleeSavedInfo, 18> FPRegs;
1495   SmallVector<CalleeSavedInfo, 18> VRegs;
1496 
1497   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1498     unsigned Reg = CSI[i].getReg();
1499     if (PPC::GPRCRegClass.contains(Reg)) {
1500       HasGPSaveArea = true;
1501 
1502       GPRegs.push_back(CSI[i]);
1503 
1504       if (Reg < MinGPR) {
1505         MinGPR = Reg;
1506       }
1507     } else if (PPC::G8RCRegClass.contains(Reg)) {
1508       HasG8SaveArea = true;
1509 
1510       G8Regs.push_back(CSI[i]);
1511 
1512       if (Reg < MinG8R) {
1513         MinG8R = Reg;
1514       }
1515     } else if (PPC::F8RCRegClass.contains(Reg)) {
1516       HasFPSaveArea = true;
1517 
1518       FPRegs.push_back(CSI[i]);
1519 
1520       if (Reg < MinFPR) {
1521         MinFPR = Reg;
1522       }
1523     } else if (PPC::CRBITRCRegClass.contains(Reg) ||
1524                PPC::CRRCRegClass.contains(Reg)) {
1525       ; // do nothing, as we already know whether CRs are spilled
1526     } else if (PPC::VRSAVERCRegClass.contains(Reg)) {
1527       HasVRSAVESaveArea = true;
1528     } else if (PPC::VRRCRegClass.contains(Reg)) {
1529       HasVRSaveArea = true;
1530 
1531       VRegs.push_back(CSI[i]);
1532 
1533       if (Reg < MinVR) {
1534         MinVR = Reg;
1535       }
1536     } else {
1537       llvm_unreachable("Unknown RegisterClass!");
1538     }
1539   }
1540 
1541   PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
1542   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1543 
1544   int64_t LowerBound = 0;
1545 
1546   // Take into account stack space reserved for tail calls.
1547   int TCSPDelta = 0;
1548   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1549       (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
1550     LowerBound = TCSPDelta;
1551   }
1552 
1553   // The Floating-point register save area is right below the back chain word
1554   // of the previous stack frame.
1555   if (HasFPSaveArea) {
1556     for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1557       int FI = FPRegs[i].getFrameIdx();
1558 
1559       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1560     }
1561 
1562     LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8;
1563   }
1564 
1565   // Check whether the frame pointer register is allocated. If so, make sure it
1566   // is spilled to the correct offset.
1567   if (needsFP(MF)) {
1568     HasGPSaveArea = true;
1569 
1570     int FI = PFI->getFramePointerSaveIndex();
1571     assert(FI && "No Frame Pointer Save Slot!");
1572 
1573     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1574   }
1575 
1576   if (PFI->usesPICBase()) {
1577     HasGPSaveArea = true;
1578 
1579     int FI = PFI->getPICBasePointerSaveIndex();
1580     assert(FI && "No PIC Base Pointer Save Slot!");
1581 
1582     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1583   }
1584 
1585   const PPCRegisterInfo *RegInfo =
1586       static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo());
1587   if (RegInfo->hasBasePointer(MF)) {
1588     HasGPSaveArea = true;
1589 
1590     int FI = PFI->getBasePointerSaveIndex();
1591     assert(FI && "No Base Pointer Save Slot!");
1592 
1593     MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1594   }
1595 
1596   // General register save area starts right below the Floating-point
1597   // register save area.
1598   if (HasGPSaveArea || HasG8SaveArea) {
1599     // Move general register save area spill slots down, taking into account
1600     // the size of the Floating-point register save area.
1601     for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1602       int FI = GPRegs[i].getFrameIdx();
1603 
1604       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1605     }
1606 
1607     // Move general register save area spill slots down, taking into account
1608     // the size of the Floating-point register save area.
1609     for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1610       int FI = G8Regs[i].getFrameIdx();
1611 
1612       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1613     }
1614 
1615     unsigned MinReg =
1616       std::min<unsigned>(TRI->getEncodingValue(MinGPR),
1617                          TRI->getEncodingValue(MinG8R));
1618 
1619     if (Subtarget.isPPC64()) {
1620       LowerBound -= (31 - MinReg + 1) * 8;
1621     } else {
1622       LowerBound -= (31 - MinReg + 1) * 4;
1623     }
1624   }
1625 
1626   // For 32-bit only, the CR save area is below the general register
1627   // save area.  For 64-bit SVR4, the CR save area is addressed relative
1628   // to the stack pointer and hence does not need an adjustment here.
1629   // Only CR2 (the first nonvolatile spilled) has an associated frame
1630   // index so that we have a single uniform save area.
1631   if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) {
1632     // Adjust the frame index of the CR spill slot.
1633     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1634       unsigned Reg = CSI[i].getReg();
1635 
1636       if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2)
1637           // Leave Darwin logic as-is.
1638           || (!Subtarget.isSVR4ABI() &&
1639               (PPC::CRBITRCRegClass.contains(Reg) ||
1640                PPC::CRRCRegClass.contains(Reg)))) {
1641         int FI = CSI[i].getFrameIdx();
1642 
1643         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1644       }
1645     }
1646 
1647     LowerBound -= 4; // The CR save area is always 4 bytes long.
1648   }
1649 
1650   if (HasVRSAVESaveArea) {
1651     // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1652     //             which have the VRSAVE register class?
1653     // Adjust the frame index of the VRSAVE spill slot.
1654     for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1655       unsigned Reg = CSI[i].getReg();
1656 
1657       if (PPC::VRSAVERCRegClass.contains(Reg)) {
1658         int FI = CSI[i].getFrameIdx();
1659 
1660         MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1661       }
1662     }
1663 
1664     LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1665   }
1666 
1667   if (HasVRSaveArea) {
1668     // Insert alignment padding, we need 16-byte alignment.
1669     LowerBound = (LowerBound - 15) & ~(15);
1670 
1671     for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1672       int FI = VRegs[i].getFrameIdx();
1673 
1674       MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI));
1675     }
1676   }
1677 
1678   addScavengingSpillSlot(MF, RS);
1679 }
1680 
1681 void
1682 PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF,
1683                                          RegScavenger *RS) const {
1684   // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1685   // a large stack, which will require scavenging a register to materialize a
1686   // large offset.
1687 
1688   // We need to have a scavenger spill slot for spills if the frame size is
1689   // large. In case there is no free register for large-offset addressing,
1690   // this slot is used for the necessary emergency spill. Also, we need the
1691   // slot for dynamic stack allocations.
1692 
1693   // The scavenger might be invoked if the frame offset does not fit into
1694   // the 16-bit immediate. We don't know the complete frame size here
1695   // because we've not yet computed callee-saved register spills or the
1696   // needed alignment padding.
1697   unsigned StackSize = determineFrameLayout(MF, false, true);
1698   MachineFrameInfo &MFI = MF.getFrameInfo();
1699   if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) ||
1700       hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) {
1701     const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1702     const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1703     const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
1704     RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(),
1705                                                       RC->getAlignment(),
1706                                                       false));
1707 
1708     // Might we have over-aligned allocas?
1709     bool HasAlVars = MFI.hasVarSizedObjects() &&
1710                      MFI.getMaxAlignment() > getStackAlignment();
1711 
1712     // These kinds of spills might need two registers.
1713     if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars)
1714       RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(),
1715                                                         RC->getAlignment(),
1716                                                         false));
1717 
1718   }
1719 }
1720 
1721 bool
1722 PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1723                                      MachineBasicBlock::iterator MI,
1724                                      const std::vector<CalleeSavedInfo> &CSI,
1725                                      const TargetRegisterInfo *TRI) const {
1726 
1727   // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1728   // Return false otherwise to maintain pre-existing behavior.
1729   if (!Subtarget.isSVR4ABI())
1730     return false;
1731 
1732   MachineFunction *MF = MBB.getParent();
1733   const PPCInstrInfo &TII =
1734       *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
1735   DebugLoc DL;
1736   bool CRSpilled = false;
1737   MachineInstrBuilder CRMIB;
1738 
1739   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1740     unsigned Reg = CSI[i].getReg();
1741     // Only Darwin actually uses the VRSAVE register, but it can still appear
1742     // here if, for example, @llvm.eh.unwind.init() is used.  If we're not on
1743     // Darwin, ignore it.
1744     if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1745       continue;
1746 
1747     // CR2 through CR4 are the nonvolatile CR fields.
1748     bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4;
1749 
1750     // Add the callee-saved register as live-in; it's killed at the spill.
1751     MBB.addLiveIn(Reg);
1752 
1753     if (CRSpilled && IsCRField) {
1754       CRMIB.addReg(Reg, RegState::ImplicitKill);
1755       continue;
1756     }
1757 
1758     // Insert the spill to the stack frame.
1759     if (IsCRField) {
1760       PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
1761       if (Subtarget.isPPC64()) {
1762         // The actual spill will happen at the start of the prologue.
1763         FuncInfo->addMustSaveCR(Reg);
1764       } else {
1765         CRSpilled = true;
1766         FuncInfo->setSpillsCR();
1767 
1768         // 32-bit:  FP-relative.  Note that we made sure CR2-CR4 all have
1769         // the same frame index in PPCRegisterInfo::hasReservedSpillSlot.
1770         CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)
1771                   .addReg(Reg, RegState::ImplicitKill);
1772 
1773         MBB.insert(MI, CRMIB);
1774         MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW))
1775                                          .addReg(PPC::R12,
1776                                                  getKillRegState(true)),
1777                                          CSI[i].getFrameIdx()));
1778       }
1779     } else {
1780       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1781       TII.storeRegToStackSlot(MBB, MI, Reg, true,
1782                               CSI[i].getFrameIdx(), RC, TRI);
1783     }
1784   }
1785   return true;
1786 }
1787 
1788 static void
1789 restoreCRs(bool isPPC64, bool is31,
1790            bool CR2Spilled, bool CR3Spilled, bool CR4Spilled,
1791            MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1792            const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) {
1793 
1794   MachineFunction *MF = MBB.getParent();
1795   const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo();
1796   DebugLoc DL;
1797   unsigned RestoreOp, MoveReg;
1798 
1799   if (isPPC64)
1800     // This is handled during epilogue generation.
1801     return;
1802   else {
1803     // 32-bit:  FP-relative
1804     MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ),
1805                                              PPC::R12),
1806                                      CSI[CSIIndex].getFrameIdx()));
1807     RestoreOp = PPC::MTOCRF;
1808     MoveReg = PPC::R12;
1809   }
1810 
1811   if (CR2Spilled)
1812     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
1813                .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
1814 
1815   if (CR3Spilled)
1816     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
1817                .addReg(MoveReg, getKillRegState(!CR4Spilled)));
1818 
1819   if (CR4Spilled)
1820     MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
1821                .addReg(MoveReg, getKillRegState(true)));
1822 }
1823 
1824 MachineBasicBlock::iterator PPCFrameLowering::
1825 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1826                               MachineBasicBlock::iterator I) const {
1827   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1828   if (MF.getTarget().Options.GuaranteedTailCallOpt &&
1829       I->getOpcode() == PPC::ADJCALLSTACKUP) {
1830     // Add (actually subtract) back the amount the callee popped on return.
1831     if (int CalleeAmt =  I->getOperand(1).getImm()) {
1832       bool is64Bit = Subtarget.isPPC64();
1833       CalleeAmt *= -1;
1834       unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
1835       unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
1836       unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
1837       unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
1838       unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
1839       unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
1840       const DebugLoc &dl = I->getDebugLoc();
1841 
1842       if (isInt<16>(CalleeAmt)) {
1843         BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg)
1844           .addReg(StackReg, RegState::Kill)
1845           .addImm(CalleeAmt);
1846       } else {
1847         MachineBasicBlock::iterator MBBI = I;
1848         BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1849           .addImm(CalleeAmt >> 16);
1850         BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1851           .addReg(TmpReg, RegState::Kill)
1852           .addImm(CalleeAmt & 0xFFFF);
1853         BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg)
1854           .addReg(StackReg, RegState::Kill)
1855           .addReg(TmpReg);
1856       }
1857     }
1858   }
1859   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
1860   return MBB.erase(I);
1861 }
1862 
1863 bool
1864 PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1865                                         MachineBasicBlock::iterator MI,
1866                                         const std::vector<CalleeSavedInfo> &CSI,
1867                                         const TargetRegisterInfo *TRI) const {
1868 
1869   // Currently, this function only handles SVR4 32- and 64-bit ABIs.
1870   // Return false otherwise to maintain pre-existing behavior.
1871   if (!Subtarget.isSVR4ABI())
1872     return false;
1873 
1874   MachineFunction *MF = MBB.getParent();
1875   const PPCInstrInfo &TII =
1876       *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo());
1877   bool CR2Spilled = false;
1878   bool CR3Spilled = false;
1879   bool CR4Spilled = false;
1880   unsigned CSIIndex = 0;
1881 
1882   // Initialize insertion-point logic; we will be restoring in reverse
1883   // order of spill.
1884   MachineBasicBlock::iterator I = MI, BeforeI = I;
1885   bool AtStart = I == MBB.begin();
1886 
1887   if (!AtStart)
1888     --BeforeI;
1889 
1890   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1891     unsigned Reg = CSI[i].getReg();
1892 
1893     // Only Darwin actually uses the VRSAVE register, but it can still appear
1894     // here if, for example, @llvm.eh.unwind.init() is used.  If we're not on
1895     // Darwin, ignore it.
1896     if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI())
1897       continue;
1898 
1899     if (Reg == PPC::CR2) {
1900       CR2Spilled = true;
1901       // The spill slot is associated only with CR2, which is the
1902       // first nonvolatile spilled.  Save it here.
1903       CSIIndex = i;
1904       continue;
1905     } else if (Reg == PPC::CR3) {
1906       CR3Spilled = true;
1907       continue;
1908     } else if (Reg == PPC::CR4) {
1909       CR4Spilled = true;
1910       continue;
1911     } else {
1912       // When we first encounter a non-CR register after seeing at
1913       // least one CR register, restore all spilled CRs together.
1914       if ((CR2Spilled || CR3Spilled || CR4Spilled)
1915           && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) {
1916         bool is31 = needsFP(*MF);
1917         restoreCRs(Subtarget.isPPC64(), is31,
1918                    CR2Spilled, CR3Spilled, CR4Spilled,
1919                    MBB, I, CSI, CSIIndex);
1920         CR2Spilled = CR3Spilled = CR4Spilled = false;
1921       }
1922 
1923       // Default behavior for non-CR saves.
1924       const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
1925       TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(),
1926                                RC, TRI);
1927       assert(I != MBB.begin() &&
1928              "loadRegFromStackSlot didn't insert any code!");
1929       }
1930 
1931     // Insert in reverse order.
1932     if (AtStart)
1933       I = MBB.begin();
1934     else {
1935       I = BeforeI;
1936       ++I;
1937     }
1938   }
1939 
1940   // If we haven't yet spilled the CRs, do so now.
1941   if (CR2Spilled || CR3Spilled || CR4Spilled) {
1942     bool is31 = needsFP(*MF);
1943     restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled,
1944                MBB, I, CSI, CSIIndex);
1945   }
1946 
1947   return true;
1948 }
1949 
1950 bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const {
1951   return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() &&
1952           MF.getSubtarget<PPCSubtarget>().isPPC64());
1953 }
1954