Skip to content

[X86] Use GFNI for LZCNT vXi8 ops #141888

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 4 commits into from
Jun 4, 2025
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
27 changes: 27 additions & 0 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28999,6 +28999,30 @@ static SDValue LowerVectorCTLZ(SDValue Op, const SDLoc &DL,
return LowerVectorCTLZInRegLUT(Op, DL, Subtarget, DAG);
}

static SDValue LowerVectorCTLZ_GFNI(SDValue Op, const SDLoc &DL,
SelectionDAG &DAG,
const X86Subtarget &Subtarget) {
MVT VT = Op.getSimpleValueType();
SDValue Input = Op.getOperand(0);

assert(VT.isVector() && VT.getVectorElementType() == MVT::i8 &&
"Expected vXi8 input for GFNI-based CTLZ lowering");

SDValue Reversed = DAG.getNode(ISD::BITREVERSE, DL, VT, Input);

SDValue Neg = DAG.getNegative(Reversed, DL, VT);
SDValue Filtered = DAG.getNode(ISD::AND, DL, VT, Reversed, Neg);

MVT VT64 = MVT::getVectorVT(MVT::i64, VT.getSizeInBits() / 64);
SDValue CTTZConst = DAG.getConstant(0xAACCF0FF00000000ULL, DL, VT64);
SDValue CTTZMatrix = DAG.getBitcast(VT, CTTZConst);

SDValue LZCNT =
DAG.getNode(X86ISD::GF2P8AFFINEQB, DL, VT, Filtered, CTTZMatrix,
DAG.getTargetConstant(8, DL, MVT::i8));
return LZCNT;
}

static SDValue LowerCTLZ(SDValue Op, const X86Subtarget &Subtarget,
SelectionDAG &DAG) {
MVT VT = Op.getSimpleValueType();
Expand All @@ -29007,6 +29031,9 @@ static SDValue LowerCTLZ(SDValue Op, const X86Subtarget &Subtarget,
SDLoc dl(Op);
unsigned Opc = Op.getOpcode();

if (VT.isVector() && VT.getScalarType() == MVT::i8 && Subtarget.hasGFNI())
return LowerVectorCTLZ_GFNI(Op, dl, DAG, Subtarget);

if (VT.isVector())
return LowerVectorCTLZ(Op, dl, Subtarget, DAG);

Expand Down
Loading
Loading