[X86][SSE] pslldq/psrldq shuffle mask decodes
[oota-llvm.git] / lib / Target / X86 / Utils / X86ShuffleDecode.cpp
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/IR/Constants.h"
17 #include "llvm/CodeGen/MachineValueType.h"
18
19 //===----------------------------------------------------------------------===//
20 //  Vector Mask Decoding
21 //===----------------------------------------------------------------------===//
22
23 namespace llvm {
24
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
48 // <3,1> or <6,7,2,3>
49 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
50   for (unsigned i = NElts/2; i != NElts; ++i)
51     ShuffleMask.push_back(NElts+i);
52
53   for (unsigned i = NElts/2; i != NElts; ++i)
54     ShuffleMask.push_back(i);
55 }
56
57 // <0,2> or <0,1,4,5>
58 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
59   for (unsigned i = 0; i != NElts/2; ++i)
60     ShuffleMask.push_back(i);
61
62   for (unsigned i = 0; i != NElts/2; ++i)
63     ShuffleMask.push_back(NElts+i);
64 }
65
66 void 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
74 void 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
82 void DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
83   unsigned VectorSizeInBits = VT.getSizeInBits();
84   unsigned NumElts = VectorSizeInBits / 8;
85   unsigned NumLanes = VectorSizeInBits / 128;
86   unsigned NumLaneElts = NumElts / NumLanes;
87
88   for (unsigned l = 0; l < NumElts; l += NumLaneElts)
89     for (unsigned i = 0; i < NumLaneElts; ++i) {
90       int M = SM_SentinelZero;
91       if (i >= Imm) M = i - Imm + l;
92       ShuffleMask.push_back(M);
93     }
94 }
95
96 void DecodePSRLDQMask(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       unsigned Base = i + Imm;
105       int M = Base + l;
106       if (Base >= NumLaneElts) M = SM_SentinelZero;
107       ShuffleMask.push_back(M);
108     }
109 }
110
111 void DecodePALIGNRMask(MVT VT, unsigned Imm,
112                        SmallVectorImpl<int> &ShuffleMask) {
113   unsigned NumElts = VT.getVectorNumElements();
114   unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
115
116   unsigned NumLanes = VT.getSizeInBits() / 128;
117   unsigned NumLaneElts = NumElts / NumLanes;
118
119   for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
120     for (unsigned i = 0; i != NumLaneElts; ++i) {
121       unsigned Base = i + Offset;
122       // if i+offset is out of this lane then we actually need the other source
123       if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
124       ShuffleMask.push_back(Base + l);
125     }
126   }
127 }
128
129 /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*.
130 /// VT indicates the type of the vector allowing it to handle different
131 /// datatypes and vector widths.
132 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
133   unsigned NumElts = VT.getVectorNumElements();
134
135   unsigned NumLanes = VT.getSizeInBits() / 128;
136   unsigned NumLaneElts = NumElts / NumLanes;
137
138   unsigned NewImm = Imm;
139   for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
140     for (unsigned i = 0; i != NumLaneElts; ++i) {
141       ShuffleMask.push_back(NewImm % NumLaneElts + l);
142       NewImm /= NumLaneElts;
143     }
144     if (NumLaneElts == 4) NewImm = Imm; // reload imm
145   }
146 }
147
148 void DecodePSHUFHWMask(MVT VT, unsigned Imm,
149                        SmallVectorImpl<int> &ShuffleMask) {
150   unsigned NumElts = VT.getVectorNumElements();
151
152   for (unsigned l = 0; l != NumElts; l += 8) {
153     unsigned NewImm = Imm;
154     for (unsigned i = 0, e = 4; i != e; ++i) {
155       ShuffleMask.push_back(l + i);
156     }
157     for (unsigned i = 4, e = 8; i != e; ++i) {
158       ShuffleMask.push_back(l + 4 + (NewImm & 3));
159       NewImm >>= 2;
160     }
161   }
162 }
163
164 void DecodePSHUFLWMask(MVT VT, unsigned Imm,
165                        SmallVectorImpl<int> &ShuffleMask) {
166   unsigned NumElts = VT.getVectorNumElements();
167
168   for (unsigned l = 0; l != NumElts; l += 8) {
169     unsigned NewImm = Imm;
170     for (unsigned i = 0, e = 4; i != e; ++i) {
171       ShuffleMask.push_back(l + (NewImm & 3));
172       NewImm >>= 2;
173     }
174     for (unsigned i = 4, e = 8; i != e; ++i) {
175       ShuffleMask.push_back(l + i);
176     }
177   }
178 }
179
180 /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
181 /// the type of the vector allowing it to handle different datatypes and vector
182 /// widths.
183 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
184   unsigned NumElts = VT.getVectorNumElements();
185
186   unsigned NumLanes = VT.getSizeInBits() / 128;
187   unsigned NumLaneElts = NumElts / NumLanes;
188
189   unsigned NewImm = Imm;
190   for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
191     // each half of a lane comes from different source
192     for (unsigned s = 0; s != NumElts*2; s += NumElts) {
193       for (unsigned i = 0; i != NumLaneElts/2; ++i) {
194         ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
195         NewImm /= NumLaneElts;
196       }
197     }
198     if (NumLaneElts == 4) NewImm = Imm; // reload imm
199   }
200 }
201
202 /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
203 /// and punpckh*. VT indicates the type of the vector allowing it to handle
204 /// different datatypes and vector widths.
205 void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
206   unsigned NumElts = VT.getVectorNumElements();
207
208   // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
209   // independently on 128-bit lanes.
210   unsigned NumLanes = VT.getSizeInBits() / 128;
211   if (NumLanes == 0 ) NumLanes = 1;  // Handle MMX
212   unsigned NumLaneElts = NumElts / NumLanes;
213
214   for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
215     for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
216       ShuffleMask.push_back(i);          // Reads from dest/src1
217       ShuffleMask.push_back(i+NumElts);  // Reads from src/src2
218     }
219   }
220 }
221
222 /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
223 /// and punpckl*. VT indicates the type of the vector allowing it to handle
224 /// different datatypes and vector widths.
225 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
226   unsigned NumElts = VT.getVectorNumElements();
227
228   // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
229   // independently on 128-bit lanes.
230   unsigned NumLanes = VT.getSizeInBits() / 128;
231   if (NumLanes == 0 ) NumLanes = 1;  // Handle MMX
232   unsigned NumLaneElts = NumElts / NumLanes;
233
234   for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
235     for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
236       ShuffleMask.push_back(i);          // Reads from dest/src1
237       ShuffleMask.push_back(i+NumElts);  // Reads from src/src2
238     }
239   }
240 }
241
242 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
243                           SmallVectorImpl<int> &ShuffleMask) {
244   if (Imm & 0x88)
245     return; // Not a shuffle
246
247   unsigned HalfSize = VT.getVectorNumElements()/2;
248
249   for (unsigned l = 0; l != 2; ++l) {
250     unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize;
251     for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i)
252       ShuffleMask.push_back(i);
253   }
254 }
255
256 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
257   Type *MaskTy = C->getType();
258   assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
259   assert(MaskTy->getVectorElementType()->isIntegerTy(8) &&
260          "Expected i8 constant mask elements!");
261   int NumElements = MaskTy->getVectorNumElements();
262   // FIXME: Add support for AVX-512.
263   assert((NumElements == 16 || NumElements == 32) &&
264          "Only 128-bit and 256-bit vectors supported!");
265   ShuffleMask.reserve(NumElements);
266
267   if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
268     assert((unsigned)NumElements == CDS->getNumElements() &&
269            "Constant mask has a different number of elements!");
270
271     for (int i = 0; i < NumElements; ++i) {
272       // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
273       // lane of the vector we're inside.
274       int Base = i < 16 ? 0 : 16;
275       uint64_t Element = CDS->getElementAsInteger(i);
276       // If the high bit (7) of the byte is set, the element is zeroed.
277       if (Element & (1 << 7))
278         ShuffleMask.push_back(SM_SentinelZero);
279       else {
280         // Only the least significant 4 bits of the byte are used.
281         int Index = Base + (Element & 0xf);
282         ShuffleMask.push_back(Index);
283       }
284     }
285   } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
286     assert((unsigned)NumElements == CV->getNumOperands() &&
287            "Constant mask has a different number of elements!");
288
289     for (int i = 0; i < NumElements; ++i) {
290       // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
291       // lane of the vector we're inside.
292       int Base = i < 16 ? 0 : 16;
293       Constant *COp = CV->getOperand(i);
294       if (isa<UndefValue>(COp)) {
295         ShuffleMask.push_back(SM_SentinelUndef);
296         continue;
297       }
298       uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
299       // If the high bit (7) of the byte is set, the element is zeroed.
300       if (Element & (1 << 7))
301         ShuffleMask.push_back(SM_SentinelZero);
302       else {
303         // Only the least significant 4 bits of the byte are used.
304         int Index = Base + (Element & 0xf);
305         ShuffleMask.push_back(Index);
306       }
307     }
308   }
309 }
310
311 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
312                       SmallVectorImpl<int> &ShuffleMask) {
313   for (int i = 0, e = RawMask.size(); i < e; ++i) {
314     uint64_t M = RawMask[i];
315     if (M == (uint64_t)SM_SentinelUndef) {
316       ShuffleMask.push_back(M);
317       continue;
318     }
319     // For AVX vectors with 32 bytes the base of the shuffle is the half of
320     // the vector we're inside.
321     int Base = i < 16 ? 0 : 16;
322     // If the high bit (7) of the byte is set, the element is zeroed.
323     if (M & (1 << 7))
324       ShuffleMask.push_back(SM_SentinelZero);
325     else {
326       // Only the least significant 4 bits of the byte are used.
327       int Index = Base + (M & 0xf);
328       ShuffleMask.push_back(Index);
329     }
330   }
331 }
332
333 void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
334   int ElementBits = VT.getScalarSizeInBits();
335   int NumElements = VT.getVectorNumElements();
336   for (int i = 0; i < NumElements; ++i) {
337     // If there are more than 8 elements in the vector, then any immediate blend
338     // mask applies to each 128-bit lane. There can never be more than
339     // 8 elements in a 128-bit lane with an immediate blend.
340     int Bit = NumElements > 8 ? i % (128 / ElementBits) : i;
341     assert(Bit < 8 &&
342            "Immediate blends only operate over 8 elements at a time!");
343     ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
344   }
345 }
346
347 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
348 /// No VT provided since it only works on 256-bit, 4 element vectors.
349 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
350   for (unsigned i = 0; i != 4; ++i) {
351     ShuffleMask.push_back((Imm >> (2*i)) & 3);
352   }
353 }
354
355 void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
356   Type *MaskTy = C->getType();
357   assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
358   assert(MaskTy->getVectorElementType()->isIntegerTy() &&
359          "Expected integer constant mask elements!");
360   int ElementBits = MaskTy->getScalarSizeInBits();
361   int NumElements = MaskTy->getVectorNumElements();
362   assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
363          "Unexpected number of vector elements.");
364   ShuffleMask.reserve(NumElements);
365   if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
366     assert((unsigned)NumElements == CDS->getNumElements() &&
367            "Constant mask has a different number of elements!");
368
369     for (int i = 0; i < NumElements; ++i) {
370       int Base = (i * ElementBits / 128) * (128 / ElementBits);
371       uint64_t Element = CDS->getElementAsInteger(i);
372       // Only the least significant 2 bits of the integer are used.
373       int Index = Base + (Element & 0x3);
374       ShuffleMask.push_back(Index);
375     }
376   } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
377     assert((unsigned)NumElements == C->getNumOperands() &&
378            "Constant mask has a different number of elements!");
379
380     for (int i = 0; i < NumElements; ++i) {
381       int Base = (i * ElementBits / 128) * (128 / ElementBits);
382       Constant *COp = CV->getOperand(i);
383       if (isa<UndefValue>(COp)) {
384         ShuffleMask.push_back(SM_SentinelUndef);
385         continue;
386       }
387       uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
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   }
393 }
394
395 } // llvm namespace