llvmpipe: Emit SSE intrinsics based on runtime cpu capability check.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_logic.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Helper functions for logical operations.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include "util/u_cpu_detect.h"
37
38 #include "lp_bld_type.h"
39 #include "lp_bld_const.h"
40 #include "lp_bld_intr.h"
41 #include "lp_bld_logic.h"
42
43
44 LLVMValueRef
45 lp_build_cmp(struct lp_build_context *bld,
46 unsigned func,
47 LLVMValueRef a,
48 LLVMValueRef b)
49 {
50 const struct lp_type type = bld->type;
51 LLVMTypeRef vec_type = lp_build_vec_type(type);
52 LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
53 LLVMValueRef zeros = LLVMConstNull(int_vec_type);
54 LLVMValueRef ones = LLVMConstAllOnes(int_vec_type);
55 LLVMValueRef cond;
56 LLVMValueRef res;
57 unsigned i;
58
59 if(func == PIPE_FUNC_NEVER)
60 return zeros;
61 if(func == PIPE_FUNC_ALWAYS)
62 return ones;
63
64 /* TODO: optimize the constant case */
65
66 /* XXX: It is not clear if we should use the ordered or unordered operators */
67
68 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
69 if(type.width * type.length == 128) {
70 if(type.floating && util_cpu_caps.has_sse) {
71 LLVMValueRef args[3];
72 unsigned cc;
73 boolean swap;
74
75 swap = FALSE;
76 switch(func) {
77 case PIPE_FUNC_EQUAL:
78 cc = 0;
79 break;
80 case PIPE_FUNC_NOTEQUAL:
81 cc = 4;
82 break;
83 case PIPE_FUNC_LESS:
84 cc = 1;
85 break;
86 case PIPE_FUNC_LEQUAL:
87 cc = 2;
88 break;
89 case PIPE_FUNC_GREATER:
90 cc = 1;
91 swap = TRUE;
92 break;
93 case PIPE_FUNC_GEQUAL:
94 cc = 2;
95 swap = TRUE;
96 break;
97 default:
98 assert(0);
99 return bld->undef;
100 }
101
102 if(swap) {
103 args[0] = b;
104 args[1] = a;
105 }
106 else {
107 args[0] = a;
108 args[1] = b;
109 }
110
111 args[2] = LLVMConstInt(LLVMInt8Type(), cc, 0);
112 res = lp_build_intrinsic(bld->builder,
113 "llvm.x86.sse.cmp.ps",
114 vec_type,
115 args, 3);
116 res = LLVMBuildBitCast(bld->builder, res, int_vec_type, "");
117 return res;
118 }
119 else if(util_cpu_caps.has_sse2) {
120 static const struct {
121 unsigned swap:1;
122 unsigned eq:1;
123 unsigned gt:1;
124 unsigned not:1;
125 } table[] = {
126 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
127 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
128 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
129 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
130 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
131 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
132 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
133 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
134 };
135 const char *pcmpeq;
136 const char *pcmpgt;
137 LLVMValueRef args[2];
138 LLVMValueRef res;
139
140 switch (type.width) {
141 case 8:
142 pcmpeq = "llvm.x86.sse2.pcmpeq.b";
143 pcmpgt = "llvm.x86.sse2.pcmpgt.b";
144 break;
145 case 16:
146 pcmpeq = "llvm.x86.sse2.pcmpeq.w";
147 pcmpgt = "llvm.x86.sse2.pcmpgt.w";
148 break;
149 case 32:
150 pcmpeq = "llvm.x86.sse2.pcmpeq.d";
151 pcmpgt = "llvm.x86.sse2.pcmpgt.d";
152 break;
153 default:
154 assert(0);
155 return bld->undef;
156 }
157
158 /* There are no signed byte and unsigned word/dword comparison
159 * instructions. So flip the sign bit so that the results match.
160 */
161 if(table[func].gt &&
162 ((type.width == 8 && type.sign) ||
163 (type.width != 8 && !type.sign))) {
164 LLVMValueRef msb = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
165 a = LLVMBuildXor(bld->builder, a, msb, "");
166 b = LLVMBuildXor(bld->builder, b, msb, "");
167 }
168
169 if(table[func].swap) {
170 args[0] = b;
171 args[1] = a;
172 }
173 else {
174 args[0] = a;
175 args[1] = b;
176 }
177
178 if(table[func].eq)
179 res = lp_build_intrinsic(bld->builder, pcmpeq, vec_type, args, 2);
180 else if (table[func].gt)
181 res = lp_build_intrinsic(bld->builder, pcmpgt, vec_type, args, 2);
182 else
183 res = LLVMConstNull(vec_type);
184
185 if(table[func].not)
186 res = LLVMBuildNot(bld->builder, res, "");
187
188 return res;
189 }
190 }
191 #endif
192
193 if(type.floating) {
194 LLVMRealPredicate op;
195 switch(func) {
196 case PIPE_FUNC_NEVER:
197 op = LLVMRealPredicateFalse;
198 break;
199 case PIPE_FUNC_ALWAYS:
200 op = LLVMRealPredicateTrue;
201 break;
202 case PIPE_FUNC_EQUAL:
203 op = LLVMRealUEQ;
204 break;
205 case PIPE_FUNC_NOTEQUAL:
206 op = LLVMRealUNE;
207 break;
208 case PIPE_FUNC_LESS:
209 op = LLVMRealULT;
210 break;
211 case PIPE_FUNC_LEQUAL:
212 op = LLVMRealULE;
213 break;
214 case PIPE_FUNC_GREATER:
215 op = LLVMRealUGT;
216 break;
217 case PIPE_FUNC_GEQUAL:
218 op = LLVMRealUGE;
219 break;
220 default:
221 assert(0);
222 return bld->undef;
223 }
224
225 #if 0
226 /* XXX: Although valid IR, no LLVM target currently support this */
227 cond = LLVMBuildFCmp(bld->builder, op, a, b, "");
228 res = LLVMBuildSelect(bld->builder, cond, ones, zeros, "");
229 #else
230 debug_printf("%s: warning: using slow element-wise vector comparison\n",
231 __FUNCTION__);
232 res = LLVMGetUndef(int_vec_type);
233 for(i = 0; i < type.length; ++i) {
234 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
235 cond = LLVMBuildFCmp(bld->builder, op,
236 LLVMBuildExtractElement(bld->builder, a, index, ""),
237 LLVMBuildExtractElement(bld->builder, b, index, ""),
238 "");
239 cond = LLVMBuildSelect(bld->builder, cond,
240 LLVMConstExtractElement(ones, index),
241 LLVMConstExtractElement(zeros, index),
242 "");
243 res = LLVMBuildInsertElement(bld->builder, res, cond, index, "");
244 }
245 #endif
246 }
247 else {
248 LLVMIntPredicate op;
249 switch(func) {
250 case PIPE_FUNC_EQUAL:
251 op = LLVMIntEQ;
252 break;
253 case PIPE_FUNC_NOTEQUAL:
254 op = LLVMIntNE;
255 break;
256 case PIPE_FUNC_LESS:
257 op = type.sign ? LLVMIntSLT : LLVMIntULT;
258 break;
259 case PIPE_FUNC_LEQUAL:
260 op = type.sign ? LLVMIntSLE : LLVMIntULE;
261 break;
262 case PIPE_FUNC_GREATER:
263 op = type.sign ? LLVMIntSGT : LLVMIntUGT;
264 break;
265 case PIPE_FUNC_GEQUAL:
266 op = type.sign ? LLVMIntSGE : LLVMIntUGE;
267 break;
268 default:
269 assert(0);
270 return bld->undef;
271 }
272
273 #if 0
274 /* XXX: Although valid IR, no LLVM target currently support this */
275 cond = LLVMBuildICmp(bld->builder, op, a, b, "");
276 res = LLVMBuildSelect(bld->builder, cond, ones, zeros, "");
277 #else
278 debug_printf("%s: warning: using slow element-wise vector comparison\n",
279 __FUNCTION__);
280 res = LLVMGetUndef(int_vec_type);
281 for(i = 0; i < type.length; ++i) {
282 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
283 cond = LLVMBuildICmp(bld->builder, op,
284 LLVMBuildExtractElement(bld->builder, a, index, ""),
285 LLVMBuildExtractElement(bld->builder, b, index, ""),
286 "");
287 cond = LLVMBuildSelect(bld->builder, cond,
288 LLVMConstExtractElement(ones, index),
289 LLVMConstExtractElement(zeros, index),
290 "");
291 res = LLVMBuildInsertElement(bld->builder, res, cond, index, "");
292 }
293 #endif
294 }
295
296 return res;
297 }
298
299
300 LLVMValueRef
301 lp_build_select(struct lp_build_context *bld,
302 LLVMValueRef mask,
303 LLVMValueRef a,
304 LLVMValueRef b)
305 {
306 struct lp_type type = bld->type;
307 LLVMValueRef res;
308
309 if(a == b)
310 return a;
311
312 if(type.floating) {
313 LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
314 a = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
315 b = LLVMBuildBitCast(bld->builder, b, int_vec_type, "");
316 }
317
318 a = LLVMBuildAnd(bld->builder, a, mask, "");
319
320 /* This often gets translated to PANDN, but sometimes the NOT is
321 * pre-computed and stored in another constant. The best strategy depends
322 * on available registers, so it is not a big deal -- hopefully LLVM does
323 * the right decision attending the rest of the program.
324 */
325 b = LLVMBuildAnd(bld->builder, b, LLVMBuildNot(bld->builder, mask, ""), "");
326
327 res = LLVMBuildOr(bld->builder, a, b, "");
328
329 if(type.floating) {
330 LLVMTypeRef vec_type = lp_build_vec_type(type);
331 res = LLVMBuildBitCast(bld->builder, res, vec_type, "");
332 }
333
334 return res;
335 }
336
337
338 LLVMValueRef
339 lp_build_select_aos(struct lp_build_context *bld,
340 LLVMValueRef a,
341 LLVMValueRef b,
342 const boolean cond[4])
343 {
344 const struct lp_type type = bld->type;
345 const unsigned n = type.length;
346 unsigned i, j;
347
348 if(a == b)
349 return a;
350 if(cond[0] && cond[1] && cond[2] && cond[3])
351 return a;
352 if(!cond[0] && !cond[1] && !cond[2] && !cond[3])
353 return b;
354 if(a == bld->undef || b == bld->undef)
355 return bld->undef;
356
357 /*
358 * There are three major ways of accomplishing this:
359 * - with a shuffle,
360 * - with a select,
361 * - or with a bit mask.
362 *
363 * Select isn't supported for vector types yet.
364 * The flip between these is empirical and might need to be.
365 */
366 if (n <= 4) {
367 /*
368 * Shuffle.
369 */
370 LLVMTypeRef elem_type = LLVMInt32Type();
371 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
372
373 for(j = 0; j < n; j += 4)
374 for(i = 0; i < 4; ++i)
375 shuffles[j + i] = LLVMConstInt(elem_type, (cond[i] ? 0 : n) + j + i, 0);
376
377 return LLVMBuildShuffleVector(bld->builder, a, b, LLVMConstVector(shuffles, n), "");
378 }
379 else {
380 #if 0
381 /* XXX: Unfortunately select of vectors do not work */
382 /* Use a select */
383 LLVMTypeRef elem_type = LLVMInt1Type();
384 LLVMValueRef cond[LP_MAX_VECTOR_LENGTH];
385
386 for(j = 0; j < n; j += 4)
387 for(i = 0; i < 4; ++i)
388 cond[j + i] = LLVMConstInt(elem_type, cond[i] ? 1 : 0, 0);
389
390 return LLVMBuildSelect(bld->builder, LLVMConstVector(cond, n), a, b, "");
391 #else
392 LLVMValueRef mask = lp_build_const_mask_aos(type, cond);
393 return lp_build_select(bld, mask, a, b);
394 #endif
395 }
396 }