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/CodeGen/MachineValueType.h"
18 //===----------------------------------------------------------------------===//
19 // Vector Mask Decoding
20 //===----------------------------------------------------------------------===//
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);
31 // Decode the immediate.
32 unsigned ZMask = Imm & 15;
33 unsigned CountD = (Imm >> 4) & 3;
34 unsigned CountS = (Imm >> 6) & 3;
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;
48 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
49 for (unsigned i = NElts / 2; i != NElts; ++i)
50 ShuffleMask.push_back(NElts + i);
52 for (unsigned i = NElts / 2; i != NElts; ++i)
53 ShuffleMask.push_back(i);
57 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
58 for (unsigned i = 0; i != NElts / 2; ++i)
59 ShuffleMask.push_back(i);
61 for (unsigned i = 0; i != NElts / 2; ++i)
62 ShuffleMask.push_back(NElts + i);
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);
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);
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;
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);
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;
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);
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;
115 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
116 for (unsigned i = 0; i < NumLaneElts; ++i) {
117 unsigned Base = i + Imm;
119 if (Base >= NumLaneElts) M = SM_SentinelZero;
120 ShuffleMask.push_back(M);
124 void DecodePALIGNRMask(MVT VT, unsigned Imm,
125 SmallVectorImpl<int> &ShuffleMask) {
126 unsigned NumElts = VT.getVectorNumElements();
127 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
129 unsigned NumLanes = VT.getSizeInBits() / 128;
130 unsigned NumLaneElts = NumElts / NumLanes;
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);
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();
148 unsigned NumLanes = VT.getSizeInBits() / 128;
149 if (NumLanes == 0) NumLanes = 1; // Handle MMX
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 void DecodePSWAPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
195 unsigned NumElts = VT.getVectorNumElements();
196 unsigned NumHalfElts = NumElts / 2;
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);
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
207 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
208 unsigned NumElts = VT.getVectorNumElements();
210 unsigned NumLanes = VT.getSizeInBits() / 128;
211 unsigned NumLaneElts = NumElts / NumLanes;
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;
222 if (NumLaneElts == 4) NewImm = Imm; // reload imm
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();
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;
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
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();
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;
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
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;
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);
286 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
287 SmallVectorImpl<int> &ShuffleMask) {
288 unsigned HalfSize = VT.getVectorNumElements() / 2;
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);
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);
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.
311 ShuffleMask.push_back(SM_SentinelZero);
313 // Only the least significant 4 bits of the byte are used.
314 int Index = Base + (M & 0xf);
315 ShuffleMask.push_back(Index);
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;
329 "Immediate blends only operate over 8 elements at a time!");
330 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
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);
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");
352 for (unsigned i = 0; i != NumDstElts; i++) {
354 for (unsigned j = 1; j != Scale; j++)
355 Mask.push_back(SM_SentinelZero);
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);
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);
375 void DecodeEXTRQIMask(int Len, int Idx,
376 SmallVectorImpl<int> &ShuffleMask) {
377 // Only the bottom 6 bits are valid for each immediate.
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))
386 // A length of zero is equivalent to a bit length of 64.
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);
396 // Convert index and index to work with bytes.
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);
410 void DecodeINSERTQIMask(int Len, int Idx,
411 SmallVectorImpl<int> &ShuffleMask) {
412 // Only the bottom 6 bits are valid for each immediate.
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))
421 // A length of zero is equivalent to a bit length of 64.
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);
431 // Convert index and index to work with bytes.
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
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);
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);
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);