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