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);
164 void MipsSETargetLowering::
165 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
166 addRegisterClass(Ty, RC);
168 // Expand all builtin opcodes.
169 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
170 setOperationAction(Opc, Ty, Expand);
172 setOperationAction(ISD::LOAD, Ty, Legal);
173 setOperationAction(ISD::STORE, Ty, Legal);
174 setOperationAction(ISD::BITCAST, Ty, Legal);
178 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
179 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
192 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
193 SelectionDAG &DAG) const {
194 switch(Op.getOpcode()) {
195 case ISD::LOAD: return lowerLOAD(Op, DAG);
196 case ISD::STORE: return lowerSTORE(Op, DAG);
197 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
198 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
199 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
200 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
201 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
202 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
203 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
205 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
206 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
207 case ISD::INTRINSIC_VOID: return lowerINTRINSIC_VOID(Op, DAG);
210 return MipsTargetLowering::LowerOperation(Op, DAG);
214 // Transforms a subgraph in CurDAG if the following pattern is found:
215 // (addc multLo, Lo0), (adde multHi, Hi0),
217 // multHi/Lo: product of multiplication
218 // Lo0: initial value of Lo register
219 // Hi0: initial value of Hi register
220 // Return true if pattern matching was successful.
221 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
222 // ADDENode's second operand must be a flag output of an ADDC node in order
223 // for the matching to be successful.
224 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
226 if (ADDCNode->getOpcode() != ISD::ADDC)
229 SDValue MultHi = ADDENode->getOperand(0);
230 SDValue MultLo = ADDCNode->getOperand(0);
231 SDNode *MultNode = MultHi.getNode();
232 unsigned MultOpc = MultHi.getOpcode();
234 // MultHi and MultLo must be generated by the same node,
235 if (MultLo.getNode() != MultNode)
238 // and it must be a multiplication.
239 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
242 // MultLo amd MultHi must be the first and second output of MultNode
244 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
247 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
248 // of the values of MultNode, in which case MultNode will be removed in later
250 // If there exist users other than ADDENode or ADDCNode, this function returns
251 // here, which will result in MultNode being mapped to a single MULT
252 // instruction node rather than a pair of MULT and MADD instructions being
254 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
259 // Initialize accumulator.
260 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
261 ADDCNode->getOperand(1),
262 ADDENode->getOperand(1));
264 // create MipsMAdd(u) node
265 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
267 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
268 MultNode->getOperand(0),// Factor 0
269 MultNode->getOperand(1),// Factor 1
272 // replace uses of adde and addc here
273 if (!SDValue(ADDCNode, 0).use_empty()) {
274 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
275 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
277 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
279 if (!SDValue(ADDENode, 0).use_empty()) {
280 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
281 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
283 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
290 // Transforms a subgraph in CurDAG if the following pattern is found:
291 // (addc Lo0, multLo), (sube Hi0, multHi),
293 // multHi/Lo: product of multiplication
294 // Lo0: initial value of Lo register
295 // Hi0: initial value of Hi register
296 // Return true if pattern matching was successful.
297 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
298 // SUBENode's second operand must be a flag output of an SUBC node in order
299 // for the matching to be successful.
300 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
302 if (SUBCNode->getOpcode() != ISD::SUBC)
305 SDValue MultHi = SUBENode->getOperand(1);
306 SDValue MultLo = SUBCNode->getOperand(1);
307 SDNode *MultNode = MultHi.getNode();
308 unsigned MultOpc = MultHi.getOpcode();
310 // MultHi and MultLo must be generated by the same node,
311 if (MultLo.getNode() != MultNode)
314 // and it must be a multiplication.
315 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
318 // MultLo amd MultHi must be the first and second output of MultNode
320 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
323 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
324 // of the values of MultNode, in which case MultNode will be removed in later
326 // If there exist users other than SUBENode or SUBCNode, this function returns
327 // here, which will result in MultNode being mapped to a single MULT
328 // instruction node rather than a pair of MULT and MSUB instructions being
330 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
335 // Initialize accumulator.
336 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
337 SUBCNode->getOperand(0),
338 SUBENode->getOperand(0));
340 // create MipsSub(u) node
341 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
343 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
344 MultNode->getOperand(0),// Factor 0
345 MultNode->getOperand(1),// Factor 1
348 // replace uses of sube and subc here
349 if (!SDValue(SUBCNode, 0).use_empty()) {
350 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
351 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
353 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
355 if (!SDValue(SUBENode, 0).use_empty()) {
356 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
357 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
359 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
365 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
366 TargetLowering::DAGCombinerInfo &DCI,
367 const MipsSubtarget *Subtarget) {
368 if (DCI.isBeforeLegalize())
371 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
373 return SDValue(N, 0);
378 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
379 TargetLowering::DAGCombinerInfo &DCI,
380 const MipsSubtarget *Subtarget) {
381 if (DCI.isBeforeLegalize())
384 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
386 return SDValue(N, 0);
391 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
392 EVT ShiftTy, SelectionDAG &DAG) {
393 // Clear the upper (64 - VT.sizeInBits) bits.
394 C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
398 return DAG.getConstant(0, VT);
404 // If c is power of 2, return (shl x, log2(c)).
405 if (isPowerOf2_64(C))
406 return DAG.getNode(ISD::SHL, DL, VT, X,
407 DAG.getConstant(Log2_64(C), ShiftTy));
409 unsigned Log2Ceil = Log2_64_Ceil(C);
410 uint64_t Floor = 1LL << Log2_64(C);
411 uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
413 // If |c - floor_c| <= |c - ceil_c|,
414 // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
415 // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
416 if (C - Floor <= Ceil - C) {
417 SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
418 SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
419 return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
422 // If |c - floor_c| > |c - ceil_c|,
423 // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
424 SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
425 SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
426 return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
429 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
430 const TargetLowering::DAGCombinerInfo &DCI,
431 const MipsSETargetLowering *TL) {
432 EVT VT = N->getValueType(0);
434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
436 return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
437 VT, TL->getScalarShiftAmountTy(VT), DAG);
439 return SDValue(N, 0);
442 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
444 const MipsSubtarget *Subtarget) {
445 // See if this is a vector splat immediate node.
446 APInt SplatValue, SplatUndef;
447 unsigned SplatBitSize;
449 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
450 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
453 !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
454 EltSize, !Subtarget->isLittle()) ||
455 (SplatBitSize != EltSize) ||
456 (SplatValue.getZExtValue() >= EltSize))
459 return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
460 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
463 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
464 TargetLowering::DAGCombinerInfo &DCI,
465 const MipsSubtarget *Subtarget) {
466 EVT Ty = N->getValueType(0);
468 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
471 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
474 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
475 TargetLowering::DAGCombinerInfo &DCI,
476 const MipsSubtarget *Subtarget) {
477 EVT Ty = N->getValueType(0);
479 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
482 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
486 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
487 TargetLowering::DAGCombinerInfo &DCI,
488 const MipsSubtarget *Subtarget) {
489 EVT Ty = N->getValueType(0);
491 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
494 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
497 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
498 bool IsV216 = (Ty == MVT::v2i16);
502 case ISD::SETNE: return true;
506 case ISD::SETGE: return IsV216;
510 case ISD::SETUGE: return !IsV216;
511 default: return false;
515 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
516 EVT Ty = N->getValueType(0);
518 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
521 if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
524 return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
525 N->getOperand(1), N->getOperand(2));
528 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
529 EVT Ty = N->getValueType(0);
531 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
534 SDValue SetCC = N->getOperand(0);
536 if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
539 return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
540 SetCC.getOperand(0), SetCC.getOperand(1), N->getOperand(1),
541 N->getOperand(2), SetCC.getOperand(2));
545 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
546 SelectionDAG &DAG = DCI.DAG;
549 switch (N->getOpcode()) {
551 return performADDECombine(N, DAG, DCI, Subtarget);
553 return performSUBECombine(N, DAG, DCI, Subtarget);
555 return performMULCombine(N, DAG, DCI, this);
557 return performSHLCombine(N, DAG, DCI, Subtarget);
559 return performSRACombine(N, DAG, DCI, Subtarget);
561 return performSRLCombine(N, DAG, DCI, Subtarget);
563 return performVSELECTCombine(N, DAG);
565 Val = performSETCCCombine(N, DAG);
573 return MipsTargetLowering::PerformDAGCombine(N, DCI);
577 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
578 MachineBasicBlock *BB) const {
579 switch (MI->getOpcode()) {
581 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
582 case Mips::BPOSGE32_PSEUDO:
583 return emitBPOSGE32(MI, BB);
584 case Mips::SNZ_B_PSEUDO:
585 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
586 case Mips::SNZ_H_PSEUDO:
587 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
588 case Mips::SNZ_W_PSEUDO:
589 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
590 case Mips::SNZ_D_PSEUDO:
591 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
592 case Mips::SNZ_V_PSEUDO:
593 return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
594 case Mips::SZ_B_PSEUDO:
595 return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
596 case Mips::SZ_H_PSEUDO:
597 return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
598 case Mips::SZ_W_PSEUDO:
599 return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
600 case Mips::SZ_D_PSEUDO:
601 return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
602 case Mips::SZ_V_PSEUDO:
603 return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
607 bool MipsSETargetLowering::
608 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
609 unsigned NextStackOffset,
610 const MipsFunctionInfo& FI) const {
611 if (!EnableMipsTailCalls)
614 // Return false if either the callee or caller has a byval argument.
615 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
618 // Return true if the callee's argument area is no larger than the
620 return NextStackOffset <= FI.getIncomingArgSize();
623 void MipsSETargetLowering::
624 getOpndList(SmallVectorImpl<SDValue> &Ops,
625 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
626 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
627 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
628 // T9 should contain the address of the callee function if
629 // -reloction-model=pic or it is an indirect call.
630 if (IsPICCall || !GlobalOrExternal) {
631 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
632 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
634 Ops.push_back(Callee);
636 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
637 InternalLinkage, CLI, Callee, Chain);
640 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
641 LoadSDNode &Nd = *cast<LoadSDNode>(Op);
643 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
644 return MipsTargetLowering::lowerLOAD(Op, DAG);
646 // Replace a double precision load with two i32 loads and a buildpair64.
648 SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
649 EVT PtrVT = Ptr.getValueType();
651 // i32 load from lower address.
652 SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
653 MachinePointerInfo(), Nd.isVolatile(),
654 Nd.isNonTemporal(), Nd.isInvariant(),
657 // i32 load from higher address.
658 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
659 SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
660 MachinePointerInfo(), Nd.isVolatile(),
661 Nd.isNonTemporal(), Nd.isInvariant(),
662 std::min(Nd.getAlignment(), 4U));
664 if (!Subtarget->isLittle())
667 SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
668 SDValue Ops[2] = {BP, Hi.getValue(1)};
669 return DAG.getMergeValues(Ops, 2, DL);
672 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
673 StoreSDNode &Nd = *cast<StoreSDNode>(Op);
675 if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
676 return MipsTargetLowering::lowerSTORE(Op, DAG);
678 // Replace a double precision store with two extractelement64s and i32 stores.
680 SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
681 EVT PtrVT = Ptr.getValueType();
682 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
683 Val, DAG.getConstant(0, MVT::i32));
684 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
685 Val, DAG.getConstant(1, MVT::i32));
687 if (!Subtarget->isLittle())
690 // i32 store to lower address.
691 Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
692 Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
695 // i32 store to higher address.
696 Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
697 return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
698 Nd.isVolatile(), Nd.isNonTemporal(),
699 std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
702 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
703 bool HasLo, bool HasHi,
704 SelectionDAG &DAG) const {
705 EVT Ty = Op.getOperand(0).getValueType();
707 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
708 Op.getOperand(0), Op.getOperand(1));
712 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
713 DAG.getConstant(Mips::sub_lo, MVT::i32));
715 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
716 DAG.getConstant(Mips::sub_hi, MVT::i32));
718 if (!HasLo || !HasHi)
719 return HasLo ? Lo : Hi;
721 SDValue Vals[] = { Lo, Hi };
722 return DAG.getMergeValues(Vals, 2, DL);
726 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
727 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
728 DAG.getConstant(0, MVT::i32));
729 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
730 DAG.getConstant(1, MVT::i32));
731 return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
734 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
735 SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
736 DAG.getConstant(Mips::sub_lo, MVT::i32));
737 SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
738 DAG.getConstant(Mips::sub_hi, MVT::i32));
739 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
742 // This function expands mips intrinsic nodes which have 64-bit input operands
745 // out64 = intrinsic-node in64
747 // lo = copy (extract-element (in64, 0))
748 // hi = copy (extract-element (in64, 1))
749 // mips-specific-node
752 // out64 = merge-values (v0, v1)
754 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
756 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
757 SmallVector<SDValue, 3> Ops;
760 // See if Op has a chain input.
762 Ops.push_back(Op->getOperand(OpNo++));
764 // The next operand is the intrinsic opcode.
765 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
767 // See if the next operand has type i64.
768 SDValue Opnd = Op->getOperand(++OpNo), In64;
770 if (Opnd.getValueType() == MVT::i64)
771 In64 = initAccumulator(Opnd, DL, DAG);
775 // Push the remaining operands.
776 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
777 Ops.push_back(Op->getOperand(OpNo));
779 // Add In64 to the end of the list.
784 SmallVector<EVT, 2> ResTys;
786 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
788 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
791 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
792 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
797 assert(Val->getValueType(1) == MVT::Other);
798 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
799 return DAG.getMergeValues(Vals, 2, DL);
802 static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
804 SDValue Value = Op->getOperand(1);
805 EVT ResTy = Op->getValueType(0);
807 SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
812 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
813 SelectionDAG &DAG) const {
814 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
817 case Intrinsic::mips_shilo:
818 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
819 case Intrinsic::mips_dpau_h_qbl:
820 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
821 case Intrinsic::mips_dpau_h_qbr:
822 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
823 case Intrinsic::mips_dpsu_h_qbl:
824 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
825 case Intrinsic::mips_dpsu_h_qbr:
826 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
827 case Intrinsic::mips_dpa_w_ph:
828 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
829 case Intrinsic::mips_dps_w_ph:
830 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
831 case Intrinsic::mips_dpax_w_ph:
832 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
833 case Intrinsic::mips_dpsx_w_ph:
834 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
835 case Intrinsic::mips_mulsa_w_ph:
836 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
837 case Intrinsic::mips_mult:
838 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
839 case Intrinsic::mips_multu:
840 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
841 case Intrinsic::mips_madd:
842 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
843 case Intrinsic::mips_maddu:
844 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
845 case Intrinsic::mips_msub:
846 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
847 case Intrinsic::mips_msubu:
848 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
849 case Intrinsic::mips_bnz_b:
850 case Intrinsic::mips_bnz_h:
851 case Intrinsic::mips_bnz_w:
852 case Intrinsic::mips_bnz_d:
853 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
854 case Intrinsic::mips_bnz_v:
855 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
856 case Intrinsic::mips_bz_b:
857 case Intrinsic::mips_bz_h:
858 case Intrinsic::mips_bz_w:
859 case Intrinsic::mips_bz_d:
860 return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
861 case Intrinsic::mips_bz_v:
862 return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
866 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
868 SDValue ChainIn = Op->getOperand(0);
869 SDValue Address = Op->getOperand(2);
870 SDValue Offset = Op->getOperand(3);
871 EVT ResTy = Op->getValueType(0);
872 EVT PtrTy = Address->getValueType(0);
874 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
876 return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
880 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
881 SelectionDAG &DAG) const {
882 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
886 case Intrinsic::mips_extp:
887 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
888 case Intrinsic::mips_extpdp:
889 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
890 case Intrinsic::mips_extr_w:
891 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
892 case Intrinsic::mips_extr_r_w:
893 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
894 case Intrinsic::mips_extr_rs_w:
895 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
896 case Intrinsic::mips_extr_s_h:
897 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
898 case Intrinsic::mips_mthlip:
899 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
900 case Intrinsic::mips_mulsaq_s_w_ph:
901 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
902 case Intrinsic::mips_maq_s_w_phl:
903 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
904 case Intrinsic::mips_maq_s_w_phr:
905 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
906 case Intrinsic::mips_maq_sa_w_phl:
907 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
908 case Intrinsic::mips_maq_sa_w_phr:
909 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
910 case Intrinsic::mips_dpaq_s_w_ph:
911 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
912 case Intrinsic::mips_dpsq_s_w_ph:
913 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
914 case Intrinsic::mips_dpaq_sa_l_w:
915 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
916 case Intrinsic::mips_dpsq_sa_l_w:
917 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
918 case Intrinsic::mips_dpaqx_s_w_ph:
919 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
920 case Intrinsic::mips_dpaqx_sa_w_ph:
921 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
922 case Intrinsic::mips_dpsqx_s_w_ph:
923 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
924 case Intrinsic::mips_dpsqx_sa_w_ph:
925 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
926 case Intrinsic::mips_ld_b:
927 case Intrinsic::mips_ld_h:
928 case Intrinsic::mips_ld_w:
929 case Intrinsic::mips_ld_d:
930 case Intrinsic::mips_ldx_b:
931 case Intrinsic::mips_ldx_h:
932 case Intrinsic::mips_ldx_w:
933 case Intrinsic::mips_ldx_d:
934 return lowerMSALoadIntr(Op, DAG, Intr);
938 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
940 SDValue ChainIn = Op->getOperand(0);
941 SDValue Value = Op->getOperand(2);
942 SDValue Address = Op->getOperand(3);
943 SDValue Offset = Op->getOperand(4);
944 EVT PtrTy = Address->getValueType(0);
946 Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
948 return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
952 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
953 SelectionDAG &DAG) const {
954 unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
958 case Intrinsic::mips_st_b:
959 case Intrinsic::mips_st_h:
960 case Intrinsic::mips_st_w:
961 case Intrinsic::mips_st_d:
962 case Intrinsic::mips_stx_b:
963 case Intrinsic::mips_stx_h:
964 case Intrinsic::mips_stx_w:
965 case Intrinsic::mips_stx_d:
966 return lowerMSAStoreIntr(Op, DAG, Intr);
970 MachineBasicBlock * MipsSETargetLowering::
971 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
973 // bposge32_pseudo $vr0
983 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
985 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
986 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
987 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
988 DebugLoc DL = MI->getDebugLoc();
989 const BasicBlock *LLVM_BB = BB->getBasicBlock();
990 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
991 MachineFunction *F = BB->getParent();
992 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
993 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
994 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
999 // Transfer the remainder of BB and its successor edges to Sink.
1000 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1002 Sink->transferSuccessorsAndUpdatePHIs(BB);
1005 BB->addSuccessor(FBB);
1006 BB->addSuccessor(TBB);
1007 FBB->addSuccessor(Sink);
1008 TBB->addSuccessor(Sink);
1010 // Insert the real bposge32 instruction to $BB.
1011 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1014 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1015 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1016 .addReg(Mips::ZERO).addImm(0);
1017 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1020 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1021 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1022 .addReg(Mips::ZERO).addImm(1);
1024 // Insert phi function to $Sink.
1025 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1026 MI->getOperand(0).getReg())
1027 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1029 MI->eraseFromParent(); // The pseudo instruction is gone now.
1033 MachineBasicBlock * MipsSETargetLowering::
1034 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1035 unsigned BranchOp) const{
1037 // vany_nonzero $rd, $ws
1048 // $rd = phi($rd1, $fbb, $rd2, $tbb)
1050 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1051 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1052 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1053 DebugLoc DL = MI->getDebugLoc();
1054 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1055 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1056 MachineFunction *F = BB->getParent();
1057 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1058 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1059 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1062 F->insert(It, Sink);
1064 // Transfer the remainder of BB and its successor edges to Sink.
1065 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1067 Sink->transferSuccessorsAndUpdatePHIs(BB);
1070 BB->addSuccessor(FBB);
1071 BB->addSuccessor(TBB);
1072 FBB->addSuccessor(Sink);
1073 TBB->addSuccessor(Sink);
1075 // Insert the real bnz.b instruction to $BB.
1076 BuildMI(BB, DL, TII->get(BranchOp))
1077 .addReg(MI->getOperand(1).getReg())
1081 unsigned RD1 = RegInfo.createVirtualRegister(RC);
1082 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1083 .addReg(Mips::ZERO).addImm(0);
1084 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1087 unsigned RD2 = RegInfo.createVirtualRegister(RC);
1088 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1089 .addReg(Mips::ZERO).addImm(1);
1091 // Insert phi function to $Sink.
1092 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1093 MI->getOperand(0).getReg())
1094 .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1096 MI->eraseFromParent(); // The pseudo instruction is gone now.