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"
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.
51 lp_build_compare(LLVMBuilderRef builder
,
52 const struct lp_type type
,
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
);
65 assert(func
>= PIPE_FUNC_NEVER
);
66 assert(func
<= PIPE_FUNC_ALWAYS
);
68 if(func
== PIPE_FUNC_NEVER
)
70 if(func
== PIPE_FUNC_ALWAYS
)
73 /* TODO: optimize the constant case */
75 /* XXX: It is not clear if we should use the ordered or unordered operators */
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 */
90 case PIPE_FUNC_NOTEQUAL
:
96 case PIPE_FUNC_LEQUAL
:
99 case PIPE_FUNC_GREATER
:
103 case PIPE_FUNC_GEQUAL
:
109 return lp_build_undef(type
);
121 args
[2] = LLVMConstInt(LLVMInt8Type(), cc
, 0);
122 res
= lp_build_intrinsic(builder
,
123 "llvm.x86.sse.cmp.ps",
126 res
= LLVMBuildBitCast(builder
, res
, int_vec_type
, "");
129 else if(util_cpu_caps
.has_sse2
) {
130 /* int[4] comparison */
131 static const struct {
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 */
148 LLVMValueRef args
[2];
151 switch (type
.width
) {
153 pcmpeq
= "llvm.x86.sse2.pcmpeq.b";
154 pcmpgt
= "llvm.x86.sse2.pcmpgt.b";
157 pcmpeq
= "llvm.x86.sse2.pcmpeq.w";
158 pcmpgt
= "llvm.x86.sse2.pcmpgt.w";
161 pcmpeq
= "llvm.x86.sse2.pcmpeq.d";
162 pcmpgt
= "llvm.x86.sse2.pcmpgt.d";
166 return lp_build_undef(type
);
169 /* There are no signed byte and unsigned word/dword comparison
170 * instructions. So flip the sign bit so that the results match.
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
, "");
180 if(table
[func
].swap
) {
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);
194 res
= LLVMConstNull(vec_type
);
197 res
= LLVMBuildNot(builder
, res
, "");
201 } /* if (type.width * type.length == 128) */
205 LLVMRealPredicate op
;
207 case PIPE_FUNC_NEVER
:
208 op
= LLVMRealPredicateFalse
;
210 case PIPE_FUNC_ALWAYS
:
211 op
= LLVMRealPredicateTrue
;
213 case PIPE_FUNC_EQUAL
:
216 case PIPE_FUNC_NOTEQUAL
:
222 case PIPE_FUNC_LEQUAL
:
225 case PIPE_FUNC_GREATER
:
228 case PIPE_FUNC_GEQUAL
:
233 return lp_build_undef(type
);
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
, "");
241 res
= LLVMGetUndef(int_vec_type
);
242 if (type
.length
== 1) {
243 res
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
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
, ""),
254 cond
= LLVMBuildSelect(builder
, cond
,
255 LLVMConstExtractElement(ones
, index
),
256 LLVMConstExtractElement(zeros
, index
),
258 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
266 case PIPE_FUNC_EQUAL
:
269 case PIPE_FUNC_NOTEQUAL
:
273 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
275 case PIPE_FUNC_LEQUAL
:
276 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
278 case PIPE_FUNC_GREATER
:
279 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
281 case PIPE_FUNC_GEQUAL
:
282 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
286 return lp_build_undef(type
);
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
, "");
294 res
= LLVMGetUndef(int_vec_type
);
295 if (type
.length
== 1) {
296 res
= LLVMBuildICmp(builder
, op
, a
, b
, "");
299 debug_printf("%s: warning: using slow element-wise int"
300 " vector comparison\n", __FUNCTION__
);
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
, ""),
308 cond
= LLVMBuildSelect(builder
, cond
,
309 LLVMConstExtractElement(ones
, index
),
310 LLVMConstExtractElement(zeros
, index
),
312 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
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.
329 lp_build_cmp(struct lp_build_context
*bld
,
334 return lp_build_compare(bld
->builder
, bld
->type
, func
, a
, b
);
339 * Return mask ? a : b;
342 lp_build_select(struct lp_build_context
*bld
,
347 struct lp_type type
= bld
->type
;
353 if (type
.length
== 1) {
354 res
= LLVMBuildSelect(bld
->builder
, mask
, a
, b
, "");
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
, "");
363 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
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.
370 b
= LLVMBuildAnd(bld
->builder
, b
, LLVMBuildNot(bld
->builder
, mask
, ""), "");
372 res
= LLVMBuildOr(bld
->builder
, a
, b
, "");
375 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
376 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
385 lp_build_select_aos(struct lp_build_context
*bld
,
388 const boolean cond
[4])
390 const struct lp_type type
= bld
->type
;
391 const unsigned n
= type
.length
;
396 if(cond
[0] && cond
[1] && cond
[2] && cond
[3])
398 if(!cond
[0] && !cond
[1] && !cond
[2] && !cond
[3])
400 if(a
== bld
->undef
|| b
== bld
->undef
)
404 * There are three major ways of accomplishing this:
407 * - or with a bit mask.
409 * Select isn't supported for vector types yet.
410 * The flip between these is empirical and might need to be.
416 LLVMTypeRef elem_type
= LLVMInt32Type();
417 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
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);
423 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
427 /* XXX: Unfortunately select of vectors do not work */
429 LLVMTypeRef elem_type
= LLVMInt1Type();
430 LLVMValueRef cond
[LP_MAX_VECTOR_LENGTH
];
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);
436 return LLVMBuildSelect(bld
->builder
, LLVMConstVector(cond
, n
), a
, b
, "");
438 LLVMValueRef mask
= lp_build_const_mask_aos(type
, cond
);
439 return lp_build_select(bld
, mask
, a
, b
);
445 lp_build_alloca(struct lp_build_context
*bld
)
447 const struct lp_type type
= bld
->type
;
449 if (type
.length
> 1) { /*vector*/
450 return LLVMBuildAlloca(bld
->builder
, lp_build_vec_type(type
), "");
452 return LLVMBuildAlloca(bld
->builder
, lp_build_elem_type(type
), "");