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