From 43f3bd310b2e41335571ee10953ffaeeef16d2e3 Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Fri, 28 Apr 2006 07:03:38 +0000 Subject: [PATCH] Implement four-wide shuffle with 2 shufps if no more than two elements come from each vector. e.g. shuffle(G1, G2, 7, 1, 5, 2) ==> movaps _G2, %xmm0 shufps $151, _G1, %xmm0 shufps $216, %xmm0, %xmm0 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28011 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/X86/X86ISelLowering.cpp | 49 ++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index b0dd6a6cee9..06c6d80c43b 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -2676,10 +2676,55 @@ X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) { } if (NumElems == 4) { - // Break it into (shuffle shuffle_hi, shuffle_lo). MVT::ValueType MaskVT = PermMask.getValueType(); MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT); - std::map > Locs; + std::vector > Locs; + Locs.reserve(NumElems); + std::vector Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); + std::vector Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); + unsigned NumHi = 0; + unsigned NumLo = 0; + // If no more than two elements come from either vector. This can be + // implemented with two shuffles. First shuffle gather the elements. + // The second shuffle, which takes the first shuffle as both of its + // vector operands, put the elements into the right order. + for (unsigned i = 0; i != NumElems; ++i) { + SDOperand Elt = PermMask.getOperand(i); + if (Elt.getOpcode() == ISD::UNDEF) { + Locs[i] = std::make_pair(-1, -1); + } else { + unsigned Val = cast(Elt)->getValue(); + if (Val < NumElems) { + Locs[i] = std::make_pair(0, NumLo); + Mask1[NumLo] = Elt; + NumLo++; + } else { + Locs[i] = std::make_pair(1, NumHi); + if (2+NumHi < NumElems) + Mask1[2+NumHi] = Elt; + NumHi++; + } + } + } + if (NumLo <= 2 && NumHi <= 2) { + V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, + DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1)); + for (unsigned i = 0; i != NumElems; ++i) { + if (Locs[i].first == -1) + continue; + else { + unsigned Idx = (i < NumElems/2) ? 0 : NumElems; + Idx += Locs[i].first * (NumElems/2) + Locs[i].second; + Mask2[i] = DAG.getConstant(Idx, MaskEVT); + } + } + + return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, + DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2)); + } + + // Break it into (shuffle shuffle_hi, shuffle_lo). + Locs.clear(); std::vector LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); std::vector HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT)); std::vector *MaskPtr = &LoMask;