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_logic.h"
49 * Selection with vector conditional like
51 * select <4 x i1> %C, %A, %B
53 * is valid IR (e.g. llvm/test/Assembler/vector-select.ll), but it is not
54 * supported on any backend.
56 * Expanding the boolean vector to full SIMD register width, as in
58 * sext <4 x i1> %C to <4 x i32>
60 * is valid and supported (e.g., llvm/test/CodeGen/X86/vec_compare.ll), but
61 * it causes assertion failures in LLVM 2.6. It appears to work correctly on
67 * Build code to compare two values 'a' and 'b' of 'type' using the given func.
68 * \param func one of PIPE_FUNC_x
69 * The result values will be 0 for false or ~0 for true.
72 lp_build_compare(LLVMBuilderRef builder
,
73 const struct lp_type type
,
78 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
79 LLVMValueRef zeros
= LLVMConstNull(int_vec_type
);
80 LLVMValueRef ones
= LLVMConstAllOnes(int_vec_type
);
84 assert(func
>= PIPE_FUNC_NEVER
);
85 assert(func
<= PIPE_FUNC_ALWAYS
);
87 if(func
== PIPE_FUNC_NEVER
)
89 if(func
== PIPE_FUNC_ALWAYS
)
92 /* TODO: optimize the constant case */
94 /* XXX: It is not clear if we should use the ordered or unordered operators */
96 #if HAVE_LLVM < 0x0207
97 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
98 if(type
.width
* type
.length
== 128) {
99 if(type
.floating
&& util_cpu_caps
.has_sse
) {
100 /* float[4] comparison */
101 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
102 LLVMValueRef args
[3];
108 case PIPE_FUNC_EQUAL
:
111 case PIPE_FUNC_NOTEQUAL
:
117 case PIPE_FUNC_LEQUAL
:
120 case PIPE_FUNC_GREATER
:
124 case PIPE_FUNC_GEQUAL
:
130 return lp_build_undef(type
);
142 args
[2] = LLVMConstInt(LLVMInt8Type(), cc
, 0);
143 res
= lp_build_intrinsic(builder
,
144 "llvm.x86.sse.cmp.ps",
147 res
= LLVMBuildBitCast(builder
, res
, int_vec_type
, "");
150 else if(util_cpu_caps
.has_sse2
) {
151 /* int[4] comparison */
152 static const struct {
158 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
159 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
160 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
161 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
162 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
163 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
164 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
165 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
169 LLVMValueRef args
[2];
171 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
173 switch (type
.width
) {
175 pcmpeq
= "llvm.x86.sse2.pcmpeq.b";
176 pcmpgt
= "llvm.x86.sse2.pcmpgt.b";
179 pcmpeq
= "llvm.x86.sse2.pcmpeq.w";
180 pcmpgt
= "llvm.x86.sse2.pcmpgt.w";
183 pcmpeq
= "llvm.x86.sse2.pcmpeq.d";
184 pcmpgt
= "llvm.x86.sse2.pcmpgt.d";
188 return lp_build_undef(type
);
191 /* There are no unsigned comparison instructions. So flip the sign bit
192 * so that the results match.
194 if (table
[func
].gt
&& !type
.sign
) {
195 LLVMValueRef msb
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
196 a
= LLVMBuildXor(builder
, a
, msb
, "");
197 b
= LLVMBuildXor(builder
, b
, msb
, "");
200 if(table
[func
].swap
) {
210 res
= lp_build_intrinsic(builder
, pcmpeq
, vec_type
, args
, 2);
211 else if (table
[func
].gt
)
212 res
= lp_build_intrinsic(builder
, pcmpgt
, vec_type
, args
, 2);
214 res
= LLVMConstNull(vec_type
);
217 res
= LLVMBuildNot(builder
, res
, "");
221 } /* if (type.width * type.length == 128) */
223 #endif /* HAVE_LLVM < 0x0207 */
226 LLVMRealPredicate op
;
228 case PIPE_FUNC_NEVER
:
229 op
= LLVMRealPredicateFalse
;
231 case PIPE_FUNC_ALWAYS
:
232 op
= LLVMRealPredicateTrue
;
234 case PIPE_FUNC_EQUAL
:
237 case PIPE_FUNC_NOTEQUAL
:
243 case PIPE_FUNC_LEQUAL
:
246 case PIPE_FUNC_GREATER
:
249 case PIPE_FUNC_GEQUAL
:
254 return lp_build_undef(type
);
257 #if HAVE_LLVM >= 0x0207
258 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
259 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
261 if (type
.length
== 1) {
262 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
263 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
268 res
= LLVMGetUndef(int_vec_type
);
270 debug_printf("%s: warning: using slow element-wise float"
271 " vector comparison\n", __FUNCTION__
);
272 for (i
= 0; i
< type
.length
; ++i
) {
273 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
274 cond
= LLVMBuildFCmp(builder
, op
,
275 LLVMBuildExtractElement(builder
, a
, index
, ""),
276 LLVMBuildExtractElement(builder
, b
, index
, ""),
278 cond
= LLVMBuildSelect(builder
, cond
,
279 LLVMConstExtractElement(ones
, index
),
280 LLVMConstExtractElement(zeros
, index
),
282 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
290 case PIPE_FUNC_EQUAL
:
293 case PIPE_FUNC_NOTEQUAL
:
297 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
299 case PIPE_FUNC_LEQUAL
:
300 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
302 case PIPE_FUNC_GREATER
:
303 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
305 case PIPE_FUNC_GEQUAL
:
306 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
310 return lp_build_undef(type
);
313 #if HAVE_LLVM >= 0x0207
314 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
315 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
317 if (type
.length
== 1) {
318 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
319 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
324 res
= LLVMGetUndef(int_vec_type
);
326 debug_printf("%s: warning: using slow element-wise int"
327 " vector comparison\n", __FUNCTION__
);
329 for(i
= 0; i
< type
.length
; ++i
) {
330 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
331 cond
= LLVMBuildICmp(builder
, op
,
332 LLVMBuildExtractElement(builder
, a
, index
, ""),
333 LLVMBuildExtractElement(builder
, b
, index
, ""),
335 cond
= LLVMBuildSelect(builder
, cond
,
336 LLVMConstExtractElement(ones
, index
),
337 LLVMConstExtractElement(zeros
, index
),
339 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
351 * Build code to compare two values 'a' and 'b' using the given func.
352 * \param func one of PIPE_FUNC_x
353 * The result values will be 0 for false or ~0 for true.
356 lp_build_cmp(struct lp_build_context
*bld
,
361 return lp_build_compare(bld
->builder
, bld
->type
, func
, a
, b
);
366 * Return (mask & a) | (~mask & b);
369 lp_build_select_bitwise(struct lp_build_context
*bld
,
374 struct lp_type type
= bld
->type
;
382 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
383 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
384 b
= LLVMBuildBitCast(bld
->builder
, b
, int_vec_type
, "");
387 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
389 /* This often gets translated to PANDN, but sometimes the NOT is
390 * pre-computed and stored in another constant. The best strategy depends
391 * on available registers, so it is not a big deal -- hopefully LLVM does
392 * the right decision attending the rest of the program.
394 b
= LLVMBuildAnd(bld
->builder
, b
, LLVMBuildNot(bld
->builder
, mask
, ""), "");
396 res
= LLVMBuildOr(bld
->builder
, a
, b
, "");
399 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
400 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
408 * Return mask ? a : b;
410 * mask is a bitwise mask, composed of 0 or ~0 for each element. Any other value
411 * will yield unpredictable results.
414 lp_build_select(struct lp_build_context
*bld
,
419 struct lp_type type
= bld
->type
;
425 if (type
.length
== 1) {
426 mask
= LLVMBuildTrunc(bld
->builder
, mask
, LLVMInt1Type(), "");
427 res
= LLVMBuildSelect(bld
->builder
, mask
, a
, b
, "");
429 else if (util_cpu_caps
.has_sse4_1
&&
430 type
.width
* type
.length
== 128 &&
431 !LLVMIsConstant(a
) &&
432 !LLVMIsConstant(b
) &&
433 !LLVMIsConstant(mask
)) {
434 const char *intrinsic
;
435 LLVMTypeRef arg_type
;
436 LLVMValueRef args
[3];
438 if (type
.width
== 64) {
439 intrinsic
= "llvm.x86.sse41.blendvpd";
440 arg_type
= LLVMVectorType(LLVMDoubleType(), 2);
441 } else if (type
.width
== 32) {
442 intrinsic
= "llvm.x86.sse41.blendvps";
443 arg_type
= LLVMVectorType(LLVMFloatType(), 4);
445 intrinsic
= "llvm.x86.sse41.pblendvb";
446 arg_type
= LLVMVectorType(LLVMInt8Type(), 16);
449 if (arg_type
!= bld
->int_vec_type
) {
450 mask
= LLVMBuildBitCast(bld
->builder
, mask
, arg_type
, "");
453 if (arg_type
!= bld
->vec_type
) {
454 a
= LLVMBuildBitCast(bld
->builder
, a
, arg_type
, "");
455 b
= LLVMBuildBitCast(bld
->builder
, b
, arg_type
, "");
462 res
= lp_build_intrinsic(bld
->builder
, intrinsic
,
463 arg_type
, args
, Elements(args
));
465 if (arg_type
!= bld
->vec_type
) {
466 res
= LLVMBuildBitCast(bld
->builder
, res
, bld
->vec_type
, "");
470 res
= lp_build_select_bitwise(bld
, mask
, a
, b
);
478 lp_build_select_aos(struct lp_build_context
*bld
,
481 const boolean cond
[4])
483 const struct lp_type type
= bld
->type
;
484 const unsigned n
= type
.length
;
489 if(cond
[0] && cond
[1] && cond
[2] && cond
[3])
491 if(!cond
[0] && !cond
[1] && !cond
[2] && !cond
[3])
493 if(a
== bld
->undef
|| b
== bld
->undef
)
497 * There are three major ways of accomplishing this:
500 * - or with a bit mask.
502 * Select isn't supported for vector types yet.
503 * The flip between these is empirical and might need to be.
509 LLVMTypeRef elem_type
= LLVMInt32Type();
510 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
512 for(j
= 0; j
< n
; j
+= 4)
513 for(i
= 0; i
< 4; ++i
)
514 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, (cond
[i
] ? 0 : n
) + j
+ i
, 0);
516 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
520 /* XXX: Unfortunately select of vectors do not work */
522 LLVMTypeRef elem_type
= LLVMInt1Type();
523 LLVMValueRef cond
[LP_MAX_VECTOR_LENGTH
];
525 for(j
= 0; j
< n
; j
+= 4)
526 for(i
= 0; i
< 4; ++i
)
527 cond
[j
+ i
] = LLVMConstInt(elem_type
, cond
[i
] ? 1 : 0, 0);
529 return LLVMBuildSelect(bld
->builder
, LLVMConstVector(cond
, n
), a
, b
, "");
531 LLVMValueRef mask
= lp_build_const_mask_aos(type
, cond
);
532 return lp_build_select(bld
, mask
, a
, b
);
538 /** Return (a & ~b) */
540 lp_build_andc(struct lp_build_context
*bld
, LLVMValueRef a
, LLVMValueRef b
)
542 b
= LLVMBuildNot(bld
->builder
, b
, "");
543 b
= LLVMBuildAnd(bld
->builder
, a
, b
, "");