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