Skip to content

[WebAssembly] Refactor PerformSETCCCombine #144875

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 2 commits into from
Jun 25, 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
91 changes: 59 additions & 32 deletions llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3239,52 +3239,79 @@ static SDValue performBitcastCombine(SDNode *N,
return SDValue();
}

static SDValue performSETCCCombine(SDNode *N,
TargetLowering::DAGCombinerInfo &DCI) {
auto &DAG = DCI.DAG;

template <int MatchRHS, ISD::CondCode MatchCond, bool RequiresNegate,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a particular reason for templating here as opposed to just passing in arguments? This will instantiate 4 different versions of the function which seems like a lot

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it is a lot... Most of this can just be left in the parent function too. I'll try again.

Copy link
Contributor

@badumbatish badumbatish Jun 19, 2025

Choose a reason for hiding this comment

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

i see that it's still templated but the helper function's size's been reduced. Is there any other particular reason besides code size that you're still opting for templating?

Intrinsic::ID Intrin>
static SDValue TryMatchTrue(SDNode *N, EVT VecVT, SelectionDAG &DAG) {
SDValue LHS = N->getOperand(0);
SDValue RHS = N->getOperand(1);
ISD::CondCode Cond = cast<CondCodeSDNode>(N->getOperand(2))->get();
SDValue Cond = N->getOperand(2);
if (MatchCond != cast<CondCodeSDNode>(Cond)->get())
return SDValue();

if (MatchRHS != cast<ConstantSDNode>(RHS)->getSExtValue())
return SDValue();

SDLoc DL(N);
SDValue Ret = DAG.getZExtOrTrunc(
DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, MVT::i32,
{DAG.getConstant(Intrin, DL, MVT::i32),
DAG.getSExtOrTrunc(LHS->getOperand(0), DL, VecVT)}),
DL, MVT::i1);
if (RequiresNegate)
Ret = DAG.getNOT(DL, Ret, MVT::i1);
return DAG.getZExtOrTrunc(Ret, DL, N->getValueType(0));
}

static SDValue performSETCCCombine(SDNode *N,
TargetLowering::DAGCombinerInfo &DCI) {
if (!DCI.isBeforeLegalize())
return SDValue();

EVT VT = N->getValueType(0);
if (!VT.isScalarInteger())
return SDValue();

SDValue LHS = N->getOperand(0);
if (LHS->getOpcode() != ISD::BITCAST)
return SDValue();

EVT FromVT = LHS->getOperand(0).getValueType();
if (!FromVT.isFixedLengthVector() || FromVT.getVectorElementType() != MVT::i1)
return SDValue();

unsigned NumElts = FromVT.getVectorNumElements();
if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
return SDValue();

if (!cast<ConstantSDNode>(N->getOperand(1)))
return SDValue();

EVT VecVT = FromVT.changeVectorElementType(MVT::getIntegerVT(128 / NumElts));
auto &DAG = DCI.DAG;
// setcc (iN (bitcast (vNi1 X))), 0, ne
// ==> any_true (vNi1 X)
if (auto Match = TryMatchTrue<0, ISD::SETNE, false, Intrinsic::wasm_anytrue>(
N, VecVT, DAG)) {
return Match;
}
// setcc (iN (bitcast (vNi1 X))), 0, eq
// ==> xor (any_true (vNi1 X)), -1
if (auto Match = TryMatchTrue<0, ISD::SETEQ, true, Intrinsic::wasm_anytrue>(
N, VecVT, DAG)) {
return Match;
}
// setcc (iN (bitcast (vNi1 X))), -1, eq
// ==> all_true (vNi1 X)
if (auto Match = TryMatchTrue<-1, ISD::SETEQ, false, Intrinsic::wasm_alltrue>(
N, VecVT, DAG)) {
return Match;
}
// setcc (iN (bitcast (vNi1 X))), -1, ne
// ==> xor (all_true (vNi1 X)), -1
if (DCI.isBeforeLegalize() && VT.isScalarInteger() &&
(Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
(isNullConstant(RHS) || isAllOnesConstant(RHS)) &&
LHS->getOpcode() == ISD::BITCAST) {
EVT FromVT = LHS->getOperand(0).getValueType();
if (FromVT.isFixedLengthVector() &&
FromVT.getVectorElementType() == MVT::i1) {
int Intrin = isNullConstant(RHS) ? Intrinsic::wasm_anytrue
: Intrinsic::wasm_alltrue;
unsigned NumElts = FromVT.getVectorNumElements();
if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
return SDValue();
EVT Width = MVT::getIntegerVT(128 / NumElts);
SDValue Ret = DAG.getZExtOrTrunc(
DAG.getNode(
ISD::INTRINSIC_WO_CHAIN, DL, MVT::i32,
{DAG.getConstant(Intrin, DL, MVT::i32),
DAG.getSExtOrTrunc(LHS->getOperand(0), DL,
FromVT.changeVectorElementType(Width))}),
DL, MVT::i1);
if ((isNullConstant(RHS) && (Cond == ISD::SETEQ)) ||
(isAllOnesConstant(RHS) && (Cond == ISD::SETNE))) {
Ret = DAG.getNOT(DL, Ret, MVT::i1);
}
return DAG.getZExtOrTrunc(Ret, DL, VT);
}
if (auto Match = TryMatchTrue<-1, ISD::SETNE, true, Intrinsic::wasm_alltrue>(
N, VecVT, DAG)) {
return Match;
}

return SDValue();
}

Expand Down