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" 16 #include "llvm/CodeGen/MachineValueType.h" 17 18 //===----------------------------------------------------------------------===// 19 // Vector Mask Decoding 20 //===----------------------------------------------------------------------===// 21 22 namespace llvm { 23 24 void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 25 // Defaults the copying the dest value. 26 ShuffleMask.push_back(0); 27 ShuffleMask.push_back(1); 28 ShuffleMask.push_back(2); 29 ShuffleMask.push_back(3); 30 31 // Decode the immediate. 32 unsigned ZMask = Imm & 15; 33 unsigned CountD = (Imm >> 4) & 3; 34 unsigned CountS = (Imm >> 6) & 3; 35 36 // CountS selects which input element to use. 37 unsigned InVal = 4 + CountS; 38 // CountD specifies which element of destination to update. 39 ShuffleMask[CountD] = InVal; 40 // ZMask zaps values, potentially overriding the CountD elt. 41 if (ZMask & 1) ShuffleMask[0] = SM_SentinelZero; 42 if (ZMask & 2) ShuffleMask[1] = SM_SentinelZero; 43 if (ZMask & 4) ShuffleMask[2] = SM_SentinelZero; 44 if (ZMask & 8) ShuffleMask[3] = SM_SentinelZero; 45 } 46 47 // <3,1> or <6,7,2,3> 48 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { 49 for (unsigned i = NElts / 2; i != NElts; ++i) 50 ShuffleMask.push_back(NElts + i); 51 52 for (unsigned i = NElts / 2; i != NElts; ++i) 53 ShuffleMask.push_back(i); 54 } 55 56 // <0,2> or <0,1,4,5> 57 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { 58 for (unsigned i = 0; i != NElts / 2; ++i) 59 ShuffleMask.push_back(i); 60 61 for (unsigned i = 0; i != NElts / 2; ++i) 62 ShuffleMask.push_back(NElts + i); 63 } 64 65 void DecodeMOVSLDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 66 unsigned NumElts = VT.getVectorNumElements(); 67 for (int i = 0, e = NumElts / 2; i < e; ++i) { 68 ShuffleMask.push_back(2 * i); 69 ShuffleMask.push_back(2 * i); 70 } 71 } 72 73 void DecodeMOVSHDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 74 unsigned NumElts = VT.getVectorNumElements(); 75 for (int i = 0, e = NumElts / 2; i < e; ++i) { 76 ShuffleMask.push_back(2 * i + 1); 77 ShuffleMask.push_back(2 * i + 1); 78 } 79 } 80 81 void DecodeMOVDDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 82 unsigned VectorSizeInBits = VT.getSizeInBits(); 83 unsigned ScalarSizeInBits = VT.getScalarSizeInBits(); 84 unsigned NumElts = VT.getVectorNumElements(); 85 unsigned NumLanes = VectorSizeInBits / 128; 86 unsigned NumLaneElts = NumElts / NumLanes; 87 unsigned NumLaneSubElts = 64 / ScalarSizeInBits; 88 89 for (unsigned l = 0; l < NumElts; l += NumLaneElts) 90 for (unsigned i = 0; i < NumLaneElts; i += NumLaneSubElts) 91 for (unsigned s = 0; s != NumLaneSubElts; s++) 92 ShuffleMask.push_back(l + s); 93 } 94 95 void DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 96 unsigned VectorSizeInBits = VT.getSizeInBits(); 97 unsigned NumElts = VectorSizeInBits / 8; 98 unsigned NumLanes = VectorSizeInBits / 128; 99 unsigned NumLaneElts = NumElts / NumLanes; 100 101 for (unsigned l = 0; l < NumElts; l += NumLaneElts) 102 for (unsigned i = 0; i < NumLaneElts; ++i) { 103 int M = SM_SentinelZero; 104 if (i >= Imm) M = i - Imm + l; 105 ShuffleMask.push_back(M); 106 } 107 } 108 109 void DecodePSRLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 110 unsigned VectorSizeInBits = VT.getSizeInBits(); 111 unsigned NumElts = VectorSizeInBits / 8; 112 unsigned NumLanes = VectorSizeInBits / 128; 113 unsigned NumLaneElts = NumElts / NumLanes; 114 115 for (unsigned l = 0; l < NumElts; l += NumLaneElts) 116 for (unsigned i = 0; i < NumLaneElts; ++i) { 117 unsigned Base = i + Imm; 118 int M = Base + l; 119 if (Base >= NumLaneElts) M = SM_SentinelZero; 120 ShuffleMask.push_back(M); 121 } 122 } 123 124 void DecodePALIGNRMask(MVT VT, unsigned Imm, 125 SmallVectorImpl<int> &ShuffleMask) { 126 unsigned NumElts = VT.getVectorNumElements(); 127 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8); 128 129 unsigned NumLanes = VT.getSizeInBits() / 128; 130 unsigned NumLaneElts = NumElts / NumLanes; 131 132 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 133 for (unsigned i = 0; i != NumLaneElts; ++i) { 134 unsigned Base = i + Offset; 135 // if i+offset is out of this lane then we actually need the other source 136 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts; 137 ShuffleMask.push_back(Base + l); 138 } 139 } 140 } 141 142 /// DecodePSHUFMask - This decodes the shuffle masks for pshufw, pshufd, and vpermilp*. 143 /// VT indicates the type of the vector allowing it to handle different 144 /// datatypes and vector widths. 145 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 146 unsigned NumElts = VT.getVectorNumElements(); 147 148 unsigned NumLanes = VT.getSizeInBits() / 128; 149 if (NumLanes == 0) NumLanes = 1; // Handle MMX 150 unsigned NumLaneElts = NumElts / NumLanes; 151 152 unsigned NewImm = Imm; 153 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 154 for (unsigned i = 0; i != NumLaneElts; ++i) { 155 ShuffleMask.push_back(NewImm % NumLaneElts + l); 156 NewImm /= NumLaneElts; 157 } 158 if (NumLaneElts == 4) NewImm = Imm; // reload imm 159 } 160 } 161 162 void DecodePSHUFHWMask(MVT VT, unsigned Imm, 163 SmallVectorImpl<int> &ShuffleMask) { 164 unsigned NumElts = VT.getVectorNumElements(); 165 166 for (unsigned l = 0; l != NumElts; l += 8) { 167 unsigned NewImm = Imm; 168 for (unsigned i = 0, e = 4; i != e; ++i) { 169 ShuffleMask.push_back(l + i); 170 } 171 for (unsigned i = 4, e = 8; i != e; ++i) { 172 ShuffleMask.push_back(l + 4 + (NewImm & 3)); 173 NewImm >>= 2; 174 } 175 } 176 } 177 178 void DecodePSHUFLWMask(MVT VT, unsigned Imm, 179 SmallVectorImpl<int> &ShuffleMask) { 180 unsigned NumElts = VT.getVectorNumElements(); 181 182 for (unsigned l = 0; l != NumElts; l += 8) { 183 unsigned NewImm = Imm; 184 for (unsigned i = 0, e = 4; i != e; ++i) { 185 ShuffleMask.push_back(l + (NewImm & 3)); 186 NewImm >>= 2; 187 } 188 for (unsigned i = 4, e = 8; i != e; ++i) { 189 ShuffleMask.push_back(l + i); 190 } 191 } 192 } 193 194 void DecodePSWAPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 195 unsigned NumElts = VT.getVectorNumElements(); 196 unsigned NumHalfElts = NumElts / 2; 197 198 for (unsigned l = 0; l != NumHalfElts; ++l) 199 ShuffleMask.push_back(l + NumHalfElts); 200 for (unsigned h = 0; h != NumHalfElts; ++h) 201 ShuffleMask.push_back(h); 202 } 203 204 /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates 205 /// the type of the vector allowing it to handle different datatypes and vector 206 /// widths. 207 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 208 unsigned NumElts = VT.getVectorNumElements(); 209 210 unsigned NumLanes = VT.getSizeInBits() / 128; 211 unsigned NumLaneElts = NumElts / NumLanes; 212 213 unsigned NewImm = Imm; 214 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 215 // each half of a lane comes from different source 216 for (unsigned s = 0; s != NumElts * 2; s += NumElts) { 217 for (unsigned i = 0; i != NumLaneElts / 2; ++i) { 218 ShuffleMask.push_back(NewImm % NumLaneElts + s + l); 219 NewImm /= NumLaneElts; 220 } 221 } 222 if (NumLaneElts == 4) NewImm = Imm; // reload imm 223 } 224 } 225 226 /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd 227 /// and punpckh*. VT indicates the type of the vector allowing it to handle 228 /// different datatypes and vector widths. 229 void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 230 unsigned NumElts = VT.getVectorNumElements(); 231 232 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate 233 // independently on 128-bit lanes. 234 unsigned NumLanes = VT.getSizeInBits() / 128; 235 if (NumLanes == 0) NumLanes = 1; // Handle MMX 236 unsigned NumLaneElts = NumElts / NumLanes; 237 238 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 239 for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) { 240 ShuffleMask.push_back(i); // Reads from dest/src1 241 ShuffleMask.push_back(i + NumElts); // Reads from src/src2 242 } 243 } 244 } 245 246 /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd 247 /// and punpckl*. VT indicates the type of the vector allowing it to handle 248 /// different datatypes and vector widths. 249 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 250 unsigned NumElts = VT.getVectorNumElements(); 251 252 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate 253 // independently on 128-bit lanes. 254 unsigned NumLanes = VT.getSizeInBits() / 128; 255 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX 256 unsigned NumLaneElts = NumElts / NumLanes; 257 258 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 259 for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) { 260 ShuffleMask.push_back(i); // Reads from dest/src1 261 ShuffleMask.push_back(i + NumElts); // Reads from src/src2 262 } 263 } 264 } 265 266 /// \brief Decode a shuffle packed values at 128-bit granularity 267 /// (SHUFF32x4/SHUFF64x2/SHUFI32x4/SHUFI64x2) 268 /// immediate mask into a shuffle mask. 269 void decodeVSHUF64x2FamilyMask(MVT VT, unsigned Imm, 270 SmallVectorImpl<int> &ShuffleMask) { 271 unsigned NumLanes = VT.getSizeInBits() / 128; 272 unsigned NumElementsInLane = 128 / VT.getScalarSizeInBits(); 273 unsigned ControlBitsMask = NumLanes - 1; 274 unsigned NumControlBits = NumLanes / 2; 275 276 for (unsigned l = 0; l != NumLanes; ++l) { 277 unsigned LaneMask = (Imm >> (l * NumControlBits)) & ControlBitsMask; 278 // We actually need the other source. 279 if (l >= NumLanes / 2) 280 LaneMask += NumLanes; 281 for (unsigned i = 0; i != NumElementsInLane; ++i) 282 ShuffleMask.push_back(LaneMask * NumElementsInLane + i); 283 } 284 } 285 286 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm, 287 SmallVectorImpl<int> &ShuffleMask) { 288 unsigned HalfSize = VT.getVectorNumElements() / 2; 289 290 for (unsigned l = 0; l != 2; ++l) { 291 unsigned HalfMask = Imm >> (l * 4); 292 unsigned HalfBegin = (HalfMask & 0x3) * HalfSize; 293 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i) 294 ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i); 295 } 296 } 297 298 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask, 299 SmallVectorImpl<int> &ShuffleMask) { 300 for (int i = 0, e = RawMask.size(); i < e; ++i) { 301 uint64_t M = RawMask[i]; 302 if (M == (uint64_t)SM_SentinelUndef) { 303 ShuffleMask.push_back(M); 304 continue; 305 } 306 // For AVX vectors with 32 bytes the base of the shuffle is the half of 307 // the vector we're inside. 308 int Base = i < 16 ? 0 : 16; 309 // If the high bit (7) of the byte is set, the element is zeroed. 310 if (M & (1 << 7)) 311 ShuffleMask.push_back(SM_SentinelZero); 312 else { 313 // Only the least significant 4 bits of the byte are used. 314 int Index = Base + (M & 0xf); 315 ShuffleMask.push_back(Index); 316 } 317 } 318 } 319 320 void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 321 int ElementBits = VT.getScalarSizeInBits(); 322 int NumElements = VT.getVectorNumElements(); 323 for (int i = 0; i < NumElements; ++i) { 324 // If there are more than 8 elements in the vector, then any immediate blend 325 // mask applies to each 128-bit lane. There can never be more than 326 // 8 elements in a 128-bit lane with an immediate blend. 327 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i; 328 assert(Bit < 8 && 329 "Immediate blends only operate over 8 elements at a time!"); 330 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i); 331 } 332 } 333 334 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD. 335 /// No VT provided since it only works on 256-bit, 4 element vectors. 336 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 337 for (unsigned i = 0; i != 4; ++i) { 338 ShuffleMask.push_back((Imm >> (2 * i)) & 3); 339 } 340 } 341 342 void DecodeZeroExtendMask(MVT SrcVT, MVT DstVT, SmallVectorImpl<int> &Mask) { 343 unsigned NumDstElts = DstVT.getVectorNumElements(); 344 unsigned SrcScalarBits = SrcVT.getScalarSizeInBits(); 345 unsigned DstScalarBits = DstVT.getScalarSizeInBits(); 346 unsigned Scale = DstScalarBits / SrcScalarBits; 347 assert(SrcScalarBits < DstScalarBits && 348 "Expected zero extension mask to increase scalar size"); 349 assert(SrcVT.getVectorNumElements() >= NumDstElts && 350 "Too many zero extension lanes"); 351 352 for (unsigned i = 0; i != NumDstElts; i++) { 353 Mask.push_back(i); 354 for (unsigned j = 1; j != Scale; j++) 355 Mask.push_back(SM_SentinelZero); 356 } 357 } 358 359 void DecodeZeroMoveLowMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 360 unsigned NumElts = VT.getVectorNumElements(); 361 ShuffleMask.push_back(0); 362 for (unsigned i = 1; i < NumElts; i++) 363 ShuffleMask.push_back(SM_SentinelZero); 364 } 365 366 void DecodeScalarMoveMask(MVT VT, bool IsLoad, SmallVectorImpl<int> &Mask) { 367 // First element comes from the first element of second source. 368 // Remaining elements: Load zero extends / Move copies from first source. 369 unsigned NumElts = VT.getVectorNumElements(); 370 Mask.push_back(NumElts); 371 for (unsigned i = 1; i < NumElts; i++) 372 Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i); 373 } 374 375 void DecodeEXTRQIMask(int Len, int Idx, 376 SmallVectorImpl<int> &ShuffleMask) { 377 // Only the bottom 6 bits are valid for each immediate. 378 Len &= 0x3F; 379 Idx &= 0x3F; 380 381 // We can only decode this bit extraction instruction as a shuffle if both the 382 // length and index work with whole bytes. 383 if (0 != (Len % 8) || 0 != (Idx % 8)) 384 return; 385 386 // A length of zero is equivalent to a bit length of 64. 387 if (Len == 0) 388 Len = 64; 389 390 // If the length + index exceeds the bottom 64 bits the result is undefined. 391 if ((Len + Idx) > 64) { 392 ShuffleMask.append(16, SM_SentinelUndef); 393 return; 394 } 395 396 // Convert index and index to work with bytes. 397 Len /= 8; 398 Idx /= 8; 399 400 // EXTRQ: Extract Len bytes starting from Idx. Zero pad the remaining bytes 401 // of the lower 64-bits. The upper 64-bits are undefined. 402 for (int i = 0; i != Len; ++i) 403 ShuffleMask.push_back(i + Idx); 404 for (int i = Len; i != 8; ++i) 405 ShuffleMask.push_back(SM_SentinelZero); 406 for (int i = 8; i != 16; ++i) 407 ShuffleMask.push_back(SM_SentinelUndef); 408 } 409 410 void DecodeINSERTQIMask(int Len, int Idx, 411 SmallVectorImpl<int> &ShuffleMask) { 412 // Only the bottom 6 bits are valid for each immediate. 413 Len &= 0x3F; 414 Idx &= 0x3F; 415 416 // We can only decode this bit insertion instruction as a shuffle if both the 417 // length and index work with whole bytes. 418 if (0 != (Len % 8) || 0 != (Idx % 8)) 419 return; 420 421 // A length of zero is equivalent to a bit length of 64. 422 if (Len == 0) 423 Len = 64; 424 425 // If the length + index exceeds the bottom 64 bits the result is undefined. 426 if ((Len + Idx) > 64) { 427 ShuffleMask.append(16, SM_SentinelUndef); 428 return; 429 } 430 431 // Convert index and index to work with bytes. 432 Len /= 8; 433 Idx /= 8; 434 435 // INSERTQ: Extract lowest Len bytes from lower half of second source and 436 // insert over first source starting at Idx byte. The upper 64-bits are 437 // undefined. 438 for (int i = 0; i != Idx; ++i) 439 ShuffleMask.push_back(i); 440 for (int i = 0; i != Len; ++i) 441 ShuffleMask.push_back(i + 16); 442 for (int i = Idx + Len; i != 8; ++i) 443 ShuffleMask.push_back(i); 444 for (int i = 8; i != 16; ++i) 445 ShuffleMask.push_back(SM_SentinelUndef); 446 } 447 448 void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask, 449 SmallVectorImpl<int> &ShuffleMask) { 450 for (int i = 0, e = RawMask.size(); i < e; ++i) { 451 uint64_t M = RawMask[i]; 452 ShuffleMask.push_back((int)M); 453 } 454 } 455 456 void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask, 457 SmallVectorImpl<int> &ShuffleMask) { 458 for (int i = 0, e = RawMask.size(); i < e; ++i) { 459 uint64_t M = RawMask[i]; 460 ShuffleMask.push_back((int)M); 461 } 462 } 463 464 } // llvm namespace 465