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_xxxx(operand a)102 swizzle_xxxx(operand a)
103 {
104    return swizzle(a, SWIZZLE_XXXX, 4);
105 }
106 
107 ir_swizzle *
swizzle_yyyy(operand a)108 swizzle_yyyy(operand a)
109 {
110    return swizzle(a, SWIZZLE_YYYY, 4);
111 }
112 
113 ir_swizzle *
swizzle_zzzz(operand a)114 swizzle_zzzz(operand a)
115 {
116    return swizzle(a, SWIZZLE_ZZZZ, 4);
117 }
118 
119 ir_swizzle *
swizzle_wwww(operand a)120 swizzle_wwww(operand a)
121 {
122    return swizzle(a, SWIZZLE_WWWW, 4);
123 }
124 
125 ir_swizzle *
swizzle_x(operand a)126 swizzle_x(operand a)
127 {
128    return swizzle(a, SWIZZLE_XXXX, 1);
129 }
130 
131 ir_swizzle *
swizzle_y(operand a)132 swizzle_y(operand a)
133 {
134    return swizzle(a, SWIZZLE_YYYY, 1);
135 }
136 
137 ir_swizzle *
swizzle_z(operand a)138 swizzle_z(operand a)
139 {
140    return swizzle(a, SWIZZLE_ZZZZ, 1);
141 }
142 
143 ir_swizzle *
swizzle_w(operand a)144 swizzle_w(operand a)
145 {
146    return swizzle(a, SWIZZLE_WWWW, 1);
147 }
148 
149 ir_swizzle *
swizzle_xy(operand a)150 swizzle_xy(operand a)
151 {
152    return swizzle(a, SWIZZLE_XYZW, 2);
153 }
154 
155 ir_swizzle *
swizzle_xyz(operand a)156 swizzle_xyz(operand a)
157 {
158    return swizzle(a, SWIZZLE_XYZW, 3);
159 }
160 
161 ir_swizzle *
swizzle_xyzw(operand a)162 swizzle_xyzw(operand a)
163 {
164    return swizzle(a, SWIZZLE_XYZW, 4);
165 }
166 
167 ir_expression *
expr(ir_expression_operation op,operand a)168 expr(ir_expression_operation op, operand a)
169 {
170    void *mem_ctx = ralloc_parent(a.val);
171 
172    return new(mem_ctx) ir_expression(op, a.val);
173 }
174 
175 ir_expression *
expr(ir_expression_operation op,operand a,operand b)176 expr(ir_expression_operation op, operand a, operand b)
177 {
178    void *mem_ctx = ralloc_parent(a.val);
179 
180    return new(mem_ctx) ir_expression(op, a.val, b.val);
181 }
182 
183 ir_expression *
expr(ir_expression_operation op,operand a,operand b,operand c)184 expr(ir_expression_operation op, operand a, operand b, operand c)
185 {
186    void *mem_ctx = ralloc_parent(a.val);
187 
188    return new(mem_ctx) ir_expression(op, a.val, b.val, c.val);
189 }
190 
add(operand a,operand b)191 ir_expression *add(operand a, operand b)
192 {
193    return expr(ir_binop_add, a, b);
194 }
195 
sub(operand a,operand b)196 ir_expression *sub(operand a, operand b)
197 {
198    return expr(ir_binop_sub, a, b);
199 }
200 
min2(operand a,operand b)201 ir_expression *min2(operand a, operand b)
202 {
203    return expr(ir_binop_min, a, b);
204 }
205 
max2(operand a,operand b)206 ir_expression *max2(operand a, operand b)
207 {
208    return expr(ir_binop_max, a, b);
209 }
210 
mul(operand a,operand b)211 ir_expression *mul(operand a, operand b)
212 {
213    return expr(ir_binop_mul, a, b);
214 }
215 
imul_high(operand a,operand b)216 ir_expression *imul_high(operand a, operand b)
217 {
218    return expr(ir_binop_imul_high, a, b);
219 }
220 
div(operand a,operand b)221 ir_expression *div(operand a, operand b)
222 {
223    return expr(ir_binop_div, a, b);
224 }
225 
carry(operand a,operand b)226 ir_expression *carry(operand a, operand b)
227 {
228    return expr(ir_binop_carry, a, b);
229 }
230 
borrow(operand a,operand b)231 ir_expression *borrow(operand a, operand b)
232 {
233    return expr(ir_binop_borrow, a, b);
234 }
235 
trunc(operand a)236 ir_expression *trunc(operand a)
237 {
238    return expr(ir_unop_trunc, a);
239 }
240 
round_even(operand a)241 ir_expression *round_even(operand a)
242 {
243    return expr(ir_unop_round_even, a);
244 }
245 
fract(operand a)246 ir_expression *fract(operand a)
247 {
248    return expr(ir_unop_fract, a);
249 }
250 
251 /* dot for vectors, mul for scalars */
dot(operand a,operand b)252 ir_expression *dot(operand a, operand b)
253 {
254    assert(a.val->type == b.val->type);
255 
256    if (a.val->type->vector_elements == 1)
257       return expr(ir_binop_mul, a, b);
258 
259    return expr(ir_binop_dot, a, b);
260 }
261 
262 ir_expression*
clamp(operand a,operand b,operand c)263 clamp(operand a, operand b, operand c)
264 {
265    return expr(ir_binop_min, expr(ir_binop_max, a, b), c);
266 }
267 
268 ir_expression *
saturate(operand a)269 saturate(operand a)
270 {
271    return expr(ir_unop_saturate, a);
272 }
273 
274 ir_expression *
abs(operand a)275 abs(operand a)
276 {
277    return expr(ir_unop_abs, a);
278 }
279 
280 ir_expression *
neg(operand a)281 neg(operand a)
282 {
283    return expr(ir_unop_neg, a);
284 }
285 
286 ir_expression *
sin(operand a)287 sin(operand a)
288 {
289    return expr(ir_unop_sin, a);
290 }
291 
292 ir_expression *
cos(operand a)293 cos(operand a)
294 {
295    return expr(ir_unop_cos, a);
296 }
297 
298 ir_expression *
exp(operand a)299 exp(operand a)
300 {
301    return expr(ir_unop_exp, a);
302 }
303 
304 ir_expression *
rcp(operand a)305 rcp(operand a)
306 {
307    return expr(ir_unop_rcp, a);
308 }
309 
310 ir_expression *
rsq(operand a)311 rsq(operand a)
312 {
313    return expr(ir_unop_rsq, a);
314 }
315 
316 ir_expression *
sqrt(operand a)317 sqrt(operand a)
318 {
319    return expr(ir_unop_sqrt, a);
320 }
321 
322 ir_expression *
log(operand a)323 log(operand a)
324 {
325    return expr(ir_unop_log, a);
326 }
327 
328 ir_expression *
sign(operand a)329 sign(operand a)
330 {
331    return expr(ir_unop_sign, a);
332 }
333 
334 ir_expression *
subr_to_int(operand a)335 subr_to_int(operand a)
336 {
337    return expr(ir_unop_subroutine_to_int, a);
338 }
339 
340 ir_expression*
equal(operand a,operand b)341 equal(operand a, operand b)
342 {
343    return expr(ir_binop_equal, a, b);
344 }
345 
346 ir_expression*
nequal(operand a,operand b)347 nequal(operand a, operand b)
348 {
349    return expr(ir_binop_nequal, a, b);
350 }
351 
352 ir_expression*
less(operand a,operand b)353 less(operand a, operand b)
354 {
355    return expr(ir_binop_less, a, b);
356 }
357 
358 ir_expression*
greater(operand a,operand b)359 greater(operand a, operand b)
360 {
361    return expr(ir_binop_less, b, a);
362 }
363 
364 ir_expression*
lequal(operand a,operand b)365 lequal(operand a, operand b)
366 {
367    return expr(ir_binop_gequal, b, a);
368 }
369 
370 ir_expression*
gequal(operand a,operand b)371 gequal(operand a, operand b)
372 {
373    return expr(ir_binop_gequal, a, b);
374 }
375 
376 ir_expression*
logic_not(operand a)377 logic_not(operand a)
378 {
379    return expr(ir_unop_logic_not, a);
380 }
381 
382 ir_expression*
logic_and(operand a,operand b)383 logic_and(operand a, operand b)
384 {
385    return expr(ir_binop_logic_and, a, b);
386 }
387 
388 ir_expression*
logic_or(operand a,operand b)389 logic_or(operand a, operand b)
390 {
391    return expr(ir_binop_logic_or, a, b);
392 }
393 
394 ir_expression*
bit_not(operand a)395 bit_not(operand a)
396 {
397    return expr(ir_unop_bit_not, a);
398 }
399 
400 ir_expression*
bit_and(operand a,operand b)401 bit_and(operand a, operand b)
402 {
403    return expr(ir_binop_bit_and, a, b);
404 }
405 
406 ir_expression*
bit_or(operand a,operand b)407 bit_or(operand a, operand b)
408 {
409    return expr(ir_binop_bit_or, a, b);
410 }
411 
412 ir_expression*
bit_xor(operand a,operand b)413 bit_xor(operand a, operand b)
414 {
415    return expr(ir_binop_bit_xor, a, b);
416 }
417 
418 ir_expression*
lshift(operand a,operand b)419 lshift(operand a, operand b)
420 {
421    return expr(ir_binop_lshift, a, b);
422 }
423 
424 ir_expression*
rshift(operand a,operand b)425 rshift(operand a, operand b)
426 {
427    return expr(ir_binop_rshift, a, b);
428 }
429 
430 ir_expression*
f2i(operand a)431 f2i(operand a)
432 {
433    return expr(ir_unop_f2i, a);
434 }
435 
436 ir_expression*
bitcast_f2i(operand a)437 bitcast_f2i(operand a)
438 {
439    return expr(ir_unop_bitcast_f2i, a);
440 }
441 
442 ir_expression*
i2f(operand a)443 i2f(operand a)
444 {
445    return expr(ir_unop_i2f, a);
446 }
447 
448 ir_expression*
bitcast_i2f(operand a)449 bitcast_i2f(operand a)
450 {
451    return expr(ir_unop_bitcast_i2f, a);
452 }
453 
454 ir_expression*
i2u(operand a)455 i2u(operand a)
456 {
457    return expr(ir_unop_i2u, a);
458 }
459 
460 ir_expression*
u2i(operand a)461 u2i(operand a)
462 {
463    return expr(ir_unop_u2i, a);
464 }
465 
466 ir_expression*
f2u(operand a)467 f2u(operand a)
468 {
469    return expr(ir_unop_f2u, a);
470 }
471 
472 ir_expression*
bitcast_f2u(operand a)473 bitcast_f2u(operand a)
474 {
475    return expr(ir_unop_bitcast_f2u, a);
476 }
477 
478 ir_expression*
u2f(operand a)479 u2f(operand a)
480 {
481    return expr(ir_unop_u2f, a);
482 }
483 
484 ir_expression*
bitcast_u2f(operand a)485 bitcast_u2f(operand a)
486 {
487    return expr(ir_unop_bitcast_u2f, a);
488 }
489 
490 ir_expression*
i2b(operand a)491 i2b(operand a)
492 {
493    return expr(ir_unop_i2b, a);
494 }
495 
496 ir_expression*
b2i(operand a)497 b2i(operand a)
498 {
499    return expr(ir_unop_b2i, a);
500 }
501 
502 ir_expression *
f2b(operand a)503 f2b(operand a)
504 {
505    return expr(ir_unop_f2b, a);
506 }
507 
508 ir_expression *
b2f(operand a)509 b2f(operand a)
510 {
511    return expr(ir_unop_b2f, a);
512 }
513 
514 ir_expression*
bitcast_d2i64(operand a)515 bitcast_d2i64(operand a)
516 {
517    return expr(ir_unop_bitcast_d2i64, a);
518 }
519 
520 ir_expression*
bitcast_d2u64(operand a)521 bitcast_d2u64(operand a)
522 {
523    return expr(ir_unop_bitcast_d2u64, a);
524 }
525 
526 ir_expression*
bitcast_i642d(operand a)527 bitcast_i642d(operand a)
528 {
529    return expr(ir_unop_bitcast_i642d, a);
530 }
531 
532 ir_expression*
bitcast_u642d(operand a)533 bitcast_u642d(operand a)
534 {
535    return expr(ir_unop_bitcast_u642d, a);
536 }
537 
538 ir_expression *
interpolate_at_centroid(operand a)539 interpolate_at_centroid(operand a)
540 {
541    return expr(ir_unop_interpolate_at_centroid, a);
542 }
543 
544 ir_expression *
interpolate_at_offset(operand a,operand b)545 interpolate_at_offset(operand a, operand b)
546 {
547    return expr(ir_binop_interpolate_at_offset, a, b);
548 }
549 
550 ir_expression *
interpolate_at_sample(operand a,operand b)551 interpolate_at_sample(operand a, operand b)
552 {
553    return expr(ir_binop_interpolate_at_sample, a, b);
554 }
555 
556 ir_expression *
f2d(operand a)557 f2d(operand a)
558 {
559    return expr(ir_unop_f2d, a);
560 }
561 
562 ir_expression *
i2d(operand a)563 i2d(operand a)
564 {
565    return expr(ir_unop_i2d, a);
566 }
567 
568 ir_expression *
u2d(operand a)569 u2d(operand a)
570 {
571    return expr(ir_unop_u2d, a);
572 }
573 
574 ir_expression *
fma(operand a,operand b,operand c)575 fma(operand a, operand b, operand c)
576 {
577    return expr(ir_triop_fma, a, b, c);
578 }
579 
580 ir_expression *
lrp(operand x,operand y,operand a)581 lrp(operand x, operand y, operand a)
582 {
583    return expr(ir_triop_lrp, x, y, a);
584 }
585 
586 ir_expression *
csel(operand a,operand b,operand c)587 csel(operand a, operand b, operand c)
588 {
589    return expr(ir_triop_csel, a, b, c);
590 }
591 
592 ir_expression *
bitfield_extract(operand a,operand b,operand c)593 bitfield_extract(operand a, operand b, operand c)
594 {
595    return expr(ir_triop_bitfield_extract, a, b, c);
596 }
597 
598 ir_expression *
bitfield_insert(operand a,operand b,operand c,operand d)599 bitfield_insert(operand a, operand b, operand c, operand d)
600 {
601    void *mem_ctx = ralloc_parent(a.val);
602    return new(mem_ctx) ir_expression(ir_quadop_bitfield_insert,
603                                      a.val->type, a.val, b.val, c.val, d.val);
604 }
605 
606 ir_if*
if_tree(operand condition,ir_instruction * then_branch)607 if_tree(operand condition,
608         ir_instruction *then_branch)
609 {
610    assert(then_branch != NULL);
611 
612    void *mem_ctx = ralloc_parent(condition.val);
613 
614    ir_if *result = new(mem_ctx) ir_if(condition.val);
615    result->then_instructions.push_tail(then_branch);
616    return result;
617 }
618 
619 ir_if*
if_tree(operand condition,ir_instruction * then_branch,ir_instruction * else_branch)620 if_tree(operand condition,
621         ir_instruction *then_branch,
622         ir_instruction *else_branch)
623 {
624    assert(then_branch != NULL);
625    assert(else_branch != NULL);
626 
627    void *mem_ctx = ralloc_parent(condition.val);
628 
629    ir_if *result = new(mem_ctx) ir_if(condition.val);
630    result->then_instructions.push_tail(then_branch);
631    result->else_instructions.push_tail(else_branch);
632    return result;
633 }
634 
635 } /* namespace ir_builder */
636