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 DecodePALIGNRMask(MVT VT, unsigned Imm,
83 SmallVectorImpl<int> &ShuffleMask) {
84 unsigned NumElts = VT.getVectorNumElements();
85 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
87 unsigned NumLanes = VT.getSizeInBits() / 128;
88 unsigned NumLaneElts = NumElts / NumLanes;
90 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
91 for (unsigned i = 0; i != NumLaneElts; ++i) {
92 unsigned Base = i + Offset;
93 // if i+offset is out of this lane then we actually need the other source
94 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
95 ShuffleMask.push_back(Base + l);
100 /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*.
101 /// VT indicates the type of the vector allowing it to handle different
102 /// datatypes and vector widths.
103 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
104 unsigned NumElts = VT.getVectorNumElements();
106 unsigned NumLanes = VT.getSizeInBits() / 128;
107 unsigned NumLaneElts = NumElts / NumLanes;
109 unsigned NewImm = Imm;
110 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
111 for (unsigned i = 0; i != NumLaneElts; ++i) {
112 ShuffleMask.push_back(NewImm % NumLaneElts + l);
113 NewImm /= NumLaneElts;
115 if (NumLaneElts == 4) NewImm = Imm; // reload imm
119 void DecodePSHUFHWMask(MVT VT, unsigned Imm,
120 SmallVectorImpl<int> &ShuffleMask) {
121 unsigned NumElts = VT.getVectorNumElements();
123 for (unsigned l = 0; l != NumElts; l += 8) {
124 unsigned NewImm = Imm;
125 for (unsigned i = 0, e = 4; i != e; ++i) {
126 ShuffleMask.push_back(l + i);
128 for (unsigned i = 4, e = 8; i != e; ++i) {
129 ShuffleMask.push_back(l + 4 + (NewImm & 3));
135 void DecodePSHUFLWMask(MVT VT, unsigned Imm,
136 SmallVectorImpl<int> &ShuffleMask) {
137 unsigned NumElts = VT.getVectorNumElements();
139 for (unsigned l = 0; l != NumElts; l += 8) {
140 unsigned NewImm = Imm;
141 for (unsigned i = 0, e = 4; i != e; ++i) {
142 ShuffleMask.push_back(l + (NewImm & 3));
145 for (unsigned i = 4, e = 8; i != e; ++i) {
146 ShuffleMask.push_back(l + i);
151 /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
152 /// the type of the vector allowing it to handle different datatypes and vector
154 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
155 unsigned NumElts = VT.getVectorNumElements();
157 unsigned NumLanes = VT.getSizeInBits() / 128;
158 unsigned NumLaneElts = NumElts / NumLanes;
160 unsigned NewImm = Imm;
161 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
162 // each half of a lane comes from different source
163 for (unsigned s = 0; s != NumElts*2; s += NumElts) {
164 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
165 ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
166 NewImm /= NumLaneElts;
169 if (NumLaneElts == 4) NewImm = Imm; // reload imm
173 /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
174 /// and punpckh*. VT indicates the type of the vector allowing it to handle
175 /// different datatypes and vector widths.
176 void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
177 unsigned NumElts = VT.getVectorNumElements();
179 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
180 // independently on 128-bit lanes.
181 unsigned NumLanes = VT.getSizeInBits() / 128;
182 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
183 unsigned NumLaneElts = NumElts / NumLanes;
185 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
186 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
187 ShuffleMask.push_back(i); // Reads from dest/src1
188 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
193 /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
194 /// and punpckl*. VT indicates the type of the vector allowing it to handle
195 /// different datatypes and vector widths.
196 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
197 unsigned NumElts = VT.getVectorNumElements();
199 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
200 // independently on 128-bit lanes.
201 unsigned NumLanes = VT.getSizeInBits() / 128;
202 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
203 unsigned NumLaneElts = NumElts / NumLanes;
205 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
206 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
207 ShuffleMask.push_back(i); // Reads from dest/src1
208 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
213 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
214 SmallVectorImpl<int> &ShuffleMask) {
216 return; // Not a shuffle
218 unsigned HalfSize = VT.getVectorNumElements()/2;
220 for (unsigned l = 0; l != 2; ++l) {
221 unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize;
222 for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i)
223 ShuffleMask.push_back(i);
227 void DecodePSHUFBMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
228 Type *MaskTy = C->getType();
229 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
230 assert(MaskTy->getVectorElementType()->isIntegerTy(8) &&
231 "Expected i8 constant mask elements!");
232 int NumElements = MaskTy->getVectorNumElements();
233 // FIXME: Add support for AVX-512.
234 assert((NumElements == 16 || NumElements == 32) &&
235 "Only 128-bit and 256-bit vectors supported!");
236 ShuffleMask.reserve(NumElements);
238 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
239 assert((unsigned)NumElements == CDS->getNumElements() &&
240 "Constant mask has a different number of elements!");
242 for (int i = 0; i < NumElements; ++i) {
243 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
244 // lane of the vector we're inside.
245 int Base = i < 16 ? 0 : 16;
246 uint64_t Element = CDS->getElementAsInteger(i);
247 // If the high bit (7) of the byte is set, the element is zeroed.
248 if (Element & (1 << 7))
249 ShuffleMask.push_back(SM_SentinelZero);
251 // Only the least significant 4 bits of the byte are used.
252 int Index = Base + (Element & 0xf);
253 ShuffleMask.push_back(Index);
256 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
257 assert((unsigned)NumElements == CV->getNumOperands() &&
258 "Constant mask has a different number of elements!");
260 for (int i = 0; i < NumElements; ++i) {
261 // For AVX vectors with 32 bytes the base of the shuffle is the 16-byte
262 // lane of the vector we're inside.
263 int Base = i < 16 ? 0 : 16;
264 Constant *COp = CV->getOperand(i);
265 if (isa<UndefValue>(COp)) {
266 ShuffleMask.push_back(SM_SentinelUndef);
269 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
270 // If the high bit (7) of the byte is set, the element is zeroed.
271 if (Element & (1 << 7))
272 ShuffleMask.push_back(SM_SentinelZero);
274 // Only the least significant 4 bits of the byte are used.
275 int Index = Base + (Element & 0xf);
276 ShuffleMask.push_back(Index);
282 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
283 SmallVectorImpl<int> &ShuffleMask) {
284 for (int i = 0, e = RawMask.size(); i < e; ++i) {
285 uint64_t M = RawMask[i];
286 if (M == (uint64_t)SM_SentinelUndef) {
287 ShuffleMask.push_back(M);
290 // For AVX vectors with 32 bytes the base of the shuffle is the half of
291 // the vector we're inside.
292 int Base = i < 16 ? 0 : 16;
293 // If the high bit (7) of the byte is set, the element is zeroed.
295 ShuffleMask.push_back(SM_SentinelZero);
297 // Only the least significant 4 bits of the byte are used.
298 int Index = Base + (M & 0xf);
299 ShuffleMask.push_back(Index);
304 void DecodeBLENDMask(MVT VT, unsigned Imm,
305 SmallVectorImpl<int> &ShuffleMask) {
306 int NumElements = VT.getVectorNumElements();
307 for (int i = 0; i < NumElements; ++i)
308 ShuffleMask.push_back(((Imm >> i) & 1) ? NumElements + i : i);
311 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
312 /// No VT provided since it only works on 256-bit, 4 element vectors.
313 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
314 for (unsigned i = 0; i != 4; ++i) {
315 ShuffleMask.push_back((Imm >> (2*i)) & 3);
319 void DecodeVPERMILPMask(const Constant *C, SmallVectorImpl<int> &ShuffleMask) {
320 Type *MaskTy = C->getType();
321 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
322 assert(MaskTy->getVectorElementType()->isIntegerTy() &&
323 "Expected integer constant mask elements!");
324 int ElementBits = MaskTy->getScalarSizeInBits();
325 int NumElements = MaskTy->getVectorNumElements();
326 assert((NumElements == 2 || NumElements == 4 || NumElements == 8) &&
327 "Unexpected number of vector elements.");
328 ShuffleMask.reserve(NumElements);
329 if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) {
330 assert((unsigned)NumElements == CDS->getNumElements() &&
331 "Constant mask has a different number of elements!");
333 for (int i = 0; i < NumElements; ++i) {
334 int Base = (i * ElementBits / 128) * (128 / ElementBits);
335 uint64_t Element = CDS->getElementAsInteger(i);
336 // Only the least significant 2 bits of the integer are used.
337 int Index = Base + (Element & 0x3);
338 ShuffleMask.push_back(Index);
340 } else if (auto *CV = dyn_cast<ConstantVector>(C)) {
341 assert((unsigned)NumElements == C->getNumOperands() &&
342 "Constant mask has a different number of elements!");
344 for (int i = 0; i < NumElements; ++i) {
345 int Base = (i * ElementBits / 128) * (128 / ElementBits);
346 Constant *COp = CV->getOperand(i);
347 if (isa<UndefValue>(COp)) {
348 ShuffleMask.push_back(SM_SentinelUndef);
351 uint64_t Element = cast<ConstantInt>(COp)->getZExtValue();
352 // Only the least significant 2 bits of the integer are used.
353 int Index = Base + (Element & 0x3);
354 ShuffleMask.push_back(Index);