Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,49 +819,6 @@ bool RISCVDAGToDAGISel::trySignedBitfieldInsertInSign(SDNode *Node) {
return false;
}

// (xor X, (and (xor X, C1), C2))
// -> (qc.insbi X, (C1 >> ShAmt), Width, ShAmt)
// where C2 is a shifted mask with width=Width and shift=ShAmt
bool RISCVDAGToDAGISel::tryBitfieldInsertOpFromXor(SDNode *Node) {

if (!Subtarget->hasVendorXqcibm())
return false;

using namespace SDPatternMatch;

SDValue X;
APInt CImm, CMask;
if (!sd_match(
Node,
m_Xor(m_Value(X),
m_OneUse(m_And(m_OneUse(m_Xor(m_Deferred(X), m_ConstInt(CImm))),
m_ConstInt(CMask))))))
return false;

unsigned Width, ShAmt;
if (!CMask.isShiftedMask(ShAmt, Width))
return false;

int64_t Imm = CImm.getSExtValue();
Imm >>= ShAmt;

SDLoc DL(Node);
SDValue ImmNode;
auto Opc = RISCV::QC_INSB;

if (isInt<5>(Imm)) {
Opc = RISCV::QC_INSBI;
ImmNode = CurDAG->getSignedTargetConstant(Imm, DL, MVT::i32);
} else {
ImmNode = selectImm(CurDAG, DL, MVT::i32, Imm, *Subtarget);
}
SDValue Ops[] = {X, ImmNode, CurDAG->getTargetConstant(Width, DL, MVT::i32),
CurDAG->getTargetConstant(ShAmt, DL, MVT::i32)};
ReplaceNode(Node, CurDAG->getMachineNode(Opc, DL, MVT::i32, Ops));

return true;
}

bool RISCVDAGToDAGISel::tryUnsignedBitfieldExtract(SDNode *Node,
const SDLoc &DL, MVT VT,
SDValue X, unsigned Msb,
Expand Down Expand Up @@ -1442,9 +1399,6 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) {
if (tryShrinkShlLogicImm(Node))
return;

if (tryBitfieldInsertOpFromXor(Node))
return;

break;
case ISD::AND: {
auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Target/RISCV/RISCVISelDAGToDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class RISCVDAGToDAGISel : public SelectionDAGISel {
bool trySignedBitfieldExtract(SDNode *Node);
bool trySignedBitfieldInsertInSign(SDNode *Node);
bool trySignedBitfieldInsertInMask(SDNode *Node);
bool tryBitfieldInsertOpFromXor(SDNode *Node);
bool tryBitfieldInsertOpFromOrAndImm(SDNode *Node);
bool tryUnsignedBitfieldExtract(SDNode *Node, const SDLoc &DL, MVT VT,
SDValue X, unsigned Msb, unsigned Lsb);
Expand Down
45 changes: 45 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16156,6 +16156,48 @@ static SDValue combineOrOfCZERO(SDNode *N, SDValue N0, SDValue N1,
return DAG.getNode(ISD::XOR, DL, VT, NewOr, TrueV.getOperand(1));
}

// (xor X, (xor (and X, C2), Y))
// ->(qc_insb X, (sra Y, ShAmt), Width, ShAmt)
// where C2 is a shifted mask with width = Width and shift = ShAmt
// qc_insb might become qc.insb or qc.insbi depending on the operands.
static SDValue combineXorToBitfieldInsert(SDNode *N, SelectionDAG &DAG,
const RISCVSubtarget &Subtarget) {
if (!Subtarget.hasVendorXqcibm())
return SDValue();

using namespace SDPatternMatch;

SDValue Base, Inserted;
APInt CMask;
if (!sd_match(N, m_Xor(m_Value(Base),
m_OneUse(m_Xor(m_OneUse(m_And(m_Deferred(Base),
m_ConstInt(CMask))),
m_Value(Inserted))))))
return SDValue();

if (N->getValueType(0) != MVT::i32)
return SDValue();

unsigned Width, ShAmt;
if (!CMask.isShiftedMask(ShAmt, Width))
return SDValue();

// Check if all zero bits in CMask are also zero in Inserted
if (!DAG.MaskedValueIsZero(Inserted, ~CMask))
return SDValue();

SDLoc DL(N);

// `Inserted` needs to be right shifted before it is put into the
// instruction.
Inserted = DAG.getNode(ISD::SRA, DL, MVT::i32, Inserted,
DAG.getShiftAmountConstant(ShAmt, MVT::i32, DL));

SDValue Ops[] = {Base, Inserted, DAG.getConstant(Width, DL, MVT::i32),
DAG.getConstant(ShAmt, DL, MVT::i32)};
return DAG.getNode(RISCVISD::QC_INSB, DL, MVT::i32, Ops);
}

static SDValue performORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI,
const RISCVSubtarget &Subtarget) {
SelectionDAG &DAG = DCI.DAG;
Expand Down Expand Up @@ -16228,6 +16270,9 @@ static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
}
}

if (SDValue V = combineXorToBitfieldInsert(N, DAG, Subtarget))
return V;

if (SDValue V = combineBinOpToReduce(N, DAG, Subtarget))
return V;
if (SDValue V = combineBinOpOfExtractToReduceTree(N, DAG, Subtarget))
Expand Down
12 changes: 12 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfoXqci.td
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def SDT_SetMultiple : SDTypeProfile<0, 4, [SDTCisSameAs<0, 1>,
def qc_setwmi : RVSDNode<"QC_SETWMI", SDT_SetMultiple,
[SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;

def qc_insb : RVSDNode<"QC_INSB", SDTypeProfile<1, 4, [SDTCisSameAs<0, 1>,
SDTCisSameAs<0, 2>,
SDTCisVT<0, i32>,
SDTCisInt<3>,
SDTCisInt<4>]>,
[]>;

def uimm5nonzero : RISCVOp<XLenVT>,
ImmLeaf<XLenVT, [{return (Imm != 0) && isUInt<5>(Imm);}]> {
let ParserMatchClass = UImmAsmOperand<5, "NonZero">;
Expand Down Expand Up @@ -1508,6 +1515,11 @@ def : Pat<(i32 (and GPRNoX0:$rs, 1023)), (QC_EXTU GPRNoX0:$rs, 10, 0)>;
def : Pat<(i32 (and GPRNoX0:$rs, 2047)), (QC_EXTU GPRNoX0:$rs, 11, 0)>;

def : Pat<(i32 (bitreverse GPRNoX0:$rs1)), (QC_BREV32 GPRNoX0:$rs1)>;

def : Pat<(qc_insb GPRNoX0:$rd, simm5:$imm5, uimm5_plus1:$width, uimm5:$shamt),
(QC_INSBI GPRNoX0:$rd, simm5:$imm5, uimm5_plus1:$width, uimm5:$shamt)>;
def : Pat<(qc_insb GPRNoX0:$rd, GPR:$rs1, uimm5_plus1:$width, uimm5:$shamt),
(QC_INSB GPRNoX0:$rd, GPR:$rs1, uimm5_plus1:$width, uimm5:$shamt)>;
} // Predicates = [HasVendorXqcibm, IsRV32]

// If Zbb is enabled sext.b/h is preferred since they are compressible
Expand Down
Loading