Home | History | Annotate | Download | only in PowerPC
      1 //===-- PPCRegisterInfo.cpp - PowerPC Register 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 PowerPC implementation of the TargetRegisterInfo
     11 // class.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "PPCRegisterInfo.h"
     16 #include "PPC.h"
     17 #include "PPCFrameLowering.h"
     18 #include "PPCInstrBuilder.h"
     19 #include "PPCMachineFunctionInfo.h"
     20 #include "PPCSubtarget.h"
     21 #include "PPCTargetMachine.h"
     22 #include "llvm/ADT/BitVector.h"
     23 #include "llvm/ADT/STLExtras.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/IR/CallingConv.h"
     31 #include "llvm/IR/Constants.h"
     32 #include "llvm/IR/Function.h"
     33 #include "llvm/IR/Type.h"
     34 #include "llvm/Support/CommandLine.h"
     35 #include "llvm/Support/Debug.h"
     36 #include "llvm/Support/ErrorHandling.h"
     37 #include "llvm/Support/MathExtras.h"
     38 #include "llvm/Support/raw_ostream.h"
     39 #include "llvm/Target/TargetFrameLowering.h"
     40 #include "llvm/Target/TargetInstrInfo.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 static cl::opt<bool>
     53 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
     54          cl::desc("Enable use of a base pointer for complex stack frames"));
     55 
     56 static cl::opt<bool>
     57 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
     58          cl::desc("Force the use of a base pointer in every function"));
     59 
     60 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
     61   : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
     62                        TM.isPPC64() ? 0 : 1,
     63                        TM.isPPC64() ? 0 : 1),
     64     TM(TM) {
     65   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
     66   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
     67   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
     68   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
     69   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
     70   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
     71   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
     72   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
     73   ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
     74 
     75   // 64-bit
     76   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
     77   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
     78   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
     79   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
     80   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
     81 }
     82 
     83 /// getPointerRegClass - Return the register class to use to hold pointers.
     84 /// This is used for addressing modes.
     85 const TargetRegisterClass *
     86 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
     87                                                                        const {
     88   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
     89   // when it checks for ZERO folding.
     90   if (Kind == 1) {
     91     if (TM.isPPC64())
     92       return &PPC::G8RC_NOX0RegClass;
     93     return &PPC::GPRC_NOR0RegClass;
     94   }
     95 
     96   if (TM.isPPC64())
     97     return &PPC::G8RCRegClass;
     98   return &PPC::GPRCRegClass;
     99 }
    100 
    101 const MCPhysReg*
    102 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
    103   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
    104   if (MF->getFunction()->getCallingConv() == CallingConv::AnyReg) {
    105     if (Subtarget.hasVSX())
    106       return CSR_64_AllRegs_VSX_SaveList;
    107     if (Subtarget.hasAltivec())
    108       return CSR_64_AllRegs_Altivec_SaveList;
    109     return CSR_64_AllRegs_SaveList;
    110   }
    111 
    112   if (Subtarget.isDarwinABI())
    113     return TM.isPPC64()
    114                ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_SaveList
    115                                          : CSR_Darwin64_SaveList)
    116                : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_SaveList
    117                                          : CSR_Darwin32_SaveList);
    118 
    119   // On PPC64, we might need to save r2 (but only if it is not reserved).
    120   bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2);
    121 
    122   return TM.isPPC64()
    123              ? (Subtarget.hasAltivec()
    124                     ? (SaveR2 ? CSR_SVR464_R2_Altivec_SaveList
    125                               : CSR_SVR464_Altivec_SaveList)
    126                     : (SaveR2 ? CSR_SVR464_R2_SaveList : CSR_SVR464_SaveList))
    127              : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_SaveList
    128                                        : CSR_SVR432_SaveList);
    129 }
    130 
    131 const uint32_t *
    132 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
    133                                       CallingConv::ID CC) const {
    134   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    135   if (CC == CallingConv::AnyReg) {
    136     if (Subtarget.hasVSX())
    137       return CSR_64_AllRegs_VSX_RegMask;
    138     if (Subtarget.hasAltivec())
    139       return CSR_64_AllRegs_Altivec_RegMask;
    140     return CSR_64_AllRegs_RegMask;
    141   }
    142 
    143   if (Subtarget.isDarwinABI())
    144     return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_Darwin64_Altivec_RegMask
    145                                                   : CSR_Darwin64_RegMask)
    146                         : (Subtarget.hasAltivec() ? CSR_Darwin32_Altivec_RegMask
    147                                                   : CSR_Darwin32_RegMask);
    148 
    149   return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR464_Altivec_RegMask
    150                                                 : CSR_SVR464_RegMask)
    151                       : (Subtarget.hasAltivec() ? CSR_SVR432_Altivec_RegMask
    152                                                 : CSR_SVR432_RegMask);
    153 }
    154 
    155 const uint32_t*
    156 PPCRegisterInfo::getNoPreservedMask() const {
    157   return CSR_NoRegs_RegMask;
    158 }
    159 
    160 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
    161   for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
    162     Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
    163 }
    164 
    165 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
    166   BitVector Reserved(getNumRegs());
    167   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    168   const PPCFrameLowering *TFI = getFrameLowering(MF);
    169 
    170   // The ZERO register is not really a register, but the representation of r0
    171   // when used in instructions that treat r0 as the constant 0.
    172   Reserved.set(PPC::ZERO);
    173   Reserved.set(PPC::ZERO8);
    174 
    175   // The FP register is also not really a register, but is the representation
    176   // of the frame pointer register used by ISD::FRAMEADDR.
    177   Reserved.set(PPC::FP);
    178   Reserved.set(PPC::FP8);
    179 
    180   // The BP register is also not really a register, but is the representation
    181   // of the base pointer register used by setjmp.
    182   Reserved.set(PPC::BP);
    183   Reserved.set(PPC::BP8);
    184 
    185   // The counter registers must be reserved so that counter-based loops can
    186   // be correctly formed (and the mtctr instructions are not DCE'd).
    187   Reserved.set(PPC::CTR);
    188   Reserved.set(PPC::CTR8);
    189 
    190   Reserved.set(PPC::R1);
    191   Reserved.set(PPC::LR);
    192   Reserved.set(PPC::LR8);
    193   Reserved.set(PPC::RM);
    194 
    195   if (!Subtarget.isDarwinABI() || !Subtarget.hasAltivec())
    196     Reserved.set(PPC::VRSAVE);
    197 
    198   // The SVR4 ABI reserves r2 and r13
    199   if (Subtarget.isSVR4ABI()) {
    200     Reserved.set(PPC::R2);  // System-reserved register
    201     Reserved.set(PPC::R13); // Small Data Area pointer register
    202   }
    203 
    204   // On PPC64, r13 is the thread pointer. Never allocate this register.
    205   if (TM.isPPC64()) {
    206     Reserved.set(PPC::R13);
    207 
    208     Reserved.set(PPC::X1);
    209     Reserved.set(PPC::X13);
    210 
    211     if (TFI->needsFP(MF))
    212       Reserved.set(PPC::X31);
    213 
    214     if (hasBasePointer(MF))
    215       Reserved.set(PPC::X30);
    216 
    217     // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
    218     if (Subtarget.isSVR4ABI()) {
    219       // We only reserve r2 if we need to use the TOC pointer. If we have no
    220       // explicit uses of the TOC pointer (meaning we're a leaf function with
    221       // no constant-pool loads, etc.) and we have no potential uses inside an
    222       // inline asm block, then we can treat r2 has an ordinary callee-saved
    223       // register.
    224       const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
    225       if (FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
    226         Reserved.set(PPC::X2);
    227       else
    228         Reserved.reset(PPC::R2);
    229     }
    230   }
    231 
    232   if (TFI->needsFP(MF))
    233     Reserved.set(PPC::R31);
    234 
    235   if (hasBasePointer(MF)) {
    236     if (Subtarget.isSVR4ABI() && !TM.isPPC64() &&
    237         TM.getRelocationModel() == Reloc::PIC_)
    238       Reserved.set(PPC::R29);
    239     else
    240       Reserved.set(PPC::R30);
    241   }
    242 
    243   if (Subtarget.isSVR4ABI() && !TM.isPPC64() &&
    244       TM.getRelocationModel() == Reloc::PIC_)
    245     Reserved.set(PPC::R30);
    246 
    247   // Reserve Altivec registers when Altivec is unavailable.
    248   if (!Subtarget.hasAltivec())
    249     for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
    250          IE = PPC::VRRCRegClass.end(); I != IE; ++I)
    251       Reserved.set(*I);
    252 
    253   return Reserved;
    254 }
    255 
    256 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
    257                                               MachineFunction &MF) const {
    258   const PPCFrameLowering *TFI = getFrameLowering(MF);
    259   const unsigned DefaultSafety = 1;
    260 
    261   switch (RC->getID()) {
    262   default:
    263     return 0;
    264   case PPC::G8RC_NOX0RegClassID:
    265   case PPC::GPRC_NOR0RegClassID:
    266   case PPC::G8RCRegClassID:
    267   case PPC::GPRCRegClassID: {
    268     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
    269     return 32 - FP - DefaultSafety;
    270   }
    271   case PPC::F8RCRegClassID:
    272   case PPC::F4RCRegClassID:
    273   case PPC::QFRCRegClassID:
    274   case PPC::QSRCRegClassID:
    275   case PPC::QBRCRegClassID:
    276   case PPC::VRRCRegClassID:
    277   case PPC::VFRCRegClassID:
    278   case PPC::VSLRCRegClassID:
    279   case PPC::VSHRCRegClassID:
    280     return 32 - DefaultSafety;
    281   case PPC::VSRCRegClassID:
    282   case PPC::VSFRCRegClassID:
    283   case PPC::VSSRCRegClassID:
    284     return 64 - DefaultSafety;
    285   case PPC::CRRCRegClassID:
    286     return 8 - DefaultSafety;
    287   }
    288 }
    289 
    290 const TargetRegisterClass *
    291 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
    292                                            const MachineFunction &MF) const {
    293   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    294   if (Subtarget.hasVSX()) {
    295     // With VSX, we can inflate various sub-register classes to the full VSX
    296     // register set.
    297 
    298     if (RC == &PPC::F8RCRegClass)
    299       return &PPC::VSFRCRegClass;
    300     else if (RC == &PPC::VRRCRegClass)
    301       return &PPC::VSRCRegClass;
    302     else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector())
    303       return &PPC::VSSRCRegClass;
    304   }
    305 
    306   return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
    307 }
    308 
    309 //===----------------------------------------------------------------------===//
    310 // Stack Frame Processing methods
    311 //===----------------------------------------------------------------------===//
    312 
    313 /// lowerDynamicAlloc - Generate the code for allocating an object in the
    314 /// current frame.  The sequence of code will be in the general form
    315 ///
    316 ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
    317 ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
    318 ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
    319 ///
    320 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
    321   // Get the instruction.
    322   MachineInstr &MI = *II;
    323   // Get the instruction's basic block.
    324   MachineBasicBlock &MBB = *MI.getParent();
    325   // Get the basic block's function.
    326   MachineFunction &MF = *MBB.getParent();
    327   // Get the frame info.
    328   MachineFrameInfo *MFI = MF.getFrameInfo();
    329   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    330   // Get the instruction info.
    331   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    332   // Determine whether 64-bit pointers are used.
    333   bool LP64 = TM.isPPC64();
    334   DebugLoc dl = MI.getDebugLoc();
    335 
    336   // Get the maximum call stack size.
    337   unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
    338   // Get the total frame size.
    339   unsigned FrameSize = MFI->getStackSize();
    340 
    341   // Get stack alignments.
    342   const PPCFrameLowering *TFI = getFrameLowering(MF);
    343   unsigned TargetAlign = TFI->getStackAlignment();
    344   unsigned MaxAlign = MFI->getMaxAlignment();
    345   assert((maxCallFrameSize & (MaxAlign-1)) == 0 &&
    346          "Maximum call-frame size not sufficiently aligned");
    347 
    348   // Determine the previous frame's address.  If FrameSize can't be
    349   // represented as 16 bits or we need special alignment, then we load the
    350   // previous frame's address from 0(SP).  Why not do an addis of the hi?
    351   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
    352   // Constructing the constant and adding would take 3 instructions.
    353   // Fortunately, a frame greater than 32K is rare.
    354   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
    355   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    356   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    357 
    358   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
    359     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
    360       .addReg(PPC::R31)
    361       .addImm(FrameSize);
    362   } else if (LP64) {
    363     BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
    364       .addImm(0)
    365       .addReg(PPC::X1);
    366   } else {
    367     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
    368       .addImm(0)
    369       .addReg(PPC::R1);
    370   }
    371 
    372   bool KillNegSizeReg = MI.getOperand(1).isKill();
    373   unsigned NegSizeReg = MI.getOperand(1).getReg();
    374 
    375   // Grow the stack and update the stack pointer link, then determine the
    376   // address of new allocated space.
    377   if (LP64) {
    378     if (MaxAlign > TargetAlign) {
    379       unsigned UnalNegSizeReg = NegSizeReg;
    380       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
    381 
    382       // Unfortunately, there is no andi, only andi., and we can't insert that
    383       // here because we might clobber cr0 while it is live.
    384       BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
    385         .addImm(~(MaxAlign-1));
    386 
    387       unsigned NegSizeReg1 = NegSizeReg;
    388       NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
    389       BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
    390         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
    391         .addReg(NegSizeReg1, RegState::Kill);
    392       KillNegSizeReg = true;
    393     }
    394 
    395     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
    396       .addReg(Reg, RegState::Kill)
    397       .addReg(PPC::X1)
    398       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
    399     BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
    400       .addReg(PPC::X1)
    401       .addImm(maxCallFrameSize);
    402   } else {
    403     if (MaxAlign > TargetAlign) {
    404       unsigned UnalNegSizeReg = NegSizeReg;
    405       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
    406 
    407       // Unfortunately, there is no andi, only andi., and we can't insert that
    408       // here because we might clobber cr0 while it is live.
    409       BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
    410         .addImm(~(MaxAlign-1));
    411 
    412       unsigned NegSizeReg1 = NegSizeReg;
    413       NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
    414       BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
    415         .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
    416         .addReg(NegSizeReg1, RegState::Kill);
    417       KillNegSizeReg = true;
    418     }
    419 
    420     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
    421       .addReg(Reg, RegState::Kill)
    422       .addReg(PPC::R1)
    423       .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
    424     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
    425       .addReg(PPC::R1)
    426       .addImm(maxCallFrameSize);
    427   }
    428 
    429   // Discard the DYNALLOC instruction.
    430   MBB.erase(II);
    431 }
    432 
    433 void PPCRegisterInfo::lowerDynamicAreaOffset(
    434     MachineBasicBlock::iterator II) const {
    435   // Get the instruction.
    436   MachineInstr &MI = *II;
    437   // Get the instruction's basic block.
    438   MachineBasicBlock &MBB = *MI.getParent();
    439   // Get the basic block's function.
    440   MachineFunction &MF = *MBB.getParent();
    441   // Get the frame info.
    442   MachineFrameInfo *MFI = MF.getFrameInfo();
    443   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    444   // Get the instruction info.
    445   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    446 
    447   unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
    448   DebugLoc dl = MI.getDebugLoc();
    449   BuildMI(MBB, II, dl, TII.get(PPC::LI), MI.getOperand(0).getReg())
    450       .addImm(maxCallFrameSize);
    451   MBB.erase(II);
    452 }
    453 
    454 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
    455 /// reserving a whole register (R0), we scrounge for one here. This generates
    456 /// code like this:
    457 ///
    458 ///   mfcr rA                  ; Move the conditional register into GPR rA.
    459 ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
    460 ///   stw rA, FI               ; Store rA to the frame.
    461 ///
    462 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
    463                                       unsigned FrameIndex) const {
    464   // Get the instruction.
    465   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
    466   // Get the instruction's basic block.
    467   MachineBasicBlock &MBB = *MI.getParent();
    468   MachineFunction &MF = *MBB.getParent();
    469   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    470   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    471   DebugLoc dl = MI.getDebugLoc();
    472 
    473   bool LP64 = TM.isPPC64();
    474   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
    475   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    476 
    477   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    478   unsigned SrcReg = MI.getOperand(0).getReg();
    479 
    480   // We need to store the CR in the low 4-bits of the saved value. First, issue
    481   // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
    482   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
    483       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
    484 
    485   // If the saved register wasn't CR0, shift the bits left so that they are in
    486   // CR0's slot.
    487   if (SrcReg != PPC::CR0) {
    488     unsigned Reg1 = Reg;
    489     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    490 
    491     // rlwinm rA, rA, ShiftBits, 0, 31.
    492     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
    493       .addReg(Reg1, RegState::Kill)
    494       .addImm(getEncodingValue(SrcReg) * 4)
    495       .addImm(0)
    496       .addImm(31);
    497   }
    498 
    499   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
    500                     .addReg(Reg, RegState::Kill),
    501                     FrameIndex);
    502 
    503   // Discard the pseudo instruction.
    504   MBB.erase(II);
    505 }
    506 
    507 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
    508                                       unsigned FrameIndex) const {
    509   // Get the instruction.
    510   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
    511   // Get the instruction's basic block.
    512   MachineBasicBlock &MBB = *MI.getParent();
    513   MachineFunction &MF = *MBB.getParent();
    514   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    515   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    516   DebugLoc dl = MI.getDebugLoc();
    517 
    518   bool LP64 = TM.isPPC64();
    519   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
    520   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    521 
    522   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    523   unsigned DestReg = MI.getOperand(0).getReg();
    524   assert(MI.definesRegister(DestReg) &&
    525     "RESTORE_CR does not define its destination");
    526 
    527   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
    528                               Reg), FrameIndex);
    529 
    530   // If the reloaded register isn't CR0, shift the bits right so that they are
    531   // in the right CR's slot.
    532   if (DestReg != PPC::CR0) {
    533     unsigned Reg1 = Reg;
    534     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    535 
    536     unsigned ShiftBits = getEncodingValue(DestReg)*4;
    537     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
    538     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
    539              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
    540              .addImm(31);
    541   }
    542 
    543   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
    544              .addReg(Reg, RegState::Kill);
    545 
    546   // Discard the pseudo instruction.
    547   MBB.erase(II);
    548 }
    549 
    550 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
    551                                          unsigned FrameIndex) const {
    552   // Get the instruction.
    553   MachineInstr &MI = *II;       // ; SPILL_CRBIT <SrcReg>, <offset>
    554   // Get the instruction's basic block.
    555   MachineBasicBlock &MBB = *MI.getParent();
    556   MachineFunction &MF = *MBB.getParent();
    557   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    558   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    559   DebugLoc dl = MI.getDebugLoc();
    560 
    561   bool LP64 = TM.isPPC64();
    562   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
    563   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    564 
    565   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    566   unsigned SrcReg = MI.getOperand(0).getReg();
    567 
    568   BuildMI(MBB, II, dl, TII.get(TargetOpcode::KILL),
    569           getCRFromCRBit(SrcReg))
    570           .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
    571 
    572   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
    573       .addReg(getCRFromCRBit(SrcReg));
    574 
    575   // If the saved register wasn't CR0LT, shift the bits left so that the bit to
    576   // store is the first one. Mask all but that bit.
    577   unsigned Reg1 = Reg;
    578   Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    579 
    580   // rlwinm rA, rA, ShiftBits, 0, 0.
    581   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
    582     .addReg(Reg1, RegState::Kill)
    583     .addImm(getEncodingValue(SrcReg))
    584     .addImm(0).addImm(0);
    585 
    586   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
    587                     .addReg(Reg, RegState::Kill),
    588                     FrameIndex);
    589 
    590   // Discard the pseudo instruction.
    591   MBB.erase(II);
    592 }
    593 
    594 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
    595                                       unsigned FrameIndex) const {
    596   // Get the instruction.
    597   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CRBIT <offset>
    598   // Get the instruction's basic block.
    599   MachineBasicBlock &MBB = *MI.getParent();
    600   MachineFunction &MF = *MBB.getParent();
    601   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    602   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    603   DebugLoc dl = MI.getDebugLoc();
    604 
    605   bool LP64 = TM.isPPC64();
    606   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
    607   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    608 
    609   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    610   unsigned DestReg = MI.getOperand(0).getReg();
    611   assert(MI.definesRegister(DestReg) &&
    612     "RESTORE_CRBIT does not define its destination");
    613 
    614   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
    615                               Reg), FrameIndex);
    616 
    617   BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
    618 
    619   unsigned RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
    620   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
    621           .addReg(getCRFromCRBit(DestReg));
    622 
    623   unsigned ShiftBits = getEncodingValue(DestReg);
    624   // rlwimi r11, r10, 32-ShiftBits, ..., ...
    625   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
    626       .addReg(RegO, RegState::Kill)
    627       .addReg(Reg, RegState::Kill)
    628       .addImm(ShiftBits ? 32 - ShiftBits : 0)
    629       .addImm(ShiftBits)
    630       .addImm(ShiftBits);
    631 
    632   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
    633           getCRFromCRBit(DestReg))
    634       .addReg(RegO, RegState::Kill)
    635       // Make sure we have a use dependency all the way through this
    636       // sequence of instructions. We can't have the other bits in the CR
    637       // modified in between the mfocrf and the mtocrf.
    638       .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
    639 
    640   // Discard the pseudo instruction.
    641   MBB.erase(II);
    642 }
    643 
    644 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
    645                                           unsigned FrameIndex) const {
    646   // Get the instruction.
    647   MachineInstr &MI = *II;       // ; SPILL_VRSAVE <SrcReg>, <offset>
    648   // Get the instruction's basic block.
    649   MachineBasicBlock &MBB = *MI.getParent();
    650   MachineFunction &MF = *MBB.getParent();
    651   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    652   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    653   DebugLoc dl = MI.getDebugLoc();
    654 
    655   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    656   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
    657   unsigned SrcReg = MI.getOperand(0).getReg();
    658 
    659   BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
    660       .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
    661 
    662   addFrameReference(
    663       BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill),
    664       FrameIndex);
    665 
    666   // Discard the pseudo instruction.
    667   MBB.erase(II);
    668 }
    669 
    670 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
    671                                          unsigned FrameIndex) const {
    672   // Get the instruction.
    673   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_VRSAVE <offset>
    674   // Get the instruction's basic block.
    675   MachineBasicBlock &MBB = *MI.getParent();
    676   MachineFunction &MF = *MBB.getParent();
    677   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    678   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    679   DebugLoc dl = MI.getDebugLoc();
    680 
    681   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    682   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
    683   unsigned DestReg = MI.getOperand(0).getReg();
    684   assert(MI.definesRegister(DestReg) &&
    685     "RESTORE_VRSAVE does not define its destination");
    686 
    687   addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
    688                               Reg), FrameIndex);
    689 
    690   BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
    691              .addReg(Reg, RegState::Kill);
    692 
    693   // Discard the pseudo instruction.
    694   MBB.erase(II);
    695 }
    696 
    697 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
    698                                            unsigned Reg, int &FrameIdx) const {
    699   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    700   // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
    701   // ABI, return true to prevent allocating an additional frame slot.
    702   // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
    703   // is arbitrary and will be subsequently ignored.  For 32-bit, we have
    704   // previously created the stack slot if needed, so return its FrameIdx.
    705   if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
    706     if (TM.isPPC64())
    707       FrameIdx = 0;
    708     else {
    709       const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
    710       FrameIdx = FI->getCRSpillFrameIndex();
    711     }
    712     return true;
    713   }
    714   return false;
    715 }
    716 
    717 // Figure out if the offset in the instruction must be a multiple of 4.
    718 // This is true for instructions like "STD".
    719 static bool usesIXAddr(const MachineInstr &MI) {
    720   unsigned OpC = MI.getOpcode();
    721 
    722   switch (OpC) {
    723   default:
    724     return false;
    725   case PPC::LWA:
    726   case PPC::LWA_32:
    727   case PPC::LD:
    728   case PPC::STD:
    729     return true;
    730   }
    731 }
    732 
    733 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
    734 static unsigned getOffsetONFromFION(const MachineInstr &MI,
    735                                     unsigned FIOperandNum) {
    736   // Take into account whether it's an add or mem instruction
    737   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
    738   if (MI.isInlineAsm())
    739     OffsetOperandNo = FIOperandNum - 1;
    740   else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
    741            MI.getOpcode() == TargetOpcode::PATCHPOINT)
    742     OffsetOperandNo = FIOperandNum + 1;
    743 
    744   return OffsetOperandNo;
    745 }
    746 
    747 void
    748 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
    749                                      int SPAdj, unsigned FIOperandNum,
    750                                      RegScavenger *RS) const {
    751   assert(SPAdj == 0 && "Unexpected");
    752 
    753   // Get the instruction.
    754   MachineInstr &MI = *II;
    755   // Get the instruction's basic block.
    756   MachineBasicBlock &MBB = *MI.getParent();
    757   // Get the basic block's function.
    758   MachineFunction &MF = *MBB.getParent();
    759   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    760   // Get the instruction info.
    761   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    762   // Get the frame info.
    763   MachineFrameInfo *MFI = MF.getFrameInfo();
    764   DebugLoc dl = MI.getDebugLoc();
    765 
    766   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
    767 
    768   // Get the frame index.
    769   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
    770 
    771   // Get the frame pointer save index.  Users of this index are primarily
    772   // DYNALLOC instructions.
    773   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
    774   int FPSI = FI->getFramePointerSaveIndex();
    775   // Get the instruction opcode.
    776   unsigned OpC = MI.getOpcode();
    777 
    778   if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
    779     lowerDynamicAreaOffset(II);
    780     return;
    781   }
    782 
    783   // Special case for dynamic alloca.
    784   if (FPSI && FrameIndex == FPSI &&
    785       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
    786     lowerDynamicAlloc(II);
    787     return;
    788   }
    789 
    790   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
    791   if (OpC == PPC::SPILL_CR) {
    792     lowerCRSpilling(II, FrameIndex);
    793     return;
    794   } else if (OpC == PPC::RESTORE_CR) {
    795     lowerCRRestore(II, FrameIndex);
    796     return;
    797   } else if (OpC == PPC::SPILL_CRBIT) {
    798     lowerCRBitSpilling(II, FrameIndex);
    799     return;
    800   } else if (OpC == PPC::RESTORE_CRBIT) {
    801     lowerCRBitRestore(II, FrameIndex);
    802     return;
    803   } else if (OpC == PPC::SPILL_VRSAVE) {
    804     lowerVRSAVESpilling(II, FrameIndex);
    805     return;
    806   } else if (OpC == PPC::RESTORE_VRSAVE) {
    807     lowerVRSAVERestore(II, FrameIndex);
    808     return;
    809   }
    810 
    811   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
    812   MI.getOperand(FIOperandNum).ChangeToRegister(
    813     FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
    814 
    815   // Figure out if the offset in the instruction is shifted right two bits.
    816   bool isIXAddr = usesIXAddr(MI);
    817 
    818   // If the instruction is not present in ImmToIdxMap, then it has no immediate
    819   // form (and must be r+r).
    820   bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
    821                    OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
    822 
    823   // Now add the frame object offset to the offset from r1.
    824   int Offset = MFI->getObjectOffset(FrameIndex);
    825   Offset += MI.getOperand(OffsetOperandNo).getImm();
    826 
    827   // If we're not using a Frame Pointer that has been set to the value of the
    828   // SP before having the stack size subtracted from it, then add the stack size
    829   // to Offset to get the correct offset.
    830   // Naked functions have stack size 0, although getStackSize may not reflect
    831   // that because we didn't call all the pieces that compute it for naked
    832   // functions.
    833   if (!MF.getFunction()->hasFnAttribute(Attribute::Naked)) {
    834     if (!(hasBasePointer(MF) && FrameIndex < 0))
    835       Offset += MFI->getStackSize();
    836   }
    837 
    838   // If we can, encode the offset directly into the instruction.  If this is a
    839   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
    840   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
    841   // clear can be encoded.  This is extremely uncommon, because normally you
    842   // only "std" to a stack slot that is at least 4-byte aligned, but it can
    843   // happen in invalid code.
    844   assert(OpC != PPC::DBG_VALUE &&
    845          "This should be handled in a target-independent way");
    846   if (!noImmForm && ((isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) ||
    847                      OpC == TargetOpcode::STACKMAP ||
    848                      OpC == TargetOpcode::PATCHPOINT)) {
    849     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
    850     return;
    851   }
    852 
    853   // The offset doesn't fit into a single register, scavenge one to build the
    854   // offset in.
    855 
    856   bool is64Bit = TM.isPPC64();
    857   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
    858   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
    859   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
    860   unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
    861            SReg = MF.getRegInfo().createVirtualRegister(RC);
    862 
    863   // Insert a set of rA with the full offset value before the ld, st, or add
    864   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
    865     .addImm(Offset >> 16);
    866   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
    867     .addReg(SRegHi, RegState::Kill)
    868     .addImm(Offset);
    869 
    870   // Convert into indexed form of the instruction:
    871   //
    872   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
    873   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
    874   unsigned OperandBase;
    875 
    876   if (noImmForm)
    877     OperandBase = 1;
    878   else if (OpC != TargetOpcode::INLINEASM) {
    879     assert(ImmToIdxMap.count(OpC) &&
    880            "No indexed form of load or store available!");
    881     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
    882     MI.setDesc(TII.get(NewOpcode));
    883     OperandBase = 1;
    884   } else {
    885     OperandBase = OffsetOperandNo;
    886   }
    887 
    888   unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
    889   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
    890   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
    891 }
    892 
    893 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
    894   const PPCFrameLowering *TFI = getFrameLowering(MF);
    895 
    896   if (!TM.isPPC64())
    897     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
    898   else
    899     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
    900 }
    901 
    902 unsigned PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
    903   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    904   if (!hasBasePointer(MF))
    905     return getFrameRegister(MF);
    906 
    907   if (TM.isPPC64())
    908     return PPC::X30;
    909 
    910   if (Subtarget.isSVR4ABI() &&
    911       TM.getRelocationModel() == Reloc::PIC_)
    912     return PPC::R29;
    913 
    914   return PPC::R30;
    915 }
    916 
    917 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
    918   if (!EnableBasePointer)
    919     return false;
    920   if (AlwaysBasePointer)
    921     return true;
    922 
    923   // If we need to realign the stack, then the stack pointer can no longer
    924   // serve as an offset into the caller's stack space. As a result, we need a
    925   // base pointer.
    926   return needsStackRealignment(MF);
    927 }
    928 
    929 /// Returns true if the instruction's frame index
    930 /// reference would be better served by a base register other than FP
    931 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
    932 /// references it should create new base registers for.
    933 bool PPCRegisterInfo::
    934 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
    935   assert(Offset < 0 && "Local offset must be negative");
    936 
    937   // It's the load/store FI references that cause issues, as it can be difficult
    938   // to materialize the offset if it won't fit in the literal field. Estimate
    939   // based on the size of the local frame and some conservative assumptions
    940   // about the rest of the stack frame (note, this is pre-regalloc, so
    941   // we don't know everything for certain yet) whether this offset is likely
    942   // to be out of range of the immediate. Return true if so.
    943 
    944   // We only generate virtual base registers for loads and stores that have
    945   // an r+i form. Return false for everything else.
    946   unsigned OpC = MI->getOpcode();
    947   if (!ImmToIdxMap.count(OpC))
    948     return false;
    949 
    950   // Don't generate a new virtual base register just to add zero to it.
    951   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
    952       MI->getOperand(2).getImm() == 0)
    953     return false;
    954 
    955   MachineBasicBlock &MBB = *MI->getParent();
    956   MachineFunction &MF = *MBB.getParent();
    957   const PPCFrameLowering *TFI = getFrameLowering(MF);
    958   unsigned StackEst = TFI->determineFrameLayout(MF, false, true);
    959 
    960   // If we likely don't need a stack frame, then we probably don't need a
    961   // virtual base register either.
    962   if (!StackEst)
    963     return false;
    964 
    965   // Estimate an offset from the stack pointer.
    966   // The incoming offset is relating to the SP at the start of the function,
    967   // but when we access the local it'll be relative to the SP after local
    968   // allocation, so adjust our SP-relative offset by that allocation size.
    969   Offset += StackEst;
    970 
    971   // The frame pointer will point to the end of the stack, so estimate the
    972   // offset as the difference between the object offset and the FP location.
    973   return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
    974 }
    975 
    976 /// Insert defining instruction(s) for BaseReg to
    977 /// be a pointer to FrameIdx at the beginning of the basic block.
    978 void PPCRegisterInfo::
    979 materializeFrameBaseRegister(MachineBasicBlock *MBB,
    980                              unsigned BaseReg, int FrameIdx,
    981                              int64_t Offset) const {
    982   unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
    983 
    984   MachineBasicBlock::iterator Ins = MBB->begin();
    985   DebugLoc DL;                  // Defaults to "unknown"
    986   if (Ins != MBB->end())
    987     DL = Ins->getDebugLoc();
    988 
    989   const MachineFunction &MF = *MBB->getParent();
    990   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
    991   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
    992   const MCInstrDesc &MCID = TII.get(ADDriOpc);
    993   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
    994   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
    995 
    996   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
    997     .addFrameIndex(FrameIdx).addImm(Offset);
    998 }
    999 
   1000 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
   1001                                         int64_t Offset) const {
   1002   unsigned FIOperandNum = 0;
   1003   while (!MI.getOperand(FIOperandNum).isFI()) {
   1004     ++FIOperandNum;
   1005     assert(FIOperandNum < MI.getNumOperands() &&
   1006            "Instr doesn't have FrameIndex operand!");
   1007   }
   1008 
   1009   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
   1010   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
   1011   Offset += MI.getOperand(OffsetOperandNo).getImm();
   1012   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
   1013 
   1014   MachineBasicBlock &MBB = *MI.getParent();
   1015   MachineFunction &MF = *MBB.getParent();
   1016   const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
   1017   const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
   1018   const MCInstrDesc &MCID = MI.getDesc();
   1019   MachineRegisterInfo &MRI = MF.getRegInfo();
   1020   MRI.constrainRegClass(BaseReg,
   1021                         TII.getRegClass(MCID, FIOperandNum, this, MF));
   1022 }
   1023 
   1024 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
   1025                                          unsigned BaseReg,
   1026                                          int64_t Offset) const {
   1027   unsigned FIOperandNum = 0;
   1028   while (!MI->getOperand(FIOperandNum).isFI()) {
   1029     ++FIOperandNum;
   1030     assert(FIOperandNum < MI->getNumOperands() &&
   1031            "Instr doesn't have FrameIndex operand!");
   1032   }
   1033 
   1034   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
   1035   Offset += MI->getOperand(OffsetOperandNo).getImm();
   1036 
   1037   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
   1038          MI->getOpcode() == TargetOpcode::STACKMAP ||
   1039          MI->getOpcode() == TargetOpcode::PATCHPOINT ||
   1040          (isInt<16>(Offset) && (!usesIXAddr(*MI) || (Offset & 3) == 0));
   1041 }
   1042