Skip to content

[RISCV] Introduce add_vl combine for identity operand #139742

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
May 13, 2025
Merged
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
35 changes: 23 additions & 12 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16619,6 +16619,25 @@ NodeExtensionHelper::getSupportedFoldings(const SDNode *Root) {
}
} // End anonymous namespace.

static SDValue simplifyOp_VL(SDNode *N) {
// TODO: Extend this to other binops using generic identity logic
assert(N->getOpcode() == RISCVISD::ADD_VL);
SDValue A = N->getOperand(0);
SDValue B = N->getOperand(1);
SDValue Passthru = N->getOperand(2);
if (!Passthru.isUndef())
// TODO:This could be a vmerge instead
return SDValue();
;
if (ISD::isConstantSplatVectorAllZeros(B.getNode()))
return A;
// Peek through fixed to scalable
if (B.getOpcode() == ISD::INSERT_SUBVECTOR && B.getOperand(0).isUndef() &&
ISD::isConstantSplatVectorAllZeros(B.getOperand(1).getNode()))
return A;
return SDValue();
}

/// Combine a binary or FMA operation to its equivalent VW or VW_W form.
/// The supported combines are:
/// add | add_vl | or disjoint | or_vl disjoint -> vwadd(u) | vwadd(u)_w
Expand Down Expand Up @@ -18515,20 +18534,10 @@ static SDValue combineVqdotAccum(SDNode *N, SelectionDAG &DAG,
return SDValue();

SDValue AccumOp = DotOp.getOperand(2);
bool IsNullAdd = ISD::isConstantSplatVectorAllZeros(AccumOp.getNode());
// Peek through fixed to scalable
if (!IsNullAdd && AccumOp.getOpcode() == ISD::INSERT_SUBVECTOR &&
AccumOp.getOperand(0).isUndef())
IsNullAdd =
ISD::isConstantSplatVectorAllZeros(AccumOp.getOperand(1).getNode());

SDLoc DL(N);
EVT VT = N->getValueType(0);
// The manual constant folding is required, this case is not constant folded
// or combined.
if (!IsNullAdd)
Addend = DAG.getNode(RISCVISD::ADD_VL, DL, VT, AccumOp, Addend,
DAG.getUNDEF(VT), AddMask, AddVL);
Addend = DAG.getNode(RISCVISD::ADD_VL, DL, VT, Addend, AccumOp,
DAG.getUNDEF(VT), AddMask, AddVL);

SDValue Ops[] = {DotOp.getOperand(0), DotOp.getOperand(1), Addend,
DotOp.getOperand(3), DotOp->getOperand(4)};
Expand Down Expand Up @@ -19657,6 +19666,8 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
break;
}
case RISCVISD::ADD_VL:
if (SDValue V = simplifyOp_VL(N))
return V;
if (SDValue V = combineOp_VLToVWOp_VL(N, DCI, Subtarget))
return V;
if (SDValue V = combineVqdotAccum(N, DAG, Subtarget))
Expand Down