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