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