• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "ir_builder.h"
25 #include "program/prog_instruction.h"
26 
27 using namespace ir_builder;
28 
29 namespace ir_builder {
30 
31 void
emit(ir_instruction * ir)32 ir_factory::emit(ir_instruction *ir)
33 {
34    instructions->push_tail(ir);
35 }
36 
37 ir_variable *
make_temp(const glsl_type * type,const char * name)38 ir_factory::make_temp(const glsl_type *type, const char *name)
39 {
40    ir_variable *var;
41 
42    var = new(mem_ctx) ir_variable(type, name, ir_var_temporary);
43    emit(var);
44 
45    return var;
46 }
47 
48 ir_assignment *
assign(deref lhs,operand rhs)49 assign(deref lhs, operand rhs)
50 {
51    return assign(lhs, rhs, (1 << lhs.val->type->vector_elements) - 1);
52 }
53 
54 ir_assignment *
assign(deref lhs,operand rhs,int writemask)55 assign(deref lhs, operand rhs, int writemask)
56 {
57    void *mem_ctx = ralloc_parent(lhs.val);
58 
59    ir_assignment *assign = new(mem_ctx) ir_assignment(lhs.val,
60                                                       rhs.val,
61                                                       writemask);
62 
63    return assign;
64 }
65 
66 ir_return *
ret(operand retval)67 ret(operand retval)
68 {
69    void *mem_ctx = ralloc_parent(retval.val);
70    return new(mem_ctx) ir_return(retval.val);
71 }
72 
73 ir_swizzle *
swizzle(operand a,int swizzle,int components)74 swizzle(operand a, int swizzle, int components)
75 {
76    void *mem_ctx = ralloc_parent(a.val);
77 
78    return new(mem_ctx) ir_swizzle(a.val,
79                                   GET_SWZ(swizzle, 0),
80                                   GET_SWZ(swizzle, 1),
81                                   GET_SWZ(swizzle, 2),
82                                   GET_SWZ(swizzle, 3),
83                                   components);
84 }
85 
86 ir_swizzle *
swizzle_for_size(operand a,unsigned components)87 swizzle_for_size(operand a, unsigned components)
88 {
89    void *mem_ctx = ralloc_parent(a.val);
90 
91    if (a.val->type->vector_elements < components)
92       components = a.val->type->vector_elements;
93 
94    unsigned s[4] = { 0, 1, 2, 3 };
95    for (int i = components; i < 4; i++)
96       s[i] = components - 1;
97 
98    return new(mem_ctx) ir_swizzle(a.val, s, components);
99 }
100 
101 ir_swizzle *
swizzle_x(operand a)102 swizzle_x(operand a)
103 {
104    return swizzle(a, SWIZZLE_XXXX, 1);
105 }
106 
107 ir_swizzle *
swizzle_y(operand a)108 swizzle_y(operand a)
109 {
110    return swizzle(a, SWIZZLE_YYYY, 1);
111 }
112 
113 ir_swizzle *
swizzle_z(operand a)114 swizzle_z(operand a)
115 {
116    return swizzle(a, SWIZZLE_ZZZZ, 1);
117 }
118 
119 ir_swizzle *
swizzle_w(operand a)120 swizzle_w(operand a)
121 {
122    return swizzle(a, SWIZZLE_WWWW, 1);
123 }
124 
125 ir_expression *
expr(ir_expression_operation op,operand a)126 expr(ir_expression_operation op, operand a)
127 {
128    void *mem_ctx = ralloc_parent(a.val);
129 
130    return new(mem_ctx) ir_expression(op, a.val);
131 }
132 
133 ir_expression *
expr(ir_expression_operation op,operand a,operand b)134 expr(ir_expression_operation op, operand a, operand b)
135 {
136    void *mem_ctx = ralloc_parent(a.val);
137 
138    return new(mem_ctx) ir_expression(op, a.val, b.val);
139 }
140 
141 ir_expression *
expr(ir_expression_operation op,operand a,operand b,operand c)142 expr(ir_expression_operation op, operand a, operand b, operand c)
143 {
144    void *mem_ctx = ralloc_parent(a.val);
145 
146    return new(mem_ctx) ir_expression(op, a.val, b.val, c.val);
147 }
148 
add(operand a,operand b)149 ir_expression *add(operand a, operand b)
150 {
151    return expr(ir_binop_add, a, b);
152 }
153 
sub(operand a,operand b)154 ir_expression *sub(operand a, operand b)
155 {
156    return expr(ir_binop_sub, a, b);
157 }
158 
min2(operand a,operand b)159 ir_expression *min2(operand a, operand b)
160 {
161    return expr(ir_binop_min, a, b);
162 }
163 
max2(operand a,operand b)164 ir_expression *max2(operand a, operand b)
165 {
166    return expr(ir_binop_max, a, b);
167 }
168 
mul(operand a,operand b)169 ir_expression *mul(operand a, operand b)
170 {
171    return expr(ir_binop_mul, a, b);
172 }
173 
div(operand a,operand b)174 ir_expression *div(operand a, operand b)
175 {
176    return expr(ir_binop_div, a, b);
177 }
178 
carry(operand a,operand b)179 ir_expression *carry(operand a, operand b)
180 {
181    return expr(ir_binop_carry, a, b);
182 }
183 
borrow(operand a,operand b)184 ir_expression *borrow(operand a, operand b)
185 {
186    return expr(ir_binop_borrow, a, b);
187 }
188 
trunc(operand a)189 ir_expression *trunc(operand a)
190 {
191    return expr(ir_unop_trunc, a);
192 }
193 
round_even(operand a)194 ir_expression *round_even(operand a)
195 {
196    return expr(ir_unop_round_even, a);
197 }
198 
199 /* dot for vectors, mul for scalars */
dot(operand a,operand b)200 ir_expression *dot(operand a, operand b)
201 {
202    assert(a.val->type == b.val->type);
203 
204    if (a.val->type->vector_elements == 1)
205       return expr(ir_binop_mul, a, b);
206 
207    return expr(ir_binop_dot, a, b);
208 }
209 
210 ir_expression*
clamp(operand a,operand b,operand c)211 clamp(operand a, operand b, operand c)
212 {
213    return expr(ir_binop_min, expr(ir_binop_max, a, b), c);
214 }
215 
216 ir_expression *
saturate(operand a)217 saturate(operand a)
218 {
219    return expr(ir_unop_saturate, a);
220 }
221 
222 ir_expression *
abs(operand a)223 abs(operand a)
224 {
225    return expr(ir_unop_abs, a);
226 }
227 
228 ir_expression *
neg(operand a)229 neg(operand a)
230 {
231    return expr(ir_unop_neg, a);
232 }
233 
234 ir_expression *
sin(operand a)235 sin(operand a)
236 {
237    return expr(ir_unop_sin, a);
238 }
239 
240 ir_expression *
cos(operand a)241 cos(operand a)
242 {
243    return expr(ir_unop_cos, a);
244 }
245 
246 ir_expression *
exp(operand a)247 exp(operand a)
248 {
249    return expr(ir_unop_exp, a);
250 }
251 
252 ir_expression *
rsq(operand a)253 rsq(operand a)
254 {
255    return expr(ir_unop_rsq, a);
256 }
257 
258 ir_expression *
sqrt(operand a)259 sqrt(operand a)
260 {
261    return expr(ir_unop_sqrt, a);
262 }
263 
264 ir_expression *
log(operand a)265 log(operand a)
266 {
267    return expr(ir_unop_log, a);
268 }
269 
270 ir_expression *
sign(operand a)271 sign(operand a)
272 {
273    return expr(ir_unop_sign, a);
274 }
275 
276 ir_expression *
subr_to_int(operand a)277 subr_to_int(operand a)
278 {
279    return expr(ir_unop_subroutine_to_int, a);
280 }
281 
282 ir_expression*
equal(operand a,operand b)283 equal(operand a, operand b)
284 {
285    return expr(ir_binop_equal, a, b);
286 }
287 
288 ir_expression*
nequal(operand a,operand b)289 nequal(operand a, operand b)
290 {
291    return expr(ir_binop_nequal, a, b);
292 }
293 
294 ir_expression*
less(operand a,operand b)295 less(operand a, operand b)
296 {
297    return expr(ir_binop_less, a, b);
298 }
299 
300 ir_expression*
greater(operand a,operand b)301 greater(operand a, operand b)
302 {
303    return expr(ir_binop_less, b, a);
304 }
305 
306 ir_expression*
gequal(operand a,operand b)307 gequal(operand a, operand b)
308 {
309    return expr(ir_binop_gequal, a, b);
310 }
311 
312 ir_expression*
logic_not(operand a)313 logic_not(operand a)
314 {
315    return expr(ir_unop_logic_not, a);
316 }
317 
318 ir_expression*
logic_and(operand a,operand b)319 logic_and(operand a, operand b)
320 {
321    return expr(ir_binop_logic_and, a, b);
322 }
323 
324 ir_expression*
logic_or(operand a,operand b)325 logic_or(operand a, operand b)
326 {
327    return expr(ir_binop_logic_or, a, b);
328 }
329 
330 ir_expression*
bit_not(operand a)331 bit_not(operand a)
332 {
333    return expr(ir_unop_bit_not, a);
334 }
335 
336 ir_expression*
bit_and(operand a,operand b)337 bit_and(operand a, operand b)
338 {
339    return expr(ir_binop_bit_and, a, b);
340 }
341 
342 ir_expression*
bit_or(operand a,operand b)343 bit_or(operand a, operand b)
344 {
345    return expr(ir_binop_bit_or, a, b);
346 }
347 
348 ir_expression*
lshift(operand a,operand b)349 lshift(operand a, operand b)
350 {
351    return expr(ir_binop_lshift, a, b);
352 }
353 
354 ir_expression*
rshift(operand a,operand b)355 rshift(operand a, operand b)
356 {
357    return expr(ir_binop_rshift, a, b);
358 }
359 
360 ir_expression*
f2i(operand a)361 f2i(operand a)
362 {
363    return expr(ir_unop_f2i, a);
364 }
365 
366 ir_expression*
bitcast_f2i(operand a)367 bitcast_f2i(operand a)
368 {
369    return expr(ir_unop_bitcast_f2i, a);
370 }
371 
372 ir_expression*
i2f(operand a)373 i2f(operand a)
374 {
375    return expr(ir_unop_i2f, a);
376 }
377 
378 ir_expression*
bitcast_i2f(operand a)379 bitcast_i2f(operand a)
380 {
381    return expr(ir_unop_bitcast_i2f, a);
382 }
383 
384 ir_expression*
i2u(operand a)385 i2u(operand a)
386 {
387    return expr(ir_unop_i2u, a);
388 }
389 
390 ir_expression*
u2i(operand a)391 u2i(operand a)
392 {
393    return expr(ir_unop_u2i, a);
394 }
395 
396 ir_expression*
f2u(operand a)397 f2u(operand a)
398 {
399    return expr(ir_unop_f2u, a);
400 }
401 
402 ir_expression*
bitcast_f2u(operand a)403 bitcast_f2u(operand a)
404 {
405    return expr(ir_unop_bitcast_f2u, a);
406 }
407 
408 ir_expression*
u2f(operand a)409 u2f(operand a)
410 {
411    return expr(ir_unop_u2f, a);
412 }
413 
414 ir_expression*
bitcast_u2f(operand a)415 bitcast_u2f(operand a)
416 {
417    return expr(ir_unop_bitcast_u2f, a);
418 }
419 
420 ir_expression*
b2i(operand a)421 b2i(operand a)
422 {
423    return expr(ir_unop_b2i, a);
424 }
425 
426 ir_expression *
b2f(operand a)427 b2f(operand a)
428 {
429    return expr(ir_unop_b2f, a);
430 }
431 
432 ir_expression*
bitcast_d2i64(operand a)433 bitcast_d2i64(operand a)
434 {
435    return expr(ir_unop_bitcast_d2i64, a);
436 }
437 
438 ir_expression*
bitcast_d2u64(operand a)439 bitcast_d2u64(operand a)
440 {
441    return expr(ir_unop_bitcast_d2u64, a);
442 }
443 
444 ir_expression*
bitcast_i642d(operand a)445 bitcast_i642d(operand a)
446 {
447    return expr(ir_unop_bitcast_i642d, a);
448 }
449 
450 ir_expression*
bitcast_u642d(operand a)451 bitcast_u642d(operand a)
452 {
453    return expr(ir_unop_bitcast_u642d, a);
454 }
455 
456 ir_expression *
interpolate_at_centroid(operand a)457 interpolate_at_centroid(operand a)
458 {
459    return expr(ir_unop_interpolate_at_centroid, a);
460 }
461 
462 ir_expression *
interpolate_at_offset(operand a,operand b)463 interpolate_at_offset(operand a, operand b)
464 {
465    return expr(ir_binop_interpolate_at_offset, a, b);
466 }
467 
468 ir_expression *
interpolate_at_sample(operand a,operand b)469 interpolate_at_sample(operand a, operand b)
470 {
471    return expr(ir_binop_interpolate_at_sample, a, b);
472 }
473 
474 ir_expression *
f2f16(operand a)475 f2f16(operand a)
476 {
477    return expr(ir_unop_f2f16, a);
478 }
479 
480 ir_expression *
f2d(operand a)481 f2d(operand a)
482 {
483    return expr(ir_unop_f2d, a);
484 }
485 
486 ir_expression *
fma(operand a,operand b,operand c)487 fma(operand a, operand b, operand c)
488 {
489    return expr(ir_triop_fma, a, b, c);
490 }
491 
492 ir_expression *
lrp(operand x,operand y,operand a)493 lrp(operand x, operand y, operand a)
494 {
495    return expr(ir_triop_lrp, x, y, a);
496 }
497 
498 ir_expression *
csel(operand a,operand b,operand c)499 csel(operand a, operand b, operand c)
500 {
501    return expr(ir_triop_csel, a, b, c);
502 }
503 
504 ir_expression *
bitfield_extract(operand a,operand b,operand c)505 bitfield_extract(operand a, operand b, operand c)
506 {
507    return expr(ir_triop_bitfield_extract, a, b, c);
508 }
509 
510 ir_expression *
bitfield_insert(operand a,operand b,operand c,operand d)511 bitfield_insert(operand a, operand b, operand c, operand d)
512 {
513    void *mem_ctx = ralloc_parent(a.val);
514    return new(mem_ctx) ir_expression(ir_quadop_bitfield_insert,
515                                      a.val->type, a.val, b.val, c.val, d.val);
516 }
517 
518 ir_if*
if_tree(operand condition,ir_instruction * then_branch)519 if_tree(operand condition,
520         ir_instruction *then_branch)
521 {
522    assert(then_branch != NULL);
523 
524    void *mem_ctx = ralloc_parent(condition.val);
525 
526    ir_if *result = new(mem_ctx) ir_if(condition.val);
527    result->then_instructions.push_tail(then_branch);
528    return result;
529 }
530 
531 ir_if*
if_tree(operand condition,ir_instruction * then_branch,ir_instruction * else_branch)532 if_tree(operand condition,
533         ir_instruction *then_branch,
534         ir_instruction *else_branch)
535 {
536    assert(then_branch != NULL);
537    assert(else_branch != NULL);
538 
539    void *mem_ctx = ralloc_parent(condition.val);
540 
541    ir_if *result = new(mem_ctx) ir_if(condition.val);
542    result->then_instructions.push_tail(then_branch);
543    result->else_instructions.push_tail(else_branch);
544    return result;
545 }
546 
547 } /* namespace ir_builder */
548