1 //===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Define several functions to decode x86 specific shuffle semantics into a
11 // generic vector mask.
13 //===----------------------------------------------------------------------===//
15 #include "X86ShuffleDecode.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/CodeGen/MachineValueType.h"
19 //===----------------------------------------------------------------------===//
20 // Vector Mask Decoding
21 //===----------------------------------------------------------------------===//
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);
32 // Decode the immediate.
33 unsigned ZMask = Imm & 15;
34 unsigned CountD = (Imm >> 4) & 3;
35 unsigned CountS = (Imm >> 6) & 3;
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;
49 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
50 for (unsigned i = NElts / 2; i != NElts; ++i)
51 ShuffleMask.push_back(NElts + i);
53 for (unsigned i = NElts / 2; i != NElts; ++i)
54 ShuffleMask.push_back(i);
58 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
59 for (unsigned i = 0; i != NElts / 2; ++i)
60 ShuffleMask.push_back(i);
62 for (unsigned i = 0; i != NElts / 2; ++i)
63 ShuffleMask.push_back(NElts + i);
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);
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);
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;
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);
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;
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);
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;
116 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
117 for (unsigned i = 0; i < NumLaneElts; ++i) {
118 unsigned Base = i + Imm;
120 if (Base >= NumLaneElts) M = SM_SentinelZero;
121 ShuffleMask.push_back(M);
125 void DecodePALIGNRMask(MVT VT, unsigned Imm,
126 SmallVectorImpl<int> &ShuffleMask) {
127 unsigned NumElts = VT.getVectorNumElements();
128 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
130 unsigned NumLanes = VT.getSizeInBits() / 128;
131 unsigned NumLaneElts = NumElts / NumLanes;
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);
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();
149 unsigned NumLanes = VT.getSizeInBits() / 128;
150 unsigned NumLaneElts = NumElts / NumLanes;
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;
158 if (NumLaneElts == 4) NewImm = Imm; // reload imm
162 void DecodePSHUFHWMask(MVT VT, unsigned Imm,
163 SmallVectorImpl<int> &ShuffleMask) {
164 unsigned NumElts = VT.getVectorNumElements();
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);
171 for (unsigned i = 4, e = 8; i != e; ++i) {
172 ShuffleMask.push_back(l + 4 + (NewImm & 3));
178 void DecodePSHUFLWMask(MVT VT, unsigned Imm,
179 SmallVectorImpl<int> &ShuffleMask) {
180 unsigned NumElts = VT.getVectorNumElements();
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));
188 for (unsigned i = 4, e = 8; i != e; ++i) {
189 ShuffleMask.push_back(l + i);
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
197 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
198 unsigned NumElts = VT.getVectorNumElements();
200 unsigned NumLanes = VT.getSizeInBits() / 128;
201 unsigned NumLaneElts = NumElts / NumLanes;
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;
212 if (NumLaneElts == 4) NewImm = Imm; // reload imm
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();
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;
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
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();
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;
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
256 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
257 SmallVectorImpl<int> &ShuffleMask) {
258 unsigned HalfSize = VT.getVectorNumElements() / 2;
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 : i);
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
275 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
277 // <4 x i32> <i32 -2147483648, i32 -2147483648,
278 // i32 -2147483648, i32 -2147483648>
280 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
282 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
285 // This is a straightforward byte vector.
286 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) {
287 int NumElements = MaskTy->getVectorNumElements();
288 ShuffleMask.reserve(NumElements);
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);
298 } else if (isa<UndefValue>(COp)) {
299 ShuffleMask.push_back(SM_SentinelUndef);
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);
307 // Only the least significant 4 bits of the byte are used.
308 int Index = Base + (Element & 0xf);
309 ShuffleMask.push_back(Index);
313 // TODO: Handle funny-looking vectors too.
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);
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.
329 ShuffleMask.push_back(SM_SentinelZero);
331 // Only the least significant 4 bits of the byte are used.
332 int Index = Base + (M & 0xf);
333 ShuffleMask.push_back(Index);
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;
347 "Immediate blends only operate over 8 elements at a time!");
348 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
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);
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!");
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);
381 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
382 assert((unsigned)NumElements == C->getNumOperands() &&
383 "Constant mask has a different number of elements!");
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);
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);
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");
410 for (unsigned i = 0; i != NumDstElts; i++) {
412 for (unsigned j = 1; j != Scale; j++)
413 Mask.push_back(SM_SentinelZero);
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);
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);
433 void DecodeEXTRQIMask(int Len, int Idx,
434 SmallVectorImpl<int> &ShuffleMask) {
435 // Only the bottom 6 bits are valid for each immediate.
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))
444 // A length of zero is equivalent to a bit length of 64.
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);
454 // Convert index and index to work with bytes.
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);
468 void DecodeINSERTQIMask(int Len, int Idx,
469 SmallVectorImpl<int> &ShuffleMask) {
470 // Only the bottom 6 bits are valid for each immediate.
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))
479 // A length of zero is equivalent to a bit length of 64.
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);
489 // Convert index and index to work with bytes.
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
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);