blob: 978619999ab2147b093edbc50a3d3e7f2a7cf483 [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"
Simon Pilgrim97a48202016-09-29 15:25:48 +000017#include "llvm/ADT/SmallBitVector.h"
Craig Topper69653af2015-12-31 22:40:45 +000018#include "llvm/CodeGen/MachineValueType.h"
19#include "llvm/IR/Constants.h"
20
21//===----------------------------------------------------------------------===//
22// Vector Mask Decoding
23//===----------------------------------------------------------------------===//
24
25namespace llvm {
26
Simon Pilgrim97a48202016-09-29 15:25:48 +000027static bool extractConstantMask(const Constant *C, unsigned MaskEltSizeInBits,
28 SmallBitVector &UndefElts,
29 SmallVectorImpl<uint64_t> &RawMask) {
30 // It is not an error for shuffle masks to not be a vector of
31 // MaskEltSizeInBits because the constant pool uniques constants by their
32 // bit representation.
Craig Topper69653af2015-12-31 22:40:45 +000033 // e.g. the following take up the same space in the constant pool:
34 // i128 -170141183420855150465331762880109871104
35 //
36 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
37 //
38 // <4 x i32> <i32 -2147483648, i32 -2147483648,
39 // i32 -2147483648, i32 -2147483648>
Simon Pilgrim97a48202016-09-29 15:25:48 +000040 Type *CstTy = C->getType();
41 if (!CstTy->isVectorTy())
42 return false;
Craig Topper69653af2015-12-31 22:40:45 +000043
Simon Pilgrim97a48202016-09-29 15:25:48 +000044 Type *CstEltTy = CstTy->getVectorElementType();
45 if (!CstEltTy->isIntegerTy())
46 return false;
Craig Topper69653af2015-12-31 22:40:45 +000047
Simon Pilgrim97a48202016-09-29 15:25:48 +000048 unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits();
49 unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits();
50 unsigned NumCstElts = CstTy->getVectorNumElements();
Craig Topper69653af2015-12-31 22:40:45 +000051
Simon Pilgrim97a48202016-09-29 15:25:48 +000052 // Extract all the undef/constant element data and pack into single bitsets.
53 APInt UndefBits(CstSizeInBits, 0);
54 APInt MaskBits(CstSizeInBits, 0);
55 for (unsigned i = 0; i != NumCstElts; ++i) {
Simon Pilgrim05e48b92016-02-18 10:17:40 +000056 Constant *COp = C->getAggregateElement(i);
Simon Pilgrim97a48202016-09-29 15:25:48 +000057 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
58 return false;
59
60 if (isa<UndefValue>(COp)) {
61 APInt EltUndef = APInt::getLowBitsSet(CstSizeInBits, CstEltSizeInBits);
62 UndefBits |= EltUndef.shl(i * CstEltSizeInBits);
Simon Pilgrim05e48b92016-02-18 10:17:40 +000063 continue;
64 }
65
Simon Pilgrim97a48202016-09-29 15:25:48 +000066 APInt EltBits = cast<ConstantInt>(COp)->getValue();
67 EltBits = EltBits.zextOrTrunc(CstSizeInBits);
68 MaskBits |= EltBits.shl(i * CstEltSizeInBits);
Craig Topper69653af2015-12-31 22:40:45 +000069 }
Simon Pilgrim05e48b92016-02-18 10:17:40 +000070
Simon Pilgrim97a48202016-09-29 15:25:48 +000071 // Now extract the undef/constant bit data into the raw shuffle masks.
72 assert((CstSizeInBits % MaskEltSizeInBits) == 0 && "");
73 unsigned NumMaskElts = CstSizeInBits / MaskEltSizeInBits;
74
75 UndefElts = SmallBitVector(NumMaskElts, false);
76 RawMask.resize(NumMaskElts, 0);
77
78 for (unsigned i = 0; i != NumMaskElts; ++i) {
79 APInt EltUndef = UndefBits.lshr(i * MaskEltSizeInBits);
80 EltUndef = EltUndef.zextOrTrunc(MaskEltSizeInBits);
81
82 // Only treat the element as UNDEF if all bits are UNDEF, otherwise
83 // treat it as zero.
84 if (EltUndef.countPopulation() == MaskEltSizeInBits) {
85 UndefElts[i] = true;
86 RawMask[i] = 0;
87 continue;
88 }
89
90 APInt EltBits = MaskBits.lshr(i * MaskEltSizeInBits);
91 EltBits = EltBits.zextOrTrunc(MaskEltSizeInBits);
92 RawMask[i] = EltBits.getZExtValue();
93 }
94
95 return true;
96}
97
98void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
99 Type *MaskTy = C->getType();
100 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
101 assert(MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512);
102
103 // The shuffle mask requires a byte vector.
104 SmallBitVector UndefElts;
105 SmallVector<uint64_t, 32> RawMask;
106 if (!extractConstantMask(C, 8, UndefElts, RawMask))
107 return;
108
109 unsigned NumElts = RawMask.size();
110 assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
111 "Unexpected number of vector elements.");
112
113 for (unsigned i = 0; i != NumElts; ++i) {
114 if (UndefElts[i]) {
115 ShuffleMask.push_back(SM_SentinelUndef);
116 continue;
117 }
118
119 uint64_t Element = RawMask[i];
120 // If the high bit (7) of the byte is set, the element is zeroed.
121 if (Element & (1 << 7))
122 ShuffleMask.push_back(SM_SentinelZero);
123 else {
124 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
125 // lane of the vector we're inside.
126 unsigned Base = i & ~0xf;
127
128 // Only the least significant 4 bits of the byte are used.
129 int Index = Base + (Element & 0xf);
130 ShuffleMask.push_back(Index);
131 }
132 }
Craig Topper69653af2015-12-31 22:40:45 +0000133}
134
135void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
136 SmallVectorImpl<int> &ShuffleMask) {
137 Type *MaskTy = C->getType();
Simon Pilgrim48d83402016-07-13 15:10:43 +0000138 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
Simon Pilgrim97a48202016-09-29 15:25:48 +0000139 assert(MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512);
140 assert(ElSize == 32 || ElSize == 64);
141
142 // The shuffle mask requires elements the same size as the target.
143 SmallBitVector UndefElts;
144 SmallVector<uint64_t, 8> RawMask;
145 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
Craig Topper69653af2015-12-31 22:40:45 +0000146 return;
147
Simon Pilgrim97a48202016-09-29 15:25:48 +0000148 unsigned NumElts = RawMask.size();
149 unsigned NumEltsPerLane = 128 / ElSize;
150 assert((NumElts == 2 || NumElts == 4 || NumElts == 8 || NumElts == 16) &&
Craig Topper69653af2015-12-31 22:40:45 +0000151 "Unexpected number of vector elements.");
Craig Topper69653af2015-12-31 22:40:45 +0000152
Simon Pilgrim97a48202016-09-29 15:25:48 +0000153 for (unsigned i = 0; i != NumElts; ++i) {
154 if (UndefElts[i]) {
Craig Topper69653af2015-12-31 22:40:45 +0000155 ShuffleMask.push_back(SM_SentinelUndef);
156 continue;
157 }
Simon Pilgrim97a48202016-09-29 15:25:48 +0000158
159 int Index = i & ~(NumEltsPerLane - 1);
160 uint64_t Element = RawMask[i];
Craig Topper69653af2015-12-31 22:40:45 +0000161 if (ElSize == 64)
162 Index += (Element >> 1) & 0x1;
163 else
164 Index += Element & 0x3;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000165
Craig Topper69653af2015-12-31 22:40:45 +0000166 ShuffleMask.push_back(Index);
167 }
Craig Topper69653af2015-12-31 22:40:45 +0000168}
169
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000170void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize,
171 SmallVectorImpl<int> &ShuffleMask) {
172 Type *MaskTy = C->getType();
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000173 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
Simon Pilgrim97a48202016-09-29 15:25:48 +0000174 assert(MaskTySize == 128 || MaskTySize == 256);
175
176 // The shuffle mask requires elements the same size as the target.
177 SmallBitVector UndefElts;
178 SmallVector<uint64_t, 8> RawMask;
179 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000180 return;
181
Simon Pilgrim97a48202016-09-29 15:25:48 +0000182 unsigned NumElts = RawMask.size();
183 unsigned NumEltsPerLane = 128 / ElSize;
184 assert((NumElts == 2 || NumElts == 4 || NumElts == 8) &&
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000185 "Unexpected number of vector elements.");
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000186
Simon Pilgrim97a48202016-09-29 15:25:48 +0000187 for (unsigned i = 0; i != NumElts; ++i) {
188 if (UndefElts[i]) {
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000189 ShuffleMask.push_back(SM_SentinelUndef);
190 continue;
191 }
192
193 // VPERMIL2 Operation.
194 // Bits[3] - Match Bit.
195 // Bits[2:1] - (Per Lane) PD Shuffle Mask.
196 // Bits[2:0] - (Per Lane) PS Shuffle Mask.
Simon Pilgrim97a48202016-09-29 15:25:48 +0000197 uint64_t Selector = RawMask[i];
Chandler Carruth4c0e94d2016-06-11 09:13:00 +0000198 unsigned MatchBit = (Selector >> 3) & 0x1;
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000199
200 // M2Z[0:1] MatchBit
201 // 0Xb X Source selected by Selector index.
202 // 10b 0 Source selected by Selector index.
203 // 10b 1 Zero.
204 // 11b 0 Zero.
205 // 11b 1 Source selected by Selector index.
Chandler Carruth306e2702016-06-11 08:02:01 +0000206 if ((M2Z & 0x2) != 0u && MatchBit != (M2Z & 0x1)) {
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000207 ShuffleMask.push_back(SM_SentinelZero);
208 continue;
209 }
210
Simon Pilgrim97a48202016-09-29 15:25:48 +0000211 int Index = i & ~(NumEltsPerLane - 1);
Simon Pilgrim163987a2016-06-05 14:33:43 +0000212 if (ElSize == 64)
213 Index += (Selector >> 1) & 0x1;
214 else
215 Index += Selector & 0x3;
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000216
217 int Src = (Selector >> 2) & 0x1;
Simon Pilgrim97a48202016-09-29 15:25:48 +0000218 Index += Src * NumElts;
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000219 ShuffleMask.push_back(Index);
220 }
Simon Pilgrim2ead8612016-06-04 21:44:28 +0000221}
222
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000223void DecodeVPPERMMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
224 Type *MaskTy = C->getType();
225 assert(MaskTy->getPrimitiveSizeInBits() == 128);
226
Simon Pilgrim97a48202016-09-29 15:25:48 +0000227 // The shuffle mask requires a byte vector.
228 SmallBitVector UndefElts;
229 SmallVector<uint64_t, 32> RawMask;
230 if (!extractConstantMask(C, 8, UndefElts, RawMask))
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000231 return;
232
Simon Pilgrim97a48202016-09-29 15:25:48 +0000233 unsigned NumElts = RawMask.size();
234 assert(NumElts == 16 && "Unexpected number of vector elements.");
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000235
Simon Pilgrim97a48202016-09-29 15:25:48 +0000236 for (unsigned i = 0; i != NumElts; ++i) {
237 if (UndefElts[i]) {
238 ShuffleMask.push_back(SM_SentinelUndef);
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000239 continue;
240 }
241
242 // VPPERM Operation
243 // Bits[4:0] - Byte Index (0 - 31)
244 // Bits[7:5] - Permute Operation
245 //
246 // Permute Operation:
247 // 0 - Source byte (no logical operation).
248 // 1 - Invert source byte.
249 // 2 - Bit reverse of source byte.
250 // 3 - Bit reverse of inverted source byte.
251 // 4 - 00h (zero - fill).
252 // 5 - FFh (ones - fill).
253 // 6 - Most significant bit of source byte replicated in all bit positions.
Simon Pilgrim97a48202016-09-29 15:25:48 +0000254 // 7 - Invert most significant bit of source byte and replicate in all bit
255 // positions.
256 uint64_t Element = RawMask[i];
257 uint64_t Index = Element & 0x1F;
258 uint64_t PermuteOp = (Element >> 5) & 0x7;
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000259
Simon Pilgrim97a48202016-09-29 15:25:48 +0000260 if (PermuteOp == 4) {
261 ShuffleMask.push_back(SM_SentinelZero);
262 continue;
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000263 }
Simon Pilgrim97a48202016-09-29 15:25:48 +0000264 if (PermuteOp != 0) {
265 ShuffleMask.clear();
266 return;
267 }
268 ShuffleMask.push_back((int)Index);
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000269 }
Simon Pilgrim1cc57122016-04-09 14:51:26 +0000270}
271
Craig Topper69653af2015-12-31 22:40:45 +0000272void DecodeVPERMVMask(const Constant *C, MVT VT,
273 SmallVectorImpl<int> &ShuffleMask) {
274 Type *MaskTy = C->getType();
275 if (MaskTy->isVectorTy()) {
276 unsigned NumElements = MaskTy->getVectorNumElements();
277 if (NumElements == VT.getVectorNumElements()) {
Simon Pilgrim48adedf2016-07-05 18:31:17 +0000278 unsigned EltMaskSize = Log2_64(NumElements);
Craig Topper69653af2015-12-31 22:40:45 +0000279 for (unsigned i = 0; i < NumElements; ++i) {
280 Constant *COp = C->getAggregateElement(i);
281 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) {
282 ShuffleMask.clear();
283 return;
284 }
285 if (isa<UndefValue>(COp))
286 ShuffleMask.push_back(SM_SentinelUndef);
287 else {
Simon Pilgrim48adedf2016-07-05 18:31:17 +0000288 APInt Element = cast<ConstantInt>(COp)->getValue();
289 Element = Element.getLoBits(EltMaskSize);
290 ShuffleMask.push_back(Element.getZExtValue());
Craig Topper69653af2015-12-31 22:40:45 +0000291 }
292 }
293 }
294 return;
295 }
296 // Scalar value; just broadcast it
297 if (!isa<ConstantInt>(C))
298 return;
299 uint64_t Element = cast<ConstantInt>(C)->getZExtValue();
300 int NumElements = VT.getVectorNumElements();
301 Element &= (1 << NumElements) - 1;
302 for (int i = 0; i < NumElements; ++i)
303 ShuffleMask.push_back(Element);
304}
305
306void DecodeVPERMV3Mask(const Constant *C, MVT VT,
307 SmallVectorImpl<int> &ShuffleMask) {
308 Type *MaskTy = C->getType();
309 unsigned NumElements = MaskTy->getVectorNumElements();
310 if (NumElements == VT.getVectorNumElements()) {
Simon Pilgrim253ca342016-03-06 21:54:52 +0000311 unsigned EltMaskSize = Log2_64(NumElements * 2);
Craig Topper69653af2015-12-31 22:40:45 +0000312 for (unsigned i = 0; i < NumElements; ++i) {
313 Constant *COp = C->getAggregateElement(i);
314 if (!COp) {
315 ShuffleMask.clear();
316 return;
317 }
318 if (isa<UndefValue>(COp))
319 ShuffleMask.push_back(SM_SentinelUndef);
320 else {
Simon Pilgrim253ca342016-03-06 21:54:52 +0000321 APInt Element = cast<ConstantInt>(COp)->getValue();
322 Element = Element.getLoBits(EltMaskSize);
323 ShuffleMask.push_back(Element.getZExtValue());
Craig Topper69653af2015-12-31 22:40:45 +0000324 }
325 }
326 }
327}
328} // llvm namespace