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 #include "MipsSEISelLowering.h"
14 #include "MipsRegisterInfo.h"
15 #include "MipsTargetMachine.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Target/TargetInstrInfo.h"
25 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
26 cl::desc("MIPS: Enable tail calls."), cl::init(false));
28 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
29 cl::desc("Expand double precision loads and "
30 "stores to their single precision "
33 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
34 : MipsTargetLowering(TM) {
35 // Set up the register classes
37 clearRegisterClasses();
39 addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
42 addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
44 if (Subtarget->hasDSP()) {
45 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
47 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
48 addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
50 // Expand all builtin opcodes.
51 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
52 setOperationAction(Opc, VecTys[i], Expand);
54 setOperationAction(ISD::ADD, VecTys[i], Legal);
55 setOperationAction(ISD::SUB, VecTys[i], Legal);
56 setOperationAction(ISD::LOAD, VecTys[i], Legal);
57 setOperationAction(ISD::STORE, VecTys[i], Legal);
58 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
61 // Expand all truncating stores and extending loads.
62 unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
63 unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
65 for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
66 for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
67 setTruncStoreAction((MVT::SimpleValueType)VT0,
68 (MVT::SimpleValueType)VT1, Expand);
70 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
71 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
72 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
75 setTargetDAGCombine(ISD::SHL);
76 setTargetDAGCombine(ISD::SRA);
77 setTargetDAGCombine(ISD::SRL);
78 setTargetDAGCombine(ISD::SETCC);
79 setTargetDAGCombine(ISD::VSELECT);
82 if (Subtarget->hasDSPR2())
83 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
85 if (Subtarget->hasMSA()) {
86 addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
87 addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
88 addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
89 addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
90 addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
91 addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
92 addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
94 setTargetDAGCombine(ISD::AND);
95 setTargetDAGCombine(ISD::SRA);
96 setTargetDAGCombine(ISD::VSELECT);
97 setTargetDAGCombine(ISD::XOR);
100 if (!Subtarget->mipsSEUsesSoftFloat()) {
101 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
103 // When dealing with single precision only, use libcalls
104 if (!Subtarget->isSingleFloat()) {
105 if (Subtarget->isFP64bit())
106 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
108 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
112 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
113 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
114 setOperationAction(ISD::MULHS, MVT::i32, Custom);
115 setOperationAction(ISD::MULHU, MVT::i32, Custom);
118 setOperationAction(ISD::MULHS, MVT::i64, Custom);
119 setOperationAction(ISD::MULHU, MVT::i64, Custom);
120 setOperationAction(ISD::MUL, MVT::i64, Custom);
123 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
124 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
126 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
127 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
128 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
129 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
130 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
131 setOperationAction(ISD::LOAD, MVT::i32, Custom);
132 setOperationAction(ISD::STORE, MVT::i32, Custom);
134 setTargetDAGCombine(ISD::ADDE);
135 setTargetDAGCombine(ISD::SUBE);
136 setTargetDAGCombine(ISD::MUL);
138 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
139 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
140 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
143 setOperationAction(ISD::LOAD, MVT::f64, Custom);
144 setOperationAction(ISD::STORE, MVT::f64, Custom);
147 computeRegisterProperties();
150 const MipsTargetLowering *
151 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
152 return new MipsSETargetLowering(TM);
155 // Enable MSA support for the given integer type and Register class.
156 void MipsSETargetLowering::
157 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
158 addRegisterClass(Ty, RC);
160 // Expand all builtin opcodes.
161 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
162 setOperationAction(Opc, Ty, Expand);
164 setOperationAction(ISD::BITCAST, Ty, Legal);
165 setOperationAction(ISD::LOAD, Ty, Legal);
166 setOperationAction(ISD::STORE, Ty, Legal);
167 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
168 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
169 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
171 setOperationAction(ISD::ADD, Ty, Legal);
172 setOperationAction(ISD::AND, Ty, Legal);
173 setOperationAction(ISD::CTLZ, Ty, Legal);
174 setOperationAction(ISD::CTPOP, Ty, Legal);
175 setOperationAction(ISD::MUL, Ty, Legal);
176 setOperationAction(ISD::OR, Ty, Legal);
177 setOperationAction(ISD::SDIV, Ty, Legal);
178 setOperationAction(ISD::SHL, Ty, Legal);
179 setOperationAction(ISD::SRA, Ty, Legal);
180 setOperationAction(ISD::SRL, Ty, Legal);
181 setOperationAction(ISD::SUB, Ty, Legal);
182 setOperationAction(ISD::UDIV, Ty, Legal);
183 setOperationAction(ISD::VSELECT, Ty, Legal);
184 setOperationAction(ISD::XOR, Ty, Legal);
186 setOperationAction(ISD::SETCC, Ty, Legal);
187 setCondCodeAction(ISD::SETNE, Ty, Expand);
188 setCondCodeAction(ISD::SETGE, Ty, Expand);
189 setCondCodeAction(ISD::SETGT, Ty, Expand);
190 setCondCodeAction(ISD::SETUGE, Ty, Expand);
191 setCondCodeAction(ISD::SETUGT, Ty, Expand);
194 // Enable MSA support for the given floating-point type and Register class.
195 void MipsSETargetLowering::
196 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
197 addRegisterClass(Ty, RC);
199 // Expand all builtin opcodes.
200 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
201 setOperationAction(Opc, Ty, Expand);
203 setOperationAction(ISD::LOAD, Ty, Legal);
204 setOperationAction(ISD::STORE, Ty, Legal);
205 setOperationAction(ISD::BITCAST, Ty, Legal);
206 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
208 if (Ty != MVT::v8f16) {
209 setOperationAction(ISD::FABS, Ty, Legal);
210 setOperationAction(ISD::FADD, Ty, Legal);
211 setOperationAction(ISD::FDIV, Ty, Legal);
212 setOperationAction(ISD::FLOG2, Ty, Legal);
213 setOperationAction(ISD::FMUL, Ty, Legal);
214 setOperationAction(ISD::FRINT, Ty, Legal);
215 setOperationAction(ISD::FSQRT, Ty, Legal);
216 setOperationAction(ISD::FSUB, Ty, Legal);
217 setOperationAction(ISD::VSELECT, Ty, Legal);
219 setOperationAction(ISD::SETCC, Ty, Legal);
220 setCondCodeAction(ISD::SETOGE, Ty, Expand);
221 setCondCodeAction(ISD::SETOGT, Ty, Expand);
222 setCondCodeAction(ISD::SETUGE, Ty, Expand);
223 setCondCodeAction(ISD::SETUGT, Ty, Expand);
224 setCondCodeAction(ISD::SETGE, Ty, Expand);
225 setCondCodeAction(ISD::SETGT, Ty, Expand);
230 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
231 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
244 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
245 SelectionDAG &DAG) const {
246 switch(Op.getOpcode()) {
247 case ISD::LOAD: return lowerLOAD(Op, DAG);
248 case ISD::STORE: return lowerSTORE(Op, DAG);
249 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
250 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
251 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
252 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
253 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
254 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
255 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
257 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
258 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
259 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
260 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
261 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
264 return MipsTargetLowering::LowerOperation(Op, DAG);
268 // Transforms a subgraph in CurDAG if the following pattern is found:
269 // (addc multLo, Lo0), (adde multHi, Hi0),
271 // multHi/Lo: product of multiplication
272 // Lo0: initial value of Lo register
273 // Hi0: initial value of Hi register
274 // Return true if pattern matching was successful.
275 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
276 // ADDENode's second operand must be a flag output of an ADDC node in order
277 // for the matching to be successful.
278 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
280 if (ADDCNode->getOpcode() != ISD::ADDC)
283 SDValue MultHi = ADDENode->getOperand(0);
284 SDValue MultLo = ADDCNode->getOperand(0);
285 SDNode *MultNode = MultHi.getNode();
286 unsigned MultOpc = MultHi.getOpcode();
288 // MultHi and MultLo must be generated by the same node,
289 if (MultLo.getNode() != MultNode)
292 // and it must be a multiplication.
293 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
296 // MultLo amd MultHi must be the first and second output of MultNode
298 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
301 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
302 // of the values of MultNode, in which case MultNode will be removed in later
304 // If there exist users other than ADDENode or ADDCNode, this function returns
305 // here, which will result in MultNode being mapped to a single MULT
306 // instruction node rather than a pair of MULT and MADD instructions being
308 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
313 // Initialize accumulator.
314 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
315 ADDCNode->getOperand(1),
316 ADDENode->getOperand(1));
318 // create MipsMAdd(u) node
319 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
321 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
322 MultNode->getOperand(0),// Factor 0
323 MultNode->getOperand(1),// Factor 1
326 // replace uses of adde and addc here
327 if (!SDValue(ADDCNode, 0).use_empty()) {
328 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
329 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
331 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
333 if (!SDValue(ADDENode, 0).use_empty()) {
334 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
335 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
337 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
344 // Transforms a subgraph in CurDAG if the following pattern is found:
345 // (addc Lo0, multLo), (sube Hi0, multHi),
347 // multHi/Lo: product of multiplication
348 // Lo0: initial value of Lo register
349 // Hi0: initial value of Hi register
350 // Return true if pattern matching was successful.
351 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
352 // SUBENode's second operand must be a flag output of an SUBC node in order
353 // for the matching to be successful.
354 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
356 if (SUBCNode->getOpcode() != ISD::SUBC)
359 SDValue MultHi = SUBENode->getOperand(1);
360 SDValue MultLo = SUBCNode->getOperand(1);
361 SDNode *MultNode = MultHi.getNode();
362 unsigned MultOpc = MultHi.getOpcode();
364 // MultHi and MultLo must be generated by the same node,
365 if (MultLo.getNode() != MultNode)
368 // and it must be a multiplication.
369 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
372 // MultLo amd MultHi must be the first and second output of MultNode
374 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
377 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
378 // of the values of MultNode, in which case MultNode will be removed in later
380 // If there exist users other than SUBENode or SUBCNode, this function returns
381 // here, which will result in MultNode being mapped to a single MULT
382 // instruction node rather than a pair of MULT and MSUB instructions being
384 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
389 // Initialize accumulator.
390 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
391 SUBCNode->getOperand(0),
392 SUBENode->getOperand(0));
394 // create MipsSub(u) node
395 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
397 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
398 MultNode->getOperand(0),// Factor 0
399 MultNode->getOperand(1),// Factor 1
402 // replace uses of sube and subc here
403 if (!SDValue(SUBCNode, 0).use_empty()) {
404 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
405 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
407 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
409 if (!SDValue(SUBENode, 0).use_empty()) {
410 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
411 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
413 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
419 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
420 TargetLowering::DAGCombinerInfo &DCI,
421 const MipsSubtarget *Subtarget) {
422 if (DCI.isBeforeLegalize())
425 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
427 return SDValue(N, 0);
432 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
434 // Performs the following transformations:
435 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
436 // sign/zero-extension is completely overwritten by the new one performed by
438 // - Removes redundant zero extensions performed by an ISD::AND.
439 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
440 TargetLowering::DAGCombinerInfo &DCI,
441 const MipsSubtarget *Subtarget) {
442 if (!Subtarget->hasMSA())
445 SDValue Op0 = N->getOperand(0);
446 SDValue Op1 = N->getOperand(1);
447 unsigned Op0Opcode = Op0->getOpcode();
449 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
450 // where $d + 1 == 2^n and n == 32
451 // or $d + 1 == 2^n and n <= 32 and ZExt
452 // -> (MipsVExtractZExt $a, $b, $c)
453 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
454 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
455 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
460 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
462 if (Log2IfPositive <= 0)
463 return SDValue(); // Mask+1 is not a power of 2
465 SDValue Op0Op2 = Op0->getOperand(2);
466 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
467 unsigned ExtendTySize = ExtendTy.getSizeInBits();
468 unsigned Log2 = Log2IfPositive;
470 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
471 Log2 == ExtendTySize) {
472 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
473 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
474 Op0->getVTList(), Ops, Op0->getNumOperands());
482 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
483 TargetLowering::DAGCombinerInfo &DCI,
484 const MipsSubtarget *Subtarget) {
485 if (DCI.isBeforeLegalize())
488 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
490 return SDValue(N, 0);
495 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
496 EVT ShiftTy, SelectionDAG &DAG) {
497 // Clear the upper (64 - VT.sizeInBits) bits.
498 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
502 return DAG.getConstant(0, VT);
508 // If c is power of 2, return (shl x, log2(c)).
509 if (isPowerOf2_64(C))
510 return DAG.getNode(ISD::SHL, DL, VT, X,
511 DAG.getConstant(Log2_64(C), ShiftTy));
513 unsigned Log2Ceil = Log2_64_Ceil(C);
514 uint64_t Floor = 1LL << Log2_64(C);
515 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
517 // If |c - floor_c| <= |c - ceil_c|,
518 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
519 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
520 if (C - Floor <= Ceil - C) {
521 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
522 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
523 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
526 // If |c - floor_c| > |c - ceil_c|,
527 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
528 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
529 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
530 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
533 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
534 const TargetLowering::DAGCombinerInfo &DCI,
535 const MipsSETargetLowering *TL) {
536 EVT VT = N->getValueType(0);
538 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
540 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
541 VT, TL->getScalarShiftAmountTy(VT), DAG);
543 return SDValue(N, 0);
546 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
548 const MipsSubtarget *Subtarget) {
549 // See if this is a vector splat immediate node.
550 APInt SplatValue, SplatUndef;
551 unsigned SplatBitSize;
553 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
554 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
557 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
558 EltSize, !Subtarget->isLittle()) ||
559 (SplatBitSize != EltSize) ||
560 (SplatValue.getZExtValue() >= EltSize))
563 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
564 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
567 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
568 TargetLowering::DAGCombinerInfo &DCI,
569 const MipsSubtarget *Subtarget) {
570 EVT Ty = N->getValueType(0);
572 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
575 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
578 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
579 // constant splats into MipsISD::SHRA_DSP for DSPr2.
581 // Performs the following transformations:
582 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
583 // sign/zero-extension is completely overwritten by the new one performed by
584 // the ISD::SRA and ISD::SHL nodes.
585 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
588 // See performDSPShiftCombine for more information about the transformation
590 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
591 TargetLowering::DAGCombinerInfo &DCI,
592 const MipsSubtarget *Subtarget) {
593 EVT Ty = N->getValueType(0);
595 if (Subtarget->hasMSA()) {
596 SDValue Op0 = N->getOperand(0);
597 SDValue Op1 = N->getOperand(1);
599 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
600 // where $d + sizeof($c) == 32
601 // or $d + sizeof($c) <= 32 and SExt
602 // -> (MipsVExtractSExt $a, $b, $c)
603 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
604 SDValue Op0Op0 = Op0->getOperand(0);
605 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
610 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
611 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
613 if (TotalBits == 32 ||
614 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
616 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
617 Op0Op0->getOperand(2) };
618 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
619 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
625 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
628 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
632 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
633 TargetLowering::DAGCombinerInfo &DCI,
634 const MipsSubtarget *Subtarget) {
635 EVT Ty = N->getValueType(0);
637 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
640 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
643 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
644 bool IsV216 = (Ty == MVT::v2i16);
648 case ISD::SETNE: return true;
652 case ISD::SETGE: return IsV216;
656 case ISD::SETUGE: return !IsV216;
657 default: return false;
661 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
662 EVT Ty = N->getValueType(0);
664 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
667 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
670 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
671 N->getOperand(1), N->getOperand(2));
674 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
675 EVT Ty = N->getValueType(0);
677 if (Ty.is128BitVector() && Ty.isInteger()) {
678 // Try the following combines:
679 // (vselect (setcc $a, $b, SETLT), $b, $a)) -> (vsmax $a, $b)
680 // (vselect (setcc $a, $b, SETLE), $b, $a)) -> (vsmax $a, $b)
681 // (vselect (setcc $a, $b, SETLT), $a, $b)) -> (vsmin $a, $b)
682 // (vselect (setcc $a, $b, SETLE), $a, $b)) -> (vsmin $a, $b)
683 // (vselect (setcc $a, $b, SETULT), $b, $a)) -> (vumax $a, $b)
684 // (vselect (setcc $a, $b, SETULE), $b, $a)) -> (vumax $a, $b)
685 // (vselect (setcc $a, $b, SETULT), $a, $b)) -> (vumin $a, $b)
686 // (vselect (setcc $a, $b, SETULE), $a, $b)) -> (vumin $a, $b)
687 // SETGT/SETGE/SETUGT/SETUGE variants of these will show up initially but
688 // will be expanded to equivalent SETLT/SETLE/SETULT/SETULE versions by the
690 SDValue Op0 = N->getOperand(0);
692 if (Op0->getOpcode() != ISD::SETCC)
695 ISD::CondCode CondCode = cast<CondCodeSDNode>(Op0->getOperand(2))->get();
698 if (CondCode == ISD::SETLT || CondCode == ISD::SETLE)
700 else if (CondCode == ISD::SETULT || CondCode == ISD::SETULE)
705 SDValue Op1 = N->getOperand(1);
706 SDValue Op2 = N->getOperand(2);
707 SDValue Op0Op0 = Op0->getOperand(0);
708 SDValue Op0Op1 = Op0->getOperand(1);
710 if (Op1 == Op0Op0 && Op2 == Op0Op1)
711 return DAG.getNode(Signed ? MipsISD::VSMIN : MipsISD::VUMIN, SDLoc(N),
713 else if (Op1 == Op0Op1 && Op2 == Op0Op0)
714 return DAG.getNode(Signed ? MipsISD::VSMAX : MipsISD::VUMAX, SDLoc(N),
716 } else if ((Ty == MVT::v2i16) || (Ty == MVT::v4i8)) {
717 SDValue SetCC = N->getOperand(0);
719 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
722 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
723 SetCC.getOperand(0), SetCC.getOperand(1),
724 N->getOperand(1), N->getOperand(2), SetCC.getOperand(2));
730 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
731 const MipsSubtarget *Subtarget) {
732 EVT Ty = N->getValueType(0);
734 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
735 // Try the following combines:
736 // (xor (or $a, $b), (build_vector allones))
737 // (xor (or $a, $b), (bitcast (build_vector allones)))
738 SDValue Op0 = N->getOperand(0);
739 SDValue Op1 = N->getOperand(1);
741 ConstantSDNode *Const;
743 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
745 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
747 else if ((Op0->getOpcode() == MipsISD::VSPLAT ||
748 Op0->getOpcode() == MipsISD::VSPLATD) &&
749 (Const = dyn_cast<ConstantSDNode>(Op0->getOperand(0))) &&
750 Const->isAllOnesValue())
752 else if ((Op1->getOpcode() == MipsISD::VSPLAT ||
753 Op1->getOpcode() == MipsISD::VSPLATD) &&
754 (Const = dyn_cast<ConstantSDNode>(Op1->getOperand(0))) &&
755 Const->isAllOnesValue())
760 if (NotOp->getOpcode() == ISD::OR)
761 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
762 NotOp->getOperand(1));
769 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
770 SelectionDAG &DAG = DCI.DAG;
773 switch (N->getOpcode()) {
775 return performADDECombine(N, DAG, DCI, Subtarget);
777 Val = performANDCombine(N, DAG, DCI, Subtarget);
780 return performSUBECombine(N, DAG, DCI, Subtarget);
782 return performMULCombine(N, DAG, DCI, this);
784 return performSHLCombine(N, DAG, DCI, Subtarget);
786 return performSRACombine(N, DAG, DCI, Subtarget);
788 return performSRLCombine(N, DAG, DCI, Subtarget);
790 return performVSELECTCombine(N, DAG);
792 Val = performXORCombine(N, DAG, Subtarget);
795 Val = performSETCCCombine(N, DAG);
802 return MipsTargetLowering::PerformDAGCombine(N, DCI);
806 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
807 MachineBasicBlock *BB) const {
808 switch (MI->getOpcode()) {
810 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
811 case Mips::BPOSGE32_PSEUDO:
812 return emitBPOSGE32(MI, BB);
813 case Mips::SNZ_B_PSEUDO:
814 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
815 case Mips::SNZ_H_PSEUDO:
816 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
817 case Mips::SNZ_W_PSEUDO:
818 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
819 case Mips::SNZ_D_PSEUDO:
820 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
821 case Mips::SNZ_V_PSEUDO:
822 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
823 case Mips::SZ_B_PSEUDO:
824 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
825 case Mips::SZ_H_PSEUDO:
826 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
827 case Mips::SZ_W_PSEUDO:
828 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
829 case Mips::SZ_D_PSEUDO:
830 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
831 case Mips::SZ_V_PSEUDO:
832 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
836 bool MipsSETargetLowering::
837 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
838 unsigned NextStackOffset,
839 const MipsFunctionInfo& FI) const {
840 if (!EnableMipsTailCalls)
843 // Return false if either the callee or caller has a byval argument.
844 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
847 // Return true if the callee's argument area is no larger than the
849 return NextStackOffset <= FI.getIncomingArgSize();
852 void MipsSETargetLowering::
853 getOpndList(SmallVectorImpl<SDValue> &Ops,
854 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
855 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
856 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
857 // T9 should contain the address of the callee function if
858 // -reloction-model=pic or it is an indirect call.
859 if (IsPICCall || !GlobalOrExternal) {
860 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
861 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
863 Ops.push_back(Callee);
865 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
866 InternalLinkage, CLI, Callee, Chain);
869 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
870 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
872 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
873 return MipsTargetLowering::lowerLOAD(Op, DAG);
875 // Replace a double precision load with two i32 loads and a buildpair64.
877 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
878 EVT PtrVT = Ptr.getValueType();
880 // i32 load from lower address.
881 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
882 MachinePointerInfo(), Nd.isVolatile(),
883 Nd.isNonTemporal(), Nd.isInvariant(),
886 // i32 load from higher address.
887 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
888 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
889 MachinePointerInfo(), Nd.isVolatile(),
890 Nd.isNonTemporal(), Nd.isInvariant(),
891 std::min(Nd.getAlignment(), 4U));
893 if (!Subtarget->isLittle())
896 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
897 SDValue Ops[2] = {BP, Hi.getValue(1)};
898 return DAG.getMergeValues(Ops, 2, DL);
901 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
902 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
904 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
905 return MipsTargetLowering::lowerSTORE(Op, DAG);
907 // Replace a double precision store with two extractelement64s and i32 stores.
909 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
910 EVT PtrVT = Ptr.getValueType();
911 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
912 Val, DAG.getConstant(0, MVT::i32));
913 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
914 Val, DAG.getConstant(1, MVT::i32));
916 if (!Subtarget->isLittle())
919 // i32 store to lower address.
920 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
921 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
924 // i32 store to higher address.
925 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
926 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
927 Nd.isVolatile(), Nd.isNonTemporal(),
928 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
931 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
932 bool HasLo, bool HasHi,
933 SelectionDAG &DAG) const {
934 EVT Ty = Op.getOperand(0).getValueType();
936 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
937 Op.getOperand(0), Op.getOperand(1));
941 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
942 DAG.getConstant(Mips::sub_lo, MVT::i32));
944 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
945 DAG.getConstant(Mips::sub_hi, MVT::i32));
947 if (!HasLo || !HasHi)
948 return HasLo ? Lo : Hi;
950 SDValue Vals[] = { Lo, Hi };
951 return DAG.getMergeValues(Vals, 2, DL);
955 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
956 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
957 DAG.getConstant(0, MVT::i32));
958 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
959 DAG.getConstant(1, MVT::i32));
960 return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
963 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
964 SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
965 DAG.getConstant(Mips::sub_lo, MVT::i32));
966 SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
967 DAG.getConstant(Mips::sub_hi, MVT::i32));
968 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
971 // This function expands mips intrinsic nodes which have 64-bit input operands
974 // out64 = intrinsic-node in64
976 // lo = copy (extract-element (in64, 0))
977 // hi = copy (extract-element (in64, 1))
978 // mips-specific-node
981 // out64 = merge-values (v0, v1)
983 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
985 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
986 SmallVector<SDValue, 3> Ops;
989 // See if Op has a chain input.
991 Ops.push_back(Op->getOperand(OpNo++));
993 // The next operand is the intrinsic opcode.
994 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
996 // See if the next operand has type i64.
997 SDValue Opnd = Op->getOperand(++OpNo), In64;
999 if (Opnd.getValueType() == MVT::i64)
1000 In64 = initAccumulator(Opnd, DL, DAG);
1002 Ops.push_back(Opnd);
1004 // Push the remaining operands.
1005 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
1006 Ops.push_back(Op->getOperand(OpNo));
1008 // Add In64 to the end of the list.
1010 Ops.push_back(In64);
1013 SmallVector<EVT, 2> ResTys;
1015 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
1017 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
1020 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
1021 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
1026 assert(Val->getValueType(1) == MVT::Other);
1027 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
1028 return DAG.getMergeValues(Vals, 2, DL);
1031 static SDValue lowerMSABinaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1033 SDValue LHS = Op->getOperand(1);
1034 SDValue RHS = Op->getOperand(2);
1035 EVT ResTy = Op->getValueType(0);
1037 SDValue Result = DAG.getNode(Opc, DL, ResTy, LHS, RHS);
1042 static SDValue lowerMSABinaryImmIntr(SDValue Op, SelectionDAG &DAG,
1043 unsigned Opc, SDValue RHS) {
1044 SDValue LHS = Op->getOperand(1);
1045 EVT ResTy = Op->getValueType(0);
1047 return DAG.getNode(Opc, SDLoc(Op), ResTy, LHS, RHS);
1050 static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1052 SDValue Value = Op->getOperand(1);
1053 EVT ResTy = Op->getValueType(0);
1055 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1060 // Lower an MSA copy intrinsic into the specified SelectionDAG node
1061 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1063 SDValue Vec = Op->getOperand(1);
1064 SDValue Idx = Op->getOperand(2);
1065 EVT ResTy = Op->getValueType(0);
1066 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1068 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1069 DAG.getValueType(EltTy));
1074 // Lower an MSA insert intrinsic into the specified SelectionDAG node
1075 static SDValue lowerMSAInsertIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1077 SDValue Op0 = Op->getOperand(1);
1078 SDValue Op1 = Op->getOperand(2);
1079 SDValue Op2 = Op->getOperand(3);
1080 EVT ResTy = Op->getValueType(0);
1082 SDValue Result = DAG.getNode(Opc, DL, ResTy, Op0, Op2, Op1);
1087 static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1088 EVT ResTy = Op->getValueType(0);
1090 unsigned SplatOp = MipsISD::VSPLAT;
1091 if (ResTy == MVT::v2i64)
1092 SplatOp = MipsISD::VSPLATD;
1094 return DAG.getNode(SplatOp, SDLoc(Op), ResTy, Op->getOperand(ImmOp));
1097 static SDValue lowerMSAUnaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1099 SDValue Value = Op->getOperand(1);
1100 EVT ResTy = Op->getValueType(0);
1102 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1107 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1108 SelectionDAG &DAG) const {
1109 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1112 case Intrinsic::mips_shilo:
1113 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1114 case Intrinsic::mips_dpau_h_qbl:
1115 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1116 case Intrinsic::mips_dpau_h_qbr:
1117 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1118 case Intrinsic::mips_dpsu_h_qbl:
1119 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1120 case Intrinsic::mips_dpsu_h_qbr:
1121 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1122 case Intrinsic::mips_dpa_w_ph:
1123 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1124 case Intrinsic::mips_dps_w_ph:
1125 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1126 case Intrinsic::mips_dpax_w_ph:
1127 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1128 case Intrinsic::mips_dpsx_w_ph:
1129 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1130 case Intrinsic::mips_mulsa_w_ph:
1131 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1132 case Intrinsic::mips_mult:
1133 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1134 case Intrinsic::mips_multu:
1135 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1136 case Intrinsic::mips_madd:
1137 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1138 case Intrinsic::mips_maddu:
1139 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1140 case Intrinsic::mips_msub:
1141 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1142 case Intrinsic::mips_msubu:
1143 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
1144 case Intrinsic::mips_addv_b:
1145 case Intrinsic::mips_addv_h:
1146 case Intrinsic::mips_addv_w:
1147 case Intrinsic::mips_addv_d:
1148 return lowerMSABinaryIntr(Op, DAG, ISD::ADD);
1149 case Intrinsic::mips_addvi_b:
1150 case Intrinsic::mips_addvi_h:
1151 case Intrinsic::mips_addvi_w:
1152 case Intrinsic::mips_addvi_d:
1153 return lowerMSABinaryImmIntr(Op, DAG, ISD::ADD,
1154 lowerMSASplatImm(Op, 2, DAG));
1155 case Intrinsic::mips_and_v:
1156 return lowerMSABinaryIntr(Op, DAG, ISD::AND);
1157 case Intrinsic::mips_andi_b:
1158 return lowerMSABinaryImmIntr(Op, DAG, ISD::AND,
1159 lowerMSASplatImm(Op, 2, DAG));
1160 case Intrinsic::mips_bnz_b:
1161 case Intrinsic::mips_bnz_h:
1162 case Intrinsic::mips_bnz_w:
1163 case Intrinsic::mips_bnz_d:
1164 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
1165 case Intrinsic::mips_bnz_v:
1166 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
1167 case Intrinsic::mips_bsel_v:
1168 return DAG.getNode(ISD::VSELECT, SDLoc(Op), Op->getValueType(0),
1169 Op->getOperand(1), Op->getOperand(2),
1171 case Intrinsic::mips_bseli_b:
1172 return DAG.getNode(ISD::VSELECT, SDLoc(Op), Op->getValueType(0),
1173 Op->getOperand(1), Op->getOperand(2),
1174 lowerMSASplatImm(Op, 3, DAG));
1175 case Intrinsic::mips_bz_b:
1176 case Intrinsic::mips_bz_h:
1177 case Intrinsic::mips_bz_w:
1178 case Intrinsic::mips_bz_d:
1179 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
1180 case Intrinsic::mips_bz_v:
1181 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
1182 case Intrinsic::mips_ceq_b:
1183 case Intrinsic::mips_ceq_h:
1184 case Intrinsic::mips_ceq_w:
1185 case Intrinsic::mips_ceq_d:
1186 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1187 Op->getOperand(2), ISD::SETEQ);
1188 case Intrinsic::mips_ceqi_b:
1189 case Intrinsic::mips_ceqi_h:
1190 case Intrinsic::mips_ceqi_w:
1191 case Intrinsic::mips_ceqi_d:
1192 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1193 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1194 case Intrinsic::mips_cle_s_b:
1195 case Intrinsic::mips_cle_s_h:
1196 case Intrinsic::mips_cle_s_w:
1197 case Intrinsic::mips_cle_s_d:
1198 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1199 Op->getOperand(2), ISD::SETLE);
1200 case Intrinsic::mips_clei_s_b:
1201 case Intrinsic::mips_clei_s_h:
1202 case Intrinsic::mips_clei_s_w:
1203 case Intrinsic::mips_clei_s_d:
1204 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1205 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1206 case Intrinsic::mips_cle_u_b:
1207 case Intrinsic::mips_cle_u_h:
1208 case Intrinsic::mips_cle_u_w:
1209 case Intrinsic::mips_cle_u_d:
1210 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1211 Op->getOperand(2), ISD::SETULE);
1212 case Intrinsic::mips_clei_u_b:
1213 case Intrinsic::mips_clei_u_h:
1214 case Intrinsic::mips_clei_u_w:
1215 case Intrinsic::mips_clei_u_d:
1216 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1217 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1218 case Intrinsic::mips_clt_s_b:
1219 case Intrinsic::mips_clt_s_h:
1220 case Intrinsic::mips_clt_s_w:
1221 case Intrinsic::mips_clt_s_d:
1222 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1223 Op->getOperand(2), ISD::SETLT);
1224 case Intrinsic::mips_clti_s_b:
1225 case Intrinsic::mips_clti_s_h:
1226 case Intrinsic::mips_clti_s_w:
1227 case Intrinsic::mips_clti_s_d:
1228 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1229 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1230 case Intrinsic::mips_clt_u_b:
1231 case Intrinsic::mips_clt_u_h:
1232 case Intrinsic::mips_clt_u_w:
1233 case Intrinsic::mips_clt_u_d:
1234 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1235 Op->getOperand(2), ISD::SETULT);
1236 case Intrinsic::mips_clti_u_b:
1237 case Intrinsic::mips_clti_u_h:
1238 case Intrinsic::mips_clti_u_w:
1239 case Intrinsic::mips_clti_u_d:
1240 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1241 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
1242 case Intrinsic::mips_copy_s_b:
1243 case Intrinsic::mips_copy_s_h:
1244 case Intrinsic::mips_copy_s_w:
1245 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1246 case Intrinsic::mips_copy_u_b:
1247 case Intrinsic::mips_copy_u_h:
1248 case Intrinsic::mips_copy_u_w:
1249 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1250 case Intrinsic::mips_div_s_b:
1251 case Intrinsic::mips_div_s_h:
1252 case Intrinsic::mips_div_s_w:
1253 case Intrinsic::mips_div_s_d:
1254 return lowerMSABinaryIntr(Op, DAG, ISD::SDIV);
1255 case Intrinsic::mips_div_u_b:
1256 case Intrinsic::mips_div_u_h:
1257 case Intrinsic::mips_div_u_w:
1258 case Intrinsic::mips_div_u_d:
1259 return lowerMSABinaryIntr(Op, DAG, ISD::UDIV);
1260 case Intrinsic::mips_fadd_w:
1261 case Intrinsic::mips_fadd_d:
1262 return lowerMSABinaryIntr(Op, DAG, ISD::FADD);
1263 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1264 case Intrinsic::mips_fceq_w:
1265 case Intrinsic::mips_fceq_d:
1266 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1267 Op->getOperand(2), ISD::SETOEQ);
1268 case Intrinsic::mips_fcle_w:
1269 case Intrinsic::mips_fcle_d:
1270 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1271 Op->getOperand(2), ISD::SETOLE);
1272 case Intrinsic::mips_fclt_w:
1273 case Intrinsic::mips_fclt_d:
1274 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1275 Op->getOperand(2), ISD::SETOLT);
1276 case Intrinsic::mips_fcne_w:
1277 case Intrinsic::mips_fcne_d:
1278 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1279 Op->getOperand(2), ISD::SETONE);
1280 case Intrinsic::mips_fcor_w:
1281 case Intrinsic::mips_fcor_d:
1282 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1283 Op->getOperand(2), ISD::SETO);
1284 case Intrinsic::mips_fcueq_w:
1285 case Intrinsic::mips_fcueq_d:
1286 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1287 Op->getOperand(2), ISD::SETUEQ);
1288 case Intrinsic::mips_fcule_w:
1289 case Intrinsic::mips_fcule_d:
1290 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1291 Op->getOperand(2), ISD::SETULE);
1292 case Intrinsic::mips_fcult_w:
1293 case Intrinsic::mips_fcult_d:
1294 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1295 Op->getOperand(2), ISD::SETULT);
1296 case Intrinsic::mips_fcun_w:
1297 case Intrinsic::mips_fcun_d:
1298 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1299 Op->getOperand(2), ISD::SETUO);
1300 case Intrinsic::mips_fcune_w:
1301 case Intrinsic::mips_fcune_d:
1302 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1303 Op->getOperand(2), ISD::SETUNE);
1304 case Intrinsic::mips_fdiv_w:
1305 case Intrinsic::mips_fdiv_d:
1306 return lowerMSABinaryIntr(Op, DAG, ISD::FDIV);
1307 case Intrinsic::mips_fill_b:
1308 case Intrinsic::mips_fill_h:
1309 case Intrinsic::mips_fill_w:
1310 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
1311 case Intrinsic::mips_flog2_w:
1312 case Intrinsic::mips_flog2_d:
1313 return lowerMSAUnaryIntr(Op, DAG, ISD::FLOG2);
1314 case Intrinsic::mips_fmul_w:
1315 case Intrinsic::mips_fmul_d:
1316 return lowerMSABinaryIntr(Op, DAG, ISD::FMUL);
1317 case Intrinsic::mips_frint_w:
1318 case Intrinsic::mips_frint_d:
1319 return lowerMSAUnaryIntr(Op, DAG, ISD::FRINT);
1320 case Intrinsic::mips_fsqrt_w:
1321 case Intrinsic::mips_fsqrt_d:
1322 return lowerMSAUnaryIntr(Op, DAG, ISD::FSQRT);
1323 case Intrinsic::mips_fsub_w:
1324 case Intrinsic::mips_fsub_d:
1325 return lowerMSABinaryIntr(Op, DAG, ISD::FSUB);
1326 case Intrinsic::mips_insert_b:
1327 case Intrinsic::mips_insert_h:
1328 case Intrinsic::mips_insert_w:
1329 return lowerMSAInsertIntr(Op, DAG, ISD::INSERT_VECTOR_ELT);
1330 case Intrinsic::mips_ldi_b:
1331 case Intrinsic::mips_ldi_h:
1332 case Intrinsic::mips_ldi_w:
1333 case Intrinsic::mips_ldi_d:
1334 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
1335 case Intrinsic::mips_max_s_b:
1336 case Intrinsic::mips_max_s_h:
1337 case Intrinsic::mips_max_s_w:
1338 case Intrinsic::mips_max_s_d:
1339 return lowerMSABinaryIntr(Op, DAG, MipsISD::VSMAX);
1340 case Intrinsic::mips_max_u_b:
1341 case Intrinsic::mips_max_u_h:
1342 case Intrinsic::mips_max_u_w:
1343 case Intrinsic::mips_max_u_d:
1344 return lowerMSABinaryIntr(Op, DAG, MipsISD::VUMAX);
1345 case Intrinsic::mips_maxi_s_b:
1346 case Intrinsic::mips_maxi_s_h:
1347 case Intrinsic::mips_maxi_s_w:
1348 case Intrinsic::mips_maxi_s_d:
1349 return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VSMAX,
1350 lowerMSASplatImm(Op, 2, DAG));
1351 case Intrinsic::mips_maxi_u_b:
1352 case Intrinsic::mips_maxi_u_h:
1353 case Intrinsic::mips_maxi_u_w:
1354 case Intrinsic::mips_maxi_u_d:
1355 return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VUMAX,
1356 lowerMSASplatImm(Op, 2, DAG));
1357 case Intrinsic::mips_min_s_b:
1358 case Intrinsic::mips_min_s_h:
1359 case Intrinsic::mips_min_s_w:
1360 case Intrinsic::mips_min_s_d:
1361 return lowerMSABinaryIntr(Op, DAG, MipsISD::VSMIN);
1362 case Intrinsic::mips_min_u_b:
1363 case Intrinsic::mips_min_u_h:
1364 case Intrinsic::mips_min_u_w:
1365 case Intrinsic::mips_min_u_d:
1366 return lowerMSABinaryIntr(Op, DAG, MipsISD::VUMIN);
1367 case Intrinsic::mips_mini_s_b:
1368 case Intrinsic::mips_mini_s_h:
1369 case Intrinsic::mips_mini_s_w:
1370 case Intrinsic::mips_mini_s_d:
1371 return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VSMIN,
1372 lowerMSASplatImm(Op, 2, DAG));
1373 case Intrinsic::mips_mini_u_b:
1374 case Intrinsic::mips_mini_u_h:
1375 case Intrinsic::mips_mini_u_w:
1376 case Intrinsic::mips_mini_u_d:
1377 return lowerMSABinaryImmIntr(Op, DAG, MipsISD::VUMIN,
1378 lowerMSASplatImm(Op, 2, DAG));
1379 case Intrinsic::mips_mulv_b:
1380 case Intrinsic::mips_mulv_h:
1381 case Intrinsic::mips_mulv_w:
1382 case Intrinsic::mips_mulv_d:
1383 return lowerMSABinaryIntr(Op, DAG, ISD::MUL);
1384 case Intrinsic::mips_nlzc_b:
1385 case Intrinsic::mips_nlzc_h:
1386 case Intrinsic::mips_nlzc_w:
1387 case Intrinsic::mips_nlzc_d:
1388 return lowerMSAUnaryIntr(Op, DAG, ISD::CTLZ);
1389 case Intrinsic::mips_nor_v: {
1390 SDValue Res = lowerMSABinaryIntr(Op, DAG, ISD::OR);
1391 return DAG.getNOT(SDLoc(Op), Res, Res->getValueType(0));
1393 case Intrinsic::mips_nori_b: {
1394 SDValue Res = lowerMSABinaryImmIntr(Op, DAG, ISD::OR,
1395 lowerMSASplatImm(Op, 2, DAG));
1396 return DAG.getNOT(SDLoc(Op), Res, Res->getValueType(0));
1398 case Intrinsic::mips_or_v:
1399 return lowerMSABinaryIntr(Op, DAG, ISD::OR);
1400 case Intrinsic::mips_ori_b:
1401 return lowerMSABinaryImmIntr(Op, DAG, ISD::OR,
1402 lowerMSASplatImm(Op, 2, DAG));
1403 case Intrinsic::mips_pcnt_b:
1404 case Intrinsic::mips_pcnt_h:
1405 case Intrinsic::mips_pcnt_w:
1406 case Intrinsic::mips_pcnt_d:
1407 return lowerMSAUnaryIntr(Op, DAG, ISD::CTPOP);
1408 case Intrinsic::mips_sll_b:
1409 case Intrinsic::mips_sll_h:
1410 case Intrinsic::mips_sll_w:
1411 case Intrinsic::mips_sll_d:
1412 return lowerMSABinaryIntr(Op, DAG, ISD::SHL);
1413 case Intrinsic::mips_slli_b:
1414 case Intrinsic::mips_slli_h:
1415 case Intrinsic::mips_slli_w:
1416 case Intrinsic::mips_slli_d:
1417 return lowerMSABinaryImmIntr(Op, DAG, ISD::SHL,
1418 lowerMSASplatImm(Op, 2, DAG));
1419 case Intrinsic::mips_sra_b:
1420 case Intrinsic::mips_sra_h:
1421 case Intrinsic::mips_sra_w:
1422 case Intrinsic::mips_sra_d:
1423 return lowerMSABinaryIntr(Op, DAG, ISD::SRA);
1424 case Intrinsic::mips_srai_b:
1425 case Intrinsic::mips_srai_h:
1426 case Intrinsic::mips_srai_w:
1427 case Intrinsic::mips_srai_d:
1428 return lowerMSABinaryImmIntr(Op, DAG, ISD::SRA,
1429 lowerMSASplatImm(Op, 2, DAG));
1430 case Intrinsic::mips_srl_b:
1431 case Intrinsic::mips_srl_h:
1432 case Intrinsic::mips_srl_w:
1433 case Intrinsic::mips_srl_d:
1434 return lowerMSABinaryIntr(Op, DAG, ISD::SRL);
1435 case Intrinsic::mips_srli_b:
1436 case Intrinsic::mips_srli_h:
1437 case Intrinsic::mips_srli_w:
1438 case Intrinsic::mips_srli_d:
1439 return lowerMSABinaryImmIntr(Op, DAG, ISD::SRL,
1440 lowerMSASplatImm(Op, 2, DAG));
1441 case Intrinsic::mips_subv_b:
1442 case Intrinsic::mips_subv_h:
1443 case Intrinsic::mips_subv_w:
1444 case Intrinsic::mips_subv_d:
1445 return lowerMSABinaryIntr(Op, DAG, ISD::SUB);
1446 case Intrinsic::mips_subvi_b:
1447 case Intrinsic::mips_subvi_h:
1448 case Intrinsic::mips_subvi_w:
1449 case Intrinsic::mips_subvi_d:
1450 return lowerMSABinaryImmIntr(Op, DAG, ISD::SUB,
1451 lowerMSASplatImm(Op, 2, DAG));
1452 case Intrinsic::mips_xor_v:
1453 return lowerMSABinaryIntr(Op, DAG, ISD::XOR);
1454 case Intrinsic::mips_xori_b:
1455 return lowerMSABinaryImmIntr(Op, DAG, ISD::XOR,
1456 lowerMSASplatImm(Op, 2, DAG));
1460 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1462 SDValue ChainIn = Op->getOperand(0);
1463 SDValue Address = Op->getOperand(2);
1464 SDValue Offset = Op->getOperand(3);
1465 EVT ResTy = Op->getValueType(0);
1466 EVT PtrTy = Address->getValueType(0);
1468 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1470 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1474 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1475 SelectionDAG &DAG) const {
1476 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1480 case Intrinsic::mips_extp:
1481 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1482 case Intrinsic::mips_extpdp:
1483 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1484 case Intrinsic::mips_extr_w:
1485 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1486 case Intrinsic::mips_extr_r_w:
1487 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1488 case Intrinsic::mips_extr_rs_w:
1489 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1490 case Intrinsic::mips_extr_s_h:
1491 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1492 case Intrinsic::mips_mthlip:
1493 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1494 case Intrinsic::mips_mulsaq_s_w_ph:
1495 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1496 case Intrinsic::mips_maq_s_w_phl:
1497 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1498 case Intrinsic::mips_maq_s_w_phr:
1499 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1500 case Intrinsic::mips_maq_sa_w_phl:
1501 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1502 case Intrinsic::mips_maq_sa_w_phr:
1503 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1504 case Intrinsic::mips_dpaq_s_w_ph:
1505 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1506 case Intrinsic::mips_dpsq_s_w_ph:
1507 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1508 case Intrinsic::mips_dpaq_sa_l_w:
1509 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1510 case Intrinsic::mips_dpsq_sa_l_w:
1511 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1512 case Intrinsic::mips_dpaqx_s_w_ph:
1513 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1514 case Intrinsic::mips_dpaqx_sa_w_ph:
1515 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1516 case Intrinsic::mips_dpsqx_s_w_ph:
1517 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1518 case Intrinsic::mips_dpsqx_sa_w_ph:
1519 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
1520 case Intrinsic::mips_ld_b:
1521 case Intrinsic::mips_ld_h:
1522 case Intrinsic::mips_ld_w:
1523 case Intrinsic::mips_ld_d:
1524 case Intrinsic::mips_ldx_b:
1525 case Intrinsic::mips_ldx_h:
1526 case Intrinsic::mips_ldx_w:
1527 case Intrinsic::mips_ldx_d:
1528 return lowerMSALoadIntr(Op, DAG, Intr);
1532 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1534 SDValue ChainIn = Op->getOperand(0);
1535 SDValue Value = Op->getOperand(2);
1536 SDValue Address = Op->getOperand(3);
1537 SDValue Offset = Op->getOperand(4);
1538 EVT PtrTy = Address->getValueType(0);
1540 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1542 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1546 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1547 SelectionDAG &DAG) const {
1548 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1552 case Intrinsic::mips_st_b:
1553 case Intrinsic::mips_st_h:
1554 case Intrinsic::mips_st_w:
1555 case Intrinsic::mips_st_d:
1556 case Intrinsic::mips_stx_b:
1557 case Intrinsic::mips_stx_h:
1558 case Intrinsic::mips_stx_w:
1559 case Intrinsic::mips_stx_d:
1560 return lowerMSAStoreIntr(Op, DAG, Intr);
1564 /// \brief Check if the given BuildVectorSDNode is a splat.
1565 /// This method currently relies on DAG nodes being reused when equivalent,
1566 /// so it's possible for this to return false even when isConstantSplat returns
1568 static bool isSplatVector(const BuildVectorSDNode *N) {
1569 unsigned int nOps = N->getNumOperands();
1570 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1572 SDValue Operand0 = N->getOperand(0);
1574 for (unsigned int i = 1; i < nOps; ++i) {
1575 if (N->getOperand(i) != Operand0)
1582 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1584 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1585 // choose to sign-extend but we could have equally chosen zero-extend. The
1586 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1587 // result into this node later (possibly changing it to a zero-extend in the
1589 SDValue MipsSETargetLowering::
1590 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1592 EVT ResTy = Op->getValueType(0);
1593 SDValue Op0 = Op->getOperand(0);
1594 SDValue Op1 = Op->getOperand(1);
1595 EVT EltTy = Op0->getValueType(0).getVectorElementType();
1596 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1597 DAG.getValueType(EltTy));
1600 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1603 // Lowers according to the following rules:
1604 // - Vectors of 128-bits may be legal subject to the other rules. Other sizes
1606 // - Non-constant splats are legal and are lowered to MipsISD::VSPLAT.
1607 // - Constant splats with an element size of 32-bits or less are legal and are
1608 // lowered to MipsISD::VSPLAT.
1609 // - Constant splats with an element size of 64-bits but whose value would fit
1610 // within a 10 bit immediate are legal and are lowered to MipsISD::VSPLATD.
1611 // - All other ISD::BUILD_VECTORS are not legal
1612 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1613 SelectionDAG &DAG) const {
1614 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1615 EVT ResTy = Op->getValueType(0);
1617 APInt SplatValue, SplatUndef;
1618 unsigned SplatBitSize;
1621 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1624 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1626 !Subtarget->isLittle())) {
1629 EVT ConstTy = MVT::i32;
1630 unsigned SplatOp = MipsISD::VSPLAT;
1632 switch (SplatBitSize) {
1636 TmpVecTy = MVT::v2i64;
1638 // i64 is an illegal type on Mips32, but if it the constant fits into a
1639 // signed 10-bit value then we can still handle it using VSPLATD and an
1643 else if (isInt<10>(SplatValue.getSExtValue())) {
1644 SplatValue = SplatValue.trunc(32);
1645 SplatOp = MipsISD::VSPLATD;
1650 TmpVecTy = MVT::v4i32;
1653 TmpVecTy = MVT::v8i16;
1654 SplatValue = SplatValue.sext(32);
1657 TmpVecTy = MVT::v16i8;
1658 SplatValue = SplatValue.sext(32);
1662 Result = DAG.getNode(SplatOp, DL, TmpVecTy,
1663 DAG.getConstant(SplatValue, ConstTy));
1664 if (ResTy != Result.getValueType())
1665 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1669 else if (isSplatVector(Node))
1670 return DAG.getNode(MipsISD::VSPLAT, DL, ResTy, Op->getOperand(0));
1672 // Use INSERT_VECTOR_ELT operations rather than expand to stores.
1673 // The resulting code is the same length as the expansion, but it doesn't
1674 // use memory operations
1675 EVT ResTy = Node->getValueType(0);
1677 assert(ResTy.isVector());
1679 unsigned NumElts = ResTy.getVectorNumElements();
1680 SDValue Vector = DAG.getUNDEF(ResTy);
1681 for (unsigned i = 0; i < NumElts; ++i) {
1682 Vector = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, ResTy, Vector,
1683 Node->getOperand(i),
1684 DAG.getConstant(i, MVT::i32));
1692 MachineBasicBlock * MipsSETargetLowering::
1693 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
1695 // bposge32_pseudo $vr0
1705 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1707 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1708 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1709 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1710 DebugLoc DL = MI->getDebugLoc();
1711 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1712 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1713 MachineFunction *F = BB->getParent();
1714 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1715 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1716 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1719 F->insert(It, Sink);
1721 // Transfer the remainder of BB and its successor edges to Sink.
1722 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1724 Sink->transferSuccessorsAndUpdatePHIs(BB);
1727 BB->addSuccessor(FBB);
1728 BB->addSuccessor(TBB);
1729 FBB->addSuccessor(Sink);
1730 TBB->addSuccessor(Sink);
1732 // Insert the real bposge32 instruction to $BB.
1733 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1736 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1737 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1738 .addReg(Mips::ZERO).addImm(0);
1739 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1742 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1743 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1744 .addReg(Mips::ZERO).addImm(1);
1746 // Insert phi function to $Sink.
1747 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1748 MI->getOperand(0).getReg())
1749 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1751 MI->eraseFromParent(); // The pseudo instruction is gone now.
1755 MachineBasicBlock * MipsSETargetLowering::
1756 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1757 unsigned BranchOp) const{
1759 // vany_nonzero $rd, $ws
1770 // $rd = phi($rd1, $fbb, $rd2, $tbb)
1772 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1773 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1774 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1775 DebugLoc DL = MI->getDebugLoc();
1776 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1777 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1778 MachineFunction *F = BB->getParent();
1779 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1780 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1781 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1784 F->insert(It, Sink);
1786 // Transfer the remainder of BB and its successor edges to Sink.
1787 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1789 Sink->transferSuccessorsAndUpdatePHIs(BB);
1792 BB->addSuccessor(FBB);
1793 BB->addSuccessor(TBB);
1794 FBB->addSuccessor(Sink);
1795 TBB->addSuccessor(Sink);
1797 // Insert the real bnz.b instruction to $BB.
1798 BuildMI(BB, DL, TII->get(BranchOp))
1799 .addReg(MI->getOperand(1).getReg())
1803 unsigned RD1 = RegInfo.createVirtualRegister(RC);
1804 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1805 .addReg(Mips::ZERO).addImm(0);
1806 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1809 unsigned RD2 = RegInfo.createVirtualRegister(RC);
1810 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1811 .addReg(Mips::ZERO).addImm(1);
1813 // Insert phi function to $Sink.
1814 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1815 MI->getOperand(0).getReg())
1816 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1818 MI->eraseFromParent(); // The pseudo instruction is gone now.