1 //===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===//
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 into a
11 // generic vector mask.
13 //===----------------------------------------------------------------------===//
15 #include "X86ShuffleDecode.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/CodeGen/MachineValueType.h"
19 //===----------------------------------------------------------------------===//
20 // Vector Mask Decoding
21 //===----------------------------------------------------------------------===//
25 void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
26 // Defaults the copying the dest value.
27 ShuffleMask.push_back(0);
28 ShuffleMask.push_back(1);
29 ShuffleMask.push_back(2);
30 ShuffleMask.push_back(3);
32 // Decode the immediate.
33 unsigned ZMask = Imm & 15;
34 unsigned CountD = (Imm >> 4) & 3;
35 unsigned CountS = (Imm >> 6) & 3;
37 // CountS selects which input element to use.
38 unsigned InVal = 4+CountS;
39 // CountD specifies which element of destination to update.
40 ShuffleMask[CountD] = InVal;
41 // ZMask zaps values, potentially overriding the CountD elt.
42 if (ZMask & 1) ShuffleMask[0] = SM_SentinelZero;
43 if (ZMask & 2) ShuffleMask[1] = SM_SentinelZero;
44 if (ZMask & 4) ShuffleMask[2] = SM_SentinelZero;
45 if (ZMask & 8) ShuffleMask[3] = SM_SentinelZero;
49 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
50 for (unsigned i = NElts/2; i != NElts; ++i)
51 ShuffleMask.push_back(NElts+i);
53 for (unsigned i = NElts/2; i != NElts; ++i)
54 ShuffleMask.push_back(i);
58 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
59 for (unsigned i = 0; i != NElts/2; ++i)
60 ShuffleMask.push_back(i);
62 for (unsigned i = 0; i != NElts/2; ++i)
63 ShuffleMask.push_back(NElts+i);
66 void DecodeMOVSLDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
67 unsigned NumElts = VT.getVectorNumElements();
68 for (int i = 0, e = NumElts / 2; i < e; ++i) {
69 ShuffleMask.push_back(2 * i);
70 ShuffleMask.push_back(2 * i);
74 void DecodeMOVSHDUPMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
75 unsigned NumElts = VT.getVectorNumElements();
76 for (int i = 0, e = NumElts / 2; i < e; ++i) {
77 ShuffleMask.push_back(2 * i + 1);
78 ShuffleMask.push_back(2 * i + 1);
82 void DecodePSLLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
83 unsigned VectorSizeInBits = VT.getSizeInBits();
84 unsigned NumElts = VectorSizeInBits / 8;
85 unsigned NumLanes = VectorSizeInBits / 128;
86 unsigned NumLaneElts = NumElts / NumLanes;
88 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
89 for (unsigned i = 0; i < NumLaneElts; ++i) {
90 int M = SM_SentinelZero;
91 if (i >= Imm) M = i - Imm + l;
92 ShuffleMask.push_back(M);
96 void DecodePSRLDQMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
97 unsigned VectorSizeInBits = VT.getSizeInBits();
98 unsigned NumElts = VectorSizeInBits / 8;
99 unsigned NumLanes = VectorSizeInBits / 128;
100 unsigned NumLaneElts = NumElts / NumLanes;
102 for (unsigned l = 0; l < NumElts; l += NumLaneElts)
103 for (unsigned i = 0; i < NumLaneElts; ++i) {
104 unsigned Base = i + Imm;
106 if (Base >= NumLaneElts) M = SM_SentinelZero;
107 ShuffleMask.push_back(M);
111 void DecodePALIGNRMask(MVT VT, unsigned Imm,
112 SmallVectorImpl<int> &ShuffleMask) {
113 unsigned NumElts = VT.getVectorNumElements();
114 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
116 unsigned NumLanes = VT.getSizeInBits() / 128;
117 unsigned NumLaneElts = NumElts / NumLanes;
119 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
120 for (unsigned i = 0; i != NumLaneElts; ++i) {
121 unsigned Base = i + Offset;
122 // if i+offset is out of this lane then we actually need the other source
123 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
124 ShuffleMask.push_back(Base + l);
129 /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*.
130 /// VT indicates the type of the vector allowing it to handle different
131 /// datatypes and vector widths.
132 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
133 unsigned NumElts = VT.getVectorNumElements();
135 unsigned NumLanes = VT.getSizeInBits() / 128;
136 unsigned NumLaneElts = NumElts / NumLanes;
138 unsigned NewImm = Imm;
139 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
140 for (unsigned i = 0; i != NumLaneElts; ++i) {
141 ShuffleMask.push_back(NewImm % NumLaneElts + l);
142 NewImm /= NumLaneElts;
144 if (NumLaneElts == 4) NewImm = Imm; // reload imm
148 void DecodePSHUFHWMask(MVT VT, unsigned Imm,
149 SmallVectorImpl<int> &ShuffleMask) {
150 unsigned NumElts = VT.getVectorNumElements();
152 for (unsigned l = 0; l != NumElts; l += 8) {
153 unsigned NewImm = Imm;
154 for (unsigned i = 0, e = 4; i != e; ++i) {
155 ShuffleMask.push_back(l + i);
157 for (unsigned i = 4, e = 8; i != e; ++i) {
158 ShuffleMask.push_back(l + 4 + (NewImm & 3));
164 void DecodePSHUFLWMask(MVT VT, unsigned Imm,
165 SmallVectorImpl<int> &ShuffleMask) {
166 unsigned NumElts = VT.getVectorNumElements();
168 for (unsigned l = 0; l != NumElts; l += 8) {
169 unsigned NewImm = Imm;
170 for (unsigned i = 0, e = 4; i != e; ++i) {
171 ShuffleMask.push_back(l + (NewImm & 3));
174 for (unsigned i = 4, e = 8; i != e; ++i) {
175 ShuffleMask.push_back(l + i);
180 /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
181 /// the type of the vector allowing it to handle different datatypes and vector
183 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
184 unsigned NumElts = VT.getVectorNumElements();
186 unsigned NumLanes = VT.getSizeInBits() / 128;
187 unsigned NumLaneElts = NumElts / NumLanes;
189 unsigned NewImm = Imm;
190 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
191 // each half of a lane comes from different source
192 for (unsigned s = 0; s != NumElts*2; s += NumElts) {
193 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
194 ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
195 NewImm /= NumLaneElts;
198 if (NumLaneElts == 4) NewImm = Imm; // reload imm
202 /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
203 /// and punpckh*. VT indicates the type of the vector allowing it to handle
204 /// different datatypes and vector widths.
205 void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
206 unsigned NumElts = VT.getVectorNumElements();
208 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
209 // independently on 128-bit lanes.
210 unsigned NumLanes = VT.getSizeInBits() / 128;
211 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
212 unsigned NumLaneElts = NumElts / NumLanes;
214 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
215 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
216 ShuffleMask.push_back(i); // Reads from dest/src1
217 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
222 /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
223 /// and punpckl*. VT indicates the type of the vector allowing it to handle
224 /// different datatypes and vector widths.
225 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
226 unsigned NumElts = VT.getVectorNumElements();
228 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
229 // independently on 128-bit lanes.
230 unsigned NumLanes = VT.getSizeInBits() / 128;
231 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
232 unsigned NumLaneElts = NumElts / NumLanes;
234 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
235 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
236 ShuffleMask.push_back(i); // Reads from dest/src1
237 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
242 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
243 SmallVectorImpl<int> &ShuffleMask) {
245 return; // Not a shuffle
247 unsigned HalfSize = VT.getVectorNumElements()/2;
249 for (unsigned l = 0; l != 2; ++l) {
250 unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize;
251 for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i)
252 ShuffleMask.push_back(i);
256 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
257 Type *MaskTy = C->getType();
258 // It is not an error for the PSHUFB mask to not be a vector of i8 because the
259 // constant pool uniques constants by their bit representation.
260 // e.g. the following take up the same space in the constant pool:
261 // i128 -170141183420855150465331762880109871104
263 // <2 x i64> <i64 -9223372034707292160, i64 -9223372034707292160>
265 // <4 x i32> <i32 -2147483648, i32 -2147483648,
266 // i32 -2147483648, i32 -2147483648>
268 unsigned MaskTySize = MaskTy->getPrimitiveSizeInBits();
270 if (MaskTySize != 128 && MaskTySize != 256) // FIXME: Add support for AVX-512.
273 // This is a straightforward byte vector.
274 if (MaskTy->isVectorTy() && MaskTy->getVectorElementType()->isIntegerTy(8)) {
275 int NumElements = MaskTy->getVectorNumElements();
276 ShuffleMask.reserve(NumElements);
278 for (int i = 0; i < NumElements; ++i) {
279 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
280 // lane of the vector we're inside.
281 int Base = i < 16 ? 0 : 16;
282 Constant *COp = C->getAggregateElement(i);
286 } else if (isa<UndefValue>(COp)) {
287 ShuffleMask.push_back(SM_SentinelUndef);
290 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
291 // If the high bit (7) of the byte is set, the element is zeroed.
292 if (Element & (1 << 7))
293 ShuffleMask.push_back(SM_SentinelZero);
295 // Only the least significant 4 bits of the byte are used.
296 int Index = Base + (Element & 0xf);
297 ShuffleMask.push_back(Index);
301 // TODO: Handle funny-looking vectors too.
304 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
305 SmallVectorImpl<int> &ShuffleMask) {
306 for (int i = 0, e = RawMask.size(); i < e; ++i) {
307 uint64_t M = RawMask[i];
308 if (M == (uint64_t)SM_SentinelUndef) {
309 ShuffleMask.push_back(M);
312 // For AVX vectors with 32 bytes the base of the shuffle is the half of
313 // the vector we're inside.
314 int Base = i < 16 ? 0 : 16;
315 // If the high bit (7) of the byte is set, the element is zeroed.
317 ShuffleMask.push_back(SM_SentinelZero);
319 // Only the least significant 4 bits of the byte are used.
320 int Index = Base + (M & 0xf);
321 ShuffleMask.push_back(Index);
326 void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
327 int ElementBits = VT.getScalarSizeInBits();
328 int NumElements = VT.getVectorNumElements();
329 for (int i = 0; i < NumElements; ++i) {
330 // If there are more than 8 elements in the vector, then any immediate blend
331 // mask applies to each 128-bit lane. There can never be more than
332 // 8 elements in a 128-bit lane with an immediate blend.
333 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i;
335 "Immediate blends only operate over 8 elements at a time!");
336 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
340 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
341 /// No VT provided since it only works on 256-bit, 4 element vectors.
342 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
343 for (unsigned i = 0; i != 4; ++i) {
344 ShuffleMask.push_back((Imm >> (2*i)) & 3);
348 void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
349 Type *MaskTy = C->getType();
350 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
351 assert(MaskTy->getVectorElementType()->isIntegerTy() &&
352 "Expected integer constant mask elements!");
353 int ElementBits = MaskTy->getScalarSizeInBits();
354 int NumElements = MaskTy->getVectorNumElements();
355 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
356 "Unexpected number of vector elements.");
357 ShuffleMask.reserve(NumElements);
358 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
359 assert((unsigned)NumElements == CDS->getNumElements() &&
360 "Constant mask has a different number of elements!");
362 for (int i = 0; i < NumElements; ++i) {
363 int Base = (i * ElementBits / 128) * (128 / ElementBits);
364 uint64_t Element = CDS->getElementAsInteger(i);
365 // Only the least significant 2 bits of the integer are used.
366 int Index = Base + (Element & 0x3);
367 ShuffleMask.push_back(Index);
369 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
370 assert((unsigned)NumElements == C->getNumOperands() &&
371 "Constant mask has a different number of elements!");
373 for (int i = 0; i < NumElements; ++i) {
374 int Base = (i * ElementBits / 128) * (128 / ElementBits);
375 Constant *COp = CV->getOperand(i);
376 if (isa<UndefValue>(COp)) {
377 ShuffleMask.push_back(SM_SentinelUndef);
380 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
381 // Only the least significant 2 bits of the integer are used.
382 int Index = Base + (Element & 0x3);
383 ShuffleMask.push_back(Index);