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/Support/raw_ostream.h"
23 #include "llvm/Target/TargetInstrInfo.h"
28 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
29 cl::desc("MIPS: Enable tail calls."), cl::init(false));
31 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
32 cl::desc("Expand double precision loads and "
33 "stores to their single precision "
36 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
37 : MipsTargetLowering(TM) {
38 // Set up the register classes
39 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
42 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
44 if (Subtarget->hasDSP() || Subtarget->hasMSA()) {
45 // Expand all truncating stores and extending loads.
46 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
47 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
49 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
50 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
51 setTruncStoreAction((MVT::SimpleValueType)VT0,
52 (MVT::SimpleValueType)VT1, Expand);
54 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
55 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
56 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
60 if (Subtarget->hasDSP()) {
61 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
63 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
64 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
66 // Expand all builtin opcodes.
67 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
68 setOperationAction(Opc, VecTys[i], Expand);
70 setOperationAction(ISD::ADD, VecTys[i], Legal);
71 setOperationAction(ISD::SUB, VecTys[i], Legal);
72 setOperationAction(ISD::LOAD, VecTys[i], Legal);
73 setOperationAction(ISD::STORE, VecTys[i], Legal);
74 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
77 setTargetDAGCombine(ISD::SHL);
78 setTargetDAGCombine(ISD::SRA);
79 setTargetDAGCombine(ISD::SRL);
80 setTargetDAGCombine(ISD::SETCC);
81 setTargetDAGCombine(ISD::VSELECT);
84 if (Subtarget->hasDSPR2())
85 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
87 if (Subtarget->hasMSA()) {
88 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
89 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
90 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
91 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
92 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
93 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
94 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
96 setTargetDAGCombine(ISD::AND);
97 setTargetDAGCombine(ISD::OR);
98 setTargetDAGCombine(ISD::SRA);
99 setTargetDAGCombine(ISD::VSELECT);
100 setTargetDAGCombine(ISD::XOR);
103 if (!Subtarget->mipsSEUsesSoftFloat()) {
104 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
106 // When dealing with single precision only, use libcalls
107 if (!Subtarget->isSingleFloat()) {
108 if (Subtarget->isFP64bit())
109 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
111 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
115 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
116 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
117 setOperationAction(ISD::MULHS, MVT::i32, Custom);
118 setOperationAction(ISD::MULHU, MVT::i32, Custom);
121 setOperationAction(ISD::MULHS, MVT::i64, Custom);
122 setOperationAction(ISD::MULHU, MVT::i64, Custom);
123 setOperationAction(ISD::MUL, MVT::i64, Custom);
126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
127 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
129 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
130 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
131 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
132 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
133 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
134 setOperationAction(ISD::LOAD, MVT::i32, Custom);
135 setOperationAction(ISD::STORE, MVT::i32, Custom);
137 setTargetDAGCombine(ISD::ADDE);
138 setTargetDAGCombine(ISD::SUBE);
139 setTargetDAGCombine(ISD::MUL);
141 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
142 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
143 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
146 setOperationAction(ISD::LOAD, MVT::f64, Custom);
147 setOperationAction(ISD::STORE, MVT::f64, Custom);
150 computeRegisterProperties();
153 const MipsTargetLowering *
154 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
155 return new MipsSETargetLowering(TM);
158 // Enable MSA support for the given integer type and Register class.
159 void MipsSETargetLowering::
160 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
161 addRegisterClass(Ty, RC);
163 // Expand all builtin opcodes.
164 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
165 setOperationAction(Opc, Ty, Expand);
167 setOperationAction(ISD::BITCAST, Ty, Legal);
168 setOperationAction(ISD::LOAD, Ty, Legal);
169 setOperationAction(ISD::STORE, Ty, Legal);
170 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
171 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
172 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
174 setOperationAction(ISD::ADD, Ty, Legal);
175 setOperationAction(ISD::AND, Ty, Legal);
176 setOperationAction(ISD::CTLZ, Ty, Legal);
177 setOperationAction(ISD::CTPOP, Ty, Legal);
178 setOperationAction(ISD::MUL, Ty, Legal);
179 setOperationAction(ISD::OR, Ty, Legal);
180 setOperationAction(ISD::SDIV, Ty, Legal);
181 setOperationAction(ISD::SREM, Ty, Legal);
182 setOperationAction(ISD::SHL, Ty, Legal);
183 setOperationAction(ISD::SRA, Ty, Legal);
184 setOperationAction(ISD::SRL, Ty, Legal);
185 setOperationAction(ISD::SUB, Ty, Legal);
186 setOperationAction(ISD::UDIV, Ty, Legal);
187 setOperationAction(ISD::UREM, Ty, Legal);
188 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
189 setOperationAction(ISD::VSELECT, Ty, Legal);
190 setOperationAction(ISD::XOR, Ty, Legal);
192 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
193 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
194 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
195 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
196 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
199 setOperationAction(ISD::SETCC, Ty, Legal);
200 setCondCodeAction(ISD::SETNE, Ty, Expand);
201 setCondCodeAction(ISD::SETGE, Ty, Expand);
202 setCondCodeAction(ISD::SETGT, Ty, Expand);
203 setCondCodeAction(ISD::SETUGE, Ty, Expand);
204 setCondCodeAction(ISD::SETUGT, Ty, Expand);
207 // Enable MSA support for the given floating-point type and Register class.
208 void MipsSETargetLowering::
209 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
210 addRegisterClass(Ty, RC);
212 // Expand all builtin opcodes.
213 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
214 setOperationAction(Opc, Ty, Expand);
216 setOperationAction(ISD::LOAD, Ty, Legal);
217 setOperationAction(ISD::STORE, Ty, Legal);
218 setOperationAction(ISD::BITCAST, Ty, Legal);
219 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
220 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
221 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
223 if (Ty != MVT::v8f16) {
224 setOperationAction(ISD::FABS, Ty, Legal);
225 setOperationAction(ISD::FADD, Ty, Legal);
226 setOperationAction(ISD::FDIV, Ty, Legal);
227 setOperationAction(ISD::FEXP2, Ty, Legal);
228 setOperationAction(ISD::FLOG2, Ty, Legal);
229 setOperationAction(ISD::FMA, Ty, Legal);
230 setOperationAction(ISD::FMUL, Ty, Legal);
231 setOperationAction(ISD::FRINT, Ty, Legal);
232 setOperationAction(ISD::FSQRT, Ty, Legal);
233 setOperationAction(ISD::FSUB, Ty, Legal);
234 setOperationAction(ISD::VSELECT, Ty, Legal);
236 setOperationAction(ISD::SETCC, Ty, Legal);
237 setCondCodeAction(ISD::SETOGE, Ty, Expand);
238 setCondCodeAction(ISD::SETOGT, Ty, Expand);
239 setCondCodeAction(ISD::SETUGE, Ty, Expand);
240 setCondCodeAction(ISD::SETUGT, Ty, Expand);
241 setCondCodeAction(ISD::SETGE, Ty, Expand);
242 setCondCodeAction(ISD::SETGT, Ty, Expand);
247 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
250 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
263 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
264 SelectionDAG &DAG) const {
265 switch(Op.getOpcode()) {
266 case ISD::LOAD: return lowerLOAD(Op, DAG);
267 case ISD::STORE: return lowerSTORE(Op, DAG);
268 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
269 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
270 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
271 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
272 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
273 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
274 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
276 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
277 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
278 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
279 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
280 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
281 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
284 return MipsTargetLowering::LowerOperation(Op, DAG);
288 // Transforms a subgraph in CurDAG if the following pattern is found:
289 // (addc multLo, Lo0), (adde multHi, Hi0),
291 // multHi/Lo: product of multiplication
292 // Lo0: initial value of Lo register
293 // Hi0: initial value of Hi register
294 // Return true if pattern matching was successful.
295 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
296 // ADDENode's second operand must be a flag output of an ADDC node in order
297 // for the matching to be successful.
298 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
300 if (ADDCNode->getOpcode() != ISD::ADDC)
303 SDValue MultHi = ADDENode->getOperand(0);
304 SDValue MultLo = ADDCNode->getOperand(0);
305 SDNode *MultNode = MultHi.getNode();
306 unsigned MultOpc = MultHi.getOpcode();
308 // MultHi and MultLo must be generated by the same node,
309 if (MultLo.getNode() != MultNode)
312 // and it must be a multiplication.
313 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
316 // MultLo amd MultHi must be the first and second output of MultNode
318 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
321 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
322 // of the values of MultNode, in which case MultNode will be removed in later
324 // If there exist users other than ADDENode or ADDCNode, this function returns
325 // here, which will result in MultNode being mapped to a single MULT
326 // instruction node rather than a pair of MULT and MADD instructions being
328 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
333 // Initialize accumulator.
334 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
335 ADDCNode->getOperand(1),
336 ADDENode->getOperand(1));
338 // create MipsMAdd(u) node
339 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
341 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
342 MultNode->getOperand(0),// Factor 0
343 MultNode->getOperand(1),// Factor 1
346 // replace uses of adde and addc here
347 if (!SDValue(ADDCNode, 0).use_empty()) {
348 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
349 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
351 if (!SDValue(ADDENode, 0).use_empty()) {
352 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
353 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
360 // Transforms a subgraph in CurDAG if the following pattern is found:
361 // (addc Lo0, multLo), (sube Hi0, multHi),
363 // multHi/Lo: product of multiplication
364 // Lo0: initial value of Lo register
365 // Hi0: initial value of Hi register
366 // Return true if pattern matching was successful.
367 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
368 // SUBENode's second operand must be a flag output of an SUBC node in order
369 // for the matching to be successful.
370 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
372 if (SUBCNode->getOpcode() != ISD::SUBC)
375 SDValue MultHi = SUBENode->getOperand(1);
376 SDValue MultLo = SUBCNode->getOperand(1);
377 SDNode *MultNode = MultHi.getNode();
378 unsigned MultOpc = MultHi.getOpcode();
380 // MultHi and MultLo must be generated by the same node,
381 if (MultLo.getNode() != MultNode)
384 // and it must be a multiplication.
385 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
388 // MultLo amd MultHi must be the first and second output of MultNode
390 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
393 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
394 // of the values of MultNode, in which case MultNode will be removed in later
396 // If there exist users other than SUBENode or SUBCNode, this function returns
397 // here, which will result in MultNode being mapped to a single MULT
398 // instruction node rather than a pair of MULT and MSUB instructions being
400 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
405 // Initialize accumulator.
406 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
407 SUBCNode->getOperand(0),
408 SUBENode->getOperand(0));
410 // create MipsSub(u) node
411 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
413 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
414 MultNode->getOperand(0),// Factor 0
415 MultNode->getOperand(1),// Factor 1
418 // replace uses of sube and subc here
419 if (!SDValue(SUBCNode, 0).use_empty()) {
420 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
421 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
423 if (!SDValue(SUBENode, 0).use_empty()) {
424 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
425 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
431 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
432 TargetLowering::DAGCombinerInfo &DCI,
433 const MipsSubtarget *Subtarget) {
434 if (DCI.isBeforeLegalize())
437 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
439 return SDValue(N, 0);
444 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
446 // Performs the following transformations:
447 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
448 // sign/zero-extension is completely overwritten by the new one performed by
450 // - Removes redundant zero extensions performed by an ISD::AND.
451 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
452 TargetLowering::DAGCombinerInfo &DCI,
453 const MipsSubtarget *Subtarget) {
454 if (!Subtarget->hasMSA())
457 SDValue Op0 = N->getOperand(0);
458 SDValue Op1 = N->getOperand(1);
459 unsigned Op0Opcode = Op0->getOpcode();
461 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
462 // where $d + 1 == 2^n and n == 32
463 // or $d + 1 == 2^n and n <= 32 and ZExt
464 // -> (MipsVExtractZExt $a, $b, $c)
465 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
466 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
467 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
472 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
474 if (Log2IfPositive <= 0)
475 return SDValue(); // Mask+1 is not a power of 2
477 SDValue Op0Op2 = Op0->getOperand(2);
478 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
479 unsigned ExtendTySize = ExtendTy.getSizeInBits();
480 unsigned Log2 = Log2IfPositive;
482 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
483 Log2 == ExtendTySize) {
484 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
485 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
486 Op0->getVTList(), Ops, Op0->getNumOperands());
494 // Determine if the specified node is a constant vector splat.
496 // Returns true and sets Imm if:
497 // * N is a ISD::BUILD_VECTOR representing a constant splat
499 // This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
500 // differences are that it assumes the MSA has already been checked and the
501 // arbitrary requirement for a maximum of 32-bit integers isn't applied (and
502 // must not be in order for binsri.d to be selectable).
503 static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
504 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
509 APInt SplatValue, SplatUndef;
510 unsigned SplatBitSize;
513 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
522 // Test whether the given node is an all-ones build_vector.
523 static bool isVectorAllOnes(SDValue N) {
524 // Look through bitcasts. Endianness doesn't matter because we are looking
525 // for an all-ones value.
526 if (N->getOpcode() == ISD::BITCAST)
527 N = N->getOperand(0);
529 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
534 APInt SplatValue, SplatUndef;
535 unsigned SplatBitSize;
538 // Endianness doesn't matter in this context because we are looking for
539 // an all-ones value.
540 if (BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs))
541 return SplatValue.isAllOnesValue();
546 // Test whether N is the bitwise inverse of OfNode.
547 static bool isBitwiseInverse(SDValue N, SDValue OfNode) {
548 if (N->getOpcode() != ISD::XOR)
551 if (isVectorAllOnes(N->getOperand(0)))
552 return N->getOperand(1) == OfNode;
554 if (isVectorAllOnes(N->getOperand(1)))
555 return N->getOperand(0) == OfNode;
560 // Perform combines where ISD::OR is the root node.
562 // Performs the following transformations:
563 // - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
564 // where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
566 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
567 TargetLowering::DAGCombinerInfo &DCI,
568 const MipsSubtarget *Subtarget) {
569 if (!Subtarget->hasMSA())
572 EVT Ty = N->getValueType(0);
574 if (!Ty.is128BitVector())
577 SDValue Op0 = N->getOperand(0);
578 SDValue Op1 = N->getOperand(1);
580 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
581 SDValue Op0Op0 = Op0->getOperand(0);
582 SDValue Op0Op1 = Op0->getOperand(1);
583 SDValue Op1Op0 = Op1->getOperand(0);
584 SDValue Op1Op1 = Op1->getOperand(1);
585 bool IsLittleEndian = !Subtarget->isLittle();
587 SDValue IfSet, IfClr, Cond;
588 bool IsConstantMask = false;
591 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
592 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
594 // IfClr will be set if we find a valid match.
595 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
599 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
600 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
602 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
603 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
606 IsConstantMask = true;
609 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
610 // thing again using this mask.
611 // IfClr will be set if we find a valid match.
612 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
616 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) &&
617 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
619 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) &&
620 Mask.getBitWidth() == InvMask.getBitWidth() && Mask == ~InvMask)
623 IsConstantMask = true;
626 // If IfClr is not yet set, try looking for a non-constant match.
627 // IfClr will be set if we find a valid match amongst the eight
629 if (!IfClr.getNode()) {
630 if (isBitwiseInverse(Op0Op0, Op1Op0)) {
634 } else if (isBitwiseInverse(Op0Op1, Op1Op0)) {
638 } else if (isBitwiseInverse(Op0Op0, Op1Op1)) {
642 } else if (isBitwiseInverse(Op0Op1, Op1Op1)) {
646 } else if (isBitwiseInverse(Op1Op0, Op0Op0)) {
650 } else if (isBitwiseInverse(Op1Op1, Op0Op0)) {
654 } else if (isBitwiseInverse(Op1Op0, Op0Op1)) {
658 } else if (isBitwiseInverse(Op1Op1, Op0Op1)) {
665 // At this point, IfClr will be set if we have a valid match.
666 if (!IfClr.getNode())
669 assert(Cond.getNode() && IfSet.getNode());
671 // Fold degenerate cases.
672 if (IsConstantMask) {
673 if (Mask.isAllOnesValue())
679 // Transform the DAG into an equivalent VSELECT.
680 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfSet, IfClr);
686 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
687 TargetLowering::DAGCombinerInfo &DCI,
688 const MipsSubtarget *Subtarget) {
689 if (DCI.isBeforeLegalize())
692 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
694 return SDValue(N, 0);
699 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
700 EVT ShiftTy, SelectionDAG &DAG) {
701 // Clear the upper (64 - VT.sizeInBits) bits.
702 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
706 return DAG.getConstant(0, VT);
712 // If c is power of 2, return (shl x, log2(c)).
713 if (isPowerOf2_64(C))
714 return DAG.getNode(ISD::SHL, DL, VT, X,
715 DAG.getConstant(Log2_64(C), ShiftTy));
717 unsigned Log2Ceil = Log2_64_Ceil(C);
718 uint64_t Floor = 1LL << Log2_64(C);
719 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
721 // If |c - floor_c| <= |c - ceil_c|,
722 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
723 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
724 if (C - Floor <= Ceil - C) {
725 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
726 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
727 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
730 // If |c - floor_c| > |c - ceil_c|,
731 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
732 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
733 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
734 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
737 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
738 const TargetLowering::DAGCombinerInfo &DCI,
739 const MipsSETargetLowering *TL) {
740 EVT VT = N->getValueType(0);
742 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
744 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
745 VT, TL->getScalarShiftAmountTy(VT), DAG);
747 return SDValue(N, 0);
750 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
752 const MipsSubtarget *Subtarget) {
753 // See if this is a vector splat immediate node.
754 APInt SplatValue, SplatUndef;
755 unsigned SplatBitSize;
757 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
758 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
760 if (!Subtarget->hasDSP())
764 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
765 EltSize, !Subtarget->isLittle()) ||
766 (SplatBitSize != EltSize) ||
767 (SplatValue.getZExtValue() >= EltSize))
770 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
771 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
774 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
775 TargetLowering::DAGCombinerInfo &DCI,
776 const MipsSubtarget *Subtarget) {
777 EVT Ty = N->getValueType(0);
779 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
782 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
785 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
786 // constant splats into MipsISD::SHRA_DSP for DSPr2.
788 // Performs the following transformations:
789 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
790 // sign/zero-extension is completely overwritten by the new one performed by
791 // the ISD::SRA and ISD::SHL nodes.
792 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
795 // See performDSPShiftCombine for more information about the transformation
797 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
798 TargetLowering::DAGCombinerInfo &DCI,
799 const MipsSubtarget *Subtarget) {
800 EVT Ty = N->getValueType(0);
802 if (Subtarget->hasMSA()) {
803 SDValue Op0 = N->getOperand(0);
804 SDValue Op1 = N->getOperand(1);
806 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
807 // where $d + sizeof($c) == 32
808 // or $d + sizeof($c) <= 32 and SExt
809 // -> (MipsVExtractSExt $a, $b, $c)
810 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
811 SDValue Op0Op0 = Op0->getOperand(0);
812 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
817 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
818 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
821 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
822 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
824 if (TotalBits == 32 ||
825 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
827 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
828 Op0Op0->getOperand(2) };
829 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
830 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
836 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
839 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
843 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
844 TargetLowering::DAGCombinerInfo &DCI,
845 const MipsSubtarget *Subtarget) {
846 EVT Ty = N->getValueType(0);
848 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
851 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
854 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
855 bool IsV216 = (Ty == MVT::v2i16);
859 case ISD::SETNE: return true;
863 case ISD::SETGE: return IsV216;
867 case ISD::SETUGE: return !IsV216;
868 default: return false;
872 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
873 EVT Ty = N->getValueType(0);
875 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
878 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
881 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
882 N->getOperand(1), N->getOperand(2));
885 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
886 EVT Ty = N->getValueType(0);
888 if (Ty.is128BitVector() && Ty.isInteger()) {
889 // Try the following combines:
890 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
891 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
892 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
893 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
894 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
895 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
896 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
897 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
898 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
899 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
901 SDValue Op0 = N->getOperand(0);
903 if (Op0->getOpcode() != ISD::SETCC)
906 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
909 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
911 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
916 SDValue Op1 = N->getOperand(1);
917 SDValue Op2 = N->getOperand(2);
918 SDValue Op0Op0 = Op0->getOperand(0);
919 SDValue Op0Op1 = Op0->getOperand(1);
921 if (Op1 == Op0Op0 && Op2 == Op0Op1)
922 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
924 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
925 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
927 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
928 SDValue SetCC = N->getOperand(0);
930 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
933 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
934 SetCC.getOperand(0), SetCC.getOperand(1),
935 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
941 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
942 const MipsSubtarget *Subtarget) {
943 EVT Ty = N->getValueType(0);
945 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
946 // Try the following combines:
947 // (xor (or $a, $b), (build_vector allones))
948 // (xor (or $a, $b), (bitcast (build_vector allones)))
949 SDValue Op0 = N->getOperand(0);
950 SDValue Op1 = N->getOperand(1);
953 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
955 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
960 if (NotOp->getOpcode() == ISD::OR)
961 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
962 NotOp->getOperand(1));
969 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
970 SelectionDAG &DAG = DCI.DAG;
973 switch (N->getOpcode()) {
975 return performADDECombine(N, DAG, DCI, Subtarget);
977 Val = performANDCombine(N, DAG, DCI, Subtarget);
980 Val = performORCombine(N, DAG, DCI, Subtarget);
983 return performSUBECombine(N, DAG, DCI, Subtarget);
985 return performMULCombine(N, DAG, DCI, this);
987 return performSHLCombine(N, DAG, DCI, Subtarget);
989 return performSRACombine(N, DAG, DCI, Subtarget);
991 return performSRLCombine(N, DAG, DCI, Subtarget);
993 return performVSELECTCombine(N, DAG);
995 Val = performXORCombine(N, DAG, Subtarget);
998 Val = performSETCCCombine(N, DAG);
1002 if (Val.getNode()) {
1003 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
1004 N->printrWithDepth(dbgs(), &DAG);
1005 dbgs() << "\n=> \n";
1006 Val.getNode()->printrWithDepth(dbgs(), &DAG);
1011 return MipsTargetLowering::PerformDAGCombine(N, DCI);
1015 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1016 MachineBasicBlock *BB) const {
1017 switch (MI->getOpcode()) {
1019 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
1020 case Mips::BPOSGE32_PSEUDO:
1021 return emitBPOSGE32(MI, BB);
1022 case Mips::SNZ_B_PSEUDO:
1023 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
1024 case Mips::SNZ_H_PSEUDO:
1025 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
1026 case Mips::SNZ_W_PSEUDO:
1027 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
1028 case Mips::SNZ_D_PSEUDO:
1029 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
1030 case Mips::SNZ_V_PSEUDO:
1031 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
1032 case Mips::SZ_B_PSEUDO:
1033 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
1034 case Mips::SZ_H_PSEUDO:
1035 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
1036 case Mips::SZ_W_PSEUDO:
1037 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
1038 case Mips::SZ_D_PSEUDO:
1039 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
1040 case Mips::SZ_V_PSEUDO:
1041 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
1042 case Mips::COPY_FW_PSEUDO:
1043 return emitCOPY_FW(MI, BB);
1044 case Mips::COPY_FD_PSEUDO:
1045 return emitCOPY_FD(MI, BB);
1046 case Mips::INSERT_FW_PSEUDO:
1047 return emitINSERT_FW(MI, BB);
1048 case Mips::INSERT_FD_PSEUDO:
1049 return emitINSERT_FD(MI, BB);
1050 case Mips::FILL_FW_PSEUDO:
1051 return emitFILL_FW(MI, BB);
1052 case Mips::FILL_FD_PSEUDO:
1053 return emitFILL_FD(MI, BB);
1054 case Mips::FEXP2_W_1_PSEUDO:
1055 return emitFEXP2_W_1(MI, BB);
1056 case Mips::FEXP2_D_1_PSEUDO:
1057 return emitFEXP2_D_1(MI, BB);
1061 bool MipsSETargetLowering::
1062 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
1063 unsigned NextStackOffset,
1064 const MipsFunctionInfo& FI) const {
1065 if (!EnableMipsTailCalls)
1068 // Return false if either the callee or caller has a byval argument.
1069 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
1072 // Return true if the callee's argument area is no larger than the
1074 return NextStackOffset <= FI.getIncomingArgSize();
1077 void MipsSETargetLowering::
1078 getOpndList(SmallVectorImpl<SDValue> &Ops,
1079 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
1080 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
1081 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
1082 Ops.push_back(Callee);
1083 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
1084 InternalLinkage, CLI, Callee, Chain);
1087 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1088 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1090 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1091 return MipsTargetLowering::lowerLOAD(Op, DAG);
1093 // Replace a double precision load with two i32 loads and a buildpair64.
1095 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1096 EVT PtrVT = Ptr.getValueType();
1098 // i32 load from lower address.
1099 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1100 MachinePointerInfo(), Nd.isVolatile(),
1101 Nd.isNonTemporal(), Nd.isInvariant(),
1104 // i32 load from higher address.
1105 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1106 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1107 MachinePointerInfo(), Nd.isVolatile(),
1108 Nd.isNonTemporal(), Nd.isInvariant(),
1109 std::min(Nd.getAlignment(), 4U));
1111 if (!Subtarget->isLittle())
1114 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1115 SDValue Ops[2] = {BP, Hi.getValue(1)};
1116 return DAG.getMergeValues(Ops, 2, DL);
1119 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1120 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1122 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1123 return MipsTargetLowering::lowerSTORE(Op, DAG);
1125 // Replace a double precision store with two extractelement64s and i32 stores.
1127 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1128 EVT PtrVT = Ptr.getValueType();
1129 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1130 Val, DAG.getConstant(0, MVT::i32));
1131 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1132 Val, DAG.getConstant(1, MVT::i32));
1134 if (!Subtarget->isLittle())
1137 // i32 store to lower address.
1138 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1139 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
1142 // i32 store to higher address.
1143 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1144 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
1145 Nd.isVolatile(), Nd.isNonTemporal(),
1146 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
1149 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1150 bool HasLo, bool HasHi,
1151 SelectionDAG &DAG) const {
1152 EVT Ty = Op.getOperand(0).getValueType();
1154 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1155 Op.getOperand(0), Op.getOperand(1));
1159 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
1161 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
1163 if (!HasLo || !HasHi)
1164 return HasLo ? Lo : Hi;
1166 SDValue Vals[] = { Lo, Hi };
1167 return DAG.getMergeValues(Vals, 2, DL);
1171 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
1172 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1173 DAG.getConstant(0, MVT::i32));
1174 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1175 DAG.getConstant(1, MVT::i32));
1176 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
1179 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
1180 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1181 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
1182 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1185 // This function expands mips intrinsic nodes which have 64-bit input operands
1186 // or output values.
1188 // out64 = intrinsic-node in64
1190 // lo = copy (extract-element (in64, 0))
1191 // hi = copy (extract-element (in64, 1))
1192 // mips-specific-node
1195 // out64 = merge-values (v0, v1)
1197 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1199 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1200 SmallVector<SDValue, 3> Ops;
1203 // See if Op has a chain input.
1205 Ops.push_back(Op->getOperand(OpNo++));
1207 // The next operand is the intrinsic opcode.
1208 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1210 // See if the next operand has type i64.
1211 SDValue Opnd = Op->getOperand(++OpNo), In64;
1213 if (Opnd.getValueType() == MVT::i64)
1214 In64 = initAccumulator(Opnd, DL, DAG);
1216 Ops.push_back(Opnd);
1218 // Push the remaining operands.
1219 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1220 Ops.push_back(Op->getOperand(OpNo));
1222 // Add In64 to the end of the list.
1224 Ops.push_back(In64);
1227 SmallVector<EVT, 2> ResTys;
1229 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1231 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1234 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1235 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1240 assert(Val->getValueType(1) == MVT::Other);
1241 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1242 return DAG.getMergeValues(Vals, 2, DL);
1245 // Lower an MSA copy intrinsic into the specified SelectionDAG node
1246 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1248 SDValue Vec = Op->getOperand(1);
1249 SDValue Idx = Op->getOperand(2);
1250 EVT ResTy = Op->getValueType(0);
1251 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1253 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1254 DAG.getValueType(EltTy));
1259 static SDValue lowerMSASplatZExt(SDValue Op, unsigned OpNr, SelectionDAG &DAG) {
1260 EVT ResVecTy = Op->getValueType(0);
1261 EVT ViaVecTy = ResVecTy;
1264 // When ResVecTy == MVT::v2i64, LaneA is the upper 32 bits of the lane and
1265 // LaneB is the lower 32-bits. Otherwise LaneA and LaneB are alternating
1268 SDValue LaneB = Op->getOperand(2);
1270 if (ResVecTy == MVT::v2i64) {
1271 LaneA = DAG.getConstant(0, MVT::i32);
1272 ViaVecTy = MVT::v4i32;
1276 SDValue Ops[16] = { LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB,
1277 LaneA, LaneB, LaneA, LaneB, LaneA, LaneB, LaneA, LaneB };
1279 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1280 ViaVecTy.getVectorNumElements());
1282 if (ViaVecTy != ResVecTy)
1283 Result = DAG.getNode(ISD::BITCAST, DL, ResVecTy, Result);
1288 static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1289 return DAG.getConstant(Op->getConstantOperandVal(ImmOp), Op->getValueType(0));
1292 static SDValue getBuildVectorSplat(EVT VecTy, SDValue SplatValue,
1293 bool BigEndian, SelectionDAG &DAG) {
1294 EVT ViaVecTy = VecTy;
1295 SDValue SplatValueA = SplatValue;
1296 SDValue SplatValueB = SplatValue;
1297 SDLoc DL(SplatValue);
1299 if (VecTy == MVT::v2i64) {
1300 // v2i64 BUILD_VECTOR must be performed via v4i32 so split into i32's.
1301 ViaVecTy = MVT::v4i32;
1303 SplatValueA = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValue);
1304 SplatValueB = DAG.getNode(ISD::SRL, DL, MVT::i64, SplatValue,
1305 DAG.getConstant(32, MVT::i32));
1306 SplatValueB = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, SplatValueB);
1309 // We currently hold the parts in little endian order. Swap them if
1312 std::swap(SplatValueA, SplatValueB);
1314 SDValue Ops[16] = { SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1315 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1316 SplatValueA, SplatValueB, SplatValueA, SplatValueB,
1317 SplatValueA, SplatValueB, SplatValueA, SplatValueB };
1319 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, Ops,
1320 ViaVecTy.getVectorNumElements());
1322 if (VecTy != ViaVecTy)
1323 Result = DAG.getNode(ISD::BITCAST, DL, VecTy, Result);
1328 static SDValue lowerMSABinaryBitImmIntr(SDValue Op, SelectionDAG &DAG,
1329 unsigned Opc, SDValue Imm,
1331 EVT VecTy = Op->getValueType(0);
1335 // The DAG Combiner can't constant fold bitcasted vectors yet so we must do it
1337 if (VecTy == MVT::v2i64) {
1338 if (ConstantSDNode *CImm = dyn_cast<ConstantSDNode>(Imm)) {
1339 APInt BitImm = APInt(64, 1) << CImm->getAPIntValue();
1341 SDValue BitImmHiOp = DAG.getConstant(BitImm.lshr(32).trunc(32), MVT::i32);
1342 SDValue BitImmLoOp = DAG.getConstant(BitImm.trunc(32), MVT::i32);
1345 std::swap(BitImmLoOp, BitImmHiOp);
1348 DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
1349 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, BitImmLoOp,
1350 BitImmHiOp, BitImmLoOp, BitImmHiOp));
1354 if (Exp2Imm.getNode() == NULL) {
1355 // We couldnt constant fold, do a vector shift instead
1357 // Extend i32 to i64 if necessary. Sign or zero extend doesn't matter since
1358 // only values 0-63 are valid.
1359 if (VecTy == MVT::v2i64)
1360 Imm = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Imm);
1362 Exp2Imm = getBuildVectorSplat(VecTy, Imm, BigEndian, DAG);
1365 DAG.getNode(ISD::SHL, DL, VecTy, DAG.getConstant(1, VecTy), Exp2Imm);
1368 return DAG.getNode(Opc, DL, VecTy, Op->getOperand(1), Exp2Imm);
1371 static SDValue lowerMSABitClear(SDValue Op, SelectionDAG &DAG) {
1372 EVT ResTy = Op->getValueType(0);
1374 SDValue One = DAG.getConstant(1, ResTy);
1375 SDValue Bit = DAG.getNode(ISD::SHL, DL, ResTy, One, Op->getOperand(2));
1377 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1),
1378 DAG.getNOT(DL, Bit, ResTy));
1381 static SDValue lowerMSABitClearImm(SDValue Op, SelectionDAG &DAG) {
1383 EVT ResTy = Op->getValueType(0);
1384 APInt BitImm = APInt(ResTy.getVectorElementType().getSizeInBits(), 1)
1385 << cast<ConstantSDNode>(Op->getOperand(2))->getAPIntValue();
1386 SDValue BitMask = DAG.getConstant(~BitImm, ResTy);
1388 return DAG.getNode(ISD::AND, DL, ResTy, Op->getOperand(1), BitMask);
1391 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1392 SelectionDAG &DAG) const {
1395 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1398 case Intrinsic::mips_shilo:
1399 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1400 case Intrinsic::mips_dpau_h_qbl:
1401 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1402 case Intrinsic::mips_dpau_h_qbr:
1403 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1404 case Intrinsic::mips_dpsu_h_qbl:
1405 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1406 case Intrinsic::mips_dpsu_h_qbr:
1407 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1408 case Intrinsic::mips_dpa_w_ph:
1409 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1410 case Intrinsic::mips_dps_w_ph:
1411 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1412 case Intrinsic::mips_dpax_w_ph:
1413 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1414 case Intrinsic::mips_dpsx_w_ph:
1415 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1416 case Intrinsic::mips_mulsa_w_ph:
1417 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1418 case Intrinsic::mips_mult:
1419 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1420 case Intrinsic::mips_multu:
1421 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1422 case Intrinsic::mips_madd:
1423 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1424 case Intrinsic::mips_maddu:
1425 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1426 case Intrinsic::mips_msub:
1427 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1428 case Intrinsic::mips_msubu:
1429 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
1430 case Intrinsic::mips_addv_b:
1431 case Intrinsic::mips_addv_h:
1432 case Intrinsic::mips_addv_w:
1433 case Intrinsic::mips_addv_d:
1434 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1436 case Intrinsic::mips_addvi_b:
1437 case Intrinsic::mips_addvi_h:
1438 case Intrinsic::mips_addvi_w:
1439 case Intrinsic::mips_addvi_d:
1440 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1441 lowerMSASplatImm(Op, 2, DAG));
1442 case Intrinsic::mips_and_v:
1443 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1445 case Intrinsic::mips_andi_b:
1446 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1447 lowerMSASplatImm(Op, 2, DAG));
1448 case Intrinsic::mips_bclr_b:
1449 case Intrinsic::mips_bclr_h:
1450 case Intrinsic::mips_bclr_w:
1451 case Intrinsic::mips_bclr_d:
1452 return lowerMSABitClear(Op, DAG);
1453 case Intrinsic::mips_bclri_b:
1454 case Intrinsic::mips_bclri_h:
1455 case Intrinsic::mips_bclri_w:
1456 case Intrinsic::mips_bclri_d:
1457 return lowerMSABitClearImm(Op, DAG);
1458 case Intrinsic::mips_binsli_b:
1459 case Intrinsic::mips_binsli_h:
1460 case Intrinsic::mips_binsli_w:
1461 case Intrinsic::mips_binsli_d: {
1462 // binsli_x(IfClear, IfSet, nbits) -> (vselect LBitsMask, IfSet, IfClear)
1463 EVT VecTy = Op->getValueType(0);
1464 EVT EltTy = VecTy.getVectorElementType();
1465 APInt Mask = APInt::getHighBitsSet(EltTy.getSizeInBits(),
1466 Op->getConstantOperandVal(3));
1467 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1468 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1471 case Intrinsic::mips_binsri_b:
1472 case Intrinsic::mips_binsri_h:
1473 case Intrinsic::mips_binsri_w:
1474 case Intrinsic::mips_binsri_d: {
1475 // binsri_x(IfClear, IfSet, nbits) -> (vselect RBitsMask, IfSet, IfClear)
1476 EVT VecTy = Op->getValueType(0);
1477 EVT EltTy = VecTy.getVectorElementType();
1478 APInt Mask = APInt::getLowBitsSet(EltTy.getSizeInBits(),
1479 Op->getConstantOperandVal(3));
1480 return DAG.getNode(ISD::VSELECT, DL, VecTy,
1481 DAG.getConstant(Mask, VecTy, true), Op->getOperand(2),
1484 case Intrinsic::mips_bmnz_v:
1485 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1486 Op->getOperand(2), Op->getOperand(1));
1487 case Intrinsic::mips_bmnzi_b:
1488 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1489 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(2),
1491 case Intrinsic::mips_bmz_v:
1492 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0), Op->getOperand(3),
1493 Op->getOperand(1), Op->getOperand(2));
1494 case Intrinsic::mips_bmzi_b:
1495 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1496 lowerMSASplatImm(Op, 3, DAG), Op->getOperand(1),
1498 case Intrinsic::mips_bneg_b:
1499 case Intrinsic::mips_bneg_h:
1500 case Intrinsic::mips_bneg_w:
1501 case Intrinsic::mips_bneg_d: {
1502 EVT VecTy = Op->getValueType(0);
1503 SDValue One = DAG.getConstant(1, VecTy);
1505 return DAG.getNode(ISD::XOR, DL, VecTy, Op->getOperand(1),
1506 DAG.getNode(ISD::SHL, DL, VecTy, One,
1507 Op->getOperand(2)));
1509 case Intrinsic::mips_bnegi_b:
1510 case Intrinsic::mips_bnegi_h:
1511 case Intrinsic::mips_bnegi_w:
1512 case Intrinsic::mips_bnegi_d:
1513 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::XOR, Op->getOperand(2),
1514 !Subtarget->isLittle());
1515 case Intrinsic::mips_bnz_b:
1516 case Intrinsic::mips_bnz_h:
1517 case Intrinsic::mips_bnz_w:
1518 case Intrinsic::mips_bnz_d:
1519 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1521 case Intrinsic::mips_bnz_v:
1522 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1524 case Intrinsic::mips_bsel_v:
1525 // bsel_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
1526 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1527 Op->getOperand(1), Op->getOperand(3),
1529 case Intrinsic::mips_bseli_b:
1530 // bseli_v(Mask, IfClear, IfSet) -> (vselect Mask, IfSet, IfClear)
1531 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1532 Op->getOperand(1), lowerMSASplatImm(Op, 3, DAG),
1534 case Intrinsic::mips_bset_b:
1535 case Intrinsic::mips_bset_h:
1536 case Intrinsic::mips_bset_w:
1537 case Intrinsic::mips_bset_d: {
1538 EVT VecTy = Op->getValueType(0);
1539 SDValue One = DAG.getConstant(1, VecTy);
1541 return DAG.getNode(ISD::OR, DL, VecTy, Op->getOperand(1),
1542 DAG.getNode(ISD::SHL, DL, VecTy, One,
1543 Op->getOperand(2)));
1545 case Intrinsic::mips_bseti_b:
1546 case Intrinsic::mips_bseti_h:
1547 case Intrinsic::mips_bseti_w:
1548 case Intrinsic::mips_bseti_d:
1549 return lowerMSABinaryBitImmIntr(Op, DAG, ISD::OR, Op->getOperand(2),
1550 !Subtarget->isLittle());
1551 case Intrinsic::mips_bz_b:
1552 case Intrinsic::mips_bz_h:
1553 case Intrinsic::mips_bz_w:
1554 case Intrinsic::mips_bz_d:
1555 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1557 case Intrinsic::mips_bz_v:
1558 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1560 case Intrinsic::mips_ceq_b:
1561 case Intrinsic::mips_ceq_h:
1562 case Intrinsic::mips_ceq_w:
1563 case Intrinsic::mips_ceq_d:
1564 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1565 Op->getOperand(2), ISD::SETEQ);
1566 case Intrinsic::mips_ceqi_b:
1567 case Intrinsic::mips_ceqi_h:
1568 case Intrinsic::mips_ceqi_w:
1569 case Intrinsic::mips_ceqi_d:
1570 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1571 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1572 case Intrinsic::mips_cle_s_b:
1573 case Intrinsic::mips_cle_s_h:
1574 case Intrinsic::mips_cle_s_w:
1575 case Intrinsic::mips_cle_s_d:
1576 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1577 Op->getOperand(2), ISD::SETLE);
1578 case Intrinsic::mips_clei_s_b:
1579 case Intrinsic::mips_clei_s_h:
1580 case Intrinsic::mips_clei_s_w:
1581 case Intrinsic::mips_clei_s_d:
1582 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1583 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1584 case Intrinsic::mips_cle_u_b:
1585 case Intrinsic::mips_cle_u_h:
1586 case Intrinsic::mips_cle_u_w:
1587 case Intrinsic::mips_cle_u_d:
1588 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1589 Op->getOperand(2), ISD::SETULE);
1590 case Intrinsic::mips_clei_u_b:
1591 case Intrinsic::mips_clei_u_h:
1592 case Intrinsic::mips_clei_u_w:
1593 case Intrinsic::mips_clei_u_d:
1594 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1595 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1596 case Intrinsic::mips_clt_s_b:
1597 case Intrinsic::mips_clt_s_h:
1598 case Intrinsic::mips_clt_s_w:
1599 case Intrinsic::mips_clt_s_d:
1600 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1601 Op->getOperand(2), ISD::SETLT);
1602 case Intrinsic::mips_clti_s_b:
1603 case Intrinsic::mips_clti_s_h:
1604 case Intrinsic::mips_clti_s_w:
1605 case Intrinsic::mips_clti_s_d:
1606 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1607 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1608 case Intrinsic::mips_clt_u_b:
1609 case Intrinsic::mips_clt_u_h:
1610 case Intrinsic::mips_clt_u_w:
1611 case Intrinsic::mips_clt_u_d:
1612 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1613 Op->getOperand(2), ISD::SETULT);
1614 case Intrinsic::mips_clti_u_b:
1615 case Intrinsic::mips_clti_u_h:
1616 case Intrinsic::mips_clti_u_w:
1617 case Intrinsic::mips_clti_u_d:
1618 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1619 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
1620 case Intrinsic::mips_copy_s_b:
1621 case Intrinsic::mips_copy_s_h:
1622 case Intrinsic::mips_copy_s_w:
1623 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1624 case Intrinsic::mips_copy_s_d:
1626 // Lower directly into VEXTRACT_SEXT_ELT since i64 is legal on Mips64.
1627 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1629 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1630 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1631 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1632 Op->getValueType(0), Op->getOperand(1),
1635 case Intrinsic::mips_copy_u_b:
1636 case Intrinsic::mips_copy_u_h:
1637 case Intrinsic::mips_copy_u_w:
1638 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1639 case Intrinsic::mips_copy_u_d:
1641 // Lower directly into VEXTRACT_ZEXT_ELT since i64 is legal on Mips64.
1642 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1644 // Lower into the generic EXTRACT_VECTOR_ELT node and let the type
1645 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1646 // Note: When i64 is illegal, this results in copy_s.w instructions
1647 // instead of copy_u.w instructions. This makes no difference to the
1648 // behaviour since i64 is only illegal when the register file is 32-bit.
1649 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
1650 Op->getValueType(0), Op->getOperand(1),
1653 case Intrinsic::mips_div_s_b:
1654 case Intrinsic::mips_div_s_h:
1655 case Intrinsic::mips_div_s_w:
1656 case Intrinsic::mips_div_s_d:
1657 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1659 case Intrinsic::mips_div_u_b:
1660 case Intrinsic::mips_div_u_h:
1661 case Intrinsic::mips_div_u_w:
1662 case Intrinsic::mips_div_u_d:
1663 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1665 case Intrinsic::mips_fadd_w:
1666 case Intrinsic::mips_fadd_d:
1667 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1669 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1670 case Intrinsic::mips_fceq_w:
1671 case Intrinsic::mips_fceq_d:
1672 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1673 Op->getOperand(2), ISD::SETOEQ);
1674 case Intrinsic::mips_fcle_w:
1675 case Intrinsic::mips_fcle_d:
1676 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1677 Op->getOperand(2), ISD::SETOLE);
1678 case Intrinsic::mips_fclt_w:
1679 case Intrinsic::mips_fclt_d:
1680 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1681 Op->getOperand(2), ISD::SETOLT);
1682 case Intrinsic::mips_fcne_w:
1683 case Intrinsic::mips_fcne_d:
1684 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1685 Op->getOperand(2), ISD::SETONE);
1686 case Intrinsic::mips_fcor_w:
1687 case Intrinsic::mips_fcor_d:
1688 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1689 Op->getOperand(2), ISD::SETO);
1690 case Intrinsic::mips_fcueq_w:
1691 case Intrinsic::mips_fcueq_d:
1692 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1693 Op->getOperand(2), ISD::SETUEQ);
1694 case Intrinsic::mips_fcule_w:
1695 case Intrinsic::mips_fcule_d:
1696 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1697 Op->getOperand(2), ISD::SETULE);
1698 case Intrinsic::mips_fcult_w:
1699 case Intrinsic::mips_fcult_d:
1700 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1701 Op->getOperand(2), ISD::SETULT);
1702 case Intrinsic::mips_fcun_w:
1703 case Intrinsic::mips_fcun_d:
1704 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1705 Op->getOperand(2), ISD::SETUO);
1706 case Intrinsic::mips_fcune_w:
1707 case Intrinsic::mips_fcune_d:
1708 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1709 Op->getOperand(2), ISD::SETUNE);
1710 case Intrinsic::mips_fdiv_w:
1711 case Intrinsic::mips_fdiv_d:
1712 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1714 case Intrinsic::mips_ffint_u_w:
1715 case Intrinsic::mips_ffint_u_d:
1716 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1718 case Intrinsic::mips_ffint_s_w:
1719 case Intrinsic::mips_ffint_s_d:
1720 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1722 case Intrinsic::mips_fill_b:
1723 case Intrinsic::mips_fill_h:
1724 case Intrinsic::mips_fill_w:
1725 case Intrinsic::mips_fill_d: {
1726 SmallVector<SDValue, 16> Ops;
1727 EVT ResTy = Op->getValueType(0);
1729 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1730 Ops.push_back(Op->getOperand(1));
1732 // If ResTy is v2i64 then the type legalizer will break this node down into
1733 // an equivalent v4i32.
1734 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
1736 case Intrinsic::mips_fexp2_w:
1737 case Intrinsic::mips_fexp2_d: {
1738 EVT ResTy = Op->getValueType(0);
1740 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1741 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1743 case Intrinsic::mips_flog2_w:
1744 case Intrinsic::mips_flog2_d:
1745 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
1746 case Intrinsic::mips_fmadd_w:
1747 case Intrinsic::mips_fmadd_d:
1748 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1749 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
1750 case Intrinsic::mips_fmul_w:
1751 case Intrinsic::mips_fmul_d:
1752 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1754 case Intrinsic::mips_fmsub_w:
1755 case Intrinsic::mips_fmsub_d: {
1756 EVT ResTy = Op->getValueType(0);
1757 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1758 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1759 Op->getOperand(2), Op->getOperand(3)));
1761 case Intrinsic::mips_frint_w:
1762 case Intrinsic::mips_frint_d:
1763 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
1764 case Intrinsic::mips_fsqrt_w:
1765 case Intrinsic::mips_fsqrt_d:
1766 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
1767 case Intrinsic::mips_fsub_w:
1768 case Intrinsic::mips_fsub_d:
1769 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1771 case Intrinsic::mips_ftrunc_u_w:
1772 case Intrinsic::mips_ftrunc_u_d:
1773 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1775 case Intrinsic::mips_ftrunc_s_w:
1776 case Intrinsic::mips_ftrunc_s_d:
1777 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1779 case Intrinsic::mips_ilvev_b:
1780 case Intrinsic::mips_ilvev_h:
1781 case Intrinsic::mips_ilvev_w:
1782 case Intrinsic::mips_ilvev_d:
1783 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
1784 Op->getOperand(1), Op->getOperand(2));
1785 case Intrinsic::mips_ilvl_b:
1786 case Intrinsic::mips_ilvl_h:
1787 case Intrinsic::mips_ilvl_w:
1788 case Intrinsic::mips_ilvl_d:
1789 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
1790 Op->getOperand(1), Op->getOperand(2));
1791 case Intrinsic::mips_ilvod_b:
1792 case Intrinsic::mips_ilvod_h:
1793 case Intrinsic::mips_ilvod_w:
1794 case Intrinsic::mips_ilvod_d:
1795 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
1796 Op->getOperand(1), Op->getOperand(2));
1797 case Intrinsic::mips_ilvr_b:
1798 case Intrinsic::mips_ilvr_h:
1799 case Intrinsic::mips_ilvr_w:
1800 case Intrinsic::mips_ilvr_d:
1801 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
1802 Op->getOperand(1), Op->getOperand(2));
1803 case Intrinsic::mips_insert_b:
1804 case Intrinsic::mips_insert_h:
1805 case Intrinsic::mips_insert_w:
1806 case Intrinsic::mips_insert_d:
1807 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1808 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
1809 case Intrinsic::mips_ldi_b:
1810 case Intrinsic::mips_ldi_h:
1811 case Intrinsic::mips_ldi_w:
1812 case Intrinsic::mips_ldi_d:
1813 return lowerMSASplatImm(Op, 1, DAG);
1814 case Intrinsic::mips_lsa:
1815 case Intrinsic::mips_dlsa: {
1816 EVT ResTy = Op->getValueType(0);
1817 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1818 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1819 Op->getOperand(2), Op->getOperand(3)));
1821 case Intrinsic::mips_maddv_b:
1822 case Intrinsic::mips_maddv_h:
1823 case Intrinsic::mips_maddv_w:
1824 case Intrinsic::mips_maddv_d: {
1825 EVT ResTy = Op->getValueType(0);
1826 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1827 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1828 Op->getOperand(2), Op->getOperand(3)));
1830 case Intrinsic::mips_max_s_b:
1831 case Intrinsic::mips_max_s_h:
1832 case Intrinsic::mips_max_s_w:
1833 case Intrinsic::mips_max_s_d:
1834 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1835 Op->getOperand(1), Op->getOperand(2));
1836 case Intrinsic::mips_max_u_b:
1837 case Intrinsic::mips_max_u_h:
1838 case Intrinsic::mips_max_u_w:
1839 case Intrinsic::mips_max_u_d:
1840 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1841 Op->getOperand(1), Op->getOperand(2));
1842 case Intrinsic::mips_maxi_s_b:
1843 case Intrinsic::mips_maxi_s_h:
1844 case Intrinsic::mips_maxi_s_w:
1845 case Intrinsic::mips_maxi_s_d:
1846 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1847 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1848 case Intrinsic::mips_maxi_u_b:
1849 case Intrinsic::mips_maxi_u_h:
1850 case Intrinsic::mips_maxi_u_w:
1851 case Intrinsic::mips_maxi_u_d:
1852 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1853 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1854 case Intrinsic::mips_min_s_b:
1855 case Intrinsic::mips_min_s_h:
1856 case Intrinsic::mips_min_s_w:
1857 case Intrinsic::mips_min_s_d:
1858 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1859 Op->getOperand(1), Op->getOperand(2));
1860 case Intrinsic::mips_min_u_b:
1861 case Intrinsic::mips_min_u_h:
1862 case Intrinsic::mips_min_u_w:
1863 case Intrinsic::mips_min_u_d:
1864 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1865 Op->getOperand(1), Op->getOperand(2));
1866 case Intrinsic::mips_mini_s_b:
1867 case Intrinsic::mips_mini_s_h:
1868 case Intrinsic::mips_mini_s_w:
1869 case Intrinsic::mips_mini_s_d:
1870 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1871 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1872 case Intrinsic::mips_mini_u_b:
1873 case Intrinsic::mips_mini_u_h:
1874 case Intrinsic::mips_mini_u_w:
1875 case Intrinsic::mips_mini_u_d:
1876 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1877 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1878 case Intrinsic::mips_mod_s_b:
1879 case Intrinsic::mips_mod_s_h:
1880 case Intrinsic::mips_mod_s_w:
1881 case Intrinsic::mips_mod_s_d:
1882 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1884 case Intrinsic::mips_mod_u_b:
1885 case Intrinsic::mips_mod_u_h:
1886 case Intrinsic::mips_mod_u_w:
1887 case Intrinsic::mips_mod_u_d:
1888 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1890 case Intrinsic::mips_mulv_b:
1891 case Intrinsic::mips_mulv_h:
1892 case Intrinsic::mips_mulv_w:
1893 case Intrinsic::mips_mulv_d:
1894 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1896 case Intrinsic::mips_msubv_b:
1897 case Intrinsic::mips_msubv_h:
1898 case Intrinsic::mips_msubv_w:
1899 case Intrinsic::mips_msubv_d: {
1900 EVT ResTy = Op->getValueType(0);
1901 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1902 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1903 Op->getOperand(2), Op->getOperand(3)));
1905 case Intrinsic::mips_nlzc_b:
1906 case Intrinsic::mips_nlzc_h:
1907 case Intrinsic::mips_nlzc_w:
1908 case Intrinsic::mips_nlzc_d:
1909 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
1910 case Intrinsic::mips_nor_v: {
1911 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1912 Op->getOperand(1), Op->getOperand(2));
1913 return DAG.getNOT(DL, Res, Res->getValueType(0));
1915 case Intrinsic::mips_nori_b: {
1916 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1918 lowerMSASplatImm(Op, 2, DAG));
1919 return DAG.getNOT(DL, Res, Res->getValueType(0));
1921 case Intrinsic::mips_or_v:
1922 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1924 case Intrinsic::mips_ori_b:
1925 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1926 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1927 case Intrinsic::mips_pckev_b:
1928 case Intrinsic::mips_pckev_h:
1929 case Intrinsic::mips_pckev_w:
1930 case Intrinsic::mips_pckev_d:
1931 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
1932 Op->getOperand(1), Op->getOperand(2));
1933 case Intrinsic::mips_pckod_b:
1934 case Intrinsic::mips_pckod_h:
1935 case Intrinsic::mips_pckod_w:
1936 case Intrinsic::mips_pckod_d:
1937 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
1938 Op->getOperand(1), Op->getOperand(2));
1939 case Intrinsic::mips_pcnt_b:
1940 case Intrinsic::mips_pcnt_h:
1941 case Intrinsic::mips_pcnt_w:
1942 case Intrinsic::mips_pcnt_d:
1943 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
1944 case Intrinsic::mips_shf_b:
1945 case Intrinsic::mips_shf_h:
1946 case Intrinsic::mips_shf_w:
1947 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
1948 Op->getOperand(2), Op->getOperand(1));
1949 case Intrinsic::mips_sll_b:
1950 case Intrinsic::mips_sll_h:
1951 case Intrinsic::mips_sll_w:
1952 case Intrinsic::mips_sll_d:
1953 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1955 case Intrinsic::mips_slli_b:
1956 case Intrinsic::mips_slli_h:
1957 case Intrinsic::mips_slli_w:
1958 case Intrinsic::mips_slli_d:
1959 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1960 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1961 case Intrinsic::mips_splat_b:
1962 case Intrinsic::mips_splat_h:
1963 case Intrinsic::mips_splat_w:
1964 case Intrinsic::mips_splat_d:
1965 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
1966 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
1967 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
1968 // Instead we lower to MipsISD::VSHF and match from there.
1969 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1970 lowerMSASplatZExt(Op, 2, DAG), Op->getOperand(1),
1972 case Intrinsic::mips_splati_b:
1973 case Intrinsic::mips_splati_h:
1974 case Intrinsic::mips_splati_w:
1975 case Intrinsic::mips_splati_d:
1976 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1977 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1979 case Intrinsic::mips_sra_b:
1980 case Intrinsic::mips_sra_h:
1981 case Intrinsic::mips_sra_w:
1982 case Intrinsic::mips_sra_d:
1983 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1985 case Intrinsic::mips_srai_b:
1986 case Intrinsic::mips_srai_h:
1987 case Intrinsic::mips_srai_w:
1988 case Intrinsic::mips_srai_d:
1989 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1990 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1991 case Intrinsic::mips_srl_b:
1992 case Intrinsic::mips_srl_h:
1993 case Intrinsic::mips_srl_w:
1994 case Intrinsic::mips_srl_d:
1995 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1997 case Intrinsic::mips_srli_b:
1998 case Intrinsic::mips_srli_h:
1999 case Intrinsic::mips_srli_w:
2000 case Intrinsic::mips_srli_d:
2001 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
2002 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2003 case Intrinsic::mips_subv_b:
2004 case Intrinsic::mips_subv_h:
2005 case Intrinsic::mips_subv_w:
2006 case Intrinsic::mips_subv_d:
2007 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
2009 case Intrinsic::mips_subvi_b:
2010 case Intrinsic::mips_subvi_h:
2011 case Intrinsic::mips_subvi_w:
2012 case Intrinsic::mips_subvi_d:
2013 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
2014 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2015 case Intrinsic::mips_vshf_b:
2016 case Intrinsic::mips_vshf_h:
2017 case Intrinsic::mips_vshf_w:
2018 case Intrinsic::mips_vshf_d:
2019 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
2020 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
2021 case Intrinsic::mips_xor_v:
2022 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
2024 case Intrinsic::mips_xori_b:
2025 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
2026 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
2030 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2032 SDValue ChainIn = Op->getOperand(0);
2033 SDValue Address = Op->getOperand(2);
2034 SDValue Offset = Op->getOperand(3);
2035 EVT ResTy = Op->getValueType(0);
2036 EVT PtrTy = Address->getValueType(0);
2038 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2040 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
2044 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2045 SelectionDAG &DAG) const {
2046 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2050 case Intrinsic::mips_extp:
2051 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
2052 case Intrinsic::mips_extpdp:
2053 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
2054 case Intrinsic::mips_extr_w:
2055 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
2056 case Intrinsic::mips_extr_r_w:
2057 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
2058 case Intrinsic::mips_extr_rs_w:
2059 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
2060 case Intrinsic::mips_extr_s_h:
2061 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
2062 case Intrinsic::mips_mthlip:
2063 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
2064 case Intrinsic::mips_mulsaq_s_w_ph:
2065 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
2066 case Intrinsic::mips_maq_s_w_phl:
2067 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
2068 case Intrinsic::mips_maq_s_w_phr:
2069 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
2070 case Intrinsic::mips_maq_sa_w_phl:
2071 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
2072 case Intrinsic::mips_maq_sa_w_phr:
2073 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
2074 case Intrinsic::mips_dpaq_s_w_ph:
2075 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
2076 case Intrinsic::mips_dpsq_s_w_ph:
2077 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
2078 case Intrinsic::mips_dpaq_sa_l_w:
2079 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
2080 case Intrinsic::mips_dpsq_sa_l_w:
2081 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
2082 case Intrinsic::mips_dpaqx_s_w_ph:
2083 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
2084 case Intrinsic::mips_dpaqx_sa_w_ph:
2085 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
2086 case Intrinsic::mips_dpsqx_s_w_ph:
2087 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
2088 case Intrinsic::mips_dpsqx_sa_w_ph:
2089 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
2090 case Intrinsic::mips_ld_b:
2091 case Intrinsic::mips_ld_h:
2092 case Intrinsic::mips_ld_w:
2093 case Intrinsic::mips_ld_d:
2094 return lowerMSALoadIntr(Op, DAG, Intr);
2098 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
2100 SDValue ChainIn = Op->getOperand(0);
2101 SDValue Value = Op->getOperand(2);
2102 SDValue Address = Op->getOperand(3);
2103 SDValue Offset = Op->getOperand(4);
2104 EVT PtrTy = Address->getValueType(0);
2106 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
2108 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
2112 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
2113 SelectionDAG &DAG) const {
2114 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
2118 case Intrinsic::mips_st_b:
2119 case Intrinsic::mips_st_h:
2120 case Intrinsic::mips_st_w:
2121 case Intrinsic::mips_st_d:
2122 return lowerMSAStoreIntr(Op, DAG, Intr);
2126 /// \brief Check if the given BuildVectorSDNode is a splat.
2127 /// This method currently relies on DAG nodes being reused when equivalent,
2128 /// so it's possible for this to return false even when isConstantSplat returns
2130 static bool isSplatVector(const BuildVectorSDNode *N) {
2131 unsigned int nOps = N->getNumOperands();
2132 assert(nOps > 1 && "isSplatVector has 0 or 1 sized build vector");
2134 SDValue Operand0 = N->getOperand(0);
2136 for (unsigned int i = 1; i < nOps; ++i) {
2137 if (N->getOperand(i) != Operand0)
2144 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
2146 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
2147 // choose to sign-extend but we could have equally chosen zero-extend. The
2148 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
2149 // result into this node later (possibly changing it to a zero-extend in the
2151 SDValue MipsSETargetLowering::
2152 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
2154 EVT ResTy = Op->getValueType(0);
2155 SDValue Op0 = Op->getOperand(0);
2156 EVT VecTy = Op0->getValueType(0);
2158 if (!VecTy.is128BitVector())
2161 if (ResTy.isInteger()) {
2162 SDValue Op1 = Op->getOperand(1);
2163 EVT EltTy = VecTy.getVectorElementType();
2164 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
2165 DAG.getValueType(EltTy));
2171 static bool isConstantOrUndef(const SDValue Op) {
2172 if (Op->getOpcode() == ISD::UNDEF)
2174 if (dyn_cast<ConstantSDNode>(Op))
2176 if (dyn_cast<ConstantFPSDNode>(Op))
2181 static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
2182 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
2183 if (isConstantOrUndef(Op->getOperand(i)))
2188 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
2191 // Lowers according to the following rules:
2192 // - Constant splats are legal as-is as long as the SplatBitSize is a power of
2193 // 2 less than or equal to 64 and the value fits into a signed 10-bit
2195 // - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
2196 // is a power of 2 less than or equal to 64 and the value does not fit into a
2197 // signed 10-bit immediate
2198 // - Non-constant splats are legal as-is.
2199 // - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
2200 // - All others are illegal and must be expanded.
2201 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
2202 SelectionDAG &DAG) const {
2203 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
2204 EVT ResTy = Op->getValueType(0);
2206 APInt SplatValue, SplatUndef;
2207 unsigned SplatBitSize;
2210 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
2213 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
2215 !Subtarget->isLittle()) && SplatBitSize <= 64) {
2216 // We can only cope with 8, 16, 32, or 64-bit elements
2217 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
2221 // If the value fits into a simm10 then we can use ldi.[bhwd]
2222 // However, if it isn't an integer type we will have to bitcast from an
2223 // integer type first. Also, if there are any undefs, we must lower them
2224 // to defined values first.
2225 if (ResTy.isInteger() && !HasAnyUndefs && SplatValue.isSignedIntN(10))
2230 switch (SplatBitSize) {
2234 ViaVecTy = MVT::v16i8;
2237 ViaVecTy = MVT::v8i16;
2240 ViaVecTy = MVT::v4i32;
2243 // There's no fill.d to fall back on for 64-bit values
2247 // SelectionDAG::getConstant will promote SplatValue appropriately.
2248 SDValue Result = DAG.getConstant(SplatValue, ViaVecTy);
2250 // Bitcast to the type we originally wanted
2251 if (ViaVecTy != ResTy)
2252 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
2255 } else if (isSplatVector(Node))
2257 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
2258 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
2259 // The resulting code is the same length as the expansion, but it doesn't
2260 // use memory operations
2261 EVT ResTy = Node->getValueType(0);
2263 assert(ResTy.isVector());
2265 unsigned NumElts = ResTy.getVectorNumElements();
2266 SDValue Vector = DAG.getUNDEF(ResTy);
2267 for (unsigned i = 0; i < NumElts; ++i) {
2268 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
2269 Node->getOperand(i),
2270 DAG.getConstant(i, MVT::i32));
2278 // Lower VECTOR_SHUFFLE into SHF (if possible).
2280 // SHF splits the vector into blocks of four elements, then shuffles these
2281 // elements according to a <4 x i2> constant (encoded as an integer immediate).
2283 // It is therefore possible to lower into SHF when the mask takes the form:
2284 // <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2285 // When undef's appear they are treated as if they were whatever value is
2286 // necessary in order to fit the above form.
2289 // %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2290 // <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2291 // i32 7, i32 6, i32 5, i32 4>
2293 // (SHF_H $w0, $w1, 27)
2294 // where the 27 comes from:
2295 // 3 + (2 << 2) + (1 << 4) + (0 << 6)
2296 static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2297 SmallVector<int, 16> Indices,
2298 SelectionDAG &DAG) {
2299 int SHFIndices[4] = { -1, -1, -1, -1 };
2301 if (Indices.size() < 4)
2304 for (unsigned i = 0; i < 4; ++i) {
2305 for (unsigned j = i; j < Indices.size(); j += 4) {
2306 int Idx = Indices[j];
2308 // Convert from vector index to 4-element subvector index
2309 // If an index refers to an element outside of the subvector then give up
2312 if (Idx < 0 || Idx >= 4)
2316 // If the mask has an undef, replace it with the current index.
2317 // Note that it might still be undef if the current index is also undef
2318 if (SHFIndices[i] == -1)
2319 SHFIndices[i] = Idx;
2321 // Check that non-undef values are the same as in the mask. If they
2322 // aren't then give up
2323 if (!(Idx == -1 || Idx == SHFIndices[i]))
2328 // Calculate the immediate. Replace any remaining undefs with zero
2330 for (int i = 3; i >= 0; --i) {
2331 int Idx = SHFIndices[i];
2340 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2341 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2344 // Lower VECTOR_SHUFFLE into ILVEV (if possible).
2346 // ILVEV interleaves the even elements from each vector.
2348 // It is possible to lower into ILVEV when the mask takes the form:
2349 // <0, n, 2, n+2, 4, n+4, ...>
2350 // where n is the number of elements in the vector.
2352 // When undef's appear in the mask they are treated as if they were whatever
2353 // value is necessary in order to fit the above form.
2354 static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2355 SmallVector<int, 16> Indices,
2356 SelectionDAG &DAG) {
2357 assert ((Indices.size() % 2) == 0);
2359 int WtIdx = ResTy.getVectorNumElements();
2361 for (unsigned i = 0; i < Indices.size(); i += 2) {
2362 if (Indices[i] != -1 && Indices[i] != WsIdx)
2364 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2370 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2374 // Lower VECTOR_SHUFFLE into ILVOD (if possible).
2376 // ILVOD interleaves the odd elements from each vector.
2378 // It is possible to lower into ILVOD when the mask takes the form:
2379 // <1, n+1, 3, n+3, 5, n+5, ...>
2380 // where n is the number of elements in the vector.
2382 // When undef's appear in the mask they are treated as if they were whatever
2383 // value is necessary in order to fit the above form.
2384 static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2385 SmallVector<int, 16> Indices,
2386 SelectionDAG &DAG) {
2387 assert ((Indices.size() % 2) == 0);
2389 int WtIdx = ResTy.getVectorNumElements() + 1;
2391 for (unsigned i = 0; i < Indices.size(); i += 2) {
2392 if (Indices[i] != -1 && Indices[i] != WsIdx)
2394 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2400 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2404 // Lower VECTOR_SHUFFLE into ILVL (if possible).
2406 // ILVL interleaves consecutive elements from the left half of each vector.
2408 // It is possible to lower into ILVL when the mask takes the form:
2409 // <0, n, 1, n+1, 2, n+2, ...>
2410 // where n is the number of elements in the vector.
2412 // When undef's appear in the mask they are treated as if they were whatever
2413 // value is necessary in order to fit the above form.
2414 static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2415 SmallVector<int, 16> Indices,
2416 SelectionDAG &DAG) {
2417 assert ((Indices.size() % 2) == 0);
2419 int WtIdx = ResTy.getVectorNumElements();
2421 for (unsigned i = 0; i < Indices.size(); i += 2) {
2422 if (Indices[i] != -1 && Indices[i] != WsIdx)
2424 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2430 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2434 // Lower VECTOR_SHUFFLE into ILVR (if possible).
2436 // ILVR interleaves consecutive elements from the right half of each vector.
2438 // It is possible to lower into ILVR when the mask takes the form:
2439 // <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2440 // where n is the number of elements in the vector and x is half n.
2442 // When undef's appear in the mask they are treated as if they were whatever
2443 // value is necessary in order to fit the above form.
2444 static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2445 SmallVector<int, 16> Indices,
2446 SelectionDAG &DAG) {
2447 assert ((Indices.size() % 2) == 0);
2448 unsigned NumElts = ResTy.getVectorNumElements();
2449 int WsIdx = NumElts / 2;
2450 int WtIdx = NumElts + NumElts / 2;
2452 for (unsigned i = 0; i < Indices.size(); i += 2) {
2453 if (Indices[i] != -1 && Indices[i] != WsIdx)
2455 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2461 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2465 // Lower VECTOR_SHUFFLE into PCKEV (if possible).
2467 // PCKEV copies the even elements of each vector into the result vector.
2469 // It is possible to lower into PCKEV when the mask takes the form:
2470 // <0, 2, 4, ..., n, n+2, n+4, ...>
2471 // where n is the number of elements in the vector.
2473 // When undef's appear in the mask they are treated as if they were whatever
2474 // value is necessary in order to fit the above form.
2475 static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2476 SmallVector<int, 16> Indices,
2477 SelectionDAG &DAG) {
2478 assert ((Indices.size() % 2) == 0);
2481 for (unsigned i = 0; i < Indices.size(); ++i) {
2482 if (Indices[i] != -1 && Indices[i] != Idx)
2487 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2491 // Lower VECTOR_SHUFFLE into PCKOD (if possible).
2493 // PCKOD copies the odd elements of each vector into the result vector.
2495 // It is possible to lower into PCKOD when the mask takes the form:
2496 // <1, 3, 5, ..., n+1, n+3, n+5, ...>
2497 // where n is the number of elements in the vector.
2499 // When undef's appear in the mask they are treated as if they were whatever
2500 // value is necessary in order to fit the above form.
2501 static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2502 SmallVector<int, 16> Indices,
2503 SelectionDAG &DAG) {
2504 assert ((Indices.size() % 2) == 0);
2507 for (unsigned i = 0; i < Indices.size(); ++i) {
2508 if (Indices[i] != -1 && Indices[i] != Idx)
2513 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2517 // Lower VECTOR_SHUFFLE into VSHF.
2519 // This mostly consists of converting the shuffle indices in Indices into a
2520 // BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2521 // also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2522 // if the type is v8i16 and all the indices are less than 8 then the second
2523 // operand is unused and can be replaced with anything. We choose to replace it
2524 // with the used operand since this reduces the number of instructions overall.
2525 static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2526 SmallVector<int, 16> Indices,
2527 SelectionDAG &DAG) {
2528 SmallVector<SDValue, 16> Ops;
2531 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2532 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2533 bool Using1stVec = false;
2534 bool Using2ndVec = false;
2536 int ResTyNumElts = ResTy.getVectorNumElements();
2538 for (int i = 0; i < ResTyNumElts; ++i) {
2539 // Idx == -1 means UNDEF
2540 int Idx = Indices[i];
2542 if (0 <= Idx && Idx < ResTyNumElts)
2544 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2548 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2550 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2552 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2555 if (Using1stVec && Using2ndVec) {
2556 Op0 = Op->getOperand(0);
2557 Op1 = Op->getOperand(1);
2558 } else if (Using1stVec)
2559 Op0 = Op1 = Op->getOperand(0);
2560 else if (Using2ndVec)
2561 Op0 = Op1 = Op->getOperand(1);
2563 llvm_unreachable("shuffle vector mask references neither vector operand?");
2565 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2568 // Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2569 // indices in the shuffle.
2570 SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2571 SelectionDAG &DAG) const {
2572 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2573 EVT ResTy = Op->getValueType(0);
2575 if (!ResTy.is128BitVector())
2578 int ResTyNumElts = ResTy.getVectorNumElements();
2579 SmallVector<int, 16> Indices;
2581 for (int i = 0; i < ResTyNumElts; ++i)
2582 Indices.push_back(Node->getMaskElt(i));
2584 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2585 if (Result.getNode())
2587 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2588 if (Result.getNode())
2590 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2591 if (Result.getNode())
2593 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2594 if (Result.getNode())
2596 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2597 if (Result.getNode())
2599 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2600 if (Result.getNode())
2602 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2603 if (Result.getNode())
2605 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2608 MachineBasicBlock * MipsSETargetLowering::
2609 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2611 // bposge32_pseudo $vr0
2621 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2623 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2624 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2625 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2626 DebugLoc DL = MI->getDebugLoc();
2627 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2628 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
2629 MachineFunction *F = BB->getParent();
2630 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2631 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2632 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2635 F->insert(It, Sink);
2637 // Transfer the remainder of BB and its successor edges to Sink.
2638 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
2640 Sink->transferSuccessorsAndUpdatePHIs(BB);
2643 BB->addSuccessor(FBB);
2644 BB->addSuccessor(TBB);
2645 FBB->addSuccessor(Sink);
2646 TBB->addSuccessor(Sink);
2648 // Insert the real bposge32 instruction to $BB.
2649 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2652 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2653 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2654 .addReg(Mips::ZERO).addImm(0);
2655 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2658 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2659 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2660 .addReg(Mips::ZERO).addImm(1);
2662 // Insert phi function to $Sink.
2663 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2664 MI->getOperand(0).getReg())
2665 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2667 MI->eraseFromParent(); // The pseudo instruction is gone now.
2671 MachineBasicBlock * MipsSETargetLowering::
2672 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2673 unsigned BranchOp) const{
2675 // vany_nonzero $rd, $ws
2686 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2688 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2689 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2690 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2691 DebugLoc DL = MI->getDebugLoc();
2692 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2693 MachineFunction::iterator It = std::next(MachineFunction::iterator(BB));
2694 MachineFunction *F = BB->getParent();
2695 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2696 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2697 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2700 F->insert(It, Sink);
2702 // Transfer the remainder of BB and its successor edges to Sink.
2703 Sink->splice(Sink->begin(), BB, std::next(MachineBasicBlock::iterator(MI)),
2705 Sink->transferSuccessorsAndUpdatePHIs(BB);
2708 BB->addSuccessor(FBB);
2709 BB->addSuccessor(TBB);
2710 FBB->addSuccessor(Sink);
2711 TBB->addSuccessor(Sink);
2713 // Insert the real bnz.b instruction to $BB.
2714 BuildMI(BB, DL, TII->get(BranchOp))
2715 .addReg(MI->getOperand(1).getReg())
2719 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2720 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2721 .addReg(Mips::ZERO).addImm(0);
2722 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2725 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2726 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2727 .addReg(Mips::ZERO).addImm(1);
2729 // Insert phi function to $Sink.
2730 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2731 MI->getOperand(0).getReg())
2732 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2734 MI->eraseFromParent(); // The pseudo instruction is gone now.
2738 // Emit the COPY_FW pseudo instruction.
2740 // copy_fw_pseudo $fd, $ws, n
2742 // copy_u_w $rt, $ws, $n
2745 // When n is zero, the equivalent operation can be performed with (potentially)
2746 // zero instructions due to register overlaps. This optimization is never valid
2747 // for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2748 MachineBasicBlock * MipsSETargetLowering::
2749 emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2750 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2751 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2752 DebugLoc DL = MI->getDebugLoc();
2753 unsigned Fd = MI->getOperand(0).getReg();
2754 unsigned Ws = MI->getOperand(1).getReg();
2755 unsigned Lane = MI->getOperand(2).getImm();
2758 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2760 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2762 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(Lane);
2763 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2766 MI->eraseFromParent(); // The pseudo instruction is gone now.
2770 // Emit the COPY_FD pseudo instruction.
2772 // copy_fd_pseudo $fd, $ws, n
2774 // splati.d $wt, $ws, $n
2775 // copy $fd, $wt:sub_64
2777 // When n is zero, the equivalent operation can be performed with (potentially)
2778 // zero instructions due to register overlaps. This optimization is always
2779 // valid because FR=1 mode which is the only supported mode in MSA.
2780 MachineBasicBlock * MipsSETargetLowering::
2781 emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2782 assert(Subtarget->isFP64bit());
2784 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2785 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2786 unsigned Fd = MI->getOperand(0).getReg();
2787 unsigned Ws = MI->getOperand(1).getReg();
2788 unsigned Lane = MI->getOperand(2).getImm() * 2;
2789 DebugLoc DL = MI->getDebugLoc();
2792 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2794 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2796 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2797 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2800 MI->eraseFromParent(); // The pseudo instruction is gone now.
2804 // Emit the INSERT_FW pseudo instruction.
2806 // insert_fw_pseudo $wd, $wd_in, $n, $fs
2808 // subreg_to_reg $wt:sub_lo, $fs
2809 // insve_w $wd[$n], $wd_in, $wt[0]
2811 MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2812 MachineBasicBlock *BB) const {
2813 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2814 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2815 DebugLoc DL = MI->getDebugLoc();
2816 unsigned Wd = MI->getOperand(0).getReg();
2817 unsigned Wd_in = MI->getOperand(1).getReg();
2818 unsigned Lane = MI->getOperand(2).getImm();
2819 unsigned Fs = MI->getOperand(3).getReg();
2820 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2822 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2825 .addImm(Mips::sub_lo);
2826 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
2831 MI->eraseFromParent(); // The pseudo instruction is gone now.
2835 // Emit the INSERT_FD pseudo instruction.
2837 // insert_fd_pseudo $wd, $fs, n
2839 // subreg_to_reg $wt:sub_64, $fs
2840 // insve_d $wd[$n], $wd_in, $wt[0]
2842 MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2843 MachineBasicBlock *BB) const {
2844 assert(Subtarget->isFP64bit());
2846 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2847 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2848 DebugLoc DL = MI->getDebugLoc();
2849 unsigned Wd = MI->getOperand(0).getReg();
2850 unsigned Wd_in = MI->getOperand(1).getReg();
2851 unsigned Lane = MI->getOperand(2).getImm();
2852 unsigned Fs = MI->getOperand(3).getReg();
2853 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2855 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2858 .addImm(Mips::sub_64);
2859 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
2864 MI->eraseFromParent(); // The pseudo instruction is gone now.
2868 // Emit the FILL_FW pseudo instruction.
2870 // fill_fw_pseudo $wd, $fs
2872 // implicit_def $wt1
2873 // insert_subreg $wt2:subreg_lo, $wt1, $fs
2874 // splati.w $wd, $wt2[0]
2876 MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2877 MachineBasicBlock *BB) const {
2878 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2879 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2880 DebugLoc DL = MI->getDebugLoc();
2881 unsigned Wd = MI->getOperand(0).getReg();
2882 unsigned Fs = MI->getOperand(1).getReg();
2883 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2884 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2886 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2887 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2890 .addImm(Mips::sub_lo);
2891 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2893 MI->eraseFromParent(); // The pseudo instruction is gone now.
2897 // Emit the FILL_FD pseudo instruction.
2899 // fill_fd_pseudo $wd, $fs
2901 // implicit_def $wt1
2902 // insert_subreg $wt2:subreg_64, $wt1, $fs
2903 // splati.d $wd, $wt2[0]
2905 MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2906 MachineBasicBlock *BB) const {
2907 assert(Subtarget->isFP64bit());
2909 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2910 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2911 DebugLoc DL = MI->getDebugLoc();
2912 unsigned Wd = MI->getOperand(0).getReg();
2913 unsigned Fs = MI->getOperand(1).getReg();
2914 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2915 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2917 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2918 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2921 .addImm(Mips::sub_64);
2922 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
2924 MI->eraseFromParent(); // The pseudo instruction is gone now.
2928 // Emit the FEXP2_W_1 pseudo instructions.
2930 // fexp2_w_1_pseudo $wd, $wt
2933 // fexp2.w $wd, $ws, $wt
2935 MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2936 MachineBasicBlock *BB) const {
2937 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2938 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2939 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2940 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2941 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2942 DebugLoc DL = MI->getDebugLoc();
2944 // Splat 1.0 into a vector
2945 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2946 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2948 // Emit 1.0 * fexp2(Wt)
2949 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2951 .addReg(MI->getOperand(1).getReg());
2953 MI->eraseFromParent(); // The pseudo instruction is gone now.
2957 // Emit the FEXP2_D_1 pseudo instructions.
2959 // fexp2_d_1_pseudo $wd, $wt
2962 // fexp2.d $wd, $ws, $wt
2964 MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2965 MachineBasicBlock *BB) const {
2966 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2967 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2968 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2969 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2970 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2971 DebugLoc DL = MI->getDebugLoc();
2973 // Splat 1.0 into a vector
2974 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2975 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2977 // Emit 1.0 * fexp2(Wt)
2978 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2980 .addReg(MI->getOperand(1).getReg());
2982 MI->eraseFromParent(); // The pseudo instruction is gone now.