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