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