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 "main/core.h" /* for MAX2 */
25 #include "ir.h"
26 #include "glsl_types.h"
27
higher_precision(ir_instruction * a,ir_instruction * b)28 glsl_precision higher_precision (ir_instruction* a, ir_instruction* b)
29 {
30 if (!a && !b)
31 return glsl_precision_undefined;
32 if (!a)
33 return precision_from_ir (b);
34 if (!b)
35 return precision_from_ir (a);
36 return higher_precision (precision_from_ir(a), precision_from_ir(b));
37 }
38
39
ir_rvalue(enum ir_node_type t,glsl_precision precision)40 ir_rvalue::ir_rvalue(enum ir_node_type t, glsl_precision precision)
41 : ir_instruction(t)
42 , precision(precision)
43 {
44 this->type = glsl_type::error_type;
45 }
46
is_zero() const47 bool ir_rvalue::is_zero() const
48 {
49 return false;
50 }
51
is_one() const52 bool ir_rvalue::is_one() const
53 {
54 return false;
55 }
56
is_negative_one() const57 bool ir_rvalue::is_negative_one() const
58 {
59 return false;
60 }
61
is_basis() const62 bool ir_rvalue::is_basis() const
63 {
64 return false;
65 }
66
67 /**
68 * Modify the swizzle make to move one component to another
69 *
70 * \param m IR swizzle to be modified
71 * \param from Component in the RHS that is to be swizzled
72 * \param to Desired swizzle location of \c from
73 */
74 static void
update_rhs_swizzle(ir_swizzle_mask & m,unsigned from,unsigned to)75 update_rhs_swizzle(ir_swizzle_mask &m, unsigned from, unsigned to)
76 {
77 switch (to) {
78 case 0: m.x = from; break;
79 case 1: m.y = from; break;
80 case 2: m.z = from; break;
81 case 3: m.w = from; break;
82 default: assert(!"Should not get here.");
83 }
84
85 m.num_components = MAX2(m.num_components, (to + 1));
86 }
87
88 void
set_lhs(ir_rvalue * lhs)89 ir_assignment::set_lhs(ir_rvalue *lhs)
90 {
91 void *mem_ctx = this;
92 bool swizzled = false;
93
94 while (lhs != NULL) {
95 ir_swizzle *swiz = lhs->as_swizzle();
96
97 if (swiz == NULL)
98 break;
99
100 unsigned write_mask = 0;
101 ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
102
103 for (unsigned i = 0; i < swiz->mask.num_components; i++) {
104 unsigned c = 0;
105
106 switch (i) {
107 case 0: c = swiz->mask.x; break;
108 case 1: c = swiz->mask.y; break;
109 case 2: c = swiz->mask.z; break;
110 case 3: c = swiz->mask.w; break;
111 default: assert(!"Should not get here.");
112 }
113
114 write_mask |= (((this->write_mask >> i) & 1) << c);
115 update_rhs_swizzle(rhs_swiz, i, c);
116 }
117
118 this->write_mask = write_mask;
119 lhs = swiz->val;
120
121 this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
122 swizzled = true;
123 }
124
125 if (swizzled) {
126 /* Now, RHS channels line up with the LHS writemask. Collapse it
127 * to just the channels that will be written.
128 */
129 ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
130 int rhs_chan = 0;
131 for (int i = 0; i < 4; i++) {
132 if (write_mask & (1 << i))
133 update_rhs_swizzle(rhs_swiz, i, rhs_chan++);
134 }
135 this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
136 }
137
138 assert((lhs == NULL) || lhs->as_dereference());
139
140 this->lhs = (ir_dereference *) lhs;
141 }
142
143 ir_variable *
whole_variable_written()144 ir_assignment::whole_variable_written()
145 {
146 ir_variable *v = this->lhs->whole_variable_referenced();
147
148 if (v == NULL)
149 return NULL;
150
151 if (v->type->is_scalar())
152 return v;
153
154 if (v->type->is_vector()) {
155 const unsigned mask = (1U << v->type->vector_elements) - 1;
156
157 if (mask != this->write_mask)
158 return NULL;
159 }
160
161 /* Either all the vector components are assigned or the variable is some
162 * composite type (and the whole thing is assigned.
163 */
164 return v;
165 }
166
ir_assignment(ir_dereference * lhs,ir_rvalue * rhs,ir_rvalue * condition,unsigned write_mask)167 ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
168 ir_rvalue *condition, unsigned write_mask)
169 : ir_instruction(ir_type_assignment)
170 {
171 this->condition = condition;
172 this->rhs = rhs;
173 this->lhs = lhs;
174 this->write_mask = write_mask;
175
176 if (lhs->type->is_scalar() || lhs->type->is_vector()) {
177 int lhs_components = 0;
178 for (int i = 0; i < 4; i++) {
179 if (write_mask & (1 << i))
180 lhs_components++;
181 }
182
183 assert(lhs_components == this->rhs->type->vector_elements);
184 }
185 }
186
ir_assignment(ir_rvalue * lhs,ir_rvalue * rhs,ir_rvalue * condition)187 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
188 ir_rvalue *condition)
189 : ir_instruction(ir_type_assignment)
190 {
191 this->condition = condition;
192 this->rhs = rhs;
193
194 /* If the RHS is a vector type, assume that all components of the vector
195 * type are being written to the LHS. The write mask comes from the RHS
196 * because we can have a case where the LHS is a vec4 and the RHS is a
197 * vec3. In that case, the assignment is:
198 *
199 * (assign (...) (xyz) (var_ref lhs) (var_ref rhs))
200 */
201 if (rhs->type->is_vector())
202 this->write_mask = (1U << rhs->type->vector_elements) - 1;
203 else if (rhs->type->is_scalar())
204 this->write_mask = 1;
205 else
206 this->write_mask = 0;
207
208 this->set_lhs(lhs);
209 }
210
ir_expression(int op,const struct glsl_type * type,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2,ir_rvalue * op3)211 ir_expression::ir_expression(int op, const struct glsl_type *type,
212 ir_rvalue *op0, ir_rvalue *op1,
213 ir_rvalue *op2, ir_rvalue *op3)
214 : ir_rvalue(ir_type_expression, higher_precision(higher_precision(op0,op1), higher_precision(op2,op3)))
215 {
216 this->type = type;
217 this->operation = ir_expression_operation(op);
218 this->operands[0] = op0;
219 this->operands[1] = op1;
220 this->operands[2] = op2;
221 this->operands[3] = op3;
222 #ifndef NDEBUG
223 int num_operands = get_num_operands(this->operation);
224 for (int i = num_operands; i < 4; i++) {
225 assert(this->operands[i] == NULL);
226 }
227 #endif
228 }
229
ir_expression(int op,ir_rvalue * op0)230 ir_expression::ir_expression(int op, ir_rvalue *op0)
231 : ir_rvalue(ir_type_expression, precision_from_ir(op0))
232 {
233 this->operation = ir_expression_operation(op);
234 this->operands[0] = op0;
235 this->operands[1] = NULL;
236 this->operands[2] = NULL;
237 this->operands[3] = NULL;
238
239 assert(op <= ir_last_unop);
240
241 switch (this->operation) {
242 case ir_unop_bit_not:
243 case ir_unop_logic_not:
244 case ir_unop_neg:
245 case ir_unop_abs:
246 case ir_unop_sign:
247 case ir_unop_rcp:
248 case ir_unop_rsq:
249 case ir_unop_sqrt:
250 case ir_unop_normalize:
251 case ir_unop_exp:
252 case ir_unop_log:
253 case ir_unop_exp2:
254 case ir_unop_log2:
255 case ir_unop_trunc:
256 case ir_unop_ceil:
257 case ir_unop_floor:
258 case ir_unop_fract:
259 case ir_unop_round_even:
260 case ir_unop_sin:
261 case ir_unop_cos:
262 case ir_unop_sin_reduced:
263 case ir_unop_cos_reduced:
264 case ir_unop_dFdx:
265 case ir_unop_dFdx_coarse:
266 case ir_unop_dFdx_fine:
267 case ir_unop_dFdy:
268 case ir_unop_dFdy_coarse:
269 case ir_unop_dFdy_fine:
270 case ir_unop_bitfield_reverse:
271 case ir_unop_interpolate_at_centroid:
272 case ir_unop_saturate:
273 this->type = op0->type;
274 break;
275
276 case ir_unop_f2i:
277 case ir_unop_b2i:
278 case ir_unop_u2i:
279 case ir_unop_bitcast_f2i:
280 case ir_unop_bit_count:
281 case ir_unop_find_msb:
282 case ir_unop_find_lsb:
283 this->type = glsl_type::get_instance(GLSL_TYPE_INT,
284 op0->type->vector_elements, 1);
285 break;
286
287 case ir_unop_b2f:
288 case ir_unop_i2f:
289 case ir_unop_u2f:
290 case ir_unop_bitcast_i2f:
291 case ir_unop_bitcast_u2f:
292 this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
293 op0->type->vector_elements, 1);
294 break;
295
296 case ir_unop_f2b:
297 case ir_unop_i2b:
298 this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
299 op0->type->vector_elements, 1);
300 break;
301
302 case ir_unop_i2u:
303 case ir_unop_f2u:
304 case ir_unop_bitcast_f2u:
305 this->type = glsl_type::get_instance(GLSL_TYPE_UINT,
306 op0->type->vector_elements, 1);
307 break;
308
309 case ir_unop_noise:
310 case ir_unop_unpack_half_2x16_split_x:
311 case ir_unop_unpack_half_2x16_split_y:
312 this->type = glsl_type::float_type;
313 break;
314
315 case ir_unop_any:
316 this->type = glsl_type::bool_type;
317 break;
318
319 case ir_unop_pack_snorm_2x16:
320 case ir_unop_pack_snorm_4x8:
321 case ir_unop_pack_unorm_2x16:
322 case ir_unop_pack_unorm_4x8:
323 case ir_unop_pack_half_2x16:
324 this->type = glsl_type::uint_type;
325 break;
326
327 case ir_unop_unpack_snorm_2x16:
328 case ir_unop_unpack_unorm_2x16:
329 case ir_unop_unpack_half_2x16:
330 this->type = glsl_type::vec2_type;
331 break;
332
333 case ir_unop_unpack_snorm_4x8:
334 case ir_unop_unpack_unorm_4x8:
335 this->type = glsl_type::vec4_type;
336 break;
337
338 default:
339 assert(!"not reached: missing automatic type setup for ir_expression");
340 this->type = op0->type;
341 break;
342 }
343 }
344
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1)345 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
346 : ir_rvalue(ir_type_expression, higher_precision(op0,op1))
347 {
348 this->operation = ir_expression_operation(op);
349 this->operands[0] = op0;
350 this->operands[1] = op1;
351 this->operands[2] = NULL;
352 this->operands[3] = NULL;
353
354 assert(op > ir_last_unop);
355
356 switch (this->operation) {
357 case ir_binop_all_equal:
358 case ir_binop_any_nequal:
359 this->type = glsl_type::bool_type;
360 break;
361
362 case ir_binop_add:
363 case ir_binop_sub:
364 case ir_binop_min:
365 case ir_binop_max:
366 case ir_binop_pow:
367 case ir_binop_mul:
368 case ir_binop_div:
369 case ir_binop_mod:
370 if (op0->type->is_scalar()) {
371 this->type = op1->type;
372 } else if (op1->type->is_scalar()) {
373 this->type = op0->type;
374 } else {
375 /* FINISHME: matrix types */
376 assert(!op0->type->is_matrix() && !op1->type->is_matrix());
377 assert(op0->type == op1->type);
378 this->type = op0->type;
379 }
380 break;
381
382 case ir_binop_logic_and:
383 case ir_binop_logic_xor:
384 case ir_binop_logic_or:
385 case ir_binop_bit_and:
386 case ir_binop_bit_xor:
387 case ir_binop_bit_or:
388 assert(!op0->type->is_matrix());
389 assert(!op1->type->is_matrix());
390 if (op0->type->is_scalar()) {
391 this->type = op1->type;
392 } else if (op1->type->is_scalar()) {
393 this->type = op0->type;
394 } else {
395 assert(op0->type->vector_elements == op1->type->vector_elements);
396 this->type = op0->type;
397 }
398 break;
399
400 case ir_binop_equal:
401 case ir_binop_nequal:
402 case ir_binop_lequal:
403 case ir_binop_gequal:
404 case ir_binop_less:
405 case ir_binop_greater:
406 assert(op0->type == op1->type);
407 this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
408 op0->type->vector_elements, 1);
409 break;
410
411 case ir_binop_dot:
412 this->type = glsl_type::float_type;
413 break;
414
415 case ir_binop_pack_half_2x16_split:
416 this->type = glsl_type::uint_type;
417 break;
418
419 case ir_binop_imul_high:
420 case ir_binop_carry:
421 case ir_binop_borrow:
422 case ir_binop_lshift:
423 case ir_binop_rshift:
424 case ir_binop_bfm:
425 case ir_binop_ldexp:
426 case ir_binop_interpolate_at_offset:
427 case ir_binop_interpolate_at_sample:
428 this->type = op0->type;
429 this->set_precision(op0->get_precision());
430 break;
431
432 case ir_binop_vector_extract:
433 this->type = op0->type->get_scalar_type();
434 this->set_precision(op0->get_precision());
435 break;
436
437 default:
438 assert(!"not reached: missing automatic type setup for ir_expression");
439 this->type = glsl_type::float_type;
440 }
441 }
442
ir_expression(int op,ir_rvalue * op0,ir_rvalue * op1,ir_rvalue * op2)443 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1,
444 ir_rvalue *op2)
445 : ir_rvalue(ir_type_expression, higher_precision(precision_from_ir(op0),higher_precision(op1,op2)))
446 {
447 this->operation = ir_expression_operation(op);
448 this->operands[0] = op0;
449 this->operands[1] = op1;
450 this->operands[2] = op2;
451 this->operands[3] = NULL;
452
453 assert(op > ir_last_binop && op <= ir_last_triop);
454
455 switch (this->operation) {
456 case ir_triop_fma:
457 case ir_triop_clamp:
458 case ir_triop_lrp:
459 case ir_triop_bitfield_extract:
460 case ir_triop_vector_insert:
461 this->type = op0->type;
462 break;
463
464 case ir_triop_bfi:
465 case ir_triop_csel:
466 this->type = op1->type;
467 break;
468
469 default:
470 assert(!"not reached: missing automatic type setup for ir_expression");
471 this->type = glsl_type::float_type;
472 }
473 }
474
475 unsigned int
get_num_operands(ir_expression_operation op)476 ir_expression::get_num_operands(ir_expression_operation op)
477 {
478 assert(op <= ir_last_opcode);
479
480 if (op <= ir_last_unop)
481 return 1;
482
483 if (op <= ir_last_binop)
484 return 2;
485
486 if (op <= ir_last_triop)
487 return 3;
488
489 if (op <= ir_last_quadop)
490 return 4;
491
492 assert(false);
493 return 0;
494 }
495
496 static const char *const operator_strs[] = {
497 "~",
498 "!",
499 "neg",
500 "abs",
501 "sign",
502 "rcp",
503 "rsq",
504 "sqrt",
505 "normalize",
506 "exp",
507 "log",
508 "exp2",
509 "log2",
510 "f2i",
511 "f2u",
512 "i2f",
513 "f2b",
514 "b2f",
515 "i2b",
516 "b2i",
517 "u2f",
518 "i2u",
519 "u2i",
520 "bitcast_i2f",
521 "bitcast_f2i",
522 "bitcast_u2f",
523 "bitcast_f2u",
524 "any",
525 "trunc",
526 "ceil",
527 "floor",
528 "fract",
529 "round_even",
530 "sin",
531 "cos",
532 "sin_reduced",
533 "cos_reduced",
534 "dFdx",
535 "dFdxCoarse",
536 "dFdxFine",
537 "dFdy",
538 "dFdyCoarse",
539 "dFdyFine",
540 "packSnorm2x16",
541 "packSnorm4x8",
542 "packUnorm2x16",
543 "packUnorm4x8",
544 "packHalf2x16",
545 "unpackSnorm2x16",
546 "unpackSnorm4x8",
547 "unpackUnorm2x16",
548 "unpackUnorm4x8",
549 "unpackHalf2x16",
550 "unpackHalf2x16_split_x",
551 "unpackHalf2x16_split_y",
552 "bitfield_reverse",
553 "bit_count",
554 "find_msb",
555 "find_lsb",
556 "sat",
557 "noise",
558 "interpolate_at_centroid",
559 "+",
560 "-",
561 "*",
562 "imul_high",
563 "/",
564 "carry",
565 "borrow",
566 "%",
567 "<",
568 ">",
569 "<=",
570 ">=",
571 "==",
572 "!=",
573 "all_equal",
574 "any_nequal",
575 "<<",
576 ">>",
577 "&",
578 "^",
579 "|",
580 "&&",
581 "^^",
582 "||",
583 "dot",
584 "min",
585 "max",
586 "pow",
587 "packHalf2x16_split",
588 "bfm",
589 "ubo_load",
590 "ldexp",
591 "vector_extract",
592 "interpolate_at_offset",
593 "interpolate_at_sample",
594 "fma",
595 "clamp",
596 "lrp",
597 "csel",
598 "bfi",
599 "bitfield_extract",
600 "vector_insert",
601 "bitfield_insert",
602 "vector",
603 };
604
operator_string(ir_expression_operation op)605 const char *ir_expression::operator_string(ir_expression_operation op)
606 {
607 assert((unsigned int) op < Elements(operator_strs));
608 assert(Elements(operator_strs) == (ir_quadop_vector + 1));
609 return operator_strs[op];
610 }
611
operator_string()612 const char *ir_expression::operator_string()
613 {
614 return operator_string(this->operation);
615 }
616
617 const char*
depth_layout_string(ir_depth_layout layout)618 depth_layout_string(ir_depth_layout layout)
619 {
620 switch(layout) {
621 case ir_depth_layout_none: return "";
622 case ir_depth_layout_any: return "depth_any";
623 case ir_depth_layout_greater: return "depth_greater";
624 case ir_depth_layout_less: return "depth_less";
625 case ir_depth_layout_unchanged: return "depth_unchanged";
626
627 default:
628 assert(0);
629 return "";
630 }
631 }
632
633 ir_expression_operation
get_operator(const char * str)634 ir_expression::get_operator(const char *str)
635 {
636 const int operator_count = sizeof(operator_strs) / sizeof(operator_strs[0]);
637 for (int op = 0; op < operator_count; op++) {
638 if (strcmp(str, operator_strs[op]) == 0)
639 return (ir_expression_operation) op;
640 }
641 return (ir_expression_operation) -1;
642 }
643
ir_constant()644 ir_constant::ir_constant()
645 : ir_rvalue(ir_type_constant, glsl_precision_undefined)
646 {
647 }
648
ir_constant(const struct glsl_type * type,const ir_constant_data * data,glsl_precision precision)649 ir_constant::ir_constant(const struct glsl_type *type,
650 const ir_constant_data *data, glsl_precision precision)
651 : ir_rvalue(ir_type_constant, precision)
652 {
653 assert((type->base_type >= GLSL_TYPE_UINT)
654 && (type->base_type <= GLSL_TYPE_BOOL));
655
656 this->type = type;
657 memcpy(& this->value, data, sizeof(this->value));
658 }
659
ir_constant(float f,unsigned vector_elements)660 ir_constant::ir_constant(float f, unsigned vector_elements)
661 : ir_rvalue(ir_type_constant, glsl_precision_undefined)
662 {
663 assert(vector_elements <= 4);
664 this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT, vector_elements, 1);
665 for (unsigned i = 0; i < vector_elements; i++) {
666 this->value.f[i] = f;
667 }
668 for (unsigned i = vector_elements; i < 16; i++) {
669 this->value.f[i] = 0;
670 }
671 }
672
ir_constant(unsigned int u,unsigned vector_elements)673 ir_constant::ir_constant(unsigned int u, unsigned vector_elements)
674 : ir_rvalue(ir_type_constant, glsl_precision_undefined)
675 {
676 assert(vector_elements <= 4);
677 this->type = glsl_type::get_instance(GLSL_TYPE_UINT, vector_elements, 1);
678 for (unsigned i = 0; i < vector_elements; i++) {
679 this->value.u[i] = u;
680 }
681 for (unsigned i = vector_elements; i < 16; i++) {
682 this->value.u[i] = 0;
683 }
684 }
685
ir_constant(int integer,unsigned vector_elements)686 ir_constant::ir_constant(int integer, unsigned vector_elements)
687 : ir_rvalue(ir_type_constant, glsl_precision_undefined)
688 {
689 assert(vector_elements <= 4);
690 this->type = glsl_type::get_instance(GLSL_TYPE_INT, vector_elements, 1);
691 for (unsigned i = 0; i < vector_elements; i++) {
692 this->value.i[i] = integer;
693 }
694 for (unsigned i = vector_elements; i < 16; i++) {
695 this->value.i[i] = 0;
696 }
697 }
698
ir_constant(bool b,unsigned vector_elements)699 ir_constant::ir_constant(bool b, unsigned vector_elements)
700 : ir_rvalue(ir_type_constant, glsl_precision_undefined)
701 {
702 assert(vector_elements <= 4);
703 this->type = glsl_type::get_instance(GLSL_TYPE_BOOL, vector_elements, 1);
704 for (unsigned i = 0; i < vector_elements; i++) {
705 this->value.b[i] = b;
706 }
707 for (unsigned i = vector_elements; i < 16; i++) {
708 this->value.b[i] = false;
709 }
710 }
711
ir_constant(const ir_constant * c,unsigned i)712 ir_constant::ir_constant(const ir_constant *c, unsigned i)
713 : ir_rvalue(ir_type_constant, c->precision)
714 {
715 this->type = c->type->get_base_type();
716
717 switch (this->type->base_type) {
718 case GLSL_TYPE_UINT: this->value.u[0] = c->value.u[i]; break;
719 case GLSL_TYPE_INT: this->value.i[0] = c->value.i[i]; break;
720 case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
721 case GLSL_TYPE_BOOL: this->value.b[0] = c->value.b[i]; break;
722 default: assert(!"Should not get here."); break;
723 }
724 }
725
ir_constant(const struct glsl_type * type,exec_list * value_list)726 ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
727 : ir_rvalue(ir_type_constant, glsl_precision_undefined)
728 {
729 this->type = type;
730
731 assert(type->is_scalar() || type->is_vector() || type->is_matrix()
732 || type->is_record() || type->is_array());
733
734 if (type->is_array()) {
735 this->array_elements = ralloc_array(this, ir_constant *, type->length);
736 unsigned i = 0;
737 foreach_in_list(ir_constant, value, value_list) {
738 assert(value->as_constant() != NULL);
739
740 this->array_elements[i++] = value;
741 }
742 return;
743 }
744
745 /* If the constant is a record, the types of each of the entries in
746 * value_list must be a 1-for-1 match with the structure components. Each
747 * entry must also be a constant. Just move the nodes from the value_list
748 * to the list in the ir_constant.
749 */
750 /* FINISHME: Should there be some type checking and / or assertions here? */
751 /* FINISHME: Should the new constant take ownership of the nodes from
752 * FINISHME: value_list, or should it make copies?
753 */
754 if (type->is_record()) {
755 value_list->move_nodes_to(& this->components);
756 return;
757 }
758
759 for (unsigned i = 0; i < 16; i++) {
760 this->value.u[i] = 0;
761 }
762
763 ir_constant *value = (ir_constant *) (value_list->head);
764
765 /* Constructors with exactly one scalar argument are special for vectors
766 * and matrices. For vectors, the scalar value is replicated to fill all
767 * the components. For matrices, the scalar fills the components of the
768 * diagonal while the rest is filled with 0.
769 */
770 if (value->type->is_scalar() && value->next->is_tail_sentinel()) {
771 if (type->is_matrix()) {
772 /* Matrix - fill diagonal (rest is already set to 0) */
773 assert(type->base_type == GLSL_TYPE_FLOAT);
774 for (unsigned i = 0; i < type->matrix_columns; i++)
775 this->value.f[i * type->vector_elements + i] = value->value.f[0];
776 } else {
777 /* Vector or scalar - fill all components */
778 switch (type->base_type) {
779 case GLSL_TYPE_UINT:
780 case GLSL_TYPE_INT:
781 for (unsigned i = 0; i < type->components(); i++)
782 this->value.u[i] = value->value.u[0];
783 break;
784 case GLSL_TYPE_FLOAT:
785 for (unsigned i = 0; i < type->components(); i++)
786 this->value.f[i] = value->value.f[0];
787 break;
788 case GLSL_TYPE_BOOL:
789 for (unsigned i = 0; i < type->components(); i++)
790 this->value.b[i] = value->value.b[0];
791 break;
792 default:
793 assert(!"Should not get here.");
794 break;
795 }
796 }
797 return;
798 }
799
800 if (type->is_matrix() && value->type->is_matrix()) {
801 assert(value->next->is_tail_sentinel());
802
803 /* From section 5.4.2 of the GLSL 1.20 spec:
804 * "If a matrix is constructed from a matrix, then each component
805 * (column i, row j) in the result that has a corresponding component
806 * (column i, row j) in the argument will be initialized from there."
807 */
808 unsigned cols = MIN2(type->matrix_columns, value->type->matrix_columns);
809 unsigned rows = MIN2(type->vector_elements, value->type->vector_elements);
810 for (unsigned i = 0; i < cols; i++) {
811 for (unsigned j = 0; j < rows; j++) {
812 const unsigned src = i * value->type->vector_elements + j;
813 const unsigned dst = i * type->vector_elements + j;
814 this->value.f[dst] = value->value.f[src];
815 }
816 }
817
818 /* "All other components will be initialized to the identity matrix." */
819 for (unsigned i = cols; i < type->matrix_columns; i++)
820 this->value.f[i * type->vector_elements + i] = 1.0;
821
822 return;
823 }
824
825 /* Use each component from each entry in the value_list to initialize one
826 * component of the constant being constructed.
827 */
828 for (unsigned i = 0; i < type->components(); /* empty */) {
829 assert(value->as_constant() != NULL);
830 assert(!value->is_tail_sentinel());
831
832 for (unsigned j = 0; j < value->type->components(); j++) {
833 switch (type->base_type) {
834 case GLSL_TYPE_UINT:
835 this->value.u[i] = value->get_uint_component(j);
836 break;
837 case GLSL_TYPE_INT:
838 this->value.i[i] = value->get_int_component(j);
839 break;
840 case GLSL_TYPE_FLOAT:
841 this->value.f[i] = value->get_float_component(j);
842 break;
843 case GLSL_TYPE_BOOL:
844 this->value.b[i] = value->get_bool_component(j);
845 break;
846 default:
847 /* FINISHME: What to do? Exceptions are not the answer.
848 */
849 break;
850 }
851
852 i++;
853 if (i >= type->components())
854 break;
855 }
856
857 value = (ir_constant *) value->next;
858 }
859 }
860
861 ir_constant *
zero(void * mem_ctx,const glsl_type * type)862 ir_constant::zero(void *mem_ctx, const glsl_type *type)
863 {
864 assert(type->is_scalar() || type->is_vector() || type->is_matrix()
865 || type->is_record() || type->is_array());
866
867 ir_constant *c = new(mem_ctx) ir_constant;
868 c->type = type;
869 memset(&c->value, 0, sizeof(c->value));
870
871 if (type->is_array()) {
872 c->array_elements = ralloc_array(c, ir_constant *, type->length);
873
874 for (unsigned i = 0; i < type->length; i++)
875 c->array_elements[i] = ir_constant::zero(c, type->element_type());
876 }
877
878 if (type->is_record()) {
879 for (unsigned i = 0; i < type->length; i++) {
880 ir_constant *comp = ir_constant::zero(mem_ctx, type->fields.structure[i].type);
881 c->components.push_tail(comp);
882 }
883 }
884
885 return c;
886 }
887
888 bool
get_bool_component(unsigned i) const889 ir_constant::get_bool_component(unsigned i) const
890 {
891 switch (this->type->base_type) {
892 case GLSL_TYPE_UINT: return this->value.u[i] != 0;
893 case GLSL_TYPE_INT: return this->value.i[i] != 0;
894 case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
895 case GLSL_TYPE_BOOL: return this->value.b[i];
896 default: assert(!"Should not get here."); break;
897 }
898
899 /* Must return something to make the compiler happy. This is clearly an
900 * error case.
901 */
902 return false;
903 }
904
905 float
get_float_component(unsigned i) const906 ir_constant::get_float_component(unsigned i) const
907 {
908 switch (this->type->base_type) {
909 case GLSL_TYPE_UINT: return (float) this->value.u[i];
910 case GLSL_TYPE_INT: return (float) this->value.i[i];
911 case GLSL_TYPE_FLOAT: return this->value.f[i];
912 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1.0f : 0.0f;
913 default: assert(!"Should not get here."); break;
914 }
915
916 /* Must return something to make the compiler happy. This is clearly an
917 * error case.
918 */
919 return 0.0;
920 }
921
922 int
get_int_component(unsigned i) const923 ir_constant::get_int_component(unsigned i) const
924 {
925 switch (this->type->base_type) {
926 case GLSL_TYPE_UINT: return this->value.u[i];
927 case GLSL_TYPE_INT: return this->value.i[i];
928 case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
929 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
930 default: assert(!"Should not get here."); break;
931 }
932
933 /* Must return something to make the compiler happy. This is clearly an
934 * error case.
935 */
936 return 0;
937 }
938
939 unsigned
get_uint_component(unsigned i) const940 ir_constant::get_uint_component(unsigned i) const
941 {
942 switch (this->type->base_type) {
943 case GLSL_TYPE_UINT: return this->value.u[i];
944 case GLSL_TYPE_INT: return this->value.i[i];
945 case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
946 case GLSL_TYPE_BOOL: return this->value.b[i] ? 1 : 0;
947 default: assert(!"Should not get here."); break;
948 }
949
950 /* Must return something to make the compiler happy. This is clearly an
951 * error case.
952 */
953 return 0;
954 }
955
956 ir_constant *
get_array_element(unsigned i) const957 ir_constant::get_array_element(unsigned i) const
958 {
959 assert(this->type->is_array());
960
961 /* From page 35 (page 41 of the PDF) of the GLSL 1.20 spec:
962 *
963 * "Behavior is undefined if a shader subscripts an array with an index
964 * less than 0 or greater than or equal to the size the array was
965 * declared with."
966 *
967 * Most out-of-bounds accesses are removed before things could get this far.
968 * There are cases where non-constant array index values can get constant
969 * folded.
970 */
971 if (int(i) < 0)
972 i = 0;
973 else if (i >= this->type->length)
974 i = this->type->length - 1;
975
976 return array_elements[i];
977 }
978
979 ir_constant *
get_record_field(const char * name)980 ir_constant::get_record_field(const char *name)
981 {
982 int idx = this->type->field_index(name);
983
984 if (idx < 0)
985 return NULL;
986
987 if (this->components.is_empty())
988 return NULL;
989
990 exec_node *node = this->components.head;
991 for (int i = 0; i < idx; i++) {
992 node = node->next;
993
994 /* If the end of the list is encountered before the element matching the
995 * requested field is found, return NULL.
996 */
997 if (node->is_tail_sentinel())
998 return NULL;
999 }
1000
1001 return (ir_constant *) node;
1002 }
1003
1004 void
copy_offset(ir_constant * src,int offset)1005 ir_constant::copy_offset(ir_constant *src, int offset)
1006 {
1007 switch (this->type->base_type) {
1008 case GLSL_TYPE_UINT:
1009 case GLSL_TYPE_INT:
1010 case GLSL_TYPE_FLOAT:
1011 case GLSL_TYPE_BOOL: {
1012 unsigned int size = src->type->components();
1013 assert (size <= this->type->components() - offset);
1014 for (unsigned int i=0; i<size; i++) {
1015 switch (this->type->base_type) {
1016 case GLSL_TYPE_UINT:
1017 value.u[i+offset] = src->get_uint_component(i);
1018 break;
1019 case GLSL_TYPE_INT:
1020 value.i[i+offset] = src->get_int_component(i);
1021 break;
1022 case GLSL_TYPE_FLOAT:
1023 value.f[i+offset] = src->get_float_component(i);
1024 break;
1025 case GLSL_TYPE_BOOL:
1026 value.b[i+offset] = src->get_bool_component(i);
1027 break;
1028 default: // Shut up the compiler
1029 break;
1030 }
1031 }
1032 break;
1033 }
1034
1035 case GLSL_TYPE_STRUCT: {
1036 assert (src->type == this->type);
1037 this->components.make_empty();
1038 foreach_in_list(ir_constant, orig, &src->components) {
1039 this->components.push_tail(orig->clone(this, NULL));
1040 }
1041 break;
1042 }
1043
1044 case GLSL_TYPE_ARRAY: {
1045 assert (src->type == this->type);
1046 for (unsigned i = 0; i < this->type->length; i++) {
1047 this->array_elements[i] = src->array_elements[i]->clone(this, NULL);
1048 }
1049 break;
1050 }
1051
1052 default:
1053 assert(!"Should not get here.");
1054 break;
1055 }
1056 }
1057
1058 void
copy_masked_offset(ir_constant * src,int offset,unsigned int mask)1059 ir_constant::copy_masked_offset(ir_constant *src, int offset, unsigned int mask)
1060 {
1061 assert (!type->is_array() && !type->is_record());
1062
1063 if (!type->is_vector() && !type->is_matrix()) {
1064 offset = 0;
1065 mask = 1;
1066 }
1067
1068 int id = 0;
1069 for (int i=0; i<4; i++) {
1070 if (mask & (1 << i)) {
1071 switch (this->type->base_type) {
1072 case GLSL_TYPE_UINT:
1073 value.u[i+offset] = src->get_uint_component(id++);
1074 break;
1075 case GLSL_TYPE_INT:
1076 value.i[i+offset] = src->get_int_component(id++);
1077 break;
1078 case GLSL_TYPE_FLOAT:
1079 value.f[i+offset] = src->get_float_component(id++);
1080 break;
1081 case GLSL_TYPE_BOOL:
1082 value.b[i+offset] = src->get_bool_component(id++);
1083 break;
1084 default:
1085 assert(!"Should not get here.");
1086 return;
1087 }
1088 }
1089 }
1090 }
1091
1092 bool
has_value(const ir_constant * c) const1093 ir_constant::has_value(const ir_constant *c) const
1094 {
1095 if (this->type != c->type)
1096 return false;
1097
1098 if (this->type->is_array()) {
1099 for (unsigned i = 0; i < this->type->length; i++) {
1100 if (!this->array_elements[i]->has_value(c->array_elements[i]))
1101 return false;
1102 }
1103 return true;
1104 }
1105
1106 if (this->type->base_type == GLSL_TYPE_STRUCT) {
1107 const exec_node *a_node = this->components.head;
1108 const exec_node *b_node = c->components.head;
1109
1110 while (!a_node->is_tail_sentinel()) {
1111 assert(!b_node->is_tail_sentinel());
1112
1113 const ir_constant *const a_field = (ir_constant *) a_node;
1114 const ir_constant *const b_field = (ir_constant *) b_node;
1115
1116 if (!a_field->has_value(b_field))
1117 return false;
1118
1119 a_node = a_node->next;
1120 b_node = b_node->next;
1121 }
1122
1123 return true;
1124 }
1125
1126 for (unsigned i = 0; i < this->type->components(); i++) {
1127 switch (this->type->base_type) {
1128 case GLSL_TYPE_UINT:
1129 if (this->value.u[i] != c->value.u[i])
1130 return false;
1131 break;
1132 case GLSL_TYPE_INT:
1133 if (this->value.i[i] != c->value.i[i])
1134 return false;
1135 break;
1136 case GLSL_TYPE_FLOAT:
1137 if (this->value.f[i] != c->value.f[i])
1138 return false;
1139 break;
1140 case GLSL_TYPE_BOOL:
1141 if (this->value.b[i] != c->value.b[i])
1142 return false;
1143 break;
1144 default:
1145 assert(!"Should not get here.");
1146 return false;
1147 }
1148 }
1149
1150 return true;
1151 }
1152
1153 bool
is_value(float f,int i) const1154 ir_constant::is_value(float f, int i) const
1155 {
1156 if (!this->type->is_scalar() && !this->type->is_vector())
1157 return false;
1158
1159 /* Only accept boolean values for 0/1. */
1160 if (int(bool(i)) != i && this->type->is_boolean())
1161 return false;
1162
1163 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1164 switch (this->type->base_type) {
1165 case GLSL_TYPE_FLOAT:
1166 if (this->value.f[c] != f)
1167 return false;
1168 break;
1169 case GLSL_TYPE_INT:
1170 if (this->value.i[c] != i)
1171 return false;
1172 break;
1173 case GLSL_TYPE_UINT:
1174 if (this->value.u[c] != unsigned(i))
1175 return false;
1176 break;
1177 case GLSL_TYPE_BOOL:
1178 if (this->value.b[c] != bool(i))
1179 return false;
1180 break;
1181 default:
1182 /* The only other base types are structures, arrays, and samplers.
1183 * Samplers cannot be constants, and the others should have been
1184 * filtered out above.
1185 */
1186 assert(!"Should not get here.");
1187 return false;
1188 }
1189 }
1190
1191 return true;
1192 }
1193
1194 bool
is_zero() const1195 ir_constant::is_zero() const
1196 {
1197 return is_value(0.0, 0);
1198 }
1199
1200 bool
is_one() const1201 ir_constant::is_one() const
1202 {
1203 return is_value(1.0, 1);
1204 }
1205
1206 bool
is_negative_one() const1207 ir_constant::is_negative_one() const
1208 {
1209 return is_value(-1.0, -1);
1210 }
1211
1212 bool
is_basis() const1213 ir_constant::is_basis() const
1214 {
1215 if (!this->type->is_scalar() && !this->type->is_vector())
1216 return false;
1217
1218 if (this->type->is_boolean())
1219 return false;
1220
1221 unsigned ones = 0;
1222 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1223 switch (this->type->base_type) {
1224 case GLSL_TYPE_FLOAT:
1225 if (this->value.f[c] == 1.0)
1226 ones++;
1227 else if (this->value.f[c] != 0.0)
1228 return false;
1229 break;
1230 case GLSL_TYPE_INT:
1231 if (this->value.i[c] == 1)
1232 ones++;
1233 else if (this->value.i[c] != 0)
1234 return false;
1235 break;
1236 case GLSL_TYPE_UINT:
1237 if (int(this->value.u[c]) == 1)
1238 ones++;
1239 else if (int(this->value.u[c]) != 0)
1240 return false;
1241 break;
1242 default:
1243 /* The only other base types are structures, arrays, samplers, and
1244 * booleans. Samplers cannot be constants, and the others should
1245 * have been filtered out above.
1246 */
1247 assert(!"Should not get here.");
1248 return false;
1249 }
1250 }
1251
1252 return ones == 1;
1253 }
1254
1255 bool
is_uint16_constant() const1256 ir_constant::is_uint16_constant() const
1257 {
1258 if (!type->is_integer())
1259 return false;
1260
1261 return value.u[0] < (1 << 16);
1262 }
1263
ir_loop()1264 ir_loop::ir_loop()
1265 : ir_instruction(ir_type_loop)
1266 {
1267 }
1268
1269
ir_dereference_variable(ir_variable * var)1270 ir_dereference_variable::ir_dereference_variable(ir_variable *var)
1271 : ir_dereference(ir_type_dereference_variable, precision_from_ir(var))
1272 {
1273 assert(var != NULL);
1274
1275 this->var = var;
1276 this->type = var->type;
1277 }
1278
1279
ir_dereference_array(ir_rvalue * value,ir_rvalue * array_index)1280 ir_dereference_array::ir_dereference_array(ir_rvalue *value,
1281 ir_rvalue *array_index)
1282 : ir_dereference(ir_type_dereference_array, precision_from_ir(value))
1283 {
1284 this->array_index = array_index;
1285 this->set_array(value);
1286 }
1287
1288
ir_dereference_array(ir_variable * var,ir_rvalue * array_index)1289 ir_dereference_array::ir_dereference_array(ir_variable *var,
1290 ir_rvalue *array_index)
1291 : ir_dereference(ir_type_dereference_array, precision_from_ir(var))
1292 {
1293 void *ctx = ralloc_parent(var);
1294
1295 this->array_index = array_index;
1296 this->set_array(new(ctx) ir_dereference_variable(var));
1297 }
1298
1299
1300 void
set_array(ir_rvalue * value)1301 ir_dereference_array::set_array(ir_rvalue *value)
1302 {
1303 assert(value != NULL);
1304
1305 this->array = value;
1306
1307 const glsl_type *const vt = this->array->type;
1308
1309 if (vt->is_array()) {
1310 type = vt->element_type();
1311 } else if (vt->is_matrix()) {
1312 type = vt->column_type();
1313 } else if (vt->is_vector()) {
1314 type = vt->get_base_type();
1315 }
1316 }
1317
1318
ir_dereference_record(ir_rvalue * value,const char * field)1319 ir_dereference_record::ir_dereference_record(ir_rvalue *value,
1320 const char *field)
1321 : ir_dereference(ir_type_dereference_record, precision_from_ir(value))
1322 {
1323 assert(value != NULL);
1324
1325 this->record = value;
1326 this->field = ralloc_strdup(this, field);
1327 this->type = this->record->type->field_type(field);
1328 if (this->record)
1329 this->precision = this->record->type->field_precision(field);
1330 }
1331
1332
ir_dereference_record(ir_variable * var,const char * field)1333 ir_dereference_record::ir_dereference_record(ir_variable *var,
1334 const char *field)
1335 : ir_dereference(ir_type_dereference_record, precision_from_ir(var))
1336 {
1337 void *ctx = ralloc_parent(var);
1338
1339 this->record = new(ctx) ir_dereference_variable(var);
1340 this->field = ralloc_strdup(this, field);
1341 this->type = this->record->type->field_type(field);
1342 if (this->record)
1343 this->precision = this->record->type->field_precision(field);
1344 }
1345
1346 bool
is_lvalue() const1347 ir_dereference::is_lvalue() const
1348 {
1349 ir_variable *var = this->variable_referenced();
1350
1351 /* Every l-value derference chain eventually ends in a variable.
1352 */
1353 if ((var == NULL) || var->data.read_only)
1354 return false;
1355
1356 /* From section 4.1.7 of the GLSL 4.40 spec:
1357 *
1358 * "Opaque variables cannot be treated as l-values; hence cannot
1359 * be used as out or inout function parameters, nor can they be
1360 * assigned into."
1361 */
1362 if (this->type->contains_opaque())
1363 return false;
1364
1365 return true;
1366 }
1367
1368
1369 static const char * const tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf", "txf_ms", "txs", "lod", "tg4", "query_levels" };
1370
opcode_string()1371 const char *ir_texture::opcode_string()
1372 {
1373 assert((unsigned int) op <=
1374 sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]));
1375 return tex_opcode_strs[op];
1376 }
1377
1378 ir_texture_opcode
get_opcode(const char * str)1379 ir_texture::get_opcode(const char *str)
1380 {
1381 const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
1382 for (int op = 0; op < count; op++) {
1383 if (strcmp(str, tex_opcode_strs[op]) == 0)
1384 return (ir_texture_opcode) op;
1385 }
1386 return (ir_texture_opcode) -1;
1387 }
1388
1389
1390 void
set_sampler(ir_dereference * sampler,const glsl_type * type)1391 ir_texture::set_sampler(ir_dereference *sampler, const glsl_type *type)
1392 {
1393 assert(sampler != NULL);
1394 assert(type != NULL);
1395 this->sampler = sampler;
1396 this->type = type;
1397
1398 if (this->op == ir_txs || this->op == ir_query_levels) {
1399 assert(type->base_type == GLSL_TYPE_INT);
1400 } else if (this->op == ir_lod) {
1401 assert(type->vector_elements == 2);
1402 assert(type->base_type == GLSL_TYPE_FLOAT);
1403 } else {
1404 assert(sampler->type->sampler_type == (int) type->base_type);
1405 if (sampler->type->sampler_shadow)
1406 assert(type->vector_elements == 4 || type->vector_elements == 1);
1407 else
1408 assert(type->vector_elements == 4);
1409 }
1410 }
1411
1412
1413 bool
has_lod(const glsl_type * sampler_type)1414 ir_texture::has_lod(const glsl_type *sampler_type)
1415 {
1416 assert(sampler_type->is_sampler());
1417
1418 switch (sampler_type->sampler_dimensionality) {
1419 case GLSL_SAMPLER_DIM_RECT:
1420 case GLSL_SAMPLER_DIM_BUF:
1421 case GLSL_SAMPLER_DIM_MS:
1422 return false;
1423 default:
1424 return true;
1425 }
1426 }
1427
1428
1429 void
init_mask(const unsigned * comp,unsigned count)1430 ir_swizzle::init_mask(const unsigned *comp, unsigned count)
1431 {
1432 assert((count >= 1) && (count <= 4));
1433
1434 memset(&this->mask, 0, sizeof(this->mask));
1435 this->mask.num_components = count;
1436
1437 unsigned dup_mask = 0;
1438 switch (count) {
1439 case 4:
1440 assert(comp[3] <= 3);
1441 dup_mask |= (1U << comp[3])
1442 & ((1U << comp[0]) | (1U << comp[1]) | (1U << comp[2]));
1443 this->mask.w = comp[3];
1444
1445 case 3:
1446 assert(comp[2] <= 3);
1447 dup_mask |= (1U << comp[2])
1448 & ((1U << comp[0]) | (1U << comp[1]));
1449 this->mask.z = comp[2];
1450
1451 case 2:
1452 assert(comp[1] <= 3);
1453 dup_mask |= (1U << comp[1])
1454 & ((1U << comp[0]));
1455 this->mask.y = comp[1];
1456
1457 case 1:
1458 assert(comp[0] <= 3);
1459 this->mask.x = comp[0];
1460 }
1461
1462 this->mask.has_duplicates = dup_mask != 0;
1463
1464 /* Based on the number of elements in the swizzle and the base type
1465 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
1466 * generate the type of the resulting value.
1467 */
1468 type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
1469 }
1470
ir_swizzle(ir_rvalue * val,unsigned x,unsigned y,unsigned z,unsigned w,unsigned count)1471 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
1472 unsigned w, unsigned count)
1473 : ir_rvalue(ir_type_swizzle, precision_from_ir(val)), val(val)
1474 {
1475 const unsigned components[4] = { x, y, z, w };
1476 this->init_mask(components, count);
1477 }
1478
ir_swizzle(ir_rvalue * val,const unsigned * comp,unsigned count)1479 ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
1480 unsigned count)
1481 : ir_rvalue(ir_type_swizzle, precision_from_ir(val)), val(val)
1482 {
1483 this->init_mask(comp, count);
1484 }
1485
ir_swizzle(ir_rvalue * val,ir_swizzle_mask mask)1486 ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
1487 : ir_rvalue(ir_type_swizzle, precision_from_ir(val))
1488 {
1489 this->val = val;
1490 this->mask = mask;
1491 this->type = glsl_type::get_instance(val->type->base_type,
1492 mask.num_components, 1);
1493 }
1494
1495 #define X 1
1496 #define R 5
1497 #define S 9
1498 #define I 13
1499
1500 ir_swizzle *
create(ir_rvalue * val,const char * str,unsigned vector_length)1501 ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
1502 {
1503 void *ctx = ralloc_parent(val);
1504
1505 /* For each possible swizzle character, this table encodes the value in
1506 * \c idx_map that represents the 0th element of the vector. For invalid
1507 * swizzle characters (e.g., 'k'), a special value is used that will allow
1508 * detection of errors.
1509 */
1510 static const unsigned char base_idx[26] = {
1511 /* a b c d e f g h i j k l m */
1512 R, R, I, I, I, I, R, I, I, I, I, I, I,
1513 /* n o p q r s t u v w x y z */
1514 I, I, S, S, R, S, S, I, I, X, X, X, X
1515 };
1516
1517 /* Each valid swizzle character has an entry in the previous table. This
1518 * table encodes the base index encoded in the previous table plus the actual
1519 * index of the swizzle character. When processing swizzles, the first
1520 * character in the string is indexed in the previous table. Each character
1521 * in the string is indexed in this table, and the value found there has the
1522 * value form the first table subtracted. The result must be on the range
1523 * [0,3].
1524 *
1525 * For example, the string "wzyx" will get X from the first table. Each of
1526 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
1527 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
1528 *
1529 * The string "wzrg" will get X from the first table. Each of the characters
1530 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
1531 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
1532 * [0,3], the error is detected.
1533 */
1534 static const unsigned char idx_map[26] = {
1535 /* a b c d e f g h i j k l m */
1536 R+3, R+2, 0, 0, 0, 0, R+1, 0, 0, 0, 0, 0, 0,
1537 /* n o p q r s t u v w x y z */
1538 0, 0, S+2, S+3, R+0, S+0, S+1, 0, 0, X+3, X+0, X+1, X+2
1539 };
1540
1541 int swiz_idx[4] = { 0, 0, 0, 0 };
1542 unsigned i;
1543
1544
1545 /* Validate the first character in the swizzle string and look up the base
1546 * index value as described above.
1547 */
1548 if ((str[0] < 'a') || (str[0] > 'z'))
1549 return NULL;
1550
1551 const unsigned base = base_idx[str[0] - 'a'];
1552
1553
1554 for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
1555 /* Validate the next character, and, as described above, convert it to a
1556 * swizzle index.
1557 */
1558 if ((str[i] < 'a') || (str[i] > 'z'))
1559 return NULL;
1560
1561 swiz_idx[i] = idx_map[str[i] - 'a'] - base;
1562 if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
1563 return NULL;
1564 }
1565
1566 if (str[i] != '\0')
1567 return NULL;
1568
1569 return new(ctx) ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
1570 swiz_idx[3], i);
1571 }
1572
1573 #undef X
1574 #undef R
1575 #undef S
1576 #undef I
1577
1578 ir_variable *
variable_referenced() const1579 ir_swizzle::variable_referenced() const
1580 {
1581 return this->val->variable_referenced();
1582 }
1583
1584
1585 bool ir_variable::temporaries_allocate_names = false;
1586
1587 const char ir_variable::tmp_name[] = "compiler_temp";
1588
ir_variable(const struct glsl_type * type,const char * name,ir_variable_mode mode,glsl_precision precision)1589 ir_variable::ir_variable(const struct glsl_type *type, const char *name,
1590 ir_variable_mode mode, glsl_precision precision)
1591 : ir_instruction(ir_type_variable)
1592 {
1593 this->type = type;
1594
1595 if (mode == ir_var_temporary && !ir_variable::temporaries_allocate_names)
1596 name = NULL;
1597
1598 /* The ir_variable clone method may call this constructor with name set to
1599 * tmp_name.
1600 */
1601 assert(name != NULL
1602 || mode == ir_var_temporary
1603 || mode == ir_var_function_in
1604 || mode == ir_var_function_out
1605 || mode == ir_var_function_inout);
1606 assert(name != ir_variable::tmp_name
1607 || mode == ir_var_temporary);
1608 if (mode == ir_var_temporary
1609 && (name == NULL || name == ir_variable::tmp_name)) {
1610 this->name = ir_variable::tmp_name;
1611 } else {
1612 this->name = ralloc_strdup(this, name);
1613 }
1614
1615 this->u.max_ifc_array_access = NULL;
1616
1617 this->data.explicit_location = false;
1618 this->data.has_initializer = false;
1619 this->data.location = -1;
1620 this->data.location_frac = 0;
1621 this->data.binding = 0;
1622 this->data.warn_extension_index = 0;
1623 this->constant_value = NULL;
1624 this->constant_initializer = NULL;
1625 this->data.origin_upper_left = false;
1626 this->data.pixel_center_integer = false;
1627 this->data.depth_layout = ir_depth_layout_none;
1628 this->data.used = false;
1629 this->data.read_only = false;
1630 this->data.centroid = false;
1631 this->data.sample = false;
1632 this->data.invariant = false;
1633 this->data.how_declared = ir_var_declared_normally;
1634 this->data.mode = mode;
1635 this->data.precision = precision;
1636 this->data.interpolation = INTERP_QUALIFIER_NONE;
1637 this->data.max_array_access = 0;
1638 this->data.atomic.offset = 0;
1639 this->data.image_read_only = false;
1640 this->data.image_write_only = false;
1641 this->data.image_coherent = false;
1642 this->data.image_volatile = false;
1643 this->data.image_restrict = false;
1644
1645 if (type != NULL) {
1646 if (type->base_type == GLSL_TYPE_SAMPLER)
1647 this->data.read_only = true;
1648
1649 if (type->is_interface())
1650 this->init_interface_type(type);
1651 else if (type->is_array() && type->fields.array->is_interface())
1652 this->init_interface_type(type->fields.array);
1653 }
1654 }
1655
1656
1657 const char *
interpolation_string(unsigned interpolation)1658 interpolation_string(unsigned interpolation)
1659 {
1660 switch (interpolation) {
1661 case INTERP_QUALIFIER_NONE: return "no";
1662 case INTERP_QUALIFIER_SMOOTH: return "smooth";
1663 case INTERP_QUALIFIER_FLAT: return "flat";
1664 case INTERP_QUALIFIER_NOPERSPECTIVE: return "noperspective";
1665 }
1666
1667 assert(!"Should not get here.");
1668 return "";
1669 }
1670
1671
1672 glsl_interp_qualifier
determine_interpolation_mode(bool flat_shade)1673 ir_variable::determine_interpolation_mode(bool flat_shade)
1674 {
1675 if (this->data.interpolation != INTERP_QUALIFIER_NONE)
1676 return (glsl_interp_qualifier) this->data.interpolation;
1677 int location = this->data.location;
1678 bool is_gl_Color =
1679 location == VARYING_SLOT_COL0 || location == VARYING_SLOT_COL1;
1680 if (flat_shade && is_gl_Color)
1681 return INTERP_QUALIFIER_FLAT;
1682 else
1683 return INTERP_QUALIFIER_SMOOTH;
1684 }
1685
1686 const char *const ir_variable::warn_extension_table[] = {
1687 "",
1688 "GL_ARB_shader_stencil_export",
1689 "GL_AMD_shader_stencil_export",
1690 };
1691
1692 void
enable_extension_warning(const char * extension)1693 ir_variable::enable_extension_warning(const char *extension)
1694 {
1695 for (unsigned i = 0; i < Elements(warn_extension_table); i++) {
1696 if (strcmp(warn_extension_table[i], extension) == 0) {
1697 this->data.warn_extension_index = i;
1698 return;
1699 }
1700 }
1701
1702 assert(!"Should not get here.");
1703 this->data.warn_extension_index = 0;
1704 }
1705
1706 const char *
get_extension_warning() const1707 ir_variable::get_extension_warning() const
1708 {
1709 return this->data.warn_extension_index == 0
1710 ? NULL : warn_extension_table[this->data.warn_extension_index];
1711 }
1712
ir_function_signature(const glsl_type * return_type,glsl_precision precision,builtin_available_predicate b)1713 ir_function_signature::ir_function_signature(const glsl_type *return_type,
1714 glsl_precision precision, builtin_available_predicate b)
1715 : ir_instruction(ir_type_function_signature),
1716 return_type(return_type), precision(precision), is_defined(false), is_intrinsic(false),
1717 builtin_avail(b), _function(NULL)
1718 {
1719 this->origin = NULL;
1720 }
1721
1722
1723 bool
is_builtin() const1724 ir_function_signature::is_builtin() const
1725 {
1726 return builtin_avail != NULL;
1727 }
1728
1729
1730 bool
is_builtin_available(const _mesa_glsl_parse_state * state) const1731 ir_function_signature::is_builtin_available(const _mesa_glsl_parse_state *state) const
1732 {
1733 /* We can't call the predicate without a state pointer, so just say that
1734 * the signature is available. At compile time, we need the filtering,
1735 * but also receive a valid state pointer. At link time, we're resolving
1736 * imported built-in prototypes to their definitions, which will always
1737 * be an exact match. So we can skip the filtering.
1738 */
1739 if (state == NULL)
1740 return true;
1741
1742 assert(builtin_avail != NULL);
1743 return builtin_avail(state);
1744 }
1745
1746
1747 static bool
modes_match(unsigned a,unsigned b)1748 modes_match(unsigned a, unsigned b)
1749 {
1750 if (a == b)
1751 return true;
1752
1753 /* Accept "in" vs. "const in" */
1754 if ((a == ir_var_const_in && b == ir_var_function_in) ||
1755 (b == ir_var_const_in && a == ir_var_function_in))
1756 return true;
1757
1758 return false;
1759 }
1760
1761
1762 const char *
qualifiers_match(exec_list * params)1763 ir_function_signature::qualifiers_match(exec_list *params)
1764 {
1765 /* check that the qualifiers match. */
1766 foreach_two_lists(a_node, &this->parameters, b_node, params) {
1767 ir_variable *a = (ir_variable *) a_node;
1768 ir_variable *b = (ir_variable *) b_node;
1769
1770 /* NOTE: precision does not affect qualifier matching */
1771 if (a->data.read_only != b->data.read_only ||
1772 !modes_match(a->data.mode, b->data.mode) ||
1773 a->data.interpolation != b->data.interpolation ||
1774 a->data.centroid != b->data.centroid ||
1775 a->data.sample != b->data.sample ||
1776 a->data.image_read_only != b->data.image_read_only ||
1777 a->data.image_write_only != b->data.image_write_only ||
1778 a->data.image_coherent != b->data.image_coherent ||
1779 a->data.image_volatile != b->data.image_volatile ||
1780 a->data.image_restrict != b->data.image_restrict) {
1781
1782 /* parameter a's qualifiers don't match */
1783 return a->name;
1784 }
1785 }
1786 return NULL;
1787 }
1788
1789
1790 void
replace_parameters(exec_list * new_params)1791 ir_function_signature::replace_parameters(exec_list *new_params)
1792 {
1793 /* Destroy all of the previous parameter information. If the previous
1794 * parameter information comes from the function prototype, it may either
1795 * specify incorrect parameter names or not have names at all.
1796 */
1797 new_params->move_nodes_to(¶meters);
1798 }
1799
1800
ir_function(const char * name)1801 ir_function::ir_function(const char *name)
1802 : ir_instruction(ir_type_function)
1803 {
1804 this->name = ralloc_strdup(this, name);
1805 }
1806
1807
1808 bool
has_user_signature()1809 ir_function::has_user_signature()
1810 {
1811 foreach_in_list(ir_function_signature, sig, &this->signatures) {
1812 if (!sig->is_builtin())
1813 return true;
1814 }
1815 return false;
1816 }
1817
1818
1819 ir_rvalue *
error_value(void * mem_ctx)1820 ir_rvalue::error_value(void *mem_ctx)
1821 {
1822 ir_rvalue *v = new(mem_ctx) ir_rvalue(ir_type_unset, glsl_precision_undefined);
1823
1824 v->type = glsl_type::error_type;
1825 return v;
1826 }
1827
1828
1829 void
visit_exec_list(exec_list * list,ir_visitor * visitor)1830 visit_exec_list(exec_list *list, ir_visitor *visitor)
1831 {
1832 foreach_in_list_safe(ir_instruction, node, list) {
1833 node->accept(visitor);
1834 }
1835 }
1836
1837
1838 static void
steal_memory(ir_instruction * ir,void * new_ctx)1839 steal_memory(ir_instruction *ir, void *new_ctx)
1840 {
1841 ir_variable *var = ir->as_variable();
1842 ir_constant *constant = ir->as_constant();
1843 if (var != NULL && var->constant_value != NULL)
1844 steal_memory(var->constant_value, ir);
1845
1846 if (var != NULL && var->constant_initializer != NULL)
1847 steal_memory(var->constant_initializer, ir);
1848
1849 /* The components of aggregate constants are not visited by the normal
1850 * visitor, so steal their values by hand.
1851 */
1852 if (constant != NULL) {
1853 if (constant->type->is_record()) {
1854 foreach_in_list(ir_constant, field, &constant->components) {
1855 steal_memory(field, ir);
1856 }
1857 } else if (constant->type->is_array()) {
1858 for (unsigned int i = 0; i < constant->type->length; i++) {
1859 steal_memory(constant->array_elements[i], ir);
1860 }
1861 }
1862 }
1863
1864 ralloc_steal(new_ctx, ir);
1865 }
1866
1867
1868 void
reparent_ir(exec_list * list,void * mem_ctx)1869 reparent_ir(exec_list *list, void *mem_ctx)
1870 {
1871 foreach_in_list(ir_instruction, node, list) {
1872 visit_tree(node, steal_memory, mem_ctx);
1873 }
1874 }
1875
1876
1877 glsl_precision
precision_from_ir(ir_instruction * ir)1878 precision_from_ir (ir_instruction* ir)
1879 {
1880 if (!ir)
1881 return glsl_precision_undefined;
1882 ir_variable* var = ir->as_variable();
1883 if (var)
1884 return (glsl_precision)var->data.precision;
1885 ir_rvalue* rv = ir->as_rvalue();
1886 if (rv)
1887 return rv->get_precision();
1888 ir_call* fcall = ir->as_call();
1889 if (fcall && fcall->return_deref)
1890 return fcall->return_deref->get_precision();
1891 if (ir->ir_type == ir_type_function_signature)
1892 {
1893 ir_function_signature* sig = (ir_function_signature*)ir;
1894 return sig->precision;
1895 }
1896 return glsl_precision_high;
1897 }
1898
1899 static ir_rvalue *
try_min_one(ir_rvalue * ir)1900 try_min_one(ir_rvalue *ir)
1901 {
1902 ir_expression *expr = ir->as_expression();
1903
1904 if (!expr || expr->operation != ir_binop_min)
1905 return NULL;
1906
1907 if (expr->operands[0]->is_one())
1908 return expr->operands[1];
1909
1910 if (expr->operands[1]->is_one())
1911 return expr->operands[0];
1912
1913 return NULL;
1914 }
1915
1916 static ir_rvalue *
try_max_zero(ir_rvalue * ir)1917 try_max_zero(ir_rvalue *ir)
1918 {
1919 ir_expression *expr = ir->as_expression();
1920
1921 if (!expr || expr->operation != ir_binop_max)
1922 return NULL;
1923
1924 if (expr->operands[0]->is_zero())
1925 return expr->operands[1];
1926
1927 if (expr->operands[1]->is_zero())
1928 return expr->operands[0];
1929
1930 return NULL;
1931 }
1932
1933 ir_rvalue *
as_rvalue_to_saturate()1934 ir_rvalue::as_rvalue_to_saturate()
1935 {
1936 ir_expression *expr = this->as_expression();
1937
1938 if (!expr)
1939 return NULL;
1940
1941 ir_rvalue *max_zero = try_max_zero(expr);
1942 if (max_zero) {
1943 return try_min_one(max_zero);
1944 } else {
1945 ir_rvalue *min_one = try_min_one(expr);
1946 if (min_one) {
1947 return try_max_zero(min_one);
1948 }
1949 }
1950
1951 return NULL;
1952 }
1953
1954
1955 unsigned
vertices_per_prim(GLenum prim)1956 vertices_per_prim(GLenum prim)
1957 {
1958 switch (prim) {
1959 case GL_POINTS:
1960 return 1;
1961 case GL_LINES:
1962 return 2;
1963 case GL_TRIANGLES:
1964 return 3;
1965 case GL_LINES_ADJACENCY:
1966 return 4;
1967 case GL_TRIANGLES_ADJACENCY:
1968 return 6;
1969 default:
1970 assert(!"Bad primitive");
1971 return 3;
1972 }
1973 }
1974
1975 /**
1976 * Generate a string describing the mode of a variable
1977 */
1978 const char *
mode_string(const ir_variable * var)1979 mode_string(const ir_variable *var)
1980 {
1981 switch (var->data.mode) {
1982 case ir_var_auto:
1983 return (var->data.read_only) ? "global constant" : "global variable";
1984
1985 case ir_var_uniform:
1986 return "uniform";
1987
1988 case ir_var_shader_in:
1989 return "shader input";
1990
1991 case ir_var_shader_out:
1992 return "shader output";
1993
1994 case ir_var_shader_inout:
1995 return "shader inout";
1996
1997 case ir_var_function_in:
1998 case ir_var_const_in:
1999 return "function input";
2000
2001 case ir_var_function_out:
2002 return "function output";
2003
2004 case ir_var_function_inout:
2005 return "function inout";
2006
2007 case ir_var_system_value:
2008 return "shader input";
2009
2010 case ir_var_temporary:
2011 return "compiler temporary";
2012
2013 case ir_var_mode_count:
2014 break;
2015 }
2016
2017 assert(!"Should not get here.");
2018 return "invalid variable";
2019 }
2020