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