1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Helper functions for logical operations.
32 * @author Jose Fonseca <jfonseca@vmware.com>
36 #include "util/u_cpu_detect.h"
37 #include "util/u_memory.h"
38 #include "util/u_debug.h"
40 #include "lp_bld_type.h"
41 #include "lp_bld_const.h"
42 #include "lp_bld_intr.h"
43 #include "lp_bld_debug.h"
44 #include "lp_bld_logic.h"
50 * Selection with vector conditional like
52 * select <4 x i1> %C, %A, %B
54 * is valid IR (e.g. llvm/test/Assembler/vector-select.ll), but it is not
55 * supported on any backend.
57 * Expanding the boolean vector to full SIMD register width, as in
59 * sext <4 x i1> %C to <4 x i32>
61 * is valid and supported (e.g., llvm/test/CodeGen/X86/vec_compare.ll), but
62 * it causes assertion failures in LLVM 2.6. It appears to work correctly on
68 * Build code to compare two values 'a' and 'b' of 'type' using the given func.
69 * \param func one of PIPE_FUNC_x
70 * The result values will be 0 for false or ~0 for true.
73 lp_build_compare(LLVMBuilderRef builder
,
74 const struct lp_type type
,
79 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
80 LLVMValueRef zeros
= LLVMConstNull(int_vec_type
);
81 LLVMValueRef ones
= LLVMConstAllOnes(int_vec_type
);
85 assert(func
>= PIPE_FUNC_NEVER
);
86 assert(func
<= PIPE_FUNC_ALWAYS
);
87 assert(lp_check_value(type
, a
));
88 assert(lp_check_value(type
, b
));
90 if(func
== PIPE_FUNC_NEVER
)
92 if(func
== PIPE_FUNC_ALWAYS
)
95 /* TODO: optimize the constant case */
97 /* XXX: It is not clear if we should use the ordered or unordered operators */
99 #if HAVE_LLVM < 0x0207
100 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
101 if(type
.width
* type
.length
== 128) {
102 if(type
.floating
&& util_cpu_caps
.has_sse
) {
103 /* float[4] comparison */
104 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
105 LLVMValueRef args
[3];
111 case PIPE_FUNC_EQUAL
:
114 case PIPE_FUNC_NOTEQUAL
:
120 case PIPE_FUNC_LEQUAL
:
123 case PIPE_FUNC_GREATER
:
127 case PIPE_FUNC_GEQUAL
:
133 return lp_build_undef(type
);
145 args
[2] = LLVMConstInt(LLVMInt8Type(), cc
, 0);
146 res
= lp_build_intrinsic(builder
,
147 "llvm.x86.sse.cmp.ps",
150 res
= LLVMBuildBitCast(builder
, res
, int_vec_type
, "");
153 else if(util_cpu_caps
.has_sse2
) {
154 /* int[4] comparison */
155 static const struct {
161 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
162 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
163 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
164 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
165 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
166 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
167 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
168 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
172 LLVMValueRef args
[2];
174 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
176 switch (type
.width
) {
178 pcmpeq
= "llvm.x86.sse2.pcmpeq.b";
179 pcmpgt
= "llvm.x86.sse2.pcmpgt.b";
182 pcmpeq
= "llvm.x86.sse2.pcmpeq.w";
183 pcmpgt
= "llvm.x86.sse2.pcmpgt.w";
186 pcmpeq
= "llvm.x86.sse2.pcmpeq.d";
187 pcmpgt
= "llvm.x86.sse2.pcmpgt.d";
191 return lp_build_undef(type
);
194 /* There are no unsigned comparison instructions. So flip the sign bit
195 * so that the results match.
197 if (table
[func
].gt
&& !type
.sign
) {
198 LLVMValueRef msb
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
199 a
= LLVMBuildXor(builder
, a
, msb
, "");
200 b
= LLVMBuildXor(builder
, b
, msb
, "");
203 if(table
[func
].swap
) {
213 res
= lp_build_intrinsic(builder
, pcmpeq
, vec_type
, args
, 2);
214 else if (table
[func
].gt
)
215 res
= lp_build_intrinsic(builder
, pcmpgt
, vec_type
, args
, 2);
217 res
= LLVMConstNull(vec_type
);
220 res
= LLVMBuildNot(builder
, res
, "");
224 } /* if (type.width * type.length == 128) */
226 #endif /* HAVE_LLVM < 0x0207 */
229 LLVMRealPredicate op
;
231 case PIPE_FUNC_NEVER
:
232 op
= LLVMRealPredicateFalse
;
234 case PIPE_FUNC_ALWAYS
:
235 op
= LLVMRealPredicateTrue
;
237 case PIPE_FUNC_EQUAL
:
240 case PIPE_FUNC_NOTEQUAL
:
246 case PIPE_FUNC_LEQUAL
:
249 case PIPE_FUNC_GREATER
:
252 case PIPE_FUNC_GEQUAL
:
257 return lp_build_undef(type
);
260 #if HAVE_LLVM >= 0x0207
261 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
262 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
264 if (type
.length
== 1) {
265 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
266 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
271 res
= LLVMGetUndef(int_vec_type
);
273 debug_printf("%s: warning: using slow element-wise float"
274 " vector comparison\n", __FUNCTION__
);
275 for (i
= 0; i
< type
.length
; ++i
) {
276 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
277 cond
= LLVMBuildFCmp(builder
, op
,
278 LLVMBuildExtractElement(builder
, a
, index
, ""),
279 LLVMBuildExtractElement(builder
, b
, index
, ""),
281 cond
= LLVMBuildSelect(builder
, cond
,
282 LLVMConstExtractElement(ones
, index
),
283 LLVMConstExtractElement(zeros
, index
),
285 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
293 case PIPE_FUNC_EQUAL
:
296 case PIPE_FUNC_NOTEQUAL
:
300 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
302 case PIPE_FUNC_LEQUAL
:
303 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
305 case PIPE_FUNC_GREATER
:
306 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
308 case PIPE_FUNC_GEQUAL
:
309 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
313 return lp_build_undef(type
);
316 #if HAVE_LLVM >= 0x0207
317 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
318 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
320 if (type
.length
== 1) {
321 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
322 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
327 res
= LLVMGetUndef(int_vec_type
);
329 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
330 debug_printf("%s: using slow element-wise int"
331 " vector comparison\n", __FUNCTION__
);
334 for(i
= 0; i
< type
.length
; ++i
) {
335 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
336 cond
= LLVMBuildICmp(builder
, op
,
337 LLVMBuildExtractElement(builder
, a
, index
, ""),
338 LLVMBuildExtractElement(builder
, b
, index
, ""),
340 cond
= LLVMBuildSelect(builder
, cond
,
341 LLVMConstExtractElement(ones
, index
),
342 LLVMConstExtractElement(zeros
, index
),
344 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
356 * Build code to compare two values 'a' and 'b' using the given func.
357 * \param func one of PIPE_FUNC_x
358 * The result values will be 0 for false or ~0 for true.
361 lp_build_cmp(struct lp_build_context
*bld
,
366 return lp_build_compare(bld
->builder
, bld
->type
, func
, a
, b
);
371 * Return (mask & a) | (~mask & b);
374 lp_build_select_bitwise(struct lp_build_context
*bld
,
379 struct lp_type type
= bld
->type
;
382 assert(lp_check_value(type
, a
));
383 assert(lp_check_value(type
, b
));
390 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
391 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
392 b
= LLVMBuildBitCast(bld
->builder
, b
, int_vec_type
, "");
395 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
397 /* This often gets translated to PANDN, but sometimes the NOT is
398 * pre-computed and stored in another constant. The best strategy depends
399 * on available registers, so it is not a big deal -- hopefully LLVM does
400 * the right decision attending the rest of the program.
402 b
= LLVMBuildAnd(bld
->builder
, b
, LLVMBuildNot(bld
->builder
, mask
, ""), "");
404 res
= LLVMBuildOr(bld
->builder
, a
, b
, "");
407 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
408 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
416 * Return mask ? a : b;
418 * mask is a bitwise mask, composed of 0 or ~0 for each element. Any other value
419 * will yield unpredictable results.
422 lp_build_select(struct lp_build_context
*bld
,
427 struct lp_type type
= bld
->type
;
430 assert(lp_check_value(type
, a
));
431 assert(lp_check_value(type
, b
));
436 if (type
.length
== 1) {
437 mask
= LLVMBuildTrunc(bld
->builder
, mask
, LLVMInt1Type(), "");
438 res
= LLVMBuildSelect(bld
->builder
, mask
, a
, b
, "");
440 else if (util_cpu_caps
.has_sse4_1
&&
441 type
.width
* type
.length
== 128 &&
442 !LLVMIsConstant(a
) &&
443 !LLVMIsConstant(b
) &&
444 !LLVMIsConstant(mask
)) {
445 const char *intrinsic
;
446 LLVMTypeRef arg_type
;
447 LLVMValueRef args
[3];
449 if (type
.width
== 64) {
450 intrinsic
= "llvm.x86.sse41.blendvpd";
451 arg_type
= LLVMVectorType(LLVMDoubleType(), 2);
452 } else if (type
.width
== 32) {
453 intrinsic
= "llvm.x86.sse41.blendvps";
454 arg_type
= LLVMVectorType(LLVMFloatType(), 4);
456 intrinsic
= "llvm.x86.sse41.pblendvb";
457 arg_type
= LLVMVectorType(LLVMInt8Type(), 16);
460 if (arg_type
!= bld
->int_vec_type
) {
461 mask
= LLVMBuildBitCast(bld
->builder
, mask
, arg_type
, "");
464 if (arg_type
!= bld
->vec_type
) {
465 a
= LLVMBuildBitCast(bld
->builder
, a
, arg_type
, "");
466 b
= LLVMBuildBitCast(bld
->builder
, b
, arg_type
, "");
473 res
= lp_build_intrinsic(bld
->builder
, intrinsic
,
474 arg_type
, args
, Elements(args
));
476 if (arg_type
!= bld
->vec_type
) {
477 res
= LLVMBuildBitCast(bld
->builder
, res
, bld
->vec_type
, "");
481 res
= lp_build_select_bitwise(bld
, mask
, a
, b
);
489 * Return mask ? a : b;
491 * mask is a TGSI_WRITEMASK_xxx.
494 lp_build_select_aos(struct lp_build_context
*bld
,
499 const struct lp_type type
= bld
->type
;
500 const unsigned n
= type
.length
;
503 assert((mask
& ~0xf) == 0);
504 assert(lp_check_value(type
, a
));
505 assert(lp_check_value(type
, b
));
509 if((mask
& 0xf) == 0xf)
511 if((mask
& 0xf) == 0x0)
513 if(a
== bld
->undef
|| b
== bld
->undef
)
517 * There are three major ways of accomplishing this:
520 * - or with a bit mask.
522 * Select isn't supported for vector types yet.
523 * The flip between these is empirical and might need to be.
529 LLVMTypeRef elem_type
= LLVMInt32Type();
530 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
532 for(j
= 0; j
< n
; j
+= 4)
533 for(i
= 0; i
< 4; ++i
)
534 shuffles
[j
+ i
] = LLVMConstInt(elem_type
,
535 (mask
& (1 << i
) ? 0 : n
) + j
+ i
,
538 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
542 /* XXX: Unfortunately select of vectors do not work */
544 LLVMTypeRef elem_type
= LLVMInt1Type();
545 LLVMValueRef cond_vec
[LP_MAX_VECTOR_LENGTH
];
547 for(j
= 0; j
< n
; j
+= 4)
548 for(i
= 0; i
< 4; ++i
)
549 cond_vec
[j
+ i
] = LLVMConstInt(elem_type
,
550 mask
& (1 << i
) ? 1 : 0, 0);
552 return LLVMBuildSelect(bld
->builder
, LLVMConstVector(cond_vec
, n
), a
, b
, "");
554 LLVMValueRef mask_vec
= lp_build_const_mask_aos(type
, mask
);
555 return lp_build_select(bld
, mask_vec
, a
, b
);