X86ShuffleDecode.cpp: Silence a warning. [-Wunused-variable]
[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 DecodePALIGNRMask(MVT VT, unsigned Imm,
67                        SmallVectorImpl<int> &ShuffleMask) {
68   unsigned NumElts = VT.getVectorNumElements();
69   unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8);
70
71   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   }
82 }
83
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();
89
90   unsigned NumLanes = VT.getSizeInBits() / 128;
91   unsigned NumLaneElts = NumElts / NumLanes;
92
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;
98     }
99     if (NumLaneElts == 4) NewImm = Imm; // reload imm
100   }
101 }
102
103 void DecodePSHUFHWMask(MVT VT, unsigned Imm,
104                        SmallVectorImpl<int> &ShuffleMask) {
105   unsigned NumElts = VT.getVectorNumElements();
106
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     }
116   }
117 }
118
119 void DecodePSHUFLWMask(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 + (NewImm & 3));
127       NewImm >>= 2;
128     }
129     for (unsigned i = 4, e = 8; i != e; ++i) {
130       ShuffleMask.push_back(l + i);
131     }
132   }
133 }
134
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
137 /// widths.
138 void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
139   unsigned NumElts = VT.getVectorNumElements();
140
141   unsigned NumLanes = VT.getSizeInBits() / 128;
142   unsigned NumLaneElts = NumElts / NumLanes;
143
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;
151       }
152     }
153     if (NumLaneElts == 4) NewImm = Imm; // reload imm
154   }
155 }
156
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();
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
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
173     }
174   }
175 }
176
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();
182
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;
188
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
193     }
194   }
195 }
196
197 void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
198                           SmallVectorImpl<int> &ShuffleMask) {
199   if (Imm & 0x88)
200     return; // Not a shuffle
201
202   unsigned HalfSize = VT.getVectorNumElements()/2;
203
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);
208   }
209 }
210
211 /// \brief Decode PSHUFB masks stored in an LLVM Constant.
212 void DecodePSHUFBMask(const ConstantDataSequential *C,
213                       SmallVectorImpl<int> &ShuffleMask) {
214   Type *MaskTy = C->getType();
215   assert(MaskTy->isVectorTy() && "Expected a vector constant mask!");
216   assert(MaskTy->getVectorElementType()->isIntegerTy(8) &&
217          "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
243 /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
244 /// No VT provided since it only works on 256-bit, 4 element vectors.
245 void 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
251 } // llvm namespace