blob: 4fe9a9e949d6048d60ad2ac41bd0b6dda561df16 [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
Simon Pilgrima76a8e52016-07-14 12:07:43 +0000278/// Decodes a broadcast of a subvector to a larger vector type.
279void DecodeSubVectorBroadcast(MVT DstVT, MVT SrcVT,
280 SmallVectorImpl<int> &ShuffleMask) {
281 assert(SrcVT.getScalarType() == DstVT.getScalarType() &&
282 "Non matching vector element types");
283 unsigned NumElts = SrcVT.getVectorNumElements();
284 unsigned Scale = DstVT.getSizeInBits() / SrcVT.getSizeInBits();
285
286 for (unsigned i = 0; i != Scale; ++i)
287 for (unsigned j = 0; j != NumElts; ++j)
288 ShuffleMask.push_back(j);
289}
290
Igor Bregerd7bae452015-10-15 13:29:07 +0000291/// \brief Decode a shuffle packed values at 128-bit granularity
292/// (SHUFF32x4/SHUFF64x2/SHUFI32x4/SHUFI64x2)
293/// immediate mask into a shuffle mask.
294void decodeVSHUF64x2FamilyMask(MVT VT, unsigned Imm,
295 SmallVectorImpl<int> &ShuffleMask) {
296 unsigned NumLanes = VT.getSizeInBits() / 128;
297 unsigned NumElementsInLane = 128 / VT.getScalarSizeInBits();
298 unsigned ControlBitsMask = NumLanes - 1;
299 unsigned NumControlBits = NumLanes / 2;
300
301 for (unsigned l = 0; l != NumLanes; ++l) {
302 unsigned LaneMask = (Imm >> (l * NumControlBits)) & ControlBitsMask;
303 // We actually need the other source.
304 if (l >= NumLanes / 2)
305 LaneMask += NumLanes;
306 for (unsigned i = 0; i != NumElementsInLane; ++i)
307 ShuffleMask.push_back(LaneMask * NumElementsInLane + i);
308 }
309}
310
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000311void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
312 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000313 unsigned HalfSize = VT.getVectorNumElements() / 2;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000314
315 for (unsigned l = 0; l != 2; ++l) {
Simon Pilgrim40343e62015-07-06 22:46:46 +0000316 unsigned HalfMask = Imm >> (l * 4);
317 unsigned HalfBegin = (HalfMask & 0x3) * HalfSize;
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000318 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
Denis Protivenskyb6129022015-07-07 07:48:48 +0000319 ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000320 }
321}
322
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000323void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
324 SmallVectorImpl<int> &ShuffleMask) {
325 for (int i = 0, e = RawMask.size(); i < e; ++i) {
326 uint64_t M = RawMask[i];
327 if (M == (uint64_t)SM_SentinelUndef) {
328 ShuffleMask.push_back(M);
329 continue;
330 }
Simon Pilgrimf33cb612016-03-03 21:55:01 +0000331 // For 256/512-bit vectors the base of the shuffle is the 128-bit
332 // subvector we're inside.
333 int Base = (i / 16) * 16;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000334 // If the high bit (7) of the byte is set, the element is zeroed.
335 if (M & (1 << 7))
336 ShuffleMask.push_back(SM_SentinelZero);
337 else {
338 // Only the least significant 4 bits of the byte are used.
339 int Index = Base + (M & 0xf);
340 ShuffleMask.push_back(Index);
341 }
342 }
343}
344
345void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
346 int ElementBits = VT.getScalarSizeInBits();
347 int NumElements = VT.getVectorNumElements();
348 for (int i = 0; i < NumElements; ++i) {
349 // If there are more than 8 elements in the vector, then any immediate blend
350 // mask applies to each 128-bit lane. There can never be more than
351 // 8 elements in a 128-bit lane with an immediate blend.
352 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i;
353 assert(Bit < 8 &&
354 "Immediate blends only operate over 8 elements at a time!");
355 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
356 }
357}
358
Simon Pilgrimfd4b9b02016-04-16 17:52:07 +0000359void DecodeVPPERMMask(ArrayRef<uint64_t> RawMask,
360 SmallVectorImpl<int> &ShuffleMask) {
361 assert(RawMask.size() == 16 && "Illegal VPPERM shuffle mask size");
362
363 // VPPERM Operation
364 // Bits[4:0] - Byte Index (0 - 31)
365 // Bits[7:5] - Permute Operation
366 //
367 // Permute Operation:
368 // 0 - Source byte (no logical operation).
369 // 1 - Invert source byte.
370 // 2 - Bit reverse of source byte.
371 // 3 - Bit reverse of inverted source byte.
372 // 4 - 00h (zero - fill).
373 // 5 - FFh (ones - fill).
374 // 6 - Most significant bit of source byte replicated in all bit positions.
375 // 7 - Invert most significant bit of source byte and replicate in all bit positions.
376 for (int i = 0, e = RawMask.size(); i < e; ++i) {
377 uint64_t M = RawMask[i];
378 if (M == (uint64_t)SM_SentinelUndef) {
379 ShuffleMask.push_back(M);
380 continue;
381 }
382
Simon Pilgrimf379a6c2016-04-24 15:05:04 +0000383 uint64_t PermuteOp = (M >> 5) & 0x7;
Simon Pilgrimfd4b9b02016-04-16 17:52:07 +0000384 if (PermuteOp == 4) {
385 ShuffleMask.push_back(SM_SentinelZero);
386 continue;
387 }
388 if (PermuteOp != 0) {
389 ShuffleMask.clear();
390 return;
391 }
392
393 uint64_t Index = M & 0x1F;
394 ShuffleMask.push_back((int)Index);
395 }
396}
397
Simon Pilgrima0d73832016-07-03 18:27:37 +0000398/// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
399void DecodeVPERMMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
400 assert((VT.is256BitVector() || VT.is512BitVector()) &&
401 (VT.getScalarSizeInBits() == 64) && "Unexpected vector value type");
402 unsigned NumElts = VT.getVectorNumElements();
403 for (unsigned l = 0; l != NumElts; l += 4)
404 for (unsigned i = 0; i != 4; ++i)
405 ShuffleMask.push_back(l + ((Imm >> (2 * i)) & 3));
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000406}
407
Simon Pilgrime1b6db92016-02-06 16:33:42 +0000408void DecodeZeroExtendMask(MVT SrcScalarVT, MVT DstVT, SmallVectorImpl<int> &Mask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000409 unsigned NumDstElts = DstVT.getVectorNumElements();
Simon Pilgrime1b6db92016-02-06 16:33:42 +0000410 unsigned SrcScalarBits = SrcScalarVT.getSizeInBits();
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000411 unsigned DstScalarBits = DstVT.getScalarSizeInBits();
412 unsigned Scale = DstScalarBits / SrcScalarBits;
413 assert(SrcScalarBits < DstScalarBits &&
414 "Expected zero extension mask to increase scalar size");
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000415
416 for (unsigned i = 0; i != NumDstElts; i++) {
417 Mask.push_back(i);
418 for (unsigned j = 1; j != Scale; j++)
419 Mask.push_back(SM_SentinelZero);
420 }
421}
422
423void DecodeZeroMoveLowMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
424 unsigned NumElts = VT.getVectorNumElements();
425 ShuffleMask.push_back(0);
426 for (unsigned i = 1; i < NumElts; i++)
427 ShuffleMask.push_back(SM_SentinelZero);
428}
429
430void DecodeScalarMoveMask(MVT VT, bool IsLoad, SmallVectorImpl<int> &Mask) {
431 // First element comes from the first element of second source.
432 // Remaining elements: Load zero extends / Move copies from first source.
433 unsigned NumElts = VT.getVectorNumElements();
434 Mask.push_back(NumElts);
435 for (unsigned i = 1; i < NumElts; i++)
436 Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i);
437}
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000438
439void DecodeEXTRQIMask(int Len, int Idx,
440 SmallVectorImpl<int> &ShuffleMask) {
441 // Only the bottom 6 bits are valid for each immediate.
442 Len &= 0x3F;
443 Idx &= 0x3F;
444
445 // We can only decode this bit extraction instruction as a shuffle if both the
446 // length and index work with whole bytes.
447 if (0 != (Len % 8) || 0 != (Idx % 8))
448 return;
449
450 // A length of zero is equivalent to a bit length of 64.
451 if (Len == 0)
452 Len = 64;
453
454 // If the length + index exceeds the bottom 64 bits the result is undefined.
455 if ((Len + Idx) > 64) {
456 ShuffleMask.append(16, SM_SentinelUndef);
457 return;
458 }
459
460 // Convert index and index to work with bytes.
461 Len /= 8;
462 Idx /= 8;
463
464 // EXTRQ: Extract Len bytes starting from Idx. Zero pad the remaining bytes
465 // of the lower 64-bits. The upper 64-bits are undefined.
466 for (int i = 0; i != Len; ++i)
467 ShuffleMask.push_back(i + Idx);
468 for (int i = Len; i != 8; ++i)
469 ShuffleMask.push_back(SM_SentinelZero);
470 for (int i = 8; i != 16; ++i)
471 ShuffleMask.push_back(SM_SentinelUndef);
472}
473
474void DecodeINSERTQIMask(int Len, int Idx,
475 SmallVectorImpl<int> &ShuffleMask) {
476 // Only the bottom 6 bits are valid for each immediate.
477 Len &= 0x3F;
478 Idx &= 0x3F;
479
480 // We can only decode this bit insertion instruction as a shuffle if both the
481 // length and index work with whole bytes.
482 if (0 != (Len % 8) || 0 != (Idx % 8))
483 return;
484
485 // A length of zero is equivalent to a bit length of 64.
486 if (Len == 0)
487 Len = 64;
488
489 // If the length + index exceeds the bottom 64 bits the result is undefined.
490 if ((Len + Idx) > 64) {
491 ShuffleMask.append(16, SM_SentinelUndef);
492 return;
493 }
494
495 // Convert index and index to work with bytes.
496 Len /= 8;
497 Idx /= 8;
498
499 // INSERTQ: Extract lowest Len bytes from lower half of second source and
500 // insert over first source starting at Idx byte. The upper 64-bits are
501 // undefined.
502 for (int i = 0; i != Idx; ++i)
503 ShuffleMask.push_back(i);
504 for (int i = 0; i != Len; ++i)
505 ShuffleMask.push_back(i + 16);
506 for (int i = Idx + Len; i != 8; ++i)
507 ShuffleMask.push_back(i);
508 for (int i = 8; i != 16; ++i)
509 ShuffleMask.push_back(SM_SentinelUndef);
510}
511
Simon Pilgrim40e1a712016-03-05 22:53:31 +0000512void DecodeVPERMILPMask(MVT VT, ArrayRef<uint64_t> RawMask,
513 SmallVectorImpl<int> &ShuffleMask) {
514 unsigned VecSize = VT.getSizeInBits();
515 unsigned EltSize = VT.getScalarSizeInBits();
516 unsigned NumLanes = VecSize / 128;
517 unsigned NumEltsPerLane = VT.getVectorNumElements() / NumLanes;
518 assert((VecSize == 128 || VecSize == 256 || VecSize == 512) &&
519 "Unexpected vector size");
520 assert((EltSize == 32 || EltSize == 64) && "Unexpected element size");
521
522 for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {
523 uint64_t M = RawMask[i];
524 M = (EltSize == 64 ? ((M >> 1) & 0x1) : (M & 0x3));
525 unsigned LaneOffset = i & ~(NumEltsPerLane - 1);
526 ShuffleMask.push_back((int)(LaneOffset + M));
527 }
528}
529
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000530void DecodeVPERMIL2PMask(MVT VT, unsigned M2Z, ArrayRef<uint64_t> RawMask,
531 SmallVectorImpl<int> &ShuffleMask) {
532 unsigned VecSize = VT.getSizeInBits();
533 unsigned EltSize = VT.getScalarSizeInBits();
534 unsigned NumLanes = VecSize / 128;
535 unsigned NumEltsPerLane = VT.getVectorNumElements() / NumLanes;
536 assert((VecSize == 128 || VecSize == 256) &&
537 "Unexpected vector size");
538 assert((EltSize == 32 || EltSize == 64) && "Unexpected element size");
539
540 for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {
541 // VPERMIL2 Operation.
542 // Bits[3] - Match Bit.
543 // Bits[2:1] - (Per Lane) PD Shuffle Mask.
544 // Bits[2:0] - (Per Lane) PS Shuffle Mask.
545 uint64_t Selector = RawMask[i];
Filipe Cabecinhas6e7d5462016-06-06 10:49:56 +0000546 unsigned MatchBit = (Selector >> 3) & 0x1;
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000547
548 // M2Z[0:1] MatchBit
549 // 0Xb X Source selected by Selector index.
550 // 10b 0 Source selected by Selector index.
551 // 10b 1 Zero.
552 // 11b 0 Zero.
553 // 11b 1 Source selected by Selector index.
554 if ((M2Z & 0x2) != 0 && MatchBit != (M2Z & 0x1)) {
555 ShuffleMask.push_back(SM_SentinelZero);
556 continue;
557 }
558
559 unsigned Index = i & ~(NumEltsPerLane - 1);
560 if (EltSize == 64)
561 Index += (Selector >> 1) & 0x1;
562 else
563 Index += Selector & 0x3;
564
565 unsigned SrcOffset = (Selector >> 2) & 1;
566 ShuffleMask.push_back((int)(SrcOffset + Index));
567 }
568}
569
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000570void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask,
571 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrim48adedf2016-07-05 18:31:17 +0000572 uint64_t EltMaskSize = RawMask.size() - 1;
573 for (auto M : RawMask) {
574 M &= EltMaskSize;
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000575 ShuffleMask.push_back((int)M);
576 }
577}
578
579void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask,
580 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrim253ca342016-03-06 21:54:52 +0000581 uint64_t EltMaskSize = (RawMask.size() * 2) - 1;
582 for (auto M : RawMask) {
583 M &= EltMaskSize;
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000584 ShuffleMask.push_back((int)M);
585 }
586}
587
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000588} // llvm namespace