1 //===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Subclass of MipsTargetLowering specialized for mips32/64.
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "mips-isel"
14 #include "MipsSEISelLowering.h"
15 #include "MipsRegisterInfo.h"
16 #include "MipsTargetMachine.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Target/TargetInstrInfo.h"
27 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
28 cl::desc("MIPS: Enable tail calls."), cl::init(false));
30 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
31 cl::desc("Expand double precision loads and "
32 "stores to their single precision "
35 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
36 : MipsTargetLowering(TM) {
37 // Set up the register classes
38 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
41 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
43 if (Subtarget->hasDSP() || Subtarget->hasMSA()) {
44 // Expand all truncating stores and extending loads.
45 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
46 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
48 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
49 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
50 setTruncStoreAction((MVT::SimpleValueType)VT0,
51 (MVT::SimpleValueType)VT1, Expand);
53 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
54 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
55 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
59 if (Subtarget->hasDSP()) {
60 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
62 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
63 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
65 // Expand all builtin opcodes.
66 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
67 setOperationAction(Opc, VecTys[i], Expand);
69 setOperationAction(ISD::ADD, VecTys[i], Legal);
70 setOperationAction(ISD::SUB, VecTys[i], Legal);
71 setOperationAction(ISD::LOAD, VecTys[i], Legal);
72 setOperationAction(ISD::STORE, VecTys[i], Legal);
73 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
76 setTargetDAGCombine(ISD::SHL);
77 setTargetDAGCombine(ISD::SRA);
78 setTargetDAGCombine(ISD::SRL);
79 setTargetDAGCombine(ISD::SETCC);
80 setTargetDAGCombine(ISD::VSELECT);
83 if (Subtarget->hasDSPR2())
84 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
86 if (Subtarget->hasMSA()) {
87 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
88 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
89 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
90 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
91 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
92 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
93 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
95 setTargetDAGCombine(ISD::AND);
96 setTargetDAGCombine(ISD::OR);
97 setTargetDAGCombine(ISD::SRA);
98 setTargetDAGCombine(ISD::VSELECT);
99 setTargetDAGCombine(ISD::XOR);
102 if (!Subtarget->mipsSEUsesSoftFloat()) {
103 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
105 // When dealing with single precision only, use libcalls
106 if (!Subtarget->isSingleFloat()) {
107 if (Subtarget->isFP64bit())
108 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
110 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
114 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
115 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
116 setOperationAction(ISD::MULHS, MVT::i32, Custom);
117 setOperationAction(ISD::MULHU, MVT::i32, Custom);
120 setOperationAction(ISD::MULHS, MVT::i64, Custom);
121 setOperationAction(ISD::MULHU, MVT::i64, Custom);
122 setOperationAction(ISD::MUL, MVT::i64, Custom);
125 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
126 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
128 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
129 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
130 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
131 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
132 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
133 setOperationAction(ISD::LOAD, MVT::i32, Custom);
134 setOperationAction(ISD::STORE, MVT::i32, Custom);
136 setTargetDAGCombine(ISD::ADDE);
137 setTargetDAGCombine(ISD::SUBE);
138 setTargetDAGCombine(ISD::MUL);
140 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
141 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
142 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
145 setOperationAction(ISD::LOAD, MVT::f64, Custom);
146 setOperationAction(ISD::STORE, MVT::f64, Custom);
149 computeRegisterProperties();
152 const MipsTargetLowering *
153 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
154 return new MipsSETargetLowering(TM);
157 // Enable MSA support for the given integer type and Register class.
158 void MipsSETargetLowering::
159 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
160 addRegisterClass(Ty, RC);
162 // Expand all builtin opcodes.
163 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
164 setOperationAction(Opc, Ty, Expand);
166 setOperationAction(ISD::BITCAST, Ty, Legal);
167 setOperationAction(ISD::LOAD, Ty, Legal);
168 setOperationAction(ISD::STORE, Ty, Legal);
169 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
170 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
171 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
173 setOperationAction(ISD::ADD, Ty, Legal);
174 setOperationAction(ISD::AND, Ty, Legal);
175 setOperationAction(ISD::CTLZ, Ty, Legal);
176 setOperationAction(ISD::CTPOP, Ty, Legal);
177 setOperationAction(ISD::MUL, Ty, Legal);
178 setOperationAction(ISD::OR, Ty, Legal);
179 setOperationAction(ISD::SDIV, Ty, Legal);
180 setOperationAction(ISD::SREM, Ty, Legal);
181 setOperationAction(ISD::SHL, Ty, Legal);
182 setOperationAction(ISD::SRA, Ty, Legal);
183 setOperationAction(ISD::SRL, Ty, Legal);
184 setOperationAction(ISD::SUB, Ty, Legal);
185 setOperationAction(ISD::UDIV, Ty, Legal);
186 setOperationAction(ISD::UREM, Ty, Legal);
187 setOperationAction(ISD::VECTOR_SHUFFLE, Ty, Custom);
188 setOperationAction(ISD::VSELECT, Ty, Legal);
189 setOperationAction(ISD::XOR, Ty, Legal);
191 if (Ty == MVT::v4i32 || Ty == MVT::v2i64) {
192 setOperationAction(ISD::FP_TO_SINT, Ty, Legal);
193 setOperationAction(ISD::FP_TO_UINT, Ty, Legal);
194 setOperationAction(ISD::SINT_TO_FP, Ty, Legal);
195 setOperationAction(ISD::UINT_TO_FP, Ty, Legal);
198 setOperationAction(ISD::SETCC, Ty, Legal);
199 setCondCodeAction(ISD::SETNE, Ty, Expand);
200 setCondCodeAction(ISD::SETGE, Ty, Expand);
201 setCondCodeAction(ISD::SETGT, Ty, Expand);
202 setCondCodeAction(ISD::SETUGE, Ty, Expand);
203 setCondCodeAction(ISD::SETUGT, Ty, Expand);
206 // Enable MSA support for the given floating-point type and Register class.
207 void MipsSETargetLowering::
208 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
209 addRegisterClass(Ty, RC);
211 // Expand all builtin opcodes.
212 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
213 setOperationAction(Opc, Ty, Expand);
215 setOperationAction(ISD::LOAD, Ty, Legal);
216 setOperationAction(ISD::STORE, Ty, Legal);
217 setOperationAction(ISD::BITCAST, Ty, Legal);
218 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
219 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
220 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
222 if (Ty != MVT::v8f16) {
223 setOperationAction(ISD::FABS, Ty, Legal);
224 setOperationAction(ISD::FADD, Ty, Legal);
225 setOperationAction(ISD::FDIV, Ty, Legal);
226 setOperationAction(ISD::FEXP2, Ty, Legal);
227 setOperationAction(ISD::FLOG2, Ty, Legal);
228 setOperationAction(ISD::FMA, Ty, Legal);
229 setOperationAction(ISD::FMUL, Ty, Legal);
230 setOperationAction(ISD::FRINT, Ty, Legal);
231 setOperationAction(ISD::FSQRT, Ty, Legal);
232 setOperationAction(ISD::FSUB, Ty, Legal);
233 setOperationAction(ISD::VSELECT, Ty, Legal);
235 setOperationAction(ISD::SETCC, Ty, Legal);
236 setCondCodeAction(ISD::SETOGE, Ty, Expand);
237 setCondCodeAction(ISD::SETOGT, Ty, Expand);
238 setCondCodeAction(ISD::SETUGE, Ty, Expand);
239 setCondCodeAction(ISD::SETUGT, Ty, Expand);
240 setCondCodeAction(ISD::SETGE, Ty, Expand);
241 setCondCodeAction(ISD::SETGT, Ty, Expand);
246 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
247 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
260 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
261 SelectionDAG &DAG) const {
262 switch(Op.getOpcode()) {
263 case ISD::LOAD: return lowerLOAD(Op, DAG);
264 case ISD::STORE: return lowerSTORE(Op, DAG);
265 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
266 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
267 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
268 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
269 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
270 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
271 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
273 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
274 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
275 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
276 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
277 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
278 case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, DAG);
281 return MipsTargetLowering::LowerOperation(Op, DAG);
285 // Transforms a subgraph in CurDAG if the following pattern is found:
286 // (addc multLo, Lo0), (adde multHi, Hi0),
288 // multHi/Lo: product of multiplication
289 // Lo0: initial value of Lo register
290 // Hi0: initial value of Hi register
291 // Return true if pattern matching was successful.
292 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
293 // ADDENode's second operand must be a flag output of an ADDC node in order
294 // for the matching to be successful.
295 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
297 if (ADDCNode->getOpcode() != ISD::ADDC)
300 SDValue MultHi = ADDENode->getOperand(0);
301 SDValue MultLo = ADDCNode->getOperand(0);
302 SDNode *MultNode = MultHi.getNode();
303 unsigned MultOpc = MultHi.getOpcode();
305 // MultHi and MultLo must be generated by the same node,
306 if (MultLo.getNode() != MultNode)
309 // and it must be a multiplication.
310 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
313 // MultLo amd MultHi must be the first and second output of MultNode
315 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
318 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
319 // of the values of MultNode, in which case MultNode will be removed in later
321 // If there exist users other than ADDENode or ADDCNode, this function returns
322 // here, which will result in MultNode being mapped to a single MULT
323 // instruction node rather than a pair of MULT and MADD instructions being
325 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
330 // Initialize accumulator.
331 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
332 ADDCNode->getOperand(1),
333 ADDENode->getOperand(1));
335 // create MipsMAdd(u) node
336 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
338 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
339 MultNode->getOperand(0),// Factor 0
340 MultNode->getOperand(1),// Factor 1
343 // replace uses of adde and addc here
344 if (!SDValue(ADDCNode, 0).use_empty()) {
345 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
346 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
348 if (!SDValue(ADDENode, 0).use_empty()) {
349 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
350 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
357 // Transforms a subgraph in CurDAG if the following pattern is found:
358 // (addc Lo0, multLo), (sube Hi0, multHi),
360 // multHi/Lo: product of multiplication
361 // Lo0: initial value of Lo register
362 // Hi0: initial value of Hi register
363 // Return true if pattern matching was successful.
364 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
365 // SUBENode's second operand must be a flag output of an SUBC node in order
366 // for the matching to be successful.
367 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
369 if (SUBCNode->getOpcode() != ISD::SUBC)
372 SDValue MultHi = SUBENode->getOperand(1);
373 SDValue MultLo = SUBCNode->getOperand(1);
374 SDNode *MultNode = MultHi.getNode();
375 unsigned MultOpc = MultHi.getOpcode();
377 // MultHi and MultLo must be generated by the same node,
378 if (MultLo.getNode() != MultNode)
381 // and it must be a multiplication.
382 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
385 // MultLo amd MultHi must be the first and second output of MultNode
387 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
390 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
391 // of the values of MultNode, in which case MultNode will be removed in later
393 // If there exist users other than SUBENode or SUBCNode, this function returns
394 // here, which will result in MultNode being mapped to a single MULT
395 // instruction node rather than a pair of MULT and MSUB instructions being
397 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
402 // Initialize accumulator.
403 SDValue ACCIn = CurDAG->getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
404 SUBCNode->getOperand(0),
405 SUBENode->getOperand(0));
407 // create MipsSub(u) node
408 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
410 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
411 MultNode->getOperand(0),// Factor 0
412 MultNode->getOperand(1),// Factor 1
415 // replace uses of sube and subc here
416 if (!SDValue(SUBCNode, 0).use_empty()) {
417 SDValue LoOut = CurDAG->getNode(MipsISD::MFLO, DL, MVT::i32, MSub);
418 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
420 if (!SDValue(SUBENode, 0).use_empty()) {
421 SDValue HiOut = CurDAG->getNode(MipsISD::MFHI, DL, MVT::i32, MSub);
422 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
428 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
429 TargetLowering::DAGCombinerInfo &DCI,
430 const MipsSubtarget *Subtarget) {
431 if (DCI.isBeforeLegalize())
434 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
436 return SDValue(N, 0);
441 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
443 // Performs the following transformations:
444 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
445 // sign/zero-extension is completely overwritten by the new one performed by
447 // - Removes redundant zero extensions performed by an ISD::AND.
448 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
449 TargetLowering::DAGCombinerInfo &DCI,
450 const MipsSubtarget *Subtarget) {
451 if (!Subtarget->hasMSA())
454 SDValue Op0 = N->getOperand(0);
455 SDValue Op1 = N->getOperand(1);
456 unsigned Op0Opcode = Op0->getOpcode();
458 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
459 // where $d + 1 == 2^n and n == 32
460 // or $d + 1 == 2^n and n <= 32 and ZExt
461 // -> (MipsVExtractZExt $a, $b, $c)
462 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
463 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
464 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
469 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
471 if (Log2IfPositive <= 0)
472 return SDValue(); // Mask+1 is not a power of 2
474 SDValue Op0Op2 = Op0->getOperand(2);
475 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
476 unsigned ExtendTySize = ExtendTy.getSizeInBits();
477 unsigned Log2 = Log2IfPositive;
479 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
480 Log2 == ExtendTySize) {
481 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
482 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
483 Op0->getVTList(), Ops, Op0->getNumOperands());
491 // Determine if the specified node is a constant vector splat.
493 // Returns true and sets Imm if:
494 // * N is a ISD::BUILD_VECTOR representing a constant splat
496 // This function is quite similar to MipsSEDAGToDAGISel::selectVSplat. The
497 // differences are that it assumes the MSA has already been checked and the
498 // arbitrary requirement for a maximum of 32-bit integers isn't applied (and
499 // must not be in order for binsri.d to be selectable).
500 static bool isVSplat(SDValue N, APInt &Imm, bool IsLittleEndian) {
501 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N.getNode());
506 APInt SplatValue, SplatUndef;
507 unsigned SplatBitSize;
510 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
519 // Perform combines where ISD::OR is the root node.
521 // Performs the following transformations:
522 // - (or (and $a, $mask), (and $b, $inv_mask)) => (vselect $mask, $a, $b)
523 // where $inv_mask is the bitwise inverse of $mask and the 'or' has a 128-bit
525 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
526 TargetLowering::DAGCombinerInfo &DCI,
527 const MipsSubtarget *Subtarget) {
528 if (!Subtarget->hasMSA())
531 EVT Ty = N->getValueType(0);
533 if (!Ty.is128BitVector())
536 SDValue Op0 = N->getOperand(0);
537 SDValue Op1 = N->getOperand(1);
539 if (Op0->getOpcode() == ISD::AND && Op1->getOpcode() == ISD::AND) {
540 SDValue Op0Op0 = Op0->getOperand(0);
541 SDValue Op0Op1 = Op0->getOperand(1);
542 SDValue Op1Op0 = Op1->getOperand(0);
543 SDValue Op1Op1 = Op1->getOperand(1);
544 bool IsLittleEndian = !Subtarget->isLittle();
546 SDValue IfSet, IfClr, Cond;
549 // If Op0Op0 is an appropriate mask, try to find it's inverse in either
550 // Op1Op0, or Op1Op1. Keep track of the Cond, IfSet, and IfClr nodes, while
552 // IfClr will be set if we find a valid match.
553 if (isVSplat(Op0Op0, Mask, IsLittleEndian)) {
557 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) && Mask == ~InvMask)
559 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) && Mask == ~InvMask)
563 // If IfClr is not yet set, and Op0Op1 is an appropriate mask, try the same
564 // thing again using this mask.
565 // IfClr will be set if we find a valid match.
566 if (!IfClr.getNode() && isVSplat(Op0Op1, Mask, IsLittleEndian)) {
570 if (isVSplat(Op1Op0, InvMask, IsLittleEndian) && Mask == ~InvMask)
572 else if (isVSplat(Op1Op1, InvMask, IsLittleEndian) && Mask == ~InvMask)
576 // At this point, IfClr will be set if we have a valid match.
577 if (!IfClr.getNode())
580 assert(Cond.getNode() && IfSet.getNode());
582 // Fold degenerate cases.
583 if (Mask.isAllOnesValue())
588 // Transform the DAG into an equivalent VSELECT.
589 return DAG.getNode(ISD::VSELECT, SDLoc(N), Ty, Cond, IfClr, IfSet);
595 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
596 TargetLowering::DAGCombinerInfo &DCI,
597 const MipsSubtarget *Subtarget) {
598 if (DCI.isBeforeLegalize())
601 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
603 return SDValue(N, 0);
608 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
609 EVT ShiftTy, SelectionDAG &DAG) {
610 // Clear the upper (64 - VT.sizeInBits) bits.
611 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
615 return DAG.getConstant(0, VT);
621 // If c is power of 2, return (shl x, log2(c)).
622 if (isPowerOf2_64(C))
623 return DAG.getNode(ISD::SHL, DL, VT, X,
624 DAG.getConstant(Log2_64(C), ShiftTy));
626 unsigned Log2Ceil = Log2_64_Ceil(C);
627 uint64_t Floor = 1LL << Log2_64(C);
628 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
630 // If |c - floor_c| <= |c - ceil_c|,
631 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
632 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
633 if (C - Floor <= Ceil - C) {
634 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
635 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
636 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
639 // If |c - floor_c| > |c - ceil_c|,
640 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
641 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
642 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
643 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
646 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
647 const TargetLowering::DAGCombinerInfo &DCI,
648 const MipsSETargetLowering *TL) {
649 EVT VT = N->getValueType(0);
651 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
653 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
654 VT, TL->getScalarShiftAmountTy(VT), DAG);
656 return SDValue(N, 0);
659 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
661 const MipsSubtarget *Subtarget) {
662 // See if this is a vector splat immediate node.
663 APInt SplatValue, SplatUndef;
664 unsigned SplatBitSize;
666 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
667 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
670 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
671 EltSize, !Subtarget->isLittle()) ||
672 (SplatBitSize != EltSize) ||
673 (SplatValue.getZExtValue() >= EltSize))
676 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
677 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
680 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
681 TargetLowering::DAGCombinerInfo &DCI,
682 const MipsSubtarget *Subtarget) {
683 EVT Ty = N->getValueType(0);
685 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
688 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
691 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
692 // constant splats into MipsISD::SHRA_DSP for DSPr2.
694 // Performs the following transformations:
695 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
696 // sign/zero-extension is completely overwritten by the new one performed by
697 // the ISD::SRA and ISD::SHL nodes.
698 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
701 // See performDSPShiftCombine for more information about the transformation
703 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
704 TargetLowering::DAGCombinerInfo &DCI,
705 const MipsSubtarget *Subtarget) {
706 EVT Ty = N->getValueType(0);
708 if (Subtarget->hasMSA()) {
709 SDValue Op0 = N->getOperand(0);
710 SDValue Op1 = N->getOperand(1);
712 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
713 // where $d + sizeof($c) == 32
714 // or $d + sizeof($c) <= 32 and SExt
715 // -> (MipsVExtractSExt $a, $b, $c)
716 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
717 SDValue Op0Op0 = Op0->getOperand(0);
718 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
723 if (Op0Op0->getOpcode() != MipsISD::VEXTRACT_SEXT_ELT &&
724 Op0Op0->getOpcode() != MipsISD::VEXTRACT_ZEXT_ELT)
727 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
728 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
730 if (TotalBits == 32 ||
731 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
733 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
734 Op0Op0->getOperand(2) };
735 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
736 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
742 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
745 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
749 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
750 TargetLowering::DAGCombinerInfo &DCI,
751 const MipsSubtarget *Subtarget) {
752 EVT Ty = N->getValueType(0);
754 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
757 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
760 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
761 bool IsV216 = (Ty == MVT::v2i16);
765 case ISD::SETNE: return true;
769 case ISD::SETGE: return IsV216;
773 case ISD::SETUGE: return !IsV216;
774 default: return false;
778 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
779 EVT Ty = N->getValueType(0);
781 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
784 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
787 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
788 N->getOperand(1), N->getOperand(2));
791 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
792 EVT Ty = N->getValueType(0);
794 if (Ty.is128BitVector() && Ty.isInteger()) {
795 // Try the following combines:
796 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
797 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
798 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
799 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
800 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
801 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
802 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
803 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
804 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
805 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
807 SDValue Op0 = N->getOperand(0);
809 if (Op0->getOpcode() != ISD::SETCC)
812 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
815 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
817 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
822 SDValue Op1 = N->getOperand(1);
823 SDValue Op2 = N->getOperand(2);
824 SDValue Op0Op0 = Op0->getOperand(0);
825 SDValue Op0Op1 = Op0->getOperand(1);
827 if (Op1 == Op0Op0 && Op2 == Op0Op1)
828 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
830 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
831 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
833 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
834 SDValue SetCC = N->getOperand(0);
836 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
839 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
840 SetCC.getOperand(0), SetCC.getOperand(1),
841 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
847 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
848 const MipsSubtarget *Subtarget) {
849 EVT Ty = N->getValueType(0);
851 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
852 // Try the following combines:
853 // (xor (or $a, $b), (build_vector allones))
854 // (xor (or $a, $b), (bitcast (build_vector allones)))
855 SDValue Op0 = N->getOperand(0);
856 SDValue Op1 = N->getOperand(1);
859 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
861 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
866 if (NotOp->getOpcode() == ISD::OR)
867 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
868 NotOp->getOperand(1));
875 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
876 SelectionDAG &DAG = DCI.DAG;
879 switch (N->getOpcode()) {
881 return performADDECombine(N, DAG, DCI, Subtarget);
883 Val = performANDCombine(N, DAG, DCI, Subtarget);
886 Val = performORCombine(N, DAG, DCI, Subtarget);
889 return performSUBECombine(N, DAG, DCI, Subtarget);
891 return performMULCombine(N, DAG, DCI, this);
893 return performSHLCombine(N, DAG, DCI, Subtarget);
895 return performSRACombine(N, DAG, DCI, Subtarget);
897 return performSRLCombine(N, DAG, DCI, Subtarget);
899 return performVSELECTCombine(N, DAG);
901 Val = performXORCombine(N, DAG, Subtarget);
904 Val = performSETCCCombine(N, DAG);
909 DEBUG(dbgs() << "\nMipsSE DAG Combine:\n";
910 N->printrWithDepth(dbgs(), &DAG);
912 Val.getNode()->printrWithDepth(dbgs(), &DAG);
917 return MipsTargetLowering::PerformDAGCombine(N, DCI);
921 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
922 MachineBasicBlock *BB) const {
923 switch (MI->getOpcode()) {
925 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
926 case Mips::BPOSGE32_PSEUDO:
927 return emitBPOSGE32(MI, BB);
928 case Mips::SNZ_B_PSEUDO:
929 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
930 case Mips::SNZ_H_PSEUDO:
931 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
932 case Mips::SNZ_W_PSEUDO:
933 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
934 case Mips::SNZ_D_PSEUDO:
935 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
936 case Mips::SNZ_V_PSEUDO:
937 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
938 case Mips::SZ_B_PSEUDO:
939 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
940 case Mips::SZ_H_PSEUDO:
941 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
942 case Mips::SZ_W_PSEUDO:
943 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
944 case Mips::SZ_D_PSEUDO:
945 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
946 case Mips::SZ_V_PSEUDO:
947 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
948 case Mips::COPY_FW_PSEUDO:
949 return emitCOPY_FW(MI, BB);
950 case Mips::COPY_FD_PSEUDO:
951 return emitCOPY_FD(MI, BB);
952 case Mips::INSERT_FW_PSEUDO:
953 return emitINSERT_FW(MI, BB);
954 case Mips::INSERT_FD_PSEUDO:
955 return emitINSERT_FD(MI, BB);
956 case Mips::FILL_FW_PSEUDO:
957 return emitFILL_FW(MI, BB);
958 case Mips::FILL_FD_PSEUDO:
959 return emitFILL_FD(MI, BB);
960 case Mips::FEXP2_W_1_PSEUDO:
961 return emitFEXP2_W_1(MI, BB);
962 case Mips::FEXP2_D_1_PSEUDO:
963 return emitFEXP2_D_1(MI, BB);
967 bool MipsSETargetLowering::
968 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
969 unsigned NextStackOffset,
970 const MipsFunctionInfo& FI) const {
971 if (!EnableMipsTailCalls)
974 // Return false if either the callee or caller has a byval argument.
975 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
978 // Return true if the callee's argument area is no larger than the
980 return NextStackOffset <= FI.getIncomingArgSize();
983 void MipsSETargetLowering::
984 getOpndList(SmallVectorImpl<SDValue> &Ops,
985 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
986 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
987 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
988 // T9 should contain the address of the callee function if
989 // -reloction-model=pic or it is an indirect call.
990 if (IsPICCall || !GlobalOrExternal) {
991 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
992 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
994 Ops.push_back(Callee);
996 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
997 InternalLinkage, CLI, Callee, Chain);
1000 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1001 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
1003 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1004 return MipsTargetLowering::lowerLOAD(Op, DAG);
1006 // Replace a double precision load with two i32 loads and a buildpair64.
1008 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1009 EVT PtrVT = Ptr.getValueType();
1011 // i32 load from lower address.
1012 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
1013 MachinePointerInfo(), Nd.isVolatile(),
1014 Nd.isNonTemporal(), Nd.isInvariant(),
1017 // i32 load from higher address.
1018 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1019 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
1020 MachinePointerInfo(), Nd.isVolatile(),
1021 Nd.isNonTemporal(), Nd.isInvariant(),
1022 std::min(Nd.getAlignment(), 4U));
1024 if (!Subtarget->isLittle())
1027 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1028 SDValue Ops[2] = {BP, Hi.getValue(1)};
1029 return DAG.getMergeValues(Ops, 2, DL);
1032 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1033 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
1035 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
1036 return MipsTargetLowering::lowerSTORE(Op, DAG);
1038 // Replace a double precision store with two extractelement64s and i32 stores.
1040 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
1041 EVT PtrVT = Ptr.getValueType();
1042 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1043 Val, DAG.getConstant(0, MVT::i32));
1044 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1045 Val, DAG.getConstant(1, MVT::i32));
1047 if (!Subtarget->isLittle())
1050 // i32 store to lower address.
1051 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
1052 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
1055 // i32 store to higher address.
1056 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
1057 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
1058 Nd.isVolatile(), Nd.isNonTemporal(),
1059 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
1062 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
1063 bool HasLo, bool HasHi,
1064 SelectionDAG &DAG) const {
1065 EVT Ty = Op.getOperand(0).getValueType();
1067 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
1068 Op.getOperand(0), Op.getOperand(1));
1072 Lo = DAG.getNode(MipsISD::MFLO, DL, Ty, Mult);
1074 Hi = DAG.getNode(MipsISD::MFHI, DL, Ty, Mult);
1076 if (!HasLo || !HasHi)
1077 return HasLo ? Lo : Hi;
1079 SDValue Vals[] = { Lo, Hi };
1080 return DAG.getMergeValues(Vals, 2, DL);
1084 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
1085 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1086 DAG.getConstant(0, MVT::i32));
1087 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
1088 DAG.getConstant(1, MVT::i32));
1089 return DAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped, InLo, InHi);
1092 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
1093 SDValue Lo = DAG.getNode(MipsISD::MFLO, DL, MVT::i32, Op);
1094 SDValue Hi = DAG.getNode(MipsISD::MFHI, DL, MVT::i32, Op);
1095 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1098 // This function expands mips intrinsic nodes which have 64-bit input operands
1099 // or output values.
1101 // out64 = intrinsic-node in64
1103 // lo = copy (extract-element (in64, 0))
1104 // hi = copy (extract-element (in64, 1))
1105 // mips-specific-node
1108 // out64 = merge-values (v0, v1)
1110 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1112 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
1113 SmallVector<SDValue, 3> Ops;
1116 // See if Op has a chain input.
1118 Ops.push_back(Op->getOperand(OpNo++));
1120 // The next operand is the intrinsic opcode.
1121 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
1123 // See if the next operand has type i64.
1124 SDValue Opnd = Op->getOperand(++OpNo), In64;
1126 if (Opnd.getValueType() == MVT::i64)
1127 In64 = initAccumulator(Opnd, DL, DAG);
1129 Ops.push_back(Opnd);
1131 // Push the remaining operands.
1132 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1133 Ops.push_back(Op->getOperand(OpNo));
1135 // Add In64 to the end of the list.
1137 Ops.push_back(In64);
1140 SmallVector<EVT, 2> ResTys;
1142 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1144 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1147 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1148 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1153 assert(Val->getValueType(1) == MVT::Other);
1154 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1155 return DAG.getMergeValues(Vals, 2, DL);
1158 // Lower an MSA copy intrinsic into the specified SelectionDAG node
1159 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1161 SDValue Vec = Op->getOperand(1);
1162 SDValue Idx = Op->getOperand(2);
1163 EVT ResTy = Op->getValueType(0);
1164 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1166 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1167 DAG.getValueType(EltTy));
1173 lowerMSASplatImm(SDLoc DL, EVT ResTy, SDValue ImmOp, SelectionDAG &DAG) {
1174 EVT ViaVecTy = ResTy;
1175 SmallVector<SDValue, 16> Ops;
1178 if (ViaVecTy == MVT::v2i64) {
1179 ImmHiOp = DAG.getNode(ISD::SRA, DL, MVT::i32, ImmOp,
1180 DAG.getConstant(31, MVT::i32));
1181 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i) {
1182 Ops.push_back(ImmHiOp);
1183 Ops.push_back(ImmOp);
1185 ViaVecTy = MVT::v4i32;
1187 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1188 Ops.push_back(ImmOp);
1191 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, DL, ViaVecTy, &Ops[0],
1194 if (ResTy != ViaVecTy)
1195 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1201 lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1202 return lowerMSASplatImm(SDLoc(Op), Op->getValueType(0),
1203 Op->getOperand(ImmOp), DAG);
1206 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1207 SelectionDAG &DAG) const {
1210 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1213 case Intrinsic::mips_shilo:
1214 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1215 case Intrinsic::mips_dpau_h_qbl:
1216 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1217 case Intrinsic::mips_dpau_h_qbr:
1218 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1219 case Intrinsic::mips_dpsu_h_qbl:
1220 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1221 case Intrinsic::mips_dpsu_h_qbr:
1222 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1223 case Intrinsic::mips_dpa_w_ph:
1224 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1225 case Intrinsic::mips_dps_w_ph:
1226 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1227 case Intrinsic::mips_dpax_w_ph:
1228 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1229 case Intrinsic::mips_dpsx_w_ph:
1230 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1231 case Intrinsic::mips_mulsa_w_ph:
1232 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1233 case Intrinsic::mips_mult:
1234 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1235 case Intrinsic::mips_multu:
1236 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1237 case Intrinsic::mips_madd:
1238 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1239 case Intrinsic::mips_maddu:
1240 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1241 case Intrinsic::mips_msub:
1242 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1243 case Intrinsic::mips_msubu:
1244 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
1245 case Intrinsic::mips_addv_b:
1246 case Intrinsic::mips_addv_h:
1247 case Intrinsic::mips_addv_w:
1248 case Intrinsic::mips_addv_d:
1249 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1251 case Intrinsic::mips_addvi_b:
1252 case Intrinsic::mips_addvi_h:
1253 case Intrinsic::mips_addvi_w:
1254 case Intrinsic::mips_addvi_d:
1255 return DAG.getNode(ISD::ADD, DL, Op->getValueType(0), Op->getOperand(1),
1256 lowerMSASplatImm(Op, 2, DAG));
1257 case Intrinsic::mips_and_v:
1258 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1260 case Intrinsic::mips_andi_b:
1261 return DAG.getNode(ISD::AND, DL, Op->getValueType(0), Op->getOperand(1),
1262 lowerMSASplatImm(Op, 2, DAG));
1263 case Intrinsic::mips_bnz_b:
1264 case Intrinsic::mips_bnz_h:
1265 case Intrinsic::mips_bnz_w:
1266 case Intrinsic::mips_bnz_d:
1267 return DAG.getNode(MipsISD::VALL_NONZERO, DL, Op->getValueType(0),
1269 case Intrinsic::mips_bnz_v:
1270 return DAG.getNode(MipsISD::VANY_NONZERO, DL, Op->getValueType(0),
1272 case Intrinsic::mips_bsel_v:
1273 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1274 Op->getOperand(1), Op->getOperand(2),
1276 case Intrinsic::mips_bseli_b:
1277 return DAG.getNode(ISD::VSELECT, DL, Op->getValueType(0),
1278 Op->getOperand(1), Op->getOperand(2),
1279 lowerMSASplatImm(Op, 3, DAG));
1280 case Intrinsic::mips_bz_b:
1281 case Intrinsic::mips_bz_h:
1282 case Intrinsic::mips_bz_w:
1283 case Intrinsic::mips_bz_d:
1284 return DAG.getNode(MipsISD::VALL_ZERO, DL, Op->getValueType(0),
1286 case Intrinsic::mips_bz_v:
1287 return DAG.getNode(MipsISD::VANY_ZERO, DL, Op->getValueType(0),
1289 case Intrinsic::mips_ceq_b:
1290 case Intrinsic::mips_ceq_h:
1291 case Intrinsic::mips_ceq_w:
1292 case Intrinsic::mips_ceq_d:
1293 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1294 Op->getOperand(2), ISD::SETEQ);
1295 case Intrinsic::mips_ceqi_b:
1296 case Intrinsic::mips_ceqi_h:
1297 case Intrinsic::mips_ceqi_w:
1298 case Intrinsic::mips_ceqi_d:
1299 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1300 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1301 case Intrinsic::mips_cle_s_b:
1302 case Intrinsic::mips_cle_s_h:
1303 case Intrinsic::mips_cle_s_w:
1304 case Intrinsic::mips_cle_s_d:
1305 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1306 Op->getOperand(2), ISD::SETLE);
1307 case Intrinsic::mips_clei_s_b:
1308 case Intrinsic::mips_clei_s_h:
1309 case Intrinsic::mips_clei_s_w:
1310 case Intrinsic::mips_clei_s_d:
1311 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1312 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1313 case Intrinsic::mips_cle_u_b:
1314 case Intrinsic::mips_cle_u_h:
1315 case Intrinsic::mips_cle_u_w:
1316 case Intrinsic::mips_cle_u_d:
1317 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1318 Op->getOperand(2), ISD::SETULE);
1319 case Intrinsic::mips_clei_u_b:
1320 case Intrinsic::mips_clei_u_h:
1321 case Intrinsic::mips_clei_u_w:
1322 case Intrinsic::mips_clei_u_d:
1323 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1324 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1325 case Intrinsic::mips_clt_s_b:
1326 case Intrinsic::mips_clt_s_h:
1327 case Intrinsic::mips_clt_s_w:
1328 case Intrinsic::mips_clt_s_d:
1329 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1330 Op->getOperand(2), ISD::SETLT);
1331 case Intrinsic::mips_clti_s_b:
1332 case Intrinsic::mips_clti_s_h:
1333 case Intrinsic::mips_clti_s_w:
1334 case Intrinsic::mips_clti_s_d:
1335 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1336 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1337 case Intrinsic::mips_clt_u_b:
1338 case Intrinsic::mips_clt_u_h:
1339 case Intrinsic::mips_clt_u_w:
1340 case Intrinsic::mips_clt_u_d:
1341 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1342 Op->getOperand(2), ISD::SETULT);
1343 case Intrinsic::mips_clti_u_b:
1344 case Intrinsic::mips_clti_u_h:
1345 case Intrinsic::mips_clti_u_w:
1346 case Intrinsic::mips_clti_u_d:
1347 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1348 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
1349 case Intrinsic::mips_copy_s_b:
1350 case Intrinsic::mips_copy_s_h:
1351 case Intrinsic::mips_copy_s_w:
1352 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1353 case Intrinsic::mips_copy_s_d:
1354 // Don't lower directly into VEXTRACT_SEXT_ELT since i64 might be illegal.
1355 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1356 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1357 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1358 Op->getOperand(1), Op->getOperand(2));
1359 case Intrinsic::mips_copy_u_b:
1360 case Intrinsic::mips_copy_u_h:
1361 case Intrinsic::mips_copy_u_w:
1362 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1363 case Intrinsic::mips_copy_u_d:
1364 // Don't lower directly into VEXTRACT_ZEXT_ELT since i64 might be illegal.
1365 // Instead lower to the generic EXTRACT_VECTOR_ELT node and let the type
1366 // legalizer and EXTRACT_VECTOR_ELT lowering sort it out.
1368 // Note: When i64 is illegal, this results in copy_s.w instructions instead
1369 // of copy_u.w instructions. This makes no difference to the behaviour
1370 // since i64 is only illegal when the register file is 32-bit.
1371 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1372 Op->getOperand(1), Op->getOperand(2));
1373 case Intrinsic::mips_div_s_b:
1374 case Intrinsic::mips_div_s_h:
1375 case Intrinsic::mips_div_s_w:
1376 case Intrinsic::mips_div_s_d:
1377 return DAG.getNode(ISD::SDIV, DL, Op->getValueType(0), Op->getOperand(1),
1379 case Intrinsic::mips_div_u_b:
1380 case Intrinsic::mips_div_u_h:
1381 case Intrinsic::mips_div_u_w:
1382 case Intrinsic::mips_div_u_d:
1383 return DAG.getNode(ISD::UDIV, DL, Op->getValueType(0), Op->getOperand(1),
1385 case Intrinsic::mips_fadd_w:
1386 case Intrinsic::mips_fadd_d:
1387 return DAG.getNode(ISD::FADD, DL, Op->getValueType(0), Op->getOperand(1),
1389 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1390 case Intrinsic::mips_fceq_w:
1391 case Intrinsic::mips_fceq_d:
1392 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1393 Op->getOperand(2), ISD::SETOEQ);
1394 case Intrinsic::mips_fcle_w:
1395 case Intrinsic::mips_fcle_d:
1396 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1397 Op->getOperand(2), ISD::SETOLE);
1398 case Intrinsic::mips_fclt_w:
1399 case Intrinsic::mips_fclt_d:
1400 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1401 Op->getOperand(2), ISD::SETOLT);
1402 case Intrinsic::mips_fcne_w:
1403 case Intrinsic::mips_fcne_d:
1404 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1405 Op->getOperand(2), ISD::SETONE);
1406 case Intrinsic::mips_fcor_w:
1407 case Intrinsic::mips_fcor_d:
1408 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1409 Op->getOperand(2), ISD::SETO);
1410 case Intrinsic::mips_fcueq_w:
1411 case Intrinsic::mips_fcueq_d:
1412 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1413 Op->getOperand(2), ISD::SETUEQ);
1414 case Intrinsic::mips_fcule_w:
1415 case Intrinsic::mips_fcule_d:
1416 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1417 Op->getOperand(2), ISD::SETULE);
1418 case Intrinsic::mips_fcult_w:
1419 case Intrinsic::mips_fcult_d:
1420 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1421 Op->getOperand(2), ISD::SETULT);
1422 case Intrinsic::mips_fcun_w:
1423 case Intrinsic::mips_fcun_d:
1424 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1425 Op->getOperand(2), ISD::SETUO);
1426 case Intrinsic::mips_fcune_w:
1427 case Intrinsic::mips_fcune_d:
1428 return DAG.getSetCC(DL, Op->getValueType(0), Op->getOperand(1),
1429 Op->getOperand(2), ISD::SETUNE);
1430 case Intrinsic::mips_fdiv_w:
1431 case Intrinsic::mips_fdiv_d:
1432 return DAG.getNode(ISD::FDIV, DL, Op->getValueType(0), Op->getOperand(1),
1434 case Intrinsic::mips_ffint_u_w:
1435 case Intrinsic::mips_ffint_u_d:
1436 return DAG.getNode(ISD::UINT_TO_FP, DL, Op->getValueType(0),
1438 case Intrinsic::mips_ffint_s_w:
1439 case Intrinsic::mips_ffint_s_d:
1440 return DAG.getNode(ISD::SINT_TO_FP, DL, Op->getValueType(0),
1442 case Intrinsic::mips_fill_b:
1443 case Intrinsic::mips_fill_h:
1444 case Intrinsic::mips_fill_w:
1445 case Intrinsic::mips_fill_d: {
1446 SmallVector<SDValue, 16> Ops;
1447 EVT ResTy = Op->getValueType(0);
1449 for (unsigned i = 0; i < ResTy.getVectorNumElements(); ++i)
1450 Ops.push_back(Op->getOperand(1));
1452 // If ResTy is v2i64 then the type legalizer will break this node down into
1453 // an equivalent v4i32.
1454 return DAG.getNode(ISD::BUILD_VECTOR, DL, ResTy, &Ops[0], Ops.size());
1456 case Intrinsic::mips_fexp2_w:
1457 case Intrinsic::mips_fexp2_d: {
1458 EVT ResTy = Op->getValueType(0);
1460 ISD::FMUL, SDLoc(Op), ResTy, Op->getOperand(1),
1461 DAG.getNode(ISD::FEXP2, SDLoc(Op), ResTy, Op->getOperand(2)));
1463 case Intrinsic::mips_flog2_w:
1464 case Intrinsic::mips_flog2_d:
1465 return DAG.getNode(ISD::FLOG2, DL, Op->getValueType(0), Op->getOperand(1));
1466 case Intrinsic::mips_fmadd_w:
1467 case Intrinsic::mips_fmadd_d:
1468 return DAG.getNode(ISD::FMA, SDLoc(Op), Op->getValueType(0),
1469 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
1470 case Intrinsic::mips_fmul_w:
1471 case Intrinsic::mips_fmul_d:
1472 return DAG.getNode(ISD::FMUL, DL, Op->getValueType(0), Op->getOperand(1),
1474 case Intrinsic::mips_fmsub_w:
1475 case Intrinsic::mips_fmsub_d: {
1476 EVT ResTy = Op->getValueType(0);
1477 return DAG.getNode(ISD::FSUB, SDLoc(Op), ResTy, Op->getOperand(1),
1478 DAG.getNode(ISD::FMUL, SDLoc(Op), ResTy,
1479 Op->getOperand(2), Op->getOperand(3)));
1481 case Intrinsic::mips_frint_w:
1482 case Intrinsic::mips_frint_d:
1483 return DAG.getNode(ISD::FRINT, DL, Op->getValueType(0), Op->getOperand(1));
1484 case Intrinsic::mips_fsqrt_w:
1485 case Intrinsic::mips_fsqrt_d:
1486 return DAG.getNode(ISD::FSQRT, DL, Op->getValueType(0), Op->getOperand(1));
1487 case Intrinsic::mips_fsub_w:
1488 case Intrinsic::mips_fsub_d:
1489 return DAG.getNode(ISD::FSUB, DL, Op->getValueType(0), Op->getOperand(1),
1491 case Intrinsic::mips_ftrunc_u_w:
1492 case Intrinsic::mips_ftrunc_u_d:
1493 return DAG.getNode(ISD::FP_TO_UINT, DL, Op->getValueType(0),
1495 case Intrinsic::mips_ftrunc_s_w:
1496 case Intrinsic::mips_ftrunc_s_d:
1497 return DAG.getNode(ISD::FP_TO_SINT, DL, Op->getValueType(0),
1499 case Intrinsic::mips_ilvev_b:
1500 case Intrinsic::mips_ilvev_h:
1501 case Intrinsic::mips_ilvev_w:
1502 case Intrinsic::mips_ilvev_d:
1503 return DAG.getNode(MipsISD::ILVEV, DL, Op->getValueType(0),
1504 Op->getOperand(1), Op->getOperand(2));
1505 case Intrinsic::mips_ilvl_b:
1506 case Intrinsic::mips_ilvl_h:
1507 case Intrinsic::mips_ilvl_w:
1508 case Intrinsic::mips_ilvl_d:
1509 return DAG.getNode(MipsISD::ILVL, DL, Op->getValueType(0),
1510 Op->getOperand(1), Op->getOperand(2));
1511 case Intrinsic::mips_ilvod_b:
1512 case Intrinsic::mips_ilvod_h:
1513 case Intrinsic::mips_ilvod_w:
1514 case Intrinsic::mips_ilvod_d:
1515 return DAG.getNode(MipsISD::ILVOD, DL, Op->getValueType(0),
1516 Op->getOperand(1), Op->getOperand(2));
1517 case Intrinsic::mips_ilvr_b:
1518 case Intrinsic::mips_ilvr_h:
1519 case Intrinsic::mips_ilvr_w:
1520 case Intrinsic::mips_ilvr_d:
1521 return DAG.getNode(MipsISD::ILVR, DL, Op->getValueType(0),
1522 Op->getOperand(1), Op->getOperand(2));
1523 case Intrinsic::mips_insert_b:
1524 case Intrinsic::mips_insert_h:
1525 case Intrinsic::mips_insert_w:
1526 case Intrinsic::mips_insert_d:
1527 return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), Op->getValueType(0),
1528 Op->getOperand(1), Op->getOperand(3), Op->getOperand(2));
1529 case Intrinsic::mips_ldi_b:
1530 case Intrinsic::mips_ldi_h:
1531 case Intrinsic::mips_ldi_w:
1532 case Intrinsic::mips_ldi_d:
1533 return lowerMSASplatImm(Op, 1, DAG);
1534 case Intrinsic::mips_lsa: {
1535 EVT ResTy = Op->getValueType(0);
1536 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1537 DAG.getNode(ISD::SHL, SDLoc(Op), ResTy,
1538 Op->getOperand(2), Op->getOperand(3)));
1540 case Intrinsic::mips_maddv_b:
1541 case Intrinsic::mips_maddv_h:
1542 case Intrinsic::mips_maddv_w:
1543 case Intrinsic::mips_maddv_d: {
1544 EVT ResTy = Op->getValueType(0);
1545 return DAG.getNode(ISD::ADD, SDLoc(Op), ResTy, Op->getOperand(1),
1546 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1547 Op->getOperand(2), Op->getOperand(3)));
1549 case Intrinsic::mips_max_s_b:
1550 case Intrinsic::mips_max_s_h:
1551 case Intrinsic::mips_max_s_w:
1552 case Intrinsic::mips_max_s_d:
1553 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1554 Op->getOperand(1), Op->getOperand(2));
1555 case Intrinsic::mips_max_u_b:
1556 case Intrinsic::mips_max_u_h:
1557 case Intrinsic::mips_max_u_w:
1558 case Intrinsic::mips_max_u_d:
1559 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1560 Op->getOperand(1), Op->getOperand(2));
1561 case Intrinsic::mips_maxi_s_b:
1562 case Intrinsic::mips_maxi_s_h:
1563 case Intrinsic::mips_maxi_s_w:
1564 case Intrinsic::mips_maxi_s_d:
1565 return DAG.getNode(MipsISD::VSMAX, DL, Op->getValueType(0),
1566 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1567 case Intrinsic::mips_maxi_u_b:
1568 case Intrinsic::mips_maxi_u_h:
1569 case Intrinsic::mips_maxi_u_w:
1570 case Intrinsic::mips_maxi_u_d:
1571 return DAG.getNode(MipsISD::VUMAX, DL, Op->getValueType(0),
1572 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1573 case Intrinsic::mips_min_s_b:
1574 case Intrinsic::mips_min_s_h:
1575 case Intrinsic::mips_min_s_w:
1576 case Intrinsic::mips_min_s_d:
1577 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1578 Op->getOperand(1), Op->getOperand(2));
1579 case Intrinsic::mips_min_u_b:
1580 case Intrinsic::mips_min_u_h:
1581 case Intrinsic::mips_min_u_w:
1582 case Intrinsic::mips_min_u_d:
1583 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1584 Op->getOperand(1), Op->getOperand(2));
1585 case Intrinsic::mips_mini_s_b:
1586 case Intrinsic::mips_mini_s_h:
1587 case Intrinsic::mips_mini_s_w:
1588 case Intrinsic::mips_mini_s_d:
1589 return DAG.getNode(MipsISD::VSMIN, DL, Op->getValueType(0),
1590 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1591 case Intrinsic::mips_mini_u_b:
1592 case Intrinsic::mips_mini_u_h:
1593 case Intrinsic::mips_mini_u_w:
1594 case Intrinsic::mips_mini_u_d:
1595 return DAG.getNode(MipsISD::VUMIN, DL, Op->getValueType(0),
1596 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1597 case Intrinsic::mips_mod_s_b:
1598 case Intrinsic::mips_mod_s_h:
1599 case Intrinsic::mips_mod_s_w:
1600 case Intrinsic::mips_mod_s_d:
1601 return DAG.getNode(ISD::SREM, DL, Op->getValueType(0), Op->getOperand(1),
1603 case Intrinsic::mips_mod_u_b:
1604 case Intrinsic::mips_mod_u_h:
1605 case Intrinsic::mips_mod_u_w:
1606 case Intrinsic::mips_mod_u_d:
1607 return DAG.getNode(ISD::UREM, DL, Op->getValueType(0), Op->getOperand(1),
1609 case Intrinsic::mips_mulv_b:
1610 case Intrinsic::mips_mulv_h:
1611 case Intrinsic::mips_mulv_w:
1612 case Intrinsic::mips_mulv_d:
1613 return DAG.getNode(ISD::MUL, DL, Op->getValueType(0), Op->getOperand(1),
1615 case Intrinsic::mips_msubv_b:
1616 case Intrinsic::mips_msubv_h:
1617 case Intrinsic::mips_msubv_w:
1618 case Intrinsic::mips_msubv_d: {
1619 EVT ResTy = Op->getValueType(0);
1620 return DAG.getNode(ISD::SUB, SDLoc(Op), ResTy, Op->getOperand(1),
1621 DAG.getNode(ISD::MUL, SDLoc(Op), ResTy,
1622 Op->getOperand(2), Op->getOperand(3)));
1624 case Intrinsic::mips_nlzc_b:
1625 case Intrinsic::mips_nlzc_h:
1626 case Intrinsic::mips_nlzc_w:
1627 case Intrinsic::mips_nlzc_d:
1628 return DAG.getNode(ISD::CTLZ, DL, Op->getValueType(0), Op->getOperand(1));
1629 case Intrinsic::mips_nor_v: {
1630 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1631 Op->getOperand(1), Op->getOperand(2));
1632 return DAG.getNOT(DL, Res, Res->getValueType(0));
1634 case Intrinsic::mips_nori_b: {
1635 SDValue Res = DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1637 lowerMSASplatImm(Op, 2, DAG));
1638 return DAG.getNOT(DL, Res, Res->getValueType(0));
1640 case Intrinsic::mips_or_v:
1641 return DAG.getNode(ISD::OR, DL, Op->getValueType(0), Op->getOperand(1),
1643 case Intrinsic::mips_ori_b:
1644 return DAG.getNode(ISD::OR, DL, Op->getValueType(0),
1645 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1646 case Intrinsic::mips_pckev_b:
1647 case Intrinsic::mips_pckev_h:
1648 case Intrinsic::mips_pckev_w:
1649 case Intrinsic::mips_pckev_d:
1650 return DAG.getNode(MipsISD::PCKEV, DL, Op->getValueType(0),
1651 Op->getOperand(1), Op->getOperand(2));
1652 case Intrinsic::mips_pckod_b:
1653 case Intrinsic::mips_pckod_h:
1654 case Intrinsic::mips_pckod_w:
1655 case Intrinsic::mips_pckod_d:
1656 return DAG.getNode(MipsISD::PCKOD, DL, Op->getValueType(0),
1657 Op->getOperand(1), Op->getOperand(2));
1658 case Intrinsic::mips_pcnt_b:
1659 case Intrinsic::mips_pcnt_h:
1660 case Intrinsic::mips_pcnt_w:
1661 case Intrinsic::mips_pcnt_d:
1662 return DAG.getNode(ISD::CTPOP, DL, Op->getValueType(0), Op->getOperand(1));
1663 case Intrinsic::mips_shf_b:
1664 case Intrinsic::mips_shf_h:
1665 case Intrinsic::mips_shf_w:
1666 return DAG.getNode(MipsISD::SHF, DL, Op->getValueType(0),
1667 Op->getOperand(2), Op->getOperand(1));
1668 case Intrinsic::mips_sll_b:
1669 case Intrinsic::mips_sll_h:
1670 case Intrinsic::mips_sll_w:
1671 case Intrinsic::mips_sll_d:
1672 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0), Op->getOperand(1),
1674 case Intrinsic::mips_slli_b:
1675 case Intrinsic::mips_slli_h:
1676 case Intrinsic::mips_slli_w:
1677 case Intrinsic::mips_slli_d:
1678 return DAG.getNode(ISD::SHL, DL, Op->getValueType(0),
1679 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1680 case Intrinsic::mips_splat_b:
1681 case Intrinsic::mips_splat_h:
1682 case Intrinsic::mips_splat_w:
1683 case Intrinsic::mips_splat_d:
1684 // We can't lower via VECTOR_SHUFFLE because it requires constant shuffle
1685 // masks, nor can we lower via BUILD_VECTOR & EXTRACT_VECTOR_ELT because
1686 // EXTRACT_VECTOR_ELT can't extract i64's on MIPS32.
1687 // Instead we lower to MipsISD::VSHF and match from there.
1688 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1689 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1691 case Intrinsic::mips_splati_b:
1692 case Intrinsic::mips_splati_h:
1693 case Intrinsic::mips_splati_w:
1694 case Intrinsic::mips_splati_d:
1695 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1696 lowerMSASplatImm(Op, 2, DAG), Op->getOperand(1),
1698 case Intrinsic::mips_sra_b:
1699 case Intrinsic::mips_sra_h:
1700 case Intrinsic::mips_sra_w:
1701 case Intrinsic::mips_sra_d:
1702 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0), Op->getOperand(1),
1704 case Intrinsic::mips_srai_b:
1705 case Intrinsic::mips_srai_h:
1706 case Intrinsic::mips_srai_w:
1707 case Intrinsic::mips_srai_d:
1708 return DAG.getNode(ISD::SRA, DL, Op->getValueType(0),
1709 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1710 case Intrinsic::mips_srl_b:
1711 case Intrinsic::mips_srl_h:
1712 case Intrinsic::mips_srl_w:
1713 case Intrinsic::mips_srl_d:
1714 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0), Op->getOperand(1),
1716 case Intrinsic::mips_srli_b:
1717 case Intrinsic::mips_srli_h:
1718 case Intrinsic::mips_srli_w:
1719 case Intrinsic::mips_srli_d:
1720 return DAG.getNode(ISD::SRL, DL, Op->getValueType(0),
1721 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1722 case Intrinsic::mips_subv_b:
1723 case Intrinsic::mips_subv_h:
1724 case Intrinsic::mips_subv_w:
1725 case Intrinsic::mips_subv_d:
1726 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0), Op->getOperand(1),
1728 case Intrinsic::mips_subvi_b:
1729 case Intrinsic::mips_subvi_h:
1730 case Intrinsic::mips_subvi_w:
1731 case Intrinsic::mips_subvi_d:
1732 return DAG.getNode(ISD::SUB, DL, Op->getValueType(0),
1733 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1734 case Intrinsic::mips_vshf_b:
1735 case Intrinsic::mips_vshf_h:
1736 case Intrinsic::mips_vshf_w:
1737 case Intrinsic::mips_vshf_d:
1738 return DAG.getNode(MipsISD::VSHF, DL, Op->getValueType(0),
1739 Op->getOperand(1), Op->getOperand(2), Op->getOperand(3));
1740 case Intrinsic::mips_xor_v:
1741 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0), Op->getOperand(1),
1743 case Intrinsic::mips_xori_b:
1744 return DAG.getNode(ISD::XOR, DL, Op->getValueType(0),
1745 Op->getOperand(1), lowerMSASplatImm(Op, 2, DAG));
1749 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1751 SDValue ChainIn = Op->getOperand(0);
1752 SDValue Address = Op->getOperand(2);
1753 SDValue Offset = Op->getOperand(3);
1754 EVT ResTy = Op->getValueType(0);
1755 EVT PtrTy = Address->getValueType(0);
1757 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1759 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1763 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1764 SelectionDAG &DAG) const {
1765 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1769 case Intrinsic::mips_extp:
1770 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1771 case Intrinsic::mips_extpdp:
1772 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1773 case Intrinsic::mips_extr_w:
1774 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1775 case Intrinsic::mips_extr_r_w:
1776 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1777 case Intrinsic::mips_extr_rs_w:
1778 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1779 case Intrinsic::mips_extr_s_h:
1780 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1781 case Intrinsic::mips_mthlip:
1782 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1783 case Intrinsic::mips_mulsaq_s_w_ph:
1784 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1785 case Intrinsic::mips_maq_s_w_phl:
1786 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1787 case Intrinsic::mips_maq_s_w_phr:
1788 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1789 case Intrinsic::mips_maq_sa_w_phl:
1790 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1791 case Intrinsic::mips_maq_sa_w_phr:
1792 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1793 case Intrinsic::mips_dpaq_s_w_ph:
1794 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1795 case Intrinsic::mips_dpsq_s_w_ph:
1796 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1797 case Intrinsic::mips_dpaq_sa_l_w:
1798 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1799 case Intrinsic::mips_dpsq_sa_l_w:
1800 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1801 case Intrinsic::mips_dpaqx_s_w_ph:
1802 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1803 case Intrinsic::mips_dpaqx_sa_w_ph:
1804 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1805 case Intrinsic::mips_dpsqx_s_w_ph:
1806 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1807 case Intrinsic::mips_dpsqx_sa_w_ph:
1808 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
1809 case Intrinsic::mips_ld_b:
1810 case Intrinsic::mips_ld_h:
1811 case Intrinsic::mips_ld_w:
1812 case Intrinsic::mips_ld_d:
1813 return lowerMSALoadIntr(Op, DAG, Intr);
1817 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1819 SDValue ChainIn = Op->getOperand(0);
1820 SDValue Value = Op->getOperand(2);
1821 SDValue Address = Op->getOperand(3);
1822 SDValue Offset = Op->getOperand(4);
1823 EVT PtrTy = Address->getValueType(0);
1825 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1827 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1831 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1832 SelectionDAG &DAG) const {
1833 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1837 case Intrinsic::mips_st_b:
1838 case Intrinsic::mips_st_h:
1839 case Intrinsic::mips_st_w:
1840 case Intrinsic::mips_st_d:
1841 return lowerMSAStoreIntr(Op, DAG, Intr);
1845 /// \brief Check if the given BuildVectorSDNode is a splat.
1846 /// This method currently relies on DAG nodes being reused when equivalent,
1847 /// so it's possible for this to return false even when isConstantSplat returns
1849 static bool isSplatVector(const BuildVectorSDNode *N) {
1850 unsigned int nOps = N->getNumOperands();
1851 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1853 SDValue Operand0 = N->getOperand(0);
1855 for (unsigned int i = 1; i < nOps; ++i) {
1856 if (N->getOperand(i) != Operand0)
1863 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1865 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1866 // choose to sign-extend but we could have equally chosen zero-extend. The
1867 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1868 // result into this node later (possibly changing it to a zero-extend in the
1870 SDValue MipsSETargetLowering::
1871 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1873 EVT ResTy = Op->getValueType(0);
1874 SDValue Op0 = Op->getOperand(0);
1875 EVT VecTy = Op0->getValueType(0);
1877 if (!VecTy.is128BitVector())
1880 if (ResTy.isInteger()) {
1881 SDValue Op1 = Op->getOperand(1);
1882 EVT EltTy = VecTy.getVectorElementType();
1883 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1884 DAG.getValueType(EltTy));
1890 static bool isConstantOrUndef(const SDValue Op) {
1891 if (Op->getOpcode() == ISD::UNDEF)
1893 if (dyn_cast<ConstantSDNode>(Op))
1895 if (dyn_cast<ConstantFPSDNode>(Op))
1900 static bool isConstantOrUndefBUILD_VECTOR(const BuildVectorSDNode *Op) {
1901 for (unsigned i = 0; i < Op->getNumOperands(); ++i)
1902 if (isConstantOrUndef(Op->getOperand(i)))
1907 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1910 // Lowers according to the following rules:
1911 // - Constant splats are legal as-is as long as the SplatBitSize is a power of
1912 // 2 less than or equal to 64 and the value fits into a signed 10-bit
1914 // - Constant splats are lowered to bitconverted BUILD_VECTORs if SplatBitSize
1915 // is a power of 2 less than or equal to 64 and the value does not fit into a
1916 // signed 10-bit immediate
1917 // - Non-constant splats are legal as-is.
1918 // - Non-constant non-splats are lowered to sequences of INSERT_VECTOR_ELT.
1919 // - All others are illegal and must be expanded.
1920 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1921 SelectionDAG &DAG) const {
1922 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1923 EVT ResTy = Op->getValueType(0);
1925 APInt SplatValue, SplatUndef;
1926 unsigned SplatBitSize;
1929 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1932 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1934 !Subtarget->isLittle()) && SplatBitSize <= 64) {
1935 // We can only cope with 8, 16, 32, or 64-bit elements
1936 if (SplatBitSize != 8 && SplatBitSize != 16 && SplatBitSize != 32 &&
1940 // If the value fits into a simm10 then we can use ldi.[bhwd]
1941 if (SplatValue.isSignedIntN(10))
1946 switch (SplatBitSize) {
1950 ViaVecTy = MVT::v16i8;
1953 ViaVecTy = MVT::v8i16;
1956 ViaVecTy = MVT::v4i32;
1959 // There's no fill.d to fall back on for 64-bit values
1963 SmallVector<SDValue, 16> Ops;
1964 SDValue Constant = DAG.getConstant(SplatValue.sextOrSelf(32), MVT::i32);
1966 for (unsigned i = 0; i < ViaVecTy.getVectorNumElements(); ++i)
1967 Ops.push_back(Constant);
1969 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Node), ViaVecTy,
1970 &Ops[0], Ops.size());
1972 if (ViaVecTy != ResTy)
1973 Result = DAG.getNode(ISD::BITCAST, SDLoc(Node), ResTy, Result);
1976 } else if (isSplatVector(Node))
1978 else if (!isConstantOrUndefBUILD_VECTOR(Node)) {
1979 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
1980 // The resulting code is the same length as the expansion, but it doesn't
1981 // use memory operations
1982 EVT ResTy = Node->getValueType(0);
1984 assert(ResTy.isVector());
1986 unsigned NumElts = ResTy.getVectorNumElements();
1987 SDValue Vector = DAG.getUNDEF(ResTy);
1988 for (unsigned i = 0; i < NumElts; ++i) {
1989 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
1990 Node->getOperand(i),
1991 DAG.getConstant(i, MVT::i32));
1999 // Lower VECTOR_SHUFFLE into SHF (if possible).
2001 // SHF splits the vector into blocks of four elements, then shuffles these
2002 // elements according to a <4 x i2> constant (encoded as an integer immediate).
2004 // It is therefore possible to lower into SHF when the mask takes the form:
2005 // <a, b, c, d, a+4, b+4, c+4, d+4, a+8, b+8, c+8, d+8, ...>
2006 // When undef's appear they are treated as if they were whatever value is
2007 // necessary in order to fit the above form.
2010 // %2 = shufflevector <8 x i16> %0, <8 x i16> undef,
2011 // <8 x i32> <i32 3, i32 2, i32 1, i32 0,
2012 // i32 7, i32 6, i32 5, i32 4>
2014 // (SHF_H $w0, $w1, 27)
2015 // where the 27 comes from:
2016 // 3 + (2 << 2) + (1 << 4) + (0 << 6)
2017 static SDValue lowerVECTOR_SHUFFLE_SHF(SDValue Op, EVT ResTy,
2018 SmallVector<int, 16> Indices,
2019 SelectionDAG &DAG) {
2020 int SHFIndices[4] = { -1, -1, -1, -1 };
2022 if (Indices.size() < 4)
2025 for (unsigned i = 0; i < 4; ++i) {
2026 for (unsigned j = i; j < Indices.size(); j += 4) {
2027 int Idx = Indices[j];
2029 // Convert from vector index to 4-element subvector index
2030 // If an index refers to an element outside of the subvector then give up
2033 if (Idx < 0 || Idx >= 4)
2037 // If the mask has an undef, replace it with the current index.
2038 // Note that it might still be undef if the current index is also undef
2039 if (SHFIndices[i] == -1)
2040 SHFIndices[i] = Idx;
2042 // Check that non-undef values are the same as in the mask. If they
2043 // aren't then give up
2044 if (!(Idx == -1 || Idx == SHFIndices[i]))
2049 // Calculate the immediate. Replace any remaining undefs with zero
2051 for (int i = 3; i >= 0; --i) {
2052 int Idx = SHFIndices[i];
2061 return DAG.getNode(MipsISD::SHF, SDLoc(Op), ResTy,
2062 DAG.getConstant(Imm, MVT::i32), Op->getOperand(0));
2065 // Lower VECTOR_SHUFFLE into ILVEV (if possible).
2067 // ILVEV interleaves the even elements from each vector.
2069 // It is possible to lower into ILVEV when the mask takes the form:
2070 // <0, n, 2, n+2, 4, n+4, ...>
2071 // where n is the number of elements in the vector.
2073 // When undef's appear in the mask they are treated as if they were whatever
2074 // value is necessary in order to fit the above form.
2075 static SDValue lowerVECTOR_SHUFFLE_ILVEV(SDValue Op, EVT ResTy,
2076 SmallVector<int, 16> Indices,
2077 SelectionDAG &DAG) {
2078 assert ((Indices.size() % 2) == 0);
2080 int WtIdx = ResTy.getVectorNumElements();
2082 for (unsigned i = 0; i < Indices.size(); i += 2) {
2083 if (Indices[i] != -1 && Indices[i] != WsIdx)
2085 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2091 return DAG.getNode(MipsISD::ILVEV, SDLoc(Op), ResTy, Op->getOperand(0),
2095 // Lower VECTOR_SHUFFLE into ILVOD (if possible).
2097 // ILVOD interleaves the odd elements from each vector.
2099 // It is possible to lower into ILVOD when the mask takes the form:
2100 // <1, n+1, 3, n+3, 5, n+5, ...>
2101 // where n is the number of elements in the vector.
2103 // When undef's appear in the mask they are treated as if they were whatever
2104 // value is necessary in order to fit the above form.
2105 static SDValue lowerVECTOR_SHUFFLE_ILVOD(SDValue Op, EVT ResTy,
2106 SmallVector<int, 16> Indices,
2107 SelectionDAG &DAG) {
2108 assert ((Indices.size() % 2) == 0);
2110 int WtIdx = ResTy.getVectorNumElements() + 1;
2112 for (unsigned i = 0; i < Indices.size(); i += 2) {
2113 if (Indices[i] != -1 && Indices[i] != WsIdx)
2115 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2121 return DAG.getNode(MipsISD::ILVOD, SDLoc(Op), ResTy, Op->getOperand(0),
2125 // Lower VECTOR_SHUFFLE into ILVL (if possible).
2127 // ILVL interleaves consecutive elements from the left half of each vector.
2129 // It is possible to lower into ILVL when the mask takes the form:
2130 // <0, n, 1, n+1, 2, n+2, ...>
2131 // where n is the number of elements in the vector.
2133 // When undef's appear in the mask they are treated as if they were whatever
2134 // value is necessary in order to fit the above form.
2135 static SDValue lowerVECTOR_SHUFFLE_ILVL(SDValue Op, EVT ResTy,
2136 SmallVector<int, 16> Indices,
2137 SelectionDAG &DAG) {
2138 assert ((Indices.size() % 2) == 0);
2140 int WtIdx = ResTy.getVectorNumElements();
2142 for (unsigned i = 0; i < Indices.size(); i += 2) {
2143 if (Indices[i] != -1 && Indices[i] != WsIdx)
2145 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2151 return DAG.getNode(MipsISD::ILVL, SDLoc(Op), ResTy, Op->getOperand(0),
2155 // Lower VECTOR_SHUFFLE into ILVR (if possible).
2157 // ILVR interleaves consecutive elements from the right half of each vector.
2159 // It is possible to lower into ILVR when the mask takes the form:
2160 // <x, n+x, x+1, n+x+1, x+2, n+x+2, ...>
2161 // where n is the number of elements in the vector and x is half n.
2163 // When undef's appear in the mask they are treated as if they were whatever
2164 // value is necessary in order to fit the above form.
2165 static SDValue lowerVECTOR_SHUFFLE_ILVR(SDValue Op, EVT ResTy,
2166 SmallVector<int, 16> Indices,
2167 SelectionDAG &DAG) {
2168 assert ((Indices.size() % 2) == 0);
2169 unsigned NumElts = ResTy.getVectorNumElements();
2170 int WsIdx = NumElts / 2;
2171 int WtIdx = NumElts + NumElts / 2;
2173 for (unsigned i = 0; i < Indices.size(); i += 2) {
2174 if (Indices[i] != -1 && Indices[i] != WsIdx)
2176 if (Indices[i+1] != -1 && Indices[i+1] != WtIdx)
2182 return DAG.getNode(MipsISD::ILVR, SDLoc(Op), ResTy, Op->getOperand(0),
2186 // Lower VECTOR_SHUFFLE into PCKEV (if possible).
2188 // PCKEV copies the even elements of each vector into the result vector.
2190 // It is possible to lower into PCKEV when the mask takes the form:
2191 // <0, 2, 4, ..., n, n+2, n+4, ...>
2192 // where n is the number of elements in the vector.
2194 // When undef's appear in the mask they are treated as if they were whatever
2195 // value is necessary in order to fit the above form.
2196 static SDValue lowerVECTOR_SHUFFLE_PCKEV(SDValue Op, EVT ResTy,
2197 SmallVector<int, 16> Indices,
2198 SelectionDAG &DAG) {
2199 assert ((Indices.size() % 2) == 0);
2202 for (unsigned i = 0; i < Indices.size(); ++i) {
2203 if (Indices[i] != -1 && Indices[i] != Idx)
2208 return DAG.getNode(MipsISD::PCKEV, SDLoc(Op), ResTy, Op->getOperand(0),
2212 // Lower VECTOR_SHUFFLE into PCKOD (if possible).
2214 // PCKOD copies the odd elements of each vector into the result vector.
2216 // It is possible to lower into PCKOD when the mask takes the form:
2217 // <1, 3, 5, ..., n+1, n+3, n+5, ...>
2218 // where n is the number of elements in the vector.
2220 // When undef's appear in the mask they are treated as if they were whatever
2221 // value is necessary in order to fit the above form.
2222 static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2223 SmallVector<int, 16> Indices,
2224 SelectionDAG &DAG) {
2225 assert ((Indices.size() % 2) == 0);
2228 for (unsigned i = 0; i < Indices.size(); ++i) {
2229 if (Indices[i] != -1 && Indices[i] != Idx)
2234 return DAG.getNode(MipsISD::PCKOD, SDLoc(Op), ResTy, Op->getOperand(0),
2238 // Lower VECTOR_SHUFFLE into VSHF.
2240 // This mostly consists of converting the shuffle indices in Indices into a
2241 // BUILD_VECTOR and adding it as an operand to the resulting VSHF. There is
2242 // also code to eliminate unused operands of the VECTOR_SHUFFLE. For example,
2243 // if the type is v8i16 and all the indices are less than 8 then the second
2244 // operand is unused and can be replaced with anything. We choose to replace it
2245 // with the used operand since this reduces the number of instructions overall.
2246 static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2247 SmallVector<int, 16> Indices,
2248 SelectionDAG &DAG) {
2249 SmallVector<SDValue, 16> Ops;
2252 EVT MaskVecTy = ResTy.changeVectorElementTypeToInteger();
2253 EVT MaskEltTy = MaskVecTy.getVectorElementType();
2254 bool Using1stVec = false;
2255 bool Using2ndVec = false;
2257 int ResTyNumElts = ResTy.getVectorNumElements();
2259 for (int i = 0; i < ResTyNumElts; ++i) {
2260 // Idx == -1 means UNDEF
2261 int Idx = Indices[i];
2263 if (0 <= Idx && Idx < ResTyNumElts)
2265 if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
2269 for (SmallVector<int, 16>::iterator I = Indices.begin(); I != Indices.end();
2271 Ops.push_back(DAG.getTargetConstant(*I, MaskEltTy));
2273 SDValue MaskVec = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskVecTy, &Ops[0],
2276 if (Using1stVec && Using2ndVec) {
2277 Op0 = Op->getOperand(0);
2278 Op1 = Op->getOperand(1);
2279 } else if (Using1stVec)
2280 Op0 = Op1 = Op->getOperand(0);
2281 else if (Using2ndVec)
2282 Op0 = Op1 = Op->getOperand(1);
2284 llvm_unreachable("shuffle vector mask references neither vector operand?");
2286 return DAG.getNode(MipsISD::VSHF, DL, ResTy, MaskVec, Op0, Op1);
2289 // Lower VECTOR_SHUFFLE into one of a number of instructions depending on the
2290 // indices in the shuffle.
2291 SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
2292 SelectionDAG &DAG) const {
2293 ShuffleVectorSDNode *Node = cast<ShuffleVectorSDNode>(Op);
2294 EVT ResTy = Op->getValueType(0);
2296 if (!ResTy.is128BitVector())
2299 int ResTyNumElts = ResTy.getVectorNumElements();
2300 SmallVector<int, 16> Indices;
2302 for (int i = 0; i < ResTyNumElts; ++i)
2303 Indices.push_back(Node->getMaskElt(i));
2305 SDValue Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG);
2306 if (Result.getNode())
2308 Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG);
2309 if (Result.getNode())
2311 Result = lowerVECTOR_SHUFFLE_ILVOD(Op, ResTy, Indices, DAG);
2312 if (Result.getNode())
2314 Result = lowerVECTOR_SHUFFLE_ILVL(Op, ResTy, Indices, DAG);
2315 if (Result.getNode())
2317 Result = lowerVECTOR_SHUFFLE_ILVR(Op, ResTy, Indices, DAG);
2318 if (Result.getNode())
2320 Result = lowerVECTOR_SHUFFLE_PCKEV(Op, ResTy, Indices, DAG);
2321 if (Result.getNode())
2323 Result = lowerVECTOR_SHUFFLE_PCKOD(Op, ResTy, Indices, DAG);
2324 if (Result.getNode())
2326 return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
2329 MachineBasicBlock * MipsSETargetLowering::
2330 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
2332 // bposge32_pseudo $vr0
2342 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
2344 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2345 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2346 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2347 DebugLoc DL = MI->getDebugLoc();
2348 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2349 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2350 MachineFunction *F = BB->getParent();
2351 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2352 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2353 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2356 F->insert(It, Sink);
2358 // Transfer the remainder of BB and its successor edges to Sink.
2359 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2361 Sink->transferSuccessorsAndUpdatePHIs(BB);
2364 BB->addSuccessor(FBB);
2365 BB->addSuccessor(TBB);
2366 FBB->addSuccessor(Sink);
2367 TBB->addSuccessor(Sink);
2369 // Insert the real bposge32 instruction to $BB.
2370 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
2373 unsigned VR2 = RegInfo.createVirtualRegister(RC);
2374 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
2375 .addReg(Mips::ZERO).addImm(0);
2376 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2379 unsigned VR1 = RegInfo.createVirtualRegister(RC);
2380 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
2381 .addReg(Mips::ZERO).addImm(1);
2383 // Insert phi function to $Sink.
2384 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2385 MI->getOperand(0).getReg())
2386 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
2388 MI->eraseFromParent(); // The pseudo instruction is gone now.
2392 MachineBasicBlock * MipsSETargetLowering::
2393 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
2394 unsigned BranchOp) const{
2396 // vany_nonzero $rd, $ws
2407 // $rd = phi($rd1, $fbb, $rd2, $tbb)
2409 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2410 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2411 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
2412 DebugLoc DL = MI->getDebugLoc();
2413 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2414 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
2415 MachineFunction *F = BB->getParent();
2416 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
2417 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
2418 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
2421 F->insert(It, Sink);
2423 // Transfer the remainder of BB and its successor edges to Sink.
2424 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
2426 Sink->transferSuccessorsAndUpdatePHIs(BB);
2429 BB->addSuccessor(FBB);
2430 BB->addSuccessor(TBB);
2431 FBB->addSuccessor(Sink);
2432 TBB->addSuccessor(Sink);
2434 // Insert the real bnz.b instruction to $BB.
2435 BuildMI(BB, DL, TII->get(BranchOp))
2436 .addReg(MI->getOperand(1).getReg())
2440 unsigned RD1 = RegInfo.createVirtualRegister(RC);
2441 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
2442 .addReg(Mips::ZERO).addImm(0);
2443 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
2446 unsigned RD2 = RegInfo.createVirtualRegister(RC);
2447 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
2448 .addReg(Mips::ZERO).addImm(1);
2450 // Insert phi function to $Sink.
2451 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
2452 MI->getOperand(0).getReg())
2453 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
2455 MI->eraseFromParent(); // The pseudo instruction is gone now.
2459 // Emit the COPY_FW pseudo instruction.
2461 // copy_fw_pseudo $fd, $ws, n
2463 // copy_u_w $rt, $ws, $n
2466 // When n is zero, the equivalent operation can be performed with (potentially)
2467 // zero instructions due to register overlaps. This optimization is never valid
2468 // for lane 1 because it would require FR=0 mode which isn't supported by MSA.
2469 MachineBasicBlock * MipsSETargetLowering::
2470 emitCOPY_FW(MachineInstr *MI, MachineBasicBlock *BB) const{
2471 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2472 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2473 DebugLoc DL = MI->getDebugLoc();
2474 unsigned Fd = MI->getOperand(0).getReg();
2475 unsigned Ws = MI->getOperand(1).getReg();
2476 unsigned Lane = MI->getOperand(2).getImm();
2479 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_lo);
2481 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2483 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wt).addReg(Ws).addImm(1);
2484 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_lo);
2487 MI->eraseFromParent(); // The pseudo instruction is gone now.
2491 // Emit the COPY_FD pseudo instruction.
2493 // copy_fd_pseudo $fd, $ws, n
2495 // splati.d $wt, $ws, $n
2496 // copy $fd, $wt:sub_64
2498 // When n is zero, the equivalent operation can be performed with (potentially)
2499 // zero instructions due to register overlaps. This optimization is always
2500 // valid because FR=1 mode which is the only supported mode in MSA.
2501 MachineBasicBlock * MipsSETargetLowering::
2502 emitCOPY_FD(MachineInstr *MI, MachineBasicBlock *BB) const{
2503 assert(Subtarget->isFP64bit());
2505 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2506 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2507 unsigned Fd = MI->getOperand(0).getReg();
2508 unsigned Ws = MI->getOperand(1).getReg();
2509 unsigned Lane = MI->getOperand(2).getImm() * 2;
2510 DebugLoc DL = MI->getDebugLoc();
2513 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Ws, 0, Mips::sub_64);
2515 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2517 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wt).addReg(Ws).addImm(1);
2518 BuildMI(*BB, MI, DL, TII->get(Mips::COPY), Fd).addReg(Wt, 0, Mips::sub_64);
2521 MI->eraseFromParent(); // The pseudo instruction is gone now.
2525 // Emit the INSERT_FW pseudo instruction.
2527 // insert_fw_pseudo $wd, $wd_in, $n, $fs
2529 // subreg_to_reg $wt:sub_lo, $fs
2530 // insve_w $wd[$n], $wd_in, $wt[0]
2532 MipsSETargetLowering::emitINSERT_FW(MachineInstr *MI,
2533 MachineBasicBlock *BB) const {
2534 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2535 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2536 DebugLoc DL = MI->getDebugLoc();
2537 unsigned Wd = MI->getOperand(0).getReg();
2538 unsigned Wd_in = MI->getOperand(1).getReg();
2539 unsigned Lane = MI->getOperand(2).getImm();
2540 unsigned Fs = MI->getOperand(3).getReg();
2541 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2543 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2546 .addImm(Mips::sub_lo);
2547 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_W), Wd)
2552 MI->eraseFromParent(); // The pseudo instruction is gone now.
2556 // Emit the INSERT_FD pseudo instruction.
2558 // insert_fd_pseudo $wd, $fs, n
2560 // subreg_to_reg $wt:sub_64, $fs
2561 // insve_d $wd[$n], $wd_in, $wt[0]
2563 MipsSETargetLowering::emitINSERT_FD(MachineInstr *MI,
2564 MachineBasicBlock *BB) const {
2565 assert(Subtarget->isFP64bit());
2567 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2568 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2569 DebugLoc DL = MI->getDebugLoc();
2570 unsigned Wd = MI->getOperand(0).getReg();
2571 unsigned Wd_in = MI->getOperand(1).getReg();
2572 unsigned Lane = MI->getOperand(2).getImm();
2573 unsigned Fs = MI->getOperand(3).getReg();
2574 unsigned Wt = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2576 BuildMI(*BB, MI, DL, TII->get(Mips::SUBREG_TO_REG), Wt)
2579 .addImm(Mips::sub_64);
2580 BuildMI(*BB, MI, DL, TII->get(Mips::INSVE_D), Wd)
2585 MI->eraseFromParent(); // The pseudo instruction is gone now.
2589 // Emit the FILL_FW pseudo instruction.
2591 // fill_fw_pseudo $wd, $fs
2593 // implicit_def $wt1
2594 // insert_subreg $wt2:subreg_lo, $wt1, $fs
2595 // splati.w $wd, $wt2[0]
2597 MipsSETargetLowering::emitFILL_FW(MachineInstr *MI,
2598 MachineBasicBlock *BB) const {
2599 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2600 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2601 DebugLoc DL = MI->getDebugLoc();
2602 unsigned Wd = MI->getOperand(0).getReg();
2603 unsigned Fs = MI->getOperand(1).getReg();
2604 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2605 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128WRegClass);
2607 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2608 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2611 .addImm(Mips::sub_lo);
2612 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_W), Wd).addReg(Wt2).addImm(0);
2614 MI->eraseFromParent(); // The pseudo instruction is gone now.
2618 // Emit the FILL_FD pseudo instruction.
2620 // fill_fd_pseudo $wd, $fs
2622 // implicit_def $wt1
2623 // insert_subreg $wt2:subreg_64, $wt1, $fs
2624 // splati.d $wd, $wt2[0]
2626 MipsSETargetLowering::emitFILL_FD(MachineInstr *MI,
2627 MachineBasicBlock *BB) const {
2628 assert(Subtarget->isFP64bit());
2630 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2631 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2632 DebugLoc DL = MI->getDebugLoc();
2633 unsigned Wd = MI->getOperand(0).getReg();
2634 unsigned Fs = MI->getOperand(1).getReg();
2635 unsigned Wt1 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2636 unsigned Wt2 = RegInfo.createVirtualRegister(&Mips::MSA128DRegClass);
2638 BuildMI(*BB, MI, DL, TII->get(Mips::IMPLICIT_DEF), Wt1);
2639 BuildMI(*BB, MI, DL, TII->get(Mips::INSERT_SUBREG), Wt2)
2642 .addImm(Mips::sub_64);
2643 BuildMI(*BB, MI, DL, TII->get(Mips::SPLATI_D), Wd).addReg(Wt2).addImm(0);
2645 MI->eraseFromParent(); // The pseudo instruction is gone now.
2649 // Emit the FEXP2_W_1 pseudo instructions.
2651 // fexp2_w_1_pseudo $wd, $wt
2654 // fexp2.w $wd, $ws, $wt
2656 MipsSETargetLowering::emitFEXP2_W_1(MachineInstr *MI,
2657 MachineBasicBlock *BB) const {
2658 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2659 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2660 const TargetRegisterClass *RC = &Mips::MSA128WRegClass;
2661 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2662 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2663 DebugLoc DL = MI->getDebugLoc();
2665 // Splat 1.0 into a vector
2666 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_W), Ws1).addImm(1);
2667 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_W), Ws2).addReg(Ws1);
2669 // Emit 1.0 * fexp2(Wt)
2670 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_W), MI->getOperand(0).getReg())
2672 .addReg(MI->getOperand(1).getReg());
2674 MI->eraseFromParent(); // The pseudo instruction is gone now.
2678 // Emit the FEXP2_D_1 pseudo instructions.
2680 // fexp2_d_1_pseudo $wd, $wt
2683 // fexp2.d $wd, $ws, $wt
2685 MipsSETargetLowering::emitFEXP2_D_1(MachineInstr *MI,
2686 MachineBasicBlock *BB) const {
2687 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2688 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
2689 const TargetRegisterClass *RC = &Mips::MSA128DRegClass;
2690 unsigned Ws1 = RegInfo.createVirtualRegister(RC);
2691 unsigned Ws2 = RegInfo.createVirtualRegister(RC);
2692 DebugLoc DL = MI->getDebugLoc();
2694 // Splat 1.0 into a vector
2695 BuildMI(*BB, MI, DL, TII->get(Mips::LDI_D), Ws1).addImm(1);
2696 BuildMI(*BB, MI, DL, TII->get(Mips::FFINT_U_D), Ws2).addReg(Ws1);
2698 // Emit 1.0 * fexp2(Wt)
2699 BuildMI(*BB, MI, DL, TII->get(Mips::FEXP2_D), MI->getOperand(0).getReg())
2701 .addReg(MI->getOperand(1).getReg());
2703 MI->eraseFromParent(); // The pseudo instruction is gone now.