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 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
259 assert(MaskTy->getVectorElementType()->isIntegerTy(8) &&
260 "Expected i8 constant mask elements!");
261 int NumElements = MaskTy->getVectorNumElements();
262 // FIXME: Add support for AVX-512.
263 assert((NumElements == 16 || NumElements == 32) &&
264 "Only 128-bit and 256-bit vectors supported!");
265 ShuffleMask.reserve(NumElements);
267 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
268 assert((unsigned)NumElements == CDS->getNumElements() &&
269 "Constant mask has a different number of elements!");
271 for (int i = 0; i < NumElements; ++i) {
272 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
273 // lane of the vector we're inside.
274 int Base = i < 16 ? 0 : 16;
275 uint64_t Element = CDS->getElementAsInteger(i);
276 // If the high bit (7) of the byte is set, the element is zeroed.
277 if (Element & (1 << 7))
278 ShuffleMask.push_back(SM_SentinelZero);
280 // Only the least significant 4 bits of the byte are used.
281 int Index = Base + (Element & 0xf);
282 ShuffleMask.push_back(Index);
285 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
286 assert((unsigned)NumElements == CV->getNumOperands() &&
287 "Constant mask has a different number of elements!");
289 for (int i = 0; i < NumElements; ++i) {
290 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
291 // lane of the vector we're inside.
292 int Base = i < 16 ? 0 : 16;
293 Constant *COp = CV->getOperand(i);
294 if (isa<UndefValue>(COp)) {
295 ShuffleMask.push_back(SM_SentinelUndef);
298 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
299 // If the high bit (7) of the byte is set, the element is zeroed.
300 if (Element & (1 << 7))
301 ShuffleMask.push_back(SM_SentinelZero);
303 // Only the least significant 4 bits of the byte are used.
304 int Index = Base + (Element & 0xf);
305 ShuffleMask.push_back(Index);
311 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
312 SmallVectorImpl<int> &ShuffleMask) {
313 for (int i = 0, e = RawMask.size(); i < e; ++i) {
314 uint64_t M = RawMask[i];
315 if (M == (uint64_t)SM_SentinelUndef) {
316 ShuffleMask.push_back(M);
319 // For AVX vectors with 32 bytes the base of the shuffle is the half of
320 // the vector we're inside.
321 int Base = i < 16 ? 0 : 16;
322 // If the high bit (7) of the byte is set, the element is zeroed.
324 ShuffleMask.push_back(SM_SentinelZero);
326 // Only the least significant 4 bits of the byte are used.
327 int Index = Base + (M & 0xf);
328 ShuffleMask.push_back(Index);
333 void DecodeBLENDMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
334 int ElementBits = VT.getScalarSizeInBits();
335 int NumElements = VT.getVectorNumElements();
336 for (int i = 0; i < NumElements; ++i) {
337 // If there are more than 8 elements in the vector, then any immediate blend
338 // mask applies to each 128-bit lane. There can never be more than
339 // 8 elements in a 128-bit lane with an immediate blend.
340 int Bit = NumElements > 8 ? i % (128 / ElementBits) : i;
342 "Immediate blends only operate over 8 elements at a time!");
343 ShuffleMask.push_back(((Imm >> Bit) & 1) ? NumElements + i : i);
347 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
348 /// No VT provided since it only works on 256-bit, 4 element vectors.
349 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
350 for (unsigned i = 0; i != 4; ++i) {
351 ShuffleMask.push_back((Imm >> (2*i)) & 3);
355 void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
356 Type *MaskTy = C->getType();
357 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
358 assert(MaskTy->getVectorElementType()->isIntegerTy() &&
359 "Expected integer constant mask elements!");
360 int ElementBits = MaskTy->getScalarSizeInBits();
361 int NumElements = MaskTy->getVectorNumElements();
362 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
363 "Unexpected number of vector elements.");
364 ShuffleMask.reserve(NumElements);
365 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
366 assert((unsigned)NumElements == CDS->getNumElements() &&
367 "Constant mask has a different number of elements!");
369 for (int i = 0; i < NumElements; ++i) {
370 int Base = (i * ElementBits / 128) * (128 / ElementBits);
371 uint64_t Element = CDS->getElementAsInteger(i);
372 // Only the least significant 2 bits of the integer are used.
373 int Index = Base + (Element & 0x3);
374 ShuffleMask.push_back(Index);
376 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
377 assert((unsigned)NumElements == C->getNumOperands() &&
378 "Constant mask has a different number of elements!");
380 for (int i = 0; i < NumElements; ++i) {
381 int Base = (i * ElementBits / 128) * (128 / ElementBits);
382 Constant *COp = CV->getOperand(i);
383 if (isa<UndefValue>(COp)) {
384 ShuffleMask.push_back(SM_SentinelUndef);
387 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
388 // Only the least significant 2 bits of the integer are used.
389 int Index = Base + (Element & 0x3);
390 ShuffleMask.push_back(Index);