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
50 lp_build_compare(LLVMBuilderRef builder
,
51 const struct lp_type type
,
56 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
57 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
58 LLVMValueRef zeros
= LLVMConstNull(int_vec_type
);
59 LLVMValueRef ones
= LLVMConstAllOnes(int_vec_type
);
64 assert(func
>= PIPE_FUNC_NEVER
);
65 assert(func
<= PIPE_FUNC_ALWAYS
);
67 if(func
== PIPE_FUNC_NEVER
)
69 if(func
== PIPE_FUNC_ALWAYS
)
72 /* TODO: optimize the constant case */
74 /* XXX: It is not clear if we should use the ordered or unordered operators */
76 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
77 if(type
.width
* type
.length
== 128) {
78 if(type
.floating
&& util_cpu_caps
.has_sse
) {
79 /* float[4] comparison */
89 case PIPE_FUNC_NOTEQUAL
:
95 case PIPE_FUNC_LEQUAL
:
98 case PIPE_FUNC_GREATER
:
102 case PIPE_FUNC_GEQUAL
:
108 return lp_build_undef(type
);
120 args
[2] = LLVMConstInt(LLVMInt8Type(), cc
, 0);
121 res
= lp_build_intrinsic(builder
,
122 "llvm.x86.sse.cmp.ps",
125 res
= LLVMBuildBitCast(builder
, res
, int_vec_type
, "");
128 else if(util_cpu_caps
.has_sse2
) {
129 /* int[4] comparison */
130 static const struct {
136 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
137 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
138 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
139 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
140 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
141 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
142 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
143 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
147 LLVMValueRef args
[2];
150 switch (type
.width
) {
152 pcmpeq
= "llvm.x86.sse2.pcmpeq.b";
153 pcmpgt
= "llvm.x86.sse2.pcmpgt.b";
156 pcmpeq
= "llvm.x86.sse2.pcmpeq.w";
157 pcmpgt
= "llvm.x86.sse2.pcmpgt.w";
160 pcmpeq
= "llvm.x86.sse2.pcmpeq.d";
161 pcmpgt
= "llvm.x86.sse2.pcmpgt.d";
165 return lp_build_undef(type
);
168 /* There are no signed byte and unsigned word/dword comparison
169 * instructions. So flip the sign bit so that the results match.
172 ((type
.width
== 8 && type
.sign
) ||
173 (type
.width
!= 8 && !type
.sign
))) {
174 LLVMValueRef msb
= lp_build_int_const_scalar(type
, (unsigned long long)1 << (type
.width
- 1));
175 a
= LLVMBuildXor(builder
, a
, msb
, "");
176 b
= LLVMBuildXor(builder
, b
, msb
, "");
179 if(table
[func
].swap
) {
189 res
= lp_build_intrinsic(builder
, pcmpeq
, vec_type
, args
, 2);
190 else if (table
[func
].gt
)
191 res
= lp_build_intrinsic(builder
, pcmpgt
, vec_type
, args
, 2);
193 res
= LLVMConstNull(vec_type
);
196 res
= LLVMBuildNot(builder
, res
, "");
204 LLVMRealPredicate op
;
206 case PIPE_FUNC_NEVER
:
207 op
= LLVMRealPredicateFalse
;
209 case PIPE_FUNC_ALWAYS
:
210 op
= LLVMRealPredicateTrue
;
212 case PIPE_FUNC_EQUAL
:
215 case PIPE_FUNC_NOTEQUAL
:
221 case PIPE_FUNC_LEQUAL
:
224 case PIPE_FUNC_GREATER
:
227 case PIPE_FUNC_GEQUAL
:
232 return lp_build_undef(type
);
236 /* XXX: Although valid IR, no LLVM target currently support this */
237 cond
= LLVMBuildFCmp(builder
, op
, a
, b
, "");
238 res
= LLVMBuildSelect(builder
, cond
, ones
, zeros
, "");
240 debug_printf("%s: warning: using slow element-wise vector comparison\n",
242 res
= LLVMGetUndef(int_vec_type
);
243 for(i
= 0; i
< type
.length
; ++i
) {
244 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
245 cond
= LLVMBuildFCmp(builder
, op
,
246 LLVMBuildExtractElement(builder
, a
, index
, ""),
247 LLVMBuildExtractElement(builder
, b
, index
, ""),
249 cond
= LLVMBuildSelect(builder
, cond
,
250 LLVMConstExtractElement(ones
, index
),
251 LLVMConstExtractElement(zeros
, index
),
253 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
260 case PIPE_FUNC_EQUAL
:
263 case PIPE_FUNC_NOTEQUAL
:
267 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
269 case PIPE_FUNC_LEQUAL
:
270 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
272 case PIPE_FUNC_GREATER
:
273 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
275 case PIPE_FUNC_GEQUAL
:
276 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
280 return lp_build_undef(type
);
284 /* XXX: Although valid IR, no LLVM target currently support this */
285 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
286 res
= LLVMBuildSelect(builder
, cond
, ones
, zeros
, "");
288 debug_printf("%s: warning: using slow element-wise int vector comparison\n",
290 res
= LLVMGetUndef(int_vec_type
);
291 for(i
= 0; i
< type
.length
; ++i
) {
292 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
293 cond
= LLVMBuildICmp(builder
, op
,
294 LLVMBuildExtractElement(builder
, a
, index
, ""),
295 LLVMBuildExtractElement(builder
, b
, index
, ""),
297 cond
= LLVMBuildSelect(builder
, cond
,
298 LLVMConstExtractElement(ones
, index
),
299 LLVMConstExtractElement(zeros
, index
),
301 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
312 * Build code to compare two values 'a' and 'b' using the given func.
313 * \param func one of PIPE_FUNC_x
316 lp_build_cmp(struct lp_build_context
*bld
,
321 return lp_build_compare(bld
->builder
, bld
->type
, func
, a
, b
);
326 lp_build_select(struct lp_build_context
*bld
,
331 struct lp_type type
= bld
->type
;
338 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
339 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
340 b
= LLVMBuildBitCast(bld
->builder
, b
, int_vec_type
, "");
343 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
345 /* This often gets translated to PANDN, but sometimes the NOT is
346 * pre-computed and stored in another constant. The best strategy depends
347 * on available registers, so it is not a big deal -- hopefully LLVM does
348 * the right decision attending the rest of the program.
350 b
= LLVMBuildAnd(bld
->builder
, b
, LLVMBuildNot(bld
->builder
, mask
, ""), "");
352 res
= LLVMBuildOr(bld
->builder
, a
, b
, "");
355 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
356 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
364 lp_build_select_aos(struct lp_build_context
*bld
,
367 const boolean cond
[4])
369 const struct lp_type type
= bld
->type
;
370 const unsigned n
= type
.length
;
375 if(cond
[0] && cond
[1] && cond
[2] && cond
[3])
377 if(!cond
[0] && !cond
[1] && !cond
[2] && !cond
[3])
379 if(a
== bld
->undef
|| b
== bld
->undef
)
383 * There are three major ways of accomplishing this:
386 * - or with a bit mask.
388 * Select isn't supported for vector types yet.
389 * The flip between these is empirical and might need to be.
395 LLVMTypeRef elem_type
= LLVMInt32Type();
396 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
398 for(j
= 0; j
< n
; j
+= 4)
399 for(i
= 0; i
< 4; ++i
)
400 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, (cond
[i
] ? 0 : n
) + j
+ i
, 0);
402 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
406 /* XXX: Unfortunately select of vectors do not work */
408 LLVMTypeRef elem_type
= LLVMInt1Type();
409 LLVMValueRef cond
[LP_MAX_VECTOR_LENGTH
];
411 for(j
= 0; j
< n
; j
+= 4)
412 for(i
= 0; i
< 4; ++i
)
413 cond
[j
+ i
] = LLVMConstInt(elem_type
, cond
[i
] ? 1 : 0, 0);
415 return LLVMBuildSelect(bld
->builder
, LLVMConstVector(cond
, n
), a
, b
, "");
417 LLVMValueRef mask
= lp_build_const_mask_aos(type
, cond
);
418 return lp_build_select(bld
, mask
, a
, b
);
424 lp_build_alloca(struct lp_build_context
*bld
)
426 const struct lp_type type
= bld
->type
;
428 if (type
.length
> 1) { /*vector*/
429 return LLVMBuildAlloca(bld
->builder
, lp_build_vec_type(type
), "");
431 return LLVMBuildAlloca(bld
->builder
, lp_build_elem_type(type
), "");