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) {
259 return; // Not a shuffle
261 unsigned HalfSize = VT.getVectorNumElements() / 2;
263 for (unsigned l = 0; l != 2; ++l) {
264 unsigned HalfBegin = ((Imm >> (l * 4)) & 0x3) * HalfSize;
265 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
266 ShuffleMask.push_back(i);
270 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
271 Type *MaskTy = C->getType();
272 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
273 // constant pool uniques constants by their bit representation.
274 // e.g. the following take up the same space in the constant pool:
275 // i128 -170141183420855150465331762880109871104
277 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
279 // <4 x i32> <i32 -2147483648, i32 -2147483648,
280 // i32 -2147483648, i32 -2147483648>
282 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
284 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
287 // This is a straightforward byte vector.
288 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) {
289 int NumElements = MaskTy->getVectorNumElements();
290 ShuffleMask.reserve(NumElements);
292 for (int i = 0; i < NumElements; ++i) {
293 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
294 // lane of the vector we're inside.
295 int Base = i < 16 ? 0 : 16;
296 Constant *COp = C->getAggregateElement(i);
300 } else if (isa<UndefValue>(COp)) {
301 ShuffleMask.push_back(SM_SentinelUndef);
304 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
305 // If the high bit (7) of the byte is set, the element is zeroed.
306 if (Element & (1 << 7))
307 ShuffleMask.push_back(SM_SentinelZero);
309 // Only the least significant 4 bits of the byte are used.
310 int Index = Base + (Element & 0xf);
311 ShuffleMask.push_back(Index);
315 // TODO: Handle funny-looking vectors too.
318 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
319 SmallVectorImpl<int> &ShuffleMask) {
320 for (int i = 0, e = RawMask.size(); i < e; ++i) {
321 uint64_t M = RawMask[i];
322 if (M == (uint64_t)SM_SentinelUndef) {
323 ShuffleMask.push_back(M);
326 // For AVX vectors with 32 bytes the base of the shuffle is the half of
327 // the vector we're inside.
328 int Base = i < 16 ? 0 : 16;
329 // If the high bit (7) of the byte is set, the element is zeroed.
331 ShuffleMask.push_back(SM_SentinelZero);
333 // Only the least significant 4 bits of the byte are used.
334 int Index = Base + (M & 0xf);
335 ShuffleMask.push_back(Index);
340 void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
341 int ElementBits = VT.getScalarSizeInBits();
342 int NumElements = VT.getVectorNumElements();
343 for (int i = 0; i < NumElements; ++i) {
344 // If there are more than 8 elements in the vector, then any immediate blend
345 // mask applies to each 128-bit lane. There can never be more than
346 // 8 elements in a 128-bit lane with an immediate blend.
347 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i;
349 "Immediate blends only operate over 8 elements at a time!");
350 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
354 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
355 /// No VT provided since it only works on 256-bit, 4 element vectors.
356 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
357 for (unsigned i = 0; i != 4; ++i) {
358 ShuffleMask.push_back((Imm >> (2 * i)) & 3);
362 void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
363 Type *MaskTy = C->getType();
364 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
365 assert(MaskTy->getVectorElementType()->isIntegerTy() &&
366 "Expected integer constant mask elements!");
367 int ElementBits = MaskTy->getScalarSizeInBits();
368 int NumElements = MaskTy->getVectorNumElements();
369 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
370 "Unexpected number of vector elements.");
371 ShuffleMask.reserve(NumElements);
372 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
373 assert((unsigned)NumElements == CDS->getNumElements() &&
374 "Constant mask has a different number of elements!");
376 for (int i = 0; i < NumElements; ++i) {
377 int Base = (i * ElementBits / 128) * (128 / ElementBits);
378 uint64_t Element = CDS->getElementAsInteger(i);
379 // Only the least significant 2 bits of the integer are used.
380 int Index = Base + (Element & 0x3);
381 ShuffleMask.push_back(Index);
383 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
384 assert((unsigned)NumElements == C->getNumOperands() &&
385 "Constant mask has a different number of elements!");
387 for (int i = 0; i < NumElements; ++i) {
388 int Base = (i * ElementBits / 128) * (128 / ElementBits);
389 Constant *COp = CV->getOperand(i);
390 if (isa<UndefValue>(COp)) {
391 ShuffleMask.push_back(SM_SentinelUndef);
394 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
395 // Only the least significant 2 bits of the integer are used.
396 int Index = Base + (Element & 0x3);
397 ShuffleMask.push_back(Index);
402 void DecodeZeroExtendMask(MVT SrcVT, MVT DstVT, SmallVectorImpl<int> &Mask) {
403 unsigned NumDstElts = DstVT.getVectorNumElements();
404 unsigned SrcScalarBits = SrcVT.getScalarSizeInBits();
405 unsigned DstScalarBits = DstVT.getScalarSizeInBits();
406 unsigned Scale = DstScalarBits / SrcScalarBits;
407 assert(SrcScalarBits < DstScalarBits &&
408 "Expected zero extension mask to increase scalar size");
409 assert(SrcVT.getVectorNumElements() >= NumDstElts &&
410 "Too many zero extension lanes");
412 for (unsigned i = 0; i != NumDstElts; i++) {
414 for (unsigned j = 1; j != Scale; j++)
415 Mask.push_back(SM_SentinelZero);
419 void DecodeZeroMoveLowMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
420 unsigned NumElts = VT.getVectorNumElements();
421 ShuffleMask.push_back(0);
422 for (unsigned i = 1; i < NumElts; i++)
423 ShuffleMask.push_back(SM_SentinelZero);
426 void DecodeScalarMoveMask(MVT VT, bool IsLoad, SmallVectorImpl<int> &Mask) {
427 // First element comes from the first element of second source.
428 // Remaining elements: Load zero extends / Move copies from first source.
429 unsigned NumElts = VT.getVectorNumElements();
430 Mask.push_back(NumElts);
431 for (unsigned i = 1; i < NumElts; i++)
432 Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i);