3 * Copyright © 2010 Intel Corporation
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:
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
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.
37 #include "ir_visitor.h"
38 #include "ir_hierarchical_visitor.h"
41 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
49 * Base class of all IR instructions
51 class ir_instruction
: public exec_node
{
53 const struct glsl_type
*type
;
55 class ir_constant
*constant_expression_value();
57 /** ir_print_visitor helper for debugging. */
58 void print(void) const;
60 virtual void accept(ir_visitor
*) = 0;
61 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*) = 0;
62 virtual ir_instruction
*clone(struct hash_table
*ht
) const = 0;
65 * \name IR instruction downcast functions
67 * These functions either cast the object to a derived class or return
68 * \c NULL if the object's type does not match the specified derived class.
69 * Additional downcast functions will be added as needed.
72 virtual class ir_variable
* as_variable() { return NULL
; }
73 virtual class ir_function
* as_function() { return NULL
; }
74 virtual class ir_dereference
* as_dereference() { return NULL
; }
75 virtual class ir_dereference_array
* as_dereference_array() { return NULL
; }
76 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
77 virtual class ir_loop
* as_loop() { return NULL
; }
78 virtual class ir_assignment
* as_assignment() { return NULL
; }
79 virtual class ir_call
* as_call() { return NULL
; }
80 virtual class ir_return
* as_return() { return NULL
; }
81 virtual class ir_if
* as_if() { return NULL
; }
82 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
83 virtual class ir_constant
* as_constant() { return NULL
; }
94 class ir_rvalue
: public ir_instruction
{
96 virtual ir_rvalue
* as_rvalue()
101 virtual bool is_lvalue()
107 * Get the variable that is ultimately referenced by an r-value
109 virtual ir_variable
*variable_referenced()
116 * If an r-value is a reference to a whole variable, get that variable
119 * Pointer to a variable that is completely dereferenced by the r-value. If
120 * the r-value is not a dereference or the dereference does not access the
121 * entire variable (i.e., it's just one array element, struct field), \c NULL
124 virtual ir_variable
*whole_variable_referenced()
137 enum ir_variable_mode
{
145 enum ir_variable_interpolation
{
152 class ir_variable
: public ir_instruction
{
154 ir_variable(const struct glsl_type
*, const char *);
156 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
158 virtual ir_variable
*as_variable()
163 virtual void accept(ir_visitor
*v
)
168 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
172 * Get the string value for the interpolation qualifier
175 * If none of \c shader_in or \c shader_out is set, an empty string will
176 * be returned. Otherwise the string that would be used in a shader to
177 * specify \c mode will be returned.
179 const char *interpolation_string() const;
182 * Calculate the number of slots required to hold this variable
184 * This is used to determine how many uniform or varying locations a variable
185 * occupies. The count is in units of floating point components.
187 unsigned component_slots() const;
192 * Highest element accessed with a constant expression array index
194 * Not used for non-array variables.
196 unsigned max_array_access
;
198 unsigned read_only
:1;
200 unsigned invariant
:1;
201 /** If the variable is initialized outside of the scope of the shader */
202 unsigned shader_in
:1;
204 * If the variable value is later used outside of the scope of the shader.
206 unsigned shader_out
:1;
209 unsigned interpolation
:2;
212 * Flag that the whole array is assignable
214 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
215 * equality). This flag enables this behavior.
217 unsigned array_lvalue
:1;
220 * Storage location of the base of this variable
222 * The precise meaning of this field depends on the nature of the variable.
224 * - Vertex shader input: one of the values from \c gl_vert_attrib.
225 * - Vertex shader output: one of the values from \c gl_vert_result.
226 * - Fragment shader input: one of the values from \c gl_frag_attrib.
227 * - Fragment shader output: one of the values from \c gl_frag_result.
228 * - Uniforms: Per-stage uniform slot number.
229 * - Other: This field is not currently used.
231 * If the variable is a uniform, shader input, or shader output, and the
232 * slot has not been assigned, the value will be -1.
237 * Emit a warning if this variable is accessed.
239 const char *warn_extension
;
242 * Value assigned in the initializer of a variable declared "const"
244 ir_constant
*constant_value
;
250 * The representation of a function instance; may be the full definition or
251 * simply a prototype.
253 class ir_function_signature
: public ir_instruction
{
254 /* An ir_function_signature will be part of the list of signatures in
258 ir_function_signature(const glsl_type
*return_type
);
260 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
262 virtual void accept(ir_visitor
*v
)
267 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
270 * Get the name of the function for which this is a signature
272 const char *function_name() const;
275 * Check whether the qualifiers match between this signature's parameters
276 * and the supplied parameter list. If not, returns the name of the first
277 * parameter with mismatched qualifiers (for use in error messages).
279 const char *qualifiers_match(exec_list
*params
);
282 * Replace the current parameter list with the given one. This is useful
283 * if the current information came from a prototype, and either has invalid
284 * or missing parameter names.
286 void replace_parameters(exec_list
*new_params
);
289 * Function return type.
291 * \note This discards the optional precision qualifier.
293 const struct glsl_type
*return_type
;
296 * List of ir_variable of function parameters.
298 * This represents the storage. The paramaters passed in a particular
299 * call will be in ir_call::actual_paramaters.
301 struct exec_list parameters
;
303 /** Whether or not this function has a body (which may be empty). */
304 unsigned is_defined
:1;
306 /** Body of instructions in the function. */
307 struct exec_list body
;
310 /** Function of which this signature is one overload. */
311 class ir_function
*function
;
313 friend class ir_function
;
318 * Header for tracking multiple overloaded functions with the same name.
319 * Contains a list of ir_function_signatures representing each of the
322 class ir_function
: public ir_instruction
{
324 ir_function(const char *name
);
326 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
328 virtual ir_function
*as_function()
333 virtual void accept(ir_visitor
*v
)
338 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
340 void add_signature(ir_function_signature
*sig
)
342 sig
->function
= this;
343 signatures
.push_tail(sig
);
347 * Get an iterator for the set of function signatures
349 exec_list_iterator
iterator()
351 return signatures
.iterator();
355 * Find a signature that matches a set of actual parameters, taking implicit
356 * conversions into account.
358 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
361 * Find a signature that exactly matches a set of actual parameters without
362 * any implicit type conversions.
364 ir_function_signature
*exact_matching_signature(exec_list
*actual_ps
);
367 * Name of the function.
373 * List of ir_function_signature for each overloaded function with this name.
375 struct exec_list signatures
;
378 inline const char *ir_function_signature::function_name() const
380 return function
->name
;
386 * IR instruction representing high-level if-statements
388 class ir_if
: public ir_instruction
{
390 ir_if(ir_rvalue
*condition
)
391 : condition(condition
)
396 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
398 virtual ir_if
*as_if()
403 virtual void accept(ir_visitor
*v
)
408 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
410 ir_rvalue
*condition
;
411 /** List of ir_instruction for the body of the then branch */
412 exec_list then_instructions
;
413 /** List of ir_instruction for the body of the else branch */
414 exec_list else_instructions
;
419 * IR instruction representing a high-level loop structure.
421 class ir_loop
: public ir_instruction
{
423 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
428 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
430 virtual void accept(ir_visitor
*v
)
435 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
437 virtual ir_loop
*as_loop()
443 * Get an iterator for the instructions of the loop body
445 exec_list_iterator
iterator()
447 return body_instructions
.iterator();
450 /** List of ir_instruction that make up the body of the loop. */
451 exec_list body_instructions
;
454 * \name Loop counter and controls
459 ir_rvalue
*increment
;
460 ir_variable
*counter
;
465 class ir_assignment
: public ir_rvalue
{
467 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
469 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
471 virtual void accept(ir_visitor
*v
)
476 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
478 virtual ir_assignment
* as_assignment()
484 * Left-hand side of the assignment.
489 * Value being assigned
494 * Optional condition for the assignment.
496 ir_rvalue
*condition
;
499 /* Update ir_expression::num_operands() and operator_strs when
500 * updating this list.
502 enum ir_expression_operation
{
515 ir_unop_f2i
, /**< Float-to-integer conversion. */
516 ir_unop_i2f
, /**< Integer-to-float conversion. */
517 ir_unop_f2b
, /**< Float-to-boolean conversion */
518 ir_unop_b2f
, /**< Boolean-to-float conversion */
519 ir_unop_i2b
, /**< int-to-boolean conversion */
520 ir_unop_b2i
, /**< Boolean-to-int conversion */
521 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
524 * \name Unary floating-point rounding operations.
533 * \name Trigonometric operations.
541 * \name Partial derivatives.
555 * \name Binary comparison operators
567 * \name Bit-wise binary operations.
588 class ir_expression
: public ir_rvalue
{
590 ir_expression(int op
, const struct glsl_type
*type
,
591 ir_rvalue
*, ir_rvalue
*);
593 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
595 static unsigned int get_num_operands(ir_expression_operation
);
596 unsigned int get_num_operands() const
598 return get_num_operands(operation
);
602 * Return a string representing this expression's operator.
604 const char *operator_string();
607 * Do a reverse-lookup to translate the given string into an operator.
609 static ir_expression_operation
get_operator(const char *);
611 virtual void accept(ir_visitor
*v
)
616 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
618 ir_expression_operation operation
;
619 ir_rvalue
*operands
[2];
624 * IR instruction representing a function call
626 class ir_call
: public ir_rvalue
{
628 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
631 assert(callee
->return_type
!= NULL
);
632 type
= callee
->return_type
;
633 actual_parameters
->move_nodes_to(& this->actual_parameters
);
636 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
638 virtual ir_call
*as_call()
643 virtual void accept(ir_visitor
*v
)
648 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
651 * Get a generic ir_call object when an error occurs
653 * Any allocation will be performed with 'ctx' as talloc owner.
655 static ir_call
*get_error_instruction(void *ctx
);
658 * Get an iterator for the set of acutal parameters
660 exec_list_iterator
iterator()
662 return actual_parameters
.iterator();
666 * Get the name of the function being called.
668 const char *callee_name() const
670 return callee
->function_name();
673 const ir_function_signature
*get_callee()
679 * Generates an inline version of the function before @ir,
680 * returning the return value of the function.
682 ir_rvalue
*generate_inline(ir_instruction
*ir
);
691 const ir_function_signature
*callee
;
693 /* List of ir_rvalue of paramaters passed in this call. */
694 exec_list actual_parameters
;
699 * \name Jump-like IR instructions.
701 * These include \c break, \c continue, \c return, and \c discard.
704 class ir_jump
: public ir_instruction
{
712 class ir_return
: public ir_jump
{
720 ir_return(ir_rvalue
*value
)
726 virtual ir_instruction
*clone(struct hash_table
*) const;
728 virtual ir_return
*as_return()
733 ir_rvalue
*get_value() const
738 virtual void accept(ir_visitor
*v
)
743 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
750 * Jump instructions used inside loops
752 * These include \c break and \c continue. The \c break within a loop is
753 * different from the \c break within a switch-statement.
757 class ir_loop_jump
: public ir_jump
{
764 ir_loop_jump(jump_mode mode
)
770 virtual ir_instruction
*clone(struct hash_table
*) const;
772 virtual void accept(ir_visitor
*v
)
777 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
779 bool is_break() const
781 return mode
== jump_break
;
784 bool is_continue() const
786 return mode
== jump_continue
;
789 /** Mode selector for the jump instruction. */
792 /** Loop containing this break instruction. */
799 * Texture sampling opcodes used in ir_texture
801 enum ir_texture_opcode
{
802 ir_tex
, /* Regular texture look-up */
803 ir_txb
, /* Texture look-up with LOD bias */
804 ir_txl
, /* Texture look-up with explicit LOD */
805 ir_txd
, /* Texture look-up with partial derivatvies */
806 ir_txf
/* Texel fetch with explicit LOD */
811 * IR instruction to sample a texture
813 * The specific form of the IR instruction depends on the \c mode value
814 * selected from \c ir_texture_opcodes. In the printed IR, these will
818 * | Projection divisor
819 * | | Shadow comparitor
822 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
823 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
824 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
825 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
826 * (txf (sampler) (coordinate) (0 0 0) (lod))
828 class ir_texture
: public ir_rvalue
{
830 ir_texture(enum ir_texture_opcode op
)
831 : op(op
), projector(NULL
), shadow_comparitor(NULL
)
836 virtual ir_instruction
*clone(struct hash_table
*) const;
838 virtual void accept(ir_visitor
*v
)
843 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
846 * Return a string representing the ir_texture_opcode.
848 const char *opcode_string();
850 /** Set the sampler and infer the type. */
851 void set_sampler(ir_dereference
*sampler
);
854 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
856 static ir_texture_opcode
get_opcode(const char *);
858 enum ir_texture_opcode op
;
860 /** Sampler to use for the texture access. */
861 ir_dereference
*sampler
;
863 /** Texture coordinate to sample */
864 ir_rvalue
*coordinate
;
867 * Value used for projective divide.
869 * If there is no projective divide (the common case), this will be
870 * \c NULL. Optimization passes should check for this to point to a constant
871 * of 1.0 and replace that with \c NULL.
873 ir_rvalue
*projector
;
876 * Coordinate used for comparison on shadow look-ups.
878 * If there is no shadow comparison, this will be \c NULL. For the
879 * \c ir_txf opcode, this *must* be \c NULL.
881 ir_rvalue
*shadow_comparitor
;
883 /** Explicit texel offsets. */
884 signed char offsets
[3];
887 ir_rvalue
*lod
; /**< Floating point LOD */
888 ir_rvalue
*bias
; /**< Floating point LOD bias */
890 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
891 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
897 struct ir_swizzle_mask
{
904 * Number of components in the swizzle.
906 unsigned num_components
:3;
909 * Does the swizzle contain duplicate components?
911 * L-value swizzles cannot contain duplicate components.
913 unsigned has_duplicates
:1;
917 class ir_swizzle
: public ir_rvalue
{
919 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
922 ir_swizzle(ir_rvalue
*val
, const unsigned *components
, unsigned count
);
924 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
926 virtual ir_instruction
*clone(struct hash_table
*) const;
928 virtual ir_swizzle
*as_swizzle()
934 * Construct an ir_swizzle from the textual representation. Can fail.
936 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
938 virtual void accept(ir_visitor
*v
)
943 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
947 return val
->is_lvalue() && !mask
.has_duplicates
;
951 * Get the variable that is ultimately referenced by an r-value
953 virtual ir_variable
*variable_referenced();
956 ir_swizzle_mask mask
;
960 * Initialize the mask component of a swizzle
962 * This is used by the \c ir_swizzle constructors.
964 void init_mask(const unsigned *components
, unsigned count
);
968 class ir_dereference
: public ir_rvalue
{
970 virtual ir_dereference
*as_dereference()
978 * Get the variable that is ultimately referenced by an r-value
980 virtual ir_variable
*variable_referenced() = 0;
984 class ir_dereference_variable
: public ir_dereference
{
986 ir_dereference_variable(ir_variable
*var
);
988 virtual ir_instruction
*clone(struct hash_table
*) const;
991 * Get the variable that is ultimately referenced by an r-value
993 virtual ir_variable
*variable_referenced()
998 virtual ir_variable
*whole_variable_referenced()
1000 /* ir_dereference_variable objects always dereference the entire
1001 * variable. However, if this dereference is dereferenced by anything
1002 * else, the complete deferefernce chain is not a whole-variable
1003 * dereference. This method should only be called on the top most
1004 * ir_rvalue in a dereference chain.
1009 virtual void accept(ir_visitor
*v
)
1014 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1017 * Object being dereferenced.
1023 class ir_dereference_array
: public ir_dereference
{
1025 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
1027 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
1029 virtual ir_instruction
*clone(struct hash_table
*) const;
1031 virtual ir_dereference_array
*as_dereference_array()
1037 * Get the variable that is ultimately referenced by an r-value
1039 virtual ir_variable
*variable_referenced()
1041 return this->array
->variable_referenced();
1044 virtual void accept(ir_visitor
*v
)
1049 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1052 ir_rvalue
*array_index
;
1055 void set_array(ir_rvalue
*value
);
1059 class ir_dereference_record
: public ir_dereference
{
1061 ir_dereference_record(ir_rvalue
*value
, const char *field
);
1063 ir_dereference_record(ir_variable
*var
, const char *field
);
1065 virtual ir_instruction
*clone(struct hash_table
*) const;
1068 * Get the variable that is ultimately referenced by an r-value
1070 virtual ir_variable
*variable_referenced()
1072 return this->record
->variable_referenced();
1075 virtual void accept(ir_visitor
*v
)
1080 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1088 * Data stored in an ir_constant
1090 union ir_constant_data
{
1098 class ir_constant
: public ir_rvalue
{
1100 ir_constant(const struct glsl_type
*type
, const ir_constant_data
*data
);
1101 ir_constant(bool b
);
1102 ir_constant(unsigned int u
);
1104 ir_constant(float f
);
1107 * Construct an ir_constant from a list of ir_constant values
1109 ir_constant(const struct glsl_type
*type
, exec_list
*values
);
1112 * Construct an ir_constant from a scalar component of another ir_constant
1114 * The new \c ir_constant inherits the type of the component from the
1118 * In the case of a matrix constant, the new constant is a scalar, \b not
1121 ir_constant(const ir_constant
*c
, unsigned i
);
1123 virtual ir_instruction
*clone(struct hash_table
*) const;
1125 virtual ir_constant
*as_constant()
1130 virtual void accept(ir_visitor
*v
)
1135 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1138 * Get a particular component of a constant as a specific type
1140 * This is useful, for example, to get a value from an integer constant
1141 * as a float or bool. This appears frequently when constructors are
1142 * called with all constant parameters.
1145 bool get_bool_component(unsigned i
) const;
1146 float get_float_component(unsigned i
) const;
1147 int get_int_component(unsigned i
) const;
1148 unsigned get_uint_component(unsigned i
) const;
1151 ir_constant
*get_record_field(const char *name
);
1154 * Determine whether a constant has the same value as another constant
1156 bool has_value(const ir_constant
*) const;
1159 * Value of the constant.
1161 * The field used to back the values supplied by the constant is determined
1162 * by the type associated with the \c ir_instruction. Constants may be
1163 * scalars, vectors, or matrices.
1165 union ir_constant_data value
;
1167 exec_list components
;
1171 * Parameterless constructor only used by the clone method
1177 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
1179 void validate_ir_tree(exec_list
*instructions
);
1182 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1183 struct _mesa_glsl_parse_state
*state
);
1186 _mesa_glsl_initialize_functions(exec_list
*instructions
,
1187 struct _mesa_glsl_parse_state
*state
);