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