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
, "");
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 debug_printf("%s: warning: using slow element-wise vector comparison\n",
243 res
= LLVMGetUndef(int_vec_type
);
244 for(i
= 0; i
< type
.length
; ++i
) {
245 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
246 cond
= LLVMBuildFCmp(builder
, op
,
247 LLVMBuildExtractElement(builder
, a
, index
, ""),
248 LLVMBuildExtractElement(builder
, b
, index
, ""),
250 cond
= LLVMBuildSelect(builder
, cond
,
251 LLVMConstExtractElement(ones
, index
),
252 LLVMConstExtractElement(zeros
, index
),
254 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
261 case PIPE_FUNC_EQUAL
:
264 case PIPE_FUNC_NOTEQUAL
:
268 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
270 case PIPE_FUNC_LEQUAL
:
271 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
273 case PIPE_FUNC_GREATER
:
274 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
276 case PIPE_FUNC_GEQUAL
:
277 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
281 return lp_build_undef(type
);
285 /* XXX: Although valid IR, no LLVM target currently support this */
286 cond
= LLVMBuildICmp(builder
, op
, a
, b
, "");
287 res
= LLVMBuildSelect(builder
, cond
, ones
, zeros
, "");
289 debug_printf("%s: warning: using slow element-wise int vector comparison\n",
291 res
= LLVMGetUndef(int_vec_type
);
292 for(i
= 0; i
< type
.length
; ++i
) {
293 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
294 cond
= LLVMBuildICmp(builder
, op
,
295 LLVMBuildExtractElement(builder
, a
, index
, ""),
296 LLVMBuildExtractElement(builder
, b
, index
, ""),
298 cond
= LLVMBuildSelect(builder
, cond
,
299 LLVMConstExtractElement(ones
, index
),
300 LLVMConstExtractElement(zeros
, index
),
302 res
= LLVMBuildInsertElement(builder
, res
, cond
, index
, "");
313 * Build code to compare two values 'a' and 'b' using the given func.
314 * \param func one of PIPE_FUNC_x
315 * The result values will be 0 for false or ~0 for true.
318 lp_build_cmp(struct lp_build_context
*bld
,
323 return lp_build_compare(bld
->builder
, bld
->type
, func
, a
, b
);
328 * Return mask ? a : b;
331 lp_build_select(struct lp_build_context
*bld
,
336 struct lp_type type
= bld
->type
;
343 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
344 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
345 b
= LLVMBuildBitCast(bld
->builder
, b
, int_vec_type
, "");
348 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
350 /* This often gets translated to PANDN, but sometimes the NOT is
351 * pre-computed and stored in another constant. The best strategy depends
352 * on available registers, so it is not a big deal -- hopefully LLVM does
353 * the right decision attending the rest of the program.
355 b
= LLVMBuildAnd(bld
->builder
, b
, LLVMBuildNot(bld
->builder
, mask
, ""), "");
357 res
= LLVMBuildOr(bld
->builder
, a
, b
, "");
360 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
361 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
369 lp_build_select_aos(struct lp_build_context
*bld
,
372 const boolean cond
[4])
374 const struct lp_type type
= bld
->type
;
375 const unsigned n
= type
.length
;
380 if(cond
[0] && cond
[1] && cond
[2] && cond
[3])
382 if(!cond
[0] && !cond
[1] && !cond
[2] && !cond
[3])
384 if(a
== bld
->undef
|| b
== bld
->undef
)
388 * There are three major ways of accomplishing this:
391 * - or with a bit mask.
393 * Select isn't supported for vector types yet.
394 * The flip between these is empirical and might need to be.
400 LLVMTypeRef elem_type
= LLVMInt32Type();
401 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
403 for(j
= 0; j
< n
; j
+= 4)
404 for(i
= 0; i
< 4; ++i
)
405 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, (cond
[i
] ? 0 : n
) + j
+ i
, 0);
407 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
411 /* XXX: Unfortunately select of vectors do not work */
413 LLVMTypeRef elem_type
= LLVMInt1Type();
414 LLVMValueRef cond
[LP_MAX_VECTOR_LENGTH
];
416 for(j
= 0; j
< n
; j
+= 4)
417 for(i
= 0; i
< 4; ++i
)
418 cond
[j
+ i
] = LLVMConstInt(elem_type
, cond
[i
] ? 1 : 0, 0);
420 return LLVMBuildSelect(bld
->builder
, LLVMConstVector(cond
, n
), a
, b
, "");
422 LLVMValueRef mask
= lp_build_const_mask_aos(type
, cond
);
423 return lp_build_select(bld
, mask
, a
, b
);
429 lp_build_alloca(struct lp_build_context
*bld
)
431 const struct lp_type type
= bld
->type
;
433 if (type
.length
> 1) { /*vector*/
434 return LLVMBuildAlloca(bld
->builder
, lp_build_vec_type(type
), "");
436 return LLVMBuildAlloca(bld
->builder
, lp_build_elem_type(type
), "");