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