LLVM API Documentation

PPCRegisterInfo.cpp

Go to the documentation of this file.
00001 //===- PPCRegisterInfo.cpp - PowerPC Register Information -------*- C++ -*-===//
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 "PPC.h"
00017 #include "PPCInstrBuilder.h"
00018 #include "PPCMachineFunctionInfo.h"
00019 #include "PPCRegisterInfo.h"
00020 #include "PPCFrameInfo.h"
00021 #include "PPCSubtarget.h"
00022 #include "llvm/CallingConv.h"
00023 #include "llvm/Constants.h"
00024 #include "llvm/Function.h"
00025 #include "llvm/Type.h"
00026 #include "llvm/CodeGen/ValueTypes.h"
00027 #include "llvm/CodeGen/MachineInstrBuilder.h"
00028 #include "llvm/CodeGen/MachineModuleInfo.h"
00029 #include "llvm/CodeGen/MachineFunction.h"
00030 #include "llvm/CodeGen/MachineFrameInfo.h"
00031 #include "llvm/CodeGen/MachineLocation.h"
00032 #include "llvm/CodeGen/MachineRegisterInfo.h"
00033 #include "llvm/CodeGen/RegisterScavenging.h"
00034 #include "llvm/Target/TargetFrameInfo.h"
00035 #include "llvm/Target/TargetInstrInfo.h"
00036 #include "llvm/Target/TargetMachine.h"
00037 #include "llvm/Target/TargetOptions.h"
00038 #include "llvm/Support/CommandLine.h"
00039 #include "llvm/Support/Debug.h"
00040 #include "llvm/Support/MathExtras.h"
00041 #include "llvm/ADT/BitVector.h"
00042 #include "llvm/ADT/STLExtras.h"
00043 #include <cstdlib>
00044 using namespace llvm;
00045 
00046 // FIXME This disables some code that aligns the stack to a boundary
00047 // bigger than the default (16 bytes on Darwin) when there is a stack local
00048 // of greater alignment.  This does not currently work, because the delta
00049 // between old and new stack pointers is added to offsets that reference
00050 // incoming parameters after the prolog is generated, and the code that 
00051 // does that doesn't handle a variable delta.  You don't want to do that
00052 // anyway; a better approach is to reserve another register that retains
00053 // to the incoming stack pointer, and reference parameters relative to that.
00054 #define ALIGN_STACK 0
00055 
00056 // FIXME (64-bit): Eventually enable by default.
00057 cl::opt<bool> EnablePPC32RS("enable-ppc32-regscavenger",
00058                             cl::init(false),
00059                             cl::desc("Enable PPC32 register scavenger"),
00060                             cl::Hidden);
00061 cl::opt<bool> EnablePPC64RS("enable-ppc64-regscavenger",
00062                             cl::init(false),
00063                             cl::desc("Enable PPC64 register scavenger"),
00064                             cl::Hidden);
00065 #define EnableRegisterScavenging \
00066   ((EnablePPC32RS && !Subtarget.isPPC64()) || \
00067    (EnablePPC64RS && Subtarget.isPPC64()))
00068 
00069 // FIXME (64-bit): Should be inlined.
00070 bool
00071 PPCRegisterInfo::requiresRegisterScavenging(const MachineFunction &) const {
00072   return EnableRegisterScavenging;
00073 }
00074 
00075 /// getRegisterNumbering - Given the enum value for some register, e.g.
00076 /// PPC::F14, return the number that it corresponds to (e.g. 14).
00077 unsigned PPCRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
00078   using namespace PPC;
00079   switch (RegEnum) {
00080   case 0: return 0;
00081   case R0 :  case X0 :  case F0 :  case V0 : case CR0:  case CR0LT: return  0;
00082   case R1 :  case X1 :  case F1 :  case V1 : case CR1:  case CR0GT: return  1;
00083   case R2 :  case X2 :  case F2 :  case V2 : case CR2:  case CR0EQ: return  2;
00084   case R3 :  case X3 :  case F3 :  case V3 : case CR3:  case CR0UN: return  3;
00085   case R4 :  case X4 :  case F4 :  case V4 : case CR4:  case CR1LT: return  4;
00086   case R5 :  case X5 :  case F5 :  case V5 : case CR5:  case CR1GT: return  5;
00087   case R6 :  case X6 :  case F6 :  case V6 : case CR6:  case CR1EQ: return  6;
00088   case R7 :  case X7 :  case F7 :  case V7 : case CR7:  case CR1UN: return  7;
00089   case R8 :  case X8 :  case F8 :  case V8 : case CR2LT: return  8;
00090   case R9 :  case X9 :  case F9 :  case V9 : case CR2GT: return  9;
00091   case R10:  case X10:  case F10:  case V10: case CR2EQ: return 10;
00092   case R11:  case X11:  case F11:  case V11: case CR2UN: return 11;
00093   case R12:  case X12:  case F12:  case V12: case CR3LT: return 12;
00094   case R13:  case X13:  case F13:  case V13: case CR3GT: return 13;
00095   case R14:  case X14:  case F14:  case V14: case CR3EQ: return 14;
00096   case R15:  case X15:  case F15:  case V15: case CR3UN: return 15;
00097   case R16:  case X16:  case F16:  case V16: case CR4LT: return 16;
00098   case R17:  case X17:  case F17:  case V17: case CR4GT: return 17;
00099   case R18:  case X18:  case F18:  case V18: case CR4EQ: return 18;
00100   case R19:  case X19:  case F19:  case V19: case CR4UN: return 19;
00101   case R20:  case X20:  case F20:  case V20: case CR5LT: return 20;
00102   case R21:  case X21:  case F21:  case V21: case CR5GT: return 21;
00103   case R22:  case X22:  case F22:  case V22: case CR5EQ: return 22;
00104   case R23:  case X23:  case F23:  case V23: case CR5UN: return 23;
00105   case R24:  case X24:  case F24:  case V24: case CR6LT: return 24;
00106   case R25:  case X25:  case F25:  case V25: case CR6GT: return 25;
00107   case R26:  case X26:  case F26:  case V26: case CR6EQ: return 26;
00108   case R27:  case X27:  case F27:  case V27: case CR6UN: return 27;
00109   case R28:  case X28:  case F28:  case V28: case CR7LT: return 28;
00110   case R29:  case X29:  case F29:  case V29: case CR7GT: return 29;
00111   case R30:  case X30:  case F30:  case V30: case CR7EQ: return 30;
00112   case R31:  case X31:  case F31:  case V31: case CR7UN: return 31;
00113   default:
00114     cerr << "Unhandled reg in PPCRegisterInfo::getRegisterNumbering!\n";
00115     abort();
00116   }
00117 }
00118 
00119 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
00120                                  const TargetInstrInfo &tii)
00121   : PPCGenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
00122     Subtarget(ST), TII(tii) {
00123   ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
00124   ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
00125   ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
00126   ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
00127   ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
00128   ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
00129   ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
00130   ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
00131 
00132   // 64-bit
00133   ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
00134   ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
00135   ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
00136   ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
00137   ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; ImmToIdxMap[PPC::STD_32] = PPC::STDX_32;
00138 }
00139 
00140 const unsigned*
00141 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
00142   // 32-bit Darwin calling convention. 
00143   static const unsigned Macho32_CalleeSavedRegs[] = {
00144               PPC::R13, PPC::R14, PPC::R15,
00145     PPC::R16, PPC::R17, PPC::R18, PPC::R19,
00146     PPC::R20, PPC::R21, PPC::R22, PPC::R23,
00147     PPC::R24, PPC::R25, PPC::R26, PPC::R27,
00148     PPC::R28, PPC::R29, PPC::R30, PPC::R31,
00149 
00150     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
00151     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
00152     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
00153     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
00154     PPC::F30, PPC::F31,
00155     
00156     PPC::CR2, PPC::CR3, PPC::CR4,
00157     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
00158     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
00159     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
00160     
00161     PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
00162     PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
00163     PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
00164     
00165     PPC::LR,  0
00166   };
00167   
00168   static const unsigned ELF32_CalleeSavedRegs[] = {
00169               PPC::R13, PPC::R14, PPC::R15,
00170     PPC::R16, PPC::R17, PPC::R18, PPC::R19,
00171     PPC::R20, PPC::R21, PPC::R22, PPC::R23,
00172     PPC::R24, PPC::R25, PPC::R26, PPC::R27,
00173     PPC::R28, PPC::R29, PPC::R30, PPC::R31,
00174 
00175                                   PPC::F9,
00176     PPC::F10, PPC::F11, PPC::F12, PPC::F13,
00177     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
00178     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
00179     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
00180     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
00181     PPC::F30, PPC::F31,
00182     
00183     PPC::CR2, PPC::CR3, PPC::CR4,
00184     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
00185     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
00186     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
00187     
00188     PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
00189     PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
00190     PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
00191     
00192     PPC::LR,  0
00193   };
00194   // 64-bit Darwin calling convention. 
00195   static const unsigned Macho64_CalleeSavedRegs[] = {
00196     PPC::X14, PPC::X15,
00197     PPC::X16, PPC::X17, PPC::X18, PPC::X19,
00198     PPC::X20, PPC::X21, PPC::X22, PPC::X23,
00199     PPC::X24, PPC::X25, PPC::X26, PPC::X27,
00200     PPC::X28, PPC::X29, PPC::X30, PPC::X31,
00201     
00202     PPC::F14, PPC::F15, PPC::F16, PPC::F17,
00203     PPC::F18, PPC::F19, PPC::F20, PPC::F21,
00204     PPC::F22, PPC::F23, PPC::F24, PPC::F25,
00205     PPC::F26, PPC::F27, PPC::F28, PPC::F29,
00206     PPC::F30, PPC::F31,
00207     
00208     PPC::CR2, PPC::CR3, PPC::CR4,
00209     PPC::V20, PPC::V21, PPC::V22, PPC::V23,
00210     PPC::V24, PPC::V25, PPC::V26, PPC::V27,
00211     PPC::V28, PPC::V29, PPC::V30, PPC::V31,
00212     
00213     PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
00214     PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
00215     PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
00216     
00217     PPC::LR8,  0
00218   };
00219   
00220   if (Subtarget.isMachoABI())
00221     return Subtarget.isPPC64() ? Macho64_CalleeSavedRegs :
00222                                  Macho32_CalleeSavedRegs;
00223   
00224   // ELF 32.
00225   return ELF32_CalleeSavedRegs;
00226 }
00227 
00228 const TargetRegisterClass* const*
00229 PPCRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
00230   // 32-bit Macho calling convention. 
00231   static const TargetRegisterClass * const Macho32_CalleeSavedRegClasses[] = {
00232                        &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00233     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00234     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00235     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00236     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00237 
00238     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00239     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00240     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00241     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00242     &PPC::F8RCRegClass,&PPC::F8RCRegClass,
00243     
00244     &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
00245     
00246     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00247     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00248     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00249     
00250     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00251     &PPC::CRBITRCRegClass, 
00252     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00253     &PPC::CRBITRCRegClass, 
00254     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00255     &PPC::CRBITRCRegClass, 
00256     
00257     &PPC::GPRCRegClass, 0
00258   };
00259   
00260   static const TargetRegisterClass * const ELF32_CalleeSavedRegClasses[] = {
00261                        &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00262     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00263     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00264     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00265     &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
00266 
00267                                                              &PPC::F8RCRegClass,
00268     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00269     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00270     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00271     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00272     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00273     &PPC::F8RCRegClass,&PPC::F8RCRegClass,
00274     
00275     &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
00276     
00277     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00278     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00279     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00280     
00281     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00282     &PPC::CRBITRCRegClass, 
00283     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00284     &PPC::CRBITRCRegClass, 
00285     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00286     &PPC::CRBITRCRegClass, 
00287     
00288     &PPC::GPRCRegClass, 0
00289   };
00290   
00291   // 64-bit Macho calling convention. 
00292   static const TargetRegisterClass * const Macho64_CalleeSavedRegClasses[] = {
00293     &PPC::G8RCRegClass,&PPC::G8RCRegClass,
00294     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
00295     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
00296     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
00297     &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
00298     
00299     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00300     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00301     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00302     &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
00303     &PPC::F8RCRegClass,&PPC::F8RCRegClass,
00304     
00305     &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
00306     
00307     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00308     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00309     &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
00310     
00311     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00312     &PPC::CRBITRCRegClass, 
00313     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00314     &PPC::CRBITRCRegClass, 
00315     &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
00316     &PPC::CRBITRCRegClass, 
00317     
00318     &PPC::G8RCRegClass, 0
00319   };
00320   
00321   if (Subtarget.isMachoABI())
00322     return Subtarget.isPPC64() ? Macho64_CalleeSavedRegClasses :
00323                                  Macho32_CalleeSavedRegClasses;
00324   
00325   // ELF 32.
00326   return ELF32_CalleeSavedRegClasses;
00327 }
00328 
00329 // needsFP - Return true if the specified function should have a dedicated frame
00330 // pointer register.  This is true if the function has variable sized allocas or
00331 // if frame pointer elimination is disabled.
00332 //
00333 static bool needsFP(const MachineFunction &MF) {
00334   const MachineFrameInfo *MFI = MF.getFrameInfo();
00335   return NoFramePointerElim || MFI->hasVarSizedObjects() ||
00336     (PerformTailCallOpt && MF.getInfo<PPCFunctionInfo>()->hasFastCall());
00337 }
00338 
00339 static bool spillsCR(const MachineFunction &MF) {
00340   const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
00341   return FuncInfo->isCRSpilled();
00342 }
00343 
00344 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
00345   BitVector Reserved(getNumRegs());
00346   Reserved.set(PPC::R0);
00347   Reserved.set(PPC::R1);
00348   Reserved.set(PPC::LR);
00349   Reserved.set(PPC::LR8);
00350   Reserved.set(PPC::RM);
00351 
00352   // In Linux, r2 is reserved for the OS.
00353   if (!Subtarget.isDarwin())
00354     Reserved.set(PPC::R2);
00355 
00356   // On PPC64, r13 is the thread pointer. Never allocate this register. Note
00357   // that this is over conservative, as it also prevents allocation of R31 when
00358   // the FP is not needed.
00359   if (Subtarget.isPPC64()) {
00360     Reserved.set(PPC::R13);
00361     Reserved.set(PPC::R31);
00362 
00363     if (!EnableRegisterScavenging)
00364       Reserved.set(PPC::R0);    // FIXME (64-bit): Remove
00365 
00366     Reserved.set(PPC::X0);
00367     Reserved.set(PPC::X1);
00368     Reserved.set(PPC::X13);
00369     Reserved.set(PPC::X31);
00370   }
00371 
00372   if (needsFP(MF))
00373     Reserved.set(PPC::R31);
00374 
00375   return Reserved;
00376 }
00377 
00378 //===----------------------------------------------------------------------===//
00379 // Stack Frame Processing methods
00380 //===----------------------------------------------------------------------===//
00381 
00382 // hasFP - Return true if the specified function actually has a dedicated frame
00383 // pointer register.  This is true if the function needs a frame pointer and has
00384 // a non-zero stack size.
00385 bool PPCRegisterInfo::hasFP(const MachineFunction &MF) const {
00386   const MachineFrameInfo *MFI = MF.getFrameInfo();
00387   return MFI->getStackSize() && needsFP(MF);
00388 }
00389 
00390 /// MustSaveLR - Return true if this function requires that we save the LR
00391 /// register onto the stack in the prolog and restore it in the epilog of the
00392 /// function.
00393 static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
00394   const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
00395   
00396   // We need a save/restore of LR if there is any def of LR (which is
00397   // defined by calls, including the PIC setup sequence), or if there is
00398   // some use of the LR stack slot (e.g. for builtin_return_address).
00399   // (LR comes in 32 and 64 bit versions.)
00400   MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
00401   return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
00402 }
00403 
00404 
00405 
00406 void PPCRegisterInfo::
00407 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
00408                               MachineBasicBlock::iterator I) const {
00409   if (PerformTailCallOpt && I->getOpcode() == PPC::ADJCALLSTACKUP) {
00410     // Add (actually subtract) back the amount the callee popped on return.
00411     if (int CalleeAmt =  I->getOperand(1).getImm()) {
00412       bool is64Bit = Subtarget.isPPC64();
00413       CalleeAmt *= -1;
00414       unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
00415       unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
00416       unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
00417       unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
00418       unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
00419       unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
00420 
00421       if (isInt16(CalleeAmt)) {
00422         BuildMI(MBB, I, TII.get(ADDIInstr), StackReg).addReg(StackReg).
00423           addImm(CalleeAmt);
00424       } else {
00425         MachineBasicBlock::iterator MBBI = I;
00426         BuildMI(MBB, MBBI, TII.get(LISInstr), TmpReg)
00427           .addImm(CalleeAmt >> 16);
00428         BuildMI(MBB, MBBI, TII.get(ORIInstr), TmpReg)
00429           .addReg(TmpReg, false, false, true)
00430           .addImm(CalleeAmt & 0xFFFF);
00431         BuildMI(MBB, MBBI, TII.get(ADDInstr))
00432           .addReg(StackReg)
00433           .addReg(StackReg)
00434           .addReg(TmpReg);
00435       }
00436     }
00437   }
00438   // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
00439   MBB.erase(I);
00440 }
00441 
00442 /// findScratchRegister - Find a 'free' PPC register. Try for a call-clobbered
00443 /// register first and then a spilled callee-saved register if that fails.
00444 static
00445 unsigned findScratchRegister(MachineBasicBlock::iterator II, RegScavenger *RS,
00446                              const TargetRegisterClass *RC, int SPAdj) {
00447   assert(RS && "Register scavenging must be on");
00448   unsigned Reg = RS->FindUnusedReg(RC, true);
00449   // FIXME: move ARM callee-saved reg scan to target independent code, then 
00450   // search for already spilled CS register here.
00451   if (Reg == 0)
00452     Reg = RS->scavengeRegister(RC, II, SPAdj);
00453   return Reg;
00454 }
00455 
00456 /// lowerDynamicAlloc - Generate the code for allocating an object in the
00457 /// current frame.  The sequence of code with be in the general form
00458 ///
00459 ///   addi   R0, SP, #frameSize ; get the address of the previous frame
00460 ///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
00461 ///   addi   Rnew, SP, #maxCalFrameSize ; get the top of the allocation
00462 ///
00463 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II,
00464                                         int SPAdj, RegScavenger *RS) const {
00465   // Get the instruction.
00466   MachineInstr &MI = *II;
00467   // Get the instruction's basic block.
00468   MachineBasicBlock &MBB = *MI.getParent();
00469   // Get the basic block's function.
00470   MachineFunction &MF = *MBB.getParent();
00471   // Get the frame info.
00472   MachineFrameInfo *MFI = MF.getFrameInfo();
00473   // Determine whether 64-bit pointers are used.
00474   bool LP64 = Subtarget.isPPC64();
00475 
00476   // Get the maximum call stack size.
00477   unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
00478   // Get the total frame size.
00479   unsigned FrameSize = MFI->getStackSize();
00480   
00481   // Get stack alignments.
00482   unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
00483   unsigned MaxAlign = MFI->getMaxAlignment();
00484   assert(MaxAlign <= TargetAlign &&
00485          "Dynamic alloca with large aligns not supported");
00486 
00487   // Determine the previous frame's address.  If FrameSize can't be
00488   // represented as 16 bits or we need special alignment, then we load the
00489   // previous frame's address from 0(SP).  Why not do an addis of the hi? 
00490   // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 
00491   // Constructing the constant and adding would take 3 instructions. 
00492   // Fortunately, a frame greater than 32K is rare.
00493   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
00494   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00495   const TargetRegisterClass *RC = LP64 ? G8RC : GPRC;
00496 
00497   // FIXME (64-bit): Use "findScratchRegister"
00498   unsigned Reg;
00499   if (EnableRegisterScavenging)
00500     Reg = findScratchRegister(II, RS, RC, SPAdj);
00501   else
00502     Reg = PPC::R0;
00503   
00504   if (MaxAlign < TargetAlign && isInt16(FrameSize)) {
00505     BuildMI(MBB, II, TII.get(PPC::ADDI), Reg)
00506       .addReg(PPC::R31)
00507       .addImm(FrameSize);
00508   } else if (LP64) {
00509     if (EnableRegisterScavenging) // FIXME (64-bit): Use "true" part.
00510       BuildMI(MBB, II, TII.get(PPC::LD), Reg)
00511         .addImm(0)
00512         .addReg(PPC::X1);
00513     else
00514       BuildMI(MBB, II, TII.get(PPC::LD), PPC::X0)
00515         .addImm(0)
00516         .addReg(PPC::X1);
00517   } else {
00518     BuildMI(MBB, II, TII.get(PPC::LWZ), Reg)
00519       .addImm(0)
00520       .addReg(PPC::R1);
00521   }
00522   
00523   // Grow the stack and update the stack pointer link, then determine the
00524   // address of new allocated space.
00525   if (LP64) {
00526     if (EnableRegisterScavenging) // FIXME (64-bit): Use "true" part.
00527       BuildMI(MBB, II, TII.get(PPC::STDUX))
00528         .addReg(Reg, false, false, true)
00529         .addReg(PPC::X1)
00530         .addReg(MI.getOperand(1).getReg());
00531     else
00532       BuildMI(MBB, II, TII.get(PPC::STDUX))
00533         .addReg(PPC::X0, false, false, true)
00534         .addReg(PPC::X1)
00535         .addReg(MI.getOperand(1).getReg());
00536 
00537     if (!MI.getOperand(1).isKill())
00538       BuildMI(MBB, II, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
00539         .addReg(PPC::X1)
00540         .addImm(maxCallFrameSize);
00541     else
00542       // Implicitly kill the register.
00543       BuildMI(MBB, II, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
00544         .addReg(PPC::X1)
00545         .addImm(maxCallFrameSize)
00546         .addReg(MI.getOperand(1).getReg(), false, true, true);
00547   } else {
00548     BuildMI(MBB, II, TII.get(PPC::STWUX))
00549       .addReg(Reg, false, false, true)
00550       .addReg(PPC::R1)
00551       .addReg(MI.getOperand(1).getReg());
00552 
00553     if (!MI.getOperand(1).isKill())
00554       BuildMI(MBB, II, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
00555         .addReg(PPC::R1)
00556         .addImm(maxCallFrameSize);
00557     else
00558       // Implicitly kill the register.
00559       BuildMI(MBB, II, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
00560         .addReg(PPC::R1)
00561         .addImm(maxCallFrameSize)
00562         .addReg(MI.getOperand(1).getReg(), false, true, true);
00563   }
00564   
00565   // Discard the DYNALLOC instruction.
00566   MBB.erase(II);
00567 }
00568 
00569 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
00570 /// reserving a whole register (R0), we scrounge for one here. This generates
00571 /// code like this:
00572 ///
00573 ///   mfcr rA                  ; Move the conditional register into GPR rA.
00574 ///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
00575 ///   stw rA, FI               ; Store rA to the frame.
00576 ///
00577 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
00578                                       unsigned FrameIndex, int SPAdj,
00579                                       RegScavenger *RS) const {
00580   // Get the instruction.
00581   MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>, <FI>
00582   // Get the instruction's basic block.
00583   MachineBasicBlock &MBB = *MI.getParent();
00584 
00585   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
00586   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00587   const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
00588   unsigned Reg = findScratchRegister(II, RS, RC, SPAdj);
00589 
00590   // We need to store the CR in the low 4-bits of the saved value. First, issue
00591   // an MFCR to save all of the CRBits. Add an implicit kill of the CR.
00592   if (!MI.getOperand(0).isKill())
00593     BuildMI(MBB, II, TII.get(PPC::MFCR), Reg);
00594   else
00595     // Implicitly kill the CR register.
00596     BuildMI(MBB, II, TII.get(PPC::MFCR), Reg)
00597       .addReg(MI.getOperand(0).getReg(), false, true, true);
00598     
00599   // If the saved register wasn't CR0, shift the bits left so that they are in
00600   // CR0's slot.
00601   unsigned SrcReg = MI.getOperand(0).getReg();
00602   if (SrcReg != PPC::CR0)
00603     // rlwinm rA, rA, ShiftBits, 0, 31.
00604     BuildMI(MBB, II, TII.get(PPC::RLWINM), Reg)
00605       .addReg(Reg, false, false, true)
00606       .addImm(PPCRegisterInfo::getRegisterNumbering(SrcReg) * 4)
00607       .addImm(0)
00608       .addImm(31);
00609 
00610   addFrameReference(BuildMI(MBB, II, TII.get(PPC::STW))
00611                     .addReg(Reg, false, false, MI.getOperand(1).getImm()),
00612                     FrameIndex);
00613 
00614   // Discard the pseudo instruction.
00615   MBB.erase(II);
00616 }
00617 
00618 void PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00619                                           int SPAdj, RegScavenger *RS) const {
00620   assert(SPAdj == 0 && "Unexpected");
00621 
00622   // Get the instruction.
00623   MachineInstr &MI = *II;
00624   // Get the instruction's basic block.
00625   MachineBasicBlock &MBB = *MI.getParent();
00626   // Get the basic block's function.
00627   MachineFunction &MF = *MBB.getParent();
00628   // Get the frame info.
00629   MachineFrameInfo *MFI = MF.getFrameInfo();
00630 
00631   // Find out which operand is the frame index.
00632   unsigned FIOperandNo = 0;
00633   while (!MI.getOperand(FIOperandNo).isFI()) {
00634     ++FIOperandNo;
00635     assert(FIOperandNo != MI.getNumOperands() &&
00636            "Instr doesn't have FrameIndex operand!");
00637   }
00638   // Take into account whether it's an add or mem instruction
00639   unsigned OffsetOperandNo = (FIOperandNo == 2) ? 1 : 2;
00640   if (MI.getOpcode() == TargetInstrInfo::INLINEASM)
00641     OffsetOperandNo = FIOperandNo-1;
00642 
00643   // Get the frame index.
00644   int FrameIndex = MI.getOperand(FIOperandNo).getIndex();
00645 
00646   // Get the frame pointer save index.  Users of this index are primarily
00647   // DYNALLOC instructions.
00648   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
00649   int FPSI = FI->getFramePointerSaveIndex();
00650   // Get the instruction opcode.
00651   unsigned OpC = MI.getOpcode();
00652   
00653   // Special case for dynamic alloca.
00654   if (FPSI && FrameIndex == FPSI &&
00655       (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
00656     lowerDynamicAlloc(II, SPAdj, RS);
00657     return;
00658   }
00659 
00660   // Special case for pseudo-op SPILL_CR.
00661   if (EnableRegisterScavenging) // FIXME (64-bit): Enable by default.
00662     if (OpC == PPC::SPILL_CR) {
00663       lowerCRSpilling(II, FrameIndex, SPAdj, RS);
00664       return;
00665     }
00666 
00667   // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
00668   MI.getOperand(FIOperandNo).ChangeToRegister(hasFP(MF) ? PPC::R31 : PPC::R1,
00669                                               false);
00670 
00671   // Figure out if the offset in the instruction is shifted right two bits. This
00672   // is true for instructions like "STD", which the machine implicitly adds two
00673   // low zeros to.
00674   bool isIXAddr = false;
00675   switch (OpC) {
00676   case PPC::LWA:
00677   case PPC::LD:
00678   case PPC::STD:
00679   case PPC::STD_32:
00680     isIXAddr = true;
00681     break;
00682   }
00683   
00684   // Now add the frame object offset to the offset from r1.
00685   int Offset = MFI->getObjectOffset(FrameIndex);
00686   if (!isIXAddr)
00687     Offset += MI.getOperand(OffsetOperandNo).getImm();
00688   else
00689     Offset += MI.getOperand(OffsetOperandNo).getImm() << 2;
00690 
00691   // If we're not using a Frame Pointer that has been set to the value of the
00692   // SP before having the stack size subtracted from it, then add the stack size
00693   // to Offset to get the correct offset.
00694   Offset += MFI->getStackSize();
00695 
00696   // If we can, encode the offset directly into the instruction.  If this is a
00697   // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
00698   // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
00699   // clear can be encoded.  This is extremely uncommon, because normally you
00700   // only "std" to a stack slot that is at least 4-byte aligned, but it can
00701   // happen in invalid code.
00702   if (isInt16(Offset) && (!isIXAddr || (Offset & 3) == 0)) {
00703     if (isIXAddr)
00704       Offset >>= 2;    // The actual encoded value has the low two bits zero.
00705     MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
00706     return;
00707   }
00708 
00709   // The offset doesn't fit into a single register, scavenge one to build the
00710   // offset in.
00711   // FIXME: figure out what SPAdj is doing here.
00712 
00713   // FIXME (64-bit): Use "findScratchRegister".
00714   unsigned SReg;
00715   if (EnableRegisterScavenging)
00716     SReg = findScratchRegister(II, RS, &PPC::GPRCRegClass, SPAdj);
00717   else
00718     SReg = PPC::R0;
00719 
00720   // Insert a set of rA with the full offset value before the ld, st, or add
00721   BuildMI(MBB, II, TII.get(PPC::LIS), SReg)
00722     .addImm(Offset >> 16);
00723   BuildMI(MBB, II, TII.get(PPC::ORI), SReg)
00724     .addReg(SReg, false, false, true)
00725     .addImm(Offset);
00726 
00727   // Convert into indexed form of the instruction:
00728   // 
00729   //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
00730   //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
00731   unsigned OperandBase;
00732 
00733   if (OpC != TargetInstrInfo::INLINEASM) {
00734     assert(ImmToIdxMap.count(OpC) &&
00735            "No indexed form of load or store available!");
00736     unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
00737     MI.setDesc(TII.get(NewOpcode));
00738     OperandBase = 1;
00739   } else {
00740     OperandBase = OffsetOperandNo;
00741   }
00742     
00743   unsigned StackReg = MI.getOperand(FIOperandNo).getReg();
00744   MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
00745   MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false);
00746 }
00747 
00748 /// VRRegNo - Map from a numbered VR register to its enum value.
00749 ///
00750 static const unsigned short VRRegNo[] = {
00751  PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
00752  PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
00753  PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
00754  PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
00755 };
00756 
00757 /// RemoveVRSaveCode - We have found that this function does not need any code
00758 /// to manipulate the VRSAVE register, even though it uses vector registers.
00759 /// This can happen when the only registers used are known to be live in or out
00760 /// of the function.  Remove all of the VRSAVE related code from the function.
00761 static void RemoveVRSaveCode(MachineInstr *MI) {
00762   MachineBasicBlock *Entry = MI->getParent();
00763   MachineFunction *MF = Entry->getParent();
00764 
00765   // We know that the MTVRSAVE instruction immediately follows MI.  Remove it.
00766   MachineBasicBlock::iterator MBBI = MI;
00767   ++MBBI;
00768   assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
00769   MBBI->eraseFromParent();
00770   
00771   bool RemovedAllMTVRSAVEs = true;
00772   // See if we can find and remove the MTVRSAVE instruction from all of the
00773   // epilog blocks.
00774   for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
00775     // If last instruction is a return instruction, add an epilogue
00776     if (!I->empty() && I->back().getDesc().isReturn()) {
00777       bool FoundIt = false;
00778       for (MBBI = I->end(); MBBI != I->begin(); ) {
00779         --MBBI;
00780         if (MBBI->getOpcode() == PPC::MTVRSAVE) {
00781           MBBI->eraseFromParent();  // remove it.
00782           FoundIt = true;
00783           break;
00784         }
00785       }
00786       RemovedAllMTVRSAVEs &= FoundIt;
00787     }
00788   }
00789 
00790   // If we found and removed all MTVRSAVE instructions, remove the read of
00791   // VRSAVE as well.
00792   if (RemovedAllMTVRSAVEs) {
00793     MBBI = MI;
00794     assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
00795     --MBBI;
00796     assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
00797     MBBI->eraseFromParent();
00798   }
00799   
00800   // Finally, nuke the UPDATE_VRSAVE.
00801   MI->eraseFromParent();
00802 }
00803 
00804 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
00805 // instruction selector.  Based on the vector registers that have been used,
00806 // transform this into the appropriate ORI instruction.
00807 static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
00808   MachineFunction *MF = MI->getParent()->getParent();
00809 
00810   unsigned UsedRegMask = 0;
00811   for (unsigned i = 0; i != 32; ++i)
00812     if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
00813       UsedRegMask |= 1 << (31-i);
00814   
00815   // Live in and live out values already must be in the mask, so don't bother
00816   // marking them.
00817   for (MachineRegisterInfo::livein_iterator
00818        I = MF->getRegInfo().livein_begin(),
00819        E = MF->getRegInfo().livein_end(); I != E; ++I) {
00820     unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
00821     if (VRRegNo[RegNo] == I->first)        // If this really is a vector reg.
00822       UsedRegMask &= ~(1 << (31-RegNo));   // Doesn't need to be marked.
00823   }
00824   for (MachineRegisterInfo::liveout_iterator
00825        I = MF->getRegInfo().liveout_begin(),
00826        E = MF->getRegInfo().liveout_end(); I != E; ++I) {
00827     unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
00828     if (VRRegNo[RegNo] == *I)              // If this really is a vector reg.
00829       UsedRegMask &= ~(1 << (31-RegNo));   // Doesn't need to be marked.
00830   }
00831   
00832   // If no registers are used, turn this into a copy.
00833   if (UsedRegMask == 0) {
00834     // Remove all VRSAVE code.
00835     RemoveVRSaveCode(MI);
00836     return;
00837   }
00838 
00839   unsigned SrcReg = MI->getOperand(1).getReg();
00840   unsigned DstReg = MI->getOperand(0).getReg();
00841 
00842   if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
00843     if (DstReg != SrcReg)
00844       BuildMI(*MI->getParent(), MI, TII.get(PPC::ORI), DstReg)
00845         .addReg(SrcReg)
00846         .addImm(UsedRegMask);
00847     else
00848       BuildMI(*MI->getParent(), MI, TII.get(PPC::ORI), DstReg)
00849         .addReg(SrcReg, false, false, true)
00850         .addImm(UsedRegMask);
00851   } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
00852     if (DstReg != SrcReg)
00853       BuildMI(*MI->getParent(), MI, TII.get(PPC::ORIS), DstReg)
00854         .addReg(SrcReg)
00855         .addImm(UsedRegMask >> 16);
00856     else
00857       BuildMI(*MI->getParent(), MI, TII.get(PPC::ORIS), DstReg)
00858         .addReg(SrcReg, false, false, true)
00859         .addImm(UsedRegMask >> 16);
00860   } else {
00861     if (DstReg != SrcReg)
00862       BuildMI(*MI->getParent(), MI, TII.get(PPC::ORIS), DstReg)
00863         .addReg(SrcReg)
00864         .addImm(UsedRegMask >> 16);
00865     else
00866       BuildMI(*MI->getParent(), MI, TII.get(PPC::ORIS), DstReg)
00867         .addReg(SrcReg, false, false, true)
00868         .addImm(UsedRegMask >> 16);
00869 
00870     BuildMI(*MI->getParent(), MI, TII.get(PPC::ORI), DstReg)
00871       .addReg(DstReg, false, false, true)
00872       .addImm(UsedRegMask & 0xFFFF);
00873   }
00874   
00875   // Remove the old UPDATE_VRSAVE instruction.
00876   MI->eraseFromParent();
00877 }
00878 
00879 /// determineFrameLayout - Determine the size of the frame and maximum call
00880 /// frame size.
00881 void PPCRegisterInfo::determineFrameLayout(MachineFunction &MF) const {
00882   MachineFrameInfo *MFI = MF.getFrameInfo();
00883 
00884   // Get the number of bytes to allocate from the FrameInfo
00885   unsigned FrameSize = MFI->getStackSize();
00886   
00887   // Get the alignments provided by the target, and the maximum alignment
00888   // (if any) of the fixed frame objects.
00889   unsigned MaxAlign = MFI->getMaxAlignment();
00890   unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
00891   unsigned AlignMask = TargetAlign - 1;  //
00892 
00893   // If we are a leaf function, and use up to 224 bytes of stack space,
00894   // don't have a frame pointer, calls, or dynamic alloca then we do not need
00895   // to adjust the stack pointer (we fit in the Red Zone).
00896   if (!DisableRedZone &&
00897       FrameSize <= 224 &&                          // Fits in red zone.
00898       !MFI->hasVarSizedObjects() &&                // No dynamic alloca.
00899       !MFI->hasCalls() &&                          // No calls.
00900       (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment.
00901     // No need for frame
00902     MFI->setStackSize(0);
00903     return;
00904   }
00905   
00906   // Get the maximum call frame size of all the calls.
00907   unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
00908   
00909   // Maximum call frame needs to be at least big enough for linkage and 8 args.
00910   unsigned minCallFrameSize =
00911     PPCFrameInfo::getMinCallFrameSize(Subtarget.isPPC64(), 
00912                                       Subtarget.isMachoABI());
00913   maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
00914 
00915   // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
00916   // that allocations will be aligned.
00917   if (MFI->hasVarSizedObjects())
00918     maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
00919   
00920   // Update maximum call frame size.
00921   MFI->setMaxCallFrameSize(maxCallFrameSize);
00922   
00923   // Include call frame size in total.
00924   FrameSize += maxCallFrameSize;
00925   
00926   // Make sure the frame is aligned.
00927   FrameSize = (FrameSize + AlignMask) & ~AlignMask;
00928 
00929   // Update frame info.
00930   MFI->setStackSize(FrameSize);
00931 }
00932 
00933 void
00934 PPCRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
00935                                                       RegScavenger *RS) const {
00936   //  Save and clear the LR state.
00937   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
00938   unsigned LR = getRARegister();
00939   FI->setMustSaveLR(MustSaveLR(MF, LR));
00940   MF.getRegInfo().setPhysRegUnused(LR);
00941 
00942   //  Save R31 if necessary
00943   int FPSI = FI->getFramePointerSaveIndex();
00944   bool IsPPC64 = Subtarget.isPPC64();
00945   bool IsELF32_ABI = Subtarget.isELF32_ABI();
00946   bool IsMachoABI  = Subtarget.isMachoABI();
00947   MachineFrameInfo *MFI = MF.getFrameInfo();
00948  
00949   // If the frame pointer save index hasn't been defined yet.
00950   if (!FPSI && (NoFramePointerElim || MFI->hasVarSizedObjects()) &&
00951       IsELF32_ABI) {
00952     // Find out what the fix offset of the frame pointer save area.
00953     int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64,
00954                                                            IsMachoABI);
00955     // Allocate the frame index for frame pointer save area.
00956     FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
00957     // Save the result.
00958     FI->setFramePointerSaveIndex(FPSI);                      
00959   }
00960 
00961   // Reserve stack space to move the linkage area to in case of a tail call.
00962   int TCSPDelta = 0;
00963   if (PerformTailCallOpt && (TCSPDelta=FI->getTailCallSPDelta()) < 0) {
00964     int AddFPOffsetAmount = IsELF32_ABI ? -4 : 0;
00965     MF.getFrameInfo()->CreateFixedObject( -1 * TCSPDelta,
00966                                           AddFPOffsetAmount + TCSPDelta);
00967   }
00968   // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
00969   // a large stack, which will require scavenging a register to materialize a
00970   // large offset.
00971   // FIXME: this doesn't actually check stack size, so is a bit pessimistic
00972   // FIXME: doesn't detect whether or not we need to spill vXX, which requires
00973   //        r0 for now.
00974 
00975   if (EnableRegisterScavenging) // FIXME (64-bit): Enable.
00976     if (needsFP(MF) || spillsCR(MF)) {
00977       const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
00978       const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
00979       const TargetRegisterClass *RC = IsPPC64 ? G8RC : GPRC;
00980       RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
00981                                                          RC->getAlignment()));
00982     }
00983 }
00984 
00985 void
00986 PPCRegisterInfo::emitPrologue(MachineFunction &MF) const {
00987   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
00988   MachineBasicBlock::iterator MBBI = MBB.begin();
00989   MachineFrameInfo *MFI = MF.getFrameInfo();
00990   MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
00991   bool needsFrameMoves = (MMI && MMI->hasDebugInfo()) ||
00992        !MF.getFunction()->doesNotThrow() ||
00993        UnwindTablesMandatory;
00994   
00995   // Prepare for frame info.
00996   unsigned FrameLabelId = 0;
00997 
00998   // Scan the prolog, looking for an UPDATE_VRSAVE instruction.  If we find it,
00999   // process it.
01000   for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
01001     if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
01002       HandleVRSaveUpdate(MBBI, TII);
01003       break;
01004     }
01005   }
01006   
01007   // Move MBBI back to the beginning of the function.
01008   MBBI = MBB.begin();
01009 
01010   // Work out frame sizes.
01011   determineFrameLayout(MF);
01012   unsigned FrameSize = MFI->getStackSize();
01013   
01014   int NegFrameSize = -FrameSize;
01015   
01016   // Get processor type.
01017   bool IsPPC64 = Subtarget.isPPC64();
01018   // Get operating system
01019   bool IsMachoABI = Subtarget.isMachoABI();
01020   // Check if the link register (LR) must be saved.
01021   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
01022   bool MustSaveLR = FI->mustSaveLR();
01023   // Do we have a frame pointer for this function?
01024   bool HasFP = hasFP(MF) && FrameSize;
01025   
01026   int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, IsMachoABI);
01027   int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64, IsMachoABI);
01028 
01029   if (IsPPC64) {
01030     if (MustSaveLR)
01031       BuildMI(MBB, MBBI, TII.get(PPC::MFLR8), PPC::X0);
01032       
01033     if (HasFP)
01034       BuildMI(MBB, MBBI, TII.get(PPC::STD))
01035         .addReg(PPC::X31)
01036         .addImm(FPOffset/4)
01037         .addReg(PPC::X1);
01038     
01039     if (MustSaveLR)
01040       BuildMI(MBB, MBBI, TII.get(PPC::STD))
01041         .addReg(PPC::X0)
01042         .addImm(LROffset / 4)
01043         .addReg(PPC::X1);
01044   } else {
01045     if (MustSaveLR)
01046       BuildMI(MBB, MBBI, TII.get(PPC::MFLR), PPC::R0);
01047       
01048     if (HasFP)
01049       BuildMI(MBB, MBBI, TII.get(PPC::STW))
01050         .addReg(PPC::R31)
01051         .addImm(FPOffset)
01052         .addReg(PPC::R1);
01053 
01054     if (MustSaveLR)
01055       BuildMI(MBB, MBBI, TII.get(PPC::STW))
01056         .addReg(PPC::R0)
01057         .addImm(LROffset)
01058         .addReg(PPC::R1);
01059   }
01060   
01061   // Skip if a leaf routine.
01062   if (!FrameSize) return;
01063   
01064   // Get stack alignments.
01065   unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
01066   unsigned MaxAlign = MFI->getMaxAlignment();
01067 
01068   if (needsFrameMoves) {
01069     // Mark effective beginning of when frame pointer becomes valid.
01070     FrameLabelId = MMI->NextLabelID();
01071     BuildMI(MBB, MBBI, TII.get(PPC::DBG_LABEL)).addImm(FrameLabelId);
01072   }
01073   
01074   // Adjust stack pointer: r1 += NegFrameSize.
01075   // If there is a preferred stack alignment, align R1 now
01076   if (!IsPPC64) {
01077     // PPC32.
01078     if (ALIGN_STACK && MaxAlign > TargetAlign) {
01079       assert(isPowerOf2_32(MaxAlign)&&isInt16(MaxAlign)&&"Invalid alignment!");
01080       assert(isInt16(NegFrameSize) && "Unhandled stack size and alignment!");
01081 
01082       BuildMI(MBB, MBBI, TII.get(PPC::RLWINM), PPC::R0)
01083         .addReg(PPC::R1)
01084         .addImm(0)
01085         .addImm(32 - Log2_32(MaxAlign))
01086         .addImm(31);
01087       BuildMI(MBB, MBBI, TII.get(PPC::SUBFIC) ,PPC::R0)
01088         .addReg(PPC::R0, false, false, true)
01089         .addImm(NegFrameSize);
01090       BuildMI(MBB, MBBI, TII.get(PPC::STWUX))
01091         .addReg(PPC::R1)
01092         .addReg(PPC::R1)
01093         .addReg(PPC::R0);
01094     } else if (isInt16(NegFrameSize)) {
01095       BuildMI(MBB, MBBI, TII.get(PPC::STWU), PPC::R1)
01096         .addReg(PPC::R1)
01097         .addImm(NegFrameSize)
01098         .addReg(PPC::R1);
01099     } else {
01100       BuildMI(MBB, MBBI, TII.get(PPC::LIS), PPC::R0)
01101         .addImm(NegFrameSize >> 16);
01102       BuildMI(MBB, MBBI, TII.get(PPC::ORI), PPC::R0)
01103         .addReg(PPC::R0, false, false, true)
01104         .addImm(NegFrameSize & 0xFFFF);
01105       BuildMI(MBB, MBBI, TII.get(PPC::STWUX))
01106         .addReg(PPC::R1)
01107         .addReg(PPC::R1)
01108         .addReg(PPC::R0);
01109     }
01110   } else {    // PPC64.
01111     if (ALIGN_STACK && MaxAlign > TargetAlign) {
01112       assert(isPowerOf2_32(MaxAlign)&&isInt16(MaxAlign)&&"Invalid alignment!");
01113       assert(isInt16(NegFrameSize) && "Unhandled stack size and alignment!");
01114 
01115       BuildMI(MBB, MBBI, TII.get(PPC::RLDICL), PPC::X0)
01116         .addReg(PPC::X1)
01117         .addImm(0)
01118         .addImm(64 - Log2_32(MaxAlign));
01119       BuildMI(MBB, MBBI, TII.get(PPC::SUBFIC8), PPC::X0)
01120         .addReg(PPC::X0)
01121         .addImm(NegFrameSize);
01122       BuildMI(MBB, MBBI, TII.get(PPC::STDUX))
01123         .addReg(PPC::X1)
01124         .addReg(PPC::X1)
01125         .addReg(PPC::X0);
01126     } else if (isInt16(NegFrameSize)) {
01127       BuildMI(MBB, MBBI, TII.get(PPC::STDU), PPC::X1)
01128         .addReg(PPC::X1)
01129         .addImm(NegFrameSize / 4)
01130         .addReg(PPC::X1);
01131     } else {
01132       BuildMI(MBB, MBBI, TII.get(PPC::LIS8), PPC::X0)
01133         .addImm(NegFrameSize >> 16);
01134       BuildMI(MBB, MBBI, TII.get(PPC::ORI8), PPC::X0)
01135         .addReg(PPC::X0, false, false, true)
01136         .addImm(NegFrameSize & 0xFFFF);
01137       BuildMI(MBB, MBBI, TII.get(PPC::STDUX))
01138         .addReg(PPC::X1)
01139         .addReg(PPC::X1)
01140         .addReg(PPC::X0);
01141     }
01142   }
01143   
01144   if (needsFrameMoves) {
01145     std::vector<MachineMove> &Moves = MMI->getFrameMoves();
01146     
01147     if (NegFrameSize) {
01148       // Show update of SP.
01149       MachineLocation SPDst(MachineLocation::VirtualFP);
01150       MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
01151       Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
01152     } else {
01153       MachineLocation SP(IsPPC64 ? PPC::X31 : PPC::R31);
01154       Moves.push_back(MachineMove(FrameLabelId, SP, SP));
01155     }
01156     
01157     if (HasFP) {
01158       MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
01159       MachineLocation FPSrc(IsPPC64 ? PPC::X31 : PPC::R31);
01160       Moves.push_back(MachineMove(FrameLabelId, FPDst, FPSrc));
01161     }
01162 
01163     // Add callee saved registers to move list.
01164     const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
01165     for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
01166       int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
01167       unsigned Reg = CSI[I].getReg();
01168       if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
01169       MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
01170       MachineLocation CSSrc(Reg);
01171       Moves.push_back(MachineMove(FrameLabelId, CSDst, CSSrc));
01172     }
01173     
01174     MachineLocation LRDst(MachineLocation::VirtualFP, LROffset);
01175     MachineLocation LRSrc(IsPPC64 ? PPC::LR8 : PPC::LR);
01176     Moves.push_back(MachineMove(FrameLabelId, LRDst, LRSrc));
01177     
01178     // Mark effective beginning of when frame pointer is ready.
01179     unsigned ReadyLabelId = MMI->NextLabelID();
01180     BuildMI(MBB, MBBI, TII.get(PPC::DBG_LABEL)).addImm(ReadyLabelId);
01181     
01182     MachineLocation FPDst(HasFP ? (IsPPC64 ? PPC::X31 : PPC::R31) :
01183                                   (IsPPC64 ? PPC::X1 : PPC::R1));
01184     MachineLocation FPSrc(MachineLocation::VirtualFP);
01185     Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
01186   }
01187 
01188   // If there is a frame pointer, copy R1 into R31
01189   if (HasFP) {
01190     if (!IsPPC64) {
01191       BuildMI(MBB, MBBI, TII.get(PPC::OR), PPC::R31)
01192         .addReg(PPC::R1)
01193         .addReg(PPC::R1);
01194     } else {
01195       BuildMI(MBB, MBBI, TII.get(PPC::OR8), PPC::X31)
01196         .addReg(PPC::X1)
01197         .addReg(PPC::X1);
01198     }
01199   }
01200 }
01201 
01202 void PPCRegisterInfo::emitEpilogue(MachineFunction &MF,
01203                                    MachineBasicBlock &MBB) const {
01204   MachineBasicBlock::iterator MBBI = prior(MBB.end());
01205   unsigned RetOpcode = MBBI->getOpcode();
01206 
01207   assert( (RetOpcode == PPC::BLR ||
01208            RetOpcode == PPC::TCRETURNri ||
01209            RetOpcode == PPC::TCRETURNdi ||
01210            RetOpcode == PPC::TCRETURNai ||
01211            RetOpcode == PPC::TCRETURNri8 ||
01212            RetOpcode == PPC::TCRETURNdi8 ||
01213            RetOpcode == PPC::TCRETURNai8) &&
01214          "Can only insert epilog into returning blocks");
01215 
01216   // Get alignment info so we know how to restore r1
01217   const MachineFrameInfo *MFI = MF.getFrameInfo();
01218   unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
01219   unsigned MaxAlign = MFI->getMaxAlignment();
01220 
01221   // Get the number of bytes allocated from the FrameInfo.
01222   int FrameSize = MFI->getStackSize();
01223 
01224   // Get processor type.
01225   bool IsPPC64 = Subtarget.isPPC64();
01226   // Get operating system
01227   bool IsMachoABI = Subtarget.isMachoABI();
01228   // Check if the link register (LR) has been saved.
01229   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
01230   bool MustSaveLR = FI->mustSaveLR();
01231   // Do we have a frame pointer for this function?
01232   bool HasFP = hasFP(MF) && FrameSize;
01233   
01234   int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, IsMachoABI);
01235   int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64, IsMachoABI);
01236   
01237   bool UsesTCRet =  RetOpcode == PPC::TCRETURNri ||
01238     RetOpcode == PPC::TCRETURNdi ||
01239     RetOpcode == PPC::TCRETURNai ||
01240     RetOpcode == PPC::TCRETURNri8 ||
01241     RetOpcode == PPC::TCRETURNdi8 ||
01242     RetOpcode == PPC::TCRETURNai8;
01243 
01244   if (UsesTCRet) {
01245     int MaxTCRetDelta = FI->getTailCallSPDelta();
01246     MachineOperand &StackAdjust = MBBI->getOperand(1);
01247     assert(StackAdjust.isImm() && "Expecting immediate value.");
01248     // Adjust stack pointer.
01249     int StackAdj = StackAdjust.getImm();
01250     int Delta = StackAdj - MaxTCRetDelta;
01251     assert((Delta >= 0) && "Delta must be positive");
01252     if (MaxTCRetDelta>0)
01253       FrameSize += (StackAdj +Delta);
01254     else
01255       FrameSize += StackAdj;
01256   }
01257 
01258   if (FrameSize) {
01259     // The loaded (or persistent) stack pointer value is offset by the 'stwu'
01260     // on entry to the function.  Add this offset back now.
01261     if (!IsPPC64) {
01262       // If this function contained a fastcc call and PerformTailCallOpt is
01263       // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
01264       // call which invalidates the stack pointer value in SP(0). So we use the
01265       // value of R31 in this case.
01266       if (FI->hasFastCall() && isInt16(FrameSize)) {
01267         assert(hasFP(MF) && "Expecting a valid the frame pointer.");
01268         BuildMI(MBB, MBBI, TII.get(PPC::ADDI), PPC::R1)
01269           .addReg(PPC::R31).addImm(FrameSize);
01270       } else if(FI->hasFastCall()) {
01271         BuildMI(MBB, MBBI, TII.get(PPC::LIS), PPC::R0)
01272           .addImm(FrameSize >> 16);
01273         BuildMI(MBB, MBBI, TII.get(PPC::ORI), PPC::R0)
01274           .addReg(PPC::R0, false, false, true)
01275           .addImm(FrameSize & 0xFFFF);
01276         BuildMI(MBB, MBBI, TII.get(PPC::ADD4))
01277           .addReg(PPC::R1)
01278           .addReg(PPC::R31)
01279           .addReg(PPC::R0);
01280       } else if (isInt16(FrameSize) &&
01281                  (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
01282                  !MFI->hasVarSizedObjects()) {
01283         BuildMI(MBB, MBBI, TII.get(PPC::ADDI), PPC::R1)
01284           .addReg(PPC::R1).addImm(FrameSize);
01285       } else {
01286         BuildMI(MBB, MBBI, TII.get(PPC::LWZ),PPC::R1).addImm(0).addReg(PPC::R1);
01287       }
01288     } else {
01289       if (FI->hasFastCall() && isInt16(FrameSize)) {
01290         assert(hasFP(MF) && "Expecting a valid the frame pointer.");
01291         BuildMI(MBB, MBBI, TII.get(PPC::ADDI8), PPC::X1)
01292           .addReg(PPC::X31).addImm(FrameSize);
01293       } else if(FI->hasFastCall()) {
01294         BuildMI(MBB, MBBI, TII.get(PPC::LIS8), PPC::X0)
01295           .addImm(FrameSize >> 16);
01296         BuildMI(MBB, MBBI, TII.get(PPC::ORI8), PPC::X0)
01297           .addReg(PPC::X0, false, false, true)
01298           .addImm(FrameSize & 0xFFFF);
01299         BuildMI(MBB, MBBI, TII.get(PPC::ADD8))
01300           .addReg(PPC::X1)
01301           .addReg(PPC::X31)
01302           .addReg(PPC::X0);
01303       } else if (isInt16(FrameSize) && TargetAlign >= MaxAlign &&
01304             !MFI->hasVarSizedObjects()) {
01305         BuildMI(MBB, MBBI, TII.get(PPC::ADDI8), PPC::X1)
01306            .addReg(PPC::X1).addImm(FrameSize);
01307       } else {
01308         BuildMI(MBB, MBBI, TII.get(PPC::LD), PPC::X1).addImm(0).addReg(PPC::X1);
01309       }
01310     }
01311   }
01312 
01313   if (IsPPC64) {
01314     if (MustSaveLR)
01315       BuildMI(MBB, MBBI, TII.get(PPC::LD), PPC::X0)
01316         .addImm(LROffset/4).addReg(PPC::X1);
01317         
01318     if (HasFP)
01319       BuildMI(MBB, MBBI, TII.get(PPC::LD), PPC::X31)
01320         .addImm(FPOffset/4).addReg(PPC::X1);
01321         
01322     if (MustSaveLR)
01323       BuildMI(MBB, MBBI, TII.get(PPC::MTLR8)).addReg(PPC::X0);
01324   } else {
01325     if (MustSaveLR)
01326       BuildMI(MBB, MBBI, TII.get(PPC::LWZ), PPC::R0)
01327           .addImm(LROffset).addReg(PPC::R1);
01328         
01329     if (HasFP)
01330       BuildMI(MBB, MBBI, TII.get(PPC::LWZ), PPC::R31)
01331           .addImm(FPOffset).addReg(PPC::R1);
01332           
01333     if (MustSaveLR)
01334       BuildMI(MBB, MBBI, TII.get(PPC::MTLR)).addReg(PPC::R0);
01335   }
01336 
01337   // Callee pop calling convention. Pop parameter/linkage area. Used for tail
01338   // call optimization
01339   if (PerformTailCallOpt && RetOpcode == PPC::BLR &&
01340       MF.getFunction()->getCallingConv() == CallingConv::Fast) {
01341      PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
01342      unsigned CallerAllocatedAmt = FI->getMinReservedArea();
01343      unsigned StackReg = IsPPC64 ? PPC::X1 : PPC::R1;
01344      unsigned FPReg = IsPPC64 ? PPC::X31 : PPC::R31;
01345      unsigned TmpReg = IsPPC64 ? PPC::X0 : PPC::R0;
01346      unsigned ADDIInstr = IsPPC64 ? PPC::ADDI8 : PPC::ADDI;
01347      unsigned ADDInstr = IsPPC64 ? PPC::ADD8 : PPC::ADD4;
01348      unsigned LISInstr = IsPPC64 ? PPC::LIS8 : PPC::LIS;
01349      unsigned ORIInstr = IsPPC64 ? PPC::ORI8 : PPC::ORI;
01350 
01351      if (CallerAllocatedAmt && isInt16(CallerAllocatedAmt)) {
01352        BuildMI(MBB, MBBI, TII.get(ADDIInstr), StackReg)
01353          .addReg(StackReg).addImm(CallerAllocatedAmt);
01354      } else {
01355         BuildMI(MBB, MBBI, TII.get(LISInstr), TmpReg)
01356           .addImm(CallerAllocatedAmt >> 16);
01357         BuildMI(MBB, MBBI, TII.get(ORIInstr), TmpReg)
01358           .addReg(TmpReg, false, false, true)
01359           .addImm(CallerAllocatedAmt & 0xFFFF);
01360         BuildMI(MBB, MBBI, TII.get(ADDInstr))
01361           .addReg(StackReg)
01362           .addReg(FPReg)
01363           .addReg(TmpReg);
01364      }
01365   } else if (RetOpcode == PPC::TCRETURNdi) {
01366     MBBI = prior(MBB.end());
01367     MachineOperand &JumpTarget = MBBI->getOperand(0);
01368     BuildMI(MBB, MBBI, TII.get(PPC::TAILB)).
01369       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
01370   } else if (RetOpcode == PPC::TCRETURNri) {
01371     MBBI = prior(MBB.end());
01372     MachineOperand &JumpTarget = MBBI->getOperand(0);
01373     assert(JumpTarget.isReg() && "Expecting register operand.");
01374     BuildMI(MBB, MBBI, TII.get(PPC::TAILBCTR));
01375   } else if (RetOpcode == PPC::TCRETURNai) {
01376     MBBI = prior(MBB.end());
01377     MachineOperand &JumpTarget = MBBI->getOperand(0);
01378     BuildMI(MBB, MBBI, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
01379   } else if (RetOpcode == PPC::TCRETURNdi8) {
01380     MBBI = prior(MBB.end());
01381     MachineOperand &JumpTarget = MBBI->getOperand(0);
01382     BuildMI(MBB, MBBI, TII.get(PPC::TAILB8)).
01383       addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
01384   } else if (RetOpcode == PPC::TCRETURNri8) {
01385     MBBI = prior(MBB.end());
01386     MachineOperand &JumpTarget = MBBI->getOperand(0);
01387     assert(JumpTarget.isReg() && "Expecting register operand.");
01388     BuildMI(MBB, MBBI, TII.get(PPC::TAILBCTR8));
01389   } else if (RetOpcode == PPC::TCRETURNai8) {
01390     MBBI = prior(MBB.end());
01391     MachineOperand &JumpTarget = MBBI->getOperand(0);
01392     BuildMI(MBB, MBBI, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
01393   }
01394 }
01395 
01396 unsigned PPCRegisterInfo::getRARegister() const {
01397   return !Subtarget.isPPC64() ? PPC::LR : PPC::LR8;
01398 }
01399 
01400 unsigned PPCRegisterInfo::getFrameRegister(MachineFunction &MF) const {
01401   if (!Subtarget.isPPC64())
01402     return hasFP(MF) ? PPC::R31 : PPC::R1;
01403   else
01404     return hasFP(MF) ? PPC::X31 : PPC::X1;
01405 }
01406 
01407 void PPCRegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves)
01408                                                                          const {
01409   // Initial state of the frame pointer is R1.
01410   MachineLocation Dst(MachineLocation::VirtualFP);
01411   MachineLocation Src(PPC::R1, 0);
01412   Moves.push_back(MachineMove(0, Dst, Src));
01413 }
01414 
01415 unsigned PPCRegisterInfo::getEHExceptionRegister() const {
01416   return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3;
01417 }
01418 
01419 unsigned PPCRegisterInfo::getEHHandlerRegister() const {
01420   return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4;
01421 }
01422 
01423 int PPCRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
01424   // FIXME: Most probably dwarf numbers differs for Linux and Darwin
01425   return PPCGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
01426 }
01427 
01428 #include "PPCGenRegisterInfo.inc"
01429