glsl: merge loop_controls.cpp with loop_unroll.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 extern bool
39 unroll_loops(exec_list *instructions, loop_state *ls,
40 const struct gl_shader_compiler_options *options);
41
42
43 /**
44 * Tracking for all variables used in a loop
45 */
46 class loop_variable_state : public exec_node {
47 public:
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 *);
52
53
54 /**
55 * Variables that have not yet been classified
56 */
57 exec_list variables;
58
59 /**
60 * Variables whose values are constant within the body of the loop
61 *
62 * This list contains \c loop_variable objects.
63 */
64 exec_list constants;
65
66 /**
67 * Induction variables for this loop
68 *
69 * This list contains \c loop_variable objects.
70 */
71 exec_list induction_variables;
72
73 /**
74 * Simple if-statements that lead to the termination of the loop
75 *
76 * This list contains \c loop_terminator objects.
77 *
78 * \sa is_loop_terminator
79 */
80 exec_list terminators;
81
82 /**
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
86 * NULL.
87 */
88 loop_terminator *limiting_terminator;
89
90 /**
91 * Hash table containing all variables accessed in this loop
92 */
93 hash_table *var_hash;
94
95 /**
96 * Number of ir_loop_jump instructions that operate on this loop
97 */
98 unsigned num_loop_jumps;
99
100 /**
101 * Whether this loop contains any function calls.
102 */
103 bool contains_calls;
104
105 loop_variable_state()
106 {
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;
112 }
113
114 ~loop_variable_state()
115 {
116 _mesa_hash_table_destroy(this->var_hash, NULL);
117 }
118
119 DECLARE_RALLOC_CXX_OPERATORS(loop_variable_state)
120 };
121
122
123 class loop_variable : public exec_node {
124 public:
125 /** The variable in question. */
126 ir_variable *var;
127
128 /** Is the variable read in the loop before it is written? */
129 bool read_before_write;
130
131 /** Are all variables in the RHS of the assignment loop constants? */
132 bool rhs_clean;
133
134 /**
135 * Is there an assignment to the variable that is conditional, or inside a
136 * nested loop?
137 */
138 bool conditional_or_nested_assignment;
139
140 /** Reference to the first assignment to the variable in the loop body. */
141 ir_assignment *first_assignment;
142
143 /** Number of assignments to the variable in the loop body. */
144 unsigned num_assignments;
145
146 /**
147 * Increment value for a loop induction variable
148 *
149 * If this is a loop induction variable, the amount by which the variable
150 * is incremented on each iteration through the loop.
151 *
152 * If this is not a loop induction variable, NULL.
153 */
154 ir_rvalue *increment;
155
156
157 inline bool is_induction_var() const
158 {
159 /* Induction variables always have a non-null increment, and vice
160 * versa.
161 */
162 return this->increment != NULL;
163 }
164
165
166 inline bool is_loop_constant() const
167 {
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);
173
174 /* If the RHS of *the* assignment is clean, then there must be exactly
175 * one assignment of the variable.
176 */
177 assert((this->rhs_clean && (this->num_assignments == 1))
178 || !this->rhs_clean);
179
180 return is_const;
181 }
182
183 void record_reference(bool in_assignee,
184 bool in_conditional_code_or_nested_loop,
185 ir_assignment *current_assignment);
186 };
187
188
189 class loop_terminator : public exec_node {
190 public:
191 loop_terminator()
192 : ir(NULL), iterations(-1)
193 {
194 }
195
196 /**
197 * Statement which terminates the loop.
198 */
199 ir_if *ir;
200
201 /**
202 * The number of iterations after which the terminator is known to
203 * terminate the loop (if that is a fixed value). Otherwise -1.
204 */
205 int iterations;
206 };
207
208
209 class loop_state {
210 public:
211 ~loop_state();
212
213 /**
214 * Get the loop variable state data for a particular loop
215 */
216 loop_variable_state *get(const ir_loop *);
217
218 loop_variable_state *insert(ir_loop *ir);
219
220 bool loop_found;
221
222 private:
223 loop_state();
224
225 /**
226 * Hash table containing all loops that have been analyzed.
227 */
228 hash_table *ht;
229
230 void *mem_ctx;
231
232 friend loop_state *analyze_loop_variables(exec_list *instructions);
233 };
234
235 #endif /* LOOP_ANALYSIS_H */