NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 1 | //===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===// |
| 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 | |
| 15 | #include "X86ShuffleDecode.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineValueType.h" |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // Vector Mask Decoding |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 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. |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 38 | unsigned InVal = 4 + CountS; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 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 | |
Simon Pilgrim | a3d6744 | 2016-02-07 15:39:22 +0000 | [diff] [blame] | 48 | void DecodeInsertElementMask(MVT VT, unsigned Idx, unsigned Len, |
| 49 | SmallVectorImpl<int> &ShuffleMask) { |
| 50 | unsigned NumElts = VT.getVectorNumElements(); |
| 51 | assert((Idx + Len) <= NumElts && "Insertion out of range"); |
| 52 | |
| 53 | for (unsigned i = 0; i != NumElts; ++i) |
| 54 | ShuffleMask.push_back(i); |
| 55 | for (unsigned i = 0; i != Len; ++i) |
| 56 | ShuffleMask[Idx + i] = NumElts + i; |
| 57 | } |
| 58 | |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 59 | // <3,1> or <6,7,2,3> |
| 60 | void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 61 | for (unsigned i = NElts / 2; i != NElts; ++i) |
| 62 | ShuffleMask.push_back(NElts + i); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 63 | |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 64 | for (unsigned i = NElts / 2; i != NElts; ++i) |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 65 | ShuffleMask.push_back(i); |
| 66 | } |
| 67 | |
| 68 | // <0,2> or <0,1,4,5> |
| 69 | void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 70 | for (unsigned i = 0; i != NElts / 2; ++i) |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 71 | ShuffleMask.push_back(i); |
| 72 | |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 73 | for (unsigned i = 0; i != NElts / 2; ++i) |
| 74 | ShuffleMask.push_back(NElts + i); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void DecodeMOVSLDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
| 78 | unsigned NumElts = VT.getVectorNumElements(); |
| 79 | for (int i = 0, e = NumElts / 2; i < e; ++i) { |
| 80 | ShuffleMask.push_back(2 * i); |
| 81 | ShuffleMask.push_back(2 * i); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void DecodeMOVSHDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
| 86 | unsigned NumElts = VT.getVectorNumElements(); |
| 87 | for (int i = 0, e = NumElts / 2; i < e; ++i) { |
| 88 | ShuffleMask.push_back(2 * i + 1); |
| 89 | ShuffleMask.push_back(2 * i + 1); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void DecodeMOVDDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
| 94 | unsigned VectorSizeInBits = VT.getSizeInBits(); |
| 95 | unsigned ScalarSizeInBits = VT.getScalarSizeInBits(); |
| 96 | unsigned NumElts = VT.getVectorNumElements(); |
| 97 | unsigned NumLanes = VectorSizeInBits / 128; |
| 98 | unsigned NumLaneElts = NumElts / NumLanes; |
| 99 | unsigned NumLaneSubElts = 64 / ScalarSizeInBits; |
| 100 | |
| 101 | for (unsigned l = 0; l < NumElts; l += NumLaneElts) |
| 102 | for (unsigned i = 0; i < NumLaneElts; i += NumLaneSubElts) |
| 103 | for (unsigned s = 0; s != NumLaneSubElts; s++) |
| 104 | ShuffleMask.push_back(l + s); |
| 105 | } |
| 106 | |
| 107 | void DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 108 | unsigned VectorSizeInBits = VT.getSizeInBits(); |
| 109 | unsigned NumElts = VectorSizeInBits / 8; |
| 110 | unsigned NumLanes = VectorSizeInBits / 128; |
| 111 | unsigned NumLaneElts = NumElts / NumLanes; |
| 112 | |
| 113 | for (unsigned l = 0; l < NumElts; l += NumLaneElts) |
| 114 | for (unsigned i = 0; i < NumLaneElts; ++i) { |
| 115 | int M = SM_SentinelZero; |
| 116 | if (i >= Imm) M = i - Imm + l; |
| 117 | ShuffleMask.push_back(M); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void DecodePSRLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 122 | unsigned VectorSizeInBits = VT.getSizeInBits(); |
| 123 | unsigned NumElts = VectorSizeInBits / 8; |
| 124 | unsigned NumLanes = VectorSizeInBits / 128; |
| 125 | unsigned NumLaneElts = NumElts / NumLanes; |
| 126 | |
| 127 | for (unsigned l = 0; l < NumElts; l += NumLaneElts) |
| 128 | for (unsigned i = 0; i < NumLaneElts; ++i) { |
| 129 | unsigned Base = i + Imm; |
| 130 | int M = Base + l; |
| 131 | if (Base >= NumLaneElts) M = SM_SentinelZero; |
| 132 | ShuffleMask.push_back(M); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void DecodePALIGNRMask(MVT VT, unsigned Imm, |
| 137 | SmallVectorImpl<int> &ShuffleMask) { |
| 138 | unsigned NumElts = VT.getVectorNumElements(); |
Sanjay Patel | 1ed771f | 2016-09-14 16:37:15 +0000 | [diff] [blame] | 139 | unsigned Offset = Imm * (VT.getScalarSizeInBits() / 8); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 140 | |
| 141 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 142 | unsigned NumLaneElts = NumElts / NumLanes; |
| 143 | |
| 144 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 145 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 146 | unsigned Base = i + Offset; |
| 147 | // if i+offset is out of this lane then we actually need the other source |
| 148 | if (Base >= NumLaneElts) Base += NumElts - NumLaneElts; |
| 149 | ShuffleMask.push_back(Base + l); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Craig Topper | b084c90 | 2016-10-22 06:51:56 +0000 | [diff] [blame] | 154 | void DecodeVALIGNMask(MVT VT, unsigned Imm, |
| 155 | SmallVectorImpl<int> &ShuffleMask) { |
| 156 | int NumElts = VT.getVectorNumElements(); |
| 157 | // Not all bits of the immediate are used so mask it. |
| 158 | assert(isPowerOf2_32(NumElts) && "NumElts should be power of 2"); |
| 159 | Imm = Imm & (NumElts - 1); |
| 160 | for (int i = 0; i != NumElts; ++i) |
| 161 | ShuffleMask.push_back(i + Imm); |
| 162 | } |
| 163 | |
Simon Pilgrim | f8f86ab | 2015-09-13 11:28:45 +0000 | [diff] [blame] | 164 | /// DecodePSHUFMask - This decodes the shuffle masks for pshufw, pshufd, and vpermilp*. |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 165 | /// VT indicates the type of the vector allowing it to handle different |
| 166 | /// datatypes and vector widths. |
| 167 | void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 168 | unsigned NumElts = VT.getVectorNumElements(); |
| 169 | |
| 170 | unsigned NumLanes = VT.getSizeInBits() / 128; |
Simon Pilgrim | f8f86ab | 2015-09-13 11:28:45 +0000 | [diff] [blame] | 171 | if (NumLanes == 0) NumLanes = 1; // Handle MMX |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 172 | unsigned NumLaneElts = NumElts / NumLanes; |
| 173 | |
| 174 | unsigned NewImm = Imm; |
| 175 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 176 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 177 | ShuffleMask.push_back(NewImm % NumLaneElts + l); |
| 178 | NewImm /= NumLaneElts; |
| 179 | } |
| 180 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void DecodePSHUFHWMask(MVT VT, unsigned Imm, |
| 185 | SmallVectorImpl<int> &ShuffleMask) { |
| 186 | unsigned NumElts = VT.getVectorNumElements(); |
| 187 | |
| 188 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 189 | unsigned NewImm = Imm; |
| 190 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 191 | ShuffleMask.push_back(l + i); |
| 192 | } |
| 193 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 194 | ShuffleMask.push_back(l + 4 + (NewImm & 3)); |
| 195 | NewImm >>= 2; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void DecodePSHUFLWMask(MVT VT, unsigned Imm, |
| 201 | SmallVectorImpl<int> &ShuffleMask) { |
| 202 | unsigned NumElts = VT.getVectorNumElements(); |
| 203 | |
| 204 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 205 | unsigned NewImm = Imm; |
| 206 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 207 | ShuffleMask.push_back(l + (NewImm & 3)); |
| 208 | NewImm >>= 2; |
| 209 | } |
| 210 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 211 | ShuffleMask.push_back(l + i); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Simon Pilgrim | f8f86ab | 2015-09-13 11:28:45 +0000 | [diff] [blame] | 216 | void DecodePSWAPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
| 217 | unsigned NumElts = VT.getVectorNumElements(); |
| 218 | unsigned NumHalfElts = NumElts / 2; |
| 219 | |
| 220 | for (unsigned l = 0; l != NumHalfElts; ++l) |
| 221 | ShuffleMask.push_back(l + NumHalfElts); |
| 222 | for (unsigned h = 0; h != NumHalfElts; ++h) |
| 223 | ShuffleMask.push_back(h); |
| 224 | } |
| 225 | |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 226 | /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates |
| 227 | /// the type of the vector allowing it to handle different datatypes and vector |
| 228 | /// widths. |
| 229 | void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 230 | unsigned NumElts = VT.getVectorNumElements(); |
| 231 | |
| 232 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 233 | unsigned NumLaneElts = NumElts / NumLanes; |
| 234 | |
| 235 | unsigned NewImm = Imm; |
| 236 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 237 | // each half of a lane comes from different source |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 238 | for (unsigned s = 0; s != NumElts * 2; s += NumElts) { |
| 239 | for (unsigned i = 0; i != NumLaneElts / 2; ++i) { |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 240 | ShuffleMask.push_back(NewImm % NumLaneElts + s + l); |
| 241 | NewImm /= NumLaneElts; |
| 242 | } |
| 243 | } |
| 244 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd |
| 249 | /// and punpckh*. VT indicates the type of the vector allowing it to handle |
| 250 | /// different datatypes and vector widths. |
| 251 | void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
| 252 | unsigned NumElts = VT.getVectorNumElements(); |
| 253 | |
| 254 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 255 | // independently on 128-bit lanes. |
| 256 | unsigned NumLanes = VT.getSizeInBits() / 128; |
Simon Pilgrim | f8f86ab | 2015-09-13 11:28:45 +0000 | [diff] [blame] | 257 | if (NumLanes == 0) NumLanes = 1; // Handle MMX |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 258 | unsigned NumLaneElts = NumElts / NumLanes; |
| 259 | |
| 260 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 261 | for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) { |
| 262 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 263 | ShuffleMask.push_back(i + NumElts); // Reads from src/src2 |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd |
| 269 | /// and punpckl*. VT indicates the type of the vector allowing it to handle |
| 270 | /// different datatypes and vector widths. |
| 271 | void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
| 272 | unsigned NumElts = VT.getVectorNumElements(); |
| 273 | |
| 274 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 275 | // independently on 128-bit lanes. |
| 276 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 277 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 278 | unsigned NumLaneElts = NumElts / NumLanes; |
| 279 | |
| 280 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 281 | for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) { |
| 282 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 283 | ShuffleMask.push_back(i + NumElts); // Reads from src/src2 |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
Simon Pilgrim | c941f6b | 2016-07-18 17:32:59 +0000 | [diff] [blame] | 288 | /// Decodes a broadcast of the first element of a vector. |
| 289 | void DecodeVectorBroadcast(MVT DstVT, SmallVectorImpl<int> &ShuffleMask) { |
| 290 | unsigned NumElts = DstVT.getVectorNumElements(); |
| 291 | ShuffleMask.append(NumElts, 0); |
| 292 | } |
| 293 | |
Simon Pilgrim | a76a8e5 | 2016-07-14 12:07:43 +0000 | [diff] [blame] | 294 | /// Decodes a broadcast of a subvector to a larger vector type. |
| 295 | void DecodeSubVectorBroadcast(MVT DstVT, MVT SrcVT, |
| 296 | SmallVectorImpl<int> &ShuffleMask) { |
| 297 | assert(SrcVT.getScalarType() == DstVT.getScalarType() && |
| 298 | "Non matching vector element types"); |
| 299 | unsigned NumElts = SrcVT.getVectorNumElements(); |
| 300 | unsigned Scale = DstVT.getSizeInBits() / SrcVT.getSizeInBits(); |
| 301 | |
| 302 | for (unsigned i = 0; i != Scale; ++i) |
| 303 | for (unsigned j = 0; j != NumElts; ++j) |
| 304 | ShuffleMask.push_back(j); |
| 305 | } |
| 306 | |
Igor Breger | d7bae45 | 2015-10-15 13:29:07 +0000 | [diff] [blame] | 307 | /// \brief Decode a shuffle packed values at 128-bit granularity |
| 308 | /// (SHUFF32x4/SHUFF64x2/SHUFI32x4/SHUFI64x2) |
| 309 | /// immediate mask into a shuffle mask. |
| 310 | void decodeVSHUF64x2FamilyMask(MVT VT, unsigned Imm, |
| 311 | SmallVectorImpl<int> &ShuffleMask) { |
| 312 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 313 | unsigned NumElementsInLane = 128 / VT.getScalarSizeInBits(); |
| 314 | unsigned ControlBitsMask = NumLanes - 1; |
| 315 | unsigned NumControlBits = NumLanes / 2; |
| 316 | |
| 317 | for (unsigned l = 0; l != NumLanes; ++l) { |
| 318 | unsigned LaneMask = (Imm >> (l * NumControlBits)) & ControlBitsMask; |
| 319 | // We actually need the other source. |
| 320 | if (l >= NumLanes / 2) |
| 321 | LaneMask += NumLanes; |
| 322 | for (unsigned i = 0; i != NumElementsInLane; ++i) |
| 323 | ShuffleMask.push_back(LaneMask * NumElementsInLane + i); |
| 324 | } |
| 325 | } |
| 326 | |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 327 | void DecodeVPERM2X128Mask(MVT VT, unsigned Imm, |
| 328 | SmallVectorImpl<int> &ShuffleMask) { |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 329 | unsigned HalfSize = VT.getVectorNumElements() / 2; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 330 | |
| 331 | for (unsigned l = 0; l != 2; ++l) { |
Simon Pilgrim | 40343e6 | 2015-07-06 22:46:46 +0000 | [diff] [blame] | 332 | unsigned HalfMask = Imm >> (l * 4); |
| 333 | unsigned HalfBegin = (HalfMask & 0x3) * HalfSize; |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 334 | for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i) |
Denis Protivensky | b612902 | 2015-07-07 07:48:48 +0000 | [diff] [blame] | 335 | ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 339 | void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask, |
| 340 | SmallVectorImpl<int> &ShuffleMask) { |
| 341 | for (int i = 0, e = RawMask.size(); i < e; ++i) { |
| 342 | uint64_t M = RawMask[i]; |
| 343 | if (M == (uint64_t)SM_SentinelUndef) { |
| 344 | ShuffleMask.push_back(M); |
| 345 | continue; |
| 346 | } |
Simon Pilgrim | f33cb61 | 2016-03-03 21:55:01 +0000 | [diff] [blame] | 347 | // For 256/512-bit vectors the base of the shuffle is the 128-bit |
| 348 | // subvector we're inside. |
| 349 | int Base = (i / 16) * 16; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 350 | // If the high bit (7) of the byte is set, the element is zeroed. |
| 351 | if (M & (1 << 7)) |
| 352 | ShuffleMask.push_back(SM_SentinelZero); |
| 353 | else { |
| 354 | // Only the least significant 4 bits of the byte are used. |
| 355 | int Index = Base + (M & 0xf); |
| 356 | ShuffleMask.push_back(Index); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 362 | int ElementBits = VT.getScalarSizeInBits(); |
| 363 | int NumElements = VT.getVectorNumElements(); |
| 364 | for (int i = 0; i < NumElements; ++i) { |
| 365 | // If there are more than 8 elements in the vector, then any immediate blend |
| 366 | // mask applies to each 128-bit lane. There can never be more than |
| 367 | // 8 elements in a 128-bit lane with an immediate blend. |
| 368 | int Bit = NumElements > 8 ? i % (128 / ElementBits) : i; |
| 369 | assert(Bit < 8 && |
| 370 | "Immediate blends only operate over 8 elements at a time!"); |
| 371 | ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i); |
| 372 | } |
| 373 | } |
| 374 | |
Simon Pilgrim | fd4b9b0 | 2016-04-16 17:52:07 +0000 | [diff] [blame] | 375 | void DecodeVPPERMMask(ArrayRef<uint64_t> RawMask, |
| 376 | SmallVectorImpl<int> &ShuffleMask) { |
| 377 | assert(RawMask.size() == 16 && "Illegal VPPERM shuffle mask size"); |
| 378 | |
| 379 | // VPPERM Operation |
| 380 | // Bits[4:0] - Byte Index (0 - 31) |
| 381 | // Bits[7:5] - Permute Operation |
| 382 | // |
| 383 | // Permute Operation: |
| 384 | // 0 - Source byte (no logical operation). |
| 385 | // 1 - Invert source byte. |
| 386 | // 2 - Bit reverse of source byte. |
| 387 | // 3 - Bit reverse of inverted source byte. |
| 388 | // 4 - 00h (zero - fill). |
| 389 | // 5 - FFh (ones - fill). |
| 390 | // 6 - Most significant bit of source byte replicated in all bit positions. |
| 391 | // 7 - Invert most significant bit of source byte and replicate in all bit positions. |
| 392 | for (int i = 0, e = RawMask.size(); i < e; ++i) { |
| 393 | uint64_t M = RawMask[i]; |
| 394 | if (M == (uint64_t)SM_SentinelUndef) { |
| 395 | ShuffleMask.push_back(M); |
| 396 | continue; |
| 397 | } |
| 398 | |
Simon Pilgrim | f379a6c | 2016-04-24 15:05:04 +0000 | [diff] [blame] | 399 | uint64_t PermuteOp = (M >> 5) & 0x7; |
Simon Pilgrim | fd4b9b0 | 2016-04-16 17:52:07 +0000 | [diff] [blame] | 400 | if (PermuteOp == 4) { |
| 401 | ShuffleMask.push_back(SM_SentinelZero); |
| 402 | continue; |
| 403 | } |
| 404 | if (PermuteOp != 0) { |
| 405 | ShuffleMask.clear(); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | uint64_t Index = M & 0x1F; |
| 410 | ShuffleMask.push_back((int)Index); |
| 411 | } |
| 412 | } |
| 413 | |
Simon Pilgrim | a0d7383 | 2016-07-03 18:27:37 +0000 | [diff] [blame] | 414 | /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD. |
| 415 | void DecodeVPERMMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 416 | assert((VT.is256BitVector() || VT.is512BitVector()) && |
| 417 | (VT.getScalarSizeInBits() == 64) && "Unexpected vector value type"); |
| 418 | unsigned NumElts = VT.getVectorNumElements(); |
| 419 | for (unsigned l = 0; l != NumElts; l += 4) |
| 420 | for (unsigned i = 0; i != 4; ++i) |
| 421 | ShuffleMask.push_back(l + ((Imm >> (2 * i)) & 3)); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Simon Pilgrim | e1b6db9 | 2016-02-06 16:33:42 +0000 | [diff] [blame] | 424 | void DecodeZeroExtendMask(MVT SrcScalarVT, MVT DstVT, SmallVectorImpl<int> &Mask) { |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 425 | unsigned NumDstElts = DstVT.getVectorNumElements(); |
Simon Pilgrim | e1b6db9 | 2016-02-06 16:33:42 +0000 | [diff] [blame] | 426 | unsigned SrcScalarBits = SrcScalarVT.getSizeInBits(); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 427 | unsigned DstScalarBits = DstVT.getScalarSizeInBits(); |
| 428 | unsigned Scale = DstScalarBits / SrcScalarBits; |
| 429 | assert(SrcScalarBits < DstScalarBits && |
| 430 | "Expected zero extension mask to increase scalar size"); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 431 | |
| 432 | for (unsigned i = 0; i != NumDstElts; i++) { |
| 433 | Mask.push_back(i); |
| 434 | for (unsigned j = 1; j != Scale; j++) |
| 435 | Mask.push_back(SM_SentinelZero); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void DecodeZeroMoveLowMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
| 440 | unsigned NumElts = VT.getVectorNumElements(); |
| 441 | ShuffleMask.push_back(0); |
| 442 | for (unsigned i = 1; i < NumElts; i++) |
| 443 | ShuffleMask.push_back(SM_SentinelZero); |
| 444 | } |
| 445 | |
| 446 | void DecodeScalarMoveMask(MVT VT, bool IsLoad, SmallVectorImpl<int> &Mask) { |
| 447 | // First element comes from the first element of second source. |
| 448 | // Remaining elements: Load zero extends / Move copies from first source. |
| 449 | unsigned NumElts = VT.getVectorNumElements(); |
| 450 | Mask.push_back(NumElts); |
| 451 | for (unsigned i = 1; i < NumElts; i++) |
| 452 | Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i); |
| 453 | } |
Simon Pilgrim | d85cae3 | 2015-07-06 20:46:41 +0000 | [diff] [blame] | 454 | |
| 455 | void DecodeEXTRQIMask(int Len, int Idx, |
| 456 | SmallVectorImpl<int> &ShuffleMask) { |
| 457 | // Only the bottom 6 bits are valid for each immediate. |
| 458 | Len &= 0x3F; |
| 459 | Idx &= 0x3F; |
| 460 | |
| 461 | // We can only decode this bit extraction instruction as a shuffle if both the |
| 462 | // length and index work with whole bytes. |
| 463 | if (0 != (Len % 8) || 0 != (Idx % 8)) |
| 464 | return; |
| 465 | |
| 466 | // A length of zero is equivalent to a bit length of 64. |
| 467 | if (Len == 0) |
| 468 | Len = 64; |
| 469 | |
| 470 | // If the length + index exceeds the bottom 64 bits the result is undefined. |
| 471 | if ((Len + Idx) > 64) { |
| 472 | ShuffleMask.append(16, SM_SentinelUndef); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | // Convert index and index to work with bytes. |
| 477 | Len /= 8; |
| 478 | Idx /= 8; |
| 479 | |
| 480 | // EXTRQ: Extract Len bytes starting from Idx. Zero pad the remaining bytes |
| 481 | // of the lower 64-bits. The upper 64-bits are undefined. |
| 482 | for (int i = 0; i != Len; ++i) |
| 483 | ShuffleMask.push_back(i + Idx); |
| 484 | for (int i = Len; i != 8; ++i) |
| 485 | ShuffleMask.push_back(SM_SentinelZero); |
| 486 | for (int i = 8; i != 16; ++i) |
| 487 | ShuffleMask.push_back(SM_SentinelUndef); |
| 488 | } |
| 489 | |
| 490 | void DecodeINSERTQIMask(int Len, int Idx, |
| 491 | SmallVectorImpl<int> &ShuffleMask) { |
| 492 | // Only the bottom 6 bits are valid for each immediate. |
| 493 | Len &= 0x3F; |
| 494 | Idx &= 0x3F; |
| 495 | |
| 496 | // We can only decode this bit insertion instruction as a shuffle if both the |
| 497 | // length and index work with whole bytes. |
| 498 | if (0 != (Len % 8) || 0 != (Idx % 8)) |
| 499 | return; |
| 500 | |
| 501 | // A length of zero is equivalent to a bit length of 64. |
| 502 | if (Len == 0) |
| 503 | Len = 64; |
| 504 | |
| 505 | // If the length + index exceeds the bottom 64 bits the result is undefined. |
| 506 | if ((Len + Idx) > 64) { |
| 507 | ShuffleMask.append(16, SM_SentinelUndef); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Convert index and index to work with bytes. |
| 512 | Len /= 8; |
| 513 | Idx /= 8; |
| 514 | |
| 515 | // INSERTQ: Extract lowest Len bytes from lower half of second source and |
| 516 | // insert over first source starting at Idx byte. The upper 64-bits are |
| 517 | // undefined. |
| 518 | for (int i = 0; i != Idx; ++i) |
| 519 | ShuffleMask.push_back(i); |
| 520 | for (int i = 0; i != Len; ++i) |
| 521 | ShuffleMask.push_back(i + 16); |
| 522 | for (int i = Idx + Len; i != 8; ++i) |
| 523 | ShuffleMask.push_back(i); |
| 524 | for (int i = 8; i != 16; ++i) |
| 525 | ShuffleMask.push_back(SM_SentinelUndef); |
| 526 | } |
| 527 | |
Simon Pilgrim | 40e1a71 | 2016-03-05 22:53:31 +0000 | [diff] [blame] | 528 | void DecodeVPERMILPMask(MVT VT, ArrayRef<uint64_t> RawMask, |
| 529 | SmallVectorImpl<int> &ShuffleMask) { |
| 530 | unsigned VecSize = VT.getSizeInBits(); |
| 531 | unsigned EltSize = VT.getScalarSizeInBits(); |
| 532 | unsigned NumLanes = VecSize / 128; |
| 533 | unsigned NumEltsPerLane = VT.getVectorNumElements() / NumLanes; |
| 534 | assert((VecSize == 128 || VecSize == 256 || VecSize == 512) && |
| 535 | "Unexpected vector size"); |
| 536 | assert((EltSize == 32 || EltSize == 64) && "Unexpected element size"); |
| 537 | |
| 538 | for (unsigned i = 0, e = RawMask.size(); i < e; ++i) { |
| 539 | uint64_t M = RawMask[i]; |
| 540 | M = (EltSize == 64 ? ((M >> 1) & 0x1) : (M & 0x3)); |
| 541 | unsigned LaneOffset = i & ~(NumEltsPerLane - 1); |
| 542 | ShuffleMask.push_back((int)(LaneOffset + M)); |
| 543 | } |
| 544 | } |
| 545 | |
Simon Pilgrim | 64c6de4 | 2016-06-05 15:21:30 +0000 | [diff] [blame] | 546 | void DecodeVPERMIL2PMask(MVT VT, unsigned M2Z, ArrayRef<uint64_t> RawMask, |
| 547 | SmallVectorImpl<int> &ShuffleMask) { |
| 548 | unsigned VecSize = VT.getSizeInBits(); |
| 549 | unsigned EltSize = VT.getScalarSizeInBits(); |
| 550 | unsigned NumLanes = VecSize / 128; |
Simon Pilgrim | d5bc5c1 | 2016-12-07 11:19:00 +0000 | [diff] [blame^] | 551 | unsigned NumElts = VT.getVectorNumElements(); |
| 552 | unsigned NumEltsPerLane = NumElts / NumLanes; |
| 553 | assert((VecSize == 128 || VecSize == 256) && "Unexpected vector size"); |
Simon Pilgrim | 64c6de4 | 2016-06-05 15:21:30 +0000 | [diff] [blame] | 554 | assert((EltSize == 32 || EltSize == 64) && "Unexpected element size"); |
Simon Pilgrim | d5bc5c1 | 2016-12-07 11:19:00 +0000 | [diff] [blame^] | 555 | assert((NumElts == RawMask.size()) && "Unexpected mask size"); |
Simon Pilgrim | 64c6de4 | 2016-06-05 15:21:30 +0000 | [diff] [blame] | 556 | |
| 557 | for (unsigned i = 0, e = RawMask.size(); i < e; ++i) { |
| 558 | // VPERMIL2 Operation. |
| 559 | // Bits[3] - Match Bit. |
| 560 | // Bits[2:1] - (Per Lane) PD Shuffle Mask. |
| 561 | // Bits[2:0] - (Per Lane) PS Shuffle Mask. |
| 562 | uint64_t Selector = RawMask[i]; |
Filipe Cabecinhas | 6e7d546 | 2016-06-06 10:49:56 +0000 | [diff] [blame] | 563 | unsigned MatchBit = (Selector >> 3) & 0x1; |
Simon Pilgrim | 64c6de4 | 2016-06-05 15:21:30 +0000 | [diff] [blame] | 564 | |
| 565 | // M2Z[0:1] MatchBit |
| 566 | // 0Xb X Source selected by Selector index. |
| 567 | // 10b 0 Source selected by Selector index. |
| 568 | // 10b 1 Zero. |
| 569 | // 11b 0 Zero. |
| 570 | // 11b 1 Source selected by Selector index. |
| 571 | if ((M2Z & 0x2) != 0 && MatchBit != (M2Z & 0x1)) { |
| 572 | ShuffleMask.push_back(SM_SentinelZero); |
| 573 | continue; |
| 574 | } |
| 575 | |
Simon Pilgrim | d5bc5c1 | 2016-12-07 11:19:00 +0000 | [diff] [blame^] | 576 | int Index = i & ~(NumEltsPerLane - 1); |
Simon Pilgrim | 64c6de4 | 2016-06-05 15:21:30 +0000 | [diff] [blame] | 577 | if (EltSize == 64) |
| 578 | Index += (Selector >> 1) & 0x1; |
| 579 | else |
| 580 | Index += Selector & 0x3; |
| 581 | |
Simon Pilgrim | d5bc5c1 | 2016-12-07 11:19:00 +0000 | [diff] [blame^] | 582 | int Src = (Selector >> 2) & 0x1; |
| 583 | Index += Src * NumElts; |
| 584 | ShuffleMask.push_back(Index); |
Simon Pilgrim | 64c6de4 | 2016-06-05 15:21:30 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Elena Demikhovsky | e88038f | 2015-09-08 06:38:21 +0000 | [diff] [blame] | 588 | void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask, |
| 589 | SmallVectorImpl<int> &ShuffleMask) { |
Simon Pilgrim | 48adedf | 2016-07-05 18:31:17 +0000 | [diff] [blame] | 590 | uint64_t EltMaskSize = RawMask.size() - 1; |
| 591 | for (auto M : RawMask) { |
| 592 | M &= EltMaskSize; |
Elena Demikhovsky | e88038f | 2015-09-08 06:38:21 +0000 | [diff] [blame] | 593 | ShuffleMask.push_back((int)M); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask, |
| 598 | SmallVectorImpl<int> &ShuffleMask) { |
Simon Pilgrim | 253ca34 | 2016-03-06 21:54:52 +0000 | [diff] [blame] | 599 | uint64_t EltMaskSize = (RawMask.size() * 2) - 1; |
| 600 | for (auto M : RawMask) { |
| 601 | M &= EltMaskSize; |
Elena Demikhovsky | e88038f | 2015-09-08 06:38:21 +0000 | [diff] [blame] | 602 | ShuffleMask.push_back((int)M); |
| 603 | } |
| 604 | } |
| 605 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 606 | } // llvm namespace |