#include "MipsSEISelDAGToDAG.h"
#include "MCTargetDesc/MipsBaseInfo.h"
#include "Mips.h"
#include "MipsAnalyzeImmediate.h"
#include "MipsMachineFunction.h"
#include "MipsRegisterInfo.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/SelectionDAGNodes.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsMips.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
#define DEBUG_TYPE "mips-isel"
bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
Subtarget = &MF.getSubtarget<MipsSubtarget>();
if (Subtarget->inMips16Mode())
return false;
return MipsDAGToDAGISel::runOnMachineFunction(MF);
}
void MipsSEDAGToDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<DominatorTreeWrapperPass>();
SelectionDAGISel::getAnalysisUsage(AU);
}
void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef, MachineInstr &MI,
MachineFunction &MF) {
MachineInstrBuilder MIB(MF, &MI);
unsigned Mask = MI.getOperand(1).getImm();
unsigned Flag =
IsDef ? RegState::ImplicitDefine : RegState::Implicit | RegState::Undef;
if (Mask & 1)
MIB.addReg(Mips::DSPPos, Flag);
if (Mask & 2)
MIB.addReg(Mips::DSPSCount, Flag);
if (Mask & 4)
MIB.addReg(Mips::DSPCarry, Flag);
if (Mask & 8)
MIB.addReg(Mips::DSPOutFlag, Flag);
if (Mask & 16)
MIB.addReg(Mips::DSPCCond, Flag);
if (Mask & 32)
MIB.addReg(Mips::DSPEFI, Flag);
}
unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const {
uint64_t RegNum = cast<ConstantSDNode>(RegIdx)->getZExtValue();
return Mips::MSACtrlRegClass.getRegister(RegNum);
}
bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI,
const MachineInstr& MI) {
unsigned DstReg = 0, ZeroReg = 0;
if ((MI.getOpcode() == Mips::ADDiu) &&
(MI.getOperand(1).getReg() == Mips::ZERO) &&
(MI.getOperand(2).isImm()) &&
(MI.getOperand(2).getImm() == 0)) {
DstReg = MI.getOperand(0).getReg();
ZeroReg = Mips::ZERO;
} else if ((MI.getOpcode() == Mips::DADDiu) &&
(MI.getOperand(1).getReg() == Mips::ZERO_64) &&
(MI.getOperand(2).isImm()) &&
(MI.getOperand(2).getImm() == 0)) {
DstReg = MI.getOperand(0).getReg();
ZeroReg = Mips::ZERO_64;
}
if (!DstReg)
return false;
for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
E = MRI->use_end(); U != E;) {
MachineOperand &MO = *U;
unsigned OpNo = U.getOperandNo();
MachineInstr *MI = MO.getParent();
++U;
if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
continue;
if (!MRI->getRegClass(MO.getReg())->contains(ZeroReg))
continue;
MO.setReg(ZeroReg);
}
return true;
}
void MipsSEDAGToDAGISel::emitMCountABI(MachineInstr &MI, MachineBasicBlock &MBB,
MachineFunction &MF) {
MachineInstrBuilder MIB(MF, &MI);
if (!Subtarget->isABI_O32()) { BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::OR64))
.addDef(Mips::AT_64)
.addUse(Mips::RA_64, RegState::Undef)
.addUse(Mips::ZERO_64);
MIB.addUse(Mips::AT_64, RegState::Implicit);
} else { BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::OR))
.addDef(Mips::AT)
.addUse(Mips::RA, RegState::Undef)
.addUse(Mips::ZERO);
BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Mips::ADDiu))
.addDef(Mips::SP)
.addUse(Mips::SP)
.addImm(-8);
MIB.addUse(Mips::AT, RegState::Implicit);
}
}
void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
MF.getInfo<MipsFunctionInfo>()->initGlobalBaseReg(MF);
MachineRegisterInfo *MRI = &MF.getRegInfo();
for (auto &MBB: MF) {
for (auto &MI: MBB) {
switch (MI.getOpcode()) {
case Mips::RDDSP:
addDSPCtrlRegOperands(false, MI, MF);
break;
case Mips::WRDSP:
addDSPCtrlRegOperands(true, MI, MF);
break;
case Mips::BuildPairF64_64:
case Mips::ExtractElementF64_64:
if (!Subtarget->useOddSPReg()) {
MI.addOperand(MachineOperand::CreateReg(Mips::SP, false, true));
break;
}
LLVM_FALLTHROUGH;
case Mips::BuildPairF64:
case Mips::ExtractElementF64:
if (Subtarget->isABI_FPXX() && !Subtarget->hasMTHC1())
MI.addOperand(MachineOperand::CreateReg(Mips::SP, false, true));
break;
case Mips::JAL:
case Mips::JAL_MM:
if (MI.getOperand(0).isGlobal() &&
MI.getOperand(0).getGlobal()->getGlobalIdentifier() == "_mcount")
emitMCountABI(MI, MBB, MF);
break;
case Mips::JALRPseudo:
case Mips::JALR64Pseudo:
case Mips::JALR16_MM:
if (MI.getOperand(2).isMCSymbol() &&
MI.getOperand(2).getMCSymbol()->getName() == "_mcount")
emitMCountABI(MI, MBB, MF);
break;
case Mips::JALR:
if (MI.getOperand(3).isMCSymbol() &&
MI.getOperand(3).getMCSymbol()->getName() == "_mcount")
emitMCountABI(MI, MBB, MF);
break;
default:
replaceUsesWithZeroReg(MRI, MI);
}
}
}
}
void MipsSEDAGToDAGISel::selectAddE(SDNode *Node, const SDLoc &DL) const {
SDValue InFlag = Node->getOperand(2);
unsigned Opc = InFlag.getOpcode();
SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1);
EVT VT = LHS.getValueType();
if (Opc == ISD::ADDC) {
SDValue Ops[3] = {LHS, RHS, InFlag};
CurDAG->SelectNodeTo(Node, Mips::ADDWC, VT, MVT::Glue, Ops);
return;
}
assert(Opc == ISD::ADDE && "ISD::ADDE not in a chain of ADDE nodes!");
SDValue CstOne = CurDAG->getTargetConstant(1, DL, MVT::i32);
SDValue OuFlag = CurDAG->getTargetConstant(20, DL, MVT::i32);
SDNode *DSPCtrlField = CurDAG->getMachineNode(Mips::RDDSP, DL, MVT::i32,
MVT::Glue, CstOne, InFlag);
SDNode *Carry = CurDAG->getMachineNode(
Mips::EXT, DL, MVT::i32, SDValue(DSPCtrlField, 0), OuFlag, CstOne);
SDValue Ops[4] = {SDValue(DSPCtrlField, 0),
CurDAG->getTargetConstant(6, DL, MVT::i32), CstOne,
SDValue(Carry, 0)};
SDNode *DSPCFWithCarry = CurDAG->getMachineNode(Mips::INS, DL, MVT::i32, Ops);
SDValue Zero = CurDAG->getRegister(Mips::ZERO, MVT::i32);
SDValue InsOps[4] = {Zero, OuFlag, CstOne, SDValue(DSPCFWithCarry, 0)};
SDNode *DSPCtrlFinal =
CurDAG->getMachineNode(Mips::INS, DL, MVT::i32, InsOps);
SDNode *WrDSP = CurDAG->getMachineNode(Mips::WRDSP, DL, MVT::Glue,
SDValue(DSPCtrlFinal, 0), CstOne);
SDValue Operands[3] = {LHS, RHS, SDValue(WrDSP, 0)};
CurDAG->SelectNodeTo(Node, Mips::ADDWC, VT, MVT::Glue, Operands);
}
bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
EVT ValTy = Addr.getValueType();
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), ValTy);
return true;
}
return false;
}
bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(
SDValue Addr, SDValue &Base, SDValue &Offset, unsigned OffsetBits,
unsigned ShiftAmount = 0) const {
if (CurDAG->isBaseWithConstantOffset(Addr)) {
auto *CN = cast<ConstantSDNode>(Addr.getOperand(1));
if (isIntN(OffsetBits + ShiftAmount, CN->getSExtValue())) {
EVT ValTy = Addr.getValueType();
if (FrameIndexSDNode *FIN =
dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
else {
Base = Addr.getOperand(0);
const Align Alignment(1ULL << ShiftAmount);
if (!isAligned(Alignment, CN->getZExtValue()))
return false;
}
Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr),
ValTy);
return true;
}
}
return false;
}
bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (Addr.getOpcode() == MipsISD::Wrapper) {
Base = Addr.getOperand(0);
Offset = Addr.getOperand(1);
return true;
}
if (!TM.isPositionIndependent()) {
if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Addr.getOpcode() == ISD::TargetGlobalAddress))
return false;
}
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
return true;
if (Addr.getOpcode() == ISD::ADD) {
if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
isa<JumpTableSDNode>(Opnd0)) {
Base = Addr.getOperand(0);
Offset = Opnd0;
return true;
}
}
}
return false;
}
bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
Base = Addr;
Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType());
return true;
}
bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
return selectAddrRegImm(Addr, Base, Offset) ||
selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9))
return true;
return false;
}
bool MipsSEDAGToDAGISel::selectAddrRegImm11(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 11))
return true;
return false;
}
bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
return true;
return false;
}
bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
return true;
return false;
}
bool MipsSEDAGToDAGISel::selectIntAddr11MM(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
return selectAddrRegImm11(Addr, Base, Offset) ||
selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectIntAddr12MM(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
return selectAddrRegImm12(Addr, Base, Offset) ||
selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectIntAddr16MM(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
return selectAddrRegImm16(Addr, Base, Offset) ||
selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) {
if (isa<FrameIndexSDNode>(Base))
return false;
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) {
unsigned CnstOff = CN->getZExtValue();
return (CnstOff == (CnstOff & 0x3c));
}
return false;
}
if (selectAddrRegImm(Addr, Base, Offset))
return false;
return selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectIntAddrSImm10(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
return true;
return selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl1(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 1))
return true;
return selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl2(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 2))
return true;
return selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl3(SDValue Addr, SDValue &Base,
SDValue &Offset) const {
if (selectAddrFrameIndex(Addr, Base, Offset))
return true;
if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 3))
return true;
return selectAddrDefault(Addr, Base, Offset);
}
bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm,
unsigned MinSizeInBits) const {
if (!Subtarget->hasMSA())
return false;
BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
if (!Node)
return false;
APInt SplatValue, SplatUndef;
unsigned SplatBitSize;
bool HasAnyUndefs;
if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
MinSizeInBits, !Subtarget->isLittle()))
return false;
Imm = SplatValue;
return true;
}
bool MipsSEDAGToDAGISel::
selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
unsigned ImmBitSize) const {
APInt ImmValue;
EVT EltTy = N->getValueType(0).getVectorElementType();
if (N->getOpcode() == ISD::BITCAST)
N = N->getOperand(0);
if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
(!Signed && ImmValue.isIntN(ImmBitSize))) {
Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy);
return true;
}
}
return false;
}
bool MipsSEDAGToDAGISel::
selectVSplatUimm1(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, false, 1);
}
bool MipsSEDAGToDAGISel::
selectVSplatUimm2(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, false, 2);
}
bool MipsSEDAGToDAGISel::
selectVSplatUimm3(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, false, 3);
}
bool MipsSEDAGToDAGISel::
selectVSplatUimm4(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, false, 4);
}
bool MipsSEDAGToDAGISel::
selectVSplatUimm5(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, false, 5);
}
bool MipsSEDAGToDAGISel::
selectVSplatUimm6(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, false, 6);
}
bool MipsSEDAGToDAGISel::
selectVSplatUimm8(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, false, 8);
}
bool MipsSEDAGToDAGISel::
selectVSplatSimm5(SDValue N, SDValue &Imm) const {
return selectVSplatCommon(N, Imm, true, 5);
}
bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
APInt ImmValue;
EVT EltTy = N->getValueType(0).getVectorElementType();
if (N->getOpcode() == ISD::BITCAST)
N = N->getOperand(0);
if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
int32_t Log2 = ImmValue.exactLogBase2();
if (Log2 != -1) {
Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
return true;
}
}
return false;
}
bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
APInt ImmValue;
EVT EltTy = N->getValueType(0).getVectorElementType();
if (N->getOpcode() == ISD::BITCAST)
N = N->getOperand(0);
if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
Imm = CurDAG->getTargetConstant(ImmValue.countPopulation() - 1, SDLoc(N),
EltTy);
return true;
}
}
return false;
}
bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
APInt ImmValue;
EVT EltTy = N->getValueType(0).getVectorElementType();
if (N->getOpcode() == ISD::BITCAST)
N = N->getOperand(0);
if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
Imm = CurDAG->getTargetConstant(ImmValue.countPopulation() - 1, SDLoc(N),
EltTy);
return true;
}
}
return false;
}
bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
SDValue &Imm) const {
APInt ImmValue;
EVT EltTy = N->getValueType(0).getVectorElementType();
if (N->getOpcode() == ISD::BITCAST)
N = N->getOperand(0);
if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) &&
ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
int32_t Log2 = (~ImmValue).exactLogBase2();
if (Log2 != -1) {
Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy);
return true;
}
}
return false;
}
bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) {
unsigned Opcode = Node->getOpcode();
SDLoc DL(Node);
switch(Opcode) {
default: break;
case Mips::PseudoD_SELECT_I:
case Mips::PseudoD_SELECT_I64: {
MVT VT = Subtarget->isGP64bit() ? MVT::i64 : MVT::i32;
SDValue cond = Node->getOperand(0);
SDValue Hi1 = Node->getOperand(1);
SDValue Lo1 = Node->getOperand(2);
SDValue Hi2 = Node->getOperand(3);
SDValue Lo2 = Node->getOperand(4);
SDValue ops[] = {cond, Hi1, Lo1, Hi2, Lo2};
EVT NodeTys[] = {VT, VT};
ReplaceNode(Node, CurDAG->getMachineNode(Subtarget->isGP64bit()
? Mips::PseudoD_SELECT_I64
: Mips::PseudoD_SELECT_I,
DL, NodeTys, ops));
return true;
}
case ISD::ADDE: {
selectAddE(Node, DL);
return true;
}
case ISD::ConstantFP: {
auto *CN = cast<ConstantFPSDNode>(Node);
if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
if (Subtarget->isGP64bit()) {
SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Mips::ZERO_64, MVT::i64);
ReplaceNode(Node,
CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero));
} else if (Subtarget->isFP64bit()) {
SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Mips::ZERO, MVT::i32);
ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL,
MVT::f64, Zero, Zero));
} else {
SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
Mips::ZERO, MVT::i32);
ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL,
MVT::f64, Zero, Zero));
}
return true;
}
break;
}
case ISD::Constant: {
auto *CN = cast<ConstantSDNode>(Node);
int64_t Imm = CN->getSExtValue();
unsigned Size = CN->getValueSizeInBits(0);
if (isInt<32>(Imm))
break;
MipsAnalyzeImmediate AnalyzeImm;
const MipsAnalyzeImmediate::InstSeq &Seq =
AnalyzeImm.Analyze(Imm, Size, false);
MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
SDLoc DL(CN);
SDNode *RegOpnd;
SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
DL, MVT::i64);
if (Inst->Opc == Mips::LUi64)
RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
else
RegOpnd =
CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
ImmOpnd);
for (++Inst; Inst != Seq.end(); ++Inst) {
ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL,
MVT::i64);
RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
SDValue(RegOpnd, 0), ImmOpnd);
}
ReplaceNode(Node, RegOpnd);
return true;
}
case ISD::INTRINSIC_W_CHAIN: {
const unsigned IntrinsicOpcode =
cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
switch (IntrinsicOpcode) {
default:
break;
case Intrinsic::mips_cfcmsa: {
SDValue ChainIn = Node->getOperand(0);
SDValue RegIdx = Node->getOperand(2);
SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
getMSACtrlReg(RegIdx), MVT::i32);
ReplaceNode(Node, Reg.getNode());
return true;
}
case Intrinsic::mips_ldr_d:
case Intrinsic::mips_ldr_w: {
unsigned Op = (IntrinsicOpcode == Intrinsic::mips_ldr_d) ? Mips::LDR_D
: Mips::LDR_W;
SDLoc DL(Node);
assert(Node->getNumOperands() == 4 && "Unexpected number of operands.");
const SDValue &Chain = Node->getOperand(0);
const SDValue &Intrinsic = Node->getOperand(1);
const SDValue &Pointer = Node->getOperand(2);
const SDValue &Constant = Node->getOperand(3);
assert(Chain.getValueType() == MVT::Other);
(void)Intrinsic;
assert(Intrinsic.getOpcode() == ISD::TargetConstant &&
Constant.getOpcode() == ISD::Constant &&
"Invalid instruction operand.");
const ConstantInt *Val =
cast<ConstantSDNode>(Constant)->getConstantIntValue();
SDValue Imm =
CurDAG->getTargetConstant(*Val, DL, Constant.getValueType());
SmallVector<SDValue, 3> Ops{Pointer, Imm, Chain};
assert(Node->getNumValues() == 2);
assert(Node->getValueType(0).is128BitVector());
assert(Node->getValueType(1) == MVT::Other);
SmallVector<EVT, 2> ResTys{Node->getValueType(0), Node->getValueType(1)};
ReplaceNode(Node, CurDAG->getMachineNode(Op, DL, ResTys, Ops));
return true;
}
}
break;
}
case ISD::INTRINSIC_WO_CHAIN: {
switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
default:
break;
case Intrinsic::mips_move_v:
ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL,
Node->getValueType(0),
Node->getOperand(1)));
return true;
}
break;
}
case ISD::INTRINSIC_VOID: {
const unsigned IntrinsicOpcode =
cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
switch (IntrinsicOpcode) {
default:
break;
case Intrinsic::mips_ctcmsa: {
SDValue ChainIn = Node->getOperand(0);
SDValue RegIdx = Node->getOperand(2);
SDValue Value = Node->getOperand(3);
SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
getMSACtrlReg(RegIdx), Value);
ReplaceNode(Node, ChainOut.getNode());
return true;
}
case Intrinsic::mips_str_d:
case Intrinsic::mips_str_w: {
unsigned Op = (IntrinsicOpcode == Intrinsic::mips_str_d) ? Mips::STR_D
: Mips::STR_W;
SDLoc DL(Node);
assert(Node->getNumOperands() == 5 && "Unexpected number of operands.");
const SDValue &Chain = Node->getOperand(0);
const SDValue &Intrinsic = Node->getOperand(1);
const SDValue &Vec = Node->getOperand(2);
const SDValue &Pointer = Node->getOperand(3);
const SDValue &Constant = Node->getOperand(4);
assert(Chain.getValueType() == MVT::Other);
(void)Intrinsic;
assert(Intrinsic.getOpcode() == ISD::TargetConstant &&
Constant.getOpcode() == ISD::Constant &&
"Invalid instruction operand.");
const ConstantInt *Val =
cast<ConstantSDNode>(Constant)->getConstantIntValue();
SDValue Imm =
CurDAG->getTargetConstant(*Val, DL, Constant.getValueType());
SmallVector<SDValue, 4> Ops{Vec, Pointer, Imm, Chain};
assert(Node->getNumValues() == 1);
assert(Node->getValueType(0) == MVT::Other);
SmallVector<EVT, 1> ResTys{Node->getValueType(0)};
ReplaceNode(Node, CurDAG->getMachineNode(Op, DL, ResTys, Ops));
return true;
}
}
break;
}
case MipsISD::FAbs: {
MVT ResTy = Node->getSimpleValueType(0);
assert((ResTy == MVT::f64 || ResTy == MVT::f32) &&
"Unsupported float type!");
unsigned Opc = 0;
if (ResTy == MVT::f64)
Opc = (Subtarget->isFP64bit() ? Mips::FABS_D64 : Mips::FABS_D32);
else
Opc = Mips::FABS_S;
if (Subtarget->inMicroMipsMode()) {
switch (Opc) {
case Mips::FABS_D64:
Opc = Mips::FABS_D64_MM;
break;
case Mips::FABS_D32:
Opc = Mips::FABS_D32_MM;
break;
case Mips::FABS_S:
Opc = Mips::FABS_S_MM;
break;
default:
llvm_unreachable("Unknown opcode for MIPS floating point abs!");
}
}
ReplaceNode(Node,
CurDAG->getMachineNode(Opc, DL, ResTy, Node->getOperand(0)));
return true;
}
case MipsISD::Ins: {
if (Node->getValueType(0) != MVT::i32 && Node->getValueType(0) != MVT::i64)
return false;
if (Node->getNumOperands() != 4)
return false;
if (Node->getOperand(1)->getOpcode() != ISD::Constant ||
Node->getOperand(2)->getOpcode() != ISD::Constant)
return false;
MVT ResTy = Node->getSimpleValueType(0);
uint64_t Pos = Node->getConstantOperandVal(1);
uint64_t Size = Node->getConstantOperandVal(2);
if (!Size)
return false;
if (Pos + Size > 64)
return false;
if (ResTy != MVT::i32 && ResTy != MVT::i64)
return false;
unsigned Opcode = 0;
if (ResTy == MVT::i32) {
if (Pos + Size <= 32)
Opcode = Mips::INS;
} else {
if (Pos + Size <= 32)
Opcode = Mips::DINS;
else if (Pos < 32 && 1 < Size)
Opcode = Mips::DINSM;
else
Opcode = Mips::DINSU;
}
if (Opcode) {
SDValue Ops[4] = {
Node->getOperand(0), CurDAG->getTargetConstant(Pos, DL, MVT::i32),
CurDAG->getTargetConstant(Size, DL, MVT::i32), Node->getOperand(3)};
ReplaceNode(Node, CurDAG->getMachineNode(Opcode, DL, ResTy, Ops));
return true;
}
return false;
}
case MipsISD::ThreadPointer: {
EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout());
unsigned RdhwrOpc, DestReg;
if (PtrVT == MVT::i32) {
RdhwrOpc = Mips::RDHWR;
DestReg = Mips::V1;
} else {
RdhwrOpc = Mips::RDHWR64;
DestReg = Mips::V1_64;
}
SDNode *Rdhwr =
CurDAG->getMachineNode(RdhwrOpc, DL, Node->getValueType(0), MVT::Glue,
CurDAG->getRegister(Mips::HWR29, MVT::i32),
CurDAG->getTargetConstant(0, DL, MVT::i32));
SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
SDValue(Rdhwr, 0), SDValue(Rdhwr, 1));
SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT,
Chain.getValue(1));
ReplaceNode(Node, ResNode.getNode());
return true;
}
case ISD::BUILD_VECTOR: {
const MipsABIInfo &ABI =
static_cast<const MipsTargetMachine &>(TM).getABI();
BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
APInt SplatValue, SplatUndef;
unsigned SplatBitSize;
bool HasAnyUndefs;
unsigned LdiOp;
EVT ResVecTy = BVN->getValueType(0);
EVT ViaVecTy;
if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
return false;
if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
HasAnyUndefs, 8,
!Subtarget->isLittle()))
return false;
switch (SplatBitSize) {
default:
return false;
case 8:
LdiOp = Mips::LDI_B;
ViaVecTy = MVT::v16i8;
break;
case 16:
LdiOp = Mips::LDI_H;
ViaVecTy = MVT::v8i16;
break;
case 32:
LdiOp = Mips::LDI_W;
ViaVecTy = MVT::v4i32;
break;
case 64:
LdiOp = Mips::LDI_D;
ViaVecTy = MVT::v2i64;
break;
}
SDNode *Res = nullptr;
if (SplatValue.isSignedIntN(10)) {
SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL,
ViaVecTy.getVectorElementType());
Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm);
} else if (SplatValue.isSignedIntN(16) &&
((ABI.IsO32() && SplatBitSize < 64) ||
(ABI.IsN32() || ABI.IsN64()))) {
bool Is32BitSplat = ABI.IsO32() || SplatBitSize < 64;
const unsigned ADDiuOp = Is32BitSplat ? Mips::ADDiu : Mips::DADDiu;
const MVT SplatMVT = Is32BitSplat ? MVT::i32 : MVT::i64;
SDValue ZeroVal = CurDAG->getRegister(
Is32BitSplat ? Mips::ZERO : Mips::ZERO_64, SplatMVT);
const unsigned FILLOp =
SplatBitSize == 16
? Mips::FILL_H
: (SplatBitSize == 32 ? Mips::FILL_W
: (SplatBitSize == 64 ? Mips::FILL_D : 0));
assert(FILLOp != 0 && "Unknown FILL Op for splat synthesis!");
assert((!ABI.IsO32() || (FILLOp != Mips::FILL_D)) &&
"Attempting to use fill.d on MIPS32!");
const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, SplatMVT);
Res = CurDAG->getMachineNode(ADDiuOp, DL, SplatMVT, ZeroVal, LoVal);
Res = CurDAG->getMachineNode(FILLOp, DL, ViaVecTy, SDValue(Res, 0));
} else if (SplatValue.isSignedIntN(32) && SplatBitSize == 32) {
const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue();
SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32);
SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32);
SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32);
if (Hi)
Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal);
if (Lo)
Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
Hi ? SDValue(Res, 0) : ZeroVal, LoVal);
assert((Hi || Lo) && "Zero case reached 32 bit case splat synthesis!");
Res =
CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32, SDValue(Res, 0));
} else if (SplatValue.isSignedIntN(32) && SplatBitSize == 64 &&
(ABI.IsN32() || ABI.IsN64())) {
const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue();
SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32);
SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32);
SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32);
if (Hi)
Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal);
if (Lo)
Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
Hi ? SDValue(Res, 0) : ZeroVal, LoVal);
Res = CurDAG->getMachineNode(
Mips::SUBREG_TO_REG, DL, MVT::i64,
CurDAG->getTargetConstant(((Hi >> 15) & 0x1), DL, MVT::i64),
SDValue(Res, 0),
CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64));
Res =
CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64, SDValue(Res, 0));
} else if (SplatValue.isSignedIntN(64)) {
const unsigned Lo = SplatValue.getLoBits(16).getZExtValue();
const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue();
const unsigned Higher = SplatValue.lshr(32).getLoBits(16).getZExtValue();
const unsigned Highest = SplatValue.lshr(48).getLoBits(16).getZExtValue();
SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32);
SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32);
SDValue HigherVal = CurDAG->getTargetConstant(Higher, DL, MVT::i32);
SDValue HighestVal = CurDAG->getTargetConstant(Highest, DL, MVT::i32);
SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32);
if (Hi)
Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal);
if (Lo)
Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
Hi ? SDValue(Res, 0) : ZeroVal, LoVal);
SDNode *HiRes;
if (Highest)
HiRes = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HighestVal);
if (Higher)
HiRes = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32,
Highest ? SDValue(HiRes, 0) : ZeroVal,
HigherVal);
if (ABI.IsO32()) {
Res = CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32,
(Hi || Lo) ? SDValue(Res, 0) : ZeroVal);
Res = CurDAG->getMachineNode(
Mips::INSERT_W, DL, MVT::v4i32, SDValue(Res, 0),
(Highest || Higher) ? SDValue(HiRes, 0) : ZeroVal,
CurDAG->getTargetConstant(1, DL, MVT::i32));
const TargetLowering *TLI = getTargetLowering();
const TargetRegisterClass *RC =
TLI->getRegClassFor(ViaVecTy.getSimpleVT());
Res = CurDAG->getMachineNode(
Mips::COPY_TO_REGCLASS, DL, ViaVecTy, SDValue(Res, 0),
CurDAG->getTargetConstant(RC->getID(), DL, MVT::i32));
Res = CurDAG->getMachineNode(
Mips::SPLATI_D, DL, MVT::v2i64, SDValue(Res, 0),
CurDAG->getTargetConstant(0, DL, MVT::i32));
} else if (ABI.IsN64() || ABI.IsN32()) {
SDValue Zero64Val = CurDAG->getRegister(Mips::ZERO_64, MVT::i64);
const bool HiResNonZero = Highest || Higher;
const bool ResNonZero = Hi || Lo;
if (HiResNonZero)
HiRes = CurDAG->getMachineNode(
Mips::SUBREG_TO_REG, DL, MVT::i64,
CurDAG->getTargetConstant(((Highest >> 15) & 0x1), DL, MVT::i64),
SDValue(HiRes, 0),
CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64));
if (ResNonZero)
Res = CurDAG->getMachineNode(
Mips::SUBREG_TO_REG, DL, MVT::i64,
CurDAG->getTargetConstant(((Hi >> 15) & 0x1), DL, MVT::i64),
SDValue(Res, 0),
CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64));
if (ResNonZero) {
IntegerType *Int32Ty =
IntegerType::get(MF->getFunction().getContext(), 32);
const ConstantInt *Const32 = ConstantInt::get(Int32Ty, 32);
SDValue Ops[4] = {HiResNonZero ? SDValue(HiRes, 0) : Zero64Val,
CurDAG->getConstant(*Const32, DL, MVT::i32),
CurDAG->getConstant(*Const32, DL, MVT::i32),
SDValue(Res, 0)};
Res = CurDAG->getMachineNode(Mips::DINSU, DL, MVT::i64, Ops);
} else if (HiResNonZero) {
Res = CurDAG->getMachineNode(
Mips::DSLL32, DL, MVT::i64, SDValue(HiRes, 0),
CurDAG->getTargetConstant(0, DL, MVT::i32));
} else
llvm_unreachable(
"Zero splat value handled by non-zero 64bit splat synthesis!");
Res = CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64,
SDValue(Res, 0));
} else
llvm_unreachable("Unknown ABI in MipsISelDAGToDAG!");
} else
return false;
if (ResVecTy != ViaVecTy) {
const TargetLowering *TLI = getTargetLowering();
MVT ResVecTySimple = ResVecTy.getSimpleVT();
const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL,
ResVecTy, SDValue(Res, 0),
CurDAG->getTargetConstant(RC->getID(), DL,
MVT::i32));
}
ReplaceNode(Node, Res);
return true;
}
}
return false;
}
bool MipsSEDAGToDAGISel::
SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
std::vector<SDValue> &OutOps) {
SDValue Base, Offset;
switch(ConstraintID) {
default:
llvm_unreachable("Unexpected asm memory constraint");
case InlineAsm::Constraint_m:
case InlineAsm::Constraint_o:
if (selectAddrRegImm16(Op, Base, Offset)) {
OutOps.push_back(Base);
OutOps.push_back(Offset);
return false;
}
OutOps.push_back(Op);
OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
return false;
case InlineAsm::Constraint_R:
if (selectAddrRegImm9(Op, Base, Offset)) {
OutOps.push_back(Base);
OutOps.push_back(Offset);
return false;
}
OutOps.push_back(Op);
OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
return false;
case InlineAsm::Constraint_ZC:
if (Subtarget->inMicroMipsMode()) {
if (selectAddrRegImm12(Op, Base, Offset)) {
OutOps.push_back(Base);
OutOps.push_back(Offset);
return false;
}
} else if (Subtarget->hasMips32r6()) {
if (selectAddrRegImm9(Op, Base, Offset)) {
OutOps.push_back(Base);
OutOps.push_back(Offset);
return false;
}
} else if (selectAddrRegImm16(Op, Base, Offset)) {
OutOps.push_back(Base);
OutOps.push_back(Offset);
return false;
}
OutOps.push_back(Op);
OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32));
return false;
}
return true;
}
FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM,
CodeGenOpt::Level OptLevel) {
return new MipsSEDAGToDAGISel(TM, OptLevel);
}