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_dereference_variable
*as_dereference_variable() { return NULL
; }
77 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
78 virtual class ir_loop
* as_loop() { return NULL
; }
79 virtual class ir_assignment
* as_assignment() { return NULL
; }
80 virtual class ir_call
* as_call() { return NULL
; }
81 virtual class ir_return
* as_return() { return NULL
; }
82 virtual class ir_if
* as_if() { return NULL
; }
83 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
84 virtual class ir_constant
* as_constant() { return NULL
; }
95 class ir_rvalue
: public ir_instruction
{
97 virtual ir_rvalue
* as_rvalue()
102 virtual bool is_lvalue()
108 * Get the variable that is ultimately referenced by an r-value
110 virtual ir_variable
*variable_referenced()
117 * If an r-value is a reference to a whole variable, get that variable
120 * Pointer to a variable that is completely dereferenced by the r-value. If
121 * the r-value is not a dereference or the dereference does not access the
122 * entire variable (i.e., it's just one array element, struct field), \c NULL
125 virtual ir_variable
*whole_variable_referenced()
138 enum ir_variable_mode
{
146 enum ir_variable_interpolation
{
153 class ir_variable
: public ir_instruction
{
155 ir_variable(const struct glsl_type
*, const char *);
157 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
159 virtual ir_variable
*as_variable()
164 virtual void accept(ir_visitor
*v
)
169 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
173 * Get the string value for the interpolation qualifier
176 * If none of \c shader_in or \c shader_out is set, an empty string will
177 * be returned. Otherwise the string that would be used in a shader to
178 * specify \c mode will be returned.
180 const char *interpolation_string() const;
183 * Calculate the number of slots required to hold this variable
185 * This is used to determine how many uniform or varying locations a variable
186 * occupies. The count is in units of floating point components.
188 unsigned component_slots() const;
193 * Highest element accessed with a constant expression array index
195 * Not used for non-array variables.
197 unsigned max_array_access
;
199 unsigned read_only
:1;
201 unsigned invariant
:1;
202 /** If the variable is initialized outside of the scope of the shader */
203 unsigned shader_in
:1;
205 * If the variable value is later used outside of the scope of the shader.
207 unsigned shader_out
:1;
210 unsigned interpolation
:2;
213 * Flag that the whole array is assignable
215 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
216 * equality). This flag enables this behavior.
218 unsigned array_lvalue
:1;
221 * Storage location of the base of this variable
223 * The precise meaning of this field depends on the nature of the variable.
225 * - Vertex shader input: one of the values from \c gl_vert_attrib.
226 * - Vertex shader output: one of the values from \c gl_vert_result.
227 * - Fragment shader input: one of the values from \c gl_frag_attrib.
228 * - Fragment shader output: one of the values from \c gl_frag_result.
229 * - Uniforms: Per-stage uniform slot number.
230 * - Other: This field is not currently used.
232 * If the variable is a uniform, shader input, or shader output, and the
233 * slot has not been assigned, the value will be -1.
238 * Emit a warning if this variable is accessed.
240 const char *warn_extension
;
243 * Value assigned in the initializer of a variable declared "const"
245 ir_constant
*constant_value
;
251 * The representation of a function instance; may be the full definition or
252 * simply a prototype.
254 class ir_function_signature
: public ir_instruction
{
255 /* An ir_function_signature will be part of the list of signatures in
259 ir_function_signature(const glsl_type
*return_type
);
261 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
263 virtual void accept(ir_visitor
*v
)
268 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
271 * Get the name of the function for which this is a signature
273 const char *function_name() const;
276 * Check whether the qualifiers match between this signature's parameters
277 * and the supplied parameter list. If not, returns the name of the first
278 * parameter with mismatched qualifiers (for use in error messages).
280 const char *qualifiers_match(exec_list
*params
);
283 * Replace the current parameter list with the given one. This is useful
284 * if the current information came from a prototype, and either has invalid
285 * or missing parameter names.
287 void replace_parameters(exec_list
*new_params
);
290 * Function return type.
292 * \note This discards the optional precision qualifier.
294 const struct glsl_type
*return_type
;
297 * List of ir_variable of function parameters.
299 * This represents the storage. The paramaters passed in a particular
300 * call will be in ir_call::actual_paramaters.
302 struct exec_list parameters
;
304 /** Whether or not this function has a body (which may be empty). */
305 unsigned is_defined
:1;
307 /** Body of instructions in the function. */
308 struct exec_list body
;
311 /** Function of which this signature is one overload. */
312 class ir_function
*function
;
314 friend class ir_function
;
319 * Header for tracking multiple overloaded functions with the same name.
320 * Contains a list of ir_function_signatures representing each of the
323 class ir_function
: public ir_instruction
{
325 ir_function(const char *name
);
327 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
329 virtual ir_function
*as_function()
334 virtual void accept(ir_visitor
*v
)
339 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
341 void add_signature(ir_function_signature
*sig
)
343 sig
->function
= this;
344 signatures
.push_tail(sig
);
348 * Get an iterator for the set of function signatures
350 exec_list_iterator
iterator()
352 return signatures
.iterator();
356 * Find a signature that matches a set of actual parameters, taking implicit
357 * conversions into account.
359 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
362 * Find a signature that exactly matches a set of actual parameters without
363 * any implicit type conversions.
365 ir_function_signature
*exact_matching_signature(exec_list
*actual_ps
);
368 * Name of the function.
374 * List of ir_function_signature for each overloaded function with this name.
376 struct exec_list signatures
;
379 inline const char *ir_function_signature::function_name() const
381 return function
->name
;
387 * IR instruction representing high-level if-statements
389 class ir_if
: public ir_instruction
{
391 ir_if(ir_rvalue
*condition
)
392 : condition(condition
)
397 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
399 virtual ir_if
*as_if()
404 virtual void accept(ir_visitor
*v
)
409 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
411 ir_rvalue
*condition
;
412 /** List of ir_instruction for the body of the then branch */
413 exec_list then_instructions
;
414 /** List of ir_instruction for the body of the else branch */
415 exec_list else_instructions
;
420 * IR instruction representing a high-level loop structure.
422 class ir_loop
: public ir_instruction
{
424 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
429 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
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 ir_instruction
*clone(struct hash_table
*ht
) const;
472 virtual void accept(ir_visitor
*v
)
477 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
479 virtual ir_assignment
* as_assignment()
485 * Left-hand side of the assignment.
490 * Value being assigned
495 * Optional condition for the assignment.
497 ir_rvalue
*condition
;
500 /* Update ir_expression::num_operands() and operator_strs when
501 * updating this list.
503 enum ir_expression_operation
{
516 ir_unop_f2i
, /**< Float-to-integer conversion. */
517 ir_unop_i2f
, /**< Integer-to-float conversion. */
518 ir_unop_f2b
, /**< Float-to-boolean conversion */
519 ir_unop_b2f
, /**< Boolean-to-float conversion */
520 ir_unop_i2b
, /**< int-to-boolean conversion */
521 ir_unop_b2i
, /**< Boolean-to-int conversion */
522 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
525 * \name Unary floating-point rounding operations.
535 * \name Trigonometric operations.
543 * \name Partial derivatives.
556 * Takes one of two combinations of arguments:
561 * Does not take integer types.
566 * \name Binary comparison operators
578 * \name Bit-wise binary operations.
599 class ir_expression
: public ir_rvalue
{
601 ir_expression(int op
, const struct glsl_type
*type
,
602 ir_rvalue
*, ir_rvalue
*);
604 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
606 static unsigned int get_num_operands(ir_expression_operation
);
607 unsigned int get_num_operands() const
609 return get_num_operands(operation
);
613 * Return a string representing this expression's operator.
615 const char *operator_string();
618 * Do a reverse-lookup to translate the given string into an operator.
620 static ir_expression_operation
get_operator(const char *);
622 virtual void accept(ir_visitor
*v
)
627 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
629 ir_expression_operation operation
;
630 ir_rvalue
*operands
[2];
635 * IR instruction representing a function call
637 class ir_call
: public ir_rvalue
{
639 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
642 assert(callee
->return_type
!= NULL
);
643 type
= callee
->return_type
;
644 actual_parameters
->move_nodes_to(& this->actual_parameters
);
647 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
649 virtual ir_call
*as_call()
654 virtual void accept(ir_visitor
*v
)
659 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
662 * Get a generic ir_call object when an error occurs
664 * Any allocation will be performed with 'ctx' as talloc owner.
666 static ir_call
*get_error_instruction(void *ctx
);
669 * Get an iterator for the set of acutal parameters
671 exec_list_iterator
iterator()
673 return actual_parameters
.iterator();
677 * Get the name of the function being called.
679 const char *callee_name() const
681 return callee
->function_name();
684 const ir_function_signature
*get_callee()
690 * Generates an inline version of the function before @ir,
691 * returning the return value of the function.
693 ir_rvalue
*generate_inline(ir_instruction
*ir
);
702 const ir_function_signature
*callee
;
704 /* List of ir_rvalue of paramaters passed in this call. */
705 exec_list actual_parameters
;
710 * \name Jump-like IR instructions.
712 * These include \c break, \c continue, \c return, and \c discard.
715 class ir_jump
: public ir_instruction
{
723 class ir_return
: public ir_jump
{
731 ir_return(ir_rvalue
*value
)
737 virtual ir_instruction
*clone(struct hash_table
*) const;
739 virtual ir_return
*as_return()
744 ir_rvalue
*get_value() const
749 virtual void accept(ir_visitor
*v
)
754 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
761 * Jump instructions used inside loops
763 * These include \c break and \c continue. The \c break within a loop is
764 * different from the \c break within a switch-statement.
768 class ir_loop_jump
: public ir_jump
{
775 ir_loop_jump(jump_mode mode
)
781 virtual ir_instruction
*clone(struct hash_table
*) const;
783 virtual void accept(ir_visitor
*v
)
788 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
790 bool is_break() const
792 return mode
== jump_break
;
795 bool is_continue() const
797 return mode
== jump_continue
;
800 /** Mode selector for the jump instruction. */
803 /** Loop containing this break instruction. */
808 * IR instruction representing discard statements.
810 class ir_discard
: public ir_jump
{
814 this->condition
= NULL
;
817 ir_discard(ir_rvalue
*cond
)
819 this->condition
= cond
;
822 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
824 virtual void accept(ir_visitor
*v
)
829 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
831 ir_rvalue
*condition
;
837 * Texture sampling opcodes used in ir_texture
839 enum ir_texture_opcode
{
840 ir_tex
, /* Regular texture look-up */
841 ir_txb
, /* Texture look-up with LOD bias */
842 ir_txl
, /* Texture look-up with explicit LOD */
843 ir_txd
, /* Texture look-up with partial derivatvies */
844 ir_txf
/* Texel fetch with explicit LOD */
849 * IR instruction to sample a texture
851 * The specific form of the IR instruction depends on the \c mode value
852 * selected from \c ir_texture_opcodes. In the printed IR, these will
856 * | Projection divisor
857 * | | Shadow comparitor
860 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
861 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
862 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
863 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
864 * (txf (sampler) (coordinate) (0 0 0) (lod))
866 class ir_texture
: public ir_rvalue
{
868 ir_texture(enum ir_texture_opcode op
)
869 : op(op
), projector(NULL
), shadow_comparitor(NULL
)
874 virtual ir_instruction
*clone(struct hash_table
*) const;
876 virtual void accept(ir_visitor
*v
)
881 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
884 * Return a string representing the ir_texture_opcode.
886 const char *opcode_string();
888 /** Set the sampler and infer the type. */
889 void set_sampler(ir_dereference
*sampler
);
892 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
894 static ir_texture_opcode
get_opcode(const char *);
896 enum ir_texture_opcode op
;
898 /** Sampler to use for the texture access. */
899 ir_dereference
*sampler
;
901 /** Texture coordinate to sample */
902 ir_rvalue
*coordinate
;
905 * Value used for projective divide.
907 * If there is no projective divide (the common case), this will be
908 * \c NULL. Optimization passes should check for this to point to a constant
909 * of 1.0 and replace that with \c NULL.
911 ir_rvalue
*projector
;
914 * Coordinate used for comparison on shadow look-ups.
916 * If there is no shadow comparison, this will be \c NULL. For the
917 * \c ir_txf opcode, this *must* be \c NULL.
919 ir_rvalue
*shadow_comparitor
;
921 /** Explicit texel offsets. */
922 signed char offsets
[3];
925 ir_rvalue
*lod
; /**< Floating point LOD */
926 ir_rvalue
*bias
; /**< Floating point LOD bias */
928 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
929 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
935 struct ir_swizzle_mask
{
942 * Number of components in the swizzle.
944 unsigned num_components
:3;
947 * Does the swizzle contain duplicate components?
949 * L-value swizzles cannot contain duplicate components.
951 unsigned has_duplicates
:1;
955 class ir_swizzle
: public ir_rvalue
{
957 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
960 ir_swizzle(ir_rvalue
*val
, const unsigned *components
, unsigned count
);
962 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
964 virtual ir_instruction
*clone(struct hash_table
*) const;
966 virtual ir_swizzle
*as_swizzle()
972 * Construct an ir_swizzle from the textual representation. Can fail.
974 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
976 virtual void accept(ir_visitor
*v
)
981 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
985 return val
->is_lvalue() && !mask
.has_duplicates
;
989 * Get the variable that is ultimately referenced by an r-value
991 virtual ir_variable
*variable_referenced();
994 ir_swizzle_mask mask
;
998 * Initialize the mask component of a swizzle
1000 * This is used by the \c ir_swizzle constructors.
1002 void init_mask(const unsigned *components
, unsigned count
);
1006 class ir_dereference
: public ir_rvalue
{
1008 virtual ir_dereference
*as_dereference()
1016 * Get the variable that is ultimately referenced by an r-value
1018 virtual ir_variable
*variable_referenced() = 0;
1022 class ir_dereference_variable
: public ir_dereference
{
1024 ir_dereference_variable(ir_variable
*var
);
1026 virtual ir_instruction
*clone(struct hash_table
*) const;
1028 virtual ir_dereference_variable
*as_dereference_variable()
1034 * Get the variable that is ultimately referenced by an r-value
1036 virtual ir_variable
*variable_referenced()
1041 virtual ir_variable
*whole_variable_referenced()
1043 /* ir_dereference_variable objects always dereference the entire
1044 * variable. However, if this dereference is dereferenced by anything
1045 * else, the complete deferefernce chain is not a whole-variable
1046 * dereference. This method should only be called on the top most
1047 * ir_rvalue in a dereference chain.
1052 virtual void accept(ir_visitor
*v
)
1057 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1060 * Object being dereferenced.
1066 class ir_dereference_array
: public ir_dereference
{
1068 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
1070 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
1072 virtual ir_instruction
*clone(struct hash_table
*) const;
1074 virtual ir_dereference_array
*as_dereference_array()
1080 * Get the variable that is ultimately referenced by an r-value
1082 virtual ir_variable
*variable_referenced()
1084 return this->array
->variable_referenced();
1087 virtual void accept(ir_visitor
*v
)
1092 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1095 ir_rvalue
*array_index
;
1098 void set_array(ir_rvalue
*value
);
1102 class ir_dereference_record
: public ir_dereference
{
1104 ir_dereference_record(ir_rvalue
*value
, const char *field
);
1106 ir_dereference_record(ir_variable
*var
, const char *field
);
1108 virtual ir_instruction
*clone(struct hash_table
*) const;
1111 * Get the variable that is ultimately referenced by an r-value
1113 virtual ir_variable
*variable_referenced()
1115 return this->record
->variable_referenced();
1118 virtual void accept(ir_visitor
*v
)
1123 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1131 * Data stored in an ir_constant
1133 union ir_constant_data
{
1141 class ir_constant
: public ir_rvalue
{
1143 ir_constant(const struct glsl_type
*type
, const ir_constant_data
*data
);
1144 ir_constant(bool b
);
1145 ir_constant(unsigned int u
);
1147 ir_constant(float f
);
1150 * Construct an ir_constant from a list of ir_constant values
1152 ir_constant(const struct glsl_type
*type
, exec_list
*values
);
1155 * Construct an ir_constant from a scalar component of another ir_constant
1157 * The new \c ir_constant inherits the type of the component from the
1161 * In the case of a matrix constant, the new constant is a scalar, \b not
1164 ir_constant(const ir_constant
*c
, unsigned i
);
1166 virtual ir_instruction
*clone(struct hash_table
*) const;
1168 virtual ir_constant
*as_constant()
1173 virtual void accept(ir_visitor
*v
)
1178 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1181 * Get a particular component of a constant as a specific type
1183 * This is useful, for example, to get a value from an integer constant
1184 * as a float or bool. This appears frequently when constructors are
1185 * called with all constant parameters.
1188 bool get_bool_component(unsigned i
) const;
1189 float get_float_component(unsigned i
) const;
1190 int get_int_component(unsigned i
) const;
1191 unsigned get_uint_component(unsigned i
) const;
1194 ir_constant
*get_record_field(const char *name
);
1197 * Determine whether a constant has the same value as another constant
1199 bool has_value(const ir_constant
*) const;
1202 * Value of the constant.
1204 * The field used to back the values supplied by the constant is determined
1205 * by the type associated with the \c ir_instruction. Constants may be
1206 * scalars, vectors, or matrices.
1208 union ir_constant_data value
;
1210 exec_list components
;
1214 * Parameterless constructor only used by the clone method
1220 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
1222 void validate_ir_tree(exec_list
*instructions
);
1225 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1226 struct _mesa_glsl_parse_state
*state
);
1229 _mesa_glsl_initialize_functions(exec_list
*instructions
,
1230 struct _mesa_glsl_parse_state
*state
);