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 "compiler/glsl_types.h"
25 #include "loop_analysis.h"
26 #include "ir_hierarchical_visitor.h"
27
28 static void try_add_loop_terminator(loop_variable_state *ls, ir_if *ir);
29
30 static bool all_expression_operands_are_loop_constant(ir_rvalue *,
31 hash_table *);
32
33 static ir_rvalue *get_basic_induction_increment(ir_assignment *, hash_table *);
34
35 /**
36 * Find an initializer of a variable outside a loop
37 *
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
40 * variables.
41 *
42 * \param loop Loop where \c var is an induction variable
43 * \param var Variable whose initializer is to be found
44 *
45 * \return
46 * The \c ir_rvalue assigned to the variable outside the loop. May return
47 * \c NULL if no initializer can be found.
48 */
49 static ir_rvalue *
find_initial_value(ir_loop * loop,ir_variable * var)50 find_initial_value(ir_loop *loop, ir_variable *var)
51 {
52 for (exec_node *node = loop->prev; !node->is_head_sentinel();
53 node = node->prev) {
54 ir_instruction *ir = (ir_instruction *) node;
55
56 switch (ir->ir_type) {
57 case ir_type_call:
58 case ir_type_loop:
59 case ir_type_loop_jump:
60 case ir_type_return:
61 case ir_type_if:
62 return NULL;
63
64 case ir_type_function:
65 case ir_type_function_signature:
66 assert(!"Should not get here.");
67 return NULL;
68
69 case ir_type_assignment: {
70 ir_assignment *assign = ir->as_assignment();
71 ir_variable *assignee = assign->lhs->whole_variable_referenced();
72
73 if (assignee == var)
74 return (assign->condition != NULL) ? NULL : assign->rhs;
75
76 break;
77 }
78
79 default:
80 break;
81 }
82 }
83
84 return NULL;
85 }
86
87
88 static int
calculate_iterations(ir_rvalue * from,ir_rvalue * to,ir_rvalue * increment,enum ir_expression_operation op,bool continue_from_then,bool swap_compare_operands,bool inc_before_terminator)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, bool inc_before_terminator)
92 {
93 if (from == NULL || to == NULL || increment == NULL)
94 return -1;
95
96 void *mem_ctx = ralloc_context(NULL);
97
98 ir_expression *const sub =
99 new(mem_ctx) ir_expression(ir_binop_sub, from->type, to, from);
100
101 ir_expression *const div =
102 new(mem_ctx) ir_expression(ir_binop_div, sub->type, sub, increment);
103
104 ir_constant *iter = div->constant_expression_value(mem_ctx);
105 if (iter == NULL) {
106 ralloc_free(mem_ctx);
107 return -1;
108 }
109
110 if (!iter->type->is_integer_32()) {
111 const ir_expression_operation op = iter->type->is_double()
112 ? ir_unop_d2i : ir_unop_f2i;
113 ir_rvalue *cast =
114 new(mem_ctx) ir_expression(op, glsl_type::int_type, iter, NULL);
115
116 iter = cast->constant_expression_value(mem_ctx);
117 }
118
119 int iter_value = iter->get_int_component(0);
120
121 /* Code after this block works under assumption that iterator will be
122 * incremented or decremented until it hits the limit,
123 * however the loop condition can be false on the first iteration.
124 * Handle such loops first.
125 */
126 {
127 ir_rvalue *first_value = from;
128 if (inc_before_terminator) {
129 first_value =
130 new(mem_ctx) ir_expression(ir_binop_add, from->type, from, increment);
131 }
132
133 ir_expression *cmp = swap_compare_operands
134 ? new(mem_ctx) ir_expression(op, glsl_type::bool_type, to, first_value)
135 : new(mem_ctx) ir_expression(op, glsl_type::bool_type, first_value, to);
136 if (continue_from_then)
137 cmp = new(mem_ctx) ir_expression(ir_unop_logic_not, cmp);
138
139 ir_constant *const cmp_result = cmp->constant_expression_value(mem_ctx);
140 assert(cmp_result != NULL);
141 if (cmp_result->get_bool_component(0)) {
142 ralloc_free(mem_ctx);
143 return 0;
144 }
145 }
146
147 /* Make sure that the calculated number of iterations satisfies the exit
148 * condition. This is needed to catch off-by-one errors and some types of
149 * ill-formed loops. For example, we need to detect that the following
150 * loop does not have a maximum iteration count.
151 *
152 * for (float x = 0.0; x != 0.9; x += 0.2)
153 * ;
154 */
155 const int bias[] = { -1, 0, 1 };
156 bool valid_loop = false;
157
158 for (unsigned i = 0; i < ARRAY_SIZE(bias); i++) {
159 /* Increment may be of type int, uint or float. */
160 switch (increment->type->base_type) {
161 case GLSL_TYPE_INT:
162 iter = new(mem_ctx) ir_constant(iter_value + bias[i]);
163 break;
164 case GLSL_TYPE_INT16:
165 iter = new(mem_ctx) ir_constant(uint16_t(iter_value + bias[i]));
166 break;
167 case GLSL_TYPE_UINT:
168 iter = new(mem_ctx) ir_constant(unsigned(iter_value + bias[i]));
169 break;
170 case GLSL_TYPE_UINT16:
171 iter = new(mem_ctx) ir_constant(uint16_t(iter_value + bias[i]));
172 break;
173 case GLSL_TYPE_FLOAT:
174 iter = new(mem_ctx) ir_constant(float(iter_value + bias[i]));
175 break;
176 case GLSL_TYPE_FLOAT16:
177 iter = new(mem_ctx) ir_constant(float16_t(float(iter_value + bias[i])));
178 break;
179 case GLSL_TYPE_DOUBLE:
180 iter = new(mem_ctx) ir_constant(double(iter_value + bias[i]));
181 break;
182 default:
183 unreachable("Unsupported type for loop iterator.");
184 }
185
186 ir_expression *const mul =
187 new(mem_ctx) ir_expression(ir_binop_mul, increment->type, iter,
188 increment);
189
190 ir_expression *const add =
191 new(mem_ctx) ir_expression(ir_binop_add, mul->type, mul, from);
192
193 ir_expression *cmp = swap_compare_operands
194 ? new(mem_ctx) ir_expression(op, glsl_type::bool_type, to, add)
195 : new(mem_ctx) ir_expression(op, glsl_type::bool_type, add, to);
196 if (continue_from_then)
197 cmp = new(mem_ctx) ir_expression(ir_unop_logic_not, cmp);
198
199 ir_constant *const cmp_result = cmp->constant_expression_value(mem_ctx);
200
201 assert(cmp_result != NULL);
202 if (cmp_result->get_bool_component(0)) {
203 iter_value += bias[i];
204 valid_loop = true;
205 break;
206 }
207 }
208
209 ralloc_free(mem_ctx);
210
211 if (inc_before_terminator) {
212 iter_value--;
213 }
214
215 return (valid_loop) ? iter_value : -1;
216 }
217
218 static bool
incremented_before_terminator(ir_loop * loop,ir_variable * var,ir_if * terminator)219 incremented_before_terminator(ir_loop *loop, ir_variable *var,
220 ir_if *terminator)
221 {
222 for (exec_node *node = loop->body_instructions.get_head();
223 !node->is_tail_sentinel();
224 node = node->get_next()) {
225 ir_instruction *ir = (ir_instruction *) node;
226
227 switch (ir->ir_type) {
228 case ir_type_if:
229 if (ir->as_if() == terminator)
230 return false;
231 break;
232
233 case ir_type_assignment: {
234 ir_assignment *assign = ir->as_assignment();
235 ir_variable *assignee = assign->lhs->whole_variable_referenced();
236
237 if (assignee == var) {
238 assert(assign->condition == NULL);
239 return true;
240 }
241
242 break;
243 }
244
245 default:
246 break;
247 }
248 }
249
250 unreachable("Unable to find induction variable");
251 }
252
253 /**
254 * Record the fact that the given loop variable was referenced inside the loop.
255 *
256 * \arg in_assignee is true if the reference was on the LHS of an assignment.
257 *
258 * \arg in_conditional_code_or_nested_loop is true if the reference occurred
259 * inside an if statement or a nested loop.
260 *
261 * \arg current_assignment is the ir_assignment node that the loop variable is
262 * on the LHS of, if any (ignored if \c in_assignee is false).
263 */
264 void
record_reference(bool in_assignee,bool in_conditional_code_or_nested_loop,ir_assignment * current_assignment)265 loop_variable::record_reference(bool in_assignee,
266 bool in_conditional_code_or_nested_loop,
267 ir_assignment *current_assignment)
268 {
269 if (in_assignee) {
270 assert(current_assignment != NULL);
271
272 if (in_conditional_code_or_nested_loop ||
273 current_assignment->condition != NULL) {
274 this->conditional_or_nested_assignment = true;
275 }
276
277 if (this->first_assignment == NULL) {
278 assert(this->num_assignments == 0);
279
280 this->first_assignment = current_assignment;
281 }
282
283 this->num_assignments++;
284 } else if (this->first_assignment == current_assignment) {
285 /* This catches the case where the variable is used in the RHS of an
286 * assignment where it is also in the LHS.
287 */
288 this->read_before_write = true;
289 }
290 }
291
292
loop_state()293 loop_state::loop_state()
294 {
295 this->ht = _mesa_pointer_hash_table_create(NULL);
296 this->mem_ctx = ralloc_context(NULL);
297 this->loop_found = false;
298 }
299
300
~loop_state()301 loop_state::~loop_state()
302 {
303 _mesa_hash_table_destroy(this->ht, NULL);
304 ralloc_free(this->mem_ctx);
305 }
306
307
308 loop_variable_state *
insert(ir_loop * ir)309 loop_state::insert(ir_loop *ir)
310 {
311 loop_variable_state *ls = new(this->mem_ctx) loop_variable_state;
312
313 _mesa_hash_table_insert(this->ht, ir, ls);
314 this->loop_found = true;
315
316 return ls;
317 }
318
319
320 loop_variable_state *
get(const ir_loop * ir)321 loop_state::get(const ir_loop *ir)
322 {
323 hash_entry *entry = _mesa_hash_table_search(this->ht, ir);
324 return entry ? (loop_variable_state *) entry->data : NULL;
325 }
326
327
328 loop_variable *
get(const ir_variable * ir)329 loop_variable_state::get(const ir_variable *ir)
330 {
331 if (ir == NULL)
332 return NULL;
333
334 hash_entry *entry = _mesa_hash_table_search(this->var_hash, ir);
335 return entry ? (loop_variable *) entry->data : NULL;
336 }
337
338
339 loop_variable *
insert(ir_variable * var)340 loop_variable_state::insert(ir_variable *var)
341 {
342 void *mem_ctx = ralloc_parent(this);
343 loop_variable *lv = rzalloc(mem_ctx, loop_variable);
344
345 lv->var = var;
346
347 _mesa_hash_table_insert(this->var_hash, lv->var, lv);
348 this->variables.push_tail(lv);
349
350 return lv;
351 }
352
353
354 loop_terminator *
insert(ir_if * if_stmt,bool continue_from_then)355 loop_variable_state::insert(ir_if *if_stmt, bool continue_from_then)
356 {
357 void *mem_ctx = ralloc_parent(this);
358 loop_terminator *t = new(mem_ctx) loop_terminator(if_stmt,
359 continue_from_then);
360
361 this->terminators.push_tail(t);
362
363 return t;
364 }
365
366
367 /**
368 * If the given variable already is recorded in the state for this loop,
369 * return the corresponding loop_variable object that records information
370 * about it.
371 *
372 * Otherwise, create a new loop_variable object to record information about
373 * the variable, and set its \c read_before_write field appropriately based on
374 * \c in_assignee.
375 *
376 * \arg in_assignee is true if this variable was encountered on the LHS of an
377 * assignment.
378 */
379 loop_variable *
get_or_insert(ir_variable * var,bool in_assignee)380 loop_variable_state::get_or_insert(ir_variable *var, bool in_assignee)
381 {
382 loop_variable *lv = this->get(var);
383
384 if (lv == NULL) {
385 lv = this->insert(var);
386 lv->read_before_write = !in_assignee;
387 }
388
389 return lv;
390 }
391
392
393 namespace {
394
395 class loop_analysis : public ir_hierarchical_visitor {
396 public:
397 loop_analysis(loop_state *loops);
398
399 virtual ir_visitor_status visit(ir_loop_jump *);
400 virtual ir_visitor_status visit(ir_dereference_variable *);
401
402 virtual ir_visitor_status visit_enter(ir_call *);
403
404 virtual ir_visitor_status visit_enter(ir_loop *);
405 virtual ir_visitor_status visit_leave(ir_loop *);
406 virtual ir_visitor_status visit_enter(ir_assignment *);
407 virtual ir_visitor_status visit_leave(ir_assignment *);
408 virtual ir_visitor_status visit_enter(ir_if *);
409 virtual ir_visitor_status visit_leave(ir_if *);
410
411 loop_state *loops;
412
413 int if_statement_depth;
414
415 ir_assignment *current_assignment;
416
417 exec_list state;
418 };
419
420 } /* anonymous namespace */
421
loop_analysis(loop_state * loops)422 loop_analysis::loop_analysis(loop_state *loops)
423 : loops(loops), if_statement_depth(0), current_assignment(NULL)
424 {
425 /* empty */
426 }
427
428
429 ir_visitor_status
visit(ir_loop_jump * ir)430 loop_analysis::visit(ir_loop_jump *ir)
431 {
432 (void) ir;
433
434 assert(!this->state.is_empty());
435
436 loop_variable_state *const ls =
437 (loop_variable_state *) this->state.get_head();
438
439 ls->num_loop_jumps++;
440
441 return visit_continue;
442 }
443
444
445 ir_visitor_status
visit_enter(ir_call *)446 loop_analysis::visit_enter(ir_call *)
447 {
448 /* Mark every loop that we're currently analyzing as containing an ir_call
449 * (even those at outer nesting levels).
450 */
451 foreach_in_list(loop_variable_state, ls, &this->state) {
452 ls->contains_calls = true;
453 }
454
455 return visit_continue_with_parent;
456 }
457
458
459 ir_visitor_status
visit(ir_dereference_variable * ir)460 loop_analysis::visit(ir_dereference_variable *ir)
461 {
462 /* If we're not somewhere inside a loop, there's nothing to do.
463 */
464 if (this->state.is_empty())
465 return visit_continue;
466
467 bool nested = false;
468
469 foreach_in_list(loop_variable_state, ls, &this->state) {
470 ir_variable *var = ir->variable_referenced();
471 loop_variable *lv = ls->get_or_insert(var, this->in_assignee);
472
473 lv->record_reference(this->in_assignee,
474 nested || this->if_statement_depth > 0,
475 this->current_assignment);
476 nested = true;
477 }
478
479 return visit_continue;
480 }
481
482 ir_visitor_status
visit_enter(ir_loop * ir)483 loop_analysis::visit_enter(ir_loop *ir)
484 {
485 loop_variable_state *ls = this->loops->insert(ir);
486 this->state.push_head(ls);
487
488 return visit_continue;
489 }
490
491 ir_visitor_status
visit_leave(ir_loop * ir)492 loop_analysis::visit_leave(ir_loop *ir)
493 {
494 loop_variable_state *const ls =
495 (loop_variable_state *) this->state.pop_head();
496
497 /* Function calls may contain side effects. These could alter any of our
498 * variables in ways that cannot be known, and may even terminate shader
499 * execution (say, calling discard in the fragment shader). So we can't
500 * rely on any of our analysis about assignments to variables.
501 *
502 * We could perform some conservative analysis (prove there's no statically
503 * possible assignment, etc.) but it isn't worth it for now; function
504 * inlining will allow us to unroll loops anyway.
505 */
506 if (ls->contains_calls)
507 return visit_continue;
508
509 foreach_in_list(ir_instruction, node, &ir->body_instructions) {
510 /* Skip over declarations at the start of a loop.
511 */
512 if (node->as_variable())
513 continue;
514
515 ir_if *if_stmt = ((ir_instruction *) node)->as_if();
516
517 if (if_stmt != NULL)
518 try_add_loop_terminator(ls, if_stmt);
519 }
520
521
522 foreach_in_list_safe(loop_variable, lv, &ls->variables) {
523 /* Move variables that are already marked as being loop constant to
524 * a separate list. These trivially don't need to be tested.
525 */
526 if (lv->is_loop_constant()) {
527 lv->remove();
528 ls->constants.push_tail(lv);
529 }
530 }
531
532 /* Each variable assigned in the loop that isn't already marked as being loop
533 * constant might still be loop constant. The requirements at this point
534 * are:
535 *
536 * - Variable is written before it is read.
537 *
538 * - Only one assignment to the variable.
539 *
540 * - All operands on the RHS of the assignment are also loop constants.
541 *
542 * The last requirement is the reason for the progress loop. A variable
543 * marked as a loop constant on one pass may allow other variables to be
544 * marked as loop constant on following passes.
545 */
546 bool progress;
547 do {
548 progress = false;
549
550 foreach_in_list_safe(loop_variable, lv, &ls->variables) {
551 if (lv->conditional_or_nested_assignment || (lv->num_assignments > 1))
552 continue;
553
554 /* Process the RHS of the assignment. If all of the variables
555 * accessed there are loop constants, then add this
556 */
557 ir_rvalue *const rhs = lv->first_assignment->rhs;
558 if (all_expression_operands_are_loop_constant(rhs, ls->var_hash)) {
559 lv->rhs_clean = true;
560
561 if (lv->is_loop_constant()) {
562 progress = true;
563
564 lv->remove();
565 ls->constants.push_tail(lv);
566 }
567 }
568 }
569 } while (progress);
570
571 /* The remaining variables that are not loop invariant might be loop
572 * induction variables.
573 */
574 foreach_in_list_safe(loop_variable, lv, &ls->variables) {
575 /* If there is more than one assignment to a variable, it cannot be a
576 * loop induction variable. This isn't strictly true, but this is a
577 * very simple induction variable detector, and it can't handle more
578 * complex cases.
579 */
580 if (lv->num_assignments > 1)
581 continue;
582
583 /* All of the variables with zero assignments in the loop are loop
584 * invariant, and they should have already been filtered out.
585 */
586 assert(lv->num_assignments == 1);
587 assert(lv->first_assignment != NULL);
588
589 /* The assignment to the variable in the loop must be unconditional and
590 * not inside a nested loop.
591 */
592 if (lv->conditional_or_nested_assignment)
593 continue;
594
595 /* Basic loop induction variables have a single assignment in the loop
596 * that has the form 'VAR = VAR + i' or 'VAR = VAR - i' where i is a
597 * loop invariant.
598 */
599 ir_rvalue *const inc =
600 get_basic_induction_increment(lv->first_assignment, ls->var_hash);
601 if (inc != NULL) {
602 lv->increment = inc;
603
604 lv->remove();
605 ls->induction_variables.push_tail(lv);
606 }
607 }
608
609 /* Search the loop terminating conditions for those of the form 'i < c'
610 * where i is a loop induction variable, c is a constant, and < is any
611 * relative operator. From each of these we can infer an iteration count.
612 * Also figure out which terminator (if any) produces the smallest
613 * iteration count--this is the limiting terminator.
614 */
615 foreach_in_list(loop_terminator, t, &ls->terminators) {
616 ir_if *if_stmt = t->ir;
617
618 /* If-statements can be either 'if (expr)' or 'if (deref)'. We only care
619 * about the former here.
620 */
621 ir_expression *cond = if_stmt->condition->as_expression();
622 if (cond == NULL)
623 continue;
624
625 switch (cond->operation) {
626 case ir_binop_less:
627 case ir_binop_gequal: {
628 /* The expressions that we care about will either be of the form
629 * 'counter < limit' or 'limit < counter'. Figure out which is
630 * which.
631 */
632 ir_rvalue *counter = cond->operands[0]->as_dereference_variable();
633 ir_constant *limit = cond->operands[1]->as_constant();
634 enum ir_expression_operation cmp = cond->operation;
635 bool swap_compare_operands = false;
636
637 if (limit == NULL) {
638 counter = cond->operands[1]->as_dereference_variable();
639 limit = cond->operands[0]->as_constant();
640 swap_compare_operands = true;
641 }
642
643 if ((counter == NULL) || (limit == NULL))
644 break;
645
646 ir_variable *var = counter->variable_referenced();
647
648 ir_rvalue *init = find_initial_value(ir, var);
649
650 loop_variable *lv = ls->get(var);
651 if (lv != NULL && lv->is_induction_var()) {
652 bool inc_before_terminator =
653 incremented_before_terminator(ir, var, t->ir);
654
655 t->iterations = calculate_iterations(init, limit, lv->increment,
656 cmp, t->continue_from_then,
657 swap_compare_operands,
658 inc_before_terminator);
659
660 if (t->iterations >= 0 &&
661 (ls->limiting_terminator == NULL ||
662 t->iterations < ls->limiting_terminator->iterations)) {
663 ls->limiting_terminator = t;
664 }
665 }
666 break;
667 }
668
669 default:
670 break;
671 }
672 }
673
674 return visit_continue;
675 }
676
677 ir_visitor_status
visit_enter(ir_if * ir)678 loop_analysis::visit_enter(ir_if *ir)
679 {
680 (void) ir;
681
682 if (!this->state.is_empty())
683 this->if_statement_depth++;
684
685 return visit_continue;
686 }
687
688 ir_visitor_status
visit_leave(ir_if * ir)689 loop_analysis::visit_leave(ir_if *ir)
690 {
691 (void) ir;
692
693 if (!this->state.is_empty())
694 this->if_statement_depth--;
695
696 return visit_continue;
697 }
698
699 ir_visitor_status
visit_enter(ir_assignment * ir)700 loop_analysis::visit_enter(ir_assignment *ir)
701 {
702 /* If we're not somewhere inside a loop, there's nothing to do.
703 */
704 if (this->state.is_empty())
705 return visit_continue_with_parent;
706
707 this->current_assignment = ir;
708
709 return visit_continue;
710 }
711
712 ir_visitor_status
visit_leave(ir_assignment * ir)713 loop_analysis::visit_leave(ir_assignment *ir)
714 {
715 /* Since the visit_enter exits with visit_continue_with_parent for this
716 * case, the loop state stack should never be empty here.
717 */
718 assert(!this->state.is_empty());
719
720 assert(this->current_assignment == ir);
721 this->current_assignment = NULL;
722
723 return visit_continue;
724 }
725
726
727 class examine_rhs : public ir_hierarchical_visitor {
728 public:
examine_rhs(hash_table * loop_variables)729 examine_rhs(hash_table *loop_variables)
730 {
731 this->only_uses_loop_constants = true;
732 this->loop_variables = loop_variables;
733 }
734
visit(ir_dereference_variable * ir)735 virtual ir_visitor_status visit(ir_dereference_variable *ir)
736 {
737 hash_entry *entry = _mesa_hash_table_search(this->loop_variables,
738 ir->var);
739 loop_variable *lv = entry ? (loop_variable *) entry->data : NULL;
740
741 assert(lv != NULL);
742
743 if (lv->is_loop_constant()) {
744 return visit_continue;
745 } else {
746 this->only_uses_loop_constants = false;
747 return visit_stop;
748 }
749 }
750
751 hash_table *loop_variables;
752 bool only_uses_loop_constants;
753 };
754
755
756 bool
all_expression_operands_are_loop_constant(ir_rvalue * ir,hash_table * variables)757 all_expression_operands_are_loop_constant(ir_rvalue *ir, hash_table *variables)
758 {
759 examine_rhs v(variables);
760
761 ir->accept(&v);
762
763 return v.only_uses_loop_constants;
764 }
765
766
767 ir_rvalue *
get_basic_induction_increment(ir_assignment * ir,hash_table * var_hash)768 get_basic_induction_increment(ir_assignment *ir, hash_table *var_hash)
769 {
770 /* The RHS must be a binary expression.
771 */
772 ir_expression *const rhs = ir->rhs->as_expression();
773 if ((rhs == NULL)
774 || ((rhs->operation != ir_binop_add)
775 && (rhs->operation != ir_binop_sub)))
776 return NULL;
777
778 /* One of the of operands of the expression must be the variable assigned.
779 * If the operation is subtraction, the variable in question must be the
780 * "left" operand.
781 */
782 ir_variable *const var = ir->lhs->variable_referenced();
783
784 ir_variable *const op0 = rhs->operands[0]->variable_referenced();
785 ir_variable *const op1 = rhs->operands[1]->variable_referenced();
786
787 if (((op0 != var) && (op1 != var))
788 || ((op1 == var) && (rhs->operation == ir_binop_sub)))
789 return NULL;
790
791 ir_rvalue *inc = (op0 == var) ? rhs->operands[1] : rhs->operands[0];
792
793 if (inc->as_constant() == NULL) {
794 ir_variable *const inc_var = inc->variable_referenced();
795 if (inc_var != NULL) {
796 hash_entry *entry = _mesa_hash_table_search(var_hash, inc_var);
797 loop_variable *lv = entry ? (loop_variable *) entry->data : NULL;
798
799 if (lv == NULL || !lv->is_loop_constant()) {
800 assert(lv != NULL);
801 inc = NULL;
802 }
803 } else
804 inc = NULL;
805 }
806
807 if ((inc != NULL) && (rhs->operation == ir_binop_sub)) {
808 void *mem_ctx = ralloc_parent(ir);
809
810 inc = new(mem_ctx) ir_expression(ir_unop_neg,
811 inc->type,
812 inc->clone(mem_ctx, NULL),
813 NULL);
814 }
815
816 return inc;
817 }
818
819
820 /**
821 * Detect whether an if-statement is a loop terminating condition, if so
822 * add it to the list of loop terminators.
823 *
824 * Detects if-statements of the form
825 *
826 * (if (expression bool ...) (...then_instrs...break))
827 *
828 * or
829 *
830 * (if (expression bool ...) ... (...else_instrs...break))
831 */
832 void
try_add_loop_terminator(loop_variable_state * ls,ir_if * ir)833 try_add_loop_terminator(loop_variable_state *ls, ir_if *ir)
834 {
835 ir_instruction *inst = (ir_instruction *) ir->then_instructions.get_tail();
836 ir_instruction *else_inst =
837 (ir_instruction *) ir->else_instructions.get_tail();
838
839 if (is_break(inst) || is_break(else_inst))
840 ls->insert(ir, is_break(else_inst));
841 }
842
843
844 loop_state *
analyze_loop_variables(exec_list * instructions)845 analyze_loop_variables(exec_list *instructions)
846 {
847 loop_state *loops = new loop_state;
848 loop_analysis v(loops);
849
850 v.run(instructions);
851 return v.loops;
852 }
853