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 static bool is_loop_terminator(ir_if
*ir
);
30 static bool all_expression_operands_are_loop_constant(ir_rvalue
*,
33 static ir_rvalue
*get_basic_induction_increment(ir_assignment
*, hash_table
*);
37 * Record the fact that the given loop variable was referenced inside the loop.
39 * \arg in_assignee is true if the reference was on the LHS of an assignment.
41 * \arg in_conditional_code_or_nested_loop is true if the reference occurred
42 * inside an if statement or a nested loop.
44 * \arg current_assignment is the ir_assignment node that the loop variable is
45 * on the LHS of, if any (ignored if \c in_assignee is false).
48 loop_variable::record_reference(bool in_assignee
,
49 bool in_conditional_code_or_nested_loop
,
50 ir_assignment
*current_assignment
)
53 assert(current_assignment
!= NULL
);
55 if (in_conditional_code_or_nested_loop
||
56 current_assignment
->condition
!= NULL
) {
57 this->conditional_or_nested_assignment
= true;
60 if (this->first_assignment
== NULL
) {
61 assert(this->num_assignments
== 0);
63 this->first_assignment
= current_assignment
;
66 this->num_assignments
++;
67 } else if (this->first_assignment
== current_assignment
) {
68 /* This catches the case where the variable is used in the RHS of an
69 * assignment where it is also in the LHS.
71 this->read_before_write
= true;
76 loop_state::loop_state()
78 this->ht
= hash_table_ctor(0, hash_table_pointer_hash
,
79 hash_table_pointer_compare
);
80 this->mem_ctx
= ralloc_context(NULL
);
81 this->loop_found
= false;
85 loop_state::~loop_state()
87 hash_table_dtor(this->ht
);
88 ralloc_free(this->mem_ctx
);
93 loop_state::insert(ir_loop
*ir
)
95 loop_variable_state
*ls
= new(this->mem_ctx
) loop_variable_state
;
97 hash_table_insert(this->ht
, ls
, ir
);
98 this->loop_found
= true;
104 loop_variable_state
*
105 loop_state::get(const ir_loop
*ir
)
107 return (loop_variable_state
*) hash_table_find(this->ht
, ir
);
112 loop_variable_state::get(const ir_variable
*ir
)
114 return (loop_variable
*) hash_table_find(this->var_hash
, ir
);
119 loop_variable_state::insert(ir_variable
*var
)
121 void *mem_ctx
= ralloc_parent(this);
122 loop_variable
*lv
= rzalloc(mem_ctx
, loop_variable
);
126 hash_table_insert(this->var_hash
, lv
, lv
->var
);
127 this->variables
.push_tail(lv
);
134 loop_variable_state::insert(ir_if
*if_stmt
)
136 void *mem_ctx
= ralloc_parent(this);
137 loop_terminator
*t
= new(mem_ctx
) loop_terminator();
140 this->terminators
.push_tail(t
);
147 * If the given variable already is recorded in the state for this loop,
148 * return the corresponding loop_variable object that records information
151 * Otherwise, create a new loop_variable object to record information about
152 * the variable, and set its \c read_before_write field appropriately based on
155 * \arg in_assignee is true if this variable was encountered on the LHS of an
159 loop_variable_state::get_or_insert(ir_variable
*var
, bool in_assignee
)
161 loop_variable
*lv
= this->get(var
);
164 lv
= this->insert(var
);
165 lv
->read_before_write
= !in_assignee
;
174 class loop_analysis
: public ir_hierarchical_visitor
{
176 loop_analysis(loop_state
*loops
);
178 virtual ir_visitor_status
visit(ir_loop_jump
*);
179 virtual ir_visitor_status
visit(ir_dereference_variable
*);
181 virtual ir_visitor_status
visit_enter(ir_call
*);
183 virtual ir_visitor_status
visit_enter(ir_loop
*);
184 virtual ir_visitor_status
visit_leave(ir_loop
*);
185 virtual ir_visitor_status
visit_enter(ir_assignment
*);
186 virtual ir_visitor_status
visit_leave(ir_assignment
*);
187 virtual ir_visitor_status
visit_enter(ir_if
*);
188 virtual ir_visitor_status
visit_leave(ir_if
*);
192 int if_statement_depth
;
194 ir_assignment
*current_assignment
;
199 } /* anonymous namespace */
201 loop_analysis::loop_analysis(loop_state
*loops
)
202 : loops(loops
), if_statement_depth(0), current_assignment(NULL
)
209 loop_analysis::visit(ir_loop_jump
*ir
)
213 assert(!this->state
.is_empty());
215 loop_variable_state
*const ls
=
216 (loop_variable_state
*) this->state
.get_head();
218 ls
->num_loop_jumps
++;
220 return visit_continue
;
225 loop_analysis::visit_enter(ir_call
*)
227 /* Mark every loop that we're currently analyzing as containing an ir_call
228 * (even those at outer nesting levels).
230 foreach_in_list(loop_variable_state
, ls
, &this->state
) {
231 ls
->contains_calls
= true;
234 return visit_continue_with_parent
;
239 loop_analysis::visit(ir_dereference_variable
*ir
)
241 /* If we're not somewhere inside a loop, there's nothing to do.
243 if (this->state
.is_empty())
244 return visit_continue
;
248 foreach_in_list(loop_variable_state
, ls
, &this->state
) {
249 ir_variable
*var
= ir
->variable_referenced();
250 loop_variable
*lv
= ls
->get_or_insert(var
, this->in_assignee
);
252 lv
->record_reference(this->in_assignee
,
253 nested
|| this->if_statement_depth
> 0,
254 this->current_assignment
);
258 return visit_continue
;
262 loop_analysis::visit_enter(ir_loop
*ir
)
264 loop_variable_state
*ls
= this->loops
->insert(ir
);
265 this->state
.push_head(ls
);
267 return visit_continue
;
271 loop_analysis::visit_leave(ir_loop
*ir
)
273 loop_variable_state
*const ls
=
274 (loop_variable_state
*) this->state
.pop_head();
276 /* Function calls may contain side effects. These could alter any of our
277 * variables in ways that cannot be known, and may even terminate shader
278 * execution (say, calling discard in the fragment shader). So we can't
279 * rely on any of our analysis about assignments to variables.
281 * We could perform some conservative analysis (prove there's no statically
282 * possible assignment, etc.) but it isn't worth it for now; function
283 * inlining will allow us to unroll loops anyway.
285 if (ls
->contains_calls
)
286 return visit_continue
;
288 foreach_in_list(ir_instruction
, node
, &ir
->body_instructions
) {
289 /* Skip over declarations at the start of a loop.
291 if (node
->as_variable())
294 ir_if
*if_stmt
= ((ir_instruction
*) node
)->as_if();
296 if ((if_stmt
!= NULL
) && is_loop_terminator(if_stmt
))
303 foreach_list_safe(node
, &ls
->variables
) {
304 loop_variable
*lv
= (loop_variable
*) node
;
306 /* Move variables that are already marked as being loop constant to
307 * a separate list. These trivially don't need to be tested.
309 if (lv
->is_loop_constant()) {
311 ls
->constants
.push_tail(lv
);
315 /* Each variable assigned in the loop that isn't already marked as being loop
316 * constant might still be loop constant. The requirements at this point
319 * - Variable is written before it is read.
321 * - Only one assignment to the variable.
323 * - All operands on the RHS of the assignment are also loop constants.
325 * The last requirement is the reason for the progress loop. A variable
326 * marked as a loop constant on one pass may allow other variables to be
327 * marked as loop constant on following passes.
333 foreach_list_safe(node
, &ls
->variables
) {
334 loop_variable
*lv
= (loop_variable
*) node
;
336 if (lv
->conditional_or_nested_assignment
|| (lv
->num_assignments
> 1))
339 /* Process the RHS of the assignment. If all of the variables
340 * accessed there are loop constants, then add this
342 ir_rvalue
*const rhs
= lv
->first_assignment
->rhs
;
343 if (all_expression_operands_are_loop_constant(rhs
, ls
->var_hash
)) {
344 lv
->rhs_clean
= true;
346 if (lv
->is_loop_constant()) {
350 ls
->constants
.push_tail(lv
);
356 /* The remaining variables that are not loop invariant might be loop
357 * induction variables.
359 foreach_list_safe(node
, &ls
->variables
) {
360 loop_variable
*lv
= (loop_variable
*) node
;
362 /* If there is more than one assignment to a variable, it cannot be a
363 * loop induction variable. This isn't strictly true, but this is a
364 * very simple induction variable detector, and it can't handle more
367 if (lv
->num_assignments
> 1)
370 /* All of the variables with zero assignments in the loop are loop
371 * invariant, and they should have already been filtered out.
373 assert(lv
->num_assignments
== 1);
374 assert(lv
->first_assignment
!= NULL
);
376 /* The assignment to the variable in the loop must be unconditional and
377 * not inside a nested loop.
379 if (lv
->conditional_or_nested_assignment
)
382 /* Basic loop induction variables have a single assignment in the loop
383 * that has the form 'VAR = VAR + i' or 'VAR = VAR - i' where i is a
386 ir_rvalue
*const inc
=
387 get_basic_induction_increment(lv
->first_assignment
, ls
->var_hash
);
392 ls
->induction_variables
.push_tail(lv
);
396 /* Search the loop terminating conditions for those of the form 'i < c'
397 * where i is a loop induction variable, c is a constant, and < is any
398 * relative operator. From each of these we can infer an iteration count.
399 * Also figure out which terminator (if any) produces the smallest
400 * iteration count--this is the limiting terminator.
402 foreach_in_list(loop_terminator
, t
, &ls
->terminators
) {
403 ir_if
*if_stmt
= t
->ir
;
405 /* If-statements can be either 'if (expr)' or 'if (deref)'. We only care
406 * about the former here.
408 ir_expression
*cond
= if_stmt
->condition
->as_expression();
412 switch (cond
->operation
) {
414 case ir_binop_greater
:
415 case ir_binop_lequal
:
416 case ir_binop_gequal
: {
417 /* The expressions that we care about will either be of the form
418 * 'counter < limit' or 'limit < counter'. Figure out which is
421 ir_rvalue
*counter
= cond
->operands
[0]->as_dereference_variable();
422 ir_constant
*limit
= cond
->operands
[1]->as_constant();
423 enum ir_expression_operation cmp
= cond
->operation
;
426 counter
= cond
->operands
[1]->as_dereference_variable();
427 limit
= cond
->operands
[0]->as_constant();
430 case ir_binop_less
: cmp
= ir_binop_greater
; break;
431 case ir_binop_greater
: cmp
= ir_binop_less
; break;
432 case ir_binop_lequal
: cmp
= ir_binop_gequal
; break;
433 case ir_binop_gequal
: cmp
= ir_binop_lequal
; break;
434 default: assert(!"Should not get here.");
438 if ((counter
== NULL
) || (limit
== NULL
))
441 ir_variable
*var
= counter
->variable_referenced();
443 ir_rvalue
*init
= find_initial_value(ir
, var
);
445 loop_variable
*lv
= ls
->get(var
);
446 if (lv
!= NULL
&& lv
->is_induction_var()) {
447 t
->iterations
= calculate_iterations(init
, limit
, lv
->increment
,
450 if (t
->iterations
>= 0 &&
451 (ls
->limiting_terminator
== NULL
||
452 t
->iterations
< ls
->limiting_terminator
->iterations
)) {
453 ls
->limiting_terminator
= t
;
464 return visit_continue
;
468 loop_analysis::visit_enter(ir_if
*ir
)
472 if (!this->state
.is_empty())
473 this->if_statement_depth
++;
475 return visit_continue
;
479 loop_analysis::visit_leave(ir_if
*ir
)
483 if (!this->state
.is_empty())
484 this->if_statement_depth
--;
486 return visit_continue
;
490 loop_analysis::visit_enter(ir_assignment
*ir
)
492 /* If we're not somewhere inside a loop, there's nothing to do.
494 if (this->state
.is_empty())
495 return visit_continue_with_parent
;
497 this->current_assignment
= ir
;
499 return visit_continue
;
503 loop_analysis::visit_leave(ir_assignment
*ir
)
505 /* Since the visit_enter exits with visit_continue_with_parent for this
506 * case, the loop state stack should never be empty here.
508 assert(!this->state
.is_empty());
510 assert(this->current_assignment
== ir
);
511 this->current_assignment
= NULL
;
513 return visit_continue
;
517 class examine_rhs
: public ir_hierarchical_visitor
{
519 examine_rhs(hash_table
*loop_variables
)
521 this->only_uses_loop_constants
= true;
522 this->loop_variables
= loop_variables
;
525 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
528 (loop_variable
*) hash_table_find(this->loop_variables
, ir
->var
);
532 if (lv
->is_loop_constant()) {
533 return visit_continue
;
535 this->only_uses_loop_constants
= false;
540 hash_table
*loop_variables
;
541 bool only_uses_loop_constants
;
546 all_expression_operands_are_loop_constant(ir_rvalue
*ir
, hash_table
*variables
)
548 examine_rhs
v(variables
);
552 return v
.only_uses_loop_constants
;
557 get_basic_induction_increment(ir_assignment
*ir
, hash_table
*var_hash
)
559 /* The RHS must be a binary expression.
561 ir_expression
*const rhs
= ir
->rhs
->as_expression();
563 || ((rhs
->operation
!= ir_binop_add
)
564 && (rhs
->operation
!= ir_binop_sub
)))
567 /* One of the of operands of the expression must be the variable assigned.
568 * If the operation is subtraction, the variable in question must be the
571 ir_variable
*const var
= ir
->lhs
->variable_referenced();
573 ir_variable
*const op0
= rhs
->operands
[0]->variable_referenced();
574 ir_variable
*const op1
= rhs
->operands
[1]->variable_referenced();
576 if (((op0
!= var
) && (op1
!= var
))
577 || ((op1
== var
) && (rhs
->operation
== ir_binop_sub
)))
580 ir_rvalue
*inc
= (op0
== var
) ? rhs
->operands
[1] : rhs
->operands
[0];
582 if (inc
->as_constant() == NULL
) {
583 ir_variable
*const inc_var
= inc
->variable_referenced();
584 if (inc_var
!= NULL
) {
586 (loop_variable
*) hash_table_find(var_hash
, inc_var
);
588 if (lv
== NULL
|| !lv
->is_loop_constant()) {
596 if ((inc
!= NULL
) && (rhs
->operation
== ir_binop_sub
)) {
597 void *mem_ctx
= ralloc_parent(ir
);
599 inc
= new(mem_ctx
) ir_expression(ir_unop_neg
,
601 inc
->clone(mem_ctx
, NULL
),
610 * Detect whether an if-statement is a loop terminating condition
612 * Detects if-statements of the form
614 * (if (expression bool ...) (break))
617 is_loop_terminator(ir_if
*ir
)
619 if (!ir
->else_instructions
.is_empty())
622 ir_instruction
*const inst
=
623 (ir_instruction
*) ir
->then_instructions
.get_head();
627 if (inst
->ir_type
!= ir_type_loop_jump
)
630 ir_loop_jump
*const jump
= (ir_loop_jump
*) inst
;
631 if (jump
->mode
!= ir_loop_jump::jump_break
)
639 analyze_loop_variables(exec_list
*instructions
)
641 loop_state
*loops
= new loop_state
;
642 loop_analysis
v(loops
);