1 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the PowerPC implementation of the TargetRegisterInfo
10 // class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PPCRegisterInfo.h"
15 #include "PPC.h"
16 #include "PPCFrameLowering.h"
17 #include "PPCInstrBuilder.h"
18 #include "PPCMachineFunctionInfo.h"
19 #include "PPCSubtarget.h"
20 #include "PPCTargetMachine.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/RegisterScavenging.h"
30 #include "llvm/CodeGen/TargetFrameLowering.h"
31 #include "llvm/CodeGen/TargetInstrInfo.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
43 #include <cstdlib>
44 
45 using namespace llvm;
46 
47 #define DEBUG_TYPE "reginfo"
48 
49 #define GET_REGINFO_TARGET_DESC
50 #include "PPCGenRegisterInfo.inc"
51 
52 STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass");
53 STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass");
54 
55 static cl::opt<bool>
56 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
57          cl::desc("Enable use of a base pointer for complex stack frames"));
58 
59 static cl::opt<bool>
60 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
61          cl::desc("Force the use of a base pointer in every function"));
62 
63 static cl::opt<bool>
64 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false),
65          cl::desc("Enable spills from gpr to vsr rather than stack"));
66 
67 static cl::opt<bool>
68 StackPtrConst("ppc-stack-ptr-caller-preserved",
69                 cl::desc("Consider R1 caller preserved so stack saves of "
70                          "caller preserved registers can be LICM candidates"),
71                 cl::init(true), cl::Hidden);
72 
73 static cl::opt<unsigned>
74 MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
75                   cl::desc("Maximum search distance for definition of CR bit "
76                            "spill on ppc"),
77                   cl::Hidden, cl::init(100));
78 
79 static unsigned offsetMinAlignForOpcode(unsigned OpC);
80 
81 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
82   : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
83                        TM.isPPC64() ? 0 : 1,
84                        TM.isPPC64() ? 0 : 1),
85     TM(TM) {
86   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
87   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
88   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
89   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
90   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
91   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
92   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
93   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
94   ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
95 
96   // 64-bit
97   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
98   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
99   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
100   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
101   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
102 
103   // VSX
104   ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
105   ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
106   ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
107   ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
108   ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
109   ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
110   ImmToIdxMap[PPC::LXV] = PPC::LXVX;
111   ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
112   ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
113   ImmToIdxMap[PPC::STXV] = PPC::STXVX;
114   ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
115   ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
116 
117   // SPE
118   ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
119   ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
120   ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
121   ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
122 }
123 
124 /// getPointerRegClass - Return the register class to use to hold pointers.
125 /// This is used for addressing modes.
126 const TargetRegisterClass *
127 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
128                                                                        const {
129   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
130   // when it checks for ZERO folding.
131   if (Kind == 1) {
132     if (TM.isPPC64())
133       return &PPC::G8RC_NOX0RegClass;
134     return &PPC::GPRC_NOR0RegClass;
135   }
136 
137   if (TM.isPPC64())
138     return &PPC::G8RCRegClass;
139   return &PPC::GPRCRegClass;
140 }
141 
142 const MCPhysReg*
143 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
144   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
145   if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) {
146     if (Subtarget.hasVSX())
147       return CSR_64_AllRegs_VSX_SaveList;
148     if (Subtarget.hasAltivec())
149       return CSR_64_AllRegs_Altivec_SaveList;
150     return CSR_64_AllRegs_SaveList;
151   }
152 
153   if (Subtarget.isDarwinABI())
154     return TM.isPPC64()
155                ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
156                                          : CSR_Darwin64_SaveList)
157                : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
158                                          : CSR_Darwin32_SaveList);
159 
160   if (TM.isPPC64() && MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
161     return CSR_SRV464_TLS_PE_SaveList;
162 
163   if (Subtarget.hasSPE())
164     return CSR_SVR432_SPE_SaveList;
165 
166   // On PPC64, we might need to save r2 (but only if it is not reserved).
167   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2);
168 
169   if (MF->getFunction().getCallingConv() == CallingConv::Cold) {
170     return TM.isPPC64()
171                ? (Subtarget.hasAltivec()
172                       ? (SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
173                                 : CSR_SVR64_ColdCC_Altivec_SaveList)
174                       : (SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
175                                 : CSR_SVR64_ColdCC_SaveList))
176                : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_SaveList
177                                          : CSR_SVR32_ColdCC_SaveList);
178   }
179 
180   return TM.isPPC64()
181              ? (Subtarget.hasAltivec()
182                     ? (SaveR2 ? CSR_SVR464_R2_Altivec_SaveList
183                               : CSR_SVR464_Altivec_SaveList)
184                     : (SaveR2 ? CSR_SVR464_R2_SaveList : CSR_SVR464_SaveList))
185              : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_SaveList
186                                        : CSR_SVR432_SaveList);
187 }
188 
189 const MCPhysReg *
190 PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction *MF) const {
191   assert(MF && "Invalid MachineFunction pointer.");
192   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
193   if (Subtarget.isDarwinABI())
194     return nullptr;
195   if (!TM.isPPC64())
196     return nullptr;
197   if (MF->getFunction().getCallingConv() != CallingConv::CXX_FAST_TLS)
198     return nullptr;
199   if (!MF->getInfo<PPCFunctionInfo>()->isSplitCSR())
200     return nullptr;
201 
202   // On PPC64, we might need to save r2 (but only if it is not reserved).
203   bool SaveR2 = !getReservedRegs(*MF).test(PPC::X2);
204   if (Subtarget.hasAltivec())
205     return SaveR2
206       ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList
207       : CSR_SVR464_Altivec_ViaCopy_SaveList;
208   else
209     return SaveR2
210       ? CSR_SVR464_R2_ViaCopy_SaveList
211       : CSR_SVR464_ViaCopy_SaveList;
212 }
213 
214 const uint32_t *
215 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
216                                       CallingConv::ID CC) const {
217   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
218   if (CC == CallingConv::AnyReg) {
219     if (Subtarget.hasVSX())
220       return CSR_64_AllRegs_VSX_RegMask;
221     if (Subtarget.hasAltivec())
222       return CSR_64_AllRegs_Altivec_RegMask;
223     return CSR_64_AllRegs_RegMask;
224   }
225 
226   if (Subtarget.isDarwinABI())
227     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_RegMask
228                                                   : CSR_Darwin64_RegMask)
229                         : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_RegMask
230                                                   : CSR_Darwin32_RegMask);
231   if (Subtarget.isAIXABI()) {
232     assert(!Subtarget.hasAltivec() && "Altivec is not implemented on AIX yet.");
233     return TM.isPPC64() ? CSR_AIX64_RegMask : CSR_AIX32_RegMask;
234   }
235 
236   if (CC == CallingConv::Cold) {
237     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
238                                                   : CSR_SVR64_ColdCC_RegMask)
239                         : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
240                                                   : CSR_SVR32_ColdCC_RegMask);
241   }
242 
243   return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR464_Altivec_RegMask
244                                                 : CSR_SVR464_RegMask)
245                       : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_RegMask
246                                                 : CSR_SVR432_RegMask);
247 }
248 
249 const uint32_t*
250 PPCRegisterInfo::getNoPreservedMask() const {
251   return CSR_NoRegs_RegMask;
252 }
253 
254 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
255   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
256     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
257 }
258 
259 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
260   BitVector Reserved(getNumRegs());
261   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
262   const PPCFrameLowering *TFI = getFrameLowering(MF);
263 
264   // The ZERO register is not really a register, but the representation of r0
265   // when used in instructions that treat r0 as the constant 0.
266   markSuperRegs(Reserved, PPC::ZERO);
267 
268   // The FP register is also not really a register, but is the representation
269   // of the frame pointer register used by ISD::FRAMEADDR.
270   markSuperRegs(Reserved, PPC::FP);
271 
272   // The BP register is also not really a register, but is the representation
273   // of the base pointer register used by setjmp.
274   markSuperRegs(Reserved, PPC::BP);
275 
276   // The counter registers must be reserved so that counter-based loops can
277   // be correctly formed (and the mtctr instructions are not DCE'd).
278   markSuperRegs(Reserved, PPC::CTR);
279   markSuperRegs(Reserved, PPC::CTR8);
280 
281   markSuperRegs(Reserved, PPC::R1);
282   markSuperRegs(Reserved, PPC::LR);
283   markSuperRegs(Reserved, PPC::LR8);
284   markSuperRegs(Reserved, PPC::RM);
285 
286   if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
287     markSuperRegs(Reserved, PPC::VRSAVE);
288 
289   // The SVR4 ABI reserves r2 and r13
290   if (Subtarget.isSVR4ABI()) {
291     // We only reserve r2 if we need to use the TOC pointer. If we have no
292     // explicit uses of the TOC pointer (meaning we're a leaf function with
293     // no constant-pool loads, etc.) and we have no potential uses inside an
294     // inline asm block, then we can treat r2 has an ordinary callee-saved
295     // register.
296     const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
297     if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
298       markSuperRegs(Reserved, PPC::R2);  // System-reserved register
299     markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
300   }
301 
302   // On PPC64, r13 is the thread pointer. Never allocate this register.
303   if (TM.isPPC64())
304     markSuperRegs(Reserved, PPC::R13);
305 
306   if (TFI->needsFP(MF))
307     markSuperRegs(Reserved, PPC::R31);
308 
309   bool IsPositionIndependent = TM.isPositionIndependent();
310   if (hasBasePointer(MF)) {
311     if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
312       markSuperRegs(Reserved, PPC::R29);
313     else
314       markSuperRegs(Reserved, PPC::R30);
315   }
316 
317   if (Subtarget.isSVR4ABI() && !TM.isPPC64() && IsPositionIndependent)
318     markSuperRegs(Reserved, PPC::R30);
319 
320   // Reserve Altivec registers when Altivec is unavailable.
321   if (!Subtarget.hasAltivec())
322     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
323          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
324       markSuperRegs(Reserved, *I);
325 
326   assert(checkAllSuperRegsMarked(Reserved));
327   return Reserved;
328 }
329 
330 bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const {
331   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
332   const PPCInstrInfo *InstrInfo =  Subtarget.getInstrInfo();
333   const MachineFrameInfo &MFI = MF.getFrameInfo();
334   const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
335 
336   // If the callee saved info is invalid we have to default to true for safety.
337   if (!MFI.isCalleeSavedInfoValid())
338     return true;
339 
340   // We will require the use of X-Forms because the frame is larger than what
341   // can be represented in signed 16 bits that fit in the immediate of a D-Form.
342   // If we need an X-Form then we need a register to store the address offset.
343   unsigned FrameSize = MFI.getStackSize();
344   // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
345   if (FrameSize & ~0x7FFF)
346     return true;
347 
348   // The callee saved info is valid so it can be traversed.
349   // Checking for registers that need saving that do not have load or store
350   // forms where the address offset is an immediate.
351   for (unsigned i = 0; i < Info.size(); i++) {
352     int FrIdx = Info[i].getFrameIdx();
353     unsigned Reg = Info[i].getReg();
354 
355     unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(Reg);
356     if (!MFI.isFixedObjectIndex(FrIdx)) {
357       // This is not a fixed object. If it requires alignment then we may still
358       // need to use the XForm.
359       if (offsetMinAlignForOpcode(Opcode) > 1)
360         return true;
361     }
362 
363     // This is eiher:
364     // 1) A fixed frame index object which we know are aligned so
365     // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
366     // need to consider the alignement here.
367     // 2) A not fixed object but in that case we now know that the min required
368     // alignment is no more than 1 based on the previous check.
369     if (InstrInfo->isXFormMemOp(Opcode))
370       return true;
371   }
372   return false;
373 }
374 
375 bool PPCRegisterInfo::isCallerPreservedPhysReg(unsigned PhysReg,
376                                                const MachineFunction &MF) const {
377   assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
378   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
379   const MachineFrameInfo &MFI = MF.getFrameInfo();
380   if (!TM.isPPC64())
381     return false;
382 
383   if (!Subtarget.isSVR4ABI())
384     return false;
385   if (PhysReg == PPC::X2)
386     // X2 is guaranteed to be preserved within a function if it is reserved.
387     // The reason it's reserved is that it's the TOC pointer (and the function
388     // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
389     // with no TOC access), we can't claim that it is preserved.
390     return (getReservedRegs(MF).test(PPC::X2));
391   if (StackPtrConst && (PhysReg == PPC::X1) && !MFI.hasVarSizedObjects()
392       && !MFI.hasOpaqueSPAdjustment())
393     // The value of the stack pointer does not change within a function after
394     // the prologue and before the epilogue if there are no dynamic allocations
395     // and no inline asm which clobbers X1.
396     return true;
397   return false;
398 }
399 
400 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
401                                               MachineFunction &MF) const {
402   const PPCFrameLowering *TFI = getFrameLowering(MF);
403   const unsigned DefaultSafety = 1;
404 
405   switch (RC->getID()) {
406   default:
407     return 0;
408   case PPC::G8RC_NOX0RegClassID:
409   case PPC::GPRC_NOR0RegClassID:
410   case PPC::SPERCRegClassID:
411   case PPC::SPE4RCRegClassID:
412   case PPC::G8RCRegClassID:
413   case PPC::GPRCRegClassID: {
414     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
415     return 32 - FP - DefaultSafety;
416   }
417   case PPC::F8RCRegClassID:
418   case PPC::F4RCRegClassID:
419   case PPC::QFRCRegClassID:
420   case PPC::QSRCRegClassID:
421   case PPC::QBRCRegClassID:
422   case PPC::VRRCRegClassID:
423   case PPC::VFRCRegClassID:
424   case PPC::VSLRCRegClassID:
425     return 32 - DefaultSafety;
426   case PPC::VSRCRegClassID:
427   case PPC::VSFRCRegClassID:
428   case PPC::VSSRCRegClassID:
429     return 64 - DefaultSafety;
430   case PPC::CRRCRegClassID:
431     return 8 - DefaultSafety;
432   }
433 }
434 
435 const TargetRegisterClass *
436 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
437                                            const MachineFunction &MF) const {
438   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
439   if (Subtarget.hasVSX()) {
440     // With VSX, we can inflate various sub-register classes to the full VSX
441     // register set.
442 
443     // For Power9 we allow the user to enable GPR to vector spills.
444     // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
445     // support to spill GPRC.
446     if (TM.isELFv2ABI()) {
447       if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
448           RC == &PPC::G8RCRegClass) {
449         InflateGP8RC++;
450         return &PPC::SPILLTOVSRRCRegClass;
451       }
452       if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
453         InflateGPRC++;
454     }
455     if (RC == &PPC::F8RCRegClass)
456       return &PPC::VSFRCRegClass;
457     else if (RC == &PPC::VRRCRegClass)
458       return &PPC::VSRCRegClass;
459     else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector())
460       return &PPC::VSSRCRegClass;
461   }
462 
463   return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
464 }
465 
466 //===----------------------------------------------------------------------===//
467 // Stack Frame Processing methods
468 //===----------------------------------------------------------------------===//
469 
470 /// lowerDynamicAlloc - Generate the code for allocating an object in the
471 /// current frame.  The sequence of code will be in the general form
472 ///
473 ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
474 ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
475 ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
476 ///
477 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
478   // Get the instruction.
479   MachineInstr &MI = *II;
480   // Get the instruction's basic block.
481   MachineBasicBlock &MBB = *MI.getParent();
482   // Get the basic block's function.
483   MachineFunction &MF = *MBB.getParent();
484   // Get the frame info.
485   MachineFrameInfo &MFI = MF.getFrameInfo();
486   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
487   // Get the instruction info.
488   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
489   // Determine whether 64-bit pointers are used.
490   bool LP64 = TM.isPPC64();
491   DebugLoc dl = MI.getDebugLoc();
492 
493   // Get the maximum call stack size.
494   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
495   // Get the total frame size.
496   unsigned FrameSize = MFI.getStackSize();
497 
498   // Get stack alignments.
499   const PPCFrameLowering *TFI = getFrameLowering(MF);
500   unsigned TargetAlign = TFI->getStackAlignment();
501   unsigned MaxAlign = MFI.getMaxAlignment();
502   assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
503          "Maximum call-frame size not sufficiently aligned");
504 
505   // Determine the previous frame's address.  If FrameSize can't be
506   // represented as 16 bits or we need special alignment, then we load the
507   // previous frame's address from 0(SP).  Why not do an addis of the hi?
508   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
509   // Constructing the constant and adding would take 3 instructions.
510   // Fortunately, a frame greater than 32K is rare.
511   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
512   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
513   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
514 
515   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
516     if (LP64)
517       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), Reg)
518         .addReg(PPC::X31)
519         .addImm(FrameSize);
520     else
521       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
522         .addReg(PPC::R31)
523         .addImm(FrameSize);
524   } else if (LP64) {
525     BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
526       .addImm(0)
527       .addReg(PPC::X1);
528   } else {
529     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
530       .addImm(0)
531       .addReg(PPC::R1);
532   }
533 
534   bool KillNegSizeReg = MI.getOperand(1).isKill();
535   unsigned NegSizeReg = MI.getOperand(1).getReg();
536 
537   // Grow the stack and update the stack pointer link, then determine the
538   // address of new allocated space.
539   if (LP64) {
540     if (MaxAlign > TargetAlign) {
541       unsigned UnalNegSizeReg = NegSizeReg;
542       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
543 
544       // Unfortunately, there is no andi, only andi., and we can't insert that
545       // here because we might clobber cr0 while it is live.
546       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
547         .addImm(~(MaxAlign-1));
548 
549       unsigned NegSizeReg1 = NegSizeReg;
550       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
551       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
552         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
553         .addReg(NegSizeReg1, RegState::Kill);
554       KillNegSizeReg = true;
555     }
556 
557     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
558       .addReg(Reg, RegState::Kill)
559       .addReg(PPC::X1)
560       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
561     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
562       .addReg(PPC::X1)
563       .addImm(maxCallFrameSize);
564   } else {
565     if (MaxAlign > TargetAlign) {
566       unsigned UnalNegSizeReg = NegSizeReg;
567       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
568 
569       // Unfortunately, there is no andi, only andi., and we can't insert that
570       // here because we might clobber cr0 while it is live.
571       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
572         .addImm(~(MaxAlign-1));
573 
574       unsigned NegSizeReg1 = NegSizeReg;
575       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
576       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
577         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
578         .addReg(NegSizeReg1, RegState::Kill);
579       KillNegSizeReg = true;
580     }
581 
582     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
583       .addReg(Reg, RegState::Kill)
584       .addReg(PPC::R1)
585       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
586     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
587       .addReg(PPC::R1)
588       .addImm(maxCallFrameSize);
589   }
590 
591   // Discard the DYNALLOC instruction.
592   MBB.erase(II);
593 }
594 
595 void PPCRegisterInfo::lowerDynamicAreaOffset(
596     MachineBasicBlock::iterator II) const {
597   // Get the instruction.
598   MachineInstr &MI = *II;
599   // Get the instruction's basic block.
600   MachineBasicBlock &MBB = *MI.getParent();
601   // Get the basic block's function.
602   MachineFunction &MF = *MBB.getParent();
603   // Get the frame info.
604   MachineFrameInfo &MFI = MF.getFrameInfo();
605   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
606   // Get the instruction info.
607   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
608 
609   unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
610   bool is64Bit = TM.isPPC64();
611   DebugLoc dl = MI.getDebugLoc();
612   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
613           MI.getOperand(0).getReg())
614       .addImm(maxCallFrameSize);
615   MBB.erase(II);
616 }
617 
618 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
619 /// reserving a whole register (R0), we scrounge for one here. This generates
620 /// code like this:
621 ///
622 ///   mfcr rA                  ; Move the conditional register into GPR rA.
623 ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
624 ///   stw rA, FI               ; Store rA to the frame.
625 ///
626 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
627                                       unsigned FrameIndex) const {
628   // Get the instruction.
629   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
630   // Get the instruction's basic block.
631   MachineBasicBlock &MBB = *MI.getParent();
632   MachineFunction &MF = *MBB.getParent();
633   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
634   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
635   DebugLoc dl = MI.getDebugLoc();
636 
637   bool LP64 = TM.isPPC64();
638   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
639   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
640 
641   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
642   unsigned SrcReg = MI.getOperand(0).getReg();
643 
644   // We need to store the CR in the low 4-bits of the saved value. First, issue
645   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
646   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
647       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
648 
649   // If the saved register wasn't CR0, shift the bits left so that they are in
650   // CR0's slot.
651   if (SrcReg != PPC::CR0) {
652     unsigned Reg1 = Reg;
653     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
654 
655     // rlwinm rA, rA, ShiftBits, 0, 31.
656     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
657       .addReg(Reg1, RegState::Kill)
658       .addImm(getEncodingValue(SrcReg) * 4)
659       .addImm(0)
660       .addImm(31);
661   }
662 
663   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
664                     .addReg(Reg, RegState::Kill),
665                     FrameIndex);
666 
667   // Discard the pseudo instruction.
668   MBB.erase(II);
669 }
670 
671 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
672                                       unsigned FrameIndex) const {
673   // Get the instruction.
674   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
675   // Get the instruction's basic block.
676   MachineBasicBlock &MBB = *MI.getParent();
677   MachineFunction &MF = *MBB.getParent();
678   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
679   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
680   DebugLoc dl = MI.getDebugLoc();
681 
682   bool LP64 = TM.isPPC64();
683   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
684   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
685 
686   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
687   unsigned DestReg = MI.getOperand(0).getReg();
688   assert(MI.definesRegister(DestReg) &&
689     "RESTORE_CR does not define its destination");
690 
691   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
692                               Reg), FrameIndex);
693 
694   // If the reloaded register isn't CR0, shift the bits right so that they are
695   // in the right CR's slot.
696   if (DestReg != PPC::CR0) {
697     unsigned Reg1 = Reg;
698     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
699 
700     unsigned ShiftBits = getEncodingValue(DestReg)*4;
701     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
702     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
703              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
704              .addImm(31);
705   }
706 
707   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
708              .addReg(Reg, RegState::Kill);
709 
710   // Discard the pseudo instruction.
711   MBB.erase(II);
712 }
713 
714 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
715                                          unsigned FrameIndex) const {
716   // Get the instruction.
717   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
718   // Get the instruction's basic block.
719   MachineBasicBlock &MBB = *MI.getParent();
720   MachineFunction &MF = *MBB.getParent();
721   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
722   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
723   const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
724   DebugLoc dl = MI.getDebugLoc();
725 
726   bool LP64 = TM.isPPC64();
727   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
728   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
729 
730   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
731   unsigned SrcReg = MI.getOperand(0).getReg();
732 
733   // Search up the BB to find the definition of the CR bit.
734   MachineBasicBlock::reverse_iterator Ins;
735   unsigned CRBitSpillDistance = 0;
736   for (Ins = MI; Ins != MBB.rend(); Ins++) {
737     // Definition found.
738     if (Ins->modifiesRegister(SrcReg, TRI))
739       break;
740     // Unable to find CR bit definition within maximum search distance.
741     if (CRBitSpillDistance == MaxCRBitSpillDist) {
742       Ins = MI;
743       break;
744     }
745     // Skip debug instructions when counting CR bit spill distance.
746     if (!Ins->isDebugInstr())
747       CRBitSpillDistance++;
748   }
749 
750   // Unable to find the definition of the CR bit in the MBB.
751   if (Ins == MBB.rend())
752     Ins = MI;
753 
754   // There is no need to extract the CR bit if its value is already known.
755   switch (Ins->getOpcode()) {
756   case PPC::CRUNSET:
757     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
758       .addImm(0);
759     break;
760   case PPC::CRSET:
761     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
762       .addImm(-32768);
763     break;
764   default:
765     // We need to move the CR field that contains the CR bit we are spilling.
766     // The super register may not be explicitly defined (i.e. it can be defined
767     // by a CR-logical that only defines the subreg) so we state that the CR
768     // field is undef. Also, in order to preserve the kill flag on the CR bit,
769     // we add it as an implicit use.
770     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
771       .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
772       .addReg(SrcReg,
773               RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
774 
775     // If the saved register wasn't CR0LT, shift the bits left so that the bit
776     // to store is the first one. Mask all but that bit.
777     unsigned Reg1 = Reg;
778     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
779 
780     // rlwinm rA, rA, ShiftBits, 0, 0.
781     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
782       .addReg(Reg1, RegState::Kill)
783       .addImm(getEncodingValue(SrcReg))
784       .addImm(0).addImm(0);
785   }
786   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
787                     .addReg(Reg, RegState::Kill),
788                     FrameIndex);
789 
790   // Discard the pseudo instruction.
791   MBB.erase(II);
792 }
793 
794 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
795                                       unsigned FrameIndex) const {
796   // Get the instruction.
797   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
798   // Get the instruction's basic block.
799   MachineBasicBlock &MBB = *MI.getParent();
800   MachineFunction &MF = *MBB.getParent();
801   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
802   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
803   DebugLoc dl = MI.getDebugLoc();
804 
805   bool LP64 = TM.isPPC64();
806   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
807   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
808 
809   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
810   unsigned DestReg = MI.getOperand(0).getReg();
811   assert(MI.definesRegister(DestReg) &&
812     "RESTORE_CRBIT does not define its destination");
813 
814   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
815                               Reg), FrameIndex);
816 
817   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
818 
819   unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
820   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
821           .addReg(getCRFromCRBit(DestReg));
822 
823   unsigned ShiftBits = getEncodingValue(DestReg);
824   // rlwimi r11, r10, 32-ShiftBits, ..., ...
825   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
826       .addReg(RegO, RegState::Kill)
827       .addReg(Reg, RegState::Kill)
828       .addImm(ShiftBits ? 32 - ShiftBits : 0)
829       .addImm(ShiftBits)
830       .addImm(ShiftBits);
831 
832   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
833           getCRFromCRBit(DestReg))
834       .addReg(RegO, RegState::Kill)
835       // Make sure we have a use dependency all the way through this
836       // sequence of instructions. We can't have the other bits in the CR
837       // modified in between the mfocrf and the mtocrf.
838       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
839 
840   // Discard the pseudo instruction.
841   MBB.erase(II);
842 }
843 
844 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
845                                           unsigned FrameIndex) const {
846   // Get the instruction.
847   MachineInstr &MI = *II;       // ; SPILL_VRSAVE <SrcReg>, <offset>
848   // Get the instruction's basic block.
849   MachineBasicBlock &MBB = *MI.getParent();
850   MachineFunction &MF = *MBB.getParent();
851   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
852   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
853   DebugLoc dl = MI.getDebugLoc();
854 
855   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
856   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
857   unsigned SrcReg = MI.getOperand(0).getReg();
858 
859   BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
860       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
861 
862   addFrameReference(
863       BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill),
864       FrameIndex);
865 
866   // Discard the pseudo instruction.
867   MBB.erase(II);
868 }
869 
870 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
871                                          unsigned FrameIndex) const {
872   // Get the instruction.
873   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_VRSAVE <offset>
874   // Get the instruction's basic block.
875   MachineBasicBlock &MBB = *MI.getParent();
876   MachineFunction &MF = *MBB.getParent();
877   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
878   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
879   DebugLoc dl = MI.getDebugLoc();
880 
881   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
882   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
883   unsigned DestReg = MI.getOperand(0).getReg();
884   assert(MI.definesRegister(DestReg) &&
885     "RESTORE_VRSAVE does not define its destination");
886 
887   addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
888                               Reg), FrameIndex);
889 
890   BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
891              .addReg(Reg, RegState::Kill);
892 
893   // Discard the pseudo instruction.
894   MBB.erase(II);
895 }
896 
897 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
898                                            unsigned Reg, int &FrameIdx) const {
899   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
900   // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
901   // ABI, return true to prevent allocating an additional frame slot.
902   // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
903   // is arbitrary and will be subsequently ignored.  For 32-bit, we have
904   // previously created the stack slot if needed, so return its FrameIdx.
905   if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
906     if (TM.isPPC64())
907       FrameIdx = 0;
908     else {
909       const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
910       FrameIdx = FI->getCRSpillFrameIndex();
911     }
912     return true;
913   }
914   return false;
915 }
916 
917 // If the offset must be a multiple of some value, return what that value is.
918 static unsigned offsetMinAlignForOpcode(unsigned OpC) {
919   switch (OpC) {
920   default:
921     return 1;
922   case PPC::LWA:
923   case PPC::LWA_32:
924   case PPC::LD:
925   case PPC::LDU:
926   case PPC::STD:
927   case PPC::STDU:
928   case PPC::DFLOADf32:
929   case PPC::DFLOADf64:
930   case PPC::DFSTOREf32:
931   case PPC::DFSTOREf64:
932   case PPC::LXSD:
933   case PPC::LXSSP:
934   case PPC::STXSD:
935   case PPC::STXSSP:
936     return 4;
937   case PPC::LXV:
938   case PPC::STXV:
939     return 16;
940   }
941 }
942 
943 // If the offset must be a multiple of some value, return what that value is.
944 static unsigned offsetMinAlign(const MachineInstr &MI) {
945   unsigned OpC = MI.getOpcode();
946   return offsetMinAlignForOpcode(OpC);
947 }
948 
949 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
950 static unsigned getOffsetONFromFION(const MachineInstr &MI,
951                                     unsigned FIOperandNum) {
952   // Take into account whether it's an add or mem instruction
953   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
954   if (MI.isInlineAsm())
955     OffsetOperandNo = FIOperandNum - 1;
956   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
957            MI.getOpcode() == TargetOpcode::PATCHPOINT)
958     OffsetOperandNo = FIOperandNum + 1;
959 
960   return OffsetOperandNo;
961 }
962 
963 void
964 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
965                                      int SPAdj, unsigned FIOperandNum,
966                                      RegScavenger *RS) const {
967   assert(SPAdj == 0 && "Unexpected");
968 
969   // Get the instruction.
970   MachineInstr &MI = *II;
971   // Get the instruction's basic block.
972   MachineBasicBlock &MBB = *MI.getParent();
973   // Get the basic block's function.
974   MachineFunction &MF = *MBB.getParent();
975   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
976   // Get the instruction info.
977   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
978   // Get the frame info.
979   MachineFrameInfo &MFI = MF.getFrameInfo();
980   DebugLoc dl = MI.getDebugLoc();
981 
982   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
983 
984   // Get the frame index.
985   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
986 
987   // Get the frame pointer save index.  Users of this index are primarily
988   // DYNALLOC instructions.
989   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
990   int FPSI = FI->getFramePointerSaveIndex();
991   // Get the instruction opcode.
992   unsigned OpC = MI.getOpcode();
993 
994   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
995     lowerDynamicAreaOffset(II);
996     return;
997   }
998 
999   // Special case for dynamic alloca.
1000   if (FPSI && FrameIndex == FPSI &&
1001       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
1002     lowerDynamicAlloc(II);
1003     return;
1004   }
1005 
1006   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
1007   if (OpC == PPC::SPILL_CR) {
1008     lowerCRSpilling(II, FrameIndex);
1009     return;
1010   } else if (OpC == PPC::RESTORE_CR) {
1011     lowerCRRestore(II, FrameIndex);
1012     return;
1013   } else if (OpC == PPC::SPILL_CRBIT) {
1014     lowerCRBitSpilling(II, FrameIndex);
1015     return;
1016   } else if (OpC == PPC::RESTORE_CRBIT) {
1017     lowerCRBitRestore(II, FrameIndex);
1018     return;
1019   } else if (OpC == PPC::SPILL_VRSAVE) {
1020     lowerVRSAVESpilling(II, FrameIndex);
1021     return;
1022   } else if (OpC == PPC::RESTORE_VRSAVE) {
1023     lowerVRSAVERestore(II, FrameIndex);
1024     return;
1025   }
1026 
1027   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
1028   MI.getOperand(FIOperandNum).ChangeToRegister(
1029     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
1030 
1031   // If the instruction is not present in ImmToIdxMap, then it has no immediate
1032   // form (and must be r+r).
1033   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
1034                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
1035 
1036   // Now add the frame object offset to the offset from r1.
1037   int Offset = MFI.getObjectOffset(FrameIndex);
1038   Offset += MI.getOperand(OffsetOperandNo).getImm();
1039 
1040   // If we're not using a Frame Pointer that has been set to the value of the
1041   // SP before having the stack size subtracted from it, then add the stack size
1042   // to Offset to get the correct offset.
1043   // Naked functions have stack size 0, although getStackSize may not reflect
1044   // that because we didn't call all the pieces that compute it for naked
1045   // functions.
1046   if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
1047     if (!(hasBasePointer(MF) && FrameIndex < 0))
1048       Offset += MFI.getStackSize();
1049   }
1050 
1051   // If we can, encode the offset directly into the instruction.  If this is a
1052   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
1053   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
1054   // clear can be encoded.  This is extremely uncommon, because normally you
1055   // only "std" to a stack slot that is at least 4-byte aligned, but it can
1056   // happen in invalid code.
1057   assert(OpC != PPC::DBG_VALUE &&
1058          "This should be handled in a target-independent way");
1059   if (!noImmForm && ((isInt<16>(Offset) &&
1060                       ((Offset % offsetMinAlign(MI)) == 0)) ||
1061                      OpC == TargetOpcode::STACKMAP ||
1062                      OpC == TargetOpcode::PATCHPOINT)) {
1063     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1064     return;
1065   }
1066 
1067   // The offset doesn't fit into a single register, scavenge one to build the
1068   // offset in.
1069 
1070   bool is64Bit = TM.isPPC64();
1071   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1072   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1073   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
1074   unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
1075            SReg = MF.getRegInfo().createVirtualRegister(RC);
1076 
1077   // Insert a set of rA with the full offset value before the ld, st, or add
1078   if (isInt<16>(Offset))
1079     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
1080       .addImm(Offset);
1081   else {
1082     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
1083       .addImm(Offset >> 16);
1084     BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
1085       .addReg(SRegHi, RegState::Kill)
1086       .addImm(Offset);
1087   }
1088 
1089   // Convert into indexed form of the instruction:
1090   //
1091   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
1092   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
1093   unsigned OperandBase;
1094 
1095   if (noImmForm)
1096     OperandBase = 1;
1097   else if (OpC != TargetOpcode::INLINEASM &&
1098            OpC != TargetOpcode::INLINEASM_BR) {
1099     assert(ImmToIdxMap.count(OpC) &&
1100            "No indexed form of load or store available!");
1101     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
1102     MI.setDesc(TII.get(NewOpcode));
1103     OperandBase = 1;
1104   } else {
1105     OperandBase = OffsetOperandNo;
1106   }
1107 
1108   unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
1109   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
1110   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
1111 }
1112 
1113 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
1114   const PPCFrameLowering *TFI = getFrameLowering(MF);
1115 
1116   if (!TM.isPPC64())
1117     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
1118   else
1119     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
1120 }
1121 
1122 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
1123   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1124   if (!hasBasePointer(MF))
1125     return getFrameRegister(MF);
1126 
1127   if (TM.isPPC64())
1128     return PPC::X30;
1129 
1130   if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
1131     return PPC::R29;
1132 
1133   return PPC::R30;
1134 }
1135 
1136 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
1137   if (!EnableBasePointer)
1138     return false;
1139   if (AlwaysBasePointer)
1140     return true;
1141 
1142   // If we need to realign the stack, then the stack pointer can no longer
1143   // serve as an offset into the caller's stack space. As a result, we need a
1144   // base pointer.
1145   return needsStackRealignment(MF);
1146 }
1147 
1148 /// Returns true if the instruction's frame index
1149 /// reference would be better served by a base register other than FP
1150 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1151 /// references it should create new base registers for.
1152 bool PPCRegisterInfo::
1153 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
1154   assert(Offset < 0 && "Local offset must be negative");
1155 
1156   // It's the load/store FI references that cause issues, as it can be difficult
1157   // to materialize the offset if it won't fit in the literal field. Estimate
1158   // based on the size of the local frame and some conservative assumptions
1159   // about the rest of the stack frame (note, this is pre-regalloc, so
1160   // we don't know everything for certain yet) whether this offset is likely
1161   // to be out of range of the immediate. Return true if so.
1162 
1163   // We only generate virtual base registers for loads and stores that have
1164   // an r+i form. Return false for everything else.
1165   unsigned OpC = MI->getOpcode();
1166   if (!ImmToIdxMap.count(OpC))
1167     return false;
1168 
1169   // Don't generate a new virtual base register just to add zero to it.
1170   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
1171       MI->getOperand(2).getImm() == 0)
1172     return false;
1173 
1174   MachineBasicBlock &MBB = *MI->getParent();
1175   MachineFunction &MF = *MBB.getParent();
1176   const PPCFrameLowering *TFI = getFrameLowering(MF);
1177   unsigned StackEst = TFI->determineFrameLayout(MF, true);
1178 
1179   // If we likely don't need a stack frame, then we probably don't need a
1180   // virtual base register either.
1181   if (!StackEst)
1182     return false;
1183 
1184   // Estimate an offset from the stack pointer.
1185   // The incoming offset is relating to the SP at the start of the function,
1186   // but when we access the local it'll be relative to the SP after local
1187   // allocation, so adjust our SP-relative offset by that allocation size.
1188   Offset += StackEst;
1189 
1190   // The frame pointer will point to the end of the stack, so estimate the
1191   // offset as the difference between the object offset and the FP location.
1192   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
1193 }
1194 
1195 /// Insert defining instruction(s) for BaseReg to
1196 /// be a pointer to FrameIdx at the beginning of the basic block.
1197 void PPCRegisterInfo::
1198 materializeFrameBaseRegister(MachineBasicBlock *MBB,
1199                              unsigned BaseReg, int FrameIdx,
1200                              int64_t Offset) const {
1201   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
1202 
1203   MachineBasicBlock::iterator Ins = MBB->begin();
1204   DebugLoc DL;                  // Defaults to "unknown"
1205   if (Ins != MBB->end())
1206     DL = Ins->getDebugLoc();
1207 
1208   const MachineFunction &MF = *MBB->getParent();
1209   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1210   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1211   const MCInstrDesc &MCID = TII.get(ADDriOpc);
1212   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1213   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
1214 
1215   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1216     .addFrameIndex(FrameIdx).addImm(Offset);
1217 }
1218 
1219 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
1220                                         int64_t Offset) const {
1221   unsigned FIOperandNum = 0;
1222   while (!MI.getOperand(FIOperandNum).isFI()) {
1223     ++FIOperandNum;
1224     assert(FIOperandNum < MI.getNumOperands() &&
1225            "Instr doesn't have FrameIndex operand!");
1226   }
1227 
1228   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
1229   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
1230   Offset += MI.getOperand(OffsetOperandNo).getImm();
1231   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
1232 
1233   MachineBasicBlock &MBB = *MI.getParent();
1234   MachineFunction &MF = *MBB.getParent();
1235   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
1236   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
1237   const MCInstrDesc &MCID = MI.getDesc();
1238   MachineRegisterInfo &MRI = MF.getRegInfo();
1239   MRI.constrainRegClass(BaseReg,
1240                         TII.getRegClass(MCID, FIOperandNum, this, MF));
1241 }
1242 
1243 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1244                                          unsigned BaseReg,
1245                                          int64_t Offset) const {
1246   unsigned FIOperandNum = 0;
1247   while (!MI->getOperand(FIOperandNum).isFI()) {
1248     ++FIOperandNum;
1249     assert(FIOperandNum < MI->getNumOperands() &&
1250            "Instr doesn't have FrameIndex operand!");
1251   }
1252 
1253   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
1254   Offset += MI->getOperand(OffsetOperandNo).getImm();
1255 
1256   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
1257          MI->getOpcode() == TargetOpcode::STACKMAP ||
1258          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
1259          (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
1260 }
1261