LLVM API Documentation

SPUISelLowering.cpp

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