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::XOR);
99 if (!Subtarget->mipsSEUsesSoftFloat()) {
100 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
102 // When dealing with single precision only, use libcalls
103 if (!Subtarget->isSingleFloat()) {
104 if (Subtarget->isFP64bit())
105 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
107 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
111 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
112 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
113 setOperationAction(ISD::MULHS, MVT::i32, Custom);
114 setOperationAction(ISD::MULHU, MVT::i32, Custom);
117 setOperationAction(ISD::MULHS, MVT::i64, Custom);
118 setOperationAction(ISD::MULHU, MVT::i64, Custom);
119 setOperationAction(ISD::MUL, MVT::i64, Custom);
122 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
123 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
125 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
126 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
127 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
128 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
129 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
130 setOperationAction(ISD::LOAD, MVT::i32, Custom);
131 setOperationAction(ISD::STORE, MVT::i32, Custom);
133 setTargetDAGCombine(ISD::ADDE);
134 setTargetDAGCombine(ISD::SUBE);
135 setTargetDAGCombine(ISD::MUL);
137 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
138 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
139 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
142 setOperationAction(ISD::LOAD, MVT::f64, Custom);
143 setOperationAction(ISD::STORE, MVT::f64, Custom);
146 computeRegisterProperties();
149 const MipsTargetLowering *
150 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
151 return new MipsSETargetLowering(TM);
154 // Enable MSA support for the given integer type and Register class.
155 void MipsSETargetLowering::
156 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
157 addRegisterClass(Ty, RC);
159 // Expand all builtin opcodes.
160 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
161 setOperationAction(Opc, Ty, Expand);
163 setOperationAction(ISD::BITCAST, Ty, Legal);
164 setOperationAction(ISD::LOAD, Ty, Legal);
165 setOperationAction(ISD::STORE, Ty, Legal);
166 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Custom);
167 setOperationAction(ISD::INSERT_VECTOR_ELT, Ty, Legal);
168 setOperationAction(ISD::BUILD_VECTOR, Ty, Custom);
170 setOperationAction(ISD::ADD, Ty, Legal);
171 setOperationAction(ISD::AND, Ty, Legal);
172 setOperationAction(ISD::CTLZ, Ty, Legal);
173 setOperationAction(ISD::CTPOP, Ty, Legal);
174 setOperationAction(ISD::MUL, Ty, Legal);
175 setOperationAction(ISD::OR, Ty, Legal);
176 setOperationAction(ISD::SDIV, Ty, Legal);
177 setOperationAction(ISD::SHL, Ty, Legal);
178 setOperationAction(ISD::SRA, Ty, Legal);
179 setOperationAction(ISD::SRL, Ty, Legal);
180 setOperationAction(ISD::SUB, Ty, Legal);
181 setOperationAction(ISD::UDIV, Ty, Legal);
182 setOperationAction(ISD::XOR, Ty, Legal);
184 setOperationAction(ISD::SETCC, Ty, Legal);
185 setCondCodeAction(ISD::SETNE, Ty, Expand);
186 setCondCodeAction(ISD::SETGE, Ty, Expand);
187 setCondCodeAction(ISD::SETGT, Ty, Expand);
188 setCondCodeAction(ISD::SETUGE, Ty, Expand);
189 setCondCodeAction(ISD::SETUGT, Ty, Expand);
192 // Enable MSA support for the given floating-point type and Register class.
193 void MipsSETargetLowering::
194 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
195 addRegisterClass(Ty, RC);
197 // Expand all builtin opcodes.
198 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
199 setOperationAction(Opc, Ty, Expand);
201 setOperationAction(ISD::LOAD, Ty, Legal);
202 setOperationAction(ISD::STORE, Ty, Legal);
203 setOperationAction(ISD::BITCAST, Ty, Legal);
204 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Ty, Legal);
206 if (Ty != MVT::v8f16) {
207 setOperationAction(ISD::FADD, Ty, Legal);
208 setOperationAction(ISD::FDIV, Ty, Legal);
209 setOperationAction(ISD::FLOG2, Ty, Legal);
210 setOperationAction(ISD::FMUL, Ty, Legal);
211 setOperationAction(ISD::FRINT, Ty, Legal);
212 setOperationAction(ISD::FSQRT, Ty, Legal);
213 setOperationAction(ISD::FSUB, Ty, Legal);
215 setOperationAction(ISD::SETCC, Ty, Legal);
216 setCondCodeAction(ISD::SETOGE, Ty, Expand);
217 setCondCodeAction(ISD::SETOGT, Ty, Expand);
218 setCondCodeAction(ISD::SETUGE, Ty, Expand);
219 setCondCodeAction(ISD::SETUGT, Ty, Expand);
220 setCondCodeAction(ISD::SETGE, Ty, Expand);
221 setCondCodeAction(ISD::SETGT, Ty, Expand);
226 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
227 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
240 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
241 SelectionDAG &DAG) const {
242 switch(Op.getOpcode()) {
243 case ISD::LOAD: return lowerLOAD(Op, DAG);
244 case ISD::STORE: return lowerSTORE(Op, DAG);
245 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
246 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
247 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
248 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
249 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
250 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
251 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
253 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
254 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
255 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
256 case ISD::EXTRACT_VECTOR_ELT: return lowerEXTRACT_VECTOR_ELT(Op, DAG);
257 case ISD::BUILD_VECTOR: return lowerBUILD_VECTOR(Op, DAG);
260 return MipsTargetLowering::LowerOperation(Op, DAG);
264 // Transforms a subgraph in CurDAG if the following pattern is found:
265 // (addc multLo, Lo0), (adde multHi, Hi0),
267 // multHi/Lo: product of multiplication
268 // Lo0: initial value of Lo register
269 // Hi0: initial value of Hi register
270 // Return true if pattern matching was successful.
271 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
272 // ADDENode's second operand must be a flag output of an ADDC node in order
273 // for the matching to be successful.
274 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
276 if (ADDCNode->getOpcode() != ISD::ADDC)
279 SDValue MultHi = ADDENode->getOperand(0);
280 SDValue MultLo = ADDCNode->getOperand(0);
281 SDNode *MultNode = MultHi.getNode();
282 unsigned MultOpc = MultHi.getOpcode();
284 // MultHi and MultLo must be generated by the same node,
285 if (MultLo.getNode() != MultNode)
288 // and it must be a multiplication.
289 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
292 // MultLo amd MultHi must be the first and second output of MultNode
294 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
297 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
298 // of the values of MultNode, in which case MultNode will be removed in later
300 // If there exist users other than ADDENode or ADDCNode, this function returns
301 // here, which will result in MultNode being mapped to a single MULT
302 // instruction node rather than a pair of MULT and MADD instructions being
304 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
309 // Initialize accumulator.
310 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
311 ADDCNode->getOperand(1),
312 ADDENode->getOperand(1));
314 // create MipsMAdd(u) node
315 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
317 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
318 MultNode->getOperand(0),// Factor 0
319 MultNode->getOperand(1),// Factor 1
322 // replace uses of adde and addc here
323 if (!SDValue(ADDCNode, 0).use_empty()) {
324 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
325 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
327 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
329 if (!SDValue(ADDENode, 0).use_empty()) {
330 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
331 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
333 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
340 // Transforms a subgraph in CurDAG if the following pattern is found:
341 // (addc Lo0, multLo), (sube Hi0, multHi),
343 // multHi/Lo: product of multiplication
344 // Lo0: initial value of Lo register
345 // Hi0: initial value of Hi register
346 // Return true if pattern matching was successful.
347 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
348 // SUBENode's second operand must be a flag output of an SUBC node in order
349 // for the matching to be successful.
350 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
352 if (SUBCNode->getOpcode() != ISD::SUBC)
355 SDValue MultHi = SUBENode->getOperand(1);
356 SDValue MultLo = SUBCNode->getOperand(1);
357 SDNode *MultNode = MultHi.getNode();
358 unsigned MultOpc = MultHi.getOpcode();
360 // MultHi and MultLo must be generated by the same node,
361 if (MultLo.getNode() != MultNode)
364 // and it must be a multiplication.
365 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
368 // MultLo amd MultHi must be the first and second output of MultNode
370 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
373 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
374 // of the values of MultNode, in which case MultNode will be removed in later
376 // If there exist users other than SUBENode or SUBCNode, this function returns
377 // here, which will result in MultNode being mapped to a single MULT
378 // instruction node rather than a pair of MULT and MSUB instructions being
380 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
385 // Initialize accumulator.
386 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
387 SUBCNode->getOperand(0),
388 SUBENode->getOperand(0));
390 // create MipsSub(u) node
391 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
393 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
394 MultNode->getOperand(0),// Factor 0
395 MultNode->getOperand(1),// Factor 1
398 // replace uses of sube and subc here
399 if (!SDValue(SUBCNode, 0).use_empty()) {
400 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
401 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
403 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
405 if (!SDValue(SUBENode, 0).use_empty()) {
406 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
407 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
409 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
415 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
416 TargetLowering::DAGCombinerInfo &DCI,
417 const MipsSubtarget *Subtarget) {
418 if (DCI.isBeforeLegalize())
421 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
423 return SDValue(N, 0);
428 // Fold zero extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT
430 // Performs the following transformations:
431 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to zero extension if its
432 // sign/zero-extension is completely overwritten by the new one performed by
434 // - Removes redundant zero extensions performed by an ISD::AND.
435 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
436 TargetLowering::DAGCombinerInfo &DCI,
437 const MipsSubtarget *Subtarget) {
438 if (!Subtarget->hasMSA())
441 SDValue Op0 = N->getOperand(0);
442 SDValue Op1 = N->getOperand(1);
443 unsigned Op0Opcode = Op0->getOpcode();
445 // (and (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d)
446 // where $d + 1 == 2^n and n == 32
447 // or $d + 1 == 2^n and n <= 32 and ZExt
448 // -> (MipsVExtractZExt $a, $b, $c)
449 if (Op0Opcode == MipsISD::VEXTRACT_SEXT_ELT ||
450 Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT) {
451 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(Op1);
456 int32_t Log2IfPositive = (Mask->getAPIntValue() + 1).exactLogBase2();
458 if (Log2IfPositive <= 0)
459 return SDValue(); // Mask+1 is not a power of 2
461 SDValue Op0Op2 = Op0->getOperand(2);
462 EVT ExtendTy = cast<VTSDNode>(Op0Op2)->getVT();
463 unsigned ExtendTySize = ExtendTy.getSizeInBits();
464 unsigned Log2 = Log2IfPositive;
466 if ((Op0Opcode == MipsISD::VEXTRACT_ZEXT_ELT && Log2 >= ExtendTySize) ||
467 Log2 == ExtendTySize) {
468 SDValue Ops[] = { Op0->getOperand(0), Op0->getOperand(1), Op0Op2 };
469 DAG.MorphNodeTo(Op0.getNode(), MipsISD::VEXTRACT_ZEXT_ELT,
470 Op0->getVTList(), Ops, Op0->getNumOperands());
478 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
479 TargetLowering::DAGCombinerInfo &DCI,
480 const MipsSubtarget *Subtarget) {
481 if (DCI.isBeforeLegalize())
484 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
486 return SDValue(N, 0);
491 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
492 EVT ShiftTy, SelectionDAG &DAG) {
493 // Clear the upper (64 - VT.sizeInBits) bits.
494 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
498 return DAG.getConstant(0, VT);
504 // If c is power of 2, return (shl x, log2(c)).
505 if (isPowerOf2_64(C))
506 return DAG.getNode(ISD::SHL, DL, VT, X,
507 DAG.getConstant(Log2_64(C), ShiftTy));
509 unsigned Log2Ceil = Log2_64_Ceil(C);
510 uint64_t Floor = 1LL << Log2_64(C);
511 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
513 // If |c - floor_c| <= |c - ceil_c|,
514 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
515 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
516 if (C - Floor <= Ceil - C) {
517 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
518 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
519 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
522 // If |c - floor_c| > |c - ceil_c|,
523 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
524 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
525 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
526 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
529 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
530 const TargetLowering::DAGCombinerInfo &DCI,
531 const MipsSETargetLowering *TL) {
532 EVT VT = N->getValueType(0);
534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
536 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
537 VT, TL->getScalarShiftAmountTy(VT), DAG);
539 return SDValue(N, 0);
542 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
544 const MipsSubtarget *Subtarget) {
545 // See if this is a vector splat immediate node.
546 APInt SplatValue, SplatUndef;
547 unsigned SplatBitSize;
549 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
550 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
553 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
554 EltSize, !Subtarget->isLittle()) ||
555 (SplatBitSize != EltSize) ||
556 (SplatValue.getZExtValue() >= EltSize))
559 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
560 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
563 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
564 TargetLowering::DAGCombinerInfo &DCI,
565 const MipsSubtarget *Subtarget) {
566 EVT Ty = N->getValueType(0);
568 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
571 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
574 // Fold sign-extensions into MipsISD::VEXTRACT_[SZ]EXT_ELT for MSA and fold
575 // constant splats into MipsISD::SHRA_DSP for DSPr2.
577 // Performs the following transformations:
578 // - Changes MipsISD::VEXTRACT_[SZ]EXT_ELT to sign extension if its
579 // sign/zero-extension is completely overwritten by the new one performed by
580 // the ISD::SRA and ISD::SHL nodes.
581 // - Removes redundant sign extensions performed by an ISD::SRA and ISD::SHL
584 // See performDSPShiftCombine for more information about the transformation
586 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
587 TargetLowering::DAGCombinerInfo &DCI,
588 const MipsSubtarget *Subtarget) {
589 EVT Ty = N->getValueType(0);
591 if (Subtarget->hasMSA()) {
592 SDValue Op0 = N->getOperand(0);
593 SDValue Op1 = N->getOperand(1);
595 // (sra (shl (MipsVExtract[SZ]Ext $a, $b, $c), imm:$d), imm:$d)
596 // where $d + sizeof($c) == 32
597 // or $d + sizeof($c) <= 32 and SExt
598 // -> (MipsVExtractSExt $a, $b, $c)
599 if (Op0->getOpcode() == ISD::SHL && Op1 == Op0->getOperand(1)) {
600 SDValue Op0Op0 = Op0->getOperand(0);
601 ConstantSDNode *ShAmount = dyn_cast<ConstantSDNode>(Op1);
606 EVT ExtendTy = cast<VTSDNode>(Op0Op0->getOperand(2))->getVT();
607 unsigned TotalBits = ShAmount->getZExtValue() + ExtendTy.getSizeInBits();
609 if (TotalBits == 32 ||
610 (Op0Op0->getOpcode() == MipsISD::VEXTRACT_SEXT_ELT &&
612 SDValue Ops[] = { Op0Op0->getOperand(0), Op0Op0->getOperand(1),
613 Op0Op0->getOperand(2) };
614 DAG.MorphNodeTo(Op0Op0.getNode(), MipsISD::VEXTRACT_SEXT_ELT,
615 Op0Op0->getVTList(), Ops, Op0Op0->getNumOperands());
621 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
624 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
628 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
629 TargetLowering::DAGCombinerInfo &DCI,
630 const MipsSubtarget *Subtarget) {
631 EVT Ty = N->getValueType(0);
633 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
636 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
639 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
640 bool IsV216 = (Ty == MVT::v2i16);
644 case ISD::SETNE: return true;
648 case ISD::SETGE: return IsV216;
652 case ISD::SETUGE: return !IsV216;
653 default: return false;
657 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
658 EVT Ty = N->getValueType(0);
660 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
663 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
666 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
667 N->getOperand(1), N->getOperand(2));
670 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
671 EVT Ty = N->getValueType(0);
673 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
676 SDValue SetCC = N->getOperand(0);
678 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
681 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
682 SetCC.getOperand(0), SetCC.getOperand(1), N->getOperand(1),
683 N->getOperand(2), SetCC.getOperand(2));
686 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
687 const MipsSubtarget *Subtarget) {
688 EVT Ty = N->getValueType(0);
690 if (Subtarget->hasMSA() && Ty.is128BitVector() && Ty.isInteger()) {
691 // Try the following combines:
692 // (xor (or $a, $b), (build_vector allones))
693 // (xor (or $a, $b), (bitcast (build_vector allones)))
694 SDValue Op0 = N->getOperand(0);
695 SDValue Op1 = N->getOperand(1);
697 ConstantSDNode *Const;
699 if (ISD::isBuildVectorAllOnes(Op0.getNode()))
701 else if (ISD::isBuildVectorAllOnes(Op1.getNode()))
703 else if ((Op0->getOpcode() == MipsISD::VSPLAT ||
704 Op0->getOpcode() == MipsISD::VSPLATD) &&
705 (Const = dyn_cast<ConstantSDNode>(Op0->getOperand(0))) &&
706 Const->isAllOnesValue())
708 else if ((Op1->getOpcode() == MipsISD::VSPLAT ||
709 Op1->getOpcode() == MipsISD::VSPLATD) &&
710 (Const = dyn_cast<ConstantSDNode>(Op1->getOperand(0))) &&
711 Const->isAllOnesValue())
716 if (NotOp->getOpcode() == ISD::OR)
717 return DAG.getNode(MipsISD::VNOR, SDLoc(N), Ty, NotOp->getOperand(0),
718 NotOp->getOperand(1));
725 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
726 SelectionDAG &DAG = DCI.DAG;
729 switch (N->getOpcode()) {
731 return performADDECombine(N, DAG, DCI, Subtarget);
733 Val = performANDCombine(N, DAG, DCI, Subtarget);
736 return performSUBECombine(N, DAG, DCI, Subtarget);
738 return performMULCombine(N, DAG, DCI, this);
740 return performSHLCombine(N, DAG, DCI, Subtarget);
742 return performSRACombine(N, DAG, DCI, Subtarget);
744 return performSRLCombine(N, DAG, DCI, Subtarget);
746 return performVSELECTCombine(N, DAG);
748 Val = performXORCombine(N, DAG, Subtarget);
751 Val = performSETCCCombine(N, DAG);
758 return MipsTargetLowering::PerformDAGCombine(N, DCI);
762 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
763 MachineBasicBlock *BB) const {
764 switch (MI->getOpcode()) {
766 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
767 case Mips::BPOSGE32_PSEUDO:
768 return emitBPOSGE32(MI, BB);
769 case Mips::SNZ_B_PSEUDO:
770 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
771 case Mips::SNZ_H_PSEUDO:
772 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
773 case Mips::SNZ_W_PSEUDO:
774 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
775 case Mips::SNZ_D_PSEUDO:
776 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
777 case Mips::SNZ_V_PSEUDO:
778 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
779 case Mips::SZ_B_PSEUDO:
780 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
781 case Mips::SZ_H_PSEUDO:
782 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
783 case Mips::SZ_W_PSEUDO:
784 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
785 case Mips::SZ_D_PSEUDO:
786 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
787 case Mips::SZ_V_PSEUDO:
788 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
792 bool MipsSETargetLowering::
793 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
794 unsigned NextStackOffset,
795 const MipsFunctionInfo& FI) const {
796 if (!EnableMipsTailCalls)
799 // Return false if either the callee or caller has a byval argument.
800 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
803 // Return true if the callee's argument area is no larger than the
805 return NextStackOffset <= FI.getIncomingArgSize();
808 void MipsSETargetLowering::
809 getOpndList(SmallVectorImpl<SDValue> &Ops,
810 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
811 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
812 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
813 // T9 should contain the address of the callee function if
814 // -reloction-model=pic or it is an indirect call.
815 if (IsPICCall || !GlobalOrExternal) {
816 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
817 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
819 Ops.push_back(Callee);
821 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
822 InternalLinkage, CLI, Callee, Chain);
825 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
826 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
828 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
829 return MipsTargetLowering::lowerLOAD(Op, DAG);
831 // Replace a double precision load with two i32 loads and a buildpair64.
833 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
834 EVT PtrVT = Ptr.getValueType();
836 // i32 load from lower address.
837 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
838 MachinePointerInfo(), Nd.isVolatile(),
839 Nd.isNonTemporal(), Nd.isInvariant(),
842 // i32 load from higher address.
843 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
844 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
845 MachinePointerInfo(), Nd.isVolatile(),
846 Nd.isNonTemporal(), Nd.isInvariant(),
847 std::min(Nd.getAlignment(), 4U));
849 if (!Subtarget->isLittle())
852 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
853 SDValue Ops[2] = {BP, Hi.getValue(1)};
854 return DAG.getMergeValues(Ops, 2, DL);
857 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
858 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
860 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
861 return MipsTargetLowering::lowerSTORE(Op, DAG);
863 // Replace a double precision store with two extractelement64s and i32 stores.
865 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
866 EVT PtrVT = Ptr.getValueType();
867 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
868 Val, DAG.getConstant(0, MVT::i32));
869 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
870 Val, DAG.getConstant(1, MVT::i32));
872 if (!Subtarget->isLittle())
875 // i32 store to lower address.
876 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
877 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
880 // i32 store to higher address.
881 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
882 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
883 Nd.isVolatile(), Nd.isNonTemporal(),
884 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
887 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
888 bool HasLo, bool HasHi,
889 SelectionDAG &DAG) const {
890 EVT Ty = Op.getOperand(0).getValueType();
892 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
893 Op.getOperand(0), Op.getOperand(1));
897 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
898 DAG.getConstant(Mips::sub_lo, MVT::i32));
900 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
901 DAG.getConstant(Mips::sub_hi, MVT::i32));
903 if (!HasLo || !HasHi)
904 return HasLo ? Lo : Hi;
906 SDValue Vals[] = { Lo, Hi };
907 return DAG.getMergeValues(Vals, 2, DL);
911 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
912 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
913 DAG.getConstant(0, MVT::i32));
914 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
915 DAG.getConstant(1, MVT::i32));
916 return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
919 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
920 SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
921 DAG.getConstant(Mips::sub_lo, MVT::i32));
922 SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
923 DAG.getConstant(Mips::sub_hi, MVT::i32));
924 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
927 // This function expands mips intrinsic nodes which have 64-bit input operands
930 // out64 = intrinsic-node in64
932 // lo = copy (extract-element (in64, 0))
933 // hi = copy (extract-element (in64, 1))
934 // mips-specific-node
937 // out64 = merge-values (v0, v1)
939 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
941 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
942 SmallVector<SDValue, 3> Ops;
945 // See if Op has a chain input.
947 Ops.push_back(Op->getOperand(OpNo++));
949 // The next operand is the intrinsic opcode.
950 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
952 // See if the next operand has type i64.
953 SDValue Opnd = Op->getOperand(++OpNo), In64;
955 if (Opnd.getValueType() == MVT::i64)
956 In64 = initAccumulator(Opnd, DL, DAG);
960 // Push the remaining operands.
961 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
962 Ops.push_back(Op->getOperand(OpNo));
964 // Add In64 to the end of the list.
969 SmallVector<EVT, 2> ResTys;
971 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
973 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
976 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
977 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
982 assert(Val->getValueType(1) == MVT::Other);
983 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
984 return DAG.getMergeValues(Vals, 2, DL);
987 static SDValue lowerMSABinaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
989 SDValue LHS = Op->getOperand(1);
990 SDValue RHS = Op->getOperand(2);
991 EVT ResTy = Op->getValueType(0);
993 SDValue Result = DAG.getNode(Opc, DL, ResTy, LHS, RHS);
998 static SDValue lowerMSABinaryImmIntr(SDValue Op, SelectionDAG &DAG,
999 unsigned Opc, SDValue RHS) {
1000 SDValue LHS = Op->getOperand(1);
1001 EVT ResTy = Op->getValueType(0);
1003 return DAG.getNode(Opc, SDLoc(Op), ResTy, LHS, RHS);
1006 static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1008 SDValue Value = Op->getOperand(1);
1009 EVT ResTy = Op->getValueType(0);
1011 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1016 // Lower an MSA copy intrinsic into the specified SelectionDAG node
1017 static SDValue lowerMSACopyIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1019 SDValue Vec = Op->getOperand(1);
1020 SDValue Idx = Op->getOperand(2);
1021 EVT ResTy = Op->getValueType(0);
1022 EVT EltTy = Vec->getValueType(0).getVectorElementType();
1024 SDValue Result = DAG.getNode(Opc, DL, ResTy, Vec, Idx,
1025 DAG.getValueType(EltTy));
1030 // Lower an MSA insert intrinsic into the specified SelectionDAG node
1031 static SDValue lowerMSAInsertIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1033 SDValue Op0 = Op->getOperand(1);
1034 SDValue Op1 = Op->getOperand(2);
1035 SDValue Op2 = Op->getOperand(3);
1036 EVT ResTy = Op->getValueType(0);
1038 SDValue Result = DAG.getNode(Opc, DL, ResTy, Op0, Op2, Op1);
1043 static SDValue lowerMSASplatImm(SDValue Op, unsigned ImmOp, SelectionDAG &DAG) {
1044 EVT ResTy = Op->getValueType(0);
1046 unsigned SplatOp = MipsISD::VSPLAT;
1047 if (ResTy == MVT::v2i64)
1048 SplatOp = MipsISD::VSPLATD;
1050 return DAG.getNode(SplatOp, SDLoc(Op), ResTy, Op->getOperand(ImmOp));
1053 static SDValue lowerMSAUnaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
1055 SDValue Value = Op->getOperand(1);
1056 EVT ResTy = Op->getValueType(0);
1058 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
1063 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
1064 SelectionDAG &DAG) const {
1065 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
1068 case Intrinsic::mips_shilo:
1069 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
1070 case Intrinsic::mips_dpau_h_qbl:
1071 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
1072 case Intrinsic::mips_dpau_h_qbr:
1073 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
1074 case Intrinsic::mips_dpsu_h_qbl:
1075 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
1076 case Intrinsic::mips_dpsu_h_qbr:
1077 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
1078 case Intrinsic::mips_dpa_w_ph:
1079 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
1080 case Intrinsic::mips_dps_w_ph:
1081 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
1082 case Intrinsic::mips_dpax_w_ph:
1083 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
1084 case Intrinsic::mips_dpsx_w_ph:
1085 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
1086 case Intrinsic::mips_mulsa_w_ph:
1087 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
1088 case Intrinsic::mips_mult:
1089 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
1090 case Intrinsic::mips_multu:
1091 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
1092 case Intrinsic::mips_madd:
1093 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
1094 case Intrinsic::mips_maddu:
1095 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
1096 case Intrinsic::mips_msub:
1097 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
1098 case Intrinsic::mips_msubu:
1099 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
1100 case Intrinsic::mips_addv_b:
1101 case Intrinsic::mips_addv_h:
1102 case Intrinsic::mips_addv_w:
1103 case Intrinsic::mips_addv_d:
1104 return lowerMSABinaryIntr(Op, DAG, ISD::ADD);
1105 case Intrinsic::mips_addvi_b:
1106 case Intrinsic::mips_addvi_h:
1107 case Intrinsic::mips_addvi_w:
1108 case Intrinsic::mips_addvi_d:
1109 return lowerMSABinaryImmIntr(Op, DAG, ISD::ADD,
1110 lowerMSASplatImm(Op, 2, DAG));
1111 case Intrinsic::mips_and_v:
1112 return lowerMSABinaryIntr(Op, DAG, ISD::AND);
1113 case Intrinsic::mips_bnz_b:
1114 case Intrinsic::mips_bnz_h:
1115 case Intrinsic::mips_bnz_w:
1116 case Intrinsic::mips_bnz_d:
1117 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
1118 case Intrinsic::mips_bnz_v:
1119 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
1120 case Intrinsic::mips_bz_b:
1121 case Intrinsic::mips_bz_h:
1122 case Intrinsic::mips_bz_w:
1123 case Intrinsic::mips_bz_d:
1124 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
1125 case Intrinsic::mips_bz_v:
1126 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
1127 case Intrinsic::mips_ceq_b:
1128 case Intrinsic::mips_ceq_h:
1129 case Intrinsic::mips_ceq_w:
1130 case Intrinsic::mips_ceq_d:
1131 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1132 Op->getOperand(2), ISD::SETEQ);
1133 case Intrinsic::mips_ceqi_b:
1134 case Intrinsic::mips_ceqi_h:
1135 case Intrinsic::mips_ceqi_w:
1136 case Intrinsic::mips_ceqi_d:
1137 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1138 lowerMSASplatImm(Op, 2, DAG), ISD::SETEQ);
1139 case Intrinsic::mips_cle_s_b:
1140 case Intrinsic::mips_cle_s_h:
1141 case Intrinsic::mips_cle_s_w:
1142 case Intrinsic::mips_cle_s_d:
1143 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1144 Op->getOperand(2), ISD::SETLE);
1145 case Intrinsic::mips_clei_s_b:
1146 case Intrinsic::mips_clei_s_h:
1147 case Intrinsic::mips_clei_s_w:
1148 case Intrinsic::mips_clei_s_d:
1149 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1150 lowerMSASplatImm(Op, 2, DAG), ISD::SETLE);
1151 case Intrinsic::mips_cle_u_b:
1152 case Intrinsic::mips_cle_u_h:
1153 case Intrinsic::mips_cle_u_w:
1154 case Intrinsic::mips_cle_u_d:
1155 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1156 Op->getOperand(2), ISD::SETULE);
1157 case Intrinsic::mips_clei_u_b:
1158 case Intrinsic::mips_clei_u_h:
1159 case Intrinsic::mips_clei_u_w:
1160 case Intrinsic::mips_clei_u_d:
1161 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1162 lowerMSASplatImm(Op, 2, DAG), ISD::SETULE);
1163 case Intrinsic::mips_clt_s_b:
1164 case Intrinsic::mips_clt_s_h:
1165 case Intrinsic::mips_clt_s_w:
1166 case Intrinsic::mips_clt_s_d:
1167 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1168 Op->getOperand(2), ISD::SETLT);
1169 case Intrinsic::mips_clti_s_b:
1170 case Intrinsic::mips_clti_s_h:
1171 case Intrinsic::mips_clti_s_w:
1172 case Intrinsic::mips_clti_s_d:
1173 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1174 lowerMSASplatImm(Op, 2, DAG), ISD::SETLT);
1175 case Intrinsic::mips_clt_u_b:
1176 case Intrinsic::mips_clt_u_h:
1177 case Intrinsic::mips_clt_u_w:
1178 case Intrinsic::mips_clt_u_d:
1179 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1180 Op->getOperand(2), ISD::SETULT);
1181 case Intrinsic::mips_clti_u_b:
1182 case Intrinsic::mips_clti_u_h:
1183 case Intrinsic::mips_clti_u_w:
1184 case Intrinsic::mips_clti_u_d:
1185 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1186 lowerMSASplatImm(Op, 2, DAG), ISD::SETULT);
1187 case Intrinsic::mips_copy_s_b:
1188 case Intrinsic::mips_copy_s_h:
1189 case Intrinsic::mips_copy_s_w:
1190 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_SEXT_ELT);
1191 case Intrinsic::mips_copy_u_b:
1192 case Intrinsic::mips_copy_u_h:
1193 case Intrinsic::mips_copy_u_w:
1194 return lowerMSACopyIntr(Op, DAG, MipsISD::VEXTRACT_ZEXT_ELT);
1195 case Intrinsic::mips_div_s_b:
1196 case Intrinsic::mips_div_s_h:
1197 case Intrinsic::mips_div_s_w:
1198 case Intrinsic::mips_div_s_d:
1199 return lowerMSABinaryIntr(Op, DAG, ISD::SDIV);
1200 case Intrinsic::mips_div_u_b:
1201 case Intrinsic::mips_div_u_h:
1202 case Intrinsic::mips_div_u_w:
1203 case Intrinsic::mips_div_u_d:
1204 return lowerMSABinaryIntr(Op, DAG, ISD::UDIV);
1205 case Intrinsic::mips_fadd_w:
1206 case Intrinsic::mips_fadd_d:
1207 return lowerMSABinaryIntr(Op, DAG, ISD::FADD);
1208 // Don't lower mips_fcaf_[wd] since LLVM folds SETFALSE condcodes away
1209 case Intrinsic::mips_fceq_w:
1210 case Intrinsic::mips_fceq_d:
1211 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1212 Op->getOperand(2), ISD::SETOEQ);
1213 case Intrinsic::mips_fcle_w:
1214 case Intrinsic::mips_fcle_d:
1215 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1216 Op->getOperand(2), ISD::SETOLE);
1217 case Intrinsic::mips_fclt_w:
1218 case Intrinsic::mips_fclt_d:
1219 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1220 Op->getOperand(2), ISD::SETOLT);
1221 case Intrinsic::mips_fcne_w:
1222 case Intrinsic::mips_fcne_d:
1223 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1224 Op->getOperand(2), ISD::SETONE);
1225 case Intrinsic::mips_fcor_w:
1226 case Intrinsic::mips_fcor_d:
1227 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1228 Op->getOperand(2), ISD::SETO);
1229 case Intrinsic::mips_fcueq_w:
1230 case Intrinsic::mips_fcueq_d:
1231 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1232 Op->getOperand(2), ISD::SETUEQ);
1233 case Intrinsic::mips_fcule_w:
1234 case Intrinsic::mips_fcule_d:
1235 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1236 Op->getOperand(2), ISD::SETULE);
1237 case Intrinsic::mips_fcult_w:
1238 case Intrinsic::mips_fcult_d:
1239 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1240 Op->getOperand(2), ISD::SETULT);
1241 case Intrinsic::mips_fcun_w:
1242 case Intrinsic::mips_fcun_d:
1243 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1244 Op->getOperand(2), ISD::SETUO);
1245 case Intrinsic::mips_fcune_w:
1246 case Intrinsic::mips_fcune_d:
1247 return DAG.getSetCC(SDLoc(Op), Op->getValueType(0), Op->getOperand(1),
1248 Op->getOperand(2), ISD::SETUNE);
1249 case Intrinsic::mips_fdiv_w:
1250 case Intrinsic::mips_fdiv_d:
1251 return lowerMSABinaryIntr(Op, DAG, ISD::FDIV);
1252 case Intrinsic::mips_fill_b:
1253 case Intrinsic::mips_fill_h:
1254 case Intrinsic::mips_fill_w:
1255 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
1256 case Intrinsic::mips_flog2_w:
1257 case Intrinsic::mips_flog2_d:
1258 return lowerMSAUnaryIntr(Op, DAG, ISD::FLOG2);
1259 case Intrinsic::mips_fmul_w:
1260 case Intrinsic::mips_fmul_d:
1261 return lowerMSABinaryIntr(Op, DAG, ISD::FMUL);
1262 case Intrinsic::mips_frint_w:
1263 case Intrinsic::mips_frint_d:
1264 return lowerMSAUnaryIntr(Op, DAG, ISD::FRINT);
1265 case Intrinsic::mips_fsqrt_w:
1266 case Intrinsic::mips_fsqrt_d:
1267 return lowerMSAUnaryIntr(Op, DAG, ISD::FSQRT);
1268 case Intrinsic::mips_fsub_w:
1269 case Intrinsic::mips_fsub_d:
1270 return lowerMSABinaryIntr(Op, DAG, ISD::FSUB);
1271 case Intrinsic::mips_insert_b:
1272 case Intrinsic::mips_insert_h:
1273 case Intrinsic::mips_insert_w:
1274 return lowerMSAInsertIntr(Op, DAG, ISD::INSERT_VECTOR_ELT);
1275 case Intrinsic::mips_ldi_b:
1276 case Intrinsic::mips_ldi_h:
1277 case Intrinsic::mips_ldi_w:
1278 case Intrinsic::mips_ldi_d:
1279 return lowerMSAUnaryIntr(Op, DAG, MipsISD::VSPLAT);
1280 case Intrinsic::mips_mulv_b:
1281 case Intrinsic::mips_mulv_h:
1282 case Intrinsic::mips_mulv_w:
1283 case Intrinsic::mips_mulv_d:
1284 return lowerMSABinaryIntr(Op, DAG, ISD::MUL);
1285 case Intrinsic::mips_nlzc_b:
1286 case Intrinsic::mips_nlzc_h:
1287 case Intrinsic::mips_nlzc_w:
1288 case Intrinsic::mips_nlzc_d:
1289 return lowerMSAUnaryIntr(Op, DAG, ISD::CTLZ);
1290 case Intrinsic::mips_nor_v: {
1291 SDValue Res = lowerMSABinaryIntr(Op, DAG, ISD::OR);
1292 return DAG.getNOT(SDLoc(Op), Res, Res->getValueType(0));
1294 case Intrinsic::mips_or_v:
1295 return lowerMSABinaryIntr(Op, DAG, ISD::OR);
1296 case Intrinsic::mips_pcnt_b:
1297 case Intrinsic::mips_pcnt_h:
1298 case Intrinsic::mips_pcnt_w:
1299 case Intrinsic::mips_pcnt_d:
1300 return lowerMSAUnaryIntr(Op, DAG, ISD::CTPOP);
1301 case Intrinsic::mips_sll_b:
1302 case Intrinsic::mips_sll_h:
1303 case Intrinsic::mips_sll_w:
1304 case Intrinsic::mips_sll_d:
1305 return lowerMSABinaryIntr(Op, DAG, ISD::SHL);
1306 case Intrinsic::mips_slli_b:
1307 case Intrinsic::mips_slli_h:
1308 case Intrinsic::mips_slli_w:
1309 case Intrinsic::mips_slli_d:
1310 return lowerMSABinaryImmIntr(Op, DAG, ISD::SHL,
1311 lowerMSASplatImm(Op, 2, DAG));
1312 case Intrinsic::mips_sra_b:
1313 case Intrinsic::mips_sra_h:
1314 case Intrinsic::mips_sra_w:
1315 case Intrinsic::mips_sra_d:
1316 return lowerMSABinaryIntr(Op, DAG, ISD::SRA);
1317 case Intrinsic::mips_srai_b:
1318 case Intrinsic::mips_srai_h:
1319 case Intrinsic::mips_srai_w:
1320 case Intrinsic::mips_srai_d:
1321 return lowerMSABinaryImmIntr(Op, DAG, ISD::SRA,
1322 lowerMSASplatImm(Op, 2, DAG));
1323 case Intrinsic::mips_srl_b:
1324 case Intrinsic::mips_srl_h:
1325 case Intrinsic::mips_srl_w:
1326 case Intrinsic::mips_srl_d:
1327 return lowerMSABinaryIntr(Op, DAG, ISD::SRL);
1328 case Intrinsic::mips_srli_b:
1329 case Intrinsic::mips_srli_h:
1330 case Intrinsic::mips_srli_w:
1331 case Intrinsic::mips_srli_d:
1332 return lowerMSABinaryImmIntr(Op, DAG, ISD::SRL,
1333 lowerMSASplatImm(Op, 2, DAG));
1334 case Intrinsic::mips_subv_b:
1335 case Intrinsic::mips_subv_h:
1336 case Intrinsic::mips_subv_w:
1337 case Intrinsic::mips_subv_d:
1338 return lowerMSABinaryIntr(Op, DAG, ISD::SUB);
1339 case Intrinsic::mips_subvi_b:
1340 case Intrinsic::mips_subvi_h:
1341 case Intrinsic::mips_subvi_w:
1342 case Intrinsic::mips_subvi_d:
1343 return lowerMSABinaryImmIntr(Op, DAG, ISD::SUB,
1344 lowerMSASplatImm(Op, 2, DAG));
1345 case Intrinsic::mips_xor_v:
1346 return lowerMSABinaryIntr(Op, DAG, ISD::XOR);
1350 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1352 SDValue ChainIn = Op->getOperand(0);
1353 SDValue Address = Op->getOperand(2);
1354 SDValue Offset = Op->getOperand(3);
1355 EVT ResTy = Op->getValueType(0);
1356 EVT PtrTy = Address->getValueType(0);
1358 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1360 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
1364 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
1365 SelectionDAG &DAG) const {
1366 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1370 case Intrinsic::mips_extp:
1371 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
1372 case Intrinsic::mips_extpdp:
1373 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
1374 case Intrinsic::mips_extr_w:
1375 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
1376 case Intrinsic::mips_extr_r_w:
1377 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
1378 case Intrinsic::mips_extr_rs_w:
1379 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
1380 case Intrinsic::mips_extr_s_h:
1381 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
1382 case Intrinsic::mips_mthlip:
1383 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
1384 case Intrinsic::mips_mulsaq_s_w_ph:
1385 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
1386 case Intrinsic::mips_maq_s_w_phl:
1387 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
1388 case Intrinsic::mips_maq_s_w_phr:
1389 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
1390 case Intrinsic::mips_maq_sa_w_phl:
1391 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
1392 case Intrinsic::mips_maq_sa_w_phr:
1393 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
1394 case Intrinsic::mips_dpaq_s_w_ph:
1395 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
1396 case Intrinsic::mips_dpsq_s_w_ph:
1397 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
1398 case Intrinsic::mips_dpaq_sa_l_w:
1399 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
1400 case Intrinsic::mips_dpsq_sa_l_w:
1401 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
1402 case Intrinsic::mips_dpaqx_s_w_ph:
1403 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
1404 case Intrinsic::mips_dpaqx_sa_w_ph:
1405 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
1406 case Intrinsic::mips_dpsqx_s_w_ph:
1407 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
1408 case Intrinsic::mips_dpsqx_sa_w_ph:
1409 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
1410 case Intrinsic::mips_ld_b:
1411 case Intrinsic::mips_ld_h:
1412 case Intrinsic::mips_ld_w:
1413 case Intrinsic::mips_ld_d:
1414 case Intrinsic::mips_ldx_b:
1415 case Intrinsic::mips_ldx_h:
1416 case Intrinsic::mips_ldx_w:
1417 case Intrinsic::mips_ldx_d:
1418 return lowerMSALoadIntr(Op, DAG, Intr);
1422 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
1424 SDValue ChainIn = Op->getOperand(0);
1425 SDValue Value = Op->getOperand(2);
1426 SDValue Address = Op->getOperand(3);
1427 SDValue Offset = Op->getOperand(4);
1428 EVT PtrTy = Address->getValueType(0);
1430 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
1432 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
1436 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
1437 SelectionDAG &DAG) const {
1438 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1442 case Intrinsic::mips_st_b:
1443 case Intrinsic::mips_st_h:
1444 case Intrinsic::mips_st_w:
1445 case Intrinsic::mips_st_d:
1446 case Intrinsic::mips_stx_b:
1447 case Intrinsic::mips_stx_h:
1448 case Intrinsic::mips_stx_w:
1449 case Intrinsic::mips_stx_d:
1450 return lowerMSAStoreIntr(Op, DAG, Intr);
1454 /// \brief Check if the given BuildVectorSDNode is a splat.
1455 /// This method currently relies on DAG nodes being reused when equivalent,
1456 /// so it's possible for this to return false even when isConstantSplat returns
1458 static bool isSplatVector(const BuildVectorSDNode *N) {
1459 unsigned int nOps = N->getNumOperands();
1460 assert(nOps > 1 && "isSplat has 0 or 1 sized build vector");
1462 SDValue Operand0 = N->getOperand(0);
1464 for (unsigned int i = 1; i < nOps; ++i) {
1465 if (N->getOperand(i) != Operand0)
1472 // Lower ISD::EXTRACT_VECTOR_ELT into MipsISD::VEXTRACT_SEXT_ELT.
1474 // The non-value bits resulting from ISD::EXTRACT_VECTOR_ELT are undefined. We
1475 // choose to sign-extend but we could have equally chosen zero-extend. The
1476 // DAGCombiner will fold any sign/zero extension of the ISD::EXTRACT_VECTOR_ELT
1477 // result into this node later (possibly changing it to a zero-extend in the
1479 SDValue MipsSETargetLowering::
1480 lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
1482 EVT ResTy = Op->getValueType(0);
1483 SDValue Op0 = Op->getOperand(0);
1484 SDValue Op1 = Op->getOperand(1);
1485 EVT EltTy = Op0->getValueType(0).getVectorElementType();
1486 return DAG.getNode(MipsISD::VEXTRACT_SEXT_ELT, DL, ResTy, Op0, Op1,
1487 DAG.getValueType(EltTy));
1490 // Lowers ISD::BUILD_VECTOR into appropriate SelectionDAG nodes for the
1493 // Lowers according to the following rules:
1494 // - Vectors of 128-bits may be legal subject to the other rules. Other sizes
1496 // - Non-constant splats are legal and are lowered to MipsISD::VSPLAT.
1497 // - Constant splats with an element size of 32-bits or less are legal and are
1498 // lowered to MipsISD::VSPLAT.
1499 // - Constant splats with an element size of 64-bits but whose value would fit
1500 // within a 10 bit immediate are legal and are lowered to MipsISD::VSPLATD.
1501 // - All other ISD::BUILD_VECTORS are not legal
1502 SDValue MipsSETargetLowering::lowerBUILD_VECTOR(SDValue Op,
1503 SelectionDAG &DAG) const {
1504 BuildVectorSDNode *Node = cast<BuildVectorSDNode>(Op);
1505 EVT ResTy = Op->getValueType(0);
1507 APInt SplatValue, SplatUndef;
1508 unsigned SplatBitSize;
1511 if (!Subtarget->hasMSA() || !ResTy.is128BitVector())
1514 if (Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1516 !Subtarget->isLittle())) {
1519 EVT ConstTy = MVT::i32;
1520 unsigned SplatOp = MipsISD::VSPLAT;
1522 switch (SplatBitSize) {
1526 TmpVecTy = MVT::v2i64;
1528 // i64 is an illegal type on Mips32, but if it the constant fits into a
1529 // signed 10-bit value then we can still handle it using VSPLATD and an
1533 else if (isInt<10>(SplatValue.getSExtValue())) {
1534 SplatValue = SplatValue.trunc(32);
1535 SplatOp = MipsISD::VSPLATD;
1540 TmpVecTy = MVT::v4i32;
1543 TmpVecTy = MVT::v8i16;
1544 SplatValue = SplatValue.sext(32);
1547 TmpVecTy = MVT::v16i8;
1548 SplatValue = SplatValue.sext(32);
1552 Result = DAG.getNode(SplatOp, DL, TmpVecTy,
1553 DAG.getConstant(SplatValue, ConstTy));
1554 if (ResTy != Result.getValueType())
1555 Result = DAG.getNode(ISD::BITCAST, DL, ResTy, Result);
1559 else if (isSplatVector(Node))
1560 return DAG.getNode(MipsISD::VSPLAT, DL, ResTy, Op->getOperand(0));
1565 MachineBasicBlock * MipsSETargetLowering::
1566 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
1568 // bposge32_pseudo $vr0
1578 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1580 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1581 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1582 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1583 DebugLoc DL = MI->getDebugLoc();
1584 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1585 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1586 MachineFunction *F = BB->getParent();
1587 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1588 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1589 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1592 F->insert(It, Sink);
1594 // Transfer the remainder of BB and its successor edges to Sink.
1595 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1597 Sink->transferSuccessorsAndUpdatePHIs(BB);
1600 BB->addSuccessor(FBB);
1601 BB->addSuccessor(TBB);
1602 FBB->addSuccessor(Sink);
1603 TBB->addSuccessor(Sink);
1605 // Insert the real bposge32 instruction to $BB.
1606 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1609 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1610 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1611 .addReg(Mips::ZERO).addImm(0);
1612 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1615 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1616 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1617 .addReg(Mips::ZERO).addImm(1);
1619 // Insert phi function to $Sink.
1620 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1621 MI->getOperand(0).getReg())
1622 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1624 MI->eraseFromParent(); // The pseudo instruction is gone now.
1628 MachineBasicBlock * MipsSETargetLowering::
1629 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1630 unsigned BranchOp) const{
1632 // vany_nonzero $rd, $ws
1643 // $rd = phi($rd1, $fbb, $rd2, $tbb)
1645 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1646 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1647 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1648 DebugLoc DL = MI->getDebugLoc();
1649 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1650 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1651 MachineFunction *F = BB->getParent();
1652 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1653 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1654 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1657 F->insert(It, Sink);
1659 // Transfer the remainder of BB and its successor edges to Sink.
1660 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1662 Sink->transferSuccessorsAndUpdatePHIs(BB);
1665 BB->addSuccessor(FBB);
1666 BB->addSuccessor(TBB);
1667 FBB->addSuccessor(Sink);
1668 TBB->addSuccessor(Sink);
1670 // Insert the real bnz.b instruction to $BB.
1671 BuildMI(BB, DL, TII->get(BranchOp))
1672 .addReg(MI->getOperand(1).getReg())
1676 unsigned RD1 = RegInfo.createVirtualRegister(RC);
1677 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1678 .addReg(Mips::ZERO).addImm(0);
1679 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1682 unsigned RD2 = RegInfo.createVirtualRegister(RC);
1683 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1684 .addReg(Mips::ZERO).addImm(1);
1686 // Insert phi function to $Sink.
1687 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1688 MI->getOperand(0).getReg())
1689 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1691 MI->eraseFromParent(); // The pseudo instruction is gone now.