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