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_init.h"
43 #include "lp_bld_intr.h"
44 #include "lp_bld_debug.h"
45 #include "lp_bld_logic.h"
51 * Selection with vector conditional like
53 * select <4 x i1> %C, %A, %B
55 * is valid IR (e.g. llvm/test/Assembler/vector-select.ll), but it is not
56 * supported on any backend.
58 * Expanding the boolean vector to full SIMD register width, as in
60 * sext <4 x i1> %C to <4 x i32>
62 * is valid and supported (e.g., llvm/test/CodeGen/X86/vec_compare.ll), but
63 * it causes assertion failures in LLVM 2.6. It appears to work correctly on
69 * Build code to compare two values 'a' and 'b' of 'type' using the given func.
70 * \param func one of PIPE_FUNC_x
71 * The result values will be 0 for false or ~0 for true.
74 lp_build_compare(struct gallivm_state
*gallivm
,
75 const struct lp_type type
,
80 LLVMBuilderRef builder
= gallivm
->builder
;
81 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(gallivm
, type
);
82 LLVMValueRef zeros
= LLVMConstNull(int_vec_type
);
83 LLVMValueRef ones
= LLVMConstAllOnes(int_vec_type
);
87 assert(func
>= PIPE_FUNC_NEVER
);
88 assert(func
<= PIPE_FUNC_ALWAYS
);
89 assert(lp_check_value(type
, a
));
90 assert(lp_check_value(type
, b
));
92 if(func
== PIPE_FUNC_NEVER
)
94 if(func
== PIPE_FUNC_ALWAYS
)
97 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
99 * There are no unsigned integer comparison instructions in SSE.
102 if (!type
.floating
&& !type
.sign
&&
103 type
.width
* type
.length
== 128 &&
104 util_cpu_caps
.has_sse2
&&
105 (func
== PIPE_FUNC_LESS
||
106 func
== PIPE_FUNC_LEQUAL
||
107 func
== PIPE_FUNC_GREATER
||
108 func
== PIPE_FUNC_GEQUAL
) &&
109 (gallivm_debug
& GALLIVM_DEBUG_PERF
)) {
110 debug_printf("%s: inefficient <%u x i%u> unsigned comparison\n",
111 __FUNCTION__
, type
.length
, type
.width
);
115 #if HAVE_LLVM < 0x0207
116 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
117 if(type
.width
* type
.length
== 128) {
118 if(type
.floating
&& util_cpu_caps
.has_sse
) {
119 /* float[4] comparison */
120 LLVMTypeRef vec_type
= lp_build_vec_type(gallivm
, type
);
121 LLVMValueRef args
[3];
127 case PIPE_FUNC_EQUAL
:
130 case PIPE_FUNC_NOTEQUAL
:
136 case PIPE_FUNC_LEQUAL
:
139 case PIPE_FUNC_GREATER
:
143 case PIPE_FUNC_GEQUAL
:
149 return lp_build_undef(gallivm
, type
);
161 args
[2] = LLVMConstInt(LLVMInt8TypeInContext(gallivm
->context
), cc
, 0);
162 res
= lp_build_intrinsic(builder
,
163 "llvm.x86.sse.cmp.ps",
166 res
= LLVMBuildBitCast(builder
, res
, int_vec_type
, "");
169 else if(util_cpu_caps
.has_sse2
) {
170 /* int[4] comparison */
171 static const struct {
177 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
178 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
179 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
180 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
181 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
182 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
183 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
184 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
188 LLVMValueRef args
[2];
190 LLVMTypeRef vec_type
= lp_build_vec_type(gallivm
, type
);
192 switch (type
.width
) {
194 pcmpeq
= "llvm.x86.sse2.pcmpeq.b";
195 pcmpgt
= "llvm.x86.sse2.pcmpgt.b";
198 pcmpeq
= "llvm.x86.sse2.pcmpeq.w";
199 pcmpgt
= "llvm.x86.sse2.pcmpgt.w";
202 pcmpeq
= "llvm.x86.sse2.pcmpeq.d";
203 pcmpgt
= "llvm.x86.sse2.pcmpgt.d";
207 return lp_build_undef(gallivm
, type
);
210 /* There are no unsigned comparison instructions. So flip the sign bit
211 * so that the results match.
213 if (table
[func
].gt
&& !type
.sign
) {
214 LLVMValueRef msb
= lp_build_const_int_vec(gallivm
, type
, (unsigned long long)1 << (type
.width
- 1));
215 a
= LLVMBuildXor(builder
, a
, msb
, "");
216 b
= LLVMBuildXor(builder
, b
, msb
, "");
219 if(table
[func
].swap
) {
229 res
= lp_build_intrinsic(builder
, pcmpeq
, vec_type
, args
, 2);
230 else if (table
[func
].gt
)
231 res
= lp_build_intrinsic(builder
, pcmpgt
, vec_type
, args
, 2);
233 res
= LLVMConstNull(vec_type
);
236 res
= LLVMBuildNot(builder
, res
, "");
240 } /* if (type.width * type.length == 128) */
242 #endif /* HAVE_LLVM < 0x0207 */
244 /* XXX: It is not clear if we should use the ordered or unordered operators */
247 LLVMRealPredicate op
;
249 case PIPE_FUNC_NEVER
:
250 op
= LLVMRealPredicateFalse
;
252 case PIPE_FUNC_ALWAYS
:
253 op
= LLVMRealPredicateTrue
;
255 case PIPE_FUNC_EQUAL
:
258 case PIPE_FUNC_NOTEQUAL
:
264 case PIPE_FUNC_LEQUAL
:
267 case PIPE_FUNC_GREATER
:
270 case PIPE_FUNC_GEQUAL
:
275 return lp_build_undef(gallivm
, type
);
278 #if HAVE_LLVM >= 0x0207
279 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
280 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
282 if (type
.length
== 1) {
283 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
284 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
289 res
= LLVMGetUndef(int_vec_type
);
291 debug_printf("%s: warning: using slow element-wise float"
292 " vector comparison\n", __FUNCTION__
);
293 for (i
= 0; i
< type
.length
; ++i
) {
294 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
295 cond
= LLVMBuildFCmp(builder
, op
,
296 LLVMBuildExtractElement(builder
, a
, index
, ""),
297 LLVMBuildExtractElement(builder
, b
, index
, ""),
299 cond
= LLVMBuildSelect(builder
, cond
,
300 LLVMConstExtractElement(ones
, index
),
301 LLVMConstExtractElement(zeros
, index
),
303 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
311 case PIPE_FUNC_EQUAL
:
314 case PIPE_FUNC_NOTEQUAL
:
318 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
320 case PIPE_FUNC_LEQUAL
:
321 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
323 case PIPE_FUNC_GREATER
:
324 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
326 case PIPE_FUNC_GEQUAL
:
327 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
331 return lp_build_undef(gallivm
, type
);
334 #if HAVE_LLVM >= 0x0207
335 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
336 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
338 if (type
.length
== 1) {
339 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
340 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
345 res
= LLVMGetUndef(int_vec_type
);
347 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
348 debug_printf("%s: using slow element-wise int"
349 " vector comparison\n", __FUNCTION__
);
352 for(i
= 0; i
< type
.length
; ++i
) {
353 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
354 cond
= LLVMBuildICmp(builder
, op
,
355 LLVMBuildExtractElement(builder
, a
, index
, ""),
356 LLVMBuildExtractElement(builder
, b
, index
, ""),
358 cond
= LLVMBuildSelect(builder
, cond
,
359 LLVMConstExtractElement(ones
, index
),
360 LLVMConstExtractElement(zeros
, index
),
362 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
374 * Build code to compare two values 'a' and 'b' using the given func.
375 * \param func one of PIPE_FUNC_x
376 * The result values will be 0 for false or ~0 for true.
379 lp_build_cmp(struct lp_build_context
*bld
,
384 return lp_build_compare(bld
->gallivm
, bld
->type
, func
, a
, b
);
389 * Return (mask & a) | (~mask & b);
392 lp_build_select_bitwise(struct lp_build_context
*bld
,
397 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
398 struct lp_type type
= bld
->type
;
401 assert(lp_check_value(type
, a
));
402 assert(lp_check_value(type
, b
));
409 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(bld
->gallivm
, type
);
410 a
= LLVMBuildBitCast(builder
, a
, int_vec_type
, "");
411 b
= LLVMBuildBitCast(builder
, b
, int_vec_type
, "");
414 a
= LLVMBuildAnd(builder
, a
, mask
, "");
416 /* This often gets translated to PANDN, but sometimes the NOT is
417 * pre-computed and stored in another constant. The best strategy depends
418 * on available registers, so it is not a big deal -- hopefully LLVM does
419 * the right decision attending the rest of the program.
421 b
= LLVMBuildAnd(builder
, b
, LLVMBuildNot(builder
, mask
, ""), "");
423 res
= LLVMBuildOr(builder
, a
, b
, "");
426 LLVMTypeRef vec_type
= lp_build_vec_type(bld
->gallivm
, type
);
427 res
= LLVMBuildBitCast(builder
, res
, vec_type
, "");
435 * Return mask ? a : b;
437 * mask is a bitwise mask, composed of 0 or ~0 for each element. Any other value
438 * will yield unpredictable results.
441 lp_build_select(struct lp_build_context
*bld
,
446 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
447 LLVMContextRef lc
= bld
->gallivm
->context
;
448 struct lp_type type
= bld
->type
;
451 assert(lp_check_value(type
, a
));
452 assert(lp_check_value(type
, b
));
457 if (type
.length
== 1) {
458 mask
= LLVMBuildTrunc(builder
, mask
, LLVMInt1TypeInContext(lc
), "");
459 res
= LLVMBuildSelect(builder
, mask
, a
, b
, "");
461 else if (util_cpu_caps
.has_sse4_1
&&
462 type
.width
* type
.length
== 128 &&
463 !LLVMIsConstant(a
) &&
464 !LLVMIsConstant(b
) &&
465 !LLVMIsConstant(mask
)) {
466 const char *intrinsic
;
467 LLVMTypeRef arg_type
;
468 LLVMValueRef args
[3];
472 intrinsic
= "llvm.x86.sse41.blendvpd";
473 arg_type
= LLVMVectorType(LLVMDoubleTypeInContext(lc
), 2);
474 } else if (type
.floating
&&
476 intrinsic
= "llvm.x86.sse41.blendvps";
477 arg_type
= LLVMVectorType(LLVMFloatTypeInContext(lc
), 4);
479 intrinsic
= "llvm.x86.sse41.pblendvb";
480 arg_type
= LLVMVectorType(LLVMInt8TypeInContext(lc
), 16);
483 if (arg_type
!= bld
->int_vec_type
) {
484 mask
= LLVMBuildBitCast(builder
, mask
, arg_type
, "");
487 if (arg_type
!= bld
->vec_type
) {
488 a
= LLVMBuildBitCast(builder
, a
, arg_type
, "");
489 b
= LLVMBuildBitCast(builder
, b
, arg_type
, "");
496 res
= lp_build_intrinsic(builder
, intrinsic
,
497 arg_type
, args
, Elements(args
));
499 if (arg_type
!= bld
->vec_type
) {
500 res
= LLVMBuildBitCast(builder
, res
, bld
->vec_type
, "");
504 res
= lp_build_select_bitwise(bld
, mask
, a
, b
);
512 * Return mask ? a : b;
514 * mask is a TGSI_WRITEMASK_xxx.
517 lp_build_select_aos(struct lp_build_context
*bld
,
522 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
523 const struct lp_type type
= bld
->type
;
524 const unsigned n
= type
.length
;
527 assert((mask
& ~0xf) == 0);
528 assert(lp_check_value(type
, a
));
529 assert(lp_check_value(type
, b
));
533 if((mask
& 0xf) == 0xf)
535 if((mask
& 0xf) == 0x0)
537 if(a
== bld
->undef
|| b
== bld
->undef
)
541 * There are three major ways of accomplishing this:
544 * - or with a bit mask.
546 * Select isn't supported for vector types yet.
547 * The flip between these is empirical and might need to be.
553 LLVMTypeRef elem_type
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
554 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
556 for(j
= 0; j
< n
; j
+= 4)
557 for(i
= 0; i
< 4; ++i
)
558 shuffles
[j
+ i
] = LLVMConstInt(elem_type
,
559 (mask
& (1 << i
) ? 0 : n
) + j
+ i
,
562 return LLVMBuildShuffleVector(builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
566 /* XXX: Unfortunately select of vectors do not work */
568 LLVMTypeRef elem_type
= LLVMInt1Type();
569 LLVMValueRef cond_vec
[LP_MAX_VECTOR_LENGTH
];
571 for(j
= 0; j
< n
; j
+= 4)
572 for(i
= 0; i
< 4; ++i
)
573 cond_vec
[j
+ i
] = LLVMConstInt(elem_type
,
574 mask
& (1 << i
) ? 1 : 0, 0);
576 return LLVMBuildSelect(builder
, LLVMConstVector(cond_vec
, n
), a
, b
, "");
578 LLVMValueRef mask_vec
= lp_build_const_mask_aos(bld
->gallivm
, type
, mask
);
579 return lp_build_select(bld
, mask_vec
, a
, b
);