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