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