895954fc2d141c78908a3122f4d89ed7681b75c6
[mesa.git] / src / compiler / glsl / loop_controls.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <limits.h>
25 #include "main/compiler.h"
26 #include "compiler/glsl_types.h"
27 #include "loop_analysis.h"
28 #include "ir_hierarchical_visitor.h"
29
30 /**
31 * Find an initializer of a variable outside a loop
32 *
33 * Works backwards from the loop to find the pre-loop value of the variable.
34 * This is used, for example, to find the initial value of loop induction
35 * variables.
36 *
37 * \param loop Loop where \c var is an induction variable
38 * \param var Variable whose initializer is to be found
39 *
40 * \return
41 * The \c ir_rvalue assigned to the variable outside the loop. May return
42 * \c NULL if no initializer can be found.
43 */
44 ir_rvalue *
45 find_initial_value(ir_loop *loop, ir_variable *var)
46 {
47 for (exec_node *node = loop->prev;
48 !node->is_head_sentinel();
49 node = node->prev) {
50 ir_instruction *ir = (ir_instruction *) node;
51
52 switch (ir->ir_type) {
53 case ir_type_call:
54 case ir_type_loop:
55 case ir_type_loop_jump:
56 case ir_type_return:
57 case ir_type_if:
58 return NULL;
59
60 case ir_type_function:
61 case ir_type_function_signature:
62 assert(!"Should not get here.");
63 return NULL;
64
65 case ir_type_assignment: {
66 ir_assignment *assign = ir->as_assignment();
67 ir_variable *assignee = assign->lhs->whole_variable_referenced();
68
69 if (assignee == var)
70 return (assign->condition != NULL) ? NULL : assign->rhs;
71
72 break;
73 }
74
75 default:
76 break;
77 }
78 }
79
80 return NULL;
81 }
82
83
84 int
85 calculate_iterations(ir_rvalue *from, ir_rvalue *to, ir_rvalue *increment,
86 enum ir_expression_operation op)
87 {
88 if (from == NULL || to == NULL || increment == NULL)
89 return -1;
90
91 void *mem_ctx = ralloc_context(NULL);
92
93 ir_expression *const sub =
94 new(mem_ctx) ir_expression(ir_binop_sub, from->type, to, from);
95
96 ir_expression *const div =
97 new(mem_ctx) ir_expression(ir_binop_div, sub->type, sub, increment);
98
99 ir_constant *iter = div->constant_expression_value(mem_ctx);
100 if (iter == NULL) {
101 ralloc_free(mem_ctx);
102 return -1;
103 }
104
105 if (!iter->type->is_integer()) {
106 const ir_expression_operation op = iter->type->is_double()
107 ? ir_unop_d2i : ir_unop_f2i;
108 ir_rvalue *cast =
109 new(mem_ctx) ir_expression(op, glsl_type::int_type, iter, NULL);
110
111 iter = cast->constant_expression_value(mem_ctx);
112 }
113
114 int iter_value = iter->get_int_component(0);
115
116 /* Make sure that the calculated number of iterations satisfies the exit
117 * condition. This is needed to catch off-by-one errors and some types of
118 * ill-formed loops. For example, we need to detect that the following
119 * loop does not have a maximum iteration count.
120 *
121 * for (float x = 0.0; x != 0.9; x += 0.2)
122 * ;
123 */
124 const int bias[] = { -1, 0, 1 };
125 bool valid_loop = false;
126
127 for (unsigned i = 0; i < ARRAY_SIZE(bias); i++) {
128 /* Increment may be of type int, uint or float. */
129 switch (increment->type->base_type) {
130 case GLSL_TYPE_INT:
131 iter = new(mem_ctx) ir_constant(iter_value + bias[i]);
132 break;
133 case GLSL_TYPE_UINT:
134 iter = new(mem_ctx) ir_constant(unsigned(iter_value + bias[i]));
135 break;
136 case GLSL_TYPE_FLOAT:
137 iter = new(mem_ctx) ir_constant(float(iter_value + bias[i]));
138 break;
139 case GLSL_TYPE_DOUBLE:
140 iter = new(mem_ctx) ir_constant(double(iter_value + bias[i]));
141 break;
142 default:
143 unreachable("Unsupported type for loop iterator.");
144 }
145
146 ir_expression *const mul =
147 new(mem_ctx) ir_expression(ir_binop_mul, increment->type, iter,
148 increment);
149
150 ir_expression *const add =
151 new(mem_ctx) ir_expression(ir_binop_add, mul->type, mul, from);
152
153 ir_expression *const cmp =
154 new(mem_ctx) ir_expression(op, glsl_type::bool_type, add, to);
155
156 ir_constant *const cmp_result = cmp->constant_expression_value(mem_ctx);
157
158 assert(cmp_result != NULL);
159 if (cmp_result->get_bool_component(0)) {
160 iter_value += bias[i];
161 valid_loop = true;
162 break;
163 }
164 }
165
166 ralloc_free(mem_ctx);
167 return (valid_loop) ? iter_value : -1;
168 }
169
170 namespace {
171
172 class loop_control_visitor : public ir_hierarchical_visitor {
173 public:
174 loop_control_visitor(loop_state *state)
175 {
176 this->state = state;
177 this->progress = false;
178 }
179
180 virtual ir_visitor_status visit_leave(ir_loop *ir);
181
182 loop_state *state;
183
184 bool progress;
185 };
186
187 } /* anonymous namespace */
188
189 ir_visitor_status
190 loop_control_visitor::visit_leave(ir_loop *ir)
191 {
192 loop_variable_state *const ls = this->state->get(ir);
193
194 /* If we've entered a loop that hasn't been analyzed, something really,
195 * really bad has happened.
196 */
197 if (ls == NULL) {
198 assert(ls != NULL);
199 return visit_continue;
200 }
201
202 if (ls->limiting_terminator != NULL) {
203 /* If the limiting terminator has an iteration count of zero, then we've
204 * proven that the loop cannot run, so delete it.
205 */
206 int iterations = ls->limiting_terminator->iterations;
207 if (iterations == 0) {
208 ir->remove();
209 this->progress = true;
210 return visit_continue;
211 }
212 }
213
214 /* Remove the conditional break statements associated with all terminators
215 * that are associated with a fixed iteration count, except for the one
216 * associated with the limiting terminator--that one needs to stay, since
217 * it terminates the loop. Exception: if the loop still has a normative
218 * bound, then that terminates the loop, so we don't even need the limiting
219 * terminator.
220 */
221 foreach_in_list(loop_terminator, t, &ls->terminators) {
222 if (t->iterations < 0)
223 continue;
224
225 if (t != ls->limiting_terminator) {
226 t->ir->remove();
227
228 assert(ls->num_loop_jumps > 0);
229 ls->num_loop_jumps--;
230
231 this->progress = true;
232 }
233 }
234
235 return visit_continue;
236 }
237
238
239 bool
240 set_loop_controls(exec_list *instructions, loop_state *ls)
241 {
242 loop_control_visitor v(ls);
243
244 v.run(instructions);
245
246 return v.progress;
247 }