Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -773,49 +773,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 @@ -1393,9 +1350,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 tryUnsignedBitfieldExtract(SDNode *Node, const SDLoc &DL, MVT VT,
SDValue X, unsigned Msb, unsigned Lsb);
bool tryUnsignedBitfieldInsertInZero(SDNode *Node, const SDLoc &DL, MVT VT,
Expand Down
42 changes: 42 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16059,6 +16059,45 @@ static SDValue combineOrOfCZERO(SDNode *N, SDValue N0, SDValue N1,
return DAG.getNode(ISD::XOR, DL, VT, NewOr, TrueV.getOperand(1));
}

// (xor X, (and(xor X, Y), C2))
// ->(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_And(m_OneUse(m_Xor(m_Deferred(Base),
m_Value(Inserted))),
m_ConstInt(CMask))))))
return SDValue();

if (N->getValueType(0) != MVT::i32 || Base.getValueType() != MVT::i32 ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are't looking through at extends or truncates, the N->getValueType() != MVT::i32 should be enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my fault, I was ensuring that the combine was conservative, and wasn't sure how much these could change.

Do we want to work harder to delay this combine until after (type?) legalisation? I wasn't sure because not many other combines do so, but I see you made some changes in that direction today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Removed the extra checks.

Inserted.getValueType() != MVT::i32)
return SDValue();

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

SDLoc DL(N);

// `Inserted` needs to be right - shifted before it is put into the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// `Inserted` needs to be right - shifted before it is put into the
// `Inserted` needs to be right shifted before it is put into the

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// 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 @@ -16131,6 +16170,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