[x86] Teach the x86 DAG combiner to form MOVSLDUP and MOVSHDUP
[oota-llvm.git] / lib / Target / X86 / Utils / X86ShuffleDecode.cpp
1 //===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===//
2 //
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
15 #include "X86ShuffleDecode.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/CodeGen/MachineValueType.h"
18
19 //===----------------------------------------------------------------------===//
20 //  Vector Mask Decoding
21 //===----------------------------------------------------------------------===//
22
23 namespace llvm {
24
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);
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
48 // <3,1> or <6,7,2,3>
49 void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
50   for (unsigned i = NElts/2; i != NElts; ++i)
51     ShuffleMask.push_back(NElts+i);
52
53   for (unsigned i = NElts/2; i != NElts; ++i)
54     ShuffleMask.push_back(i);
55 }
56
57 // <0,2> or <0,1,4,5>
58 void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
59   for (unsigned i = 0; i != NElts/2; ++i)
60     ShuffleMask.push_back(i);
61
62   for (unsigned i = 0; i != NElts/2; ++i)
63     ShuffleMask.push_back(NElts+i);
64 }
65
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);
71   }
72 }
73
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);
79   }
80 }
81
82 void DecodePALIGNRMask(MVT VT, unsigned Imm,
83                        SmallVectorImpl<int> &ShuffleMask) {
84   unsigned NumElts = VT.getVectorNumElements();
85   unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
86
87   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   }
98 }
99
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();
105
106   unsigned NumLanes = VT.getSizeInBits() / 128;
107   unsigned NumLaneElts = NumElts / NumLanes;
108
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;
114     }
115     if (NumLaneElts == 4) NewImm = Imm; // reload imm
116   }
117 }
118
119 void DecodePSHUFHWMask(MVT VT, unsigned Imm,
120                        SmallVectorImpl<int> &ShuffleMask) {
121   unsigned NumElts = VT.getVectorNumElements();
122
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     }
132   }
133 }
134
135 void DecodePSHUFLWMask(MVT VT, unsigned Imm,
136                        SmallVectorImpl<int> &ShuffleMask) {
137   unsigned NumElts = VT.getVectorNumElements();
138
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     }
148   }
149 }
150
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
153 /// widths.
154 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
155   unsigned NumElts = VT.getVectorNumElements();
156
157   unsigned NumLanes = VT.getSizeInBits() / 128;
158   unsigned NumLaneElts = NumElts / NumLanes;
159
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;
167       }
168     }
169     if (NumLaneElts == 4) NewImm = Imm; // reload imm
170   }
171 }
172
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();
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
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
189     }
190   }
191 }
192
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();
198
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;
204
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
209     }
210   }
211 }
212
213 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
214                           SmallVectorImpl<int> &ShuffleMask) {
215   if (Imm & 0x88)
216     return; // Not a shuffle
217
218   unsigned HalfSize = VT.getVectorNumElements()/2;
219
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);
224   }
225 }
226
227 void DecodePSHUFBMask(const ConstantDataSequential *C,
228                       SmallVectorImpl<int> &ShuffleMask) {
229   Type *MaskTy = C->getType();
230   assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
231   assert(MaskTy->getVectorElementType()->isIntegerTy(8) &&
232          "Expected i8 constant mask elements!");
233   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;
251       assert((Index >= 0 && Index < NumElements) &&
252              "Out of bounds shuffle index for pshub instruction!");
253       ShuffleMask.push_back(Index);
254     }
255   }
256 }
257
258 void 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
277 void 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
284 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
285 /// No VT provided since it only works on 256-bit, 4 element vectors.
286 void 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
292 } // llvm namespace