blob: 720be8afa62c2c44b7e8e3e5d6985a95e083163c [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
Craig Topper69653af2015-12-31 22:40:45 +000015#include "Utils/X86ShuffleDecode.h"
Craig Topper53e5a38d2017-02-27 16:15:25 +000016#include "llvm/ADT/APInt.h"
Craig Topper69653af2015-12-31 22:40:45 +000017#include "llvm/IR/Constants.h"
18
19//===----------------------------------------------------------------------===//
20// Vector Mask Decoding
21//===----------------------------------------------------------------------===//
22
23namespace llvm {
24
Simon Pilgrim97a48202016-09-29 15:25:48 +000025static bool extractConstantMask(const Constant *C, unsigned MaskEltSizeInBits,
Craig Topper53e5a38d2017-02-27 16:15:25 +000026 APInt &UndefElts,
Simon Pilgrim97a48202016-09-29 15:25:48 +000027 SmallVectorImpl<uint64_t> &RawMask) {
28 // It is not an error for shuffle masks to not be a vector of
29 // MaskEltSizeInBits because the constant pool uniques constants by their
30 // bit representation.
Craig Topper69653af2015-12-31 22:40:45 +000031 // e.g. the following take up the same space in the constant pool:
32 // i128 -170141183420855150465331762880109871104
33 //
34 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
35 //
36 // <4 x i32> <i32 -2147483648, i32 -2147483648,
37 // i32 -2147483648, i32 -2147483648>
Simon Pilgrim97a48202016-09-29 15:25:48 +000038 Type *CstTy = C->getType();
39 if (!CstTy->isVectorTy())
40 return false;
Craig Topper69653af2015-12-31 22:40:45 +000041
Simon Pilgrim97a48202016-09-29 15:25:48 +000042 Type *CstEltTy = CstTy->getVectorElementType();
43 if (!CstEltTy->isIntegerTy())
44 return false;
Craig Topper69653af2015-12-31 22:40:45 +000045
Simon Pilgrim97a48202016-09-29 15:25:48 +000046 unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits();
47 unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits();
48 unsigned NumCstElts = CstTy->getVectorNumElements();
Craig Topper69653af2015-12-31 22:40:45 +000049
Simon Pilgrime86b7e22017-03-09 14:06:39 +000050 assert((CstSizeInBits % MaskEltSizeInBits) == 0 &&
51 "Unaligned shuffle mask size");
52
53 unsigned NumMaskElts = CstSizeInBits / MaskEltSizeInBits;
54 UndefElts = APInt(NumMaskElts, 0);
55 RawMask.resize(NumMaskElts, 0);
56
57 // Fast path - if the constants match the mask size then copy direct.
58 if (MaskEltSizeInBits == CstEltSizeInBits) {
59 assert(NumCstElts == NumMaskElts && "Unaligned shuffle mask size");
60 for (unsigned i = 0; i != NumMaskElts; ++i) {
61 Constant *COp = C->getAggregateElement(i);
62 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
63 return false;
64
65 if (isa<UndefValue>(COp)) {
66 UndefElts.setBit(i);
67 RawMask[i] = 0;
68 continue;
69 }
70
71 auto *Elt = cast<ConstantInt>(COp);
72 RawMask[i] = Elt->getValue().getZExtValue();
73 }
74 return true;
75 }
76
Simon Pilgrim97a48202016-09-29 15:25:48 +000077 // Extract all the undef/constant element data and pack into single bitsets.
78 APInt UndefBits(CstSizeInBits, 0);
79 APInt MaskBits(CstSizeInBits, 0);
80 for (unsigned i = 0; i != NumCstElts; ++i) {
Simon Pilgrim05e48b92016-02-18 10:17:40 +000081 Constant *COp = C->getAggregateElement(i);
Simon Pilgrim97a48202016-09-29 15:25:48 +000082 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
83 return false;
84
Simon Pilgrim3a895c42017-02-22 15:04:55 +000085 unsigned BitOffset = i * CstEltSizeInBits;
86
Simon Pilgrim97a48202016-09-29 15:25:48 +000087 if (isa<UndefValue>(COp)) {
Simon Pilgrimaed35222017-02-24 10:15:29 +000088 UndefBits.setBits(BitOffset, BitOffset + CstEltSizeInBits);
Simon Pilgrim05e48b92016-02-18 10:17:40 +000089 continue;
90 }
91
Simon Pilgrimb02667c2017-03-10 13:44:32 +000092 MaskBits.insertBits(cast<ConstantInt>(COp)->getValue(), BitOffset);
Craig Topper69653af2015-12-31 22:40:45 +000093 }
Simon Pilgrim05e48b92016-02-18 10:17:40 +000094
Simon Pilgrim97a48202016-09-29 15:25:48 +000095 // Now extract the undef/constant bit data into the raw shuffle masks.
Simon Pilgrim97a48202016-09-29 15:25:48 +000096 for (unsigned i = 0; i != NumMaskElts; ++i) {
Simon Pilgrim0f5fb5f2017-02-25 20:01:58 +000097 unsigned BitOffset = i * MaskEltSizeInBits;
98 APInt EltUndef = UndefBits.extractBits(MaskEltSizeInBits, BitOffset);
Simon Pilgrim97a48202016-09-29 15:25:48 +000099
100 // Only treat the element as UNDEF if all bits are UNDEF, otherwise
101 // treat it as zero.
Simon Pilgrim96ef0c12016-10-23 15:09:44 +0000102 if (EltUndef.isAllOnesValue()) {
Craig Topper53e5a38d2017-02-27 16:15:25 +0000103 UndefElts.setBit(i);
Simon Pilgrim97a48202016-09-29 15:25:48 +0000104 RawMask[i] = 0;
105 continue;
106 }
107
Simon Pilgrim0f5fb5f2017-02-25 20:01:58 +0000108 APInt EltBits = MaskBits.extractBits(MaskEltSizeInBits, BitOffset);
Simon Pilgrim97a48202016-09-29 15:25:48 +0000109 RawMask[i] = EltBits.getZExtValue();
110 }
111
112 return true;
113}
114
Craig Topperc8e183f2018-10-22 22:14:05 +0000115void DecodePSHUFBMask(const Constant *C, unsigned Width,
116 SmallVectorImpl<int> &ShuffleMask) {
117 assert((Width == 128 || Width == 256 || Width == 512) &&
118 C->getType()->getPrimitiveSizeInBits() >= Width &&
Simon Pilgrim630dd6f2016-10-01 20:12:56 +0000119 "Unexpected vector size.");
Simon Pilgrim97a48202016-09-29 15:25:48 +0000120
121 // The shuffle mask requires a byte vector.
Craig Topper53e5a38d2017-02-27 16:15:25 +0000122 APInt UndefElts;
Craig Toppere1be95c2017-02-27 16:15:27 +0000123 SmallVector<uint64_t, 64> RawMask;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000124 if (!extractConstantMask(C, 8, UndefElts, RawMask))
125 return;
126
Craig Topperc8e183f2018-10-22 22:14:05 +0000127 unsigned NumElts = Width / 8;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000128 assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
129 "Unexpected number of vector elements.");
130
131 for (unsigned i = 0; i != NumElts; ++i) {
132 if (UndefElts[i]) {
133 ShuffleMask.push_back(SM_SentinelUndef);
134 continue;
135 }
136
137 uint64_t Element = RawMask[i];
138 // If the high bit (7) of the byte is set, the element is zeroed.
139 if (Element & (1 << 7))
140 ShuffleMask.push_back(SM_SentinelZero);
141 else {
142 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
143 // lane of the vector we're inside.
144 unsigned Base = i & ~0xf;
145
146 // Only the least significant 4 bits of the byte are used.
147 int Index = Base + (Element & 0xf);
148 ShuffleMask.push_back(Index);
149 }
150 }
Craig Topper69653af2015-12-31 22:40:45 +0000151}
152
Craig Topperc8e183f2018-10-22 22:14:05 +0000153void DecodeVPERMILPMask(const Constant *C, unsigned ElSize, unsigned Width,
Craig Topper69653af2015-12-31 22:40:45 +0000154 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc8e183f2018-10-22 22:14:05 +0000155 assert((Width == 128 || Width == 256 || Width == 512) &&
156 C->getType()->getPrimitiveSizeInBits() >= Width &&
Simon Pilgrim630dd6f2016-10-01 20:12:56 +0000157 "Unexpected vector size.");
158 assert((ElSize == 32 || ElSize == 64) && "Unexpected vector element size.");
Simon Pilgrim97a48202016-09-29 15:25:48 +0000159
160 // The shuffle mask requires elements the same size as the target.
Craig Topper53e5a38d2017-02-27 16:15:25 +0000161 APInt UndefElts;
Craig Toppere1be95c2017-02-27 16:15:27 +0000162 SmallVector<uint64_t, 16> RawMask;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000163 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
Craig Topper69653af2015-12-31 22:40:45 +0000164 return;
165
Craig Topperc8e183f2018-10-22 22:14:05 +0000166 unsigned NumElts = Width / ElSize;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000167 unsigned NumEltsPerLane = 128 / ElSize;
168 assert((NumElts == 2 || NumElts == 4 || NumElts == 8 || NumElts == 16) &&
Craig Topper69653af2015-12-31 22:40:45 +0000169 "Unexpected number of vector elements.");
Craig Topper69653af2015-12-31 22:40:45 +0000170
Simon Pilgrim97a48202016-09-29 15:25:48 +0000171 for (unsigned i = 0; i != NumElts; ++i) {
172 if (UndefElts[i]) {
Craig Topper69653af2015-12-31 22:40:45 +0000173 ShuffleMask.push_back(SM_SentinelUndef);
174 continue;
175 }
Simon Pilgrim97a48202016-09-29 15:25:48 +0000176
177 int Index = i & ~(NumEltsPerLane - 1);
178 uint64_t Element = RawMask[i];
Craig Topper69653af2015-12-31 22:40:45 +0000179 if (ElSize == 64)
180 Index += (Element >> 1) & 0x1;
181 else
182 Index += Element & 0x3;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000183
Craig Topper69653af2015-12-31 22:40:45 +0000184 ShuffleMask.push_back(Index);
185 }
Craig Topper69653af2015-12-31 22:40:45 +0000186}
187
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000188void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize,
Craig Topperc8e183f2018-10-22 22:14:05 +0000189 unsigned Width,
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000190 SmallVectorImpl<int> &ShuffleMask) {
191 Type *MaskTy = C->getType();
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000192 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
Douglas Katzman3ace13a2016-09-29 17:26:12 +0000193 (void)MaskTySize;
Craig Topperc8e183f2018-10-22 22:14:05 +0000194 assert((MaskTySize == 128 || MaskTySize == 256) &&
195 Width >= MaskTySize && "Unexpected vector size.");
Simon Pilgrim97a48202016-09-29 15:25:48 +0000196
197 // The shuffle mask requires elements the same size as the target.
Craig Topper53e5a38d2017-02-27 16:15:25 +0000198 APInt UndefElts;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000199 SmallVector<uint64_t, 8> RawMask;
200 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000201 return;
202
Craig Topperc8e183f2018-10-22 22:14:05 +0000203 unsigned NumElts = Width / ElSize;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000204 unsigned NumEltsPerLane = 128 / ElSize;
205 assert((NumElts == 2 || NumElts == 4 || NumElts == 8) &&
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000206 "Unexpected number of vector elements.");
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000207
Simon Pilgrim97a48202016-09-29 15:25:48 +0000208 for (unsigned i = 0; i != NumElts; ++i) {
209 if (UndefElts[i]) {
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000210 ShuffleMask.push_back(SM_SentinelUndef);
211 continue;
212 }
213
214 // VPERMIL2 Operation.
215 // Bits[3] - Match Bit.
216 // Bits[2:1] - (Per Lane) PD Shuffle Mask.
217 // Bits[2:0] - (Per Lane) PS Shuffle Mask.
Simon Pilgrim97a48202016-09-29 15:25:48 +0000218 uint64_t Selector = RawMask[i];
Chandler Carruth4c0e94d2016-06-11 09:13:00 +0000219 unsigned MatchBit = (Selector >> 3) & 0x1;
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000220
221 // M2Z[0:1] MatchBit
222 // 0Xb X Source selected by Selector index.
223 // 10b 0 Source selected by Selector index.
224 // 10b 1 Zero.
225 // 11b 0 Zero.
226 // 11b 1 Source selected by Selector index.
Chandler Carruth306e2702016-06-11 08:02:01 +0000227 if ((M2Z & 0x2) != 0u && MatchBit != (M2Z & 0x1)) {
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000228 ShuffleMask.push_back(SM_SentinelZero);
229 continue;
230 }
231
Simon Pilgrim97a48202016-09-29 15:25:48 +0000232 int Index = i & ~(NumEltsPerLane - 1);
Simon Pilgrim163987a2016-06-05 14:33:43 +0000233 if (ElSize == 64)
234 Index += (Selector >> 1) & 0x1;
235 else
236 Index += Selector & 0x3;
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000237
238 int Src = (Selector >> 2) & 0x1;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000239 Index += Src * NumElts;
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000240 ShuffleMask.push_back(Index);
241 }
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000242}
243
Craig Topperc8e183f2018-10-22 22:14:05 +0000244void DecodeVPPERMMask(const Constant *C, unsigned Width,
245 SmallVectorImpl<int> &ShuffleMask) {
246 Type *MaskTy = C->getType();
247 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
248 (void)MaskTySize;
249 assert(Width == 128 && Width >= MaskTySize && "Unexpected vector size.");
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000250
Simon Pilgrim97a48202016-09-29 15:25:48 +0000251 // The shuffle mask requires a byte vector.
Craig Topper53e5a38d2017-02-27 16:15:25 +0000252 APInt UndefElts;
Craig Toppere1be95c2017-02-27 16:15:27 +0000253 SmallVector<uint64_t, 16> RawMask;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000254 if (!extractConstantMask(C, 8, UndefElts, RawMask))
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000255 return;
256
Craig Topperc8e183f2018-10-22 22:14:05 +0000257 unsigned NumElts = Width / 8;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000258 assert(NumElts == 16 && "Unexpected number of vector elements.");
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000259
Simon Pilgrim97a48202016-09-29 15:25:48 +0000260 for (unsigned i = 0; i != NumElts; ++i) {
261 if (UndefElts[i]) {
262 ShuffleMask.push_back(SM_SentinelUndef);
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000263 continue;
264 }
265
266 // VPPERM Operation
267 // Bits[4:0] - Byte Index (0 - 31)
268 // Bits[7:5] - Permute Operation
269 //
270 // Permute Operation:
271 // 0 - Source byte (no logical operation).
272 // 1 - Invert source byte.
273 // 2 - Bit reverse of source byte.
274 // 3 - Bit reverse of inverted source byte.
275 // 4 - 00h (zero - fill).
276 // 5 - FFh (ones - fill).
277 // 6 - Most significant bit of source byte replicated in all bit positions.
Simon Pilgrim97a48202016-09-29 15:25:48 +0000278 // 7 - Invert most significant bit of source byte and replicate in all bit
279 // positions.
280 uint64_t Element = RawMask[i];
281 uint64_t Index = Element & 0x1F;
282 uint64_t PermuteOp = (Element >> 5) & 0x7;
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000283
Simon Pilgrim97a48202016-09-29 15:25:48 +0000284 if (PermuteOp == 4) {
285 ShuffleMask.push_back(SM_SentinelZero);
286 continue;
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000287 }
Simon Pilgrim97a48202016-09-29 15:25:48 +0000288 if (PermuteOp != 0) {
289 ShuffleMask.clear();
290 return;
291 }
292 ShuffleMask.push_back((int)Index);
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000293 }
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000294}
295
Craig Topperc8e183f2018-10-22 22:14:05 +0000296void DecodeVPERMVMask(const Constant *C, unsigned ElSize, unsigned Width,
Craig Topper69653af2015-12-31 22:40:45 +0000297 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc8e183f2018-10-22 22:14:05 +0000298 assert((Width == 128 || Width == 256 || Width == 512) &&
299 C->getType()->getPrimitiveSizeInBits() >= Width &&
Craig Topper448358b2016-10-18 04:48:33 +0000300 "Unexpected vector size.");
301 assert((ElSize == 8 || ElSize == 16 || ElSize == 32 || ElSize == 64) &&
302 "Unexpected vector element size.");
303
304 // The shuffle mask requires elements the same size as the target.
Craig Topper53e5a38d2017-02-27 16:15:25 +0000305 APInt UndefElts;
Craig Toppere1be95c2017-02-27 16:15:27 +0000306 SmallVector<uint64_t, 64> RawMask;
Craig Topper448358b2016-10-18 04:48:33 +0000307 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
308 return;
309
Craig Topperc8e183f2018-10-22 22:14:05 +0000310 unsigned NumElts = Width / ElSize;
Craig Topper448358b2016-10-18 04:48:33 +0000311
312 for (unsigned i = 0; i != NumElts; ++i) {
313 if (UndefElts[i]) {
314 ShuffleMask.push_back(SM_SentinelUndef);
315 continue;
Craig Topper69653af2015-12-31 22:40:45 +0000316 }
Craig Topper448358b2016-10-18 04:48:33 +0000317 int Index = RawMask[i] & (NumElts - 1);
318 ShuffleMask.push_back(Index);
Craig Topper69653af2015-12-31 22:40:45 +0000319 }
Craig Topper69653af2015-12-31 22:40:45 +0000320}
321
Craig Topperc8e183f2018-10-22 22:14:05 +0000322void DecodeVPERMV3Mask(const Constant *C, unsigned ElSize, unsigned Width,
Craig Topper69653af2015-12-31 22:40:45 +0000323 SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc8e183f2018-10-22 22:14:05 +0000324 assert((Width == 128 || Width == 256 || Width == 512) &&
325 C->getType()->getPrimitiveSizeInBits() >= Width &&
Craig Topper7268bf92016-10-18 04:00:32 +0000326 "Unexpected vector size.");
327 assert((ElSize == 8 || ElSize == 16 || ElSize == 32 || ElSize == 64) &&
328 "Unexpected vector element size.");
329
330 // The shuffle mask requires elements the same size as the target.
Craig Topper53e5a38d2017-02-27 16:15:25 +0000331 APInt UndefElts;
Craig Toppere1be95c2017-02-27 16:15:27 +0000332 SmallVector<uint64_t, 64> RawMask;
Craig Topper7268bf92016-10-18 04:00:32 +0000333 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
334 return;
335
Craig Topperc8e183f2018-10-22 22:14:05 +0000336 unsigned NumElts = Width / ElSize;
Craig Topper7268bf92016-10-18 04:00:32 +0000337
338 for (unsigned i = 0; i != NumElts; ++i) {
339 if (UndefElts[i]) {
340 ShuffleMask.push_back(SM_SentinelUndef);
341 continue;
Craig Topper69653af2015-12-31 22:40:45 +0000342 }
Craig Topper7268bf92016-10-18 04:00:32 +0000343 int Index = RawMask[i] & (NumElts*2 - 1);
344 ShuffleMask.push_back(Index);
Craig Topper69653af2015-12-31 22:40:45 +0000345 }
346}
347} // llvm namespace