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