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/IR/Constants.h" 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. 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 48 // <3,1> or <6,7,2,3> 49 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { 50 for (unsigned i = NElts / 2; i != NElts; ++i) 51 ShuffleMask.push_back(NElts + i); 52 53 for (unsigned i = NElts / 2; i != NElts; ++i) 54 ShuffleMask.push_back(i); 55 } 56 57 // <0,2> or <0,1,4,5> 58 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { 59 for (unsigned i = 0; i != NElts / 2; ++i) 60 ShuffleMask.push_back(i); 61 62 for (unsigned i = 0; i != NElts / 2; ++i) 63 ShuffleMask.push_back(NElts + i); 64 } 65 66 void DecodeMOVSLDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 67 unsigned NumElts = VT.getVectorNumElements(); 68 for (int i = 0, e = NumElts / 2; i < e; ++i) { 69 ShuffleMask.push_back(2 * i); 70 ShuffleMask.push_back(2 * i); 71 } 72 } 73 74 void DecodeMOVSHDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 75 unsigned NumElts = VT.getVectorNumElements(); 76 for (int i = 0, e = NumElts / 2; i < e; ++i) { 77 ShuffleMask.push_back(2 * i + 1); 78 ShuffleMask.push_back(2 * i + 1); 79 } 80 } 81 82 void DecodeMOVDDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 83 unsigned VectorSizeInBits = VT.getSizeInBits(); 84 unsigned ScalarSizeInBits = VT.getScalarSizeInBits(); 85 unsigned NumElts = VT.getVectorNumElements(); 86 unsigned NumLanes = VectorSizeInBits / 128; 87 unsigned NumLaneElts = NumElts / NumLanes; 88 unsigned NumLaneSubElts = 64 / ScalarSizeInBits; 89 90 for (unsigned l = 0; l < NumElts; l += NumLaneElts) 91 for (unsigned i = 0; i < NumLaneElts; i += NumLaneSubElts) 92 for (unsigned s = 0; s != NumLaneSubElts; s++) 93 ShuffleMask.push_back(l + s); 94 } 95 96 void DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 97 unsigned VectorSizeInBits = VT.getSizeInBits(); 98 unsigned NumElts = VectorSizeInBits / 8; 99 unsigned NumLanes = VectorSizeInBits / 128; 100 unsigned NumLaneElts = NumElts / NumLanes; 101 102 for (unsigned l = 0; l < NumElts; l += NumLaneElts) 103 for (unsigned i = 0; i < NumLaneElts; ++i) { 104 int M = SM_SentinelZero; 105 if (i >= Imm) M = i - Imm + l; 106 ShuffleMask.push_back(M); 107 } 108 } 109 110 void DecodePSRLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 111 unsigned VectorSizeInBits = VT.getSizeInBits(); 112 unsigned NumElts = VectorSizeInBits / 8; 113 unsigned NumLanes = VectorSizeInBits / 128; 114 unsigned NumLaneElts = NumElts / NumLanes; 115 116 for (unsigned l = 0; l < NumElts; l += NumLaneElts) 117 for (unsigned i = 0; i < NumLaneElts; ++i) { 118 unsigned Base = i + Imm; 119 int M = Base + l; 120 if (Base >= NumLaneElts) M = SM_SentinelZero; 121 ShuffleMask.push_back(M); 122 } 123 } 124 125 void DecodePALIGNRMask(MVT VT, unsigned Imm, 126 SmallVectorImpl<int> &ShuffleMask) { 127 unsigned NumElts = VT.getVectorNumElements(); 128 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8); 129 130 unsigned NumLanes = VT.getSizeInBits() / 128; 131 unsigned NumLaneElts = NumElts / NumLanes; 132 133 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 134 for (unsigned i = 0; i != NumLaneElts; ++i) { 135 unsigned Base = i + Offset; 136 // if i+offset is out of this lane then we actually need the other source 137 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts; 138 ShuffleMask.push_back(Base + l); 139 } 140 } 141 } 142 143 /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*. 144 /// VT indicates the type of the vector allowing it to handle different 145 /// datatypes and vector widths. 146 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 147 unsigned NumElts = VT.getVectorNumElements(); 148 149 unsigned NumLanes = VT.getSizeInBits() / 128; 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 /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates 195 /// the type of the vector allowing it to handle different datatypes and vector 196 /// widths. 197 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 198 unsigned NumElts = VT.getVectorNumElements(); 199 200 unsigned NumLanes = VT.getSizeInBits() / 128; 201 unsigned NumLaneElts = NumElts / NumLanes; 202 203 unsigned NewImm = Imm; 204 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 205 // each half of a lane comes from different source 206 for (unsigned s = 0; s != NumElts * 2; s += NumElts) { 207 for (unsigned i = 0; i != NumLaneElts / 2; ++i) { 208 ShuffleMask.push_back(NewImm % NumLaneElts + s + l); 209 NewImm /= NumLaneElts; 210 } 211 } 212 if (NumLaneElts == 4) NewImm = Imm; // reload imm 213 } 214 } 215 216 /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd 217 /// and punpckh*. VT indicates the type of the vector allowing it to handle 218 /// different datatypes and vector widths. 219 void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 220 unsigned NumElts = VT.getVectorNumElements(); 221 222 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate 223 // independently on 128-bit lanes. 224 unsigned NumLanes = VT.getSizeInBits() / 128; 225 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX 226 unsigned NumLaneElts = NumElts / NumLanes; 227 228 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 229 for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) { 230 ShuffleMask.push_back(i); // Reads from dest/src1 231 ShuffleMask.push_back(i + NumElts); // Reads from src/src2 232 } 233 } 234 } 235 236 /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd 237 /// and punpckl*. VT indicates the type of the vector allowing it to handle 238 /// different datatypes and vector widths. 239 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 240 unsigned NumElts = VT.getVectorNumElements(); 241 242 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate 243 // independently on 128-bit lanes. 244 unsigned NumLanes = VT.getSizeInBits() / 128; 245 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX 246 unsigned NumLaneElts = NumElts / NumLanes; 247 248 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 249 for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) { 250 ShuffleMask.push_back(i); // Reads from dest/src1 251 ShuffleMask.push_back(i + NumElts); // Reads from src/src2 252 } 253 } 254 } 255 256 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm, 257 SmallVectorImpl<int> &ShuffleMask) { 258 unsigned HalfSize = VT.getVectorNumElements() / 2; 259 260 for (unsigned l = 0; l != 2; ++l) { 261 unsigned HalfMask = Imm >> (l * 4); 262 unsigned HalfBegin = (HalfMask & 0x3) * HalfSize; 263 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i) 264 ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i); 265 } 266 } 267 268 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) { 269 Type *MaskTy = C->getType(); 270 // It is not an error for the PSHUFB mask to not be a vector of i8 because the 271 // constant pool uniques constants by their bit representation. 272 // e.g. the following take up the same space in the constant pool: 273 // i128 -170141183420855150465331762880109871104 274 // 275 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160> 276 // 277 // <4 x i32> <i32 -2147483648, i32 -2147483648, 278 // i32 -2147483648, i32 -2147483648> 279 280 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits(); 281 282 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512. 283 return; 284 285 // This is a straightforward byte vector. 286 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) { 287 int NumElements = MaskTy->getVectorNumElements(); 288 ShuffleMask.reserve(NumElements); 289 290 for (int i = 0; i < NumElements; ++i) { 291 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte 292 // lane of the vector we're inside. 293 int Base = i < 16 ? 0 : 16; 294 Constant *COp = C->getAggregateElement(i); 295 if (!COp) { 296 ShuffleMask.clear(); 297 return; 298 } else if (isa<UndefValue>(COp)) { 299 ShuffleMask.push_back(SM_SentinelUndef); 300 continue; 301 } 302 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 303 // If the high bit (7) of the byte is set, the element is zeroed. 304 if (Element & (1 << 7)) 305 ShuffleMask.push_back(SM_SentinelZero); 306 else { 307 // Only the least significant 4 bits of the byte are used. 308 int Index = Base + (Element & 0xf); 309 ShuffleMask.push_back(Index); 310 } 311 } 312 } 313 // TODO: Handle funny-looking vectors too. 314 } 315 316 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask, 317 SmallVectorImpl<int> &ShuffleMask) { 318 for (int i = 0, e = RawMask.size(); i < e; ++i) { 319 uint64_t M = RawMask[i]; 320 if (M == (uint64_t)SM_SentinelUndef) { 321 ShuffleMask.push_back(M); 322 continue; 323 } 324 // For AVX vectors with 32 bytes the base of the shuffle is the half of 325 // the vector we're inside. 326 int Base = i < 16 ? 0 : 16; 327 // If the high bit (7) of the byte is set, the element is zeroed. 328 if (M & (1 << 7)) 329 ShuffleMask.push_back(SM_SentinelZero); 330 else { 331 // Only the least significant 4 bits of the byte are used. 332 int Index = Base + (M & 0xf); 333 ShuffleMask.push_back(Index); 334 } 335 } 336 } 337 338 void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 339 int ElementBits = VT.getScalarSizeInBits(); 340 int NumElements = VT.getVectorNumElements(); 341 for (int i = 0; i < NumElements; ++i) { 342 // If there are more than 8 elements in the vector, then any immediate blend 343 // mask applies to each 128-bit lane. There can never be more than 344 // 8 elements in a 128-bit lane with an immediate blend. 345 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i; 346 assert(Bit < 8 && 347 "Immediate blends only operate over 8 elements at a time!"); 348 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i); 349 } 350 } 351 352 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD. 353 /// No VT provided since it only works on 256-bit, 4 element vectors. 354 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 355 for (unsigned i = 0; i != 4; ++i) { 356 ShuffleMask.push_back((Imm >> (2 * i)) & 3); 357 } 358 } 359 360 void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) { 361 Type *MaskTy = C->getType(); 362 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!"); 363 assert(MaskTy->getVectorElementType()->isIntegerTy() && 364 "Expected integer constant mask elements!"); 365 int ElementBits = MaskTy->getScalarSizeInBits(); 366 int NumElements = MaskTy->getVectorNumElements(); 367 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) && 368 "Unexpected number of vector elements."); 369 ShuffleMask.reserve(NumElements); 370 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) { 371 assert((unsigned)NumElements == CDS->getNumElements() && 372 "Constant mask has a different number of elements!"); 373 374 for (int i = 0; i < NumElements; ++i) { 375 int Base = (i * ElementBits / 128) * (128 / ElementBits); 376 uint64_t Element = CDS->getElementAsInteger(i); 377 // Only the least significant 2 bits of the integer are used. 378 int Index = Base + (Element & 0x3); 379 ShuffleMask.push_back(Index); 380 } 381 } else if (auto *CV = dyn_cast<ConstantVector>(C)) { 382 assert((unsigned)NumElements == C->getNumOperands() && 383 "Constant mask has a different number of elements!"); 384 385 for (int i = 0; i < NumElements; ++i) { 386 int Base = (i * ElementBits / 128) * (128 / ElementBits); 387 Constant *COp = CV->getOperand(i); 388 if (isa<UndefValue>(COp)) { 389 ShuffleMask.push_back(SM_SentinelUndef); 390 continue; 391 } 392 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 393 // Only the least significant 2 bits of the integer are used. 394 int Index = Base + (Element & 0x3); 395 ShuffleMask.push_back(Index); 396 } 397 } 398 } 399 400 void DecodeZeroExtendMask(MVT SrcVT, MVT DstVT, SmallVectorImpl<int> &Mask) { 401 unsigned NumDstElts = DstVT.getVectorNumElements(); 402 unsigned SrcScalarBits = SrcVT.getScalarSizeInBits(); 403 unsigned DstScalarBits = DstVT.getScalarSizeInBits(); 404 unsigned Scale = DstScalarBits / SrcScalarBits; 405 assert(SrcScalarBits < DstScalarBits && 406 "Expected zero extension mask to increase scalar size"); 407 assert(SrcVT.getVectorNumElements() >= NumDstElts && 408 "Too many zero extension lanes"); 409 410 for (unsigned i = 0; i != NumDstElts; i++) { 411 Mask.push_back(i); 412 for (unsigned j = 1; j != Scale; j++) 413 Mask.push_back(SM_SentinelZero); 414 } 415 } 416 417 void DecodeZeroMoveLowMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 418 unsigned NumElts = VT.getVectorNumElements(); 419 ShuffleMask.push_back(0); 420 for (unsigned i = 1; i < NumElts; i++) 421 ShuffleMask.push_back(SM_SentinelZero); 422 } 423 424 void DecodeScalarMoveMask(MVT VT, bool IsLoad, SmallVectorImpl<int> &Mask) { 425 // First element comes from the first element of second source. 426 // Remaining elements: Load zero extends / Move copies from first source. 427 unsigned NumElts = VT.getVectorNumElements(); 428 Mask.push_back(NumElts); 429 for (unsigned i = 1; i < NumElts; i++) 430 Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i); 431 } 432 433 void DecodeEXTRQIMask(int Len, int Idx, 434 SmallVectorImpl<int> &ShuffleMask) { 435 // Only the bottom 6 bits are valid for each immediate. 436 Len &= 0x3F; 437 Idx &= 0x3F; 438 439 // We can only decode this bit extraction instruction as a shuffle if both the 440 // length and index work with whole bytes. 441 if (0 != (Len % 8) || 0 != (Idx % 8)) 442 return; 443 444 // A length of zero is equivalent to a bit length of 64. 445 if (Len == 0) 446 Len = 64; 447 448 // If the length + index exceeds the bottom 64 bits the result is undefined. 449 if ((Len + Idx) > 64) { 450 ShuffleMask.append(16, SM_SentinelUndef); 451 return; 452 } 453 454 // Convert index and index to work with bytes. 455 Len /= 8; 456 Idx /= 8; 457 458 // EXTRQ: Extract Len bytes starting from Idx. Zero pad the remaining bytes 459 // of the lower 64-bits. The upper 64-bits are undefined. 460 for (int i = 0; i != Len; ++i) 461 ShuffleMask.push_back(i + Idx); 462 for (int i = Len; i != 8; ++i) 463 ShuffleMask.push_back(SM_SentinelZero); 464 for (int i = 8; i != 16; ++i) 465 ShuffleMask.push_back(SM_SentinelUndef); 466 } 467 468 void DecodeINSERTQIMask(int Len, int Idx, 469 SmallVectorImpl<int> &ShuffleMask) { 470 // Only the bottom 6 bits are valid for each immediate. 471 Len &= 0x3F; 472 Idx &= 0x3F; 473 474 // We can only decode this bit insertion instruction as a shuffle if both the 475 // length and index work with whole bytes. 476 if (0 != (Len % 8) || 0 != (Idx % 8)) 477 return; 478 479 // A length of zero is equivalent to a bit length of 64. 480 if (Len == 0) 481 Len = 64; 482 483 // If the length + index exceeds the bottom 64 bits the result is undefined. 484 if ((Len + Idx) > 64) { 485 ShuffleMask.append(16, SM_SentinelUndef); 486 return; 487 } 488 489 // Convert index and index to work with bytes. 490 Len /= 8; 491 Idx /= 8; 492 493 // INSERTQ: Extract lowest Len bytes from lower half of second source and 494 // insert over first source starting at Idx byte. The upper 64-bits are 495 // undefined. 496 for (int i = 0; i != Idx; ++i) 497 ShuffleMask.push_back(i); 498 for (int i = 0; i != Len; ++i) 499 ShuffleMask.push_back(i + 16); 500 for (int i = Idx + Len; i != 8; ++i) 501 ShuffleMask.push_back(i); 502 for (int i = 8; i != 16; ++i) 503 ShuffleMask.push_back(SM_SentinelUndef); 504 } 505 506 } // llvm namespace 507