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