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_debug.h"
39 #include "lp_bld_type.h"
40 #include "lp_bld_const.h"
41 #include "lp_bld_intr.h"
42 #include "lp_bld_logic.h"
48 * Selection with vector conditional like
50 * select <4 x i1> %C, %A, %B
52 * is valid IR (e.g. llvm/test/Assembler/vector-select.ll), but it is not
53 * supported on any backend.
55 * Expanding the boolean vector to full SIMD register width, as in
57 * sext <4 x i1> %C to <4 x i32>
59 * is valid and supported (e.g., llvm/test/CodeGen/X86/vec_compare.ll), but
60 * it causes assertion failures in LLVM 2.6. It appears to work correctly on
66 * Build code to compare two values 'a' and 'b' of 'type' using the given func.
67 * \param func one of PIPE_FUNC_x
68 * The result values will be 0 for false or ~0 for true.
71 lp_build_compare(LLVMBuilderRef builder
,
72 const struct lp_type type
,
77 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
78 LLVMValueRef zeros
= LLVMConstNull(int_vec_type
);
79 LLVMValueRef ones
= LLVMConstAllOnes(int_vec_type
);
83 assert(func
>= PIPE_FUNC_NEVER
);
84 assert(func
<= PIPE_FUNC_ALWAYS
);
86 if(func
== PIPE_FUNC_NEVER
)
88 if(func
== PIPE_FUNC_ALWAYS
)
91 /* TODO: optimize the constant case */
93 /* XXX: It is not clear if we should use the ordered or unordered operators */
95 #if HAVE_LLVM < 0x0207
96 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
97 if(type
.width
* type
.length
== 128) {
98 if(type
.floating
&& util_cpu_caps
.has_sse
) {
99 /* float[4] comparison */
100 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
101 LLVMValueRef args
[3];
107 case PIPE_FUNC_EQUAL
:
110 case PIPE_FUNC_NOTEQUAL
:
116 case PIPE_FUNC_LEQUAL
:
119 case PIPE_FUNC_GREATER
:
123 case PIPE_FUNC_GEQUAL
:
129 return lp_build_undef(type
);
141 args
[2] = LLVMConstInt(LLVMInt8Type(), cc
, 0);
142 res
= lp_build_intrinsic(builder
,
143 "llvm.x86.sse.cmp.ps",
146 res
= LLVMBuildBitCast(builder
, res
, int_vec_type
, "");
149 else if(util_cpu_caps
.has_sse2
) {
150 /* int[4] comparison */
151 static const struct {
157 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
158 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
159 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
160 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
161 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
162 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
163 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
164 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
168 LLVMValueRef args
[2];
170 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
172 switch (type
.width
) {
174 pcmpeq
= "llvm.x86.sse2.pcmpeq.b";
175 pcmpgt
= "llvm.x86.sse2.pcmpgt.b";
178 pcmpeq
= "llvm.x86.sse2.pcmpeq.w";
179 pcmpgt
= "llvm.x86.sse2.pcmpgt.w";
182 pcmpeq
= "llvm.x86.sse2.pcmpeq.d";
183 pcmpgt
= "llvm.x86.sse2.pcmpgt.d";
187 return lp_build_undef(type
);
190 /* There are no unsigned comparison instructions. So flip the sign bit
191 * so that the results match.
193 if (table
[func
].gt
&& !type
.sign
) {
194 LLVMValueRef msb
= lp_build_const_int_vec(type
, (unsigned long long)1 << (type
.width
- 1));
195 a
= LLVMBuildXor(builder
, a
, msb
, "");
196 b
= LLVMBuildXor(builder
, b
, msb
, "");
199 if(table
[func
].swap
) {
209 res
= lp_build_intrinsic(builder
, pcmpeq
, vec_type
, args
, 2);
210 else if (table
[func
].gt
)
211 res
= lp_build_intrinsic(builder
, pcmpgt
, vec_type
, args
, 2);
213 res
= LLVMConstNull(vec_type
);
216 res
= LLVMBuildNot(builder
, res
, "");
220 } /* if (type.width * type.length == 128) */
222 #endif /* HAVE_LLVM < 0x0207 */
225 LLVMRealPredicate op
;
227 case PIPE_FUNC_NEVER
:
228 op
= LLVMRealPredicateFalse
;
230 case PIPE_FUNC_ALWAYS
:
231 op
= LLVMRealPredicateTrue
;
233 case PIPE_FUNC_EQUAL
:
236 case PIPE_FUNC_NOTEQUAL
:
242 case PIPE_FUNC_LEQUAL
:
245 case PIPE_FUNC_GREATER
:
248 case PIPE_FUNC_GEQUAL
:
253 return lp_build_undef(type
);
256 #if HAVE_LLVM >= 0x0207
257 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
258 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
260 if (type
.length
== 1) {
261 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
262 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
267 res
= LLVMGetUndef(int_vec_type
);
269 debug_printf("%s: warning: using slow element-wise float"
270 " vector comparison\n", __FUNCTION__
);
271 for (i
= 0; i
< type
.length
; ++i
) {
272 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
273 cond
= LLVMBuildFCmp(builder
, op
,
274 LLVMBuildExtractElement(builder
, a
, index
, ""),
275 LLVMBuildExtractElement(builder
, b
, index
, ""),
277 cond
= LLVMBuildSelect(builder
, cond
,
278 LLVMConstExtractElement(ones
, index
),
279 LLVMConstExtractElement(zeros
, index
),
281 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
289 case PIPE_FUNC_EQUAL
:
292 case PIPE_FUNC_NOTEQUAL
:
296 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
298 case PIPE_FUNC_LEQUAL
:
299 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
301 case PIPE_FUNC_GREATER
:
302 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
304 case PIPE_FUNC_GEQUAL
:
305 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
309 return lp_build_undef(type
);
312 #if HAVE_LLVM >= 0x0207
313 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
314 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
316 if (type
.length
== 1) {
317 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
318 res
= LLVMBuildSExt(builder
, cond
, int_vec_type
, "");
323 res
= LLVMGetUndef(int_vec_type
);
325 debug_printf("%s: warning: using slow element-wise int"
326 " vector comparison\n", __FUNCTION__
);
328 for(i
= 0; i
< type
.length
; ++i
) {
329 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
330 cond
= LLVMBuildICmp(builder
, op
,
331 LLVMBuildExtractElement(builder
, a
, index
, ""),
332 LLVMBuildExtractElement(builder
, b
, index
, ""),
334 cond
= LLVMBuildSelect(builder
, cond
,
335 LLVMConstExtractElement(ones
, index
),
336 LLVMConstExtractElement(zeros
, index
),
338 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
350 * Build code to compare two values 'a' and 'b' using the given func.
351 * \param func one of PIPE_FUNC_x
352 * The result values will be 0 for false or ~0 for true.
355 lp_build_cmp(struct lp_build_context
*bld
,
360 return lp_build_compare(bld
->builder
, bld
->type
, func
, a
, b
);
365 * Return mask ? a : b;
367 * mask is a bitwise mask, composed of 0 or ~0 for each element.
370 lp_build_select(struct lp_build_context
*bld
,
375 struct lp_type type
= bld
->type
;
381 if (type
.length
== 1) {
382 mask
= LLVMBuildTrunc(bld
->builder
, mask
, LLVMInt1Type(), "");
383 res
= LLVMBuildSelect(bld
->builder
, mask
, a
, b
, "");
387 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
388 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
389 b
= LLVMBuildBitCast(bld
->builder
, b
, int_vec_type
, "");
392 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
394 /* This often gets translated to PANDN, but sometimes the NOT is
395 * pre-computed and stored in another constant. The best strategy depends
396 * on available registers, so it is not a big deal -- hopefully LLVM does
397 * the right decision attending the rest of the program.
399 b
= LLVMBuildAnd(bld
->builder
, b
, LLVMBuildNot(bld
->builder
, mask
, ""), "");
401 res
= LLVMBuildOr(bld
->builder
, a
, b
, "");
404 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
405 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
414 lp_build_select_aos(struct lp_build_context
*bld
,
417 const boolean cond
[4])
419 const struct lp_type type
= bld
->type
;
420 const unsigned n
= type
.length
;
425 if(cond
[0] && cond
[1] && cond
[2] && cond
[3])
427 if(!cond
[0] && !cond
[1] && !cond
[2] && !cond
[3])
429 if(a
== bld
->undef
|| b
== bld
->undef
)
433 * There are three major ways of accomplishing this:
436 * - or with a bit mask.
438 * Select isn't supported for vector types yet.
439 * The flip between these is empirical and might need to be.
445 LLVMTypeRef elem_type
= LLVMInt32Type();
446 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
448 for(j
= 0; j
< n
; j
+= 4)
449 for(i
= 0; i
< 4; ++i
)
450 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, (cond
[i
] ? 0 : n
) + j
+ i
, 0);
452 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
456 /* XXX: Unfortunately select of vectors do not work */
458 LLVMTypeRef elem_type
= LLVMInt1Type();
459 LLVMValueRef cond
[LP_MAX_VECTOR_LENGTH
];
461 for(j
= 0; j
< n
; j
+= 4)
462 for(i
= 0; i
< 4; ++i
)
463 cond
[j
+ i
] = LLVMConstInt(elem_type
, cond
[i
] ? 1 : 0, 0);
465 return LLVMBuildSelect(bld
->builder
, LLVMConstVector(cond
, n
), a
, b
, "");
467 LLVMValueRef mask
= lp_build_const_mask_aos(type
, cond
);
468 return lp_build_select(bld
, mask
, a
, b
);
474 /** Return (a & ~b) */
476 lp_build_andc(struct lp_build_context
*bld
, LLVMValueRef a
, LLVMValueRef b
)
478 b
= LLVMBuildNot(bld
->builder
, b
, "");
479 b
= LLVMBuildAnd(bld
->builder
, a
, b
, "");