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.
25 #ifndef LOOP_ANALYSIS_H
26 #define LOOP_ANALYSIS_H
29 #include "util/hash_table.h"
32 * Analyze and classify all variables used in all loops in the instruction list
34 extern class loop_state
*
35 analyze_loop_variables(exec_list
*instructions
);
39 unroll_loops(exec_list
*instructions
, loop_state
*ls
,
40 const struct gl_shader_compiler_options
*options
);
44 * Tracking for all variables used in a loop
46 class loop_variable_state
: public exec_node
{
48 class loop_variable
*get(const ir_variable
*);
49 class loop_variable
*insert(ir_variable
*);
50 class loop_variable
*get_or_insert(ir_variable
*, bool in_assignee
);
51 class loop_terminator
*insert(ir_if
*);
55 * Variables that have not yet been classified
60 * Variables whose values are constant within the body of the loop
62 * This list contains \c loop_variable objects.
67 * Induction variables for this loop
69 * This list contains \c loop_variable objects.
71 exec_list induction_variables
;
74 * Simple if-statements that lead to the termination of the loop
76 * This list contains \c loop_terminator objects.
78 * \sa is_loop_terminator
80 exec_list terminators
;
83 * If any of the terminators in \c terminators leads to termination of the
84 * loop after a constant number of iterations, this is the terminator that
85 * leads to termination after the smallest number of iterations. Otherwise
88 loop_terminator
*limiting_terminator
;
91 * Hash table containing all variables accessed in this loop
96 * Number of ir_loop_jump instructions that operate on this loop
98 unsigned num_loop_jumps
;
101 * Whether this loop contains any function calls.
105 loop_variable_state()
107 this->num_loop_jumps
= 0;
108 this->contains_calls
= false;
109 this->var_hash
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
110 _mesa_key_pointer_equal
);
111 this->limiting_terminator
= NULL
;
114 ~loop_variable_state()
116 _mesa_hash_table_destroy(this->var_hash
, NULL
);
119 DECLARE_RALLOC_CXX_OPERATORS(loop_variable_state
)
123 class loop_variable
: public exec_node
{
125 /** The variable in question. */
128 /** Is the variable read in the loop before it is written? */
129 bool read_before_write
;
131 /** Are all variables in the RHS of the assignment loop constants? */
135 * Is there an assignment to the variable that is conditional, or inside a
138 bool conditional_or_nested_assignment
;
140 /** Reference to the first assignment to the variable in the loop body. */
141 ir_assignment
*first_assignment
;
143 /** Number of assignments to the variable in the loop body. */
144 unsigned num_assignments
;
147 * Increment value for a loop induction variable
149 * If this is a loop induction variable, the amount by which the variable
150 * is incremented on each iteration through the loop.
152 * If this is not a loop induction variable, NULL.
154 ir_rvalue
*increment
;
157 inline bool is_induction_var() const
159 /* Induction variables always have a non-null increment, and vice
162 return this->increment
!= NULL
;
166 inline bool is_loop_constant() const
168 const bool is_const
= (this->num_assignments
== 0)
169 || (((this->num_assignments
== 1)
170 && !this->conditional_or_nested_assignment
171 && !this->read_before_write
172 && this->rhs_clean
) || this->var
->data
.read_only
);
174 /* If the RHS of *the* assignment is clean, then there must be exactly
175 * one assignment of the variable.
177 assert((this->rhs_clean
&& (this->num_assignments
== 1))
178 || !this->rhs_clean
);
183 void record_reference(bool in_assignee
,
184 bool in_conditional_code_or_nested_loop
,
185 ir_assignment
*current_assignment
);
189 class loop_terminator
: public exec_node
{
192 : ir(NULL
), iterations(-1)
197 * Statement which terminates the loop.
202 * The number of iterations after which the terminator is known to
203 * terminate the loop (if that is a fixed value). Otherwise -1.
214 * Get the loop variable state data for a particular loop
216 loop_variable_state
*get(const ir_loop
*);
218 loop_variable_state
*insert(ir_loop
*ir
);
226 * Hash table containing all loops that have been analyzed.
232 friend loop_state
*analyze_loop_variables(exec_list
*instructions
);
235 #endif /* LOOP_ANALYSIS_H */