Home | History | Annotate | Download | only in Mips
      1 //===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Subclass of MipsTargetLowering specialized for mips32/64.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #include "MipsSEISelLowering.h"
     14 #include "MipsMachineFunction.h"
     15 #include "MipsRegisterInfo.h"
     16 #include "MipsTargetMachine.h"
     17 #include "llvm/CodeGen/MachineInstrBuilder.h"
     18 #include "llvm/CodeGen/MachineRegisterInfo.h"
     19 #include "llvm/IR/Intrinsics.h"
     20 #include "llvm/Support/CommandLine.h"
     21 #include "llvm/Support/Debug.h"
     22 #include "llvm/Support/raw_ostream.h"
     23 #include "llvm/Target/TargetInstrInfo.h"
     24 
     25 using namespace llvm;
     26 
     27 #define DEBUG_TYPE "mips-isel"
     28 
     29 static cl::opt<bool>
     30 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
     31                     cl::desc("MIPS: Enable tail calls."), cl::init(false));
     32 
     33 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
     34                                    cl::desc("Expand double precision loads and "
     35                                             "stores to their single precision "
     36                                             "counterparts"));
     37 
     38 MipsSETargetLowering::MipsSETargetLowering(const MipsTargetMachine &TM,
     39                                            const MipsSubtarget &STI)
     40     : MipsTargetLowering(TM, STI) {
     41   // Set up the register classes
     42   addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
     43 
     44   if (Subtarget.isGP64bit())
     45     addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
     46 
     47   if (Subtarget.hasDSP() || Subtarget.hasMSA()) {
     48     // Expand all truncating stores and extending loads.
     49     for (MVT VT0 : MVT::vector_valuetypes()) {
     50       for (MVT VT1 : MVT::vector_valuetypes()) {
     51         setTruncStoreAction(VT0, VT1, Expand);
     52         setLoadExtAction(ISD::SEXTLOAD, VT0, VT1, Expand);
     53         setLoadExtAction(ISD::ZEXTLOAD, VT0, VT1, Expand);
     54         setLoadExtAction(ISD::EXTLOAD, VT0, VT1, Expand);
     55       }
     56     }
     57   }
     58 
     59   if (Subtarget.hasDSP()) {
     60     MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
     61 
     62     for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
     63       addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
     64 
     65       // Expand all builtin opcodes.
     66       for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
     67         setOperationAction(Opc, VecTys[i], Expand);
     68 
     69       setOperationAction(ISD::ADD, VecTys[i], Legal);
     70       setOperationAction(ISD::SUB, VecTys[i], Legal);
     71       setOperationAction(ISD::LOAD, VecTys[i], Legal);
     72       setOperationAction(ISD::STORE, VecTys[i], Legal);
     73       setOperationAction(ISD::BITCAST, VecTys[i], Legal);
     74     }
     75 
     76     setTargetDAGCombine(ISD::SHL);
     77     setTargetDAGCombine(ISD::SRA);
     78     setTargetDAGCombine(ISD::SRL);
     79     setTargetDAGCombine(ISD::SETCC);
     80     setTargetDAGCombine(ISD::VSELECT);
     81   }
     82 
     83   if (Subtarget.hasDSPR2())
     84     setOperationAction(ISD::MUL, MVT::v2i16, Legal);
     85 
     86   if (Subtarget.hasMSA()) {
     87     addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
     88     addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
     89     addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
     90     addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
     91     addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
     92     addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
     93     addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
     94 
     95     setTargetDAGCombine(ISD::AND);
     96     setTargetDAGCombine(ISD::OR);
     97     setTargetDAGCombine(ISD::SRA);
     98     setTargetDAGCombine(ISD::VSELECT);
     99     setTargetDAGCombine(ISD::XOR);
    100   }
    101 
    102   if (!Subtarget.useSoftFloat()) {
    103     addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
    104 
    105     // When dealing with single precision only, use libcalls
    106     if (!Subtarget.isSingleFloat()) {
    107       if (Subtarget.isFP64bit())
    108         addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
    109       else
    110         addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
    111     }
    112   }
    113 
    114   setOperationAction(ISD::SMUL_LOHI,          MVT::i32, Custom);
    115   setOperationAction(ISD::UMUL_LOHI,          MVT::i32, Custom);
    116   setOperationAction(ISD::MULHS,              MVT::i32, Custom);
    117   setOperationAction(ISD::MULHU,              MVT::i32, Custom);
    118 
    119   if (Subtarget.hasCnMips())
    120     setOperationAction(ISD::MUL,              MVT::i64, Legal);
    121   else if (Subtarget.isGP64bit())
    122     setOperationAction(ISD::MUL,              MVT::i64, Custom);
    123 
    124   if (Subtarget.isGP64bit()) {
    125     setOperationAction(ISD::SMUL_LOHI,        MVT::i64, Custom);
    126     setOperationAction(ISD::UMUL_LOHI,        MVT::i64, Custom);
    127     setOperationAction(ISD::MULHS,            MVT::i64, Custom);
    128     setOperationAction(ISD::MULHU,            MVT::i64, Custom);
    129     setOperationAction(ISD::SDIVREM,          MVT::i64, Custom);
    130     setOperationAction(ISD::UDIVREM,          MVT::i64, Custom);
    131   }
    132 
    133   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
    134   setOperationAction(ISD::INTRINSIC_W_CHAIN,  MVT::i64, Custom);
    135 
    136   setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
    137   setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
    138   setOperationAction(ISD::ATOMIC_FENCE,       MVT::Other, Custom);
    139   setOperationAction(ISD::LOAD,               MVT::i32, Custom);
    140   setOperationAction(ISD::STORE,              MVT::i32, Custom);
    141 
    142   setTargetDAGCombine(ISD::ADDE);
    143   setTargetDAGCombine(ISD::SUBE);
    144   setTargetDAGCombine(ISD::MUL);
    145 
    146   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
    147   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
    148   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
    149 
    150   if (NoDPLoadStore) {
    151     setOperationAction(ISD::LOAD, MVT::f64, Custom);
    152     setOperationAction(ISD::STORE, MVT::f64, Custom);
    153   }
    154 
    155   if (Subtarget.hasMips32r6()) {
    156     // MIPS32r6 replaces the accumulator-based multiplies with a three register
    157     // instruction
    158     setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
    159     setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
    160     setOperationAction(ISD::MUL, MVT::i32, Legal);
    161     setOperationAction(ISD::MULHS, MVT::i32, Legal);
    162     setOperationAction(ISD::MULHU, MVT::i32, Legal);
    163 
    164     // MIPS32r6 replaces the accumulator-based division/remainder with separate
    165     // three register division and remainder instructions.
    166     setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
    167     setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
    168     setOperationAction(ISD::SDIV, MVT::i32, Legal);
    169     setOperationAction(ISD::UDIV, MVT::i32, Legal);
    170     setOperationAction(ISD::SREM, MVT::i32, Legal);
    171     setOperationAction(ISD::UREM, MVT::i32, Legal);
    172 
    173     // MIPS32r6 replaces conditional moves with an equivalent that removes the
    174     // need for three GPR read ports.
    175     setOperationAction(ISD::SETCC, MVT::i32, Legal);
    176     setOperationAction(ISD::SELECT, MVT::i32, Legal);
    177     setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
    178 
    179     setOperationAction(ISD::SETCC, MVT::f32, Legal);
    180     setOperationAction(ISD::SELECT, MVT::f32, Legal);
    181     setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
    182 
    183     assert(Subtarget.isFP64bit() && "FR=1 is required for MIPS32r6");
    184     setOperationAction(ISD::SETCC, MVT::f64, Legal);
    185     setOperationAction(ISD::SELECT, MVT::f64, Legal);
    186     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
    187 
    188     setOperationAction(ISD::BRCOND, MVT::Other, Legal);
    189 
    190     // Floating point > and >= are supported via < and <=
    191     setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
    192     setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
    193     setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
    194     setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
    195 
    196     setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
    197     setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
    198     setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
    199     setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
    200   }
    201 
    202   if (Subtarget.hasMips64r6()) {
    203     // MIPS64r6 replaces the accumulator-based multiplies with a three register
    204     // instruction
    205     setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
    206     setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
    207     setOperationAction(ISD::MUL, MVT::i64, Legal);
    208     setOperationAction(ISD::MULHS, MVT::i64, Legal);
    209     setOperationAction(ISD::MULHU, MVT::i64, Legal);
    210 
    211     // MIPS32r6 replaces the accumulator-based division/remainder with separate
    212     // three register division and remainder instructions.
    213     setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
    214     setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
    215     setOperationAction(ISD::SDIV, MVT::i64, Legal);
    216     setOperationAction(ISD::UDIV, MVT::i64, Legal);
    217     setOperationAction(ISD::SREM, MVT::i64, Legal);
    218     setOperationAction(ISD::UREM, MVT::i64, Legal);
    219 
    220     // MIPS64r6 replaces conditional moves with an equivalent that removes the
    221     // need for three GPR read ports.
    222     setOperationAction(ISD::SETCC, MVT::i64, Legal);
    223     setOperationAction(ISD::SELECT, MVT::i64, Legal);
    224     setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
    225   }
    226 
    227   computeRegisterProperties(Subtarget.getRegisterInfo());
    228 }
    229 
    230 const MipsTargetLowering *
    231 llvm::createMipsSETargetLowering(const MipsTargetMachine &TM,
    232                                  const MipsSubtarget &STI) {
    233   return new MipsSETargetLowering(TM, STI);
    234 }
    235 
    236 const TargetRegisterClass *
    237 MipsSETargetLowering::getRepRegClassFor(MVT VT) const {
    238   if (VT == MVT::Untyped)
    239     return Subtarget.hasDSP() ? &Mips::ACC64DSPRegClass : &Mips::ACC64RegClass;
    240 
    241   return TargetLowering::getRepRegClassFor(VT);
    242 }
    243 
    244 // Enable MSA support for the given integer type and Register class.
    245 void MipsSETargetLowering::
    246 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
    247   addRegisterClass(Ty, RC);
    248 
    249   // Expand all builtin opcodes.
    250   for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
    251     setOperationAction(Opc, Ty, Expand);
    252 
    253   setOperationAction(ISD::BITCAST, Ty, Legal);
    254   setOperationAction(ISD::LOAD, Ty, Legal);
    255   setOperationAction(ISD::STORE, Ty, Legal);
    256   setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
    257   setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
    258   setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
    259 
    260   setOperationAction(ISD::ADD, Ty, Legal);
    261   setOperationAction(ISD::AND, Ty, Legal);
    262   setOperationAction(ISD::CTLZ, Ty, Legal);
    263   setOperationAction(ISD::CTPOP, Ty, Legal);
    264   setOperationAction(ISD::MUL, Ty, Legal);
    265   setOperationAction(ISD::OR, Ty, Legal);
    266   setOperationAction(ISD::SDIV, Ty, Legal);
    267   setOperationAction(ISD::SREM, Ty, Legal);
    268   setOperationAction(ISD::SHL, Ty, Legal);
    269   setOperationAction(ISD::SRA, Ty, Legal);
    270   setOperationAction(ISD::SRL, Ty, Legal);
    271   setOperationAction(ISD::SUB, Ty, Legal);
    272   setOperationAction(ISD::UDIV, Ty, Legal);
    273   setOperationAction(ISD::UREM, Ty, Legal);
    274   setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
    275   setOperationAction(ISD::VSELECT, Ty, Legal);
    276   setOperationAction(ISD::XOR, Ty, Legal);
    277 
    278   if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
    279     setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
    280     setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
    281     setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
    282     setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
    283   }
    284 
    285   setOperationAction(ISD::SETCC, Ty, Legal);
    286   setCondCodeAction(ISD::SETNE, Ty, Expand);
    287   setCondCodeAction(ISD::SETGE, Ty, Expand);
    288   setCondCodeAction(ISD::SETGT, Ty, Expand);
    289   setCondCodeAction(ISD::SETUGE, Ty, Expand);
    290   setCondCodeAction(ISD::SETUGT, Ty, Expand);
    291 }
    292 
    293 // Enable MSA support for the given floating-point type and Register class.
    294 void MipsSETargetLowering::
    295 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
    296   addRegisterClass(Ty, RC);
    297 
    298   // Expand all builtin opcodes.
    299   for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
    300     setOperationAction(Opc, Ty, Expand);
    301 
    302   setOperationAction(ISD::LOAD, Ty, Legal);
    303   setOperationAction(ISD::STORE, Ty, Legal);
    304   setOperationAction(ISD::BITCAST, Ty, Legal);
    305   setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
    306   setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
    307   setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
    308 
    309   if (Ty != MVT::v8f16) {
    310     setOperationAction(ISD::FABS,  Ty, Legal);
    311     setOperationAction(ISD::FADD,  Ty, Legal);
    312     setOperationAction(ISD::FDIV,  Ty, Legal);
    313     setOperationAction(ISD::FEXP2, Ty, Legal);
    314     setOperationAction(ISD::FLOG2, Ty, Legal);
    315     setOperationAction(ISD::FMA,   Ty, Legal);
    316     setOperationAction(ISD::FMUL,  Ty, Legal);
    317     setOperationAction(ISD::FRINT, Ty, Legal);
    318     setOperationAction(ISD::FSQRT, Ty, Legal);
    319     setOperationAction(ISD::FSUB,  Ty, Legal);
    320     setOperationAction(ISD::VSELECT, Ty, Legal);
    321 
    322     setOperationAction(ISD::SETCC, Ty, Legal);
    323     setCondCodeAction(ISD::SETOGE, Ty, Expand);
    324     setCondCodeAction(ISD::SETOGT, Ty, Expand);
    325     setCondCodeAction(ISD::SETUGE, Ty, Expand);
    326     setCondCodeAction(ISD::SETUGT, Ty, Expand);
    327     setCondCodeAction(ISD::SETGE,  Ty, Expand);
    328     setCondCodeAction(ISD::SETGT,  Ty, Expand);
    329   }
    330 }
    331 
    332 bool
    333 MipsSETargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
    334                                                      unsigned,
    335                                                      unsigned,
    336                                                      bool *Fast) const {
    337   MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
    338 
    339   if (Subtarget.systemSupportsUnalignedAccess()) {
    340     // MIPS32r6/MIPS64r6 is required to support unaligned access. It's
    341     // implementation defined whether this is handled by hardware, software, or
    342     // a hybrid of the two but it's expected that most implementations will
    343     // handle the majority of cases in hardware.
    344     if (Fast)
    345       *Fast = true;
    346     return true;
    347   }
    348 
    349   switch (SVT) {
    350   case MVT::i64:
    351   case MVT::i32:
    352     if (Fast)
    353       *Fast = true;
    354     return true;
    355   default:
    356     return false;
    357   }
    358 }
    359 
    360 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
    361                                              SelectionDAG &DAG) const {
    362   switch(Op.getOpcode()) {
    363   case ISD::LOAD:  return lowerLOAD(Op, DAG);
    364   case ISD::STORE: return lowerSTORE(Op, DAG);
    365   case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
    366   case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
    367   case ISD::MULHS:     return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
    368   case ISD::MULHU:     return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
    369   case ISD::MUL:       return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
    370   case ISD::SDIVREM:   return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
    371   case ISD::UDIVREM:   return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
    372                                           DAG);
    373   case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
    374   case ISD::INTRINSIC_W_CHAIN:  return lowerINTRINSIC_W_CHAIN(Op, DAG);
    375   case ISD::INTRINSIC_VOID:     return lowerINTRINSIC_VOID(Op, DAG);
    376   case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
    377   case ISD::BUILD_VECTOR:       return lowerBUILD_VECTOR(Op, DAG);
    378   case ISD::VECTOR_SHUFFLE:     return lowerVECTOR_SHUFFLE(Op, DAG);
    379   }
    380 
    381   return MipsTargetLowering::LowerOperation(Op, DAG);
    382 }
    383 
    384 // selectMADD -
    385 // Transforms a subgraph in CurDAG if the following pattern is found:
    386 //  (addc multLo, Lo0), (adde multHi, Hi0),
    387 // where,
    388 //  multHi/Lo: product of multiplication
    389 //  Lo0: initial value of Lo register
    390 //  Hi0: initial value of Hi register
    391 // Return true if pattern matching was successful.
    392 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
    393   // ADDENode's second operand must be a flag output of an ADDC node in order
    394   // for the matching to be successful.
    395   SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
    396 
    397   if (ADDCNode->getOpcode() != ISD::ADDC)
    398     return false;
    399 
    400   SDValue MultHi = ADDENode->getOperand(0);
    401   SDValue MultLo = ADDCNode->getOperand(0);
    402   SDNode *MultNode = MultHi.getNode();
    403   unsigned MultOpc = MultHi.getOpcode();
    404 
    405   // MultHi and MultLo must be generated by the same node,
    406   if (MultLo.getNode() != MultNode)
    407     return false;
    408 
    409   // and it must be a multiplication.
    410   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
    411     return false;
    412 
    413   // MultLo amd MultHi must be the first and second output of MultNode
    414   // respectively.
    415   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
    416     return false;
    417 
    418   // Transform this to a MADD only if ADDENode and ADDCNode are the only users
    419   // of the values of MultNode, in which case MultNode will be removed in later
    420   // phases.
    421   // If there exist users other than ADDENode or ADDCNode, this function returns
    422   // here, which will result in MultNode being mapped to a single MULT
    423   // instruction node rather than a pair of MULT and MADD instructions being
    424   // produced.
    425   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
    426     return false;
    427 
    428   SDLoc DL(ADDENode);
    429 
    430   // Initialize accumulator.
    431   SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
    432                                   ADDCNode->getOperand(1),
    433                                   ADDENode->getOperand(1));
    434 
    435   // create MipsMAdd(u) node
    436   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
    437 
    438   SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
    439                                  MultNode->getOperand(0),// Factor 0
    440                                  MultNode->getOperand(1),// Factor 1
    441                                  ACCIn);
    442 
    443   // replace uses of adde and addc here
    444   if (!SDValue(ADDCNode, 0).use_empty()) {
    445     SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
    446     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
    447   }
    448   if (!SDValue(ADDENode, 0).use_empty()) {
    449     SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
    450     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
    451   }
    452 
    453   return true;
    454 }
    455 
    456 // selectMSUB -
    457 // Transforms a subgraph in CurDAG if the following pattern is found:
    458 //  (addc Lo0, multLo), (sube Hi0, multHi),
    459 // where,
    460 //  multHi/Lo: product of multiplication
    461 //  Lo0: initial value of Lo register
    462 //  Hi0: initial value of Hi register
    463 // Return true if pattern matching was successful.
    464 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
    465   // SUBENode's second operand must be a flag output of an SUBC node in order
    466   // for the matching to be successful.
    467   SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
    468 
    469   if (SUBCNode->getOpcode() != ISD::SUBC)
    470     return false;
    471 
    472   SDValue MultHi = SUBENode->getOperand(1);
    473   SDValue MultLo = SUBCNode->getOperand(1);
    474   SDNode *MultNode = MultHi.getNode();
    475   unsigned MultOpc = MultHi.getOpcode();
    476 
    477   // MultHi and MultLo must be generated by the same node,
    478   if (MultLo.getNode() != MultNode)
    479     return false;
    480 
    481   // and it must be a multiplication.
    482   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
    483     return false;
    484 
    485   // MultLo amd MultHi must be the first and second output of MultNode
    486   // respectively.
    487   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
    488     return false;
    489 
    490   // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
    491   // of the values of MultNode, in which case MultNode will be removed in later
    492   // phases.
    493   // If there exist users other than SUBENode or SUBCNode, this function returns
    494   // here, which will result in MultNode being mapped to a single MULT
    495   // instruction node rather than a pair of MULT and MSUB instructions being
    496   // produced.
    497   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
    498     return false;
    499 
    500   SDLoc DL(SUBENode);
    501 
    502   // Initialize accumulator.
    503   SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
    504                                   SUBCNode->getOperand(0),
    505                                   SUBENode->getOperand(0));
    506 
    507   // create MipsSub(u) node
    508   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
    509 
    510   SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
    511                                  MultNode->getOperand(0),// Factor 0
    512                                  MultNode->getOperand(1),// Factor 1
    513                                  ACCIn);
    514 
    515   // replace uses of sube and subc here
    516   if (!SDValue(SUBCNode, 0).use_empty()) {
    517     SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
    518     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
    519   }
    520   if (!SDValue(SUBENode, 0).use_empty()) {
    521     SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
    522     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
    523   }
    524 
    525   return true;
    526 }
    527 
    528 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
    529                                   TargetLowering::DAGCombinerInfo &DCI,
    530                                   const MipsSubtarget &Subtarget) {
    531   if (DCI.isBeforeLegalize())
    532     return SDValue();
    533 
    534   if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
    535       N->getValueType(0) == MVT::i32 && selectMADD(N, &DAG))
    536     return SDValue(N, 0);
    537 
    538   return SDValue();
    539 }
    540 
    541 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
    542 //
    543 // Performs the following transformations:
    544 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
    545 //   sign/zero-extension is completely overwritten by the new one performed by
    546 //   the ISD::AND.
    547 // - Removes redundant zero extensions performed by an ISD::AND.
    548 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
    549                                  TargetLowering::DAGCombinerInfo &DCI,
    550                                  const MipsSubtarget &Subtarget) {
    551   if (!Subtarget.hasMSA())
    552     return SDValue();
    553 
    554   SDValue Op0 = N->getOperand(0);
    555   SDValue Op1 = N->getOperand(1);
    556   unsigned Op0Opcode = Op0->getOpcode();
    557 
    558   // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
    559   // where $d + 1 == 2^n and n == 32
    560   // or    $d + 1 == 2^n and n <= 32 and ZExt
    561   // -> (MipsVExtractZExt $a, $b, $c)
    562   if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
    563       Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
    564     ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
    565 
    566     if (!Mask)
    567       return SDValue();
    568 
    569     int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
    570 
    571     if (Log2IfPositive <= 0)
    572       return SDValue(); // Mask+1 is not a power of 2
    573 
    574     SDValue Op0Op2 = Op0->getOperand(2);
    575     EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
    576     unsigned ExtendTySize = ExtendTy.getSizeInBits();
    577     unsigned Log2 = Log2IfPositive;
    578 
    579     if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
    580         Log2 == ExtendTySize) {
    581       SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
    582       return DAG.getNode(MipsISD::VEXTRACT_ZEXT_ELT, SDLoc(Op0),
    583                          Op0->getVTList(),
    584                          makeArrayRef(Ops, Op0->getNumOperands()));
    585     }
    586   }
    587 
    588   return SDValue();
    589 }
    590 
    591 // Determine if the specified node is a constant vector splat.
    592 //
    593 // Returns true and sets Imm if:
    594 // * N is a ISD::BUILD_VECTOR representing a constant splat
    595 //
    596 // This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
    597 // differences are that it assumes the MSA has already been checked and the
    598 // arbitrary requirement for a maximum of 32-bit integers isn't applied (and
    599 // must not be in order for binsri.d to be selectable).
    600 static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
    601   BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
    602 
    603   if (!Node)
    604     return false;
    605 
    606   APInt SplatValue, SplatUndef;
    607   unsigned SplatBitSize;
    608   bool HasAnyUndefs;
    609 
    610   if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
    611                              8, !IsLittleEndian))
    612     return false;
    613 
    614   Imm = SplatValue;
    615 
    616   return true;
    617 }
    618 
    619 // Test whether the given node is an all-ones build_vector.
    620 static bool isVectorAllOnes(SDValue N) {
    621   // Look through bitcasts. Endianness doesn't matter because we are looking
    622   // for an all-ones value.
    623   if (N->getOpcode() == ISD::BITCAST)
    624     N = N->getOperand(0);
    625 
    626   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
    627 
    628   if (!BVN)
    629     return false;
    630 
    631   APInt SplatValue, SplatUndef;
    632   unsigned SplatBitSize;
    633   bool HasAnyUndefs;
    634 
    635   // Endianness doesn't matter in this context because we are looking for
    636   // an all-ones value.
    637   if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
    638     return SplatValue.isAllOnesValue();
    639 
    640   return false;
    641 }
    642 
    643 // Test whether N is the bitwise inverse of OfNode.
    644 static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
    645   if (N->getOpcode() != ISD::XOR)
    646     return false;
    647 
    648   if (isVectorAllOnes(N->getOperand(0)))
    649     return N->getOperand(1) == OfNode;
    650 
    651   if (isVectorAllOnes(N->getOperand(1)))
    652     return N->getOperand(0) == OfNode;
    653 
    654   return false;
    655 }
    656 
    657 // Perform combines where ISD::OR is the root node.
    658 //
    659 // Performs the following transformations:
    660 // - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
    661 //   where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
    662 //   vector type.
    663 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
    664                                 TargetLowering::DAGCombinerInfo &DCI,
    665                                 const MipsSubtarget &Subtarget) {
    666   if (!Subtarget.hasMSA())
    667     return SDValue();
    668 
    669   EVT Ty = N->getValueType(0);
    670 
    671   if (!Ty.is128BitVector())
    672     return SDValue();
    673 
    674   SDValue Op0 = N->getOperand(0);
    675   SDValue Op1 = N->getOperand(1);
    676 
    677   if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
    678     SDValue Op0Op0 = Op0->getOperand(0);
    679     SDValue Op0Op1 = Op0->getOperand(1);
    680     SDValue Op1Op0 = Op1->getOperand(0);
    681     SDValue Op1Op1 = Op1->getOperand(1);
    682     bool IsLittleEndian = !Subtarget.isLittle();
    683 
    684     SDValue IfSet, IfClr, Cond;
    685     bool IsConstantMask = false;
    686     APInt Mask, InvMask;
    687 
    688     // If Op0Op0 is an appropriate mask, try to find it's inverse in either
    689     // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
    690     // looking.
    691     // IfClr will be set if we find a valid match.
    692     if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
    693       Cond = Op0Op0;
    694       IfSet = Op0Op1;
    695 
    696       if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
    697           Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
    698         IfClr = Op1Op1;
    699       else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
    700                Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
    701         IfClr = Op1Op0;
    702 
    703       IsConstantMask = true;
    704     }
    705 
    706     // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
    707     // thing again using this mask.
    708     // IfClr will be set if we find a valid match.
    709     if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
    710       Cond = Op0Op1;
    711       IfSet = Op0Op0;
    712 
    713       if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
    714           Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
    715         IfClr = Op1Op1;
    716       else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
    717                Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
    718         IfClr = Op1Op0;
    719 
    720       IsConstantMask = true;
    721     }
    722 
    723     // If IfClr is not yet set, try looking for a non-constant match.
    724     // IfClr will be set if we find a valid match amongst the eight
    725     // possibilities.
    726     if (!IfClr.getNode()) {
    727       if (isBitwiseInverse(Op0Op0, Op1Op0)) {
    728         Cond = Op1Op0;
    729         IfSet = Op1Op1;
    730         IfClr = Op0Op1;
    731       } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
    732         Cond = Op1Op0;
    733         IfSet = Op1Op1;
    734         IfClr = Op0Op0;
    735       } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
    736         Cond = Op1Op1;
    737         IfSet = Op1Op0;
    738         IfClr = Op0Op1;
    739       } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
    740         Cond = Op1Op1;
    741         IfSet = Op1Op0;
    742         IfClr = Op0Op0;
    743       } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
    744         Cond = Op0Op0;
    745         IfSet = Op0Op1;
    746         IfClr = Op1Op1;
    747       } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
    748         Cond = Op0Op0;
    749         IfSet = Op0Op1;
    750         IfClr = Op1Op0;
    751       } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
    752         Cond = Op0Op1;
    753         IfSet = Op0Op0;
    754         IfClr = Op1Op1;
    755       } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
    756         Cond = Op0Op1;
    757         IfSet = Op0Op0;
    758         IfClr = Op1Op0;
    759       }
    760     }
    761 
    762     // At this point, IfClr will be set if we have a valid match.
    763     if (!IfClr.getNode())
    764       return SDValue();
    765 
    766     assert(Cond.getNode() && IfSet.getNode());
    767 
    768     // Fold degenerate cases.
    769     if (IsConstantMask) {
    770       if (Mask.isAllOnesValue())
    771         return IfSet;
    772       else if (Mask == 0)
    773         return IfClr;
    774     }
    775 
    776     // Transform the DAG into an equivalent VSELECT.
    777     return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
    778   }
    779 
    780   return SDValue();
    781 }
    782 
    783 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
    784                                   TargetLowering::DAGCombinerInfo &DCI,
    785                                   const MipsSubtarget &Subtarget) {
    786   if (DCI.isBeforeLegalize())
    787     return SDValue();
    788 
    789   if (Subtarget.hasMips32() && N->getValueType(0) == MVT::i32 &&
    790       selectMSUB(N, &DAG))
    791     return SDValue(N, 0);
    792 
    793   return SDValue();
    794 }
    795 
    796 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
    797                             EVT ShiftTy, SelectionDAG &DAG) {
    798   // Clear the upper (64 - VT.sizeInBits) bits.
    799   C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
    800 
    801   // Return 0.
    802   if (C == 0)
    803     return DAG.getConstant(0, DL, VT);
    804 
    805   // Return x.
    806   if (C == 1)
    807     return X;
    808 
    809   // If c is power of 2, return (shl x, log2(c)).
    810   if (isPowerOf2_64(C))
    811     return DAG.getNode(ISD::SHL, DL, VT, X,
    812                        DAG.getConstant(Log2_64(C), DL, ShiftTy));
    813 
    814   unsigned Log2Ceil = Log2_64_Ceil(C);
    815   uint64_t Floor = 1LL << Log2_64(C);
    816   uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
    817 
    818   // If |c - floor_c| <= |c - ceil_c|,
    819   // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
    820   // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
    821   if (C - Floor <= Ceil - C) {
    822     SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
    823     SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
    824     return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
    825   }
    826 
    827   // If |c - floor_c| > |c - ceil_c|,
    828   // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
    829   SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
    830   SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
    831   return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
    832 }
    833 
    834 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
    835                                  const TargetLowering::DAGCombinerInfo &DCI,
    836                                  const MipsSETargetLowering *TL) {
    837   EVT VT = N->getValueType(0);
    838 
    839   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
    840     if (!VT.isVector())
    841       return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N), VT,
    842                           TL->getScalarShiftAmountTy(DAG.getDataLayout(), VT),
    843                           DAG);
    844 
    845   return SDValue(N, 0);
    846 }
    847 
    848 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
    849                                       SelectionDAG &DAG,
    850                                       const MipsSubtarget &Subtarget) {
    851   // See if this is a vector splat immediate node.
    852   APInt SplatValue, SplatUndef;
    853   unsigned SplatBitSize;
    854   bool HasAnyUndefs;
    855   unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
    856   BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
    857 
    858   if (!Subtarget.hasDSP())
    859     return SDValue();
    860 
    861   if (!BV ||
    862       !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
    863                            EltSize, !Subtarget.isLittle()) ||
    864       (SplatBitSize != EltSize) ||
    865       (SplatValue.getZExtValue() >= EltSize))
    866     return SDValue();
    867 
    868   SDLoc DL(N);
    869   return DAG.getNode(Opc, DL, Ty, N->getOperand(0),
    870                      DAG.getConstant(SplatValue.getZExtValue(), DL, MVT::i32));
    871 }
    872 
    873 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
    874                                  TargetLowering::DAGCombinerInfo &DCI,
    875                                  const MipsSubtarget &Subtarget) {
    876   EVT Ty = N->getValueType(0);
    877 
    878   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
    879     return SDValue();
    880 
    881   return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
    882 }
    883 
    884 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
    885 // constant splats into MipsISD::SHRA_DSP for DSPr2.
    886 //
    887 // Performs the following transformations:
    888 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
    889 //   sign/zero-extension is completely overwritten by the new one performed by
    890 //   the ISD::SRA and ISD::SHL nodes.
    891 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
    892 //   sequence.
    893 //
    894 // See performDSPShiftCombine for more information about the transformation
    895 // used for DSPr2.
    896 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
    897                                  TargetLowering::DAGCombinerInfo &DCI,
    898                                  const MipsSubtarget &Subtarget) {
    899   EVT Ty = N->getValueType(0);
    900 
    901   if (Subtarget.hasMSA()) {
    902     SDValue Op0 = N->getOperand(0);
    903     SDValue Op1 = N->getOperand(1);
    904 
    905     // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
    906     // where $d + sizeof($c) == 32
    907     // or    $d + sizeof($c) <= 32 and SExt
    908     // -> (MipsVExtractSExt $a, $b, $c)
    909     if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
    910       SDValue Op0Op0 = Op0->getOperand(0);
    911       ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
    912 
    913       if (!ShAmount)
    914         return SDValue();
    915 
    916       if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
    917           Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
    918         return SDValue();
    919 
    920       EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
    921       unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
    922 
    923       if (TotalBits == 32 ||
    924           (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
    925            TotalBits <= 32)) {
    926         SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
    927                           Op0Op0->getOperand(2) };
    928         return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, SDLoc(Op0Op0),
    929                            Op0Op0->getVTList(),
    930                            makeArrayRef(Ops, Op0Op0->getNumOperands()));
    931       }
    932     }
    933   }
    934 
    935   if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget.hasDSPR2()))
    936     return SDValue();
    937 
    938   return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
    939 }
    940 
    941 
    942 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
    943                                  TargetLowering::DAGCombinerInfo &DCI,
    944                                  const MipsSubtarget &Subtarget) {
    945   EVT Ty = N->getValueType(0);
    946 
    947   if (((Ty != MVT::v2i16) || !Subtarget.hasDSPR2()) && (Ty != MVT::v4i8))
    948     return SDValue();
    949 
    950   return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
    951 }
    952 
    953 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
    954   bool IsV216 = (Ty == MVT::v2i16);
    955 
    956   switch (CC) {
    957   case ISD::SETEQ:
    958   case ISD::SETNE:  return true;
    959   case ISD::SETLT:
    960   case ISD::SETLE:
    961   case ISD::SETGT:
    962   case ISD::SETGE:  return IsV216;
    963   case ISD::SETULT:
    964   case ISD::SETULE:
    965   case ISD::SETUGT:
    966   case ISD::SETUGE: return !IsV216;
    967   default:          return false;
    968   }
    969 }
    970 
    971 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
    972   EVT Ty = N->getValueType(0);
    973 
    974   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
    975     return SDValue();
    976 
    977   if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
    978     return SDValue();
    979 
    980   return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
    981                      N->getOperand(1), N->getOperand(2));
    982 }
    983 
    984 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
    985   EVT Ty = N->getValueType(0);
    986 
    987   if (Ty.is128BitVector() && Ty.isInteger()) {
    988     // Try the following combines:
    989     //   (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
    990     //   (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
    991     //   (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
    992     //   (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
    993     //   (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
    994     //   (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
    995     //   (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
    996     //   (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
    997     // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
    998     // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
    999     // legalizer.
   1000     SDValue Op0 = N->getOperand(0);
   1001 
   1002     if (Op0->getOpcode() != ISD::SETCC)
   1003       return SDValue();
   1004 
   1005     ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
   1006     bool Signed;
   1007 
   1008     if (CondCode == ISD::SETLT  || CondCode == ISD::SETLE)
   1009       Signed = true;
   1010     else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
   1011       Signed = false;
   1012     else
   1013       return SDValue();
   1014 
   1015     SDValue Op1 = N->getOperand(1);
   1016     SDValue Op2 = N->getOperand(2);
   1017     SDValue Op0Op0 = Op0->getOperand(0);
   1018     SDValue Op0Op1 = Op0->getOperand(1);
   1019 
   1020     if (Op1 == Op0Op0 && Op2 == Op0Op1)
   1021       return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
   1022                          Ty, Op1, Op2);
   1023     else if (Op1 == Op0Op1 && Op2 == Op0Op0)
   1024       return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
   1025                          Ty, Op1, Op2);
   1026   } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
   1027     SDValue SetCC = N->getOperand(0);
   1028 
   1029     if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
   1030       return SDValue();
   1031 
   1032     return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
   1033                        SetCC.getOperand(0), SetCC.getOperand(1),
   1034                        N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
   1035   }
   1036 
   1037   return SDValue();
   1038 }
   1039 
   1040 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
   1041                                  const MipsSubtarget &Subtarget) {
   1042   EVT Ty = N->getValueType(0);
   1043 
   1044   if (Subtarget.hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
   1045     // Try the following combines:
   1046     //   (xor (or $a, $b), (build_vector allones))
   1047     //   (xor (or $a, $b), (bitcast (build_vector allones)))
   1048     SDValue Op0 = N->getOperand(0);
   1049     SDValue Op1 = N->getOperand(1);
   1050     SDValue NotOp;
   1051 
   1052     if (ISD::isBuildVectorAllOnes(Op0.getNode()))
   1053       NotOp = Op1;
   1054     else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
   1055       NotOp = Op0;
   1056     else
   1057       return SDValue();
   1058 
   1059     if (NotOp->getOpcode() == ISD::OR)
   1060       return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
   1061                          NotOp->getOperand(1));
   1062   }
   1063 
   1064   return SDValue();
   1065 }
   1066 
   1067 SDValue
   1068 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
   1069   SelectionDAG &DAG = DCI.DAG;
   1070   SDValue Val;
   1071 
   1072   switch (N->getOpcode()) {
   1073   case ISD::ADDE:
   1074     return performADDECombine(N, DAG, DCI, Subtarget);
   1075   case ISD::AND:
   1076     Val = performANDCombine(N, DAG, DCI, Subtarget);
   1077     break;
   1078   case ISD::OR:
   1079     Val = performORCombine(N, DAG, DCI, Subtarget);
   1080     break;
   1081   case ISD::SUBE:
   1082     return performSUBECombine(N, DAG, DCI, Subtarget);
   1083   case ISD::MUL:
   1084     return performMULCombine(N, DAG, DCI, this);
   1085   case ISD::SHL:
   1086     return performSHLCombine(N, DAG, DCI, Subtarget);
   1087   case ISD::SRA:
   1088     return performSRACombine(N, DAG, DCI, Subtarget);
   1089   case ISD::SRL:
   1090     return performSRLCombine(N, DAG, DCI, Subtarget);
   1091   case ISD::VSELECT:
   1092     return performVSELECTCombine(N, DAG);
   1093   case ISD::XOR:
   1094     Val = performXORCombine(N, DAG, Subtarget);
   1095     break;
   1096   case ISD::SETCC:
   1097     Val = performSETCCCombine(N, DAG);
   1098     break;
   1099   }
   1100 
   1101   if (Val.getNode()) {
   1102     DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
   1103           N->printrWithDepth(dbgs(), &DAG);
   1104           dbgs() << "\n=> \n";
   1105           Val.getNode()->printrWithDepth(dbgs(), &DAG);
   1106           dbgs() << "\n");
   1107     return Val;
   1108   }
   1109 
   1110   return MipsTargetLowering::PerformDAGCombine(N, DCI);
   1111 }
   1112 
   1113 MachineBasicBlock *
   1114 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
   1115                                                   MachineBasicBlock *BB) const {
   1116   switch (MI->getOpcode()) {
   1117   default:
   1118     return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
   1119   case Mips::BPOSGE32_PSEUDO:
   1120     return emitBPOSGE32(MI, BB);
   1121   case Mips::SNZ_B_PSEUDO:
   1122     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
   1123   case Mips::SNZ_H_PSEUDO:
   1124     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
   1125   case Mips::SNZ_W_PSEUDO:
   1126     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
   1127   case Mips::SNZ_D_PSEUDO:
   1128     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
   1129   case Mips::SNZ_V_PSEUDO:
   1130     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
   1131   case Mips::SZ_B_PSEUDO:
   1132     return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
   1133   case Mips::SZ_H_PSEUDO:
   1134     return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
   1135   case Mips::SZ_W_PSEUDO:
   1136     return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
   1137   case Mips::SZ_D_PSEUDO:
   1138     return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
   1139   case Mips::SZ_V_PSEUDO:
   1140     return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
   1141   case Mips::COPY_FW_PSEUDO:
   1142     return emitCOPY_FW(MI, BB);
   1143   case Mips::COPY_FD_PSEUDO:
   1144     return emitCOPY_FD(MI, BB);
   1145   case Mips::INSERT_FW_PSEUDO:
   1146     return emitINSERT_FW(MI, BB);
   1147   case Mips::INSERT_FD_PSEUDO:
   1148     return emitINSERT_FD(MI, BB);
   1149   case Mips::INSERT_B_VIDX_PSEUDO:
   1150   case Mips::INSERT_B_VIDX64_PSEUDO:
   1151     return emitINSERT_DF_VIDX(MI, BB, 1, false);
   1152   case Mips::INSERT_H_VIDX_PSEUDO:
   1153   case Mips::INSERT_H_VIDX64_PSEUDO:
   1154     return emitINSERT_DF_VIDX(MI, BB, 2, false);
   1155   case Mips::INSERT_W_VIDX_PSEUDO:
   1156   case Mips::INSERT_W_VIDX64_PSEUDO:
   1157     return emitINSERT_DF_VIDX(MI, BB, 4, false);
   1158   case Mips::INSERT_D_VIDX_PSEUDO:
   1159   case Mips::INSERT_D_VIDX64_PSEUDO:
   1160     return emitINSERT_DF_VIDX(MI, BB, 8, false);
   1161   case Mips::INSERT_FW_VIDX_PSEUDO:
   1162   case Mips::INSERT_FW_VIDX64_PSEUDO:
   1163     return emitINSERT_DF_VIDX(MI, BB, 4, true);
   1164   case Mips::INSERT_FD_VIDX_PSEUDO:
   1165   case Mips::INSERT_FD_VIDX64_PSEUDO:
   1166     return emitINSERT_DF_VIDX(MI, BB, 8, true);
   1167   case Mips::FILL_FW_PSEUDO:
   1168     return emitFILL_FW(MI, BB);
   1169   case Mips::FILL_FD_PSEUDO:
   1170     return emitFILL_FD(MI, BB);
   1171   case Mips::FEXP2_W_1_PSEUDO:
   1172     return emitFEXP2_W_1(MI, BB);
   1173   case Mips::FEXP2_D_1_PSEUDO:
   1174     return emitFEXP2_D_1(MI, BB);
   1175   }
   1176 }
   1177 
   1178 bool MipsSETargetLowering::isEligibleForTailCallOptimization(
   1179     const CCState &CCInfo, unsigned NextStackOffset,
   1180     const MipsFunctionInfo &FI) const {
   1181   if (!EnableMipsTailCalls)
   1182     return false;
   1183 
   1184   // Exception has to be cleared with eret.
   1185   if (FI.isISR())
   1186     return false;
   1187 
   1188   // Return false if either the callee or caller has a byval argument.
   1189   if (CCInfo.getInRegsParamsCount() > 0 || FI.hasByvalArg())
   1190     return false;
   1191 
   1192   // Return true if the callee's argument area is no larger than the
   1193   // caller's.
   1194   return NextStackOffset <= FI.getIncomingArgSize();
   1195 }
   1196 
   1197 void MipsSETargetLowering::
   1198 getOpndList(SmallVectorImpl<SDValue> &Ops,
   1199             std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
   1200             bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
   1201             bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
   1202             SDValue Chain) const {
   1203   Ops.push_back(Callee);
   1204   MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
   1205                                   InternalLinkage, IsCallReloc, CLI, Callee,
   1206                                   Chain);
   1207 }
   1208 
   1209 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
   1210   LoadSDNode &Nd = *cast<LoadSDNode>(Op);
   1211 
   1212   if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
   1213     return MipsTargetLowering::lowerLOAD(Op, DAG);
   1214 
   1215   // Replace a double precision load with two i32 loads and a buildpair64.
   1216   SDLoc DL(Op);
   1217   SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
   1218   EVT PtrVT = Ptr.getValueType();
   1219 
   1220   // i32 load from lower address.
   1221   SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
   1222                            MachinePointerInfo(), Nd.isVolatile(),
   1223                            Nd.isNonTemporal(), Nd.isInvariant(),
   1224                            Nd.getAlignment());
   1225 
   1226   // i32 load from higher address.
   1227   Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
   1228   SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
   1229                            MachinePointerInfo(), Nd.isVolatile(),
   1230                            Nd.isNonTemporal(), Nd.isInvariant(),
   1231                            std::min(Nd.getAlignment(), 4U));
   1232 
   1233   if (!Subtarget.isLittle())
   1234     std::swap(Lo, Hi);
   1235 
   1236   SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
   1237   SDValue Ops[2] = {BP, Hi.getValue(1)};
   1238   return DAG.getMergeValues(Ops, DL);
   1239 }
   1240 
   1241 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
   1242   StoreSDNode &Nd = *cast<StoreSDNode>(Op);
   1243 
   1244   if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
   1245     return MipsTargetLowering::lowerSTORE(Op, DAG);
   1246 
   1247   // Replace a double precision store with two extractelement64s and i32 stores.
   1248   SDLoc DL(Op);
   1249   SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
   1250   EVT PtrVT = Ptr.getValueType();
   1251   SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
   1252                            Val, DAG.getConstant(0, DL, MVT::i32));
   1253   SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
   1254                            Val, DAG.getConstant(1, DL, MVT::i32));
   1255 
   1256   if (!Subtarget.isLittle())
   1257     std::swap(Lo, Hi);
   1258 
   1259   // i32 store to lower address.
   1260   Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
   1261                        Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
   1262                        Nd.getAAInfo());
   1263 
   1264   // i32 store to higher address.
   1265   Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, DL, PtrVT));
   1266   return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
   1267                       Nd.isVolatile(), Nd.isNonTemporal(),
   1268                       std::min(Nd.getAlignment(), 4U), Nd.getAAInfo());
   1269 }
   1270 
   1271 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
   1272                                           bool HasLo, bool HasHi,
   1273                                           SelectionDAG &DAG) const {
   1274   // MIPS32r6/MIPS64r6 removed accumulator based multiplies.
   1275   assert(!Subtarget.hasMips32r6());
   1276 
   1277   EVT Ty = Op.getOperand(0).getValueType();
   1278   SDLoc DL(Op);
   1279   SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
   1280                              Op.getOperand(0), Op.getOperand(1));
   1281   SDValue Lo, Hi;
   1282 
   1283   if (HasLo)
   1284     Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
   1285   if (HasHi)
   1286     Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
   1287 
   1288   if (!HasLo || !HasHi)
   1289     return HasLo ? Lo : Hi;
   1290 
   1291   SDValue Vals[] = { Lo, Hi };
   1292   return DAG.getMergeValues(Vals, DL);
   1293 }
   1294 
   1295 
   1296 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
   1297   SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
   1298                              DAG.getConstant(0, DL, MVT::i32));
   1299   SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
   1300                              DAG.getConstant(1, DL, MVT::i32));
   1301   return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
   1302 }
   1303 
   1304 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
   1305   SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
   1306   SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
   1307   return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
   1308 }
   1309 
   1310 // This function expands mips intrinsic nodes which have 64-bit input operands
   1311 // or output values.
   1312 //
   1313 // out64 = intrinsic-node in64
   1314 // =>
   1315 // lo = copy (extract-element (in64, 0))
   1316 // hi = copy (extract-element (in64, 1))
   1317 // mips-specific-node
   1318 // v0 = copy lo
   1319 // v1 = copy hi
   1320 // out64 = merge-values (v0, v1)
   1321 //
   1322 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
   1323   SDLoc DL(Op);
   1324   bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
   1325   SmallVector<SDValue, 3> Ops;
   1326   unsigned OpNo = 0;
   1327 
   1328   // See if Op has a chain input.
   1329   if (HasChainIn)
   1330     Ops.push_back(Op->getOperand(OpNo++));
   1331 
   1332   // The next operand is the intrinsic opcode.
   1333   assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
   1334 
   1335   // See if the next operand has type i64.
   1336   SDValue Opnd = Op->getOperand(++OpNo), In64;
   1337 
   1338   if (Opnd.getValueType() == MVT::i64)
   1339     In64 = initAccumulator(Opnd, DL, DAG);
   1340   else
   1341     Ops.push_back(Opnd);
   1342 
   1343   // Push the remaining operands.
   1344   for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
   1345     Ops.push_back(Op->getOperand(OpNo));
   1346 
   1347   // Add In64 to the end of the list.
   1348   if (In64.getNode())
   1349     Ops.push_back(In64);
   1350 
   1351   // Scan output.
   1352   SmallVector<EVT, 2> ResTys;
   1353 
   1354   for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
   1355        I != E; ++I)
   1356     ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
   1357 
   1358   // Create node.
   1359   SDValue Val = DAG.getNode(Opc, DL, ResTys, Ops);
   1360   SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
   1361 
   1362   if (!HasChainIn)
   1363     return Out;
   1364 
   1365   assert(Val->getValueType(1) == MVT::Other);
   1366   SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
   1367   return DAG.getMergeValues(Vals, DL);
   1368 }
   1369 
   1370 // Lower an MSA copy intrinsic into the specified SelectionDAG node
   1371 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
   1372   SDLoc DL(Op);
   1373   SDValue Vec = Op->getOperand(1);
   1374   SDValue Idx = Op->getOperand(2);
   1375   EVT ResTy = Op->getValueType(0);
   1376   EVT EltTy = Vec->getValueType(0).getVectorElementType();
   1377 
   1378   SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
   1379                                DAG.getValueType(EltTy));
   1380 
   1381   return Result;
   1382 }
   1383 
   1384 static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
   1385   EVT ResVecTy = Op->getValueType(0);
   1386   EVT ViaVecTy = ResVecTy;
   1387   SDLoc DL(Op);
   1388 
   1389   // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
   1390   // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
   1391   // lanes.
   1392   SDValue LaneA;
   1393   SDValue LaneB = Op->getOperand(2);
   1394 
   1395   if (ResVecTy == MVT::v2i64) {
   1396     LaneA = DAG.getConstant(0, DL, MVT::i32);
   1397     ViaVecTy = MVT::v4i32;
   1398   } else
   1399     LaneA = LaneB;
   1400 
   1401   SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
   1402                       LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
   1403 
   1404   SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
   1405                        makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
   1406 
   1407   if (ViaVecTy != ResVecTy)
   1408     Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
   1409 
   1410   return Result;
   1411 }
   1412 
   1413 static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
   1414   return DAG.getConstant(Op->getConstantOperandVal(ImmOp), SDLoc(Op),
   1415                          Op->getValueType(0));
   1416 }
   1417 
   1418 static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
   1419                                    bool BigEndian, SelectionDAG &DAG) {
   1420   EVT ViaVecTy = VecTy;
   1421   SDValue SplatValueA = SplatValue;
   1422   SDValue SplatValueB = SplatValue;
   1423   SDLoc DL(SplatValue);
   1424 
   1425   if (VecTy == MVT::v2i64) {
   1426     // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
   1427     ViaVecTy = MVT::v4i32;
   1428 
   1429     SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
   1430     SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
   1431                               DAG.getConstant(32, DL, MVT::i32));
   1432     SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
   1433   }
   1434 
   1435   // We currently hold the parts in little endian order. Swap them if
   1436   // necessary.
   1437   if (BigEndian)
   1438     std::swap(SplatValueA, SplatValueB);
   1439 
   1440   SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
   1441                       SplatValueA, SplatValueB, SplatValueA, SplatValueB,
   1442                       SplatValueA, SplatValueB, SplatValueA, SplatValueB,
   1443                       SplatValueA, SplatValueB, SplatValueA, SplatValueB };
   1444 
   1445   SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy,
   1446                        makeArrayRef(Ops, ViaVecTy.getVectorNumElements()));
   1447 
   1448   if (VecTy != ViaVecTy)
   1449     Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
   1450 
   1451   return Result;
   1452 }
   1453 
   1454 static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
   1455                                         unsigned Opc, SDValue Imm,
   1456                                         bool BigEndian) {
   1457   EVT VecTy = Op->getValueType(0);
   1458   SDValue Exp2Imm;
   1459   SDLoc DL(Op);
   1460 
   1461   // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
   1462   // here for now.
   1463   if (VecTy == MVT::v2i64) {
   1464     if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
   1465       APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
   1466 
   1467       SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), DL,
   1468                                            MVT::i32);
   1469       SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), DL, MVT::i32);
   1470 
   1471       if (BigEndian)
   1472         std::swap(BitImmLoOp, BitImmHiOp);
   1473 
   1474       Exp2Imm =
   1475           DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
   1476                       DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
   1477                                   BitImmHiOp, BitImmLoOp, BitImmHiOp));
   1478     }
   1479   }
   1480 
   1481   if (!Exp2Imm.getNode()) {
   1482     // We couldnt constant fold, do a vector shift instead
   1483 
   1484     // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
   1485     // only values 0-63 are valid.
   1486     if (VecTy == MVT::v2i64)
   1487       Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
   1488 
   1489     Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
   1490 
   1491     Exp2Imm = DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, DL, VecTy),
   1492                           Exp2Imm);
   1493   }
   1494 
   1495   return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
   1496 }
   1497 
   1498 static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
   1499   EVT ResTy = Op->getValueType(0);
   1500   SDLoc DL(Op);
   1501   SDValue One = DAG.getConstant(1, DL, ResTy);
   1502   SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
   1503 
   1504   return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
   1505                      DAG.getNOT(DL, Bit, ResTy));
   1506 }
   1507 
   1508 static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
   1509   SDLoc DL(Op);
   1510   EVT ResTy = Op->getValueType(0);
   1511   APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
   1512                  << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
   1513   SDValue BitMask = DAG.getConstant(~BitImm, DL, ResTy);
   1514 
   1515   return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
   1516 }
   1517 
   1518 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
   1519                                                       SelectionDAG &DAG) const {
   1520   SDLoc DL(Op);
   1521 
   1522   switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
   1523   default:
   1524     return SDValue();
   1525   case Intrinsic::mips_shilo:
   1526     return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
   1527   case Intrinsic::mips_dpau_h_qbl:
   1528     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
   1529   case Intrinsic::mips_dpau_h_qbr:
   1530     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
   1531   case Intrinsic::mips_dpsu_h_qbl:
   1532     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
   1533   case Intrinsic::mips_dpsu_h_qbr:
   1534     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
   1535   case Intrinsic::mips_dpa_w_ph:
   1536     return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
   1537   case Intrinsic::mips_dps_w_ph:
   1538     return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
   1539   case Intrinsic::mips_dpax_w_ph:
   1540     return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
   1541   case Intrinsic::mips_dpsx_w_ph:
   1542     return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
   1543   case Intrinsic::mips_mulsa_w_ph:
   1544     return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
   1545   case Intrinsic::mips_mult:
   1546     return lowerDSPIntr(Op, DAG, MipsISD::Mult);
   1547   case Intrinsic::mips_multu:
   1548     return lowerDSPIntr(Op, DAG, MipsISD::Multu);
   1549   case Intrinsic::mips_madd:
   1550     return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
   1551   case Intrinsic::mips_maddu:
   1552     return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
   1553   case Intrinsic::mips_msub:
   1554     return lowerDSPIntr(Op, DAG, MipsISD::MSub);
   1555   case Intrinsic::mips_msubu:
   1556     return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
   1557   case Intrinsic::mips_addv_b:
   1558   case Intrinsic::mips_addv_h:
   1559   case Intrinsic::mips_addv_w:
   1560   case Intrinsic::mips_addv_d:
   1561     return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
   1562                        Op->getOperand(2));
   1563   case Intrinsic::mips_addvi_b:
   1564   case Intrinsic::mips_addvi_h:
   1565   case Intrinsic::mips_addvi_w:
   1566   case Intrinsic::mips_addvi_d:
   1567     return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
   1568                        lowerMSASplatImm(Op, 2, DAG));
   1569   case Intrinsic::mips_and_v:
   1570     return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
   1571                        Op->getOperand(2));
   1572   case Intrinsic::mips_andi_b:
   1573     return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
   1574                        lowerMSASplatImm(Op, 2, DAG));
   1575   case Intrinsic::mips_bclr_b:
   1576   case Intrinsic::mips_bclr_h:
   1577   case Intrinsic::mips_bclr_w:
   1578   case Intrinsic::mips_bclr_d:
   1579     return lowerMSABitClear(Op, DAG);
   1580   case Intrinsic::mips_bclri_b:
   1581   case Intrinsic::mips_bclri_h:
   1582   case Intrinsic::mips_bclri_w:
   1583   case Intrinsic::mips_bclri_d:
   1584     return lowerMSABitClearImm(Op, DAG);
   1585   case Intrinsic::mips_binsli_b:
   1586   case Intrinsic::mips_binsli_h:
   1587   case Intrinsic::mips_binsli_w:
   1588   case Intrinsic::mips_binsli_d: {
   1589     // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
   1590     EVT VecTy = Op->getValueType(0);
   1591     EVT EltTy = VecTy.getVectorElementType();
   1592     APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
   1593                                        Op->getConstantOperandVal(3));
   1594     return DAG.getNode(ISD::VSELECT, DL, VecTy,
   1595                        DAG.getConstant(Mask, DL, VecTy, true),
   1596                        Op->getOperand(2), Op->getOperand(1));
   1597   }
   1598   case Intrinsic::mips_binsri_b:
   1599   case Intrinsic::mips_binsri_h:
   1600   case Intrinsic::mips_binsri_w:
   1601   case Intrinsic::mips_binsri_d: {
   1602     // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
   1603     EVT VecTy = Op->getValueType(0);
   1604     EVT EltTy = VecTy.getVectorElementType();
   1605     APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
   1606                                       Op->getConstantOperandVal(3));
   1607     return DAG.getNode(ISD::VSELECT, DL, VecTy,
   1608                        DAG.getConstant(Mask, DL, VecTy, true),
   1609                        Op->getOperand(2), Op->getOperand(1));
   1610   }
   1611   case Intrinsic::mips_bmnz_v:
   1612     return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
   1613                        Op->getOperand(2), Op->getOperand(1));
   1614   case Intrinsic::mips_bmnzi_b:
   1615     return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
   1616                        lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
   1617                        Op->getOperand(1));
   1618   case Intrinsic::mips_bmz_v:
   1619     return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
   1620                        Op->getOperand(1), Op->getOperand(2));
   1621   case Intrinsic::mips_bmzi_b:
   1622     return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
   1623                        lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
   1624                        Op->getOperand(2));
   1625   case Intrinsic::mips_bneg_b:
   1626   case Intrinsic::mips_bneg_h:
   1627   case Intrinsic::mips_bneg_w:
   1628   case Intrinsic::mips_bneg_d: {
   1629     EVT VecTy = Op->getValueType(0);
   1630     SDValue One = DAG.getConstant(1, DL, VecTy);
   1631 
   1632     return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
   1633                        DAG.getNode(ISD::SHL, DL, VecTy, One,
   1634                                    Op->getOperand(2)));
   1635   }
   1636   case Intrinsic::mips_bnegi_b:
   1637   case Intrinsic::mips_bnegi_h:
   1638   case Intrinsic::mips_bnegi_w:
   1639   case Intrinsic::mips_bnegi_d:
   1640     return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
   1641                                     !Subtarget.isLittle());
   1642   case Intrinsic::mips_bnz_b:
   1643   case Intrinsic::mips_bnz_h:
   1644   case Intrinsic::mips_bnz_w:
   1645   case Intrinsic::mips_bnz_d:
   1646     return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
   1647                        Op->getOperand(1));
   1648   case Intrinsic::mips_bnz_v:
   1649     return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
   1650                        Op->getOperand(1));
   1651   case Intrinsic::mips_bsel_v:
   1652     // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
   1653     return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
   1654                        Op->getOperand(1), Op->getOperand(3),
   1655                        Op->getOperand(2));
   1656   case Intrinsic::mips_bseli_b:
   1657     // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
   1658     return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
   1659                        Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
   1660                        Op->getOperand(2));
   1661   case Intrinsic::mips_bset_b:
   1662   case Intrinsic::mips_bset_h:
   1663   case Intrinsic::mips_bset_w:
   1664   case Intrinsic::mips_bset_d: {
   1665     EVT VecTy = Op->getValueType(0);
   1666     SDValue One = DAG.getConstant(1, DL, VecTy);
   1667 
   1668     return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
   1669                        DAG.getNode(ISD::SHL, DL, VecTy, One,
   1670                                    Op->getOperand(2)));
   1671   }
   1672   case Intrinsic::mips_bseti_b:
   1673   case Intrinsic::mips_bseti_h:
   1674   case Intrinsic::mips_bseti_w:
   1675   case Intrinsic::mips_bseti_d:
   1676     return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
   1677                                     !Subtarget.isLittle());
   1678   case Intrinsic::mips_bz_b:
   1679   case Intrinsic::mips_bz_h:
   1680   case Intrinsic::mips_bz_w:
   1681   case Intrinsic::mips_bz_d:
   1682     return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
   1683                        Op->getOperand(1));
   1684   case Intrinsic::mips_bz_v:
   1685     return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
   1686                        Op->getOperand(1));
   1687   case Intrinsic::mips_ceq_b:
   1688   case Intrinsic::mips_ceq_h:
   1689   case Intrinsic::mips_ceq_w:
   1690   case Intrinsic::mips_ceq_d:
   1691     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1692                         Op->getOperand(2), ISD::SETEQ);
   1693   case Intrinsic::mips_ceqi_b:
   1694   case Intrinsic::mips_ceqi_h:
   1695   case Intrinsic::mips_ceqi_w:
   1696   case Intrinsic::mips_ceqi_d:
   1697     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1698                         lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
   1699   case Intrinsic::mips_cle_s_b:
   1700   case Intrinsic::mips_cle_s_h:
   1701   case Intrinsic::mips_cle_s_w:
   1702   case Intrinsic::mips_cle_s_d:
   1703     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1704                         Op->getOperand(2), ISD::SETLE);
   1705   case Intrinsic::mips_clei_s_b:
   1706   case Intrinsic::mips_clei_s_h:
   1707   case Intrinsic::mips_clei_s_w:
   1708   case Intrinsic::mips_clei_s_d:
   1709     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1710                         lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
   1711   case Intrinsic::mips_cle_u_b:
   1712   case Intrinsic::mips_cle_u_h:
   1713   case Intrinsic::mips_cle_u_w:
   1714   case Intrinsic::mips_cle_u_d:
   1715     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1716                         Op->getOperand(2), ISD::SETULE);
   1717   case Intrinsic::mips_clei_u_b:
   1718   case Intrinsic::mips_clei_u_h:
   1719   case Intrinsic::mips_clei_u_w:
   1720   case Intrinsic::mips_clei_u_d:
   1721     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1722                         lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
   1723   case Intrinsic::mips_clt_s_b:
   1724   case Intrinsic::mips_clt_s_h:
   1725   case Intrinsic::mips_clt_s_w:
   1726   case Intrinsic::mips_clt_s_d:
   1727     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1728                         Op->getOperand(2), ISD::SETLT);
   1729   case Intrinsic::mips_clti_s_b:
   1730   case Intrinsic::mips_clti_s_h:
   1731   case Intrinsic::mips_clti_s_w:
   1732   case Intrinsic::mips_clti_s_d:
   1733     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1734                         lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
   1735   case Intrinsic::mips_clt_u_b:
   1736   case Intrinsic::mips_clt_u_h:
   1737   case Intrinsic::mips_clt_u_w:
   1738   case Intrinsic::mips_clt_u_d:
   1739     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1740                         Op->getOperand(2), ISD::SETULT);
   1741   case Intrinsic::mips_clti_u_b:
   1742   case Intrinsic::mips_clti_u_h:
   1743   case Intrinsic::mips_clti_u_w:
   1744   case Intrinsic::mips_clti_u_d:
   1745     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1746                         lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
   1747   case Intrinsic::mips_copy_s_b:
   1748   case Intrinsic::mips_copy_s_h:
   1749   case Intrinsic::mips_copy_s_w:
   1750     return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
   1751   case Intrinsic::mips_copy_s_d:
   1752     if (Subtarget.hasMips64())
   1753       // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
   1754       return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
   1755     else {
   1756       // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
   1757       // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
   1758       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
   1759                          Op->getValueType(0), Op->getOperand(1),
   1760                          Op->getOperand(2));
   1761     }
   1762   case Intrinsic::mips_copy_u_b:
   1763   case Intrinsic::mips_copy_u_h:
   1764   case Intrinsic::mips_copy_u_w:
   1765     return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
   1766   case Intrinsic::mips_copy_u_d:
   1767     if (Subtarget.hasMips64())
   1768       // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
   1769       return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
   1770     else {
   1771       // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
   1772       // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
   1773       // Note: When i64 is illegal, this results in copy_s.w instructions
   1774       // instead of copy_u.w instructions. This makes no difference to the
   1775       // behaviour since i64 is only illegal when the register file is 32-bit.
   1776       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
   1777                          Op->getValueType(0), Op->getOperand(1),
   1778                          Op->getOperand(2));
   1779     }
   1780   case Intrinsic::mips_div_s_b:
   1781   case Intrinsic::mips_div_s_h:
   1782   case Intrinsic::mips_div_s_w:
   1783   case Intrinsic::mips_div_s_d:
   1784     return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
   1785                        Op->getOperand(2));
   1786   case Intrinsic::mips_div_u_b:
   1787   case Intrinsic::mips_div_u_h:
   1788   case Intrinsic::mips_div_u_w:
   1789   case Intrinsic::mips_div_u_d:
   1790     return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
   1791                        Op->getOperand(2));
   1792   case Intrinsic::mips_fadd_w:
   1793   case Intrinsic::mips_fadd_d: {
   1794     // TODO: If intrinsics have fast-math-flags, propagate them.
   1795     return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
   1796                        Op->getOperand(2));
   1797   }
   1798   // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
   1799   case Intrinsic::mips_fceq_w:
   1800   case Intrinsic::mips_fceq_d:
   1801     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1802                         Op->getOperand(2), ISD::SETOEQ);
   1803   case Intrinsic::mips_fcle_w:
   1804   case Intrinsic::mips_fcle_d:
   1805     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1806                         Op->getOperand(2), ISD::SETOLE);
   1807   case Intrinsic::mips_fclt_w:
   1808   case Intrinsic::mips_fclt_d:
   1809     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1810                         Op->getOperand(2), ISD::SETOLT);
   1811   case Intrinsic::mips_fcne_w:
   1812   case Intrinsic::mips_fcne_d:
   1813     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1814                         Op->getOperand(2), ISD::SETONE);
   1815   case Intrinsic::mips_fcor_w:
   1816   case Intrinsic::mips_fcor_d:
   1817     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1818                         Op->getOperand(2), ISD::SETO);
   1819   case Intrinsic::mips_fcueq_w:
   1820   case Intrinsic::mips_fcueq_d:
   1821     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1822                         Op->getOperand(2), ISD::SETUEQ);
   1823   case Intrinsic::mips_fcule_w:
   1824   case Intrinsic::mips_fcule_d:
   1825     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1826                         Op->getOperand(2), ISD::SETULE);
   1827   case Intrinsic::mips_fcult_w:
   1828   case Intrinsic::mips_fcult_d:
   1829     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1830                         Op->getOperand(2), ISD::SETULT);
   1831   case Intrinsic::mips_fcun_w:
   1832   case Intrinsic::mips_fcun_d:
   1833     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1834                         Op->getOperand(2), ISD::SETUO);
   1835   case Intrinsic::mips_fcune_w:
   1836   case Intrinsic::mips_fcune_d:
   1837     return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
   1838                         Op->getOperand(2), ISD::SETUNE);
   1839   case Intrinsic::mips_fdiv_w:
   1840   case Intrinsic::mips_fdiv_d: {
   1841     // TODO: If intrinsics have fast-math-flags, propagate them.
   1842     return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
   1843                        Op->getOperand(2));
   1844   }
   1845   case Intrinsic::mips_ffint_u_w:
   1846   case Intrinsic::mips_ffint_u_d:
   1847     return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
   1848                        Op->getOperand(1));
   1849   case Intrinsic::mips_ffint_s_w:
   1850   case Intrinsic::mips_ffint_s_d:
   1851     return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
   1852                        Op->getOperand(1));
   1853   case Intrinsic::mips_fill_b:
   1854   case Intrinsic::mips_fill_h:
   1855   case Intrinsic::mips_fill_w:
   1856   case Intrinsic::mips_fill_d: {
   1857     EVT ResTy = Op->getValueType(0);
   1858     SmallVector<SDValue, 16> Ops(ResTy.getVectorNumElements(),
   1859                                  Op->getOperand(1));
   1860 
   1861     // If ResTy is v2i64 then the type legalizer will break this node down into
   1862     // an equivalent v4i32.
   1863     return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, Ops);
   1864   }
   1865   case Intrinsic::mips_fexp2_w:
   1866   case Intrinsic::mips_fexp2_d: {
   1867     // TODO: If intrinsics have fast-math-flags, propagate them.
   1868     EVT ResTy = Op->getValueType(0);
   1869     return DAG.getNode(
   1870         ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
   1871         DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
   1872   }
   1873   case Intrinsic::mips_flog2_w:
   1874   case Intrinsic::mips_flog2_d:
   1875     return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
   1876   case Intrinsic::mips_fmadd_w:
   1877   case Intrinsic::mips_fmadd_d:
   1878     return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
   1879                        Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
   1880   case Intrinsic::mips_fmul_w:
   1881   case Intrinsic::mips_fmul_d: {
   1882     // TODO: If intrinsics have fast-math-flags, propagate them.
   1883     return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
   1884                        Op->getOperand(2));
   1885   }
   1886   case Intrinsic::mips_fmsub_w:
   1887   case Intrinsic::mips_fmsub_d: {
   1888     // TODO: If intrinsics have fast-math-flags, propagate them.
   1889     EVT ResTy = Op->getValueType(0);
   1890     return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
   1891                        DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
   1892                                    Op->getOperand(2), Op->getOperand(3)));
   1893   }
   1894   case Intrinsic::mips_frint_w:
   1895   case Intrinsic::mips_frint_d:
   1896     return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
   1897   case Intrinsic::mips_fsqrt_w:
   1898   case Intrinsic::mips_fsqrt_d:
   1899     return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
   1900   case Intrinsic::mips_fsub_w:
   1901   case Intrinsic::mips_fsub_d: {
   1902     // TODO: If intrinsics have fast-math-flags, propagate them.
   1903     return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
   1904                        Op->getOperand(2));
   1905   }
   1906   case Intrinsic::mips_ftrunc_u_w:
   1907   case Intrinsic::mips_ftrunc_u_d:
   1908     return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
   1909                        Op->getOperand(1));
   1910   case Intrinsic::mips_ftrunc_s_w:
   1911   case Intrinsic::mips_ftrunc_s_d:
   1912     return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
   1913                        Op->getOperand(1));
   1914   case Intrinsic::mips_ilvev_b:
   1915   case Intrinsic::mips_ilvev_h:
   1916   case Intrinsic::mips_ilvev_w:
   1917   case Intrinsic::mips_ilvev_d:
   1918     return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
   1919                        Op->getOperand(1), Op->getOperand(2));
   1920   case Intrinsic::mips_ilvl_b:
   1921   case Intrinsic::mips_ilvl_h:
   1922   case Intrinsic::mips_ilvl_w:
   1923   case Intrinsic::mips_ilvl_d:
   1924     return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
   1925                        Op->getOperand(1), Op->getOperand(2));
   1926   case Intrinsic::mips_ilvod_b:
   1927   case Intrinsic::mips_ilvod_h:
   1928   case Intrinsic::mips_ilvod_w:
   1929   case Intrinsic::mips_ilvod_d:
   1930     return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
   1931                        Op->getOperand(1), Op->getOperand(2));
   1932   case Intrinsic::mips_ilvr_b:
   1933   case Intrinsic::mips_ilvr_h:
   1934   case Intrinsic::mips_ilvr_w:
   1935   case Intrinsic::mips_ilvr_d:
   1936     return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
   1937                        Op->getOperand(1), Op->getOperand(2));
   1938   case Intrinsic::mips_insert_b:
   1939   case Intrinsic::mips_insert_h:
   1940   case Intrinsic::mips_insert_w:
   1941   case Intrinsic::mips_insert_d:
   1942     return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
   1943                        Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
   1944   case Intrinsic::mips_insve_b:
   1945   case Intrinsic::mips_insve_h:
   1946   case Intrinsic::mips_insve_w:
   1947   case Intrinsic::mips_insve_d:
   1948     return DAG.getNode(MipsISD::INSVE, DL, Op->getValueType(0),
   1949                        Op->getOperand(1), Op->getOperand(2), Op->getOperand(3),
   1950                        DAG.getConstant(0, DL, MVT::i32));
   1951   case Intrinsic::mips_ldi_b:
   1952   case Intrinsic::mips_ldi_h:
   1953   case Intrinsic::mips_ldi_w:
   1954   case Intrinsic::mips_ldi_d:
   1955     return lowerMSASplatImm(Op, 1, DAG);
   1956   case Intrinsic::mips_lsa:
   1957   case Intrinsic::mips_dlsa: {
   1958     EVT ResTy = Op->getValueType(0);
   1959     return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
   1960                        DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
   1961                                    Op->getOperand(2), Op->getOperand(3)));
   1962   }
   1963   case Intrinsic::mips_maddv_b:
   1964   case Intrinsic::mips_maddv_h:
   1965   case Intrinsic::mips_maddv_w:
   1966   case Intrinsic::mips_maddv_d: {
   1967     EVT ResTy = Op->getValueType(0);
   1968     return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
   1969                        DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
   1970                                    Op->getOperand(2), Op->getOperand(3)));
   1971   }
   1972   case Intrinsic::mips_max_s_b:
   1973   case Intrinsic::mips_max_s_h:
   1974   case Intrinsic::mips_max_s_w:
   1975   case Intrinsic::mips_max_s_d:
   1976     return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
   1977                        Op->getOperand(1), Op->getOperand(2));
   1978   case Intrinsic::mips_max_u_b:
   1979   case Intrinsic::mips_max_u_h:
   1980   case Intrinsic::mips_max_u_w:
   1981   case Intrinsic::mips_max_u_d:
   1982     return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
   1983                        Op->getOperand(1), Op->getOperand(2));
   1984   case Intrinsic::mips_maxi_s_b:
   1985   case Intrinsic::mips_maxi_s_h:
   1986   case Intrinsic::mips_maxi_s_w:
   1987   case Intrinsic::mips_maxi_s_d:
   1988     return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
   1989                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   1990   case Intrinsic::mips_maxi_u_b:
   1991   case Intrinsic::mips_maxi_u_h:
   1992   case Intrinsic::mips_maxi_u_w:
   1993   case Intrinsic::mips_maxi_u_d:
   1994     return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
   1995                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   1996   case Intrinsic::mips_min_s_b:
   1997   case Intrinsic::mips_min_s_h:
   1998   case Intrinsic::mips_min_s_w:
   1999   case Intrinsic::mips_min_s_d:
   2000     return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
   2001                        Op->getOperand(1), Op->getOperand(2));
   2002   case Intrinsic::mips_min_u_b:
   2003   case Intrinsic::mips_min_u_h:
   2004   case Intrinsic::mips_min_u_w:
   2005   case Intrinsic::mips_min_u_d:
   2006     return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
   2007                        Op->getOperand(1), Op->getOperand(2));
   2008   case Intrinsic::mips_mini_s_b:
   2009   case Intrinsic::mips_mini_s_h:
   2010   case Intrinsic::mips_mini_s_w:
   2011   case Intrinsic::mips_mini_s_d:
   2012     return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
   2013                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2014   case Intrinsic::mips_mini_u_b:
   2015   case Intrinsic::mips_mini_u_h:
   2016   case Intrinsic::mips_mini_u_w:
   2017   case Intrinsic::mips_mini_u_d:
   2018     return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
   2019                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2020   case Intrinsic::mips_mod_s_b:
   2021   case Intrinsic::mips_mod_s_h:
   2022   case Intrinsic::mips_mod_s_w:
   2023   case Intrinsic::mips_mod_s_d:
   2024     return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
   2025                        Op->getOperand(2));
   2026   case Intrinsic::mips_mod_u_b:
   2027   case Intrinsic::mips_mod_u_h:
   2028   case Intrinsic::mips_mod_u_w:
   2029   case Intrinsic::mips_mod_u_d:
   2030     return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
   2031                        Op->getOperand(2));
   2032   case Intrinsic::mips_mulv_b:
   2033   case Intrinsic::mips_mulv_h:
   2034   case Intrinsic::mips_mulv_w:
   2035   case Intrinsic::mips_mulv_d:
   2036     return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
   2037                        Op->getOperand(2));
   2038   case Intrinsic::mips_msubv_b:
   2039   case Intrinsic::mips_msubv_h:
   2040   case Intrinsic::mips_msubv_w:
   2041   case Intrinsic::mips_msubv_d: {
   2042     EVT ResTy = Op->getValueType(0);
   2043     return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
   2044                        DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
   2045                                    Op->getOperand(2), Op->getOperand(3)));
   2046   }
   2047   case Intrinsic::mips_nlzc_b:
   2048   case Intrinsic::mips_nlzc_h:
   2049   case Intrinsic::mips_nlzc_w:
   2050   case Intrinsic::mips_nlzc_d:
   2051     return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
   2052   case Intrinsic::mips_nor_v: {
   2053     SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
   2054                               Op->getOperand(1), Op->getOperand(2));
   2055     return DAG.getNOT(DL, Res, Res->getValueType(0));
   2056   }
   2057   case Intrinsic::mips_nori_b: {
   2058     SDValue Res =  DAG.getNode(ISD::OR, DL, Op->getValueType(0),
   2059                                Op->getOperand(1),
   2060                                lowerMSASplatImm(Op, 2, DAG));
   2061     return DAG.getNOT(DL, Res, Res->getValueType(0));
   2062   }
   2063   case Intrinsic::mips_or_v:
   2064     return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
   2065                        Op->getOperand(2));
   2066   case Intrinsic::mips_ori_b:
   2067     return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
   2068                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2069   case Intrinsic::mips_pckev_b:
   2070   case Intrinsic::mips_pckev_h:
   2071   case Intrinsic::mips_pckev_w:
   2072   case Intrinsic::mips_pckev_d:
   2073     return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
   2074                        Op->getOperand(1), Op->getOperand(2));
   2075   case Intrinsic::mips_pckod_b:
   2076   case Intrinsic::mips_pckod_h:
   2077   case Intrinsic::mips_pckod_w:
   2078   case Intrinsic::mips_pckod_d:
   2079     return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
   2080                        Op->getOperand(1), Op->getOperand(2));
   2081   case Intrinsic::mips_pcnt_b:
   2082   case Intrinsic::mips_pcnt_h:
   2083   case Intrinsic::mips_pcnt_w:
   2084   case Intrinsic::mips_pcnt_d:
   2085     return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
   2086   case Intrinsic::mips_shf_b:
   2087   case Intrinsic::mips_shf_h:
   2088   case Intrinsic::mips_shf_w:
   2089     return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
   2090                        Op->getOperand(2), Op->getOperand(1));
   2091   case Intrinsic::mips_sll_b:
   2092   case Intrinsic::mips_sll_h:
   2093   case Intrinsic::mips_sll_w:
   2094   case Intrinsic::mips_sll_d:
   2095     return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
   2096                        Op->getOperand(2));
   2097   case Intrinsic::mips_slli_b:
   2098   case Intrinsic::mips_slli_h:
   2099   case Intrinsic::mips_slli_w:
   2100   case Intrinsic::mips_slli_d:
   2101     return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
   2102                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2103   case Intrinsic::mips_splat_b:
   2104   case Intrinsic::mips_splat_h:
   2105   case Intrinsic::mips_splat_w:
   2106   case Intrinsic::mips_splat_d:
   2107     // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
   2108     // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
   2109     // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
   2110     // Instead we lower to MipsISD::VSHF and match from there.
   2111     return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
   2112                        lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
   2113                        Op->getOperand(1));
   2114   case Intrinsic::mips_splati_b:
   2115   case Intrinsic::mips_splati_h:
   2116   case Intrinsic::mips_splati_w:
   2117   case Intrinsic::mips_splati_d:
   2118     return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
   2119                        lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
   2120                        Op->getOperand(1));
   2121   case Intrinsic::mips_sra_b:
   2122   case Intrinsic::mips_sra_h:
   2123   case Intrinsic::mips_sra_w:
   2124   case Intrinsic::mips_sra_d:
   2125     return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
   2126                        Op->getOperand(2));
   2127   case Intrinsic::mips_srai_b:
   2128   case Intrinsic::mips_srai_h:
   2129   case Intrinsic::mips_srai_w:
   2130   case Intrinsic::mips_srai_d:
   2131     return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
   2132                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2133   case Intrinsic::mips_srl_b:
   2134   case Intrinsic::mips_srl_h:
   2135   case Intrinsic::mips_srl_w:
   2136   case Intrinsic::mips_srl_d:
   2137     return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
   2138                        Op->getOperand(2));
   2139   case Intrinsic::mips_srli_b:
   2140   case Intrinsic::mips_srli_h:
   2141   case Intrinsic::mips_srli_w:
   2142   case Intrinsic::mips_srli_d:
   2143     return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
   2144                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2145   case Intrinsic::mips_subv_b:
   2146   case Intrinsic::mips_subv_h:
   2147   case Intrinsic::mips_subv_w:
   2148   case Intrinsic::mips_subv_d:
   2149     return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
   2150                        Op->getOperand(2));
   2151   case Intrinsic::mips_subvi_b:
   2152   case Intrinsic::mips_subvi_h:
   2153   case Intrinsic::mips_subvi_w:
   2154   case Intrinsic::mips_subvi_d:
   2155     return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
   2156                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2157   case Intrinsic::mips_vshf_b:
   2158   case Intrinsic::mips_vshf_h:
   2159   case Intrinsic::mips_vshf_w:
   2160   case Intrinsic::mips_vshf_d:
   2161     return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
   2162                        Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
   2163   case Intrinsic::mips_xor_v:
   2164     return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
   2165                        Op->getOperand(2));
   2166   case Intrinsic::mips_xori_b:
   2167     return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
   2168                        Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
   2169   }
   2170 }
   2171 
   2172 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
   2173   SDLoc DL(Op);
   2174   SDValue ChainIn = Op->getOperand(0);
   2175   SDValue Address = Op->getOperand(2);
   2176   SDValue Offset  = Op->getOperand(3);
   2177   EVT ResTy = Op->getValueType(0);
   2178   EVT PtrTy = Address->getValueType(0);
   2179 
   2180   Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
   2181 
   2182   return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
   2183                      false, false, 16);
   2184 }
   2185 
   2186 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
   2187                                                      SelectionDAG &DAG) const {
   2188   unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
   2189   switch (Intr) {
   2190   default:
   2191     return SDValue();
   2192   case Intrinsic::mips_extp:
   2193     return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
   2194   case Intrinsic::mips_extpdp:
   2195     return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
   2196   case Intrinsic::mips_extr_w:
   2197     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
   2198   case Intrinsic::mips_extr_r_w:
   2199     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
   2200   case Intrinsic::mips_extr_rs_w:
   2201     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
   2202   case Intrinsic::mips_extr_s_h:
   2203     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
   2204   case Intrinsic::mips_mthlip:
   2205     return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
   2206   case Intrinsic::mips_mulsaq_s_w_ph:
   2207     return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
   2208   case Intrinsic::mips_maq_s_w_phl:
   2209     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
   2210   case Intrinsic::mips_maq_s_w_phr:
   2211     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
   2212   case Intrinsic::mips_maq_sa_w_phl:
   2213     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
   2214   case Intrinsic::mips_maq_sa_w_phr:
   2215     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
   2216   case Intrinsic::mips_dpaq_s_w_ph:
   2217     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
   2218   case Intrinsic::mips_dpsq_s_w_ph:
   2219     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
   2220   case Intrinsic::mips_dpaq_sa_l_w:
   2221     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
   2222   case Intrinsic::mips_dpsq_sa_l_w:
   2223     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
   2224   case Intrinsic::mips_dpaqx_s_w_ph:
   2225     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
   2226   case Intrinsic::mips_dpaqx_sa_w_ph:
   2227     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
   2228   case Intrinsic::mips_dpsqx_s_w_ph:
   2229     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
   2230   case Intrinsic::mips_dpsqx_sa_w_ph:
   2231     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
   2232   case Intrinsic::mips_ld_b:
   2233   case Intrinsic::mips_ld_h:
   2234   case Intrinsic::mips_ld_w:
   2235   case Intrinsic::mips_ld_d:
   2236    return lowerMSALoadIntr(Op, DAG, Intr);
   2237   }
   2238 }
   2239 
   2240 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
   2241   SDLoc DL(Op);
   2242   SDValue ChainIn = Op->getOperand(0);
   2243   SDValue Value   = Op->getOperand(2);
   2244   SDValue Address = Op->getOperand(3);
   2245   SDValue Offset  = Op->getOperand(4);
   2246   EVT PtrTy = Address->getValueType(0);
   2247 
   2248   Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
   2249 
   2250   return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
   2251                       false, 16);
   2252 }
   2253 
   2254 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
   2255                                                   SelectionDAG &DAG) const {
   2256   unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
   2257   switch (Intr) {
   2258   default:
   2259     return SDValue();
   2260   case Intrinsic::mips_st_b:
   2261   case Intrinsic::mips_st_h:
   2262   case Intrinsic::mips_st_w:
   2263   case Intrinsic::mips_st_d:
   2264     return lowerMSAStoreIntr(Op, DAG, Intr);
   2265   }
   2266 }
   2267 
   2268 /// \brief Check if the given BuildVectorSDNode is a splat.
   2269 /// This method currently relies on DAG nodes being reused when equivalent,
   2270 /// so it's possible for this to return false even when isConstantSplat returns
   2271 /// true.
   2272 static bool isSplatVector(const BuildVectorSDNode *N) {
   2273   unsigned int nOps = N->getNumOperands();
   2274   assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
   2275 
   2276   SDValue Operand0 = N->getOperand(0);
   2277 
   2278   for (unsigned int i = 1; i < nOps; ++i) {
   2279     if (N->getOperand(i) != Operand0)
   2280       return false;
   2281   }
   2282 
   2283   return true;
   2284 }
   2285 
   2286 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
   2287 //
   2288 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
   2289 // choose to sign-extend but we could have equally chosen zero-extend. The
   2290 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
   2291 // result into this node later (possibly changing it to a zero-extend in the
   2292 // process).
   2293 SDValue MipsSETargetLowering::
   2294 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
   2295   SDLoc DL(Op);
   2296   EVT ResTy = Op->getValueType(0);
   2297   SDValue Op0 = Op->getOperand(0);
   2298   EVT VecTy = Op0->getValueType(0);
   2299 
   2300   if (!VecTy.is128BitVector())
   2301     return SDValue();
   2302 
   2303   if (ResTy.isInteger()) {
   2304     SDValue Op1 = Op->getOperand(1);
   2305     EVT EltTy = VecTy.getVectorElementType();
   2306     return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
   2307                        DAG.getValueType(EltTy));
   2308   }
   2309 
   2310   return Op;
   2311 }
   2312 
   2313 static bool isConstantOrUndef(const SDValue Op) {
   2314   if (Op->getOpcode() == ISD::UNDEF)
   2315     return true;
   2316   if (isa<ConstantSDNode>(Op))
   2317     return true;
   2318   if (isa<ConstantFPSDNode>(Op))
   2319     return true;
   2320   return false;
   2321 }
   2322 
   2323 static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
   2324   for (unsigned i = 0; i < Op->getNumOperands(); ++i)
   2325     if (isConstantOrUndef(Op->getOperand(i)))
   2326       return true;
   2327   return false;
   2328 }
   2329 
   2330 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
   2331 // backend.
   2332 //
   2333 // Lowers according to the following rules:
   2334 // - Constant splats are legal as-is as long as the SplatBitSize is a power of
   2335 //   2 less than or equal to 64 and the value fits into a signed 10-bit
   2336 //   immediate
   2337 // - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
   2338 //   is a power of 2 less than or equal to 64 and the value does not fit into a
   2339 //   signed 10-bit immediate
   2340 // - Non-constant splats are legal as-is.
   2341 // - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
   2342 // - All others are illegal and must be expanded.
   2343 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
   2344                                                 SelectionDAG &DAG) const {
   2345   BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
   2346   EVT ResTy = Op->getValueType(0);
   2347   SDLoc DL(Op);
   2348   APInt SplatValue, SplatUndef;
   2349   unsigned SplatBitSize;
   2350   bool HasAnyUndefs;
   2351 
   2352   if (!Subtarget.hasMSA() || !ResTy.is128BitVector())
   2353     return SDValue();
   2354 
   2355   if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
   2356                             HasAnyUndefs, 8,
   2357                             !Subtarget.isLittle()) && SplatBitSize <= 64) {
   2358     // We can only cope with 8, 16, 32, or 64-bit elements
   2359     if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
   2360         SplatBitSize != 64)
   2361       return SDValue();
   2362 
   2363     // If the value fits into a simm10 then we can use ldi.[bhwd]
   2364     // However, if it isn't an integer type we will have to bitcast from an
   2365     // integer type first. Also, if there are any undefs, we must lower them
   2366     // to defined values first.
   2367     if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
   2368       return Op;
   2369 
   2370     EVT ViaVecTy;
   2371 
   2372     switch (SplatBitSize) {
   2373     default:
   2374       return SDValue();
   2375     case 8:
   2376       ViaVecTy = MVT::v16i8;
   2377       break;
   2378     case 16:
   2379       ViaVecTy = MVT::v8i16;
   2380       break;
   2381     case 32:
   2382       ViaVecTy = MVT::v4i32;
   2383       break;
   2384     case 64:
   2385       // There's no fill.d to fall back on for 64-bit values
   2386       return SDValue();
   2387     }
   2388 
   2389     // SelectionDAG::getConstant will promote SplatValue appropriately.
   2390     SDValue Result = DAG.getConstant(SplatValue, DL, ViaVecTy);
   2391 
   2392     // Bitcast to the type we originally wanted
   2393     if (ViaVecTy != ResTy)
   2394       Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
   2395 
   2396     return Result;
   2397   } else if (isSplatVector(Node))
   2398     return Op;
   2399   else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
   2400     // Use INSERT_VECTOR_ELT operations rather than expand to stores.
   2401     // The resulting code is the same length as the expansion, but it doesn't
   2402     // use memory operations
   2403     EVT ResTy = Node->getValueType(0);
   2404 
   2405     assert(ResTy.isVector());
   2406 
   2407     unsigned NumElts = ResTy.getVectorNumElements();
   2408     SDValue Vector = DAG.getUNDEF(ResTy);
   2409     for (unsigned i = 0; i < NumElts; ++i) {
   2410       Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
   2411                            Node->getOperand(i),
   2412                            DAG.getConstant(i, DL, MVT::i32));
   2413     }
   2414     return Vector;
   2415   }
   2416 
   2417   return SDValue();
   2418 }
   2419 
   2420 // Lower VECTOR_SHUFFLE into SHF (if possible).
   2421 //
   2422 // SHF splits the vector into blocks of four elements, then shuffles these
   2423 // elements according to a <4 x i2> constant (encoded as an integer immediate).
   2424 //
   2425 // It is therefore possible to lower into SHF when the mask takes the form:
   2426 //   <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
   2427 // When undef's appear they are treated as if they were whatever value is
   2428 // necessary in order to fit the above forms.
   2429 //
   2430 // For example:
   2431 //   %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
   2432 //                      <8 x i32> <i32 3, i32 2, i32 1, i32 0,
   2433 //                                 i32 7, i32 6, i32 5, i32 4>
   2434 // is lowered to:
   2435 //   (SHF_H $w0, $w1, 27)
   2436 // where the 27 comes from:
   2437 //   3 + (2 << 2) + (1 << 4) + (0 << 6)
   2438 static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
   2439                                        SmallVector<int, 16> Indices,
   2440                                        SelectionDAG &DAG) {
   2441   int SHFIndices[4] = { -1, -1, -1, -1 };
   2442 
   2443   if (Indices.size() < 4)
   2444     return SDValue();
   2445 
   2446   for (unsigned i = 0; i < 4; ++i) {
   2447     for (unsigned j = i; j < Indices.size(); j += 4) {
   2448       int Idx = Indices[j];
   2449 
   2450       // Convert from vector index to 4-element subvector index
   2451       // If an index refers to an element outside of the subvector then give up
   2452       if (Idx != -1) {
   2453         Idx -= 4 * (j / 4);
   2454         if (Idx < 0 || Idx >= 4)
   2455           return SDValue();
   2456       }
   2457 
   2458       // If the mask has an undef, replace it with the current index.
   2459       // Note that it might still be undef if the current index is also undef
   2460       if (SHFIndices[i] == -1)
   2461         SHFIndices[i] = Idx;
   2462 
   2463       // Check that non-undef values are the same as in the mask. If they
   2464       // aren't then give up
   2465       if (!(Idx == -1 || Idx == SHFIndices[i]))
   2466         return SDValue();
   2467     }
   2468   }
   2469 
   2470   // Calculate the immediate. Replace any remaining undefs with zero
   2471   APInt Imm(32, 0);
   2472   for (int i = 3; i >= 0; --i) {
   2473     int Idx = SHFIndices[i];
   2474 
   2475     if (Idx == -1)
   2476       Idx = 0;
   2477 
   2478     Imm <<= 2;
   2479     Imm |= Idx & 0x3;
   2480   }
   2481 
   2482   SDLoc DL(Op);
   2483   return DAG.getNode(MipsISD::SHF, DL, ResTy,
   2484                      DAG.getConstant(Imm, DL, MVT::i32), Op->getOperand(0));
   2485 }
   2486 
   2487 /// Determine whether a range fits a regular pattern of values.
   2488 /// This function accounts for the possibility of jumping over the End iterator.
   2489 template <typename ValType>
   2490 static bool
   2491 fitsRegularPattern(typename SmallVectorImpl<ValType>::const_iterator Begin,
   2492                    unsigned CheckStride,
   2493                    typename SmallVectorImpl<ValType>::const_iterator End,
   2494                    ValType ExpectedIndex, unsigned ExpectedIndexStride) {
   2495   auto &I = Begin;
   2496 
   2497   while (I != End) {
   2498     if (*I != -1 && *I != ExpectedIndex)
   2499       return false;
   2500     ExpectedIndex += ExpectedIndexStride;
   2501 
   2502     // Incrementing past End is undefined behaviour so we must increment one
   2503     // step at a time and check for End at each step.
   2504     for (unsigned n = 0; n < CheckStride && I != End; ++n, ++I)
   2505       ; // Empty loop body.
   2506   }
   2507   return true;
   2508 }
   2509 
   2510 // Determine whether VECTOR_SHUFFLE is a SPLATI.
   2511 //
   2512 // It is a SPLATI when the mask is:
   2513 //   <x, x, x, ...>
   2514 // where x is any valid index.
   2515 //
   2516 // When undef's appear in the mask they are treated as if they were whatever
   2517 // value is necessary in order to fit the above form.
   2518 static bool isVECTOR_SHUFFLE_SPLATI(SDValue Op, EVT ResTy,
   2519                                     SmallVector<int, 16> Indices,
   2520                                     SelectionDAG &DAG) {
   2521   assert((Indices.size() % 2) == 0);
   2522 
   2523   int SplatIndex = -1;
   2524   for (const auto &V : Indices) {
   2525     if (V != -1) {
   2526       SplatIndex = V;
   2527       break;
   2528     }
   2529   }
   2530 
   2531   return fitsRegularPattern<int>(Indices.begin(), 1, Indices.end(), SplatIndex,
   2532                                  0);
   2533 }
   2534 
   2535 // Lower VECTOR_SHUFFLE into ILVEV (if possible).
   2536 //
   2537 // ILVEV interleaves the even elements from each vector.
   2538 //
   2539 // It is possible to lower into ILVEV when the mask consists of two of the
   2540 // following forms interleaved:
   2541 //   <0, 2, 4, ...>
   2542 //   <n, n+2, n+4, ...>
   2543 // where n is the number of elements in the vector.
   2544 // For example:
   2545 //   <0, 0, 2, 2, 4, 4, ...>
   2546 //   <0, n, 2, n+2, 4, n+4, ...>
   2547 //
   2548 // When undef's appear in the mask they are treated as if they were whatever
   2549 // value is necessary in order to fit the above forms.
   2550 static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
   2551                                          SmallVector<int, 16> Indices,
   2552                                          SelectionDAG &DAG) {
   2553   assert((Indices.size() % 2) == 0);
   2554 
   2555   SDValue Wt;
   2556   SDValue Ws;
   2557   const auto &Begin = Indices.begin();
   2558   const auto &End = Indices.end();
   2559 
   2560   // Check even elements are taken from the even elements of one half or the
   2561   // other and pick an operand accordingly.
   2562   if (fitsRegularPattern<int>(Begin, 2, End, 0, 2))
   2563     Wt = Op->getOperand(0);
   2564   else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 2))
   2565     Wt = Op->getOperand(1);
   2566   else
   2567     return SDValue();
   2568 
   2569   // Check odd elements are taken from the even elements of one half or the
   2570   // other and pick an operand accordingly.
   2571   if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 2))
   2572     Ws = Op->getOperand(0);
   2573   else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 2))
   2574     Ws = Op->getOperand(1);
   2575   else
   2576     return SDValue();
   2577 
   2578   return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Ws, Wt);
   2579 }
   2580 
   2581 // Lower VECTOR_SHUFFLE into ILVOD (if possible).
   2582 //
   2583 // ILVOD interleaves the odd elements from each vector.
   2584 //
   2585 // It is possible to lower into ILVOD when the mask consists of two of the
   2586 // following forms interleaved:
   2587 //   <1, 3, 5, ...>
   2588 //   <n+1, n+3, n+5, ...>
   2589 // where n is the number of elements in the vector.
   2590 // For example:
   2591 //   <1, 1, 3, 3, 5, 5, ...>
   2592 //   <1, n+1, 3, n+3, 5, n+5, ...>
   2593 //
   2594 // When undef's appear in the mask they are treated as if they were whatever
   2595 // value is necessary in order to fit the above forms.
   2596 static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
   2597                                          SmallVector<int, 16> Indices,
   2598                                          SelectionDAG &DAG) {
   2599   assert((Indices.size() % 2) == 0);
   2600 
   2601   SDValue Wt;
   2602   SDValue Ws;
   2603   const auto &Begin = Indices.begin();
   2604   const auto &End = Indices.end();
   2605 
   2606   // Check even elements are taken from the odd elements of one half or the
   2607   // other and pick an operand accordingly.
   2608   if (fitsRegularPattern<int>(Begin, 2, End, 1, 2))
   2609     Wt = Op->getOperand(0);
   2610   else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + 1, 2))
   2611     Wt = Op->getOperand(1);
   2612   else
   2613     return SDValue();
   2614 
   2615   // Check odd elements are taken from the odd elements of one half or the
   2616   // other and pick an operand accordingly.
   2617   if (fitsRegularPattern<int>(Begin + 1, 2, End, 1, 2))
   2618     Ws = Op->getOperand(0);
   2619   else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + 1, 2))
   2620     Ws = Op->getOperand(1);
   2621   else
   2622     return SDValue();
   2623 
   2624   return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Wt, Ws);
   2625 }
   2626 
   2627 // Lower VECTOR_SHUFFLE into ILVR (if possible).
   2628 //
   2629 // ILVR interleaves consecutive elements from the right (lowest-indexed) half of
   2630 // each vector.
   2631 //
   2632 // It is possible to lower into ILVR when the mask consists of two of the
   2633 // following forms interleaved:
   2634 //   <0, 1, 2, ...>
   2635 //   <n, n+1, n+2, ...>
   2636 // where n is the number of elements in the vector.
   2637 // For example:
   2638 //   <0, 0, 1, 1, 2, 2, ...>
   2639 //   <0, n, 1, n+1, 2, n+2, ...>
   2640 //
   2641 // When undef's appear in the mask they are treated as if they were whatever
   2642 // value is necessary in order to fit the above forms.
   2643 static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
   2644                                         SmallVector<int, 16> Indices,
   2645                                         SelectionDAG &DAG) {
   2646   assert((Indices.size() % 2) == 0);
   2647 
   2648   SDValue Wt;
   2649   SDValue Ws;
   2650   const auto &Begin = Indices.begin();
   2651   const auto &End = Indices.end();
   2652 
   2653   // Check even elements are taken from the right (lowest-indexed) elements of
   2654   // one half or the other and pick an operand accordingly.
   2655   if (fitsRegularPattern<int>(Begin, 2, End, 0, 1))
   2656     Wt = Op->getOperand(0);
   2657   else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size(), 1))
   2658     Wt = Op->getOperand(1);
   2659   else
   2660     return SDValue();
   2661 
   2662   // Check odd elements are taken from the right (lowest-indexed) elements of
   2663   // one half or the other and pick an operand accordingly.
   2664   if (fitsRegularPattern<int>(Begin + 1, 2, End, 0, 1))
   2665     Ws = Op->getOperand(0);
   2666   else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size(), 1))
   2667     Ws = Op->getOperand(1);
   2668   else
   2669     return SDValue();
   2670 
   2671   return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Ws, Wt);
   2672 }
   2673 
   2674 // Lower VECTOR_SHUFFLE into ILVL (if possible).
   2675 //
   2676 // ILVL interleaves consecutive elements from the left (highest-indexed) half
   2677 // of each vector.
   2678 //
   2679 // It is possible to lower into ILVL when the mask consists of two of the
   2680 // following forms interleaved:
   2681 //   <x, x+1, x+2, ...>
   2682 //   <n+x, n+x+1, n+x+2, ...>
   2683 // where n is the number of elements in the vector and x is half n.
   2684 // For example:
   2685 //   <x, x, x+1, x+1, x+2, x+2, ...>
   2686 //   <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
   2687 //
   2688 // When undef's appear in the mask they are treated as if they were whatever
   2689 // value is necessary in order to fit the above forms.
   2690 static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
   2691                                         SmallVector<int, 16> Indices,
   2692                                         SelectionDAG &DAG) {
   2693   assert((Indices.size() % 2) == 0);
   2694 
   2695   unsigned HalfSize = Indices.size() / 2;
   2696   SDValue Wt;
   2697   SDValue Ws;
   2698   const auto &Begin = Indices.begin();
   2699   const auto &End = Indices.end();
   2700 
   2701   // Check even elements are taken from the left (highest-indexed) elements of
   2702   // one half or the other and pick an operand accordingly.
   2703   if (fitsRegularPattern<int>(Begin, 2, End, HalfSize, 1))
   2704     Wt = Op->getOperand(0);
   2705   else if (fitsRegularPattern<int>(Begin, 2, End, Indices.size() + HalfSize, 1))
   2706     Wt = Op->getOperand(1);
   2707   else
   2708     return SDValue();
   2709 
   2710   // Check odd elements are taken from the left (highest-indexed) elements of
   2711   // one half or the other and pick an operand accordingly.
   2712   if (fitsRegularPattern<int>(Begin + 1, 2, End, HalfSize, 1))
   2713     Ws = Op->getOperand(0);
   2714   else if (fitsRegularPattern<int>(Begin + 1, 2, End, Indices.size() + HalfSize,
   2715                                    1))
   2716     Ws = Op->getOperand(1);
   2717   else
   2718     return SDValue();
   2719 
   2720   return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Ws, Wt);
   2721 }
   2722 
   2723 // Lower VECTOR_SHUFFLE into PCKEV (if possible).
   2724 //
   2725 // PCKEV copies the even elements of each vector into the result vector.
   2726 //
   2727 // It is possible to lower into PCKEV when the mask consists of two of the
   2728 // following forms concatenated:
   2729 //   <0, 2, 4, ...>
   2730 //   <n, n+2, n+4, ...>
   2731 // where n is the number of elements in the vector.
   2732 // For example:
   2733 //   <0, 2, 4, ..., 0, 2, 4, ...>
   2734 //   <0, 2, 4, ..., n, n+2, n+4, ...>
   2735 //
   2736 // When undef's appear in the mask they are treated as if they were whatever
   2737 // value is necessary in order to fit the above forms.
   2738 static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
   2739                                          SmallVector<int, 16> Indices,
   2740                                          SelectionDAG &DAG) {
   2741   assert((Indices.size() % 2) == 0);
   2742 
   2743   SDValue Wt;
   2744   SDValue Ws;
   2745   const auto &Begin = Indices.begin();
   2746   const auto &Mid = Indices.begin() + Indices.size() / 2;
   2747   const auto &End = Indices.end();
   2748 
   2749   if (fitsRegularPattern<int>(Begin, 1, Mid, 0, 2))
   2750     Wt = Op->getOperand(0);
   2751   else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size(), 2))
   2752     Wt = Op->getOperand(1);
   2753   else
   2754     return SDValue();
   2755 
   2756   if (fitsRegularPattern<int>(Mid, 1, End, 0, 2))
   2757     Ws = Op->getOperand(0);
   2758   else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size(), 2))
   2759     Ws = Op->getOperand(1);
   2760   else
   2761     return SDValue();
   2762 
   2763   return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Ws, Wt);
   2764 }
   2765 
   2766 // Lower VECTOR_SHUFFLE into PCKOD (if possible).
   2767 //
   2768 // PCKOD copies the odd elements of each vector into the result vector.
   2769 //
   2770 // It is possible to lower into PCKOD when the mask consists of two of the
   2771 // following forms concatenated:
   2772 //   <1, 3, 5, ...>
   2773 //   <n+1, n+3, n+5, ...>
   2774 // where n is the number of elements in the vector.
   2775 // For example:
   2776 //   <1, 3, 5, ..., 1, 3, 5, ...>
   2777 //   <1, 3, 5, ..., n+1, n+3, n+5, ...>
   2778 //
   2779 // When undef's appear in the mask they are treated as if they were whatever
   2780 // value is necessary in order to fit the above forms.
   2781 static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
   2782                                          SmallVector<int, 16> Indices,
   2783                                          SelectionDAG &DAG) {
   2784   assert((Indices.size() % 2) == 0);
   2785 
   2786   SDValue Wt;
   2787   SDValue Ws;
   2788   const auto &Begin = Indices.begin();
   2789   const auto &Mid = Indices.begin() + Indices.size() / 2;
   2790   const auto &End = Indices.end();
   2791 
   2792   if (fitsRegularPattern<int>(Begin, 1, Mid, 1, 2))
   2793     Wt = Op->getOperand(0);
   2794   else if (fitsRegularPattern<int>(Begin, 1, Mid, Indices.size() + 1, 2))
   2795     Wt = Op->getOperand(1);
   2796   else
   2797     return SDValue();
   2798 
   2799   if (fitsRegularPattern<int>(Mid, 1, End, 1, 2))
   2800     Ws = Op->getOperand(0);
   2801   else if (fitsRegularPattern<int>(Mid, 1, End, Indices.size() + 1, 2))
   2802     Ws = Op->getOperand(1);
   2803   else
   2804     return SDValue();
   2805 
   2806   return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Ws, Wt);
   2807 }
   2808 
   2809 // Lower VECTOR_SHUFFLE into VSHF.
   2810 //
   2811 // This mostly consists of converting the shuffle indices in Indices into a
   2812 // BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
   2813 // also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
   2814 // if the type is v8i16 and all the indices are less than 8 then the second
   2815 // operand is unused and can be replaced with anything. We choose to replace it
   2816 // with the used operand since this reduces the number of instructions overall.
   2817 static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
   2818                                         SmallVector<int, 16> Indices,
   2819                                         SelectionDAG &DAG) {
   2820   SmallVector<SDValue, 16> Ops;
   2821   SDValue Op0;
   2822   SDValue Op1;
   2823   EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
   2824   EVT MaskEltTy = MaskVecTy.getVectorElementType();
   2825   bool Using1stVec = false;
   2826   bool Using2ndVec = false;
   2827   SDLoc DL(Op);
   2828   int ResTyNumElts = ResTy.getVectorNumElements();
   2829 
   2830   for (int i = 0; i < ResTyNumElts; ++i) {
   2831     // Idx == -1 means UNDEF
   2832     int Idx = Indices[i];
   2833 
   2834     if (0 <= Idx && Idx < ResTyNumElts)
   2835       Using1stVec = true;
   2836     if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
   2837       Using2ndVec = true;
   2838   }
   2839 
   2840   for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
   2841        ++I)
   2842     Ops.push_back(DAG.getTargetConstant(*I, DL, MaskEltTy));
   2843 
   2844   SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, Ops);
   2845 
   2846   if (Using1stVec && Using2ndVec) {
   2847     Op0 = Op->getOperand(0);
   2848     Op1 = Op->getOperand(1);
   2849   } else if (Using1stVec)
   2850     Op0 = Op1 = Op->getOperand(0);
   2851   else if (Using2ndVec)
   2852     Op0 = Op1 = Op->getOperand(1);
   2853   else
   2854     llvm_unreachable("shuffle vector mask references neither vector operand?");
   2855 
   2856   // VECTOR_SHUFFLE concatenates the vectors in an vectorwise fashion.
   2857   // <0b00, 0b01> + <0b10, 0b11> -> <0b00, 0b01, 0b10, 0b11>
   2858   // VSHF concatenates the vectors in a bitwise fashion:
   2859   // <0b00, 0b01> + <0b10, 0b11> ->
   2860   // 0b0100       + 0b1110       -> 0b01001110
   2861   //                                <0b10, 0b11, 0b00, 0b01>
   2862   // We must therefore swap the operands to get the correct result.
   2863   return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op1, Op0);
   2864 }
   2865 
   2866 // Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
   2867 // indices in the shuffle.
   2868 SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
   2869                                                   SelectionDAG &DAG) const {
   2870   ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
   2871   EVT ResTy = Op->getValueType(0);
   2872 
   2873   if (!ResTy.is128BitVector())
   2874     return SDValue();
   2875 
   2876   int ResTyNumElts = ResTy.getVectorNumElements();
   2877   SmallVector<int, 16> Indices;
   2878 
   2879   for (int i = 0; i < ResTyNumElts; ++i)
   2880     Indices.push_back(Node->getMaskElt(i));
   2881 
   2882   // splati.[bhwd] is preferable to the others but is matched from
   2883   // MipsISD::VSHF.
   2884   if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
   2885     return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
   2886   SDValue Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
   2887   if (Result.getNode())
   2888     return Result;
   2889   Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
   2890   if (Result.getNode())
   2891     return Result;
   2892   Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
   2893   if (Result.getNode())
   2894     return Result;
   2895   Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
   2896   if (Result.getNode())
   2897     return Result;
   2898   Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
   2899   if (Result.getNode())
   2900     return Result;
   2901   Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
   2902   if (Result.getNode())
   2903     return Result;
   2904   Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
   2905   if (Result.getNode())
   2906     return Result;
   2907   return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
   2908 }
   2909 
   2910 MachineBasicBlock * MipsSETargetLowering::
   2911 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
   2912   // $bb:
   2913   //  bposge32_pseudo $vr0
   2914   //  =>
   2915   // $bb:
   2916   //  bposge32 $tbb
   2917   // $fbb:
   2918   //  li $vr2, 0
   2919   //  b $sink
   2920   // $tbb:
   2921   //  li $vr1, 1
   2922   // $sink:
   2923   //  $vr0 = phi($vr2, $fbb, $vr1, $tbb)
   2924 
   2925   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   2926   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   2927   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
   2928   DebugLoc DL = MI->getDebugLoc();
   2929   const BasicBlock *LLVM_BB = BB->getBasicBlock();
   2930   MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
   2931   MachineFunction *F = BB->getParent();
   2932   MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
   2933   MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
   2934   MachineBasicBlock *Sink  = F->CreateMachineBasicBlock(LLVM_BB);
   2935   F->insert(It, FBB);
   2936   F->insert(It, TBB);
   2937   F->insert(It, Sink);
   2938 
   2939   // Transfer the remainder of BB and its successor edges to Sink.
   2940   Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
   2941                BB->end());
   2942   Sink->transferSuccessorsAndUpdatePHIs(BB);
   2943 
   2944   // Add successors.
   2945   BB->addSuccessor(FBB);
   2946   BB->addSuccessor(TBB);
   2947   FBB->addSuccessor(Sink);
   2948   TBB->addSuccessor(Sink);
   2949 
   2950   // Insert the real bposge32 instruction to $BB.
   2951   BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
   2952 
   2953   // Fill $FBB.
   2954   unsigned VR2 = RegInfo.createVirtualRegister(RC);
   2955   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
   2956     .addReg(Mips::ZERO).addImm(0);
   2957   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
   2958 
   2959   // Fill $TBB.
   2960   unsigned VR1 = RegInfo.createVirtualRegister(RC);
   2961   BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
   2962     .addReg(Mips::ZERO).addImm(1);
   2963 
   2964   // Insert phi function to $Sink.
   2965   BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
   2966           MI->getOperand(0).getReg())
   2967     .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
   2968 
   2969   MI->eraseFromParent();   // The pseudo instruction is gone now.
   2970   return Sink;
   2971 }
   2972 
   2973 MachineBasicBlock * MipsSETargetLowering::
   2974 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
   2975                      unsigned BranchOp) const{
   2976   // $bb:
   2977   //  vany_nonzero $rd, $ws
   2978   //  =>
   2979   // $bb:
   2980   //  bnz.b $ws, $tbb
   2981   //  b $fbb
   2982   // $fbb:
   2983   //  li $rd1, 0
   2984   //  b $sink
   2985   // $tbb:
   2986   //  li $rd2, 1
   2987   // $sink:
   2988   //  $rd = phi($rd1, $fbb, $rd2, $tbb)
   2989 
   2990   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   2991   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   2992   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
   2993   DebugLoc DL = MI->getDebugLoc();
   2994   const BasicBlock *LLVM_BB = BB->getBasicBlock();
   2995   MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
   2996   MachineFunction *F = BB->getParent();
   2997   MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
   2998   MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
   2999   MachineBasicBlock *Sink  = F->CreateMachineBasicBlock(LLVM_BB);
   3000   F->insert(It, FBB);
   3001   F->insert(It, TBB);
   3002   F->insert(It, Sink);
   3003 
   3004   // Transfer the remainder of BB and its successor edges to Sink.
   3005   Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
   3006                BB->end());
   3007   Sink->transferSuccessorsAndUpdatePHIs(BB);
   3008 
   3009   // Add successors.
   3010   BB->addSuccessor(FBB);
   3011   BB->addSuccessor(TBB);
   3012   FBB->addSuccessor(Sink);
   3013   TBB->addSuccessor(Sink);
   3014 
   3015   // Insert the real bnz.b instruction to $BB.
   3016   BuildMI(BB, DL, TII->get(BranchOp))
   3017     .addReg(MI->getOperand(1).getReg())
   3018     .addMBB(TBB);
   3019 
   3020   // Fill $FBB.
   3021   unsigned RD1 = RegInfo.createVirtualRegister(RC);
   3022   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
   3023     .addReg(Mips::ZERO).addImm(0);
   3024   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
   3025 
   3026   // Fill $TBB.
   3027   unsigned RD2 = RegInfo.createVirtualRegister(RC);
   3028   BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
   3029     .addReg(Mips::ZERO).addImm(1);
   3030 
   3031   // Insert phi function to $Sink.
   3032   BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
   3033           MI->getOperand(0).getReg())
   3034     .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
   3035 
   3036   MI->eraseFromParent();   // The pseudo instruction is gone now.
   3037   return Sink;
   3038 }
   3039 
   3040 // Emit the COPY_FW pseudo instruction.
   3041 //
   3042 // copy_fw_pseudo $fd, $ws, n
   3043 // =>
   3044 // copy_u_w $rt, $ws, $n
   3045 // mtc1     $rt, $fd
   3046 //
   3047 // When n is zero, the equivalent operation can be performed with (potentially)
   3048 // zero instructions due to register overlaps. This optimization is never valid
   3049 // for lane 1 because it would require FR=0 mode which isn't supported by MSA.
   3050 MachineBasicBlock * MipsSETargetLowering::
   3051 emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
   3052   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3053   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3054   DebugLoc DL = MI->getDebugLoc();
   3055   unsigned Fd = MI->getOperand(0).getReg();
   3056   unsigned Ws = MI->getOperand(1).getReg();
   3057   unsigned Lane = MI->getOperand(2).getImm();
   3058 
   3059   if (Lane == 0) {
   3060     unsigned Wt = Ws;
   3061     if (!Subtarget.useOddSPReg()) {
   3062       // We must copy to an even-numbered MSA register so that the
   3063       // single-precision sub-register is also guaranteed to be even-numbered.
   3064       Wt = RegInfo.createVirtualRegister(&Mips::MSA128WEvensRegClass);
   3065 
   3066       BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Wt).addReg(Ws);
   3067     }
   3068 
   3069     BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
   3070   } else {
   3071     unsigned Wt = RegInfo.createVirtualRegister(
   3072         Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
   3073                                   &Mips::MSA128WEvensRegClass);
   3074 
   3075     BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
   3076     BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
   3077   }
   3078 
   3079   MI->eraseFromParent();   // The pseudo instruction is gone now.
   3080   return BB;
   3081 }
   3082 
   3083 // Emit the COPY_FD pseudo instruction.
   3084 //
   3085 // copy_fd_pseudo $fd, $ws, n
   3086 // =>
   3087 // splati.d $wt, $ws, $n
   3088 // copy $fd, $wt:sub_64
   3089 //
   3090 // When n is zero, the equivalent operation can be performed with (potentially)
   3091 // zero instructions due to register overlaps. This optimization is always
   3092 // valid because FR=1 mode which is the only supported mode in MSA.
   3093 MachineBasicBlock * MipsSETargetLowering::
   3094 emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
   3095   assert(Subtarget.isFP64bit());
   3096 
   3097   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3098   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3099   unsigned Fd  = MI->getOperand(0).getReg();
   3100   unsigned Ws  = MI->getOperand(1).getReg();
   3101   unsigned Lane = MI->getOperand(2).getImm() * 2;
   3102   DebugLoc DL = MI->getDebugLoc();
   3103 
   3104   if (Lane == 0)
   3105     BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
   3106   else {
   3107     unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
   3108 
   3109     BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
   3110     BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
   3111   }
   3112 
   3113   MI->eraseFromParent();   // The pseudo instruction is gone now.
   3114   return BB;
   3115 }
   3116 
   3117 // Emit the INSERT_FW pseudo instruction.
   3118 //
   3119 // insert_fw_pseudo $wd, $wd_in, $n, $fs
   3120 // =>
   3121 // subreg_to_reg $wt:sub_lo, $fs
   3122 // insve_w $wd[$n], $wd_in, $wt[0]
   3123 MachineBasicBlock *
   3124 MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
   3125                                     MachineBasicBlock *BB) const {
   3126   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3127   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3128   DebugLoc DL = MI->getDebugLoc();
   3129   unsigned Wd = MI->getOperand(0).getReg();
   3130   unsigned Wd_in = MI->getOperand(1).getReg();
   3131   unsigned Lane = MI->getOperand(2).getImm();
   3132   unsigned Fs = MI->getOperand(3).getReg();
   3133   unsigned Wt = RegInfo.createVirtualRegister(
   3134       Subtarget.useOddSPReg() ? &Mips::MSA128WRegClass :
   3135                                 &Mips::MSA128WEvensRegClass);
   3136 
   3137   BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
   3138       .addImm(0)
   3139       .addReg(Fs)
   3140       .addImm(Mips::sub_lo);
   3141   BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
   3142       .addReg(Wd_in)
   3143       .addImm(Lane)
   3144       .addReg(Wt)
   3145       .addImm(0);
   3146 
   3147   MI->eraseFromParent(); // The pseudo instruction is gone now.
   3148   return BB;
   3149 }
   3150 
   3151 // Emit the INSERT_FD pseudo instruction.
   3152 //
   3153 // insert_fd_pseudo $wd, $fs, n
   3154 // =>
   3155 // subreg_to_reg $wt:sub_64, $fs
   3156 // insve_d $wd[$n], $wd_in, $wt[0]
   3157 MachineBasicBlock *
   3158 MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
   3159                                     MachineBasicBlock *BB) const {
   3160   assert(Subtarget.isFP64bit());
   3161 
   3162   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3163   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3164   DebugLoc DL = MI->getDebugLoc();
   3165   unsigned Wd = MI->getOperand(0).getReg();
   3166   unsigned Wd_in = MI->getOperand(1).getReg();
   3167   unsigned Lane = MI->getOperand(2).getImm();
   3168   unsigned Fs = MI->getOperand(3).getReg();
   3169   unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
   3170 
   3171   BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
   3172       .addImm(0)
   3173       .addReg(Fs)
   3174       .addImm(Mips::sub_64);
   3175   BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
   3176       .addReg(Wd_in)
   3177       .addImm(Lane)
   3178       .addReg(Wt)
   3179       .addImm(0);
   3180 
   3181   MI->eraseFromParent(); // The pseudo instruction is gone now.
   3182   return BB;
   3183 }
   3184 
   3185 // Emit the INSERT_([BHWD]|F[WD])_VIDX pseudo instruction.
   3186 //
   3187 // For integer:
   3188 // (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $rs)
   3189 // =>
   3190 // (SLL $lanetmp1, $lane, <log2size)
   3191 // (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
   3192 // (INSERT_[BHWD], $wdtmp2, $wdtmp1, 0, $rs)
   3193 // (NEG $lanetmp2, $lanetmp1)
   3194 // (SLD_B $wd, $wdtmp2, $wdtmp2,  $lanetmp2)
   3195 //
   3196 // For floating point:
   3197 // (INSERT_([BHWD]|F[WD])_PSEUDO $wd, $wd_in, $n, $fs)
   3198 // =>
   3199 // (SUBREG_TO_REG $wt, $fs, <subreg>)
   3200 // (SLL $lanetmp1, $lane, <log2size)
   3201 // (SLD_B $wdtmp1, $wd_in, $wd_in, $lanetmp1)
   3202 // (INSVE_[WD], $wdtmp2, 0, $wdtmp1, 0)
   3203 // (NEG $lanetmp2, $lanetmp1)
   3204 // (SLD_B $wd, $wdtmp2, $wdtmp2,  $lanetmp2)
   3205 MachineBasicBlock *
   3206 MipsSETargetLowering::emitINSERT_DF_VIDX(MachineInstr *MI,
   3207                                          MachineBasicBlock *BB,
   3208                                          unsigned EltSizeInBytes,
   3209                                          bool IsFP) const {
   3210   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3211   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3212   DebugLoc DL = MI->getDebugLoc();
   3213   unsigned Wd = MI->getOperand(0).getReg();
   3214   unsigned SrcVecReg = MI->getOperand(1).getReg();
   3215   unsigned LaneReg = MI->getOperand(2).getReg();
   3216   unsigned SrcValReg = MI->getOperand(3).getReg();
   3217 
   3218   const TargetRegisterClass *VecRC = nullptr;
   3219   const TargetRegisterClass *GPRRC =
   3220       Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
   3221   unsigned EltLog2Size;
   3222   unsigned InsertOp = 0;
   3223   unsigned InsveOp = 0;
   3224   switch (EltSizeInBytes) {
   3225   default:
   3226     llvm_unreachable("Unexpected size");
   3227   case 1:
   3228     EltLog2Size = 0;
   3229     InsertOp = Mips::INSERT_B;
   3230     InsveOp = Mips::INSVE_B;
   3231     VecRC = &Mips::MSA128BRegClass;
   3232     break;
   3233   case 2:
   3234     EltLog2Size = 1;
   3235     InsertOp = Mips::INSERT_H;
   3236     InsveOp = Mips::INSVE_H;
   3237     VecRC = &Mips::MSA128HRegClass;
   3238     break;
   3239   case 4:
   3240     EltLog2Size = 2;
   3241     InsertOp = Mips::INSERT_W;
   3242     InsveOp = Mips::INSVE_W;
   3243     VecRC = &Mips::MSA128WRegClass;
   3244     break;
   3245   case 8:
   3246     EltLog2Size = 3;
   3247     InsertOp = Mips::INSERT_D;
   3248     InsveOp = Mips::INSVE_D;
   3249     VecRC = &Mips::MSA128DRegClass;
   3250     break;
   3251   }
   3252 
   3253   if (IsFP) {
   3254     unsigned Wt = RegInfo.createVirtualRegister(VecRC);
   3255     BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
   3256         .addImm(0)
   3257         .addReg(SrcValReg)
   3258         .addImm(EltSizeInBytes == 8 ? Mips::sub_64 : Mips::sub_lo);
   3259     SrcValReg = Wt;
   3260   }
   3261 
   3262   // Convert the lane index into a byte index
   3263   if (EltSizeInBytes != 1) {
   3264     unsigned LaneTmp1 = RegInfo.createVirtualRegister(GPRRC);
   3265     BuildMI(*BB, MI, DL, TII->get(Mips::SLL), LaneTmp1)
   3266         .addReg(LaneReg)
   3267         .addImm(EltLog2Size);
   3268     LaneReg = LaneTmp1;
   3269   }
   3270 
   3271   // Rotate bytes around so that the desired lane is element zero
   3272   unsigned WdTmp1 = RegInfo.createVirtualRegister(VecRC);
   3273   BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), WdTmp1)
   3274       .addReg(SrcVecReg)
   3275       .addReg(SrcVecReg)
   3276       .addReg(LaneReg);
   3277 
   3278   unsigned WdTmp2 = RegInfo.createVirtualRegister(VecRC);
   3279   if (IsFP) {
   3280     // Use insve.df to insert to element zero
   3281     BuildMI(*BB, MI, DL, TII->get(InsveOp), WdTmp2)
   3282         .addReg(WdTmp1)
   3283         .addImm(0)
   3284         .addReg(SrcValReg)
   3285         .addImm(0);
   3286   } else {
   3287     // Use insert.df to insert to element zero
   3288     BuildMI(*BB, MI, DL, TII->get(InsertOp), WdTmp2)
   3289         .addReg(WdTmp1)
   3290         .addReg(SrcValReg)
   3291         .addImm(0);
   3292   }
   3293 
   3294   // Rotate elements the rest of the way for a full rotation.
   3295   // sld.df inteprets $rt modulo the number of columns so we only need to negate
   3296   // the lane index to do this.
   3297   unsigned LaneTmp2 = RegInfo.createVirtualRegister(GPRRC);
   3298   BuildMI(*BB, MI, DL, TII->get(Subtarget.isABI_N64() ? Mips::DSUB : Mips::SUB),
   3299           LaneTmp2)
   3300       .addReg(Subtarget.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO)
   3301       .addReg(LaneReg);
   3302   BuildMI(*BB, MI, DL, TII->get(Mips::SLD_B), Wd)
   3303       .addReg(WdTmp2)
   3304       .addReg(WdTmp2)
   3305       .addReg(LaneTmp2);
   3306 
   3307   MI->eraseFromParent(); // The pseudo instruction is gone now.
   3308   return BB;
   3309 }
   3310 
   3311 // Emit the FILL_FW pseudo instruction.
   3312 //
   3313 // fill_fw_pseudo $wd, $fs
   3314 // =>
   3315 // implicit_def $wt1
   3316 // insert_subreg $wt2:subreg_lo, $wt1, $fs
   3317 // splati.w $wd, $wt2[0]
   3318 MachineBasicBlock *
   3319 MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
   3320                                   MachineBasicBlock *BB) const {
   3321   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3322   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3323   DebugLoc DL = MI->getDebugLoc();
   3324   unsigned Wd = MI->getOperand(0).getReg();
   3325   unsigned Fs = MI->getOperand(1).getReg();
   3326   unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
   3327   unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
   3328 
   3329   BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
   3330   BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
   3331       .addReg(Wt1)
   3332       .addReg(Fs)
   3333       .addImm(Mips::sub_lo);
   3334   BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
   3335 
   3336   MI->eraseFromParent(); // The pseudo instruction is gone now.
   3337   return BB;
   3338 }
   3339 
   3340 // Emit the FILL_FD pseudo instruction.
   3341 //
   3342 // fill_fd_pseudo $wd, $fs
   3343 // =>
   3344 // implicit_def $wt1
   3345 // insert_subreg $wt2:subreg_64, $wt1, $fs
   3346 // splati.d $wd, $wt2[0]
   3347 MachineBasicBlock *
   3348 MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
   3349                                   MachineBasicBlock *BB) const {
   3350   assert(Subtarget.isFP64bit());
   3351 
   3352   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3353   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3354   DebugLoc DL = MI->getDebugLoc();
   3355   unsigned Wd = MI->getOperand(0).getReg();
   3356   unsigned Fs = MI->getOperand(1).getReg();
   3357   unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
   3358   unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
   3359 
   3360   BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
   3361   BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
   3362       .addReg(Wt1)
   3363       .addReg(Fs)
   3364       .addImm(Mips::sub_64);
   3365   BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
   3366 
   3367   MI->eraseFromParent();   // The pseudo instruction is gone now.
   3368   return BB;
   3369 }
   3370 
   3371 // Emit the FEXP2_W_1 pseudo instructions.
   3372 //
   3373 // fexp2_w_1_pseudo $wd, $wt
   3374 // =>
   3375 // ldi.w $ws, 1
   3376 // fexp2.w $wd, $ws, $wt
   3377 MachineBasicBlock *
   3378 MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
   3379                                     MachineBasicBlock *BB) const {
   3380   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3381   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3382   const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
   3383   unsigned Ws1 = RegInfo.createVirtualRegister(RC);
   3384   unsigned Ws2 = RegInfo.createVirtualRegister(RC);
   3385   DebugLoc DL = MI->getDebugLoc();
   3386 
   3387   // Splat 1.0 into a vector
   3388   BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
   3389   BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
   3390 
   3391   // Emit 1.0 * fexp2(Wt)
   3392   BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
   3393       .addReg(Ws2)
   3394       .addReg(MI->getOperand(1).getReg());
   3395 
   3396   MI->eraseFromParent(); // The pseudo instruction is gone now.
   3397   return BB;
   3398 }
   3399 
   3400 // Emit the FEXP2_D_1 pseudo instructions.
   3401 //
   3402 // fexp2_d_1_pseudo $wd, $wt
   3403 // =>
   3404 // ldi.d $ws, 1
   3405 // fexp2.d $wd, $ws, $wt
   3406 MachineBasicBlock *
   3407 MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
   3408                                     MachineBasicBlock *BB) const {
   3409   const TargetInstrInfo *TII = Subtarget.getInstrInfo();
   3410   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
   3411   const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
   3412   unsigned Ws1 = RegInfo.createVirtualRegister(RC);
   3413   unsigned Ws2 = RegInfo.createVirtualRegister(RC);
   3414   DebugLoc DL = MI->getDebugLoc();
   3415 
   3416   // Splat 1.0 into a vector
   3417   BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
   3418   BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
   3419 
   3420   // Emit 1.0 * fexp2(Wt)
   3421   BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
   3422       .addReg(Ws2)
   3423       .addReg(MI->getOperand(1).getReg());
   3424 
   3425   MI->eraseFromParent(); // The pseudo instruction is gone now.
   3426   return BB;
   3427 }
   3428