Skip to content

Commit 5877500

Browse files
committed
[X86] Use GFNI for vXi8 per-element shifts
As detailed here: https://github.com/InstLatx64/InstLatX64_Demo/blob/master/GFNI_Demo.h These are a bit more complicated than gf2p8affine look ups, requiring us to convert a SHL shift value / amount into a GF so we can perform a multiplication. SRL/SRA need to be converted to SHL via bitreverse/variable-sign-extension. Followup to #89115
1 parent f94ed6f commit 5877500

File tree

3 files changed

+1435
-2411
lines changed

3 files changed

+1435
-2411
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29564,6 +29564,62 @@ static SDValue LowerShift(SDValue Op, const X86Subtarget &Subtarget,
2956429564
DAG.getNode(Opc, dl, ExtVT, R, Amt));
2956529565
}
2956629566

29567+
// GFNI - we can perform SHL with a GF multiplication, and can convert
29568+
// SRL/SRA to a SHL.
29569+
if (VT == MVT::v16i8 ||
29570+
(VT == MVT::v32i8 && Subtarget.hasInt256() && !Subtarget.hasXOP()) ||
29571+
(VT == MVT::v64i8 && Subtarget.hasBWI())) {
29572+
if (Subtarget.hasGFNI() && Subtarget.hasSSSE3()) {
29573+
auto GFShiftLeft = [&](SDValue Val) {
29574+
// Use PSHUFB as a LUT from the shift amount to create a per-element
29575+
// byte mask for the shift value and an index. For shift amounts greater
29576+
// than 7, the result will be zero.
29577+
SmallVector<APInt, 8> MaskBits, IdxBits;
29578+
for (unsigned I = 0, E = VT.getSizeInBits() / 128; I != E; ++I) {
29579+
MaskBits.push_back(APInt(64, 0x0103070F1F3F7FFFULL));
29580+
IdxBits.push_back(APInt(64, 0x8040201008040201ULL));
29581+
MaskBits.push_back(APInt::getZero(64));
29582+
IdxBits.push_back(APInt::getZero(64));
29583+
}
29584+
29585+
MVT CVT = MVT::getVectorVT(MVT::i64, VT.getSizeInBits() / 64);
29586+
SDValue Mask =
29587+
DAG.getBitcast(VT, getConstVector(MaskBits, CVT, DAG, dl));
29588+
SDValue Idx = DAG.getBitcast(VT, getConstVector(IdxBits, CVT, DAG, dl));
29589+
Mask = DAG.getNode(X86ISD::PSHUFB, dl, VT, Mask, Amt);
29590+
Idx = DAG.getNode(X86ISD::PSHUFB, dl, VT, Idx, Amt);
29591+
Mask = DAG.getNode(ISD::AND, dl, VT, Val, Mask);
29592+
return DAG.getNode(X86ISD::GF2P8MULB, dl, VT, Mask, Idx);
29593+
};
29594+
29595+
if (Opc == ISD::SHL)
29596+
return GFShiftLeft(R);
29597+
29598+
// srl(x,y)
29599+
// --> bitreverse(shl(bitreverse(x),y))
29600+
if (Opc == ISD::SRL) {
29601+
R = DAG.getNode(ISD::BITREVERSE, dl, VT, R);
29602+
R = GFShiftLeft(R);
29603+
return DAG.getNode(ISD::BITREVERSE, dl, VT, R);
29604+
}
29605+
29606+
// sra(x,y)
29607+
// --> sub(xor(srl(x,y), m),m)
29608+
// --> sub(xor(bitreverse(shl(bitreverse(x),y)), m),m)
29609+
// where m = srl(signbit, amt) --> bitreverse(shl(lsb, amt))
29610+
if (Opc == ISD::SRA) {
29611+
SDValue LSB = DAG.getConstant(APInt::getOneBitSet(8, 0), dl, VT);
29612+
SDValue M = DAG.getNode(ISD::BITREVERSE, dl, VT, GFShiftLeft(LSB));
29613+
R = DAG.getNode(ISD::BITREVERSE, dl, VT, R);
29614+
R = GFShiftLeft(R);
29615+
R = DAG.getNode(ISD::BITREVERSE, dl, VT, R);
29616+
R = DAG.getNode(ISD::XOR, dl, VT, R, M);
29617+
R = DAG.getNode(ISD::SUB, dl, VT, R, M);
29618+
return R;
29619+
}
29620+
}
29621+
}
29622+
2956729623
// Constant ISD::SRA/SRL can be performed efficiently on vXi8 vectors as we
2956829624
// extend to vXi16 to perform a MUL scale effectively as a MUL_LOHI.
2956929625
if (ConstantAmt && (Opc == ISD::SRA || Opc == ISD::SRL) &&
@@ -55614,6 +55670,15 @@ static SDValue combineConcatVectorOps(const SDLoc &DL, MVT VT,
5561455670
ConcatSubOperand(VT, Ops, 0));
5561555671
}
5561655672
break;
55673+
case X86ISD::GF2P8MULB:
55674+
if (!IsSplat &&
55675+
(VT.is256BitVector() ||
55676+
(VT.is512BitVector() && Subtarget.useAVX512Regs()))) {
55677+
return DAG.getNode(Op0.getOpcode(), DL, VT,
55678+
ConcatSubOperand(VT, Ops, 0),
55679+
ConcatSubOperand(VT, Ops, 1));
55680+
}
55681+
break;
5561755682
case X86ISD::GF2P8AFFINEQB:
5561855683
if (!IsSplat &&
5561955684
(VT.is256BitVector() ||

0 commit comments

Comments
 (0)