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.
26 #ifndef LOOP_ANALYSIS_H
27 #define LOOP_ANALYSIS_H
30 #include "program/hash_table.h"
33 * Analyze and classify all variables used in all loops in the instruction list
35 extern class loop_state
*
36 analyze_loop_variables(exec_list
*instructions
);
40 * Fill in loop control fields
42 * Based on analysis of loop variables, this function tries to remove
43 * redundant sequences in the loop of the form
45 * (if (expression bool ...) (break))
47 * For example, if it is provable that one loop exit condition will
48 * always be satisfied before another, the unnecessary exit condition will be
52 set_loop_controls(exec_list
*instructions
, loop_state
*ls
);
56 unroll_loops(exec_list
*instructions
, loop_state
*ls
,
57 const struct gl_shader_compiler_options
*options
);
60 find_initial_value(ir_loop
*loop
, ir_variable
*var
);
63 calculate_iterations(ir_rvalue
*from
, ir_rvalue
*to
, ir_rvalue
*increment
,
64 enum ir_expression_operation op
);
68 * Tracking for all variables used in a loop
70 class loop_variable_state
: public exec_node
{
72 class loop_variable
*get(const ir_variable
*);
73 class loop_variable
*insert(ir_variable
*);
74 class loop_variable
*get_or_insert(ir_variable
*, bool in_assignee
);
75 class loop_terminator
*insert(ir_if
*);
79 * Variables that have not yet been classified
84 * Variables whose values are constant within the body of the loop
86 * This list contains \c loop_variable objects.
91 * Induction variables for this loop
93 * This list contains \c loop_variable objects.
95 exec_list induction_variables
;
98 * Simple if-statements that lead to the termination of the loop
100 * This list contains \c loop_terminator objects.
102 * \sa is_loop_terminator
104 exec_list terminators
;
107 * If any of the terminators in \c terminators leads to termination of the
108 * loop after a constant number of iterations, this is the terminator that
109 * leads to termination after the smallest number of iterations. Otherwise
112 loop_terminator
*limiting_terminator
;
115 * Hash table containing all variables accessed in this loop
117 hash_table
*var_hash
;
120 * Number of ir_loop_jump instructions that operate on this loop
122 unsigned num_loop_jumps
;
125 * Whether this loop contains any function calls.
129 loop_variable_state()
131 this->num_loop_jumps
= 0;
132 this->contains_calls
= false;
133 this->var_hash
= hash_table_ctor(0, hash_table_pointer_hash
,
134 hash_table_pointer_compare
);
135 this->limiting_terminator
= NULL
;
138 ~loop_variable_state()
140 hash_table_dtor(this->var_hash
);
143 static void* operator new(size_t size
, void *ctx
)
145 void *lvs
= ralloc_size(ctx
, size
);
148 ralloc_set_destructor(lvs
, (void (*)(void*)) destructor
);
155 destructor(loop_variable_state
*lvs
)
157 lvs
->~loop_variable_state();
162 class loop_variable
: public exec_node
{
164 /** The variable in question. */
167 /** Is the variable read in the loop before it is written? */
168 bool read_before_write
;
170 /** Are all variables in the RHS of the assignment loop constants? */
174 * Is there an assignment to the variable that is conditional, or inside a
177 bool conditional_or_nested_assignment
;
179 /** Reference to the first assignment to the variable in the loop body. */
180 ir_assignment
*first_assignment
;
182 /** Number of assignments to the variable in the loop body. */
183 unsigned num_assignments
;
186 * Increment value for a loop induction variable
188 * If this is a loop induction variable, the amount by which the variable
189 * is incremented on each iteration through the loop.
191 * If this is not a loop induction variable, NULL.
193 ir_rvalue
*increment
;
196 inline bool is_induction_var() const
198 /* Induction variables always have a non-null increment, and vice
201 return this->increment
!= NULL
;
205 inline bool is_loop_constant() const
207 const bool is_const
= (this->num_assignments
== 0)
208 || (((this->num_assignments
== 1)
209 && !this->conditional_or_nested_assignment
210 && !this->read_before_write
211 && this->rhs_clean
) || this->var
->data
.read_only
);
213 /* If the RHS of *the* assignment is clean, then there must be exactly
214 * one assignment of the variable.
216 assert((this->rhs_clean
&& (this->num_assignments
== 1))
217 || !this->rhs_clean
);
222 void record_reference(bool in_assignee
,
223 bool in_conditional_code_or_nested_loop
,
224 ir_assignment
*current_assignment
);
228 class loop_terminator
: public exec_node
{
231 : ir(NULL
), iterations(-1)
236 * Statement which terminates the loop.
241 * The number of iterations after which the terminator is known to
242 * terminate the loop (if that is a fixed value). Otherwise -1.
253 * Get the loop variable state data for a particular loop
255 loop_variable_state
*get(const ir_loop
*);
257 loop_variable_state
*insert(ir_loop
*ir
);
265 * Hash table containing all loops that have been analyzed.
271 friend loop_state
*analyze_loop_variables(exec_list
*instructions
);
274 #endif /* LOOP_ANALYSIS_H */