LLVM API Documentation

PPCRegisterInfo.cpp
Go to the documentation of this file.
00001 //===-- PPCRegisterInfo.cpp - PowerPC Register Information ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the PowerPC implementation of the TargetRegisterInfo
00011 // class.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "reginfo"
00016 #include "PPCRegisterInfo.h"
00017 #include "PPC.h"
00018 #include "PPCFrameLowering.h"
00019 #include "PPCInstrBuilder.h"
00020 #include "PPCMachineFunctionInfo.h"
00021 #include "PPCSubtarget.h"
00022 #include "llvm/ADT/BitVector.h"
00023 #include "llvm/ADT/STLExtras.h"
00024 #include "llvm/CodeGen/MachineFrameInfo.h"
00025 #include "llvm/CodeGen/MachineFunction.h"
00026 #include "llvm/CodeGen/MachineInstrBuilder.h"
00027 #include "llvm/CodeGen/MachineModuleInfo.h"
00028 #include "llvm/CodeGen/MachineRegisterInfo.h"
00029 #include "llvm/CodeGen/RegisterScavenging.h"
00030 #include "llvm/CodeGen/ValueTypes.h"
00031 #include "llvm/IR/CallingConv.h"
00032 #include "llvm/IR/Constants.h"
00033 #include "llvm/IR/Function.h"
00034 #include "llvm/IR/Type.h"
00035 #include "llvm/Support/CommandLine.h"
00036 #include "llvm/Support/Debug.h"
00037 #include "llvm/Support/ErrorHandling.h"
00038 #include "llvm/Support/MathExtras.h"
00039 #include "llvm/Support/raw_ostream.h"
00040 #include "llvm/Target/TargetFrameLowering.h"
00041 #include "llvm/Target/TargetInstrInfo.h"
00042 #include "llvm/Target/TargetMachine.h"
00043 #include "llvm/Target/TargetOptions.h"
00044 #include <cstdlib>
00045 
00046 #define GET_REGINFO_TARGET_DESC
00047 #include "PPCGenRegisterInfo.inc"
00048 
00049 using namespace llvm;
00050 
00051 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
00052                                  const TargetInstrInfo &tii)
00053   : PPCGenRegisterInfo(ST.isPPC64() ? PPC::LR8 : PPC::LR,
00054                        ST.isPPC64() ? 0 : 1,
00055                        ST.isPPC64() ? 0 : 1),
00056     Subtarget(ST), TII(tii) {
00057   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
00058   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
00059   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
00060   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
00061   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
00062   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
00063   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
00064   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
00065 
00066   // 64-bit
00067   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
00068   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
00069   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
00070   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
00071   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
00072 }
00073 
00074 /// getPointerRegClass - Return the register class to use to hold pointers.
00075 /// This is used for addressing modes.
00076 const TargetRegisterClass *
00077 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
00078                                                                        const {
00079   // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
00080   // when it checks for ZERO folding.
00081   if (Kind == 1) {
00082     if (Subtarget.isPPC64())
00083       return &PPC::G8RC_NOX0RegClass;
00084     return &PPC::GPRC_NOR0RegClass;
00085   }
00086 
00087   if (Subtarget.isPPC64())
00088     return &PPC::G8RCRegClass;
00089   return &PPC::GPRCRegClass;
00090 }
00091 
00092 const uint16_t*
00093 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
00094   if (Subtarget.isDarwinABI())
00095     return Subtarget.isPPC64() ? CSR_Darwin64_SaveList :
00096                                  CSR_Darwin32_SaveList;
00097 
00098   return Subtarget.isPPC64() ? CSR_SVR464_SaveList : CSR_SVR432_SaveList;
00099 }
00100 
00101 const uint32_t*
00102 PPCRegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
00103   if (Subtarget.isDarwinABI())
00104     return Subtarget.isPPC64() ? CSR_Darwin64_RegMask :
00105                                  CSR_Darwin32_RegMask;
00106 
00107   return Subtarget.isPPC64() ? CSR_SVR464_RegMask : CSR_SVR432_RegMask;
00108 }
00109 
00110 const uint32_t*
00111 PPCRegisterInfo::getNoPreservedMask() const {
00112   // The naming here is inverted: The CSR_NoRegs_Altivec has the
00113   // Altivec registers masked so that they're not saved and restored around
00114   // instructions with this preserved mask.
00115 
00116   if (!Subtarget.hasAltivec())
00117     return CSR_NoRegs_Altivec_RegMask;
00118 
00119   if (Subtarget.isDarwin())
00120     return CSR_NoRegs_Darwin_RegMask;
00121   return CSR_NoRegs_RegMask;
00122 }
00123 
00124 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
00125   BitVector Reserved(getNumRegs());
00126   const PPCFrameLowering *PPCFI =
00127     static_cast<const PPCFrameLowering*>(MF.getTarget().getFrameLowering());
00128 
00129   // The ZERO register is not really a register, but the representation of r0
00130   // when used in instructions that treat r0 as the constant 0.
00131   Reserved.set(PPC::ZERO);
00132   Reserved.set(PPC::ZERO8);
00133 
00134   // The FP register is also not really a register, but is the representation
00135   // of the frame pointer register used by ISD::FRAMEADDR.
00136   Reserved.set(PPC::FP);
00137   Reserved.set(PPC::FP8);
00138 
00139   // The counter registers must be reserved so that counter-based loops can
00140   // be correctly formed (and the mtctr instructions are not DCE'd).
00141   Reserved.set(PPC::CTR);
00142   Reserved.set(PPC::CTR8);
00143 
00144   Reserved.set(PPC::R1);
00145   Reserved.set(PPC::LR);
00146   Reserved.set(PPC::LR8);
00147   Reserved.set(PPC::RM);
00148 
00149   // The SVR4 ABI reserves r2 and r13
00150   if (Subtarget.isSVR4ABI()) {
00151     Reserved.set(PPC::R2);  // System-reserved register
00152     Reserved.set(PPC::R13); // Small Data Area pointer register
00153   }
00154   
00155   // On PPC64, r13 is the thread pointer. Never allocate this register.
00156   if (Subtarget.isPPC64()) {
00157     Reserved.set(PPC::R13);
00158 
00159     Reserved.set(PPC::X1);
00160     Reserved.set(PPC::X13);
00161 
00162     if (PPCFI->needsFP(MF))
00163       Reserved.set(PPC::X31);
00164 
00165     // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
00166     if (Subtarget.isSVR4ABI()) {
00167       Reserved.set(PPC::X2);
00168     }
00169   }
00170 
00171   if (PPCFI->needsFP(MF))
00172     Reserved.set(PPC::R31);
00173 
00174   return Reserved;
00175 }
00176 
00177 unsigned
00178 PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
00179                                          MachineFunction &MF) const {
00180   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
00181   const unsigned DefaultSafety = 1;
00182 
00183   switch (RC->getID()) {
00184   default:
00185     return 0;
00186   case PPC::G8RC_NOX0RegClassID:
00187   case PPC::GPRC_NOR0RegClassID: 
00188   case PPC::G8RCRegClassID:
00189   case PPC::GPRCRegClassID: {
00190     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
00191     return 32 - FP - DefaultSafety;
00192   }
00193   case PPC::F8RCRegClassID:
00194   case PPC::F4RCRegClassID:
00195   case PPC::VRRCRegClassID:
00196     return 32 - DefaultSafety;
00197   case PPC::CRRCRegClassID:
00198     return 8 - DefaultSafety;
00199   }
00200 }
00201 
00202 //===----------------------------------------------------------------------===//
00203 // Stack Frame Processing methods
00204 //===----------------------------------------------------------------------===//
00205 
00206 /// lowerDynamicAlloc - Generate the code for allocating an object in the
00207 /// current frame.  The sequence of code with be in the general form
00208 ///
00209 ///   addi   R0, SP, \#frameSize ; get the address of the previous frame
00210 ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
00211 ///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
00212 ///
00213 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
00214   // Get the instruction.
00215   MachineInstr &MI = *II;
00216   // Get the instruction's basic block.
00217   MachineBasicBlock &MBB = *MI.getParent();
00218   // Get the basic block's function.
00219   MachineFunction &MF = *MBB.getParent();
00220   // Get the frame info.
00221   MachineFrameInfo *MFI = MF.getFrameInfo();
00222   // Determine whether 64-bit pointers are used.
00223   bool LP64 = Subtarget.isPPC64();
00224   DebugLoc dl = MI.getDebugLoc();
00225 
00226   // Get the maximum call stack size.
00227   unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
00228   // Get the total frame size.
00229   unsigned FrameSize = MFI->getStackSize();
00230   
00231   // Get stack alignments.
00232   unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
00233   unsigned MaxAlign = MFI->getMaxAlignment();
00234   if (MaxAlign > TargetAlign)
00235     report_fatal_error("Dynamic alloca with large aligns not supported");
00236 
00237   // Determine the previous frame's address.  If FrameSize can't be
00238   // represented as 16 bits or we need special alignment, then we load the
00239   // previous frame's address from 0(SP).  Why not do an addis of the hi? 
00240   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 
00241   // Constructing the constant and adding would take 3 instructions. 
00242   // Fortunately, a frame greater than 32K is rare.
00243   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
00244   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00245   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
00246   
00247   if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
00248     BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
00249       .addReg(PPC::R31)
00250       .addImm(FrameSize);
00251   } else if (LP64) {
00252     BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
00253       .addImm(0)
00254       .addReg(PPC::X1);
00255   } else {
00256     BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
00257       .addImm(0)
00258       .addReg(PPC::R1);
00259   }
00260   
00261   // Grow the stack and update the stack pointer link, then determine the
00262   // address of new allocated space.
00263   if (LP64) {
00264     BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
00265       .addReg(Reg, RegState::Kill)
00266       .addReg(PPC::X1)
00267       .addReg(MI.getOperand(1).getReg());
00268     if (!MI.getOperand(1).isKill())
00269       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
00270         .addReg(PPC::X1)
00271         .addImm(maxCallFrameSize);
00272     else
00273       // Implicitly kill the register.
00274       BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
00275         .addReg(PPC::X1)
00276         .addImm(maxCallFrameSize)
00277         .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
00278   } else {
00279     BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
00280       .addReg(Reg, RegState::Kill)
00281       .addReg(PPC::R1)
00282       .addReg(MI.getOperand(1).getReg());
00283 
00284     if (!MI.getOperand(1).isKill())
00285       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
00286         .addReg(PPC::R1)
00287         .addImm(maxCallFrameSize);
00288     else
00289       // Implicitly kill the register.
00290       BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
00291         .addReg(PPC::R1)
00292         .addImm(maxCallFrameSize)
00293         .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
00294   }
00295   
00296   // Discard the DYNALLOC instruction.
00297   MBB.erase(II);
00298 }
00299 
00300 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
00301 /// reserving a whole register (R0), we scrounge for one here. This generates
00302 /// code like this:
00303 ///
00304 ///   mfcr rA                  ; Move the conditional register into GPR rA.
00305 ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
00306 ///   stw rA, FI               ; Store rA to the frame.
00307 ///
00308 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
00309                                       unsigned FrameIndex) const {
00310   // Get the instruction.
00311   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>
00312   // Get the instruction's basic block.
00313   MachineBasicBlock &MBB = *MI.getParent();
00314   MachineFunction &MF = *MBB.getParent();
00315   DebugLoc dl = MI.getDebugLoc();
00316 
00317   bool LP64 = Subtarget.isPPC64();
00318   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
00319   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00320 
00321   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
00322   unsigned SrcReg = MI.getOperand(0).getReg();
00323 
00324   // We need to store the CR in the low 4-bits of the saved value. First, issue
00325   // an MFCRpsued to save all of the CRBits and, if needed, kill the SrcReg.
00326   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFCR8pseud : PPC::MFCRpseud), Reg)
00327           .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
00328     
00329   // If the saved register wasn't CR0, shift the bits left so that they are in
00330   // CR0's slot.
00331   if (SrcReg != PPC::CR0) {
00332     unsigned Reg1 = Reg;
00333     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
00334 
00335     // rlwinm rA, rA, ShiftBits, 0, 31.
00336     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
00337       .addReg(Reg1, RegState::Kill)
00338       .addImm(getEncodingValue(SrcReg) * 4)
00339       .addImm(0)
00340       .addImm(31);
00341   }
00342 
00343   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
00344                     .addReg(Reg, RegState::Kill),
00345                     FrameIndex);
00346 
00347   // Discard the pseudo instruction.
00348   MBB.erase(II);
00349 }
00350 
00351 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
00352                                       unsigned FrameIndex) const {
00353   // Get the instruction.
00354   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_CR <offset>
00355   // Get the instruction's basic block.
00356   MachineBasicBlock &MBB = *MI.getParent();
00357   MachineFunction &MF = *MBB.getParent();
00358   DebugLoc dl = MI.getDebugLoc();
00359 
00360   bool LP64 = Subtarget.isPPC64();
00361   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
00362   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00363 
00364   unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
00365   unsigned DestReg = MI.getOperand(0).getReg();
00366   assert(MI.definesRegister(DestReg) &&
00367     "RESTORE_CR does not define its destination");
00368 
00369   addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
00370                               Reg), FrameIndex);
00371 
00372   // If the reloaded register isn't CR0, shift the bits right so that they are
00373   // in the right CR's slot.
00374   if (DestReg != PPC::CR0) {
00375     unsigned Reg1 = Reg;
00376     Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
00377 
00378     unsigned ShiftBits = getEncodingValue(DestReg)*4;
00379     // rlwinm r11, r11, 32-ShiftBits, 0, 31.
00380     BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
00381              .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
00382              .addImm(31);
00383   }
00384 
00385   BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTCRF8 : PPC::MTCRF), DestReg)
00386              .addReg(Reg, RegState::Kill);
00387 
00388   // Discard the pseudo instruction.
00389   MBB.erase(II);
00390 }
00391 
00392 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II,
00393                                           unsigned FrameIndex) const {
00394   // Get the instruction.
00395   MachineInstr &MI = *II;       // ; SPILL_VRSAVE <SrcReg>, <offset>
00396   // Get the instruction's basic block.
00397   MachineBasicBlock &MBB = *MI.getParent();
00398   MachineFunction &MF = *MBB.getParent();
00399   DebugLoc dl = MI.getDebugLoc();
00400 
00401   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00402   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
00403   unsigned SrcReg = MI.getOperand(0).getReg();
00404 
00405   BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg)
00406           .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
00407     
00408   addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::STW))
00409                     .addReg(Reg, RegState::Kill),
00410                     FrameIndex);
00411 
00412   // Discard the pseudo instruction.
00413   MBB.erase(II);
00414 }
00415 
00416 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II,
00417                                          unsigned FrameIndex) const {
00418   // Get the instruction.
00419   MachineInstr &MI = *II;       // ; <DestReg> = RESTORE_VRSAVE <offset>
00420   // Get the instruction's basic block.
00421   MachineBasicBlock &MBB = *MI.getParent();
00422   MachineFunction &MF = *MBB.getParent();
00423   DebugLoc dl = MI.getDebugLoc();
00424 
00425   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00426   unsigned Reg = MF.getRegInfo().createVirtualRegister(GPRC);
00427   unsigned DestReg = MI.getOperand(0).getReg();
00428   assert(MI.definesRegister(DestReg) &&
00429     "RESTORE_VRSAVE does not define its destination");
00430 
00431   addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ),
00432                               Reg), FrameIndex);
00433 
00434   BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg)
00435              .addReg(Reg, RegState::Kill);
00436 
00437   // Discard the pseudo instruction.
00438   MBB.erase(II);
00439 }
00440 
00441 bool
00442 PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
00443               unsigned Reg, int &FrameIdx) const {
00444 
00445   // For the nonvolatile condition registers (CR2, CR3, CR4) in an SVR4
00446   // ABI, return true to prevent allocating an additional frame slot.
00447   // For 64-bit, the CR save area is at SP+8; the value of FrameIdx = 0
00448   // is arbitrary and will be subsequently ignored.  For 32-bit, we have
00449   // previously created the stack slot if needed, so return its FrameIdx.
00450   if (Subtarget.isSVR4ABI() && PPC::CR2 <= Reg && Reg <= PPC::CR4) {
00451     if (Subtarget.isPPC64())
00452       FrameIdx = 0;
00453     else {
00454       const PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
00455       FrameIdx = FI->getCRSpillFrameIndex();
00456     }
00457     return true;
00458   }
00459   return false;
00460 }
00461 
00462 // Figure out if the offset in the instruction must be a multiple of 4.
00463 // This is true for instructions like "STD".
00464 static bool usesIXAddr(const MachineInstr &MI) {
00465   unsigned OpC = MI.getOpcode();
00466 
00467   switch (OpC) {
00468   default:
00469     return false;
00470   case PPC::LWA:
00471   case PPC::LD:
00472   case PPC::STD:
00473     return true;
00474   }
00475 }
00476 
00477 // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
00478 static unsigned getOffsetONFromFION(const MachineInstr &MI,
00479                                     unsigned FIOperandNum) {
00480   // Take into account whether it's an add or mem instruction
00481   unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
00482   if (MI.isInlineAsm())
00483     OffsetOperandNo = FIOperandNum-1;
00484 
00485   return OffsetOperandNo;
00486 }
00487 
00488 void
00489 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00490                                      int SPAdj, unsigned FIOperandNum,
00491                                      RegScavenger *RS) const {
00492   assert(SPAdj == 0 && "Unexpected");
00493 
00494   // Get the instruction.
00495   MachineInstr &MI = *II;
00496   // Get the instruction's basic block.
00497   MachineBasicBlock &MBB = *MI.getParent();
00498   // Get the basic block's function.
00499   MachineFunction &MF = *MBB.getParent();
00500   // Get the frame info.
00501   MachineFrameInfo *MFI = MF.getFrameInfo();
00502   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
00503   DebugLoc dl = MI.getDebugLoc();
00504 
00505   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
00506 
00507   // Get the frame index.
00508   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
00509 
00510   // Get the frame pointer save index.  Users of this index are primarily
00511   // DYNALLOC instructions.
00512   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
00513   int FPSI = FI->getFramePointerSaveIndex();
00514   // Get the instruction opcode.
00515   unsigned OpC = MI.getOpcode();
00516   
00517   // Special case for dynamic alloca.
00518   if (FPSI && FrameIndex == FPSI &&
00519       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
00520     lowerDynamicAlloc(II);
00521     return;
00522   }
00523 
00524   // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
00525   if (OpC == PPC::SPILL_CR) {
00526     lowerCRSpilling(II, FrameIndex);
00527     return;
00528   } else if (OpC == PPC::RESTORE_CR) {
00529     lowerCRRestore(II, FrameIndex);
00530     return;
00531   } else if (OpC == PPC::SPILL_VRSAVE) {
00532     lowerVRSAVESpilling(II, FrameIndex);
00533     return;
00534   } else if (OpC == PPC::RESTORE_VRSAVE) {
00535     lowerVRSAVERestore(II, FrameIndex);
00536     return;
00537   }
00538 
00539   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
00540 
00541   bool is64Bit = Subtarget.isPPC64();
00542   MI.getOperand(FIOperandNum).ChangeToRegister(TFI->hasFP(MF) ?
00543                                               (is64Bit ? PPC::X31 : PPC::R31) :
00544                                                 (is64Bit ? PPC::X1 : PPC::R1),
00545                                               false);
00546 
00547   // Figure out if the offset in the instruction is shifted right two bits.
00548   bool isIXAddr = usesIXAddr(MI);
00549 
00550   // If the instruction is not present in ImmToIdxMap, then it has no immediate
00551   // form (and must be r+r).
00552   bool noImmForm = !MI.isInlineAsm() && !ImmToIdxMap.count(OpC);
00553 
00554   // Now add the frame object offset to the offset from r1.
00555   int Offset = MFI->getObjectOffset(FrameIndex);
00556   Offset += MI.getOperand(OffsetOperandNo).getImm();
00557 
00558   // If we're not using a Frame Pointer that has been set to the value of the
00559   // SP before having the stack size subtracted from it, then add the stack size
00560   // to Offset to get the correct offset.
00561   // Naked functions have stack size 0, although getStackSize may not reflect that
00562   // because we didn't call all the pieces that compute it for naked functions.
00563   if (!MF.getFunction()->getAttributes().
00564         hasAttribute(AttributeSet::FunctionIndex, Attribute::Naked))
00565     Offset += MFI->getStackSize();
00566 
00567   // If we can, encode the offset directly into the instruction.  If this is a
00568   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
00569   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
00570   // clear can be encoded.  This is extremely uncommon, because normally you
00571   // only "std" to a stack slot that is at least 4-byte aligned, but it can
00572   // happen in invalid code.
00573   if (OpC == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
00574       (!noImmForm &&
00575        isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0))) {
00576     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
00577     return;
00578   }
00579 
00580   // The offset doesn't fit into a single register, scavenge one to build the
00581   // offset in.
00582 
00583   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
00584   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00585   const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
00586   unsigned SRegHi = MF.getRegInfo().createVirtualRegister(RC),
00587            SReg = MF.getRegInfo().createVirtualRegister(RC);
00588 
00589   // Insert a set of rA with the full offset value before the ld, st, or add
00590   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
00591     .addImm(Offset >> 16);
00592   BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
00593     .addReg(SRegHi, RegState::Kill)
00594     .addImm(Offset);
00595 
00596   // Convert into indexed form of the instruction:
00597   // 
00598   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
00599   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
00600   unsigned OperandBase;
00601 
00602   if (noImmForm)
00603     OperandBase = 1;
00604   else if (OpC != TargetOpcode::INLINEASM) {
00605     assert(ImmToIdxMap.count(OpC) &&
00606            "No indexed form of load or store available!");
00607     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
00608     MI.setDesc(TII.get(NewOpcode));
00609     OperandBase = 1;
00610   } else {
00611     OperandBase = OffsetOperandNo;
00612   }
00613 
00614   unsigned StackReg = MI.getOperand(FIOperandNum).getReg();
00615   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
00616   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
00617 }
00618 
00619 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
00620   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
00621 
00622   if (!Subtarget.isPPC64())
00623     return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
00624   else
00625     return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
00626 }
00627 
00628 unsigned PPCRegisterInfo::getEHExceptionRegister() const {
00629   return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3;
00630 }
00631 
00632 unsigned PPCRegisterInfo::getEHHandlerRegister() const {
00633   return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4;
00634 }
00635 
00636 /// Returns true if the instruction's frame index
00637 /// reference would be better served by a base register other than FP
00638 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
00639 /// references it should create new base registers for.
00640 bool PPCRegisterInfo::
00641 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
00642   assert(Offset < 0 && "Local offset must be negative");
00643 
00644   unsigned FIOperandNum = 0;
00645   while (!MI->getOperand(FIOperandNum).isFI()) {
00646     ++FIOperandNum;
00647     assert(FIOperandNum < MI->getNumOperands() &&
00648            "Instr doesn't have FrameIndex operand!");
00649   }
00650 
00651   unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
00652   Offset += MI->getOperand(OffsetOperandNo).getImm();
00653 
00654   // It's the load/store FI references that cause issues, as it can be difficult
00655   // to materialize the offset if it won't fit in the literal field. Estimate
00656   // based on the size of the local frame and some conservative assumptions
00657   // about the rest of the stack frame (note, this is pre-regalloc, so
00658   // we don't know everything for certain yet) whether this offset is likely
00659   // to be out of range of the immediate. Return true if so.
00660 
00661   // We only generate virtual base registers for loads and stores that have
00662   // an r+i form. Return false for everything else.
00663   unsigned OpC = MI->getOpcode();
00664   if (!ImmToIdxMap.count(OpC))
00665     return false;
00666 
00667   // Don't generate a new virtual base register just to add zero to it.
00668   if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
00669       MI->getOperand(2).getImm() == 0)
00670     return false;
00671 
00672   MachineBasicBlock &MBB = *MI->getParent();
00673   MachineFunction &MF = *MBB.getParent();
00674 
00675   const PPCFrameLowering *PPCFI =
00676     static_cast<const PPCFrameLowering*>(MF.getTarget().getFrameLowering());
00677   unsigned StackEst =
00678     PPCFI->determineFrameLayout(MF, false, true);
00679 
00680   // If we likely don't need a stack frame, then we probably don't need a
00681   // virtual base register either.
00682   if (!StackEst)
00683     return false;
00684 
00685   // Estimate an offset from the stack pointer.
00686   // The incoming offset is relating to the SP at the start of the function,
00687   // but when we access the local it'll be relative to the SP after local
00688   // allocation, so adjust our SP-relative offset by that allocation size.
00689   Offset += StackEst;
00690 
00691   // The frame pointer will point to the end of the stack, so estimate the
00692   // offset as the difference between the object offset and the FP location.
00693   return !isFrameOffsetLegal(MI, Offset);
00694 }
00695 
00696 /// Insert defining instruction(s) for BaseReg to
00697 /// be a pointer to FrameIdx at the beginning of the basic block.
00698 void PPCRegisterInfo::
00699 materializeFrameBaseRegister(MachineBasicBlock *MBB,
00700                              unsigned BaseReg, int FrameIdx,
00701                              int64_t Offset) const {
00702   unsigned ADDriOpc = Subtarget.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
00703 
00704   MachineBasicBlock::iterator Ins = MBB->begin();
00705   DebugLoc DL;                  // Defaults to "unknown"
00706   if (Ins != MBB->end())
00707     DL = Ins->getDebugLoc();
00708 
00709   const MCInstrDesc &MCID = TII.get(ADDriOpc);
00710   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
00711   const MachineFunction &MF = *MBB->getParent();
00712   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
00713 
00714   BuildMI(*MBB, Ins, DL, MCID, BaseReg)
00715     .addFrameIndex(FrameIdx).addImm(Offset);
00716 }
00717 
00718 void
00719 PPCRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
00720                                    unsigned BaseReg, int64_t Offset) const {
00721   MachineInstr &MI = *I;
00722 
00723   unsigned FIOperandNum = 0;
00724   while (!MI.getOperand(FIOperandNum).isFI()) {
00725     ++FIOperandNum;
00726     assert(FIOperandNum < MI.getNumOperands() &&
00727            "Instr doesn't have FrameIndex operand!");
00728   }
00729 
00730   MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
00731   unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
00732   Offset += MI.getOperand(OffsetOperandNo).getImm();
00733   MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
00734 }
00735 
00736 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
00737                                          int64_t Offset) const {
00738   return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
00739          (isInt<16>(Offset) && (!usesIXAddr(*MI) || (Offset & 3) == 0));
00740 }
00741