blob: e66c0ff11ebb4807d1c06866315ab1ba9181b588 [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"
16#include "llvm/IR/Constants.h"
17#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
48// <3,1> or <6,7,2,3>
49void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000050 for (unsigned i = NElts / 2; i != NElts; ++i)
51 ShuffleMask.push_back(NElts + i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000052
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000053 for (unsigned i = NElts / 2; i != NElts; ++i)
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000054 ShuffleMask.push_back(i);
55}
56
57// <0,2> or <0,1,4,5>
58void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000059 for (unsigned i = 0; i != NElts / 2; ++i)
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000060 ShuffleMask.push_back(i);
61
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +000062 for (unsigned i = 0; i != NElts / 2; ++i)
63 ShuffleMask.push_back(NElts + i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +000064}
65
66void DecodeMOVSLDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
67 unsigned NumElts = VT.getVectorNumElements();
68 for (int i = 0, e = NumElts / 2; i < e; ++i) {
69 ShuffleMask.push_back(2 * i);
70 ShuffleMask.push_back(2 * i);
71 }
72}
73
74void DecodeMOVSHDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
75 unsigned NumElts = VT.getVectorNumElements();
76 for (int i = 0, e = NumElts / 2; i < e; ++i) {
77 ShuffleMask.push_back(2 * i + 1);
78 ShuffleMask.push_back(2 * i + 1);
79 }
80}
81
82void DecodeMOVDDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
83 unsigned VectorSizeInBits = VT.getSizeInBits();
84 unsigned ScalarSizeInBits = VT.getScalarSizeInBits();
85 unsigned NumElts = VT.getVectorNumElements();
86 unsigned NumLanes = VectorSizeInBits / 128;
87 unsigned NumLaneElts = NumElts / NumLanes;
88 unsigned NumLaneSubElts = 64 / ScalarSizeInBits;
89
90 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
91 for (unsigned i = 0; i < NumLaneElts; i += NumLaneSubElts)
92 for (unsigned s = 0; s != NumLaneSubElts; s++)
93 ShuffleMask.push_back(l + s);
94}
95
96void DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
97 unsigned VectorSizeInBits = VT.getSizeInBits();
98 unsigned NumElts = VectorSizeInBits / 8;
99 unsigned NumLanes = VectorSizeInBits / 128;
100 unsigned NumLaneElts = NumElts / NumLanes;
101
102 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
103 for (unsigned i = 0; i < NumLaneElts; ++i) {
104 int M = SM_SentinelZero;
105 if (i >= Imm) M = i - Imm + l;
106 ShuffleMask.push_back(M);
107 }
108}
109
110void DecodePSRLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
111 unsigned VectorSizeInBits = VT.getSizeInBits();
112 unsigned NumElts = VectorSizeInBits / 8;
113 unsigned NumLanes = VectorSizeInBits / 128;
114 unsigned NumLaneElts = NumElts / NumLanes;
115
116 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
117 for (unsigned i = 0; i < NumLaneElts; ++i) {
118 unsigned Base = i + Imm;
119 int M = Base + l;
120 if (Base >= NumLaneElts) M = SM_SentinelZero;
121 ShuffleMask.push_back(M);
122 }
123}
124
125void DecodePALIGNRMask(MVT VT, unsigned Imm,
126 SmallVectorImpl<int> &ShuffleMask) {
127 unsigned NumElts = VT.getVectorNumElements();
128 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
129
130 unsigned NumLanes = VT.getSizeInBits() / 128;
131 unsigned NumLaneElts = NumElts / NumLanes;
132
133 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
134 for (unsigned i = 0; i != NumLaneElts; ++i) {
135 unsigned Base = i + Offset;
136 // if i+offset is out of this lane then we actually need the other source
137 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
138 ShuffleMask.push_back(Base + l);
139 }
140 }
141}
142
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000143/// DecodePSHUFMask - This decodes the shuffle masks for pshufw, pshufd, and vpermilp*.
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000144/// VT indicates the type of the vector allowing it to handle different
145/// datatypes and vector widths.
146void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
147 unsigned NumElts = VT.getVectorNumElements();
148
149 unsigned NumLanes = VT.getSizeInBits() / 128;
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000150 if (NumLanes == 0) NumLanes = 1; // Handle MMX
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000151 unsigned NumLaneElts = NumElts / NumLanes;
152
153 unsigned NewImm = Imm;
154 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
155 for (unsigned i = 0; i != NumLaneElts; ++i) {
156 ShuffleMask.push_back(NewImm % NumLaneElts + l);
157 NewImm /= NumLaneElts;
158 }
159 if (NumLaneElts == 4) NewImm = Imm; // reload imm
160 }
161}
162
163void DecodePSHUFHWMask(MVT VT, unsigned Imm,
164 SmallVectorImpl<int> &ShuffleMask) {
165 unsigned NumElts = VT.getVectorNumElements();
166
167 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
179void DecodePSHUFLWMask(MVT VT, unsigned Imm,
180 SmallVectorImpl<int> &ShuffleMask) {
181 unsigned NumElts = VT.getVectorNumElements();
182
183 for (unsigned l = 0; l != NumElts; l += 8) {
184 unsigned NewImm = Imm;
185 for (unsigned i = 0, e = 4; i != e; ++i) {
186 ShuffleMask.push_back(l + (NewImm & 3));
187 NewImm >>= 2;
188 }
189 for (unsigned i = 4, e = 8; i != e; ++i) {
190 ShuffleMask.push_back(l + i);
191 }
192 }
193}
194
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000195void DecodePSWAPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
196 unsigned NumElts = VT.getVectorNumElements();
197 unsigned NumHalfElts = NumElts / 2;
198
199 for (unsigned l = 0; l != NumHalfElts; ++l)
200 ShuffleMask.push_back(l + NumHalfElts);
201 for (unsigned h = 0; h != NumHalfElts; ++h)
202 ShuffleMask.push_back(h);
203}
204
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000205/// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
206/// the type of the vector allowing it to handle different datatypes and vector
207/// widths.
208void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
209 unsigned NumElts = VT.getVectorNumElements();
210
211 unsigned NumLanes = VT.getSizeInBits() / 128;
212 unsigned NumLaneElts = NumElts / NumLanes;
213
214 unsigned NewImm = Imm;
215 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
216 // each half of a lane comes from different source
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000217 for (unsigned s = 0; s != NumElts * 2; s += NumElts) {
218 for (unsigned i = 0; i != NumLaneElts / 2; ++i) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000219 ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
220 NewImm /= NumLaneElts;
221 }
222 }
223 if (NumLaneElts == 4) NewImm = Imm; // reload imm
224 }
225}
226
227/// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
228/// and punpckh*. VT indicates the type of the vector allowing it to handle
229/// different datatypes and vector widths.
230void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
231 unsigned NumElts = VT.getVectorNumElements();
232
233 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
234 // independently on 128-bit lanes.
235 unsigned NumLanes = VT.getSizeInBits() / 128;
Simon Pilgrimf8f86ab2015-09-13 11:28:45 +0000236 if (NumLanes == 0) NumLanes = 1; // Handle MMX
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000237 unsigned NumLaneElts = NumElts / NumLanes;
238
239 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000240 for (unsigned i = l + NumLaneElts / 2, e = l + NumLaneElts; i != e; ++i) {
241 ShuffleMask.push_back(i); // Reads from dest/src1
242 ShuffleMask.push_back(i + NumElts); // Reads from src/src2
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000243 }
244 }
245}
246
247/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
248/// and punpckl*. VT indicates the type of the vector allowing it to handle
249/// different datatypes and vector widths.
250void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
251 unsigned NumElts = VT.getVectorNumElements();
252
253 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
254 // independently on 128-bit lanes.
255 unsigned NumLanes = VT.getSizeInBits() / 128;
256 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
257 unsigned NumLaneElts = NumElts / NumLanes;
258
259 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000260 for (unsigned i = l, e = l + NumLaneElts / 2; i != e; ++i) {
261 ShuffleMask.push_back(i); // Reads from dest/src1
262 ShuffleMask.push_back(i + NumElts); // Reads from src/src2
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000263 }
264 }
265}
266
267void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
268 SmallVectorImpl<int> &ShuffleMask) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000269 unsigned HalfSize = VT.getVectorNumElements() / 2;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000270
271 for (unsigned l = 0; l != 2; ++l) {
Simon Pilgrim40343e62015-07-06 22:46:46 +0000272 unsigned HalfMask = Imm >> (l * 4);
273 unsigned HalfBegin = (HalfMask & 0x3) * HalfSize;
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000274 for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
Denis Protivenskyb6129022015-07-07 07:48:48 +0000275 ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000276 }
277}
278
279void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
280 Type *MaskTy = C->getType();
281 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
282 // constant pool uniques constants by their bit representation.
283 // e.g. the following take up the same space in the constant pool:
284 // i128 -170141183420855150465331762880109871104
285 //
286 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
287 //
288 // <4 x i32> <i32 -2147483648, i32 -2147483648,
289 // i32 -2147483648, i32 -2147483648>
290
291 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
292
293 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
294 return;
295
296 // This is a straightforward byte vector.
297 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) {
298 int NumElements = MaskTy->getVectorNumElements();
299 ShuffleMask.reserve(NumElements);
300
301 for (int i = 0; i < NumElements; ++i) {
302 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
303 // lane of the vector we're inside.
304 int Base = i < 16 ? 0 : 16;
305 Constant *COp = C->getAggregateElement(i);
306 if (!COp) {
307 ShuffleMask.clear();
308 return;
309 } else if (isa<UndefValue>(COp)) {
310 ShuffleMask.push_back(SM_SentinelUndef);
311 continue;
312 }
313 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
314 // If the high bit (7) of the byte is set, the element is zeroed.
315 if (Element & (1 << 7))
316 ShuffleMask.push_back(SM_SentinelZero);
317 else {
318 // Only the least significant 4 bits of the byte are used.
319 int Index = Base + (Element & 0xf);
320 ShuffleMask.push_back(Index);
321 }
322 }
323 }
324 // TODO: Handle funny-looking vectors too.
325}
326
327void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
328 SmallVectorImpl<int> &ShuffleMask) {
329 for (int i = 0, e = RawMask.size(); i < e; ++i) {
330 uint64_t M = RawMask[i];
331 if (M == (uint64_t)SM_SentinelUndef) {
332 ShuffleMask.push_back(M);
333 continue;
334 }
335 // For AVX vectors with 32 bytes the base of the shuffle is the half of
336 // the vector we're inside.
337 int Base = i < 16 ? 0 : 16;
338 // If the high bit (7) of the byte is set, the element is zeroed.
339 if (M & (1 << 7))
340 ShuffleMask.push_back(SM_SentinelZero);
341 else {
342 // Only the least significant 4 bits of the byte are used.
343 int Index = Base + (M & 0xf);
344 ShuffleMask.push_back(Index);
345 }
346 }
347}
348
349void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
350 int ElementBits = VT.getScalarSizeInBits();
351 int NumElements = VT.getVectorNumElements();
352 for (int i = 0; i < NumElements; ++i) {
353 // If there are more than 8 elements in the vector, then any immediate blend
354 // mask applies to each 128-bit lane. There can never be more than
355 // 8 elements in a 128-bit lane with an immediate blend.
356 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i;
357 assert(Bit < 8 &&
358 "Immediate blends only operate over 8 elements at a time!");
359 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
360 }
361}
362
363/// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
364/// No VT provided since it only works on 256-bit, 4 element vectors.
365void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
366 for (unsigned i = 0; i != 4; ++i) {
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +0000367 ShuffleMask.push_back((Imm >> (2 * i)) & 3);
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000368 }
369}
370
371void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
372 Type *MaskTy = C->getType();
373 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
374 assert(MaskTy->getVectorElementType()->isIntegerTy() &&
375 "Expected integer constant mask elements!");
376 int ElementBits = MaskTy->getScalarSizeInBits();
377 int NumElements = MaskTy->getVectorNumElements();
378 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
379 "Unexpected number of vector elements.");
380 ShuffleMask.reserve(NumElements);
381 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
382 assert((unsigned)NumElements == CDS->getNumElements() &&
383 "Constant mask has a different number of elements!");
384
385 for (int i = 0; i < NumElements; ++i) {
386 int Base = (i * ElementBits / 128) * (128 / ElementBits);
387 uint64_t Element = CDS->getElementAsInteger(i);
388 // Only the least significant 2 bits of the integer are used.
389 int Index = Base + (Element & 0x3);
390 ShuffleMask.push_back(Index);
391 }
392 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
393 assert((unsigned)NumElements == C->getNumOperands() &&
394 "Constant mask has a different number of elements!");
395
396 for (int i = 0; i < NumElements; ++i) {
397 int Base = (i * ElementBits / 128) * (128 / ElementBits);
398 Constant *COp = CV->getOperand(i);
399 if (isa<UndefValue>(COp)) {
400 ShuffleMask.push_back(SM_SentinelUndef);
401 continue;
402 }
403 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
404 // Only the least significant 2 bits of the integer are used.
405 int Index = Base + (Element & 0x3);
406 ShuffleMask.push_back(Index);
407 }
408 }
409}
410
411void DecodeZeroExtendMask(MVT SrcVT, MVT DstVT, SmallVectorImpl<int> &Mask) {
412 unsigned NumDstElts = DstVT.getVectorNumElements();
413 unsigned SrcScalarBits = SrcVT.getScalarSizeInBits();
414 unsigned DstScalarBits = DstVT.getScalarSizeInBits();
415 unsigned Scale = DstScalarBits / SrcScalarBits;
416 assert(SrcScalarBits < DstScalarBits &&
417 "Expected zero extension mask to increase scalar size");
418 assert(SrcVT.getVectorNumElements() >= NumDstElts &&
419 "Too many zero extension lanes");
420
421 for (unsigned i = 0; i != NumDstElts; i++) {
422 Mask.push_back(i);
423 for (unsigned j = 1; j != Scale; j++)
424 Mask.push_back(SM_SentinelZero);
425 }
426}
427
428void DecodeZeroMoveLowMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
429 unsigned NumElts = VT.getVectorNumElements();
430 ShuffleMask.push_back(0);
431 for (unsigned i = 1; i < NumElts; i++)
432 ShuffleMask.push_back(SM_SentinelZero);
433}
434
435void DecodeScalarMoveMask(MVT VT, bool IsLoad, SmallVectorImpl<int> &Mask) {
436 // First element comes from the first element of second source.
437 // Remaining elements: Load zero extends / Move copies from first source.
438 unsigned NumElts = VT.getVectorNumElements();
439 Mask.push_back(NumElts);
440 for (unsigned i = 1; i < NumElts; i++)
441 Mask.push_back(IsLoad ? static_cast<int>(SM_SentinelZero) : i);
442}
Simon Pilgrimd85cae32015-07-06 20:46:41 +0000443
444void DecodeEXTRQIMask(int Len, int Idx,
445 SmallVectorImpl<int> &ShuffleMask) {
446 // Only the bottom 6 bits are valid for each immediate.
447 Len &= 0x3F;
448 Idx &= 0x3F;
449
450 // We can only decode this bit extraction instruction as a shuffle if both the
451 // length and index work with whole bytes.
452 if (0 != (Len % 8) || 0 != (Idx % 8))
453 return;
454
455 // A length of zero is equivalent to a bit length of 64.
456 if (Len == 0)
457 Len = 64;
458
459 // If the length + index exceeds the bottom 64 bits the result is undefined.
460 if ((Len + Idx) > 64) {
461 ShuffleMask.append(16, SM_SentinelUndef);
462 return;
463 }
464
465 // Convert index and index to work with bytes.
466 Len /= 8;
467 Idx /= 8;
468
469 // EXTRQ: Extract Len bytes starting from Idx. Zero pad the remaining bytes
470 // of the lower 64-bits. The upper 64-bits are undefined.
471 for (int i = 0; i != Len; ++i)
472 ShuffleMask.push_back(i + Idx);
473 for (int i = Len; i != 8; ++i)
474 ShuffleMask.push_back(SM_SentinelZero);
475 for (int i = 8; i != 16; ++i)
476 ShuffleMask.push_back(SM_SentinelUndef);
477}
478
479void DecodeINSERTQIMask(int Len, int Idx,
480 SmallVectorImpl<int> &ShuffleMask) {
481 // Only the bottom 6 bits are valid for each immediate.
482 Len &= 0x3F;
483 Idx &= 0x3F;
484
485 // We can only decode this bit insertion instruction as a shuffle if both the
486 // length and index work with whole bytes.
487 if (0 != (Len % 8) || 0 != (Idx % 8))
488 return;
489
490 // A length of zero is equivalent to a bit length of 64.
491 if (Len == 0)
492 Len = 64;
493
494 // If the length + index exceeds the bottom 64 bits the result is undefined.
495 if ((Len + Idx) > 64) {
496 ShuffleMask.append(16, SM_SentinelUndef);
497 return;
498 }
499
500 // Convert index and index to work with bytes.
501 Len /= 8;
502 Idx /= 8;
503
504 // INSERTQ: Extract lowest Len bytes from lower half of second source and
505 // insert over first source starting at Idx byte. The upper 64-bits are
506 // undefined.
507 for (int i = 0; i != Idx; ++i)
508 ShuffleMask.push_back(i);
509 for (int i = 0; i != Len; ++i)
510 ShuffleMask.push_back(i + 16);
511 for (int i = Idx + Len; i != 8; ++i)
512 ShuffleMask.push_back(i);
513 for (int i = 8; i != 16; ++i)
514 ShuffleMask.push_back(SM_SentinelUndef);
515}
516
Elena Demikhovskye88038f2015-09-08 06:38:21 +0000517void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask,
518 SmallVectorImpl<int> &ShuffleMask) {
519 for (int i = 0, e = RawMask.size(); i < e; ++i) {
520 uint64_t M = RawMask[i];
521 ShuffleMask.push_back((int)M);
522 }
523}
524
525void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask,
526 SmallVectorImpl<int> &ShuffleMask) {
527 for (int i = 0, e = RawMask.size(); i < e; ++i) {
528 uint64_t M = RawMask[i];
529 ShuffleMask.push_back((int)M);
530 }
531}
532
533void DecodeVPERMVMask(const Constant *C, MVT VT,
534 SmallVectorImpl<int> &ShuffleMask) {
535 Type *MaskTy = C->getType();
536 if (MaskTy->isVectorTy()) {
537 unsigned NumElements = MaskTy->getVectorNumElements();
538 if (NumElements == VT.getVectorNumElements()) {
539 for (unsigned i = 0; i < NumElements; ++i) {
540 Constant *COp = C->getAggregateElement(i);
541 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) {
542 ShuffleMask.clear();
543 return;
544 }
545 if (isa<UndefValue>(COp))
546 ShuffleMask.push_back(SM_SentinelUndef);
547 else {
548 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
549 Element &= (1 << NumElements) - 1;
550 ShuffleMask.push_back(Element);
551 }
552 }
553 }
554 return;
555 }
556 // Scalar value; just broadcast it
557 if (!isa<ConstantInt>(C))
558 return;
559 uint64_t Element = cast<ConstantInt>(C)->getZExtValue();
560 int NumElements = VT.getVectorNumElements();
561 Element &= (1 << NumElements) - 1;
562 for (int i = 0; i < NumElements; ++i)
563 ShuffleMask.push_back(Element);
564}
565
566void DecodeVPERMV3Mask(const Constant *C, MVT VT,
567 SmallVectorImpl<int> &ShuffleMask) {
568 Type *MaskTy = C->getType();
569 unsigned NumElements = MaskTy->getVectorNumElements();
570 if (NumElements == VT.getVectorNumElements()) {
571 for (unsigned i = 0; i < NumElements; ++i) {
572 Constant *COp = C->getAggregateElement(i);
573 if (!COp) {
574 ShuffleMask.clear();
575 return;
576 }
577 if (isa<UndefValue>(COp))
578 ShuffleMask.push_back(SM_SentinelUndef);
579 else {
580 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
581 Element &= (1 << NumElements*2) - 1;
582 ShuffleMask.push_back(Element);
583 }
584 }
585 }
586}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000587} // llvm namespace