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);
95 if (!Subtarget->mipsSEUsesSoftFloat()) {
96 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
98 // When dealing with single precision only, use libcalls
99 if (!Subtarget->isSingleFloat()) {
100 if (Subtarget->isFP64bit())
101 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
103 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
107 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
108 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
109 setOperationAction(ISD::MULHS, MVT::i32, Custom);
110 setOperationAction(ISD::MULHU, MVT::i32, Custom);
113 setOperationAction(ISD::MULHS, MVT::i64, Custom);
114 setOperationAction(ISD::MULHU, MVT::i64, Custom);
115 setOperationAction(ISD::MUL, MVT::i64, Custom);
118 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
119 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
121 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
122 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
123 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
124 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
125 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
126 setOperationAction(ISD::LOAD, MVT::i32, Custom);
127 setOperationAction(ISD::STORE, MVT::i32, Custom);
129 setTargetDAGCombine(ISD::ADDE);
130 setTargetDAGCombine(ISD::SUBE);
131 setTargetDAGCombine(ISD::MUL);
133 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
134 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
135 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
138 setOperationAction(ISD::LOAD, MVT::f64, Custom);
139 setOperationAction(ISD::STORE, MVT::f64, Custom);
142 computeRegisterProperties();
145 const MipsTargetLowering *
146 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
147 return new MipsSETargetLowering(TM);
150 void MipsSETargetLowering::
151 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
152 addRegisterClass(Ty, RC);
154 // Expand all builtin opcodes.
155 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
156 setOperationAction(Opc, Ty, Expand);
158 setOperationAction(ISD::BITCAST, Ty, Legal);
159 setOperationAction(ISD::LOAD, Ty, Legal);
160 setOperationAction(ISD::STORE, Ty, Legal);
162 setOperationAction(ISD::ADD, Ty, Legal);
165 void MipsSETargetLowering::
166 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
167 addRegisterClass(Ty, RC);
169 // Expand all builtin opcodes.
170 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
171 setOperationAction(Opc, Ty, Expand);
173 setOperationAction(ISD::LOAD, Ty, Legal);
174 setOperationAction(ISD::STORE, Ty, Legal);
175 setOperationAction(ISD::BITCAST, Ty, Legal);
179 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
180 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
193 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
194 SelectionDAG &DAG) const {
195 switch(Op.getOpcode()) {
196 case ISD::LOAD: return lowerLOAD(Op, DAG);
197 case ISD::STORE: return lowerSTORE(Op, DAG);
198 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
199 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
200 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
201 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
202 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
203 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
204 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
206 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
207 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
208 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
211 return MipsTargetLowering::LowerOperation(Op, DAG);
215 // Transforms a subgraph in CurDAG if the following pattern is found:
216 // (addc multLo, Lo0), (adde multHi, Hi0),
218 // multHi/Lo: product of multiplication
219 // Lo0: initial value of Lo register
220 // Hi0: initial value of Hi register
221 // Return true if pattern matching was successful.
222 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
223 // ADDENode's second operand must be a flag output of an ADDC node in order
224 // for the matching to be successful.
225 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
227 if (ADDCNode->getOpcode() != ISD::ADDC)
230 SDValue MultHi = ADDENode->getOperand(0);
231 SDValue MultLo = ADDCNode->getOperand(0);
232 SDNode *MultNode = MultHi.getNode();
233 unsigned MultOpc = MultHi.getOpcode();
235 // MultHi and MultLo must be generated by the same node,
236 if (MultLo.getNode() != MultNode)
239 // and it must be a multiplication.
240 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
243 // MultLo amd MultHi must be the first and second output of MultNode
245 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
248 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
249 // of the values of MultNode, in which case MultNode will be removed in later
251 // If there exist users other than ADDENode or ADDCNode, this function returns
252 // here, which will result in MultNode being mapped to a single MULT
253 // instruction node rather than a pair of MULT and MADD instructions being
255 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
260 // Initialize accumulator.
261 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
262 ADDCNode->getOperand(1),
263 ADDENode->getOperand(1));
265 // create MipsMAdd(u) node
266 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
268 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
269 MultNode->getOperand(0),// Factor 0
270 MultNode->getOperand(1),// Factor 1
273 // replace uses of adde and addc here
274 if (!SDValue(ADDCNode, 0).use_empty()) {
275 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
276 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
278 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
280 if (!SDValue(ADDENode, 0).use_empty()) {
281 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
282 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
284 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
291 // Transforms a subgraph in CurDAG if the following pattern is found:
292 // (addc Lo0, multLo), (sube Hi0, multHi),
294 // multHi/Lo: product of multiplication
295 // Lo0: initial value of Lo register
296 // Hi0: initial value of Hi register
297 // Return true if pattern matching was successful.
298 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
299 // SUBENode's second operand must be a flag output of an SUBC node in order
300 // for the matching to be successful.
301 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
303 if (SUBCNode->getOpcode() != ISD::SUBC)
306 SDValue MultHi = SUBENode->getOperand(1);
307 SDValue MultLo = SUBCNode->getOperand(1);
308 SDNode *MultNode = MultHi.getNode();
309 unsigned MultOpc = MultHi.getOpcode();
311 // MultHi and MultLo must be generated by the same node,
312 if (MultLo.getNode() != MultNode)
315 // and it must be a multiplication.
316 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
319 // MultLo amd MultHi must be the first and second output of MultNode
321 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
324 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
325 // of the values of MultNode, in which case MultNode will be removed in later
327 // If there exist users other than SUBENode or SUBCNode, this function returns
328 // here, which will result in MultNode being mapped to a single MULT
329 // instruction node rather than a pair of MULT and MSUB instructions being
331 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
336 // Initialize accumulator.
337 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
338 SUBCNode->getOperand(0),
339 SUBENode->getOperand(0));
341 // create MipsSub(u) node
342 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
344 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
345 MultNode->getOperand(0),// Factor 0
346 MultNode->getOperand(1),// Factor 1
349 // replace uses of sube and subc here
350 if (!SDValue(SUBCNode, 0).use_empty()) {
351 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
352 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
354 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
356 if (!SDValue(SUBENode, 0).use_empty()) {
357 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
358 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
360 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
366 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
367 TargetLowering::DAGCombinerInfo &DCI,
368 const MipsSubtarget *Subtarget) {
369 if (DCI.isBeforeLegalize())
372 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
374 return SDValue(N, 0);
379 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
380 TargetLowering::DAGCombinerInfo &DCI,
381 const MipsSubtarget *Subtarget) {
382 if (DCI.isBeforeLegalize())
385 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
387 return SDValue(N, 0);
392 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
393 EVT ShiftTy, SelectionDAG &DAG) {
394 // Clear the upper (64 - VT.sizeInBits) bits.
395 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
399 return DAG.getConstant(0, VT);
405 // If c is power of 2, return (shl x, log2(c)).
406 if (isPowerOf2_64(C))
407 return DAG.getNode(ISD::SHL, DL, VT, X,
408 DAG.getConstant(Log2_64(C), ShiftTy));
410 unsigned Log2Ceil = Log2_64_Ceil(C);
411 uint64_t Floor = 1LL << Log2_64(C);
412 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
414 // If |c - floor_c| <= |c - ceil_c|,
415 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
416 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
417 if (C - Floor <= Ceil - C) {
418 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
419 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
420 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
423 // If |c - floor_c| > |c - ceil_c|,
424 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
425 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
426 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
427 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
430 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
431 const TargetLowering::DAGCombinerInfo &DCI,
432 const MipsSETargetLowering *TL) {
433 EVT VT = N->getValueType(0);
435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
437 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
438 VT, TL->getScalarShiftAmountTy(VT), DAG);
440 return SDValue(N, 0);
443 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
445 const MipsSubtarget *Subtarget) {
446 // See if this is a vector splat immediate node.
447 APInt SplatValue, SplatUndef;
448 unsigned SplatBitSize;
450 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
451 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
454 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
455 EltSize, !Subtarget->isLittle()) ||
456 (SplatBitSize != EltSize) ||
457 (SplatValue.getZExtValue() >= EltSize))
460 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
461 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
464 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
465 TargetLowering::DAGCombinerInfo &DCI,
466 const MipsSubtarget *Subtarget) {
467 EVT Ty = N->getValueType(0);
469 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
472 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
475 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
476 TargetLowering::DAGCombinerInfo &DCI,
477 const MipsSubtarget *Subtarget) {
478 EVT Ty = N->getValueType(0);
480 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
483 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
487 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
488 TargetLowering::DAGCombinerInfo &DCI,
489 const MipsSubtarget *Subtarget) {
490 EVT Ty = N->getValueType(0);
492 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
495 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
498 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
499 bool IsV216 = (Ty == MVT::v2i16);
503 case ISD::SETNE: return true;
507 case ISD::SETGE: return IsV216;
511 case ISD::SETUGE: return !IsV216;
512 default: return false;
516 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
517 EVT Ty = N->getValueType(0);
519 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
522 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
525 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
526 N->getOperand(1), N->getOperand(2));
529 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
530 EVT Ty = N->getValueType(0);
532 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
535 SDValue SetCC = N->getOperand(0);
537 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
540 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
541 SetCC.getOperand(0), SetCC.getOperand(1), N->getOperand(1),
542 N->getOperand(2), SetCC.getOperand(2));
546 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
547 SelectionDAG &DAG = DCI.DAG;
550 switch (N->getOpcode()) {
552 return performADDECombine(N, DAG, DCI, Subtarget);
554 return performSUBECombine(N, DAG, DCI, Subtarget);
556 return performMULCombine(N, DAG, DCI, this);
558 return performSHLCombine(N, DAG, DCI, Subtarget);
560 return performSRACombine(N, DAG, DCI, Subtarget);
562 return performSRLCombine(N, DAG, DCI, Subtarget);
564 return performVSELECTCombine(N, DAG);
566 Val = performSETCCCombine(N, DAG);
574 return MipsTargetLowering::PerformDAGCombine(N, DCI);
578 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
579 MachineBasicBlock *BB) const {
580 switch (MI->getOpcode()) {
582 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
583 case Mips::BPOSGE32_PSEUDO:
584 return emitBPOSGE32(MI, BB);
585 case Mips::SNZ_B_PSEUDO:
586 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
587 case Mips::SNZ_H_PSEUDO:
588 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
589 case Mips::SNZ_W_PSEUDO:
590 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
591 case Mips::SNZ_D_PSEUDO:
592 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
593 case Mips::SNZ_V_PSEUDO:
594 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
595 case Mips::SZ_B_PSEUDO:
596 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
597 case Mips::SZ_H_PSEUDO:
598 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
599 case Mips::SZ_W_PSEUDO:
600 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
601 case Mips::SZ_D_PSEUDO:
602 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
603 case Mips::SZ_V_PSEUDO:
604 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
608 bool MipsSETargetLowering::
609 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
610 unsigned NextStackOffset,
611 const MipsFunctionInfo& FI) const {
612 if (!EnableMipsTailCalls)
615 // Return false if either the callee or caller has a byval argument.
616 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
619 // Return true if the callee's argument area is no larger than the
621 return NextStackOffset <= FI.getIncomingArgSize();
624 void MipsSETargetLowering::
625 getOpndList(SmallVectorImpl<SDValue> &Ops,
626 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
627 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
628 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
629 // T9 should contain the address of the callee function if
630 // -reloction-model=pic or it is an indirect call.
631 if (IsPICCall || !GlobalOrExternal) {
632 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
633 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
635 Ops.push_back(Callee);
637 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
638 InternalLinkage, CLI, Callee, Chain);
641 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
642 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
644 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
645 return MipsTargetLowering::lowerLOAD(Op, DAG);
647 // Replace a double precision load with two i32 loads and a buildpair64.
649 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
650 EVT PtrVT = Ptr.getValueType();
652 // i32 load from lower address.
653 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
654 MachinePointerInfo(), Nd.isVolatile(),
655 Nd.isNonTemporal(), Nd.isInvariant(),
658 // i32 load from higher address.
659 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
660 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
661 MachinePointerInfo(), Nd.isVolatile(),
662 Nd.isNonTemporal(), Nd.isInvariant(),
663 std::min(Nd.getAlignment(), 4U));
665 if (!Subtarget->isLittle())
668 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
669 SDValue Ops[2] = {BP, Hi.getValue(1)};
670 return DAG.getMergeValues(Ops, 2, DL);
673 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
674 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
676 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
677 return MipsTargetLowering::lowerSTORE(Op, DAG);
679 // Replace a double precision store with two extractelement64s and i32 stores.
681 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
682 EVT PtrVT = Ptr.getValueType();
683 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
684 Val, DAG.getConstant(0, MVT::i32));
685 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
686 Val, DAG.getConstant(1, MVT::i32));
688 if (!Subtarget->isLittle())
691 // i32 store to lower address.
692 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
693 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
696 // i32 store to higher address.
697 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
698 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
699 Nd.isVolatile(), Nd.isNonTemporal(),
700 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
703 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
704 bool HasLo, bool HasHi,
705 SelectionDAG &DAG) const {
706 EVT Ty = Op.getOperand(0).getValueType();
708 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
709 Op.getOperand(0), Op.getOperand(1));
713 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
714 DAG.getConstant(Mips::sub_lo, MVT::i32));
716 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
717 DAG.getConstant(Mips::sub_hi, MVT::i32));
719 if (!HasLo || !HasHi)
720 return HasLo ? Lo : Hi;
722 SDValue Vals[] = { Lo, Hi };
723 return DAG.getMergeValues(Vals, 2, DL);
727 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
728 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
729 DAG.getConstant(0, MVT::i32));
730 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
731 DAG.getConstant(1, MVT::i32));
732 return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
735 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
736 SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
737 DAG.getConstant(Mips::sub_lo, MVT::i32));
738 SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
739 DAG.getConstant(Mips::sub_hi, MVT::i32));
740 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
743 // This function expands mips intrinsic nodes which have 64-bit input operands
746 // out64 = intrinsic-node in64
748 // lo = copy (extract-element (in64, 0))
749 // hi = copy (extract-element (in64, 1))
750 // mips-specific-node
753 // out64 = merge-values (v0, v1)
755 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
757 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
758 SmallVector<SDValue, 3> Ops;
761 // See if Op has a chain input.
763 Ops.push_back(Op->getOperand(OpNo++));
765 // The next operand is the intrinsic opcode.
766 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
768 // See if the next operand has type i64.
769 SDValue Opnd = Op->getOperand(++OpNo), In64;
771 if (Opnd.getValueType() == MVT::i64)
772 In64 = initAccumulator(Opnd, DL, DAG);
776 // Push the remaining operands.
777 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
778 Ops.push_back(Op->getOperand(OpNo));
780 // Add In64 to the end of the list.
785 SmallVector<EVT, 2> ResTys;
787 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
789 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
792 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
793 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
798 assert(Val->getValueType(1) == MVT::Other);
799 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
800 return DAG.getMergeValues(Vals, 2, DL);
803 static SDValue lowerMSABinaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
805 SDValue LHS = Op->getOperand(1);
806 SDValue RHS = Op->getOperand(2);
807 EVT ResTy = Op->getValueType(0);
809 SDValue Result = DAG.getNode(Opc, DL, ResTy, LHS, RHS);
814 static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
816 SDValue Value = Op->getOperand(1);
817 EVT ResTy = Op->getValueType(0);
819 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
824 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
825 SelectionDAG &DAG) const {
826 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
829 case Intrinsic::mips_shilo:
830 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
831 case Intrinsic::mips_dpau_h_qbl:
832 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
833 case Intrinsic::mips_dpau_h_qbr:
834 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
835 case Intrinsic::mips_dpsu_h_qbl:
836 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
837 case Intrinsic::mips_dpsu_h_qbr:
838 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
839 case Intrinsic::mips_dpa_w_ph:
840 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
841 case Intrinsic::mips_dps_w_ph:
842 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
843 case Intrinsic::mips_dpax_w_ph:
844 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
845 case Intrinsic::mips_dpsx_w_ph:
846 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
847 case Intrinsic::mips_mulsa_w_ph:
848 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
849 case Intrinsic::mips_mult:
850 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
851 case Intrinsic::mips_multu:
852 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
853 case Intrinsic::mips_madd:
854 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
855 case Intrinsic::mips_maddu:
856 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
857 case Intrinsic::mips_msub:
858 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
859 case Intrinsic::mips_msubu:
860 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
861 case Intrinsic::mips_addv_b:
862 case Intrinsic::mips_addv_h:
863 case Intrinsic::mips_addv_w:
864 case Intrinsic::mips_addv_d:
865 return lowerMSABinaryIntr(Op, DAG, ISD::ADD);
866 case Intrinsic::mips_bnz_b:
867 case Intrinsic::mips_bnz_h:
868 case Intrinsic::mips_bnz_w:
869 case Intrinsic::mips_bnz_d:
870 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
871 case Intrinsic::mips_bnz_v:
872 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
873 case Intrinsic::mips_bz_b:
874 case Intrinsic::mips_bz_h:
875 case Intrinsic::mips_bz_w:
876 case Intrinsic::mips_bz_d:
877 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
878 case Intrinsic::mips_bz_v:
879 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
883 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
885 SDValue ChainIn = Op->getOperand(0);
886 SDValue Address = Op->getOperand(2);
887 SDValue Offset = Op->getOperand(3);
888 EVT ResTy = Op->getValueType(0);
889 EVT PtrTy = Address->getValueType(0);
891 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
893 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
897 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
898 SelectionDAG &DAG) const {
899 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
903 case Intrinsic::mips_extp:
904 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
905 case Intrinsic::mips_extpdp:
906 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
907 case Intrinsic::mips_extr_w:
908 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
909 case Intrinsic::mips_extr_r_w:
910 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
911 case Intrinsic::mips_extr_rs_w:
912 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
913 case Intrinsic::mips_extr_s_h:
914 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
915 case Intrinsic::mips_mthlip:
916 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
917 case Intrinsic::mips_mulsaq_s_w_ph:
918 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
919 case Intrinsic::mips_maq_s_w_phl:
920 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
921 case Intrinsic::mips_maq_s_w_phr:
922 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
923 case Intrinsic::mips_maq_sa_w_phl:
924 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
925 case Intrinsic::mips_maq_sa_w_phr:
926 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
927 case Intrinsic::mips_dpaq_s_w_ph:
928 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
929 case Intrinsic::mips_dpsq_s_w_ph:
930 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
931 case Intrinsic::mips_dpaq_sa_l_w:
932 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
933 case Intrinsic::mips_dpsq_sa_l_w:
934 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
935 case Intrinsic::mips_dpaqx_s_w_ph:
936 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
937 case Intrinsic::mips_dpaqx_sa_w_ph:
938 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
939 case Intrinsic::mips_dpsqx_s_w_ph:
940 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
941 case Intrinsic::mips_dpsqx_sa_w_ph:
942 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
943 case Intrinsic::mips_ld_b:
944 case Intrinsic::mips_ld_h:
945 case Intrinsic::mips_ld_w:
946 case Intrinsic::mips_ld_d:
947 case Intrinsic::mips_ldx_b:
948 case Intrinsic::mips_ldx_h:
949 case Intrinsic::mips_ldx_w:
950 case Intrinsic::mips_ldx_d:
951 return lowerMSALoadIntr(Op, DAG, Intr);
955 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
957 SDValue ChainIn = Op->getOperand(0);
958 SDValue Value = Op->getOperand(2);
959 SDValue Address = Op->getOperand(3);
960 SDValue Offset = Op->getOperand(4);
961 EVT PtrTy = Address->getValueType(0);
963 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
965 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
969 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
970 SelectionDAG &DAG) const {
971 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
975 case Intrinsic::mips_st_b:
976 case Intrinsic::mips_st_h:
977 case Intrinsic::mips_st_w:
978 case Intrinsic::mips_st_d:
979 case Intrinsic::mips_stx_b:
980 case Intrinsic::mips_stx_h:
981 case Intrinsic::mips_stx_w:
982 case Intrinsic::mips_stx_d:
983 return lowerMSAStoreIntr(Op, DAG, Intr);
987 MachineBasicBlock * MipsSETargetLowering::
988 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
990 // bposge32_pseudo $vr0
1000 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1002 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1003 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1004 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1005 DebugLoc DL = MI->getDebugLoc();
1006 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1007 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1008 MachineFunction *F = BB->getParent();
1009 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1010 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1011 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1014 F->insert(It, Sink);
1016 // Transfer the remainder of BB and its successor edges to Sink.
1017 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1019 Sink->transferSuccessorsAndUpdatePHIs(BB);
1022 BB->addSuccessor(FBB);
1023 BB->addSuccessor(TBB);
1024 FBB->addSuccessor(Sink);
1025 TBB->addSuccessor(Sink);
1027 // Insert the real bposge32 instruction to $BB.
1028 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1031 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1032 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1033 .addReg(Mips::ZERO).addImm(0);
1034 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1037 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1038 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1039 .addReg(Mips::ZERO).addImm(1);
1041 // Insert phi function to $Sink.
1042 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1043 MI->getOperand(0).getReg())
1044 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1046 MI->eraseFromParent(); // The pseudo instruction is gone now.
1050 MachineBasicBlock * MipsSETargetLowering::
1051 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1052 unsigned BranchOp) const{
1054 // vany_nonzero $rd, $ws
1065 // $rd = phi($rd1, $fbb, $rd2, $tbb)
1067 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1068 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1069 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1070 DebugLoc DL = MI->getDebugLoc();
1071 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1072 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1073 MachineFunction *F = BB->getParent();
1074 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1075 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1076 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1079 F->insert(It, Sink);
1081 // Transfer the remainder of BB and its successor edges to Sink.
1082 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1084 Sink->transferSuccessorsAndUpdatePHIs(BB);
1087 BB->addSuccessor(FBB);
1088 BB->addSuccessor(TBB);
1089 FBB->addSuccessor(Sink);
1090 TBB->addSuccessor(Sink);
1092 // Insert the real bnz.b instruction to $BB.
1093 BuildMI(BB, DL, TII->get(BranchOp))
1094 .addReg(MI->getOperand(1).getReg())
1098 unsigned RD1 = RegInfo.createVirtualRegister(RC);
1099 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1100 .addReg(Mips::ZERO).addImm(0);
1101 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1104 unsigned RD2 = RegInfo.createVirtualRegister(RC);
1105 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1106 .addReg(Mips::ZERO).addImm(1);
1108 // Insert phi function to $Sink.
1109 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1110 MI->getOperand(0).getReg())
1111 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1113 MI->eraseFromParent(); // The pseudo instruction is gone now.