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