From a2623b33c2eaca9db8444461185af7b6c4b70fcf Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Sat, 14 Mar 2020 13:11:03 +0000 Subject: [PATCH] [X86] getFauxShuffleMask - merge insertelement paths Merge the INSERT_VECTOR_ELT/SCALAR_TO_VECTOR and PINSRW/PINSRB shuffle mask paths - they both do the same thing (find source vector + handle implicit zero extension). The PINSRW/PINSRB path also handled in the insertion of zero case which needed to be added to the general case as well. --- lib/Target/X86/X86ISelLowering.cpp | 72 +++++++++--------------------- 1 file changed, 22 insertions(+), 50 deletions(-) diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index 521dc578da9..ef324c05489 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -7373,15 +7373,34 @@ static bool getFauxShuffleMask(SDValue N, const APInt &DemandedElts, } return true; } + case X86ISD::PINSRB: + case X86ISD::PINSRW: case ISD::SCALAR_TO_VECTOR: case ISD::INSERT_VECTOR_ELT: { // Match against a insert_vector_elt/scalar_to_vector of an extract from a // vector, for matching src/dst vector types. - // TODO: Merge with PINSRB/PINSRW cases below. - // TODO: Handle truncate/zext/shift of scalars. SDValue Scl = N.getOperand(Opcode == ISD::SCALAR_TO_VECTOR ? 0 : 1); - SDValue SrcExtract; + unsigned DstIdx = 0; + if (Opcode != ISD::SCALAR_TO_VECTOR) { + // Check we have an in-range constant insertion index. + if (!isa(N.getOperand(2)) || + N.getConstantOperandAPInt(2).uge(NumElts)) + return false; + DstIdx = N.getConstantOperandVal(2); + + // Attempt to recognise an INSERT*(VEC, 0, DstIdx) shuffle pattern. + if (X86::isZeroNode(Scl)) { + Ops.push_back(N.getOperand(0)); + for (unsigned i = 0; i != NumElts; ++i) + Mask.push_back(i == DstIdx ? SM_SentinelZero : (int)i); + return true; + } + } + + // Attempt to find the source vector the scalar was extracted from. + // TODO: Handle truncate/zext/shift of scalars. + SDValue SrcExtract; if ((Scl.getOpcode() == ISD::EXTRACT_VECTOR_ELT && Scl.getOperand(0).getValueType() == VT) || (Scl.getOpcode() == X86ISD::PEXTRW && @@ -7390,12 +7409,8 @@ static bool getFauxShuffleMask(SDValue N, const APInt &DemandedElts, Scl.getOperand(0).getValueType() == MVT::v16i8)) { SrcExtract = Scl; } - if (!SrcExtract || !isa(SrcExtract.getOperand(1))) return false; - if (Opcode != ISD::SCALAR_TO_VECTOR && - !isa(N.getOperand(2))) - return false; SDValue SrcVec = SrcExtract.getOperand(0); EVT SrcVT = SrcVec.getValueType(); @@ -7411,11 +7426,6 @@ static bool getFauxShuffleMask(SDValue N, const APInt &DemandedElts, if (NumSrcElts <= SrcIdx) return false; - unsigned DstIdx = - Opcode == ISD::SCALAR_TO_VECTOR ? 0 : N.getConstantOperandVal(2); - if (NumElts <= DstIdx) - return false; - if (Opcode == ISD::SCALAR_TO_VECTOR) { Ops.push_back(SrcVec); Mask.append(NumSrcElts, SM_SentinelUndef); @@ -7432,44 +7442,6 @@ static bool getFauxShuffleMask(SDValue N, const APInt &DemandedElts, Mask[(Scale * DstIdx) + i + 1] = SM_SentinelZero; return true; } - case X86ISD::PINSRB: - case X86ISD::PINSRW: { - SDValue InVec = N.getOperand(0); - SDValue InScl = N.getOperand(1); - SDValue InIndex = N.getOperand(2); - if (!isa(InIndex) || - cast(InIndex)->getAPIntValue().uge(NumElts)) - return false; - uint64_t InIdx = N.getConstantOperandVal(2); - - // Attempt to recognise a PINSR*(VEC, 0, Idx) shuffle pattern. - if (X86::isZeroNode(InScl)) { - Ops.push_back(InVec); - for (unsigned i = 0; i != NumElts; ++i) - Mask.push_back(i == InIdx ? SM_SentinelZero : (int)i); - return true; - } - - // Attempt to recognise a PINSR*(PEXTR*) shuffle pattern. - // TODO: Expand this to support INSERT_VECTOR_ELT/etc. - unsigned ExOp = - (X86ISD::PINSRB == Opcode ? X86ISD::PEXTRB : X86ISD::PEXTRW); - if (InScl.getOpcode() != ExOp) - return false; - - SDValue ExVec = InScl.getOperand(0); - SDValue ExIndex = InScl.getOperand(1); - if (!isa(ExIndex) || - cast(ExIndex)->getAPIntValue().uge(NumElts)) - return false; - uint64_t ExIdx = InScl.getConstantOperandVal(1); - - Ops.push_back(InVec); - Ops.push_back(ExVec); - for (unsigned i = 0; i != NumElts; ++i) - Mask.push_back(i == InIdx ? NumElts + ExIdx : i); - return true; - } case X86ISD::PACKSS: case X86ISD::PACKUS: { SDValue N0 = N.getOperand(0);