Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_logic.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 logical operations.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include "util/u_cpu_detect.h"
37 #include "util/u_debug.h"
38
39 #include "lp_bld_type.h"
40 #include "lp_bld_const.h"
41 #include "lp_bld_intr.h"
42 #include "lp_bld_logic.h"
43
44
45 /**
46 * Build code to compare two values 'a' and 'b' of 'type' using the given func.
47 * \param func one of PIPE_FUNC_x
48 * The result values will be 0 for false or ~0 for true.
49 */
50 LLVMValueRef
51 lp_build_compare(LLVMBuilderRef builder,
52 const struct lp_type type,
53 unsigned func,
54 LLVMValueRef a,
55 LLVMValueRef b)
56 {
57 LLVMTypeRef vec_type = lp_build_vec_type(type);
58 LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
59 LLVMValueRef zeros = LLVMConstNull(int_vec_type);
60 LLVMValueRef ones = LLVMConstAllOnes(int_vec_type);
61 LLVMValueRef cond;
62 LLVMValueRef res;
63 unsigned i;
64
65 assert(func >= PIPE_FUNC_NEVER);
66 assert(func <= PIPE_FUNC_ALWAYS);
67
68 if(func == PIPE_FUNC_NEVER)
69 return zeros;
70 if(func == PIPE_FUNC_ALWAYS)
71 return ones;
72
73 /* TODO: optimize the constant case */
74
75 /* XXX: It is not clear if we should use the ordered or unordered operators */
76
77 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
78 if(type.width * type.length == 128) {
79 if(type.floating && util_cpu_caps.has_sse) {
80 /* float[4] comparison */
81 LLVMValueRef args[3];
82 unsigned cc;
83 boolean swap;
84
85 swap = FALSE;
86 switch(func) {
87 case PIPE_FUNC_EQUAL:
88 cc = 0;
89 break;
90 case PIPE_FUNC_NOTEQUAL:
91 cc = 4;
92 break;
93 case PIPE_FUNC_LESS:
94 cc = 1;
95 break;
96 case PIPE_FUNC_LEQUAL:
97 cc = 2;
98 break;
99 case PIPE_FUNC_GREATER:
100 cc = 1;
101 swap = TRUE;
102 break;
103 case PIPE_FUNC_GEQUAL:
104 cc = 2;
105 swap = TRUE;
106 break;
107 default:
108 assert(0);
109 return lp_build_undef(type);
110 }
111
112 if(swap) {
113 args[0] = b;
114 args[1] = a;
115 }
116 else {
117 args[0] = a;
118 args[1] = b;
119 }
120
121 args[2] = LLVMConstInt(LLVMInt8Type(), cc, 0);
122 res = lp_build_intrinsic(builder,
123 "llvm.x86.sse.cmp.ps",
124 vec_type,
125 args, 3);
126 res = LLVMBuildBitCast(builder, res, int_vec_type, "");
127 return res;
128 }
129 else if(util_cpu_caps.has_sse2) {
130 /* int[4] comparison */
131 static const struct {
132 unsigned swap:1;
133 unsigned eq:1;
134 unsigned gt:1;
135 unsigned not:1;
136 } table[] = {
137 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
138 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
139 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
140 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
141 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
142 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
143 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
144 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
145 };
146 const char *pcmpeq;
147 const char *pcmpgt;
148 LLVMValueRef args[2];
149 LLVMValueRef res;
150
151 switch (type.width) {
152 case 8:
153 pcmpeq = "llvm.x86.sse2.pcmpeq.b";
154 pcmpgt = "llvm.x86.sse2.pcmpgt.b";
155 break;
156 case 16:
157 pcmpeq = "llvm.x86.sse2.pcmpeq.w";
158 pcmpgt = "llvm.x86.sse2.pcmpgt.w";
159 break;
160 case 32:
161 pcmpeq = "llvm.x86.sse2.pcmpeq.d";
162 pcmpgt = "llvm.x86.sse2.pcmpgt.d";
163 break;
164 default:
165 assert(0);
166 return lp_build_undef(type);
167 }
168
169 /* There are no signed byte and unsigned word/dword comparison
170 * instructions. So flip the sign bit so that the results match.
171 */
172 if(table[func].gt &&
173 ((type.width == 8 && type.sign) ||
174 (type.width != 8 && !type.sign))) {
175 LLVMValueRef msb = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
176 a = LLVMBuildXor(builder, a, msb, "");
177 b = LLVMBuildXor(builder, b, msb, "");
178 }
179
180 if(table[func].swap) {
181 args[0] = b;
182 args[1] = a;
183 }
184 else {
185 args[0] = a;
186 args[1] = b;
187 }
188
189 if(table[func].eq)
190 res = lp_build_intrinsic(builder, pcmpeq, vec_type, args, 2);
191 else if (table[func].gt)
192 res = lp_build_intrinsic(builder, pcmpgt, vec_type, args, 2);
193 else
194 res = LLVMConstNull(vec_type);
195
196 if(table[func].not)
197 res = LLVMBuildNot(builder, res, "");
198
199 return res;
200 }
201 } /* if (type.width * type.length == 128) */
202 #endif
203
204 if(type.floating) {
205 LLVMRealPredicate op;
206 switch(func) {
207 case PIPE_FUNC_NEVER:
208 op = LLVMRealPredicateFalse;
209 break;
210 case PIPE_FUNC_ALWAYS:
211 op = LLVMRealPredicateTrue;
212 break;
213 case PIPE_FUNC_EQUAL:
214 op = LLVMRealUEQ;
215 break;
216 case PIPE_FUNC_NOTEQUAL:
217 op = LLVMRealUNE;
218 break;
219 case PIPE_FUNC_LESS:
220 op = LLVMRealULT;
221 break;
222 case PIPE_FUNC_LEQUAL:
223 op = LLVMRealULE;
224 break;
225 case PIPE_FUNC_GREATER:
226 op = LLVMRealUGT;
227 break;
228 case PIPE_FUNC_GEQUAL:
229 op = LLVMRealUGE;
230 break;
231 default:
232 assert(0);
233 return lp_build_undef(type);
234 }
235
236 #if 0
237 /* XXX: Although valid IR, no LLVM target currently support this */
238 cond = LLVMBuildFCmp(builder, op, a, b, "");
239 res = LLVMBuildSelect(builder, cond, ones, zeros, "");
240 #else
241 res = LLVMGetUndef(int_vec_type);
242 if (type.length == 1) {
243 res = LLVMBuildFCmp(builder, op, a, b, "");
244 }
245 else {
246 debug_printf("%s: warning: using slow element-wise float"
247 " vector comparison\n", __FUNCTION__);
248 for (i = 0; i < type.length; ++i) {
249 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
250 cond = LLVMBuildFCmp(builder, op,
251 LLVMBuildExtractElement(builder, a, index, ""),
252 LLVMBuildExtractElement(builder, b, index, ""),
253 "");
254 cond = LLVMBuildSelect(builder, cond,
255 LLVMConstExtractElement(ones, index),
256 LLVMConstExtractElement(zeros, index),
257 "");
258 res = LLVMBuildInsertElement(builder, res, cond, index, "");
259 }
260 }
261 #endif
262 }
263 else {
264 LLVMIntPredicate op;
265 switch(func) {
266 case PIPE_FUNC_EQUAL:
267 op = LLVMIntEQ;
268 break;
269 case PIPE_FUNC_NOTEQUAL:
270 op = LLVMIntNE;
271 break;
272 case PIPE_FUNC_LESS:
273 op = type.sign ? LLVMIntSLT : LLVMIntULT;
274 break;
275 case PIPE_FUNC_LEQUAL:
276 op = type.sign ? LLVMIntSLE : LLVMIntULE;
277 break;
278 case PIPE_FUNC_GREATER:
279 op = type.sign ? LLVMIntSGT : LLVMIntUGT;
280 break;
281 case PIPE_FUNC_GEQUAL:
282 op = type.sign ? LLVMIntSGE : LLVMIntUGE;
283 break;
284 default:
285 assert(0);
286 return lp_build_undef(type);
287 }
288
289 #if 0
290 /* XXX: Although valid IR, no LLVM target currently support this */
291 cond = LLVMBuildICmp(builder, op, a, b, "");
292 res = LLVMBuildSelect(builder, cond, ones, zeros, "");
293 #else
294 res = LLVMGetUndef(int_vec_type);
295 if (type.length == 1) {
296 res = LLVMBuildICmp(builder, op, a, b, "");
297 }
298 else {
299 debug_printf("%s: warning: using slow element-wise int"
300 " vector comparison\n", __FUNCTION__);
301
302 for(i = 0; i < type.length; ++i) {
303 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
304 cond = LLVMBuildICmp(builder, op,
305 LLVMBuildExtractElement(builder, a, index, ""),
306 LLVMBuildExtractElement(builder, b, index, ""),
307 "");
308 cond = LLVMBuildSelect(builder, cond,
309 LLVMConstExtractElement(ones, index),
310 LLVMConstExtractElement(zeros, index),
311 "");
312 res = LLVMBuildInsertElement(builder, res, cond, index, "");
313 }
314 }
315 #endif
316 }
317
318 return res;
319 }
320
321
322
323 /**
324 * Build code to compare two values 'a' and 'b' using the given func.
325 * \param func one of PIPE_FUNC_x
326 * The result values will be 0 for false or ~0 for true.
327 */
328 LLVMValueRef
329 lp_build_cmp(struct lp_build_context *bld,
330 unsigned func,
331 LLVMValueRef a,
332 LLVMValueRef b)
333 {
334 return lp_build_compare(bld->builder, bld->type, func, a, b);
335 }
336
337
338 /**
339 * Return mask ? a : b;
340 */
341 LLVMValueRef
342 lp_build_select(struct lp_build_context *bld,
343 LLVMValueRef mask,
344 LLVMValueRef a,
345 LLVMValueRef b)
346 {
347 struct lp_type type = bld->type;
348 LLVMValueRef res;
349
350 if(a == b)
351 return a;
352
353 if (type.length == 1) {
354 res = LLVMBuildSelect(bld->builder, mask, a, b, "");
355 }
356 else {
357 if(type.floating) {
358 LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
359 a = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
360 b = LLVMBuildBitCast(bld->builder, b, int_vec_type, "");
361 }
362
363 a = LLVMBuildAnd(bld->builder, a, mask, "");
364
365 /* This often gets translated to PANDN, but sometimes the NOT is
366 * pre-computed and stored in another constant. The best strategy depends
367 * on available registers, so it is not a big deal -- hopefully LLVM does
368 * the right decision attending the rest of the program.
369 */
370 b = LLVMBuildAnd(bld->builder, b, LLVMBuildNot(bld->builder, mask, ""), "");
371
372 res = LLVMBuildOr(bld->builder, a, b, "");
373
374 if(type.floating) {
375 LLVMTypeRef vec_type = lp_build_vec_type(type);
376 res = LLVMBuildBitCast(bld->builder, res, vec_type, "");
377 }
378 }
379
380 return res;
381 }
382
383
384 LLVMValueRef
385 lp_build_select_aos(struct lp_build_context *bld,
386 LLVMValueRef a,
387 LLVMValueRef b,
388 const boolean cond[4])
389 {
390 const struct lp_type type = bld->type;
391 const unsigned n = type.length;
392 unsigned i, j;
393
394 if(a == b)
395 return a;
396 if(cond[0] && cond[1] && cond[2] && cond[3])
397 return a;
398 if(!cond[0] && !cond[1] && !cond[2] && !cond[3])
399 return b;
400 if(a == bld->undef || b == bld->undef)
401 return bld->undef;
402
403 /*
404 * There are three major ways of accomplishing this:
405 * - with a shuffle,
406 * - with a select,
407 * - or with a bit mask.
408 *
409 * Select isn't supported for vector types yet.
410 * The flip between these is empirical and might need to be.
411 */
412 if (n <= 4) {
413 /*
414 * Shuffle.
415 */
416 LLVMTypeRef elem_type = LLVMInt32Type();
417 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
418
419 for(j = 0; j < n; j += 4)
420 for(i = 0; i < 4; ++i)
421 shuffles[j + i] = LLVMConstInt(elem_type, (cond[i] ? 0 : n) + j + i, 0);
422
423 return LLVMBuildShuffleVector(bld->builder, a, b, LLVMConstVector(shuffles, n), "");
424 }
425 else {
426 #if 0
427 /* XXX: Unfortunately select of vectors do not work */
428 /* Use a select */
429 LLVMTypeRef elem_type = LLVMInt1Type();
430 LLVMValueRef cond[LP_MAX_VECTOR_LENGTH];
431
432 for(j = 0; j < n; j += 4)
433 for(i = 0; i < 4; ++i)
434 cond[j + i] = LLVMConstInt(elem_type, cond[i] ? 1 : 0, 0);
435
436 return LLVMBuildSelect(bld->builder, LLVMConstVector(cond, n), a, b, "");
437 #else
438 LLVMValueRef mask = lp_build_const_mask_aos(type, cond);
439 return lp_build_select(bld, mask, a, b);
440 #endif
441 }
442 }
443
444 LLVMValueRef
445 lp_build_alloca(struct lp_build_context *bld)
446 {
447 const struct lp_type type = bld->type;
448
449 if (type.length > 1) { /*vector*/
450 return LLVMBuildAlloca(bld->builder, lp_build_vec_type(type), "");
451 } else { /*scalar*/
452 return LLVMBuildAlloca(bld->builder, lp_build_elem_type(type), "");
453 }
454 }