• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- 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/ADT/ArrayRef.h"
17 #include "llvm/CodeGen/MachineValueType.h"
18 
19 //===----------------------------------------------------------------------===//
20 //  Vector Mask Decoding
21 //===----------------------------------------------------------------------===//
22 
23 namespace llvm {
24 
DecodeINSERTPSMask(unsigned Imm,SmallVectorImpl<int> & ShuffleMask)25 void 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.
38   unsigned InVal = 4 + CountS;
39   // 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 
DecodeInsertElementMask(MVT VT,unsigned Idx,unsigned Len,SmallVectorImpl<int> & ShuffleMask)48 void 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 
59 // <3,1> or <6,7,2,3>
DecodeMOVHLPSMask(unsigned NElts,SmallVectorImpl<int> & ShuffleMask)60 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
61   for (unsigned i = NElts / 2; i != NElts; ++i)
62     ShuffleMask.push_back(NElts + i);
63 
64   for (unsigned i = NElts / 2; i != NElts; ++i)
65     ShuffleMask.push_back(i);
66 }
67 
68 // <0,2> or <0,1,4,5>
DecodeMOVLHPSMask(unsigned NElts,SmallVectorImpl<int> & ShuffleMask)69 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
70   for (unsigned i = 0; i != NElts / 2; ++i)
71     ShuffleMask.push_back(i);
72 
73   for (unsigned i = 0; i != NElts / 2; ++i)
74     ShuffleMask.push_back(NElts + i);
75 }
76 
DecodeMOVSLDUPMask(MVT VT,SmallVectorImpl<int> & ShuffleMask)77 void 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 
DecodeMOVSHDUPMask(MVT VT,SmallVectorImpl<int> & ShuffleMask)85 void 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 
DecodeMOVDDUPMask(MVT VT,SmallVectorImpl<int> & ShuffleMask)93 void 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 
DecodePSLLDQMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)107 void 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 
DecodePSRLDQMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)121 void 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 
DecodePALIGNRMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)136 void 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 
154 /// DecodePSHUFMask - This decodes the shuffle masks for pshufw, pshufd, and vpermilp*.
155 /// VT indicates the type of the vector allowing it to handle different
156 /// datatypes and vector widths.
DecodePSHUFMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)157 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
158   unsigned NumElts = VT.getVectorNumElements();
159 
160   unsigned NumLanes = VT.getSizeInBits() / 128;
161   if (NumLanes == 0) NumLanes = 1;  // Handle MMX
162   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 
DecodePSHUFHWMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)174 void 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 
DecodePSHUFLWMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)190 void 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 
DecodePSWAPMask(MVT VT,SmallVectorImpl<int> & ShuffleMask)206 void 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 
216 /// 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.
DecodeSHUFPMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)219 void 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
228     for (unsigned s = 0; s != NumElts * 2; s += NumElts) {
229       for (unsigned i = 0; i != NumLaneElts / 2; ++i) {
230         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.
DecodeUNPCKHMask(MVT VT,SmallVectorImpl<int> & ShuffleMask)241 void 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;
247   if (NumLanes == 0) NumLanes = 1;  // Handle MMX
248   unsigned NumLaneElts = NumElts / NumLanes;
249 
250   for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
251     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
254     }
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.
DecodeUNPCKLMask(MVT VT,SmallVectorImpl<int> & ShuffleMask)261 void 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) {
271     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
274     }
275   }
276 }
277 
278 /// Decodes a broadcast of a subvector to a larger vector type.
DecodeSubVectorBroadcast(MVT DstVT,MVT SrcVT,SmallVectorImpl<int> & ShuffleMask)279 void 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 
291 /// \brief Decode a shuffle packed values at 128-bit granularity
292 /// (SHUFF32x4/SHUFF64x2/SHUFI32x4/SHUFI64x2)
293 /// immediate mask into a shuffle mask.
decodeVSHUF64x2FamilyMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)294 void 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 
DecodeVPERM2X128Mask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)311 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
312                           SmallVectorImpl<int> &ShuffleMask) {
313   unsigned HalfSize = VT.getVectorNumElements() / 2;
314 
315   for (unsigned l = 0; l != 2; ++l) {
316     unsigned HalfMask = Imm >> (l * 4);
317     unsigned HalfBegin = (HalfMask & 0x3) * HalfSize;
318     for (unsigned i = HalfBegin, e = HalfBegin + HalfSize; i != e; ++i)
319       ShuffleMask.push_back(HalfMask & 8 ? SM_SentinelZero : (int)i);
320   }
321 }
322 
DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,SmallVectorImpl<int> & ShuffleMask)323 void 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     }
331     // 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;
334     // 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 
DecodeBLENDMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)345 void 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 
DecodeVPPERMMask(ArrayRef<uint64_t> RawMask,SmallVectorImpl<int> & ShuffleMask)359 void 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 
383     uint64_t PermuteOp = (M >> 5) & 0x7;
384     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 
398 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
DecodeVPERMMask(MVT VT,unsigned Imm,SmallVectorImpl<int> & ShuffleMask)399 void 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));
406 }
407 
DecodeZeroExtendMask(MVT SrcScalarVT,MVT DstVT,SmallVectorImpl<int> & Mask)408 void DecodeZeroExtendMask(MVT SrcScalarVT, MVT DstVT, SmallVectorImpl<int> &Mask) {
409   unsigned NumDstElts = DstVT.getVectorNumElements();
410   unsigned SrcScalarBits = SrcScalarVT.getSizeInBits();
411   unsigned DstScalarBits = DstVT.getScalarSizeInBits();
412   unsigned Scale = DstScalarBits / SrcScalarBits;
413   assert(SrcScalarBits < DstScalarBits &&
414          "Expected zero extension mask to increase scalar size");
415 
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 
DecodeZeroMoveLowMask(MVT VT,SmallVectorImpl<int> & ShuffleMask)423 void 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 
DecodeScalarMoveMask(MVT VT,bool IsLoad,SmallVectorImpl<int> & Mask)430 void 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 }
438 
DecodeEXTRQIMask(int Len,int Idx,SmallVectorImpl<int> & ShuffleMask)439 void 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 
DecodeINSERTQIMask(int Len,int Idx,SmallVectorImpl<int> & ShuffleMask)474 void 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 
DecodeVPERMILPMask(MVT VT,ArrayRef<uint64_t> RawMask,SmallVectorImpl<int> & ShuffleMask)512 void 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 
DecodeVPERMIL2PMask(MVT VT,unsigned M2Z,ArrayRef<uint64_t> RawMask,SmallVectorImpl<int> & ShuffleMask)530 void 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];
546     unsigned MatchBit = (Selector >> 3) & 0x1;
547 
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 
DecodeVPERMVMask(ArrayRef<uint64_t> RawMask,SmallVectorImpl<int> & ShuffleMask)570 void DecodeVPERMVMask(ArrayRef<uint64_t> RawMask,
571                       SmallVectorImpl<int> &ShuffleMask) {
572   uint64_t EltMaskSize = RawMask.size() - 1;
573   for (auto M : RawMask) {
574     M &= EltMaskSize;
575     ShuffleMask.push_back((int)M);
576   }
577 }
578 
DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask,SmallVectorImpl<int> & ShuffleMask)579 void DecodeVPERMV3Mask(ArrayRef<uint64_t> RawMask,
580                       SmallVectorImpl<int> &ShuffleMask) {
581   uint64_t EltMaskSize = (RawMask.size() * 2) - 1;
582   for (auto M : RawMask) {
583     M &= EltMaskSize;
584     ShuffleMask.push_back((int)M);
585   }
586 }
587 
588 } // llvm namespace
589