blob: 869ef0ee43e4ed1bf3901a1ccdf05e2b4bdac8af [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
Craig Topper8fb09f02013-01-28 06:48:25 +000066void DecodePALIGNRMask(MVT VT, unsigned Imm,
67 SmallVectorImpl<int> &ShuffleMask) {
Benjamin Kramer6a935962013-01-26 13:31:37 +000068 unsigned NumElts = VT.getVectorNumElements();
69 unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
70
Craig Topper5c683972013-01-28 07:41:18 +000071 unsigned NumLanes = VT.getSizeInBits() / 128;
72 unsigned NumLaneElts = NumElts / NumLanes;
73
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);
80 }
81 }
Benjamin Kramer6a935962013-01-26 13:31:37 +000082}
83
Craig Topper1f710572012-02-06 07:17:51 +000084/// 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.
Craig Topper00a1e6d2012-05-06 19:46:21 +000087void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topper1f710572012-02-06 07:17:51 +000088 unsigned NumElts = VT.getVectorNumElements();
89
90 unsigned NumLanes = VT.getSizeInBits() / 128;
91 unsigned NumLaneElts = NumElts / NumLanes;
92
Craig Topperc73bc392012-05-02 08:03:44 +000093 unsigned NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +000094 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;
98 }
99 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000100 }
101}
102
Craig Topper00a1e6d2012-05-06 19:46:21 +0000103void DecodePSHUFHWMask(MVT VT, unsigned Imm,
Craig Topperc73bc392012-05-02 08:03:44 +0000104 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper315a5cc2012-05-03 07:12:59 +0000105 unsigned NumElts = VT.getVectorNumElements();
Craig Topperc73bc392012-05-02 08:03:44 +0000106
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);
111 }
112 for (unsigned i = 4, e = 8; i != e; ++i) {
113 ShuffleMask.push_back(l + 4 + (NewImm & 3));
114 NewImm >>= 2;
115 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000116 }
117}
118
Craig Topper00a1e6d2012-05-06 19:46:21 +0000119void DecodePSHUFLWMask(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 + (NewImm & 3));
127 NewImm >>= 2;
128 }
129 for (unsigned i = 4, e = 8; i != e; ++i) {
130 ShuffleMask.push_back(l + i);
131 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000132 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000133}
134
Craig Topper1f710572012-02-06 07:17:51 +0000135/// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
136/// the type of the vector allowing it to handle different datatypes and vector
137/// widths.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000138void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc16db842011-11-29 07:49:05 +0000139 unsigned NumElts = VT.getVectorNumElements();
140
141 unsigned NumLanes = VT.getSizeInBits() / 128;
142 unsigned NumLaneElts = NumElts / NumLanes;
143
Craig Topperc73bc392012-05-02 08:03:44 +0000144 unsigned NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +0000145 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
Craig Topper00a1e6d2012-05-06 19:46:21 +0000146 // 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;
151 }
Craig Topperc16db842011-11-29 07:49:05 +0000152 }
153 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000154 }
155}
156
Craig Topper1f710572012-02-06 07:17:51 +0000157/// 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.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000160void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperccb70972011-11-22 01:57:35 +0000161 unsigned NumElts = VT.getVectorNumElements();
162
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;
168
Craig Topper1f710572012-02-06 07:17:51 +0000169 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
170 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000171 ShuffleMask.push_back(i); // Reads from dest/src1
172 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
173 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000174 }
175}
176
Craig Topper3cb802c2011-12-06 05:31:16 +0000177/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
Craig Topper1f710572012-02-06 07:17:51 +0000178/// and punpckl*. VT indicates the type of the vector allowing it to handle
179/// different datatypes and vector widths.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000180void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
David Greenedd567b22011-03-02 17:23:43 +0000181 unsigned NumElts = VT.getVectorNumElements();
David Greene20a1cbe2011-02-28 19:06:56 +0000182
Bruno Cardoso Lopesf8fe47b2011-07-26 22:03:40 +0000183 // 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;
David Greene20a1cbe2011-02-28 19:06:56 +0000188
Craig Topper1f710572012-02-06 07:17:51 +0000189 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
190 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000191 ShuffleMask.push_back(i); // Reads from dest/src1
192 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
David Greenedd567b22011-03-02 17:23:43 +0000193 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000194 }
195}
196
Craig Topper00a1e6d2012-05-06 19:46:21 +0000197void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
Craig Toppercbc96a62012-03-20 06:42:26 +0000198 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper354103d2012-04-17 05:54:54 +0000199 if (Imm & 0x88)
200 return; // Not a shuffle
201
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000202 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000203
Craig Topper00a1e6d2012-05-06 19:46:21 +0000204 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);
208 }
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000209}
210
Chandler Carruth185cc182014-07-25 23:47:11 +0000211/// \brief Decode PSHUFB masks stored in an LLVM Constant.
212void DecodePSHUFBMask(const ConstantDataSequential *C,
213 SmallVectorImpl<int> &ShuffleMask) {
214 Type *MaskTy = C->getType();
215 assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
216 Type *EltTy = MaskTy->getVectorElementType();
217 assert(EltTy->isIntegerTy(8) && "Expected i8 constant mask elements!");
218 int NumElements = MaskTy->getVectorNumElements();
219 // FIXME: Add support for AVX-512.
220 assert((NumElements == 16 || NumElements == 32) &&
221 "Only 128-bit and 256-bit vectors supported!");
222 assert((unsigned)NumElements == C->getNumElements() &&
223 "Constant mask has a different number of elements!");
224
225 ShuffleMask.reserve(NumElements);
226 for (int i = 0; i < NumElements; ++i) {
227 // For AVX vectors with 32 bytes the base of the shuffle is the half of the
228 // vector we're inside.
229 int Base = i < 16 ? 0 : 16;
230 uint64_t Element = C->getElementAsInteger(i);
231 // If the high bit (7) of the byte is set, the element is zeroed.
232 if (Element & (1 << 7))
233 ShuffleMask.push_back(SM_SentinelZero);
234 else {
235 int Index = Base + Element;
236 assert((Index >= 0 && Index < NumElements) ||
237 "Out of bounds shuffle index for pshub instruction!");
238 ShuffleMask.push_back(Index);
239 }
240 }
241}
242
Craig Topper54bdb352012-05-06 18:44:02 +0000243/// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
244/// No VT provided since it only works on 256-bit, 4 element vectors.
245void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
246 for (unsigned i = 0; i != 4; ++i) {
247 ShuffleMask.push_back((Imm >> (2*i)) & 3);
248 }
249}
250
David Greene3a2b5082011-02-17 19:18:59 +0000251} // llvm namespace