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 * Fill in loop control fields
41 * Based on analysis of loop variables, this function tries to remove
42 * redundant sequences in the loop of the form
44 * (if (expression bool ...) (break))
46 * For example, if it is provable that one loop exit condition will
47 * always be satisfied before another, the unnecessary exit condition will be
51 set_loop_controls(exec_list
*instructions
, loop_state
*ls
);
55 unroll_loops(exec_list
*instructions
, loop_state
*ls
,
56 const struct gl_shader_compiler_options
*options
);
59 find_initial_value(ir_loop
*loop
, ir_variable
*var
);
62 calculate_iterations(ir_rvalue
*from
, ir_rvalue
*to
, ir_rvalue
*increment
,
63 enum ir_expression_operation op
);
67 * Tracking for all variables used in a loop
69 class loop_variable_state
: public exec_node
{
71 class loop_variable
*get(const ir_variable
*);
72 class loop_variable
*insert(ir_variable
*);
73 class loop_variable
*get_or_insert(ir_variable
*, bool in_assignee
);
74 class loop_terminator
*insert(ir_if
*);
78 * Variables that have not yet been classified
83 * Variables whose values are constant within the body of the loop
85 * This list contains \c loop_variable objects.
90 * Induction variables for this loop
92 * This list contains \c loop_variable objects.
94 exec_list induction_variables
;
97 * Simple if-statements that lead to the termination of the loop
99 * This list contains \c loop_terminator objects.
101 * \sa is_loop_terminator
103 exec_list terminators
;
106 * If any of the terminators in \c terminators leads to termination of the
107 * loop after a constant number of iterations, this is the terminator that
108 * leads to termination after the smallest number of iterations. Otherwise
111 loop_terminator
*limiting_terminator
;
114 * Hash table containing all variables accessed in this loop
116 hash_table
*var_hash
;
119 * Number of ir_loop_jump instructions that operate on this loop
121 unsigned num_loop_jumps
;
124 * Whether this loop contains any function calls.
128 loop_variable_state()
130 this->num_loop_jumps
= 0;
131 this->contains_calls
= false;
132 this->var_hash
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
133 _mesa_key_pointer_equal
);
134 this->limiting_terminator
= NULL
;
137 ~loop_variable_state()
139 _mesa_hash_table_destroy(this->var_hash
, NULL
);
142 DECLARE_RALLOC_CXX_OPERATORS(loop_variable_state
)
146 class loop_variable
: public exec_node
{
148 /** The variable in question. */
151 /** Is the variable read in the loop before it is written? */
152 bool read_before_write
;
154 /** Are all variables in the RHS of the assignment loop constants? */
158 * Is there an assignment to the variable that is conditional, or inside a
161 bool conditional_or_nested_assignment
;
163 /** Reference to the first assignment to the variable in the loop body. */
164 ir_assignment
*first_assignment
;
166 /** Number of assignments to the variable in the loop body. */
167 unsigned num_assignments
;
170 * Increment value for a loop induction variable
172 * If this is a loop induction variable, the amount by which the variable
173 * is incremented on each iteration through the loop.
175 * If this is not a loop induction variable, NULL.
177 ir_rvalue
*increment
;
180 inline bool is_induction_var() const
182 /* Induction variables always have a non-null increment, and vice
185 return this->increment
!= NULL
;
189 inline bool is_loop_constant() const
191 const bool is_const
= (this->num_assignments
== 0)
192 || (((this->num_assignments
== 1)
193 && !this->conditional_or_nested_assignment
194 && !this->read_before_write
195 && this->rhs_clean
) || this->var
->data
.read_only
);
197 /* If the RHS of *the* assignment is clean, then there must be exactly
198 * one assignment of the variable.
200 assert((this->rhs_clean
&& (this->num_assignments
== 1))
201 || !this->rhs_clean
);
206 void record_reference(bool in_assignee
,
207 bool in_conditional_code_or_nested_loop
,
208 ir_assignment
*current_assignment
);
212 class loop_terminator
: public exec_node
{
215 : ir(NULL
), iterations(-1)
220 * Statement which terminates the loop.
225 * The number of iterations after which the terminator is known to
226 * terminate the loop (if that is a fixed value). Otherwise -1.
237 * Get the loop variable state data for a particular loop
239 loop_variable_state
*get(const ir_loop
*);
241 loop_variable_state
*insert(ir_loop
*ir
);
249 * Hash table containing all loops that have been analyzed.
255 friend loop_state
*analyze_loop_variables(exec_list
*instructions
);
258 #endif /* LOOP_ANALYSIS_H */