1 /*
2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <string.h>
24 #include "ir.h"
25 #include "util/half_float.h"
26 #include "util/bitscan.h"
27 #include "compiler/glsl_types.h"
28 #include "glsl_parser_extras.h"
29
30
ir_rvalue(enum ir_node_type t)31 ir_rvalue::ir_rvalue(enum ir_node_type t)
32 : ir_instruction(t)
33 {
34 this->type = glsl_type::error_type;
35 }
36
is_zero() const37 bool ir_rvalue::is_zero() const
38 {
39 return false;
40 }
41
is_one() const42 bool ir_rvalue::is_one() const
43 {
44 return false;
45 }
46
is_negative_one() const47 bool ir_rvalue::is_negative_one() const
48 {
49 return false;
50 }
51
52 /**
53 * Modify the swizzle make to move one component to another
54 *
55 * \param m IR swizzle to be modified
56 * \param from Component in the RHS that is to be swizzled
57 * \param to Desired swizzle location of \c from
58 */
59 static void
update_rhs_swizzle(ir_swizzle_mask & m,unsigned from,unsigned to)60 update_rhs_swizzle(ir_swizzle_mask &m, unsigned from, unsigned to)
61 {
62 switch (to) {
63 case 0: m.x = from; break;
64 case 1: m.y = from; break;
65 case 2: m.z = from; break;
66 case 3: m.w = from; break;
67 default: assert(!"Should not get here.");
68 }
69 }
70
71 void
set_lhs(ir_rvalue * lhs)72 ir_assignment::set_lhs(ir_rvalue *lhs)
73 {
74 void *mem_ctx = this;
75 bool swizzled = false;
76
77 while (lhs != NULL) {
78 ir_swizzle *swiz = lhs->as_swizzle();
79
80 if (swiz == NULL)
81 break;
82
83 unsigned write_mask = 0;
84 ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
85
86 for (unsigned i = 0; i < swiz->mask.num_components; i++) {
87 unsigned c = 0;
88
89 switch (i) {
90 case 0: c = swiz->mask.x; break;
91 case 1: c = swiz->mask.y; break;
92 case 2: c = swiz->mask.z; break;
93 case 3: c = swiz->mask.w; break;
94 default: assert(!"Should not get here.");
95 }
96
97 write_mask |= (((this->write_mask >> i) & 1) << c);
98 update_rhs_swizzle(rhs_swiz, i, c);
99 rhs_swiz.num_components = swiz->val->type->vector_elements;
100 }
101
102 this->write_mask = write_mask;
103 lhs = swiz->val;
104
105 this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
106 swizzled = true;
107 }
108
109 if (swizzled) {
110 /* Now, RHS channels line up with the LHS writemask. Collapse it
111 * to just the channels that will be written.
112 */
113 ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
114 int rhs_chan = 0;
115 for (int i = 0; i < 4; i++) {
116 if (write_mask & (1 << i))
117 update_rhs_swizzle(rhs_swiz, i, rhs_chan++);
118 }
119 rhs_swiz.num_components = rhs_chan;
120 this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
121 }
122
123 assert((lhs == NULL) || lhs->as_dereference());
124
125 this->lhs = (ir_dereference *) lhs;
126 }
127
128 ir_variable *
whole_variable_written()129 ir_assignment::whole_variable_written()
130 {
131 ir_variable *v = this->lhs->whole_variable_referenced();
132
133 if (v == NULL)
134 return NULL;
135
136 if (v->type->is_scalar())
137 return v;
138
139 if (v->type->is_vector()) {
140 const unsigned mask = (1U << v->type->vector_elements) - 1;
141
142 if (mask != this->write_mask)
143 return NULL;
144 }
145
146 /* Either all the vector components are assigned or the variable is some
147 * composite type (and the whole thing is assigned.
148 */
149 return v;
150 }
151
ir_assignment(ir_dereference * lhs,ir_rvalue * rhs,unsigned write_mask)152 ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
153 unsigned write_mask)
154 : ir_instruction(ir_type_assignment)
155 {
156 this->rhs = rhs;
157 this->lhs = lhs;
158 this->write_mask = write_mask;
159
160 if (lhs->type->is_scalar() || lhs->type->is_vector())
161 assert(util_bitcount(write_mask) == this->rhs->type->vector_elements);
162 }
163
ir_assignment(ir_rvalue * lhs,ir_rvalue * rhs)164 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs)
165 : ir_instruction(ir_type_assignment)
166 {
167 this->rhs = rhs;
168
169 /* If the RHS is a vector type, assume that all components of the vector
170 * type are being written to the LHS. The write mask comes from the RHS
171 * because we can have a case where the LHS is a vec4 and the RHS is a
172 * vec3. In that case, the assignment is:
173 *
174 * (assign (...) (xyz) (var_ref lhs) (var_ref rhs))
175 */
176 if (rhs->type->is_vector())
177 this->write_mask = (1U << rhs->type->vector_elements) - 1;
178 else if (rhs->type->is_scalar())
179 this->write_mask = 1;
180 else
181 this->write_mask = 0;
182
183 this->set_lhs(lhs);
184 }
185
ir_expression(int op,const struct glsl_type * type,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2,ir_rvalue * op3)186 ir_expression::ir_expression(int op, const struct glsl_type *type,
187 ir_rvalue *op0, ir_rvalue *op1,
188 ir_rvalue *op2, ir_rvalue *op3)
189 : ir_rvalue(ir_type_expression)
190 {
191 this->type = type;
192 this->operation = ir_expression_operation(op);
193 this->operands[0] = op0;
194 this->operands[1] = op1;
195 this->operands[2] = op2;
196 this->operands[3] = op3;
197 init_num_operands();
198
199 #ifndef NDEBUG
200 for (unsigned i = num_operands; i < 4; i++) {
201 assert(this->operands[i] == NULL);
202 }
203
204 for (unsigned i = 0; i < num_operands; i++) {
205 assert(this->operands[i] != NULL);
206 }
207 #endif
208 }
209
ir_expression(int op,ir_rvalue * op0)210 ir_expression::ir_expression(int op, ir_rvalue *op0)
211 : ir_rvalue(ir_type_expression)
212 {
213 this->operation = ir_expression_operation(op);
214 this->operands[0] = op0;
215 this->operands[1] = NULL;
216 this->operands[2] = NULL;
217 this->operands[3] = NULL;
218
219 assert(op <= ir_last_unop);
220 init_num_operands();
221 assert(num_operands == 1);
222 assert(this->operands[0]);
223
224 switch (this->operation) {
225 case ir_unop_bit_not:
226 case ir_unop_logic_not:
227 case ir_unop_neg:
228 case ir_unop_abs:
229 case ir_unop_sign:
230 case ir_unop_rcp:
231 case ir_unop_rsq:
232 case ir_unop_sqrt:
233 case ir_unop_exp:
234 case ir_unop_log:
235 case ir_unop_exp2:
236 case ir_unop_log2:
237 case ir_unop_trunc:
238 case ir_unop_ceil:
239 case ir_unop_floor:
240 case ir_unop_fract:
241 case ir_unop_round_even:
242 case ir_unop_sin:
243 case ir_unop_cos:
244 case ir_unop_dFdx:
245 case ir_unop_dFdx_coarse:
246 case ir_unop_dFdx_fine:
247 case ir_unop_dFdy:
248 case ir_unop_dFdy_coarse:
249 case ir_unop_dFdy_fine:
250 case ir_unop_bitfield_reverse:
251 case ir_unop_interpolate_at_centroid:
252 case ir_unop_clz:
253 case ir_unop_saturate:
254 case ir_unop_atan:
255 this->type = op0->type;
256 break;
257
258 case ir_unop_f2i:
259 case ir_unop_b2i:
260 case ir_unop_u2i:
261 case ir_unop_d2i:
262 case ir_unop_bitcast_f2i:
263 case ir_unop_bit_count:
264 case ir_unop_find_msb:
265 case ir_unop_find_lsb:
266 case ir_unop_subroutine_to_int:
267 case ir_unop_i642i:
268 case ir_unop_u642i:
269 this->type = glsl_type::get_instance(GLSL_TYPE_INT,
270 op0->type->vector_elements, 1);
271 break;
272
273 case ir_unop_b2f:
274 case ir_unop_i2f:
275 case ir_unop_u2f:
276 case ir_unop_d2f:
277 case ir_unop_f162f:
278 case ir_unop_bitcast_i2f:
279 case ir_unop_bitcast_u2f:
280 case ir_unop_i642f:
281 case ir_unop_u642f:
282 this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
283 op0->type->vector_elements, 1);
284 break;
285
286 case ir_unop_f2f16:
287 case ir_unop_f2fmp:
288 case ir_unop_b2f16:
289 this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT16,
290 op0->type->vector_elements, 1);
291 break;
292
293 case ir_unop_i2imp:
294 this->type = glsl_type::get_instance(GLSL_TYPE_INT16,
295 op0->type->vector_elements, 1);
296 break;
297
298 case ir_unop_i2i:
299 if (op0->type->base_type == GLSL_TYPE_INT) {
300 this->type = glsl_type::get_instance(GLSL_TYPE_INT16,
301 op0->type->vector_elements, 1);
302 } else {
303 assert(op0->type->base_type == GLSL_TYPE_INT16);
304 this->type = glsl_type::get_instance(GLSL_TYPE_INT,
305 op0->type->vector_elements, 1);
306 }
307 break;
308
309 case ir_unop_u2u:
310 if (op0->type->base_type == GLSL_TYPE_UINT) {
311 this->type = glsl_type::get_instance(GLSL_TYPE_UINT16,
312 op0->type->vector_elements, 1);
313 } else {
314 assert(op0->type->base_type == GLSL_TYPE_UINT16);
315 this->type = glsl_type::get_instance(GLSL_TYPE_UINT,
316 op0->type->vector_elements, 1);
317 }
318 break;
319
320 case ir_unop_u2ump:
321 this->type = glsl_type::get_instance(GLSL_TYPE_UINT16,
322 op0->type->vector_elements, 1);
323 break;
324
325 case ir_unop_f2b:
326 case ir_unop_i2b:
327 case ir_unop_d2b:
328 case ir_unop_f162b:
329 case ir_unop_i642b:
330 this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
331 op0->type->vector_elements, 1);
332 break;
333
334 case ir_unop_f2d:
335 case ir_unop_i2d:
336 case ir_unop_u2d:
337 case ir_unop_i642d:
338 case ir_unop_u642d:
339 this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE,
340 op0->type->vector_elements, 1);
341 break;
342
343 case ir_unop_i2u:
344 case ir_unop_f2u:
345 case ir_unop_d2u:
346 case ir_unop_bitcast_f2u:
347 case ir_unop_i642u:
348 case ir_unop_u642u:
349 this->type = glsl_type::get_instance(GLSL_TYPE_UINT,
350 op0->type->vector_elements, 1);
351 break;
352
353 case ir_unop_i2i64:
354 case ir_unop_u2i64:
355 case ir_unop_b2i64:
356 case ir_unop_f2i64:
357 case ir_unop_d2i64:
358 case ir_unop_u642i64:
359 this->type = glsl_type::get_instance(GLSL_TYPE_INT64,
360 op0->type->vector_elements, 1);
361 break;
362
363 case ir_unop_i2u64:
364 case ir_unop_u2u64:
365 case ir_unop_f2u64:
366 case ir_unop_d2u64:
367 case ir_unop_i642u64:
368 this->type = glsl_type::get_instance(GLSL_TYPE_UINT64,
369 op0->type->vector_elements, 1);
370 break;
371
372 case ir_unop_unpack_double_2x32:
373 case ir_unop_unpack_uint_2x32:
374 this->type = glsl_type::uvec2_type;
375 break;
376
377 case ir_unop_unpack_int_2x32:
378 this->type = glsl_type::ivec2_type;
379 break;
380
381 case ir_unop_pack_snorm_2x16:
382 case ir_unop_pack_snorm_4x8:
383 case ir_unop_pack_unorm_2x16:
384 case ir_unop_pack_unorm_4x8:
385 case ir_unop_pack_half_2x16:
386 this->type = glsl_type::uint_type;
387 break;
388
389 case ir_unop_pack_double_2x32:
390 this->type = glsl_type::double_type;
391 break;
392
393 case ir_unop_pack_int_2x32:
394 this->type = glsl_type::int64_t_type;
395 break;
396
397 case ir_unop_pack_uint_2x32:
398 this->type = glsl_type::uint64_t_type;
399 break;
400
401 case ir_unop_unpack_snorm_2x16:
402 case ir_unop_unpack_unorm_2x16:
403 case ir_unop_unpack_half_2x16:
404 this->type = glsl_type::vec2_type;
405 break;
406
407 case ir_unop_unpack_snorm_4x8:
408 case ir_unop_unpack_unorm_4x8:
409 this->type = glsl_type::vec4_type;
410 break;
411
412 case ir_unop_unpack_sampler_2x32:
413 case ir_unop_unpack_image_2x32:
414 this->type = glsl_type::uvec2_type;
415 break;
416
417 case ir_unop_pack_sampler_2x32:
418 case ir_unop_pack_image_2x32:
419 this->type = op0->type;
420 break;
421
422 case ir_unop_frexp_sig:
423 this->type = op0->type;
424 break;
425 case ir_unop_frexp_exp:
426 this->type = glsl_type::get_instance(GLSL_TYPE_INT,
427 op0->type->vector_elements, 1);
428 break;
429
430 case ir_unop_get_buffer_size:
431 case ir_unop_ssbo_unsized_array_length:
432 case ir_unop_implicitly_sized_array_length:
433 this->type = glsl_type::int_type;
434 break;
435
436 case ir_unop_bitcast_i642d:
437 case ir_unop_bitcast_u642d:
438 this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE,
439 op0->type->vector_elements, 1);
440 break;
441
442 case ir_unop_bitcast_d2i64:
443 this->type = glsl_type::get_instance(GLSL_TYPE_INT64,
444 op0->type->vector_elements, 1);
445 break;
446 case ir_unop_bitcast_d2u64:
447 this->type = glsl_type::get_instance(GLSL_TYPE_UINT64,
448 op0->type->vector_elements, 1);
449 break;
450
451 default:
452 assert(!"not reached: missing automatic type setup for ir_expression");
453 this->type = op0->type;
454 break;
455 }
456 }
457
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1)458 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
459 : ir_rvalue(ir_type_expression)
460 {
461 this->operation = ir_expression_operation(op);
462 this->operands[0] = op0;
463 this->operands[1] = op1;
464 this->operands[2] = NULL;
465 this->operands[3] = NULL;
466
467 assert(op > ir_last_unop);
468 init_num_operands();
469 assert(num_operands == 2);
470 for (unsigned i = 0; i < num_operands; i++) {
471 assert(this->operands[i] != NULL);
472 }
473
474 switch (this->operation) {
475 case ir_binop_all_equal:
476 case ir_binop_any_nequal:
477 this->type = glsl_type::bool_type;
478 break;
479
480 case ir_binop_add:
481 case ir_binop_sub:
482 case ir_binop_min:
483 case ir_binop_max:
484 case ir_binop_pow:
485 case ir_binop_mul:
486 case ir_binop_div:
487 case ir_binop_mod:
488 case ir_binop_atan2:
489 if (op0->type->is_scalar()) {
490 this->type = op1->type;
491 } else if (op1->type->is_scalar()) {
492 this->type = op0->type;
493 } else {
494 if (this->operation == ir_binop_mul) {
495 this->type = glsl_type::get_mul_type(op0->type, op1->type);
496 } else {
497 assert(op0->type == op1->type);
498 this->type = op0->type;
499 }
500 }
501 break;
502
503 case ir_binop_logic_and:
504 case ir_binop_logic_xor:
505 case ir_binop_logic_or:
506 case ir_binop_bit_and:
507 case ir_binop_bit_xor:
508 case ir_binop_bit_or:
509 assert(!op0->type->is_matrix());
510 assert(!op1->type->is_matrix());
511 if (op0->type->is_scalar()) {
512 this->type = op1->type;
513 } else if (op1->type->is_scalar()) {
514 this->type = op0->type;
515 } else {
516 assert(op0->type->vector_elements == op1->type->vector_elements);
517 this->type = op0->type;
518 }
519 break;
520
521 case ir_binop_equal:
522 case ir_binop_nequal:
523 case ir_binop_gequal:
524 case ir_binop_less:
525 assert(op0->type == op1->type);
526 this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
527 op0->type->vector_elements, 1);
528 break;
529
530 case ir_binop_dot:
531 this->type = op0->type->get_base_type();
532 break;
533
534 case ir_binop_imul_high:
535 case ir_binop_mul_32x16:
536 case ir_binop_carry:
537 case ir_binop_borrow:
538 case ir_binop_lshift:
539 case ir_binop_rshift:
540 case ir_binop_ldexp:
541 case ir_binop_interpolate_at_offset:
542 case ir_binop_interpolate_at_sample:
543 this->type = op0->type;
544 break;
545
546 case ir_binop_add_sat:
547 case ir_binop_sub_sat:
548 case ir_binop_avg:
549 case ir_binop_avg_round:
550 assert(op0->type == op1->type);
551 this->type = op0->type;
552 break;
553
554 case ir_binop_abs_sub: {
555 enum glsl_base_type base;
556
557 assert(op0->type == op1->type);
558
559 switch (op0->type->base_type) {
560 case GLSL_TYPE_UINT:
561 case GLSL_TYPE_INT:
562 base = GLSL_TYPE_UINT;
563 break;
564 case GLSL_TYPE_UINT8:
565 case GLSL_TYPE_INT8:
566 base = GLSL_TYPE_UINT8;
567 break;
568 case GLSL_TYPE_UINT16:
569 case GLSL_TYPE_INT16:
570 base = GLSL_TYPE_UINT16;
571 break;
572 case GLSL_TYPE_UINT64:
573 case GLSL_TYPE_INT64:
574 base = GLSL_TYPE_UINT64;
575 break;
576 default:
577 unreachable(!"Invalid base type.");
578 }
579
580 this->type = glsl_type::get_instance(base, op0->type->vector_elements, 1);
581 break;
582 }
583
584 case ir_binop_vector_extract:
585 this->type = op0->type->get_scalar_type();
586 break;
587
588 default:
589 assert(!"not reached: missing automatic type setup for ir_expression");
590 this->type = glsl_type::float_type;
591 }
592 }
593
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2)594 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1,
595 ir_rvalue *op2)
596 : ir_rvalue(ir_type_expression)
597 {
598 this->operation = ir_expression_operation(op);
599 this->operands[0] = op0;
600 this->operands[1] = op1;
601 this->operands[2] = op2;
602 this->operands[3] = NULL;
603
604 assert(op > ir_last_binop && op <= ir_last_triop);
605 init_num_operands();
606 assert(num_operands == 3);
607 for (unsigned i = 0; i < num_operands; i++) {
608 assert(this->operands[i] != NULL);
609 }
610
611 switch (this->operation) {
612 case ir_triop_fma:
613 case ir_triop_lrp:
614 case ir_triop_bitfield_extract:
615 case ir_triop_vector_insert:
616 this->type = op0->type;
617 break;
618
619 case ir_triop_csel:
620 this->type = op1->type;
621 break;
622
623 default:
624 assert(!"not reached: missing automatic type setup for ir_expression");
625 this->type = glsl_type::float_type;
626 }
627 }
628
629 /**
630 * This is only here for ir_reader to used for testing purposes. Please use
631 * the precomputed num_operands field if you need the number of operands.
632 */
633 unsigned
get_num_operands(ir_expression_operation op)634 ir_expression::get_num_operands(ir_expression_operation op)
635 {
636 assert(op <= ir_last_opcode);
637
638 if (op <= ir_last_unop)
639 return 1;
640
641 if (op <= ir_last_binop)
642 return 2;
643
644 if (op <= ir_last_triop)
645 return 3;
646
647 if (op <= ir_last_quadop)
648 return 4;
649
650 unreachable("Could not calculate number of operands");
651 }
652
653 #include "ir_expression_operation_strings.h"
654
655 const char*
depth_layout_string(ir_depth_layout layout)656 depth_layout_string(ir_depth_layout layout)
657 {
658 switch(layout) {
659 case ir_depth_layout_none: return "";
660 case ir_depth_layout_any: return "depth_any";
661 case ir_depth_layout_greater: return "depth_greater";
662 case ir_depth_layout_less: return "depth_less";
663 case ir_depth_layout_unchanged: return "depth_unchanged";
664
665 default:
666 assert(0);
667 return "";
668 }
669 }
670
671 ir_expression_operation
get_operator(const char * str)672 ir_expression::get_operator(const char *str)
673 {
674 for (int op = 0; op <= int(ir_last_opcode); op++) {
675 if (strcmp(str, ir_expression_operation_strings[op]) == 0)
676 return (ir_expression_operation) op;
677 }
678 return (ir_expression_operation) -1;
679 }
680
681 ir_variable *
variable_referenced() const682 ir_expression::variable_referenced() const
683 {
684 switch (operation) {
685 case ir_binop_vector_extract:
686 case ir_triop_vector_insert:
687 /* We get these for things like a[0] where a is a vector type. In these
688 * cases we want variable_referenced() to return the actual vector
689 * variable this is wrapping.
690 */
691 return operands[0]->variable_referenced();
692 default:
693 return ir_rvalue::variable_referenced();
694 }
695 }
696
ir_constant()697 ir_constant::ir_constant()
698 : ir_rvalue(ir_type_constant)
699 {
700 this->const_elements = NULL;
701 }
702
ir_constant(const struct glsl_type * type,const ir_constant_data * data)703 ir_constant::ir_constant(const struct glsl_type *type,
704 const ir_constant_data *data)
705 : ir_rvalue(ir_type_constant)
706 {
707 this->const_elements = NULL;
708
709 assert((type->base_type >= GLSL_TYPE_UINT)
710 && (type->base_type <= GLSL_TYPE_IMAGE));
711
712 this->type = type;
713 memcpy(& this->value, data, sizeof(this->value));
714 }
715
ir_constant(float16_t f16,unsigned vector_elements)716 ir_constant::ir_constant(float16_t f16, unsigned vector_elements)
717 : ir_rvalue(ir_type_constant)
718 {
719 this->const_elements = NULL;
720 assert(vector_elements <= 4);
721 this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT16, vector_elements, 1);
722 for (unsigned i = 0; i < vector_elements; i++) {
723 this->value.f16[i] = f16.bits;
724 }
725 for (unsigned i = vector_elements; i < 16; i++) {
726 this->value.f[i] = 0;
727 }
728 }
729
ir_constant(float f,unsigned vector_elements)730 ir_constant::ir_constant(float f, unsigned vector_elements)
731 : ir_rvalue(ir_type_constant)
732 {
733 this->const_elements = NULL;
734 assert(vector_elements <= 4);
735 this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT, vector_elements, 1);
736 for (unsigned i = 0; i < vector_elements; i++) {
737 this->value.f[i] = f;
738 }
739 for (unsigned i = vector_elements; i < 16; i++) {
740 this->value.f[i] = 0;
741 }
742 }
743
ir_constant(double d,unsigned vector_elements)744 ir_constant::ir_constant(double d, unsigned vector_elements)
745 : ir_rvalue(ir_type_constant)
746 {
747 this->const_elements = NULL;
748 assert(vector_elements <= 4);
749 this->type = glsl_type::get_instance(GLSL_TYPE_DOUBLE, vector_elements, 1);
750 for (unsigned i = 0; i < vector_elements; i++) {
751 this->value.d[i] = d;
752 }
753 for (unsigned i = vector_elements; i < 16; i++) {
754 this->value.d[i] = 0.0;
755 }
756 }
757
ir_constant(int16_t i16,unsigned vector_elements)758 ir_constant::ir_constant(int16_t i16, unsigned vector_elements)
759 : ir_rvalue(ir_type_constant)
760 {
761 this->const_elements = NULL;
762 assert(vector_elements <= 4);
763 this->type = glsl_type::get_instance(GLSL_TYPE_INT16, vector_elements, 1);
764 for (unsigned i = 0; i < vector_elements; i++) {
765 this->value.i16[i] = i16;
766 }
767 for (unsigned i = vector_elements; i < 16; i++) {
768 this->value.i16[i] = 0;
769 }
770 }
771
ir_constant(uint16_t u16,unsigned vector_elements)772 ir_constant::ir_constant(uint16_t u16, unsigned vector_elements)
773 : ir_rvalue(ir_type_constant)
774 {
775 this->const_elements = NULL;
776 assert(vector_elements <= 4);
777 this->type = glsl_type::get_instance(GLSL_TYPE_UINT16, vector_elements, 1);
778 for (unsigned i = 0; i < vector_elements; i++) {
779 this->value.u16[i] = u16;
780 }
781 for (unsigned i = vector_elements; i < 16; i++) {
782 this->value.u16[i] = 0;
783 }
784 }
785
ir_constant(unsigned int u,unsigned vector_elements)786 ir_constant::ir_constant(unsigned int u, unsigned vector_elements)
787 : ir_rvalue(ir_type_constant)
788 {
789 this->const_elements = NULL;
790 assert(vector_elements <= 4);
791 this->type = glsl_type::get_instance(GLSL_TYPE_UINT, vector_elements, 1);
792 for (unsigned i = 0; i < vector_elements; i++) {
793 this->value.u[i] = u;
794 }
795 for (unsigned i = vector_elements; i < 16; i++) {
796 this->value.u[i] = 0;
797 }
798 }
799
ir_constant(int integer,unsigned vector_elements)800 ir_constant::ir_constant(int integer, unsigned vector_elements)
801 : ir_rvalue(ir_type_constant)
802 {
803 this->const_elements = NULL;
804 assert(vector_elements <= 4);
805 this->type = glsl_type::get_instance(GLSL_TYPE_INT, vector_elements, 1);
806 for (unsigned i = 0; i < vector_elements; i++) {
807 this->value.i[i] = integer;
808 }
809 for (unsigned i = vector_elements; i < 16; i++) {
810 this->value.i[i] = 0;
811 }
812 }
813
ir_constant(uint64_t u64,unsigned vector_elements)814 ir_constant::ir_constant(uint64_t u64, unsigned vector_elements)
815 : ir_rvalue(ir_type_constant)
816 {
817 this->const_elements = NULL;
818 assert(vector_elements <= 4);
819 this->type = glsl_type::get_instance(GLSL_TYPE_UINT64, vector_elements, 1);
820 for (unsigned i = 0; i < vector_elements; i++) {
821 this->value.u64[i] = u64;
822 }
823 for (unsigned i = vector_elements; i < 16; i++) {
824 this->value.u64[i] = 0;
825 }
826 }
827
ir_constant(int64_t int64,unsigned vector_elements)828 ir_constant::ir_constant(int64_t int64, unsigned vector_elements)
829 : ir_rvalue(ir_type_constant)
830 {
831 this->const_elements = NULL;
832 assert(vector_elements <= 4);
833 this->type = glsl_type::get_instance(GLSL_TYPE_INT64, vector_elements, 1);
834 for (unsigned i = 0; i < vector_elements; i++) {
835 this->value.i64[i] = int64;
836 }
837 for (unsigned i = vector_elements; i < 16; i++) {
838 this->value.i64[i] = 0;
839 }
840 }
841
ir_constant(bool b,unsigned vector_elements)842 ir_constant::ir_constant(bool b, unsigned vector_elements)
843 : ir_rvalue(ir_type_constant)
844 {
845 this->const_elements = NULL;
846 assert(vector_elements <= 4);
847 this->type = glsl_type::get_instance(GLSL_TYPE_BOOL, vector_elements, 1);
848 for (unsigned i = 0; i < vector_elements; i++) {
849 this->value.b[i] = b;
850 }
851 for (unsigned i = vector_elements; i < 16; i++) {
852 this->value.b[i] = false;
853 }
854 }
855
ir_constant(const ir_constant * c,unsigned i)856 ir_constant::ir_constant(const ir_constant *c, unsigned i)
857 : ir_rvalue(ir_type_constant)
858 {
859 this->const_elements = NULL;
860 this->type = c->type->get_base_type();
861
862 /* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
863 *
864 * In the subsections described above for array, vector, matrix and
865 * structure accesses, any out-of-bounds access produced undefined
866 * behavior....Out-of-bounds reads return undefined values, which
867 * include values from other variables of the active program or zero.
868 *
869 * GL_KHR_robustness and GL_ARB_robustness encourage us to return zero.
870 */
871 if (i >= c->type->vector_elements) {
872 this->value = { { 0 } };
873 return;
874 }
875
876 switch (this->type->base_type) {
877 case GLSL_TYPE_UINT16: this->value.u16[0] = c->value.u16[i]; break;
878 case GLSL_TYPE_INT16: this->value.i16[0] = c->value.i16[i]; break;
879 case GLSL_TYPE_UINT: this->value.u[0] = c->value.u[i]; break;
880 case GLSL_TYPE_INT: this->value.i[0] = c->value.i[i]; break;
881 case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
882 case GLSL_TYPE_FLOAT16: this->value.f16[0] = c->value.f16[i]; break;
883 case GLSL_TYPE_BOOL: this->value.b[0] = c->value.b[i]; break;
884 case GLSL_TYPE_DOUBLE: this->value.d[0] = c->value.d[i]; break;
885 default: assert(!"Should not get here."); break;
886 }
887 }
888
ir_constant(const struct glsl_type * type,exec_list * value_list)889 ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
890 : ir_rvalue(ir_type_constant)
891 {
892 this->const_elements = NULL;
893 this->type = type;
894
895 assert(type->is_scalar() || type->is_vector() || type->is_matrix()
896 || type->is_struct() || type->is_array());
897
898 /* If the constant is a record, the types of each of the entries in
899 * value_list must be a 1-for-1 match with the structure components. Each
900 * entry must also be a constant. Just move the nodes from the value_list
901 * to the list in the ir_constant.
902 */
903 if (type->is_array() || type->is_struct()) {
904 this->const_elements = ralloc_array(this, ir_constant *, type->length);
905 unsigned i = 0;
906 foreach_in_list(ir_constant, value, value_list) {
907 assert(value->as_constant() != NULL);
908
909 this->const_elements[i++] = value;
910 }
911 return;
912 }
913
914 for (unsigned i = 0; i < 16; i++) {
915 this->value.u[i] = 0;
916 }
917
918 ir_constant *value = (ir_constant *) (value_list->get_head_raw());
919
920 /* Constructors with exactly one scalar argument are special for vectors
921 * and matrices. For vectors, the scalar value is replicated to fill all
922 * the components. For matrices, the scalar fills the components of the
923 * diagonal while the rest is filled with 0.
924 */
925 if (value->type->is_scalar() && value->next->is_tail_sentinel()) {
926 if (type->is_matrix()) {
927 /* Matrix - fill diagonal (rest is already set to 0) */
928 for (unsigned i = 0; i < type->matrix_columns; i++) {
929 switch (type->base_type) {
930 case GLSL_TYPE_FLOAT:
931 this->value.f[i * type->vector_elements + i] =
932 value->value.f[0];
933 break;
934 case GLSL_TYPE_DOUBLE:
935 this->value.d[i * type->vector_elements + i] =
936 value->value.d[0];
937 break;
938 case GLSL_TYPE_FLOAT16:
939 this->value.f16[i * type->vector_elements + i] =
940 value->value.f16[0];
941 break;
942 default:
943 assert(!"unexpected matrix base type");
944 }
945 }
946 } else {
947 /* Vector or scalar - fill all components */
948 switch (type->base_type) {
949 case GLSL_TYPE_UINT16:
950 case GLSL_TYPE_INT16:
951 for (unsigned i = 0; i < type->components(); i++)
952 this->value.u16[i] = value->value.u16[0];
953 break;
954 case GLSL_TYPE_UINT:
955 case GLSL_TYPE_INT:
956 for (unsigned i = 0; i < type->components(); i++)
957 this->value.u[i] = value->value.u[0];
958 break;
959 case GLSL_TYPE_FLOAT:
960 for (unsigned i = 0; i < type->components(); i++)
961 this->value.f[i] = value->value.f[0];
962 break;
963 case GLSL_TYPE_FLOAT16:
964 for (unsigned i = 0; i < type->components(); i++)
965 this->value.f16[i] = value->value.f16[0];
966 break;
967 case GLSL_TYPE_DOUBLE:
968 for (unsigned i = 0; i < type->components(); i++)
969 this->value.d[i] = value->value.d[0];
970 break;
971 case GLSL_TYPE_UINT64:
972 case GLSL_TYPE_INT64:
973 for (unsigned i = 0; i < type->components(); i++)
974 this->value.u64[i] = value->value.u64[0];
975 break;
976 case GLSL_TYPE_BOOL:
977 for (unsigned i = 0; i < type->components(); i++)
978 this->value.b[i] = value->value.b[0];
979 break;
980 case GLSL_TYPE_SAMPLER:
981 case GLSL_TYPE_IMAGE:
982 this->value.u64[0] = value->value.u64[0];
983 break;
984 default:
985 assert(!"Should not get here.");
986 break;
987 }
988 }
989 return;
990 }
991
992 if (type->is_matrix() && value->type->is_matrix()) {
993 assert(value->next->is_tail_sentinel());
994
995 /* From section 5.4.2 of the GLSL 1.20 spec:
996 * "If a matrix is constructed from a matrix, then each component
997 * (column i, row j) in the result that has a corresponding component
998 * (column i, row j) in the argument will be initialized from there."
999 */
1000 unsigned cols = MIN2(type->matrix_columns, value->type->matrix_columns);
1001 unsigned rows = MIN2(type->vector_elements, value->type->vector_elements);
1002 for (unsigned i = 0; i < cols; i++) {
1003 for (unsigned j = 0; j < rows; j++) {
1004 const unsigned src = i * value->type->vector_elements + j;
1005 const unsigned dst = i * type->vector_elements + j;
1006 this->value.f[dst] = value->value.f[src];
1007 }
1008 }
1009
1010 /* "All other components will be initialized to the identity matrix." */
1011 for (unsigned i = cols; i < type->matrix_columns; i++)
1012 this->value.f[i * type->vector_elements + i] = 1.0;
1013
1014 return;
1015 }
1016
1017 /* Use each component from each entry in the value_list to initialize one
1018 * component of the constant being constructed.
1019 */
1020 unsigned i = 0;
1021 for (;;) {
1022 assert(value->as_constant() != NULL);
1023 assert(!value->is_tail_sentinel());
1024
1025 for (unsigned j = 0; j < value->type->components(); j++) {
1026 switch (type->base_type) {
1027 case GLSL_TYPE_UINT16:
1028 this->value.u16[i] = value->get_uint16_component(j);
1029 break;
1030 case GLSL_TYPE_INT16:
1031 this->value.i16[i] = value->get_int16_component(j);
1032 break;
1033 case GLSL_TYPE_UINT:
1034 this->value.u[i] = value->get_uint_component(j);
1035 break;
1036 case GLSL_TYPE_INT:
1037 this->value.i[i] = value->get_int_component(j);
1038 break;
1039 case GLSL_TYPE_FLOAT:
1040 this->value.f[i] = value->get_float_component(j);
1041 break;
1042 case GLSL_TYPE_FLOAT16:
1043 this->value.f16[i] = value->get_float16_component(j);
1044 break;
1045 case GLSL_TYPE_BOOL:
1046 this->value.b[i] = value->get_bool_component(j);
1047 break;
1048 case GLSL_TYPE_DOUBLE:
1049 this->value.d[i] = value->get_double_component(j);
1050 break;
1051 case GLSL_TYPE_UINT64:
1052 this->value.u64[i] = value->get_uint64_component(j);
1053 break;
1054 case GLSL_TYPE_INT64:
1055 this->value.i64[i] = value->get_int64_component(j);
1056 break;
1057 default:
1058 /* FINISHME: What to do? Exceptions are not the answer.
1059 */
1060 break;
1061 }
1062
1063 i++;
1064 if (i >= type->components())
1065 break;
1066 }
1067
1068 if (i >= type->components())
1069 break; /* avoid downcasting a list sentinel */
1070 value = (ir_constant *) value->next;
1071 }
1072 }
1073
1074 ir_constant *
zero(void * mem_ctx,const glsl_type * type)1075 ir_constant::zero(void *mem_ctx, const glsl_type *type)
1076 {
1077 assert(type->is_scalar() || type->is_vector() || type->is_matrix()
1078 || type->is_struct() || type->is_array());
1079
1080 ir_constant *c = new(mem_ctx) ir_constant;
1081 c->type = type;
1082 memset(&c->value, 0, sizeof(c->value));
1083
1084 if (type->is_array()) {
1085 c->const_elements = ralloc_array(c, ir_constant *, type->length);
1086
1087 for (unsigned i = 0; i < type->length; i++)
1088 c->const_elements[i] = ir_constant::zero(c, type->fields.array);
1089 }
1090
1091 if (type->is_struct()) {
1092 c->const_elements = ralloc_array(c, ir_constant *, type->length);
1093
1094 for (unsigned i = 0; i < type->length; i++) {
1095 c->const_elements[i] =
1096 ir_constant::zero(mem_ctx, type->fields.structure[i].type);
1097 }
1098 }
1099
1100 return c;
1101 }
1102
1103 bool
get_bool_component(unsigned i) const1104 ir_constant::get_bool_component(unsigned i) const
1105 {
1106 switch (this->type->base_type) {
1107 case GLSL_TYPE_UINT16:return this->value.u16[i] != 0;
1108 case GLSL_TYPE_INT16: return this->value.i16[i] != 0;
1109 case GLSL_TYPE_UINT: return this->value.u[i] != 0;
1110 case GLSL_TYPE_INT: return this->value.i[i] != 0;
1111 case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
1112 case GLSL_TYPE_FLOAT16: return ((int)_mesa_half_to_float(this->value.f16[i])) != 0;
1113 case GLSL_TYPE_BOOL: return this->value.b[i];
1114 case GLSL_TYPE_DOUBLE: return this->value.d[i] != 0.0;
1115 case GLSL_TYPE_SAMPLER:
1116 case GLSL_TYPE_IMAGE:
1117 case GLSL_TYPE_UINT64: return this->value.u64[i] != 0;
1118 case GLSL_TYPE_INT64: return this->value.i64[i] != 0;
1119 default: assert(!"Should not get here."); break;
1120 }
1121
1122 /* Must return something to make the compiler happy. This is clearly an
1123 * error case.
1124 */
1125 return false;
1126 }
1127
1128 float
get_float_component(unsigned i) const1129 ir_constant::get_float_component(unsigned i) const
1130 {
1131 switch (this->type->base_type) {
1132 case GLSL_TYPE_UINT16:return (float) this->value.u16[i];
1133 case GLSL_TYPE_INT16: return (float) this->value.i16[i];
1134 case GLSL_TYPE_UINT: return (float) this->value.u[i];
1135 case GLSL_TYPE_INT: return (float) this->value.i[i];
1136 case GLSL_TYPE_FLOAT: return this->value.f[i];
1137 case GLSL_TYPE_FLOAT16: return _mesa_half_to_float(this->value.f16[i]);
1138 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0f : 0.0f;
1139 case GLSL_TYPE_DOUBLE: return (float) this->value.d[i];
1140 case GLSL_TYPE_SAMPLER:
1141 case GLSL_TYPE_IMAGE:
1142 case GLSL_TYPE_UINT64: return (float) this->value.u64[i];
1143 case GLSL_TYPE_INT64: return (float) this->value.i64[i];
1144 default: assert(!"Should not get here."); break;
1145 }
1146
1147 /* Must return something to make the compiler happy. This is clearly an
1148 * error case.
1149 */
1150 return 0.0;
1151 }
1152
1153 uint16_t
get_float16_component(unsigned i) const1154 ir_constant::get_float16_component(unsigned i) const
1155 {
1156 if (this->type->base_type == GLSL_TYPE_FLOAT16)
1157 return this->value.f16[i];
1158 else
1159 return _mesa_float_to_half(get_float_component(i));
1160 }
1161
1162 double
get_double_component(unsigned i) const1163 ir_constant::get_double_component(unsigned i) const
1164 {
1165 switch (this->type->base_type) {
1166 case GLSL_TYPE_UINT16:return (double) this->value.u16[i];
1167 case GLSL_TYPE_INT16: return (double) this->value.i16[i];
1168 case GLSL_TYPE_UINT: return (double) this->value.u[i];
1169 case GLSL_TYPE_INT: return (double) this->value.i[i];
1170 case GLSL_TYPE_FLOAT: return (double) this->value.f[i];
1171 case GLSL_TYPE_FLOAT16: return (double) _mesa_half_to_float(this->value.f16[i]);
1172 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0 : 0.0;
1173 case GLSL_TYPE_DOUBLE: return this->value.d[i];
1174 case GLSL_TYPE_SAMPLER:
1175 case GLSL_TYPE_IMAGE:
1176 case GLSL_TYPE_UINT64: return (double) this->value.u64[i];
1177 case GLSL_TYPE_INT64: return (double) this->value.i64[i];
1178 default: assert(!"Should not get here."); break;
1179 }
1180
1181 /* Must return something to make the compiler happy. This is clearly an
1182 * error case.
1183 */
1184 return 0.0;
1185 }
1186
1187 int16_t
get_int16_component(unsigned i) const1188 ir_constant::get_int16_component(unsigned i) const
1189 {
1190 switch (this->type->base_type) {
1191 case GLSL_TYPE_UINT16:return this->value.u16[i];
1192 case GLSL_TYPE_INT16: return this->value.i16[i];
1193 case GLSL_TYPE_UINT: return this->value.u[i];
1194 case GLSL_TYPE_INT: return this->value.i[i];
1195 case GLSL_TYPE_FLOAT: return (int16_t) this->value.f[i];
1196 case GLSL_TYPE_FLOAT16: return (int16_t) _mesa_half_to_float(this->value.f16[i]);
1197 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1198 case GLSL_TYPE_DOUBLE: return (int16_t) this->value.d[i];
1199 case GLSL_TYPE_SAMPLER:
1200 case GLSL_TYPE_IMAGE:
1201 case GLSL_TYPE_UINT64: return (int16_t) this->value.u64[i];
1202 case GLSL_TYPE_INT64: return (int16_t) this->value.i64[i];
1203 default: assert(!"Should not get here."); break;
1204 }
1205
1206 /* Must return something to make the compiler happy. This is clearly an
1207 * error case.
1208 */
1209 return 0;
1210 }
1211
1212 uint16_t
get_uint16_component(unsigned i) const1213 ir_constant::get_uint16_component(unsigned i) const
1214 {
1215 switch (this->type->base_type) {
1216 case GLSL_TYPE_UINT16:return this->value.u16[i];
1217 case GLSL_TYPE_INT16: return this->value.i16[i];
1218 case GLSL_TYPE_UINT: return this->value.u[i];
1219 case GLSL_TYPE_INT: return this->value.i[i];
1220 case GLSL_TYPE_FLOAT: return (uint16_t) this->value.f[i];
1221 case GLSL_TYPE_FLOAT16: return (uint16_t) _mesa_half_to_float(this->value.f16[i]);
1222 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1223 case GLSL_TYPE_DOUBLE: return (uint16_t) this->value.d[i];
1224 case GLSL_TYPE_SAMPLER:
1225 case GLSL_TYPE_IMAGE:
1226 case GLSL_TYPE_UINT64: return (uint16_t) this->value.u64[i];
1227 case GLSL_TYPE_INT64: return (uint16_t) this->value.i64[i];
1228 default: assert(!"Should not get here."); break;
1229 }
1230
1231 /* Must return something to make the compiler happy. This is clearly an
1232 * error case.
1233 */
1234 return 0;
1235 }
1236
1237 int
get_int_component(unsigned i) const1238 ir_constant::get_int_component(unsigned i) const
1239 {
1240 switch (this->type->base_type) {
1241 case GLSL_TYPE_UINT16:return this->value.u16[i];
1242 case GLSL_TYPE_INT16: return this->value.i16[i];
1243 case GLSL_TYPE_UINT: return this->value.u[i];
1244 case GLSL_TYPE_INT: return this->value.i[i];
1245 case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
1246 case GLSL_TYPE_FLOAT16: return (int) _mesa_half_to_float(this->value.f16[i]);
1247 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1248 case GLSL_TYPE_DOUBLE: return (int) this->value.d[i];
1249 case GLSL_TYPE_SAMPLER:
1250 case GLSL_TYPE_IMAGE:
1251 case GLSL_TYPE_UINT64: return (int) this->value.u64[i];
1252 case GLSL_TYPE_INT64: return (int) this->value.i64[i];
1253 default: assert(!"Should not get here."); break;
1254 }
1255
1256 /* Must return something to make the compiler happy. This is clearly an
1257 * error case.
1258 */
1259 return 0;
1260 }
1261
1262 unsigned
get_uint_component(unsigned i) const1263 ir_constant::get_uint_component(unsigned i) const
1264 {
1265 switch (this->type->base_type) {
1266 case GLSL_TYPE_UINT16:return this->value.u16[i];
1267 case GLSL_TYPE_INT16: return this->value.i16[i];
1268 case GLSL_TYPE_UINT: return this->value.u[i];
1269 case GLSL_TYPE_INT: return this->value.i[i];
1270 case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
1271 case GLSL_TYPE_FLOAT16: return (unsigned) _mesa_half_to_float(this->value.f16[i]);
1272 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1273 case GLSL_TYPE_DOUBLE: return (unsigned) this->value.d[i];
1274 case GLSL_TYPE_SAMPLER:
1275 case GLSL_TYPE_IMAGE:
1276 case GLSL_TYPE_UINT64: return (unsigned) this->value.u64[i];
1277 case GLSL_TYPE_INT64: return (unsigned) this->value.i64[i];
1278 default: assert(!"Should not get here."); break;
1279 }
1280
1281 /* Must return something to make the compiler happy. This is clearly an
1282 * error case.
1283 */
1284 return 0;
1285 }
1286
1287 int64_t
get_int64_component(unsigned i) const1288 ir_constant::get_int64_component(unsigned i) const
1289 {
1290 switch (this->type->base_type) {
1291 case GLSL_TYPE_UINT16:return this->value.u16[i];
1292 case GLSL_TYPE_INT16: return this->value.i16[i];
1293 case GLSL_TYPE_UINT: return this->value.u[i];
1294 case GLSL_TYPE_INT: return this->value.i[i];
1295 case GLSL_TYPE_FLOAT: return (int64_t) this->value.f[i];
1296 case GLSL_TYPE_FLOAT16: return (int64_t) _mesa_half_to_float(this->value.f16[i]);
1297 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1298 case GLSL_TYPE_DOUBLE: return (int64_t) this->value.d[i];
1299 case GLSL_TYPE_SAMPLER:
1300 case GLSL_TYPE_IMAGE:
1301 case GLSL_TYPE_UINT64: return (int64_t) this->value.u64[i];
1302 case GLSL_TYPE_INT64: return this->value.i64[i];
1303 default: assert(!"Should not get here."); break;
1304 }
1305
1306 /* Must return something to make the compiler happy. This is clearly an
1307 * error case.
1308 */
1309 return 0;
1310 }
1311
1312 uint64_t
get_uint64_component(unsigned i) const1313 ir_constant::get_uint64_component(unsigned i) const
1314 {
1315 switch (this->type->base_type) {
1316 case GLSL_TYPE_UINT16:return this->value.u16[i];
1317 case GLSL_TYPE_INT16: return this->value.i16[i];
1318 case GLSL_TYPE_UINT: return this->value.u[i];
1319 case GLSL_TYPE_INT: return this->value.i[i];
1320 case GLSL_TYPE_FLOAT: return (uint64_t) this->value.f[i];
1321 case GLSL_TYPE_FLOAT16: return (uint64_t) _mesa_half_to_float(this->value.f16[i]);
1322 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
1323 case GLSL_TYPE_DOUBLE: return (uint64_t) this->value.d[i];
1324 case GLSL_TYPE_SAMPLER:
1325 case GLSL_TYPE_IMAGE:
1326 case GLSL_TYPE_UINT64: return this->value.u64[i];
1327 case GLSL_TYPE_INT64: return (uint64_t) this->value.i64[i];
1328 default: assert(!"Should not get here."); break;
1329 }
1330
1331 /* Must return something to make the compiler happy. This is clearly an
1332 * error case.
1333 */
1334 return 0;
1335 }
1336
1337 ir_constant *
get_array_element(unsigned i) const1338 ir_constant::get_array_element(unsigned i) const
1339 {
1340 assert(this->type->is_array());
1341
1342 /* From page 35 (page 41 of the PDF) of the GLSL 1.20 spec:
1343 *
1344 * "Behavior is undefined if a shader subscripts an array with an index
1345 * less than 0 or greater than or equal to the size the array was
1346 * declared with."
1347 *
1348 * Most out-of-bounds accesses are removed before things could get this far.
1349 * There are cases where non-constant array index values can get constant
1350 * folded.
1351 */
1352 if (int(i) < 0)
1353 i = 0;
1354 else if (i >= this->type->length)
1355 i = this->type->length - 1;
1356
1357 return const_elements[i];
1358 }
1359
1360 ir_constant *
get_record_field(int idx)1361 ir_constant::get_record_field(int idx)
1362 {
1363 assert(this->type->is_struct());
1364 assert(idx >= 0 && (unsigned) idx < this->type->length);
1365
1366 return const_elements[idx];
1367 }
1368
1369 void
copy_offset(ir_constant * src,int offset)1370 ir_constant::copy_offset(ir_constant *src, int offset)
1371 {
1372 switch (this->type->base_type) {
1373 case GLSL_TYPE_UINT16:
1374 case GLSL_TYPE_INT16:
1375 case GLSL_TYPE_UINT:
1376 case GLSL_TYPE_INT:
1377 case GLSL_TYPE_FLOAT:
1378 case GLSL_TYPE_FLOAT16:
1379 case GLSL_TYPE_DOUBLE:
1380 case GLSL_TYPE_SAMPLER:
1381 case GLSL_TYPE_IMAGE:
1382 case GLSL_TYPE_UINT64:
1383 case GLSL_TYPE_INT64:
1384 case GLSL_TYPE_BOOL: {
1385 unsigned int size = src->type->components();
1386 assert (size <= this->type->components() - offset);
1387 for (unsigned int i=0; i<size; i++) {
1388 switch (this->type->base_type) {
1389 case GLSL_TYPE_UINT16:
1390 value.u16[i+offset] = src->get_uint16_component(i);
1391 break;
1392 case GLSL_TYPE_INT16:
1393 value.i16[i+offset] = src->get_int16_component(i);
1394 break;
1395 case GLSL_TYPE_UINT:
1396 value.u[i+offset] = src->get_uint_component(i);
1397 break;
1398 case GLSL_TYPE_INT:
1399 value.i[i+offset] = src->get_int_component(i);
1400 break;
1401 case GLSL_TYPE_FLOAT:
1402 value.f[i+offset] = src->get_float_component(i);
1403 break;
1404 case GLSL_TYPE_FLOAT16:
1405 value.f16[i+offset] = src->get_float16_component(i);
1406 break;
1407 case GLSL_TYPE_BOOL:
1408 value.b[i+offset] = src->get_bool_component(i);
1409 break;
1410 case GLSL_TYPE_DOUBLE:
1411 value.d[i+offset] = src->get_double_component(i);
1412 break;
1413 case GLSL_TYPE_SAMPLER:
1414 case GLSL_TYPE_IMAGE:
1415 case GLSL_TYPE_UINT64:
1416 value.u64[i+offset] = src->get_uint64_component(i);
1417 break;
1418 case GLSL_TYPE_INT64:
1419 value.i64[i+offset] = src->get_int64_component(i);
1420 break;
1421 default: // Shut up the compiler
1422 break;
1423 }
1424 }
1425 break;
1426 }
1427
1428 case GLSL_TYPE_STRUCT:
1429 case GLSL_TYPE_ARRAY: {
1430 assert (src->type == this->type);
1431 for (unsigned i = 0; i < this->type->length; i++) {
1432 this->const_elements[i] = src->const_elements[i]->clone(this, NULL);
1433 }
1434 break;
1435 }
1436
1437 default:
1438 assert(!"Should not get here.");
1439 break;
1440 }
1441 }
1442
1443 void
copy_masked_offset(ir_constant * src,int offset,unsigned int mask)1444 ir_constant::copy_masked_offset(ir_constant *src, int offset, unsigned int mask)
1445 {
1446 assert (!type->is_array() && !type->is_struct());
1447
1448 if (!type->is_vector() && !type->is_matrix()) {
1449 offset = 0;
1450 mask = 1;
1451 }
1452
1453 int id = 0;
1454 for (int i=0; i<4; i++) {
1455 if (mask & (1 << i)) {
1456 switch (this->type->base_type) {
1457 case GLSL_TYPE_UINT16:
1458 value.u16[i+offset] = src->get_uint16_component(id++);
1459 break;
1460 case GLSL_TYPE_INT16:
1461 value.i16[i+offset] = src->get_int16_component(id++);
1462 break;
1463 case GLSL_TYPE_UINT:
1464 value.u[i+offset] = src->get_uint_component(id++);
1465 break;
1466 case GLSL_TYPE_INT:
1467 value.i[i+offset] = src->get_int_component(id++);
1468 break;
1469 case GLSL_TYPE_FLOAT:
1470 value.f[i+offset] = src->get_float_component(id++);
1471 break;
1472 case GLSL_TYPE_FLOAT16:
1473 value.f16[i+offset] = src->get_float16_component(id++);
1474 break;
1475 case GLSL_TYPE_BOOL:
1476 value.b[i+offset] = src->get_bool_component(id++);
1477 break;
1478 case GLSL_TYPE_DOUBLE:
1479 value.d[i+offset] = src->get_double_component(id++);
1480 break;
1481 case GLSL_TYPE_SAMPLER:
1482 case GLSL_TYPE_IMAGE:
1483 case GLSL_TYPE_UINT64:
1484 value.u64[i+offset] = src->get_uint64_component(id++);
1485 break;
1486 case GLSL_TYPE_INT64:
1487 value.i64[i+offset] = src->get_int64_component(id++);
1488 break;
1489 default:
1490 assert(!"Should not get here.");
1491 return;
1492 }
1493 }
1494 }
1495 }
1496
1497 bool
has_value(const ir_constant * c) const1498 ir_constant::has_value(const ir_constant *c) const
1499 {
1500 if (this->type != c->type)
1501 return false;
1502
1503 if (this->type->is_array() || this->type->is_struct()) {
1504 for (unsigned i = 0; i < this->type->length; i++) {
1505 if (!this->const_elements[i]->has_value(c->const_elements[i]))
1506 return false;
1507 }
1508 return true;
1509 }
1510
1511 for (unsigned i = 0; i < this->type->components(); i++) {
1512 switch (this->type->base_type) {
1513 case GLSL_TYPE_UINT16:
1514 if (this->value.u16[i] != c->value.u16[i])
1515 return false;
1516 break;
1517 case GLSL_TYPE_INT16:
1518 if (this->value.i16[i] != c->value.i16[i])
1519 return false;
1520 break;
1521 case GLSL_TYPE_UINT:
1522 if (this->value.u[i] != c->value.u[i])
1523 return false;
1524 break;
1525 case GLSL_TYPE_INT:
1526 if (this->value.i[i] != c->value.i[i])
1527 return false;
1528 break;
1529 case GLSL_TYPE_FLOAT:
1530 if (this->value.f[i] != c->value.f[i])
1531 return false;
1532 break;
1533 case GLSL_TYPE_FLOAT16:
1534 /* Convert to float to make sure NaN and ±0.0 compares correctly */
1535 if (_mesa_half_to_float(this->value.f16[i]) !=
1536 _mesa_half_to_float(c->value.f16[i]))
1537 return false;
1538 break;
1539 case GLSL_TYPE_BOOL:
1540 if (this->value.b[i] != c->value.b[i])
1541 return false;
1542 break;
1543 case GLSL_TYPE_DOUBLE:
1544 if (this->value.d[i] != c->value.d[i])
1545 return false;
1546 break;
1547 case GLSL_TYPE_SAMPLER:
1548 case GLSL_TYPE_IMAGE:
1549 case GLSL_TYPE_UINT64:
1550 if (this->value.u64[i] != c->value.u64[i])
1551 return false;
1552 break;
1553 case GLSL_TYPE_INT64:
1554 if (this->value.i64[i] != c->value.i64[i])
1555 return false;
1556 break;
1557 default:
1558 assert(!"Should not get here.");
1559 return false;
1560 }
1561 }
1562
1563 return true;
1564 }
1565
1566 bool
is_value(float f,int i) const1567 ir_constant::is_value(float f, int i) const
1568 {
1569 if (!this->type->is_scalar() && !this->type->is_vector())
1570 return false;
1571
1572 /* Only accept boolean values for 0/1. */
1573 if (int(bool(i)) != i && this->type->is_boolean())
1574 return false;
1575
1576 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1577 switch (this->type->base_type) {
1578 case GLSL_TYPE_FLOAT:
1579 if (this->value.f[c] != f)
1580 return false;
1581 break;
1582 case GLSL_TYPE_FLOAT16:
1583 if (_mesa_half_to_float(this->value.f16[c]) != f)
1584 return false;
1585 break;
1586 case GLSL_TYPE_INT16:
1587 if (this->value.i16[c] != int16_t(i))
1588 return false;
1589 break;
1590 case GLSL_TYPE_UINT16:
1591 if (this->value.u16[c] != uint16_t(i))
1592 return false;
1593 break;
1594 case GLSL_TYPE_INT:
1595 if (this->value.i[c] != i)
1596 return false;
1597 break;
1598 case GLSL_TYPE_UINT:
1599 if (this->value.u[c] != unsigned(i))
1600 return false;
1601 break;
1602 case GLSL_TYPE_BOOL:
1603 if (this->value.b[c] != bool(i))
1604 return false;
1605 break;
1606 case GLSL_TYPE_DOUBLE:
1607 if (this->value.d[c] != double(f))
1608 return false;
1609 break;
1610 case GLSL_TYPE_SAMPLER:
1611 case GLSL_TYPE_IMAGE:
1612 case GLSL_TYPE_UINT64:
1613 if (this->value.u64[c] != uint64_t(i))
1614 return false;
1615 break;
1616 case GLSL_TYPE_INT64:
1617 if (this->value.i64[c] != i)
1618 return false;
1619 break;
1620 default:
1621 /* The only other base types are structures, arrays, and samplers.
1622 * Samplers cannot be constants, and the others should have been
1623 * filtered out above.
1624 */
1625 assert(!"Should not get here.");
1626 return false;
1627 }
1628 }
1629
1630 return true;
1631 }
1632
1633 bool
is_zero() const1634 ir_constant::is_zero() const
1635 {
1636 return is_value(0.0, 0);
1637 }
1638
1639 bool
is_one() const1640 ir_constant::is_one() const
1641 {
1642 return is_value(1.0, 1);
1643 }
1644
1645 bool
is_negative_one() const1646 ir_constant::is_negative_one() const
1647 {
1648 return is_value(-1.0, -1);
1649 }
1650
1651 bool
is_uint16_constant() const1652 ir_constant::is_uint16_constant() const
1653 {
1654 if (!type->is_integer_32())
1655 return false;
1656
1657 return value.u[0] < (1 << 16);
1658 }
1659
ir_loop()1660 ir_loop::ir_loop()
1661 : ir_instruction(ir_type_loop)
1662 {
1663 }
1664
1665
ir_dereference_variable(ir_variable * var)1666 ir_dereference_variable::ir_dereference_variable(ir_variable *var)
1667 : ir_dereference(ir_type_dereference_variable)
1668 {
1669 assert(var != NULL);
1670
1671 this->var = var;
1672 this->type = var->type;
1673 }
1674
1675
ir_dereference_array(ir_rvalue * value,ir_rvalue * array_index)1676 ir_dereference_array::ir_dereference_array(ir_rvalue *value,
1677 ir_rvalue *array_index)
1678 : ir_dereference(ir_type_dereference_array)
1679 {
1680 this->array_index = array_index;
1681 this->set_array(value);
1682 }
1683
1684
ir_dereference_array(ir_variable * var,ir_rvalue * array_index)1685 ir_dereference_array::ir_dereference_array(ir_variable *var,
1686 ir_rvalue *array_index)
1687 : ir_dereference(ir_type_dereference_array)
1688 {
1689 void *ctx = ralloc_parent(var);
1690
1691 this->array_index = array_index;
1692 this->set_array(new(ctx) ir_dereference_variable(var));
1693 }
1694
1695
1696 void
set_array(ir_rvalue * value)1697 ir_dereference_array::set_array(ir_rvalue *value)
1698 {
1699 assert(value != NULL);
1700
1701 this->array = value;
1702
1703 const glsl_type *const vt = this->array->type;
1704
1705 if (vt->is_array()) {
1706 type = vt->fields.array;
1707 } else if (vt->is_matrix()) {
1708 type = vt->column_type();
1709 } else if (vt->is_vector()) {
1710 type = vt->get_base_type();
1711 }
1712 }
1713
1714
ir_dereference_record(ir_rvalue * value,const char * field)1715 ir_dereference_record::ir_dereference_record(ir_rvalue *value,
1716 const char *field)
1717 : ir_dereference(ir_type_dereference_record)
1718 {
1719 assert(value != NULL);
1720
1721 this->record = value;
1722 this->type = this->record->type->field_type(field);
1723 this->field_idx = this->record->type->field_index(field);
1724 }
1725
1726
ir_dereference_record(ir_variable * var,const char * field)1727 ir_dereference_record::ir_dereference_record(ir_variable *var,
1728 const char *field)
1729 : ir_dereference(ir_type_dereference_record)
1730 {
1731 void *ctx = ralloc_parent(var);
1732
1733 this->record = new(ctx) ir_dereference_variable(var);
1734 this->type = this->record->type->field_type(field);
1735 this->field_idx = this->record->type->field_index(field);
1736 }
1737
1738 bool
is_lvalue(const struct _mesa_glsl_parse_state * state) const1739 ir_dereference::is_lvalue(const struct _mesa_glsl_parse_state *state) const
1740 {
1741 ir_variable *var = this->variable_referenced();
1742
1743 /* Every l-value dereference chain eventually ends in a variable.
1744 */
1745 if ((var == NULL) || var->data.read_only)
1746 return false;
1747
1748 /* From section 4.1.7 of the ARB_bindless_texture spec:
1749 *
1750 * "Samplers can be used as l-values, so can be assigned into and used as
1751 * "out" and "inout" function parameters."
1752 *
1753 * From section 4.1.X of the ARB_bindless_texture spec:
1754 *
1755 * "Images can be used as l-values, so can be assigned into and used as
1756 * "out" and "inout" function parameters."
1757 */
1758 if ((!state || state->has_bindless()) &&
1759 (this->type->contains_sampler() || this->type->contains_image()))
1760 return true;
1761
1762 /* From section 4.1.7 of the GLSL 4.40 spec:
1763 *
1764 * "Opaque variables cannot be treated as l-values; hence cannot
1765 * be used as out or inout function parameters, nor can they be
1766 * assigned into."
1767 */
1768 if (this->type->contains_opaque())
1769 return false;
1770
1771 return true;
1772 }
1773
1774
1775 static const char * const tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf", "txf_ms", "txs", "lod", "tg4", "query_levels", "texture_samples", "samples_identical" };
1776
opcode_string()1777 const char *ir_texture::opcode_string()
1778 {
1779 assert((unsigned int) op < ARRAY_SIZE(tex_opcode_strs));
1780 return tex_opcode_strs[op];
1781 }
1782
1783 ir_texture_opcode
get_opcode(const char * str)1784 ir_texture::get_opcode(const char *str)
1785 {
1786 const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
1787 for (int op = 0; op < count; op++) {
1788 if (strcmp(str, tex_opcode_strs[op]) == 0)
1789 return (ir_texture_opcode) op;
1790 }
1791 return (ir_texture_opcode) -1;
1792 }
1793
1794
1795 void
set_sampler(ir_dereference * sampler,const glsl_type * type)1796 ir_texture::set_sampler(ir_dereference *sampler, const glsl_type *type)
1797 {
1798 assert(sampler != NULL);
1799 assert(type != NULL);
1800 this->sampler = sampler;
1801
1802 if (this->is_sparse) {
1803 /* code holds residency info */
1804 glsl_struct_field fields[2] = {
1805 glsl_struct_field(glsl_type::int_type, "code"),
1806 glsl_struct_field(type, "texel"),
1807 };
1808 this->type = glsl_type::get_struct_instance(fields, 2, "struct");
1809 } else
1810 this->type = type;
1811
1812 if (this->op == ir_txs || this->op == ir_query_levels ||
1813 this->op == ir_texture_samples) {
1814 assert(type->base_type == GLSL_TYPE_INT);
1815 } else if (this->op == ir_lod) {
1816 assert(type->vector_elements == 2);
1817 assert(type->is_float());
1818 } else if (this->op == ir_samples_identical) {
1819 assert(type == glsl_type::bool_type);
1820 assert(sampler->type->is_sampler());
1821 assert(sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS);
1822 } else {
1823 assert(sampler->type->sampled_type == (int) type->base_type);
1824 if (sampler->type->sampler_shadow)
1825 assert(type->vector_elements == 4 || type->vector_elements == 1);
1826 else
1827 assert(type->vector_elements == 4);
1828 }
1829 }
1830
1831
1832 void
init_mask(const unsigned * comp,unsigned count)1833 ir_swizzle::init_mask(const unsigned *comp, unsigned count)
1834 {
1835 assert((count >= 1) && (count <= 4));
1836
1837 memset(&this->mask, 0, sizeof(this->mask));
1838 this->mask.num_components = count;
1839
1840 unsigned dup_mask = 0;
1841 switch (count) {
1842 case 4:
1843 assert(comp[3] <= 3);
1844 dup_mask |= (1U << comp[3])
1845 & ((1U << comp[0]) | (1U << comp[1]) | (1U << comp[2]));
1846 this->mask.w = comp[3];
1847
1848 case 3:
1849 assert(comp[2] <= 3);
1850 dup_mask |= (1U << comp[2])
1851 & ((1U << comp[0]) | (1U << comp[1]));
1852 this->mask.z = comp[2];
1853
1854 case 2:
1855 assert(comp[1] <= 3);
1856 dup_mask |= (1U << comp[1])
1857 & ((1U << comp[0]));
1858 this->mask.y = comp[1];
1859
1860 case 1:
1861 assert(comp[0] <= 3);
1862 this->mask.x = comp[0];
1863 }
1864
1865 this->mask.has_duplicates = dup_mask != 0;
1866
1867 /* Based on the number of elements in the swizzle and the base type
1868 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
1869 * generate the type of the resulting value.
1870 */
1871 type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
1872 }
1873
ir_swizzle(ir_rvalue * val,unsigned x,unsigned y,unsigned z,unsigned w,unsigned count)1874 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
1875 unsigned w, unsigned count)
1876 : ir_rvalue(ir_type_swizzle), val(val)
1877 {
1878 const unsigned components[4] = { x, y, z, w };
1879 this->init_mask(components, count);
1880 }
1881
ir_swizzle(ir_rvalue * val,const unsigned * comp,unsigned count)1882 ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
1883 unsigned count)
1884 : ir_rvalue(ir_type_swizzle), val(val)
1885 {
1886 this->init_mask(comp, count);
1887 }
1888
ir_swizzle(ir_rvalue * val,ir_swizzle_mask mask)1889 ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
1890 : ir_rvalue(ir_type_swizzle), val(val), mask(mask)
1891 {
1892 this->type = glsl_type::get_instance(val->type->base_type,
1893 mask.num_components, 1);
1894 }
1895
1896 #define X 1
1897 #define R 5
1898 #define S 9
1899 #define I 13
1900
1901 ir_swizzle *
create(ir_rvalue * val,const char * str,unsigned vector_length)1902 ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
1903 {
1904 void *ctx = ralloc_parent(val);
1905
1906 /* For each possible swizzle character, this table encodes the value in
1907 * \c idx_map that represents the 0th element of the vector. For invalid
1908 * swizzle characters (e.g., 'k'), a special value is used that will allow
1909 * detection of errors.
1910 */
1911 static const unsigned char base_idx[26] = {
1912 /* a b c d e f g h i j k l m */
1913 R, R, I, I, I, I, R, I, I, I, I, I, I,
1914 /* n o p q r s t u v w x y z */
1915 I, I, S, S, R, S, S, I, I, X, X, X, X
1916 };
1917
1918 /* Each valid swizzle character has an entry in the previous table. This
1919 * table encodes the base index encoded in the previous table plus the actual
1920 * index of the swizzle character. When processing swizzles, the first
1921 * character in the string is indexed in the previous table. Each character
1922 * in the string is indexed in this table, and the value found there has the
1923 * value form the first table subtracted. The result must be on the range
1924 * [0,3].
1925 *
1926 * For example, the string "wzyx" will get X from the first table. Each of
1927 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
1928 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
1929 *
1930 * The string "wzrg" will get X from the first table. Each of the characters
1931 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
1932 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
1933 * [0,3], the error is detected.
1934 */
1935 static const unsigned char idx_map[26] = {
1936 /* a b c d e f g h i j k l m */
1937 R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
1938 /* n o p q r s t u v w x y z */
1939 0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
1940 };
1941
1942 int swiz_idx[4] = { 0, 0, 0, 0 };
1943 unsigned i;
1944
1945
1946 /* Validate the first character in the swizzle string and look up the base
1947 * index value as described above.
1948 */
1949 if ((str[0] < 'a') || (str[0] > 'z'))
1950 return NULL;
1951
1952 const unsigned base = base_idx[str[0] - 'a'];
1953
1954
1955 for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
1956 /* Validate the next character, and, as described above, convert it to a
1957 * swizzle index.
1958 */
1959 if ((str[i] < 'a') || (str[i] > 'z'))
1960 return NULL;
1961
1962 swiz_idx[i] = idx_map[str[i] - 'a'] - base;
1963 if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
1964 return NULL;
1965 }
1966
1967 if (str[i] != '\0')
1968 return NULL;
1969
1970 return new(ctx) ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
1971 swiz_idx[3], i);
1972 }
1973
1974 #undef X
1975 #undef R
1976 #undef S
1977 #undef I
1978
1979 ir_variable *
variable_referenced() const1980 ir_swizzle::variable_referenced() const
1981 {
1982 return this->val->variable_referenced();
1983 }
1984
1985
1986 bool ir_variable::temporaries_allocate_names = false;
1987
1988 const char ir_variable::tmp_name[] = "compiler_temp";
1989
ir_variable(const struct glsl_type * type,const char * name,ir_variable_mode mode)1990 ir_variable::ir_variable(const struct glsl_type *type, const char *name,
1991 ir_variable_mode mode)
1992 : ir_instruction(ir_type_variable)
1993 {
1994 this->type = type;
1995
1996 if (mode == ir_var_temporary && !ir_variable::temporaries_allocate_names)
1997 name = NULL;
1998
1999 /* The ir_variable clone method may call this constructor with name set to
2000 * tmp_name.
2001 */
2002 assert(name != NULL
2003 || mode == ir_var_temporary
2004 || mode == ir_var_function_in
2005 || mode == ir_var_function_out
2006 || mode == ir_var_function_inout);
2007 assert(name != ir_variable::tmp_name
2008 || mode == ir_var_temporary);
2009 if (mode == ir_var_temporary
2010 && (name == NULL || name == ir_variable::tmp_name)) {
2011 this->name = ir_variable::tmp_name;
2012 } else if (name == NULL ||
2013 strlen(name) < ARRAY_SIZE(this->name_storage)) {
2014 strcpy(this->name_storage, name ? name : "");
2015 this->name = this->name_storage;
2016 } else {
2017 this->name = ralloc_strdup(this, name);
2018 }
2019
2020 this->u.max_ifc_array_access = NULL;
2021
2022 this->data.explicit_location = false;
2023 this->data.explicit_index = false;
2024 this->data.explicit_binding = false;
2025 this->data.explicit_component = false;
2026 this->data.has_initializer = false;
2027 this->data.is_implicit_initializer = false;
2028 this->data.is_xfb = false;
2029 this->data.is_xfb_only = false;
2030 this->data.explicit_xfb_buffer = false;
2031 this->data.explicit_xfb_offset = false;
2032 this->data.explicit_xfb_stride = false;
2033 this->data.location = -1;
2034 this->data.location_frac = 0;
2035 this->data.matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
2036 this->data.from_named_ifc_block = false;
2037 this->data.must_be_shader_input = false;
2038 this->data.index = 0;
2039 this->data.binding = 0;
2040 this->data.warn_extension_index = 0;
2041 this->constant_value = NULL;
2042 this->constant_initializer = NULL;
2043 this->data.depth_layout = ir_depth_layout_none;
2044 this->data.used = false;
2045 this->data.assigned = false;
2046 this->data.always_active_io = false;
2047 this->data.read_only = false;
2048 this->data.centroid = false;
2049 this->data.sample = false;
2050 this->data.patch = false;
2051 this->data.explicit_invariant = false;
2052 this->data.invariant = false;
2053 this->data.precise = false;
2054 this->data.how_declared = ir_var_declared_normally;
2055 this->data.mode = mode;
2056 this->data.interpolation = INTERP_MODE_NONE;
2057 this->data.max_array_access = -1;
2058 this->data.offset = 0;
2059 this->data.precision = GLSL_PRECISION_NONE;
2060 this->data.memory_read_only = false;
2061 this->data.memory_write_only = false;
2062 this->data.memory_coherent = false;
2063 this->data.memory_volatile = false;
2064 this->data.memory_restrict = false;
2065 this->data.from_ssbo_unsized_array = false;
2066 this->data.implicit_sized_array = false;
2067 this->data.fb_fetch_output = false;
2068 this->data.bindless = false;
2069 this->data.bound = false;
2070 this->data.image_format = PIPE_FORMAT_NONE;
2071 this->data._num_state_slots = 0;
2072 this->data.param_index = 0;
2073 this->data.stream = 0;
2074 this->data.xfb_buffer = -1;
2075 this->data.xfb_stride = -1;
2076 this->data.implicit_conversion_prohibited = false;
2077
2078 this->interface_type = NULL;
2079
2080 if (type != NULL) {
2081 if (type->is_interface())
2082 this->init_interface_type(type);
2083 else if (type->without_array()->is_interface())
2084 this->init_interface_type(type->without_array());
2085 }
2086 }
2087
2088
2089 const char *
interpolation_string(unsigned interpolation)2090 interpolation_string(unsigned interpolation)
2091 {
2092 switch (interpolation) {
2093 case INTERP_MODE_NONE: return "no";
2094 case INTERP_MODE_SMOOTH: return "smooth";
2095 case INTERP_MODE_FLAT: return "flat";
2096 case INTERP_MODE_NOPERSPECTIVE: return "noperspective";
2097 }
2098
2099 assert(!"Should not get here.");
2100 return "";
2101 }
2102
2103 const char *const ir_variable::warn_extension_table[] = {
2104 "",
2105 "GL_ARB_shader_stencil_export",
2106 "GL_AMD_shader_stencil_export",
2107 };
2108
2109 void
enable_extension_warning(const char * extension)2110 ir_variable::enable_extension_warning(const char *extension)
2111 {
2112 for (unsigned i = 0; i < ARRAY_SIZE(warn_extension_table); i++) {
2113 if (strcmp(warn_extension_table[i], extension) == 0) {
2114 this->data.warn_extension_index = i;
2115 return;
2116 }
2117 }
2118
2119 assert(!"Should not get here.");
2120 this->data.warn_extension_index = 0;
2121 }
2122
2123 const char *
get_extension_warning() const2124 ir_variable::get_extension_warning() const
2125 {
2126 return this->data.warn_extension_index == 0
2127 ? NULL : warn_extension_table[this->data.warn_extension_index];
2128 }
2129
ir_function_signature(const glsl_type * return_type,builtin_available_predicate b)2130 ir_function_signature::ir_function_signature(const glsl_type *return_type,
2131 builtin_available_predicate b)
2132 : ir_instruction(ir_type_function_signature),
2133 return_type(return_type), is_defined(false),
2134 return_precision(GLSL_PRECISION_NONE),
2135 intrinsic_id(ir_intrinsic_invalid), builtin_avail(b), _function(NULL)
2136 {
2137 this->origin = NULL;
2138 }
2139
2140
2141 bool
is_builtin() const2142 ir_function_signature::is_builtin() const
2143 {
2144 return builtin_avail != NULL;
2145 }
2146
2147
2148 bool
is_builtin_available(const _mesa_glsl_parse_state * state) const2149 ir_function_signature::is_builtin_available(const _mesa_glsl_parse_state *state) const
2150 {
2151 /* We can't call the predicate without a state pointer, so just say that
2152 * the signature is available. At compile time, we need the filtering,
2153 * but also receive a valid state pointer. At link time, we're resolving
2154 * imported built-in prototypes to their definitions, which will always
2155 * be an exact match. So we can skip the filtering.
2156 */
2157 if (state == NULL)
2158 return true;
2159
2160 assert(builtin_avail != NULL);
2161 return builtin_avail(state);
2162 }
2163
2164
2165 static bool
modes_match(unsigned a,unsigned b)2166 modes_match(unsigned a, unsigned b)
2167 {
2168 if (a == b)
2169 return true;
2170
2171 /* Accept "in" vs. "const in" */
2172 if ((a == ir_var_const_in && b == ir_var_function_in) ||
2173 (b == ir_var_const_in && a == ir_var_function_in))
2174 return true;
2175
2176 return false;
2177 }
2178
2179
2180 const char *
qualifiers_match(exec_list * params)2181 ir_function_signature::qualifiers_match(exec_list *params)
2182 {
2183 /* check that the qualifiers match. */
2184 foreach_two_lists(a_node, &this->parameters, b_node, params) {
2185 ir_variable *a = (ir_variable *) a_node;
2186 ir_variable *b = (ir_variable *) b_node;
2187
2188 if (a->data.read_only != b->data.read_only ||
2189 !modes_match(a->data.mode, b->data.mode) ||
2190 a->data.interpolation != b->data.interpolation ||
2191 a->data.centroid != b->data.centroid ||
2192 a->data.sample != b->data.sample ||
2193 a->data.patch != b->data.patch ||
2194 a->data.memory_read_only != b->data.memory_read_only ||
2195 a->data.memory_write_only != b->data.memory_write_only ||
2196 a->data.memory_coherent != b->data.memory_coherent ||
2197 a->data.memory_volatile != b->data.memory_volatile ||
2198 a->data.memory_restrict != b->data.memory_restrict) {
2199
2200 /* parameter a's qualifiers don't match */
2201 return a->name;
2202 }
2203 }
2204 return NULL;
2205 }
2206
2207
2208 void
replace_parameters(exec_list * new_params)2209 ir_function_signature::replace_parameters(exec_list *new_params)
2210 {
2211 /* Destroy all of the previous parameter information. If the previous
2212 * parameter information comes from the function prototype, it may either
2213 * specify incorrect parameter names or not have names at all.
2214 */
2215 new_params->move_nodes_to(¶meters);
2216 }
2217
2218
ir_function(const char * name)2219 ir_function::ir_function(const char *name)
2220 : ir_instruction(ir_type_function)
2221 {
2222 this->subroutine_index = -1;
2223 this->name = ralloc_strdup(this, name);
2224 }
2225
2226
2227 bool
has_user_signature()2228 ir_function::has_user_signature()
2229 {
2230 foreach_in_list(ir_function_signature, sig, &this->signatures) {
2231 if (!sig->is_builtin())
2232 return true;
2233 }
2234 return false;
2235 }
2236
2237
2238 ir_rvalue *
error_value(void * mem_ctx)2239 ir_rvalue::error_value(void *mem_ctx)
2240 {
2241 ir_rvalue *v = new(mem_ctx) ir_rvalue(ir_type_unset);
2242
2243 v->type = glsl_type::error_type;
2244 return v;
2245 }
2246
2247
2248 void
visit_exec_list(exec_list * list,ir_visitor * visitor)2249 visit_exec_list(exec_list *list, ir_visitor *visitor)
2250 {
2251 foreach_in_list_safe(ir_instruction, node, list) {
2252 node->accept(visitor);
2253 }
2254 }
2255
2256
2257 static void
steal_memory(ir_instruction * ir,void * new_ctx)2258 steal_memory(ir_instruction *ir, void *new_ctx)
2259 {
2260 ir_variable *var = ir->as_variable();
2261 ir_function *fn = ir->as_function();
2262 ir_constant *constant = ir->as_constant();
2263 if (var != NULL && var->constant_value != NULL)
2264 steal_memory(var->constant_value, ir);
2265
2266 if (var != NULL && var->constant_initializer != NULL)
2267 steal_memory(var->constant_initializer, ir);
2268
2269 if (fn != NULL && fn->subroutine_types)
2270 ralloc_steal(new_ctx, fn->subroutine_types);
2271
2272 /* The components of aggregate constants are not visited by the normal
2273 * visitor, so steal their values by hand.
2274 */
2275 if (constant != NULL &&
2276 (constant->type->is_array() || constant->type->is_struct())) {
2277 for (unsigned int i = 0; i < constant->type->length; i++) {
2278 steal_memory(constant->const_elements[i], ir);
2279 }
2280 }
2281
2282 ralloc_steal(new_ctx, ir);
2283 }
2284
2285
2286 void
reparent_ir(exec_list * list,void * mem_ctx)2287 reparent_ir(exec_list *list, void *mem_ctx)
2288 {
2289 foreach_in_list(ir_instruction, node, list) {
2290 visit_tree(node, steal_memory, mem_ctx);
2291 }
2292 }
2293
2294
2295 static ir_rvalue *
try_min_one(ir_rvalue * ir)2296 try_min_one(ir_rvalue *ir)
2297 {
2298 ir_expression *expr = ir->as_expression();
2299
2300 if (!expr || expr->operation != ir_binop_min)
2301 return NULL;
2302
2303 if (expr->operands[0]->is_one())
2304 return expr->operands[1];
2305
2306 if (expr->operands[1]->is_one())
2307 return expr->operands[0];
2308
2309 return NULL;
2310 }
2311
2312 static ir_rvalue *
try_max_zero(ir_rvalue * ir)2313 try_max_zero(ir_rvalue *ir)
2314 {
2315 ir_expression *expr = ir->as_expression();
2316
2317 if (!expr || expr->operation != ir_binop_max)
2318 return NULL;
2319
2320 if (expr->operands[0]->is_zero())
2321 return expr->operands[1];
2322
2323 if (expr->operands[1]->is_zero())
2324 return expr->operands[0];
2325
2326 return NULL;
2327 }
2328
2329 ir_rvalue *
as_rvalue_to_saturate()2330 ir_rvalue::as_rvalue_to_saturate()
2331 {
2332 ir_expression *expr = this->as_expression();
2333
2334 if (!expr)
2335 return NULL;
2336
2337 ir_rvalue *max_zero = try_max_zero(expr);
2338 if (max_zero) {
2339 return try_min_one(max_zero);
2340 } else {
2341 ir_rvalue *min_one = try_min_one(expr);
2342 if (min_one) {
2343 return try_max_zero(min_one);
2344 }
2345 }
2346
2347 return NULL;
2348 }
2349
2350
2351 unsigned
vertices_per_prim(GLenum prim)2352 vertices_per_prim(GLenum prim)
2353 {
2354 switch (prim) {
2355 case GL_POINTS:
2356 return 1;
2357 case GL_LINES:
2358 return 2;
2359 case GL_TRIANGLES:
2360 return 3;
2361 case GL_LINES_ADJACENCY:
2362 return 4;
2363 case GL_TRIANGLES_ADJACENCY:
2364 return 6;
2365 default:
2366 assert(!"Bad primitive");
2367 return 3;
2368 }
2369 }
2370
2371 /**
2372 * Generate a string describing the mode of a variable
2373 */
2374 const char *
mode_string(const ir_variable * var)2375 mode_string(const ir_variable *var)
2376 {
2377 switch (var->data.mode) {
2378 case ir_var_auto:
2379 return (var->data.read_only) ? "global constant" : "global variable";
2380
2381 case ir_var_uniform:
2382 return "uniform";
2383
2384 case ir_var_shader_storage:
2385 return "buffer";
2386
2387 case ir_var_shader_in:
2388 return "shader input";
2389
2390 case ir_var_shader_out:
2391 return "shader output";
2392
2393 case ir_var_function_in:
2394 case ir_var_const_in:
2395 return "function input";
2396
2397 case ir_var_function_out:
2398 return "function output";
2399
2400 case ir_var_function_inout:
2401 return "function inout";
2402
2403 case ir_var_system_value:
2404 return "shader input";
2405
2406 case ir_var_temporary:
2407 return "compiler temporary";
2408
2409 case ir_var_mode_count:
2410 break;
2411 }
2412
2413 assert(!"Should not get here.");
2414 return "invalid variable";
2415 }
2416