2 * Copyright © 2010 Intel Corporation
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:
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
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.
24 #include "glsl_types.h"
25 #include "loop_analysis.h"
26 #include "ir_hierarchical_visitor.h"
28 #include "main/mtypes.h"
32 class loop_unroll_visitor
: public ir_hierarchical_visitor
{
34 loop_unroll_visitor(loop_state
*state
,
35 const struct gl_shader_compiler_options
*options
)
38 this->progress
= false;
39 this->options
= options
;
42 virtual ir_visitor_status
visit_leave(ir_loop
*ir
);
43 void simple_unroll(ir_loop
*ir
, int iterations
);
44 void complex_unroll(ir_loop
*ir
, int iterations
,
45 bool continue_from_then_branch
);
46 void splice_post_if_instructions(ir_if
*ir_if
, exec_list
*splice_dest
);
51 const struct gl_shader_compiler_options
*options
;
54 } /* anonymous namespace */
57 is_break(ir_instruction
*ir
)
59 return ir
!= NULL
&& ir
->ir_type
== ir_type_loop_jump
60 && ((ir_loop_jump
*) ir
)->is_break();
63 class loop_unroll_count
: public ir_hierarchical_visitor
{
66 bool unsupported_variable_indexing
;
67 /* If there are nested loops, the node count will be inaccurate. */
70 loop_unroll_count(exec_list
*list
, loop_variable_state
*ls
,
71 const struct gl_shader_compiler_options
*options
)
72 : ls(ls
), options(options
)
76 unsupported_variable_indexing
= false;
81 virtual ir_visitor_status
visit_enter(ir_assignment
*)
84 return visit_continue
;
87 virtual ir_visitor_status
visit_enter(ir_expression
*)
90 return visit_continue
;
93 virtual ir_visitor_status
visit_enter(ir_loop
*)
96 return visit_continue
;
99 virtual ir_visitor_status
visit_enter(ir_dereference_array
*ir
)
101 /* Check for arrays variably-indexed by a loop induction variable.
102 * Unrolling the loop may convert that access into constant-indexing.
104 * Many drivers don't support particular kinds of variable indexing,
105 * and have to resort to using lower_variable_index_to_cond_assign to
106 * handle it. This results in huge amounts of horrible code, so we'd
107 * like to avoid that if possible. Here, we just note that it will
110 if ((ir
->array
->type
->is_array() || ir
->array
->type
->is_matrix()) &&
111 !ir
->array_index
->as_constant()) {
112 ir_variable
*array
= ir
->array
->variable_referenced();
113 loop_variable
*lv
= ls
->get(ir
->array_index
->variable_referenced());
114 if (array
&& lv
&& lv
->is_induction_var()) {
115 switch (array
->data
.mode
) {
117 case ir_var_temporary
:
118 case ir_var_const_in
:
119 case ir_var_function_in
:
120 case ir_var_function_out
:
121 case ir_var_function_inout
:
122 if (options
->EmitNoIndirectTemp
)
123 unsupported_variable_indexing
= true;
126 if (options
->EmitNoIndirectUniform
)
127 unsupported_variable_indexing
= true;
129 case ir_var_shader_in
:
130 if (options
->EmitNoIndirectInput
)
131 unsupported_variable_indexing
= true;
133 case ir_var_shader_out
:
134 if (options
->EmitNoIndirectOutput
)
135 unsupported_variable_indexing
= true;
140 return visit_continue
;
144 loop_variable_state
*ls
;
145 const struct gl_shader_compiler_options
*options
;
150 * Unroll a loop which does not contain any jumps. For example, if the input
153 * (loop (...) ...instrs...)
155 * And the iteration count is 3, the output will be:
157 * ...instrs... ...instrs... ...instrs...
160 loop_unroll_visitor::simple_unroll(ir_loop
*ir
, int iterations
)
162 void *const mem_ctx
= ralloc_parent(ir
);
164 for (int i
= 0; i
< iterations
; i
++) {
167 copy_list
.make_empty();
168 clone_ir_list(mem_ctx
, ©_list
, &ir
->body_instructions
);
170 ir
->insert_before(©_list
);
173 /* The loop has been replaced by the unrolled copies. Remove the original
174 * loop from the IR sequence.
178 this->progress
= true;
183 * Unroll a loop whose last statement is an ir_if. If \c
184 * continue_from_then_branch is true, the loop is repeated only when the
185 * "then" branch of the if is taken; otherwise it is repeated only when the
186 * "else" branch of the if is taken.
188 * For example, if the input is:
193 * (...then_instrs...)
194 * (...else_instrs...)))
196 * And the iteration count is 3, and \c continue_from_then_branch is true,
197 * then the output will be:
207 * (...then_instrs...)
208 * (...else_instrs...)))
209 * (...else_instrs...)))
213 loop_unroll_visitor::complex_unroll(ir_loop
*ir
, int iterations
,
214 bool continue_from_then_branch
)
216 void *const mem_ctx
= ralloc_parent(ir
);
217 ir_instruction
*ir_to_replace
= ir
;
219 for (int i
= 0; i
< iterations
; i
++) {
222 copy_list
.make_empty();
223 clone_ir_list(mem_ctx
, ©_list
, &ir
->body_instructions
);
225 ir_if
*ir_if
= ((ir_instruction
*) copy_list
.get_tail())->as_if();
226 assert(ir_if
!= NULL
);
228 ir_to_replace
->insert_before(©_list
);
229 ir_to_replace
->remove();
231 /* placeholder that will be removed in the next iteration */
233 new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
235 exec_list
*const list
= (continue_from_then_branch
)
236 ? &ir_if
->then_instructions
: &ir_if
->else_instructions
;
238 list
->push_tail(ir_to_replace
);
241 ir_to_replace
->remove();
243 this->progress
= true;
248 * Move all of the instructions which follow \c ir_if to the end of
251 * For example, in the code snippet:
254 * (...then_instructions...
256 * (...else_instructions...))
257 * ...post_if_instructions...
259 * If \c ir_if points to the "if" instruction, and \c splice_dest points to
260 * (...else_instructions...), the code snippet is transformed into:
263 * (...then_instructions...
265 * (...else_instructions...
266 * ...post_if_instructions...))
269 loop_unroll_visitor::splice_post_if_instructions(ir_if
*ir_if
,
270 exec_list
*splice_dest
)
272 while (!ir_if
->get_next()->is_tail_sentinel()) {
273 ir_instruction
*move_ir
= (ir_instruction
*) ir_if
->get_next();
276 splice_dest
->push_tail(move_ir
);
282 loop_unroll_visitor::visit_leave(ir_loop
*ir
)
284 loop_variable_state
*const ls
= this->state
->get(ir
);
287 /* If we've entered a loop that hasn't been analyzed, something really,
288 * really bad has happened.
292 return visit_continue
;
295 /* Don't try to unroll loops where the number of iterations is not known
298 if (ls
->limiting_terminator
== NULL
)
299 return visit_continue
;
301 iterations
= ls
->limiting_terminator
->iterations
;
303 const int max_iterations
= options
->MaxUnrollIterations
;
305 /* Don't try to unroll loops that have zillions of iterations either.
307 if (iterations
> max_iterations
)
308 return visit_continue
;
310 /* Don't try to unroll nested loops and loops with a huge body.
312 loop_unroll_count
count(&ir
->body_instructions
, ls
, options
);
314 bool loop_too_large
=
315 count
.nested_loop
|| count
.nodes
* iterations
> max_iterations
* 5;
317 if (loop_too_large
&& !count
.unsupported_variable_indexing
)
318 return visit_continue
;
320 /* Note: the limiting terminator contributes 1 to ls->num_loop_jumps.
321 * We'll be removing the limiting terminator before we unroll.
323 assert(ls
->num_loop_jumps
> 0);
324 unsigned predicted_num_loop_jumps
= ls
->num_loop_jumps
- 1;
326 if (predicted_num_loop_jumps
> 1)
327 return visit_continue
;
329 if (predicted_num_loop_jumps
== 0) {
330 ls
->limiting_terminator
->ir
->remove();
331 simple_unroll(ir
, iterations
);
332 return visit_continue
;
335 ir_instruction
*last_ir
= (ir_instruction
*) ir
->body_instructions
.get_tail();
336 assert(last_ir
!= NULL
);
338 if (is_break(last_ir
)) {
339 /* If the only loop-jump is a break at the end of the loop, the loop
340 * will execute exactly once. Remove the break and use the simple
341 * unroller with an iteration count of 1.
345 ls
->limiting_terminator
->ir
->remove();
346 simple_unroll(ir
, 1);
347 return visit_continue
;
350 /* recognize loops in the form produced by ir_lower_jumps */
351 foreach_in_list(ir_instruction
, cur_ir
, &ir
->body_instructions
) {
352 /* Skip the limiting terminator, since it will go away when we
355 if (cur_ir
== ls
->limiting_terminator
->ir
)
358 ir_if
*ir_if
= cur_ir
->as_if();
360 /* Determine which if-statement branch, if any, ends with a
361 * break. The branch that did *not* have the break will get a
362 * temporary continue inserted in each iteration of the loop
365 * Note that since ls->num_loop_jumps is <= 1, it is impossible
366 * for both branches to end with a break.
368 ir_instruction
*ir_if_last
=
369 (ir_instruction
*) ir_if
->then_instructions
.get_tail();
371 if (is_break(ir_if_last
)) {
372 ls
->limiting_terminator
->ir
->remove();
373 splice_post_if_instructions(ir_if
, &ir_if
->else_instructions
);
374 ir_if_last
->remove();
375 complex_unroll(ir
, iterations
, false);
376 return visit_continue
;
379 (ir_instruction
*) ir_if
->else_instructions
.get_tail();
381 if (is_break(ir_if_last
)) {
382 ls
->limiting_terminator
->ir
->remove();
383 splice_post_if_instructions(ir_if
, &ir_if
->then_instructions
);
384 ir_if_last
->remove();
385 complex_unroll(ir
, iterations
, true);
386 return visit_continue
;
392 /* Did not find the break statement. It must be in a complex if-nesting,
393 * so don't try to unroll.
395 return visit_continue
;
400 unroll_loops(exec_list
*instructions
, loop_state
*ls
,
401 const struct gl_shader_compiler_options
*options
)
403 loop_unroll_visitor
v(ls
, options
);