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