blob: 51d251e551c33211f602d8562babc8867fe87d67 [file] [log] [blame]
David Greene20a1cbe2011-02-28 19:06:56 +00001//===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===//
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Define several functions to decode x86 specific shuffle semantics into a
11// generic vector mask.
12//
13//===----------------------------------------------------------------------===//
14
David Greene3a2b5082011-02-17 19:18:59 +000015#include "X86ShuffleDecode.h"
Chandler Carruth185cc182014-07-25 23:47:11 +000016#include "llvm/IR/Constants.h"
Patrik Hagglund8d09a6c2014-03-15 09:11:41 +000017#include "llvm/CodeGen/MachineValueType.h"
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000018
19//===----------------------------------------------------------------------===//
20// Vector Mask Decoding
21//===----------------------------------------------------------------------===//
22
David Greene3a2b5082011-02-17 19:18:59 +000023namespace llvm {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000024
Craig Toppercbc96a62012-03-20 06:42:26 +000025void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopes02a05a62010-09-02 22:43:39 +000026 // 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);
31
32 // Decode the immediate.
33 unsigned ZMask = Imm & 15;
34 unsigned CountD = (Imm >> 4) & 3;
35 unsigned CountS = (Imm >> 6) & 3;
36
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;
46}
47
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000048// <3,1> or <6,7,2,3>
Craig Toppercbc96a62012-03-20 06:42:26 +000049void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000050 for (unsigned i = NElts/2; i != NElts; ++i)
51 ShuffleMask.push_back(NElts+i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000052
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000053 for (unsigned i = NElts/2; i != NElts; ++i)
54 ShuffleMask.push_back(i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000055}
56
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000057// <0,2> or <0,1,4,5>
Craig Toppercbc96a62012-03-20 06:42:26 +000058void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000059 for (unsigned i = 0; i != NElts/2; ++i)
60 ShuffleMask.push_back(i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000061
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000062 for (unsigned i = 0; i != NElts/2; ++i)
63 ShuffleMask.push_back(NElts+i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000064}
65
Chandler Carruth41a25dd2014-09-15 11:15:23 +000066void 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);
71 }
72}
73
74void 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);
79 }
80}
81
Craig Topper8fb09f02013-01-28 06:48:25 +000082void DecodePALIGNRMask(MVT VT, unsigned Imm,
83 SmallVectorImpl<int> &ShuffleMask) {
Benjamin Kramer6a935962013-01-26 13:31:37 +000084 unsigned NumElts = VT.getVectorNumElements();
85 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
86
Craig Topper5c683972013-01-28 07:41:18 +000087 unsigned NumLanes = VT.getSizeInBits() / 128;
88 unsigned NumLaneElts = NumElts / NumLanes;
89
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);
96 }
97 }
Benjamin Kramer6a935962013-01-26 13:31:37 +000098}
99
Craig Topper1f710572012-02-06 07:17:51 +0000100/// 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.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000103void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topper1f710572012-02-06 07:17:51 +0000104 unsigned NumElts = VT.getVectorNumElements();
105
106 unsigned NumLanes = VT.getSizeInBits() / 128;
107 unsigned NumLaneElts = NumElts / NumLanes;
108
Craig Topperc73bc392012-05-02 08:03:44 +0000109 unsigned NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +0000110 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;
114 }
115 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000116 }
117}
118
Craig Topper00a1e6d2012-05-06 19:46:21 +0000119void DecodePSHUFHWMask(MVT VT, unsigned Imm,
Craig Topperc73bc392012-05-02 08:03:44 +0000120 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper315a5cc2012-05-03 07:12:59 +0000121 unsigned NumElts = VT.getVectorNumElements();
Craig Topperc73bc392012-05-02 08:03:44 +0000122
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);
127 }
128 for (unsigned i = 4, e = 8; i != e; ++i) {
129 ShuffleMask.push_back(l + 4 + (NewImm & 3));
130 NewImm >>= 2;
131 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000132 }
133}
134
Craig Topper00a1e6d2012-05-06 19:46:21 +0000135void DecodePSHUFLWMask(MVT VT, unsigned Imm,
Craig Topperc73bc392012-05-02 08:03:44 +0000136 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper315a5cc2012-05-03 07:12:59 +0000137 unsigned NumElts = VT.getVectorNumElements();
Craig Topperc73bc392012-05-02 08:03:44 +0000138
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));
143 NewImm >>= 2;
144 }
145 for (unsigned i = 4, e = 8; i != e; ++i) {
146 ShuffleMask.push_back(l + i);
147 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000148 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000149}
150
Craig Topper1f710572012-02-06 07:17:51 +0000151/// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
152/// the type of the vector allowing it to handle different datatypes and vector
153/// widths.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000154void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc16db842011-11-29 07:49:05 +0000155 unsigned NumElts = VT.getVectorNumElements();
156
157 unsigned NumLanes = VT.getSizeInBits() / 128;
158 unsigned NumLaneElts = NumElts / NumLanes;
159
Craig Topperc73bc392012-05-02 08:03:44 +0000160 unsigned NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +0000161 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
Craig Topper00a1e6d2012-05-06 19:46:21 +0000162 // 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;
167 }
Craig Topperc16db842011-11-29 07:49:05 +0000168 }
169 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000170 }
171}
172
Craig Topper1f710572012-02-06 07:17:51 +0000173/// 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.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000176void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperccb70972011-11-22 01:57:35 +0000177 unsigned NumElts = VT.getVectorNumElements();
178
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;
184
Craig Topper1f710572012-02-06 07:17:51 +0000185 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
186 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000187 ShuffleMask.push_back(i); // Reads from dest/src1
188 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
189 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000190 }
191}
192
Craig Topper3cb802c2011-12-06 05:31:16 +0000193/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
Craig Topper1f710572012-02-06 07:17:51 +0000194/// and punpckl*. VT indicates the type of the vector allowing it to handle
195/// different datatypes and vector widths.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000196void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
David Greenedd567b22011-03-02 17:23:43 +0000197 unsigned NumElts = VT.getVectorNumElements();
David Greene20a1cbe2011-02-28 19:06:56 +0000198
Bruno Cardoso Lopesf8fe47b2011-07-26 22:03:40 +0000199 // 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;
David Greene20a1cbe2011-02-28 19:06:56 +0000204
Craig Topper1f710572012-02-06 07:17:51 +0000205 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
206 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000207 ShuffleMask.push_back(i); // Reads from dest/src1
208 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
David Greenedd567b22011-03-02 17:23:43 +0000209 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000210 }
211}
212
Craig Topper00a1e6d2012-05-06 19:46:21 +0000213void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
Craig Toppercbc96a62012-03-20 06:42:26 +0000214 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper354103d2012-04-17 05:54:54 +0000215 if (Imm & 0x88)
216 return; // Not a shuffle
217
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000218 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000219
Craig Topper00a1e6d2012-05-06 19:46:21 +0000220 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);
224 }
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000225}
226
Chandler Carruth185cc182014-07-25 23:47:11 +0000227void DecodePSHUFBMask(const ConstantDataSequential *C,
228 SmallVectorImpl<int> &ShuffleMask) {
229 Type *MaskTy = C->getType();
230 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
NAKAMURA Takumi1fa77692014-07-26 04:53:05 +0000231 assert(MaskTy->getVectorElementType()->isIntegerTy(8) &&
232 "Expected i8 constant mask elements!");
Chandler Carruth185cc182014-07-25 23:47:11 +0000233 int NumElements = MaskTy->getVectorNumElements();
234 // FIXME: Add support for AVX-512.
235 assert((NumElements == 16 || NumElements == 32) &&
236 "Only 128-bit and 256-bit vectors supported!");
237 assert((unsigned)NumElements == C->getNumElements() &&
238 "Constant mask has a different number of elements!");
239
240 ShuffleMask.reserve(NumElements);
241 for (int i = 0; i < NumElements; ++i) {
242 // For AVX vectors with 32 bytes the base of the shuffle is the half of the
243 // vector we're inside.
244 int Base = i < 16 ? 0 : 16;
245 uint64_t Element = C->getElementAsInteger(i);
246 // If the high bit (7) of the byte is set, the element is zeroed.
247 if (Element & (1 << 7))
248 ShuffleMask.push_back(SM_SentinelZero);
249 else {
250 int Index = Base + Element;
Nick Lewyckyd7c726c2014-07-26 05:44:15 +0000251 assert((Index >= 0 && Index < NumElements) &&
Chandler Carruth185cc182014-07-25 23:47:11 +0000252 "Out of bounds shuffle index for pshub instruction!");
253 ShuffleMask.push_back(Index);
254 }
255 }
256}
257
Chandler Carruth4c579552014-08-02 10:39:15 +0000258void DecodePSHUFBMask(ArrayRef<uint64_t> RawMask,
259 SmallVectorImpl<int> &ShuffleMask) {
260 for (int i = 0, e = RawMask.size(); i < e; ++i) {
261 uint64_t M = RawMask[i];
262 // For AVX vectors with 32 bytes the base of the shuffle is the half of
263 // the vector we're inside.
264 int Base = i < 16 ? 0 : 16;
265 // If the high bit (7) of the byte is set, the element is zeroed.
266 if (M & (1 << 7))
267 ShuffleMask.push_back(SM_SentinelZero);
268 else {
269 int Index = Base + M;
270 assert((Index >= 0 && (unsigned)Index < RawMask.size()) &&
271 "Out of bounds shuffle index for pshub instruction!");
272 ShuffleMask.push_back(Index);
273 }
274 }
275}
276
Chandler Carruth02886202014-08-15 11:01:37 +0000277void DecodeBLENDMask(MVT VT, unsigned Imm,
278 SmallVectorImpl<int> &ShuffleMask) {
279 int NumElements = VT.getVectorNumElements();
280 for (int i = 0; i < NumElements; ++i)
281 ShuffleMask.push_back(((Imm >> i) & 1) ? NumElements + i : i);
282}
283
Craig Topper54bdb352012-05-06 18:44:02 +0000284/// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
285/// No VT provided since it only works on 256-bit, 4 element vectors.
286void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
287 for (unsigned i = 0; i != 4; ++i) {
288 ShuffleMask.push_back((Imm >> (2*i)) & 3);
289 }
290}
291
David Greene3a2b5082011-02-17 19:18:59 +0000292} // llvm namespace