David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 1 | //===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===// |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Define several functions to decode x86 specific shuffle semantics into a |
| 11 | // generic vector mask. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 15 | #include "X86ShuffleDecode.h" |
Chandler Carruth | 185cc18 | 2014-07-25 23:47:11 +0000 | [diff] [blame^] | 16 | #include "llvm/IR/Constants.h" |
Patrik Hagglund | 8d09a6c | 2014-03-15 09:11:41 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineValueType.h" |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // Vector Mask Decoding |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 23 | namespace llvm { |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 24 | |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 25 | void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Bruno Cardoso Lopes | 02a05a6 | 2010-09-02 22:43:39 +0000 | [diff] [blame] | 26 | // Defaults the copying the dest value. |
| 27 | ShuffleMask.push_back(0); |
| 28 | ShuffleMask.push_back(1); |
| 29 | ShuffleMask.push_back(2); |
| 30 | ShuffleMask.push_back(3); |
| 31 | |
| 32 | // Decode the immediate. |
| 33 | unsigned ZMask = Imm & 15; |
| 34 | unsigned CountD = (Imm >> 4) & 3; |
| 35 | unsigned CountS = (Imm >> 6) & 3; |
| 36 | |
| 37 | // CountS selects which input element to use. |
| 38 | unsigned InVal = 4+CountS; |
| 39 | // CountD specifies which element of destination to update. |
| 40 | ShuffleMask[CountD] = InVal; |
| 41 | // ZMask zaps values, potentially overriding the CountD elt. |
| 42 | if (ZMask & 1) ShuffleMask[0] = SM_SentinelZero; |
| 43 | if (ZMask & 2) ShuffleMask[1] = SM_SentinelZero; |
| 44 | if (ZMask & 4) ShuffleMask[2] = SM_SentinelZero; |
| 45 | if (ZMask & 8) ShuffleMask[3] = SM_SentinelZero; |
| 46 | } |
| 47 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 48 | // <3,1> or <6,7,2,3> |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 49 | void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 50 | for (unsigned i = NElts/2; i != NElts; ++i) |
| 51 | ShuffleMask.push_back(NElts+i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 52 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 53 | for (unsigned i = NElts/2; i != NElts; ++i) |
| 54 | ShuffleMask.push_back(i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 57 | // <0,2> or <0,1,4,5> |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 58 | void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 59 | for (unsigned i = 0; i != NElts/2; ++i) |
| 60 | ShuffleMask.push_back(i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 61 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 62 | for (unsigned i = 0; i != NElts/2; ++i) |
| 63 | ShuffleMask.push_back(NElts+i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Craig Topper | 8fb09f0 | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 66 | void DecodePALIGNRMask(MVT VT, unsigned Imm, |
| 67 | SmallVectorImpl<int> &ShuffleMask) { |
Benjamin Kramer | 6a93596 | 2013-01-26 13:31:37 +0000 | [diff] [blame] | 68 | unsigned NumElts = VT.getVectorNumElements(); |
| 69 | unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8); |
| 70 | |
Craig Topper | 5c68397 | 2013-01-28 07:41:18 +0000 | [diff] [blame] | 71 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 72 | unsigned NumLaneElts = NumElts / NumLanes; |
| 73 | |
| 74 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 75 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 76 | unsigned Base = i + Offset; |
| 77 | // if i+offset is out of this lane then we actually need the other source |
| 78 | if (Base >= NumLaneElts) Base += NumElts - NumLaneElts; |
| 79 | ShuffleMask.push_back(Base + l); |
| 80 | } |
| 81 | } |
Benjamin Kramer | 6a93596 | 2013-01-26 13:31:37 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 84 | /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*. |
| 85 | /// VT indicates the type of the vector allowing it to handle different |
| 86 | /// datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 87 | void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 88 | unsigned NumElts = VT.getVectorNumElements(); |
| 89 | |
| 90 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 91 | unsigned NumLaneElts = NumElts / NumLanes; |
| 92 | |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 93 | unsigned NewImm = Imm; |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 94 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 95 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 96 | ShuffleMask.push_back(NewImm % NumLaneElts + l); |
| 97 | NewImm /= NumLaneElts; |
| 98 | } |
| 99 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 103 | void DecodePSHUFHWMask(MVT VT, unsigned Imm, |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 104 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 315a5cc | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 105 | unsigned NumElts = VT.getVectorNumElements(); |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 106 | |
| 107 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 108 | unsigned NewImm = Imm; |
| 109 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 110 | ShuffleMask.push_back(l + i); |
| 111 | } |
| 112 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 113 | ShuffleMask.push_back(l + 4 + (NewImm & 3)); |
| 114 | NewImm >>= 2; |
| 115 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 119 | void DecodePSHUFLWMask(MVT VT, unsigned Imm, |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 120 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 315a5cc | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 121 | unsigned NumElts = VT.getVectorNumElements(); |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 122 | |
| 123 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 124 | unsigned NewImm = Imm; |
| 125 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 126 | ShuffleMask.push_back(l + (NewImm & 3)); |
| 127 | NewImm >>= 2; |
| 128 | } |
| 129 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 130 | ShuffleMask.push_back(l + i); |
| 131 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 132 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 135 | /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates |
| 136 | /// the type of the vector allowing it to handle different datatypes and vector |
| 137 | /// widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 138 | void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | c16db84 | 2011-11-29 07:49:05 +0000 | [diff] [blame] | 139 | unsigned NumElts = VT.getVectorNumElements(); |
| 140 | |
| 141 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 142 | unsigned NumLaneElts = NumElts / NumLanes; |
| 143 | |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 144 | unsigned NewImm = Imm; |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 145 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 146 | // each half of a lane comes from different source |
| 147 | for (unsigned s = 0; s != NumElts*2; s += NumElts) { |
| 148 | for (unsigned i = 0; i != NumLaneElts/2; ++i) { |
| 149 | ShuffleMask.push_back(NewImm % NumLaneElts + s + l); |
| 150 | NewImm /= NumLaneElts; |
| 151 | } |
Craig Topper | c16db84 | 2011-11-29 07:49:05 +0000 | [diff] [blame] | 152 | } |
| 153 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 157 | /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd |
| 158 | /// and punpckh*. VT indicates the type of the vector allowing it to handle |
| 159 | /// different datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 160 | void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 161 | unsigned NumElts = VT.getVectorNumElements(); |
| 162 | |
| 163 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 164 | // independently on 128-bit lanes. |
| 165 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 166 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 167 | unsigned NumLaneElts = NumElts / NumLanes; |
| 168 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 169 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 170 | for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 171 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 172 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
| 173 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Craig Topper | 3cb802c | 2011-12-06 05:31:16 +0000 | [diff] [blame] | 177 | /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 178 | /// and punpckl*. VT indicates the type of the vector allowing it to handle |
| 179 | /// different datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 180 | void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 181 | unsigned NumElts = VT.getVectorNumElements(); |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 182 | |
Bruno Cardoso Lopes | f8fe47b | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 183 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 184 | // independently on 128-bit lanes. |
| 185 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 186 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 187 | unsigned NumLaneElts = NumElts / NumLanes; |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 188 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 189 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 190 | for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 191 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 192 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 193 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 197 | void DecodeVPERM2X128Mask(MVT VT, unsigned Imm, |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 198 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 354103d | 2012-04-17 05:54:54 +0000 | [diff] [blame] | 199 | if (Imm & 0x88) |
| 200 | return; // Not a shuffle |
| 201 | |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 202 | unsigned HalfSize = VT.getVectorNumElements()/2; |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 203 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 204 | for (unsigned l = 0; l != 2; ++l) { |
| 205 | unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize; |
| 206 | for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i) |
| 207 | ShuffleMask.push_back(i); |
| 208 | } |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Chandler Carruth | 185cc18 | 2014-07-25 23:47:11 +0000 | [diff] [blame^] | 211 | /// \brief Decode PSHUFB masks stored in an LLVM Constant. |
| 212 | void DecodePSHUFBMask(const ConstantDataSequential *C, |
| 213 | SmallVectorImpl<int> &ShuffleMask) { |
| 214 | Type *MaskTy = C->getType(); |
| 215 | assert(MaskTy->isVectorTy() && "Expected a vector constant mask!"); |
| 216 | Type *EltTy = MaskTy->getVectorElementType(); |
| 217 | assert(EltTy->isIntegerTy(8) && "Expected i8 constant mask elements!"); |
| 218 | int NumElements = MaskTy->getVectorNumElements(); |
| 219 | // FIXME: Add support for AVX-512. |
| 220 | assert((NumElements == 16 || NumElements == 32) && |
| 221 | "Only 128-bit and 256-bit vectors supported!"); |
| 222 | assert((unsigned)NumElements == C->getNumElements() && |
| 223 | "Constant mask has a different number of elements!"); |
| 224 | |
| 225 | ShuffleMask.reserve(NumElements); |
| 226 | for (int i = 0; i < NumElements; ++i) { |
| 227 | // For AVX vectors with 32 bytes the base of the shuffle is the half of the |
| 228 | // vector we're inside. |
| 229 | int Base = i < 16 ? 0 : 16; |
| 230 | uint64_t Element = C->getElementAsInteger(i); |
| 231 | // If the high bit (7) of the byte is set, the element is zeroed. |
| 232 | if (Element & (1 << 7)) |
| 233 | ShuffleMask.push_back(SM_SentinelZero); |
| 234 | else { |
| 235 | int Index = Base + Element; |
| 236 | assert((Index >= 0 && Index < NumElements) || |
| 237 | "Out of bounds shuffle index for pshub instruction!"); |
| 238 | ShuffleMask.push_back(Index); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Craig Topper | 54bdb35 | 2012-05-06 18:44:02 +0000 | [diff] [blame] | 243 | /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD. |
| 244 | /// No VT provided since it only works on 256-bit, 4 element vectors. |
| 245 | void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 246 | for (unsigned i = 0; i != 4; ++i) { |
| 247 | ShuffleMask.push_back((Imm >> (2*i)) & 3); |
| 248 | } |
| 249 | } |
| 250 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 251 | } // llvm namespace |