/* -*- c++ -*- */1/*2* Copyright © 2010 Intel Corporation3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*/2324#ifndef IR_H25#define IR_H2627#include <stdio.h>28#include <stdlib.h>2930#include "util/ralloc.h"31#include "util/format/u_format.h"32#include "util/half_float.h"33#include "compiler/glsl_types.h"34#include "list.h"35#include "ir_visitor.h"36#include "ir_hierarchical_visitor.h"3738#ifdef __cplusplus3940/**41* \defgroup IR Intermediate representation nodes42*43* @{44*/4546/**47* Class tags48*49* Each concrete class derived from \c ir_instruction has a value in this50* enumerant. The value for the type is stored in \c ir_instruction::ir_type51* by the constructor. While using type tags is not very C++, it is extremely52* convenient. For example, during debugging you can simply inspect53* \c ir_instruction::ir_type to find out the actual type of the object.54*55* In addition, it is possible to use a switch-statement based on \c56* \c ir_instruction::ir_type to select different behavior for different object57* types. For functions that have only slight differences for several object58* types, this allows writing very straightforward, readable code.59*/60enum ir_node_type {61ir_type_dereference_array,62ir_type_dereference_record,63ir_type_dereference_variable,64ir_type_constant,65ir_type_expression,66ir_type_swizzle,67ir_type_texture,68ir_type_variable,69ir_type_assignment,70ir_type_call,71ir_type_function,72ir_type_function_signature,73ir_type_if,74ir_type_loop,75ir_type_loop_jump,76ir_type_return,77ir_type_discard,78ir_type_demote,79ir_type_emit_vertex,80ir_type_end_primitive,81ir_type_barrier,82ir_type_max, /**< maximum ir_type enum number, for validation */83ir_type_unset = ir_type_max84};858687/**88* Base class of all IR instructions89*/90class ir_instruction : public exec_node {91public:92enum ir_node_type ir_type;9394/**95* GCC 4.7+ and clang warn when deleting an ir_instruction unless96* there's a virtual destructor present. Because we almost97* universally use ralloc for our memory management of98* ir_instructions, the destructor doesn't need to do any work.99*/100virtual ~ir_instruction()101{102}103104/** ir_print_visitor helper for debugging. */105void print(void) const;106void fprint(FILE *f) const;107108virtual void accept(ir_visitor *) = 0;109virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;110virtual ir_instruction *clone(void *mem_ctx,111struct hash_table *ht) const = 0;112113bool is_rvalue() const114{115return ir_type == ir_type_dereference_array ||116ir_type == ir_type_dereference_record ||117ir_type == ir_type_dereference_variable ||118ir_type == ir_type_constant ||119ir_type == ir_type_expression ||120ir_type == ir_type_swizzle ||121ir_type == ir_type_texture;122}123124bool is_dereference() const125{126return ir_type == ir_type_dereference_array ||127ir_type == ir_type_dereference_record ||128ir_type == ir_type_dereference_variable;129}130131bool is_jump() const132{133return ir_type == ir_type_loop_jump ||134ir_type == ir_type_return ||135ir_type == ir_type_discard;136}137138/**139* \name IR instruction downcast functions140*141* These functions either cast the object to a derived class or return142* \c NULL if the object's type does not match the specified derived class.143* Additional downcast functions will be added as needed.144*/145/*@{*/146#define AS_BASE(TYPE) \147class ir_##TYPE *as_##TYPE() \148{ \149assume(this != NULL); \150return is_##TYPE() ? (ir_##TYPE *) this : NULL; \151} \152const class ir_##TYPE *as_##TYPE() const \153{ \154assume(this != NULL); \155return is_##TYPE() ? (ir_##TYPE *) this : NULL; \156}157158AS_BASE(rvalue)159AS_BASE(dereference)160AS_BASE(jump)161#undef AS_BASE162163#define AS_CHILD(TYPE) \164class ir_##TYPE * as_##TYPE() \165{ \166assume(this != NULL); \167return ir_type == ir_type_##TYPE ? (ir_##TYPE *) this : NULL; \168} \169const class ir_##TYPE * as_##TYPE() const \170{ \171assume(this != NULL); \172return ir_type == ir_type_##TYPE ? (const ir_##TYPE *) this : NULL; \173}174AS_CHILD(variable)175AS_CHILD(function)176AS_CHILD(dereference_array)177AS_CHILD(dereference_variable)178AS_CHILD(dereference_record)179AS_CHILD(expression)180AS_CHILD(loop)181AS_CHILD(assignment)182AS_CHILD(call)183AS_CHILD(return)184AS_CHILD(if)185AS_CHILD(swizzle)186AS_CHILD(texture)187AS_CHILD(constant)188AS_CHILD(discard)189#undef AS_CHILD190/*@}*/191192/**193* IR equality method: Return true if the referenced instruction would194* return the same value as this one.195*196* This intended to be used for CSE and algebraic optimizations, on rvalues197* in particular. No support for other instruction types (assignments,198* jumps, calls, etc.) is planned.199*/200virtual bool equals(const ir_instruction *ir,201enum ir_node_type ignore = ir_type_unset) const;202203protected:204ir_instruction(enum ir_node_type t)205: ir_type(t)206{207}208209private:210ir_instruction()211{212assert(!"Should not get here.");213}214};215216217/**218* The base class for all "values"/expression trees.219*/220class ir_rvalue : public ir_instruction {221public:222const struct glsl_type *type;223224virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const;225226virtual void accept(ir_visitor *v)227{228v->visit(this);229}230231virtual ir_visitor_status accept(ir_hierarchical_visitor *);232233virtual ir_constant *constant_expression_value(void *mem_ctx,234struct hash_table *variable_context = NULL);235236ir_rvalue *as_rvalue_to_saturate();237238virtual bool is_lvalue(const struct _mesa_glsl_parse_state * = NULL) const239{240return false;241}242243/**244* Get the variable that is ultimately referenced by an r-value245*/246virtual ir_variable *variable_referenced() const247{248return NULL;249}250251252/**253* If an r-value is a reference to a whole variable, get that variable254*255* \return256* Pointer to a variable that is completely dereferenced by the r-value. If257* the r-value is not a dereference or the dereference does not access the258* entire variable (i.e., it's just one array element, struct field), \c NULL259* is returned.260*/261virtual ir_variable *whole_variable_referenced()262{263return NULL;264}265266/**267* Determine if an r-value has the value zero268*269* The base implementation of this function always returns \c false. The270* \c ir_constant class over-rides this function to return \c true \b only271* for vector and scalar types that have all elements set to the value272* zero (or \c false for booleans).273*274* \sa ir_constant::has_value, ir_rvalue::is_one, ir_rvalue::is_negative_one275*/276virtual bool is_zero() const;277278/**279* Determine if an r-value has the value one280*281* The base implementation of this function always returns \c false. The282* \c ir_constant class over-rides this function to return \c true \b only283* for vector and scalar types that have all elements set to the value284* one (or \c true for booleans).285*286* \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_negative_one287*/288virtual bool is_one() const;289290/**291* Determine if an r-value has the value negative one292*293* The base implementation of this function always returns \c false. The294* \c ir_constant class over-rides this function to return \c true \b only295* for vector and scalar types that have all elements set to the value296* negative one. For boolean types, the result is always \c false.297*298* \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one299*/300virtual bool is_negative_one() const;301302/**303* Determine if an r-value is an unsigned integer constant which can be304* stored in 16 bits.305*306* \sa ir_constant::is_uint16_constant.307*/308virtual bool is_uint16_constant() const { return false; }309310/**311* Return a generic value of error_type.312*313* Allocation will be performed with 'mem_ctx' as ralloc owner.314*/315static ir_rvalue *error_value(void *mem_ctx);316317protected:318ir_rvalue(enum ir_node_type t);319};320321322/**323* Variable storage classes324*/325enum ir_variable_mode {326ir_var_auto = 0, /**< Function local variables and globals. */327ir_var_uniform, /**< Variable declared as a uniform. */328ir_var_shader_storage, /**< Variable declared as an ssbo. */329ir_var_shader_shared, /**< Variable declared as shared. */330ir_var_shader_in,331ir_var_shader_out,332ir_var_function_in,333ir_var_function_out,334ir_var_function_inout,335ir_var_const_in, /**< "in" param that must be a constant expression */336ir_var_system_value, /**< Ex: front-face, instance-id, etc. */337ir_var_temporary, /**< Temporary variable generated during compilation. */338ir_var_mode_count /**< Number of variable modes */339};340341/**342* Enum keeping track of how a variable was declared. For error checking of343* the gl_PerVertex redeclaration rules.344*/345enum ir_var_declaration_type {346/**347* Normal declaration (for most variables, this means an explicit348* declaration. Exception: temporaries are always implicitly declared, but349* they still use ir_var_declared_normally).350*351* Note: an ir_variable that represents a named interface block uses352* ir_var_declared_normally.353*/354ir_var_declared_normally = 0,355356/**357* Variable was explicitly declared (or re-declared) in an unnamed358* interface block.359*/360ir_var_declared_in_block,361362/**363* Variable is an implicitly declared built-in that has not been explicitly364* re-declared by the shader.365*/366ir_var_declared_implicitly,367368/**369* Variable is implicitly generated by the compiler and should not be370* visible via the API.371*/372ir_var_hidden,373};374375/**376* \brief Layout qualifiers for gl_FragDepth.377*378* The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared379* with a layout qualifier.380*/381enum ir_depth_layout {382ir_depth_layout_none, /**< No depth layout is specified. */383ir_depth_layout_any,384ir_depth_layout_greater,385ir_depth_layout_less,386ir_depth_layout_unchanged387};388389/**390* \brief Convert depth layout qualifier to string.391*/392const char*393depth_layout_string(ir_depth_layout layout);394395/**396* Description of built-in state associated with a uniform397*398* \sa ir_variable::state_slots399*/400struct ir_state_slot {401gl_state_index16 tokens[STATE_LENGTH];402int swizzle;403};404405406/**407* Get the string value for an interpolation qualifier408*409* \return The string that would be used in a shader to specify \c410* mode will be returned.411*412* This function is used to generate error messages of the form "shader413* uses %s interpolation qualifier", so in the case where there is no414* interpolation qualifier, it returns "no".415*416* This function should only be used on a shader input or output variable.417*/418const char *interpolation_string(unsigned interpolation);419420421class ir_variable : public ir_instruction {422public:423ir_variable(const struct glsl_type *, const char *, ir_variable_mode);424425virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;426427virtual void accept(ir_visitor *v)428{429v->visit(this);430}431432virtual ir_visitor_status accept(ir_hierarchical_visitor *);433434435/**436* Determine whether or not a variable is part of a uniform or437* shader storage block.438*/439inline bool is_in_buffer_block() const440{441return (this->data.mode == ir_var_uniform ||442this->data.mode == ir_var_shader_storage) &&443this->interface_type != NULL;444}445446/**447* Determine whether or not a variable is part of a shader storage block.448*/449inline bool is_in_shader_storage_block() const450{451return this->data.mode == ir_var_shader_storage &&452this->interface_type != NULL;453}454455/**456* Determine whether or not a variable is the declaration of an interface457* block458*459* For the first declaration below, there will be an \c ir_variable named460* "instance" whose type and whose instance_type will be the same461* \c glsl_type. For the second declaration, there will be an \c ir_variable462* named "f" whose type is float and whose instance_type is B2.463*464* "instance" is an interface instance variable, but "f" is not.465*466* uniform B1 {467* float f;468* } instance;469*470* uniform B2 {471* float f;472* };473*/474inline bool is_interface_instance() const475{476return this->type->without_array() == this->interface_type;477}478479/**480* Return whether this variable contains a bindless sampler/image.481*/482inline bool contains_bindless() const483{484if (!this->type->contains_sampler() && !this->type->contains_image())485return false;486487return this->data.bindless || this->data.mode != ir_var_uniform;488}489490/**491* Set this->interface_type on a newly created variable.492*/493void init_interface_type(const struct glsl_type *type)494{495assert(this->interface_type == NULL);496this->interface_type = type;497if (this->is_interface_instance()) {498this->u.max_ifc_array_access =499ralloc_array(this, int, type->length);500for (unsigned i = 0; i < type->length; i++) {501this->u.max_ifc_array_access[i] = -1;502}503}504}505506/**507* Change this->interface_type on a variable that previously had a508* different, but compatible, interface_type. This is used during linking509* to set the size of arrays in interface blocks.510*/511void change_interface_type(const struct glsl_type *type)512{513if (this->u.max_ifc_array_access != NULL) {514/* max_ifc_array_access has already been allocated, so make sure the515* new interface has the same number of fields as the old one.516*/517assert(this->interface_type->length == type->length);518}519this->interface_type = type;520}521522/**523* Change this->interface_type on a variable that previously had a524* different, and incompatible, interface_type. This is used during525* compilation to handle redeclaration of the built-in gl_PerVertex526* interface block.527*/528void reinit_interface_type(const struct glsl_type *type)529{530if (this->u.max_ifc_array_access != NULL) {531#ifndef NDEBUG532/* Redeclaring gl_PerVertex is only allowed if none of the built-ins533* it defines have been accessed yet; so it's safe to throw away the534* old max_ifc_array_access pointer, since all of its values are535* zero.536*/537for (unsigned i = 0; i < this->interface_type->length; i++)538assert(this->u.max_ifc_array_access[i] == -1);539#endif540ralloc_free(this->u.max_ifc_array_access);541this->u.max_ifc_array_access = NULL;542}543this->interface_type = NULL;544init_interface_type(type);545}546547const glsl_type *get_interface_type() const548{549return this->interface_type;550}551552enum glsl_interface_packing get_interface_type_packing() const553{554return this->interface_type->get_interface_packing();555}556/**557* Get the max_ifc_array_access pointer558*559* A "set" function is not needed because the array is dynamically allocated560* as necessary.561*/562inline int *get_max_ifc_array_access()563{564assert(this->data._num_state_slots == 0);565return this->u.max_ifc_array_access;566}567568inline unsigned get_num_state_slots() const569{570assert(!this->is_interface_instance()571|| this->data._num_state_slots == 0);572return this->data._num_state_slots;573}574575inline void set_num_state_slots(unsigned n)576{577assert(!this->is_interface_instance()578|| n == 0);579this->data._num_state_slots = n;580}581582inline ir_state_slot *get_state_slots()583{584return this->is_interface_instance() ? NULL : this->u.state_slots;585}586587inline const ir_state_slot *get_state_slots() const588{589return this->is_interface_instance() ? NULL : this->u.state_slots;590}591592inline ir_state_slot *allocate_state_slots(unsigned n)593{594assert(!this->is_interface_instance());595596this->u.state_slots = ralloc_array(this, ir_state_slot, n);597this->data._num_state_slots = 0;598599if (this->u.state_slots != NULL)600this->data._num_state_slots = n;601602return this->u.state_slots;603}604605inline bool is_interpolation_flat() const606{607return this->data.interpolation == INTERP_MODE_FLAT ||608this->type->contains_integer() ||609this->type->contains_double();610}611612inline bool is_name_ralloced() const613{614return this->name != ir_variable::tmp_name &&615this->name != this->name_storage;616}617618/**619* Enable emitting extension warnings for this variable620*/621void enable_extension_warning(const char *extension);622623/**624* Get the extension warning string for this variable625*626* If warnings are not enabled, \c NULL is returned.627*/628const char *get_extension_warning() const;629630/**631* Declared type of the variable632*/633const struct glsl_type *type;634635/**636* Declared name of the variable637*/638const char *name;639640private:641/**642* If the name length fits into name_storage, it's used, otherwise643* the name is ralloc'd. shader-db mining showed that 70% of variables644* fit here. This is a win over ralloc where only ralloc_header has645* 20 bytes on 64-bit (28 bytes with DEBUG), and we can also skip malloc.646*/647char name_storage[16];648649public:650struct ir_variable_data {651652/**653* Is the variable read-only?654*655* This is set for variables declared as \c const, shader inputs,656* and uniforms.657*/658unsigned read_only:1;659unsigned centroid:1;660unsigned sample:1;661unsigned patch:1;662/**663* Was an 'invariant' qualifier explicitly set in the shader?664*665* This is used to cross validate qualifiers.666*/667unsigned explicit_invariant:1;668/**669* Is the variable invariant?670*671* It can happen either by having the 'invariant' qualifier672* explicitly set in the shader or by being used in calculations673* of other invariant variables.674*/675unsigned invariant:1;676unsigned precise:1;677678/**679* Has this variable been used for reading or writing?680*681* Several GLSL semantic checks require knowledge of whether or not a682* variable has been used. For example, it is an error to redeclare a683* variable as invariant after it has been used.684*685* This is maintained in the ast_to_hir.cpp path and during linking,686* but not in Mesa's fixed function or ARB program paths.687*/688unsigned used:1;689690/**691* Has this variable been statically assigned?692*693* This answers whether the variable was assigned in any path of694* the shader during ast_to_hir. This doesn't answer whether it is695* still written after dead code removal, nor is it maintained in696* non-ast_to_hir.cpp (GLSL parsing) paths.697*/698unsigned assigned:1;699700/**701* When separate shader programs are enabled, only input/outputs between702* the stages of a multi-stage separate program can be safely removed703* from the shader interface. Other input/outputs must remains active.704*/705unsigned always_active_io:1;706707/**708* Enum indicating how the variable was declared. See709* ir_var_declaration_type.710*711* This is used to detect certain kinds of illegal variable redeclarations.712*/713unsigned how_declared:2;714715/**716* Storage class of the variable.717*718* \sa ir_variable_mode719*/720unsigned mode:4;721722/**723* Interpolation mode for shader inputs / outputs724*725* \sa glsl_interp_mode726*/727unsigned interpolation:2;728729/**730* Was the location explicitly set in the shader?731*732* If the location is explicitly set in the shader, it \b cannot be changed733* by the linker or by the API (e.g., calls to \c glBindAttribLocation have734* no effect).735*/736unsigned explicit_location:1;737unsigned explicit_index:1;738739/**740* Was an initial binding explicitly set in the shader?741*742* If so, constant_value contains an integer ir_constant representing the743* initial binding point.744*/745unsigned explicit_binding:1;746747/**748* Was an initial component explicitly set in the shader?749*/750unsigned explicit_component:1;751752/**753* Does this variable have an initializer?754*755* This is used by the linker to cross-validiate initializers of global756* variables.757*/758unsigned has_initializer:1;759760/**761* Is the initializer created by the compiler (glsl_zero_init)762*/763unsigned is_implicit_initializer:1;764765/**766* Is this variable a generic output or input that has not yet been matched767* up to a variable in another stage of the pipeline?768*769* This is used by the linker as scratch storage while assigning locations770* to generic inputs and outputs.771*/772unsigned is_unmatched_generic_inout:1;773774/**775* Is this varying used by transform feedback?776*777* This is used by the linker to decide if it's safe to pack the varying.778*/779unsigned is_xfb:1;780781/**782* Is this varying used only by transform feedback?783*784* This is used by the linker to decide if its safe to pack the varying.785*/786unsigned is_xfb_only:1;787788/**789* Was a transform feedback buffer set in the shader?790*/791unsigned explicit_xfb_buffer:1;792793/**794* Was a transform feedback offset set in the shader?795*/796unsigned explicit_xfb_offset:1;797798/**799* Was a transform feedback stride set in the shader?800*/801unsigned explicit_xfb_stride:1;802803/**804* If non-zero, then this variable may be packed along with other variables805* into a single varying slot, so this offset should be applied when806* accessing components. For example, an offset of 1 means that the x807* component of this variable is actually stored in component y of the808* location specified by \c location.809*/810unsigned location_frac:2;811812/**813* Layout of the matrix. Uses glsl_matrix_layout values.814*/815unsigned matrix_layout:2;816817/**818* Non-zero if this variable was created by lowering a named interface819* block.820*/821unsigned from_named_ifc_block:1;822823/**824* Non-zero if the variable must be a shader input. This is useful for825* constraints on function parameters.826*/827unsigned must_be_shader_input:1;828829/**830* Output index for dual source blending.831*832* \note833* The GLSL spec only allows the values 0 or 1 for the index in \b dual834* source blending.835*/836unsigned index:1;837838/**839* Precision qualifier.840*841* In desktop GLSL we do not care about precision qualifiers at all, in842* fact, the spec says that precision qualifiers are ignored.843*844* To make things easy, we make it so that this field is always845* GLSL_PRECISION_NONE on desktop shaders. This way all the variables846* have the same precision value and the checks we add in the compiler847* for this field will never break a desktop shader compile.848*/849unsigned precision:2;850851/**852* \brief Layout qualifier for gl_FragDepth.853*854* This is not equal to \c ir_depth_layout_none if and only if this855* variable is \c gl_FragDepth and a layout qualifier is specified.856*/857ir_depth_layout depth_layout:3;858859/**860* Memory qualifiers.861*/862unsigned memory_read_only:1; /**< "readonly" qualifier. */863unsigned memory_write_only:1; /**< "writeonly" qualifier. */864unsigned memory_coherent:1;865unsigned memory_volatile:1;866unsigned memory_restrict:1;867868/**869* ARB_shader_storage_buffer_object870*/871unsigned from_ssbo_unsized_array:1; /**< unsized array buffer variable. */872873unsigned implicit_sized_array:1;874875/**876* Whether this is a fragment shader output implicitly initialized with877* the previous contents of the specified render target at the878* framebuffer location corresponding to this shader invocation.879*/880unsigned fb_fetch_output:1;881882/**883* Non-zero if this variable is considered bindless as defined by884* ARB_bindless_texture.885*/886unsigned bindless:1;887888/**889* Non-zero if this variable is considered bound as defined by890* ARB_bindless_texture.891*/892unsigned bound:1;893894/**895* Non-zero if the variable shall not be implicitly converted during896* functions matching.897*/898unsigned implicit_conversion_prohibited:1;899900/**901* Emit a warning if this variable is accessed.902*/903private:904uint8_t warn_extension_index;905906public:907/**908* Image internal format if specified explicitly, otherwise909* PIPE_FORMAT_NONE.910*/911enum pipe_format image_format;912913private:914/**915* Number of state slots used916*917* \note918* This could be stored in as few as 7-bits, if necessary. If it is made919* smaller, add an assertion to \c ir_variable::allocate_state_slots to920* be safe.921*/922uint16_t _num_state_slots;923924public:925/**926* Initial binding point for a sampler, atomic, or UBO.927*928* For array types, this represents the binding point for the first element.929*/930uint16_t binding;931932/**933* Storage location of the base of this variable934*935* The precise meaning of this field depends on the nature of the variable.936*937* - Vertex shader input: one of the values from \c gl_vert_attrib.938* - Vertex shader output: one of the values from \c gl_varying_slot.939* - Geometry shader input: one of the values from \c gl_varying_slot.940* - Geometry shader output: one of the values from \c gl_varying_slot.941* - Fragment shader input: one of the values from \c gl_varying_slot.942* - Fragment shader output: one of the values from \c gl_frag_result.943* - Uniforms: Per-stage uniform slot number for default uniform block.944* - Uniforms: Index within the uniform block definition for UBO members.945* - Non-UBO Uniforms: explicit location until linking then reused to946* store uniform slot number.947* - Other: This field is not currently used.948*949* If the variable is a uniform, shader input, or shader output, and the950* slot has not been assigned, the value will be -1.951*/952int location;953954/**955* for glsl->tgsi/mesa IR we need to store the index into the956* parameters for uniforms, initially the code overloaded location957* but this causes problems with indirect samplers and AoA.958* This is assigned in _mesa_generate_parameters_list_for_uniforms.959*/960int param_index;961962/**963* Vertex stream output identifier.964*965* For packed outputs, bit 31 is set and bits [2*i+1,2*i] indicate the966* stream of the i-th component.967*/968unsigned stream;969970/**971* Atomic, transform feedback or block member offset.972*/973unsigned offset;974975/**976* Highest element accessed with a constant expression array index977*978* Not used for non-array variables. -1 is never accessed.979*/980int max_array_access;981982/**983* Transform feedback buffer.984*/985unsigned xfb_buffer;986987/**988* Transform feedback stride.989*/990unsigned xfb_stride;991992/**993* Allow (only) ir_variable direct access private members.994*/995friend class ir_variable;996} data;997998/**999* Value assigned in the initializer of a variable declared "const"1000*/1001ir_constant *constant_value;10021003/**1004* Constant expression assigned in the initializer of the variable1005*1006* \warning1007* This field and \c ::constant_value are distinct. Even if the two fields1008* refer to constants with the same value, they must point to separate1009* objects.1010*/1011ir_constant *constant_initializer;10121013private:1014static const char *const warn_extension_table[];10151016union {1017/**1018* For variables which satisfy the is_interface_instance() predicate,1019* this points to an array of integers such that if the ith member of1020* the interface block is an array, max_ifc_array_access[i] is the1021* maximum array element of that member that has been accessed. If the1022* ith member of the interface block is not an array,1023* max_ifc_array_access[i] is unused.1024*1025* For variables whose type is not an interface block, this pointer is1026* NULL.1027*/1028int *max_ifc_array_access;10291030/**1031* Built-in state that backs this uniform1032*1033* Once set at variable creation, \c state_slots must remain invariant.1034*1035* If the variable is not a uniform, \c _num_state_slots will be zero1036* and \c state_slots will be \c NULL.1037*/1038ir_state_slot *state_slots;1039} u;10401041/**1042* For variables that are in an interface block or are an instance of an1043* interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.1044*1045* \sa ir_variable::location1046*/1047const glsl_type *interface_type;10481049/**1050* Name used for anonymous compiler temporaries1051*/1052static const char tmp_name[];10531054public:1055/**1056* Should the construct keep names for ir_var_temporary variables?1057*1058* When this global is false, names passed to the constructor for1059* \c ir_var_temporary variables will be dropped. Instead, the variable will1060* be named "compiler_temp". This name will be in static storage.1061*1062* \warning1063* \b NEVER change the mode of an \c ir_var_temporary.1064*1065* \warning1066* This variable is \b not thread-safe. It is global, \b not1067* per-context. It begins life false. A context can, at some point, make1068* it true. From that point on, it will be true forever. This should be1069* okay since it will only be set true while debugging.1070*/1071static bool temporaries_allocate_names;1072};10731074/**1075* A function that returns whether a built-in function is available in the1076* current shading language (based on version, ES or desktop, and extensions).1077*/1078typedef bool (*builtin_available_predicate)(const _mesa_glsl_parse_state *);10791080#define MAKE_INTRINSIC_FOR_TYPE(op, t) \1081ir_intrinsic_generic_ ## op - ir_intrinsic_generic_load + ir_intrinsic_ ## t ## _ ## load10821083#define MAP_INTRINSIC_TO_TYPE(i, t) \1084ir_intrinsic_id(int(i) - int(ir_intrinsic_generic_load) + int(ir_intrinsic_ ## t ## _ ## load))10851086enum ir_intrinsic_id {1087ir_intrinsic_invalid = 0,10881089/**1090* \name Generic intrinsics1091*1092* Each of these intrinsics has a specific version for shared variables and1093* SSBOs.1094*/1095/*@{*/1096ir_intrinsic_generic_load,1097ir_intrinsic_generic_store,1098ir_intrinsic_generic_atomic_add,1099ir_intrinsic_generic_atomic_and,1100ir_intrinsic_generic_atomic_or,1101ir_intrinsic_generic_atomic_xor,1102ir_intrinsic_generic_atomic_min,1103ir_intrinsic_generic_atomic_max,1104ir_intrinsic_generic_atomic_exchange,1105ir_intrinsic_generic_atomic_comp_swap,1106/*@}*/11071108ir_intrinsic_atomic_counter_read,1109ir_intrinsic_atomic_counter_increment,1110ir_intrinsic_atomic_counter_predecrement,1111ir_intrinsic_atomic_counter_add,1112ir_intrinsic_atomic_counter_and,1113ir_intrinsic_atomic_counter_or,1114ir_intrinsic_atomic_counter_xor,1115ir_intrinsic_atomic_counter_min,1116ir_intrinsic_atomic_counter_max,1117ir_intrinsic_atomic_counter_exchange,1118ir_intrinsic_atomic_counter_comp_swap,11191120ir_intrinsic_image_load,1121ir_intrinsic_image_store,1122ir_intrinsic_image_atomic_add,1123ir_intrinsic_image_atomic_and,1124ir_intrinsic_image_atomic_or,1125ir_intrinsic_image_atomic_xor,1126ir_intrinsic_image_atomic_min,1127ir_intrinsic_image_atomic_max,1128ir_intrinsic_image_atomic_exchange,1129ir_intrinsic_image_atomic_comp_swap,1130ir_intrinsic_image_size,1131ir_intrinsic_image_samples,1132ir_intrinsic_image_atomic_inc_wrap,1133ir_intrinsic_image_atomic_dec_wrap,11341135ir_intrinsic_ssbo_load,1136ir_intrinsic_ssbo_store = MAKE_INTRINSIC_FOR_TYPE(store, ssbo),1137ir_intrinsic_ssbo_atomic_add = MAKE_INTRINSIC_FOR_TYPE(atomic_add, ssbo),1138ir_intrinsic_ssbo_atomic_and = MAKE_INTRINSIC_FOR_TYPE(atomic_and, ssbo),1139ir_intrinsic_ssbo_atomic_or = MAKE_INTRINSIC_FOR_TYPE(atomic_or, ssbo),1140ir_intrinsic_ssbo_atomic_xor = MAKE_INTRINSIC_FOR_TYPE(atomic_xor, ssbo),1141ir_intrinsic_ssbo_atomic_min = MAKE_INTRINSIC_FOR_TYPE(atomic_min, ssbo),1142ir_intrinsic_ssbo_atomic_max = MAKE_INTRINSIC_FOR_TYPE(atomic_max, ssbo),1143ir_intrinsic_ssbo_atomic_exchange = MAKE_INTRINSIC_FOR_TYPE(atomic_exchange, ssbo),1144ir_intrinsic_ssbo_atomic_comp_swap = MAKE_INTRINSIC_FOR_TYPE(atomic_comp_swap, ssbo),11451146ir_intrinsic_memory_barrier,1147ir_intrinsic_shader_clock,1148ir_intrinsic_group_memory_barrier,1149ir_intrinsic_memory_barrier_atomic_counter,1150ir_intrinsic_memory_barrier_buffer,1151ir_intrinsic_memory_barrier_image,1152ir_intrinsic_memory_barrier_shared,1153ir_intrinsic_begin_invocation_interlock,1154ir_intrinsic_end_invocation_interlock,11551156ir_intrinsic_vote_all,1157ir_intrinsic_vote_any,1158ir_intrinsic_vote_eq,1159ir_intrinsic_ballot,1160ir_intrinsic_read_invocation,1161ir_intrinsic_read_first_invocation,11621163ir_intrinsic_helper_invocation,11641165ir_intrinsic_shared_load,1166ir_intrinsic_shared_store = MAKE_INTRINSIC_FOR_TYPE(store, shared),1167ir_intrinsic_shared_atomic_add = MAKE_INTRINSIC_FOR_TYPE(atomic_add, shared),1168ir_intrinsic_shared_atomic_and = MAKE_INTRINSIC_FOR_TYPE(atomic_and, shared),1169ir_intrinsic_shared_atomic_or = MAKE_INTRINSIC_FOR_TYPE(atomic_or, shared),1170ir_intrinsic_shared_atomic_xor = MAKE_INTRINSIC_FOR_TYPE(atomic_xor, shared),1171ir_intrinsic_shared_atomic_min = MAKE_INTRINSIC_FOR_TYPE(atomic_min, shared),1172ir_intrinsic_shared_atomic_max = MAKE_INTRINSIC_FOR_TYPE(atomic_max, shared),1173ir_intrinsic_shared_atomic_exchange = MAKE_INTRINSIC_FOR_TYPE(atomic_exchange, shared),1174ir_intrinsic_shared_atomic_comp_swap = MAKE_INTRINSIC_FOR_TYPE(atomic_comp_swap, shared),1175};11761177/*@{*/1178/**1179* The representation of a function instance; may be the full definition or1180* simply a prototype.1181*/1182class ir_function_signature : public ir_instruction {1183/* An ir_function_signature will be part of the list of signatures in1184* an ir_function.1185*/1186public:1187ir_function_signature(const glsl_type *return_type,1188builtin_available_predicate builtin_avail = NULL);11891190virtual ir_function_signature *clone(void *mem_ctx,1191struct hash_table *ht) const;1192ir_function_signature *clone_prototype(void *mem_ctx,1193struct hash_table *ht) const;11941195virtual void accept(ir_visitor *v)1196{1197v->visit(this);1198}11991200virtual ir_visitor_status accept(ir_hierarchical_visitor *);12011202/**1203* Attempt to evaluate this function as a constant expression,1204* given a list of the actual parameters and the variable context.1205* Returns NULL for non-built-ins.1206*/1207ir_constant *constant_expression_value(void *mem_ctx,1208exec_list *actual_parameters,1209struct hash_table *variable_context);12101211/**1212* Get the name of the function for which this is a signature1213*/1214const char *function_name() const;12151216/**1217* Get a handle to the function for which this is a signature1218*1219* There is no setter function, this function returns a \c const pointer,1220* and \c ir_function_signature::_function is private for a reason. The1221* only way to make a connection between a function and function signature1222* is via \c ir_function::add_signature. This helps ensure that certain1223* invariants (i.e., a function signature is in the list of signatures for1224* its \c _function) are met.1225*1226* \sa ir_function::add_signature1227*/1228inline const class ir_function *function() const1229{1230return this->_function;1231}12321233/**1234* Check whether the qualifiers match between this signature's parameters1235* and the supplied parameter list. If not, returns the name of the first1236* parameter with mismatched qualifiers (for use in error messages).1237*/1238const char *qualifiers_match(exec_list *params);12391240/**1241* Replace the current parameter list with the given one. This is useful1242* if the current information came from a prototype, and either has invalid1243* or missing parameter names.1244*/1245void replace_parameters(exec_list *new_params);12461247/**1248* Function return type.1249*1250* \note The precision qualifier is stored separately in return_precision.1251*/1252const struct glsl_type *return_type;12531254/**1255* List of ir_variable of function parameters.1256*1257* This represents the storage. The paramaters passed in a particular1258* call will be in ir_call::actual_paramaters.1259*/1260struct exec_list parameters;12611262/** Whether or not this function has a body (which may be empty). */1263unsigned is_defined:1;12641265/*1266* Precision qualifier for the return type.1267*1268* See the comment for ir_variable_data::precision for more details.1269*/1270unsigned return_precision:2;12711272/** Whether or not this function signature is a built-in. */1273bool is_builtin() const;12741275/**1276* Whether or not this function is an intrinsic to be implemented1277* by the driver.1278*/1279inline bool is_intrinsic() const1280{1281return intrinsic_id != ir_intrinsic_invalid;1282}12831284/** Identifier for this intrinsic. */1285enum ir_intrinsic_id intrinsic_id;12861287/** Whether or not a built-in is available for this shader. */1288bool is_builtin_available(const _mesa_glsl_parse_state *state) const;12891290/** Body of instructions in the function. */1291struct exec_list body;12921293private:1294/**1295* A function pointer to a predicate that answers whether a built-in1296* function is available in the current shader. NULL if not a built-in.1297*/1298builtin_available_predicate builtin_avail;12991300/** Function of which this signature is one overload. */1301class ir_function *_function;13021303/** Function signature of which this one is a prototype clone */1304const ir_function_signature *origin;13051306friend class ir_function;13071308/**1309* Helper function to run a list of instructions for constant1310* expression evaluation.1311*1312* The hash table represents the values of the visible variables.1313* There are no scoping issues because the table is indexed on1314* ir_variable pointers, not variable names.1315*1316* Returns false if the expression is not constant, true otherwise,1317* and the value in *result if result is non-NULL.1318*/1319bool constant_expression_evaluate_expression_list(void *mem_ctx,1320const struct exec_list &body,1321struct hash_table *variable_context,1322ir_constant **result);1323};132413251326/**1327* Header for tracking multiple overloaded functions with the same name.1328* Contains a list of ir_function_signatures representing each of the1329* actual functions.1330*/1331class ir_function : public ir_instruction {1332public:1333ir_function(const char *name);13341335virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;13361337virtual void accept(ir_visitor *v)1338{1339v->visit(this);1340}13411342virtual ir_visitor_status accept(ir_hierarchical_visitor *);13431344void add_signature(ir_function_signature *sig)1345{1346sig->_function = this;1347this->signatures.push_tail(sig);1348}13491350/**1351* Find a signature that matches a set of actual parameters, taking implicit1352* conversions into account. Also flags whether the match was exact.1353*/1354ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,1355const exec_list *actual_param,1356bool allow_builtins,1357bool *match_is_exact);13581359/**1360* Find a signature that matches a set of actual parameters, taking implicit1361* conversions into account.1362*/1363ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,1364const exec_list *actual_param,1365bool allow_builtins);13661367/**1368* Find a signature that exactly matches a set of actual parameters without1369* any implicit type conversions.1370*/1371ir_function_signature *exact_matching_signature(_mesa_glsl_parse_state *state,1372const exec_list *actual_ps);13731374/**1375* Name of the function.1376*/1377const char *name;13781379/** Whether or not this function has a signature that isn't a built-in. */1380bool has_user_signature();13811382/**1383* List of ir_function_signature for each overloaded function with this name.1384*/1385struct exec_list signatures;13861387/**1388* is this function a subroutine type declaration1389* e.g. subroutine void type1(float arg1);1390*/1391bool is_subroutine;13921393/**1394* is this function associated to a subroutine type1395* e.g. subroutine (type1, type2) function_name { function_body };1396* would have num_subroutine_types 2,1397* and pointers to the type1 and type2 types.1398*/1399int num_subroutine_types;1400const struct glsl_type **subroutine_types;14011402int subroutine_index;1403};14041405inline const char *ir_function_signature::function_name() const1406{1407return this->_function->name;1408}1409/*@}*/141014111412/**1413* IR instruction representing high-level if-statements1414*/1415class ir_if : public ir_instruction {1416public:1417ir_if(ir_rvalue *condition)1418: ir_instruction(ir_type_if), condition(condition)1419{1420}14211422virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;14231424virtual void accept(ir_visitor *v)1425{1426v->visit(this);1427}14281429virtual ir_visitor_status accept(ir_hierarchical_visitor *);14301431ir_rvalue *condition;1432/** List of ir_instruction for the body of the then branch */1433exec_list then_instructions;1434/** List of ir_instruction for the body of the else branch */1435exec_list else_instructions;1436};143714381439/**1440* IR instruction representing a high-level loop structure.1441*/1442class ir_loop : public ir_instruction {1443public:1444ir_loop();14451446virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;14471448virtual void accept(ir_visitor *v)1449{1450v->visit(this);1451}14521453virtual ir_visitor_status accept(ir_hierarchical_visitor *);14541455/** List of ir_instruction that make up the body of the loop. */1456exec_list body_instructions;1457};145814591460class ir_assignment : public ir_instruction {1461public:1462ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition = NULL);14631464/**1465* Construct an assignment with an explicit write mask1466*1467* \note1468* Since a write mask is supplied, the LHS must already be a bare1469* \c ir_dereference. The cannot be any swizzles in the LHS.1470*/1471ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition,1472unsigned write_mask);14731474virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;14751476virtual ir_constant *constant_expression_value(void *mem_ctx,1477struct hash_table *variable_context = NULL);14781479virtual void accept(ir_visitor *v)1480{1481v->visit(this);1482}14831484virtual ir_visitor_status accept(ir_hierarchical_visitor *);14851486/**1487* Get a whole variable written by an assignment1488*1489* If the LHS of the assignment writes a whole variable, the variable is1490* returned. Otherwise \c NULL is returned. Examples of whole-variable1491* assignment are:1492*1493* - Assigning to a scalar1494* - Assigning to all components of a vector1495* - Whole array (or matrix) assignment1496* - Whole structure assignment1497*/1498ir_variable *whole_variable_written();14991500/**1501* Set the LHS of an assignment1502*/1503void set_lhs(ir_rvalue *lhs);15041505/**1506* Left-hand side of the assignment.1507*1508* This should be treated as read only. If you need to set the LHS of an1509* assignment, use \c ir_assignment::set_lhs.1510*/1511ir_dereference *lhs;15121513/**1514* Value being assigned1515*/1516ir_rvalue *rhs;15171518/**1519* Optional condition for the assignment.1520*/1521ir_rvalue *condition;152215231524/**1525* Component mask written1526*1527* For non-vector types in the LHS, this field will be zero. For vector1528* types, a bit will be set for each component that is written. Note that1529* for \c vec2 and \c vec3 types only the lower bits will ever be set.1530*1531* A partially-set write mask means that each enabled channel gets1532* the value from a consecutive channel of the rhs. For example,1533* to write just .xyw of gl_FrontColor with color:1534*1535* (assign (constant bool (1)) (xyw)1536* (var_ref gl_FragColor)1537* (swiz xyw (var_ref color)))1538*/1539unsigned write_mask:4;1540};15411542#include "ir_expression_operation.h"15431544extern const char *const ir_expression_operation_strings[ir_last_opcode + 1];1545extern const char *const ir_expression_operation_enum_strings[ir_last_opcode + 1];15461547class ir_expression : public ir_rvalue {1548public:1549ir_expression(int op, const struct glsl_type *type,1550ir_rvalue *op0, ir_rvalue *op1 = NULL,1551ir_rvalue *op2 = NULL, ir_rvalue *op3 = NULL);15521553/**1554* Constructor for unary operation expressions1555*/1556ir_expression(int op, ir_rvalue *);15571558/**1559* Constructor for binary operation expressions1560*/1561ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1);15621563/**1564* Constructor for ternary operation expressions1565*/1566ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1, ir_rvalue *op2);15671568virtual bool equals(const ir_instruction *ir,1569enum ir_node_type ignore = ir_type_unset) const;15701571virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;15721573/**1574* Attempt to constant-fold the expression1575*1576* The "variable_context" hash table links ir_variable * to ir_constant *1577* that represent the variables' values. \c NULL represents an empty1578* context.1579*1580* If the expression cannot be constant folded, this method will return1581* \c NULL.1582*/1583virtual ir_constant *constant_expression_value(void *mem_ctx,1584struct hash_table *variable_context = NULL);15851586/**1587* This is only here for ir_reader to used for testing purposes please use1588* the precomputed num_operands field if you need the number of operands.1589*/1590static unsigned get_num_operands(ir_expression_operation);15911592/**1593* Return whether the expression operates on vectors horizontally.1594*/1595bool is_horizontal() const1596{1597return operation == ir_binop_all_equal ||1598operation == ir_binop_any_nequal ||1599operation == ir_binop_dot ||1600operation == ir_binop_vector_extract ||1601operation == ir_triop_vector_insert ||1602operation == ir_binop_ubo_load ||1603operation == ir_quadop_vector;1604}16051606/**1607* Do a reverse-lookup to translate the given string into an operator.1608*/1609static ir_expression_operation get_operator(const char *);16101611virtual void accept(ir_visitor *v)1612{1613v->visit(this);1614}16151616virtual ir_visitor_status accept(ir_hierarchical_visitor *);16171618virtual ir_variable *variable_referenced() const;16191620/**1621* Determine the number of operands used by an expression1622*/1623void init_num_operands()1624{1625if (operation == ir_quadop_vector) {1626num_operands = this->type->vector_elements;1627} else {1628num_operands = get_num_operands(operation);1629}1630}16311632ir_expression_operation operation;1633ir_rvalue *operands[4];1634uint8_t num_operands;1635};163616371638/**1639* HIR instruction representing a high-level function call, containing a list1640* of parameters and returning a value in the supplied temporary.1641*/1642class ir_call : public ir_instruction {1643public:1644ir_call(ir_function_signature *callee,1645ir_dereference_variable *return_deref,1646exec_list *actual_parameters)1647: ir_instruction(ir_type_call), return_deref(return_deref), callee(callee), sub_var(NULL), array_idx(NULL)1648{1649assert(callee->return_type != NULL);1650actual_parameters->move_nodes_to(& this->actual_parameters);1651}16521653ir_call(ir_function_signature *callee,1654ir_dereference_variable *return_deref,1655exec_list *actual_parameters,1656ir_variable *var, ir_rvalue *array_idx)1657: ir_instruction(ir_type_call), return_deref(return_deref), callee(callee), sub_var(var), array_idx(array_idx)1658{1659assert(callee->return_type != NULL);1660actual_parameters->move_nodes_to(& this->actual_parameters);1661}16621663virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;16641665virtual ir_constant *constant_expression_value(void *mem_ctx,1666struct hash_table *variable_context = NULL);16671668virtual void accept(ir_visitor *v)1669{1670v->visit(this);1671}16721673virtual ir_visitor_status accept(ir_hierarchical_visitor *);16741675/**1676* Get the name of the function being called.1677*/1678const char *callee_name() const1679{1680return callee->function_name();1681}16821683/**1684* Generates an inline version of the function before @ir,1685* storing the return value in return_deref.1686*/1687void generate_inline(ir_instruction *ir);16881689/**1690* Storage for the function's return value.1691* This must be NULL if the return type is void.1692*/1693ir_dereference_variable *return_deref;16941695/**1696* The specific function signature being called.1697*/1698ir_function_signature *callee;16991700/* List of ir_rvalue of paramaters passed in this call. */1701exec_list actual_parameters;17021703/*1704* ARB_shader_subroutine support -1705* the subroutine uniform variable and array index1706* rvalue to be used in the lowering pass later.1707*/1708ir_variable *sub_var;1709ir_rvalue *array_idx;1710};171117121713/**1714* \name Jump-like IR instructions.1715*1716* These include \c break, \c continue, \c return, and \c discard.1717*/1718/*@{*/1719class ir_jump : public ir_instruction {1720protected:1721ir_jump(enum ir_node_type t)1722: ir_instruction(t)1723{1724}1725};17261727class ir_return : public ir_jump {1728public:1729ir_return()1730: ir_jump(ir_type_return), value(NULL)1731{1732}17331734ir_return(ir_rvalue *value)1735: ir_jump(ir_type_return), value(value)1736{1737}17381739virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;17401741ir_rvalue *get_value() const1742{1743return value;1744}17451746virtual void accept(ir_visitor *v)1747{1748v->visit(this);1749}17501751virtual ir_visitor_status accept(ir_hierarchical_visitor *);17521753ir_rvalue *value;1754};175517561757/**1758* Jump instructions used inside loops1759*1760* These include \c break and \c continue. The \c break within a loop is1761* different from the \c break within a switch-statement.1762*1763* \sa ir_switch_jump1764*/1765class ir_loop_jump : public ir_jump {1766public:1767enum jump_mode {1768jump_break,1769jump_continue1770};17711772ir_loop_jump(jump_mode mode)1773: ir_jump(ir_type_loop_jump)1774{1775this->mode = mode;1776}17771778virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;17791780virtual void accept(ir_visitor *v)1781{1782v->visit(this);1783}17841785virtual ir_visitor_status accept(ir_hierarchical_visitor *);17861787bool is_break() const1788{1789return mode == jump_break;1790}17911792bool is_continue() const1793{1794return mode == jump_continue;1795}17961797/** Mode selector for the jump instruction. */1798enum jump_mode mode;1799};18001801/**1802* IR instruction representing discard statements.1803*/1804class ir_discard : public ir_jump {1805public:1806ir_discard()1807: ir_jump(ir_type_discard)1808{1809this->condition = NULL;1810}18111812ir_discard(ir_rvalue *cond)1813: ir_jump(ir_type_discard)1814{1815this->condition = cond;1816}18171818virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;18191820virtual void accept(ir_visitor *v)1821{1822v->visit(this);1823}18241825virtual ir_visitor_status accept(ir_hierarchical_visitor *);18261827ir_rvalue *condition;1828};1829/*@}*/183018311832/**1833* IR instruction representing demote statements from1834* GL_EXT_demote_to_helper_invocation.1835*/1836class ir_demote : public ir_instruction {1837public:1838ir_demote()1839: ir_instruction(ir_type_demote)1840{1841}18421843virtual ir_demote *clone(void *mem_ctx, struct hash_table *ht) const;18441845virtual void accept(ir_visitor *v)1846{1847v->visit(this);1848}18491850virtual ir_visitor_status accept(ir_hierarchical_visitor *);1851};185218531854/**1855* Texture sampling opcodes used in ir_texture1856*/1857enum ir_texture_opcode {1858ir_tex, /**< Regular texture look-up */1859ir_txb, /**< Texture look-up with LOD bias */1860ir_txl, /**< Texture look-up with explicit LOD */1861ir_txd, /**< Texture look-up with partial derivatives */1862ir_txf, /**< Texel fetch with explicit LOD */1863ir_txf_ms, /**< Multisample texture fetch */1864ir_txs, /**< Texture size */1865ir_lod, /**< Texture lod query */1866ir_tg4, /**< Texture gather */1867ir_query_levels, /**< Texture levels query */1868ir_texture_samples, /**< Texture samples query */1869ir_samples_identical, /**< Query whether all samples are definitely identical. */1870};187118721873/**1874* IR instruction to sample a texture1875*1876* The specific form of the IR instruction depends on the \c mode value1877* selected from \c ir_texture_opcodes. In the printed IR, these will1878* appear as:1879*1880* Texel offset (0 or an expression)1881* | Projection divisor1882* | | Shadow comparator1883* | | |1884* v v v1885* (tex <type> <sampler> <coordinate> 0 1 ( ))1886* (txb <type> <sampler> <coordinate> 0 1 ( ) <bias>)1887* (txl <type> <sampler> <coordinate> 0 1 ( ) <lod>)1888* (txd <type> <sampler> <coordinate> 0 1 ( ) (dPdx dPdy))1889* (txf <type> <sampler> <coordinate> 0 <lod>)1890* (txf_ms1891* <type> <sampler> <coordinate> <sample_index>)1892* (txs <type> <sampler> <lod>)1893* (lod <type> <sampler> <coordinate>)1894* (tg4 <type> <sampler> <coordinate> <offset> <component>)1895* (query_levels <type> <sampler>)1896* (samples_identical <sampler> <coordinate>)1897*/1898class ir_texture : public ir_rvalue {1899public:1900ir_texture(enum ir_texture_opcode op)1901: ir_rvalue(ir_type_texture),1902op(op), sampler(NULL), coordinate(NULL), projector(NULL),1903shadow_comparator(NULL), offset(NULL)1904{1905memset(&lod_info, 0, sizeof(lod_info));1906}19071908virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;19091910virtual ir_constant *constant_expression_value(void *mem_ctx,1911struct hash_table *variable_context = NULL);19121913virtual void accept(ir_visitor *v)1914{1915v->visit(this);1916}19171918virtual ir_visitor_status accept(ir_hierarchical_visitor *);19191920virtual bool equals(const ir_instruction *ir,1921enum ir_node_type ignore = ir_type_unset) const;19221923/**1924* Return a string representing the ir_texture_opcode.1925*/1926const char *opcode_string();19271928/** Set the sampler and type. */1929void set_sampler(ir_dereference *sampler, const glsl_type *type);19301931/**1932* Do a reverse-lookup to translate a string into an ir_texture_opcode.1933*/1934static ir_texture_opcode get_opcode(const char *);19351936enum ir_texture_opcode op;19371938/** Sampler to use for the texture access. */1939ir_dereference *sampler;19401941/** Texture coordinate to sample */1942ir_rvalue *coordinate;19431944/**1945* Value used for projective divide.1946*1947* If there is no projective divide (the common case), this will be1948* \c NULL. Optimization passes should check for this to point to a constant1949* of 1.0 and replace that with \c NULL.1950*/1951ir_rvalue *projector;19521953/**1954* Coordinate used for comparison on shadow look-ups.1955*1956* If there is no shadow comparison, this will be \c NULL. For the1957* \c ir_txf opcode, this *must* be \c NULL.1958*/1959ir_rvalue *shadow_comparator;19601961/** Texel offset. */1962ir_rvalue *offset;19631964union {1965ir_rvalue *lod; /**< Floating point LOD */1966ir_rvalue *bias; /**< Floating point LOD bias */1967ir_rvalue *sample_index; /**< MSAA sample index */1968ir_rvalue *component; /**< Gather component selector */1969struct {1970ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */1971ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */1972} grad;1973} lod_info;1974};197519761977struct ir_swizzle_mask {1978unsigned x:2;1979unsigned y:2;1980unsigned z:2;1981unsigned w:2;19821983/**1984* Number of components in the swizzle.1985*/1986unsigned num_components:3;19871988/**1989* Does the swizzle contain duplicate components?1990*1991* L-value swizzles cannot contain duplicate components.1992*/1993unsigned has_duplicates:1;1994};199519961997class ir_swizzle : public ir_rvalue {1998public:1999ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,2000unsigned count);20012002ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);20032004ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);20052006virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;20072008virtual ir_constant *constant_expression_value(void *mem_ctx,2009struct hash_table *variable_context = NULL);20102011/**2012* Construct an ir_swizzle from the textual representation. Can fail.2013*/2014static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);20152016virtual void accept(ir_visitor *v)2017{2018v->visit(this);2019}20202021virtual ir_visitor_status accept(ir_hierarchical_visitor *);20222023virtual bool equals(const ir_instruction *ir,2024enum ir_node_type ignore = ir_type_unset) const;20252026bool is_lvalue(const struct _mesa_glsl_parse_state *state) const2027{2028return val->is_lvalue(state) && !mask.has_duplicates;2029}20302031/**2032* Get the variable that is ultimately referenced by an r-value2033*/2034virtual ir_variable *variable_referenced() const;20352036ir_rvalue *val;2037ir_swizzle_mask mask;20382039private:2040/**2041* Initialize the mask component of a swizzle2042*2043* This is used by the \c ir_swizzle constructors.2044*/2045void init_mask(const unsigned *components, unsigned count);2046};204720482049class ir_dereference : public ir_rvalue {2050public:2051virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;20522053bool is_lvalue(const struct _mesa_glsl_parse_state *state) const;20542055/**2056* Get the variable that is ultimately referenced by an r-value2057*/2058virtual ir_variable *variable_referenced() const = 0;20592060/**2061* Get the precision. This can either come from the eventual variable that2062* is dereferenced, or from a record member.2063*/2064virtual int precision() const = 0;20652066protected:2067ir_dereference(enum ir_node_type t)2068: ir_rvalue(t)2069{2070}2071};207220732074class ir_dereference_variable : public ir_dereference {2075public:2076ir_dereference_variable(ir_variable *var);20772078virtual ir_dereference_variable *clone(void *mem_ctx,2079struct hash_table *) const;20802081virtual ir_constant *constant_expression_value(void *mem_ctx,2082struct hash_table *variable_context = NULL);20832084virtual bool equals(const ir_instruction *ir,2085enum ir_node_type ignore = ir_type_unset) const;20862087/**2088* Get the variable that is ultimately referenced by an r-value2089*/2090virtual ir_variable *variable_referenced() const2091{2092return this->var;2093}20942095virtual int precision() const2096{2097return this->var->data.precision;2098}20992100virtual ir_variable *whole_variable_referenced()2101{2102/* ir_dereference_variable objects always dereference the entire2103* variable. However, if this dereference is dereferenced by anything2104* else, the complete dereference chain is not a whole-variable2105* dereference. This method should only be called on the top most2106* ir_rvalue in a dereference chain.2107*/2108return this->var;2109}21102111virtual void accept(ir_visitor *v)2112{2113v->visit(this);2114}21152116virtual ir_visitor_status accept(ir_hierarchical_visitor *);21172118/**2119* Object being dereferenced.2120*/2121ir_variable *var;2122};212321242125class ir_dereference_array : public ir_dereference {2126public:2127ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);21282129ir_dereference_array(ir_variable *var, ir_rvalue *array_index);21302131virtual ir_dereference_array *clone(void *mem_ctx,2132struct hash_table *) const;21332134virtual ir_constant *constant_expression_value(void *mem_ctx,2135struct hash_table *variable_context = NULL);21362137virtual bool equals(const ir_instruction *ir,2138enum ir_node_type ignore = ir_type_unset) const;21392140/**2141* Get the variable that is ultimately referenced by an r-value2142*/2143virtual ir_variable *variable_referenced() const2144{2145return this->array->variable_referenced();2146}21472148virtual int precision() const2149{2150ir_dereference *deref = this->array->as_dereference();21512152if (deref == NULL)2153return GLSL_PRECISION_NONE;2154else2155return deref->precision();2156}21572158virtual void accept(ir_visitor *v)2159{2160v->visit(this);2161}21622163virtual ir_visitor_status accept(ir_hierarchical_visitor *);21642165ir_rvalue *array;2166ir_rvalue *array_index;21672168private:2169void set_array(ir_rvalue *value);2170};217121722173class ir_dereference_record : public ir_dereference {2174public:2175ir_dereference_record(ir_rvalue *value, const char *field);21762177ir_dereference_record(ir_variable *var, const char *field);21782179virtual ir_dereference_record *clone(void *mem_ctx,2180struct hash_table *) const;21812182virtual ir_constant *constant_expression_value(void *mem_ctx,2183struct hash_table *variable_context = NULL);21842185/**2186* Get the variable that is ultimately referenced by an r-value2187*/2188virtual ir_variable *variable_referenced() const2189{2190return this->record->variable_referenced();2191}21922193virtual int precision() const2194{2195glsl_struct_field *field = record->type->fields.structure + field_idx;21962197return field->precision;2198}21992200virtual void accept(ir_visitor *v)2201{2202v->visit(this);2203}22042205virtual ir_visitor_status accept(ir_hierarchical_visitor *);22062207ir_rvalue *record;2208int field_idx;2209};221022112212/**2213* Data stored in an ir_constant2214*/2215union ir_constant_data {2216unsigned u[16];2217int i[16];2218float f[16];2219bool b[16];2220double d[16];2221uint16_t f16[16];2222uint16_t u16[16];2223int16_t i16[16];2224uint64_t u64[16];2225int64_t i64[16];2226};222722282229class ir_constant : public ir_rvalue {2230public:2231ir_constant(const struct glsl_type *type, const ir_constant_data *data);2232ir_constant(bool b, unsigned vector_elements=1);2233ir_constant(int16_t i16, unsigned vector_elements=1);2234ir_constant(uint16_t u16, unsigned vector_elements=1);2235ir_constant(unsigned int u, unsigned vector_elements=1);2236ir_constant(int i, unsigned vector_elements=1);2237ir_constant(float16_t f16, unsigned vector_elements=1);2238ir_constant(float f, unsigned vector_elements=1);2239ir_constant(double d, unsigned vector_elements=1);2240ir_constant(uint64_t u64, unsigned vector_elements=1);2241ir_constant(int64_t i64, unsigned vector_elements=1);22422243/**2244* Construct an ir_constant from a list of ir_constant values2245*/2246ir_constant(const struct glsl_type *type, exec_list *values);22472248/**2249* Construct an ir_constant from a scalar component of another ir_constant2250*2251* The new \c ir_constant inherits the type of the component from the2252* source constant.2253*2254* \note2255* In the case of a matrix constant, the new constant is a scalar, \b not2256* a vector.2257*/2258ir_constant(const ir_constant *c, unsigned i);22592260/**2261* Return a new ir_constant of the specified type containing all zeros.2262*/2263static ir_constant *zero(void *mem_ctx, const glsl_type *type);22642265virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;22662267virtual ir_constant *constant_expression_value(void *mem_ctx,2268struct hash_table *variable_context = NULL);22692270virtual void accept(ir_visitor *v)2271{2272v->visit(this);2273}22742275virtual ir_visitor_status accept(ir_hierarchical_visitor *);22762277virtual bool equals(const ir_instruction *ir,2278enum ir_node_type ignore = ir_type_unset) const;22792280/**2281* Get a particular component of a constant as a specific type2282*2283* This is useful, for example, to get a value from an integer constant2284* as a float or bool. This appears frequently when constructors are2285* called with all constant parameters.2286*/2287/*@{*/2288bool get_bool_component(unsigned i) const;2289float get_float_component(unsigned i) const;2290uint16_t get_float16_component(unsigned i) const;2291double get_double_component(unsigned i) const;2292int16_t get_int16_component(unsigned i) const;2293uint16_t get_uint16_component(unsigned i) const;2294int get_int_component(unsigned i) const;2295unsigned get_uint_component(unsigned i) const;2296int64_t get_int64_component(unsigned i) const;2297uint64_t get_uint64_component(unsigned i) const;2298/*@}*/22992300ir_constant *get_array_element(unsigned i) const;23012302ir_constant *get_record_field(int idx);23032304/**2305* Copy the values on another constant at a given offset.2306*2307* The offset is ignored for array or struct copies, it's only for2308* scalars or vectors into vectors or matrices.2309*2310* With identical types on both sides and zero offset it's clone()2311* without creating a new object.2312*/23132314void copy_offset(ir_constant *src, int offset);23152316/**2317* Copy the values on another constant at a given offset and2318* following an assign-like mask.2319*2320* The mask is ignored for scalars.2321*2322* Note that this function only handles what assign can handle,2323* i.e. at most a vector as source and a column of a matrix as2324* destination.2325*/23262327void copy_masked_offset(ir_constant *src, int offset, unsigned int mask);23282329/**2330* Determine whether a constant has the same value as another constant2331*2332* \sa ir_constant::is_zero, ir_constant::is_one,2333* ir_constant::is_negative_one2334*/2335bool has_value(const ir_constant *) const;23362337/**2338* Return true if this ir_constant represents the given value.2339*2340* For vectors, this checks that each component is the given value.2341*/2342virtual bool is_value(float f, int i) const;2343virtual bool is_zero() const;2344virtual bool is_one() const;2345virtual bool is_negative_one() const;23462347/**2348* Return true for constants that could be stored as 16-bit unsigned values.2349*2350* Note that this will return true even for signed integer ir_constants, as2351* long as the value is non-negative and fits in 16-bits.2352*/2353virtual bool is_uint16_constant() const;23542355/**2356* Value of the constant.2357*2358* The field used to back the values supplied by the constant is determined2359* by the type associated with the \c ir_instruction. Constants may be2360* scalars, vectors, or matrices.2361*/2362union ir_constant_data value;23632364/* Array elements and structure fields */2365ir_constant **const_elements;23662367private:2368/**2369* Parameterless constructor only used by the clone method2370*/2371ir_constant(void);2372};23732374/**2375* IR instruction to emit a vertex in a geometry shader.2376*/2377class ir_emit_vertex : public ir_instruction {2378public:2379ir_emit_vertex(ir_rvalue *stream)2380: ir_instruction(ir_type_emit_vertex),2381stream(stream)2382{2383assert(stream);2384}23852386virtual void accept(ir_visitor *v)2387{2388v->visit(this);2389}23902391virtual ir_emit_vertex *clone(void *mem_ctx, struct hash_table *ht) const2392{2393return new(mem_ctx) ir_emit_vertex(this->stream->clone(mem_ctx, ht));2394}23952396virtual ir_visitor_status accept(ir_hierarchical_visitor *);23972398int stream_id() const2399{2400return stream->as_constant()->value.i[0];2401}24022403ir_rvalue *stream;2404};24052406/**2407* IR instruction to complete the current primitive and start a new one in a2408* geometry shader.2409*/2410class ir_end_primitive : public ir_instruction {2411public:2412ir_end_primitive(ir_rvalue *stream)2413: ir_instruction(ir_type_end_primitive),2414stream(stream)2415{2416assert(stream);2417}24182419virtual void accept(ir_visitor *v)2420{2421v->visit(this);2422}24232424virtual ir_end_primitive *clone(void *mem_ctx, struct hash_table *ht) const2425{2426return new(mem_ctx) ir_end_primitive(this->stream->clone(mem_ctx, ht));2427}24282429virtual ir_visitor_status accept(ir_hierarchical_visitor *);24302431int stream_id() const2432{2433return stream->as_constant()->value.i[0];2434}24352436ir_rvalue *stream;2437};24382439/**2440* IR instruction for tessellation control and compute shader barrier.2441*/2442class ir_barrier : public ir_instruction {2443public:2444ir_barrier()2445: ir_instruction(ir_type_barrier)2446{2447}24482449virtual void accept(ir_visitor *v)2450{2451v->visit(this);2452}24532454virtual ir_barrier *clone(void *mem_ctx, struct hash_table *) const2455{2456return new(mem_ctx) ir_barrier();2457}24582459virtual ir_visitor_status accept(ir_hierarchical_visitor *);2460};24612462/*@}*/24632464/**2465* Apply a visitor to each IR node in a list2466*/2467void2468visit_exec_list(exec_list *list, ir_visitor *visitor);24692470/**2471* Validate invariants on each IR node in a list2472*/2473void validate_ir_tree(exec_list *instructions);24742475struct _mesa_glsl_parse_state;2476struct gl_shader_program;24772478/**2479* Detect whether an unlinked shader contains static recursion2480*2481* If the list of instructions is determined to contain static recursion,2482* \c _mesa_glsl_error will be called to emit error messages for each function2483* that is in the recursion cycle.2484*/2485void2486detect_recursion_unlinked(struct _mesa_glsl_parse_state *state,2487exec_list *instructions);24882489/**2490* Detect whether a linked shader contains static recursion2491*2492* If the list of instructions is determined to contain static recursion,2493* \c link_error_printf will be called to emit error messages for each function2494* that is in the recursion cycle. In addition,2495* \c gl_shader_program::LinkStatus will be set to false.2496*/2497void2498detect_recursion_linked(struct gl_shader_program *prog,2499exec_list *instructions);25002501/**2502* Make a clone of each IR instruction in a list2503*2504* \param in List of IR instructions that are to be cloned2505* \param out List to hold the cloned instructions2506*/2507void2508clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);25092510extern void2511_mesa_glsl_initialize_variables(exec_list *instructions,2512struct _mesa_glsl_parse_state *state);25132514extern void2515reparent_ir(exec_list *list, void *mem_ctx);25162517extern void2518do_set_program_inouts(exec_list *instructions, struct gl_program *prog,2519gl_shader_stage shader_stage);25202521extern char *2522prototype_string(const glsl_type *return_type, const char *name,2523exec_list *parameters);25242525const char *2526mode_string(const ir_variable *var);25272528/**2529* Built-in / reserved GL variables names start with "gl_"2530*/2531static inline bool2532is_gl_identifier(const char *s)2533{2534return s && s[0] == 'g' && s[1] == 'l' && s[2] == '_';2535}25362537extern "C" {2538#endif /* __cplusplus */25392540extern void _mesa_print_ir(FILE *f, struct exec_list *instructions,2541struct _mesa_glsl_parse_state *state);25422543extern void2544fprint_ir(FILE *f, const void *instruction);25452546extern const struct gl_builtin_uniform_desc *2547_mesa_glsl_get_builtin_uniform_desc(const char *name);25482549#ifdef __cplusplus2550} /* extern "C" */2551#endif25522553unsigned2554vertices_per_prim(GLenum prim);25552556#endif /* IR_H */255725582559