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