blob: 8ac1762a30cf3790c98ae2ef3daa375e457aed83 [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
18//===----------------------------------------------------------------------===//
19// Vector Mask Decoding
20//===----------------------------------------------------------------------===//
21
22namespace llvm {
23
24void 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);
30
31 // Decode the immediate.
32 unsigned ZMask = Imm & 15;
33 unsigned CountD = (Imm >> 4) & 3;
34 unsigned CountS = (Imm >> 6) & 3;
35
36 // CountS selects which input element to use.
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000037 unsigned InVal = 4 + CountS;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000038 // 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;
45}
46
Craig Topperacaba3b2018-03-12 16:43:11 +000047void DecodeInsertElementMask(unsigned NumElts, unsigned Idx, unsigned Len,
Simon Pilgrima3d67442016-02-07 15:39:22 +000048 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrima3d67442016-02-07 15:39:22 +000049 assert((Idx + Len) <= NumElts && "Insertion out of range");
50
51 for (unsigned i = 0; i != NumElts; ++i)
52 ShuffleMask.push_back(i);
53 for (unsigned i = 0; i != Len; ++i)
54 ShuffleMask[Idx + i] = NumElts + i;
55}
56
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000057// <3,1> or <6,7,2,3>
58void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000059 for (unsigned i = NElts / 2; i != NElts; ++i)
60 ShuffleMask.push_back(NElts + i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000061
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000062 for (unsigned i = NElts / 2; i != NElts; ++i)
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000063 ShuffleMask.push_back(i);
64}
65
66// <0,2> or <0,1,4,5>
67void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000068 for (unsigned i = 0; i != NElts / 2; ++i)
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000069 ShuffleMask.push_back(i);
70
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000071 for (unsigned i = 0; i != NElts / 2; ++i)
72 ShuffleMask.push_back(NElts + i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000073}
74
Craig Topperacaba3b2018-03-12 16:43:11 +000075void DecodeMOVSLDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000076 for (int i = 0, e = NumElts / 2; i < e; ++i) {
77 ShuffleMask.push_back(2 * i);
78 ShuffleMask.push_back(2 * i);
79 }
80}
81
Craig Topperacaba3b2018-03-12 16:43:11 +000082void DecodeMOVSHDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000083 for (int i = 0, e = NumElts / 2; i < e; ++i) {
84 ShuffleMask.push_back(2 * i + 1);
85 ShuffleMask.push_back(2 * i + 1);
86 }
87}
88
Craig Topperacaba3b2018-03-12 16:43:11 +000089void DecodeMOVDDUPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
90 const unsigned NumLaneElts = 2;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000091
92 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
Craig Topperacaba3b2018-03-12 16:43:11 +000093 for (unsigned i = 0; i < NumLaneElts; ++i)
94 ShuffleMask.push_back(l);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000095}
96
Craig Topperacaba3b2018-03-12 16:43:11 +000097void DecodePSLLDQMask(unsigned NumElts, unsigned Imm,
98 SmallVectorImpl<int> &ShuffleMask) {
99 const unsigned NumLaneElts = 16;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000100
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);
106 }
107}
108
Craig Topperacaba3b2018-03-12 16:43:11 +0000109void DecodePSRLDQMask(unsigned NumElts, unsigned Imm,
110 SmallVectorImpl<int> &ShuffleMask) {
111 const unsigned NumLaneElts = 16;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000112
113 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
114 for (unsigned i = 0; i < NumLaneElts; ++i) {
115 unsigned Base = i + Imm;
116 int M = Base + l;
117 if (Base >= NumLaneElts) M = SM_SentinelZero;
118 ShuffleMask.push_back(M);
119 }
120}
121
Craig Topperacaba3b2018-03-12 16:43:11 +0000122void DecodePALIGNRMask(unsigned NumElts, unsigned Imm,
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000123 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperacaba3b2018-03-12 16:43:11 +0000124 const unsigned NumLaneElts = 16;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000125
126 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
127 for (unsigned i = 0; i != NumLaneElts; ++i) {
Craig Topperacaba3b2018-03-12 16:43:11 +0000128 unsigned Base = i + Imm;
129 // if i+imm is out of this lane then we actually need the other source
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000130 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
131 ShuffleMask.push_back(Base + l);
132 }
133 }
134}
135
Craig Topperacaba3b2018-03-12 16:43:11 +0000136void DecodeVALIGNMask(unsigned NumElts, unsigned Imm,
Craig Topperb084c902016-10-22 06:51:56 +0000137 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperb084c902016-10-22 06:51:56 +0000138 // Not all bits of the immediate are used so mask it.
139 assert(isPowerOf2_32(NumElts) && "NumElts should be power of 2");
140 Imm = Imm & (NumElts - 1);
Craig Topperacaba3b2018-03-12 16:43:11 +0000141 for (unsigned i = 0; i != NumElts; ++i)
Craig Topperb084c902016-10-22 06:51:56 +0000142 ShuffleMask.push_back(i + Imm);
143}
144
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000145/// DecodePSHUFMask - This decodes the shuffle masks for pshufw, pshufd, and vpermilp*.
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000146/// VT indicates the type of the vector allowing it to handle different
147/// datatypes and vector widths.
Craig Topperacaba3b2018-03-12 16:43:11 +0000148void DecodePSHUFMask(unsigned NumElts, unsigned ScalarBits, unsigned Imm,
149 SmallVectorImpl<int> &ShuffleMask) {
150 unsigned Size = NumElts * ScalarBits;
151 unsigned NumLanes = Size / 128;
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000152 if (NumLanes == 0) NumLanes = 1; // Handle MMX
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000153 unsigned NumLaneElts = NumElts / NumLanes;
154
155 unsigned NewImm = Imm;
156 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
157 for (unsigned i = 0; i != NumLaneElts; ++i) {
158 ShuffleMask.push_back(NewImm % NumLaneElts + l);
159 NewImm /= NumLaneElts;
160 }
161 if (NumLaneElts == 4) NewImm = Imm; // reload imm
162 }
163}
164
Craig Topperacaba3b2018-03-12 16:43:11 +0000165void DecodePSHUFHWMask(unsigned NumElts, unsigned Imm,
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000166 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000167 for (unsigned l = 0; l != NumElts; l += 8) {
168 unsigned NewImm = Imm;
169 for (unsigned i = 0, e = 4; i != e; ++i) {
170 ShuffleMask.push_back(l + i);
171 }
172 for (unsigned i = 4, e = 8; i != e; ++i) {
173 ShuffleMask.push_back(l + 4 + (NewImm & 3));
174 NewImm >>= 2;
175 }
176 }
177}
178
Craig Topperacaba3b2018-03-12 16:43:11 +0000179void DecodePSHUFLWMask(unsigned NumElts, unsigned Imm,
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000180 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000181 for (unsigned l = 0; l != NumElts; l += 8) {
182 unsigned NewImm = Imm;
183 for (unsigned i = 0, e = 4; i != e; ++i) {
184 ShuffleMask.push_back(l + (NewImm & 3));
185 NewImm >>= 2;
186 }
187 for (unsigned i = 4, e = 8; i != e; ++i) {
188 ShuffleMask.push_back(l + i);
189 }
190 }
191}
192
Craig Topperacaba3b2018-03-12 16:43:11 +0000193void DecodePSWAPMask(unsigned NumElts, SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000194 unsigned NumHalfElts = NumElts / 2;
195
196 for (unsigned l = 0; l != NumHalfElts; ++l)
197 ShuffleMask.push_back(l + NumHalfElts);
198 for (unsigned h = 0; h != NumHalfElts; ++h)
199 ShuffleMask.push_back(h);
200}
201
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000202/// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
203/// the type of the vector allowing it to handle different datatypes and vector
204/// widths.
Craig Topperacaba3b2018-03-12 16:43:11 +0000205void DecodeSHUFPMask(unsigned NumElts, unsigned ScalarBits,
206 unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
207 unsigned NumLaneElts = 128 / ScalarBits;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000208
209 unsigned NewImm = Imm;
210 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
211 // each half of a lane comes from different source
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000212 for (unsigned s = 0; s != NumElts * 2; s += NumElts) {
213 for (unsigned i = 0; i != NumLaneElts / 2; ++i) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000214 ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
215 NewImm /= NumLaneElts;
216 }
217 }
218 if (NumLaneElts == 4) NewImm = Imm; // reload imm
219 }
220}
221
222/// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
223/// and punpckh*. VT indicates the type of the vector allowing it to handle
224/// different datatypes and vector widths.
Craig Topperacaba3b2018-03-12 16:43:11 +0000225void DecodeUNPCKHMask(unsigned NumElts, unsigned ScalarBits,
226 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000227 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
228 // independently on 128-bit lanes.
Craig Topperacaba3b2018-03-12 16:43:11 +0000229 unsigned NumLanes = (NumElts * ScalarBits) / 128;
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000230 if (NumLanes == 0) NumLanes = 1; // Handle MMX
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000231 unsigned NumLaneElts = NumElts / NumLanes;
232
233 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000234 for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) {
235 ShuffleMask.push_back(i); // Reads from dest/src1
236 ShuffleMask.push_back(i + NumElts); // Reads from src/src2
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000237 }
238 }
239}
240
241/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
242/// and punpckl*. VT indicates the type of the vector allowing it to handle
243/// different datatypes and vector widths.
Craig Topperacaba3b2018-03-12 16:43:11 +0000244void DecodeUNPCKLMask(unsigned NumElts, unsigned ScalarBits,
245 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000246 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
247 // independently on 128-bit lanes.
Craig Topperacaba3b2018-03-12 16:43:11 +0000248 unsigned NumLanes = (NumElts * ScalarBits) / 128;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000249 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
250 unsigned NumLaneElts = NumElts / NumLanes;
251
252 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000253 for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) {
254 ShuffleMask.push_back(i); // Reads from dest/src1
255 ShuffleMask.push_back(i + NumElts); // Reads from src/src2
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000256 }
257 }
258}
259
Simon Pilgrimc941f6b2016-07-18 17:32:59 +0000260/// Decodes a broadcast of the first element of a vector.
Craig Topperacaba3b2018-03-12 16:43:11 +0000261void DecodeVectorBroadcast(unsigned NumElts,
262 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrimc941f6b2016-07-18 17:32:59 +0000263 ShuffleMask.append(NumElts, 0);
264}
265
Simon Pilgrima76a8e52016-07-14 12:07:43 +0000266/// Decodes a broadcast of a subvector to a larger vector type.
Craig Topperacaba3b2018-03-12 16:43:11 +0000267void DecodeSubVectorBroadcast(unsigned DstNumElts, unsigned SrcNumElts,
Simon Pilgrima76a8e52016-07-14 12:07:43 +0000268 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperacaba3b2018-03-12 16:43:11 +0000269 unsigned Scale = DstNumElts / SrcNumElts;
Simon Pilgrima76a8e52016-07-14 12:07:43 +0000270
271 for (unsigned i = 0; i != Scale; ++i)
Craig Topperacaba3b2018-03-12 16:43:11 +0000272 for (unsigned j = 0; j != SrcNumElts; ++j)
Simon Pilgrima76a8e52016-07-14 12:07:43 +0000273 ShuffleMask.push_back(j);
274}
275
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000276/// Decode a shuffle packed values at 128-bit granularity
Igor Bregerd7bae452015-10-15 13:29:07 +0000277/// (SHUFF32x4/SHUFF64x2/SHUFI32x4/SHUFI64x2)
278/// immediate mask into a shuffle mask.
Craig Topperacaba3b2018-03-12 16:43:11 +0000279void decodeVSHUF64x2FamilyMask(unsigned NumElts, unsigned ScalarSize,
280 unsigned Imm,
281 SmallVectorImpl<int> &ShuffleMask) {
282 unsigned NumElementsInLane = 128 / ScalarSize;
283 unsigned NumLanes = NumElts / NumElementsInLane;
Igor Bregerd7bae452015-10-15 13:29:07 +0000284 unsigned ControlBitsMask = NumLanes - 1;
285 unsigned NumControlBits = NumLanes / 2;
286
287 for (unsigned l = 0; l != NumLanes; ++l) {
288 unsigned LaneMask = (Imm >> (l * NumControlBits)) & ControlBitsMask;
289 // We actually need the other source.
290 if (l >= NumLanes / 2)
291 LaneMask += NumLanes;
292 for (unsigned i = 0; i != NumElementsInLane; ++i)
293 ShuffleMask.push_back(LaneMask * NumElementsInLane + i);
294 }
295}
296
Craig Topperacaba3b2018-03-12 16:43:11 +0000297void DecodeVPERM2X128Mask(unsigned NumElts, unsigned Imm,
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000298 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperacaba3b2018-03-12 16:43:11 +0000299 unsigned HalfSize = NumElts / 2;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000300
301 for (unsigned l = 0; l != 2; ++l) {
Simon Pilgrim40343e62015-07-06 22:46:46 +0000302 unsigned HalfMask = Imm >> (l * 4);
303 unsigned HalfBegin = (HalfMask & 0x3) * HalfSize;
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000304 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
Denis Protivenskyb6129022015-07-07 07:48:48 +0000305 ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000306 }
307}
308
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000309void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
310 SmallVectorImpl<int> &ShuffleMask) {
311 for (int i = 0, e = RawMask.size(); i < e; ++i) {
312 uint64_t M = RawMask[i];
313 if (M == (uint64_t)SM_SentinelUndef) {
314 ShuffleMask.push_back(M);
315 continue;
316 }
Simon Pilgrimf33cb612016-03-03 21:55:01 +0000317 // For 256/512-bit vectors the base of the shuffle is the 128-bit
318 // subvector we're inside.
319 int Base = (i / 16) * 16;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000320 // If the high bit (7) of the byte is set, the element is zeroed.
321 if (M & (1 << 7))
322 ShuffleMask.push_back(SM_SentinelZero);
323 else {
324 // Only the least significant 4 bits of the byte are used.
325 int Index = Base + (M & 0xf);
326 ShuffleMask.push_back(Index);
327 }
328 }
329}
330
Craig Topperacaba3b2018-03-12 16:43:11 +0000331void DecodeBLENDMask(unsigned NumElts, unsigned Imm,
332 SmallVectorImpl<int> &ShuffleMask) {
333 for (unsigned i = 0; i < NumElts; ++i) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000334 // If there are more than 8 elements in the vector, then any immediate blend
Craig Topperacaba3b2018-03-12 16:43:11 +0000335 // mask wraps around.
336 unsigned Bit = i % 8;
337 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElts + i : i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000338 }
339}
340
Simon Pilgrimfd4b9b02016-04-16 17:52:07 +0000341void DecodeVPPERMMask(ArrayRef<uint64_t> RawMask,
342 SmallVectorImpl<int> &ShuffleMask) {
343 assert(RawMask.size() == 16 && "Illegal VPPERM shuffle mask size");
344
345 // VPPERM Operation
346 // Bits[4:0] - Byte Index (0 - 31)
347 // Bits[7:5] - Permute Operation
348 //
349 // Permute Operation:
350 // 0 - Source byte (no logical operation).
351 // 1 - Invert source byte.
352 // 2 - Bit reverse of source byte.
353 // 3 - Bit reverse of inverted source byte.
354 // 4 - 00h (zero - fill).
355 // 5 - FFh (ones - fill).
356 // 6 - Most significant bit of source byte replicated in all bit positions.
357 // 7 - Invert most significant bit of source byte and replicate in all bit positions.
358 for (int i = 0, e = RawMask.size(); i < e; ++i) {
359 uint64_t M = RawMask[i];
360 if (M == (uint64_t)SM_SentinelUndef) {
361 ShuffleMask.push_back(M);
362 continue;
363 }
364
Simon Pilgrimf379a6c2016-04-24 15:05:04 +0000365 uint64_t PermuteOp = (M >> 5) & 0x7;
Simon Pilgrimfd4b9b02016-04-16 17:52:07 +0000366 if (PermuteOp == 4) {
367 ShuffleMask.push_back(SM_SentinelZero);
368 continue;
369 }
370 if (PermuteOp != 0) {
371 ShuffleMask.clear();
372 return;
373 }
374
375 uint64_t Index = M & 0x1F;
376 ShuffleMask.push_back((int)Index);
377 }
378}
379
Simon Pilgrima0d73832016-07-03 18:27:37 +0000380/// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
Craig Topperacaba3b2018-03-12 16:43:11 +0000381void DecodeVPERMMask(unsigned NumElts, unsigned Imm,
382 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrima0d73832016-07-03 18:27:37 +0000383 for (unsigned l = 0; l != NumElts; l += 4)
384 for (unsigned i = 0; i != 4; ++i)
385 ShuffleMask.push_back(l + ((Imm >> (2 * i)) & 3));
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000386}
387
Craig Topperacaba3b2018-03-12 16:43:11 +0000388void DecodeZeroExtendMask(unsigned SrcScalarBits, unsigned DstScalarBits,
389 unsigned NumDstElts, SmallVectorImpl<int> &Mask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000390 unsigned Scale = DstScalarBits / SrcScalarBits;
391 assert(SrcScalarBits < DstScalarBits &&
392 "Expected zero extension mask to increase scalar size");
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000393
394 for (unsigned i = 0; i != NumDstElts; i++) {
395 Mask.push_back(i);
396 for (unsigned j = 1; j != Scale; j++)
397 Mask.push_back(SM_SentinelZero);
398 }
399}
400
Craig Topperacaba3b2018-03-12 16:43:11 +0000401void DecodeZeroMoveLowMask(unsigned NumElts,
402 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000403 ShuffleMask.push_back(0);
404 for (unsigned i = 1; i < NumElts; i++)
405 ShuffleMask.push_back(SM_SentinelZero);
406}
407
Craig Topperacaba3b2018-03-12 16:43:11 +0000408void DecodeScalarMoveMask(unsigned NumElts, bool IsLoad,
409 SmallVectorImpl<int> &Mask) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000410 // First element comes from the first element of second source.
411 // Remaining elements: Load zero extends / Move copies from first source.
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000412 Mask.push_back(NumElts);
413 for (unsigned i = 1; i < NumElts; i++)
414 Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i);
415}
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000416
Craig Topperacaba3b2018-03-12 16:43:11 +0000417void DecodeEXTRQIMask(unsigned NumElts, unsigned EltSize, int Len, int Idx,
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000418 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000419 unsigned HalfElts = NumElts / 2;
420
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000421 // Only the bottom 6 bits are valid for each immediate.
422 Len &= 0x3F;
423 Idx &= 0x3F;
424
425 // We can only decode this bit extraction instruction as a shuffle if both the
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000426 // length and index work with whole elements.
427 if (0 != (Len % EltSize) || 0 != (Idx % EltSize))
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000428 return;
429
430 // A length of zero is equivalent to a bit length of 64.
431 if (Len == 0)
432 Len = 64;
433
434 // If the length + index exceeds the bottom 64 bits the result is undefined.
435 if ((Len + Idx) > 64) {
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000436 ShuffleMask.append(NumElts, SM_SentinelUndef);
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000437 return;
438 }
439
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000440 // Convert index and index to work with elements.
441 Len /= EltSize;
442 Idx /= EltSize;
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000443
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000444 // EXTRQ: Extract Len elements starting from Idx. Zero pad the remaining
445 // elements of the lower 64-bits. The upper 64-bits are undefined.
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000446 for (int i = 0; i != Len; ++i)
447 ShuffleMask.push_back(i + Idx);
Simon Pilgrimf809c5f2017-07-04 17:42:01 +0000448 for (int i = Len; i != (int)HalfElts; ++i)
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000449 ShuffleMask.push_back(SM_SentinelZero);
Simon Pilgrimf809c5f2017-07-04 17:42:01 +0000450 for (int i = HalfElts; i != (int)NumElts; ++i)
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000451 ShuffleMask.push_back(SM_SentinelUndef);
452}
453
Craig Topperacaba3b2018-03-12 16:43:11 +0000454void DecodeINSERTQIMask(unsigned NumElts, unsigned EltSize, int Len, int Idx,
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000455 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000456 unsigned HalfElts = NumElts / 2;
457
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000458 // Only the bottom 6 bits are valid for each immediate.
459 Len &= 0x3F;
460 Idx &= 0x3F;
461
462 // We can only decode this bit insertion instruction as a shuffle if both the
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000463 // length and index work with whole elements.
464 if (0 != (Len % EltSize) || 0 != (Idx % EltSize))
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000465 return;
466
467 // A length of zero is equivalent to a bit length of 64.
468 if (Len == 0)
469 Len = 64;
470
471 // If the length + index exceeds the bottom 64 bits the result is undefined.
472 if ((Len + Idx) > 64) {
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000473 ShuffleMask.append(NumElts, SM_SentinelUndef);
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000474 return;
475 }
476
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000477 // Convert index and index to work with elements.
478 Len /= EltSize;
479 Idx /= EltSize;
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000480
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000481 // INSERTQ: Extract lowest Len elements from lower half of second source and
482 // insert over first source starting at Idx element. The upper 64-bits are
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000483 // undefined.
484 for (int i = 0; i != Idx; ++i)
485 ShuffleMask.push_back(i);
486 for (int i = 0; i != Len; ++i)
Simon Pilgrim9f0a0bd2017-07-04 16:53:12 +0000487 ShuffleMask.push_back(i + NumElts);
Simon Pilgrimf809c5f2017-07-04 17:42:01 +0000488 for (int i = Idx + Len; i != (int)HalfElts; ++i)
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000489 ShuffleMask.push_back(i);
Simon Pilgrimf809c5f2017-07-04 17:42:01 +0000490 for (int i = HalfElts; i != (int)NumElts; ++i)
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000491 ShuffleMask.push_back(SM_SentinelUndef);
492}
493
Craig Topperacaba3b2018-03-12 16:43:11 +0000494void DecodeVPERMILPMask(unsigned NumElts, unsigned ScalarBits,
495 ArrayRef<uint64_t> RawMask,
Simon Pilgrim40e1a712016-03-05 22:53:31 +0000496 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperacaba3b2018-03-12 16:43:11 +0000497 unsigned VecSize = NumElts * ScalarBits;
Simon Pilgrim40e1a712016-03-05 22:53:31 +0000498 unsigned NumLanes = VecSize / 128;
Craig Topperacaba3b2018-03-12 16:43:11 +0000499 unsigned NumEltsPerLane = NumElts / NumLanes;
Simon Pilgrim40e1a712016-03-05 22:53:31 +0000500 assert((VecSize == 128 || VecSize == 256 || VecSize == 512) &&
501 "Unexpected vector size");
Craig Topperacaba3b2018-03-12 16:43:11 +0000502 assert((ScalarBits == 32 || ScalarBits == 64) && "Unexpected element size");
Simon Pilgrim40e1a712016-03-05 22:53:31 +0000503
504 for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {
505 uint64_t M = RawMask[i];
Craig Topperacaba3b2018-03-12 16:43:11 +0000506 M = (ScalarBits == 64 ? ((M >> 1) & 0x1) : (M & 0x3));
Simon Pilgrim40e1a712016-03-05 22:53:31 +0000507 unsigned LaneOffset = i & ~(NumEltsPerLane - 1);
508 ShuffleMask.push_back((int)(LaneOffset + M));
509 }
510}
511
Craig Topperacaba3b2018-03-12 16:43:11 +0000512void DecodeVPERMIL2PMask(unsigned NumElts, unsigned ScalarBits, unsigned M2Z,
513 ArrayRef<uint64_t> RawMask,
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000514 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperacaba3b2018-03-12 16:43:11 +0000515 unsigned VecSize = NumElts * ScalarBits;
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000516 unsigned NumLanes = VecSize / 128;
Simon Pilgrimd5bc5c12016-12-07 11:19:00 +0000517 unsigned NumEltsPerLane = NumElts / NumLanes;
518 assert((VecSize == 128 || VecSize == 256) && "Unexpected vector size");
Craig Topperacaba3b2018-03-12 16:43:11 +0000519 assert((ScalarBits == 32 || ScalarBits == 64) && "Unexpected element size");
Simon Pilgrimd5bc5c12016-12-07 11:19:00 +0000520 assert((NumElts == RawMask.size()) && "Unexpected mask size");
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000521
522 for (unsigned i = 0, e = RawMask.size(); i < e; ++i) {
523 // VPERMIL2 Operation.
524 // Bits[3] - Match Bit.
525 // Bits[2:1] - (Per Lane) PD Shuffle Mask.
526 // Bits[2:0] - (Per Lane) PS Shuffle Mask.
527 uint64_t Selector = RawMask[i];
Filipe Cabecinhas6e7d5462016-06-06 10:49:56 +0000528 unsigned MatchBit = (Selector >> 3) & 0x1;
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000529
530 // M2Z[0:1] MatchBit
531 // 0Xb X Source selected by Selector index.
532 // 10b 0 Source selected by Selector index.
533 // 10b 1 Zero.
534 // 11b 0 Zero.
535 // 11b 1 Source selected by Selector index.
536 if ((M2Z & 0x2) != 0 && MatchBit != (M2Z & 0x1)) {
537 ShuffleMask.push_back(SM_SentinelZero);
538 continue;
539 }
540
Simon Pilgrimd5bc5c12016-12-07 11:19:00 +0000541 int Index = i & ~(NumEltsPerLane - 1);
Craig Topperacaba3b2018-03-12 16:43:11 +0000542 if (ScalarBits == 64)
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000543 Index += (Selector >> 1) & 0x1;
544 else
545 Index += Selector & 0x3;
546
Simon Pilgrimd5bc5c12016-12-07 11:19:00 +0000547 int Src = (Selector >> 2) & 0x1;
548 Index += Src * NumElts;
549 ShuffleMask.push_back(Index);
Simon Pilgrim64c6de42016-06-05 15:21:30 +0000550 }
551}
552
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000553void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask,
554 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrim48adedf2016-07-05 18:31:17 +0000555 uint64_t EltMaskSize = RawMask.size() - 1;
556 for (auto M : RawMask) {
557 M &= EltMaskSize;
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000558 ShuffleMask.push_back((int)M);
559 }
560}
561
562void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask,
563 SmallVectorImpl<int> &ShuffleMask) {
Simon Pilgrim253ca342016-03-06 21:54:52 +0000564 uint64_t EltMaskSize = (RawMask.size() * 2) - 1;
565 for (auto M : RawMask) {
566 M &= EltMaskSize;
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000567 ShuffleMask.push_back((int)M);
568 }
569}
570
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000571} // llvm namespace