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.
33 #include "ir_visitor.h"
34 #include "ir_hierarchical_visitor.h"
41 * Base class of all IR instructions
43 class ir_instruction
: public exec_node
{
45 const struct glsl_type
*type
;
47 class ir_constant
*constant_expression_value();
49 /** ir_print_visitor helper for debugging. */
52 virtual void accept(ir_visitor
*) = 0;
53 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*) = 0;
56 * \name IR instruction downcast functions
58 * These functions either cast the object to a derived class or return
59 * \c NULL if the object's type does not match the specified derived class.
60 * Additional downcast functions will be added as needed.
63 virtual class ir_variable
* as_variable() { return NULL
; }
64 virtual class ir_function
* as_function() { return NULL
; }
65 virtual class ir_dereference
* as_dereference() { return NULL
; }
66 virtual class ir_dereference_array
* as_dereference_array() { return NULL
; }
67 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
68 virtual class ir_loop
* as_loop() { return NULL
; }
69 virtual class ir_assignment
* as_assignment() { return NULL
; }
70 virtual class ir_call
* as_call() { return NULL
; }
71 virtual class ir_return
* as_return() { return NULL
; }
72 virtual class ir_if
* as_if() { return NULL
; }
73 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
74 virtual class ir_constant
* as_constant() { return NULL
; }
85 class ir_rvalue
: public ir_instruction
{
87 virtual ir_rvalue
* as_rvalue()
92 virtual bool is_lvalue()
98 * Get the variable that is ultimately referenced by an r-value
100 virtual ir_variable
*variable_referenced()
107 * If an r-value is a reference to a whole variable, get that variable
110 * Pointer to a variable that is completely dereferenced by the r-value. If
111 * the r-value is not a dereference or the dereference does not access the
112 * entire variable (i.e., it's just one array element, struct field), \c NULL
115 virtual ir_variable
*whole_variable_referenced()
128 enum ir_variable_mode
{
136 enum ir_varaible_interpolation
{
143 class ir_variable
: public ir_instruction
{
145 ir_variable(const struct glsl_type
*, const char *);
147 virtual ir_variable
*as_variable()
152 virtual void accept(ir_visitor
*v
)
157 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
160 * Duplicate an IR variable
163 * This will probably be made \c virtual and moved to the base class
166 ir_variable
*clone() const
168 ir_variable
*var
= new ir_variable(type
, name
);
170 var
->max_array_access
= this->max_array_access
;
171 var
->read_only
= this->read_only
;
172 var
->centroid
= this->centroid
;
173 var
->invariant
= this->invariant
;
174 var
->mode
= this->mode
;
175 var
->interpolation
= this->interpolation
;
181 * Get the string value for the interpolation qualifier
184 * If none of \c shader_in or \c shader_out is set, an empty string will
185 * be returned. Otherwise the string that would be used in a shader to
186 * specify \c mode will be returned.
188 const char *interpolation_string() const;
191 * Calculate the number of slots required to hold this variable
193 * This is used to determine how many uniform or varying locations a variable
194 * occupies. The count is in units of floating point components.
196 unsigned component_slots() const;
201 * Highest element accessed with a constant expression array index
203 * Not used for non-array variables.
205 unsigned max_array_access
;
207 unsigned read_only
:1;
209 unsigned invariant
:1;
210 /** If the variable is initialized outside of the scope of the shader */
211 unsigned shader_in
:1;
213 * If the variable value is later used outside of the scope of the shader.
215 unsigned shader_out
:1;
218 unsigned interpolation
:2;
221 * Flag that the whole array is assignable
223 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
224 * equality). This flag enables this behavior.
226 unsigned array_lvalue
:1;
229 * Storage location of the base of this variable
231 * The precise meaning of this field depends on the nature of the variable.
233 * - Vertex shader input: one of the values from \c gl_vert_attrib.
234 * - Vertex shader output: one of the values from \c gl_vert_result.
235 * - Fragment shader input: one of the values from \c gl_frag_attrib.
236 * - Fragment shader output: one of the values from \c gl_frag_result.
237 * - Uniforms: Per-stage uniform slot number.
238 * - Other: This field is not currently used.
240 * If the variable is a uniform, shader input, or shader output, and the
241 * slot has not been assigned, the value will be -1.
246 * Emit a warning if this variable is accessed.
248 const char *warn_extension
;
251 * Value assigned in the initializer of a variable declared "const"
253 ir_constant
*constant_value
;
259 * The representation of a function instance; may be the full definition or
260 * simply a prototype.
262 class ir_function_signature
: public ir_instruction
{
263 /* An ir_function_signature will be part of the list of signatures in
267 ir_function_signature(const glsl_type
*return_type
);
269 virtual void accept(ir_visitor
*v
)
274 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
277 * Get the name of the function for which this is a signature
279 const char *function_name() const;
282 * Check whether the qualifiers match between this signature's parameters
283 * and the supplied parameter list. If not, returns the name of the first
284 * parameter with mismatched qualifiers (for use in error messages).
286 const char *qualifiers_match(exec_list
*params
);
289 * Replace the current parameter list with the given one. This is useful
290 * if the current information came from a prototype, and either has invalid
291 * or missing parameter names.
293 void replace_parameters(exec_list
*new_params
);
296 * Function return type.
298 * \note This discards the optional precision qualifier.
300 const struct glsl_type
*return_type
;
303 * List of ir_variable of function parameters.
305 * This represents the storage. The paramaters passed in a particular
306 * call will be in ir_call::actual_paramaters.
308 struct exec_list parameters
;
310 /** Whether or not this function has a body (which may be empty). */
311 unsigned is_defined
:1;
313 /** Body of instructions in the function. */
314 struct exec_list body
;
317 /** Function of which this signature is one overload. */
318 class ir_function
*function
;
320 friend class ir_function
;
325 * Header for tracking multiple overloaded functions with the same name.
326 * Contains a list of ir_function_signatures representing each of the
329 class ir_function
: public ir_instruction
{
331 ir_function(const char *name
);
333 virtual ir_function
*as_function()
338 virtual void accept(ir_visitor
*v
)
343 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
345 void add_signature(ir_function_signature
*sig
)
347 sig
->function
= this;
348 signatures
.push_tail(sig
);
352 * Get an iterator for the set of function signatures
354 exec_list_iterator
iterator()
356 return signatures
.iterator();
360 * Find a signature that matches a set of actual parameters, taking implicit
361 * conversions into account.
363 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
366 * Find a signature that exactly matches a set of actual parameters without
367 * any implicit type conversions.
369 ir_function_signature
*exact_matching_signature(exec_list
*actual_ps
);
372 * Name of the function.
378 * List of ir_function_signature for each overloaded function with this name.
380 struct exec_list signatures
;
383 inline const char *ir_function_signature::function_name() const
385 return function
->name
;
391 * IR instruction representing high-level if-statements
393 class ir_if
: public ir_instruction
{
395 ir_if(ir_rvalue
*condition
)
396 : condition(condition
)
401 virtual ir_if
*as_if()
406 virtual void accept(ir_visitor
*v
)
411 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
413 ir_rvalue
*condition
;
414 /** List of ir_instruction for the body of the then branch */
415 exec_list then_instructions
;
416 /** List of ir_instruction for the body of the else branch */
417 exec_list else_instructions
;
422 * IR instruction representing a high-level loop structure.
424 class ir_loop
: public ir_instruction
{
426 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
431 virtual void accept(ir_visitor
*v
)
436 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
438 virtual ir_loop
*as_loop()
444 * Get an iterator for the instructions of the loop body
446 exec_list_iterator
iterator()
448 return body_instructions
.iterator();
451 /** List of ir_instruction that make up the body of the loop. */
452 exec_list body_instructions
;
455 * \name Loop counter and controls
460 ir_rvalue
*increment
;
461 ir_variable
*counter
;
466 class ir_assignment
: public ir_rvalue
{
468 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
470 virtual void accept(ir_visitor
*v
)
475 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
477 virtual ir_assignment
* as_assignment()
483 * Left-hand side of the assignment.
488 * Value being assigned
493 * Optional condition for the assignment.
495 ir_rvalue
*condition
;
498 /* Update ir_expression::num_operands() and operator_strs when
499 * updating this list.
501 enum ir_expression_operation
{
514 ir_unop_f2i
, /**< Float-to-integer conversion. */
515 ir_unop_i2f
, /**< Integer-to-float conversion. */
516 ir_unop_f2b
, /**< Float-to-boolean conversion */
517 ir_unop_b2f
, /**< Boolean-to-float conversion */
518 ir_unop_i2b
, /**< int-to-boolean conversion */
519 ir_unop_b2i
, /**< Boolean-to-int conversion */
520 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
523 * \name Unary floating-point rounding operations.
532 * \name Trigonometric operations.
540 * \name Partial derivatives.
554 * \name Binary comparison operators
566 * \name Bit-wise binary operations.
587 class ir_expression
: public ir_rvalue
{
589 ir_expression(int op
, const struct glsl_type
*type
,
590 ir_rvalue
*, ir_rvalue
*);
592 static unsigned int get_num_operands(ir_expression_operation
);
593 unsigned int get_num_operands()
595 return get_num_operands(operation
);
599 * Return a string representing this expression's operator.
601 const char *operator_string();
604 * Do a reverse-lookup to translate the given string into an operator.
606 static ir_expression_operation
get_operator(const char *);
608 virtual void accept(ir_visitor
*v
)
613 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
615 ir_expression
*clone();
617 ir_expression_operation operation
;
618 ir_rvalue
*operands
[2];
623 * IR instruction representing a function call
625 class ir_call
: public ir_rvalue
{
627 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
630 assert(callee
->return_type
!= NULL
);
631 type
= callee
->return_type
;
632 actual_parameters
->move_nodes_to(& this->actual_parameters
);
635 virtual ir_call
*as_call()
640 virtual void accept(ir_visitor
*v
)
645 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
648 * Get a generic ir_call object when an error occurs
650 static ir_call
*get_error_instruction();
653 * Get an iterator for the set of acutal parameters
655 exec_list_iterator
iterator()
657 return actual_parameters
.iterator();
661 * Get the name of the function being called.
663 const char *callee_name() const
665 return callee
->function_name();
668 const ir_function_signature
*get_callee()
674 * Generates an inline version of the function before @ir,
675 * returning the return value of the function.
677 ir_rvalue
*generate_inline(ir_instruction
*ir
);
686 const ir_function_signature
*callee
;
688 /* List of ir_rvalue of paramaters passed in this call. */
689 exec_list actual_parameters
;
694 * \name Jump-like IR instructions.
696 * These include \c break, \c continue, \c return, and \c discard.
699 class ir_jump
: public ir_instruction
{
707 class ir_return
: public ir_jump
{
715 ir_return(ir_rvalue
*value
)
721 virtual ir_return
*as_return()
726 ir_rvalue
*get_value() const
731 virtual void accept(ir_visitor
*v
)
736 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
743 * Jump instructions used inside loops
745 * These include \c break and \c continue. The \c break within a loop is
746 * different from the \c break within a switch-statement.
750 class ir_loop_jump
: public ir_jump
{
757 ir_loop_jump(ir_loop
*loop
, jump_mode mode
)
758 : loop(loop
), mode(mode
)
763 virtual void accept(ir_visitor
*v
)
768 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
770 bool is_break() const
772 return mode
== jump_break
;
775 bool is_continue() const
777 return mode
== jump_continue
;
781 /** Loop containing this break instruction. */
784 /** Mode selector for the jump instruction. */
791 * Texture sampling opcodes used in ir_texture
793 enum ir_texture_opcode
{
794 ir_tex
, /* Regular texture look-up */
795 ir_txb
, /* Texture look-up with LOD bias */
796 ir_txl
, /* Texture look-up with explicit LOD */
797 ir_txd
, /* Texture look-up with partial derivatvies */
798 ir_txf
/* Texel fetch with explicit LOD */
803 * IR instruction to sample a texture
805 * The specific form of the IR instruction depends on the \c mode value
806 * selected from \c ir_texture_opcodes. In the printed IR, these will
810 * | Projection divisor
811 * | | Shadow comparitor
814 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
815 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
816 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
817 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
818 * (txf (sampler) (coordinate) (0 0 0) (lod))
820 class ir_texture
: public ir_rvalue
{
822 ir_texture(enum ir_texture_opcode op
)
823 : op(op
), projector(NULL
), shadow_comparitor(NULL
)
828 virtual void accept(ir_visitor
*v
)
833 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
836 * Return a string representing the ir_texture_opcode.
838 const char *opcode_string();
840 /** Set the sampler and infer the type. */
841 void set_sampler(ir_dereference
*sampler
);
844 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
846 static ir_texture_opcode
get_opcode(const char *);
848 enum ir_texture_opcode op
;
850 /** Sampler to use for the texture access. */
851 ir_dereference
*sampler
;
853 /** Texture coordinate to sample */
854 ir_rvalue
*coordinate
;
857 * Value used for projective divide.
859 * If there is no projective divide (the common case), this will be
860 * \c NULL. Optimization passes should check for this to point to a constant
861 * of 1.0 and replace that with \c NULL.
863 ir_rvalue
*projector
;
866 * Coordinate used for comparison on shadow look-ups.
868 * If there is no shadow comparison, this will be \c NULL. For the
869 * \c ir_txf opcode, this *must* be \c NULL.
871 ir_rvalue
*shadow_comparitor
;
873 /** Explicit texel offsets. */
874 signed char offsets
[3];
877 ir_rvalue
*lod
; /**< Floating point LOD */
878 ir_rvalue
*bias
; /**< Floating point LOD bias */
880 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
881 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
887 struct ir_swizzle_mask
{
894 * Number of components in the swizzle.
896 unsigned num_components
:3;
899 * Does the swizzle contain duplicate components?
901 * L-value swizzles cannot contain duplicate components.
903 unsigned has_duplicates
:1;
907 class ir_swizzle
: public ir_rvalue
{
909 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
911 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
913 virtual ir_swizzle
*as_swizzle()
920 return new ir_swizzle(this->val
, this->mask
);
924 * Construct an ir_swizzle from the textual representation. Can fail.
926 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
928 virtual void accept(ir_visitor
*v
)
933 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
937 return val
->is_lvalue() && !mask
.has_duplicates
;
941 * Get the variable that is ultimately referenced by an r-value
943 virtual ir_variable
*variable_referenced();
946 ir_swizzle_mask mask
;
950 class ir_dereference
: public ir_rvalue
{
952 virtual ir_dereference
*as_dereference()
960 * Get the variable that is ultimately referenced by an r-value
962 virtual ir_variable
*variable_referenced() = 0;
966 class ir_dereference_variable
: public ir_dereference
{
968 ir_dereference_variable(ir_variable
*var
);
971 * Get the variable that is ultimately referenced by an r-value
973 virtual ir_variable
*variable_referenced()
978 virtual ir_variable
*whole_variable_referenced()
980 /* ir_dereference_variable objects always dereference the entire
981 * variable. However, if this dereference is dereferenced by anything
982 * else, the complete deferefernce chain is not a whole-variable
983 * dereference. This method should only be called on the top most
984 * ir_rvalue in a dereference chain.
989 virtual void accept(ir_visitor
*v
)
994 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
997 * Object being dereferenced.
1003 class ir_dereference_array
: public ir_dereference
{
1005 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
1007 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
1009 virtual ir_dereference_array
*as_dereference_array()
1015 * Get the variable that is ultimately referenced by an r-value
1017 virtual ir_variable
*variable_referenced()
1019 return this->array
->variable_referenced();
1022 virtual void accept(ir_visitor
*v
)
1027 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1030 ir_rvalue
*array_index
;
1033 void set_array(ir_rvalue
*value
);
1037 class ir_dereference_record
: public ir_dereference
{
1039 ir_dereference_record(ir_rvalue
*value
, const char *field
);
1041 ir_dereference_record(ir_variable
*var
, const char *field
);
1044 * Get the variable that is ultimately referenced by an r-value
1046 virtual ir_variable
*variable_referenced()
1048 return this->record
->variable_referenced();
1051 virtual void accept(ir_visitor
*v
)
1056 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1064 * Data stored in an ir_constant
1066 union ir_constant_data
{
1074 class ir_constant
: public ir_rvalue
{
1076 ir_constant(const struct glsl_type
*type
, const ir_constant_data
*data
);
1077 ir_constant(bool b
);
1078 ir_constant(unsigned int u
);
1080 ir_constant(float f
);
1083 * Construct an ir_constant from a list of ir_constant values
1085 ir_constant(const struct glsl_type
*type
, exec_list
*values
);
1088 * Construct an ir_constant from a scalar component of another ir_constant
1090 * The new \c ir_constant inherits the type of the component from the
1094 * In the case of a matrix constant, the new constant is a scalar, \b not
1097 ir_constant(const ir_constant
*c
, unsigned i
);
1099 virtual ir_constant
*as_constant()
1104 virtual void accept(ir_visitor
*v
)
1109 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1111 ir_constant
*clone();
1114 * Get a particular component of a constant as a specific type
1116 * This is useful, for example, to get a value from an integer constant
1117 * as a float or bool. This appears frequently when constructors are
1118 * called with all constant parameters.
1121 bool get_bool_component(unsigned i
) const;
1122 float get_float_component(unsigned i
) const;
1123 int get_int_component(unsigned i
) const;
1124 unsigned get_uint_component(unsigned i
) const;
1127 ir_constant
*get_record_field(const char *name
);
1130 * Determine whether a constant has the same value as another constant
1132 bool has_value(const ir_constant
*) const;
1135 * Value of the constant.
1137 * The field used to back the values supplied by the constant is determined
1138 * by the type associated with the \c ir_instruction. Constants may be
1139 * scalars, vectors, or matrices.
1141 union ir_constant_data value
;
1143 exec_list components
;
1147 * Parameterless constructor only used by the clone method
1153 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
1155 void validate_ir_tree(exec_list
*instructions
);
1158 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1159 struct _mesa_glsl_parse_state
*state
);
1162 _mesa_glsl_initialize_functions(exec_list
*instructions
,
1163 struct _mesa_glsl_parse_state
*state
);