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 "compiler/glsl_types.h"
25 #include "loop_analysis.h"
26 #include "ir_hierarchical_visitor.h"
28 static void try_add_loop_terminator(loop_variable_state
*ls
, 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
*);
36 * Find an initializer of a variable outside a loop
38 * Works backwards from the loop to find the pre-loop value of the variable.
39 * This is used, for example, to find the initial value of loop induction
42 * \param loop Loop where \c var is an induction variable
43 * \param var Variable whose initializer is to be found
46 * The \c ir_rvalue assigned to the variable outside the loop. May return
47 * \c NULL if no initializer can be found.
50 find_initial_value(ir_loop
*loop
, ir_variable
*var
)
52 for (exec_node
*node
= loop
->prev
; !node
->is_head_sentinel();
54 ir_instruction
*ir
= (ir_instruction
*) node
;
56 switch (ir
->ir_type
) {
59 case ir_type_loop_jump
:
64 case ir_type_function
:
65 case ir_type_function_signature
:
66 assert(!"Should not get here.");
69 case ir_type_assignment
: {
70 ir_assignment
*assign
= ir
->as_assignment();
71 ir_variable
*assignee
= assign
->lhs
->whole_variable_referenced();
74 return (assign
->condition
!= NULL
) ? NULL
: assign
->rhs
;
89 calculate_iterations(ir_rvalue
*from
, ir_rvalue
*to
, ir_rvalue
*increment
,
90 enum ir_expression_operation op
, bool continue_from_then
,
91 bool swap_compare_operands
)
93 if (from
== NULL
|| to
== NULL
|| increment
== NULL
)
96 void *mem_ctx
= ralloc_context(NULL
);
98 ir_expression
*const sub
=
99 new(mem_ctx
) ir_expression(ir_binop_sub
, from
->type
, to
, from
);
101 ir_expression
*const div
=
102 new(mem_ctx
) ir_expression(ir_binop_div
, sub
->type
, sub
, increment
);
104 ir_constant
*iter
= div
->constant_expression_value(mem_ctx
);
106 ralloc_free(mem_ctx
);
110 if (!iter
->type
->is_integer()) {
111 const ir_expression_operation op
= iter
->type
->is_double()
112 ? ir_unop_d2i
: ir_unop_f2i
;
114 new(mem_ctx
) ir_expression(op
, glsl_type::int_type
, iter
, NULL
);
116 iter
= cast
->constant_expression_value(mem_ctx
);
119 int iter_value
= iter
->get_int_component(0);
121 /* Make sure that the calculated number of iterations satisfies the exit
122 * condition. This is needed to catch off-by-one errors and some types of
123 * ill-formed loops. For example, we need to detect that the following
124 * loop does not have a maximum iteration count.
126 * for (float x = 0.0; x != 0.9; x += 0.2)
129 const int bias
[] = { -1, 0, 1 };
130 bool valid_loop
= false;
132 for (unsigned i
= 0; i
< ARRAY_SIZE(bias
); i
++) {
133 /* Increment may be of type int, uint or float. */
134 switch (increment
->type
->base_type
) {
136 iter
= new(mem_ctx
) ir_constant(iter_value
+ bias
[i
]);
139 iter
= new(mem_ctx
) ir_constant(unsigned(iter_value
+ bias
[i
]));
141 case GLSL_TYPE_FLOAT
:
142 iter
= new(mem_ctx
) ir_constant(float(iter_value
+ bias
[i
]));
144 case GLSL_TYPE_DOUBLE
:
145 iter
= new(mem_ctx
) ir_constant(double(iter_value
+ bias
[i
]));
148 unreachable("Unsupported type for loop iterator.");
151 ir_expression
*const mul
=
152 new(mem_ctx
) ir_expression(ir_binop_mul
, increment
->type
, iter
,
155 ir_expression
*const add
=
156 new(mem_ctx
) ir_expression(ir_binop_add
, mul
->type
, mul
, from
);
158 ir_expression
*cmp
= swap_compare_operands
159 ? new(mem_ctx
) ir_expression(op
, glsl_type::bool_type
, to
, add
)
160 : new(mem_ctx
) ir_expression(op
, glsl_type::bool_type
, add
, to
);
161 if (continue_from_then
)
162 cmp
= new(mem_ctx
) ir_expression(ir_unop_logic_not
, cmp
);
164 ir_constant
*const cmp_result
= cmp
->constant_expression_value(mem_ctx
);
166 assert(cmp_result
!= NULL
);
167 if (cmp_result
->get_bool_component(0)) {
168 iter_value
+= bias
[i
];
174 ralloc_free(mem_ctx
);
175 return (valid_loop
) ? iter_value
: -1;
179 incremented_before_terminator(ir_loop
*loop
, ir_variable
*var
,
182 for (exec_node
*node
= loop
->body_instructions
.get_head();
183 !node
->is_tail_sentinel();
184 node
= node
->get_next()) {
185 ir_instruction
*ir
= (ir_instruction
*) node
;
187 switch (ir
->ir_type
) {
189 if (ir
->as_if() == terminator
)
193 case ir_type_assignment
: {
194 ir_assignment
*assign
= ir
->as_assignment();
195 ir_variable
*assignee
= assign
->lhs
->whole_variable_referenced();
197 if (assignee
== var
) {
198 assert(assign
->condition
== NULL
);
210 unreachable("Unable to find induction variable");
214 * Record the fact that the given loop variable was referenced inside the loop.
216 * \arg in_assignee is true if the reference was on the LHS of an assignment.
218 * \arg in_conditional_code_or_nested_loop is true if the reference occurred
219 * inside an if statement or a nested loop.
221 * \arg current_assignment is the ir_assignment node that the loop variable is
222 * on the LHS of, if any (ignored if \c in_assignee is false).
225 loop_variable::record_reference(bool in_assignee
,
226 bool in_conditional_code_or_nested_loop
,
227 ir_assignment
*current_assignment
)
230 assert(current_assignment
!= NULL
);
232 if (in_conditional_code_or_nested_loop
||
233 current_assignment
->condition
!= NULL
) {
234 this->conditional_or_nested_assignment
= true;
237 if (this->first_assignment
== NULL
) {
238 assert(this->num_assignments
== 0);
240 this->first_assignment
= current_assignment
;
243 this->num_assignments
++;
244 } else if (this->first_assignment
== current_assignment
) {
245 /* This catches the case where the variable is used in the RHS of an
246 * assignment where it is also in the LHS.
248 this->read_before_write
= true;
253 loop_state::loop_state()
255 this->ht
= _mesa_pointer_hash_table_create(NULL
);
256 this->mem_ctx
= ralloc_context(NULL
);
257 this->loop_found
= false;
261 loop_state::~loop_state()
263 _mesa_hash_table_destroy(this->ht
, NULL
);
264 ralloc_free(this->mem_ctx
);
268 loop_variable_state
*
269 loop_state::insert(ir_loop
*ir
)
271 loop_variable_state
*ls
= new(this->mem_ctx
) loop_variable_state
;
273 _mesa_hash_table_insert(this->ht
, ir
, ls
);
274 this->loop_found
= true;
280 loop_variable_state
*
281 loop_state::get(const ir_loop
*ir
)
283 hash_entry
*entry
= _mesa_hash_table_search(this->ht
, ir
);
284 return entry
? (loop_variable_state
*) entry
->data
: NULL
;
289 loop_variable_state::get(const ir_variable
*ir
)
291 hash_entry
*entry
= _mesa_hash_table_search(this->var_hash
, ir
);
292 return entry
? (loop_variable
*) entry
->data
: NULL
;
297 loop_variable_state::insert(ir_variable
*var
)
299 void *mem_ctx
= ralloc_parent(this);
300 loop_variable
*lv
= rzalloc(mem_ctx
, loop_variable
);
304 _mesa_hash_table_insert(this->var_hash
, lv
->var
, lv
);
305 this->variables
.push_tail(lv
);
312 loop_variable_state::insert(ir_if
*if_stmt
, bool continue_from_then
)
314 void *mem_ctx
= ralloc_parent(this);
315 loop_terminator
*t
= new(mem_ctx
) loop_terminator();
318 t
->continue_from_then
= continue_from_then
;
320 this->terminators
.push_tail(t
);
327 * If the given variable already is recorded in the state for this loop,
328 * return the corresponding loop_variable object that records information
331 * Otherwise, create a new loop_variable object to record information about
332 * the variable, and set its \c read_before_write field appropriately based on
335 * \arg in_assignee is true if this variable was encountered on the LHS of an
339 loop_variable_state::get_or_insert(ir_variable
*var
, bool in_assignee
)
341 loop_variable
*lv
= this->get(var
);
344 lv
= this->insert(var
);
345 lv
->read_before_write
= !in_assignee
;
354 class loop_analysis
: public ir_hierarchical_visitor
{
356 loop_analysis(loop_state
*loops
);
358 virtual ir_visitor_status
visit(ir_loop_jump
*);
359 virtual ir_visitor_status
visit(ir_dereference_variable
*);
361 virtual ir_visitor_status
visit_enter(ir_call
*);
363 virtual ir_visitor_status
visit_enter(ir_loop
*);
364 virtual ir_visitor_status
visit_leave(ir_loop
*);
365 virtual ir_visitor_status
visit_enter(ir_assignment
*);
366 virtual ir_visitor_status
visit_leave(ir_assignment
*);
367 virtual ir_visitor_status
visit_enter(ir_if
*);
368 virtual ir_visitor_status
visit_leave(ir_if
*);
372 int if_statement_depth
;
374 ir_assignment
*current_assignment
;
379 } /* anonymous namespace */
381 loop_analysis::loop_analysis(loop_state
*loops
)
382 : loops(loops
), if_statement_depth(0), current_assignment(NULL
)
389 loop_analysis::visit(ir_loop_jump
*ir
)
393 assert(!this->state
.is_empty());
395 loop_variable_state
*const ls
=
396 (loop_variable_state
*) this->state
.get_head();
398 ls
->num_loop_jumps
++;
400 return visit_continue
;
405 loop_analysis::visit_enter(ir_call
*)
407 /* Mark every loop that we're currently analyzing as containing an ir_call
408 * (even those at outer nesting levels).
410 foreach_in_list(loop_variable_state
, ls
, &this->state
) {
411 ls
->contains_calls
= true;
414 return visit_continue_with_parent
;
419 loop_analysis::visit(ir_dereference_variable
*ir
)
421 /* If we're not somewhere inside a loop, there's nothing to do.
423 if (this->state
.is_empty())
424 return visit_continue
;
428 foreach_in_list(loop_variable_state
, ls
, &this->state
) {
429 ir_variable
*var
= ir
->variable_referenced();
430 loop_variable
*lv
= ls
->get_or_insert(var
, this->in_assignee
);
432 lv
->record_reference(this->in_assignee
,
433 nested
|| this->if_statement_depth
> 0,
434 this->current_assignment
);
438 return visit_continue
;
442 loop_analysis::visit_enter(ir_loop
*ir
)
444 loop_variable_state
*ls
= this->loops
->insert(ir
);
445 this->state
.push_head(ls
);
447 return visit_continue
;
451 loop_analysis::visit_leave(ir_loop
*ir
)
453 loop_variable_state
*const ls
=
454 (loop_variable_state
*) this->state
.pop_head();
456 /* Function calls may contain side effects. These could alter any of our
457 * variables in ways that cannot be known, and may even terminate shader
458 * execution (say, calling discard in the fragment shader). So we can't
459 * rely on any of our analysis about assignments to variables.
461 * We could perform some conservative analysis (prove there's no statically
462 * possible assignment, etc.) but it isn't worth it for now; function
463 * inlining will allow us to unroll loops anyway.
465 if (ls
->contains_calls
)
466 return visit_continue
;
468 foreach_in_list(ir_instruction
, node
, &ir
->body_instructions
) {
469 /* Skip over declarations at the start of a loop.
471 if (node
->as_variable())
474 ir_if
*if_stmt
= ((ir_instruction
*) node
)->as_if();
477 try_add_loop_terminator(ls
, if_stmt
);
481 foreach_in_list_safe(loop_variable
, lv
, &ls
->variables
) {
482 /* Move variables that are already marked as being loop constant to
483 * a separate list. These trivially don't need to be tested.
485 if (lv
->is_loop_constant()) {
487 ls
->constants
.push_tail(lv
);
491 /* Each variable assigned in the loop that isn't already marked as being loop
492 * constant might still be loop constant. The requirements at this point
495 * - Variable is written before it is read.
497 * - Only one assignment to the variable.
499 * - All operands on the RHS of the assignment are also loop constants.
501 * The last requirement is the reason for the progress loop. A variable
502 * marked as a loop constant on one pass may allow other variables to be
503 * marked as loop constant on following passes.
509 foreach_in_list_safe(loop_variable
, lv
, &ls
->variables
) {
510 if (lv
->conditional_or_nested_assignment
|| (lv
->num_assignments
> 1))
513 /* Process the RHS of the assignment. If all of the variables
514 * accessed there are loop constants, then add this
516 ir_rvalue
*const rhs
= lv
->first_assignment
->rhs
;
517 if (all_expression_operands_are_loop_constant(rhs
, ls
->var_hash
)) {
518 lv
->rhs_clean
= true;
520 if (lv
->is_loop_constant()) {
524 ls
->constants
.push_tail(lv
);
530 /* The remaining variables that are not loop invariant might be loop
531 * induction variables.
533 foreach_in_list_safe(loop_variable
, lv
, &ls
->variables
) {
534 /* If there is more than one assignment to a variable, it cannot be a
535 * loop induction variable. This isn't strictly true, but this is a
536 * very simple induction variable detector, and it can't handle more
539 if (lv
->num_assignments
> 1)
542 /* All of the variables with zero assignments in the loop are loop
543 * invariant, and they should have already been filtered out.
545 assert(lv
->num_assignments
== 1);
546 assert(lv
->first_assignment
!= NULL
);
548 /* The assignment to the variable in the loop must be unconditional and
549 * not inside a nested loop.
551 if (lv
->conditional_or_nested_assignment
)
554 /* Basic loop induction variables have a single assignment in the loop
555 * that has the form 'VAR = VAR + i' or 'VAR = VAR - i' where i is a
558 ir_rvalue
*const inc
=
559 get_basic_induction_increment(lv
->first_assignment
, ls
->var_hash
);
564 ls
->induction_variables
.push_tail(lv
);
568 /* Search the loop terminating conditions for those of the form 'i < c'
569 * where i is a loop induction variable, c is a constant, and < is any
570 * relative operator. From each of these we can infer an iteration count.
571 * Also figure out which terminator (if any) produces the smallest
572 * iteration count--this is the limiting terminator.
574 foreach_in_list(loop_terminator
, t
, &ls
->terminators
) {
575 ir_if
*if_stmt
= t
->ir
;
577 /* If-statements can be either 'if (expr)' or 'if (deref)'. We only care
578 * about the former here.
580 ir_expression
*cond
= if_stmt
->condition
->as_expression();
584 switch (cond
->operation
) {
586 case ir_binop_gequal
: {
587 /* The expressions that we care about will either be of the form
588 * 'counter < limit' or 'limit < counter'. Figure out which is
591 ir_rvalue
*counter
= cond
->operands
[0]->as_dereference_variable();
592 ir_constant
*limit
= cond
->operands
[1]->as_constant();
593 enum ir_expression_operation cmp
= cond
->operation
;
594 bool swap_compare_operands
= false;
597 counter
= cond
->operands
[1]->as_dereference_variable();
598 limit
= cond
->operands
[0]->as_constant();
599 swap_compare_operands
= true;
602 if ((counter
== NULL
) || (limit
== NULL
))
605 ir_variable
*var
= counter
->variable_referenced();
607 ir_rvalue
*init
= find_initial_value(ir
, var
);
609 loop_variable
*lv
= ls
->get(var
);
610 if (lv
!= NULL
&& lv
->is_induction_var()) {
611 t
->iterations
= calculate_iterations(init
, limit
, lv
->increment
,
612 cmp
, t
->continue_from_then
,
613 swap_compare_operands
);
615 if (incremented_before_terminator(ir
, var
, t
->ir
)) {
619 if (t
->iterations
>= 0 &&
620 (ls
->limiting_terminator
== NULL
||
621 t
->iterations
< ls
->limiting_terminator
->iterations
)) {
622 ls
->limiting_terminator
= t
;
633 return visit_continue
;
637 loop_analysis::visit_enter(ir_if
*ir
)
641 if (!this->state
.is_empty())
642 this->if_statement_depth
++;
644 return visit_continue
;
648 loop_analysis::visit_leave(ir_if
*ir
)
652 if (!this->state
.is_empty())
653 this->if_statement_depth
--;
655 return visit_continue
;
659 loop_analysis::visit_enter(ir_assignment
*ir
)
661 /* If we're not somewhere inside a loop, there's nothing to do.
663 if (this->state
.is_empty())
664 return visit_continue_with_parent
;
666 this->current_assignment
= ir
;
668 return visit_continue
;
672 loop_analysis::visit_leave(ir_assignment
*ir
)
674 /* Since the visit_enter exits with visit_continue_with_parent for this
675 * case, the loop state stack should never be empty here.
677 assert(!this->state
.is_empty());
679 assert(this->current_assignment
== ir
);
680 this->current_assignment
= NULL
;
682 return visit_continue
;
686 class examine_rhs
: public ir_hierarchical_visitor
{
688 examine_rhs(hash_table
*loop_variables
)
690 this->only_uses_loop_constants
= true;
691 this->loop_variables
= loop_variables
;
694 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
696 hash_entry
*entry
= _mesa_hash_table_search(this->loop_variables
,
698 loop_variable
*lv
= entry
? (loop_variable
*) entry
->data
: NULL
;
702 if (lv
->is_loop_constant()) {
703 return visit_continue
;
705 this->only_uses_loop_constants
= false;
710 hash_table
*loop_variables
;
711 bool only_uses_loop_constants
;
716 all_expression_operands_are_loop_constant(ir_rvalue
*ir
, hash_table
*variables
)
718 examine_rhs
v(variables
);
722 return v
.only_uses_loop_constants
;
727 get_basic_induction_increment(ir_assignment
*ir
, hash_table
*var_hash
)
729 /* The RHS must be a binary expression.
731 ir_expression
*const rhs
= ir
->rhs
->as_expression();
733 || ((rhs
->operation
!= ir_binop_add
)
734 && (rhs
->operation
!= ir_binop_sub
)))
737 /* One of the of operands of the expression must be the variable assigned.
738 * If the operation is subtraction, the variable in question must be the
741 ir_variable
*const var
= ir
->lhs
->variable_referenced();
743 ir_variable
*const op0
= rhs
->operands
[0]->variable_referenced();
744 ir_variable
*const op1
= rhs
->operands
[1]->variable_referenced();
746 if (((op0
!= var
) && (op1
!= var
))
747 || ((op1
== var
) && (rhs
->operation
== ir_binop_sub
)))
750 ir_rvalue
*inc
= (op0
== var
) ? rhs
->operands
[1] : rhs
->operands
[0];
752 if (inc
->as_constant() == NULL
) {
753 ir_variable
*const inc_var
= inc
->variable_referenced();
754 if (inc_var
!= NULL
) {
755 hash_entry
*entry
= _mesa_hash_table_search(var_hash
, inc_var
);
756 loop_variable
*lv
= entry
? (loop_variable
*) entry
->data
: NULL
;
758 if (lv
== NULL
|| !lv
->is_loop_constant()) {
766 if ((inc
!= NULL
) && (rhs
->operation
== ir_binop_sub
)) {
767 void *mem_ctx
= ralloc_parent(ir
);
769 inc
= new(mem_ctx
) ir_expression(ir_unop_neg
,
771 inc
->clone(mem_ctx
, NULL
),
780 * Detect whether an if-statement is a loop terminating condition, if so
781 * add it to the list of loop terminators.
783 * Detects if-statements of the form
785 * (if (expression bool ...) (...then_instrs...break))
789 * (if (expression bool ...) ... (...else_instrs...break))
792 try_add_loop_terminator(loop_variable_state
*ls
, ir_if
*ir
)
794 ir_instruction
*inst
= (ir_instruction
*) ir
->then_instructions
.get_tail();
795 ir_instruction
*else_inst
=
796 (ir_instruction
*) ir
->else_instructions
.get_tail();
798 if (is_break(inst
) || is_break(else_inst
))
799 ls
->insert(ir
, is_break(else_inst
));
804 analyze_loop_variables(exec_list
*instructions
)
806 loop_state
*loops
= new loop_state
;
807 loop_analysis
v(loops
);