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.
534 * \name Trigonometric operations.
542 * \name Partial derivatives.
556 * \name Binary comparison operators
568 * \name Bit-wise binary operations.
589 class ir_expression
: public ir_rvalue
{
591 ir_expression(int op
, const struct glsl_type
*type
,
592 ir_rvalue
*, ir_rvalue
*);
594 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
596 static unsigned int get_num_operands(ir_expression_operation
);
597 unsigned int get_num_operands() const
599 return get_num_operands(operation
);
603 * Return a string representing this expression's operator.
605 const char *operator_string();
608 * Do a reverse-lookup to translate the given string into an operator.
610 static ir_expression_operation
get_operator(const char *);
612 virtual void accept(ir_visitor
*v
)
617 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
619 ir_expression_operation operation
;
620 ir_rvalue
*operands
[2];
625 * IR instruction representing a function call
627 class ir_call
: public ir_rvalue
{
629 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
632 assert(callee
->return_type
!= NULL
);
633 type
= callee
->return_type
;
634 actual_parameters
->move_nodes_to(& this->actual_parameters
);
637 virtual ir_instruction
*clone(struct hash_table
*ht
) const;
639 virtual ir_call
*as_call()
644 virtual void accept(ir_visitor
*v
)
649 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
652 * Get a generic ir_call object when an error occurs
654 * Any allocation will be performed with 'ctx' as talloc owner.
656 static ir_call
*get_error_instruction(void *ctx
);
659 * Get an iterator for the set of acutal parameters
661 exec_list_iterator
iterator()
663 return actual_parameters
.iterator();
667 * Get the name of the function being called.
669 const char *callee_name() const
671 return callee
->function_name();
674 const ir_function_signature
*get_callee()
680 * Generates an inline version of the function before @ir,
681 * returning the return value of the function.
683 ir_rvalue
*generate_inline(ir_instruction
*ir
);
692 const ir_function_signature
*callee
;
694 /* List of ir_rvalue of paramaters passed in this call. */
695 exec_list actual_parameters
;
700 * \name Jump-like IR instructions.
702 * These include \c break, \c continue, \c return, and \c discard.
705 class ir_jump
: public ir_instruction
{
713 class ir_return
: public ir_jump
{
721 ir_return(ir_rvalue
*value
)
727 virtual ir_instruction
*clone(struct hash_table
*) const;
729 virtual ir_return
*as_return()
734 ir_rvalue
*get_value() const
739 virtual void accept(ir_visitor
*v
)
744 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
751 * Jump instructions used inside loops
753 * These include \c break and \c continue. The \c break within a loop is
754 * different from the \c break within a switch-statement.
758 class ir_loop_jump
: public ir_jump
{
765 ir_loop_jump(jump_mode mode
)
771 virtual ir_instruction
*clone(struct hash_table
*) const;
773 virtual void accept(ir_visitor
*v
)
778 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
780 bool is_break() const
782 return mode
== jump_break
;
785 bool is_continue() const
787 return mode
== jump_continue
;
790 /** Mode selector for the jump instruction. */
793 /** Loop containing this break instruction. */
800 * Texture sampling opcodes used in ir_texture
802 enum ir_texture_opcode
{
803 ir_tex
, /* Regular texture look-up */
804 ir_txb
, /* Texture look-up with LOD bias */
805 ir_txl
, /* Texture look-up with explicit LOD */
806 ir_txd
, /* Texture look-up with partial derivatvies */
807 ir_txf
/* Texel fetch with explicit LOD */
812 * IR instruction to sample a texture
814 * The specific form of the IR instruction depends on the \c mode value
815 * selected from \c ir_texture_opcodes. In the printed IR, these will
819 * | Projection divisor
820 * | | Shadow comparitor
823 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
824 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
825 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
826 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
827 * (txf (sampler) (coordinate) (0 0 0) (lod))
829 class ir_texture
: public ir_rvalue
{
831 ir_texture(enum ir_texture_opcode op
)
832 : op(op
), projector(NULL
), shadow_comparitor(NULL
)
837 virtual ir_instruction
*clone(struct hash_table
*) const;
839 virtual void accept(ir_visitor
*v
)
844 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
847 * Return a string representing the ir_texture_opcode.
849 const char *opcode_string();
851 /** Set the sampler and infer the type. */
852 void set_sampler(ir_dereference
*sampler
);
855 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
857 static ir_texture_opcode
get_opcode(const char *);
859 enum ir_texture_opcode op
;
861 /** Sampler to use for the texture access. */
862 ir_dereference
*sampler
;
864 /** Texture coordinate to sample */
865 ir_rvalue
*coordinate
;
868 * Value used for projective divide.
870 * If there is no projective divide (the common case), this will be
871 * \c NULL. Optimization passes should check for this to point to a constant
872 * of 1.0 and replace that with \c NULL.
874 ir_rvalue
*projector
;
877 * Coordinate used for comparison on shadow look-ups.
879 * If there is no shadow comparison, this will be \c NULL. For the
880 * \c ir_txf opcode, this *must* be \c NULL.
882 ir_rvalue
*shadow_comparitor
;
884 /** Explicit texel offsets. */
885 signed char offsets
[3];
888 ir_rvalue
*lod
; /**< Floating point LOD */
889 ir_rvalue
*bias
; /**< Floating point LOD bias */
891 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
892 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
898 struct ir_swizzle_mask
{
905 * Number of components in the swizzle.
907 unsigned num_components
:3;
910 * Does the swizzle contain duplicate components?
912 * L-value swizzles cannot contain duplicate components.
914 unsigned has_duplicates
:1;
918 class ir_swizzle
: public ir_rvalue
{
920 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
923 ir_swizzle(ir_rvalue
*val
, const unsigned *components
, unsigned count
);
925 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
927 virtual ir_instruction
*clone(struct hash_table
*) const;
929 virtual ir_swizzle
*as_swizzle()
935 * Construct an ir_swizzle from the textual representation. Can fail.
937 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
939 virtual void accept(ir_visitor
*v
)
944 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
948 return val
->is_lvalue() && !mask
.has_duplicates
;
952 * Get the variable that is ultimately referenced by an r-value
954 virtual ir_variable
*variable_referenced();
957 ir_swizzle_mask mask
;
961 * Initialize the mask component of a swizzle
963 * This is used by the \c ir_swizzle constructors.
965 void init_mask(const unsigned *components
, unsigned count
);
969 class ir_dereference
: public ir_rvalue
{
971 virtual ir_dereference
*as_dereference()
979 * Get the variable that is ultimately referenced by an r-value
981 virtual ir_variable
*variable_referenced() = 0;
985 class ir_dereference_variable
: public ir_dereference
{
987 ir_dereference_variable(ir_variable
*var
);
989 virtual ir_instruction
*clone(struct hash_table
*) const;
991 virtual ir_dereference_variable
*as_dereference_variable()
997 * Get the variable that is ultimately referenced by an r-value
999 virtual ir_variable
*variable_referenced()
1004 virtual ir_variable
*whole_variable_referenced()
1006 /* ir_dereference_variable objects always dereference the entire
1007 * variable. However, if this dereference is dereferenced by anything
1008 * else, the complete deferefernce chain is not a whole-variable
1009 * dereference. This method should only be called on the top most
1010 * ir_rvalue in a dereference chain.
1015 virtual void accept(ir_visitor
*v
)
1020 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1023 * Object being dereferenced.
1029 class ir_dereference_array
: public ir_dereference
{
1031 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
1033 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
1035 virtual ir_instruction
*clone(struct hash_table
*) const;
1037 virtual ir_dereference_array
*as_dereference_array()
1043 * Get the variable that is ultimately referenced by an r-value
1045 virtual ir_variable
*variable_referenced()
1047 return this->array
->variable_referenced();
1050 virtual void accept(ir_visitor
*v
)
1055 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1058 ir_rvalue
*array_index
;
1061 void set_array(ir_rvalue
*value
);
1065 class ir_dereference_record
: public ir_dereference
{
1067 ir_dereference_record(ir_rvalue
*value
, const char *field
);
1069 ir_dereference_record(ir_variable
*var
, const char *field
);
1071 virtual ir_instruction
*clone(struct hash_table
*) const;
1074 * Get the variable that is ultimately referenced by an r-value
1076 virtual ir_variable
*variable_referenced()
1078 return this->record
->variable_referenced();
1081 virtual void accept(ir_visitor
*v
)
1086 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1094 * Data stored in an ir_constant
1096 union ir_constant_data
{
1104 class ir_constant
: public ir_rvalue
{
1106 ir_constant(const struct glsl_type
*type
, const ir_constant_data
*data
);
1107 ir_constant(bool b
);
1108 ir_constant(unsigned int u
);
1110 ir_constant(float f
);
1113 * Construct an ir_constant from a list of ir_constant values
1115 ir_constant(const struct glsl_type
*type
, exec_list
*values
);
1118 * Construct an ir_constant from a scalar component of another ir_constant
1120 * The new \c ir_constant inherits the type of the component from the
1124 * In the case of a matrix constant, the new constant is a scalar, \b not
1127 ir_constant(const ir_constant
*c
, unsigned i
);
1129 virtual ir_instruction
*clone(struct hash_table
*) const;
1131 virtual ir_constant
*as_constant()
1136 virtual void accept(ir_visitor
*v
)
1141 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1144 * Get a particular component of a constant as a specific type
1146 * This is useful, for example, to get a value from an integer constant
1147 * as a float or bool. This appears frequently when constructors are
1148 * called with all constant parameters.
1151 bool get_bool_component(unsigned i
) const;
1152 float get_float_component(unsigned i
) const;
1153 int get_int_component(unsigned i
) const;
1154 unsigned get_uint_component(unsigned i
) const;
1157 ir_constant
*get_record_field(const char *name
);
1160 * Determine whether a constant has the same value as another constant
1162 bool has_value(const ir_constant
*) const;
1165 * Value of the constant.
1167 * The field used to back the values supplied by the constant is determined
1168 * by the type associated with the \c ir_instruction. Constants may be
1169 * scalars, vectors, or matrices.
1171 union ir_constant_data value
;
1173 exec_list components
;
1177 * Parameterless constructor only used by the clone method
1183 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
1185 void validate_ir_tree(exec_list
*instructions
);
1188 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1189 struct _mesa_glsl_parse_state
*state
);
1192 _mesa_glsl_initialize_functions(exec_list
*instructions
,
1193 struct _mesa_glsl_parse_state
*state
);