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