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