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