Skip to content

[RISCV] Add DAG combine to turn (sub (shl X, 8-Y), (shr X, Y)) into orc.b #111828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2024
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
44 changes: 36 additions & 8 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13572,8 +13572,10 @@ static SDValue combineSubOfBoolean(SDNode *N, SelectionDAG &DAG) {
return DAG.getNode(ISD::ADD, DL, VT, NewLHS, NewRHS);
}

// Looks for (sub (shl X, 8), X) where only bits 8, 16, 24, 32, etc. of X are
// non-zero. Replace with orc.b.
// Looks for (sub (shl X, 8-Y), (shr X, Y)) where the Y-th bit in each byte is
// potentially set. It is fine for Y to be 0, meaning that (sub (shl X, 8), X)
// is also valid. Replace with (orc.b X). For example, 0b0000_1000_0000_1000 is
// valid with Y=3, while 0b0000_1000_0000_0100 is not.
static SDValue combineSubShiftToOrcB(SDNode *N, SelectionDAG &DAG,
const RISCVSubtarget &Subtarget) {
if (!Subtarget.hasStdExtZbb())
Expand All @@ -13587,18 +13589,44 @@ static SDValue combineSubShiftToOrcB(SDNode *N, SelectionDAG &DAG,
SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);

if (N0.getOpcode() != ISD::SHL || N0.getOperand(0) != N1 || !N0.hasOneUse())
if (N0->getOpcode() != ISD::SHL)
return SDValue();

auto *ShAmtC = dyn_cast<ConstantSDNode>(N0.getOperand(1));
if (!ShAmtC || ShAmtC->getZExtValue() != 8)
auto *ShAmtCLeft = dyn_cast<ConstantSDNode>(N0.getOperand(1));
if (!ShAmtCLeft)
return SDValue();
unsigned ShiftedAmount = 8 - ShAmtCLeft->getZExtValue();

APInt Mask = APInt::getSplat(VT.getSizeInBits(), APInt(8, 0xfe));
if (!DAG.MaskedValueIsZero(N1, Mask))
if (ShiftedAmount >= 8)
return SDValue();

return DAG.getNode(RISCVISD::ORC_B, SDLoc(N), VT, N1);
SDValue LeftShiftOperand = N0->getOperand(0);
SDValue RightShiftOperand = N1;

if (ShiftedAmount != 0) { // Right operand must be a right shift.
if (N1->getOpcode() != ISD::SRL)
return SDValue();
auto *ShAmtCRight = dyn_cast<ConstantSDNode>(N1.getOperand(1));
if (!ShAmtCRight || ShAmtCRight->getZExtValue() != ShiftedAmount)
return SDValue();
RightShiftOperand = N1.getOperand(0);
}

// At least one shift should have a single use.
if (!N0.hasOneUse() && (ShiftedAmount == 0 || !N1.hasOneUse()))
return SDValue();

if (LeftShiftOperand != RightShiftOperand)
return SDValue();

APInt Mask = APInt::getSplat(VT.getSizeInBits(), APInt(8, 0x1));
Mask <<= ShiftedAmount;
// Check that X has indeed the right shape (only the Y-th bit can be set in
// every byte).
if (!DAG.MaskedValueIsZero(LeftShiftOperand, ~Mask))
return SDValue();

return DAG.getNode(RISCVISD::ORC_B, SDLoc(N), VT, LeftShiftOperand);
}

static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG,
Expand Down
Loading
Loading