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 DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 83 unsigned VectorSizeInBits = VT.getSizeInBits(); 84 unsigned NumElts = VectorSizeInBits / 8; 85 unsigned NumLanes = VectorSizeInBits / 128; 86 unsigned NumLaneElts = NumElts / NumLanes; 87 88 for (unsigned l = 0; l < NumElts; l += NumLaneElts) 89 for (unsigned i = 0; i < NumLaneElts; ++i) { 90 int M = SM_SentinelZero; 91 if (i >= Imm) M = i - Imm + l; 92 ShuffleMask.push_back(M); 93 } 94 } 95 96 void DecodePSRLDQMask(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 unsigned Base = i + Imm; 105 int M = Base + l; 106 if (Base >= NumLaneElts) M = SM_SentinelZero; 107 ShuffleMask.push_back(M); 108 } 109 } 110 111 void DecodePALIGNRMask(MVT VT, unsigned Imm, 112 SmallVectorImpl<int> &ShuffleMask) { 113 unsigned NumElts = VT.getVectorNumElements(); 114 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8); 115 116 unsigned NumLanes = VT.getSizeInBits() / 128; 117 unsigned NumLaneElts = NumElts / NumLanes; 118 119 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 120 for (unsigned i = 0; i != NumLaneElts; ++i) { 121 unsigned Base = i + Offset; 122 // if i+offset is out of this lane then we actually need the other source 123 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts; 124 ShuffleMask.push_back(Base + l); 125 } 126 } 127 } 128 129 /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*. 130 /// VT indicates the type of the vector allowing it to handle different 131 /// datatypes and vector widths. 132 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 133 unsigned NumElts = VT.getVectorNumElements(); 134 135 unsigned NumLanes = VT.getSizeInBits() / 128; 136 unsigned NumLaneElts = NumElts / NumLanes; 137 138 unsigned NewImm = Imm; 139 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 140 for (unsigned i = 0; i != NumLaneElts; ++i) { 141 ShuffleMask.push_back(NewImm % NumLaneElts + l); 142 NewImm /= NumLaneElts; 143 } 144 if (NumLaneElts == 4) NewImm = Imm; // reload imm 145 } 146 } 147 148 void DecodePSHUFHWMask(MVT VT, unsigned Imm, 149 SmallVectorImpl<int> &ShuffleMask) { 150 unsigned NumElts = VT.getVectorNumElements(); 151 152 for (unsigned l = 0; l != NumElts; l += 8) { 153 unsigned NewImm = Imm; 154 for (unsigned i = 0, e = 4; i != e; ++i) { 155 ShuffleMask.push_back(l + i); 156 } 157 for (unsigned i = 4, e = 8; i != e; ++i) { 158 ShuffleMask.push_back(l + 4 + (NewImm & 3)); 159 NewImm >>= 2; 160 } 161 } 162 } 163 164 void DecodePSHUFLWMask(MVT VT, unsigned Imm, 165 SmallVectorImpl<int> &ShuffleMask) { 166 unsigned NumElts = VT.getVectorNumElements(); 167 168 for (unsigned l = 0; l != NumElts; l += 8) { 169 unsigned NewImm = Imm; 170 for (unsigned i = 0, e = 4; i != e; ++i) { 171 ShuffleMask.push_back(l + (NewImm & 3)); 172 NewImm >>= 2; 173 } 174 for (unsigned i = 4, e = 8; i != e; ++i) { 175 ShuffleMask.push_back(l + i); 176 } 177 } 178 } 179 180 /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates 181 /// the type of the vector allowing it to handle different datatypes and vector 182 /// widths. 183 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 184 unsigned NumElts = VT.getVectorNumElements(); 185 186 unsigned NumLanes = VT.getSizeInBits() / 128; 187 unsigned NumLaneElts = NumElts / NumLanes; 188 189 unsigned NewImm = Imm; 190 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 191 // each half of a lane comes from different source 192 for (unsigned s = 0; s != NumElts*2; s += NumElts) { 193 for (unsigned i = 0; i != NumLaneElts/2; ++i) { 194 ShuffleMask.push_back(NewImm % NumLaneElts + s + l); 195 NewImm /= NumLaneElts; 196 } 197 } 198 if (NumLaneElts == 4) NewImm = Imm; // reload imm 199 } 200 } 201 202 /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd 203 /// and punpckh*. VT indicates the type of the vector allowing it to handle 204 /// different datatypes and vector widths. 205 void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 206 unsigned NumElts = VT.getVectorNumElements(); 207 208 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate 209 // independently on 128-bit lanes. 210 unsigned NumLanes = VT.getSizeInBits() / 128; 211 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX 212 unsigned NumLaneElts = NumElts / NumLanes; 213 214 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 215 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) { 216 ShuffleMask.push_back(i); // Reads from dest/src1 217 ShuffleMask.push_back(i+NumElts); // Reads from src/src2 218 } 219 } 220 } 221 222 /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd 223 /// and punpckl*. VT indicates the type of the vector allowing it to handle 224 /// different datatypes and vector widths. 225 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { 226 unsigned NumElts = VT.getVectorNumElements(); 227 228 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate 229 // independently on 128-bit lanes. 230 unsigned NumLanes = VT.getSizeInBits() / 128; 231 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX 232 unsigned NumLaneElts = NumElts / NumLanes; 233 234 for (unsigned l = 0; l != NumElts; l += NumLaneElts) { 235 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) { 236 ShuffleMask.push_back(i); // Reads from dest/src1 237 ShuffleMask.push_back(i+NumElts); // Reads from src/src2 238 } 239 } 240 } 241 242 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm, 243 SmallVectorImpl<int> &ShuffleMask) { 244 if (Imm & 0x88) 245 return; // Not a shuffle 246 247 unsigned HalfSize = VT.getVectorNumElements()/2; 248 249 for (unsigned l = 0; l != 2; ++l) { 250 unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize; 251 for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i) 252 ShuffleMask.push_back(i); 253 } 254 } 255 256 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) { 257 Type *MaskTy = C->getType(); 258 // It is not an error for the PSHUFB mask to not be a vector of i8 because the 259 // constant pool uniques constants by their bit representation. 260 // e.g. the following take up the same space in the constant pool: 261 // i128 -170141183420855150465331762880109871104 262 // 263 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160> 264 // 265 // <4 x i32> <i32 -2147483648, i32 -2147483648, 266 // i32 -2147483648, i32 -2147483648> 267 268 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits(); 269 270 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512. 271 return; 272 273 // This is a straightforward byte vector. 274 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) { 275 int NumElements = MaskTy->getVectorNumElements(); 276 ShuffleMask.reserve(NumElements); 277 278 for (int i = 0; i < NumElements; ++i) { 279 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte 280 // lane of the vector we're inside. 281 int Base = i < 16 ? 0 : 16; 282 Constant *COp = C->getAggregateElement(i); 283 if (!COp) { 284 ShuffleMask.clear(); 285 return; 286 } else if (isa<UndefValue>(COp)) { 287 ShuffleMask.push_back(SM_SentinelUndef); 288 continue; 289 } 290 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 291 // If the high bit (7) of the byte is set, the element is zeroed. 292 if (Element & (1 << 7)) 293 ShuffleMask.push_back(SM_SentinelZero); 294 else { 295 // Only the least significant 4 bits of the byte are used. 296 int Index = Base + (Element & 0xf); 297 ShuffleMask.push_back(Index); 298 } 299 } 300 } 301 // TODO: Handle funny-looking vectors too. 302 } 303 304 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask, 305 SmallVectorImpl<int> &ShuffleMask) { 306 for (int i = 0, e = RawMask.size(); i < e; ++i) { 307 uint64_t M = RawMask[i]; 308 if (M == (uint64_t)SM_SentinelUndef) { 309 ShuffleMask.push_back(M); 310 continue; 311 } 312 // For AVX vectors with 32 bytes the base of the shuffle is the half of 313 // the vector we're inside. 314 int Base = i < 16 ? 0 : 16; 315 // If the high bit (7) of the byte is set, the element is zeroed. 316 if (M & (1 << 7)) 317 ShuffleMask.push_back(SM_SentinelZero); 318 else { 319 // Only the least significant 4 bits of the byte are used. 320 int Index = Base + (M & 0xf); 321 ShuffleMask.push_back(Index); 322 } 323 } 324 } 325 326 void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 327 int ElementBits = VT.getScalarSizeInBits(); 328 int NumElements = VT.getVectorNumElements(); 329 for (int i = 0; i < NumElements; ++i) { 330 // If there are more than 8 elements in the vector, then any immediate blend 331 // mask applies to each 128-bit lane. There can never be more than 332 // 8 elements in a 128-bit lane with an immediate blend. 333 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i; 334 assert(Bit < 8 && 335 "Immediate blends only operate over 8 elements at a time!"); 336 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i); 337 } 338 } 339 340 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD. 341 /// No VT provided since it only works on 256-bit, 4 element vectors. 342 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { 343 for (unsigned i = 0; i != 4; ++i) { 344 ShuffleMask.push_back((Imm >> (2*i)) & 3); 345 } 346 } 347 348 void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) { 349 Type *MaskTy = C->getType(); 350 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!"); 351 assert(MaskTy->getVectorElementType()->isIntegerTy() && 352 "Expected integer constant mask elements!"); 353 int ElementBits = MaskTy->getScalarSizeInBits(); 354 int NumElements = MaskTy->getVectorNumElements(); 355 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) && 356 "Unexpected number of vector elements."); 357 ShuffleMask.reserve(NumElements); 358 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) { 359 assert((unsigned)NumElements == CDS->getNumElements() && 360 "Constant mask has a different number of elements!"); 361 362 for (int i = 0; i < NumElements; ++i) { 363 int Base = (i * ElementBits / 128) * (128 / ElementBits); 364 uint64_t Element = CDS->getElementAsInteger(i); 365 // Only the least significant 2 bits of the integer are used. 366 int Index = Base + (Element & 0x3); 367 ShuffleMask.push_back(Index); 368 } 369 } else if (auto *CV = dyn_cast<ConstantVector>(C)) { 370 assert((unsigned)NumElements == C->getNumOperands() && 371 "Constant mask has a different number of elements!"); 372 373 for (int i = 0; i < NumElements; ++i) { 374 int Base = (i * ElementBits / 128) * (128 / ElementBits); 375 Constant *COp = CV->getOperand(i); 376 if (isa<UndefValue>(COp)) { 377 ShuffleMask.push_back(SM_SentinelUndef); 378 continue; 379 } 380 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue(); 381 // Only the least significant 2 bits of the integer are used. 382 int Index = Base + (Element & 0x3); 383 ShuffleMask.push_back(Index); 384 } 385 } 386 } 387 388 } // llvm namespace 389