Skip to content

Commit 73ed2a2

Browse files
committed
[X86] Cleanup the LowerMULH code by hoisting some commonalities between the vXi32 and vXi8 handling. NFCI
vXi32 support was recently moved from LowerMUL_LOHI to LowerMULH. This commit shares the getOperand calls, switches both to use common IsSigned flag, and hoists the NumElems/NumElts variable. llvm-svn: 340720
1 parent c330ca8 commit 73ed2a2

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

+20-24
Original file line numberDiff line numberDiff line change
@@ -22901,6 +22901,10 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2290122901
SelectionDAG &DAG) {
2290222902
SDLoc dl(Op);
2290322903
MVT VT = Op.getSimpleValueType();
22904+
bool IsSigned = Op->getOpcode() == ISD::MULHS;
22905+
unsigned NumElts = VT.getVectorNumElements();
22906+
SDValue A = Op.getOperand(0);
22907+
SDValue B = Op.getOperand(1);
2290422908

2290522909
// Decompose 256-bit ops into smaller 128-bit ops.
2290622910
if (VT.is256BitVector() && !Subtarget.hasInt256())
@@ -22910,9 +22914,6 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2291022914
assert((VT == MVT::v4i32 && Subtarget.hasSSE2()) ||
2291122915
(VT == MVT::v8i32 && Subtarget.hasInt256()) ||
2291222916
(VT == MVT::v16i32 && Subtarget.hasAVX512()));
22913-
SDValue Op0 = Op.getOperand(0), Op1 = Op.getOperand(1);
22914-
22915-
int NumElts = VT.getVectorNumElements();
2291622917

2291722918
// PMULxD operations multiply each even value (starting at 0) of LHS with
2291822919
// the related value of RHS and produce a widen result.
@@ -22929,23 +22930,22 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2292922930
const int Mask[] = {1, -1, 3, -1, 5, -1, 7, -1,
2293022931
9, -1, 11, -1, 13, -1, 15, -1};
2293122932
// <a|b|c|d> => <b|undef|d|undef>
22932-
SDValue Odd0 = DAG.getVectorShuffle(VT, dl, Op0, Op0,
22933+
SDValue Odd0 = DAG.getVectorShuffle(VT, dl, A, A,
2293322934
makeArrayRef(&Mask[0], NumElts));
2293422935
// <e|f|g|h> => <f|undef|h|undef>
22935-
SDValue Odd1 = DAG.getVectorShuffle(VT, dl, Op1, Op1,
22936+
SDValue Odd1 = DAG.getVectorShuffle(VT, dl, B, B,
2293622937
makeArrayRef(&Mask[0], NumElts));
2293722938

2293822939
// Emit two multiplies, one for the lower 2 ints and one for the higher 2
2293922940
// ints.
2294022941
MVT MulVT = MVT::getVectorVT(MVT::i64, NumElts / 2);
22941-
bool IsSigned = Op->getOpcode() == ISD::MULHS;
2294222942
unsigned Opcode =
22943-
(!IsSigned || !Subtarget.hasSSE41()) ? X86ISD::PMULUDQ : X86ISD::PMULDQ;
22943+
(IsSigned && Subtarget.hasSSE41()) ? X86ISD::PMULDQ : X86ISD::PMULUDQ;
2294422944
// PMULUDQ <4 x i32> <a|b|c|d>, <4 x i32> <e|f|g|h>
2294522945
// => <2 x i64> <ae|cg>
2294622946
SDValue Mul1 = DAG.getBitcast(VT, DAG.getNode(Opcode, dl, MulVT,
22947-
DAG.getBitcast(MulVT, Op0),
22948-
DAG.getBitcast(MulVT, Op1)));
22947+
DAG.getBitcast(MulVT, A),
22948+
DAG.getBitcast(MulVT, B)));
2294922949
// PMULUDQ <4 x i32> <b|undef|d|undef>, <4 x i32> <f|undef|h|undef>
2295022950
// => <2 x i64> <bf|dh>
2295122951
SDValue Mul2 = DAG.getBitcast(VT, DAG.getNode(Opcode, dl, MulVT,
@@ -22954,7 +22954,7 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2295422954

2295522955
// Shuffle it back into the right order.
2295622956
SmallVector<int, 16> ShufMask(NumElts);
22957-
for (int i = 0; i != NumElts; ++i)
22957+
for (int i = 0; i != (int)NumElts; ++i)
2295822958
ShufMask[i] = (i / 2) * 2 + ((i % 2) * NumElts) + 1;
2295922959

2296022960
SDValue Res = DAG.getVectorShuffle(VT, dl, Mul1, Mul2, ShufMask);
@@ -22964,9 +22964,9 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2296422964
if (IsSigned && !Subtarget.hasSSE41()) {
2296522965
SDValue ShAmt = DAG.getConstant(31, dl, VT);
2296622966
SDValue T1 = DAG.getNode(ISD::AND, dl, VT,
22967-
DAG.getNode(ISD::SRA, dl, VT, Op0, ShAmt), Op1);
22967+
DAG.getNode(ISD::SRA, dl, VT, A, ShAmt), B);
2296822968
SDValue T2 = DAG.getNode(ISD::AND, dl, VT,
22969-
DAG.getNode(ISD::SRA, dl, VT, Op1, ShAmt), Op0);
22969+
DAG.getNode(ISD::SRA, dl, VT, B, ShAmt), A);
2297022970

2297122971
SDValue Fixup = DAG.getNode(ISD::ADD, dl, VT, T1, T2);
2297222972
Res = DAG.getNode(ISD::SUB, dl, VT, Res, Fixup);
@@ -22982,14 +22982,11 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2298222982

2298322983
// Lower v16i8/v32i8 as extension to v8i16/v16i16 vector pairs, multiply,
2298422984
// logical shift down the upper half and pack back to i8.
22985-
SDValue A = Op.getOperand(0);
22986-
SDValue B = Op.getOperand(1);
2298722985

2298822986
// With SSE41 we can use sign/zero extend, but for pre-SSE41 we unpack
2298922987
// and then ashr/lshr the upper bits down to the lower bits before multiply.
22990-
unsigned Opcode = Op.getOpcode();
22991-
unsigned ExShift = (ISD::MULHU == Opcode ? X86ISD::VSRLI : X86ISD::VSRAI);
22992-
unsigned ExAVX = (ISD::MULHU == Opcode ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND);
22988+
unsigned ExShift = IsSigned ? X86ISD::VSRAI : X86ISD::VSRLI;
22989+
unsigned ExAVX = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
2299322990

2299422991
// For 512-bit vectors, split into 256-bit vectors to allow the
2299522992
// sign-extension to occur.
@@ -22998,9 +22995,8 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2299822995

2299922996
// AVX2 implementations - extend xmm subvectors to ymm.
2300022997
if (Subtarget.hasInt256()) {
23001-
unsigned NumElems = VT.getVectorNumElements();
2300222998
SDValue Lo = DAG.getIntPtrConstant(0, dl);
23003-
SDValue Hi = DAG.getIntPtrConstant(NumElems / 2, dl);
22999+
SDValue Hi = DAG.getIntPtrConstant(NumElts / 2, dl);
2300423000

2300523001
if (VT == MVT::v32i8) {
2300623002
if (Subtarget.canExtendTo512BW()) {
@@ -23014,8 +23010,8 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2301423010
MVT ExVT = MVT::v16i16;
2301523011
SDValue ALo = extract128BitVector(A, 0, DAG, dl);
2301623012
SDValue BLo = extract128BitVector(B, 0, DAG, dl);
23017-
SDValue AHi = extract128BitVector(A, NumElems / 2, DAG, dl);
23018-
SDValue BHi = extract128BitVector(B, NumElems / 2, DAG, dl);
23013+
SDValue AHi = extract128BitVector(A, NumElts / 2, DAG, dl);
23014+
SDValue BHi = extract128BitVector(B, NumElts / 2, DAG, dl);
2301923015
ALo = DAG.getNode(ExAVX, dl, ExVT, ALo);
2302023016
BLo = DAG.getNode(ExAVX, dl, ExVT, BLo);
2302123017
AHi = DAG.getNode(ExAVX, dl, ExVT, AHi);
@@ -23054,8 +23050,8 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2305423050
assert(VT == MVT::v16i8 &&
2305523051
"Pre-AVX2 support only supports v16i8 multiplication");
2305623052
MVT ExVT = MVT::v8i16;
23057-
unsigned ExSSE41 = ISD::MULHU == Opcode ? ISD::ZERO_EXTEND_VECTOR_INREG
23058-
: ISD::SIGN_EXTEND_VECTOR_INREG;
23053+
unsigned ExSSE41 = IsSigned ? ISD::SIGN_EXTEND_VECTOR_INREG
23054+
: ISD::ZERO_EXTEND_VECTOR_INREG;
2305923055

2306023056
// Extract the lo parts and zero/sign extend to i16.
2306123057
SDValue ALo, BLo;
@@ -23076,7 +23072,7 @@ static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget,
2307623072
// Extract the hi parts and zero/sign extend to i16.
2307723073
SDValue AHi, BHi;
2307823074
if (Subtarget.hasSSE41()) {
23079-
const int ShufMask[] = {8, 9, 10, 11, 12, 13, 14, 15,
23075+
const int ShufMask[] = { 8, 9, 10, 11, 12, 13, 14, 15,
2308023076
-1, -1, -1, -1, -1, -1, -1, -1};
2308123077
AHi = DAG.getVectorShuffle(VT, dl, A, A, ShufMask);
2308223078
BHi = DAG.getVectorShuffle(VT, dl, B, B, ShufMask);

0 commit comments

Comments
 (0)