LLVM API Documentation

SPUISelLowering.cpp

Go to the documentation of this file.
00001 //
00002 //===-- SPUISelLowering.cpp - Cell SPU DAG Lowering Implementation --------===//
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 implements the SPUTargetLowering class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "SPURegisterNames.h"
00015 #include "SPUISelLowering.h"
00016 #include "SPUTargetMachine.h"
00017 #include "SPUFrameInfo.h"
00018 #include "llvm/Constants.h"
00019 #include "llvm/Function.h"
00020 #include "llvm/Intrinsics.h"
00021 #include "llvm/CallingConv.h"
00022 #include "llvm/CodeGen/CallingConvLower.h"
00023 #include "llvm/CodeGen/MachineFrameInfo.h"
00024 #include "llvm/CodeGen/MachineFunction.h"
00025 #include "llvm/CodeGen/MachineInstrBuilder.h"
00026 #include "llvm/CodeGen/MachineRegisterInfo.h"
00027 #include "llvm/CodeGen/SelectionDAG.h"
00028 #include "llvm/Target/TargetLoweringObjectFile.h"
00029 #include "llvm/Target/TargetOptions.h"
00030 #include "llvm/ADT/VectorExtras.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/MathExtras.h"
00034 #include "llvm/Support/raw_ostream.h"
00035 #include <map>
00036 
00037 using namespace llvm;
00038 
00039 // Used in getTargetNodeName() below
00040 namespace {
00041   std::map<unsigned, const char *> node_names;
00042 
00043   //! EVT mapping to useful data for Cell SPU
00044   struct valtype_map_s {
00045     const EVT   valtype;
00046     const int   prefslot_byte;
00047   };
00048 
00049   const valtype_map_s valtype_map[] = {
00050     { MVT::i1,   3 },
00051     { MVT::i8,   3 },
00052     { MVT::i16,  2 },
00053     { MVT::i32,  0 },
00054     { MVT::f32,  0 },
00055     { MVT::i64,  0 },
00056     { MVT::f64,  0 },
00057     { MVT::i128, 0 }
00058   };
00059 
00060   const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
00061 
00062   const valtype_map_s *getValueTypeMapEntry(EVT VT) {
00063     const valtype_map_s *retval = 0;
00064 
00065     for (size_t i = 0; i < n_valtype_map; ++i) {
00066       if (valtype_map[i].valtype == VT) {
00067         retval = valtype_map + i;
00068         break;
00069       }
00070     }
00071 
00072 #ifndef NDEBUG
00073     if (retval == 0) {
00074       std::string msg;
00075       raw_string_ostream Msg(msg);
00076       Msg << "getValueTypeMapEntry returns NULL for "
00077            << VT.getEVTString();
00078       llvm_report_error(Msg.str());
00079     }
00080 #endif
00081 
00082     return retval;
00083   }
00084 
00085   //! Expand a library call into an actual call DAG node
00086   /*!
00087    \note
00088    This code is taken from SelectionDAGLegalize, since it is not exposed as
00089    part of the LLVM SelectionDAG API.
00090    */
00091 
00092   SDValue
00093   ExpandLibCall(RTLIB::Libcall LC, SDValue Op, SelectionDAG &DAG,
00094                 bool isSigned, SDValue &Hi, SPUTargetLowering &TLI) {
00095     // The input chain to this libcall is the entry node of the function.
00096     // Legalizing the call will automatically add the previous call to the
00097     // dependence.
00098     SDValue InChain = DAG.getEntryNode();
00099 
00100     TargetLowering::ArgListTy Args;
00101     TargetLowering::ArgListEntry Entry;
00102     for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
00103       EVT ArgVT = Op.getOperand(i).getValueType();
00104       const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
00105       Entry.Node = Op.getOperand(i);
00106       Entry.Ty = ArgTy;
00107       Entry.isSExt = isSigned;
00108       Entry.isZExt = !isSigned;
00109       Args.push_back(Entry);
00110     }
00111     SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
00112                                            TLI.getPointerTy());
00113 
00114     // Splice the libcall in wherever FindInputOutputChains tells us to.
00115     const Type *RetTy =
00116                 Op.getNode()->getValueType(0).getTypeForEVT(*DAG.getContext());
00117     std::pair<SDValue, SDValue> CallInfo =
00118             TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
00119                             0, TLI.getLibcallCallingConv(LC), false,
00120                             /*isReturnValueUsed=*/true,
00121                             Callee, Args, DAG,
00122                             Op.getDebugLoc());
00123 
00124     return CallInfo.first;
00125   }
00126 }
00127 
00128 SPUTargetLowering::SPUTargetLowering(SPUTargetMachine &TM)
00129   : TargetLowering(TM, new TargetLoweringObjectFileELF()),
00130     SPUTM(TM) {
00131   // Fold away setcc operations if possible.
00132   setPow2DivIsCheap();
00133 
00134   // Use _setjmp/_longjmp instead of setjmp/longjmp.
00135   setUseUnderscoreSetJmp(true);
00136   setUseUnderscoreLongJmp(true);
00137 
00138   // Set RTLIB libcall names as used by SPU:
00139   setLibcallName(RTLIB::DIV_F64, "__fast_divdf3");
00140 
00141   // Set up the SPU's register classes:
00142   addRegisterClass(MVT::i8,   SPU::R8CRegisterClass);
00143   addRegisterClass(MVT::i16,  SPU::R16CRegisterClass);
00144   addRegisterClass(MVT::i32,  SPU::R32CRegisterClass);
00145   addRegisterClass(MVT::i64,  SPU::R64CRegisterClass);
00146   addRegisterClass(MVT::f32,  SPU::R32FPRegisterClass);
00147   addRegisterClass(MVT::f64,  SPU::R64FPRegisterClass);
00148   addRegisterClass(MVT::i128, SPU::GPRCRegisterClass);
00149 
00150   // SPU has no sign or zero extended loads for i1, i8, i16:
00151   setLoadExtAction(ISD::EXTLOAD,  MVT::i1, Promote);
00152   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
00153   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
00154 
00155   setLoadExtAction(ISD::EXTLOAD,  MVT::f32, Expand);
00156   setLoadExtAction(ISD::EXTLOAD,  MVT::f64, Expand);
00157 
00158   setTruncStoreAction(MVT::i128, MVT::i64, Expand);
00159   setTruncStoreAction(MVT::i128, MVT::i32, Expand);
00160   setTruncStoreAction(MVT::i128, MVT::i16, Expand);
00161   setTruncStoreAction(MVT::i128, MVT::i8, Expand);
00162 
00163   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
00164 
00165   // SPU constant load actions are custom lowered:
00166   setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
00167   setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
00168 
00169   // SPU's loads and stores have to be custom lowered:
00170   for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::i128;
00171        ++sctype) {
00172     MVT::SimpleValueType VT = (MVT::SimpleValueType)sctype;
00173 
00174     setOperationAction(ISD::LOAD,   VT, Custom);
00175     setOperationAction(ISD::STORE,  VT, Custom);
00176     setLoadExtAction(ISD::EXTLOAD,  VT, Custom);
00177     setLoadExtAction(ISD::ZEXTLOAD, VT, Custom);
00178     setLoadExtAction(ISD::SEXTLOAD, VT, Custom);
00179 
00180     for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::i8; --stype) {
00181       MVT::SimpleValueType StoreVT = (MVT::SimpleValueType) stype;
00182       setTruncStoreAction(VT, StoreVT, Expand);
00183     }
00184   }
00185 
00186   for (unsigned sctype = (unsigned) MVT::f32; sctype < (unsigned) MVT::f64;
00187        ++sctype) {
00188     MVT::SimpleValueType VT = (MVT::SimpleValueType) sctype;
00189 
00190     setOperationAction(ISD::LOAD,   VT, Custom);
00191     setOperationAction(ISD::STORE,  VT, Custom);
00192 
00193     for (unsigned stype = sctype - 1; stype >= (unsigned) MVT::f32; --stype) {
00194       MVT::SimpleValueType StoreVT = (MVT::SimpleValueType) stype;
00195       setTruncStoreAction(VT, StoreVT, Expand);
00196     }
00197   }
00198 
00199   // Expand the jumptable branches
00200   setOperationAction(ISD::BR_JT,        MVT::Other, Expand);
00201   setOperationAction(ISD::BR_CC,        MVT::Other, Expand);
00202 
00203   // Custom lower SELECT_CC for most cases, but expand by default
00204   setOperationAction(ISD::SELECT_CC,    MVT::Other, Expand);
00205   setOperationAction(ISD::SELECT_CC,    MVT::i8,    Custom);
00206   setOperationAction(ISD::SELECT_CC,    MVT::i16,   Custom);
00207   setOperationAction(ISD::SELECT_CC,    MVT::i32,   Custom);
00208   setOperationAction(ISD::SELECT_CC,    MVT::i64,   Custom);
00209 
00210   // SPU has no intrinsics for these particular operations:
00211   setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
00212 
00213   // SPU has no division/remainder instructions
00214   setOperationAction(ISD::SREM,    MVT::i8,   Expand);
00215   setOperationAction(ISD::UREM,    MVT::i8,   Expand);
00216   setOperationAction(ISD::SDIV,    MVT::i8,   Expand);
00217   setOperationAction(ISD::UDIV,    MVT::i8,   Expand);
00218   setOperationAction(ISD::SDIVREM, MVT::i8,   Expand);
00219   setOperationAction(ISD::UDIVREM, MVT::i8,   Expand);
00220   setOperationAction(ISD::SREM,    MVT::i16,  Expand);
00221   setOperationAction(ISD::UREM,    MVT::i16,  Expand);
00222   setOperationAction(ISD::SDIV,    MVT::i16,  Expand);
00223   setOperationAction(ISD::UDIV,    MVT::i16,  Expand);
00224   setOperationAction(ISD::SDIVREM, MVT::i16,  Expand);
00225   setOperationAction(ISD::UDIVREM, MVT::i16,  Expand);
00226   setOperationAction(ISD::SREM,    MVT::i32,  Expand);
00227   setOperationAction(ISD::UREM,    MVT::i32,  Expand);
00228   setOperationAction(ISD::SDIV,    MVT::i32,  Expand);
00229   setOperationAction(ISD::UDIV,    MVT::i32,  Expand);
00230   setOperationAction(ISD::SDIVREM, MVT::i32,  Expand);
00231   setOperationAction(ISD::UDIVREM, MVT::i32,  Expand);
00232   setOperationAction(ISD::SREM,    MVT::i64,  Expand);
00233   setOperationAction(ISD::UREM,    MVT::i64,  Expand);
00234   setOperationAction(ISD::SDIV,    MVT::i64,  Expand);
00235   setOperationAction(ISD::UDIV,    MVT::i64,  Expand);
00236   setOperationAction(ISD::SDIVREM, MVT::i64,  Expand);
00237   setOperationAction(ISD::UDIVREM, MVT::i64,  Expand);
00238   setOperationAction(ISD::SREM,    MVT::i128, Expand);
00239   setOperationAction(ISD::UREM,    MVT::i128, Expand);
00240   setOperationAction(ISD::SDIV,    MVT::i128, Expand);
00241   setOperationAction(ISD::UDIV,    MVT::i128, Expand);
00242   setOperationAction(ISD::SDIVREM, MVT::i128, Expand);
00243   setOperationAction(ISD::UDIVREM, MVT::i128, Expand);
00244 
00245   // We don't support sin/cos/sqrt/fmod
00246   setOperationAction(ISD::FSIN , MVT::f64, Expand);
00247   setOperationAction(ISD::FCOS , MVT::f64, Expand);
00248   setOperationAction(ISD::FREM , MVT::f64, Expand);
00249   setOperationAction(ISD::FSIN , MVT::f32, Expand);
00250   setOperationAction(ISD::FCOS , MVT::f32, Expand);
00251   setOperationAction(ISD::FREM , MVT::f32, Expand);
00252 
00253   // Expand fsqrt to the appropriate libcall (NOTE: should use h/w fsqrt
00254   // for f32!)
00255   setOperationAction(ISD::FSQRT, MVT::f64, Expand);
00256   setOperationAction(ISD::FSQRT, MVT::f32, Expand);
00257 
00258   setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
00259   setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
00260 
00261   // SPU can do rotate right and left, so legalize it... but customize for i8
00262   // because instructions don't exist.
00263 
00264   // FIXME: Change from "expand" to appropriate type once ROTR is supported in
00265   //        .td files.
00266   setOperationAction(ISD::ROTR, MVT::i32,    Expand /*Legal*/);
00267   setOperationAction(ISD::ROTR, MVT::i16,    Expand /*Legal*/);
00268   setOperationAction(ISD::ROTR, MVT::i8,     Expand /*Custom*/);
00269 
00270   setOperationAction(ISD::ROTL, MVT::i32,    Legal);
00271   setOperationAction(ISD::ROTL, MVT::i16,    Legal);
00272   setOperationAction(ISD::ROTL, MVT::i8,     Custom);
00273 
00274   // SPU has no native version of shift left/right for i8
00275   setOperationAction(ISD::SHL,  MVT::i8,     Custom);
00276   setOperationAction(ISD::SRL,  MVT::i8,     Custom);
00277   setOperationAction(ISD::SRA,  MVT::i8,     Custom);
00278 
00279   // Make these operations legal and handle them during instruction selection:
00280   setOperationAction(ISD::SHL,  MVT::i64,    Legal);
00281   setOperationAction(ISD::SRL,  MVT::i64,    Legal);
00282   setOperationAction(ISD::SRA,  MVT::i64,    Legal);
00283 
00284   // Custom lower i8, i32 and i64 multiplications
00285   setOperationAction(ISD::MUL,  MVT::i8,     Custom);
00286   setOperationAction(ISD::MUL,  MVT::i32,    Legal);
00287   setOperationAction(ISD::MUL,  MVT::i64,    Legal);
00288 
00289   // Expand double-width multiplication
00290   // FIXME: It would probably be reasonable to support some of these operations
00291   setOperationAction(ISD::UMUL_LOHI, MVT::i8,  Expand);
00292   setOperationAction(ISD::SMUL_LOHI, MVT::i8,  Expand);
00293   setOperationAction(ISD::MULHU,     MVT::i8,  Expand);
00294   setOperationAction(ISD::MULHS,     MVT::i8,  Expand);
00295   setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
00296   setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
00297   setOperationAction(ISD::MULHU,     MVT::i16, Expand);
00298   setOperationAction(ISD::MULHS,     MVT::i16, Expand);
00299   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
00300   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
00301   setOperationAction(ISD::MULHU,     MVT::i32, Expand);
00302   setOperationAction(ISD::MULHS,     MVT::i32, Expand);
00303   setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
00304   setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
00305   setOperationAction(ISD::MULHU,     MVT::i64, Expand);
00306   setOperationAction(ISD::MULHS,     MVT::i64, Expand);
00307 
00308   // Need to custom handle (some) common i8, i64 math ops
00309   setOperationAction(ISD::ADD,  MVT::i8,     Custom);
00310   setOperationAction(ISD::ADD,  MVT::i64,    Legal);
00311   setOperationAction(ISD::SUB,  MVT::i8,     Custom);
00312   setOperationAction(ISD::SUB,  MVT::i64,    Legal);
00313 
00314   // SPU does not have BSWAP. It does have i32 support CTLZ.
00315   // CTPOP has to be custom lowered.
00316   setOperationAction(ISD::BSWAP, MVT::i32,   Expand);
00317   setOperationAction(ISD::BSWAP, MVT::i64,   Expand);
00318 
00319   setOperationAction(ISD::CTPOP, MVT::i8,    Custom);
00320   setOperationAction(ISD::CTPOP, MVT::i16,   Custom);
00321   setOperationAction(ISD::CTPOP, MVT::i32,   Custom);
00322   setOperationAction(ISD::CTPOP, MVT::i64,   Custom);
00323   setOperationAction(ISD::CTPOP, MVT::i128,  Expand);
00324 
00325   setOperationAction(ISD::CTTZ , MVT::i8,    Expand);
00326   setOperationAction(ISD::CTTZ , MVT::i16,   Expand);
00327   setOperationAction(ISD::CTTZ , MVT::i32,   Expand);
00328   setOperationAction(ISD::CTTZ , MVT::i64,   Expand);
00329   setOperationAction(ISD::CTTZ , MVT::i128,  Expand);
00330 
00331   setOperationAction(ISD::CTLZ , MVT::i8,    Promote);
00332   setOperationAction(ISD::CTLZ , MVT::i16,   Promote);
00333   setOperationAction(ISD::CTLZ , MVT::i32,   Legal);
00334   setOperationAction(ISD::CTLZ , MVT::i64,   Expand);
00335   setOperationAction(ISD::CTLZ , MVT::i128,  Expand);
00336 
00337   // SPU has a version of select that implements (a&~c)|(b&c), just like
00338   // select ought to work:
00339   setOperationAction(ISD::SELECT, MVT::i8,   Legal);
00340   setOperationAction(ISD::SELECT, MVT::i16,  Legal);
00341   setOperationAction(ISD::SELECT, MVT::i32,  Legal);
00342   setOperationAction(ISD::SELECT, MVT::i64,  Legal);
00343 
00344   setOperationAction(ISD::SETCC, MVT::i8,    Legal);
00345   setOperationAction(ISD::SETCC, MVT::i16,   Legal);
00346   setOperationAction(ISD::SETCC, MVT::i32,   Legal);
00347   setOperationAction(ISD::SETCC, MVT::i64,   Legal);
00348   setOperationAction(ISD::SETCC, MVT::f64,   Custom);
00349 
00350   // Custom lower i128 -> i64 truncates
00351   setOperationAction(ISD::TRUNCATE, MVT::i64, Custom);
00352 
00353   setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
00354   setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
00355   setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
00356   setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
00357   // SPU has a legal FP -> signed INT instruction for f32, but for f64, need
00358   // to expand to a libcall, hence the custom lowering:
00359   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
00360   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
00361   setOperationAction(ISD::FP_TO_SINT, MVT::i64, Expand);
00362   setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
00363   setOperationAction(ISD::FP_TO_SINT, MVT::i128, Expand);
00364   setOperationAction(ISD::FP_TO_UINT, MVT::i128, Expand);
00365 
00366   // FDIV on SPU requires custom lowering
00367   setOperationAction(ISD::FDIV, MVT::f64, Expand);      // to libcall
00368 
00369   // SPU has [U|S]INT_TO_FP for f32->i32, but not for f64->i32, f64->i64:
00370   setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
00371   setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
00372   setOperationAction(ISD::SINT_TO_FP, MVT::i8,  Promote);
00373   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
00374   setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
00375   setOperationAction(ISD::UINT_TO_FP, MVT::i8,  Promote);
00376   setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
00377   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
00378 
00379   setOperationAction(ISD::BIT_CONVERT, MVT::i32, Legal);
00380   setOperationAction(ISD::BIT_CONVERT, MVT::f32, Legal);
00381   setOperationAction(ISD::BIT_CONVERT, MVT::i64, Legal);
00382   setOperationAction(ISD::BIT_CONVERT, MVT::f64, Legal);
00383 
00384   // We cannot sextinreg(i1).  Expand to shifts.
00385   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
00386 
00387   // Support label based line numbers.
00388   setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
00389   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
00390 
00391   // We want to legalize GlobalAddress and ConstantPool nodes into the
00392   // appropriate instructions to materialize the address.
00393   for (unsigned sctype = (unsigned) MVT::i8; sctype < (unsigned) MVT::f128;
00394        ++sctype) {
00395     MVT::SimpleValueType VT = (MVT::SimpleValueType)sctype;
00396 
00397     setOperationAction(ISD::GlobalAddress,  VT, Custom);
00398     setOperationAction(ISD::ConstantPool,   VT, Custom);
00399     setOperationAction(ISD::JumpTable,      VT, Custom);
00400   }
00401 
00402   // VASTART needs to be custom lowered to use the VarArgsFrameIndex
00403   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
00404 
00405   // Use the default implementation.
00406   setOperationAction(ISD::VAARG             , MVT::Other, Expand);
00407   setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
00408   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
00409   setOperationAction(ISD::STACKSAVE         , MVT::Other, Expand);
00410   setOperationAction(ISD::STACKRESTORE      , MVT::Other, Expand);
00411   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Expand);
00412   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64  , Expand);
00413 
00414   // Cell SPU has instructions for converting between i64 and fp.
00415   setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
00416   setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
00417 
00418   // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
00419   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
00420 
00421   // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
00422   setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
00423 
00424   // First set operation action for all vector types to expand. Then we
00425   // will selectively turn on ones that can be effectively codegen'd.
00426   addRegisterClass(MVT::v16i8, SPU::VECREGRegisterClass);
00427   addRegisterClass(MVT::v8i16, SPU::VECREGRegisterClass);
00428   addRegisterClass(MVT::v4i32, SPU::VECREGRegisterClass);
00429   addRegisterClass(MVT::v2i64, SPU::VECREGRegisterClass);
00430   addRegisterClass(MVT::v4f32, SPU::VECREGRegisterClass);
00431   addRegisterClass(MVT::v2f64, SPU::VECREGRegisterClass);
00432 
00433   // "Odd size" vector classes that we're willing to support:
00434   addRegisterClass(MVT::v2i32, SPU::VECREGRegisterClass);
00435 
00436   for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
00437        i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
00438     MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
00439 
00440     // add/sub are legal for all supported vector VT's.
00441     setOperationAction(ISD::ADD,     VT, Legal);
00442     setOperationAction(ISD::SUB,     VT, Legal);
00443     // mul has to be custom lowered.
00444     setOperationAction(ISD::MUL,     VT, Legal);
00445 
00446     setOperationAction(ISD::AND,     VT, Legal);
00447     setOperationAction(ISD::OR,      VT, Legal);
00448     setOperationAction(ISD::XOR,     VT, Legal);
00449     setOperationAction(ISD::LOAD,    VT, Legal);
00450     setOperationAction(ISD::SELECT,  VT, Legal);
00451     setOperationAction(ISD::STORE,   VT, Legal);
00452 
00453     // These operations need to be expanded:
00454     setOperationAction(ISD::SDIV,    VT, Expand);
00455     setOperationAction(ISD::SREM,    VT, Expand);
00456     setOperationAction(ISD::UDIV,    VT, Expand);
00457     setOperationAction(ISD::UREM,    VT, Expand);
00458 
00459     // Custom lower build_vector, constant pool spills, insert and
00460     // extract vector elements:
00461     setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
00462     setOperationAction(ISD::ConstantPool, VT, Custom);
00463     setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
00464     setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
00465     setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
00466     setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
00467   }
00468 
00469   setOperationAction(ISD::AND, MVT::v16i8, Custom);
00470   setOperationAction(ISD::OR,  MVT::v16i8, Custom);
00471   setOperationAction(ISD::XOR, MVT::v16i8, Custom);
00472   setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
00473 
00474   setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
00475 
00476   setShiftAmountType(MVT::i32);
00477   setBooleanContents(ZeroOrNegativeOneBooleanContent);
00478 
00479   setStackPointerRegisterToSaveRestore(SPU::R1);
00480 
00481   // We have target-specific dag combine patterns for the following nodes:
00482   setTargetDAGCombine(ISD::ADD);
00483   setTargetDAGCombine(ISD::ZERO_EXTEND);
00484   setTargetDAGCombine(ISD::SIGN_EXTEND);
00485   setTargetDAGCombine(ISD::ANY_EXTEND);
00486 
00487   computeRegisterProperties();
00488 
00489   // Set pre-RA register scheduler default to BURR, which produces slightly
00490   // better code than the default (could also be TDRR, but TargetLowering.h
00491   // needs a mod to support that model):
00492   setSchedulingPreference(SchedulingForRegPressure);
00493 }
00494 
00495 const char *
00496 SPUTargetLowering::getTargetNodeName(unsigned Opcode) const
00497 {
00498   if (node_names.empty()) {
00499     node_names[(unsigned) SPUISD::RET_FLAG] = "SPUISD::RET_FLAG";
00500     node_names[(unsigned) SPUISD::Hi] = "SPUISD::Hi";
00501     node_names[(unsigned) SPUISD::Lo] = "SPUISD::Lo";
00502     node_names[(unsigned) SPUISD::PCRelAddr] = "SPUISD::PCRelAddr";
00503     node_names[(unsigned) SPUISD::AFormAddr] = "SPUISD::AFormAddr";
00504     node_names[(unsigned) SPUISD::IndirectAddr] = "SPUISD::IndirectAddr";
00505     node_names[(unsigned) SPUISD::LDRESULT] = "SPUISD::LDRESULT";
00506     node_names[(unsigned) SPUISD::CALL] = "SPUISD::CALL";
00507     node_names[(unsigned) SPUISD::SHUFB] = "SPUISD::SHUFB";
00508     node_names[(unsigned) SPUISD::SHUFFLE_MASK] = "SPUISD::SHUFFLE_MASK";
00509     node_names[(unsigned) SPUISD::CNTB] = "SPUISD::CNTB";
00510     node_names[(unsigned) SPUISD::PREFSLOT2VEC] = "SPUISD::PREFSLOT2VEC";
00511     node_names[(unsigned) SPUISD::VEC2PREFSLOT] = "SPUISD::VEC2PREFSLOT";
00512     node_names[(unsigned) SPUISD::SHLQUAD_L_BITS] = "SPUISD::SHLQUAD_L_BITS";
00513     node_names[(unsigned) SPUISD::SHLQUAD_L_BYTES] = "SPUISD::SHLQUAD_L_BYTES";
00514     node_names[(unsigned) SPUISD::VEC_SHL] = "SPUISD::VEC_SHL";
00515     node_names[(unsigned) SPUISD::VEC_SRL] = "SPUISD::VEC_SRL";
00516     node_names[(unsigned) SPUISD::VEC_SRA] = "SPUISD::VEC_SRA";
00517     node_names[(unsigned) SPUISD::VEC_ROTL] = "SPUISD::VEC_ROTL";
00518     node_names[(unsigned) SPUISD::VEC_ROTR] = "SPUISD::VEC_ROTR";
00519     node_names[(unsigned) SPUISD::ROTBYTES_LEFT] = "SPUISD::ROTBYTES_LEFT";
00520     node_names[(unsigned) SPUISD::ROTBYTES_LEFT_BITS] =
00521             "SPUISD::ROTBYTES_LEFT_BITS";
00522     node_names[(unsigned) SPUISD::SELECT_MASK] = "SPUISD::SELECT_MASK";
00523     node_names[(unsigned) SPUISD::SELB] = "SPUISD::SELB";
00524     node_names[(unsigned) SPUISD::ADD64_MARKER] = "SPUISD::ADD64_MARKER";
00525     node_names[(unsigned) SPUISD::SUB64_MARKER] = "SPUISD::SUB64_MARKER";
00526     node_names[(unsigned) SPUISD::MUL64_MARKER] = "SPUISD::MUL64_MARKER";
00527   }
00528 
00529   std::map<unsigned, const char *>::iterator i = node_names.find(Opcode);
00530 
00531   return ((i != node_names.end()) ? i->second : 0);
00532 }
00533 
00534 /// getFunctionAlignment - Return the Log2 alignment of this function.
00535 unsigned SPUTargetLowering::getFunctionAlignment(const Function *) const {
00536   return 3;
00537 }
00538 
00539 //===----------------------------------------------------------------------===//
00540 // Return the Cell SPU's SETCC result type
00541 //===----------------------------------------------------------------------===//
00542 
00543 MVT::SimpleValueType SPUTargetLowering::getSetCCResultType(EVT VT) const {
00544   // i16 and i32 are valid SETCC result types
00545   return ((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) ?
00546     VT.getSimpleVT().SimpleTy :
00547     MVT::i32);
00548 }
00549 
00550 //===----------------------------------------------------------------------===//
00551 // Calling convention code:
00552 //===----------------------------------------------------------------------===//
00553 
00554 #include "SPUGenCallingConv.inc"
00555 
00556 //===----------------------------------------------------------------------===//
00557 //  LowerOperation implementation
00558 //===----------------------------------------------------------------------===//
00559 
00560 /// Custom lower loads for CellSPU
00561 /*!
00562  All CellSPU loads and stores are aligned to 16-byte boundaries, so for elements
00563  within a 16-byte block, we have to rotate to extract the requested element.
00564 
00565  For extending loads, we also want to ensure that the following sequence is
00566  emitted, e.g. for MVT::f32 extending load to MVT::f64:
00567 
00568 \verbatim
00569 %1  v16i8,ch = load
00570 %2  v16i8,ch = rotate %1
00571 %3  v4f8, ch = bitconvert %2
00572 %4  f32      = vec2perfslot %3
00573 %5  f64      = fp_extend %4
00574 \endverbatim
00575 */
00576 static SDValue
00577 LowerLOAD(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
00578   LoadSDNode *LN = cast<LoadSDNode>(Op);
00579   SDValue the_chain = LN->getChain();
00580   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
00581   EVT InVT = LN->getMemoryVT();
00582   EVT OutVT = Op.getValueType();
00583   ISD::LoadExtType ExtType = LN->getExtensionType();
00584   unsigned alignment = LN->getAlignment();
00585   const valtype_map_s *vtm = getValueTypeMapEntry(InVT);
00586   DebugLoc dl = Op.getDebugLoc();
00587 
00588   switch (LN->getAddressingMode()) {
00589   case ISD::UNINDEXED: {
00590     SDValue result;
00591     SDValue basePtr = LN->getBasePtr();
00592     SDValue rotate;
00593 
00594     if (alignment == 16) {
00595       ConstantSDNode *CN;
00596 
00597       // Special cases for a known aligned load to simplify the base pointer
00598       // and the rotation amount:
00599       if (basePtr.getOpcode() == ISD::ADD
00600           && (CN = dyn_cast<ConstantSDNode > (basePtr.getOperand(1))) != 0) {
00601         // Known offset into basePtr
00602         int64_t offset = CN->getSExtValue();
00603         int64_t rotamt = int64_t((offset & 0xf) - vtm->prefslot_byte);
00604 
00605         if (rotamt < 0)
00606           rotamt += 16;
00607 
00608         rotate = DAG.getConstant(rotamt, MVT::i16);
00609 
00610         // Simplify the base pointer for this case:
00611         basePtr = basePtr.getOperand(0);
00612         if ((offset & ~0xf) > 0) {
00613           basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
00614                                 basePtr,
00615                                 DAG.getConstant((offset & ~0xf), PtrVT));
00616         }
00617       } else if ((basePtr.getOpcode() == SPUISD::AFormAddr)
00618                  || (basePtr.getOpcode() == SPUISD::IndirectAddr
00619                      && basePtr.getOperand(0).getOpcode() == SPUISD::Hi
00620                      && basePtr.getOperand(1).getOpcode() == SPUISD::Lo)) {
00621         // Plain aligned a-form address: rotate into preferred slot
00622         // Same for (SPUindirect (SPUhi ...), (SPUlo ...))
00623         int64_t rotamt = -vtm->prefslot_byte;
00624         if (rotamt < 0)
00625           rotamt += 16;
00626         rotate = DAG.getConstant(rotamt, MVT::i16);
00627       } else {
00628         // Offset the rotate amount by the basePtr and the preferred slot
00629         // byte offset
00630         int64_t rotamt = -vtm->prefslot_byte;
00631         if (rotamt < 0)
00632           rotamt += 16;
00633         rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
00634                              basePtr,
00635                              DAG.getConstant(rotamt, PtrVT));
00636       }
00637     } else {
00638       // Unaligned load: must be more pessimistic about addressing modes:
00639       if (basePtr.getOpcode() == ISD::ADD) {
00640         MachineFunction &MF = DAG.getMachineFunction();
00641         MachineRegisterInfo &RegInfo = MF.getRegInfo();
00642         unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
00643         SDValue Flag;
00644 
00645         SDValue Op0 = basePtr.getOperand(0);
00646         SDValue Op1 = basePtr.getOperand(1);
00647 
00648         if (isa<ConstantSDNode>(Op1)) {
00649           // Convert the (add <ptr>, <const>) to an indirect address contained
00650           // in a register. Note that this is done because we need to avoid
00651           // creating a 0(reg) d-form address due to the SPU's block loads.
00652           basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
00653           the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
00654           basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
00655         } else {
00656           // Convert the (add <arg1>, <arg2>) to an indirect address, which
00657           // will likely be lowered as a reg(reg) x-form address.
00658           basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
00659         }
00660       } else {
00661         basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
00662                               basePtr,
00663                               DAG.getConstant(0, PtrVT));
00664       }
00665 
00666       // Offset the rotate amount by the basePtr and the preferred slot
00667       // byte offset
00668       rotate = DAG.getNode(ISD::ADD, dl, PtrVT,
00669                            basePtr,
00670                            DAG.getConstant(-vtm->prefslot_byte, PtrVT));
00671     }
00672 
00673     // Re-emit as a v16i8 vector load
00674     result = DAG.getLoad(MVT::v16i8, dl, the_chain, basePtr,
00675                          LN->getSrcValue(), LN->getSrcValueOffset(),
00676                          LN->isVolatile(), 16);
00677 
00678     // Update the chain
00679     the_chain = result.getValue(1);
00680 
00681     // Rotate into the preferred slot:
00682     result = DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, MVT::v16i8,
00683                          result.getValue(0), rotate);
00684 
00685     // Convert the loaded v16i8 vector to the appropriate vector type
00686     // specified by the operand:
00687     EVT vecVT = EVT::getVectorVT(*DAG.getContext(), 
00688                                  InVT, (128 / InVT.getSizeInBits()));
00689     result = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, InVT,
00690                          DAG.getNode(ISD::BIT_CONVERT, dl, vecVT, result));
00691 
00692     // Handle extending loads by extending the scalar result:
00693     if (ExtType == ISD::SEXTLOAD) {
00694       result = DAG.getNode(ISD::SIGN_EXTEND, dl, OutVT, result);
00695     } else if (ExtType == ISD::ZEXTLOAD) {
00696       result = DAG.getNode(ISD::ZERO_EXTEND, dl, OutVT, result);
00697     } else if (ExtType == ISD::EXTLOAD) {
00698       unsigned NewOpc = ISD::ANY_EXTEND;
00699 
00700       if (OutVT.isFloatingPoint())
00701         NewOpc = ISD::FP_EXTEND;
00702 
00703       result = DAG.getNode(NewOpc, dl, OutVT, result);
00704     }
00705 
00706     SDVTList retvts = DAG.getVTList(OutVT, MVT::Other);
00707     SDValue retops[2] = {
00708       result,
00709       the_chain
00710     };
00711 
00712     result = DAG.getNode(SPUISD::LDRESULT, dl, retvts,
00713                          retops, sizeof(retops) / sizeof(retops[0]));
00714     return result;
00715   }
00716   case ISD::PRE_INC:
00717   case ISD::PRE_DEC:
00718   case ISD::POST_INC:
00719   case ISD::POST_DEC:
00720   case ISD::LAST_INDEXED_MODE:
00721     {
00722       std::string msg;
00723       raw_string_ostream Msg(msg);
00724       Msg << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
00725             "UNINDEXED\n";
00726       Msg << (unsigned) LN->getAddressingMode();
00727       llvm_report_error(Msg.str());
00728       /*NOTREACHED*/
00729     }
00730   }
00731 
00732   return SDValue();
00733 }
00734 
00735 /// Custom lower stores for CellSPU
00736 /*!
00737  All CellSPU stores are aligned to 16-byte boundaries, so for elements
00738  within a 16-byte block, we have to generate a shuffle to insert the
00739  requested element into its place, then store the resulting block.
00740  */
00741 static SDValue
00742 LowerSTORE(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
00743   StoreSDNode *SN = cast<StoreSDNode>(Op);
00744   SDValue Value = SN->getValue();
00745   EVT VT = Value.getValueType();
00746   EVT StVT = (!SN->isTruncatingStore() ? VT : SN->getMemoryVT());
00747   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
00748   DebugLoc dl = Op.getDebugLoc();
00749   unsigned alignment = SN->getAlignment();
00750 
00751   switch (SN->getAddressingMode()) {
00752   case ISD::UNINDEXED: {
00753     // The vector type we really want to load from the 16-byte chunk.
00754     EVT vecVT = EVT::getVectorVT(*DAG.getContext(),
00755                                  VT, (128 / VT.getSizeInBits())),
00756         stVecVT = EVT::getVectorVT(*DAG.getContext(),
00757                                    StVT, (128 / StVT.getSizeInBits()));
00758 
00759     SDValue alignLoadVec;
00760     SDValue basePtr = SN->getBasePtr();
00761     SDValue the_chain = SN->getChain();
00762     SDValue insertEltOffs;
00763 
00764     if (alignment == 16) {
00765       ConstantSDNode *CN;
00766 
00767       // Special cases for a known aligned load to simplify the base pointer
00768       // and insertion byte:
00769       if (basePtr.getOpcode() == ISD::ADD
00770           && (CN = dyn_cast<ConstantSDNode>(basePtr.getOperand(1))) != 0) {
00771         // Known offset into basePtr
00772         int64_t offset = CN->getSExtValue();
00773 
00774         // Simplify the base pointer for this case:
00775         basePtr = basePtr.getOperand(0);
00776         insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
00777                                     basePtr,
00778                                     DAG.getConstant((offset & 0xf), PtrVT));
00779 
00780         if ((offset & ~0xf) > 0) {
00781           basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
00782                                 basePtr,
00783                                 DAG.getConstant((offset & ~0xf), PtrVT));
00784         }
00785       } else {
00786         // Otherwise, assume it's at byte 0 of basePtr
00787         insertEltOffs = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
00788                                     basePtr,
00789                                     DAG.getConstant(0, PtrVT));
00790       }
00791     } else {
00792       // Unaligned load: must be more pessimistic about addressing modes:
00793       if (basePtr.getOpcode() == ISD::ADD) {
00794         MachineFunction &MF = DAG.getMachineFunction();
00795         MachineRegisterInfo &RegInfo = MF.getRegInfo();
00796         unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
00797         SDValue Flag;
00798 
00799         SDValue Op0 = basePtr.getOperand(0);
00800         SDValue Op1 = basePtr.getOperand(1);
00801 
00802         if (isa<ConstantSDNode>(Op1)) {
00803           // Convert the (add <ptr>, <const>) to an indirect address contained
00804           // in a register. Note that this is done because we need to avoid
00805           // creating a 0(reg) d-form address due to the SPU's block loads.
00806           basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
00807           the_chain = DAG.getCopyToReg(the_chain, dl, VReg, basePtr, Flag);
00808           basePtr = DAG.getCopyFromReg(the_chain, dl, VReg, PtrVT);
00809         } else {
00810           // Convert the (add <arg1>, <arg2>) to an indirect address, which
00811           // will likely be lowered as a reg(reg) x-form address.
00812           basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Op0, Op1);
00813         }
00814       } else {
00815         basePtr = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
00816                               basePtr,
00817                               DAG.getConstant(0, PtrVT));
00818       }
00819 
00820       // Insertion point is solely determined by basePtr's contents
00821       insertEltOffs = DAG.getNode(ISD::ADD, dl, PtrVT,
00822                                   basePtr,
00823                                   DAG.getConstant(0, PtrVT));
00824     }
00825 
00826     // Re-emit as a v16i8 vector load
00827     alignLoadVec = DAG.getLoad(MVT::v16i8, dl, the_chain, basePtr,
00828                                SN->getSrcValue(), SN->getSrcValueOffset(),
00829                                SN->isVolatile(), 16);
00830 
00831     // Update the chain
00832     the_chain = alignLoadVec.getValue(1);
00833 
00834     LoadSDNode *LN = cast<LoadSDNode>(alignLoadVec);
00835     SDValue theValue = SN->getValue();
00836     SDValue result;
00837 
00838     if (StVT != VT
00839         && (theValue.getOpcode() == ISD::AssertZext
00840             || theValue.getOpcode() == ISD::AssertSext)) {
00841       // Drill down and get the value for zero- and sign-extended
00842       // quantities
00843       theValue = theValue.getOperand(0);
00844     }
00845 
00846     // If the base pointer is already a D-form address, then just create
00847     // a new D-form address with a slot offset and the orignal base pointer.
00848     // Otherwise generate a D-form address with the slot offset relative
00849     // to the stack pointer, which is always aligned.
00850 #if !defined(NDEBUG)
00851       if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
00852         cerr << "CellSPU LowerSTORE: basePtr = ";
00853         basePtr.getNode()->dump(&DAG);
00854         cerr << "\n";
00855       }
00856 #endif
00857 
00858     SDValue insertEltOp =
00859             DAG.getNode(SPUISD::SHUFFLE_MASK, dl, vecVT, insertEltOffs);
00860     SDValue vectorizeOp =
00861             DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, vecVT, theValue);
00862 
00863     result = DAG.getNode(SPUISD::SHUFB, dl, vecVT,
00864                          vectorizeOp, alignLoadVec,
00865                          DAG.getNode(ISD::BIT_CONVERT, dl,
00866                                      MVT::v4i32, insertEltOp));
00867 
00868     result = DAG.getStore(the_chain, dl, result, basePtr,
00869                           LN->getSrcValue(), LN->getSrcValueOffset(),
00870                           LN->isVolatile(), LN->getAlignment());
00871 
00872 #if 0 && !defined(NDEBUG)
00873     if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
00874       const SDValue &currentRoot = DAG.getRoot();
00875 
00876       DAG.setRoot(result);
00877       cerr << "------- CellSPU:LowerStore result:\n";
00878       DAG.dump();
00879       cerr << "-------\n";
00880       DAG.setRoot(currentRoot);
00881     }
00882 #endif
00883 
00884     return result;
00885     /*UNREACHED*/
00886   }
00887   case ISD::PRE_INC:
00888   case ISD::PRE_DEC:
00889   case ISD::POST_INC:
00890   case ISD::POST_DEC:
00891   case ISD::LAST_INDEXED_MODE:
00892     {
00893       std::string msg;
00894       raw_string_ostream Msg(msg);
00895       Msg << "LowerLOAD: Got a LoadSDNode with an addr mode other than "
00896             "UNINDEXED\n";
00897       Msg << (unsigned) SN->getAddressingMode();
00898       llvm_report_error(Msg.str());
00899       /*NOTREACHED*/
00900     }
00901   }
00902 
00903   return SDValue();
00904 }
00905 
00906 //! Generate the address of a constant pool entry.
00907 static SDValue
00908 LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
00909   EVT PtrVT = Op.getValueType();
00910   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
00911   Constant *C = CP->getConstVal();
00912   SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
00913   SDValue Zero = DAG.getConstant(0, PtrVT);
00914   const TargetMachine &TM = DAG.getTarget();
00915   // FIXME there is no actual debug info here
00916   DebugLoc dl = Op.getDebugLoc();
00917 
00918   if (TM.getRelocationModel() == Reloc::Static) {
00919     if (!ST->usingLargeMem()) {
00920       // Just return the SDValue with the constant pool address in it.
00921       return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, CPI, Zero);
00922     } else {
00923       SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, CPI, Zero);
00924       SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, CPI, Zero);
00925       return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
00926     }
00927   }
00928 
00929   llvm_unreachable("LowerConstantPool: Relocation model other than static"
00930                    " not supported.");
00931   return SDValue();
00932 }
00933 
00934 //! Alternate entry point for generating the address of a constant pool entry
00935 SDValue
00936 SPU::LowerConstantPool(SDValue Op, SelectionDAG &DAG, const SPUTargetMachine &TM) {
00937   return ::LowerConstantPool(Op, DAG, TM.getSubtargetImpl());
00938 }
00939 
00940 static SDValue
00941 LowerJumpTable(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
00942   EVT PtrVT = Op.getValueType();
00943   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
00944   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
00945   SDValue Zero = DAG.getConstant(0, PtrVT);
00946   const TargetMachine &TM = DAG.getTarget();
00947   // FIXME there is no actual debug info here
00948   DebugLoc dl = Op.getDebugLoc();
00949 
00950   if (TM.getRelocationModel() == Reloc::Static) {
00951     if (!ST->usingLargeMem()) {
00952       return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, JTI, Zero);
00953     } else {
00954       SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, JTI, Zero);
00955       SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, JTI, Zero);
00956       return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
00957     }
00958   }
00959 
00960   llvm_unreachable("LowerJumpTable: Relocation model other than static"
00961                    " not supported.");
00962   return SDValue();
00963 }
00964 
00965 static SDValue
00966 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG, const SPUSubtarget *ST) {
00967   EVT PtrVT = Op.getValueType();
00968   GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
00969   GlobalValue *GV = GSDN->getGlobal();
00970   SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
00971   const TargetMachine &TM = DAG.getTarget();
00972   SDValue Zero = DAG.getConstant(0, PtrVT);
00973   // FIXME there is no actual debug info here
00974   DebugLoc dl = Op.getDebugLoc();
00975 
00976   if (TM.getRelocationModel() == Reloc::Static) {
00977     if (!ST->usingLargeMem()) {
00978       return DAG.getNode(SPUISD::AFormAddr, dl, PtrVT, GA, Zero);
00979     } else {
00980       SDValue Hi = DAG.getNode(SPUISD::Hi, dl, PtrVT, GA, Zero);
00981       SDValue Lo = DAG.getNode(SPUISD::Lo, dl, PtrVT, GA, Zero);
00982       return DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, Hi, Lo);
00983     }
00984   } else {
00985     llvm_report_error("LowerGlobalAddress: Relocation model other than static"
00986                       "not supported.");
00987     /*NOTREACHED*/
00988   }
00989 
00990   return SDValue();
00991 }
00992 
00993 //! Custom lower double precision floating point constants
00994 static SDValue
00995 LowerConstantFP(SDValue Op, SelectionDAG &DAG) {
00996   EVT VT = Op.getValueType();
00997   // FIXME there is no actual debug info here
00998   DebugLoc dl = Op.getDebugLoc();
00999 
01000   if (VT == MVT::f64) {
01001     ConstantFPSDNode *FP = cast<ConstantFPSDNode>(Op.getNode());
01002 
01003     assert((FP != 0) &&
01004            "LowerConstantFP: Node is not ConstantFPSDNode");
01005 
01006     uint64_t dbits = DoubleToBits(FP->getValueAPF().convertToDouble());
01007     SDValue T = DAG.getConstant(dbits, MVT::i64);
01008     SDValue Tvec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T);
01009     return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
01010                        DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Tvec));
01011   }
01012 
01013   return SDValue();
01014 }
01015 
01016 SDValue
01017 SPUTargetLowering::LowerFormalArguments(SDValue Chain,
01018                                         unsigned CallConv, bool isVarArg,
01019                                         const SmallVectorImpl<ISD::InputArg>
01020                                           &Ins,
01021                                         DebugLoc dl, SelectionDAG &DAG,
01022                                         SmallVectorImpl<SDValue> &InVals) {
01023 
01024   MachineFunction &MF = DAG.getMachineFunction();
01025   MachineFrameInfo *MFI = MF.getFrameInfo();
01026   MachineRegisterInfo &RegInfo = MF.getRegInfo();
01027 
01028   const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
01029   const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
01030 
01031   unsigned ArgOffset = SPUFrameInfo::minStackSize();
01032   unsigned ArgRegIdx = 0;
01033   unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
01034 
01035   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01036 
01037   // Add DAG nodes to load the arguments or copy them out of registers.
01038   for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
01039     EVT ObjectVT = Ins[ArgNo].VT;
01040     unsigned ObjSize = ObjectVT.getSizeInBits()/8;
01041     SDValue ArgVal;
01042 
01043     if (ArgRegIdx < NumArgRegs) {
01044       const TargetRegisterClass *ArgRegClass;
01045 
01046       switch (ObjectVT.getSimpleVT().SimpleTy) {
01047       default: {
01048         std::string msg;
01049         raw_string_ostream Msg(msg);
01050         Msg << "LowerFormalArguments Unhandled argument type: "
01051              << ObjectVT.getEVTString();
01052         llvm_report_error(Msg.str());
01053       }
01054       case MVT::i8:
01055         ArgRegClass = &SPU::R8CRegClass;
01056         break;
01057       case MVT::i16:
01058         ArgRegClass = &SPU::R16CRegClass;
01059         break;
01060       case MVT::i32:
01061         ArgRegClass = &SPU::R32CRegClass;
01062         break;
01063       case MVT::i64:
01064         ArgRegClass = &SPU::R64CRegClass;
01065         break;
01066       case MVT::i128:
01067         ArgRegClass = &SPU::GPRCRegClass;
01068         break;
01069       case MVT::f32:
01070         ArgRegClass = &SPU::R32FPRegClass;
01071         break;
01072       case MVT::f64:
01073         ArgRegClass = &SPU::R64FPRegClass;
01074         break;
01075       case MVT::v2f64:
01076       case MVT::v4f32:
01077       case MVT::v2i64:
01078       case MVT::v4i32:
01079       case MVT::v8i16:
01080       case MVT::v16i8:
01081         ArgRegClass = &SPU::VECREGRegClass;
01082         break;
01083       }
01084 
01085       unsigned VReg = RegInfo.createVirtualRegister(ArgRegClass);
01086       RegInfo.addLiveIn(ArgRegs[ArgRegIdx], VReg);
01087       ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
01088       ++ArgRegIdx;
01089     } else {
01090       // We need to load the argument to a virtual register if we determined
01091       // above that we ran out of physical registers of the appropriate type
01092       // or we're forced to do vararg
01093       int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
01094       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
01095       ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, NULL, 0);
01096       ArgOffset += StackSlotSize;
01097     }
01098 
01099     InVals.push_back(ArgVal);
01100     // Update the chain
01101     Chain = ArgVal.getOperand(0);
01102   }
01103 
01104   // vararg handling:
01105   if (isVarArg) {
01106     // unsigned int ptr_size = PtrVT.getSizeInBits() / 8;
01107     // We will spill (79-3)+1 registers to the stack
01108     SmallVector<SDValue, 79-3+1> MemOps;
01109 
01110     // Create the frame slot
01111 
01112     for (; ArgRegIdx != NumArgRegs; ++ArgRegIdx) {
01113       VarArgsFrameIndex = MFI->CreateFixedObject(StackSlotSize, ArgOffset);
01114       SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
01115       SDValue ArgVal = DAG.getRegister(ArgRegs[ArgRegIdx], MVT::v16i8);
01116       SDValue Store = DAG.getStore(Chain, dl, ArgVal, FIN, NULL, 0);
01117       Chain = Store.getOperand(0);
01118       MemOps.push_back(Store);
01119 
01120       // Increment address by stack slot size for the next stored argument
01121       ArgOffset += StackSlotSize;
01122     }
01123     if (!MemOps.empty())
01124       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
01125                           &MemOps[0], MemOps.size());
01126   }
01127 
01128   return Chain;
01129 }
01130 
01131 /// isLSAAddress - Return the immediate to use if the specified
01132 /// value is representable as a LSA address.
01133 static SDNode *isLSAAddress(SDValue Op, SelectionDAG &DAG) {
01134   ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
01135   if (!C) return 0;
01136 
01137   int Addr = C->getZExtValue();
01138   if ((Addr & 3) != 0 ||  // Low 2 bits are implicitly zero.
01139       (Addr << 14 >> 14) != Addr)
01140     return 0;  // Top 14 bits have to be sext of immediate.
01141 
01142   return DAG.getConstant((int)C->getZExtValue() >> 2, MVT::i32).getNode();
01143 }
01144 
01145 SDValue
01146 SPUTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
01147                              unsigned CallConv, bool isVarArg,
01148                              bool isTailCall,
01149                              const SmallVectorImpl<ISD::OutputArg> &Outs,
01150                              const SmallVectorImpl<ISD::InputArg> &Ins,
01151                              DebugLoc dl, SelectionDAG &DAG,
01152                              SmallVectorImpl<SDValue> &InVals) {
01153 
01154   const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
01155   unsigned NumOps     = Outs.size();
01156   unsigned StackSlotSize = SPUFrameInfo::stackSlotSize();
01157   const unsigned *ArgRegs = SPURegisterInfo::getArgRegs();
01158   const unsigned NumArgRegs = SPURegisterInfo::getNumArgRegs();
01159 
01160   // Handy pointer type
01161   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01162 
01163   // Accumulate how many bytes are to be pushed on the stack, including the
01164   // linkage area, and parameter passing area.  According to the SPU ABI,
01165   // we minimally need space for [LR] and [SP]
01166   unsigned NumStackBytes = SPUFrameInfo::minStackSize();
01167 
01168   // Set up a copy of the stack pointer for use loading and storing any
01169   // arguments that may not fit in the registers available for argument
01170   // passing.
01171   SDValue StackPtr = DAG.getRegister(SPU::R1, MVT::i32);
01172 
01173   // Figure out which arguments are going to go in registers, and which in
01174   // memory.
01175   unsigned ArgOffset = SPUFrameInfo::minStackSize(); // Just below [LR]
01176   unsigned ArgRegIdx = 0;
01177 
01178   // Keep track of registers passing arguments
01179   std::vector<std::pair<unsigned, SDValue> > RegsToPass;
01180   // And the arguments passed on the stack
01181   SmallVector<SDValue, 8> MemOpChains;
01182 
01183   for (unsigned i = 0; i != NumOps; ++i) {
01184     SDValue Arg = Outs[i].Val;
01185 
01186     // PtrOff will be used to store the current argument to the stack if a
01187     // register cannot be found for it.
01188     SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
01189     PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
01190 
01191     switch (Arg.getValueType().getSimpleVT().SimpleTy) {
01192     default: llvm_unreachable("Unexpected ValueType for argument!");
01193     case MVT::i8:
01194     case MVT::i16:
01195     case MVT::i32:
01196     case MVT::i64:
01197     case MVT::i128:
01198       if (ArgRegIdx != NumArgRegs) {
01199         RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
01200       } else {
01201         MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
01202         ArgOffset += StackSlotSize;
01203       }
01204       break;
01205     case MVT::f32:
01206     case MVT::f64:
01207       if (ArgRegIdx != NumArgRegs) {
01208         RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
01209       } else {
01210         MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
01211         ArgOffset += StackSlotSize;
01212       }
01213       break;
01214     case MVT::v2i64:
01215     case MVT::v2f64:
01216     case MVT::v4f32:
01217     case MVT::v4i32:
01218     case MVT::v8i16:
01219     case MVT::v16i8:
01220       if (ArgRegIdx != NumArgRegs) {
01221         RegsToPass.push_back(std::make_pair(ArgRegs[ArgRegIdx++], Arg));
01222       } else {
01223         MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
01224         ArgOffset += StackSlotSize;
01225       }
01226       break;
01227     }
01228   }
01229 
01230   // Update number of stack bytes actually used, insert a call sequence start
01231   NumStackBytes = (ArgOffset - SPUFrameInfo::minStackSize());
01232   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumStackBytes,
01233                                                             true));
01234 
01235   if (!MemOpChains.empty()) {
01236     // Adjust the stack pointer for the stack arguments.
01237     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
01238                         &MemOpChains[0], MemOpChains.size());
01239   }
01240 
01241   // Build a sequence of copy-to-reg nodes chained together with token chain
01242   // and flag operands which copy the outgoing args into the appropriate regs.
01243   SDValue InFlag;
01244   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
01245     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
01246                              RegsToPass[i].second, InFlag);
01247     InFlag = Chain.getValue(1);
01248   }
01249 
01250   SmallVector<SDValue, 8> Ops;
01251   unsigned CallOpc = SPUISD::CALL;
01252 
01253   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
01254   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
01255   // node so that legalize doesn't hack it.
01256   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
01257     GlobalValue *GV = G->getGlobal();
01258     EVT CalleeVT = Callee.getValueType();
01259     SDValue Zero = DAG.getConstant(0, PtrVT);
01260     SDValue GA = DAG.getTargetGlobalAddress(GV, CalleeVT);
01261 
01262     if (!ST->usingLargeMem()) {
01263       // Turn calls to targets that are defined (i.e., have bodies) into BRSL
01264       // style calls, otherwise, external symbols are BRASL calls. This assumes
01265       // that declared/defined symbols are in the same compilation unit and can
01266       // be reached through PC-relative jumps.
01267       //
01268       // NOTE:
01269       // This may be an unsafe assumption for JIT and really large compilation
01270       // units.
01271       if (GV->isDeclaration()) {
01272         Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, GA, Zero);
01273       } else {
01274         Callee = DAG.getNode(SPUISD::PCRelAddr, dl, CalleeVT, GA, Zero);
01275       }
01276     } else {
01277       // "Large memory" mode: Turn all calls into indirect calls with a X-form
01278       // address pairs:
01279       Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, GA, Zero);
01280     }
01281   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
01282     EVT CalleeVT = Callee.getValueType();
01283     SDValue Zero = DAG.getConstant(0, PtrVT);
01284     SDValue ExtSym = DAG.getTargetExternalSymbol(S->getSymbol(),
01285         Callee.getValueType());
01286 
01287     if (!ST->usingLargeMem()) {
01288       Callee = DAG.getNode(SPUISD::AFormAddr, dl, CalleeVT, ExtSym, Zero);
01289     } else {
01290       Callee = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT, ExtSym, Zero);
01291     }
01292   } else if (SDNode *Dest = isLSAAddress(Callee, DAG)) {
01293     // If this is an absolute destination address that appears to be a legal
01294     // local store address, use the munged value.
01295     Callee = SDValue(Dest, 0);
01296   }
01297 
01298   Ops.push_back(Chain);
01299   Ops.push_back(Callee);
01300 
01301   // Add argument registers to the end of the list so that they are known live
01302   // into the call.
01303   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
01304     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
01305                                   RegsToPass[i].second.getValueType()));
01306 
01307   if (InFlag.getNode())
01308     Ops.push_back(InFlag);
01309   // Returns a chain and a flag for retval copy to use.
01310   Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
01311                       &Ops[0], Ops.size());
01312   InFlag = Chain.getValue(1);
01313 
01314   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumStackBytes, true),
01315                              DAG.getIntPtrConstant(0, true), InFlag);
01316   if (!Ins.empty())
01317     InFlag = Chain.getValue(1);
01318 
01319   // If the function returns void, just return the chain.
01320   if (Ins.empty())
01321     return Chain;
01322 
01323   // If the call has results, copy the values out of the ret val registers.
01324   switch (Ins[0].VT.getSimpleVT().SimpleTy) {
01325   default: llvm_unreachable("Unexpected ret value!");
01326   case MVT::Other: break;
01327   case MVT::i32:
01328     if (Ins.size() > 1 && Ins[1].VT == MVT::i32) {
01329       Chain = DAG.getCopyFromReg(Chain, dl, SPU::R4,
01330                                  MVT::i32, InFlag).getValue(1);
01331       InVals.push_back(Chain.getValue(0));
01332       Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i32,
01333                                  Chain.getValue(2)).getValue(1);
01334       InVals.push_back(Chain.getValue(0));
01335     } else {
01336       Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i32,
01337                                  InFlag).getValue(1);
01338       InVals.push_back(Chain.getValue(0));
01339     }
01340     break;
01341   case MVT::i64:
01342     Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i64,
01343                                InFlag).getValue(1);
01344     InVals.push_back(Chain.getValue(0));
01345     break;
01346   case MVT::i128:
01347     Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, MVT::i128,
01348                                InFlag).getValue(1);
01349     InVals.push_back(Chain.getValue(0));
01350     break;
01351   case MVT::f32:
01352   case MVT::f64:
01353     Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, Ins[0].VT,
01354                                InFlag).getValue(1);
01355     InVals.push_back(Chain.getValue(0));
01356     break;
01357   case MVT::v2f64:
01358   case MVT::v2i64:
01359   case MVT::v4f32:
01360   case MVT::v4i32:
01361   case MVT::v8i16:
01362   case MVT::v16i8:
01363     Chain = DAG.getCopyFromReg(Chain, dl, SPU::R3, Ins[0].VT,
01364                                    InFlag).getValue(1);
01365     InVals.push_back(Chain.getValue(0));
01366     break;
01367   }
01368 
01369   return Chain;
01370 }
01371 
01372 SDValue
01373 SPUTargetLowering::LowerReturn(SDValue Chain,
01374                                unsigned CallConv, bool isVarArg,
01375                                const SmallVectorImpl<ISD::OutputArg> &Outs,
01376                                DebugLoc dl, SelectionDAG &DAG) {
01377 
01378   SmallVector<CCValAssign, 16> RVLocs;
01379   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
01380                  RVLocs, *DAG.getContext());
01381   CCInfo.AnalyzeReturn(Outs, RetCC_SPU);
01382 
01383   // If this is the first return lowered for this function, add the regs to the
01384   // liveout set for the function.
01385   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
01386     for (unsigned i = 0; i != RVLocs.size(); ++i)
01387       DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
01388   }
01389 
01390   SDValue Flag;
01391 
01392   // Copy the result values into the output registers.
01393   for (unsigned i = 0; i != RVLocs.size(); ++i) {
01394     CCValAssign &VA = RVLocs[i];
01395     assert(VA.isRegLoc() && "Can only return in registers!");
01396     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
01397                              Outs[i].Val, Flag);
01398     Flag = Chain.getValue(1);
01399   }
01400 
01401   if (Flag.getNode())
01402     return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
01403   else
01404     return DAG.getNode(SPUISD::RET_FLAG, dl, MVT::Other, Chain);
01405 }
01406 
01407 
01408 //===----------------------------------------------------------------------===//
01409 // Vector related lowering:
01410 //===----------------------------------------------------------------------===//
01411 
01412 static ConstantSDNode *
01413 getVecImm(SDNode *N) {
01414   SDValue OpVal(0, 0);
01415 
01416   // Check to see if this buildvec has a single non-undef value in its elements.
01417   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
01418     if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
01419     if (OpVal.getNode() == 0)
01420       OpVal = N->getOperand(i);
01421     else if (OpVal != N->getOperand(i))
01422       return 0;
01423   }
01424 
01425   if (OpVal.getNode() != 0) {
01426     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
01427       return CN;
01428     }
01429   }
01430 
01431   return 0;
01432 }
01433 
01434 /// get_vec_i18imm - Test if this vector is a vector filled with the same value
01435 /// and the value fits into an unsigned 18-bit constant, and if so, return the
01436 /// constant
01437 SDValue SPU::get_vec_u18imm(SDNode *N, SelectionDAG &DAG,
01438                               EVT ValueType) {
01439   if (ConstantSDNode *CN = getVecImm(N)) {
01440     uint64_t Value = CN->getZExtValue();
01441     if (ValueType == MVT::i64) {
01442       uint64_t UValue = CN->getZExtValue();
01443       uint32_t upper = uint32_t(UValue >> 32);
01444       uint32_t lower = uint32_t(UValue);
01445       if (upper != lower)
01446         return SDValue();
01447       Value = Value >> 32;
01448     }
01449     if (Value <= 0x3ffff)
01450       return DAG.getTargetConstant(Value, ValueType);
01451   }
01452 
01453   return SDValue();
01454 }
01455 
01456 /// get_vec_i16imm - Test if this vector is a vector filled with the same value
01457 /// and the value fits into a signed 16-bit constant, and if so, return the
01458 /// constant
01459 SDValue SPU::get_vec_i16imm(SDNode *N, SelectionDAG &DAG,
01460                               EVT ValueType) {
01461   if (ConstantSDNode *CN = getVecImm(N)) {
01462     int64_t Value = CN->getSExtValue();
01463     if (ValueType == MVT::i64) {
01464       uint64_t UValue = CN->getZExtValue();
01465       uint32_t upper = uint32_t(UValue >> 32);
01466       uint32_t lower = uint32_t(UValue);
01467       if (upper != lower)
01468         return SDValue();
01469       Value = Value >> 32;
01470     }
01471     if (Value >= -(1 << 15) && Value <= ((1 << 15) - 1)) {
01472       return DAG.getTargetConstant(Value, ValueType);
01473     }
01474   }
01475 
01476   return SDValue();
01477 }
01478 
01479 /// get_vec_i10imm - Test if this vector is a vector filled with the same value
01480 /// and the value fits into a signed 10-bit constant, and if so, return the
01481 /// constant
01482 SDValue SPU::get_vec_i10imm(SDNode *N, SelectionDAG &DAG,
01483                               EVT ValueType) {
01484   if (ConstantSDNode *CN = getVecImm(N)) {
01485     int64_t Value = CN->getSExtValue();
01486     if (ValueType == MVT::i64) {
01487       uint64_t UValue = CN->getZExtValue();
01488       uint32_t upper = uint32_t(UValue >> 32);
01489       uint32_t lower = uint32_t(UValue);
01490       if (upper != lower)
01491         return SDValue();
01492       Value = Value >> 32;
01493     }
01494     if (isS10Constant(Value))
01495       return DAG.getTargetConstant(Value, ValueType);
01496   }
01497 
01498   return SDValue();
01499 }
01500 
01501 /// get_vec_i8imm - Test if this vector is a vector filled with the same value
01502 /// and the value fits into a signed 8-bit constant, and if so, return the
01503 /// constant.
01504 ///
01505 /// @note: The incoming vector is v16i8 because that's the only way we can load
01506 /// constant vectors. Thus, we test to see if the upper and lower bytes are the
01507 /// same value.
01508 SDValue SPU::get_vec_i8imm(SDNode *N, SelectionDAG &DAG,
01509                              EVT ValueType) {
01510   if (ConstantSDNode *CN = getVecImm(N)) {
01511     int Value = (int) CN->getZExtValue();
01512     if (ValueType == MVT::i16
01513         && Value <= 0xffff                 /* truncated from uint64_t */
01514         && ((short) Value >> 8) == ((short) Value & 0xff))
01515       return DAG.getTargetConstant(Value & 0xff, ValueType);
01516     else if (ValueType == MVT::i8
01517              && (Value & 0xff) == Value)
01518       return DAG.getTargetConstant(Value, ValueType);
01519   }
01520 
01521   return SDValue();
01522 }
01523 
01524 /// get_ILHUvec_imm - Test if this vector is a vector filled with the same value
01525 /// and the value fits into a signed 16-bit constant, and if so, return the
01526 /// constant
01527 SDValue SPU::get_ILHUvec_imm(SDNode *N, SelectionDAG &DAG,
01528                                EVT ValueType) {
01529   if (ConstantSDNode *CN = getVecImm(N)) {
01530     uint64_t Value = CN->getZExtValue();
01531     if ((ValueType == MVT::i32
01532           && ((unsigned) Value & 0xffff0000) == (unsigned) Value)
01533         || (ValueType == MVT::i64 && (Value & 0xffff0000) == Value))
01534       return DAG.getTargetConstant(Value >> 16, ValueType);
01535   }
01536 
01537   return SDValue();
01538 }
01539 
01540 /// get_v4i32_imm - Catch-all for general 32-bit constant vectors
01541 SDValue SPU::get_v4i32_imm(SDNode *N, SelectionDAG &DAG) {
01542   if (ConstantSDNode *CN = getVecImm(N)) {
01543     return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i32);
01544   }
01545 
01546   return SDValue();
01547 }
01548 
01549 /// get_v4i32_imm - Catch-all for general 64-bit constant vectors
01550 SDValue SPU::get_v2i64_imm(SDNode *N, SelectionDAG &DAG) {
01551   if (ConstantSDNode *CN = getVecImm(N)) {
01552     return DAG.getTargetConstant((unsigned) CN->getZExtValue(), MVT::i64);
01553   }
01554 
01555   return SDValue();
01556 }
01557 
01558 //! Lower a BUILD_VECTOR instruction creatively:
01559 static SDValue
01560 LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
01561   EVT VT = Op.getValueType();
01562   EVT EltVT = VT.getVectorElementType();
01563   DebugLoc dl = Op.getDebugLoc();
01564   BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(Op.getNode());
01565   assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerBUILD_VECTOR");
01566   unsigned minSplatBits = EltVT.getSizeInBits();
01567 
01568   if (minSplatBits < 16)
01569     minSplatBits = 16;
01570 
01571   APInt APSplatBits, APSplatUndef;
01572   unsigned SplatBitSize;
01573   bool HasAnyUndefs;
01574 
01575   if (!BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
01576                             HasAnyUndefs, minSplatBits)
01577       || minSplatBits < SplatBitSize)
01578     return SDValue();   // Wasn't a constant vector or splat exceeded min
01579 
01580   uint64_t SplatBits = APSplatBits.getZExtValue();
01581 
01582   switch (VT.getSimpleVT().SimpleTy) {
01583   default: {
01584     std::string msg;
01585     raw_string_ostream Msg(msg);
01586     Msg << "CellSPU: Unhandled VT in LowerBUILD_VECTOR, VT = "
01587          << VT.getEVTString();
01588     llvm_report_error(Msg.str());
01589     /*NOTREACHED*/
01590   }
01591   case MVT::v4f32: {
01592     uint32_t Value32 = uint32_t(SplatBits);
01593     assert(SplatBitSize == 32
01594            && "LowerBUILD_VECTOR: Unexpected floating point vector element.");
01595     // NOTE: pretend the constant is an integer. LLVM won't load FP constants
01596     SDValue T = DAG.getConstant(Value32, MVT::i32);
01597     return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32,
01598                        DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, T,T,T,T));
01599     break;
01600   }
01601   case MVT::v2f64: {
01602     uint64_t f64val = uint64_t(SplatBits);
01603     assert(SplatBitSize == 64
01604            && "LowerBUILD_VECTOR: 64-bit float vector size > 8 bytes.");
01605     // NOTE: pretend the constant is an integer. LLVM won't load FP constants
01606     SDValue T = DAG.getConstant(f64val, MVT::i64);
01607     return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64,
01608                        DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64, T, T));
01609     break;
01610   }
01611   case MVT::v16i8: {
01612    // 8-bit constants have to be expanded to 16-bits
01613    unsigned short Value16 = SplatBits /* | (SplatBits << 8) */;
01614    SmallVector<SDValue, 8> Ops;
01615 
01616    Ops.assign(8, DAG.getConstant(Value16, MVT::i16));
01617    return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
01618                       DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i16, &Ops[0], Ops.size()));
01619   }
01620   case MVT::v8i16: {
01621     unsigned short Value16 = SplatBits;
01622     SDValue T = DAG.getConstant(Value16, EltVT);
01623     SmallVector<SDValue, 8> Ops;
01624 
01625     Ops.assign(8, T);
01626     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
01627   }
01628   case MVT::v4i32: {
01629     SDValue T = DAG.getConstant(unsigned(SplatBits), VT.getVectorElementType());
01630     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, T, T, T, T);
01631   }
01632   case MVT::v2i32: {
01633     SDValue T = DAG.getConstant(unsigned(SplatBits), VT.getVectorElementType());
01634     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, T, T);
01635   }
01636   case MVT::v2i64: {
01637     return SPU::LowerV2I64Splat(VT, DAG, SplatBits, dl);
01638   }
01639   }
01640 
01641   return SDValue();
01642 }
01643 
01644 /*!
01645  */
01646 SDValue
01647 SPU::LowerV2I64Splat(EVT OpVT, SelectionDAG& DAG, uint64_t SplatVal,
01648                      DebugLoc dl) {
01649   uint32_t upper = uint32_t(SplatVal >> 32);
01650   uint32_t lower = uint32_t(SplatVal);
01651 
01652   if (upper == lower) {
01653     // Magic constant that can be matched by IL, ILA, et. al.
01654     SDValue Val = DAG.getTargetConstant(upper, MVT::i32);
01655     return DAG.getNode(ISD::BIT_CONVERT, dl, OpVT,
01656                        DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
01657                                    Val, Val, Val, Val));
01658   } else {
01659     bool upper_special, lower_special;
01660 
01661     // NOTE: This code creates common-case shuffle masks that can be easily
01662     // detected as common expressions. It is not attempting to create highly
01663     // specialized masks to replace any and all 0's, 0xff's and 0x80's.
01664 
01665     // Detect if the upper or lower half is a special shuffle mask pattern:
01666     upper_special = (upper == 0 || upper == 0xffffffff || upper == 0x80000000);
01667     lower_special = (lower == 0 || lower == 0xffffffff || lower == 0x80000000);
01668 
01669     // Both upper and lower are special, lower to a constant pool load:
01670     if (lower_special && upper_special) {
01671       SDValue SplatValCN = DAG.getConstant(SplatVal, MVT::i64);
01672       return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
01673                          SplatValCN, SplatValCN);
01674     }
01675 
01676     SDValue LO32;
01677     SDValue HI32;
01678     SmallVector<SDValue, 16> ShufBytes;
01679     SDValue Result;
01680 
01681     // Create lower vector if not a special pattern
01682     if (!lower_special) {
01683       SDValue LO32C = DAG.getConstant(lower, MVT::i32);
01684       LO32 = DAG.getNode(ISD::BIT_CONVERT, dl, OpVT,
01685                          DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
01686                                      LO32C, LO32C, LO32C, LO32C));
01687     }
01688 
01689     // Create upper vector if not a special pattern
01690     if (!upper_special) {
01691       SDValue HI32C = DAG.getConstant(upper, MVT::i32);
01692       HI32 = DAG.getNode(ISD::BIT_CONVERT, dl, OpVT,
01693                          DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
01694                                      HI32C, HI32C, HI32C, HI32C));
01695     }
01696 
01697     // If either upper or lower are special, then the two input operands are
01698     // the same (basically, one of them is a "don't care")
01699     if (lower_special)
01700       LO32 = HI32;
01701     if (upper_special)
01702       HI32 = LO32;
01703 
01704     for (int i = 0; i < 4; ++i) {
01705       uint64_t val = 0;
01706       for (int j = 0; j < 4; ++j) {
01707         SDValue V;
01708         bool process_upper, process_lower;
01709         val <<= 8;
01710         process_upper = (upper_special && (i & 1) == 0);
01711         process_lower = (lower_special && (i & 1) == 1);
01712 
01713         if (process_upper || process_lower) {
01714           if ((process_upper && upper == 0)
01715                   || (process_lower && lower == 0))
01716             val |= 0x80;
01717           else if ((process_upper && upper == 0xffffffff)
01718                   || (process_lower && lower == 0xffffffff))
01719             val |= 0xc0;
01720           else if ((process_upper && upper == 0x80000000)
01721                   || (process_lower && lower == 0x80000000))
01722             val |= (j == 0 ? 0xe0 : 0x80);
01723         } else
01724           val |= i * 4 + j + ((i & 1) * 16);
01725       }
01726 
01727       ShufBytes.push_back(DAG.getConstant(val, MVT::i32));
01728     }
01729 
01730     return DAG.getNode(SPUISD::SHUFB, dl, OpVT, HI32, LO32,
01731                        DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
01732                                    &ShufBytes[0], ShufBytes.size()));
01733   }
01734 }
01735 
01736 /// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3) to something on
01737 /// which the Cell can operate. The code inspects V3 to ascertain whether the
01738 /// permutation vector, V3, is monotonically increasing with one "exception"
01739 /// element, e.g., (0, 1, _, 3). If this is the case, then generate a
01740 /// SHUFFLE_MASK synthetic instruction. Otherwise, spill V3 to the constant pool.
01741 /// In either case, the net result is going to eventually invoke SHUFB to
01742 /// permute/shuffle the bytes from V1 and V2.
01743 /// \note
01744 /// SHUFFLE_MASK is eventually selected as one of the C*D instructions, generate
01745 /// control word for byte/halfword/word insertion. This takes care of a single
01746 /// element move from V2 into V1.
01747 /// \note
01748 /// SPUISD::SHUFB is eventually selected as Cell's <i>shufb</i> instructions.
01749 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
01750   const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
01751   SDValue V1 = Op.getOperand(0);
01752   SDValue V2 = Op.getOperand(1);
01753   DebugLoc dl = Op.getDebugLoc();
01754 
01755   if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
01756 
01757   // If we have a single element being moved from V1 to V2, this can be handled
01758   // using the C*[DX] compute mask instructions, but the vector elements have
01759   // to be monotonically increasing with one exception element.
01760   EVT VecVT = V1.getValueType();
01761   EVT EltVT = VecVT.getVectorElementType();
01762   unsigned EltsFromV2 = 0;
01763   unsigned V2Elt = 0;
01764   unsigned V2EltIdx0 = 0;
01765   unsigned CurrElt = 0;
01766   unsigned MaxElts = VecVT.getVectorNumElements();
01767   unsigned PrevElt = 0;
01768   unsigned V0Elt = 0;
01769   bool monotonic = true;
01770   bool rotate = true;
01771 
01772   if (EltVT == MVT::i8) {
01773     V2EltIdx0 = 16;
01774   } else if (EltVT == MVT::i16) {
01775     V2EltIdx0 = 8;
01776   } else if (EltVT == MVT::i32 || EltVT == MVT::f32) {
01777     V2EltIdx0 = 4;
01778   } else if (EltVT == MVT::i64 || EltVT == MVT::f64) {
01779     V2EltIdx0 = 2;
01780   } else
01781     llvm_unreachable("Unhandled vector type in LowerVECTOR_SHUFFLE");
01782 
01783   for (unsigned i = 0; i != MaxElts; ++i) {
01784     if (SVN->getMaskElt(i) < 0)
01785       continue;
01786     
01787     unsigned SrcElt = SVN->getMaskElt(i);
01788 
01789     if (monotonic) {
01790       if (SrcElt >= V2EltIdx0) {
01791         if (1 >= (++EltsFromV2)) {
01792           V2Elt = (V2EltIdx0 - SrcElt) << 2;
01793         }
01794       } else if (CurrElt != SrcElt) {
01795         monotonic = false;
01796       }
01797 
01798       ++CurrElt;
01799     }
01800 
01801     if (rotate) {
01802       if (PrevElt > 0 && SrcElt < MaxElts) {
01803         if ((PrevElt == SrcElt - 1)
01804             || (PrevElt == MaxElts - 1 && SrcElt == 0)) {
01805           PrevElt = SrcElt;
01806           if (SrcElt == 0)
01807             V0Elt = i;
01808         } else {
01809           rotate = false;
01810         }
01811       } else if (PrevElt == 0) {
01812         // First time through, need to keep track of previous element
01813         PrevElt = SrcElt;
01814       } else {
01815         // This isn't a rotation, takes elements from vector 2
01816         rotate = false;
01817       }
01818     }
01819   }
01820 
01821   if (EltsFromV2 == 1 && monotonic) {
01822     // Compute mask and shuffle
01823     MachineFunction &MF = DAG.getMachineFunction();
01824     MachineRegisterInfo &RegInfo = MF.getRegInfo();
01825     unsigned VReg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
01826     EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01827     // Initialize temporary register to 0
01828     SDValue InitTempReg =
01829       DAG.getCopyToReg(DAG.getEntryNode(), dl, VReg, DAG.getConstant(0, PtrVT));
01830     // Copy register's contents as index in SHUFFLE_MASK:
01831     SDValue ShufMaskOp =
01832       DAG.getNode(SPUISD::SHUFFLE_MASK, dl, MVT::v4i32,
01833                   DAG.getTargetConstant(V2Elt, MVT::i32),
01834                   DAG.getCopyFromReg(InitTempReg, dl, VReg, PtrVT));
01835     // Use shuffle mask in SHUFB synthetic instruction:
01836     return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V2, V1,
01837                        ShufMaskOp);
01838   } else if (rotate) {
01839     int rotamt = (MaxElts - V0Elt) * EltVT.getSizeInBits()/8;
01840 
01841     return DAG.getNode(SPUISD::ROTBYTES_LEFT, dl, V1.getValueType(),
01842                        V1, DAG.getConstant(rotamt, MVT::i16));
01843   } else {
01844    // Convert the SHUFFLE_VECTOR mask's input element units to the
01845    // actual bytes.
01846     unsigned BytesPerElement = EltVT.getSizeInBits()/8;
01847 
01848     SmallVector<SDValue, 16> ResultMask;
01849     for (unsigned i = 0, e = MaxElts; i != e; ++i) {
01850       unsigned SrcElt = SVN->getMaskElt(i) < 0 ? 0 : SVN->getMaskElt(i);
01851 
01852       for (unsigned j = 0; j < BytesPerElement; ++j)
01853         ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,MVT::i8));
01854     }
01855 
01856     SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
01857                                     &ResultMask[0], ResultMask.size());
01858     return DAG.getNode(SPUISD::SHUFB, dl, V1.getValueType(), V1, V2, VPermMask);
01859   }
01860 }
01861 
01862 static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
01863   SDValue Op0 = Op.getOperand(0);                     // Op0 = the scalar
01864   DebugLoc dl = Op.getDebugLoc();
01865 
01866   if (Op0.getNode()->getOpcode() == ISD::Constant) {
01867     // For a constant, build the appropriate constant vector, which will
01868     // eventually simplify to a vector register load.
01869 
01870     ConstantSDNode *CN = cast<ConstantSDNode>(Op0.getNode());
01871     SmallVector<SDValue, 16> ConstVecValues;
01872     EVT VT;
01873     size_t n_copies;
01874 
01875     // Create a constant vector:
01876     switch (Op.getValueType().getSimpleVT().SimpleTy) {
01877     default: llvm_unreachable("Unexpected constant value type in "
01878                               "LowerSCALAR_TO_VECTOR");
01879     case MVT::v16i8: n_copies = 16; VT = MVT::i8; break;
01880     case MVT::v8i16: n_copies = 8; VT = MVT::i16; break;
01881     case MVT::v4i32: n_copies = 4; VT = MVT::i32; break;
01882     case MVT::v4f32: n_copies = 4; VT = MVT::f32; break;
01883     case MVT::v2i64: n_copies = 2; VT = MVT::i64; break;
01884     case MVT::v2f64: n_copies = 2; VT = MVT::f64; break;
01885     }
01886 
01887     SDValue CValue = DAG.getConstant(CN->getZExtValue(), VT);
01888     for (size_t j = 0; j < n_copies; ++j)
01889       ConstVecValues.push_back(CValue);
01890 
01891     return DAG.getNode(ISD::BUILD_VECTOR, dl, Op.getValueType(),
01892                        &ConstVecValues[0], ConstVecValues.size());
01893   } else {
01894     // Otherwise, copy the value from one register to another:
01895     switch (Op0.getValueType().getSimpleVT().SimpleTy) {
01896     default: llvm_unreachable("Unexpected value type in LowerSCALAR_TO_VECTOR");
01897     case MVT::i8:
01898     case MVT::i16:
01899     case MVT::i32:
01900     case MVT::i64:
01901     case MVT::f32:
01902     case MVT::f64:
01903       return DAG.getNode(SPUISD::PREFSLOT2VEC, dl, Op.getValueType(), Op0, Op0);
01904     }
01905   }
01906 
01907   return SDValue();
01908 }
01909 
01910 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
01911   EVT VT = Op.getValueType();
01912   SDValue N = Op.getOperand(0);
01913   SDValue Elt = Op.getOperand(1);
01914   DebugLoc dl = Op.getDebugLoc();
01915   SDValue retval;
01916 
01917   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
01918     // Constant argument:
01919     int EltNo = (int) C->getZExtValue();
01920 
01921     // sanity checks:
01922     if (VT == MVT::i8 && EltNo >= 16)
01923       llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i8 extraction slot > 15");
01924     else if (VT == MVT::i16 && EltNo >= 8)
01925       llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i16 extraction slot > 7");
01926     else if (VT == MVT::i32 && EltNo >= 4)
01927       llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i32 extraction slot > 4");
01928     else if (VT == MVT::i64 && EltNo >= 2)
01929       llvm_unreachable("SPU LowerEXTRACT_VECTOR_ELT: i64 extraction slot > 2");
01930 
01931     if (EltNo == 0 && (VT == MVT::i32 || VT == MVT::i64)) {
01932       // i32 and i64: Element 0 is the preferred slot
01933       return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, N);
01934     }
01935 
01936     // Need to generate shuffle mask and extract:
01937     int prefslot_begin = -1, prefslot_end = -1;
01938     int elt_byte = EltNo * VT.getSizeInBits() / 8;
01939 
01940     switch (VT.getSimpleVT().SimpleTy) {
01941     default:
01942       assert(false && "Invalid value type!");
01943     case MVT::i8: {
01944       prefslot_begin = prefslot_end = 3;
01945       break;
01946     }
01947     case MVT::i16: {
01948       prefslot_begin = 2; prefslot_end = 3;
01949       break;
01950     }
01951     case MVT::i32:
01952     case MVT::f32: {
01953       prefslot_begin = 0; prefslot_end = 3;
01954       break;
01955     }
01956     case MVT::i64:
01957     case MVT::f64: {
01958       prefslot_begin = 0; prefslot_end = 7;
01959       break;
01960     }
01961     }
01962 
01963     assert(prefslot_begin != -1 && prefslot_end != -1 &&
01964            "LowerEXTRACT_VECTOR_ELT: preferred slots uninitialized");
01965 
01966     unsigned int ShufBytes[16];
01967     for (int i = 0; i < 16; ++i) {
01968       // zero fill uppper part of preferred slot, don't care about the
01969       // other slots:
01970       unsigned int mask_val;
01971       if (i <= prefslot_end) {
01972         mask_val =
01973           ((i < prefslot_begin)
01974            ? 0x80
01975            : elt_byte + (i - prefslot_begin));
01976 
01977         ShufBytes[i] = mask_val;
01978       } else
01979         ShufBytes[i] = ShufBytes[i % (prefslot_end + 1)];
01980     }
01981 
01982     SDValue ShufMask[4];
01983     for (unsigned i = 0; i < sizeof(ShufMask)/sizeof(ShufMask[0]); ++i) {
01984       unsigned bidx = i * 4;
01985       unsigned int bits = ((ShufBytes[bidx] << 24) |
01986                            (ShufBytes[bidx+1] << 16) |
01987                            (ShufBytes[bidx+2] << 8) |
01988                            ShufBytes[bidx+3]);
01989       ShufMask[i] = DAG.getConstant(bits, MVT::i32);
01990     }
01991 
01992     SDValue ShufMaskVec =
01993       DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
01994                   &ShufMask[0], sizeof(ShufMask)/sizeof(ShufMask[0]));
01995 
01996     retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
01997                          DAG.getNode(SPUISD::SHUFB, dl, N.getValueType(),
01998                                      N, N, ShufMaskVec));
01999   } else {
02000     // Variable index: Rotate the requested element into slot 0, then replicate
02001     // slot 0 across the vector
02002     EVT VecVT = N.getValueType();
02003     if (!VecVT.isSimple() || !VecVT.isVector() || !VecVT.is128BitVector()) {
02004       llvm_report_error("LowerEXTRACT_VECTOR_ELT: Must have a simple, 128-bit"
02005                         "vector type!");
02006     }
02007 
02008     // Make life easier by making sure the index is zero-extended to i32
02009     if (Elt.getValueType() != MVT::i32)
02010       Elt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Elt);
02011 
02012     // Scale the index to a bit/byte shift quantity
02013     APInt scaleFactor =
02014             APInt(32, uint64_t(16 / N.getValueType().getVectorNumElements()), false);
02015     unsigned scaleShift = scaleFactor.logBase2();
02016     SDValue vecShift;
02017 
02018     if (scaleShift > 0) {
02019       // Scale the shift factor:
02020       Elt = DAG.getNode(ISD::SHL, dl, MVT::i32, Elt,
02021                         DAG.getConstant(scaleShift, MVT::i32));
02022     }
02023 
02024     vecShift = DAG.getNode(SPUISD::SHLQUAD_L_BYTES, dl, VecVT, N, Elt);
02025 
02026     // Replicate the bytes starting at byte 0 across the entire vector (for
02027     // consistency with the notion of a unified register set)
02028     SDValue replicate;
02029 
02030     switch (VT.getSimpleVT().SimpleTy) {
02031     default:
02032       llvm_report_error("LowerEXTRACT_VECTOR_ELT(varable): Unhandled vector"
02033                         "type");
02034       /*NOTREACHED*/
02035     case MVT::i8: {
02036       SDValue factor = DAG.getConstant(0x00000000, MVT::i32);
02037       replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
02038                               factor, factor, factor, factor);
02039       break;
02040     }
02041     case MVT::i16: {
02042       SDValue factor = DAG.getConstant(0x00010001, MVT::i32);
02043       replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
02044                               factor, factor, factor, factor);
02045       break;
02046     }
02047     case MVT::i32:
02048     case MVT::f32: {
02049       SDValue factor = DAG.getConstant(0x00010203, MVT::i32);
02050       replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
02051                               factor, factor, factor, factor);
02052       break;
02053     }
02054     case MVT::i64:
02055     case MVT::f64: {
02056       SDValue loFactor = DAG.getConstant(0x00010203, MVT::i32);
02057       SDValue hiFactor = DAG.getConstant(0x04050607, MVT::i32);
02058       replicate = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
02059                               loFactor, hiFactor, loFactor, hiFactor);
02060       break;
02061     }
02062     }
02063 
02064     retval = DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT,
02065                          DAG.getNode(SPUISD::SHUFB, dl, VecVT,
02066                                      vecShift, vecShift, replicate));
02067   }
02068 
02069   return retval;
02070 }
02071 
02072 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
02073   SDValue VecOp = Op.getOperand(0);
02074   SDValue ValOp = Op.getOperand(1);
02075   SDValue IdxOp = Op.getOperand(2);
02076   DebugLoc dl = Op.getDebugLoc();
02077   EVT VT = Op.getValueType();
02078 
02079   ConstantSDNode *CN = cast<ConstantSDNode>(IdxOp);
02080   assert(CN != 0 && "LowerINSERT_VECTOR_ELT: Index is not constant!");
02081 
02082   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
02083   // Use $sp ($1) because it's always 16-byte aligned and it's available:
02084   SDValue Pointer = DAG.getNode(SPUISD::IndirectAddr, dl, PtrVT,
02085                                 DAG.getRegister(SPU::R1, PtrVT),
02086                                 DAG.getConstant(CN->getSExtValue(), PtrVT));
02087   SDValue ShufMask = DAG.getNode(SPUISD::SHUFFLE_MASK, dl, VT, Pointer);
02088 
02089   SDValue result =
02090     DAG.getNode(SPUISD::SHUFB, dl, VT,
02091                 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, ValOp),
02092                 VecOp,
02093                 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, ShufMask));
02094 
02095   return result;
02096 }
02097 
02098 static SDValue LowerI8Math(SDValue Op, SelectionDAG &DAG, unsigned Opc,
02099                            const TargetLowering &TLI)
02100 {
02101   SDValue N0 = Op.getOperand(0);      // Everything has at least one operand
02102   DebugLoc dl = Op.getDebugLoc();
02103   EVT ShiftVT = TLI.getShiftAmountTy();
02104 
02105   assert(Op.getValueType() == MVT::i8);
02106   switch (Opc) {
02107   default:
02108     llvm_unreachable("Unhandled i8 math operator");
02109     /*NOTREACHED*/
02110     break;
02111   case ISD::ADD: {
02112     // 8-bit addition: Promote the arguments up to 16-bits and truncate
02113     // the result:
02114     SDValue N1 = Op.getOperand(1);
02115     N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
02116     N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
02117     return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
02118                        DAG.getNode(Opc, dl, MVT::i16, N0, N1));
02119 
02120   }
02121 
02122   case ISD::SUB: {
02123     // 8-bit subtraction: Promote the arguments up to 16-bits and truncate
02124     // the result:
02125     SDValue N1 = Op.getOperand(1);
02126     N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
02127     N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
02128     return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
02129                        DAG.getNode(Opc, dl, MVT::i16, N0, N1));
02130   }
02131   case ISD::ROTR:
02132   case ISD::ROTL: {
02133     SDValue N1 = Op.getOperand(1);
02134     EVT N1VT = N1.getValueType();
02135 
02136     N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
02137     if (!N1VT.bitsEq(ShiftVT)) {
02138       unsigned N1Opc = N1.getValueType().bitsLT(ShiftVT)
02139                        ? ISD::ZERO_EXTEND
02140                        : ISD::TRUNCATE;
02141       N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
02142     }
02143 
02144     // Replicate lower 8-bits into upper 8:
02145     SDValue ExpandArg =
02146       DAG.getNode(ISD::OR, dl, MVT::i16, N0,
02147                   DAG.getNode(ISD::SHL, dl, MVT::i16,
02148                               N0, DAG.getConstant(8, MVT::i32)));
02149 
02150     // Truncate back down to i8
02151     return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
02152                        DAG.getNode(Opc, dl, MVT::i16, ExpandArg, N1));
02153   }
02154   case ISD::SRL:
02155   case ISD::SHL: {
02156     SDValue N1 = Op.getOperand(1);
02157     EVT N1VT = N1.getValueType();
02158 
02159     N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, N0);
02160     if (!N1VT.bitsEq(ShiftVT)) {
02161       unsigned N1Opc = ISD::ZERO_EXTEND;
02162 
02163       if (N1.getValueType().bitsGT(ShiftVT))
02164         N1Opc = ISD::TRUNCATE;
02165 
02166       N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
02167     }
02168 
02169     return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
02170                        DAG.getNode(Opc, dl, MVT::i16, N0, N1));
02171   }
02172   case ISD::SRA: {
02173     SDValue N1 = Op.getOperand(1);
02174     EVT N1VT = N1.getValueType();
02175 
02176     N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
02177     if (!N1VT.bitsEq(ShiftVT)) {
02178       unsigned N1Opc = ISD::SIGN_EXTEND;
02179 
02180       if (N1VT.bitsGT(ShiftVT))
02181         N1Opc = ISD::TRUNCATE;
02182       N1 = DAG.getNode(N1Opc, dl, ShiftVT, N1);
02183     }
02184 
02185     return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
02186                        DAG.getNode(Opc, dl, MVT::i16, N0, N1));
02187   }
02188   case ISD::MUL: {
02189     SDValue N1 = Op.getOperand(1);
02190 
02191     N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N0);
02192     N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, N1);
02193     return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8,
02194                        DAG.getNode(Opc, dl, MVT::i16, N0, N1));
02195     break;
02196   }
02197   }
02198 
02199   return SDValue();
02200 }
02201 
02202 //! Lower byte immediate operations for v16i8 vectors:
02203 static SDValue
02204 LowerByteImmed(SDValue Op, SelectionDAG &DAG) {
02205   SDValue ConstVec;
02206   SDValue Arg;
02207   EVT VT = Op.getValueType();
02208   DebugLoc dl = Op.getDebugLoc();
02209 
02210   ConstVec = Op.getOperand(0);
02211   Arg = Op.getOperand(1);
02212   if (ConstVec.getNode()->getOpcode() != ISD::BUILD_VECTOR) {
02213     if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
02214       ConstVec = ConstVec.getOperand(0);
02215     } else {
02216       ConstVec = Op.getOperand(1);
02217       Arg = Op.getOperand(0);
02218       if (ConstVec.getNode()->getOpcode() == ISD::BIT_CONVERT) {
02219         ConstVec = ConstVec.getOperand(0);
02220       }
02221     }
02222   }
02223 
02224   if (ConstVec.getNode()->getOpcode() == ISD::BUILD_VECTOR) {
02225     BuildVectorSDNode *BCN = dyn_cast<BuildVectorSDNode>(ConstVec.getNode());
02226     assert(BCN != 0 && "Expected BuildVectorSDNode in SPU LowerByteImmed");
02227 
02228     APInt APSplatBits, APSplatUndef;
02229     unsigned SplatBitSize;
02230     bool HasAnyUndefs;
02231     unsigned minSplatBits = VT.getVectorElementType().getSizeInBits();
02232 
02233     if (BCN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
02234                               HasAnyUndefs, minSplatBits)
02235         && minSplatBits <= SplatBitSize) {
02236       uint64_t SplatBits = APSplatBits.getZExtValue();
02237       SDValue tc = DAG.getTargetConstant(SplatBits & 0xff, MVT::i8);
02238 
02239       SmallVector<SDValue, 16> tcVec;
02240       tcVec.assign(16, tc);
02241       return DAG.getNode(Op.getNode()->getOpcode(), dl, VT, Arg,
02242                          DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &tcVec[0], tcVec.size()));
02243     }
02244   }
02245 
02246   // These operations (AND, OR, XOR) are legal, they just couldn't be custom
02247   // lowered.  Return the operation, rather than a null SDValue.
02248   return Op;
02249 }
02250 
02251 //! Custom lowering for CTPOP (count population)
02252 /*!
02253   Custom lowering code that counts the number ones in the input
02254   operand. SPU has such an instruction, but it counts the number of
02255   ones per byte, which then have to be accumulated.
02256 */
02257 static SDValue LowerCTPOP(SDValue Op, SelectionDAG &DAG) {
02258   EVT VT = Op.getValueType();
02259   EVT vecVT = EVT::getVectorVT(*DAG.getContext(), 
02260                                VT, (128 / VT.getSizeInBits()));
02261   DebugLoc dl = Op.getDebugLoc();
02262 
02263   switch (VT.getSimpleVT().SimpleTy) {
02264   default:
02265     assert(false && "Invalid value type!");
02266   case MVT::i8: {
02267     SDValue N = Op.getOperand(0);
02268     SDValue Elt0 = DAG.getConstant(0, MVT::i32);
02269 
02270     SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
02271     SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
02272 
02273     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i8, CNTB, Elt0);
02274   }
02275 
02276   case MVT::i16: {
02277     MachineFunction &MF = DAG.getMachineFunction();
02278     MachineRegisterInfo &RegInfo = MF.getRegInfo();
02279 
02280     unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R16CRegClass);
02281 
02282     SDValue N = Op.getOperand(0);
02283     SDValue Elt0 = DAG.getConstant(0, MVT::i16);
02284     SDValue Mask0 = DAG.getConstant(0x0f, MVT::i16);
02285     SDValue Shift1 = DAG.getConstant(8, MVT::i32);
02286 
02287     SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
02288     SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
02289 
02290     // CNTB_result becomes the chain to which all of the virtual registers
02291     // CNTB_reg, SUM1_reg become associated:
02292     SDValue CNTB_result =
02293       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, CNTB, Elt0);
02294 
02295     SDValue CNTB_rescopy =
02296       DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
02297 
02298     SDValue Tmp1 = DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i16);
02299 
02300     return DAG.getNode(ISD::AND, dl, MVT::i16,
02301                        DAG.getNode(ISD::ADD, dl, MVT::i16,
02302                                    DAG.getNode(ISD::SRL, dl, MVT::i16,
02303                                                Tmp1, Shift1),
02304                                    Tmp1),
02305                        Mask0);
02306   }
02307 
02308   case MVT::i32: {
02309     MachineFunction &MF = DAG.getMachineFunction();
02310     MachineRegisterInfo &RegInfo = MF.getRegInfo();
02311 
02312     unsigned CNTB_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
02313     unsigned SUM1_reg = RegInfo.createVirtualRegister(&SPU::R32CRegClass);
02314 
02315     SDValue N = Op.getOperand(0);
02316     SDValue Elt0 = DAG.getConstant(0, MVT::i32);
02317     SDValue Mask0 = DAG.getConstant(0xff, MVT::i32);
02318     SDValue Shift1 = DAG.getConstant(16, MVT::i32);
02319     SDValue Shift2 = DAG.getConstant(8, MVT::i32);
02320 
02321     SDValue Promote = DAG.getNode(SPUISD::PREFSLOT2VEC, dl, vecVT, N, N);
02322     SDValue CNTB = DAG.getNode(SPUISD::CNTB, dl, vecVT, Promote);
02323 
02324     // CNTB_result becomes the chain to which all of the virtual registers
02325     // CNTB_reg, SUM1_reg become associated:
02326     SDValue CNTB_result =
02327       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, CNTB, Elt0);
02328 
02329     SDValue CNTB_rescopy =
02330       DAG.getCopyToReg(CNTB_result, dl, CNTB_reg, CNTB_result);
02331 
02332     SDValue Comp1 =
02333       DAG.getNode(ISD::SRL, dl, MVT::i32,
02334                   DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32),
02335                   Shift1);
02336 
02337     SDValue Sum1 =
02338       DAG.getNode(ISD::ADD, dl, MVT::i32, Comp1,
02339                   DAG.getCopyFromReg(CNTB_rescopy, dl, CNTB_reg, MVT::i32));
02340 
02341     SDValue Sum1_rescopy =
02342       DAG.getCopyToReg(CNTB_result, dl, SUM1_reg, Sum1);
02343 
02344     SDValue Comp2 =
02345       DAG.getNode(ISD::SRL, dl, MVT::i32,
02346                   DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32),
02347                   Shift2);
02348     SDValue Sum2 =
02349       DAG.getNode(ISD::ADD, dl, MVT::i32, Comp2,
02350                   DAG.getCopyFromReg(Sum1_rescopy, dl, SUM1_reg, MVT::i32));
02351 
02352     return DAG.getNode(ISD::AND, dl, MVT::i32, Sum2, Mask0);
02353   }
02354 
02355   case MVT::i64:
02356     break;
02357   }
02358 
02359   return SDValue();
02360 }
02361 
02362 //! Lower ISD::FP_TO_SINT, ISD::FP_TO_UINT for i32
02363 /*!
02364  f32->i32 passes through unchanged, whereas f64->i32 expands to a libcall.
02365  All conversions to i64 are expanded to a libcall.
02366  */
02367 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
02368                               SPUTargetLowering &TLI) {
02369   EVT OpVT = Op.getValueType();
02370   SDValue Op0 = Op.getOperand(0);
02371   EVT Op0VT = Op0.getValueType();
02372 
02373   if ((OpVT == MVT::i32 && Op0VT == MVT::f64)
02374       || OpVT == MVT::i64) {
02375     // Convert f32 / f64 to i32 / i64 via libcall.
02376     RTLIB::Libcall LC =
02377             (Op.getOpcode() == ISD::FP_TO_SINT)
02378              ? RTLIB::getFPTOSINT(Op0VT, OpVT)
02379              : RTLIB::getFPTOUINT(Op0VT, OpVT);
02380     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
02381     SDValue Dummy;
02382     return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
02383   }
02384 
02385   return Op;
02386 }
02387 
02388 //! Lower ISD::SINT_TO_FP, ISD::UINT_TO_FP for i32
02389 /*!
02390  i32->f32 passes through unchanged, whereas i32->f64 is expanded to a libcall.
02391  All conversions from i64 are expanded to a libcall.
02392  */
02393 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG,
02394                               SPUTargetLowering &TLI) {
02395   EVT OpVT = Op.getValueType();
02396   SDValue Op0 = Op.getOperand(0);
02397   EVT Op0VT = Op0.getValueType();
02398 
02399   if ((OpVT == MVT::f64 && Op0VT == MVT::i32)
02400       || Op0VT == MVT::i64) {
02401     // Convert i32, i64 to f64 via libcall:
02402     RTLIB::Libcall LC =
02403             (Op.getOpcode() == ISD::SINT_TO_FP)
02404              ? RTLIB::getSINTTOFP(Op0VT, OpVT)
02405              : RTLIB::getUINTTOFP(Op0VT, OpVT);
02406     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd int-to-fp conversion!");
02407     SDValue Dummy;
02408     return ExpandLibCall(LC, Op, DAG, false, Dummy, TLI);
02409   }
02410 
02411   return Op;
02412 }
02413 
02414 //! Lower ISD::SETCC
02415 /*!
02416  This handles MVT::f64 (double floating point) condition lowering
02417  */
02418 static SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG,
02419                           const TargetLowering &TLI) {
02420   CondCodeSDNode *CC = dyn_cast<CondCodeSDNode>(Op.getOperand(2));
02421   DebugLoc dl = Op.getDebugLoc();
02422   assert(CC != 0 && "LowerSETCC: CondCodeSDNode should not be null here!\n");
02423 
02424   SDValue lhs = Op.getOperand(0);
02425   SDValue rhs = Op.getOperand(1);
02426   EVT lhsVT = lhs.getValueType();
02427   assert(lhsVT == MVT::f64 && "LowerSETCC: type other than MVT::64\n");
02428 
02429   EVT ccResultVT = TLI.getSetCCResultType(lhs.getValueType());
02430   APInt ccResultOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
02431   EVT IntVT(MVT::i64);
02432 
02433   // Take advantage of the fact that (truncate (sra arg, 32)) is efficiently
02434   // selected to a NOP:
02435   SDValue i64lhs = DAG.getNode(ISD::BIT_CONVERT, dl, IntVT, lhs);
02436   SDValue lhsHi32 =
02437           DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
02438                       DAG.getNode(ISD::SRL, dl, IntVT,
02439                                   i64lhs, DAG.getConstant(32, MVT::i32)));
02440   SDValue lhsHi32abs =
02441           DAG.getNode(ISD::AND, dl, MVT::i32,
02442                       lhsHi32, DAG.getConstant(0x7fffffff, MVT::i32));
02443   SDValue lhsLo32 =
02444           DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, i64lhs);
02445 
02446   // SETO and SETUO only use the lhs operand:
02447   if (CC->get() == ISD::SETO) {
02448     // Evaluates to true if Op0 is not [SQ]NaN - lowers to the inverse of
02449     // SETUO
02450     APInt ccResultAllOnes = APInt::getAllOnesValue(ccResultVT.getSizeInBits());
02451     return DAG.getNode(ISD::XOR, dl, ccResultVT,
02452                        DAG.getSetCC(dl, ccResultVT,
02453                                     lhs, DAG.getConstantFP(0.0, lhsVT),
02454                                     ISD::SETUO),
02455                        DAG.getConstant(ccResultAllOnes, ccResultVT));
02456   } else if (CC->get() == ISD::SETUO) {
02457     // Evaluates to true if Op0 is [SQ]NaN
02458     return DAG.getNode(ISD::AND, dl, ccResultVT,
02459                        DAG.getSetCC(dl, ccResultVT,
02460                                     lhsHi32abs,
02461                                     DAG.getConstant(0x7ff00000, MVT::i32),
02462                                     ISD::SETGE),
02463                        DAG.getSetCC(dl, ccResultVT,
02464                                     lhsLo32,
02465                                     DAG.getConstant(0, MVT::i32),
02466                                     ISD::SETGT));
02467   }
02468 
02469   SDValue i64rhs = DAG.getNode(ISD::BIT_CONVERT, dl, IntVT, rhs);
02470   SDValue rhsHi32 =
02471           DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
02472                       DAG.getNode(ISD::SRL, dl, IntVT,
02473                                   i64rhs, DAG.getConstant(32, MVT::i32)));
02474 
02475   // If a value is negative, subtract from the sign magnitude constant:
02476   SDValue signMag2TC = DAG.getConstant(0x8000000000000000ULL, IntVT);
02477 
02478   // Convert the sign-magnitude representation into 2's complement:
02479   SDValue lhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
02480                                       lhsHi32, DAG.getConstant(31, MVT::i32));
02481   SDValue lhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64lhs);
02482   SDValue lhsSelect =
02483           DAG.getNode(ISD::SELECT, dl, IntVT,
02484                       lhsSelectMask, lhsSignMag2TC, i64lhs);
02485 
02486   SDValue rhsSelectMask = DAG.getNode(ISD::SRA, dl, ccResultVT,
02487                                       rhsHi32, DAG.getConstant(31, MVT::i32));
02488   SDValue rhsSignMag2TC = DAG.getNode(ISD::SUB, dl, IntVT, signMag2TC, i64rhs);
02489   SDValue rhsSelect =
02490           DAG.getNode(ISD::SELECT, dl, IntVT,
02491                       rhsSelectMask, rhsSignMag2TC, i64rhs);
02492 
02493   unsigned compareOp;
02494 
02495   switch (CC->get()) {
02496   case ISD::SETOEQ:
02497   case ISD::SETUEQ:
02498     compareOp = ISD::SETEQ; break;
02499   case ISD::SETOGT:
02500   case ISD::SETUGT:
02501     compareOp = ISD::SETGT; break;
02502   case ISD::SETOGE:
02503   case ISD::SETUGE:
02504     compareOp = ISD::SETGE; break;
02505   case ISD::SETOLT:
02506   case ISD::SETULT:
02507     compareOp = ISD::SETLT; break;
02508   case ISD::SETOLE:
02509   case ISD::SETULE:
02510     compareOp = ISD::SETLE; break;
02511   case ISD::SETUNE:
02512   case ISD::SETONE:
02513     compareOp = ISD::SETNE; break;
02514   default:
02515     llvm_report_error("CellSPU ISel Select: unimplemented f64 condition");
02516   }
02517 
02518   SDValue result =
02519           DAG.getSetCC(dl, ccResultVT, lhsSelect, rhsSelect,
02520                        (ISD::CondCode) compareOp);
02521 
02522   if ((CC->get() & 0x8) == 0) {
02523     // Ordered comparison:
02524     SDValue lhsNaN = DAG.getSetCC(dl, ccResultVT,
02525                                   lhs, DAG.getConstantFP(0.0, MVT::f64),
02526                                   ISD::SETO);
02527     SDValue rhsNaN = DAG.getSetCC(dl, ccResultVT,
02528                                   rhs, DAG.getConstantFP(0.0, MVT::f64),
02529                                   ISD::SETO);
02530     SDValue ordered = DAG.getNode(ISD::AND, dl, ccResultVT, lhsNaN, rhsNaN);
02531 
02532     result = DAG.getNode(ISD::AND, dl, ccResultVT, ordered, result);
02533   }
02534 
02535   return result;
02536 }
02537 
02538 //! Lower ISD::SELECT_CC
02539 /*!
02540   ISD::SELECT_CC can (generally) be implemented directly on the SPU using the
02541   SELB instruction.
02542 
02543   \note Need to revisit this in the future: if the code path through the true
02544   and false value computations is longer than the latency of a branch (6
02545   cycles), then it would be more advantageous to branch and insert a new basic
02546   block and branch on the condition. However, this code does not make that
02547   assumption, given the simplisitc uses so far.
02548  */
02549 
02550 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
02551                               const TargetLowering &TLI) {
02552   EVT VT = Op.getValueType();
02553   SDValue lhs = Op.getOperand(0);
02554   SDValue rhs = Op.getOperand(1);
02555   SDValue trueval = Op.getOperand(2);
02556   SDValue falseval = Op.getOperand(3);
02557   SDValue condition = Op.getOperand(4);
02558   DebugLoc dl = Op.getDebugLoc();
02559 
02560   // NOTE: SELB's arguments: $rA, $rB, $mask
02561   //
02562   // SELB selects bits from $rA where bits in $mask are 0, bits from $rB
02563   // where bits in $mask are 1. CCond will be inverted, having 1s where the
02564   // condition was true and 0s where the condition was false. Hence, the
02565   // arguments to SELB get reversed.
02566 
02567   // Note: Really should be ISD::SELECT instead of SPUISD::SELB, but LLVM's
02568   // legalizer insists on combining SETCC/SELECT into SELECT_CC, so we end up
02569   // with another "cannot select select_cc" assert:
02570 
02571   SDValue compare = DAG.getNode(ISD::SETCC, dl,
02572                                 TLI.getSetCCResultType(Op.getValueType()),
02573                                 lhs, rhs, condition);
02574   return DAG.getNode(SPUISD::SELB, dl, VT, falseval, trueval, compare);
02575 }
02576 
02577 //! Custom lower ISD::TRUNCATE
02578 static SDValue LowerTRUNCATE(SDValue Op, SelectionDAG &DAG)
02579 {
02580   // Type to truncate to
02581   EVT VT = Op.getValueType();
02582   MVT simpleVT = VT.getSimpleVT();
02583   EVT VecVT = EVT::getVectorVT(*DAG.getContext(), 
02584                                VT, (128 / VT.getSizeInBits()));
02585   DebugLoc dl = Op.getDebugLoc();
02586 
02587   // Type to truncate from
02588   SDValue Op0 = Op.getOperand(0);
02589   EVT Op0VT = Op0.getValueType();
02590 
02591   if (Op0VT.getSimpleVT() == MVT::i128 && simpleVT == MVT::i64) {
02592     // Create shuffle mask, least significant doubleword of quadword
02593     unsigned maskHigh = 0x08090a0b;
02594     unsigned maskLow = 0x0c0d0e0f;
02595     // Use a shuffle to perform the truncation
02596     SDValue shufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
02597                                    DAG.getConstant(maskHigh, MVT::i32),
02598                                    DAG.getConstant(maskLow, MVT::i32),
02599                                    DAG.getConstant(maskHigh, MVT::i32),
02600                                    DAG.getConstant(maskLow, MVT::i32));
02601 
02602     SDValue truncShuffle = DAG.getNode(SPUISD::SHUFB, dl, VecVT,
02603                                        Op0, Op0, shufMask);
02604 
02605     return DAG.getNode(SPUISD::VEC2PREFSLOT, dl, VT, truncShuffle);
02606   }
02607 
02608   return SDValue();             // Leave the truncate unmolested
02609 }
02610 
02611 //! Custom (target-specific) lowering entry point
02612 /*!
02613   This is where LLVM's DAG selection process calls to do target-specific
02614   lowering of nodes.
02615  */
02616 SDValue
02617 SPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
02618 {
02619   unsigned Opc = (unsigned) Op.getOpcode();
02620   EVT VT = Op.getValueType();
02621 
02622   switch (Opc) {
02623   default: {
02624 #ifndef NDEBUG
02625     cerr << "SPUTargetLowering::LowerOperation(): need to lower this!\n";
02626     cerr << "Op.getOpcode() = " << Opc << "\n";
02627     cerr << "*Op.getNode():\n";
02628     Op.getNode()->dump();
02629 #endif
02630     llvm_unreachable(0);
02631   }
02632   case ISD::LOAD:
02633   case ISD::EXTLOAD:
02634   case ISD::SEXTLOAD:
02635   case ISD::ZEXTLOAD:
02636     return LowerLOAD(Op, DAG, SPUTM.getSubtargetImpl());
02637   case ISD::STORE:
02638     return LowerSTORE(Op, DAG, SPUTM.getSubtargetImpl());
02639   case ISD::ConstantPool:
02640     return LowerConstantPool(Op, DAG, SPUTM.getSubtargetImpl());
02641   case ISD::GlobalAddress:
02642     return LowerGlobalAddress(Op, DAG, SPUTM.getSubtargetImpl());
02643   case ISD::JumpTable:
02644     return LowerJumpTable(Op, DAG, SPUTM.getSubtargetImpl());
02645   case ISD::ConstantFP:
02646     return LowerConstantFP(Op, DAG);
02647 
02648   // i8, i64 math ops:
02649   case ISD::ADD:
02650   case ISD::SUB:
02651   case ISD::ROTR:
02652   case ISD::ROTL:
02653   case ISD::SRL:
02654   case ISD::SHL:
02655   case ISD::SRA: {
02656     if (VT == MVT::i8)
02657       return LowerI8Math(Op, DAG, Opc, *this);
02658     break;
02659   }
02660 
02661   case ISD::FP_TO_SINT:
02662   case ISD::FP_TO_UINT:
02663     return LowerFP_TO_INT(Op, DAG, *this);
02664 
02665   case ISD::SINT_TO_FP:
02666   case ISD::UINT_TO_FP:
02667     return LowerINT_TO_FP(Op, DAG, *this);
02668 
02669   // Vector-related lowering.
02670   case ISD::BUILD_VECTOR:
02671     return LowerBUILD_VECTOR(Op, DAG);
02672   case ISD::SCALAR_TO_VECTOR:
02673     return LowerSCALAR_TO_VECTOR(Op, DAG);
02674   case ISD::VECTOR_SHUFFLE:
02675     return LowerVECTOR_SHUFFLE(Op, DAG);
02676   case ISD::EXTRACT_VECTOR_ELT:
02677     return LowerEXTRACT_VECTOR_ELT(Op, DAG);
02678   case ISD::INSERT_VECTOR_ELT:
02679     return LowerINSERT_VECTOR_ELT(Op, DAG);
02680 
02681   // Look for ANDBI, ORBI and XORBI opportunities and lower appropriately:
02682   case ISD::AND:
02683   case ISD::OR:
02684   case ISD::XOR:
02685     return LowerByteImmed(Op, DAG);
02686 
02687   // Vector and i8 multiply:
02688   case ISD::MUL:
02689     if (VT == MVT::i8)
02690       return LowerI8Math(Op, DAG, Opc, *this);
02691 
02692   case ISD::CTPOP:
02693     return LowerCTPOP(Op, DAG);
02694 
02695   case ISD::SELECT_CC:
02696     return LowerSELECT_CC(Op, DAG, *this);
02697 
02698   case ISD::SETCC:
02699     return LowerSETCC(Op, DAG, *this);
02700 
02701   case ISD::TRUNCATE:
02702     return LowerTRUNCATE(Op, DAG);
02703   }
02704 
02705   return SDValue();
02706 }
02707 
02708 void SPUTargetLowering::ReplaceNodeResults(SDNode *N,
02709                                            SmallVectorImpl<SDValue>&Results,
02710                                            SelectionDAG &DAG)
02711 {
02712 #if 0
02713   unsigned Opc = (unsigned) N->getOpcode();
02714   EVT OpVT = N->getValueType(0);
02715 
02716   switch (Opc) {
02717   default: {
02718     cerr << "SPUTargetLowering::ReplaceNodeResults(): need to fix this!\n";
02719     cerr << "Op.getOpcode() = " << Opc << "\n";
02720     cerr << "*Op.getNode():\n";
02721     N->dump();
02722     abort();
02723     /*NOTREACHED*/
02724   }
02725   }
02726 #endif
02727 
02728   /* Otherwise, return unchanged */
02729 }
02730 
02731 //===----------------------------------------------------------------------===//
02732 // Target Optimization Hooks
02733 //===----------------------------------------------------------------------===//
02734 
02735 SDValue
02736 SPUTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const
02737 {
02738 #if 0
02739   TargetMachine &TM = getTargetMachine();
02740 #endif
02741   const SPUSubtarget *ST = SPUTM.getSubtargetImpl();
02742   SelectionDAG &DAG = DCI.DAG;
02743   SDValue Op0 = N->getOperand(0);       // everything has at least one operand
02744   EVT NodeVT = N->getValueType(0);      // The node's value type
02745   EVT Op0VT = Op0.getValueType();       // The first operand's result
02746   SDValue Result;                       // Initially, empty result
02747   DebugLoc dl = N->getDebugLoc();
02748 
02749   switch (N->getOpcode()) {
02750   default: break;
02751   case ISD::ADD: {
02752     SDValue Op1 = N->getOperand(1);
02753 
02754     if (Op0.getOpcode() == SPUISD::IndirectAddr
02755         || Op1.getOpcode() == SPUISD::IndirectAddr) {
02756       // Normalize the operands to reduce repeated code
02757       SDValue IndirectArg = Op0, AddArg = Op1;
02758 
02759       if (Op1.getOpcode() == SPUISD::IndirectAddr) {
02760         IndirectArg = Op1;
02761         AddArg = Op0;
02762       }
02763 
02764       if (isa<ConstantSDNode>(AddArg)) {
02765         ConstantSDNode *CN0 = cast<ConstantSDNode > (AddArg);
02766         SDValue IndOp1 = IndirectArg.getOperand(1);
02767 
02768         if (CN0->isNullValue()) {
02769           // (add (SPUindirect <arg>, <arg>), 0) ->
02770           // (SPUindirect <arg>, <arg>)
02771 
02772 #if !defined(NDEBUG)
02773           if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
02774             cerr << "\n"
02775                  << "Replace: (add (SPUindirect <arg>, <arg>), 0)\n"
02776                  << "With:    (SPUindirect <arg>, <arg>)\n";
02777           }
02778 #endif
02779 
02780           return IndirectArg;
02781         } else if (isa<ConstantSDNode>(IndOp1)) {
02782           // (add (SPUindirect <arg>, <const>), <const>) ->
02783           // (SPUindirect <arg>, <const + const>)
02784           ConstantSDNode *CN1 = cast<ConstantSDNode > (IndOp1);
02785           int64_t combinedConst = CN0->getSExtValue() + CN1->getSExtValue();
02786           SDValue combinedValue = DAG.getConstant(combinedConst, Op0VT);
02787 
02788 #if !defined(NDEBUG)
02789           if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
02790             cerr << "\n"
02791                  << "Replace: (add (SPUindirect <arg>, " << CN1->getSExtValue()
02792                  << "), " << CN0->getSExtValue() << ")\n"
02793                  << "With:    (SPUindirect <arg>, "
02794                  << combinedConst << ")\n";
02795           }
02796 #endif
02797 
02798           return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
02799                              IndirectArg, combinedValue);
02800         }
02801       }
02802     }
02803     break;
02804   }
02805   case ISD::SIGN_EXTEND:
02806   case ISD::ZERO_EXTEND:
02807   case ISD::ANY_EXTEND: {
02808     if (Op0.getOpcode() == SPUISD::VEC2PREFSLOT && NodeVT == Op0VT) {
02809       // (any_extend (SPUextract_elt0 <arg>)) ->
02810       // (SPUextract_elt0 <arg>)
02811       // Types must match, however...
02812 #if !defined(NDEBUG)
02813       if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
02814         cerr << "\nReplace: ";
02815         N->dump(&DAG);
02816         cerr << "\nWith:    ";
02817         Op0.getNode()->dump(&DAG);
02818         cerr << "\n";
02819       }
02820 #endif
02821 
02822       return Op0;
02823     }
02824     break;
02825   }
02826   case SPUISD::IndirectAddr: {
02827     if (!ST->usingLargeMem() && Op0.getOpcode() == SPUISD::AFormAddr) {
02828       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1));
02829       if (CN != 0 && CN->getZExtValue() == 0) {
02830         // (SPUindirect (SPUaform <addr>, 0), 0) ->
02831         // (SPUaform <addr>, 0)
02832 
02833         DEBUG(cerr << "Replace: ");
02834         DEBUG(N->dump(&DAG));
02835         DEBUG(cerr << "\nWith:    ");
02836         DEBUG(Op0.getNode()->dump(&DAG));
02837         DEBUG(cerr << "\n");
02838 
02839         return Op0;
02840       }
02841     } else if (Op0.getOpcode() == ISD::ADD) {
02842       SDValue Op1 = N->getOperand(1);
02843       if (ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(Op1)) {
02844         // (SPUindirect (add <arg>, <arg>), 0) ->
02845         // (SPUindirect <arg>, <arg>)
02846         if (CN1->isNullValue()) {
02847 
02848 #if !defined(NDEBUG)
02849           if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) {
02850             cerr << "\n"
02851                  << "Replace: (SPUindirect (add <arg>, <arg>), 0)\n"
02852                  << "With:    (SPUindirect <arg>, <arg>)\n";
02853           }
02854 #endif
02855 
02856           return DAG.getNode(SPUISD::IndirectAddr, dl, Op0VT,
02857                              Op0.getOperand(0), Op0.getOperand(1));
02858         }
02859       }
02860     }
02861     break;
02862   }
02863   case SPUISD::SHLQUAD_L_BITS:
02864   case SPUISD::SHLQUAD_L_BYTES:
02865   case SPUISD::VEC_SHL:
02866   case SPUISD::VEC_SRL:
02867   case SPUISD::VEC_SRA:
02868   case SPUISD::ROTBYTES_LEFT: {
02869     SDValue Op1 = N->getOperand(1);
02870 
02871     // Kill degenerate vector shifts:
02872     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
02873       if (CN->isNullValue()) {
02874         Result = Op0;
02875       }
02876     }
02877     break;
02878   }
02879   case SPUISD::PREFSLOT2VEC: {
02880     switch (Op0.getOpcode()) {
02881     default:
02882       break;
02883     case ISD::ANY_EXTEND:
02884     case ISD::ZERO_EXTEND:
02885     case ISD::SIGN_EXTEND: {
02886       // (SPUprefslot2vec (any|zero|sign_extend (SPUvec2prefslot <arg>))) ->
02887       // <arg>
02888       // but only if the SPUprefslot2vec and <arg> types match.
02889       SDValue Op00 = Op0.getOperand(0);
02890       if (Op00.getOpcode() == SPUISD::VEC2PREFSLOT) {
02891         SDValue Op000 = Op00.getOperand(0);
02892         if (Op000.getValueType() == NodeVT) {
02893           Result = Op000;
02894         }
02895       }
02896       break;
02897     }
02898     case SPUISD::VEC2PREFSLOT: {
02899       // (SPUprefslot2vec (SPUvec2prefslot <arg>)) ->
02900       // <arg>
02901       Result = Op0.getOperand(0);
02902       break;
02903     }
02904     }
02905     break;
02906   }
02907   }
02908 
02909   // Otherwise, return unchanged.
02910 #ifndef NDEBUG
02911   if (Result.getNode()) {
02912     DEBUG(cerr << "\nReplace.SPU: ");
02913     DEBUG(N->dump(&DAG));
02914     DEBUG(cerr << "\nWith:        ");
02915     DEBUG(Result.getNode()->dump(&DAG));
02916     DEBUG(cerr << "\n");
02917   }
02918 #endif
02919 
02920   return Result;
02921 }
02922 
02923 //===----------------------------------------------------------------------===//
02924 // Inline Assembly Support
02925 //===----------------------------------------------------------------------===//
02926 
02927 /// getConstraintType - Given a constraint letter, return the type of
02928 /// constraint it is for this target.
02929 SPUTargetLowering::ConstraintType
02930 SPUTargetLowering::getConstraintType(const std::string &ConstraintLetter) const {
02931   if (ConstraintLetter.size() == 1) {
02932     switch (ConstraintLetter[0]) {
02933     default: break;
02934     case 'b':
02935     case 'r':
02936     case 'f':
02937     case 'v':
02938     case 'y':
02939       return C_RegisterClass;
02940     }
02941   }
02942   return TargetLowering::getConstraintType(ConstraintLetter);
02943 }
02944 
02945 std::pair<unsigned, const TargetRegisterClass*>
02946 SPUTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
02947                                                 EVT VT) const
02948 {
02949   if (Constraint.size() == 1) {
02950     // GCC RS6000 Constraint Letters
02951     switch (Constraint[0]) {
02952     case 'b':   // R1-R31
02953     case 'r':   // R0-R31
02954       if (VT == MVT::i64)
02955         return std::make_pair(0U, SPU::R64CRegisterClass);
02956       return std::make_pair(0U, SPU::R32CRegisterClass);
02957     case 'f':
02958       if (VT == MVT::f32)
02959         return std::make_pair(0U, SPU::R32FPRegisterClass);
02960       else if (VT == MVT::f64)
02961         return std::make_pair(0U, SPU::R64FPRegisterClass);
02962       break;
02963     case 'v':
02964       return std::make_pair(0U, SPU::GPRCRegisterClass);
02965     }
02966   }
02967 
02968   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
02969 }
02970 
02971 //! Compute used/known bits for a SPU operand
02972 void
02973 SPUTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
02974                                                   const APInt &Mask,
02975                                                   APInt &KnownZero,
02976                                                   APInt &KnownOne,
02977                                                   const SelectionDAG &DAG,
02978                                                   unsigned Depth ) const {
02979 #if 0
02980   const uint64_t uint64_sizebits = sizeof(uint64_t) * CHAR_BIT;
02981 
02982   switch (Op.getOpcode()) {
02983   default:
02984     // KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
02985     break;
02986   case CALL:
02987   case SHUFB:
02988   case SHUFFLE_MASK:
02989   case CNTB:
02990   case SPUISD::PREFSLOT2VEC:
02991   case SPUISD::LDRESULT:
02992   case SPUISD::VEC2PREFSLOT:
02993   case SPUISD::SHLQUAD_L_BITS:
02994   case SPUISD::SHLQUAD_L_BYTES:
02995   case SPUISD::VEC_SHL:
02996   case SPUISD::VEC_SRL:
02997   case SPUISD::VEC_SRA:
02998   case SPUISD::VEC_ROTL:
02999   case SPUISD::VEC_ROTR:
03000   case SPUISD::ROTBYTES_LEFT:
03001   case SPUISD::SELECT_MASK:
03002   case SPUISD::SELB:
03003   }
03004 #endif
03005 }
03006 
03007 unsigned
03008 SPUTargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
03009                                                    unsigned Depth) const {
03010   switch (Op.getOpcode()) {
03011   default:
03012     return 1;
03013 
03014   case ISD::SETCC: {
03015     EVT VT = Op.getValueType();
03016 
03017     if (VT != MVT::i8 && VT != MVT::i16 && VT != MVT::i32) {
03018       VT = MVT::i32;
03019     }
03020     return VT.getSizeInBits();
03021   }
03022   }
03023 }
03024 
03025 // LowerAsmOperandForConstraint
03026 void
03027 SPUTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
03028                                                 char ConstraintLetter,
03029                                                 bool hasMemory,
03030                                                 std::vector<SDValue> &Ops,
03031                                                 SelectionDAG &DAG) const {
03032   // Default, for the time being, to the base class handler
03033   TargetLowering::LowerAsmOperandForConstraint(Op, ConstraintLetter, hasMemory,
03034                                                Ops, DAG);
03035 }
03036 
03037 /// isLegalAddressImmediate - Return true if the integer value can be used
03038 /// as the offset of the target addressing mode.
03039 bool SPUTargetLowering::isLegalAddressImmediate(int64_t V,
03040                                                 const Type *Ty) const {
03041   // SPU's addresses are 256K:
03042   return (V > -(1 << 18) && V < (1 << 18) - 1);
03043 }
03044 
03045 bool SPUTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
03046   return false;
03047 }
03048 
03049 bool
03050 SPUTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
03051   // The SPU target isn't yet aware of offsets.
03052   return false;
03053 }