1 //===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Define several functions to decode x86 specific shuffle semantics using
11 // constants from the constant pool.
13 //===----------------------------------------------------------------------===//
15 #include "X86ShuffleDecodeConstantPool.h"
16 #include "Utils/X86ShuffleDecode.h"
17 #include "llvm/ADT/SmallBitVector.h"
18 #include "llvm/CodeGen/MachineValueType.h"
19 #include "llvm/IR/Constants.h"
21 //===----------------------------------------------------------------------===//
22 // Vector Mask Decoding
23 //===----------------------------------------------------------------------===//
27 static 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.
33 // e.g. the following take up the same space in the constant pool:
34 // i128 -170141183420855150465331762880109871104
36 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
38 // <4 x i32> <i32 -2147483648, i32 -2147483648,
39 // i32 -2147483648, i32 -2147483648>
40 Type *CstTy = C->getType();
41 if (!CstTy->isVectorTy())
44 Type *CstEltTy = CstTy->getVectorElementType();
45 if (!CstEltTy->isIntegerTy())
48 unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits();
49 unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits();
50 unsigned NumCstElts = CstTy->getVectorNumElements();
52 // 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) {
56 Constant *COp = C->getAggregateElement(i);
57 if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
60 if (isa<UndefValue>(COp)) {
61 APInt EltUndef = APInt::getLowBitsSet(CstSizeInBits, CstEltSizeInBits);
62 UndefBits |= EltUndef.shl(i * CstEltSizeInBits);
66 APInt EltBits = cast<ConstantInt>(COp)->getValue();
67 EltBits = EltBits.zextOrTrunc(CstSizeInBits);
68 MaskBits |= EltBits.shl(i * CstEltSizeInBits);
71 // Now extract the undef/constant bit data into the raw shuffle masks.
72 assert((CstSizeInBits % MaskEltSizeInBits) == 0 &&
73 "Unaligned shuffle mask size");
75 unsigned NumMaskElts = CstSizeInBits / MaskEltSizeInBits;
76 UndefElts = SmallBitVector(NumMaskElts, false);
77 RawMask.resize(NumMaskElts, 0);
79 for (unsigned i = 0; i != NumMaskElts; ++i) {
80 APInt EltUndef = UndefBits.lshr(i * MaskEltSizeInBits);
81 EltUndef = EltUndef.zextOrTrunc(MaskEltSizeInBits);
83 // Only treat the element as UNDEF if all bits are UNDEF, otherwise
85 if (EltUndef.isAllOnesValue()) {
91 APInt EltBits = MaskBits.lshr(i * MaskEltSizeInBits);
92 EltBits = EltBits.zextOrTrunc(MaskEltSizeInBits);
93 RawMask[i] = EltBits.getZExtValue();
99 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
100 Type *MaskTy = C->getType();
101 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
103 assert((MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512) &&
104 "Unexpected vector size.");
106 // The shuffle mask requires a byte vector.
107 SmallBitVector UndefElts;
108 SmallVector<uint64_t, 32> RawMask;
109 if (!extractConstantMask(C, 8, UndefElts, RawMask))
112 unsigned NumElts = RawMask.size();
113 assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
114 "Unexpected number of vector elements.");
116 for (unsigned i = 0; i != NumElts; ++i) {
118 ShuffleMask.push_back(SM_SentinelUndef);
122 uint64_t Element = RawMask[i];
123 // If the high bit (7) of the byte is set, the element is zeroed.
124 if (Element & (1 << 7))
125 ShuffleMask.push_back(SM_SentinelZero);
127 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
128 // lane of the vector we're inside.
129 unsigned Base = i & ~0xf;
131 // Only the least significant 4 bits of the byte are used.
132 int Index = Base + (Element & 0xf);
133 ShuffleMask.push_back(Index);
138 void DecodeVPERMILPMask(const Constant *C, unsigned ElSize,
139 SmallVectorImpl<int> &ShuffleMask) {
140 Type *MaskTy = C->getType();
141 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
143 assert((MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512) &&
144 "Unexpected vector size.");
145 assert((ElSize == 32 || ElSize == 64) && "Unexpected vector element size.");
147 // The shuffle mask requires elements the same size as the target.
148 SmallBitVector UndefElts;
149 SmallVector<uint64_t, 8> RawMask;
150 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
153 unsigned NumElts = RawMask.size();
154 unsigned NumEltsPerLane = 128 / ElSize;
155 assert((NumElts == 2 || NumElts == 4 || NumElts == 8 || NumElts == 16) &&
156 "Unexpected number of vector elements.");
158 for (unsigned i = 0; i != NumElts; ++i) {
160 ShuffleMask.push_back(SM_SentinelUndef);
164 int Index = i & ~(NumEltsPerLane - 1);
165 uint64_t Element = RawMask[i];
167 Index += (Element >> 1) & 0x1;
169 Index += Element & 0x3;
171 ShuffleMask.push_back(Index);
175 void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize,
176 SmallVectorImpl<int> &ShuffleMask) {
177 Type *MaskTy = C->getType();
178 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
180 assert((MaskTySize == 128 || MaskTySize == 256) && "Unexpected vector size.");
182 // The shuffle mask requires elements the same size as the target.
183 SmallBitVector UndefElts;
184 SmallVector<uint64_t, 8> RawMask;
185 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
188 unsigned NumElts = RawMask.size();
189 unsigned NumEltsPerLane = 128 / ElSize;
190 assert((NumElts == 2 || NumElts == 4 || NumElts == 8) &&
191 "Unexpected number of vector elements.");
193 for (unsigned i = 0; i != NumElts; ++i) {
195 ShuffleMask.push_back(SM_SentinelUndef);
199 // VPERMIL2 Operation.
200 // Bits[3] - Match Bit.
201 // Bits[2:1] - (Per Lane) PD Shuffle Mask.
202 // Bits[2:0] - (Per Lane) PS Shuffle Mask.
203 uint64_t Selector = RawMask[i];
204 unsigned MatchBit = (Selector >> 3) & 0x1;
207 // 0Xb X Source selected by Selector index.
208 // 10b 0 Source selected by Selector index.
211 // 11b 1 Source selected by Selector index.
212 if ((M2Z & 0x2) != 0u && MatchBit != (M2Z & 0x1)) {
213 ShuffleMask.push_back(SM_SentinelZero);
217 int Index = i & ~(NumEltsPerLane - 1);
219 Index += (Selector >> 1) & 0x1;
221 Index += Selector & 0x3;
223 int Src = (Selector >> 2) & 0x1;
224 Index += Src * NumElts;
225 ShuffleMask.push_back(Index);
229 void DecodeVPPERMMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
230 assert(C->getType()->getPrimitiveSizeInBits() == 128 &&
231 "Unexpected vector size.");
233 // The shuffle mask requires a byte vector.
234 SmallBitVector UndefElts;
235 SmallVector<uint64_t, 32> RawMask;
236 if (!extractConstantMask(C, 8, UndefElts, RawMask))
239 unsigned NumElts = RawMask.size();
240 assert(NumElts == 16 && "Unexpected number of vector elements.");
242 for (unsigned i = 0; i != NumElts; ++i) {
244 ShuffleMask.push_back(SM_SentinelUndef);
249 // Bits[4:0] - Byte Index (0 - 31)
250 // Bits[7:5] - Permute Operation
252 // Permute Operation:
253 // 0 - Source byte (no logical operation).
254 // 1 - Invert source byte.
255 // 2 - Bit reverse of source byte.
256 // 3 - Bit reverse of inverted source byte.
257 // 4 - 00h (zero - fill).
258 // 5 - FFh (ones - fill).
259 // 6 - Most significant bit of source byte replicated in all bit positions.
260 // 7 - Invert most significant bit of source byte and replicate in all bit
262 uint64_t Element = RawMask[i];
263 uint64_t Index = Element & 0x1F;
264 uint64_t PermuteOp = (Element >> 5) & 0x7;
266 if (PermuteOp == 4) {
267 ShuffleMask.push_back(SM_SentinelZero);
270 if (PermuteOp != 0) {
274 ShuffleMask.push_back((int)Index);
278 void DecodeVPERMVMask(const Constant *C, unsigned ElSize,
279 SmallVectorImpl<int> &ShuffleMask) {
280 Type *MaskTy = C->getType();
281 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
283 assert((MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512) &&
284 "Unexpected vector size.");
285 assert((ElSize == 8 || ElSize == 16 || ElSize == 32 || ElSize == 64) &&
286 "Unexpected vector element size.");
288 // The shuffle mask requires elements the same size as the target.
289 SmallBitVector UndefElts;
290 SmallVector<uint64_t, 8> RawMask;
291 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
294 unsigned NumElts = RawMask.size();
296 for (unsigned i = 0; i != NumElts; ++i) {
298 ShuffleMask.push_back(SM_SentinelUndef);
301 int Index = RawMask[i] & (NumElts - 1);
302 ShuffleMask.push_back(Index);
306 void DecodeVPERMV3Mask(const Constant *C, unsigned ElSize,
307 SmallVectorImpl<int> &ShuffleMask) {
308 Type *MaskTy = C->getType();
309 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
311 assert((MaskTySize == 128 || MaskTySize == 256 || MaskTySize == 512) &&
312 "Unexpected vector size.");
313 assert((ElSize == 8 || ElSize == 16 || ElSize == 32 || ElSize == 64) &&
314 "Unexpected vector element size.");
316 // The shuffle mask requires elements the same size as the target.
317 SmallBitVector UndefElts;
318 SmallVector<uint64_t, 8> RawMask;
319 if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
322 unsigned NumElts = RawMask.size();
324 for (unsigned i = 0; i != NumElts; ++i) {
326 ShuffleMask.push_back(SM_SentinelUndef);
329 int Index = RawMask[i] & (NumElts*2 - 1);
330 ShuffleMask.push_back(Index);