blob: cf1c55e0f7ee4d2228eb0b7eaa1dee2813fb1974 [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"
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000016
17//===----------------------------------------------------------------------===//
18// Vector Mask Decoding
19//===----------------------------------------------------------------------===//
20
David Greene3a2b5082011-02-17 19:18:59 +000021namespace llvm {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000022
Bruno Cardoso Lopes02a05a62010-09-02 22:43:39 +000023void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<unsigned> &ShuffleMask) {
24 // Defaults the copying the dest value.
25 ShuffleMask.push_back(0);
26 ShuffleMask.push_back(1);
27 ShuffleMask.push_back(2);
28 ShuffleMask.push_back(3);
29
30 // Decode the immediate.
31 unsigned ZMask = Imm & 15;
32 unsigned CountD = (Imm >> 4) & 3;
33 unsigned CountS = (Imm >> 6) & 3;
34
35 // CountS selects which input element to use.
36 unsigned InVal = 4+CountS;
37 // CountD specifies which element of destination to update.
38 ShuffleMask[CountD] = InVal;
39 // ZMask zaps values, potentially overriding the CountD elt.
40 if (ZMask & 1) ShuffleMask[0] = SM_SentinelZero;
41 if (ZMask & 2) ShuffleMask[1] = SM_SentinelZero;
42 if (ZMask & 4) ShuffleMask[2] = SM_SentinelZero;
43 if (ZMask & 8) ShuffleMask[3] = SM_SentinelZero;
44}
45
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000046// <3,1> or <6,7,2,3>
David Greene3a2b5082011-02-17 19:18:59 +000047void DecodeMOVHLPSMask(unsigned NElts,
48 SmallVectorImpl<unsigned> &ShuffleMask) {
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000049 for (unsigned i = NElts/2; i != NElts; ++i)
50 ShuffleMask.push_back(NElts+i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000051
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000052 for (unsigned i = NElts/2; i != NElts; ++i)
53 ShuffleMask.push_back(i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000054}
55
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000056// <0,2> or <0,1,4,5>
David Greene3a2b5082011-02-17 19:18:59 +000057void DecodeMOVLHPSMask(unsigned NElts,
58 SmallVectorImpl<unsigned> &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
David Greene3a2b5082011-02-17 19:18:59 +000066void DecodePSHUFMask(unsigned NElts, unsigned Imm,
67 SmallVectorImpl<unsigned> &ShuffleMask) {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000068 for (unsigned i = 0; i != NElts; ++i) {
69 ShuffleMask.push_back(Imm % NElts);
70 Imm /= NElts;
71 }
72}
73
David Greene3a2b5082011-02-17 19:18:59 +000074void DecodePSHUFHWMask(unsigned Imm,
75 SmallVectorImpl<unsigned> &ShuffleMask) {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000076 ShuffleMask.push_back(0);
77 ShuffleMask.push_back(1);
78 ShuffleMask.push_back(2);
79 ShuffleMask.push_back(3);
80 for (unsigned i = 0; i != 4; ++i) {
81 ShuffleMask.push_back(4+(Imm & 3));
82 Imm >>= 2;
83 }
84}
85
David Greene3a2b5082011-02-17 19:18:59 +000086void DecodePSHUFLWMask(unsigned Imm,
87 SmallVectorImpl<unsigned> &ShuffleMask) {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000088 for (unsigned i = 0; i != 4; ++i) {
89 ShuffleMask.push_back((Imm & 3));
90 Imm >>= 2;
91 }
92 ShuffleMask.push_back(4);
93 ShuffleMask.push_back(5);
94 ShuffleMask.push_back(6);
95 ShuffleMask.push_back(7);
96}
97
David Greene20a1cbe2011-02-28 19:06:56 +000098void DecodePUNPCKLBWMask(unsigned NElts,
99 SmallVectorImpl<unsigned> &ShuffleMask) {
100 DecodeUNPCKLPMask(MVT::getVectorVT(MVT::i8, NElts), ShuffleMask);
101}
102
103void DecodePUNPCKLWDMask(unsigned NElts,
104 SmallVectorImpl<unsigned> &ShuffleMask) {
105 DecodeUNPCKLPMask(MVT::getVectorVT(MVT::i16, NElts), ShuffleMask);
106}
107
108void DecodePUNPCKLDQMask(unsigned NElts,
109 SmallVectorImpl<unsigned> &ShuffleMask) {
110 DecodeUNPCKLPMask(MVT::getVectorVT(MVT::i32, NElts), ShuffleMask);
111}
112
113void DecodePUNPCKLQDQMask(unsigned NElts,
114 SmallVectorImpl<unsigned> &ShuffleMask) {
115 DecodeUNPCKLPMask(MVT::getVectorVT(MVT::i64, NElts), ShuffleMask);
116}
117
118void DecodePUNPCKLMask(EVT VT,
David Greene3a2b5082011-02-17 19:18:59 +0000119 SmallVectorImpl<unsigned> &ShuffleMask) {
David Greene20a1cbe2011-02-28 19:06:56 +0000120 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000121}
122
David Greene3a2b5082011-02-17 19:18:59 +0000123void DecodePUNPCKHMask(unsigned NElts,
124 SmallVectorImpl<unsigned> &ShuffleMask) {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000125 for (unsigned i = 0; i != NElts/2; ++i) {
126 ShuffleMask.push_back(i+NElts/2);
127 ShuffleMask.push_back(i+NElts+NElts/2);
128 }
129}
130
Craig Topperc16db842011-11-29 07:49:05 +0000131void DecodeSHUFPMask(EVT VT, unsigned Imm,
132 SmallVectorImpl<unsigned> &ShuffleMask) {
133 unsigned NumElts = VT.getVectorNumElements();
134
135 unsigned NumLanes = VT.getSizeInBits() / 128;
136 unsigned NumLaneElts = NumElts / NumLanes;
137
138 int NewImm = Imm;
139 for (unsigned l = 0; l < NumLanes; ++l) {
140 unsigned LaneStart = l * NumLaneElts;
141 // Part that reads from dest.
142 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
143 ShuffleMask.push_back(NewImm % NumLaneElts + LaneStart);
144 NewImm /= NumLaneElts;
145 }
146 // Part that reads from src.
147 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
148 ShuffleMask.push_back(NewImm % NumLaneElts + NumElts + LaneStart);
149 NewImm /= NumLaneElts;
150 }
151 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000152 }
153}
154
Craig Topperccb70972011-11-22 01:57:35 +0000155void DecodeUNPCKHPMask(EVT VT, SmallVectorImpl<unsigned> &ShuffleMask) {
156 unsigned NumElts = VT.getVectorNumElements();
157
158 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
159 // independently on 128-bit lanes.
160 unsigned NumLanes = VT.getSizeInBits() / 128;
161 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
162 unsigned NumLaneElts = NumElts / NumLanes;
163
164 for (unsigned s = 0; s < NumLanes; ++s) {
165 unsigned Start = s * NumLaneElts + NumLaneElts/2;
166 unsigned End = s * NumLaneElts + NumLaneElts;
167 for (unsigned i = Start; i != End; ++i) {
168 ShuffleMask.push_back(i); // Reads from dest/src1
169 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
170 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000171 }
172}
173
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000174/// DecodeUNPCKLPMask - This decodes the shuffle masks for unpcklps/unpcklpd
David Greene20a1cbe2011-02-28 19:06:56 +0000175/// etc. VT indicates the type of the vector allowing it to handle different
176/// datatypes and vector widths.
Craig Topperccb70972011-11-22 01:57:35 +0000177void DecodeUNPCKLPMask(EVT VT, SmallVectorImpl<unsigned> &ShuffleMask) {
David Greenedd567b22011-03-02 17:23:43 +0000178 unsigned NumElts = VT.getVectorNumElements();
David Greene20a1cbe2011-02-28 19:06:56 +0000179
Bruno Cardoso Lopesf8fe47b2011-07-26 22:03:40 +0000180 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
181 // independently on 128-bit lanes.
182 unsigned NumLanes = VT.getSizeInBits() / 128;
183 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
184 unsigned NumLaneElts = NumElts / NumLanes;
David Greene20a1cbe2011-02-28 19:06:56 +0000185
Bruno Cardoso Lopesf8fe47b2011-07-26 22:03:40 +0000186 for (unsigned s = 0; s < NumLanes; ++s) {
Craig Topperccb70972011-11-22 01:57:35 +0000187 unsigned Start = s * NumLaneElts;
188 unsigned End = s * NumLaneElts + NumLaneElts/2;
David Greenedd567b22011-03-02 17:23:43 +0000189 for (unsigned i = Start; i != End; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000190 ShuffleMask.push_back(i); // Reads from dest/src1
191 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
David Greenedd567b22011-03-02 17:23:43 +0000192 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000193 }
194}
195
Bruno Cardoso Lopes795f5582011-07-29 01:31:11 +0000196// DecodeVPERMILPSMask - Decodes VPERMILPS permutes for any 128-bit 32-bit
197// elements. For 256-bit vectors, it's considered as two 128 lanes, the
198// referenced elements can't cross lanes and the mask of the first lane must
199// be the same of the second.
200void DecodeVPERMILPSMask(unsigned NumElts, unsigned Imm,
201 SmallVectorImpl<unsigned> &ShuffleMask) {
202 unsigned NumLanes = (NumElts*32)/128;
203 unsigned LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopesb878caa2011-07-21 01:55:47 +0000204
205 for (unsigned l = 0; l != NumLanes; ++l) {
Bruno Cardoso Lopes795f5582011-07-29 01:31:11 +0000206 for (unsigned i = 0; i != LaneSize; ++i) {
Bruno Cardoso Lopesb878caa2011-07-21 01:55:47 +0000207 unsigned Idx = (Imm >> (i*2)) & 0x3 ;
Bruno Cardoso Lopes795f5582011-07-29 01:31:11 +0000208 ShuffleMask.push_back(Idx+(l*LaneSize));
209 }
210 }
211}
212
213// DecodeVPERMILPDMask - Decodes VPERMILPD permutes for any 128-bit 64-bit
214// elements. For 256-bit vectors, it's considered as two 128 lanes, the
215// referenced elements can't cross lanes but the mask of the first lane can
216// be the different of the second (not like VPERMILPS).
217void DecodeVPERMILPDMask(unsigned NumElts, unsigned Imm,
218 SmallVectorImpl<unsigned> &ShuffleMask) {
219 unsigned NumLanes = (NumElts*64)/128;
220 unsigned LaneSize = NumElts/NumLanes;
221
222 for (unsigned l = 0; l < NumLanes; ++l) {
223 for (unsigned i = l*LaneSize; i < LaneSize*(l+1); ++i) {
224 unsigned Idx = (Imm >> i) & 0x1;
225 ShuffleMask.push_back(Idx+(l*LaneSize));
Bruno Cardoso Lopesb878caa2011-07-21 01:55:47 +0000226 }
227 }
228}
229
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000230void DecodeVPERM2F128Mask(EVT VT, unsigned Imm,
231 SmallVectorImpl<unsigned> &ShuffleMask) {
232 unsigned HalfSize = VT.getVectorNumElements()/2;
233 unsigned FstHalfBegin = (Imm & 0x3) * HalfSize;
234 unsigned SndHalfBegin = ((Imm >> 4) & 0x3) * HalfSize;
235
236 for (int i = FstHalfBegin, e = FstHalfBegin+HalfSize; i != e; ++i)
237 ShuffleMask.push_back(i);
238 for (int i = SndHalfBegin, e = SndHalfBegin+HalfSize; i != e; ++i)
239 ShuffleMask.push_back(i);
240}
241
242void DecodeVPERM2F128Mask(unsigned Imm,
243 SmallVectorImpl<unsigned> &ShuffleMask) {
244 // VPERM2F128 is used by any 256-bit EVT, but X86InstComments only
245 // has information about the instruction and not the types. So for
246 // instruction comments purpose, assume the 256-bit vector is v4i64.
247 return DecodeVPERM2F128Mask(MVT::v4i64, Imm, ShuffleMask);
248}
249
David Greene3a2b5082011-02-17 19:18:59 +0000250} // llvm namespace