Skip to content

[X86] Add i8 CTPOP lowering using i32 MUL #79989

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
Feb 2, 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
28 changes: 23 additions & 5 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
// on the dest that popcntl hasn't had since Cannon Lake.
setOperationPromotedToType(ISD::CTPOP, MVT::i16, MVT::i32);
} else {
setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
setOperationAction(ISD::CTPOP , MVT::i8 , Custom);
setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
if (Subtarget.is64Bit())
Expand Down Expand Up @@ -30989,12 +30989,12 @@ static SDValue LowerVectorCTPOPInRegLUT(SDValue Op, const SDLoc &DL,

// Please ensure that any codegen change from LowerVectorCTPOP is reflected in
// updated cost models in X86TTIImpl::getIntrinsicInstrCost.
static SDValue LowerVectorCTPOP(SDValue Op, const X86Subtarget &Subtarget,
static SDValue LowerVectorCTPOP(SDValue Op, const SDLoc &DL,
const X86Subtarget &Subtarget,
SelectionDAG &DAG) {
MVT VT = Op.getSimpleValueType();
assert((VT.is512BitVector() || VT.is256BitVector() || VT.is128BitVector()) &&
"Unknown CTPOP type to handle");
SDLoc DL(Op.getNode());
SDValue Op0 = Op.getOperand(0);

// TRUNC(CTPOP(ZEXT(X))) to make use of vXi32/vXi64 VPOPCNT instructions.
Expand Down Expand Up @@ -31035,9 +31035,27 @@ static SDValue LowerVectorCTPOP(SDValue Op, const X86Subtarget &Subtarget,

static SDValue LowerCTPOP(SDValue Op, const X86Subtarget &Subtarget,
SelectionDAG &DAG) {
assert(Op.getSimpleValueType().isVector() &&
MVT VT = Op.getSimpleValueType();
SDLoc DL(Op);

// i8 CTPOP - with efficient i32 MUL, then attempt multiply-mask-multiply.
if (VT == MVT::i8) {
SDValue Mask11 = DAG.getConstant(0x11111111U, DL, MVT::i32);
Op = DAG.getZExtOrTrunc(Op.getOperand(0), DL, MVT::i32);
Op = DAG.getNode(ISD::MUL, DL, MVT::i32, Op,
DAG.getConstant(0x08040201U, DL, MVT::i32));
Op = DAG.getNode(ISD::SRL, DL, MVT::i32, Op,
DAG.getShiftAmountConstant(3, MVT::i32, DL));
Op = DAG.getNode(ISD::AND, DL, MVT::i32, Op, Mask11);
Op = DAG.getNode(ISD::MUL, DL, MVT::i32, Op, Mask11);
Op = DAG.getNode(ISD::SRL, DL, MVT::i32, Op,
DAG.getShiftAmountConstant(28, MVT::i32, DL));
return DAG.getZExtOrTrunc(Op, DL, VT);
}

assert(VT.isVector() &&
"We only do custom lowering for vector population count.");
return LowerVectorCTPOP(Op, Subtarget, DAG);
return LowerVectorCTPOP(Op, DL, Subtarget, DAG);
}

static SDValue LowerBITREVERSE_XOP(SDValue Op, SelectionDAG &DAG) {
Expand Down
21 changes: 7 additions & 14 deletions llvm/test/CodeGen/X86/ctpop-combine.ll
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,13 @@ define i8 @test4(i8 %x) nounwind readnone {
;
; NO-POPCOUNT-LABEL: test4:
; NO-POPCOUNT: # %bb.0:
; NO-POPCOUNT-NEXT: movl %edi, %ecx
; NO-POPCOUNT-NEXT: andb $127, %cl
; NO-POPCOUNT-NEXT: shrb %dil
; NO-POPCOUNT-NEXT: andb $21, %dil
; NO-POPCOUNT-NEXT: subb %dil, %cl
; NO-POPCOUNT-NEXT: movl %ecx, %eax
; NO-POPCOUNT-NEXT: andb $51, %al
; NO-POPCOUNT-NEXT: shrb $2, %cl
; NO-POPCOUNT-NEXT: andb $51, %cl
; NO-POPCOUNT-NEXT: addb %al, %cl
; NO-POPCOUNT-NEXT: movl %ecx, %eax
; NO-POPCOUNT-NEXT: shrb $4, %al
; NO-POPCOUNT-NEXT: addb %cl, %al
; NO-POPCOUNT-NEXT: andb $15, %al
; NO-POPCOUNT-NEXT: andl $127, %edi
; NO-POPCOUNT-NEXT: imull $134480385, %edi, %eax # imm = 0x8040201
; NO-POPCOUNT-NEXT: shrl $3, %eax
; NO-POPCOUNT-NEXT: andl $286331153, %eax # imm = 0x11111111
; NO-POPCOUNT-NEXT: imull $286331153, %eax, %eax # imm = 0x11111111
; NO-POPCOUNT-NEXT: shrl $28, %eax
; NO-POPCOUNT-NEXT: # kill: def $al killed $al killed $eax
; NO-POPCOUNT-NEXT: retq
%x2 = and i8 %x, 127
%count = tail call i8 @llvm.ctpop.i8(i8 %x2)
Expand Down
Loading