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.
30 #include "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
38 * Base class of all IR instructions
40 class ir_instruction
: public exec_node
{
42 const struct glsl_type
*type
;
44 class ir_constant
*constant_expression_value();
45 virtual void accept(ir_visitor
*) = 0;
46 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*) = 0;
49 * \name IR instruction downcast functions
51 * These functions either cast the object to a derived class or return
52 * \c NULL if the object's type does not match the specified derived class.
53 * Additional downcast functions will be added as needed.
56 virtual class ir_variable
* as_variable() { return NULL
; }
57 virtual class ir_function
* as_function() { return NULL
; }
58 virtual class ir_dereference
* as_dereference() { return NULL
; }
59 virtual class ir_dereference_array
* as_dereference_array() { return NULL
; }
60 virtual class ir_rvalue
* as_rvalue() { return NULL
; }
61 virtual class ir_loop
* as_loop() { return NULL
; }
62 virtual class ir_assignment
* as_assignment() { return NULL
; }
63 virtual class ir_call
* as_call() { return NULL
; }
64 virtual class ir_return
* as_return() { return NULL
; }
65 virtual class ir_if
* as_if() { return NULL
; }
66 virtual class ir_swizzle
* as_swizzle() { return NULL
; }
67 virtual class ir_constant
* as_constant() { return NULL
; }
78 class ir_rvalue
: public ir_instruction
{
80 virtual ir_rvalue
* as_rvalue()
85 virtual bool is_lvalue()
91 * Get the variable that is ultimately referenced by an r-value
93 virtual ir_variable
*variable_referenced()
100 * If an r-value is a reference to a whole variable, get that variable
103 * Pointer to a variable that is completely dereferenced by the r-value. If
104 * the r-value is not a dereference or the dereference does not access the
105 * entire variable (i.e., it's just one array element, struct field), \c NULL
108 virtual ir_variable
*whole_variable_referenced()
121 enum ir_variable_mode
{
129 enum ir_varaible_interpolation
{
136 class ir_variable
: public ir_instruction
{
138 ir_variable(const struct glsl_type
*, const char *);
140 virtual ir_variable
*as_variable()
145 virtual void accept(ir_visitor
*v
)
150 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
153 * Duplicate an IR variable
156 * This will probably be made \c virtual and moved to the base class
159 ir_variable
*clone() const
161 ir_variable
*var
= new ir_variable(type
, name
);
163 var
->max_array_access
= this->max_array_access
;
164 var
->read_only
= this->read_only
;
165 var
->centroid
= this->centroid
;
166 var
->invariant
= this->invariant
;
167 var
->mode
= this->mode
;
168 var
->interpolation
= this->interpolation
;
176 * Highest element accessed with a constant expression array index
178 * Not used for non-array variables.
180 unsigned max_array_access
;
182 unsigned read_only
:1;
184 unsigned invariant
:1;
185 /** If the variable is initialized outside of the scope of the shader */
186 unsigned shader_in
:1;
188 * If the variable value is later used outside of the scope of the shader.
190 unsigned shader_out
:1;
193 unsigned interpolation
:2;
196 * Flag that the whole array is assignable
198 * In GLSL 1.20 and later whole arrays are assignable (and comparable for
199 * equality). This flag enables this behavior.
201 unsigned array_lvalue
:1;
204 * Emit a warning if this variable is accessed.
206 const char *warn_extension
;
209 * Value assigned in the initializer of a variable declared "const"
211 ir_constant
*constant_value
;
217 * The representation of a function instance; may be the full definition or
218 * simply a prototype.
220 class ir_function_signature
: public ir_instruction
{
221 /* An ir_function_signature will be part of the list of signatures in
225 ir_function_signature(const glsl_type
*return_type
);
227 virtual void accept(ir_visitor
*v
)
232 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
235 * Get the name of the function for which this is a signature
237 const char *function_name() const;
240 * Check whether the qualifiers match between this signature's parameters
241 * and the supplied parameter list. If not, returns the name of the first
242 * parameter with mismatched qualifiers (for use in error messages).
244 const char *qualifiers_match(exec_list
*params
);
247 * Replace the current parameter list with the given one. This is useful
248 * if the current information came from a prototype, and either has invalid
249 * or missing parameter names.
251 void replace_parameters(exec_list
*new_params
);
254 * Function return type.
256 * \note This discards the optional precision qualifier.
258 const struct glsl_type
*return_type
;
261 * List of ir_variable of function parameters.
263 * This represents the storage. The paramaters passed in a particular
264 * call will be in ir_call::actual_paramaters.
266 struct exec_list parameters
;
268 /** Whether or not this function has a body (which may be empty). */
269 unsigned is_defined
:1;
271 /** Body of instructions in the function. */
272 struct exec_list body
;
275 /** Function of which this signature is one overload. */
276 class ir_function
*function
;
278 friend class ir_function
;
283 * Header for tracking multiple overloaded functions with the same name.
284 * Contains a list of ir_function_signatures representing each of the
287 class ir_function
: public ir_instruction
{
289 ir_function(const char *name
);
291 virtual ir_function
*as_function()
296 virtual void accept(ir_visitor
*v
)
301 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
303 void add_signature(ir_function_signature
*sig
)
305 sig
->function
= this;
306 signatures
.push_tail(sig
);
310 * Get an iterator for the set of function signatures
312 exec_list_iterator
iterator()
314 return signatures
.iterator();
318 * Find a signature that matches a set of actual parameters, taking implicit
319 * conversions into account.
321 const ir_function_signature
*matching_signature(exec_list
*actual_param
);
324 * Find a signature that exactly matches a set of actual parameters without
325 * any implicit type conversions.
327 ir_function_signature
*exact_matching_signature(exec_list
*actual_ps
);
330 * Name of the function.
336 * List of ir_function_signature for each overloaded function with this name.
338 struct exec_list signatures
;
341 inline const char *ir_function_signature::function_name() const
343 return function
->name
;
349 * IR instruction representing high-level if-statements
351 class ir_if
: public ir_instruction
{
353 ir_if(ir_rvalue
*condition
)
354 : condition(condition
)
359 virtual ir_if
*as_if()
364 virtual void accept(ir_visitor
*v
)
369 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
371 ir_rvalue
*condition
;
372 /** List of ir_instruction for the body of the then branch */
373 exec_list then_instructions
;
374 /** List of ir_instruction for the body of the else branch */
375 exec_list else_instructions
;
380 * IR instruction representing a high-level loop structure.
382 class ir_loop
: public ir_instruction
{
384 ir_loop() : from(NULL
), to(NULL
), increment(NULL
), counter(NULL
)
389 virtual void accept(ir_visitor
*v
)
394 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
396 virtual ir_loop
*as_loop()
402 * Get an iterator for the instructions of the loop body
404 exec_list_iterator
iterator()
406 return body_instructions
.iterator();
409 /** List of ir_instruction that make up the body of the loop. */
410 exec_list body_instructions
;
413 * \name Loop counter and controls
418 ir_rvalue
*increment
;
419 ir_variable
*counter
;
424 class ir_assignment
: public ir_rvalue
{
426 ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
, ir_rvalue
*condition
);
428 virtual void accept(ir_visitor
*v
)
433 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
435 virtual ir_assignment
* as_assignment()
441 * Left-hand side of the assignment.
446 * Value being assigned
451 * Optional condition for the assignment.
453 ir_rvalue
*condition
;
456 /* Update ir_expression::num_operands() and operator_strs when
457 * updating this list.
459 enum ir_expression_operation
{
472 ir_unop_f2i
, /**< Float-to-integer conversion. */
473 ir_unop_i2f
, /**< Integer-to-float conversion. */
474 ir_unop_f2b
, /**< Float-to-boolean conversion */
475 ir_unop_b2f
, /**< Boolean-to-float conversion */
476 ir_unop_i2b
, /**< int-to-boolean conversion */
477 ir_unop_b2i
, /**< Boolean-to-int conversion */
478 ir_unop_u2f
, /**< Unsigned-to-float conversion. */
481 * \name Unary floating-point rounding operations.
490 * \name Trigonometric operations.
498 * \name Partial derivatives.
512 * \name Binary comparison operators
524 * \name Bit-wise binary operations.
545 class ir_expression
: public ir_rvalue
{
547 ir_expression(int op
, const struct glsl_type
*type
,
548 ir_rvalue
*, ir_rvalue
*);
550 static unsigned int get_num_operands(ir_expression_operation
);
551 unsigned int get_num_operands()
553 return get_num_operands(operation
);
557 * Return a string representing this expression's operator.
559 const char *operator_string();
562 * Do a reverse-lookup to translate the given string into an operator.
564 static ir_expression_operation
get_operator(const char *);
566 virtual void accept(ir_visitor
*v
)
571 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
573 ir_expression
*clone();
575 ir_expression_operation operation
;
576 ir_rvalue
*operands
[2];
581 * IR instruction representing a function call
583 class ir_call
: public ir_rvalue
{
585 ir_call(const ir_function_signature
*callee
, exec_list
*actual_parameters
)
588 assert(callee
->return_type
!= NULL
);
589 type
= callee
->return_type
;
590 actual_parameters
->move_nodes_to(& this->actual_parameters
);
593 virtual ir_call
*as_call()
598 virtual void accept(ir_visitor
*v
)
603 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
606 * Get a generic ir_call object when an error occurs
608 static ir_call
*get_error_instruction();
611 * Get an iterator for the set of acutal parameters
613 exec_list_iterator
iterator()
615 return actual_parameters
.iterator();
619 * Get the name of the function being called.
621 const char *callee_name() const
623 return callee
->function_name();
626 const ir_function_signature
*get_callee()
632 * Generates an inline version of the function before @ir,
633 * returning the return value of the function.
635 ir_rvalue
*generate_inline(ir_instruction
*ir
);
644 const ir_function_signature
*callee
;
646 /* List of ir_rvalue of paramaters passed in this call. */
647 exec_list actual_parameters
;
652 * \name Jump-like IR instructions.
654 * These include \c break, \c continue, \c return, and \c discard.
657 class ir_jump
: public ir_instruction
{
665 class ir_return
: public ir_jump
{
673 ir_return(ir_rvalue
*value
)
679 virtual ir_return
*as_return()
684 ir_rvalue
*get_value() const
689 virtual void accept(ir_visitor
*v
)
694 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
701 * Jump instructions used inside loops
703 * These include \c break and \c continue. The \c break within a loop is
704 * different from the \c break within a switch-statement.
708 class ir_loop_jump
: public ir_jump
{
715 ir_loop_jump(ir_loop
*loop
, jump_mode mode
)
716 : loop(loop
), mode(mode
)
721 virtual void accept(ir_visitor
*v
)
726 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
728 bool is_break() const
730 return mode
== jump_break
;
733 bool is_continue() const
735 return mode
== jump_continue
;
739 /** Loop containing this break instruction. */
742 /** Mode selector for the jump instruction. */
749 * Texture sampling opcodes used in ir_texture
751 enum ir_texture_opcode
{
752 ir_tex
, /* Regular texture look-up */
753 ir_txb
, /* Texture look-up with LOD bias */
754 ir_txl
, /* Texture look-up with explicit LOD */
755 ir_txd
, /* Texture look-up with partial derivatvies */
756 ir_txf
/* Texel fetch with explicit LOD */
761 * IR instruction to sample a texture
763 * The specific form of the IR instruction depends on the \c mode value
764 * selected from \c ir_texture_opcodes. In the printed IR, these will
768 * | Projection divisor
769 * | | Shadow comparitor
772 * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
773 * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
774 * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
775 * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
776 * (txf (sampler) (coordinate) (0 0 0) (lod))
778 class ir_texture
: public ir_rvalue
{
780 ir_texture(enum ir_texture_opcode op
)
781 : op(op
), projector(NULL
), shadow_comparitor(NULL
)
786 virtual void accept(ir_visitor
*v
)
791 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
794 * Return a string representing the ir_texture_opcode.
796 const char *opcode_string();
798 /** Set the sampler and infer the type. */
799 void set_sampler(ir_dereference
*sampler
);
802 * Do a reverse-lookup to translate a string into an ir_texture_opcode.
804 static ir_texture_opcode
get_opcode(const char *);
806 enum ir_texture_opcode op
;
808 /** Sampler to use for the texture access. */
809 ir_dereference
*sampler
;
811 /** Texture coordinate to sample */
812 ir_rvalue
*coordinate
;
815 * Value used for projective divide.
817 * If there is no projective divide (the common case), this will be
818 * \c NULL. Optimization passes should check for this to point to a constant
819 * of 1.0 and replace that with \c NULL.
821 ir_rvalue
*projector
;
824 * Coordinate used for comparison on shadow look-ups.
826 * If there is no shadow comparison, this will be \c NULL. For the
827 * \c ir_txf opcode, this *must* be \c NULL.
829 ir_rvalue
*shadow_comparitor
;
831 /** Explicit texel offsets. */
832 signed char offsets
[3];
835 ir_rvalue
*lod
; /**< Floating point LOD */
836 ir_rvalue
*bias
; /**< Floating point LOD bias */
838 ir_rvalue
*dPdx
; /**< Partial derivative of coordinate wrt X */
839 ir_rvalue
*dPdy
; /**< Partial derivative of coordinate wrt Y */
845 struct ir_swizzle_mask
{
852 * Number of components in the swizzle.
854 unsigned num_components
:3;
857 * Does the swizzle contain duplicate components?
859 * L-value swizzles cannot contain duplicate components.
861 unsigned has_duplicates
:1;
865 class ir_swizzle
: public ir_rvalue
{
867 ir_swizzle(ir_rvalue
*, unsigned x
, unsigned y
, unsigned z
, unsigned w
,
869 ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
);
871 virtual ir_swizzle
*as_swizzle()
878 return new ir_swizzle(this->val
, this->mask
);
882 * Construct an ir_swizzle from the textual representation. Can fail.
884 static ir_swizzle
*create(ir_rvalue
*, const char *, unsigned vector_length
);
886 virtual void accept(ir_visitor
*v
)
891 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
895 return val
->is_lvalue() && !mask
.has_duplicates
;
899 * Get the variable that is ultimately referenced by an r-value
901 virtual ir_variable
*variable_referenced();
904 ir_swizzle_mask mask
;
908 class ir_dereference
: public ir_rvalue
{
910 virtual ir_dereference
*as_dereference()
918 * Get the variable that is ultimately referenced by an r-value
920 virtual ir_variable
*variable_referenced() = 0;
924 class ir_dereference_variable
: public ir_dereference
{
926 ir_dereference_variable(ir_variable
*var
);
929 * Get the variable that is ultimately referenced by an r-value
931 virtual ir_variable
*variable_referenced()
936 virtual ir_variable
*whole_variable_referenced()
938 /* ir_dereference_variable objects always dereference the entire
939 * variable. However, if this dereference is dereferenced by anything
940 * else, the complete deferefernce chain is not a whole-variable
941 * dereference. This method should only be called on the top most
942 * ir_rvalue in a dereference chain.
947 virtual void accept(ir_visitor
*v
)
952 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
955 * Object being dereferenced.
961 class ir_dereference_array
: public ir_dereference
{
963 ir_dereference_array(ir_rvalue
*value
, ir_rvalue
*array_index
);
965 ir_dereference_array(ir_variable
*var
, ir_rvalue
*array_index
);
967 virtual ir_dereference_array
*as_dereference_array()
973 * Get the variable that is ultimately referenced by an r-value
975 virtual ir_variable
*variable_referenced()
977 return this->array
->variable_referenced();
980 virtual void accept(ir_visitor
*v
)
985 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
988 ir_rvalue
*array_index
;
991 void set_array(ir_rvalue
*value
);
995 class ir_dereference_record
: public ir_dereference
{
997 ir_dereference_record(ir_rvalue
*value
, const char *field
);
999 ir_dereference_record(ir_variable
*var
, const char *field
);
1002 * Get the variable that is ultimately referenced by an r-value
1004 virtual ir_variable
*variable_referenced()
1006 return this->record
->variable_referenced();
1009 virtual void accept(ir_visitor
*v
)
1014 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1021 class ir_constant
: public ir_rvalue
{
1023 ir_constant(const struct glsl_type
*type
, const void *data
);
1024 ir_constant(bool b
);
1025 ir_constant(unsigned int u
);
1027 ir_constant(float f
);
1030 * Construct an ir_constant from a scalar component of another ir_constant
1032 * The new \c ir_constant inherits the type of the component from the
1036 * In the case of a matrix constant, the new constant is a scalar, \b not
1039 ir_constant(const ir_constant
*c
, unsigned i
);
1041 virtual ir_constant
*as_constant()
1046 virtual void accept(ir_visitor
*v
)
1051 virtual ir_visitor_status
accept(ir_hierarchical_visitor
*);
1053 ir_constant
*clone()
1055 return new ir_constant(this->type
, &this->value
);
1059 * Get a particular component of a constant as a specific type
1061 * This is useful, for example, to get a value from an integer constant
1062 * as a float or bool. This appears frequently when constructors are
1063 * called with all constant parameters.
1066 bool get_bool_component(unsigned i
) const;
1067 float get_float_component(unsigned i
) const;
1068 int get_int_component(unsigned i
) const;
1069 unsigned get_uint_component(unsigned i
) const;
1073 * Value of the constant.
1075 * The field used to back the values supplied by the constant is determined
1076 * by the type associated with the \c ir_instruction. Constants may be
1077 * scalars, vectors, or matrices.
1088 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
);
1091 _mesa_glsl_initialize_variables(exec_list
*instructions
,
1092 struct _mesa_glsl_parse_state
*state
);
1095 _mesa_glsl_initialize_functions(exec_list
*instructions
,
1096 struct _mesa_glsl_parse_state
*state
);