blob: 472474d566c0d78008a4f087e3cefcf64c1ceda2 [file] [log] [blame]
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +00001//===-- 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"
Mehdi Aminib550cb12016-04-18 09:17:29 +000016#include "llvm/ADT/ArrayRef.h"
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000017#include "llvm/CodeGen/MachineValueType.h"
18
19//===----------------------------------------------------------------------===//
20// Vector Mask Decoding
21//===----------------------------------------------------------------------===//
22
23namespace llvm {
24
25void 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.
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000038 unsigned InVal = 4 + CountS;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000039 // 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
Simon Pilgrima3d67442016-02-07 15:39:22 +000048void DecodeInsertElementMask(MVT VT, unsigned Idx, unsigned Len,
49 SmallVectorImpl<int> &ShuffleMask) {
50 unsigned NumElts = VT.getVectorNumElements();
51 assert((Idx + Len) <= NumElts && "Insertion out of range");
52
53 for (unsigned i = 0; i != NumElts; ++i)
54 ShuffleMask.push_back(i);
55 for (unsigned i = 0; i != Len; ++i)
56 ShuffleMask[Idx + i] = NumElts + i;
57}
58
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000059// <3,1> or <6,7,2,3>
60void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000061 for (unsigned i = NElts / 2; i != NElts; ++i)
62 ShuffleMask.push_back(NElts + i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000063
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000064 for (unsigned i = NElts / 2; i != NElts; ++i)
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000065 ShuffleMask.push_back(i);
66}
67
68// <0,2> or <0,1,4,5>
69void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000070 for (unsigned i = 0; i != NElts / 2; ++i)
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000071 ShuffleMask.push_back(i);
72
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000073 for (unsigned i = 0; i != NElts / 2; ++i)
74 ShuffleMask.push_back(NElts + i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000075}
76
77void DecodeMOVSLDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
78 unsigned NumElts = VT.getVectorNumElements();
79 for (int i = 0, e = NumElts / 2; i < e; ++i) {
80 ShuffleMask.push_back(2 * i);
81 ShuffleMask.push_back(2 * i);
82 }
83}
84
85void DecodeMOVSHDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
86 unsigned NumElts = VT.getVectorNumElements();
87 for (int i = 0, e = NumElts / 2; i < e; ++i) {
88 ShuffleMask.push_back(2 * i + 1);
89 ShuffleMask.push_back(2 * i + 1);
90 }
91}
92
93void DecodeMOVDDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
94 unsigned VectorSizeInBits = VT.getSizeInBits();
95 unsigned ScalarSizeInBits = VT.getScalarSizeInBits();
96 unsigned NumElts = VT.getVectorNumElements();
97 unsigned NumLanes = VectorSizeInBits / 128;
98 unsigned NumLaneElts = NumElts / NumLanes;
99 unsigned NumLaneSubElts = 64 / ScalarSizeInBits;
100
101 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
102 for (unsigned i = 0; i < NumLaneElts; i += NumLaneSubElts)
103 for (unsigned s = 0; s != NumLaneSubElts; s++)
104 ShuffleMask.push_back(l + s);
105}
106
107void DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
108 unsigned VectorSizeInBits = VT.getSizeInBits();
109 unsigned NumElts = VectorSizeInBits / 8;
110 unsigned NumLanes = VectorSizeInBits / 128;
111 unsigned NumLaneElts = NumElts / NumLanes;
112
113 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
114 for (unsigned i = 0; i < NumLaneElts; ++i) {
115 int M = SM_SentinelZero;
116 if (i >= Imm) M = i - Imm + l;
117 ShuffleMask.push_back(M);
118 }
119}
120
121void DecodePSRLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
122 unsigned VectorSizeInBits = VT.getSizeInBits();
123 unsigned NumElts = VectorSizeInBits / 8;
124 unsigned NumLanes = VectorSizeInBits / 128;
125 unsigned NumLaneElts = NumElts / NumLanes;
126
127 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
128 for (unsigned i = 0; i < NumLaneElts; ++i) {
129 unsigned Base = i + Imm;
130 int M = Base + l;
131 if (Base >= NumLaneElts) M = SM_SentinelZero;
132 ShuffleMask.push_back(M);
133 }
134}
135
136void DecodePALIGNRMask(MVT VT, unsigned Imm,
137 SmallVectorImpl<int> &ShuffleMask) {
138 unsigned NumElts = VT.getVectorNumElements();
139 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
140
141 unsigned NumLanes = VT.getSizeInBits() / 128;
142 unsigned NumLaneElts = NumElts / NumLanes;
143
144 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
145 for (unsigned i = 0; i != NumLaneElts; ++i) {
146 unsigned Base = i + Offset;
147 // if i+offset is out of this lane then we actually need the other source
148 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
149 ShuffleMask.push_back(Base + l);
150 }
151 }
152}
153
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000154/// DecodePSHUFMask - This decodes the shuffle masks for pshufw, pshufd, and vpermilp*.
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000155/// VT indicates the type of the vector allowing it to handle different
156/// datatypes and vector widths.
157void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
158 unsigned NumElts = VT.getVectorNumElements();
159
160 unsigned NumLanes = VT.getSizeInBits() / 128;
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000161 if (NumLanes == 0) NumLanes = 1; // Handle MMX
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000162 unsigned NumLaneElts = NumElts / NumLanes;
163
164 unsigned NewImm = Imm;
165 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
166 for (unsigned i = 0; i != NumLaneElts; ++i) {
167 ShuffleMask.push_back(NewImm % NumLaneElts + l);
168 NewImm /= NumLaneElts;
169 }
170 if (NumLaneElts == 4) NewImm = Imm; // reload imm
171 }
172}
173
174void DecodePSHUFHWMask(MVT VT, unsigned Imm,
175 SmallVectorImpl<int> &ShuffleMask) {
176 unsigned NumElts = VT.getVectorNumElements();
177
178 for (unsigned l = 0; l != NumElts; l += 8) {
179 unsigned NewImm = Imm;
180 for (unsigned i = 0, e = 4; i != e; ++i) {
181 ShuffleMask.push_back(l + i);
182 }
183 for (unsigned i = 4, e = 8; i != e; ++i) {
184 ShuffleMask.push_back(l + 4 + (NewImm & 3));
185 NewImm >>= 2;
186 }
187 }
188}
189
190void DecodePSHUFLWMask(MVT VT, unsigned Imm,
191 SmallVectorImpl<int> &ShuffleMask) {
192 unsigned NumElts = VT.getVectorNumElements();
193
194 for (unsigned l = 0; l != NumElts; l += 8) {
195 unsigned NewImm = Imm;
196 for (unsigned i = 0, e = 4; i != e; ++i) {
197 ShuffleMask.push_back(l + (NewImm & 3));
198 NewImm >>= 2;
199 }
200 for (unsigned i = 4, e = 8; i != e; ++i) {
201 ShuffleMask.push_back(l + i);
202 }
203 }
204}
205
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000206void DecodePSWAPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
207 unsigned NumElts = VT.getVectorNumElements();
208 unsigned NumHalfElts = NumElts / 2;
209
210 for (unsigned l = 0; l != NumHalfElts; ++l)
211 ShuffleMask.push_back(l + NumHalfElts);
212 for (unsigned h = 0; h != NumHalfElts; ++h)
213 ShuffleMask.push_back(h);
214}
215
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000216/// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
217/// the type of the vector allowing it to handle different datatypes and vector
218/// widths.
219void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
220 unsigned NumElts = VT.getVectorNumElements();
221
222 unsigned NumLanes = VT.getSizeInBits() / 128;
223 unsigned NumLaneElts = NumElts / NumLanes;
224
225 unsigned NewImm = Imm;
226 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
227 // each half of a lane comes from different source
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000228 for (unsigned s = 0; s != NumElts * 2; s += NumElts) {
229 for (unsigned i = 0; i != NumLaneElts / 2; ++i) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000230 ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
231 NewImm /= NumLaneElts;
232 }
233 }
234 if (NumLaneElts == 4) NewImm = Imm; // reload imm
235 }
236}
237
238/// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
239/// and punpckh*. VT indicates the type of the vector allowing it to handle
240/// different datatypes and vector widths.
241void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
242 unsigned NumElts = VT.getVectorNumElements();
243
244 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
245 // independently on 128-bit lanes.
246 unsigned NumLanes = VT.getSizeInBits() / 128;
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000247 if (NumLanes == 0) NumLanes = 1; // Handle MMX
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000248 unsigned NumLaneElts = NumElts / NumLanes;
249
250 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000251 for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) {
252 ShuffleMask.push_back(i); // Reads from dest/src1
253 ShuffleMask.push_back(i + NumElts); // Reads from src/src2
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000254 }
255 }
256}
257
258/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
259/// and punpckl*. VT indicates the type of the vector allowing it to handle
260/// different datatypes and vector widths.
261void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
262 unsigned NumElts = VT.getVectorNumElements();
263
264 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
265 // independently on 128-bit lanes.
266 unsigned NumLanes = VT.getSizeInBits() / 128;
267 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
268 unsigned NumLaneElts = NumElts / NumLanes;
269
270 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000271 for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) {
272 ShuffleMask.push_back(i); // Reads from dest/src1
273 ShuffleMask.push_back(i + NumElts); // Reads from src/src2
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000274 }
275 }
276}
277
Igor Bregerd7bae452015-10-15 13:29:07 +0000278/// \brief Decode a shuffle packed values at 128-bit granularity
279/// (SHUFF32x4/SHUFF64x2/SHUFI32x4/SHUFI64x2)
280/// immediate mask into a shuffle mask.
281void decodeVSHUF64x2FamilyMask(MVT VT, unsigned Imm,
282 SmallVectorImpl<int> &ShuffleMask) {
283 unsigned NumLanes = VT.getSizeInBits() / 128;
284 unsigned NumElementsInLane = 128 / VT.getScalarSizeInBits();
285 unsigned ControlBitsMask = NumLanes - 1;
286 unsigned NumControlBits = NumLanes / 2;
287
288 for (unsigned l = 0; l != NumLanes; ++l) {
289 unsigned LaneMask = (Imm >> (l * NumControlBits)) & ControlBitsMask;
290 // We actually need the other source.
291 if (l >= NumLanes / 2)
292 LaneMask += NumLanes;
293 for (unsigned i = 0; i != NumElementsInLane; ++i)
294 ShuffleMask.push_back(LaneMask * NumElementsInLane + i);
295 }
296}
297
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000298void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
299 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000300 unsigned HalfSize = VT.getVectorNumElements() / 2;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000301
302 for (unsigned l = 0; l != 2; ++l) {
Simon Pilgrim40343e62015-07-06 22:46:46 +0000303 unsigned HalfMask = Imm >> (l * 4);
304 unsigned HalfBegin = (HalfMask & 0x3) * HalfSize;
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000305 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
Denis Protivenskyb6129022015-07-07 07:48:48 +0000306 ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000307 }
308}
309
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000310void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
311 SmallVectorImpl<int> &ShuffleMask) {
312 for (int i = 0, e = RawMask.size(); i < e; ++i) {
313 uint64_t M = RawMask[i];
314 if (M == (uint64_t)SM_SentinelUndef) {
315 ShuffleMask.push_back(M);
316 continue;
317 }
Simon Pilgrimf33cb612016-03-03 21:55:01 +0000318 // For 256/512-bit vectors the base of the shuffle is the 128-bit
319 // subvector we're inside.
320 int Base = (i / 16) * 16;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000321 // If the high bit (7) of the byte is set, the element is zeroed.
322 if (M & (1 << 7))
323 ShuffleMask.push_back(SM_SentinelZero);
324 else {
325 // Only the least significant 4 bits of the byte are used.
326 int Index = Base + (M & 0xf);
327 ShuffleMask.push_back(Index);
328 }
329 }
330}
331
332void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
333 int ElementBits = VT.getScalarSizeInBits();
334 int NumElements = VT.getVectorNumElements();
335 for (int i = 0; i < NumElements; ++i) {
336 // If there are more than 8 elements in the vector, then any immediate blend
337 // mask applies to each 128-bit lane. There can never be more than
338 // 8 elements in a 128-bit lane with an immediate blend.
339 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i;
340 assert(Bit < 8 &&
341 "Immediate blends only operate over 8 elements at a time!");
342 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
343 }
344}
345
Simon Pilgrimfd4b9b02016-04-16 17:52:07 +0000346void DecodeVPPERMMask(ArrayRef<uint64_t> RawMask,
347 SmallVectorImpl<int> &ShuffleMask) {
348 assert(RawMask.size() == 16 && "Illegal VPPERM shuffle mask size");
349
350 // VPPERM Operation
351 // Bits[4:0] - Byte Index (0 - 31)
352 // Bits[7:5] - Permute Operation
353 //
354 // Permute Operation:
355 // 0 - Source byte (no logical operation).
356 // 1 - Invert source byte.
357 // 2 - Bit reverse of source byte.
358 // 3 - Bit reverse of inverted source byte.
359 // 4 - 00h (zero - fill).
360 // 5 - FFh (ones - fill).
361 // 6 - Most significant bit of source byte replicated in all bit positions.
362 // 7 - Invert most significant bit of source byte and replicate in all bit positions.
363 for (int i = 0, e = RawMask.size(); i < e; ++i) {
364 uint64_t M = RawMask[i];
365 if (M == (uint64_t)SM_SentinelUndef) {
366 ShuffleMask.push_back(M);
367 continue;
368 }
369
Simon Pilgrimf379a6c2016-04-24 15:05:04 +0000370 uint64_t PermuteOp = (M >> 5) & 0x7;
Simon Pilgrimfd4b9b02016-04-16 17:52:07 +0000371 if (PermuteOp == 4) {
372 ShuffleMask.push_back(SM_SentinelZero);
373 continue;
374 }
375 if (PermuteOp != 0) {
376 ShuffleMask.clear();
377 return;
378 }
379
380 uint64_t Index = M & 0x1F;
381 ShuffleMask.push_back((int)Index);
382 }
383}
384
385 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000386/// No VT provided since it only works on 256-bit, 4 element vectors.
387void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
388 for (unsigned i = 0; i != 4; ++i) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000389 ShuffleMask.push_back((Imm >> (2 * i)) & 3);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000390 }
391}
392
Simon Pilgrime1b6db92016-02-06 16:33:42 +0000393void DecodeZeroExtendMask(MVT SrcScalarVT, MVT DstVT, SmallVectorImpl<int> &Mask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000394 unsigned NumDstElts = DstVT.getVectorNumElements();
Simon Pilgrime1b6db92016-02-06 16:33:42 +0000395 unsigned SrcScalarBits = SrcScalarVT.getSizeInBits();
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000396 unsigned DstScalarBits = DstVT.getScalarSizeInBits();
397 unsigned Scale = DstScalarBits / SrcScalarBits;
398 assert(SrcScalarBits < DstScalarBits &&
399 "Expected zero extension mask to increase scalar size");
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000400
401 for (unsigned i = 0; i != NumDstElts; i++) {
402 Mask.push_back(i);
403 for (unsigned j = 1; j != Scale; j++)
404 Mask.push_back(SM_SentinelZero);
405 }
406}
407
408void DecodeZeroMoveLowMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
409 unsigned NumElts = VT.getVectorNumElements();
410 ShuffleMask.push_back(0);
411 for (unsigned i = 1; i < NumElts; i++)
412 ShuffleMask.push_back(SM_SentinelZero);
413}
414
415void DecodeScalarMoveMask(MVT VT, bool IsLoad, SmallVectorImpl<int> &Mask) {
416 // First element comes from the first element of second source.
417 // Remaining elements: Load zero extends / Move copies from first source.
418 unsigned NumElts = VT.getVectorNumElements();
419 Mask.push_back(NumElts);
420 for (unsigned i = 1; i < NumElts; i++)
421 Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i);
422}
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000423
424void DecodeEXTRQIMask(int Len, int Idx,
425 SmallVectorImpl<int> &ShuffleMask) {
426 // Only the bottom 6 bits are valid for each immediate.
427 Len &= 0x3F;
428 Idx &= 0x3F;
429
430 // We can only decode this bit extraction instruction as a shuffle if both the
431 // length and index work with whole bytes.
432 if (0 != (Len % 8) || 0 != (Idx % 8))
433 return;
434
435 // A length of zero is equivalent to a bit length of 64.
436 if (Len == 0)
437 Len = 64;
438
439 // If the length + index exceeds the bottom 64 bits the result is undefined.
440 if ((Len + Idx) > 64) {
441 ShuffleMask.append(16, SM_SentinelUndef);
442 return;
443 }
444
445 // Convert index and index to work with bytes.
446 Len /= 8;
447 Idx /= 8;
448
449 // EXTRQ: Extract Len bytes starting from Idx. Zero pad the remaining bytes
450 // of the lower 64-bits. The upper 64-bits are undefined.
451 for (int i = 0; i != Len; ++i)
452 ShuffleMask.push_back(i + Idx);
453 for (int i = Len; i != 8; ++i)
454 ShuffleMask.push_back(SM_SentinelZero);
455 for (int i = 8; i != 16; ++i)
456 ShuffleMask.push_back(SM_SentinelUndef);
457}
458
459void DecodeINSERTQIMask(int Len, int Idx,
460 SmallVectorImpl<int> &ShuffleMask) {
461 // Only the bottom 6 bits are valid for each immediate.
462 Len &= 0x3F;
463 Idx &= 0x3F;
464
465 // We can only decode this bit insertion instruction as a shuffle if both the
466 // length and index work with whole bytes.
467 if (0 != (Len % 8) || 0 != (Idx % 8))
468 return;
469
470 // A length of zero is equivalent to a bit length of 64.
471 if (Len == 0)
472 Len = 64;
473
474 // If the length + index exceeds the bottom 64 bits the result is undefined.
475 if ((Len + Idx) > 64) {
476 ShuffleMask.append(16, SM_SentinelUndef);
477 return;
478 }
479
480 // Convert index and index to work with bytes.
481 Len /= 8;
482 Idx /= 8;
483
484 // INSERTQ: Extract lowest Len bytes from lower half of second source and
485 // insert over first source starting at Idx byte. The upper 64-bits are
486 // undefined.
487 for (int i = 0; i != Idx; ++i)
488 ShuffleMask.push_back(i);
489 for (int i = 0; i != Len; ++i)
490 ShuffleMask.push_back(i + 16);
491 for (int i = Idx + Len; i != 8; ++i)
492 ShuffleMask.push_back(i);
493 for (int i = 8; i != 16; ++i)
494 ShuffleMask.push_back(SM_SentinelUndef);
495}
496
Simon Pilgrim40e1a712016-03-05 22:53:31 +0000497void DecodeVPERMILPMask(MVT VT, ArrayRef<uint64_t> RawMask,
498 SmallVectorImpl<int> &ShuffleMask) {
499 unsigned VecSize = VT.getSizeInBits();
500 unsigned EltSize = VT.getScalarSizeInBits();
501 unsigned NumLanes = VecSize / 128;
502 unsigned NumEltsPerLane = VT.getVectorNumElements() / NumLanes;
503 assert((VecSize == 128 || VecSize == 256 || VecSize == 512) &&
504 "Unexpected vector size");
505 assert((EltSize == 32 || EltSize == 64) && "Unexpected element size");
506
507 for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {
508 uint64_t M = RawMask[i];
509 M = (EltSize == 64 ? ((M >> 1) & 0x1) : (M & 0x3));
510 unsigned LaneOffset = i & ~(NumEltsPerLane - 1);
511 ShuffleMask.push_back((int)(LaneOffset + M));
512 }
513}
514
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000515void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask,
516 SmallVectorImpl<int> &ShuffleMask) {
517 for (int i = 0, e = RawMask.size(); i < e; ++i) {
518 uint64_t M = RawMask[i];
519 ShuffleMask.push_back((int)M);
520 }
521}
522
523void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask,
524 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrim253ca342016-03-06 21:54:52 +0000525 uint64_t EltMaskSize = (RawMask.size() * 2) - 1;
526 for (auto M : RawMask) {
527 M &= EltMaskSize;
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000528 ShuffleMask.push_back((int)M);
529 }
530}
531
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000532} // llvm namespace