blob: abc05e436767fe8809d1848525574539c5023f2f [file] [log] [blame]
Craig Topper69653af2015-12-31 22:40:45 +00001//===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===//
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 using
11// constants from the constant pool.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86ShuffleDecodeConstantPool.h"
16#include "Utils/X86ShuffleDecode.h"
17#include "llvm/CodeGen/MachineValueType.h"
18#include "llvm/IR/Constants.h"
19
20//===----------------------------------------------------------------------===//
21// Vector Mask Decoding
22//===----------------------------------------------------------------------===//
23
24namespace llvm {
25
26void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
27 Type *MaskTy = C->getType();
28 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
29 // constant pool uniques constants by their bit representation.
30 // e.g. the following take up the same space in the constant pool:
31 // i128 -170141183420855150465331762880109871104
32 //
33 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
34 //
35 // <4 x i32> <i32 -2147483648, i32 -2147483648,
36 // i32 -2147483648, i32 -2147483648>
37
38#ifndef NDEBUG
39 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
40 assert(MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512);
41#endif
42
Simon Pilgrim05e48b92016-02-18 10:17:40 +000043 if (!MaskTy->isVectorTy())
44 return;
45 int NumElts = MaskTy->getVectorNumElements();
Craig Topper69653af2015-12-31 22:40:45 +000046
Simon Pilgrim05e48b92016-02-18 10:17:40 +000047 Type *EltTy = MaskTy->getVectorElementType();
48 if (!EltTy->isIntegerTy())
49 return;
50
51 // The shuffle mask requires a byte vector - decode cases with
52 // wider elements as well.
53 unsigned BitWidth = cast<IntegerType>(EltTy)->getBitWidth();
54 if ((BitWidth % 8) != 0)
55 return;
56
57 int Scale = BitWidth / 8;
58 int NumBytes = NumElts * Scale;
59 ShuffleMask.reserve(NumBytes);
60
61 for (int i = 0; i != NumElts; ++i) {
62 Constant *COp = C->getAggregateElement(i);
63 if (!COp) {
64 ShuffleMask.clear();
65 return;
66 } else if (isa<UndefValue>(COp)) {
67 ShuffleMask.append(Scale, SM_SentinelUndef);
68 continue;
69 }
70
71 APInt APElt = cast<ConstantInt>(COp)->getValue();
72 for (int j = 0; j != Scale; ++j) {
Craig Topper69653af2015-12-31 22:40:45 +000073 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
74 // lane of the vector we're inside.
Simon Pilgrim05e48b92016-02-18 10:17:40 +000075 int Base = ((i * Scale) + j) & ~0xf;
76
77 uint64_t Element = APElt.getLoBits(8).getZExtValue();
78 APElt = APElt.lshr(8);
79
Craig Topper69653af2015-12-31 22:40:45 +000080 // If the high bit (7) of the byte is set, the element is zeroed.
81 if (Element & (1 << 7))
82 ShuffleMask.push_back(SM_SentinelZero);
83 else {
84 // Only the least significant 4 bits of the byte are used.
85 int Index = Base + (Element & 0xf);
86 ShuffleMask.push_back(Index);
87 }
88 }
89 }
Simon Pilgrim05e48b92016-02-18 10:17:40 +000090
91 assert(NumBytes == (int)ShuffleMask.size() && "Unexpected shuffle mask size");
Craig Topper69653af2015-12-31 22:40:45 +000092}
93
94void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
95 SmallVectorImpl<int> &ShuffleMask) {
96 Type *MaskTy = C->getType();
97 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
98 // constant pool uniques constants by their bit representation.
99 // e.g. the following take up the same space in the constant pool:
100 // i128 -170141183420855150465331762880109871104
101 //
102 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
103 //
104 // <4 x i32> <i32 -2147483648, i32 -2147483648,
105 // i32 -2147483648, i32 -2147483648>
106
107 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
108
109 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
110 return;
111
112 // Only support vector types.
113 if (!MaskTy->isVectorTy())
114 return;
115
116 // Make sure its an integer type.
117 Type *VecEltTy = MaskTy->getVectorElementType();
118 if (!VecEltTy->isIntegerTy())
119 return;
120
121 // Support any element type from byte up to element size.
Simon Pilgrim10e3ca22016-02-26 21:56:27 +0000122 // This is necessary primarily because 64-bit elements get split to 32-bit
Craig Topper69653af2015-12-31 22:40:45 +0000123 // in the constant pool on 32-bit target.
124 unsigned EltTySize = VecEltTy->getIntegerBitWidth();
125 if (EltTySize < 8 || EltTySize > ElSize)
126 return;
127
128 unsigned NumElements = MaskTySize / ElSize;
129 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
130 "Unexpected number of vector elements.");
131 ShuffleMask.reserve(NumElements);
132 unsigned NumElementsPerLane = 128 / ElSize;
133 unsigned Factor = ElSize / EltTySize;
134
135 for (unsigned i = 0; i < NumElements; ++i) {
136 Constant *COp = C->getAggregateElement(i * Factor);
137 if (!COp) {
138 ShuffleMask.clear();
139 return;
140 } else if (isa<UndefValue>(COp)) {
141 ShuffleMask.push_back(SM_SentinelUndef);
142 continue;
143 }
144 int Index = i & ~(NumElementsPerLane - 1);
145 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
146 if (ElSize == 64)
147 Index += (Element >> 1) & 0x1;
148 else
149 Index += Element & 0x3;
150 ShuffleMask.push_back(Index);
151 }
152
153 // TODO: Handle funny-looking vectors too.
154}
155
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000156void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize,
157 SmallVectorImpl<int> &ShuffleMask) {
158 Type *MaskTy = C->getType();
159
160 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
161 if (MaskTySize != 128 && MaskTySize != 256)
162 return;
163
164 // Only support vector types.
165 if (!MaskTy->isVectorTy())
166 return;
167
168 // Make sure its an integer type.
169 Type *VecEltTy = MaskTy->getVectorElementType();
170 if (!VecEltTy->isIntegerTy())
171 return;
172
173 // Support any element type from byte up to element size.
174 // This is necessary primarily because 64-bit elements get split to 32-bit
175 // in the constant pool on 32-bit target.
176 unsigned EltTySize = VecEltTy->getIntegerBitWidth();
177 if (EltTySize < 8 || EltTySize > ElSize)
178 return;
179
180 unsigned NumElements = MaskTySize / ElSize;
181 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
182 "Unexpected number of vector elements.");
183 ShuffleMask.reserve(NumElements);
184 unsigned NumElementsPerLane = 128 / ElSize;
185 unsigned Factor = ElSize / EltTySize;
186
187 for (unsigned i = 0; i < NumElements; ++i) {
188 Constant *COp = C->getAggregateElement(i * Factor);
189 if (!COp) {
190 ShuffleMask.clear();
191 return;
192 } else if (isa<UndefValue>(COp)) {
193 ShuffleMask.push_back(SM_SentinelUndef);
194 continue;
195 }
196
197 // VPERMIL2 Operation.
198 // Bits[3] - Match Bit.
199 // Bits[2:1] - (Per Lane) PD Shuffle Mask.
200 // Bits[2:0] - (Per Lane) PS Shuffle Mask.
201 uint64_t Selector = cast<ConstantInt>(COp)->getZExtValue();
202 int MatchBit = (Selector >> 3) & 0x1;
203
204 // M2Z[0:1] MatchBit
205 // 0Xb X Source selected by Selector index.
206 // 10b 0 Source selected by Selector index.
207 // 10b 1 Zero.
208 // 11b 0 Zero.
209 // 11b 1 Source selected by Selector index.
210 if ((M2Z & 0x2) != 0 && MatchBit != (M2Z & 0x1)) {
211 ShuffleMask.push_back(SM_SentinelZero);
212 continue;
213 }
214
215 int Index = Selector & 0x3;
216 Index >>= (ElSize == 64 ? 1 : 0);
217 Index += (i / NumElementsPerLane) * NumElementsPerLane;
218
219 int Src = (Selector >> 2) & 0x1;
220 Index += Src * NumElements;
221 ShuffleMask.push_back(Index);
222 }
223
224 // TODO: Handle funny-looking vectors too.
225}
226
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000227void DecodeVPPERMMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
228 Type *MaskTy = C->getType();
229 assert(MaskTy->getPrimitiveSizeInBits() == 128);
230
231 // Only support vector types.
232 if (!MaskTy->isVectorTy())
233 return;
234
235 // Make sure its an integer type.
236 Type *VecEltTy = MaskTy->getVectorElementType();
237 if (!VecEltTy->isIntegerTy())
238 return;
239
240 // The shuffle mask requires a byte vector - decode cases with
241 // wider elements as well.
242 unsigned BitWidth = cast<IntegerType>(VecEltTy)->getBitWidth();
243 if ((BitWidth % 8) != 0)
244 return;
245
246 int NumElts = MaskTy->getVectorNumElements();
247 int Scale = BitWidth / 8;
248 int NumBytes = NumElts * Scale;
249 ShuffleMask.reserve(NumBytes);
250
251 for (int i = 0; i != NumElts; ++i) {
252 Constant *COp = C->getAggregateElement(i);
253 if (!COp) {
254 ShuffleMask.clear();
255 return;
256 } else if (isa<UndefValue>(COp)) {
257 ShuffleMask.append(Scale, SM_SentinelUndef);
258 continue;
259 }
260
261 // VPPERM Operation
262 // Bits[4:0] - Byte Index (0 - 31)
263 // Bits[7:5] - Permute Operation
264 //
265 // Permute Operation:
266 // 0 - Source byte (no logical operation).
267 // 1 - Invert source byte.
268 // 2 - Bit reverse of source byte.
269 // 3 - Bit reverse of inverted source byte.
270 // 4 - 00h (zero - fill).
271 // 5 - FFh (ones - fill).
272 // 6 - Most significant bit of source byte replicated in all bit positions.
273 // 7 - Invert most significant bit of source byte and replicate in all bit positions.
274 APInt MaskElt = cast<ConstantInt>(COp)->getValue();
275 for (int j = 0; j != Scale; ++j) {
276 APInt Index = MaskElt.getLoBits(5);
277 APInt PermuteOp = MaskElt.lshr(5).getLoBits(3);
278 MaskElt = MaskElt.lshr(8);
279
280 if (PermuteOp == 4) {
281 ShuffleMask.push_back(SM_SentinelZero);
282 continue;
283 }
284 if (PermuteOp != 0) {
285 ShuffleMask.clear();
286 return;
287 }
288 ShuffleMask.push_back((int)Index.getZExtValue());
289 }
290 }
291
292 assert(NumBytes == (int)ShuffleMask.size() && "Unexpected shuffle mask size");
293}
294
Craig Topper69653af2015-12-31 22:40:45 +0000295void DecodeVPERMVMask(const Constant *C, MVT VT,
296 SmallVectorImpl<int> &ShuffleMask) {
297 Type *MaskTy = C->getType();
298 if (MaskTy->isVectorTy()) {
299 unsigned NumElements = MaskTy->getVectorNumElements();
300 if (NumElements == VT.getVectorNumElements()) {
301 for (unsigned i = 0; i < NumElements; ++i) {
302 Constant *COp = C->getAggregateElement(i);
303 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) {
304 ShuffleMask.clear();
305 return;
306 }
307 if (isa<UndefValue>(COp))
308 ShuffleMask.push_back(SM_SentinelUndef);
309 else {
310 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
311 Element &= (1 << NumElements) - 1;
312 ShuffleMask.push_back(Element);
313 }
314 }
315 }
316 return;
317 }
318 // Scalar value; just broadcast it
319 if (!isa<ConstantInt>(C))
320 return;
321 uint64_t Element = cast<ConstantInt>(C)->getZExtValue();
322 int NumElements = VT.getVectorNumElements();
323 Element &= (1 << NumElements) - 1;
324 for (int i = 0; i < NumElements; ++i)
325 ShuffleMask.push_back(Element);
326}
327
328void DecodeVPERMV3Mask(const Constant *C, MVT VT,
329 SmallVectorImpl<int> &ShuffleMask) {
330 Type *MaskTy = C->getType();
331 unsigned NumElements = MaskTy->getVectorNumElements();
332 if (NumElements == VT.getVectorNumElements()) {
Simon Pilgrim253ca342016-03-06 21:54:52 +0000333 unsigned EltMaskSize = Log2_64(NumElements * 2);
Craig Topper69653af2015-12-31 22:40:45 +0000334 for (unsigned i = 0; i < NumElements; ++i) {
335 Constant *COp = C->getAggregateElement(i);
336 if (!COp) {
337 ShuffleMask.clear();
338 return;
339 }
340 if (isa<UndefValue>(COp))
341 ShuffleMask.push_back(SM_SentinelUndef);
342 else {
Simon Pilgrim253ca342016-03-06 21:54:52 +0000343 APInt Element = cast<ConstantInt>(COp)->getValue();
344 Element = Element.getLoBits(EltMaskSize);
345 ShuffleMask.push_back(Element.getZExtValue());
Craig Topper69653af2015-12-31 22:40:45 +0000346 }
347 }
348 }
349}
350} // llvm namespace