1*0b57cec5SDimitry Andric //===-- X86ShuffleDecodeConstantPool.cpp - X86 shuffle decode -------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // Define several functions to decode x86 specific shuffle semantics using
10*0b57cec5SDimitry Andric // constants from the constant pool.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric #include "X86ShuffleDecodeConstantPool.h"
15*0b57cec5SDimitry Andric #include "MCTargetDesc/X86ShuffleDecode.h"
16*0b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
17*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
18*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
21*0b57cec5SDimitry Andric // Vector Mask Decoding
22*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
23*0b57cec5SDimitry Andric
24*0b57cec5SDimitry Andric namespace llvm {
25*0b57cec5SDimitry Andric
extractConstantMask(const Constant * C,unsigned MaskEltSizeInBits,APInt & UndefElts,SmallVectorImpl<uint64_t> & RawMask)26*0b57cec5SDimitry Andric static bool extractConstantMask(const Constant *C, unsigned MaskEltSizeInBits,
27*0b57cec5SDimitry Andric APInt &UndefElts,
28*0b57cec5SDimitry Andric SmallVectorImpl<uint64_t> &RawMask) {
29*0b57cec5SDimitry Andric // It is not an error for shuffle masks to not be a vector of
30*0b57cec5SDimitry Andric // MaskEltSizeInBits because the constant pool uniques constants by their
31*0b57cec5SDimitry Andric // bit representation.
32*0b57cec5SDimitry Andric // e.g. the following take up the same space in the constant pool:
33*0b57cec5SDimitry Andric // i128 -170141183420855150465331762880109871104
34*0b57cec5SDimitry Andric //
35*0b57cec5SDimitry Andric // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
36*0b57cec5SDimitry Andric //
37*0b57cec5SDimitry Andric // <4 x i32> <i32 -2147483648, i32 -2147483648,
38*0b57cec5SDimitry Andric // i32 -2147483648, i32 -2147483648>
39*0b57cec5SDimitry Andric auto *CstTy = dyn_cast<FixedVectorType>(C->getType());
40*0b57cec5SDimitry Andric if (!CstTy)
41*0b57cec5SDimitry Andric return false;
42*0b57cec5SDimitry Andric
43*0b57cec5SDimitry Andric Type *CstEltTy = CstTy->getElementType();
44*0b57cec5SDimitry Andric if (!CstEltTy->isIntegerTy())
45*0b57cec5SDimitry Andric return false;
46*0b57cec5SDimitry Andric
47*0b57cec5SDimitry Andric unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits();
48*0b57cec5SDimitry Andric unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits();
49*0b57cec5SDimitry Andric unsigned NumCstElts = CstTy->getNumElements();
50*0b57cec5SDimitry Andric
51*0b57cec5SDimitry Andric assert((CstSizeInBits % MaskEltSizeInBits) == 0 &&
52*0b57cec5SDimitry Andric "Unaligned shuffle mask size");
53*0b57cec5SDimitry Andric
54*0b57cec5SDimitry Andric unsigned NumMaskElts = CstSizeInBits / MaskEltSizeInBits;
55*0b57cec5SDimitry Andric UndefElts = APInt(NumMaskElts, 0);
56*0b57cec5SDimitry Andric RawMask.resize(NumMaskElts, 0);
57*0b57cec5SDimitry Andric
58*0b57cec5SDimitry Andric // Fast path - if the constants match the mask size then copy direct.
59*0b57cec5SDimitry Andric if (MaskEltSizeInBits == CstEltSizeInBits) {
60*0b57cec5SDimitry Andric assert(NumCstElts == NumMaskElts && "Unaligned shuffle mask size");
61*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumMaskElts; ++i) {
62*0b57cec5SDimitry Andric Constant *COp = C->getAggregateElement(i);
63*0b57cec5SDimitry Andric if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
64*0b57cec5SDimitry Andric return false;
65*0b57cec5SDimitry Andric
66*0b57cec5SDimitry Andric if (isa<UndefValue>(COp)) {
67*0b57cec5SDimitry Andric UndefElts.setBit(i);
68*0b57cec5SDimitry Andric RawMask[i] = 0;
69*0b57cec5SDimitry Andric continue;
70*0b57cec5SDimitry Andric }
71*0b57cec5SDimitry Andric
72*0b57cec5SDimitry Andric auto *Elt = cast<ConstantInt>(COp);
73*0b57cec5SDimitry Andric RawMask[i] = Elt->getValue().getZExtValue();
74*0b57cec5SDimitry Andric }
75*0b57cec5SDimitry Andric return true;
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric
78*0b57cec5SDimitry Andric // Extract all the undef/constant element data and pack into single bitsets.
79*0b57cec5SDimitry Andric APInt UndefBits(CstSizeInBits, 0);
80*0b57cec5SDimitry Andric APInt MaskBits(CstSizeInBits, 0);
81*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumCstElts; ++i) {
82*0b57cec5SDimitry Andric Constant *COp = C->getAggregateElement(i);
83*0b57cec5SDimitry Andric if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp)))
84*0b57cec5SDimitry Andric return false;
85*0b57cec5SDimitry Andric
86*0b57cec5SDimitry Andric unsigned BitOffset = i * CstEltSizeInBits;
87*0b57cec5SDimitry Andric
88*0b57cec5SDimitry Andric if (isa<UndefValue>(COp)) {
89*0b57cec5SDimitry Andric UndefBits.setBits(BitOffset, BitOffset + CstEltSizeInBits);
90*0b57cec5SDimitry Andric continue;
91*0b57cec5SDimitry Andric }
92*0b57cec5SDimitry Andric
93*0b57cec5SDimitry Andric MaskBits.insertBits(cast<ConstantInt>(COp)->getValue(), BitOffset);
94*0b57cec5SDimitry Andric }
95*0b57cec5SDimitry Andric
96*0b57cec5SDimitry Andric // Now extract the undef/constant bit data into the raw shuffle masks.
97*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumMaskElts; ++i) {
98*0b57cec5SDimitry Andric unsigned BitOffset = i * MaskEltSizeInBits;
99*0b57cec5SDimitry Andric APInt EltUndef = UndefBits.extractBits(MaskEltSizeInBits, BitOffset);
100*0b57cec5SDimitry Andric
101*0b57cec5SDimitry Andric // Only treat the element as UNDEF if all bits are UNDEF, otherwise
102*0b57cec5SDimitry Andric // treat it as zero.
103*0b57cec5SDimitry Andric if (EltUndef.isAllOnes()) {
104*0b57cec5SDimitry Andric UndefElts.setBit(i);
105*0b57cec5SDimitry Andric RawMask[i] = 0;
106*0b57cec5SDimitry Andric continue;
107*0b57cec5SDimitry Andric }
108*0b57cec5SDimitry Andric
109*0b57cec5SDimitry Andric APInt EltBits = MaskBits.extractBits(MaskEltSizeInBits, BitOffset);
110*0b57cec5SDimitry Andric RawMask[i] = EltBits.getZExtValue();
111*0b57cec5SDimitry Andric }
112*0b57cec5SDimitry Andric
113*0b57cec5SDimitry Andric return true;
114*0b57cec5SDimitry Andric }
115*0b57cec5SDimitry Andric
DecodePSHUFBMask(const Constant * C,unsigned Width,SmallVectorImpl<int> & ShuffleMask)116*0b57cec5SDimitry Andric void DecodePSHUFBMask(const Constant *C, unsigned Width,
117*0b57cec5SDimitry Andric SmallVectorImpl<int> &ShuffleMask) {
118*0b57cec5SDimitry Andric assert((Width == 128 || Width == 256 || Width == 512) &&
119*0b57cec5SDimitry Andric C->getType()->getPrimitiveSizeInBits() >= Width &&
120*0b57cec5SDimitry Andric "Unexpected vector size.");
121*0b57cec5SDimitry Andric
122*0b57cec5SDimitry Andric // The shuffle mask requires a byte vector.
123*0b57cec5SDimitry Andric APInt UndefElts;
124*0b57cec5SDimitry Andric SmallVector<uint64_t, 64> RawMask;
125*0b57cec5SDimitry Andric if (!extractConstantMask(C, 8, UndefElts, RawMask))
126*0b57cec5SDimitry Andric return;
127*0b57cec5SDimitry Andric
128*0b57cec5SDimitry Andric unsigned NumElts = Width / 8;
129*0b57cec5SDimitry Andric assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
130*0b57cec5SDimitry Andric "Unexpected number of vector elements.");
131*0b57cec5SDimitry Andric
132*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
133*0b57cec5SDimitry Andric if (UndefElts[i]) {
134*0b57cec5SDimitry Andric ShuffleMask.push_back(SM_SentinelUndef);
135*0b57cec5SDimitry Andric continue;
136*0b57cec5SDimitry Andric }
137*0b57cec5SDimitry Andric
138*0b57cec5SDimitry Andric uint64_t Element = RawMask[i];
139*0b57cec5SDimitry Andric // If the high bit (7) of the byte is set, the element is zeroed.
140*0b57cec5SDimitry Andric if (Element & (1 << 7))
141*0b57cec5SDimitry Andric ShuffleMask.push_back(SM_SentinelZero);
142*0b57cec5SDimitry Andric else {
143*0b57cec5SDimitry Andric // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
144*0b57cec5SDimitry Andric // lane of the vector we're inside.
145*0b57cec5SDimitry Andric unsigned Base = i & ~0xf;
146*0b57cec5SDimitry Andric
147*0b57cec5SDimitry Andric // Only the least significant 4 bits of the byte are used.
148*0b57cec5SDimitry Andric int Index = Base + (Element & 0xf);
149*0b57cec5SDimitry Andric ShuffleMask.push_back(Index);
150*0b57cec5SDimitry Andric }
151*0b57cec5SDimitry Andric }
152*0b57cec5SDimitry Andric }
153*0b57cec5SDimitry Andric
DecodeVPERMILPMask(const Constant * C,unsigned ElSize,unsigned Width,SmallVectorImpl<int> & ShuffleMask)154*0b57cec5SDimitry Andric void DecodeVPERMILPMask(const Constant *C, unsigned ElSize, unsigned Width,
155*0b57cec5SDimitry Andric SmallVectorImpl<int> &ShuffleMask) {
156*0b57cec5SDimitry Andric assert((Width == 128 || Width == 256 || Width == 512) &&
157*0b57cec5SDimitry Andric C->getType()->getPrimitiveSizeInBits() >= Width &&
158*0b57cec5SDimitry Andric "Unexpected vector size.");
159*0b57cec5SDimitry Andric assert((ElSize == 32 || ElSize == 64) && "Unexpected vector element size.");
160*0b57cec5SDimitry Andric
161*0b57cec5SDimitry Andric // The shuffle mask requires elements the same size as the target.
162*0b57cec5SDimitry Andric APInt UndefElts;
163*0b57cec5SDimitry Andric SmallVector<uint64_t, 16> RawMask;
164*0b57cec5SDimitry Andric if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
165*0b57cec5SDimitry Andric return;
166*0b57cec5SDimitry Andric
167*0b57cec5SDimitry Andric unsigned NumElts = Width / ElSize;
168*0b57cec5SDimitry Andric unsigned NumEltsPerLane = 128 / ElSize;
169*0b57cec5SDimitry Andric assert((NumElts == 2 || NumElts == 4 || NumElts == 8 || NumElts == 16) &&
170*0b57cec5SDimitry Andric "Unexpected number of vector elements.");
171*0b57cec5SDimitry Andric
172*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
173*0b57cec5SDimitry Andric if (UndefElts[i]) {
174*0b57cec5SDimitry Andric ShuffleMask.push_back(SM_SentinelUndef);
175*0b57cec5SDimitry Andric continue;
176*0b57cec5SDimitry Andric }
177*0b57cec5SDimitry Andric
178*0b57cec5SDimitry Andric int Index = i & ~(NumEltsPerLane - 1);
179*0b57cec5SDimitry Andric uint64_t Element = RawMask[i];
180*0b57cec5SDimitry Andric if (ElSize == 64)
181*0b57cec5SDimitry Andric Index += (Element >> 1) & 0x1;
182*0b57cec5SDimitry Andric else
183*0b57cec5SDimitry Andric Index += Element & 0x3;
184*0b57cec5SDimitry Andric
185*0b57cec5SDimitry Andric ShuffleMask.push_back(Index);
186*0b57cec5SDimitry Andric }
187*0b57cec5SDimitry Andric }
188*0b57cec5SDimitry Andric
DecodeVPERMIL2PMask(const Constant * C,unsigned M2Z,unsigned ElSize,unsigned Width,SmallVectorImpl<int> & ShuffleMask)189*0b57cec5SDimitry Andric void DecodeVPERMIL2PMask(const Constant *C, unsigned M2Z, unsigned ElSize,
190*0b57cec5SDimitry Andric unsigned Width, SmallVectorImpl<int> &ShuffleMask) {
191*0b57cec5SDimitry Andric Type *MaskTy = C->getType();
192*0b57cec5SDimitry Andric unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
193*0b57cec5SDimitry Andric (void)MaskTySize;
194*0b57cec5SDimitry Andric assert((MaskTySize == 128 || MaskTySize == 256) && Width >= MaskTySize &&
195*0b57cec5SDimitry Andric "Unexpected vector size.");
196*0b57cec5SDimitry Andric
197*0b57cec5SDimitry Andric // The shuffle mask requires elements the same size as the target.
198*0b57cec5SDimitry Andric APInt UndefElts;
199*0b57cec5SDimitry Andric SmallVector<uint64_t, 8> RawMask;
200*0b57cec5SDimitry Andric if (!extractConstantMask(C, ElSize, UndefElts, RawMask))
201*0b57cec5SDimitry Andric return;
202*0b57cec5SDimitry Andric
203*0b57cec5SDimitry Andric unsigned NumElts = Width / ElSize;
204*0b57cec5SDimitry Andric unsigned NumEltsPerLane = 128 / ElSize;
205*0b57cec5SDimitry Andric assert((NumElts == 2 || NumElts == 4 || NumElts == 8) &&
206*0b57cec5SDimitry Andric "Unexpected number of vector elements.");
207*0b57cec5SDimitry Andric
208*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
209*0b57cec5SDimitry Andric if (UndefElts[i]) {
210*0b57cec5SDimitry Andric ShuffleMask.push_back(SM_SentinelUndef);
211*0b57cec5SDimitry Andric continue;
212*0b57cec5SDimitry Andric }
213*0b57cec5SDimitry Andric
214*0b57cec5SDimitry Andric // VPERMIL2 Operation.
215*0b57cec5SDimitry Andric // Bits[3] - Match Bit.
216*0b57cec5SDimitry Andric // Bits[2:1] - (Per Lane) PD Shuffle Mask.
217*0b57cec5SDimitry Andric // Bits[2:0] - (Per Lane) PS Shuffle Mask.
218*0b57cec5SDimitry Andric uint64_t Selector = RawMask[i];
219*0b57cec5SDimitry Andric unsigned MatchBit = (Selector >> 3) & 0x1;
220*0b57cec5SDimitry Andric
221*0b57cec5SDimitry Andric // M2Z[0:1] MatchBit
222*0b57cec5SDimitry Andric // 0Xb X Source selected by Selector index.
223*0b57cec5SDimitry Andric // 10b 0 Source selected by Selector index.
224*0b57cec5SDimitry Andric // 10b 1 Zero.
225*0b57cec5SDimitry Andric // 11b 0 Zero.
226*0b57cec5SDimitry Andric // 11b 1 Source selected by Selector index.
227*0b57cec5SDimitry Andric if ((M2Z & 0x2) != 0u && MatchBit != (M2Z & 0x1)) {
228*0b57cec5SDimitry Andric ShuffleMask.push_back(SM_SentinelZero);
229*0b57cec5SDimitry Andric continue;
230*0b57cec5SDimitry Andric }
231*0b57cec5SDimitry Andric
232*0b57cec5SDimitry Andric int Index = i & ~(NumEltsPerLane - 1);
233*0b57cec5SDimitry Andric if (ElSize == 64)
234*0b57cec5SDimitry Andric Index += (Selector >> 1) & 0x1;
235*0b57cec5SDimitry Andric else
236*0b57cec5SDimitry Andric Index += Selector & 0x3;
237*0b57cec5SDimitry Andric
238*0b57cec5SDimitry Andric int Src = (Selector >> 2) & 0x1;
239*0b57cec5SDimitry Andric Index += Src * NumElts;
240*0b57cec5SDimitry Andric ShuffleMask.push_back(Index);
241*0b57cec5SDimitry Andric }
242*0b57cec5SDimitry Andric }
243*0b57cec5SDimitry Andric
DecodeVPPERMMask(const Constant * C,unsigned Width,SmallVectorImpl<int> & ShuffleMask)244*0b57cec5SDimitry Andric void DecodeVPPERMMask(const Constant *C, unsigned Width,
245*0b57cec5SDimitry Andric SmallVectorImpl<int> &ShuffleMask) {
246*0b57cec5SDimitry Andric Type *MaskTy = C->getType();
247*0b57cec5SDimitry Andric unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
248*0b57cec5SDimitry Andric (void)MaskTySize;
249*0b57cec5SDimitry Andric assert(Width == 128 && Width >= MaskTySize && "Unexpected vector size.");
250*0b57cec5SDimitry Andric
251*0b57cec5SDimitry Andric // The shuffle mask requires a byte vector.
252*0b57cec5SDimitry Andric APInt UndefElts;
253*0b57cec5SDimitry Andric SmallVector<uint64_t, 16> RawMask;
254*0b57cec5SDimitry Andric if (!extractConstantMask(C, 8, UndefElts, RawMask))
255*0b57cec5SDimitry Andric return;
256*0b57cec5SDimitry Andric
257*0b57cec5SDimitry Andric unsigned NumElts = Width / 8;
258*0b57cec5SDimitry Andric assert(NumElts == 16 && "Unexpected number of vector elements.");
259*0b57cec5SDimitry Andric
260*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
261*0b57cec5SDimitry Andric if (UndefElts[i]) {
262*0b57cec5SDimitry Andric ShuffleMask.push_back(SM_SentinelUndef);
263*0b57cec5SDimitry Andric continue;
264*0b57cec5SDimitry Andric }
265*0b57cec5SDimitry Andric
266*0b57cec5SDimitry Andric // VPPERM Operation
267*0b57cec5SDimitry Andric // Bits[4:0] - Byte Index (0 - 31)
268*0b57cec5SDimitry Andric // Bits[7:5] - Permute Operation
269*0b57cec5SDimitry Andric //
270*0b57cec5SDimitry Andric // Permute Operation:
271*0b57cec5SDimitry Andric // 0 - Source byte (no logical operation).
272*0b57cec5SDimitry Andric // 1 - Invert source byte.
273*0b57cec5SDimitry Andric // 2 - Bit reverse of source byte.
274*0b57cec5SDimitry Andric // 3 - Bit reverse of inverted source byte.
275*0b57cec5SDimitry Andric // 4 - 00h (zero - fill).
276*0b57cec5SDimitry Andric // 5 - FFh (ones - fill).
277*0b57cec5SDimitry Andric // 6 - Most significant bit of source byte replicated in all bit positions.
278*0b57cec5SDimitry Andric // 7 - Invert most significant bit of source byte and replicate in all bit
279*0b57cec5SDimitry Andric // positions.
280*0b57cec5SDimitry Andric uint64_t Element = RawMask[i];
281*0b57cec5SDimitry Andric uint64_t Index = Element & 0x1F;
282*0b57cec5SDimitry Andric uint64_t PermuteOp = (Element >> 5) & 0x7;
283*0b57cec5SDimitry Andric
284*0b57cec5SDimitry Andric if (PermuteOp == 4) {
285*0b57cec5SDimitry Andric ShuffleMask.push_back(SM_SentinelZero);
286*0b57cec5SDimitry Andric continue;
287*0b57cec5SDimitry Andric }
288*0b57cec5SDimitry Andric if (PermuteOp != 0) {
289*0b57cec5SDimitry Andric ShuffleMask.clear();
290*0b57cec5SDimitry Andric return;
291*0b57cec5SDimitry Andric }
292*0b57cec5SDimitry Andric ShuffleMask.push_back((int)Index);
293*0b57cec5SDimitry Andric }
294*0b57cec5SDimitry Andric }
295*0b57cec5SDimitry Andric
296*0b57cec5SDimitry Andric } // namespace llvm
297*0b57cec5SDimitry Andric