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 DecodePALIGNRMask(MVT VT, unsigned Imm,
67 SmallVectorImpl<int> &ShuffleMask) {
68 unsigned NumElts = VT.getVectorNumElements();
69 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
71 unsigned NumLanes = VT.getSizeInBits() / 128;
72 unsigned NumLaneElts = NumElts / NumLanes;
74 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
75 for (unsigned i = 0; i != NumLaneElts; ++i) {
76 unsigned Base = i + Offset;
77 // if i+offset is out of this lane then we actually need the other source
78 if (Base >= NumLaneElts) Base += NumElts - NumLaneElts;
79 ShuffleMask.push_back(Base + l);
84 /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*.
85 /// VT indicates the type of the vector allowing it to handle different
86 /// datatypes and vector widths.
87 void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
88 unsigned NumElts = VT.getVectorNumElements();
90 unsigned NumLanes = VT.getSizeInBits() / 128;
91 unsigned NumLaneElts = NumElts / NumLanes;
93 unsigned NewImm = Imm;
94 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
95 for (unsigned i = 0; i != NumLaneElts; ++i) {
96 ShuffleMask.push_back(NewImm % NumLaneElts + l);
97 NewImm /= NumLaneElts;
99 if (NumLaneElts == 4) NewImm = Imm; // reload imm
103 void DecodePSHUFHWMask(MVT VT, unsigned Imm,
104 SmallVectorImpl<int> &ShuffleMask) {
105 unsigned NumElts = VT.getVectorNumElements();
107 for (unsigned l = 0; l != NumElts; l += 8) {
108 unsigned NewImm = Imm;
109 for (unsigned i = 0, e = 4; i != e; ++i) {
110 ShuffleMask.push_back(l + i);
112 for (unsigned i = 4, e = 8; i != e; ++i) {
113 ShuffleMask.push_back(l + 4 + (NewImm & 3));
119 void DecodePSHUFLWMask(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 + (NewImm & 3));
129 for (unsigned i = 4, e = 8; i != e; ++i) {
130 ShuffleMask.push_back(l + i);
135 /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
136 /// the type of the vector allowing it to handle different datatypes and vector
138 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
139 unsigned NumElts = VT.getVectorNumElements();
141 unsigned NumLanes = VT.getSizeInBits() / 128;
142 unsigned NumLaneElts = NumElts / NumLanes;
144 unsigned NewImm = Imm;
145 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
146 // each half of a lane comes from different source
147 for (unsigned s = 0; s != NumElts*2; s += NumElts) {
148 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
149 ShuffleMask.push_back(NewImm % NumLaneElts + s + l);
150 NewImm /= NumLaneElts;
153 if (NumLaneElts == 4) NewImm = Imm; // reload imm
157 /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
158 /// and punpckh*. VT indicates the type of the vector allowing it to handle
159 /// different datatypes and vector widths.
160 void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
161 unsigned NumElts = VT.getVectorNumElements();
163 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
164 // independently on 128-bit lanes.
165 unsigned NumLanes = VT.getSizeInBits() / 128;
166 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
167 unsigned NumLaneElts = NumElts / NumLanes;
169 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
170 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
171 ShuffleMask.push_back(i); // Reads from dest/src1
172 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
177 /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
178 /// and punpckl*. VT indicates the type of the vector allowing it to handle
179 /// different datatypes and vector widths.
180 void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
181 unsigned NumElts = VT.getVectorNumElements();
183 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
184 // independently on 128-bit lanes.
185 unsigned NumLanes = VT.getSizeInBits() / 128;
186 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
187 unsigned NumLaneElts = NumElts / NumLanes;
189 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
190 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
191 ShuffleMask.push_back(i); // Reads from dest/src1
192 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
197 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
198 SmallVectorImpl<int> &ShuffleMask) {
200 return; // Not a shuffle
202 unsigned HalfSize = VT.getVectorNumElements()/2;
204 for (unsigned l = 0; l != 2; ++l) {
205 unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize;
206 for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i)
207 ShuffleMask.push_back(i);
211 void DecodePSHUFBMask(const ConstantDataSequential *C,
212 SmallVectorImpl<int> &ShuffleMask) {
213 Type *MaskTy = C->getType();
214 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
215 assert(MaskTy->getVectorElementType()->isIntegerTy(8) &&
216 "Expected i8 constant mask elements!");
217 int NumElements = MaskTy->getVectorNumElements();
218 // FIXME: Add support for AVX-512.
219 assert((NumElements == 16 || NumElements == 32) &&
220 "Only 128-bit and 256-bit vectors supported!");
221 assert((unsigned)NumElements == C->getNumElements() &&
222 "Constant mask has a different number of elements!");
224 ShuffleMask.reserve(NumElements);
225 for (int i = 0; i < NumElements; ++i) {
226 // For AVX vectors with 32 bytes the base of the shuffle is the half of the
227 // vector we're inside.
228 int Base = i < 16 ? 0 : 16;
229 uint64_t Element = C->getElementAsInteger(i);
230 // If the high bit (7) of the byte is set, the element is zeroed.
231 if (Element & (1 << 7))
232 ShuffleMask.push_back(SM_SentinelZero);
234 int Index = Base + Element;
235 assert((Index >= 0 && Index < NumElements) &&
236 "Out of bounds shuffle index for pshub instruction!");
237 ShuffleMask.push_back(Index);
242 void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
243 SmallVectorImpl<int> &ShuffleMask) {
244 for (int i = 0, e = RawMask.size(); i < e; ++i) {
245 uint64_t M = RawMask[i];
246 // For AVX vectors with 32 bytes the base of the shuffle is the half of
247 // the vector we're inside.
248 int Base = i < 16 ? 0 : 16;
249 // If the high bit (7) of the byte is set, the element is zeroed.
251 ShuffleMask.push_back(SM_SentinelZero);
253 int Index = Base + M;
254 assert((Index >= 0 && (unsigned)Index < RawMask.size()) &&
255 "Out of bounds shuffle index for pshub instruction!");
256 ShuffleMask.push_back(Index);
261 void DecodeBLENDMask(MVT VT, unsigned Imm,
262 SmallVectorImpl<int> &ShuffleMask) {
263 int NumElements = VT.getVectorNumElements();
264 for (int i = 0; i < NumElements; ++i)
265 ShuffleMask.push_back(((Imm >> i) & 1) ? NumElements + i : i);
268 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
269 /// No VT provided since it only works on 256-bit, 4 element vectors.
270 void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
271 for (unsigned i = 0; i != 4; ++i) {
272 ShuffleMask.push_back((Imm >> (2*i)) & 3);