glsl: move loop analysis helpers to loop_analysis.cpp
[mesa.git] / src / compiler / glsl / loop_analysis.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #ifndef LOOP_ANALYSIS_H
26 #define LOOP_ANALYSIS_H
27
28 #include "ir.h"
29 #include "util/hash_table.h"
30
31 /**
32 * Analyze and classify all variables used in all loops in the instruction list
33 */
34 extern class loop_state *
35 analyze_loop_variables(exec_list *instructions);
36
37
38 /**
39 * Fill in loop control fields
40 *
41 * Based on analysis of loop variables, this function tries to remove
42 * redundant sequences in the loop of the form
43 *
44 * (if (expression bool ...) (break))
45 *
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
48 * removed.
49 */
50 extern bool
51 set_loop_controls(exec_list *instructions, loop_state *ls);
52
53
54 extern bool
55 unroll_loops(exec_list *instructions, loop_state *ls,
56 const struct gl_shader_compiler_options *options);
57
58
59 /**
60 * Tracking for all variables used in a loop
61 */
62 class loop_variable_state : public exec_node {
63 public:
64 class loop_variable *get(const ir_variable *);
65 class loop_variable *insert(ir_variable *);
66 class loop_variable *get_or_insert(ir_variable *, bool in_assignee);
67 class loop_terminator *insert(ir_if *);
68
69
70 /**
71 * Variables that have not yet been classified
72 */
73 exec_list variables;
74
75 /**
76 * Variables whose values are constant within the body of the loop
77 *
78 * This list contains \c loop_variable objects.
79 */
80 exec_list constants;
81
82 /**
83 * Induction variables for this loop
84 *
85 * This list contains \c loop_variable objects.
86 */
87 exec_list induction_variables;
88
89 /**
90 * Simple if-statements that lead to the termination of the loop
91 *
92 * This list contains \c loop_terminator objects.
93 *
94 * \sa is_loop_terminator
95 */
96 exec_list terminators;
97
98 /**
99 * If any of the terminators in \c terminators leads to termination of the
100 * loop after a constant number of iterations, this is the terminator that
101 * leads to termination after the smallest number of iterations. Otherwise
102 * NULL.
103 */
104 loop_terminator *limiting_terminator;
105
106 /**
107 * Hash table containing all variables accessed in this loop
108 */
109 hash_table *var_hash;
110
111 /**
112 * Number of ir_loop_jump instructions that operate on this loop
113 */
114 unsigned num_loop_jumps;
115
116 /**
117 * Whether this loop contains any function calls.
118 */
119 bool contains_calls;
120
121 loop_variable_state()
122 {
123 this->num_loop_jumps = 0;
124 this->contains_calls = false;
125 this->var_hash = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
126 _mesa_key_pointer_equal);
127 this->limiting_terminator = NULL;
128 }
129
130 ~loop_variable_state()
131 {
132 _mesa_hash_table_destroy(this->var_hash, NULL);
133 }
134
135 DECLARE_RALLOC_CXX_OPERATORS(loop_variable_state)
136 };
137
138
139 class loop_variable : public exec_node {
140 public:
141 /** The variable in question. */
142 ir_variable *var;
143
144 /** Is the variable read in the loop before it is written? */
145 bool read_before_write;
146
147 /** Are all variables in the RHS of the assignment loop constants? */
148 bool rhs_clean;
149
150 /**
151 * Is there an assignment to the variable that is conditional, or inside a
152 * nested loop?
153 */
154 bool conditional_or_nested_assignment;
155
156 /** Reference to the first assignment to the variable in the loop body. */
157 ir_assignment *first_assignment;
158
159 /** Number of assignments to the variable in the loop body. */
160 unsigned num_assignments;
161
162 /**
163 * Increment value for a loop induction variable
164 *
165 * If this is a loop induction variable, the amount by which the variable
166 * is incremented on each iteration through the loop.
167 *
168 * If this is not a loop induction variable, NULL.
169 */
170 ir_rvalue *increment;
171
172
173 inline bool is_induction_var() const
174 {
175 /* Induction variables always have a non-null increment, and vice
176 * versa.
177 */
178 return this->increment != NULL;
179 }
180
181
182 inline bool is_loop_constant() const
183 {
184 const bool is_const = (this->num_assignments == 0)
185 || (((this->num_assignments == 1)
186 && !this->conditional_or_nested_assignment
187 && !this->read_before_write
188 && this->rhs_clean) || this->var->data.read_only);
189
190 /* If the RHS of *the* assignment is clean, then there must be exactly
191 * one assignment of the variable.
192 */
193 assert((this->rhs_clean && (this->num_assignments == 1))
194 || !this->rhs_clean);
195
196 return is_const;
197 }
198
199 void record_reference(bool in_assignee,
200 bool in_conditional_code_or_nested_loop,
201 ir_assignment *current_assignment);
202 };
203
204
205 class loop_terminator : public exec_node {
206 public:
207 loop_terminator()
208 : ir(NULL), iterations(-1)
209 {
210 }
211
212 /**
213 * Statement which terminates the loop.
214 */
215 ir_if *ir;
216
217 /**
218 * The number of iterations after which the terminator is known to
219 * terminate the loop (if that is a fixed value). Otherwise -1.
220 */
221 int iterations;
222 };
223
224
225 class loop_state {
226 public:
227 ~loop_state();
228
229 /**
230 * Get the loop variable state data for a particular loop
231 */
232 loop_variable_state *get(const ir_loop *);
233
234 loop_variable_state *insert(ir_loop *ir);
235
236 bool loop_found;
237
238 private:
239 loop_state();
240
241 /**
242 * Hash table containing all loops that have been analyzed.
243 */
244 hash_table *ht;
245
246 void *mem_ctx;
247
248 friend loop_state *analyze_loop_variables(exec_list *instructions);
249 };
250
251 #endif /* LOOP_ANALYSIS_H */