gallivm: Omit references to NoFramePointerElimNonLeaf
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_swizzle.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Helper functions for swizzling/shuffling.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include "util/u_debug.h"
37
38 #include "lp_bld_type.h"
39 #include "lp_bld_const.h"
40 #include "lp_bld_logic.h"
41 #include "lp_bld_swizzle.h"
42
43
44 LLVMValueRef
45 lp_build_broadcast(LLVMBuilderRef builder,
46 LLVMTypeRef vec_type,
47 LLVMValueRef scalar)
48 {
49 const unsigned n = LLVMGetVectorSize(vec_type);
50 LLVMValueRef res;
51 unsigned i;
52
53 res = LLVMGetUndef(vec_type);
54 for(i = 0; i < n; ++i) {
55 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
56 res = LLVMBuildInsertElement(builder, res, scalar, index, "");
57 }
58
59 return res;
60 }
61
62
63 /**
64 * Broadcast
65 */
66 LLVMValueRef
67 lp_build_broadcast_scalar(struct lp_build_context *bld,
68 LLVMValueRef scalar)
69 {
70 const struct lp_type type = bld->type;
71
72 assert(lp_check_elem_type(type, LLVMTypeOf(scalar)));
73
74 if (type.length == 1) {
75 return scalar;
76 }
77 else {
78 LLVMValueRef res;
79 #if HAVE_LLVM >= 0x207
80 res = LLVMBuildInsertElement(bld->builder, bld->undef, scalar,
81 LLVMConstInt(LLVMInt32Type(), 0, 0), "");
82 res = LLVMBuildShuffleVector(bld->builder, res, bld->undef,
83 lp_build_const_int_vec(type, 0), "");
84 #else
85 /* XXX: The above path provokes a bug in LLVM 2.6 */
86 unsigned i;
87 res = bld->undef;
88 for(i = 0; i < type.length; ++i) {
89 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
90 res = LLVMBuildInsertElement(bld->builder, res, scalar, index, "");
91 }
92 #endif
93 return res;
94 }
95 }
96
97
98 LLVMValueRef
99 lp_build_broadcast_aos(struct lp_build_context *bld,
100 LLVMValueRef a,
101 unsigned channel)
102 {
103 const struct lp_type type = bld->type;
104 const unsigned n = type.length;
105 unsigned i, j;
106
107 if(a == bld->undef || a == bld->zero || a == bld->one)
108 return a;
109
110 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
111 * using shuffles here actually causes worst results. More investigation is
112 * needed. */
113 if (n <= 4) {
114 /*
115 * Shuffle.
116 */
117 LLVMTypeRef elem_type = LLVMInt32Type();
118 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
119
120 for(j = 0; j < n; j += 4)
121 for(i = 0; i < 4; ++i)
122 shuffles[j + i] = LLVMConstInt(elem_type, j + channel, 0);
123
124 return LLVMBuildShuffleVector(bld->builder, a, bld->undef, LLVMConstVector(shuffles, n), "");
125 }
126 else {
127 /*
128 * Bit mask and recursive shifts
129 *
130 * XYZW XYZW .... XYZW <= input
131 * 0Y00 0Y00 .... 0Y00
132 * YY00 YY00 .... YY00
133 * YYYY YYYY .... YYYY <= output
134 */
135 struct lp_type type4 = type;
136 const char shifts[4][2] = {
137 { 1, 2},
138 {-1, 2},
139 { 1, -2},
140 {-1, -2}
141 };
142 boolean cond[4];
143 unsigned i;
144
145 memset(cond, 0, sizeof cond);
146 cond[channel] = 1;
147
148 a = LLVMBuildAnd(bld->builder, a, lp_build_const_mask_aos(type, cond), "");
149
150 type4.width *= 4;
151 type4.length /= 4;
152
153 a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type4), "");
154
155 for(i = 0; i < 2; ++i) {
156 LLVMValueRef tmp = NULL;
157 int shift = shifts[channel][i];
158
159 #ifdef PIPE_ARCH_LITTLE_ENDIAN
160 shift = -shift;
161 #endif
162
163 if(shift > 0)
164 tmp = LLVMBuildLShr(bld->builder, a, lp_build_const_int_vec(type4, shift*type.width), "");
165 if(shift < 0)
166 tmp = LLVMBuildShl(bld->builder, a, lp_build_const_int_vec(type4, -shift*type.width), "");
167
168 assert(tmp);
169 if(tmp)
170 a = LLVMBuildOr(bld->builder, a, tmp, "");
171 }
172
173 return LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type), "");
174 }
175 }
176
177
178 LLVMValueRef
179 lp_build_swizzle1_aos(struct lp_build_context *bld,
180 LLVMValueRef a,
181 const unsigned char swizzle[4])
182 {
183 const unsigned n = bld->type.length;
184 unsigned i, j;
185
186 if(a == bld->undef || a == bld->zero || a == bld->one)
187 return a;
188
189 if(swizzle[0] == swizzle[1] && swizzle[1] == swizzle[2] && swizzle[2] == swizzle[3])
190 return lp_build_broadcast_aos(bld, a, swizzle[0]);
191
192 {
193 /*
194 * Shuffle.
195 */
196 LLVMTypeRef elem_type = LLVMInt32Type();
197 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
198
199 for(j = 0; j < n; j += 4)
200 for(i = 0; i < 4; ++i)
201 shuffles[j + i] = LLVMConstInt(elem_type, j + swizzle[i], 0);
202
203 return LLVMBuildShuffleVector(bld->builder, a, bld->undef, LLVMConstVector(shuffles, n), "");
204 }
205 }
206
207
208 LLVMValueRef
209 lp_build_swizzle2_aos(struct lp_build_context *bld,
210 LLVMValueRef a,
211 LLVMValueRef b,
212 const unsigned char swizzle[4])
213 {
214 const unsigned n = bld->type.length;
215 unsigned i, j;
216
217 if(swizzle[0] < 4 && swizzle[1] < 4 && swizzle[2] < 4 && swizzle[3] < 4)
218 return lp_build_swizzle1_aos(bld, a, swizzle);
219
220 if(a == b) {
221 unsigned char swizzle1[4];
222 swizzle1[0] = swizzle[0] % 4;
223 swizzle1[1] = swizzle[1] % 4;
224 swizzle1[2] = swizzle[2] % 4;
225 swizzle1[3] = swizzle[3] % 4;
226 return lp_build_swizzle1_aos(bld, a, swizzle1);
227 }
228
229 if(swizzle[0] % 4 == 0 &&
230 swizzle[1] % 4 == 1 &&
231 swizzle[2] % 4 == 2 &&
232 swizzle[3] % 4 == 3) {
233 boolean cond[4];
234 cond[0] = swizzle[0] / 4;
235 cond[1] = swizzle[1] / 4;
236 cond[2] = swizzle[2] / 4;
237 cond[3] = swizzle[3] / 4;
238 return lp_build_select_aos(bld, a, b, cond);
239 }
240
241 {
242 /*
243 * Shuffle.
244 */
245 LLVMTypeRef elem_type = LLVMInt32Type();
246 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
247
248 for(j = 0; j < n; j += 4)
249 for(i = 0; i < 4; ++i)
250 shuffles[j + i] = LLVMConstInt(elem_type, j + (swizzle[i] % 4) + (swizzle[i] / 4 * n), 0);
251
252 return LLVMBuildShuffleVector(bld->builder, a, b, LLVMConstVector(shuffles, n), "");
253 }
254 }
255
256
257 /**
258 * Extended swizzle of a single channel of a SoA vector.
259 *
260 * @param bld building context
261 * @param unswizzled array with the 4 unswizzled values
262 * @param swizzle one of the PIPE_SWIZZLE_*
263 *
264 * @return the swizzled value.
265 */
266 LLVMValueRef
267 lp_build_swizzle_soa_channel(struct lp_build_context *bld,
268 const LLVMValueRef *unswizzled,
269 unsigned swizzle)
270 {
271 switch (swizzle) {
272 case PIPE_SWIZZLE_RED:
273 case PIPE_SWIZZLE_GREEN:
274 case PIPE_SWIZZLE_BLUE:
275 case PIPE_SWIZZLE_ALPHA:
276 return unswizzled[swizzle];
277 case PIPE_SWIZZLE_ZERO:
278 return bld->zero;
279 case PIPE_SWIZZLE_ONE:
280 return bld->one;
281 default:
282 assert(0);
283 return bld->undef;
284 }
285 }
286
287
288 /**
289 * Extended swizzle of a SoA vector.
290 *
291 * @param bld building context
292 * @param unswizzled array with the 4 unswizzled values
293 * @param swizzles array of PIPE_SWIZZLE_*
294 * @param swizzled output swizzled values
295 */
296 void
297 lp_build_swizzle_soa(struct lp_build_context *bld,
298 const LLVMValueRef *unswizzled,
299 const unsigned char swizzles[4],
300 LLVMValueRef *swizzled)
301 {
302 unsigned chan;
303
304 for (chan = 0; chan < 4; ++chan) {
305 swizzled[chan] = lp_build_swizzle_soa_channel(bld, unswizzled,
306 swizzles[chan]);
307 }
308 }
309
310
311 /**
312 * Do an extended swizzle of a SoA vector inplace.
313 *
314 * @param bld building context
315 * @param values intput/output array with the 4 values
316 * @param swizzles array of PIPE_SWIZZLE_*
317 */
318 void
319 lp_build_swizzle_soa_inplace(struct lp_build_context *bld,
320 LLVMValueRef *values,
321 const unsigned char swizzles[4])
322 {
323 LLVMValueRef unswizzled[4];
324 unsigned chan;
325
326 for (chan = 0; chan < 4; ++chan) {
327 unswizzled[chan] = values[chan];
328 }
329
330 lp_build_swizzle_soa(bld, unswizzled, swizzles, values);
331 }