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