1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef IR_H
26 #define IR_H
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "util/ralloc.h"
32 #include "util/format/u_format.h"
33 #include "util/half_float.h"
34 #include "compiler/glsl_types.h"
35 #include "list.h"
36 #include "ir_visitor.h"
37 #include "ir_hierarchical_visitor.h"
38 #include "util/glheader.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 struct _mesa_glsl_parse_state;
45 struct gl_shader_program;
46 struct gl_builtin_uniform_desc;
47
48 #ifdef __cplusplus
49 }
50 #endif
51
52 #ifdef __cplusplus
53
54 /**
55 * \defgroup IR Intermediate representation nodes
56 *
57 * @{
58 */
59
60 /**
61 * Class tags
62 *
63 * Each concrete class derived from \c ir_instruction has a value in this
64 * enumerant. The value for the type is stored in \c ir_instruction::ir_type
65 * by the constructor. While using type tags is not very C++, it is extremely
66 * convenient. For example, during debugging you can simply inspect
67 * \c ir_instruction::ir_type to find out the actual type of the object.
68 *
69 * In addition, it is possible to use a switch-statement based on \c
70 * \c ir_instruction::ir_type to select different behavior for different object
71 * types. For functions that have only slight differences for several object
72 * types, this allows writing very straightforward, readable code.
73 */
74 enum ir_node_type {
75 ir_type_dereference_array,
76 ir_type_dereference_record,
77 ir_type_dereference_variable,
78 ir_type_constant,
79 ir_type_expression,
80 ir_type_swizzle,
81 ir_type_texture,
82 ir_type_variable,
83 ir_type_assignment,
84 ir_type_call,
85 ir_type_function,
86 ir_type_function_signature,
87 ir_type_if,
88 ir_type_loop,
89 ir_type_loop_jump,
90 ir_type_return,
91 ir_type_discard,
92 ir_type_demote,
93 ir_type_emit_vertex,
94 ir_type_end_primitive,
95 ir_type_barrier,
96 ir_type_max, /**< maximum ir_type enum number, for validation */
97 ir_type_unset = ir_type_max,
98 ir_type_error
99 };
100
101
102 /**
103 * Base class of all IR instructions
104 */
105 class ir_instruction : public exec_node {
106 public:
107 enum ir_node_type ir_type;
108
109 /**
110 * GCC 4.7+ and clang warn when deleting an ir_instruction unless
111 * there's a virtual destructor present. Because we almost
112 * universally use ralloc for our memory management of
113 * ir_instructions, the destructor doesn't need to do any work.
114 */
~ir_instruction()115 virtual ~ir_instruction()
116 {
117 }
118
119 /** ir_print_visitor helper for debugging. */
120 void print(void) const;
121 void fprint(FILE *f) const;
122
123 virtual void accept(ir_visitor *) = 0;
124 virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
125 virtual ir_instruction *clone(void *mem_ctx,
126 struct hash_table *ht) const = 0;
127
is_rvalue()128 bool is_rvalue() const
129 {
130 return ir_type == ir_type_dereference_array ||
131 ir_type == ir_type_dereference_record ||
132 ir_type == ir_type_dereference_variable ||
133 ir_type == ir_type_constant ||
134 ir_type == ir_type_expression ||
135 ir_type == ir_type_swizzle ||
136 ir_type == ir_type_texture ||
137 ir_type == ir_type_error;
138 }
139
is_dereference()140 bool is_dereference() const
141 {
142 return ir_type == ir_type_dereference_array ||
143 ir_type == ir_type_dereference_record ||
144 ir_type == ir_type_dereference_variable;
145 }
146
is_jump()147 bool is_jump() const
148 {
149 return ir_type == ir_type_loop_jump ||
150 ir_type == ir_type_return ||
151 ir_type == ir_type_discard;
152 }
153
154 /**
155 * \name IR instruction downcast functions
156 *
157 * These functions either cast the object to a derived class or return
158 * \c NULL if the object's type does not match the specified derived class.
159 * Additional downcast functions will be added as needed.
160 */
161 /*@{*/
162 #define AS_BASE(TYPE) \
163 class ir_##TYPE *as_##TYPE() \
164 { \
165 return is_##TYPE() ? (ir_##TYPE *) this : NULL; \
166 } \
167 const class ir_##TYPE *as_##TYPE() const \
168 { \
169 return is_##TYPE() ? (ir_##TYPE *) this : NULL; \
170 }
171
172 AS_BASE(rvalue)
AS_BASE(dereference)173 AS_BASE(dereference)
174 AS_BASE(jump)
175 #undef AS_BASE
176
177 #define AS_CHILD(TYPE) \
178 class ir_##TYPE * as_##TYPE() \
179 { \
180 return ir_type == ir_type_##TYPE ? (ir_##TYPE *) this : NULL; \
181 } \
182 const class ir_##TYPE * as_##TYPE() const \
183 { \
184 return ir_type == ir_type_##TYPE ? (const ir_##TYPE *) this : NULL; \
185 }
186 AS_CHILD(variable)
187 AS_CHILD(function)
188 AS_CHILD(dereference_array)
189 AS_CHILD(dereference_variable)
190 AS_CHILD(dereference_record)
191 AS_CHILD(expression)
192 AS_CHILD(loop)
193 AS_CHILD(assignment)
194 AS_CHILD(call)
195 AS_CHILD(return)
196 AS_CHILD(if)
197 AS_CHILD(swizzle)
198 AS_CHILD(texture)
199 AS_CHILD(constant)
200 AS_CHILD(discard)
201 #undef AS_CHILD
202 /*@}*/
203
204 protected:
205 ir_instruction(enum ir_node_type t)
206 : ir_type(t)
207 {
208 }
209
210 private:
ir_instruction()211 ir_instruction()
212 {
213 assert(!"Should not get here.");
214 }
215 };
216
217
218 /**
219 * The base class for all "values"/expression trees.
220 */
221 class ir_rvalue : public ir_instruction {
222 public:
223 const struct glsl_type *type;
224
225 virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const;
226
accept(ir_visitor * v)227 virtual void accept(ir_visitor *v)
228 {
229 v->visit(this);
230 }
231
232 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
233
234 virtual ir_constant *constant_expression_value(void *mem_ctx,
235 struct hash_table *variable_context = NULL);
236
237 virtual bool is_lvalue(const struct _mesa_glsl_parse_state * = NULL) const
238 {
239 return false;
240 }
241
242 /**
243 * Get the variable that is ultimately referenced by an r-value
244 */
variable_referenced()245 virtual ir_variable *variable_referenced() const
246 {
247 return NULL;
248 }
249
250
251 /**
252 * If an r-value is a reference to a whole variable, get that variable
253 *
254 * \return
255 * Pointer to a variable that is completely dereferenced by the r-value. If
256 * the r-value is not a dereference or the dereference does not access the
257 * entire variable (i.e., it's just one array element, struct field), \c NULL
258 * is returned.
259 */
whole_variable_referenced()260 virtual ir_variable *whole_variable_referenced()
261 {
262 return NULL;
263 }
264
265 /**
266 * Determine if an r-value has the value zero
267 *
268 * The base implementation of this function always returns \c false. The
269 * \c ir_constant class over-rides this function to return \c true \b only
270 * for vector and scalar types that have all elements set to the value
271 * zero (or \c false for booleans).
272 *
273 * \sa ir_constant::has_value, ir_rvalue::is_one
274 */
275 virtual bool is_zero() const;
276
277 /**
278 * Determine if an r-value has the value one
279 *
280 * The base implementation of this function always returns \c false. The
281 * \c ir_constant class over-rides this function to return \c true \b only
282 * for vector and scalar types that have all elements set to the value
283 * one (or \c true for booleans).
284 *
285 * \sa ir_constant::has_value, ir_rvalue::is_zero
286 */
287 virtual bool is_one() const;
288
289 /**
290 * Return a generic value of error_type.
291 *
292 * Allocation will be performed with 'mem_ctx' as ralloc owner.
293 */
294 static ir_rvalue *error_value(void *mem_ctx);
295
296 protected:
297 ir_rvalue(enum ir_node_type t);
298 };
299
300
301 /**
302 * Variable storage classes
303 */
304 enum ir_variable_mode {
305 ir_var_auto = 0, /**< Function local variables and globals. */
306 ir_var_uniform, /**< Variable declared as a uniform. */
307 ir_var_shader_storage, /**< Variable declared as an ssbo. */
308 ir_var_shader_shared, /**< Variable declared as shared. */
309 ir_var_shader_in,
310 ir_var_shader_out,
311 ir_var_function_in,
312 ir_var_function_out,
313 ir_var_function_inout,
314 ir_var_const_in, /**< "in" param that must be a constant expression */
315 ir_var_system_value, /**< Ex: front-face, instance-id, etc. */
316 ir_var_temporary, /**< Temporary variable generated during compilation. */
317 ir_var_mode_count /**< Number of variable modes */
318 };
319
320 /**
321 * Enum keeping track of how a variable was declared. For error checking of
322 * the gl_PerVertex redeclaration rules.
323 */
324 enum ir_var_declaration_type {
325 /**
326 * Normal declaration (for most variables, this means an explicit
327 * declaration. Exception: temporaries are always implicitly declared, but
328 * they still use ir_var_declared_normally).
329 *
330 * Note: an ir_variable that represents a named interface block uses
331 * ir_var_declared_normally.
332 */
333 ir_var_declared_normally = 0,
334
335 /**
336 * Variable was explicitly declared (or re-declared) in an unnamed
337 * interface block.
338 */
339 ir_var_declared_in_block,
340
341 /**
342 * Variable is an implicitly declared built-in that has not been explicitly
343 * re-declared by the shader.
344 */
345 ir_var_declared_implicitly,
346
347 /**
348 * Variable is implicitly generated by the compiler and should not be
349 * visible via the API.
350 */
351 ir_var_hidden,
352 };
353
354 /**
355 * \brief Layout qualifiers for gl_FragDepth.
356 *
357 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
358 * with a layout qualifier.
359 */
360 enum ir_depth_layout {
361 ir_depth_layout_none, /**< No depth layout is specified. */
362 ir_depth_layout_any,
363 ir_depth_layout_greater,
364 ir_depth_layout_less,
365 ir_depth_layout_unchanged
366 };
367
368 /**
369 * \brief Convert depth layout qualifier to string.
370 */
371 const char*
372 depth_layout_string(ir_depth_layout layout);
373
374 /**
375 * Description of built-in state associated with a uniform
376 *
377 * \sa ir_variable::state_slots
378 */
379 struct ir_state_slot {
380 gl_state_index16 tokens[STATE_LENGTH];
381 };
382
383
384 class ir_variable : public ir_instruction {
385 public:
386 ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
387
388 virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;
389
accept(ir_visitor * v)390 virtual void accept(ir_visitor *v)
391 {
392 v->visit(this);
393 }
394
395 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
396
397
398 /**
399 * Determine whether or not a variable is part of a uniform or
400 * shader storage block.
401 */
is_in_buffer_block()402 inline bool is_in_buffer_block() const
403 {
404 return (this->data.mode == ir_var_uniform ||
405 this->data.mode == ir_var_shader_storage) &&
406 this->interface_type != NULL;
407 }
408
409 /**
410 * Determine whether or not a variable is part of a shader storage block.
411 */
is_in_shader_storage_block()412 inline bool is_in_shader_storage_block() const
413 {
414 return this->data.mode == ir_var_shader_storage &&
415 this->interface_type != NULL;
416 }
417
418 /**
419 * Determine whether or not a variable is the declaration of an interface
420 * block
421 *
422 * For the first declaration below, there will be an \c ir_variable named
423 * "instance" whose type and whose instance_type will be the same
424 * \c glsl_type. For the second declaration, there will be an \c ir_variable
425 * named "f" whose type is float and whose instance_type is B2.
426 *
427 * "instance" is an interface instance variable, but "f" is not.
428 *
429 * uniform B1 {
430 * float f;
431 * } instance;
432 *
433 * uniform B2 {
434 * float f;
435 * };
436 */
is_interface_instance()437 inline bool is_interface_instance() const
438 {
439 return glsl_without_array(this->type) == this->interface_type;
440 }
441
442 /**
443 * Return whether this variable contains a bindless sampler/image.
444 */
contains_bindless()445 inline bool contains_bindless() const
446 {
447 if (!glsl_contains_sampler(this->type) && !glsl_type_contains_image(this->type))
448 return false;
449
450 return this->data.bindless || this->data.mode != ir_var_uniform;
451 }
452
453 /**
454 * Set this->interface_type on a newly created variable.
455 */
init_interface_type(const struct glsl_type * type)456 void init_interface_type(const struct glsl_type *type)
457 {
458 assert(this->interface_type == NULL);
459 this->interface_type = type;
460 if (this->is_interface_instance()) {
461 this->u.max_ifc_array_access =
462 ralloc_array(this, int, type->length);
463 for (unsigned i = 0; i < type->length; i++) {
464 this->u.max_ifc_array_access[i] = -1;
465 }
466 }
467 }
468
469 /**
470 * Change this->interface_type on a variable that previously had a
471 * different, but compatible, interface_type. This is used during linking
472 * to set the size of arrays in interface blocks.
473 */
change_interface_type(const struct glsl_type * type)474 void change_interface_type(const struct glsl_type *type)
475 {
476 if (this->u.max_ifc_array_access != NULL) {
477 /* max_ifc_array_access has already been allocated, so make sure the
478 * new interface has the same number of fields as the old one.
479 */
480 assert(this->interface_type->length == type->length);
481 }
482 this->interface_type = type;
483 }
484
485 /**
486 * Change this->interface_type on a variable that previously had a
487 * different, and incompatible, interface_type. This is used during
488 * compilation to handle redeclaration of the built-in gl_PerVertex
489 * interface block.
490 */
reinit_interface_type(const struct glsl_type * type)491 void reinit_interface_type(const struct glsl_type *type)
492 {
493 if (this->u.max_ifc_array_access != NULL) {
494 #ifndef NDEBUG
495 /* Redeclaring gl_PerVertex is only allowed if none of the built-ins
496 * it defines have been accessed yet; so it's safe to throw away the
497 * old max_ifc_array_access pointer, since all of its values are
498 * zero.
499 */
500 for (unsigned i = 0; i < this->interface_type->length; i++)
501 assert(this->u.max_ifc_array_access[i] == -1);
502 #endif
503 ralloc_free(this->u.max_ifc_array_access);
504 this->u.max_ifc_array_access = NULL;
505 }
506 this->interface_type = NULL;
507 init_interface_type(type);
508 }
509
get_interface_type()510 const glsl_type *get_interface_type() const
511 {
512 return this->interface_type;
513 }
514
get_interface_type_packing()515 enum glsl_interface_packing get_interface_type_packing() const
516 {
517 return glsl_get_ifc_packing(this->interface_type);
518 }
519 /**
520 * Get the max_ifc_array_access pointer
521 *
522 * A "set" function is not needed because the array is dynamically allocated
523 * as necessary.
524 */
get_max_ifc_array_access()525 inline int *get_max_ifc_array_access()
526 {
527 assert(this->data._num_state_slots == 0);
528 return this->u.max_ifc_array_access;
529 }
530
get_num_state_slots()531 inline unsigned get_num_state_slots() const
532 {
533 assert(!this->is_interface_instance()
534 || this->data._num_state_slots == 0);
535 return this->data._num_state_slots;
536 }
537
set_num_state_slots(unsigned n)538 inline void set_num_state_slots(unsigned n)
539 {
540 assert(!this->is_interface_instance()
541 || n == 0);
542 this->data._num_state_slots = n;
543 }
544
get_state_slots()545 inline ir_state_slot *get_state_slots()
546 {
547 return this->is_interface_instance() ? NULL : this->u.state_slots;
548 }
549
get_state_slots()550 inline const ir_state_slot *get_state_slots() const
551 {
552 return this->is_interface_instance() ? NULL : this->u.state_slots;
553 }
554
allocate_state_slots(unsigned n)555 inline ir_state_slot *allocate_state_slots(unsigned n)
556 {
557 assert(!this->is_interface_instance());
558
559 this->u.state_slots = ralloc_array(this, ir_state_slot, n);
560 this->data._num_state_slots = 0;
561
562 if (this->u.state_slots != NULL)
563 this->data._num_state_slots = n;
564
565 return this->u.state_slots;
566 }
567
is_interpolation_flat()568 inline bool is_interpolation_flat() const
569 {
570 return this->data.interpolation == INTERP_MODE_FLAT ||
571 glsl_contains_integer(this->type) ||
572 glsl_contains_double(this->type);
573 }
574
is_name_ralloced()575 inline bool is_name_ralloced() const
576 {
577 return this->name != ir_variable::tmp_name &&
578 this->name != this->name_storage;
579 }
580
is_fb_fetch_color_output()581 inline bool is_fb_fetch_color_output() const
582 {
583 return this->data.fb_fetch_output &&
584 this->data.location != FRAG_RESULT_DEPTH &&
585 this->data.location != FRAG_RESULT_STENCIL;
586 }
587
588 /**
589 * Enable emitting extension warnings for this variable
590 */
591 void enable_extension_warning(const char *extension);
592
593 /**
594 * Declared type of the variable
595 */
596 const struct glsl_type *type;
597
598 /**
599 * Declared name of the variable
600 */
601 const char *name;
602
603 private:
604 /**
605 * If the name length fits into name_storage, it's used, otherwise
606 * the name is ralloc'd. shader-db mining showed that 70% of variables
607 * fit here. This is a win over ralloc where only ralloc_header has
608 * 20 bytes on 64-bit (28 bytes with debug), and we can also skip malloc.
609 */
610 char name_storage[16];
611
612 public:
613 struct ir_variable_data {
614
615 /**
616 * Is the variable read-only?
617 *
618 * This is set for variables declared as \c const, shader inputs,
619 * and uniforms.
620 */
621 unsigned read_only:1;
622 unsigned centroid:1;
623 unsigned sample:1;
624 unsigned patch:1;
625 /**
626 * Was an 'invariant' qualifier explicitly set in the shader?
627 *
628 * This is used to cross validate qualifiers.
629 */
630 unsigned explicit_invariant:1;
631 /**
632 * Is the variable invariant?
633 *
634 * It can happen either by having the 'invariant' qualifier
635 * explicitly set in the shader or by being used in calculations
636 * of other invariant variables.
637 */
638 unsigned invariant:1;
639 unsigned precise:1;
640
641 /**
642 * Has this variable been used for reading or writing?
643 *
644 * Several GLSL semantic checks require knowledge of whether or not a
645 * variable has been used. For example, it is an error to redeclare a
646 * variable as invariant after it has been used.
647 *
648 * This is maintained in the ast_to_hir.cpp path and during linking,
649 * but not in Mesa's fixed function or ARB program paths.
650 */
651 unsigned used:1;
652
653 /**
654 * Has this variable been statically assigned?
655 *
656 * This answers whether the variable was assigned in any path of
657 * the shader during ast_to_hir. This doesn't answer whether it is
658 * still written after dead code removal, nor is it maintained in
659 * non-ast_to_hir.cpp (GLSL parsing) paths.
660 */
661 unsigned assigned:1;
662
663 /**
664 * Enum indicating how the variable was declared. See
665 * ir_var_declaration_type.
666 *
667 * This is used to detect certain kinds of illegal variable redeclarations.
668 */
669 unsigned how_declared:2;
670
671 /**
672 * Storage class of the variable.
673 *
674 * \sa ir_variable_mode
675 */
676 unsigned mode:4;
677
678 /**
679 * Interpolation mode for shader inputs / outputs
680 *
681 * \sa glsl_interp_mode
682 */
683 unsigned interpolation:2;
684
685 /**
686 * Was the location explicitly set in the shader?
687 *
688 * If the location is explicitly set in the shader, it \b cannot be changed
689 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
690 * no effect).
691 */
692 unsigned explicit_location:1;
693 unsigned explicit_index:1;
694
695 /**
696 * Was an initial binding explicitly set in the shader?
697 *
698 * If so, constant_value contains an integer ir_constant representing the
699 * initial binding point.
700 */
701 unsigned explicit_binding:1;
702
703 /**
704 * Was an initial component explicitly set in the shader?
705 */
706 unsigned explicit_component:1;
707
708 /**
709 * Does this variable have an initializer?
710 *
711 * This is used by the linker to cross-validiate initializers of global
712 * variables.
713 */
714 unsigned has_initializer:1;
715
716 /**
717 * Is the initializer created by the compiler (glsl_zero_init)
718 */
719 unsigned is_implicit_initializer:1;
720
721 /**
722 * Is this varying used by transform feedback?
723 *
724 * This is used by the linker to decide if it's safe to pack the varying.
725 */
726 unsigned is_xfb:1;
727
728 /**
729 * Is this varying used only by transform feedback?
730 *
731 * This is used by the linker to decide if its safe to pack the varying.
732 */
733 unsigned is_xfb_only:1;
734
735 /**
736 * Was a transform feedback buffer set in the shader?
737 */
738 unsigned explicit_xfb_buffer:1;
739
740 /**
741 * Was a transform feedback offset set in the shader?
742 */
743 unsigned explicit_xfb_offset:1;
744
745 /**
746 * Was a transform feedback stride set in the shader?
747 */
748 unsigned explicit_xfb_stride:1;
749
750 /**
751 * If non-zero, then this variable may be packed along with other variables
752 * into a single varying slot, so this offset should be applied when
753 * accessing components. For example, an offset of 1 means that the x
754 * component of this variable is actually stored in component y of the
755 * location specified by \c location.
756 */
757 unsigned location_frac:2;
758
759 /**
760 * Layout of the matrix. Uses glsl_matrix_layout values.
761 */
762 unsigned matrix_layout:2;
763
764 /**
765 * Non-zero if this variable was created by lowering a named interface
766 * block.
767 */
768 unsigned from_named_ifc_block:1;
769
770 /**
771 * Non-zero if the variable must be a shader input. This is useful for
772 * constraints on function parameters.
773 */
774 unsigned must_be_shader_input:1;
775
776 /**
777 * Output index for dual source blending.
778 *
779 * \note
780 * The GLSL spec only allows the values 0 or 1 for the index in \b dual
781 * source blending.
782 */
783 unsigned index:1;
784
785 /**
786 * Precision qualifier.
787 *
788 * In desktop GLSL we do not care about precision qualifiers at all, in
789 * fact, the spec says that precision qualifiers are ignored.
790 *
791 * To make things easy, we make it so that this field is always
792 * GLSL_PRECISION_NONE on desktop shaders. This way all the variables
793 * have the same precision value and the checks we add in the compiler
794 * for this field will never break a desktop shader compile.
795 */
796 unsigned precision:2;
797
798 /**
799 * \brief Layout qualifier for gl_FragDepth.
800 *
801 * This is not equal to \c ir_depth_layout_none if and only if this
802 * variable is \c gl_FragDepth and a layout qualifier is specified.
803 */
804 unsigned depth_layout:3; /*ir_depth_layout*/
805
806 /**
807 * Memory qualifiers.
808 */
809 unsigned memory_read_only:1; /**< "readonly" qualifier. */
810 unsigned memory_write_only:1; /**< "writeonly" qualifier. */
811 unsigned memory_coherent:1;
812 unsigned memory_volatile:1;
813 unsigned memory_restrict:1;
814
815 /**
816 * ARB_shader_storage_buffer_object
817 */
818 unsigned from_ssbo_unsized_array:1; /**< unsized array buffer variable. */
819
820 unsigned implicit_sized_array:1;
821
822 /**
823 * Whether this is a fragment shader output implicitly initialized with
824 * the previous contents of the specified render target at the
825 * framebuffer location corresponding to this shader invocation.
826 */
827 unsigned fb_fetch_output:1;
828
829 /**
830 * Non-zero if this variable is considered bindless as defined by
831 * ARB_bindless_texture.
832 */
833 unsigned bindless:1;
834
835 /**
836 * Non-zero if this variable is considered bound as defined by
837 * ARB_bindless_texture.
838 */
839 unsigned bound:1;
840
841 /**
842 * Non-zero if the variable shall not be implicitly converted during
843 * functions matching.
844 */
845 unsigned implicit_conversion_prohibited:1;
846
847 /**
848 * Emit a warning if this variable is accessed.
849 */
850 private:
851 uint8_t warn_extension_index;
852
853 public:
854 /**
855 * Image internal format if specified explicitly, otherwise
856 * PIPE_FORMAT_NONE.
857 */
858 enum pipe_format image_format;
859
860 private:
861 /**
862 * Number of state slots used
863 *
864 * \note
865 * This could be stored in as few as 7-bits, if necessary. If it is made
866 * smaller, add an assertion to \c ir_variable::allocate_state_slots to
867 * be safe.
868 */
869 uint16_t _num_state_slots;
870
871 public:
872 /**
873 * Initial binding point for a sampler, atomic, or UBO.
874 *
875 * For array types, this represents the binding point for the first element.
876 */
877 uint16_t binding;
878
879 /**
880 * Storage location of the base of this variable
881 *
882 * The precise meaning of this field depends on the nature of the variable.
883 *
884 * - Vertex shader input: one of the values from \c gl_vert_attrib.
885 * - Vertex shader output: one of the values from \c gl_varying_slot.
886 * - Geometry shader input: one of the values from \c gl_varying_slot.
887 * - Geometry shader output: one of the values from \c gl_varying_slot.
888 * - Fragment shader input: one of the values from \c gl_varying_slot.
889 * - Fragment shader output: one of the values from \c gl_frag_result.
890 * - Uniforms: Per-stage uniform slot number for default uniform block.
891 * - Uniforms: Index within the uniform block definition for UBO members.
892 * - Non-UBO Uniforms: explicit location until linking then reused to
893 * store uniform slot number.
894 * - Other: This field is not currently used.
895 *
896 * If the variable is a uniform, shader input, or shader output, and the
897 * slot has not been assigned, the value will be -1.
898 */
899 int location;
900
901 /**
902 * for glsl->tgsi/mesa IR we need to store the index into the
903 * parameters for uniforms, initially the code overloaded location
904 * but this causes problems with indirect samplers and AoA.
905 * This is assigned in _mesa_generate_parameters_list_for_uniforms.
906 */
907 int param_index;
908
909 /**
910 * Vertex stream output identifier.
911 *
912 * For packed outputs, bit 31 is set and bits [2*i+1,2*i] indicate the
913 * stream of the i-th component.
914 */
915 unsigned stream;
916
917 /**
918 * Atomic, transform feedback or block member offset.
919 */
920 unsigned offset;
921
922 /**
923 * Highest element accessed with a constant expression array index
924 *
925 * Not used for non-array variables. -1 is never accessed.
926 */
927 int max_array_access;
928
929 /**
930 * Transform feedback buffer.
931 */
932 unsigned xfb_buffer;
933
934 /**
935 * Transform feedback stride.
936 */
937 unsigned xfb_stride;
938
939 /**
940 * Allow (only) ir_variable direct access private members.
941 */
942 friend class ir_variable;
943 } data;
944
945 /**
946 * Value assigned in the initializer of a variable declared "const"
947 */
948 ir_constant *constant_value;
949
950 /**
951 * Constant expression assigned in the initializer of the variable
952 *
953 * \warning
954 * This field and \c ::constant_value are distinct. Even if the two fields
955 * refer to constants with the same value, they must point to separate
956 * objects.
957 */
958 ir_constant *constant_initializer;
959
960 private:
961 static const char *const warn_extension_table[];
962
963 union {
964 /**
965 * For variables which satisfy the is_interface_instance() predicate,
966 * this points to an array of integers such that if the ith member of
967 * the interface block is an array, max_ifc_array_access[i] is the
968 * maximum array element of that member that has been accessed. If the
969 * ith member of the interface block is not an array,
970 * max_ifc_array_access[i] is unused.
971 *
972 * For variables whose type is not an interface block, this pointer is
973 * NULL.
974 */
975 int *max_ifc_array_access;
976
977 /**
978 * Built-in state that backs this uniform
979 *
980 * Once set at variable creation, \c state_slots must remain invariant.
981 *
982 * If the variable is not a uniform, \c _num_state_slots will be zero
983 * and \c state_slots will be \c NULL.
984 */
985 ir_state_slot *state_slots;
986 } u;
987
988 /**
989 * For variables that are in an interface block or are an instance of an
990 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
991 *
992 * \sa ir_variable::location
993 */
994 const glsl_type *interface_type;
995
996 /**
997 * Name used for anonymous compiler temporaries
998 */
999 static const char tmp_name[];
1000
1001 public:
1002 /**
1003 * Should the construct keep names for ir_var_temporary variables?
1004 *
1005 * When this global is false, names passed to the constructor for
1006 * \c ir_var_temporary variables will be dropped. Instead, the variable will
1007 * be named "compiler_temp". This name will be in static storage.
1008 *
1009 * \warning
1010 * \b NEVER change the mode of an \c ir_var_temporary.
1011 *
1012 * \warning
1013 * This variable is \b not thread-safe. It is global, \b not
1014 * per-context. It begins life false. A context can, at some point, make
1015 * it true. From that point on, it will be true forever. This should be
1016 * okay since it will only be set true while debugging.
1017 */
1018 static bool temporaries_allocate_names;
1019 };
1020
1021 /**
1022 * A function that returns whether a built-in function is available in the
1023 * current shading language (based on version, ES or desktop, and extensions).
1024 */
1025 typedef bool (*builtin_available_predicate)(const _mesa_glsl_parse_state *);
1026
1027 enum ir_intrinsic_id {
1028 ir_intrinsic_invalid = 0,
1029
1030 /**
1031 * \name Generic intrinsics
1032 *
1033 * Each of these intrinsics has a specific version for shared variables and
1034 * SSBOs.
1035 */
1036 /*@{*/
1037 ir_intrinsic_generic_load,
1038 ir_intrinsic_generic_store,
1039 ir_intrinsic_generic_atomic_add,
1040 ir_intrinsic_generic_atomic_and,
1041 ir_intrinsic_generic_atomic_or,
1042 ir_intrinsic_generic_atomic_xor,
1043 ir_intrinsic_generic_atomic_min,
1044 ir_intrinsic_generic_atomic_max,
1045 ir_intrinsic_generic_atomic_exchange,
1046 ir_intrinsic_generic_atomic_comp_swap,
1047 /*@}*/
1048
1049 ir_intrinsic_atomic_counter_read,
1050 ir_intrinsic_atomic_counter_increment,
1051 ir_intrinsic_atomic_counter_predecrement,
1052 ir_intrinsic_atomic_counter_add,
1053 ir_intrinsic_atomic_counter_and,
1054 ir_intrinsic_atomic_counter_or,
1055 ir_intrinsic_atomic_counter_xor,
1056 ir_intrinsic_atomic_counter_min,
1057 ir_intrinsic_atomic_counter_max,
1058 ir_intrinsic_atomic_counter_exchange,
1059 ir_intrinsic_atomic_counter_comp_swap,
1060
1061 ir_intrinsic_image_load,
1062 ir_intrinsic_image_store,
1063 ir_intrinsic_image_atomic_add,
1064 ir_intrinsic_image_atomic_and,
1065 ir_intrinsic_image_atomic_or,
1066 ir_intrinsic_image_atomic_xor,
1067 ir_intrinsic_image_atomic_min,
1068 ir_intrinsic_image_atomic_max,
1069 ir_intrinsic_image_atomic_exchange,
1070 ir_intrinsic_image_atomic_comp_swap,
1071 ir_intrinsic_image_size,
1072 ir_intrinsic_image_samples,
1073 ir_intrinsic_image_atomic_inc_wrap,
1074 ir_intrinsic_image_atomic_dec_wrap,
1075 ir_intrinsic_image_sparse_load,
1076
1077 ir_intrinsic_memory_barrier,
1078 ir_intrinsic_shader_clock,
1079 ir_intrinsic_group_memory_barrier,
1080 ir_intrinsic_memory_barrier_atomic_counter,
1081 ir_intrinsic_memory_barrier_buffer,
1082 ir_intrinsic_memory_barrier_image,
1083 ir_intrinsic_memory_barrier_shared,
1084 ir_intrinsic_begin_invocation_interlock,
1085 ir_intrinsic_end_invocation_interlock,
1086
1087 ir_intrinsic_vote_all,
1088 ir_intrinsic_vote_any,
1089 ir_intrinsic_vote_eq,
1090 ir_intrinsic_ballot,
1091 ir_intrinsic_inverse_ballot,
1092 ir_intrinsic_ballot_bit_extract,
1093 ir_intrinsic_ballot_bit_count,
1094 ir_intrinsic_ballot_inclusive_bit_count,
1095 ir_intrinsic_ballot_exclusive_bit_count,
1096 ir_intrinsic_ballot_find_lsb,
1097 ir_intrinsic_ballot_find_msb,
1098 ir_intrinsic_read_invocation,
1099 ir_intrinsic_read_first_invocation,
1100
1101 ir_intrinsic_helper_invocation,
1102
1103 ir_intrinsic_is_sparse_texels_resident,
1104
1105 ir_intrinsic_subgroup_barrier,
1106 ir_intrinsic_subgroup_memory_barrier,
1107 ir_intrinsic_subgroup_memory_barrier_buffer,
1108 ir_intrinsic_subgroup_memory_barrier_shared,
1109 ir_intrinsic_subgroup_memory_barrier_image,
1110 ir_intrinsic_elect,
1111
1112 ir_intrinsic_shuffle,
1113 ir_intrinsic_shuffle_xor,
1114 ir_intrinsic_shuffle_up,
1115 ir_intrinsic_shuffle_down,
1116
1117 ir_intrinsic_reduce_add,
1118 ir_intrinsic_reduce_mul,
1119 ir_intrinsic_reduce_min,
1120 ir_intrinsic_reduce_max,
1121 ir_intrinsic_reduce_and,
1122 ir_intrinsic_reduce_or,
1123 ir_intrinsic_reduce_xor,
1124
1125 ir_intrinsic_inclusive_add,
1126 ir_intrinsic_inclusive_mul,
1127 ir_intrinsic_inclusive_min,
1128 ir_intrinsic_inclusive_max,
1129 ir_intrinsic_inclusive_and,
1130 ir_intrinsic_inclusive_or,
1131 ir_intrinsic_inclusive_xor,
1132
1133 ir_intrinsic_exclusive_add,
1134 ir_intrinsic_exclusive_mul,
1135 ir_intrinsic_exclusive_min,
1136 ir_intrinsic_exclusive_max,
1137 ir_intrinsic_exclusive_and,
1138 ir_intrinsic_exclusive_or,
1139 ir_intrinsic_exclusive_xor,
1140
1141 ir_intrinsic_clustered_add,
1142 ir_intrinsic_clustered_mul,
1143 ir_intrinsic_clustered_min,
1144 ir_intrinsic_clustered_max,
1145 ir_intrinsic_clustered_and,
1146 ir_intrinsic_clustered_or,
1147 ir_intrinsic_clustered_xor,
1148
1149 ir_intrinsic_quad_broadcast,
1150 ir_intrinsic_quad_swap_horizontal,
1151 ir_intrinsic_quad_swap_vertical,
1152 ir_intrinsic_quad_swap_diagonal,
1153 };
1154
1155 /*@{*/
1156 /**
1157 * The representation of a function instance; may be the full definition or
1158 * simply a prototype.
1159 */
1160 class ir_function_signature : public ir_instruction {
1161 /* An ir_function_signature will be part of the list of signatures in
1162 * an ir_function.
1163 */
1164 public:
1165 ir_function_signature(const glsl_type *return_type,
1166 builtin_available_predicate builtin_avail = NULL);
1167
1168 virtual ir_function_signature *clone(void *mem_ctx,
1169 struct hash_table *ht) const;
1170 ir_function_signature *clone_prototype(void *mem_ctx,
1171 struct hash_table *ht) const;
1172
accept(ir_visitor * v)1173 virtual void accept(ir_visitor *v)
1174 {
1175 v->visit(this);
1176 }
1177
1178 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1179
1180 /**
1181 * Attempt to evaluate this function as a constant expression,
1182 * given a list of the actual parameters and the variable context.
1183 * Returns NULL for non-built-ins.
1184 */
1185 ir_constant *constant_expression_value(void *mem_ctx,
1186 exec_list *actual_parameters,
1187 struct hash_table *variable_context);
1188
1189 /**
1190 * Get the name of the function for which this is a signature
1191 */
1192 const char *function_name() const;
1193
1194 /**
1195 * Get a handle to the function for which this is a signature
1196 *
1197 * There is no setter function, this function returns a \c const pointer,
1198 * and \c ir_function_signature::_function is private for a reason. The
1199 * only way to make a connection between a function and function signature
1200 * is via \c ir_function::add_signature. This helps ensure that certain
1201 * invariants (i.e., a function signature is in the list of signatures for
1202 * its \c _function) are met.
1203 *
1204 * \sa ir_function::add_signature
1205 */
function()1206 inline const class ir_function *function() const
1207 {
1208 return this->_function;
1209 }
1210
1211 /**
1212 * Check whether the qualifiers match between this signature's parameters
1213 * and the supplied parameter list. If not, returns the name of the first
1214 * parameter with mismatched qualifiers (for use in error messages).
1215 */
1216 const char *qualifiers_match(exec_list *params);
1217
1218 /**
1219 * Replace the current parameter list with the given one. This is useful
1220 * if the current information came from a prototype, and either has invalid
1221 * or missing parameter names.
1222 */
1223 void replace_parameters(exec_list *new_params);
1224
1225 /**
1226 * Function return type.
1227 *
1228 * \note The precision qualifier is stored separately in return_precision.
1229 */
1230 const struct glsl_type *return_type;
1231
1232 /**
1233 * List of ir_variable of function parameters.
1234 *
1235 * This represents the storage. The paramaters passed in a particular
1236 * call will be in ir_call::actual_paramaters.
1237 */
1238 struct exec_list parameters;
1239
1240 /** Whether or not this function has a body (which may be empty). */
1241 unsigned is_defined:1;
1242
1243 /*
1244 * Precision qualifier for the return type.
1245 *
1246 * See the comment for ir_variable_data::precision for more details.
1247 */
1248 unsigned return_precision:2;
1249
1250 /** Whether or not this function signature is a built-in. */
1251 bool is_builtin() const;
1252
1253 /**
1254 * Whether or not this function is an intrinsic to be implemented
1255 * by the driver.
1256 */
is_intrinsic()1257 inline bool is_intrinsic() const
1258 {
1259 return intrinsic_id != ir_intrinsic_invalid;
1260 }
1261
1262 /** Identifier for this intrinsic. */
1263 enum ir_intrinsic_id intrinsic_id;
1264
1265 /** Whether or not a built-in is available for this shader. */
1266 bool is_builtin_available(const _mesa_glsl_parse_state *state) const;
1267
1268 /** Body of instructions in the function. */
1269 struct exec_list body;
1270
1271 private:
1272 /**
1273 * A function pointer to a predicate that answers whether a built-in
1274 * function is available in the current shader. NULL if not a built-in.
1275 */
1276 builtin_available_predicate builtin_avail;
1277
1278 /** Function of which this signature is one overload. */
1279 class ir_function *_function;
1280
1281 /** Function signature of which this one is a prototype clone */
1282 const ir_function_signature *origin;
1283
1284 friend class ir_function;
1285
1286 /**
1287 * Helper function to run a list of instructions for constant
1288 * expression evaluation.
1289 *
1290 * The hash table represents the values of the visible variables.
1291 * There are no scoping issues because the table is indexed on
1292 * ir_variable pointers, not variable names.
1293 *
1294 * Returns false if the expression is not constant, true otherwise,
1295 * and the value in *result if result is non-NULL.
1296 */
1297 bool constant_expression_evaluate_expression_list(void *mem_ctx,
1298 const struct exec_list &body,
1299 struct hash_table *variable_context,
1300 ir_constant **result);
1301 };
1302
1303
1304 /**
1305 * Header for tracking multiple overloaded functions with the same name.
1306 * Contains a list of ir_function_signatures representing each of the
1307 * actual functions.
1308 */
1309 class ir_function : public ir_instruction {
1310 public:
1311 ir_function(const char *name);
1312
1313 virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;
1314
accept(ir_visitor * v)1315 virtual void accept(ir_visitor *v)
1316 {
1317 v->visit(this);
1318 }
1319
1320 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1321
add_signature(ir_function_signature * sig)1322 void add_signature(ir_function_signature *sig)
1323 {
1324 sig->_function = this;
1325 this->signatures.push_tail(sig);
1326 }
1327
1328 /**
1329 * Find a signature that matches a set of actual parameters, taking implicit
1330 * conversions into account. Also flags whether the match was exact.
1331 */
1332 ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,
1333 const exec_list *actual_param,
1334 bool has_implicit_conversions,
1335 bool has_implicit_int_to_uint_conversion,
1336 bool allow_builtins,
1337 bool *match_is_exact);
1338
1339 /**
1340 * Find a signature that matches a set of actual parameters, taking implicit
1341 * conversions into account.
1342 */
1343 ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,
1344 const exec_list *actual_param,
1345 bool has_implicit_conversions,
1346 bool has_implicit_int_to_uint_conversion,
1347 bool allow_builtins);
1348
1349 /**
1350 * Find a signature that exactly matches a set of actual parameters without
1351 * any implicit type conversions.
1352 */
1353 ir_function_signature *exact_matching_signature(_mesa_glsl_parse_state *state,
1354 const exec_list *actual_ps);
1355
1356 /**
1357 * Name of the function.
1358 */
1359 const char *name;
1360
1361 /** Whether or not this function has a signature that isn't a built-in. */
1362 bool has_user_signature();
1363
1364 /**
1365 * List of ir_function_signature for each overloaded function with this name.
1366 */
1367 struct exec_list signatures;
1368
1369 /**
1370 * is this function a subroutine type declaration
1371 * e.g. subroutine void type1(float arg1);
1372 */
1373 bool is_subroutine;
1374
1375 /**
1376 * is this function associated to a subroutine type
1377 * e.g. subroutine (type1, type2) function_name { function_body };
1378 * would have num_subroutine_types 2,
1379 * and pointers to the type1 and type2 types.
1380 */
1381 int num_subroutine_types;
1382 const struct glsl_type **subroutine_types;
1383
1384 int subroutine_index;
1385 };
1386
function_name()1387 inline const char *ir_function_signature::function_name() const
1388 {
1389 return this->_function->name;
1390 }
1391 /*@}*/
1392
1393
1394 /**
1395 * IR instruction representing high-level if-statements
1396 */
1397 class ir_if : public ir_instruction {
1398 public:
ir_if(ir_rvalue * condition)1399 ir_if(ir_rvalue *condition)
1400 : ir_instruction(ir_type_if), condition(condition)
1401 {
1402 }
1403
1404 virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
1405
accept(ir_visitor * v)1406 virtual void accept(ir_visitor *v)
1407 {
1408 v->visit(this);
1409 }
1410
1411 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1412
1413 ir_rvalue *condition;
1414 /** List of ir_instruction for the body of the then branch */
1415 exec_list then_instructions;
1416 /** List of ir_instruction for the body of the else branch */
1417 exec_list else_instructions;
1418 };
1419
1420
1421 /**
1422 * IR instruction representing a high-level loop structure.
1423 */
1424 class ir_loop : public ir_instruction {
1425 public:
1426 ir_loop();
1427
1428 virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
1429
accept(ir_visitor * v)1430 virtual void accept(ir_visitor *v)
1431 {
1432 v->visit(this);
1433 }
1434
1435 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1436
1437 /** List of ir_instruction that make up the body of the loop. */
1438 exec_list body_instructions;
1439 };
1440
1441
1442 class ir_assignment : public ir_instruction {
1443 public:
1444 ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs);
1445
1446 /**
1447 * Construct an assignment with an explicit write mask
1448 *
1449 * \note
1450 * Since a write mask is supplied, the LHS must already be a bare
1451 * \c ir_dereference. The cannot be any swizzles in the LHS.
1452 */
1453 ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, unsigned write_mask);
1454
1455 virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;
1456
1457 virtual ir_constant *constant_expression_value(void *mem_ctx,
1458 struct hash_table *variable_context = NULL);
1459
accept(ir_visitor * v)1460 virtual void accept(ir_visitor *v)
1461 {
1462 v->visit(this);
1463 }
1464
1465 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1466
1467 /**
1468 * Get a whole variable written by an assignment
1469 *
1470 * If the LHS of the assignment writes a whole variable, the variable is
1471 * returned. Otherwise \c NULL is returned. Examples of whole-variable
1472 * assignment are:
1473 *
1474 * - Assigning to a scalar
1475 * - Assigning to all components of a vector
1476 * - Whole array (or matrix) assignment
1477 * - Whole structure assignment
1478 */
1479 ir_variable *whole_variable_written();
1480
1481 /**
1482 * Set the LHS of an assignment
1483 */
1484 void set_lhs(ir_rvalue *lhs);
1485
1486 /**
1487 * Left-hand side of the assignment.
1488 *
1489 * This should be treated as read only. If you need to set the LHS of an
1490 * assignment, use \c ir_assignment::set_lhs.
1491 */
1492 ir_dereference *lhs;
1493
1494 /**
1495 * Value being assigned
1496 */
1497 ir_rvalue *rhs;
1498
1499 /**
1500 * Component mask written
1501 *
1502 * For non-vector types in the LHS, this field will be zero. For vector
1503 * types, a bit will be set for each component that is written. Note that
1504 * for \c vec2 and \c vec3 types only the lower bits will ever be set.
1505 *
1506 * A partially-set write mask means that each enabled channel gets
1507 * the value from a consecutive channel of the rhs. For example,
1508 * to write just .xyw of gl_FrontColor with color:
1509 *
1510 * (assign (constant bool (1)) (xyw)
1511 * (var_ref gl_FragColor)
1512 * (swiz xyw (var_ref color)))
1513 */
1514 unsigned write_mask:4;
1515 };
1516
1517 #include "ir_expression_operation.h"
1518
1519 extern const char *const ir_expression_operation_strings[ir_last_opcode + 1];
1520 extern const char *const ir_expression_operation_enum_strings[ir_last_opcode + 1];
1521
1522 class ir_expression : public ir_rvalue {
1523 public:
1524 ir_expression(int op, const struct glsl_type *type,
1525 ir_rvalue *op0, ir_rvalue *op1 = NULL,
1526 ir_rvalue *op2 = NULL, ir_rvalue *op3 = NULL);
1527
1528 /**
1529 * Constructor for unary operation expressions
1530 */
1531 ir_expression(int op, ir_rvalue *);
1532
1533 /**
1534 * Constructor for binary operation expressions
1535 */
1536 ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1);
1537
1538 /**
1539 * Constructor for ternary operation expressions
1540 */
1541 ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1, ir_rvalue *op2);
1542
1543 virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;
1544
1545 /**
1546 * Attempt to constant-fold the expression
1547 *
1548 * The "variable_context" hash table links ir_variable * to ir_constant *
1549 * that represent the variables' values. \c NULL represents an empty
1550 * context.
1551 *
1552 * If the expression cannot be constant folded, this method will return
1553 * \c NULL.
1554 */
1555 virtual ir_constant *constant_expression_value(void *mem_ctx,
1556 struct hash_table *variable_context = NULL);
1557
1558 /**
1559 * This is only here for ir_reader to used for testing purposes please use
1560 * the precomputed num_operands field if you need the number of operands.
1561 */
1562 static unsigned get_num_operands(ir_expression_operation);
1563
1564 /**
1565 * Return whether the expression operates on vectors horizontally.
1566 */
is_horizontal()1567 bool is_horizontal() const
1568 {
1569 return operation == ir_binop_all_equal ||
1570 operation == ir_binop_any_nequal ||
1571 operation == ir_binop_dot ||
1572 operation == ir_binop_vector_extract ||
1573 operation == ir_triop_vector_insert ||
1574 operation == ir_quadop_vector;
1575 }
1576
accept(ir_visitor * v)1577 virtual void accept(ir_visitor *v)
1578 {
1579 v->visit(this);
1580 }
1581
1582 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1583
1584 virtual ir_variable *variable_referenced() const;
1585
1586 /**
1587 * Determine the number of operands used by an expression
1588 */
init_num_operands()1589 void init_num_operands()
1590 {
1591 if (operation == ir_quadop_vector) {
1592 num_operands = this->type->vector_elements;
1593 } else {
1594 num_operands = get_num_operands(operation);
1595 }
1596 }
1597
1598 ir_expression_operation operation;
1599 ir_rvalue *operands[4];
1600 uint8_t num_operands;
1601 };
1602
1603
1604 /**
1605 * HIR instruction representing a high-level function call, containing a list
1606 * of parameters and returning a value in the supplied temporary.
1607 */
1608 class ir_call : public ir_instruction {
1609 public:
ir_call(ir_function_signature * callee,ir_dereference_variable * return_deref,exec_list * actual_parameters)1610 ir_call(ir_function_signature *callee,
1611 ir_dereference_variable *return_deref,
1612 exec_list *actual_parameters)
1613 : ir_instruction(ir_type_call), return_deref(return_deref), callee(callee), sub_var(NULL), array_idx(NULL)
1614 {
1615 assert(callee->return_type != NULL);
1616 actual_parameters->move_nodes_to(& this->actual_parameters);
1617 }
1618
ir_call(ir_function_signature * callee,ir_dereference_variable * return_deref,exec_list * actual_parameters,ir_variable * var,ir_rvalue * array_idx)1619 ir_call(ir_function_signature *callee,
1620 ir_dereference_variable *return_deref,
1621 exec_list *actual_parameters,
1622 ir_variable *var, ir_rvalue *array_idx)
1623 : ir_instruction(ir_type_call), return_deref(return_deref), callee(callee), sub_var(var), array_idx(array_idx)
1624 {
1625 assert(callee->return_type != NULL);
1626 actual_parameters->move_nodes_to(& this->actual_parameters);
1627 }
1628
1629 virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;
1630
1631 virtual ir_constant *constant_expression_value(void *mem_ctx,
1632 struct hash_table *variable_context = NULL);
1633
accept(ir_visitor * v)1634 virtual void accept(ir_visitor *v)
1635 {
1636 v->visit(this);
1637 }
1638
1639 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1640
1641 /**
1642 * Get the name of the function being called.
1643 */
callee_name()1644 const char *callee_name() const
1645 {
1646 return callee->function_name();
1647 }
1648
1649 /**
1650 * Generates an inline version of the function before @ir,
1651 * storing the return value in return_deref.
1652 */
1653 void generate_inline(ir_instruction *ir);
1654
1655 /**
1656 * Storage for the function's return value.
1657 * This must be NULL if the return type is void.
1658 */
1659 ir_dereference_variable *return_deref;
1660
1661 /**
1662 * The specific function signature being called.
1663 */
1664 ir_function_signature *callee;
1665
1666 /* List of ir_rvalue of paramaters passed in this call. */
1667 exec_list actual_parameters;
1668
1669 /*
1670 * ARB_shader_subroutine support -
1671 * the subroutine uniform variable and array index
1672 * rvalue to be used in the lowering pass later.
1673 */
1674 ir_variable *sub_var;
1675 ir_rvalue *array_idx;
1676 };
1677
1678
1679 /**
1680 * \name Jump-like IR instructions.
1681 *
1682 * These include \c break, \c continue, \c return, and \c discard.
1683 */
1684 /*@{*/
1685 class ir_jump : public ir_instruction {
1686 protected:
ir_jump(enum ir_node_type t)1687 ir_jump(enum ir_node_type t)
1688 : ir_instruction(t)
1689 {
1690 }
1691 };
1692
1693 class ir_return : public ir_jump {
1694 public:
ir_return()1695 ir_return()
1696 : ir_jump(ir_type_return), value(NULL)
1697 {
1698 }
1699
ir_return(ir_rvalue * value)1700 ir_return(ir_rvalue *value)
1701 : ir_jump(ir_type_return), value(value)
1702 {
1703 }
1704
1705 virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
1706
get_value()1707 ir_rvalue *get_value() const
1708 {
1709 return value;
1710 }
1711
accept(ir_visitor * v)1712 virtual void accept(ir_visitor *v)
1713 {
1714 v->visit(this);
1715 }
1716
1717 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1718
1719 ir_rvalue *value;
1720 };
1721
1722
1723 /**
1724 * Jump instructions used inside loops
1725 *
1726 * These include \c break and \c continue. The \c break within a loop is
1727 * different from the \c break within a switch-statement.
1728 *
1729 * \sa ir_switch_jump
1730 */
1731 class ir_loop_jump : public ir_jump {
1732 public:
1733 enum jump_mode {
1734 jump_break,
1735 jump_continue
1736 };
1737
ir_loop_jump(jump_mode mode)1738 ir_loop_jump(jump_mode mode)
1739 : ir_jump(ir_type_loop_jump)
1740 {
1741 this->mode = mode;
1742 }
1743
1744 virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
1745
accept(ir_visitor * v)1746 virtual void accept(ir_visitor *v)
1747 {
1748 v->visit(this);
1749 }
1750
1751 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1752
is_break()1753 bool is_break() const
1754 {
1755 return mode == jump_break;
1756 }
1757
is_continue()1758 bool is_continue() const
1759 {
1760 return mode == jump_continue;
1761 }
1762
1763 /** Mode selector for the jump instruction. */
1764 enum jump_mode mode;
1765 };
1766
1767 /**
1768 * IR instruction representing discard statements.
1769 */
1770 class ir_discard : public ir_jump {
1771 public:
ir_discard()1772 ir_discard()
1773 : ir_jump(ir_type_discard)
1774 {
1775 this->condition = NULL;
1776 }
1777
ir_discard(ir_rvalue * cond)1778 ir_discard(ir_rvalue *cond)
1779 : ir_jump(ir_type_discard)
1780 {
1781 this->condition = cond;
1782 }
1783
1784 virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
1785
accept(ir_visitor * v)1786 virtual void accept(ir_visitor *v)
1787 {
1788 v->visit(this);
1789 }
1790
1791 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1792
1793 ir_rvalue *condition;
1794 };
1795 /*@}*/
1796
1797
1798 /**
1799 * IR instruction representing demote statements from
1800 * GL_EXT_demote_to_helper_invocation.
1801 */
1802 class ir_demote : public ir_instruction {
1803 public:
ir_demote()1804 ir_demote()
1805 : ir_instruction(ir_type_demote)
1806 {
1807 }
1808
1809 virtual ir_demote *clone(void *mem_ctx, struct hash_table *ht) const;
1810
accept(ir_visitor * v)1811 virtual void accept(ir_visitor *v)
1812 {
1813 v->visit(this);
1814 }
1815
1816 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1817 };
1818
1819
1820 /**
1821 * Texture sampling opcodes used in ir_texture
1822 */
1823 enum ir_texture_opcode {
1824 ir_tex, /**< Regular texture look-up */
1825 ir_txb, /**< Texture look-up with LOD bias */
1826 ir_txl, /**< Texture look-up with explicit LOD */
1827 ir_txd, /**< Texture look-up with partial derivatives */
1828 ir_txf, /**< Texel fetch with explicit LOD */
1829 ir_txf_ms, /**< Multisample texture fetch */
1830 ir_txs, /**< Texture size */
1831 ir_lod, /**< Texture lod query */
1832 ir_tg4, /**< Texture gather */
1833 ir_query_levels, /**< Texture levels query */
1834 ir_texture_samples, /**< Texture samples query */
1835 ir_samples_identical, /**< Query whether all samples are definitely identical. */
1836 };
1837
1838
1839 /**
1840 * IR instruction to sample a texture
1841 *
1842 * The specific form of the IR instruction depends on the \c mode value
1843 * selected from \c ir_texture_opcodes. In the printed IR, these will
1844 * appear as:
1845 *
1846 * Texel offset (0 or an expression)
1847 * | Projection divisor
1848 * | | Shadow comparator
1849 * | | | Lod clamp
1850 * | | | |
1851 * v v v v
1852 * (tex <type> <sampler> <coordinate> <sparse> 0 1 ( ) ( ))
1853 * (txb <type> <sampler> <coordinate> <sparse> 0 1 ( ) ( ) <bias>)
1854 * (txl <type> <sampler> <coordinate> <sparse> 0 1 ( ) <lod>)
1855 * (txd <type> <sampler> <coordinate> <sparse> 0 1 ( ) ( ) (dPdx dPdy))
1856 * (txf <type> <sampler> <coordinate> <sparse> 0 <lod>)
1857 * (txf_ms
1858 * <type> <sampler> <coordinate> <sparse> <sample_index>)
1859 * (txs <type> <sampler> <lod>)
1860 * (lod <type> <sampler> <coordinate>)
1861 * (tg4 <type> <sampler> <coordinate> <sparse> <offset> <component>)
1862 * (query_levels <type> <sampler>)
1863 * (samples_identical <sampler> <coordinate>)
1864 */
1865 class ir_texture : public ir_rvalue {
1866 public:
1867 ir_texture(enum ir_texture_opcode op, bool sparse = false)
ir_rvalue(ir_type_texture)1868 : ir_rvalue(ir_type_texture),
1869 op(op), sampler(NULL), coordinate(NULL), projector(NULL),
1870 shadow_comparator(NULL), offset(NULL), clamp(NULL),
1871 is_sparse(sparse)
1872 {
1873 memset(&lod_info, 0, sizeof(lod_info));
1874 }
1875
1876 virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;
1877
1878 virtual ir_constant *constant_expression_value(void *mem_ctx,
1879 struct hash_table *variable_context = NULL);
1880
accept(ir_visitor * v)1881 virtual void accept(ir_visitor *v)
1882 {
1883 v->visit(this);
1884 }
1885
1886 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1887
1888 /**
1889 * Return a string representing the ir_texture_opcode.
1890 */
1891 const char *opcode_string();
1892
1893 /** Set the sampler and type. */
1894 void set_sampler(ir_dereference *sampler, const glsl_type *type);
1895
1896 enum ir_texture_opcode op;
1897
1898 /** Sampler to use for the texture access. */
1899 ir_dereference *sampler;
1900
1901 /** Texture coordinate to sample */
1902 ir_rvalue *coordinate;
1903
1904 /**
1905 * Value used for projective divide.
1906 *
1907 * If there is no projective divide (the common case), this will be
1908 * \c NULL. Optimization passes should check for this to point to a constant
1909 * of 1.0 and replace that with \c NULL.
1910 */
1911 ir_rvalue *projector;
1912
1913 /**
1914 * Coordinate used for comparison on shadow look-ups.
1915 *
1916 * If there is no shadow comparison, this will be \c NULL. For the
1917 * \c ir_txf opcode, this *must* be \c NULL.
1918 */
1919 ir_rvalue *shadow_comparator;
1920
1921 /** Texel offset. */
1922 ir_rvalue *offset;
1923
1924 /** Lod clamp. */
1925 ir_rvalue *clamp;
1926
1927 union {
1928 ir_rvalue *lod; /**< Floating point LOD */
1929 ir_rvalue *bias; /**< Floating point LOD bias */
1930 ir_rvalue *sample_index; /**< MSAA sample index */
1931 ir_rvalue *component; /**< Gather component selector */
1932 struct {
1933 ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
1934 ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
1935 } grad;
1936 } lod_info;
1937
1938 /* Whether a sparse texture */
1939 bool is_sparse;
1940 };
1941
1942
1943 struct ir_swizzle_mask {
1944 unsigned x:2;
1945 unsigned y:2;
1946 unsigned z:2;
1947 unsigned w:2;
1948
1949 /**
1950 * Number of components in the swizzle.
1951 */
1952 unsigned num_components:3;
1953
1954 /**
1955 * Does the swizzle contain duplicate components?
1956 *
1957 * L-value swizzles cannot contain duplicate components.
1958 */
1959 unsigned has_duplicates:1;
1960 };
1961
1962
1963 class ir_swizzle : public ir_rvalue {
1964 public:
1965 ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
1966 unsigned count);
1967
1968 ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
1969
1970 ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
1971
1972 virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;
1973
1974 virtual ir_constant *constant_expression_value(void *mem_ctx,
1975 struct hash_table *variable_context = NULL);
1976
1977 /**
1978 * Construct an ir_swizzle from the textual representation. Can fail.
1979 */
1980 static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
1981
accept(ir_visitor * v)1982 virtual void accept(ir_visitor *v)
1983 {
1984 v->visit(this);
1985 }
1986
1987 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
1988
is_lvalue(const struct _mesa_glsl_parse_state * state)1989 bool is_lvalue(const struct _mesa_glsl_parse_state *state) const
1990 {
1991 return val->is_lvalue(state) && !mask.has_duplicates;
1992 }
1993
1994 /**
1995 * Get the variable that is ultimately referenced by an r-value
1996 */
1997 virtual ir_variable *variable_referenced() const;
1998
1999 ir_rvalue *val;
2000 ir_swizzle_mask mask;
2001
2002 private:
2003 /**
2004 * Initialize the mask component of a swizzle
2005 *
2006 * This is used by the \c ir_swizzle constructors.
2007 */
2008 void init_mask(const unsigned *components, unsigned count);
2009 };
2010
2011
2012 class ir_dereference : public ir_rvalue {
2013 public:
2014 virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;
2015
2016 bool is_lvalue(const struct _mesa_glsl_parse_state *state) const;
2017
2018 /**
2019 * Get the variable that is ultimately referenced by an r-value
2020 */
2021 virtual ir_variable *variable_referenced() const = 0;
2022
2023 /**
2024 * Get the precision. This can either come from the eventual variable that
2025 * is dereferenced, or from a record member.
2026 */
2027 virtual int precision() const = 0;
2028
2029 protected:
ir_dereference(enum ir_node_type t)2030 ir_dereference(enum ir_node_type t)
2031 : ir_rvalue(t)
2032 {
2033 }
2034 };
2035
2036
2037 class ir_dereference_variable : public ir_dereference {
2038 public:
2039 ir_dereference_variable(ir_variable *var);
2040
2041 virtual ir_dereference_variable *clone(void *mem_ctx,
2042 struct hash_table *) const;
2043
2044 virtual ir_constant *constant_expression_value(void *mem_ctx,
2045 struct hash_table *variable_context = NULL);
2046
2047 /**
2048 * Get the variable that is ultimately referenced by an r-value
2049 */
variable_referenced()2050 virtual ir_variable *variable_referenced() const
2051 {
2052 return this->var;
2053 }
2054
precision()2055 virtual int precision() const
2056 {
2057 return this->var->data.precision;
2058 }
2059
whole_variable_referenced()2060 virtual ir_variable *whole_variable_referenced()
2061 {
2062 /* ir_dereference_variable objects always dereference the entire
2063 * variable. However, if this dereference is dereferenced by anything
2064 * else, the complete dereference chain is not a whole-variable
2065 * dereference. This method should only be called on the top most
2066 * ir_rvalue in a dereference chain.
2067 */
2068 return this->var;
2069 }
2070
accept(ir_visitor * v)2071 virtual void accept(ir_visitor *v)
2072 {
2073 v->visit(this);
2074 }
2075
2076 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2077
2078 /**
2079 * Object being dereferenced.
2080 */
2081 ir_variable *var;
2082 };
2083
2084
2085 class ir_dereference_array : public ir_dereference {
2086 public:
2087 ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
2088
2089 ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
2090
2091 virtual ir_dereference_array *clone(void *mem_ctx,
2092 struct hash_table *) const;
2093
2094 virtual ir_constant *constant_expression_value(void *mem_ctx,
2095 struct hash_table *variable_context = NULL);
2096 /**
2097 * Get the variable that is ultimately referenced by an r-value
2098 */
variable_referenced()2099 virtual ir_variable *variable_referenced() const
2100 {
2101 return this->array->variable_referenced();
2102 }
2103
precision()2104 virtual int precision() const
2105 {
2106 ir_dereference *deref = this->array->as_dereference();
2107
2108 if (deref == NULL)
2109 return GLSL_PRECISION_NONE;
2110 else
2111 return deref->precision();
2112 }
2113
accept(ir_visitor * v)2114 virtual void accept(ir_visitor *v)
2115 {
2116 v->visit(this);
2117 }
2118
2119 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2120
2121 ir_rvalue *array;
2122 ir_rvalue *array_index;
2123
2124 private:
2125 void set_array(ir_rvalue *value);
2126 };
2127
2128
2129 class ir_dereference_record : public ir_dereference {
2130 public:
2131 ir_dereference_record(ir_rvalue *value, const char *field);
2132
2133 ir_dereference_record(ir_variable *var, const char *field);
2134
2135 virtual ir_dereference_record *clone(void *mem_ctx,
2136 struct hash_table *) const;
2137
2138 virtual ir_constant *constant_expression_value(void *mem_ctx,
2139 struct hash_table *variable_context = NULL);
2140
2141 /**
2142 * Get the variable that is ultimately referenced by an r-value
2143 */
variable_referenced()2144 virtual ir_variable *variable_referenced() const
2145 {
2146 return this->record->variable_referenced();
2147 }
2148
precision()2149 virtual int precision() const
2150 {
2151 const glsl_struct_field *field = record->type->fields.structure + field_idx;
2152
2153 return field->precision;
2154 }
2155
accept(ir_visitor * v)2156 virtual void accept(ir_visitor *v)
2157 {
2158 v->visit(this);
2159 }
2160
2161 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2162
2163 ir_rvalue *record;
2164 int field_idx;
2165 };
2166
2167
2168 /**
2169 * Data stored in an ir_constant
2170 */
2171 union ir_constant_data {
2172 unsigned u[16];
2173 int i[16];
2174 float f[16];
2175 bool b[16];
2176 double d[16];
2177 uint16_t f16[16];
2178 uint16_t u16[16];
2179 int16_t i16[16];
2180 uint64_t u64[16];
2181 int64_t i64[16];
2182 };
2183
2184
2185 class ir_constant : public ir_rvalue {
2186 public:
2187 ir_constant(const struct glsl_type *type, const ir_constant_data *data);
2188 ir_constant(bool b, unsigned vector_elements=1);
2189 ir_constant(int16_t i16, unsigned vector_elements=1);
2190 ir_constant(uint16_t u16, unsigned vector_elements=1);
2191 ir_constant(unsigned int u, unsigned vector_elements=1);
2192 ir_constant(int i, unsigned vector_elements=1);
2193 ir_constant(float16_t f16, unsigned vector_elements=1);
2194 ir_constant(float f, unsigned vector_elements=1);
2195 ir_constant(double d, unsigned vector_elements=1);
2196 ir_constant(uint64_t u64, unsigned vector_elements=1);
2197 ir_constant(int64_t i64, unsigned vector_elements=1);
2198
2199 /**
2200 * Construct an ir_constant from a list of ir_constant values
2201 */
2202 ir_constant(const struct glsl_type *type, exec_list *values);
2203
2204 /**
2205 * Construct an ir_constant from a scalar component of another ir_constant
2206 *
2207 * The new \c ir_constant inherits the type of the component from the
2208 * source constant.
2209 *
2210 * \note
2211 * In the case of a matrix constant, the new constant is a scalar, \b not
2212 * a vector.
2213 */
2214 ir_constant(const ir_constant *c, unsigned i);
2215
2216 /**
2217 * Return a new ir_constant of the specified type containing all zeros.
2218 */
2219 static ir_constant *zero(void *mem_ctx, const glsl_type *type);
2220
2221 virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;
2222
2223 virtual ir_constant *constant_expression_value(void *mem_ctx,
2224 struct hash_table *variable_context = NULL);
2225
accept(ir_visitor * v)2226 virtual void accept(ir_visitor *v)
2227 {
2228 v->visit(this);
2229 }
2230
2231 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2232
2233 /**
2234 * Get a particular component of a constant as a specific type
2235 *
2236 * This is useful, for example, to get a value from an integer constant
2237 * as a float or bool. This appears frequently when constructors are
2238 * called with all constant parameters.
2239 */
2240 /*@{*/
2241 bool get_bool_component(unsigned i) const;
2242 float get_float_component(unsigned i) const;
2243 uint16_t get_float16_component(unsigned i) const;
2244 double get_double_component(unsigned i) const;
2245 int16_t get_int16_component(unsigned i) const;
2246 uint16_t get_uint16_component(unsigned i) const;
2247 int get_int_component(unsigned i) const;
2248 unsigned get_uint_component(unsigned i) const;
2249 int64_t get_int64_component(unsigned i) const;
2250 uint64_t get_uint64_component(unsigned i) const;
2251 /*@}*/
2252
2253 ir_constant *get_array_element(unsigned i) const;
2254
2255 ir_constant *get_record_field(int idx);
2256
2257 /**
2258 * Copy the values on another constant at a given offset.
2259 *
2260 * The offset is ignored for array or struct copies, it's only for
2261 * scalars or vectors into vectors or matrices.
2262 *
2263 * With identical types on both sides and zero offset it's clone()
2264 * without creating a new object.
2265 */
2266
2267 void copy_offset(ir_constant *src, int offset);
2268
2269 /**
2270 * Copy the values on another constant at a given offset and
2271 * following an assign-like mask.
2272 *
2273 * The mask is ignored for scalars.
2274 *
2275 * Note that this function only handles what assign can handle,
2276 * i.e. at most a vector as source and a column of a matrix as
2277 * destination.
2278 */
2279
2280 void copy_masked_offset(ir_constant *src, int offset, unsigned int mask);
2281
2282 /**
2283 * Determine whether a constant has the same value as another constant
2284 *
2285 * \sa ir_constant::is_zero, ir_constant::is_one,
2286 * ir_constant::is_negative_one
2287 */
2288 bool has_value(const ir_constant *) const;
2289
2290 /**
2291 * Return true if this ir_constant represents the given value.
2292 *
2293 * For vectors, this checks that each component is the given value.
2294 */
2295 virtual bool is_value(float f, int i) const;
2296 virtual bool is_zero() const;
2297 virtual bool is_one() const;
2298
2299 /**
2300 * Value of the constant.
2301 *
2302 * The field used to back the values supplied by the constant is determined
2303 * by the type associated with the \c ir_instruction. Constants may be
2304 * scalars, vectors, or matrices.
2305 */
2306 union ir_constant_data value;
2307
2308 /* Array elements and structure fields */
2309 ir_constant **const_elements;
2310
2311 private:
2312 /**
2313 * Parameterless constructor only used by the clone method
2314 */
2315 ir_constant(void);
2316 };
2317
2318 /**
2319 * IR instruction to emit a vertex in a geometry shader.
2320 */
2321 class ir_emit_vertex : public ir_instruction {
2322 public:
ir_emit_vertex(ir_rvalue * stream)2323 ir_emit_vertex(ir_rvalue *stream)
2324 : ir_instruction(ir_type_emit_vertex),
2325 stream(stream)
2326 {
2327 assert(stream);
2328 }
2329
accept(ir_visitor * v)2330 virtual void accept(ir_visitor *v)
2331 {
2332 v->visit(this);
2333 }
2334
clone(void * mem_ctx,struct hash_table * ht)2335 virtual ir_emit_vertex *clone(void *mem_ctx, struct hash_table *ht) const
2336 {
2337 return new(mem_ctx) ir_emit_vertex(this->stream->clone(mem_ctx, ht));
2338 }
2339
2340 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2341
stream_id()2342 int stream_id() const
2343 {
2344 return stream->as_constant()->value.i[0];
2345 }
2346
2347 ir_rvalue *stream;
2348 };
2349
2350 /**
2351 * IR instruction to complete the current primitive and start a new one in a
2352 * geometry shader.
2353 */
2354 class ir_end_primitive : public ir_instruction {
2355 public:
ir_end_primitive(ir_rvalue * stream)2356 ir_end_primitive(ir_rvalue *stream)
2357 : ir_instruction(ir_type_end_primitive),
2358 stream(stream)
2359 {
2360 assert(stream);
2361 }
2362
accept(ir_visitor * v)2363 virtual void accept(ir_visitor *v)
2364 {
2365 v->visit(this);
2366 }
2367
clone(void * mem_ctx,struct hash_table * ht)2368 virtual ir_end_primitive *clone(void *mem_ctx, struct hash_table *ht) const
2369 {
2370 return new(mem_ctx) ir_end_primitive(this->stream->clone(mem_ctx, ht));
2371 }
2372
2373 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2374
stream_id()2375 int stream_id() const
2376 {
2377 return stream->as_constant()->value.i[0];
2378 }
2379
2380 ir_rvalue *stream;
2381 };
2382
2383 /**
2384 * IR instruction for tessellation control and compute shader barrier.
2385 */
2386 class ir_barrier : public ir_instruction {
2387 public:
ir_barrier()2388 ir_barrier()
2389 : ir_instruction(ir_type_barrier)
2390 {
2391 }
2392
accept(ir_visitor * v)2393 virtual void accept(ir_visitor *v)
2394 {
2395 v->visit(this);
2396 }
2397
clone(void * mem_ctx,struct hash_table *)2398 virtual ir_barrier *clone(void *mem_ctx, struct hash_table *) const
2399 {
2400 return new(mem_ctx) ir_barrier();
2401 }
2402
2403 virtual ir_visitor_status accept(ir_hierarchical_visitor *);
2404 };
2405
2406 /*@}*/
2407
2408 /**
2409 * Apply a visitor to each IR node in a list
2410 */
2411 void
2412 visit_exec_list(exec_list *list, ir_visitor *visitor);
2413
2414 void
2415 visit_exec_list_safe(exec_list *list, ir_visitor *visitor);
2416
2417 /**
2418 * Validate invariants on each IR node in a list
2419 */
2420 void validate_ir_tree(exec_list *instructions);
2421
2422 /**
2423 * Detect whether an unlinked shader contains static recursion
2424 *
2425 * If the list of instructions is determined to contain static recursion,
2426 * \c _mesa_glsl_error will be called to emit error messages for each function
2427 * that is in the recursion cycle.
2428 */
2429 void
2430 detect_recursion_unlinked(struct _mesa_glsl_parse_state *state,
2431 exec_list *instructions);
2432
2433 /**
2434 * Make a clone of each IR instruction in a list
2435 *
2436 * \param in List of IR instructions that are to be cloned
2437 * \param out List to hold the cloned instructions
2438 */
2439 void
2440 clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);
2441
2442 extern void
2443 reparent_ir(exec_list *list, void *mem_ctx);
2444
2445 extern char *
2446 prototype_string(const glsl_type *return_type, const char *name,
2447 exec_list *parameters);
2448
2449 const char *
2450 mode_string(const ir_variable *var);
2451
2452 extern "C" {
2453 #endif /* __cplusplus */
2454
2455 extern void
2456 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state);
2457
2458 extern void
2459 _mesa_glsl_initialize_variables(struct exec_list *instructions,
2460 struct _mesa_glsl_parse_state *state);
2461
2462 extern void _mesa_print_ir(FILE *f, struct exec_list *instructions,
2463 struct _mesa_glsl_parse_state *state);
2464
2465 extern void
2466 fprint_ir(FILE *f, const void *instruction);
2467
2468 extern const struct gl_builtin_uniform_desc *
2469 _mesa_glsl_get_builtin_uniform_desc(const char *name);
2470
2471 #ifdef __cplusplus
2472 } /* extern "C" */
2473 #endif
2474
2475 enum mesa_prim
2476 gl_to_mesa_prim(GLenum prim);
2477
2478 #endif /* IR_H */
2479