9687bb90958a99f0e58032b600ca199c06112783
[oota-llvm.git] / lib / Target / Mips / MipsSEISelLowering.cpp
1 //===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Subclass of MipsTargetLowering specialized for mips32/64.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "MipsSEISelLowering.h"
14 #include "MipsRegisterInfo.h"
15 #include "MipsTargetMachine.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21
22 using namespace llvm;
23
24 static cl::opt<bool>
25 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
26                     cl::desc("MIPS: Enable tail calls."), cl::init(false));
27
28 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
29                                    cl::desc("Expand double precision loads and "
30                                             "stores to their single precision "
31                                             "counterparts"));
32
33 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
34   : MipsTargetLowering(TM) {
35   // Set up the register classes
36
37   clearRegisterClasses();
38
39   addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
40
41   if (HasMips64)
42     addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
43
44   if (Subtarget->hasDSP()) {
45     MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
46
47     for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
48       addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
49
50       // Expand all builtin opcodes.
51       for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
52         setOperationAction(Opc, VecTys[i], Expand);
53
54       setOperationAction(ISD::ADD, VecTys[i], Legal);
55       setOperationAction(ISD::SUB, VecTys[i], Legal);
56       setOperationAction(ISD::LOAD, VecTys[i], Legal);
57       setOperationAction(ISD::STORE, VecTys[i], Legal);
58       setOperationAction(ISD::BITCAST, VecTys[i], Legal);
59     }
60
61     // Expand all truncating stores and extending loads.
62     unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
63     unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
64
65     for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
66       for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
67         setTruncStoreAction((MVT::SimpleValueType)VT0,
68                             (MVT::SimpleValueType)VT1, Expand);
69
70       setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
71       setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
72       setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
73     }
74
75     setTargetDAGCombine(ISD::SHL);
76     setTargetDAGCombine(ISD::SRA);
77     setTargetDAGCombine(ISD::SRL);
78     setTargetDAGCombine(ISD::SETCC);
79     setTargetDAGCombine(ISD::VSELECT);
80   }
81
82   if (Subtarget->hasDSPR2())
83     setOperationAction(ISD::MUL, MVT::v2i16, Legal);
84
85   if (Subtarget->hasMSA()) {
86     addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
87     addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
88     addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
89     addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
90     addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
91     addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
92     addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
93
94     setTargetDAGCombine(ISD::AND);
95     setTargetDAGCombine(ISD::SRA);
96     setTargetDAGCombine(ISD::VSELECT);
97     setTargetDAGCombine(ISD::XOR);
98   }
99
100   if (!Subtarget->mipsSEUsesSoftFloat()) {
101     addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
102
103     // When dealing with single precision only, use libcalls
104     if (!Subtarget->isSingleFloat()) {
105       if (Subtarget->isFP64bit())
106         addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
107       else
108         addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
109     }
110   }
111
112   setOperationAction(ISD::SMUL_LOHI,          MVT::i32, Custom);
113   setOperationAction(ISD::UMUL_LOHI,          MVT::i32, Custom);
114   setOperationAction(ISD::MULHS,              MVT::i32, Custom);
115   setOperationAction(ISD::MULHU,              MVT::i32, Custom);
116
117   if (HasMips64) {
118     setOperationAction(ISD::MULHS,            MVT::i64, Custom);
119     setOperationAction(ISD::MULHU,            MVT::i64, Custom);
120     setOperationAction(ISD::MUL,              MVT::i64, Custom);
121   }
122
123   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
124   setOperationAction(ISD::INTRINSIC_W_CHAIN,  MVT::i64, Custom);
125
126   setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
127   setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
128   setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
129   setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
130   setOperationAction(ISD::ATOMIC_FENCE,       MVT::Other, Custom);
131   setOperationAction(ISD::LOAD,               MVT::i32, Custom);
132   setOperationAction(ISD::STORE,              MVT::i32, Custom);
133
134   setTargetDAGCombine(ISD::ADDE);
135   setTargetDAGCombine(ISD::SUBE);
136   setTargetDAGCombine(ISD::MUL);
137
138   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
139   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
140   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
141
142   if (NoDPLoadStore) {
143     setOperationAction(ISD::LOAD, MVT::f64, Custom);
144     setOperationAction(ISD::STORE, MVT::f64, Custom);
145   }
146
147   computeRegisterProperties();
148 }
149
150 const MipsTargetLowering *
151 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
152   return new MipsSETargetLowering(TM);
153 }
154
155 // Enable MSA support for the given integer type and Register class.
156 void MipsSETargetLowering::
157 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
158   addRegisterClass(Ty, RC);
159
160   // Expand all builtin opcodes.
161   for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
162     setOperationAction(Opc, Ty, Expand);
163
164   setOperationAction(ISD::BITCAST, Ty, Legal);
165   setOperationAction(ISD::LOAD, Ty, Legal);
166   setOperationAction(ISD::STORE, Ty, Legal);
167   setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
168   setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
169   setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
170
171   setOperationAction(ISD::ADD, Ty, Legal);
172   setOperationAction(ISD::AND, Ty, Legal);
173   setOperationAction(ISD::CTLZ, Ty, Legal);
174   setOperationAction(ISD::CTPOP, Ty, Legal);
175   setOperationAction(ISD::MUL, Ty, Legal);
176   setOperationAction(ISD::OR, Ty, Legal);
177   setOperationAction(ISD::SDIV, Ty, Legal);
178   setOperationAction(ISD::SHL, Ty, Legal);
179   setOperationAction(ISD::SRA, Ty, Legal);
180   setOperationAction(ISD::SRL, Ty, Legal);
181   setOperationAction(ISD::SUB, Ty, Legal);
182   setOperationAction(ISD::UDIV, Ty, Legal);
183   setOperationAction(ISD::VSELECT, Ty, Legal);
184   setOperationAction(ISD::XOR, Ty, Legal);
185
186   setOperationAction(ISD::SETCC, Ty, Legal);
187   setCondCodeAction(ISD::SETNE, Ty, Expand);
188   setCondCodeAction(ISD::SETGE, Ty, Expand);
189   setCondCodeAction(ISD::SETGT, Ty, Expand);
190   setCondCodeAction(ISD::SETUGE, Ty, Expand);
191   setCondCodeAction(ISD::SETUGT, Ty, Expand);
192 }
193
194 // Enable MSA support for the given floating-point type and Register class.
195 void MipsSETargetLowering::
196 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
197   addRegisterClass(Ty, RC);
198
199   // Expand all builtin opcodes.
200   for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
201     setOperationAction(Opc, Ty, Expand);
202
203   setOperationAction(ISD::LOAD, Ty, Legal);
204   setOperationAction(ISD::STORE, Ty, Legal);
205   setOperationAction(ISD::BITCAST, Ty, Legal);
206   setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
207
208   if (Ty != MVT::v8f16) {
209     setOperationAction(ISD::FADD,  Ty, Legal);
210     setOperationAction(ISD::FDIV,  Ty, Legal);
211     setOperationAction(ISD::FLOG2, Ty, Legal);
212     setOperationAction(ISD::FMUL,  Ty, Legal);
213     setOperationAction(ISD::FRINT, Ty, Legal);
214     setOperationAction(ISD::FSQRT, Ty, Legal);
215     setOperationAction(ISD::FSUB,  Ty, Legal);
216     setOperationAction(ISD::VSELECT, Ty, Legal);
217
218     setOperationAction(ISD::SETCC, Ty, Legal);
219     setCondCodeAction(ISD::SETOGE, Ty, Expand);
220     setCondCodeAction(ISD::SETOGT, Ty, Expand);
221     setCondCodeAction(ISD::SETUGE, Ty, Expand);
222     setCondCodeAction(ISD::SETUGT, Ty, Expand);
223     setCondCodeAction(ISD::SETGE,  Ty, Expand);
224     setCondCodeAction(ISD::SETGT,  Ty, Expand);
225   }
226 }
227
228 bool
229 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
230   MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
231
232   switch (SVT) {
233   case MVT::i64:
234   case MVT::i32:
235     if (Fast)
236       *Fast = true;
237     return true;
238   default:
239     return false;
240   }
241 }
242
243 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
244                                              SelectionDAG &DAG) const {
245   switch(Op.getOpcode()) {
246   case ISD::LOAD:  return lowerLOAD(Op, DAG);
247   case ISD::STORE: return lowerSTORE(Op, DAG);
248   case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
249   case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
250   case ISD::MULHS:     return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
251   case ISD::MULHU:     return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
252   case ISD::MUL:       return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
253   case ISD::SDIVREM:   return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
254   case ISD::UDIVREM:   return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
255                                           DAG);
256   case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
257   case ISD::INTRINSIC_W_CHAIN:  return lowerINTRINSIC_W_CHAIN(Op, DAG);
258   case ISD::INTRINSIC_VOID:     return lowerINTRINSIC_VOID(Op, DAG);
259   case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
260   case ISD::BUILD_VECTOR:       return lowerBUILD_VECTOR(Op, DAG);
261   }
262
263   return MipsTargetLowering::LowerOperation(Op, DAG);
264 }
265
266 // selectMADD -
267 // Transforms a subgraph in CurDAG if the following pattern is found:
268 //  (addc multLo, Lo0), (adde multHi, Hi0),
269 // where,
270 //  multHi/Lo: product of multiplication
271 //  Lo0: initial value of Lo register
272 //  Hi0: initial value of Hi register
273 // Return true if pattern matching was successful.
274 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
275   // ADDENode's second operand must be a flag output of an ADDC node in order
276   // for the matching to be successful.
277   SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
278
279   if (ADDCNode->getOpcode() != ISD::ADDC)
280     return false;
281
282   SDValue MultHi = ADDENode->getOperand(0);
283   SDValue MultLo = ADDCNode->getOperand(0);
284   SDNode *MultNode = MultHi.getNode();
285   unsigned MultOpc = MultHi.getOpcode();
286
287   // MultHi and MultLo must be generated by the same node,
288   if (MultLo.getNode() != MultNode)
289     return false;
290
291   // and it must be a multiplication.
292   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
293     return false;
294
295   // MultLo amd MultHi must be the first and second output of MultNode
296   // respectively.
297   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
298     return false;
299
300   // Transform this to a MADD only if ADDENode and ADDCNode are the only users
301   // of the values of MultNode, in which case MultNode will be removed in later
302   // phases.
303   // If there exist users other than ADDENode or ADDCNode, this function returns
304   // here, which will result in MultNode being mapped to a single MULT
305   // instruction node rather than a pair of MULT and MADD instructions being
306   // produced.
307   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
308     return false;
309
310   SDLoc DL(ADDENode);
311
312   // Initialize accumulator.
313   SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
314                                   ADDCNode->getOperand(1),
315                                   ADDENode->getOperand(1));
316
317   // create MipsMAdd(u) node
318   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
319
320   SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
321                                  MultNode->getOperand(0),// Factor 0
322                                  MultNode->getOperand(1),// Factor 1
323                                  ACCIn);
324
325   // replace uses of adde and addc here
326   if (!SDValue(ADDCNode, 0).use_empty()) {
327     SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
328     SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
329                                     LoIdx);
330     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
331   }
332   if (!SDValue(ADDENode, 0).use_empty()) {
333     SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
334     SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
335                                     HiIdx);
336     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
337   }
338
339   return true;
340 }
341
342 // selectMSUB -
343 // Transforms a subgraph in CurDAG if the following pattern is found:
344 //  (addc Lo0, multLo), (sube Hi0, multHi),
345 // where,
346 //  multHi/Lo: product of multiplication
347 //  Lo0: initial value of Lo register
348 //  Hi0: initial value of Hi register
349 // Return true if pattern matching was successful.
350 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
351   // SUBENode's second operand must be a flag output of an SUBC node in order
352   // for the matching to be successful.
353   SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
354
355   if (SUBCNode->getOpcode() != ISD::SUBC)
356     return false;
357
358   SDValue MultHi = SUBENode->getOperand(1);
359   SDValue MultLo = SUBCNode->getOperand(1);
360   SDNode *MultNode = MultHi.getNode();
361   unsigned MultOpc = MultHi.getOpcode();
362
363   // MultHi and MultLo must be generated by the same node,
364   if (MultLo.getNode() != MultNode)
365     return false;
366
367   // and it must be a multiplication.
368   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
369     return false;
370
371   // MultLo amd MultHi must be the first and second output of MultNode
372   // respectively.
373   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
374     return false;
375
376   // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
377   // of the values of MultNode, in which case MultNode will be removed in later
378   // phases.
379   // If there exist users other than SUBENode or SUBCNode, this function returns
380   // here, which will result in MultNode being mapped to a single MULT
381   // instruction node rather than a pair of MULT and MSUB instructions being
382   // produced.
383   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
384     return false;
385
386   SDLoc DL(SUBENode);
387
388   // Initialize accumulator.
389   SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
390                                   SUBCNode->getOperand(0),
391                                   SUBENode->getOperand(0));
392
393   // create MipsSub(u) node
394   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
395
396   SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
397                                  MultNode->getOperand(0),// Factor 0
398                                  MultNode->getOperand(1),// Factor 1
399                                  ACCIn);
400
401   // replace uses of sube and subc here
402   if (!SDValue(SUBCNode, 0).use_empty()) {
403     SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
404     SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
405                                     LoIdx);
406     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
407   }
408   if (!SDValue(SUBENode, 0).use_empty()) {
409     SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
410     SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
411                                     HiIdx);
412     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
413   }
414
415   return true;
416 }
417
418 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
419                                   TargetLowering::DAGCombinerInfo &DCI,
420                                   const MipsSubtarget *Subtarget) {
421   if (DCI.isBeforeLegalize())
422     return SDValue();
423
424   if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
425       selectMADD(N, &DAG))
426     return SDValue(N, 0);
427
428   return SDValue();
429 }
430
431 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
432 //
433 // Performs the following transformations:
434 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
435 //   sign/zero-extension is completely overwritten by the new one performed by
436 //   the ISD::AND.
437 // - Removes redundant zero extensions performed by an ISD::AND.
438 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
439                                  TargetLowering::DAGCombinerInfo &DCI,
440                                  const MipsSubtarget *Subtarget) {
441   if (!Subtarget->hasMSA())
442     return SDValue();
443
444   SDValue Op0 = N->getOperand(0);
445   SDValue Op1 = N->getOperand(1);
446   unsigned Op0Opcode = Op0->getOpcode();
447
448   // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
449   // where $d + 1 == 2^n and n == 32
450   // or    $d + 1 == 2^n and n <= 32 and ZExt
451   // -> (MipsVExtractZExt $a, $b, $c)
452   if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
453       Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
454     ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
455
456     if (!Mask)
457       return SDValue();
458
459     int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
460
461     if (Log2IfPositive <= 0)
462       return SDValue(); // Mask+1 is not a power of 2
463
464     SDValue Op0Op2 = Op0->getOperand(2);
465     EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
466     unsigned ExtendTySize = ExtendTy.getSizeInBits();
467     unsigned Log2 = Log2IfPositive;
468
469     if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
470         Log2 == ExtendTySize) {
471       SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
472       DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
473                       Op0->getVTList(), Ops, Op0->getNumOperands());
474       return Op0;
475     }
476   }
477
478   return SDValue();
479 }
480
481 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
482                                   TargetLowering::DAGCombinerInfo &DCI,
483                                   const MipsSubtarget *Subtarget) {
484   if (DCI.isBeforeLegalize())
485     return SDValue();
486
487   if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
488       selectMSUB(N, &DAG))
489     return SDValue(N, 0);
490
491   return SDValue();
492 }
493
494 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
495                             EVT ShiftTy, SelectionDAG &DAG) {
496   // Clear the upper (64 - VT.sizeInBits) bits.
497   C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
498
499   // Return 0.
500   if (C == 0)
501     return DAG.getConstant(0, VT);
502
503   // Return x.
504   if (C == 1)
505     return X;
506
507   // If c is power of 2, return (shl x, log2(c)).
508   if (isPowerOf2_64(C))
509     return DAG.getNode(ISD::SHL, DL, VT, X,
510                        DAG.getConstant(Log2_64(C), ShiftTy));
511
512   unsigned Log2Ceil = Log2_64_Ceil(C);
513   uint64_t Floor = 1LL << Log2_64(C);
514   uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
515
516   // If |c - floor_c| <= |c - ceil_c|,
517   // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
518   // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
519   if (C - Floor <= Ceil - C) {
520     SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
521     SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
522     return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
523   }
524
525   // If |c - floor_c| > |c - ceil_c|,
526   // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
527   SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
528   SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
529   return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
530 }
531
532 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
533                                  const TargetLowering::DAGCombinerInfo &DCI,
534                                  const MipsSETargetLowering *TL) {
535   EVT VT = N->getValueType(0);
536
537   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
538     if (!VT.isVector())
539       return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
540                           VT, TL->getScalarShiftAmountTy(VT), DAG);
541
542   return SDValue(N, 0);
543 }
544
545 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
546                                       SelectionDAG &DAG,
547                                       const MipsSubtarget *Subtarget) {
548   // See if this is a vector splat immediate node.
549   APInt SplatValue, SplatUndef;
550   unsigned SplatBitSize;
551   bool HasAnyUndefs;
552   unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
553   BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
554
555   if (!BV ||
556       !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
557                            EltSize, !Subtarget->isLittle()) ||
558       (SplatBitSize != EltSize) ||
559       (SplatValue.getZExtValue() >= EltSize))
560     return SDValue();
561
562   return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
563                      DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
564 }
565
566 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
567                                  TargetLowering::DAGCombinerInfo &DCI,
568                                  const MipsSubtarget *Subtarget) {
569   EVT Ty = N->getValueType(0);
570
571   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
572     return SDValue();
573
574   return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
575 }
576
577 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
578 // constant splats into MipsISD::SHRA_DSP for DSPr2.
579 //
580 // Performs the following transformations:
581 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
582 //   sign/zero-extension is completely overwritten by the new one performed by
583 //   the ISD::SRA and ISD::SHL nodes.
584 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
585 //   sequence.
586 //
587 // See performDSPShiftCombine for more information about the transformation
588 // used for DSPr2.
589 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
590                                  TargetLowering::DAGCombinerInfo &DCI,
591                                  const MipsSubtarget *Subtarget) {
592   EVT Ty = N->getValueType(0);
593
594   if (Subtarget->hasMSA()) {
595     SDValue Op0 = N->getOperand(0);
596     SDValue Op1 = N->getOperand(1);
597
598     // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
599     // where $d + sizeof($c) == 32
600     // or    $d + sizeof($c) <= 32 and SExt
601     // -> (MipsVExtractSExt $a, $b, $c)
602     if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
603       SDValue Op0Op0 = Op0->getOperand(0);
604       ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
605
606       if (!ShAmount)
607         return SDValue();
608
609       EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
610       unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
611
612       if (TotalBits == 32 ||
613           (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
614            TotalBits <= 32)) {
615         SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
616                           Op0Op0->getOperand(2) };
617         DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
618                         Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
619         return Op0Op0;
620       }
621     }
622   }
623
624   if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
625     return SDValue();
626
627   return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
628 }
629
630
631 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
632                                  TargetLowering::DAGCombinerInfo &DCI,
633                                  const MipsSubtarget *Subtarget) {
634   EVT Ty = N->getValueType(0);
635
636   if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
637     return SDValue();
638
639   return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
640 }
641
642 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
643   bool IsV216 = (Ty == MVT::v2i16);
644
645   switch (CC) {
646   case ISD::SETEQ:
647   case ISD::SETNE:  return true;
648   case ISD::SETLT:
649   case ISD::SETLE:
650   case ISD::SETGT:
651   case ISD::SETGE:  return IsV216;
652   case ISD::SETULT:
653   case ISD::SETULE:
654   case ISD::SETUGT:
655   case ISD::SETUGE: return !IsV216;
656   default:          return false;
657   }
658 }
659
660 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
661   EVT Ty = N->getValueType(0);
662
663   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
664     return SDValue();
665
666   if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
667     return SDValue();
668
669   return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
670                      N->getOperand(1), N->getOperand(2));
671 }
672
673 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
674   EVT Ty = N->getValueType(0);
675
676   if (Ty.is128BitVector() && Ty.isInteger()) {
677     // Try the following combines:
678     //   (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
679     //   (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
680     //   (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
681     //   (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
682     //   (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
683     //   (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
684     //   (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
685     //   (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
686     // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
687     // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
688     // legalizer.
689     SDValue Op0 = N->getOperand(0);
690
691     if (Op0->getOpcode() != ISD::SETCC)
692       return SDValue();
693
694     ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
695     bool Signed;
696
697     if (CondCode == ISD::SETLT  || CondCode == ISD::SETLE)
698       Signed = true;
699     else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
700       Signed = false;
701     else
702       return SDValue();
703
704     SDValue Op1 = N->getOperand(1);
705     SDValue Op2 = N->getOperand(2);
706     SDValue Op0Op0 = Op0->getOperand(0);
707     SDValue Op0Op1 = Op0->getOperand(1);
708
709     if (Op1 == Op0Op0 && Op2 == Op0Op1)
710       return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
711                          Ty, Op1, Op2);
712     else if (Op1 == Op0Op1 && Op2 == Op0Op0)
713       return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
714                          Ty, Op1, Op2);
715   } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
716     SDValue SetCC = N->getOperand(0);
717
718     if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
719       return SDValue();
720
721     return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
722                        SetCC.getOperand(0), SetCC.getOperand(1),
723                        N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
724   }
725
726   return SDValue();
727 }
728
729 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
730                                  const MipsSubtarget *Subtarget) {
731   EVT Ty = N->getValueType(0);
732
733   if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
734     // Try the following combines:
735     //   (xor (or $a, $b), (build_vector allones))
736     //   (xor (or $a, $b), (bitcast (build_vector allones)))
737     SDValue Op0 = N->getOperand(0);
738     SDValue Op1 = N->getOperand(1);
739     SDValue NotOp;
740     ConstantSDNode *Const;
741
742     if (ISD::isBuildVectorAllOnes(Op0.getNode()))
743       NotOp = Op1;
744     else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
745       NotOp = Op0;
746     else if ((Op0->getOpcode() == MipsISD::VSPLAT ||
747               Op0->getOpcode() == MipsISD::VSPLATD) &&
748              (Const = dyn_cast<ConstantSDNode>(Op0->getOperand(0))) &&
749              Const->isAllOnesValue())
750       NotOp = Op1;
751     else if ((Op1->getOpcode() == MipsISD::VSPLAT ||
752               Op1->getOpcode() == MipsISD::VSPLATD) &&
753              (Const = dyn_cast<ConstantSDNode>(Op1->getOperand(0))) &&
754              Const->isAllOnesValue())
755       NotOp = Op0;
756     else
757       return SDValue();
758
759     if (NotOp->getOpcode() == ISD::OR)
760       return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
761                          NotOp->getOperand(1));
762   }
763
764   return SDValue();
765 }
766
767 SDValue
768 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
769   SelectionDAG &DAG = DCI.DAG;
770   SDValue Val;
771
772   switch (N->getOpcode()) {
773   case ISD::ADDE:
774     return performADDECombine(N, DAG, DCI, Subtarget);
775   case ISD::AND:
776     Val = performANDCombine(N, DAG, DCI, Subtarget);
777     break;
778   case ISD::SUBE:
779     return performSUBECombine(N, DAG, DCI, Subtarget);
780   case ISD::MUL:
781     return performMULCombine(N, DAG, DCI, this);
782   case ISD::SHL:
783     return performSHLCombine(N, DAG, DCI, Subtarget);
784   case ISD::SRA:
785     return performSRACombine(N, DAG, DCI, Subtarget);
786   case ISD::SRL:
787     return performSRLCombine(N, DAG, DCI, Subtarget);
788   case ISD::VSELECT:
789     return performVSELECTCombine(N, DAG);
790   case ISD::XOR:
791     Val = performXORCombine(N, DAG, Subtarget);
792     break;
793   case ISD::SETCC:
794     Val = performSETCCCombine(N, DAG);
795     break;
796   }
797
798   if (Val.getNode())
799     return Val;
800
801   return MipsTargetLowering::PerformDAGCombine(N, DCI);
802 }
803
804 MachineBasicBlock *
805 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
806                                                   MachineBasicBlock *BB) const {
807   switch (MI->getOpcode()) {
808   default:
809     return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
810   case Mips::BPOSGE32_PSEUDO:
811     return emitBPOSGE32(MI, BB);
812   case Mips::SNZ_B_PSEUDO:
813     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
814   case Mips::SNZ_H_PSEUDO:
815     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
816   case Mips::SNZ_W_PSEUDO:
817     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
818   case Mips::SNZ_D_PSEUDO:
819     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
820   case Mips::SNZ_V_PSEUDO:
821     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
822   case Mips::SZ_B_PSEUDO:
823     return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
824   case Mips::SZ_H_PSEUDO:
825     return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
826   case Mips::SZ_W_PSEUDO:
827     return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
828   case Mips::SZ_D_PSEUDO:
829     return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
830   case Mips::SZ_V_PSEUDO:
831     return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
832   }
833 }
834
835 bool MipsSETargetLowering::
836 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
837                                   unsigned NextStackOffset,
838                                   const MipsFunctionInfo& FI) const {
839   if (!EnableMipsTailCalls)
840     return false;
841
842   // Return false if either the callee or caller has a byval argument.
843   if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
844     return false;
845
846   // Return true if the callee's argument area is no larger than the
847   // caller's.
848   return NextStackOffset <= FI.getIncomingArgSize();
849 }
850
851 void MipsSETargetLowering::
852 getOpndList(SmallVectorImpl<SDValue> &Ops,
853             std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
854             bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
855             CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
856   // T9 should contain the address of the callee function if
857   // -reloction-model=pic or it is an indirect call.
858   if (IsPICCall || !GlobalOrExternal) {
859     unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
860     RegsToPass.push_front(std::make_pair(T9Reg, Callee));
861   } else
862     Ops.push_back(Callee);
863
864   MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
865                                   InternalLinkage, CLI, Callee, Chain);
866 }
867
868 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
869   LoadSDNode &Nd = *cast<LoadSDNode>(Op);
870
871   if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
872     return MipsTargetLowering::lowerLOAD(Op, DAG);
873
874   // Replace a double precision load with two i32 loads and a buildpair64.
875   SDLoc DL(Op);
876   SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
877   EVT PtrVT = Ptr.getValueType();
878
879   // i32 load from lower address.
880   SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
881                            MachinePointerInfo(), Nd.isVolatile(),
882                            Nd.isNonTemporal(), Nd.isInvariant(),
883                            Nd.getAlignment());
884
885   // i32 load from higher address.
886   Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
887   SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
888                            MachinePointerInfo(), Nd.isVolatile(),
889                            Nd.isNonTemporal(), Nd.isInvariant(),
890                            std::min(Nd.getAlignment(), 4U));
891
892   if (!Subtarget->isLittle())
893     std::swap(Lo, Hi);
894
895   SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
896   SDValue Ops[2] = {BP, Hi.getValue(1)};
897   return DAG.getMergeValues(Ops, 2, DL);
898 }
899
900 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
901   StoreSDNode &Nd = *cast<StoreSDNode>(Op);
902
903   if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
904     return MipsTargetLowering::lowerSTORE(Op, DAG);
905
906   // Replace a double precision store with two extractelement64s and i32 stores.
907   SDLoc DL(Op);
908   SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
909   EVT PtrVT = Ptr.getValueType();
910   SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
911                            Val, DAG.getConstant(0, MVT::i32));
912   SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
913                            Val, DAG.getConstant(1, MVT::i32));
914
915   if (!Subtarget->isLittle())
916     std::swap(Lo, Hi);
917
918   // i32 store to lower address.
919   Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
920                        Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
921                        Nd.getTBAAInfo());
922
923   // i32 store to higher address.
924   Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
925   return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
926                       Nd.isVolatile(), Nd.isNonTemporal(),
927                       std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
928 }
929
930 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
931                                           bool HasLo, bool HasHi,
932                                           SelectionDAG &DAG) const {
933   EVT Ty = Op.getOperand(0).getValueType();
934   SDLoc DL(Op);
935   SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
936                              Op.getOperand(0), Op.getOperand(1));
937   SDValue Lo, Hi;
938
939   if (HasLo)
940     Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
941                      DAG.getConstant(Mips::sub_lo, MVT::i32));
942   if (HasHi)
943     Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
944                      DAG.getConstant(Mips::sub_hi, MVT::i32));
945
946   if (!HasLo || !HasHi)
947     return HasLo ? Lo : Hi;
948
949   SDValue Vals[] = { Lo, Hi };
950   return DAG.getMergeValues(Vals, 2, DL);
951 }
952
953
954 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
955   SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
956                              DAG.getConstant(0, MVT::i32));
957   SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
958                              DAG.getConstant(1, MVT::i32));
959   return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
960 }
961
962 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
963   SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
964                            DAG.getConstant(Mips::sub_lo, MVT::i32));
965   SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
966                            DAG.getConstant(Mips::sub_hi, MVT::i32));
967   return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
968 }
969
970 // This function expands mips intrinsic nodes which have 64-bit input operands
971 // or output values.
972 //
973 // out64 = intrinsic-node in64
974 // =>
975 // lo = copy (extract-element (in64, 0))
976 // hi = copy (extract-element (in64, 1))
977 // mips-specific-node
978 // v0 = copy lo
979 // v1 = copy hi
980 // out64 = merge-values (v0, v1)
981 //
982 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
983   SDLoc DL(Op);
984   bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
985   SmallVector<SDValue, 3> Ops;
986   unsigned OpNo = 0;
987
988   // See if Op has a chain input.
989   if (HasChainIn)
990     Ops.push_back(Op->getOperand(OpNo++));
991
992   // The next operand is the intrinsic opcode.
993   assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
994
995   // See if the next operand has type i64.
996   SDValue Opnd = Op->getOperand(++OpNo), In64;
997
998   if (Opnd.getValueType() == MVT::i64)
999     In64 = initAccumulator(Opnd, DL, DAG);
1000   else
1001     Ops.push_back(Opnd);
1002
1003   // Push the remaining operands.
1004   for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1005     Ops.push_back(Op->getOperand(OpNo));
1006
1007   // Add In64 to the end of the list.
1008   if (In64.getNode())
1009     Ops.push_back(In64);
1010
1011   // Scan output.
1012   SmallVector<EVT, 2> ResTys;
1013
1014   for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1015        I != E; ++I)
1016     ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1017
1018   // Create node.
1019   SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1020   SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1021
1022   if (!HasChainIn)
1023     return Out;
1024
1025   assert(Val->getValueType(1) == MVT::Other);
1026   SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1027   return DAG.getMergeValues(Vals, 2, DL);
1028 }
1029
1030 static SDValue lowerMSABinaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1031   SDLoc DL(Op);
1032   SDValue LHS = Op->getOperand(1);
1033   SDValue RHS = Op->getOperand(2);
1034   EVT ResTy = Op->getValueType(0);
1035
1036   SDValue Result = DAG.getNode(Opc, DL, ResTy, LHS, RHS);
1037
1038   return Result;
1039 }
1040
1041 static SDValue lowerMSABinaryImmIntr(SDValue Op, SelectionDAG &DAG,
1042                                      unsigned Opc, SDValue RHS) {
1043   SDValue LHS = Op->getOperand(1);
1044   EVT ResTy = Op->getValueType(0);
1045
1046   return DAG.getNode(Opc, SDLoc(Op), ResTy, LHS, RHS);
1047 }
1048
1049 static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1050   SDLoc DL(Op);
1051   SDValue Value = Op->getOperand(1);
1052   EVT ResTy = Op->getValueType(0);
1053
1054   SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1055
1056   return Result;
1057 }
1058
1059 // Lower an MSA copy intrinsic into the specified SelectionDAG node
1060 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1061   SDLoc DL(Op);
1062   SDValue Vec = Op->getOperand(1);
1063   SDValue Idx = Op->getOperand(2);
1064   EVT ResTy = Op->getValueType(0);
1065   EVT EltTy = Vec->getValueType(0).getVectorElementType();
1066
1067   SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1068                                DAG.getValueType(EltTy));
1069
1070   return Result;
1071 }
1072
1073 // Lower an MSA insert intrinsic into the specified SelectionDAG node
1074 static SDValue lowerMSAInsertIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1075   SDLoc DL(Op);
1076   SDValue Op0 = Op->getOperand(1);
1077   SDValue Op1 = Op->getOperand(2);
1078   SDValue Op2 = Op->getOperand(3);
1079   EVT ResTy = Op->getValueType(0);
1080
1081   SDValue Result = DAG.getNode(Opc, DL, ResTy, Op0, Op2, Op1);
1082
1083   return Result;
1084 }
1085
1086 static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1087   EVT ResTy = Op->getValueType(0);
1088
1089   unsigned SplatOp = MipsISD::VSPLAT;
1090   if (ResTy == MVT::v2i64)
1091     SplatOp = MipsISD::VSPLATD;
1092
1093   return DAG.getNode(SplatOp, SDLoc(Op), ResTy, Op->getOperand(ImmOp));
1094 }
1095
1096 static SDValue lowerMSAUnaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1097   SDLoc DL(Op);
1098   SDValue Value = Op->getOperand(1);
1099   EVT ResTy = Op->getValueType(0);
1100
1101   SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1102
1103   return Result;
1104 }
1105
1106 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1107                                                       SelectionDAG &DAG) const {
1108   switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1109   default:
1110     return SDValue();
1111   case Intrinsic::mips_shilo:
1112     return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1113   case Intrinsic::mips_dpau_h_qbl:
1114     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1115   case Intrinsic::mips_dpau_h_qbr:
1116     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1117   case Intrinsic::mips_dpsu_h_qbl:
1118     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1119   case Intrinsic::mips_dpsu_h_qbr:
1120     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1121   case Intrinsic::mips_dpa_w_ph:
1122     return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1123   case Intrinsic::mips_dps_w_ph:
1124     return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1125   case Intrinsic::mips_dpax_w_ph:
1126     return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1127   case Intrinsic::mips_dpsx_w_ph:
1128     return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1129   case Intrinsic::mips_mulsa_w_ph:
1130     return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1131   case Intrinsic::mips_mult:
1132     return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1133   case Intrinsic::mips_multu:
1134     return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1135   case Intrinsic::mips_madd:
1136     return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1137   case Intrinsic::mips_maddu:
1138     return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1139   case Intrinsic::mips_msub:
1140     return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1141   case Intrinsic::mips_msubu:
1142     return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
1143   case Intrinsic::mips_addv_b:
1144   case Intrinsic::mips_addv_h:
1145   case Intrinsic::mips_addv_w:
1146   case Intrinsic::mips_addv_d:
1147     return lowerMSABinaryIntr(Op, DAG, ISD::ADD);
1148   case Intrinsic::mips_addvi_b:
1149   case Intrinsic::mips_addvi_h:
1150   case Intrinsic::mips_addvi_w:
1151   case Intrinsic::mips_addvi_d:
1152     return lowerMSABinaryImmIntr(Op, DAG, ISD::ADD,
1153                                  lowerMSASplatImm(Op, 2, DAG));
1154   case Intrinsic::mips_and_v:
1155     return lowerMSABinaryIntr(Op, DAG, ISD::AND);
1156   case Intrinsic::mips_bnz_b:
1157   case Intrinsic::mips_bnz_h:
1158   case Intrinsic::mips_bnz_w:
1159   case Intrinsic::mips_bnz_d:
1160     return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
1161   case Intrinsic::mips_bnz_v:
1162     return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
1163   case Intrinsic::mips_bsel_v:
1164     return DAG.getNode(ISD::VSELECT, SDLoc(Op), Op->getValueType(0),
1165                        Op->getOperand(1), Op->getOperand(2),
1166                        Op->getOperand(3));
1167   case Intrinsic::mips_bseli_b:
1168     return DAG.getNode(ISD::VSELECT, SDLoc(Op), Op->getValueType(0),
1169                        Op->getOperand(1), Op->getOperand(2),
1170                        lowerMSASplatImm(Op, 3, DAG));
1171   case Intrinsic::mips_bz_b:
1172   case Intrinsic::mips_bz_h:
1173   case Intrinsic::mips_bz_w:
1174   case Intrinsic::mips_bz_d:
1175     return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
1176   case Intrinsic::mips_bz_v:
1177     return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
1178   case Intrinsic::mips_ceq_b:
1179   case Intrinsic::mips_ceq_h:
1180   case Intrinsic::mips_ceq_w:
1181   case Intrinsic::mips_ceq_d:
1182     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1183                         Op->getOperand(2), ISD::SETEQ);
1184   case Intrinsic::mips_ceqi_b:
1185   case Intrinsic::mips_ceqi_h:
1186   case Intrinsic::mips_ceqi_w:
1187   case Intrinsic::mips_ceqi_d:
1188     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1189                         lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1190   case Intrinsic::mips_cle_s_b:
1191   case Intrinsic::mips_cle_s_h:
1192   case Intrinsic::mips_cle_s_w:
1193   case Intrinsic::mips_cle_s_d:
1194     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1195                         Op->getOperand(2), ISD::SETLE);
1196   case Intrinsic::mips_clei_s_b:
1197   case Intrinsic::mips_clei_s_h:
1198   case Intrinsic::mips_clei_s_w:
1199   case Intrinsic::mips_clei_s_d:
1200     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1201                         lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1202   case Intrinsic::mips_cle_u_b:
1203   case Intrinsic::mips_cle_u_h:
1204   case Intrinsic::mips_cle_u_w:
1205   case Intrinsic::mips_cle_u_d:
1206     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1207                         Op->getOperand(2), ISD::SETULE);
1208   case Intrinsic::mips_clei_u_b:
1209   case Intrinsic::mips_clei_u_h:
1210   case Intrinsic::mips_clei_u_w:
1211   case Intrinsic::mips_clei_u_d:
1212     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1213                         lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1214   case Intrinsic::mips_clt_s_b:
1215   case Intrinsic::mips_clt_s_h:
1216   case Intrinsic::mips_clt_s_w:
1217   case Intrinsic::mips_clt_s_d:
1218     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1219                         Op->getOperand(2), ISD::SETLT);
1220   case Intrinsic::mips_clti_s_b:
1221   case Intrinsic::mips_clti_s_h:
1222   case Intrinsic::mips_clti_s_w:
1223   case Intrinsic::mips_clti_s_d:
1224     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1225                         lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1226   case Intrinsic::mips_clt_u_b:
1227   case Intrinsic::mips_clt_u_h:
1228   case Intrinsic::mips_clt_u_w:
1229   case Intrinsic::mips_clt_u_d:
1230     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1231                         Op->getOperand(2), ISD::SETULT);
1232   case Intrinsic::mips_clti_u_b:
1233   case Intrinsic::mips_clti_u_h:
1234   case Intrinsic::mips_clti_u_w:
1235   case Intrinsic::mips_clti_u_d:
1236     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1237                         lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
1238   case Intrinsic::mips_copy_s_b:
1239   case Intrinsic::mips_copy_s_h:
1240   case Intrinsic::mips_copy_s_w:
1241     return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1242   case Intrinsic::mips_copy_u_b:
1243   case Intrinsic::mips_copy_u_h:
1244   case Intrinsic::mips_copy_u_w:
1245     return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1246   case Intrinsic::mips_div_s_b:
1247   case Intrinsic::mips_div_s_h:
1248   case Intrinsic::mips_div_s_w:
1249   case Intrinsic::mips_div_s_d:
1250     return lowerMSABinaryIntr(Op, DAG, ISD::SDIV);
1251   case Intrinsic::mips_div_u_b:
1252   case Intrinsic::mips_div_u_h:
1253   case Intrinsic::mips_div_u_w:
1254   case Intrinsic::mips_div_u_d:
1255     return lowerMSABinaryIntr(Op, DAG, ISD::UDIV);
1256   case Intrinsic::mips_fadd_w:
1257   case Intrinsic::mips_fadd_d:
1258     return lowerMSABinaryIntr(Op, DAG, ISD::FADD);
1259   // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1260   case Intrinsic::mips_fceq_w:
1261   case Intrinsic::mips_fceq_d:
1262     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1263                         Op->getOperand(2), ISD::SETOEQ);
1264   case Intrinsic::mips_fcle_w:
1265   case Intrinsic::mips_fcle_d:
1266     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1267                         Op->getOperand(2), ISD::SETOLE);
1268   case Intrinsic::mips_fclt_w:
1269   case Intrinsic::mips_fclt_d:
1270     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1271                         Op->getOperand(2), ISD::SETOLT);
1272   case Intrinsic::mips_fcne_w:
1273   case Intrinsic::mips_fcne_d:
1274     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1275                         Op->getOperand(2), ISD::SETONE);
1276   case Intrinsic::mips_fcor_w:
1277   case Intrinsic::mips_fcor_d:
1278     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1279                         Op->getOperand(2), ISD::SETO);
1280   case Intrinsic::mips_fcueq_w:
1281   case Intrinsic::mips_fcueq_d:
1282     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1283                         Op->getOperand(2), ISD::SETUEQ);
1284   case Intrinsic::mips_fcule_w:
1285   case Intrinsic::mips_fcule_d:
1286     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1287                         Op->getOperand(2), ISD::SETULE);
1288   case Intrinsic::mips_fcult_w:
1289   case Intrinsic::mips_fcult_d:
1290     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1291                         Op->getOperand(2), ISD::SETULT);
1292   case Intrinsic::mips_fcun_w:
1293   case Intrinsic::mips_fcun_d:
1294     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1295                         Op->getOperand(2), ISD::SETUO);
1296   case Intrinsic::mips_fcune_w:
1297   case Intrinsic::mips_fcune_d:
1298     return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1299                         Op->getOperand(2), ISD::SETUNE);
1300   case Intrinsic::mips_fdiv_w:
1301   case Intrinsic::mips_fdiv_d:
1302     return lowerMSABinaryIntr(Op, DAG, ISD::FDIV);
1303   case Intrinsic::mips_fill_b:
1304   case Intrinsic::mips_fill_h:
1305   case Intrinsic::mips_fill_w:
1306     return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
1307   case Intrinsic::mips_flog2_w:
1308   case Intrinsic::mips_flog2_d:
1309     return lowerMSAUnaryIntr(Op, DAG, ISD::FLOG2);
1310   case Intrinsic::mips_fmul_w:
1311   case Intrinsic::mips_fmul_d:
1312     return lowerMSABinaryIntr(Op, DAG, ISD::FMUL);
1313   case Intrinsic::mips_frint_w:
1314   case Intrinsic::mips_frint_d:
1315     return lowerMSAUnaryIntr(Op, DAG, ISD::FRINT);
1316   case Intrinsic::mips_fsqrt_w:
1317   case Intrinsic::mips_fsqrt_d:
1318     return lowerMSAUnaryIntr(Op, DAG, ISD::FSQRT);
1319   case Intrinsic::mips_fsub_w:
1320   case Intrinsic::mips_fsub_d:
1321     return lowerMSABinaryIntr(Op, DAG, ISD::FSUB);
1322   case Intrinsic::mips_insert_b:
1323   case Intrinsic::mips_insert_h:
1324   case Intrinsic::mips_insert_w:
1325     return lowerMSAInsertIntr(Op, DAG, ISD::INSERT_VECTOR_ELT);
1326   case Intrinsic::mips_ldi_b:
1327   case Intrinsic::mips_ldi_h:
1328   case Intrinsic::mips_ldi_w:
1329   case Intrinsic::mips_ldi_d:
1330     return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
1331   case Intrinsic::mips_max_s_b:
1332   case Intrinsic::mips_max_s_h:
1333   case Intrinsic::mips_max_s_w:
1334   case Intrinsic::mips_max_s_d:
1335     return lowerMSABinaryIntr(Op, DAG, MipsISD::VSMAX);
1336   case Intrinsic::mips_max_u_b:
1337   case Intrinsic::mips_max_u_h:
1338   case Intrinsic::mips_max_u_w:
1339   case Intrinsic::mips_max_u_d:
1340     return lowerMSABinaryIntr(Op, DAG, MipsISD::VUMAX);
1341   case Intrinsic::mips_maxi_s_b:
1342   case Intrinsic::mips_maxi_s_h:
1343   case Intrinsic::mips_maxi_s_w:
1344   case Intrinsic::mips_maxi_s_d:
1345     return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VSMAX,
1346                                  lowerMSASplatImm(Op, 2, DAG));
1347   case Intrinsic::mips_maxi_u_b:
1348   case Intrinsic::mips_maxi_u_h:
1349   case Intrinsic::mips_maxi_u_w:
1350   case Intrinsic::mips_maxi_u_d:
1351     return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VUMAX,
1352                                  lowerMSASplatImm(Op, 2, DAG));
1353   case Intrinsic::mips_min_s_b:
1354   case Intrinsic::mips_min_s_h:
1355   case Intrinsic::mips_min_s_w:
1356   case Intrinsic::mips_min_s_d:
1357     return lowerMSABinaryIntr(Op, DAG, MipsISD::VSMIN);
1358   case Intrinsic::mips_min_u_b:
1359   case Intrinsic::mips_min_u_h:
1360   case Intrinsic::mips_min_u_w:
1361   case Intrinsic::mips_min_u_d:
1362     return lowerMSABinaryIntr(Op, DAG, MipsISD::VUMIN);
1363   case Intrinsic::mips_mini_s_b:
1364   case Intrinsic::mips_mini_s_h:
1365   case Intrinsic::mips_mini_s_w:
1366   case Intrinsic::mips_mini_s_d:
1367     return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VSMIN,
1368                                  lowerMSASplatImm(Op, 2, DAG));
1369   case Intrinsic::mips_mini_u_b:
1370   case Intrinsic::mips_mini_u_h:
1371   case Intrinsic::mips_mini_u_w:
1372   case Intrinsic::mips_mini_u_d:
1373     return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VUMIN,
1374                                  lowerMSASplatImm(Op, 2, DAG));
1375   case Intrinsic::mips_mulv_b:
1376   case Intrinsic::mips_mulv_h:
1377   case Intrinsic::mips_mulv_w:
1378   case Intrinsic::mips_mulv_d:
1379     return lowerMSABinaryIntr(Op, DAG, ISD::MUL);
1380   case Intrinsic::mips_nlzc_b:
1381   case Intrinsic::mips_nlzc_h:
1382   case Intrinsic::mips_nlzc_w:
1383   case Intrinsic::mips_nlzc_d:
1384     return lowerMSAUnaryIntr(Op, DAG, ISD::CTLZ);
1385   case Intrinsic::mips_nor_v: {
1386     SDValue Res = lowerMSABinaryIntr(Op, DAG, ISD::OR);
1387     return DAG.getNOT(SDLoc(Op), Res, Res->getValueType(0));
1388   }
1389   case Intrinsic::mips_or_v:
1390     return lowerMSABinaryIntr(Op, DAG, ISD::OR);
1391   case Intrinsic::mips_pcnt_b:
1392   case Intrinsic::mips_pcnt_h:
1393   case Intrinsic::mips_pcnt_w:
1394   case Intrinsic::mips_pcnt_d:
1395     return lowerMSAUnaryIntr(Op, DAG, ISD::CTPOP);
1396   case Intrinsic::mips_sll_b:
1397   case Intrinsic::mips_sll_h:
1398   case Intrinsic::mips_sll_w:
1399   case Intrinsic::mips_sll_d:
1400     return lowerMSABinaryIntr(Op, DAG, ISD::SHL);
1401   case Intrinsic::mips_slli_b:
1402   case Intrinsic::mips_slli_h:
1403   case Intrinsic::mips_slli_w:
1404   case Intrinsic::mips_slli_d:
1405     return lowerMSABinaryImmIntr(Op, DAG, ISD::SHL,
1406                                  lowerMSASplatImm(Op, 2, DAG));
1407   case Intrinsic::mips_sra_b:
1408   case Intrinsic::mips_sra_h:
1409   case Intrinsic::mips_sra_w:
1410   case Intrinsic::mips_sra_d:
1411     return lowerMSABinaryIntr(Op, DAG, ISD::SRA);
1412   case Intrinsic::mips_srai_b:
1413   case Intrinsic::mips_srai_h:
1414   case Intrinsic::mips_srai_w:
1415   case Intrinsic::mips_srai_d:
1416     return lowerMSABinaryImmIntr(Op, DAG, ISD::SRA,
1417                                  lowerMSASplatImm(Op, 2, DAG));
1418   case Intrinsic::mips_srl_b:
1419   case Intrinsic::mips_srl_h:
1420   case Intrinsic::mips_srl_w:
1421   case Intrinsic::mips_srl_d:
1422     return lowerMSABinaryIntr(Op, DAG, ISD::SRL);
1423   case Intrinsic::mips_srli_b:
1424   case Intrinsic::mips_srli_h:
1425   case Intrinsic::mips_srli_w:
1426   case Intrinsic::mips_srli_d:
1427     return lowerMSABinaryImmIntr(Op, DAG, ISD::SRL,
1428                                  lowerMSASplatImm(Op, 2, DAG));
1429   case Intrinsic::mips_subv_b:
1430   case Intrinsic::mips_subv_h:
1431   case Intrinsic::mips_subv_w:
1432   case Intrinsic::mips_subv_d:
1433     return lowerMSABinaryIntr(Op, DAG, ISD::SUB);
1434   case Intrinsic::mips_subvi_b:
1435   case Intrinsic::mips_subvi_h:
1436   case Intrinsic::mips_subvi_w:
1437   case Intrinsic::mips_subvi_d:
1438     return lowerMSABinaryImmIntr(Op, DAG, ISD::SUB,
1439                                  lowerMSASplatImm(Op, 2, DAG));
1440   case Intrinsic::mips_xor_v:
1441     return lowerMSABinaryIntr(Op, DAG, ISD::XOR);
1442   }
1443 }
1444
1445 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1446   SDLoc DL(Op);
1447   SDValue ChainIn = Op->getOperand(0);
1448   SDValue Address = Op->getOperand(2);
1449   SDValue Offset  = Op->getOperand(3);
1450   EVT ResTy = Op->getValueType(0);
1451   EVT PtrTy = Address->getValueType(0);
1452
1453   Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1454
1455   return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1456                      false, false, 16);
1457 }
1458
1459 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1460                                                      SelectionDAG &DAG) const {
1461   unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1462   switch (Intr) {
1463   default:
1464     return SDValue();
1465   case Intrinsic::mips_extp:
1466     return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1467   case Intrinsic::mips_extpdp:
1468     return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1469   case Intrinsic::mips_extr_w:
1470     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1471   case Intrinsic::mips_extr_r_w:
1472     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1473   case Intrinsic::mips_extr_rs_w:
1474     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1475   case Intrinsic::mips_extr_s_h:
1476     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1477   case Intrinsic::mips_mthlip:
1478     return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1479   case Intrinsic::mips_mulsaq_s_w_ph:
1480     return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1481   case Intrinsic::mips_maq_s_w_phl:
1482     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1483   case Intrinsic::mips_maq_s_w_phr:
1484     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1485   case Intrinsic::mips_maq_sa_w_phl:
1486     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1487   case Intrinsic::mips_maq_sa_w_phr:
1488     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1489   case Intrinsic::mips_dpaq_s_w_ph:
1490     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1491   case Intrinsic::mips_dpsq_s_w_ph:
1492     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1493   case Intrinsic::mips_dpaq_sa_l_w:
1494     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1495   case Intrinsic::mips_dpsq_sa_l_w:
1496     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1497   case Intrinsic::mips_dpaqx_s_w_ph:
1498     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1499   case Intrinsic::mips_dpaqx_sa_w_ph:
1500     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1501   case Intrinsic::mips_dpsqx_s_w_ph:
1502     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1503   case Intrinsic::mips_dpsqx_sa_w_ph:
1504     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
1505   case Intrinsic::mips_ld_b:
1506   case Intrinsic::mips_ld_h:
1507   case Intrinsic::mips_ld_w:
1508   case Intrinsic::mips_ld_d:
1509   case Intrinsic::mips_ldx_b:
1510   case Intrinsic::mips_ldx_h:
1511   case Intrinsic::mips_ldx_w:
1512   case Intrinsic::mips_ldx_d:
1513    return lowerMSALoadIntr(Op, DAG, Intr);
1514   }
1515 }
1516
1517 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1518   SDLoc DL(Op);
1519   SDValue ChainIn = Op->getOperand(0);
1520   SDValue Value   = Op->getOperand(2);
1521   SDValue Address = Op->getOperand(3);
1522   SDValue Offset  = Op->getOperand(4);
1523   EVT PtrTy = Address->getValueType(0);
1524
1525   Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1526
1527   return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1528                       false, 16);
1529 }
1530
1531 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1532                                                   SelectionDAG &DAG) const {
1533   unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1534   switch (Intr) {
1535   default:
1536     return SDValue();
1537   case Intrinsic::mips_st_b:
1538   case Intrinsic::mips_st_h:
1539   case Intrinsic::mips_st_w:
1540   case Intrinsic::mips_st_d:
1541   case Intrinsic::mips_stx_b:
1542   case Intrinsic::mips_stx_h:
1543   case Intrinsic::mips_stx_w:
1544   case Intrinsic::mips_stx_d:
1545     return lowerMSAStoreIntr(Op, DAG, Intr);
1546   }
1547 }
1548
1549 /// \brief Check if the given BuildVectorSDNode is a splat.
1550 /// This method currently relies on DAG nodes being reused when equivalent,
1551 /// so it's possible for this to return false even when isConstantSplat returns
1552 /// true.
1553 static bool isSplatVector(const BuildVectorSDNode *N) {
1554   unsigned int nOps = N->getNumOperands();
1555   assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1556
1557   SDValue Operand0 = N->getOperand(0);
1558
1559   for (unsigned int i = 1; i < nOps; ++i) {
1560     if (N->getOperand(i) != Operand0)
1561       return false;
1562   }
1563
1564   return true;
1565 }
1566
1567 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1568 //
1569 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1570 // choose to sign-extend but we could have equally chosen zero-extend. The
1571 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1572 // result into this node later (possibly changing it to a zero-extend in the
1573 // process).
1574 SDValue MipsSETargetLowering::
1575 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1576   SDLoc DL(Op);
1577   EVT ResTy = Op->getValueType(0);
1578   SDValue Op0 = Op->getOperand(0);
1579   SDValue Op1 = Op->getOperand(1);
1580   EVT EltTy = Op0->getValueType(0).getVectorElementType();
1581   return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1582                      DAG.getValueType(EltTy));
1583 }
1584
1585 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1586 // backend.
1587 //
1588 // Lowers according to the following rules:
1589 // - Vectors of 128-bits may be legal subject to the other rules. Other sizes
1590 //   are not legal.
1591 // - Non-constant splats are legal and are lowered to MipsISD::VSPLAT.
1592 // - Constant splats with an element size of 32-bits or less are legal and are
1593 //   lowered to MipsISD::VSPLAT.
1594 // - Constant splats with an element size of 64-bits but whose value would fit
1595 //   within a 10 bit immediate are legal and are lowered to MipsISD::VSPLATD.
1596 // - All other ISD::BUILD_VECTORS are not legal
1597 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1598                                                 SelectionDAG &DAG) const {
1599   BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1600   EVT ResTy = Op->getValueType(0);
1601   SDLoc DL(Op);
1602   APInt SplatValue, SplatUndef;
1603   unsigned SplatBitSize;
1604   bool HasAnyUndefs;
1605
1606   if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1607     return SDValue();
1608
1609   if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1610                             HasAnyUndefs, 8,
1611                             !Subtarget->isLittle())) {
1612     SDValue Result;
1613     EVT TmpVecTy;
1614     EVT ConstTy = MVT::i32;
1615     unsigned SplatOp = MipsISD::VSPLAT;
1616
1617     switch (SplatBitSize) {
1618     default:
1619       return SDValue();
1620     case 64:
1621       TmpVecTy = MVT::v2i64;
1622
1623       // i64 is an illegal type on Mips32, but if it the constant fits into a
1624       // signed 10-bit value then we can still handle it using VSPLATD and an
1625       // i32 constant
1626       if (HasMips64)
1627         ConstTy = MVT::i64;
1628       else if (isInt<10>(SplatValue.getSExtValue())) {
1629         SplatValue = SplatValue.trunc(32);
1630         SplatOp = MipsISD::VSPLATD;
1631       } else
1632         return SDValue();
1633       break;
1634     case 32:
1635       TmpVecTy = MVT::v4i32;
1636       break;
1637     case 16:
1638       TmpVecTy = MVT::v8i16;
1639       SplatValue = SplatValue.sext(32);
1640       break;
1641     case 8:
1642       TmpVecTy = MVT::v16i8;
1643       SplatValue = SplatValue.sext(32);
1644       break;
1645     }
1646
1647     Result = DAG.getNode(SplatOp, DL, TmpVecTy,
1648                          DAG.getConstant(SplatValue, ConstTy));
1649     if (ResTy != Result.getValueType())
1650       Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1651
1652     return Result;
1653   }
1654   else if (isSplatVector(Node))
1655     return DAG.getNode(MipsISD::VSPLAT, DL, ResTy, Op->getOperand(0));
1656
1657   return SDValue();
1658 }
1659
1660 MachineBasicBlock * MipsSETargetLowering::
1661 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
1662   // $bb:
1663   //  bposge32_pseudo $vr0
1664   //  =>
1665   // $bb:
1666   //  bposge32 $tbb
1667   // $fbb:
1668   //  li $vr2, 0
1669   //  b $sink
1670   // $tbb:
1671   //  li $vr1, 1
1672   // $sink:
1673   //  $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1674
1675   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1676   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1677   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1678   DebugLoc DL = MI->getDebugLoc();
1679   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1680   MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1681   MachineFunction *F = BB->getParent();
1682   MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1683   MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1684   MachineBasicBlock *Sink  = F->CreateMachineBasicBlock(LLVM_BB);
1685   F->insert(It, FBB);
1686   F->insert(It, TBB);
1687   F->insert(It, Sink);
1688
1689   // Transfer the remainder of BB and its successor edges to Sink.
1690   Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1691                BB->end());
1692   Sink->transferSuccessorsAndUpdatePHIs(BB);
1693
1694   // Add successors.
1695   BB->addSuccessor(FBB);
1696   BB->addSuccessor(TBB);
1697   FBB->addSuccessor(Sink);
1698   TBB->addSuccessor(Sink);
1699
1700   // Insert the real bposge32 instruction to $BB.
1701   BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1702
1703   // Fill $FBB.
1704   unsigned VR2 = RegInfo.createVirtualRegister(RC);
1705   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1706     .addReg(Mips::ZERO).addImm(0);
1707   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1708
1709   // Fill $TBB.
1710   unsigned VR1 = RegInfo.createVirtualRegister(RC);
1711   BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1712     .addReg(Mips::ZERO).addImm(1);
1713
1714   // Insert phi function to $Sink.
1715   BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1716           MI->getOperand(0).getReg())
1717     .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1718
1719   MI->eraseFromParent();   // The pseudo instruction is gone now.
1720   return Sink;
1721 }
1722
1723 MachineBasicBlock * MipsSETargetLowering::
1724 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1725                      unsigned BranchOp) const{
1726   // $bb:
1727   //  vany_nonzero $rd, $ws
1728   //  =>
1729   // $bb:
1730   //  bnz.b $ws, $tbb
1731   //  b $fbb
1732   // $fbb:
1733   //  li $rd1, 0
1734   //  b $sink
1735   // $tbb:
1736   //  li $rd2, 1
1737   // $sink:
1738   //  $rd = phi($rd1, $fbb, $rd2, $tbb)
1739
1740   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1741   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1742   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1743   DebugLoc DL = MI->getDebugLoc();
1744   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1745   MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1746   MachineFunction *F = BB->getParent();
1747   MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1748   MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1749   MachineBasicBlock *Sink  = F->CreateMachineBasicBlock(LLVM_BB);
1750   F->insert(It, FBB);
1751   F->insert(It, TBB);
1752   F->insert(It, Sink);
1753
1754   // Transfer the remainder of BB and its successor edges to Sink.
1755   Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1756                BB->end());
1757   Sink->transferSuccessorsAndUpdatePHIs(BB);
1758
1759   // Add successors.
1760   BB->addSuccessor(FBB);
1761   BB->addSuccessor(TBB);
1762   FBB->addSuccessor(Sink);
1763   TBB->addSuccessor(Sink);
1764
1765   // Insert the real bnz.b instruction to $BB.
1766   BuildMI(BB, DL, TII->get(BranchOp))
1767     .addReg(MI->getOperand(1).getReg())
1768     .addMBB(TBB);
1769
1770   // Fill $FBB.
1771   unsigned RD1 = RegInfo.createVirtualRegister(RC);
1772   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1773     .addReg(Mips::ZERO).addImm(0);
1774   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1775
1776   // Fill $TBB.
1777   unsigned RD2 = RegInfo.createVirtualRegister(RC);
1778   BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1779     .addReg(Mips::ZERO).addImm(1);
1780
1781   // Insert phi function to $Sink.
1782   BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1783           MI->getOperand(0).getReg())
1784     .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1785
1786   MI->eraseFromParent();   // The pseudo instruction is gone now.
1787   return Sink;
1788 }