1 //===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Subclass of MipsTargetLowering specialized for mips32/64.
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "mips-isel"
14 #include "MipsSEISelLowering.h"
15 #include "MipsRegisterInfo.h"
16 #include "MipsTargetMachine.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Target/TargetInstrInfo.h"
27 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
28 cl::desc("MIPS: Enable tail calls."), cl::init(false));
30 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
31 cl::desc("Expand double precision loads and "
32 "stores to their single precision "
35 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
36 : MipsTargetLowering(TM) {
37 // Set up the register classes
38 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
41 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
43 if (Subtarget->hasDSP() || Subtarget->hasMSA()) {
44 // Expand all truncating stores and extending loads.
45 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
46 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
48 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
49 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
50 setTruncStoreAction((MVT::SimpleValueType)VT0,
51 (MVT::SimpleValueType)VT1, Expand);
53 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
54 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
55 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
59 if (Subtarget->hasDSP()) {
60 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
62 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
63 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
65 // Expand all builtin opcodes.
66 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
67 setOperationAction(Opc, VecTys[i], Expand);
69 setOperationAction(ISD::ADD, VecTys[i], Legal);
70 setOperationAction(ISD::SUB, VecTys[i], Legal);
71 setOperationAction(ISD::LOAD, VecTys[i], Legal);
72 setOperationAction(ISD::STORE, VecTys[i], Legal);
73 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
76 setTargetDAGCombine(ISD::SHL);
77 setTargetDAGCombine(ISD::SRA);
78 setTargetDAGCombine(ISD::SRL);
79 setTargetDAGCombine(ISD::SETCC);
80 setTargetDAGCombine(ISD::VSELECT);
83 if (Subtarget->hasDSPR2())
84 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
86 if (Subtarget->hasMSA()) {
87 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
88 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
89 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
90 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
91 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
92 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
93 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
95 setTargetDAGCombine(ISD::AND);
96 setTargetDAGCombine(ISD::SRA);
97 setTargetDAGCombine(ISD::VSELECT);
98 setTargetDAGCombine(ISD::XOR);
101 if (!Subtarget->mipsSEUsesSoftFloat()) {
102 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
104 // When dealing with single precision only, use libcalls
105 if (!Subtarget->isSingleFloat()) {
106 if (Subtarget->isFP64bit())
107 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
109 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
113 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
114 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
115 setOperationAction(ISD::MULHS, MVT::i32, Custom);
116 setOperationAction(ISD::MULHU, MVT::i32, Custom);
119 setOperationAction(ISD::MULHS, MVT::i64, Custom);
120 setOperationAction(ISD::MULHU, MVT::i64, Custom);
121 setOperationAction(ISD::MUL, MVT::i64, Custom);
124 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
125 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
127 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
128 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
129 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
130 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
131 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
132 setOperationAction(ISD::LOAD, MVT::i32, Custom);
133 setOperationAction(ISD::STORE, MVT::i32, Custom);
135 setTargetDAGCombine(ISD::ADDE);
136 setTargetDAGCombine(ISD::SUBE);
137 setTargetDAGCombine(ISD::MUL);
139 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
140 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
141 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
144 setOperationAction(ISD::LOAD, MVT::f64, Custom);
145 setOperationAction(ISD::STORE, MVT::f64, Custom);
148 computeRegisterProperties();
151 const MipsTargetLowering *
152 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
153 return new MipsSETargetLowering(TM);
156 // Enable MSA support for the given integer type and Register class.
157 void MipsSETargetLowering::
158 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
159 addRegisterClass(Ty, RC);
161 // Expand all builtin opcodes.
162 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
163 setOperationAction(Opc, Ty, Expand);
165 setOperationAction(ISD::BITCAST, Ty, Legal);
166 setOperationAction(ISD::LOAD, Ty, Legal);
167 setOperationAction(ISD::STORE, Ty, Legal);
168 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
169 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
170 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
172 setOperationAction(ISD::ADD, Ty, Legal);
173 setOperationAction(ISD::AND, Ty, Legal);
174 setOperationAction(ISD::CTLZ, Ty, Legal);
175 setOperationAction(ISD::CTPOP, Ty, Legal);
176 setOperationAction(ISD::MUL, Ty, Legal);
177 setOperationAction(ISD::OR, Ty, Legal);
178 setOperationAction(ISD::SDIV, Ty, Legal);
179 setOperationAction(ISD::SREM, Ty, Legal);
180 setOperationAction(ISD::SHL, Ty, Legal);
181 setOperationAction(ISD::SRA, Ty, Legal);
182 setOperationAction(ISD::SRL, Ty, Legal);
183 setOperationAction(ISD::SUB, Ty, Legal);
184 setOperationAction(ISD::UDIV, Ty, Legal);
185 setOperationAction(ISD::UREM, Ty, Legal);
186 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
187 setOperationAction(ISD::VSELECT, Ty, Legal);
188 setOperationAction(ISD::XOR, Ty, Legal);
190 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
191 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
192 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
193 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
194 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
197 setOperationAction(ISD::SETCC, Ty, Legal);
198 setCondCodeAction(ISD::SETNE, Ty, Expand);
199 setCondCodeAction(ISD::SETGE, Ty, Expand);
200 setCondCodeAction(ISD::SETGT, Ty, Expand);
201 setCondCodeAction(ISD::SETUGE, Ty, Expand);
202 setCondCodeAction(ISD::SETUGT, Ty, Expand);
205 // Enable MSA support for the given floating-point type and Register class.
206 void MipsSETargetLowering::
207 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
208 addRegisterClass(Ty, RC);
210 // Expand all builtin opcodes.
211 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
212 setOperationAction(Opc, Ty, Expand);
214 setOperationAction(ISD::LOAD, Ty, Legal);
215 setOperationAction(ISD::STORE, Ty, Legal);
216 setOperationAction(ISD::BITCAST, Ty, Legal);
217 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
218 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
219 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
221 if (Ty != MVT::v8f16) {
222 setOperationAction(ISD::FABS, Ty, Legal);
223 setOperationAction(ISD::FADD, Ty, Legal);
224 setOperationAction(ISD::FDIV, Ty, Legal);
225 setOperationAction(ISD::FEXP2, Ty, Legal);
226 setOperationAction(ISD::FLOG2, Ty, Legal);
227 setOperationAction(ISD::FMA, Ty, Legal);
228 setOperationAction(ISD::FMUL, Ty, Legal);
229 setOperationAction(ISD::FRINT, Ty, Legal);
230 setOperationAction(ISD::FSQRT, Ty, Legal);
231 setOperationAction(ISD::FSUB, Ty, Legal);
232 setOperationAction(ISD::VSELECT, Ty, Legal);
234 setOperationAction(ISD::SETCC, Ty, Legal);
235 setCondCodeAction(ISD::SETOGE, Ty, Expand);
236 setCondCodeAction(ISD::SETOGT, Ty, Expand);
237 setCondCodeAction(ISD::SETUGE, Ty, Expand);
238 setCondCodeAction(ISD::SETUGT, Ty, Expand);
239 setCondCodeAction(ISD::SETGE, Ty, Expand);
240 setCondCodeAction(ISD::SETGT, Ty, Expand);
245 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
246 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
259 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
260 SelectionDAG &DAG) const {
261 switch(Op.getOpcode()) {
262 case ISD::LOAD: return lowerLOAD(Op, DAG);
263 case ISD::STORE: return lowerSTORE(Op, DAG);
264 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
265 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
266 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
267 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
268 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
269 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
270 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
272 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
273 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
274 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
275 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
276 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
277 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
280 return MipsTargetLowering::LowerOperation(Op, DAG);
284 // Transforms a subgraph in CurDAG if the following pattern is found:
285 // (addc multLo, Lo0), (adde multHi, Hi0),
287 // multHi/Lo: product of multiplication
288 // Lo0: initial value of Lo register
289 // Hi0: initial value of Hi register
290 // Return true if pattern matching was successful.
291 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
292 // ADDENode's second operand must be a flag output of an ADDC node in order
293 // for the matching to be successful.
294 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
296 if (ADDCNode->getOpcode() != ISD::ADDC)
299 SDValue MultHi = ADDENode->getOperand(0);
300 SDValue MultLo = ADDCNode->getOperand(0);
301 SDNode *MultNode = MultHi.getNode();
302 unsigned MultOpc = MultHi.getOpcode();
304 // MultHi and MultLo must be generated by the same node,
305 if (MultLo.getNode() != MultNode)
308 // and it must be a multiplication.
309 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
312 // MultLo amd MultHi must be the first and second output of MultNode
314 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
317 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
318 // of the values of MultNode, in which case MultNode will be removed in later
320 // If there exist users other than ADDENode or ADDCNode, this function returns
321 // here, which will result in MultNode being mapped to a single MULT
322 // instruction node rather than a pair of MULT and MADD instructions being
324 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
329 // Initialize accumulator.
330 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
331 ADDCNode->getOperand(1),
332 ADDENode->getOperand(1));
334 // create MipsMAdd(u) node
335 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
337 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
338 MultNode->getOperand(0),// Factor 0
339 MultNode->getOperand(1),// Factor 1
342 // replace uses of adde and addc here
343 if (!SDValue(ADDCNode, 0).use_empty()) {
344 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
345 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
347 if (!SDValue(ADDENode, 0).use_empty()) {
348 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
349 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
356 // Transforms a subgraph in CurDAG if the following pattern is found:
357 // (addc Lo0, multLo), (sube Hi0, multHi),
359 // multHi/Lo: product of multiplication
360 // Lo0: initial value of Lo register
361 // Hi0: initial value of Hi register
362 // Return true if pattern matching was successful.
363 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
364 // SUBENode's second operand must be a flag output of an SUBC node in order
365 // for the matching to be successful.
366 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
368 if (SUBCNode->getOpcode() != ISD::SUBC)
371 SDValue MultHi = SUBENode->getOperand(1);
372 SDValue MultLo = SUBCNode->getOperand(1);
373 SDNode *MultNode = MultHi.getNode();
374 unsigned MultOpc = MultHi.getOpcode();
376 // MultHi and MultLo must be generated by the same node,
377 if (MultLo.getNode() != MultNode)
380 // and it must be a multiplication.
381 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
384 // MultLo amd MultHi must be the first and second output of MultNode
386 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
389 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
390 // of the values of MultNode, in which case MultNode will be removed in later
392 // If there exist users other than SUBENode or SUBCNode, this function returns
393 // here, which will result in MultNode being mapped to a single MULT
394 // instruction node rather than a pair of MULT and MSUB instructions being
396 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
401 // Initialize accumulator.
402 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
403 SUBCNode->getOperand(0),
404 SUBENode->getOperand(0));
406 // create MipsSub(u) node
407 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
409 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
410 MultNode->getOperand(0),// Factor 0
411 MultNode->getOperand(1),// Factor 1
414 // replace uses of sube and subc here
415 if (!SDValue(SUBCNode, 0).use_empty()) {
416 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
417 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
419 if (!SDValue(SUBENode, 0).use_empty()) {
420 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
421 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
427 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
428 TargetLowering::DAGCombinerInfo &DCI,
429 const MipsSubtarget *Subtarget) {
430 if (DCI.isBeforeLegalize())
433 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
435 return SDValue(N, 0);
440 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
442 // Performs the following transformations:
443 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
444 // sign/zero-extension is completely overwritten by the new one performed by
446 // - Removes redundant zero extensions performed by an ISD::AND.
447 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
448 TargetLowering::DAGCombinerInfo &DCI,
449 const MipsSubtarget *Subtarget) {
450 if (!Subtarget->hasMSA())
453 SDValue Op0 = N->getOperand(0);
454 SDValue Op1 = N->getOperand(1);
455 unsigned Op0Opcode = Op0->getOpcode();
457 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
458 // where $d + 1 == 2^n and n == 32
459 // or $d + 1 == 2^n and n <= 32 and ZExt
460 // -> (MipsVExtractZExt $a, $b, $c)
461 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
462 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
463 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
468 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
470 if (Log2IfPositive <= 0)
471 return SDValue(); // Mask+1 is not a power of 2
473 SDValue Op0Op2 = Op0->getOperand(2);
474 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
475 unsigned ExtendTySize = ExtendTy.getSizeInBits();
476 unsigned Log2 = Log2IfPositive;
478 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
479 Log2 == ExtendTySize) {
480 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
481 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
482 Op0->getVTList(), Ops, Op0->getNumOperands());
490 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
491 TargetLowering::DAGCombinerInfo &DCI,
492 const MipsSubtarget *Subtarget) {
493 if (DCI.isBeforeLegalize())
496 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
498 return SDValue(N, 0);
503 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
504 EVT ShiftTy, SelectionDAG &DAG) {
505 // Clear the upper (64 - VT.sizeInBits) bits.
506 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
510 return DAG.getConstant(0, VT);
516 // If c is power of 2, return (shl x, log2(c)).
517 if (isPowerOf2_64(C))
518 return DAG.getNode(ISD::SHL, DL, VT, X,
519 DAG.getConstant(Log2_64(C), ShiftTy));
521 unsigned Log2Ceil = Log2_64_Ceil(C);
522 uint64_t Floor = 1LL << Log2_64(C);
523 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
525 // If |c - floor_c| <= |c - ceil_c|,
526 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
527 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
528 if (C - Floor <= Ceil - C) {
529 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
530 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
531 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
534 // If |c - floor_c| > |c - ceil_c|,
535 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
536 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
537 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
538 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
541 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
542 const TargetLowering::DAGCombinerInfo &DCI,
543 const MipsSETargetLowering *TL) {
544 EVT VT = N->getValueType(0);
546 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
548 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
549 VT, TL->getScalarShiftAmountTy(VT), DAG);
551 return SDValue(N, 0);
554 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
556 const MipsSubtarget *Subtarget) {
557 // See if this is a vector splat immediate node.
558 APInt SplatValue, SplatUndef;
559 unsigned SplatBitSize;
561 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
562 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
565 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
566 EltSize, !Subtarget->isLittle()) ||
567 (SplatBitSize != EltSize) ||
568 (SplatValue.getZExtValue() >= EltSize))
571 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
572 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
575 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
576 TargetLowering::DAGCombinerInfo &DCI,
577 const MipsSubtarget *Subtarget) {
578 EVT Ty = N->getValueType(0);
580 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
583 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
586 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
587 // constant splats into MipsISD::SHRA_DSP for DSPr2.
589 // Performs the following transformations:
590 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
591 // sign/zero-extension is completely overwritten by the new one performed by
592 // the ISD::SRA and ISD::SHL nodes.
593 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
596 // See performDSPShiftCombine for more information about the transformation
598 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
599 TargetLowering::DAGCombinerInfo &DCI,
600 const MipsSubtarget *Subtarget) {
601 EVT Ty = N->getValueType(0);
603 if (Subtarget->hasMSA()) {
604 SDValue Op0 = N->getOperand(0);
605 SDValue Op1 = N->getOperand(1);
607 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
608 // where $d + sizeof($c) == 32
609 // or $d + sizeof($c) <= 32 and SExt
610 // -> (MipsVExtractSExt $a, $b, $c)
611 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
612 SDValue Op0Op0 = Op0->getOperand(0);
613 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
618 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
619 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
622 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
623 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
625 if (TotalBits == 32 ||
626 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
628 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
629 Op0Op0->getOperand(2) };
630 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
631 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
637 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
640 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
644 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
645 TargetLowering::DAGCombinerInfo &DCI,
646 const MipsSubtarget *Subtarget) {
647 EVT Ty = N->getValueType(0);
649 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
652 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
655 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
656 bool IsV216 = (Ty == MVT::v2i16);
660 case ISD::SETNE: return true;
664 case ISD::SETGE: return IsV216;
668 case ISD::SETUGE: return !IsV216;
669 default: return false;
673 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
674 EVT Ty = N->getValueType(0);
676 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
679 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
682 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
683 N->getOperand(1), N->getOperand(2));
686 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
687 EVT Ty = N->getValueType(0);
689 if (Ty.is128BitVector() && Ty.isInteger()) {
690 // Try the following combines:
691 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
692 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
693 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
694 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
695 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
696 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
697 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
698 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
699 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
700 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
702 SDValue Op0 = N->getOperand(0);
704 if (Op0->getOpcode() != ISD::SETCC)
707 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
710 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
712 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
717 SDValue Op1 = N->getOperand(1);
718 SDValue Op2 = N->getOperand(2);
719 SDValue Op0Op0 = Op0->getOperand(0);
720 SDValue Op0Op1 = Op0->getOperand(1);
722 if (Op1 == Op0Op0 && Op2 == Op0Op1)
723 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
725 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
726 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
728 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
729 SDValue SetCC = N->getOperand(0);
731 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
734 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
735 SetCC.getOperand(0), SetCC.getOperand(1),
736 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
742 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
743 const MipsSubtarget *Subtarget) {
744 EVT Ty = N->getValueType(0);
746 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
747 // Try the following combines:
748 // (xor (or $a, $b), (build_vector allones))
749 // (xor (or $a, $b), (bitcast (build_vector allones)))
750 SDValue Op0 = N->getOperand(0);
751 SDValue Op1 = N->getOperand(1);
754 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
756 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
761 if (NotOp->getOpcode() == ISD::OR)
762 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
763 NotOp->getOperand(1));
770 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
771 SelectionDAG &DAG = DCI.DAG;
774 switch (N->getOpcode()) {
776 return performADDECombine(N, DAG, DCI, Subtarget);
778 Val = performANDCombine(N, DAG, DCI, Subtarget);
781 return performSUBECombine(N, DAG, DCI, Subtarget);
783 return performMULCombine(N, DAG, DCI, this);
785 return performSHLCombine(N, DAG, DCI, Subtarget);
787 return performSRACombine(N, DAG, DCI, Subtarget);
789 return performSRLCombine(N, DAG, DCI, Subtarget);
791 return performVSELECTCombine(N, DAG);
793 Val = performXORCombine(N, DAG, Subtarget);
796 Val = performSETCCCombine(N, DAG);
801 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
802 N->printrWithDepth(dbgs(), &DAG);
804 Val.getNode()->printrWithDepth(dbgs(), &DAG);
809 return MipsTargetLowering::PerformDAGCombine(N, DCI);
813 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
814 MachineBasicBlock *BB) const {
815 switch (MI->getOpcode()) {
817 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
818 case Mips::BPOSGE32_PSEUDO:
819 return emitBPOSGE32(MI, BB);
820 case Mips::SNZ_B_PSEUDO:
821 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
822 case Mips::SNZ_H_PSEUDO:
823 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
824 case Mips::SNZ_W_PSEUDO:
825 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
826 case Mips::SNZ_D_PSEUDO:
827 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
828 case Mips::SNZ_V_PSEUDO:
829 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
830 case Mips::SZ_B_PSEUDO:
831 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
832 case Mips::SZ_H_PSEUDO:
833 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
834 case Mips::SZ_W_PSEUDO:
835 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
836 case Mips::SZ_D_PSEUDO:
837 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
838 case Mips::SZ_V_PSEUDO:
839 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
840 case Mips::COPY_FW_PSEUDO:
841 return emitCOPY_FW(MI, BB);
842 case Mips::COPY_FD_PSEUDO:
843 return emitCOPY_FD(MI, BB);
844 case Mips::INSERT_FW_PSEUDO:
845 return emitINSERT_FW(MI, BB);
846 case Mips::INSERT_FD_PSEUDO:
847 return emitINSERT_FD(MI, BB);
848 case Mips::FILL_FW_PSEUDO:
849 return emitFILL_FW(MI, BB);
850 case Mips::FILL_FD_PSEUDO:
851 return emitFILL_FD(MI, BB);
852 case Mips::FEXP2_W_1_PSEUDO:
853 return emitFEXP2_W_1(MI, BB);
854 case Mips::FEXP2_D_1_PSEUDO:
855 return emitFEXP2_D_1(MI, BB);
859 bool MipsSETargetLowering::
860 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
861 unsigned NextStackOffset,
862 const MipsFunctionInfo& FI) const {
863 if (!EnableMipsTailCalls)
866 // Return false if either the callee or caller has a byval argument.
867 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
870 // Return true if the callee's argument area is no larger than the
872 return NextStackOffset <= FI.getIncomingArgSize();
875 void MipsSETargetLowering::
876 getOpndList(SmallVectorImpl<SDValue> &Ops,
877 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
878 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
879 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
880 // T9 should contain the address of the callee function if
881 // -reloction-model=pic or it is an indirect call.
882 if (IsPICCall || !GlobalOrExternal) {
883 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
884 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
886 Ops.push_back(Callee);
888 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
889 InternalLinkage, CLI, Callee, Chain);
892 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
893 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
895 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
896 return MipsTargetLowering::lowerLOAD(Op, DAG);
898 // Replace a double precision load with two i32 loads and a buildpair64.
900 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
901 EVT PtrVT = Ptr.getValueType();
903 // i32 load from lower address.
904 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
905 MachinePointerInfo(), Nd.isVolatile(),
906 Nd.isNonTemporal(), Nd.isInvariant(),
909 // i32 load from higher address.
910 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
911 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
912 MachinePointerInfo(), Nd.isVolatile(),
913 Nd.isNonTemporal(), Nd.isInvariant(),
914 std::min(Nd.getAlignment(), 4U));
916 if (!Subtarget->isLittle())
919 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
920 SDValue Ops[2] = {BP, Hi.getValue(1)};
921 return DAG.getMergeValues(Ops, 2, DL);
924 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
925 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
927 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
928 return MipsTargetLowering::lowerSTORE(Op, DAG);
930 // Replace a double precision store with two extractelement64s and i32 stores.
932 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
933 EVT PtrVT = Ptr.getValueType();
934 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
935 Val, DAG.getConstant(0, MVT::i32));
936 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
937 Val, DAG.getConstant(1, MVT::i32));
939 if (!Subtarget->isLittle())
942 // i32 store to lower address.
943 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
944 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
947 // i32 store to higher address.
948 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
949 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
950 Nd.isVolatile(), Nd.isNonTemporal(),
951 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
954 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
955 bool HasLo, bool HasHi,
956 SelectionDAG &DAG) const {
957 EVT Ty = Op.getOperand(0).getValueType();
959 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
960 Op.getOperand(0), Op.getOperand(1));
964 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
966 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
968 if (!HasLo || !HasHi)
969 return HasLo ? Lo : Hi;
971 SDValue Vals[] = { Lo, Hi };
972 return DAG.getMergeValues(Vals, 2, DL);
976 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
977 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
978 DAG.getConstant(0, MVT::i32));
979 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
980 DAG.getConstant(1, MVT::i32));
981 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
984 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
985 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
986 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
987 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
990 // This function expands mips intrinsic nodes which have 64-bit input operands
993 // out64 = intrinsic-node in64
995 // lo = copy (extract-element (in64, 0))
996 // hi = copy (extract-element (in64, 1))
997 // mips-specific-node
1000 // out64 = merge-values (v0, v1)
1002 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1004 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1005 SmallVector<SDValue, 3> Ops;
1008 // See if Op has a chain input.
1010 Ops.push_back(Op->getOperand(OpNo++));
1012 // The next operand is the intrinsic opcode.
1013 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1015 // See if the next operand has type i64.
1016 SDValue Opnd = Op->getOperand(++OpNo), In64;
1018 if (Opnd.getValueType() == MVT::i64)
1019 In64 = initAccumulator(Opnd, DL, DAG);
1021 Ops.push_back(Opnd);
1023 // Push the remaining operands.
1024 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1025 Ops.push_back(Op->getOperand(OpNo));
1027 // Add In64 to the end of the list.
1029 Ops.push_back(In64);
1032 SmallVector<EVT, 2> ResTys;
1034 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1036 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1039 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1040 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1045 assert(Val->getValueType(1) == MVT::Other);
1046 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1047 return DAG.getMergeValues(Vals, 2, DL);
1050 // Lower an MSA copy intrinsic into the specified SelectionDAG node
1051 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1053 SDValue Vec = Op->getOperand(1);
1054 SDValue Idx = Op->getOperand(2);
1055 EVT ResTy = Op->getValueType(0);
1056 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1058 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1059 DAG.getValueType(EltTy));
1065 lowerMSASplatImm(SDLoc DL, EVT ResTy, SDValue ImmOp, SelectionDAG &DAG) {
1066 EVT ViaVecTy = ResTy;
1067 SmallVector<SDValue, 16> Ops;
1070 if (ViaVecTy == MVT::v2i64) {
1071 ImmHiOp = DAG.getNode(ISD::SRA, DL, MVT::i32, ImmOp,
1072 DAG.getConstant(31, MVT::i32));
1073 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i) {
1074 Ops.push_back(ImmHiOp);
1075 Ops.push_back(ImmOp);
1077 ViaVecTy = MVT::v4i32;
1079 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1080 Ops.push_back(ImmOp);
1083 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, &Ops[0],
1086 if (ResTy != ViaVecTy)
1087 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1093 lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1094 return lowerMSASplatImm(SDLoc(Op), Op->getValueType(0),
1095 Op->getOperand(ImmOp), DAG);
1098 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1099 SelectionDAG &DAG) const {
1102 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1105 case Intrinsic::mips_shilo:
1106 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1107 case Intrinsic::mips_dpau_h_qbl:
1108 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1109 case Intrinsic::mips_dpau_h_qbr:
1110 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1111 case Intrinsic::mips_dpsu_h_qbl:
1112 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1113 case Intrinsic::mips_dpsu_h_qbr:
1114 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1115 case Intrinsic::mips_dpa_w_ph:
1116 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1117 case Intrinsic::mips_dps_w_ph:
1118 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1119 case Intrinsic::mips_dpax_w_ph:
1120 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1121 case Intrinsic::mips_dpsx_w_ph:
1122 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1123 case Intrinsic::mips_mulsa_w_ph:
1124 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1125 case Intrinsic::mips_mult:
1126 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1127 case Intrinsic::mips_multu:
1128 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1129 case Intrinsic::mips_madd:
1130 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1131 case Intrinsic::mips_maddu:
1132 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1133 case Intrinsic::mips_msub:
1134 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1135 case Intrinsic::mips_msubu:
1136 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
1137 case Intrinsic::mips_addv_b:
1138 case Intrinsic::mips_addv_h:
1139 case Intrinsic::mips_addv_w:
1140 case Intrinsic::mips_addv_d:
1141 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1143 case Intrinsic::mips_addvi_b:
1144 case Intrinsic::mips_addvi_h:
1145 case Intrinsic::mips_addvi_w:
1146 case Intrinsic::mips_addvi_d:
1147 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1148 lowerMSASplatImm(Op, 2, DAG));
1149 case Intrinsic::mips_and_v:
1150 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1152 case Intrinsic::mips_andi_b:
1153 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1154 lowerMSASplatImm(Op, 2, DAG));
1155 case Intrinsic::mips_bnz_b:
1156 case Intrinsic::mips_bnz_h:
1157 case Intrinsic::mips_bnz_w:
1158 case Intrinsic::mips_bnz_d:
1159 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1161 case Intrinsic::mips_bnz_v:
1162 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1164 case Intrinsic::mips_bsel_v:
1165 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1166 Op->getOperand(1), Op->getOperand(2),
1168 case Intrinsic::mips_bseli_b:
1169 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1170 Op->getOperand(1), Op->getOperand(2),
1171 lowerMSASplatImm(Op, 3, DAG));
1172 case Intrinsic::mips_bz_b:
1173 case Intrinsic::mips_bz_h:
1174 case Intrinsic::mips_bz_w:
1175 case Intrinsic::mips_bz_d:
1176 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1178 case Intrinsic::mips_bz_v:
1179 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1181 case Intrinsic::mips_ceq_b:
1182 case Intrinsic::mips_ceq_h:
1183 case Intrinsic::mips_ceq_w:
1184 case Intrinsic::mips_ceq_d:
1185 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1186 Op->getOperand(2), ISD::SETEQ);
1187 case Intrinsic::mips_ceqi_b:
1188 case Intrinsic::mips_ceqi_h:
1189 case Intrinsic::mips_ceqi_w:
1190 case Intrinsic::mips_ceqi_d:
1191 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1192 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1193 case Intrinsic::mips_cle_s_b:
1194 case Intrinsic::mips_cle_s_h:
1195 case Intrinsic::mips_cle_s_w:
1196 case Intrinsic::mips_cle_s_d:
1197 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1198 Op->getOperand(2), ISD::SETLE);
1199 case Intrinsic::mips_clei_s_b:
1200 case Intrinsic::mips_clei_s_h:
1201 case Intrinsic::mips_clei_s_w:
1202 case Intrinsic::mips_clei_s_d:
1203 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1204 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1205 case Intrinsic::mips_cle_u_b:
1206 case Intrinsic::mips_cle_u_h:
1207 case Intrinsic::mips_cle_u_w:
1208 case Intrinsic::mips_cle_u_d:
1209 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1210 Op->getOperand(2), ISD::SETULE);
1211 case Intrinsic::mips_clei_u_b:
1212 case Intrinsic::mips_clei_u_h:
1213 case Intrinsic::mips_clei_u_w:
1214 case Intrinsic::mips_clei_u_d:
1215 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1216 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1217 case Intrinsic::mips_clt_s_b:
1218 case Intrinsic::mips_clt_s_h:
1219 case Intrinsic::mips_clt_s_w:
1220 case Intrinsic::mips_clt_s_d:
1221 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1222 Op->getOperand(2), ISD::SETLT);
1223 case Intrinsic::mips_clti_s_b:
1224 case Intrinsic::mips_clti_s_h:
1225 case Intrinsic::mips_clti_s_w:
1226 case Intrinsic::mips_clti_s_d:
1227 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1228 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1229 case Intrinsic::mips_clt_u_b:
1230 case Intrinsic::mips_clt_u_h:
1231 case Intrinsic::mips_clt_u_w:
1232 case Intrinsic::mips_clt_u_d:
1233 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1234 Op->getOperand(2), ISD::SETULT);
1235 case Intrinsic::mips_clti_u_b:
1236 case Intrinsic::mips_clti_u_h:
1237 case Intrinsic::mips_clti_u_w:
1238 case Intrinsic::mips_clti_u_d:
1239 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1240 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
1241 case Intrinsic::mips_copy_s_b:
1242 case Intrinsic::mips_copy_s_h:
1243 case Intrinsic::mips_copy_s_w:
1244 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1245 case Intrinsic::mips_copy_s_d:
1246 // Don't lower directly into VEXTRACT_SEXT_ELT since i64 might be illegal.
1247 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1248 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1249 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1250 Op->getOperand(1), Op->getOperand(2));
1251 case Intrinsic::mips_copy_u_b:
1252 case Intrinsic::mips_copy_u_h:
1253 case Intrinsic::mips_copy_u_w:
1254 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1255 case Intrinsic::mips_copy_u_d:
1256 // Don't lower directly into VEXTRACT_ZEXT_ELT since i64 might be illegal.
1257 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1258 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1260 // Note: When i64 is illegal, this results in copy_s.w instructions instead
1261 // of copy_u.w instructions. This makes no difference to the behaviour
1262 // since i64 is only illegal when the register file is 32-bit.
1263 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1264 Op->getOperand(1), Op->getOperand(2));
1265 case Intrinsic::mips_div_s_b:
1266 case Intrinsic::mips_div_s_h:
1267 case Intrinsic::mips_div_s_w:
1268 case Intrinsic::mips_div_s_d:
1269 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1271 case Intrinsic::mips_div_u_b:
1272 case Intrinsic::mips_div_u_h:
1273 case Intrinsic::mips_div_u_w:
1274 case Intrinsic::mips_div_u_d:
1275 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1277 case Intrinsic::mips_fadd_w:
1278 case Intrinsic::mips_fadd_d:
1279 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1281 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1282 case Intrinsic::mips_fceq_w:
1283 case Intrinsic::mips_fceq_d:
1284 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1285 Op->getOperand(2), ISD::SETOEQ);
1286 case Intrinsic::mips_fcle_w:
1287 case Intrinsic::mips_fcle_d:
1288 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1289 Op->getOperand(2), ISD::SETOLE);
1290 case Intrinsic::mips_fclt_w:
1291 case Intrinsic::mips_fclt_d:
1292 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1293 Op->getOperand(2), ISD::SETOLT);
1294 case Intrinsic::mips_fcne_w:
1295 case Intrinsic::mips_fcne_d:
1296 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1297 Op->getOperand(2), ISD::SETONE);
1298 case Intrinsic::mips_fcor_w:
1299 case Intrinsic::mips_fcor_d:
1300 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1301 Op->getOperand(2), ISD::SETO);
1302 case Intrinsic::mips_fcueq_w:
1303 case Intrinsic::mips_fcueq_d:
1304 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1305 Op->getOperand(2), ISD::SETUEQ);
1306 case Intrinsic::mips_fcule_w:
1307 case Intrinsic::mips_fcule_d:
1308 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1309 Op->getOperand(2), ISD::SETULE);
1310 case Intrinsic::mips_fcult_w:
1311 case Intrinsic::mips_fcult_d:
1312 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1313 Op->getOperand(2), ISD::SETULT);
1314 case Intrinsic::mips_fcun_w:
1315 case Intrinsic::mips_fcun_d:
1316 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1317 Op->getOperand(2), ISD::SETUO);
1318 case Intrinsic::mips_fcune_w:
1319 case Intrinsic::mips_fcune_d:
1320 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1321 Op->getOperand(2), ISD::SETUNE);
1322 case Intrinsic::mips_fdiv_w:
1323 case Intrinsic::mips_fdiv_d:
1324 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1326 case Intrinsic::mips_ffint_u_w:
1327 case Intrinsic::mips_ffint_u_d:
1328 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1330 case Intrinsic::mips_ffint_s_w:
1331 case Intrinsic::mips_ffint_s_d:
1332 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1334 case Intrinsic::mips_fill_b:
1335 case Intrinsic::mips_fill_h:
1336 case Intrinsic::mips_fill_w:
1337 case Intrinsic::mips_fill_d: {
1338 SmallVector<SDValue, 16> Ops;
1339 EVT ResTy = Op->getValueType(0);
1341 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1342 Ops.push_back(Op->getOperand(1));
1344 // If ResTy is v2i64 then the type legalizer will break this node down into
1345 // an equivalent v4i32.
1346 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
1348 case Intrinsic::mips_fexp2_w:
1349 case Intrinsic::mips_fexp2_d: {
1350 EVT ResTy = Op->getValueType(0);
1352 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1353 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1355 case Intrinsic::mips_flog2_w:
1356 case Intrinsic::mips_flog2_d:
1357 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
1358 case Intrinsic::mips_fmadd_w:
1359 case Intrinsic::mips_fmadd_d:
1360 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1361 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
1362 case Intrinsic::mips_fmul_w:
1363 case Intrinsic::mips_fmul_d:
1364 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1366 case Intrinsic::mips_fmsub_w:
1367 case Intrinsic::mips_fmsub_d: {
1368 EVT ResTy = Op->getValueType(0);
1369 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1370 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1371 Op->getOperand(2), Op->getOperand(3)));
1373 case Intrinsic::mips_frint_w:
1374 case Intrinsic::mips_frint_d:
1375 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
1376 case Intrinsic::mips_fsqrt_w:
1377 case Intrinsic::mips_fsqrt_d:
1378 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
1379 case Intrinsic::mips_fsub_w:
1380 case Intrinsic::mips_fsub_d:
1381 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1383 case Intrinsic::mips_ftrunc_u_w:
1384 case Intrinsic::mips_ftrunc_u_d:
1385 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1387 case Intrinsic::mips_ftrunc_s_w:
1388 case Intrinsic::mips_ftrunc_s_d:
1389 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1391 case Intrinsic::mips_ilvev_b:
1392 case Intrinsic::mips_ilvev_h:
1393 case Intrinsic::mips_ilvev_w:
1394 case Intrinsic::mips_ilvev_d:
1395 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
1396 Op->getOperand(1), Op->getOperand(2));
1397 case Intrinsic::mips_ilvl_b:
1398 case Intrinsic::mips_ilvl_h:
1399 case Intrinsic::mips_ilvl_w:
1400 case Intrinsic::mips_ilvl_d:
1401 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
1402 Op->getOperand(1), Op->getOperand(2));
1403 case Intrinsic::mips_ilvod_b:
1404 case Intrinsic::mips_ilvod_h:
1405 case Intrinsic::mips_ilvod_w:
1406 case Intrinsic::mips_ilvod_d:
1407 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
1408 Op->getOperand(1), Op->getOperand(2));
1409 case Intrinsic::mips_ilvr_b:
1410 case Intrinsic::mips_ilvr_h:
1411 case Intrinsic::mips_ilvr_w:
1412 case Intrinsic::mips_ilvr_d:
1413 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
1414 Op->getOperand(1), Op->getOperand(2));
1415 case Intrinsic::mips_insert_b:
1416 case Intrinsic::mips_insert_h:
1417 case Intrinsic::mips_insert_w:
1418 case Intrinsic::mips_insert_d:
1419 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1420 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
1421 case Intrinsic::mips_ldi_b:
1422 case Intrinsic::mips_ldi_h:
1423 case Intrinsic::mips_ldi_w:
1424 case Intrinsic::mips_ldi_d:
1425 return lowerMSASplatImm(Op, 1, DAG);
1426 case Intrinsic::mips_lsa: {
1427 EVT ResTy = Op->getValueType(0);
1428 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1429 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1430 Op->getOperand(2), Op->getOperand(3)));
1432 case Intrinsic::mips_maddv_b:
1433 case Intrinsic::mips_maddv_h:
1434 case Intrinsic::mips_maddv_w:
1435 case Intrinsic::mips_maddv_d: {
1436 EVT ResTy = Op->getValueType(0);
1437 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1438 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1439 Op->getOperand(2), Op->getOperand(3)));
1441 case Intrinsic::mips_max_s_b:
1442 case Intrinsic::mips_max_s_h:
1443 case Intrinsic::mips_max_s_w:
1444 case Intrinsic::mips_max_s_d:
1445 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1446 Op->getOperand(1), Op->getOperand(2));
1447 case Intrinsic::mips_max_u_b:
1448 case Intrinsic::mips_max_u_h:
1449 case Intrinsic::mips_max_u_w:
1450 case Intrinsic::mips_max_u_d:
1451 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1452 Op->getOperand(1), Op->getOperand(2));
1453 case Intrinsic::mips_maxi_s_b:
1454 case Intrinsic::mips_maxi_s_h:
1455 case Intrinsic::mips_maxi_s_w:
1456 case Intrinsic::mips_maxi_s_d:
1457 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1458 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1459 case Intrinsic::mips_maxi_u_b:
1460 case Intrinsic::mips_maxi_u_h:
1461 case Intrinsic::mips_maxi_u_w:
1462 case Intrinsic::mips_maxi_u_d:
1463 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1464 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1465 case Intrinsic::mips_min_s_b:
1466 case Intrinsic::mips_min_s_h:
1467 case Intrinsic::mips_min_s_w:
1468 case Intrinsic::mips_min_s_d:
1469 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1470 Op->getOperand(1), Op->getOperand(2));
1471 case Intrinsic::mips_min_u_b:
1472 case Intrinsic::mips_min_u_h:
1473 case Intrinsic::mips_min_u_w:
1474 case Intrinsic::mips_min_u_d:
1475 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1476 Op->getOperand(1), Op->getOperand(2));
1477 case Intrinsic::mips_mini_s_b:
1478 case Intrinsic::mips_mini_s_h:
1479 case Intrinsic::mips_mini_s_w:
1480 case Intrinsic::mips_mini_s_d:
1481 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1482 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1483 case Intrinsic::mips_mini_u_b:
1484 case Intrinsic::mips_mini_u_h:
1485 case Intrinsic::mips_mini_u_w:
1486 case Intrinsic::mips_mini_u_d:
1487 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1488 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1489 case Intrinsic::mips_mod_s_b:
1490 case Intrinsic::mips_mod_s_h:
1491 case Intrinsic::mips_mod_s_w:
1492 case Intrinsic::mips_mod_s_d:
1493 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1495 case Intrinsic::mips_mod_u_b:
1496 case Intrinsic::mips_mod_u_h:
1497 case Intrinsic::mips_mod_u_w:
1498 case Intrinsic::mips_mod_u_d:
1499 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1501 case Intrinsic::mips_mulv_b:
1502 case Intrinsic::mips_mulv_h:
1503 case Intrinsic::mips_mulv_w:
1504 case Intrinsic::mips_mulv_d:
1505 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1507 case Intrinsic::mips_msubv_b:
1508 case Intrinsic::mips_msubv_h:
1509 case Intrinsic::mips_msubv_w:
1510 case Intrinsic::mips_msubv_d: {
1511 EVT ResTy = Op->getValueType(0);
1512 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1513 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1514 Op->getOperand(2), Op->getOperand(3)));
1516 case Intrinsic::mips_nlzc_b:
1517 case Intrinsic::mips_nlzc_h:
1518 case Intrinsic::mips_nlzc_w:
1519 case Intrinsic::mips_nlzc_d:
1520 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
1521 case Intrinsic::mips_nor_v: {
1522 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1523 Op->getOperand(1), Op->getOperand(2));
1524 return DAG.getNOT(DL, Res, Res->getValueType(0));
1526 case Intrinsic::mips_nori_b: {
1527 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1529 lowerMSASplatImm(Op, 2, DAG));
1530 return DAG.getNOT(DL, Res, Res->getValueType(0));
1532 case Intrinsic::mips_or_v:
1533 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1535 case Intrinsic::mips_ori_b:
1536 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1537 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1538 case Intrinsic::mips_pckev_b:
1539 case Intrinsic::mips_pckev_h:
1540 case Intrinsic::mips_pckev_w:
1541 case Intrinsic::mips_pckev_d:
1542 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
1543 Op->getOperand(1), Op->getOperand(2));
1544 case Intrinsic::mips_pckod_b:
1545 case Intrinsic::mips_pckod_h:
1546 case Intrinsic::mips_pckod_w:
1547 case Intrinsic::mips_pckod_d:
1548 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
1549 Op->getOperand(1), Op->getOperand(2));
1550 case Intrinsic::mips_pcnt_b:
1551 case Intrinsic::mips_pcnt_h:
1552 case Intrinsic::mips_pcnt_w:
1553 case Intrinsic::mips_pcnt_d:
1554 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
1555 case Intrinsic::mips_shf_b:
1556 case Intrinsic::mips_shf_h:
1557 case Intrinsic::mips_shf_w:
1558 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
1559 Op->getOperand(2), Op->getOperand(1));
1560 case Intrinsic::mips_sll_b:
1561 case Intrinsic::mips_sll_h:
1562 case Intrinsic::mips_sll_w:
1563 case Intrinsic::mips_sll_d:
1564 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1566 case Intrinsic::mips_slli_b:
1567 case Intrinsic::mips_slli_h:
1568 case Intrinsic::mips_slli_w:
1569 case Intrinsic::mips_slli_d:
1570 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1571 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1572 case Intrinsic::mips_splat_b:
1573 case Intrinsic::mips_splat_h:
1574 case Intrinsic::mips_splat_w:
1575 case Intrinsic::mips_splat_d:
1576 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
1577 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
1578 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
1579 // Instead we lower to MipsISD::VSHF and match from there.
1580 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1581 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1583 case Intrinsic::mips_splati_b:
1584 case Intrinsic::mips_splati_h:
1585 case Intrinsic::mips_splati_w:
1586 case Intrinsic::mips_splati_d:
1587 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1588 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1590 case Intrinsic::mips_sra_b:
1591 case Intrinsic::mips_sra_h:
1592 case Intrinsic::mips_sra_w:
1593 case Intrinsic::mips_sra_d:
1594 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1596 case Intrinsic::mips_srai_b:
1597 case Intrinsic::mips_srai_h:
1598 case Intrinsic::mips_srai_w:
1599 case Intrinsic::mips_srai_d:
1600 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1601 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1602 case Intrinsic::mips_srl_b:
1603 case Intrinsic::mips_srl_h:
1604 case Intrinsic::mips_srl_w:
1605 case Intrinsic::mips_srl_d:
1606 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1608 case Intrinsic::mips_srli_b:
1609 case Intrinsic::mips_srli_h:
1610 case Intrinsic::mips_srli_w:
1611 case Intrinsic::mips_srli_d:
1612 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
1613 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1614 case Intrinsic::mips_subv_b:
1615 case Intrinsic::mips_subv_h:
1616 case Intrinsic::mips_subv_w:
1617 case Intrinsic::mips_subv_d:
1618 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
1620 case Intrinsic::mips_subvi_b:
1621 case Intrinsic::mips_subvi_h:
1622 case Intrinsic::mips_subvi_w:
1623 case Intrinsic::mips_subvi_d:
1624 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
1625 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1626 case Intrinsic::mips_vshf_b:
1627 case Intrinsic::mips_vshf_h:
1628 case Intrinsic::mips_vshf_w:
1629 case Intrinsic::mips_vshf_d:
1630 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1631 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
1632 case Intrinsic::mips_xor_v:
1633 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
1635 case Intrinsic::mips_xori_b:
1636 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
1637 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1641 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1643 SDValue ChainIn = Op->getOperand(0);
1644 SDValue Address = Op->getOperand(2);
1645 SDValue Offset = Op->getOperand(3);
1646 EVT ResTy = Op->getValueType(0);
1647 EVT PtrTy = Address->getValueType(0);
1649 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1651 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1655 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1656 SelectionDAG &DAG) const {
1657 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1661 case Intrinsic::mips_extp:
1662 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1663 case Intrinsic::mips_extpdp:
1664 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1665 case Intrinsic::mips_extr_w:
1666 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1667 case Intrinsic::mips_extr_r_w:
1668 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1669 case Intrinsic::mips_extr_rs_w:
1670 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1671 case Intrinsic::mips_extr_s_h:
1672 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1673 case Intrinsic::mips_mthlip:
1674 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1675 case Intrinsic::mips_mulsaq_s_w_ph:
1676 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1677 case Intrinsic::mips_maq_s_w_phl:
1678 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1679 case Intrinsic::mips_maq_s_w_phr:
1680 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1681 case Intrinsic::mips_maq_sa_w_phl:
1682 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1683 case Intrinsic::mips_maq_sa_w_phr:
1684 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1685 case Intrinsic::mips_dpaq_s_w_ph:
1686 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1687 case Intrinsic::mips_dpsq_s_w_ph:
1688 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1689 case Intrinsic::mips_dpaq_sa_l_w:
1690 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1691 case Intrinsic::mips_dpsq_sa_l_w:
1692 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1693 case Intrinsic::mips_dpaqx_s_w_ph:
1694 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1695 case Intrinsic::mips_dpaqx_sa_w_ph:
1696 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1697 case Intrinsic::mips_dpsqx_s_w_ph:
1698 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1699 case Intrinsic::mips_dpsqx_sa_w_ph:
1700 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
1701 case Intrinsic::mips_ld_b:
1702 case Intrinsic::mips_ld_h:
1703 case Intrinsic::mips_ld_w:
1704 case Intrinsic::mips_ld_d:
1705 return lowerMSALoadIntr(Op, DAG, Intr);
1709 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1711 SDValue ChainIn = Op->getOperand(0);
1712 SDValue Value = Op->getOperand(2);
1713 SDValue Address = Op->getOperand(3);
1714 SDValue Offset = Op->getOperand(4);
1715 EVT PtrTy = Address->getValueType(0);
1717 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1719 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1723 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1724 SelectionDAG &DAG) const {
1725 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1729 case Intrinsic::mips_st_b:
1730 case Intrinsic::mips_st_h:
1731 case Intrinsic::mips_st_w:
1732 case Intrinsic::mips_st_d:
1733 return lowerMSAStoreIntr(Op, DAG, Intr);
1737 /// \brief Check if the given BuildVectorSDNode is a splat.
1738 /// This method currently relies on DAG nodes being reused when equivalent,
1739 /// so it's possible for this to return false even when isConstantSplat returns
1741 static bool isSplatVector(const BuildVectorSDNode *N) {
1742 unsigned int nOps = N->getNumOperands();
1743 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1745 SDValue Operand0 = N->getOperand(0);
1747 for (unsigned int i = 1; i < nOps; ++i) {
1748 if (N->getOperand(i) != Operand0)
1755 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1757 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1758 // choose to sign-extend but we could have equally chosen zero-extend. The
1759 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1760 // result into this node later (possibly changing it to a zero-extend in the
1762 SDValue MipsSETargetLowering::
1763 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1765 EVT ResTy = Op->getValueType(0);
1766 SDValue Op0 = Op->getOperand(0);
1767 EVT VecTy = Op0->getValueType(0);
1769 if (!VecTy.is128BitVector())
1772 if (ResTy.isInteger()) {
1773 SDValue Op1 = Op->getOperand(1);
1774 EVT EltTy = VecTy.getVectorElementType();
1775 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1776 DAG.getValueType(EltTy));
1782 static bool isConstantOrUndef(const SDValue Op) {
1783 if (Op->getOpcode() == ISD::UNDEF)
1785 if (dyn_cast<ConstantSDNode>(Op))
1787 if (dyn_cast<ConstantFPSDNode>(Op))
1792 static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
1793 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
1794 if (isConstantOrUndef(Op->getOperand(i)))
1799 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1802 // Lowers according to the following rules:
1803 // - Constant splats are legal as-is as long as the SplatBitSize is a power of
1804 // 2 less than or equal to 64 and the value fits into a signed 10-bit
1806 // - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
1807 // is a power of 2 less than or equal to 64 and the value does not fit into a
1808 // signed 10-bit immediate
1809 // - Non-constant splats are legal as-is.
1810 // - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
1811 // - All others are illegal and must be expanded.
1812 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1813 SelectionDAG &DAG) const {
1814 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1815 EVT ResTy = Op->getValueType(0);
1817 APInt SplatValue, SplatUndef;
1818 unsigned SplatBitSize;
1821 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1824 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1826 !Subtarget->isLittle()) && SplatBitSize <= 64) {
1827 // We can only cope with 8, 16, 32, or 64-bit elements
1828 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
1832 // If the value fits into a simm10 then we can use ldi.[bhwd]
1833 if (SplatValue.isSignedIntN(10))
1838 switch (SplatBitSize) {
1842 ViaVecTy = MVT::v16i8;
1845 ViaVecTy = MVT::v8i16;
1848 ViaVecTy = MVT::v4i32;
1851 // There's no fill.d to fall back on for 64-bit values
1855 SmallVector<SDValue, 16> Ops;
1856 SDValue Constant = DAG.getConstant(SplatValue.sextOrSelf(32), MVT::i32);
1858 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i)
1859 Ops.push_back(Constant);
1861 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Node), ViaVecTy,
1862 &Ops[0], Ops.size());
1864 if (ViaVecTy != ResTy)
1865 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
1868 } else if (isSplatVector(Node))
1870 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
1871 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
1872 // The resulting code is the same length as the expansion, but it doesn't
1873 // use memory operations
1874 EVT ResTy = Node->getValueType(0);
1876 assert(ResTy.isVector());
1878 unsigned NumElts = ResTy.getVectorNumElements();
1879 SDValue Vector = DAG.getUNDEF(ResTy);
1880 for (unsigned i = 0; i < NumElts; ++i) {
1881 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
1882 Node->getOperand(i),
1883 DAG.getConstant(i, MVT::i32));
1891 // Lower VECTOR_SHUFFLE into SHF (if possible).
1893 // SHF splits the vector into blocks of four elements, then shuffles these
1894 // elements according to a <4 x i2> constant (encoded as an integer immediate).
1896 // It is therefore possible to lower into SHF when the mask takes the form:
1897 // <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
1898 // When undef's appear they are treated as if they were whatever value is
1899 // necessary in order to fit the above form.
1902 // %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
1903 // <8 x i32> <i32 3, i32 2, i32 1, i32 0,
1904 // i32 7, i32 6, i32 5, i32 4>
1906 // (SHF_H $w0, $w1, 27)
1907 // where the 27 comes from:
1908 // 3 + (2 << 2) + (1 << 4) + (0 << 6)
1909 static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
1910 SmallVector<int, 16> Indices,
1911 SelectionDAG &DAG) {
1912 int SHFIndices[4] = { -1, -1, -1, -1 };
1914 if (Indices.size() < 4)
1917 for (unsigned i = 0; i < 4; ++i) {
1918 for (unsigned j = i; j < Indices.size(); j += 4) {
1919 int Idx = Indices[j];
1921 // Convert from vector index to 4-element subvector index
1922 // If an index refers to an element outside of the subvector then give up
1925 if (Idx < 0 || Idx >= 4)
1929 // If the mask has an undef, replace it with the current index.
1930 // Note that it might still be undef if the current index is also undef
1931 if (SHFIndices[i] == -1)
1932 SHFIndices[i] = Idx;
1934 // Check that non-undef values are the same as in the mask. If they
1935 // aren't then give up
1936 if (!(Idx == -1 || Idx == SHFIndices[i]))
1941 // Calculate the immediate. Replace any remaining undefs with zero
1943 for (int i = 3; i >= 0; --i) {
1944 int Idx = SHFIndices[i];
1953 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
1954 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
1957 // Lower VECTOR_SHUFFLE into ILVEV (if possible).
1959 // ILVEV interleaves the even elements from each vector.
1961 // It is possible to lower into ILVEV when the mask takes the form:
1962 // <0, n, 2, n+2, 4, n+4, ...>
1963 // where n is the number of elements in the vector.
1965 // When undef's appear in the mask they are treated as if they were whatever
1966 // value is necessary in order to fit the above form.
1967 static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
1968 SmallVector<int, 16> Indices,
1969 SelectionDAG &DAG) {
1970 assert ((Indices.size() % 2) == 0);
1972 int WtIdx = ResTy.getVectorNumElements();
1974 for (unsigned i = 0; i < Indices.size(); i += 2) {
1975 if (Indices[i] != -1 && Indices[i] != WsIdx)
1977 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
1983 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
1987 // Lower VECTOR_SHUFFLE into ILVOD (if possible).
1989 // ILVOD interleaves the odd elements from each vector.
1991 // It is possible to lower into ILVOD when the mask takes the form:
1992 // <1, n+1, 3, n+3, 5, n+5, ...>
1993 // where n is the number of elements in the vector.
1995 // When undef's appear in the mask they are treated as if they were whatever
1996 // value is necessary in order to fit the above form.
1997 static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
1998 SmallVector<int, 16> Indices,
1999 SelectionDAG &DAG) {
2000 assert ((Indices.size() % 2) == 0);
2002 int WtIdx = ResTy.getVectorNumElements() + 1;
2004 for (unsigned i = 0; i < Indices.size(); i += 2) {
2005 if (Indices[i] != -1 && Indices[i] != WsIdx)
2007 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2013 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2017 // Lower VECTOR_SHUFFLE into ILVL (if possible).
2019 // ILVL interleaves consecutive elements from the left half of each vector.
2021 // It is possible to lower into ILVL when the mask takes the form:
2022 // <0, n, 1, n+1, 2, n+2, ...>
2023 // where n is the number of elements in the vector.
2025 // When undef's appear in the mask they are treated as if they were whatever
2026 // value is necessary in order to fit the above form.
2027 static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2028 SmallVector<int, 16> Indices,
2029 SelectionDAG &DAG) {
2030 assert ((Indices.size() % 2) == 0);
2032 int WtIdx = ResTy.getVectorNumElements();
2034 for (unsigned i = 0; i < Indices.size(); i += 2) {
2035 if (Indices[i] != -1 && Indices[i] != WsIdx)
2037 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2043 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2047 // Lower VECTOR_SHUFFLE into ILVR (if possible).
2049 // ILVR interleaves consecutive elements from the right half of each vector.
2051 // It is possible to lower into ILVR when the mask takes the form:
2052 // <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2053 // where n is the number of elements in the vector and x is half n.
2055 // When undef's appear in the mask they are treated as if they were whatever
2056 // value is necessary in order to fit the above form.
2057 static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2058 SmallVector<int, 16> Indices,
2059 SelectionDAG &DAG) {
2060 assert ((Indices.size() % 2) == 0);
2061 unsigned NumElts = ResTy.getVectorNumElements();
2062 int WsIdx = NumElts / 2;
2063 int WtIdx = NumElts + NumElts / 2;
2065 for (unsigned i = 0; i < Indices.size(); i += 2) {
2066 if (Indices[i] != -1 && Indices[i] != WsIdx)
2068 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2074 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2078 // Lower VECTOR_SHUFFLE into PCKEV (if possible).
2080 // PCKEV copies the even elements of each vector into the result vector.
2082 // It is possible to lower into PCKEV when the mask takes the form:
2083 // <0, 2, 4, ..., n, n+2, n+4, ...>
2084 // where n is the number of elements in the vector.
2086 // When undef's appear in the mask they are treated as if they were whatever
2087 // value is necessary in order to fit the above form.
2088 static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2089 SmallVector<int, 16> Indices,
2090 SelectionDAG &DAG) {
2091 assert ((Indices.size() % 2) == 0);
2094 for (unsigned i = 0; i < Indices.size(); ++i) {
2095 if (Indices[i] != -1 && Indices[i] != Idx)
2100 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2104 // Lower VECTOR_SHUFFLE into PCKOD (if possible).
2106 // PCKOD copies the odd elements of each vector into the result vector.
2108 // It is possible to lower into PCKOD when the mask takes the form:
2109 // <1, 3, 5, ..., n+1, n+3, n+5, ...>
2110 // where n is the number of elements in the vector.
2112 // When undef's appear in the mask they are treated as if they were whatever
2113 // value is necessary in order to fit the above form.
2114 static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2115 SmallVector<int, 16> Indices,
2116 SelectionDAG &DAG) {
2117 assert ((Indices.size() % 2) == 0);
2120 for (unsigned i = 0; i < Indices.size(); ++i) {
2121 if (Indices[i] != -1 && Indices[i] != Idx)
2126 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2130 // Lower VECTOR_SHUFFLE into VSHF.
2132 // This mostly consists of converting the shuffle indices in Indices into a
2133 // BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2134 // also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2135 // if the type is v8i16 and all the indices are less than 8 then the second
2136 // operand is unused and can be replaced with anything. We choose to replace it
2137 // with the used operand since this reduces the number of instructions overall.
2138 static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2139 SmallVector<int, 16> Indices,
2140 SelectionDAG &DAG) {
2141 SmallVector<SDValue, 16> Ops;
2144 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2145 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2146 bool Using1stVec = false;
2147 bool Using2ndVec = false;
2149 int ResTyNumElts = ResTy.getVectorNumElements();
2151 for (int i = 0; i < ResTyNumElts; ++i) {
2152 // Idx == -1 means UNDEF
2153 int Idx = Indices[i];
2155 if (0 <= Idx && Idx < ResTyNumElts)
2157 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2161 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2163 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2165 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2168 if (Using1stVec && Using2ndVec) {
2169 Op0 = Op->getOperand(0);
2170 Op1 = Op->getOperand(1);
2171 } else if (Using1stVec)
2172 Op0 = Op1 = Op->getOperand(0);
2173 else if (Using2ndVec)
2174 Op0 = Op1 = Op->getOperand(1);
2176 llvm_unreachable("shuffle vector mask references neither vector operand?");
2178 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2181 // Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2182 // indices in the shuffle.
2183 SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2184 SelectionDAG &DAG) const {
2185 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2186 EVT ResTy = Op->getValueType(0);
2188 if (!ResTy.is128BitVector())
2191 int ResTyNumElts = ResTy.getVectorNumElements();
2192 SmallVector<int, 16> Indices;
2194 for (int i = 0; i < ResTyNumElts; ++i)
2195 Indices.push_back(Node->getMaskElt(i));
2197 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2198 if (Result.getNode())
2200 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2201 if (Result.getNode())
2203 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2204 if (Result.getNode())
2206 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2207 if (Result.getNode())
2209 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2210 if (Result.getNode())
2212 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2213 if (Result.getNode())
2215 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2216 if (Result.getNode())
2218 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2221 MachineBasicBlock * MipsSETargetLowering::
2222 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2224 // bposge32_pseudo $vr0
2234 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2236 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2237 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2238 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2239 DebugLoc DL = MI->getDebugLoc();
2240 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2241 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2242 MachineFunction *F = BB->getParent();
2243 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2244 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2245 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2248 F->insert(It, Sink);
2250 // Transfer the remainder of BB and its successor edges to Sink.
2251 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2253 Sink->transferSuccessorsAndUpdatePHIs(BB);
2256 BB->addSuccessor(FBB);
2257 BB->addSuccessor(TBB);
2258 FBB->addSuccessor(Sink);
2259 TBB->addSuccessor(Sink);
2261 // Insert the real bposge32 instruction to $BB.
2262 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2265 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2266 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2267 .addReg(Mips::ZERO).addImm(0);
2268 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2271 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2272 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2273 .addReg(Mips::ZERO).addImm(1);
2275 // Insert phi function to $Sink.
2276 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2277 MI->getOperand(0).getReg())
2278 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2280 MI->eraseFromParent(); // The pseudo instruction is gone now.
2284 MachineBasicBlock * MipsSETargetLowering::
2285 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2286 unsigned BranchOp) const{
2288 // vany_nonzero $rd, $ws
2299 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2301 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2302 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2303 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2304 DebugLoc DL = MI->getDebugLoc();
2305 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2306 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2307 MachineFunction *F = BB->getParent();
2308 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2309 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2310 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2313 F->insert(It, Sink);
2315 // Transfer the remainder of BB and its successor edges to Sink.
2316 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2318 Sink->transferSuccessorsAndUpdatePHIs(BB);
2321 BB->addSuccessor(FBB);
2322 BB->addSuccessor(TBB);
2323 FBB->addSuccessor(Sink);
2324 TBB->addSuccessor(Sink);
2326 // Insert the real bnz.b instruction to $BB.
2327 BuildMI(BB, DL, TII->get(BranchOp))
2328 .addReg(MI->getOperand(1).getReg())
2332 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2333 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2334 .addReg(Mips::ZERO).addImm(0);
2335 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2338 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2339 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2340 .addReg(Mips::ZERO).addImm(1);
2342 // Insert phi function to $Sink.
2343 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2344 MI->getOperand(0).getReg())
2345 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2347 MI->eraseFromParent(); // The pseudo instruction is gone now.
2351 // Emit the COPY_FW pseudo instruction.
2353 // copy_fw_pseudo $fd, $ws, n
2355 // copy_u_w $rt, $ws, $n
2358 // When n is zero, the equivalent operation can be performed with (potentially)
2359 // zero instructions due to register overlaps. This optimization is never valid
2360 // for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2361 MachineBasicBlock * MipsSETargetLowering::
2362 emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2363 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2364 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2365 DebugLoc DL = MI->getDebugLoc();
2366 unsigned Fd = MI->getOperand(0).getReg();
2367 unsigned Ws = MI->getOperand(1).getReg();
2368 unsigned Lane = MI->getOperand(2).getImm();
2371 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2373 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2375 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(1);
2376 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2379 MI->eraseFromParent(); // The pseudo instruction is gone now.
2383 // Emit the COPY_FD pseudo instruction.
2385 // copy_fd_pseudo $fd, $ws, n
2387 // splati.d $wt, $ws, $n
2388 // copy $fd, $wt:sub_64
2390 // When n is zero, the equivalent operation can be performed with (potentially)
2391 // zero instructions due to register overlaps. This optimization is always
2392 // valid because FR=1 mode which is the only supported mode in MSA.
2393 MachineBasicBlock * MipsSETargetLowering::
2394 emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2395 assert(Subtarget->isFP64bit());
2397 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2398 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2399 unsigned Fd = MI->getOperand(0).getReg();
2400 unsigned Ws = MI->getOperand(1).getReg();
2401 unsigned Lane = MI->getOperand(2).getImm() * 2;
2402 DebugLoc DL = MI->getDebugLoc();
2405 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2407 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2409 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2410 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2413 MI->eraseFromParent(); // The pseudo instruction is gone now.
2417 // Emit the INSERT_FW pseudo instruction.
2419 // insert_fw_pseudo $wd, $wd_in, $n, $fs
2421 // subreg_to_reg $wt:sub_lo, $fs
2422 // insve_w $wd[$n], $wd_in, $wt[0]
2424 MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2425 MachineBasicBlock *BB) const {
2426 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2427 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2428 DebugLoc DL = MI->getDebugLoc();
2429 unsigned Wd = MI->getOperand(0).getReg();
2430 unsigned Wd_in = MI->getOperand(1).getReg();
2431 unsigned Lane = MI->getOperand(2).getImm();
2432 unsigned Fs = MI->getOperand(3).getReg();
2433 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2435 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2438 .addImm(Mips::sub_lo);
2439 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
2444 MI->eraseFromParent(); // The pseudo instruction is gone now.
2448 // Emit the INSERT_FD pseudo instruction.
2450 // insert_fd_pseudo $wd, $fs, n
2452 // subreg_to_reg $wt:sub_64, $fs
2453 // insve_d $wd[$n], $wd_in, $wt[0]
2455 MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2456 MachineBasicBlock *BB) const {
2457 assert(Subtarget->isFP64bit());
2459 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2460 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2461 DebugLoc DL = MI->getDebugLoc();
2462 unsigned Wd = MI->getOperand(0).getReg();
2463 unsigned Wd_in = MI->getOperand(1).getReg();
2464 unsigned Lane = MI->getOperand(2).getImm();
2465 unsigned Fs = MI->getOperand(3).getReg();
2466 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2468 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2471 .addImm(Mips::sub_64);
2472 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
2477 MI->eraseFromParent(); // The pseudo instruction is gone now.
2481 // Emit the FILL_FW pseudo instruction.
2483 // fill_fw_pseudo $wd, $fs
2485 // implicit_def $wt1
2486 // insert_subreg $wt2:subreg_lo, $wt1, $fs
2487 // splati.w $wd, $wt2[0]
2489 MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2490 MachineBasicBlock *BB) const {
2491 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2492 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2493 DebugLoc DL = MI->getDebugLoc();
2494 unsigned Wd = MI->getOperand(0).getReg();
2495 unsigned Fs = MI->getOperand(1).getReg();
2496 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2497 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2499 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2500 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2503 .addImm(Mips::sub_lo);
2504 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2506 MI->eraseFromParent(); // The pseudo instruction is gone now.
2510 // Emit the FILL_FD pseudo instruction.
2512 // fill_fd_pseudo $wd, $fs
2514 // implicit_def $wt1
2515 // insert_subreg $wt2:subreg_64, $wt1, $fs
2516 // splati.d $wd, $wt2[0]
2518 MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2519 MachineBasicBlock *BB) const {
2520 assert(Subtarget->isFP64bit());
2522 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2523 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2524 DebugLoc DL = MI->getDebugLoc();
2525 unsigned Wd = MI->getOperand(0).getReg();
2526 unsigned Fs = MI->getOperand(1).getReg();
2527 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2528 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2530 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2531 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2534 .addImm(Mips::sub_64);
2535 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
2537 MI->eraseFromParent(); // The pseudo instruction is gone now.
2541 // Emit the FEXP2_W_1 pseudo instructions.
2543 // fexp2_w_1_pseudo $wd, $wt
2546 // fexp2.w $wd, $ws, $wt
2548 MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2549 MachineBasicBlock *BB) const {
2550 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2551 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2552 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2553 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2554 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2555 DebugLoc DL = MI->getDebugLoc();
2557 // Splat 1.0 into a vector
2558 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2559 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2561 // Emit 1.0 * fexp2(Wt)
2562 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2564 .addReg(MI->getOperand(1).getReg());
2566 MI->eraseFromParent(); // The pseudo instruction is gone now.
2570 // Emit the FEXP2_D_1 pseudo instructions.
2572 // fexp2_d_1_pseudo $wd, $wt
2575 // fexp2.d $wd, $ws, $wt
2577 MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2578 MachineBasicBlock *BB) const {
2579 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2580 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2581 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2582 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2583 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2584 DebugLoc DL = MI->getDebugLoc();
2586 // Splat 1.0 into a vector
2587 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2588 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2590 // Emit 1.0 * fexp2(Wt)
2591 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2593 .addReg(MI->getOperand(1).getReg());
2595 MI->eraseFromParent(); // The pseudo instruction is gone now.