1 /* Natural loop analysis code for GNU compiler.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
32 struct unmark_altered_insn_data;
33 static void unmark_altered (rtx, rtx, regset);
34 static void blocks_invariant_registers (basic_block *, int, regset);
35 static void unmark_altered_insn (rtx, rtx, struct unmark_altered_insn_data *);
36 static void blocks_single_set_registers (basic_block *, int, rtx *);
37 static int invariant_rtx_wrto_regs_p_helper (rtx *, regset);
38 static bool invariant_rtx_wrto_regs_p (rtx, regset);
39 static rtx test_for_iteration (struct loop_desc *desc, unsigned HOST_WIDE_INT);
40 static bool constant_iterations (struct loop_desc *, unsigned HOST_WIDE_INT *,
42 static bool simple_loop_exit_p (struct loops *, struct loop *, edge, regset,
43 rtx *, struct loop_desc *);
44 static rtx variable_initial_value (rtx, regset, rtx, rtx *);
45 static rtx variable_initial_values (edge, rtx);
46 static bool simple_condition_p (struct loop *, rtx, regset,
48 static basic_block simple_increment (struct loops *, struct loop *, rtx *,
51 /* Checks whether BB is executed exactly once in each LOOP iteration. */
53 just_once_each_iteration_p (struct loops *loops, struct loop *loop, basic_block bb)
55 /* It must be executed at least once each iteration. */
56 if (!dominated_by_p (loops->cfg.dom, loop->latch, bb))
60 if (bb->loop_father != loop)
63 /* But this was not enough. We might have some irreducible loop here. */
64 if (bb->flags & BB_IRREDUCIBLE_LOOP)
71 /* Unmarks modified registers; helper to blocks_invariant_registers. */
73 unmark_altered (rtx what, rtx by ATTRIBUTE_UNUSED, regset regs)
75 if (GET_CODE (what) == SUBREG)
76 what = SUBREG_REG (what);
79 CLEAR_REGNO_REG_SET (regs, REGNO (what));
82 /* Marks registers that are invariant inside blocks BBS. */
84 blocks_invariant_registers (basic_block *bbs, int nbbs, regset regs)
89 for (i = 0; i < max_reg_num (); i++)
90 SET_REGNO_REG_SET (regs, i);
91 for (i = 0; i < nbbs; i++)
92 for (insn = bbs[i]->head;
93 insn != NEXT_INSN (bbs[i]->end);
94 insn = NEXT_INSN (insn))
96 note_stores (PATTERN (insn),
97 (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered,
101 /* Unmarks modified registers; helper to blocks_single_set_registers. */
102 struct unmark_altered_insn_data
109 unmark_altered_insn (rtx what, rtx by ATTRIBUTE_UNUSED,
110 struct unmark_altered_insn_data *data)
114 if (GET_CODE (what) == SUBREG)
115 what = SUBREG_REG (what);
119 if (data->regs[rn] == data->insn)
121 data->regs[rn] = NULL;
124 /* Marks registers that have just single simple set in BBS; the relevant
125 insn is returned in REGS. */
127 blocks_single_set_registers (basic_block *bbs, int nbbs, rtx *regs)
131 struct unmark_altered_insn_data data;
133 for (i = 0; i < max_reg_num (); i++)
136 for (i = 0; i < nbbs; i++)
137 for (insn = bbs[i]->head;
138 insn != NEXT_INSN (bbs[i]->end);
139 insn = NEXT_INSN (insn))
141 rtx set = single_set (insn);
144 if (!REG_P (SET_DEST (set)))
146 regs[REGNO (SET_DEST (set))] = insn;
150 for (i = 0; i < nbbs; i++)
151 for (insn = bbs[i]->head;
152 insn != NEXT_INSN (bbs[i]->end);
153 insn = NEXT_INSN (insn))
158 note_stores (PATTERN (insn),
159 (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered_insn,
164 /* Helper for invariant_rtx_wrto_regs_p. */
166 invariant_rtx_wrto_regs_p_helper (rtx *expr, regset invariant_regs)
168 switch (GET_CODE (*expr))
172 case UNSPEC_VOLATILE:
183 return MEM_VOLATILE_P (*expr);
186 /* If the memory is not constant, assume it is modified. If it is
187 constant, we still have to check the address. */
188 return !RTX_UNCHANGING_P (*expr);
191 return !REGNO_REG_SET_P (invariant_regs, REGNO (*expr));
198 /* Checks that EXPR is invariant provided that INVARIANT_REGS are invariant. */
200 invariant_rtx_wrto_regs_p (rtx expr, regset invariant_regs)
202 return !for_each_rtx (&expr, (rtx_function) invariant_rtx_wrto_regs_p_helper,
206 /* Checks whether CONDITION is a simple comparison in that one of operands
207 is register and the other one is invariant in the LOOP. Fills var, lim
208 and cond fields in DESC. */
210 simple_condition_p (struct loop *loop ATTRIBUTE_UNUSED, rtx condition,
211 regset invariant_regs, struct loop_desc *desc)
215 /* Check condition. */
216 switch (GET_CODE (condition))
233 /* Of integers or pointers. */
234 if (GET_MODE_CLASS (GET_MODE (XEXP (condition, 0))) != MODE_INT
235 && GET_MODE_CLASS (GET_MODE (XEXP (condition, 0))) != MODE_PARTIAL_INT)
238 /* One of operands must be a simple register. */
239 op0 = XEXP (condition, 0);
240 op1 = XEXP (condition, 1);
242 /* One of operands must be invariant. */
243 if (invariant_rtx_wrto_regs_p (op0, invariant_regs))
245 /* And the other one must be a register. */
251 desc->cond = swap_condition (GET_CODE (condition));
252 if (desc->cond == UNKNOWN)
257 /* Check the other operand. */
258 if (!invariant_rtx_wrto_regs_p (op1, invariant_regs))
266 desc->cond = GET_CODE (condition);
271 /* Checks whether DESC->var is incremented/decremented exactly once each
272 iteration. Fills in DESC->stride and returns block in that DESC->var is
275 simple_increment (struct loops *loops, struct loop *loop,
276 rtx *simple_increment_regs, struct loop_desc *desc)
278 rtx mod_insn, set, set_src, set_add;
281 /* Find insn that modifies var. */
282 mod_insn = simple_increment_regs[REGNO (desc->var)];
285 mod_bb = BLOCK_FOR_INSN (mod_insn);
287 /* Check that it is executed exactly once each iteration. */
288 if (!just_once_each_iteration_p (loops, loop, mod_bb))
291 /* mod_insn must be a simple increment/decrement. */
292 set = single_set (mod_insn);
295 if (!rtx_equal_p (SET_DEST (set), desc->var))
298 set_src = find_reg_equal_equiv_note (mod_insn);
300 set_src = SET_SRC (set);
301 if (GET_CODE (set_src) != PLUS)
303 if (!rtx_equal_p (XEXP (set_src, 0), desc->var))
306 /* Set desc->stride. */
307 set_add = XEXP (set_src, 1);
308 if (CONSTANT_P (set_add))
309 desc->stride = set_add;
316 /* Tries to find initial value of VAR in INSN. This value must be invariant
317 wrto INVARIANT_REGS. If SET_INSN is not NULL, insn in that var is set is
320 variable_initial_value (rtx insn, regset invariant_regs, rtx var, rtx *set_insn)
325 /* Go back through cfg. */
326 bb = BLOCK_FOR_INSN (insn);
329 for (; insn != bb->head; insn = PREV_INSN (insn))
332 note_stores (PATTERN (insn),
333 (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered,
335 if (modified_between_p (var, PREV_INSN (insn), NEXT_INSN (insn)))
339 if (insn != bb->head)
341 /* We found place where var is set. */
346 set = single_set (insn);
349 set_dest = SET_DEST (set);
350 if (!rtx_equal_p (set_dest, var))
353 note = find_reg_equal_equiv_note (insn);
354 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
355 val = XEXP (note, 0);
358 if (!invariant_rtx_wrto_regs_p (val, invariant_regs))
367 if (bb->pred->pred_next || bb->pred->src == ENTRY_BLOCK_PTR)
377 /* Returns list of definitions of initial value of VAR at Edge. */
379 variable_initial_values (edge e, rtx var)
382 regset invariant_regs;
383 regset_head invariant_regs_head;
386 invariant_regs = INITIALIZE_REG_SET (invariant_regs_head);
387 for (i = 0; i < max_reg_num (); i++)
388 SET_REGNO_REG_SET (invariant_regs, i);
390 list = alloc_EXPR_LIST (0, copy_rtx (var), NULL);
392 if (e->src == ENTRY_BLOCK_PTR)
395 set_insn = e->src->end;
397 && (var = variable_initial_value (set_insn, invariant_regs, var, &set_insn)))
398 list = alloc_EXPR_LIST (0, copy_rtx (var), list);
400 FREE_REG_SET (invariant_regs);
404 /* Counts constant number of iterations of the loop described by DESC;
405 returns false if impossible. */
407 constant_iterations (struct loop_desc *desc, unsigned HOST_WIDE_INT *niter,
413 test = test_for_iteration (desc, 0);
414 if (test == const0_rtx)
417 *may_be_zero = false;
421 *may_be_zero = (test != const_true_rtx);
423 /* It would make a little sense to check every with every when we
424 know that all but the first alternative are simply registers. */
425 for (ainit = desc->var_alts; ainit; ainit = XEXP (ainit, 1))
427 alim = XEXP (desc->lim_alts, 0);
428 if (!(expr = count_loop_iterations (desc, XEXP (ainit, 0), alim)))
430 if (GET_CODE (expr) == CONST_INT)
432 *niter = INTVAL (expr);
436 for (alim = XEXP (desc->lim_alts, 1); alim; alim = XEXP (alim, 1))
438 ainit = XEXP (desc->var_alts, 0);
439 if (!(expr = count_loop_iterations (desc, ainit, XEXP (alim, 0))))
441 if (GET_CODE (expr) == CONST_INT)
443 *niter = INTVAL (expr);
451 /* Return RTX expression representing number of iterations of loop as bounded
452 by test described by DESC (in the case loop really has multiple exit
453 edges, fewer iterations may happen in the practice).
455 Return NULL if it is unknown. Additionally the value may be invalid for
456 paradoxical loop (lets define paradoxical loops as loops whose test is
457 failing at -1th iteration, for instance "for (i=5;i<1;i++);").
459 These cases needs to be either cared by copying the loop test in the front
460 of loop or keeping the test in first iteration of loop.
462 When INIT/LIM are set, they are used instead of var/lim of DESC. */
464 count_loop_iterations (struct loop_desc *desc, rtx init, rtx lim)
466 enum rtx_code cond = desc->cond;
467 rtx stride = desc->stride;
470 /* Give up on floating point modes and friends. It can be possible to do
471 the job for constant loop bounds, but it is probably not worthwhile. */
472 if (!INTEGRAL_MODE_P (GET_MODE (desc->var)))
475 init = copy_rtx (init ? init : desc->var);
476 lim = copy_rtx (lim ? lim : desc->lim);
478 /* Ensure that we always handle the condition to stay inside loop. */
480 cond = reverse_condition (cond);
482 /* Compute absolute value of the difference of initial and final value. */
483 if (INTVAL (stride) > 0)
485 /* Bypass nonsensical tests. */
486 if (cond == EQ || cond == GE || cond == GT || cond == GEU
489 exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
494 /* Bypass nonsensical tests. */
495 if (cond == EQ || cond == LE || cond == LT || cond == LEU
498 exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
500 stride = simplify_gen_unary (NEG, GET_MODE (desc->var),
501 stride, GET_MODE (desc->var));
504 /* Normalize difference so the value is always first examined
505 and later incremented. */
508 exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
511 /* Determine delta caused by exit condition. */
515 /* For NE tests, make sure that the iteration variable won't miss
516 the final value. If EXP mod STRIDE is not zero, then the
517 iteration variable will overflow before the loop exits, and we
518 can not calculate the number of iterations easily. */
519 if (stride != const1_rtx
520 && (simplify_gen_binary (UMOD, GET_MODE (desc->var), exp, stride)
533 exp = simplify_gen_binary (PLUS, GET_MODE (desc->var),
540 if (stride != const1_rtx)
542 /* Number of iterations is now (EXP + STRIDE - 1 / STRIDE),
543 but we need to take care for overflows. */
545 mod = simplify_gen_binary (UMOD, GET_MODE (desc->var), exp, stride);
547 /* This is dirty trick. When we can't compute number of iterations
548 to be constant, we simply ignore the possible overflow, as
549 runtime unroller always use power of 2 amounts and does not
550 care about possible lost bits. */
552 if (GET_CODE (mod) != CONST_INT)
554 rtx stridem1 = simplify_gen_binary (PLUS, GET_MODE (desc->var),
555 stride, constm1_rtx);
556 exp = simplify_gen_binary (PLUS, GET_MODE (desc->var),
558 exp = simplify_gen_binary (UDIV, GET_MODE (desc->var), exp, stride);
562 exp = simplify_gen_binary (UDIV, GET_MODE (desc->var), exp, stride);
563 if (mod != const0_rtx)
564 exp = simplify_gen_binary (PLUS, GET_MODE (desc->var),
571 fprintf (rtl_dump_file, "; Number of iterations: ");
572 print_simple_rtl (rtl_dump_file, exp);
573 fprintf (rtl_dump_file, "\n");
579 /* Return simplified RTX expression representing the value of test
580 described of DESC at given iteration of loop. */
583 test_for_iteration (struct loop_desc *desc, unsigned HOST_WIDE_INT iter)
585 enum rtx_code cond = desc->cond;
586 rtx exp = XEXP (desc->var_alts, 0);
589 /* Give up on floating point modes and friends. It can be possible to do
590 the job for constant loop bounds, but it is probably not worthwhile. */
591 if (!INTEGRAL_MODE_P (GET_MODE (desc->var)))
594 /* Ensure that we always handle the condition to stay inside loop. */
596 cond = reverse_condition (cond);
598 /* Compute the value of induction variable. */
599 addval = simplify_gen_binary (MULT, GET_MODE (desc->var),
601 gen_int_mode (desc->postincr
603 GET_MODE (desc->var)));
604 exp = simplify_gen_binary (PLUS, GET_MODE (desc->var), exp, addval);
605 /* Test at given condition. */
606 exp = simplify_gen_relational (cond, SImode,
607 GET_MODE (desc->var), exp, desc->lim);
611 fprintf (rtl_dump_file, "; Conditional to continue loop at "
612 HOST_WIDE_INT_PRINT_UNSIGNED "th iteration: ", iter);
613 print_simple_rtl (rtl_dump_file, exp);
614 fprintf (rtl_dump_file, "\n");
620 /* Tests whether exit at EXIT_EDGE from LOOP is simple. Returns simple loop
621 description joined to it in in DESC. INVARIANT_REGS and SINGLE_SET_REGS
622 are results of blocks_{invariant,single_set}_regs over BODY. */
624 simple_loop_exit_p (struct loops *loops, struct loop *loop, edge exit_edge,
625 regset invariant_regs, rtx *single_set_regs,
626 struct loop_desc *desc)
628 basic_block mod_bb, exit_bb;
633 exit_bb = exit_edge->src;
635 fallthru_out = (exit_edge->flags & EDGE_FALLTHRU);
640 /* It must be tested (at least) once during any iteration. */
641 if (!dominated_by_p (loops->cfg.dom, loop->latch, exit_bb))
644 /* It must end in a simple conditional jump. */
645 if (!any_condjump_p (exit_bb->end))
652 desc->out_edge = exit_edge;
655 /* Condition must be a simple comparison in that one of operands
656 is register and the other one is invariant. */
657 if (!(condition = get_condition (exit_bb->end, NULL)))
660 if (!simple_condition_p (loop, condition, invariant_regs, desc))
663 /* Var must be simply incremented or decremented in exactly one insn that
664 is executed just once every iteration. */
665 if (!(mod_bb = simple_increment (loops, loop, single_set_regs, desc)))
668 /* OK, it is simple loop. Now just fill in remaining info. */
669 desc->postincr = !dominated_by_p (loops->cfg.dom, exit_bb, mod_bb);
670 desc->neg = !fallthru_out;
672 /* Find initial value of var and alternative values for lim. */
673 e = loop_preheader_edge (loop);
674 desc->var_alts = variable_initial_values (e, desc->var);
675 desc->lim_alts = variable_initial_values (e, desc->lim);
677 /* Number of iterations. */
678 if (!count_loop_iterations (desc, NULL, NULL))
681 constant_iterations (desc, &desc->niter, &desc->may_be_zero);
685 /* Tests whether LOOP is simple for loop. Returns simple loop description
688 simple_loop_p (struct loops *loops, struct loop *loop, struct loop_desc *desc)
693 struct loop_desc act;
695 regset invariant_regs;
696 regset_head invariant_regs_head;
697 rtx *single_set_regs;
700 body = get_loop_body (loop);
702 invariant_regs = INITIALIZE_REG_SET (invariant_regs_head);
703 single_set_regs = xmalloc (max_reg_num () * sizeof (rtx));
705 blocks_invariant_registers (body, loop->num_nodes, invariant_regs);
706 blocks_single_set_registers (body, loop->num_nodes, single_set_regs);
709 for (i = 0; i < loop->num_nodes; i++)
711 for (e = body[i]->succ; e; e = e->succ_next)
712 if (!flow_bb_inside_loop_p (loop, e->dest)
713 && simple_loop_exit_p (loops, loop, e,
714 invariant_regs, single_set_regs, &act))
716 /* Prefer constant iterations; the less the better. */
719 else if (!act.const_iter
720 || (desc->const_iter && act.niter >= desc->niter))
725 if (body[i]->succ && body[i]->succ->succ_next)
728 desc->n_branches = n_branches;
730 if (rtl_dump_file && any)
732 fprintf (rtl_dump_file, "; Simple loop %i\n", loop->num);
734 fprintf (rtl_dump_file,
735 "; does postincrement after loop exit condition\n");
737 fprintf (rtl_dump_file, "; Induction variable:");
738 print_simple_rtl (rtl_dump_file, desc->var);
739 fputc ('\n', rtl_dump_file);
741 fprintf (rtl_dump_file, "; Initial values:");
742 print_simple_rtl (rtl_dump_file, desc->var_alts);
743 fputc ('\n', rtl_dump_file);
745 fprintf (rtl_dump_file, "; Stride:");
746 print_simple_rtl (rtl_dump_file, desc->stride);
747 fputc ('\n', rtl_dump_file);
749 fprintf (rtl_dump_file, "; Compared with:");
750 print_simple_rtl (rtl_dump_file, desc->lim);
751 fputc ('\n', rtl_dump_file);
753 fprintf (rtl_dump_file, "; Alternative values:");
754 print_simple_rtl (rtl_dump_file, desc->lim_alts);
755 fputc ('\n', rtl_dump_file);
757 fprintf (rtl_dump_file, "; Exit condition:");
759 fprintf (rtl_dump_file, "(negated)");
760 fprintf (rtl_dump_file, "%s\n", GET_RTX_NAME (desc->cond));
762 fprintf (rtl_dump_file, "; Number of branches:");
763 fprintf (rtl_dump_file, "%d\n", desc->n_branches);
765 fputc ('\n', rtl_dump_file);
769 FREE_REG_SET (invariant_regs);
770 free (single_set_regs);
774 /* Marks blocks and edges that are part of non-recognized loops; i.e. we
775 throw away all latch edges and mark blocks inside any remaining cycle.
776 Everything is a bit complicated due to fact we do not want to do this
777 for parts of cycles that only "pass" through some loop -- i.e. for
778 each cycle, we want to mark blocks that belong directly to innermost
779 loop containing the whole cycle. */
781 mark_irreducible_loops (struct loops *loops)
783 int *dfs_in, *closed, *mr, *mri, *n_edges, *stack;
788 int stack_top, tick, depth;
791 /* Reset the flags. */
792 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
794 act->flags &= ~BB_IRREDUCIBLE_LOOP;
795 for (e = act->succ; e; e = e->succ_next)
796 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
799 /* The first last_basic_block + 1 entries are for real blocks (including
800 entry); then we have loops->num - 1 fake blocks for loops to that we
801 assign edges leading from loops (fake loop 0 is not interesting). */
802 dfs_in = xmalloc ((last_basic_block + loops->num) * sizeof (int));
803 closed = xmalloc ((last_basic_block + loops->num) * sizeof (int));
804 mr = xmalloc ((last_basic_block + loops->num) * sizeof (int));
805 mri = xmalloc ((last_basic_block + loops->num) * sizeof (int));
806 n_edges = xmalloc ((last_basic_block + loops->num) * sizeof (int));
807 edges = xmalloc ((last_basic_block + loops->num) * sizeof (edge *));
808 stack = xmalloc ((n_basic_blocks + loops->num) * sizeof (int));
809 estack = xmalloc ((n_basic_blocks + loops->num) * sizeof (edge));
811 /* Create the edge lists. */
812 for (i = 0; i < last_basic_block + loops->num; i++)
814 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
815 for (e = act->succ; e; e = e->succ_next)
817 /* Ignore edges to exit. */
818 if (e->dest == EXIT_BLOCK_PTR)
820 /* And latch edges. */
821 if (e->dest->loop_father->header == e->dest
822 && e->dest->loop_father->latch == act)
824 /* Edges inside a single loop should be left where they are. Edges
825 to subloop headers should lead to representative of the subloop,
826 but from the same place. */
827 if (act->loop_father == e->dest->loop_father
828 || act->loop_father == e->dest->loop_father->outer)
830 n_edges[act->index + 1]++;
833 /* Edges exiting loops remain. They should lead from representative
834 of the son of nearest common ancestor of the loops in that
836 depth = find_common_loop (act->loop_father, e->dest->loop_father)->depth + 1;
837 if (depth == act->loop_father->depth)
838 cloop = act->loop_father;
840 cloop = act->loop_father->pred[depth];
841 n_edges[cloop->num + last_basic_block]++;
844 for (i = 0; i < last_basic_block + loops->num; i++)
846 edges[i] = xmalloc (n_edges[i] * sizeof (edge));
850 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
851 for (e = act->succ; e; e = e->succ_next)
853 if (e->dest == EXIT_BLOCK_PTR)
855 if (e->dest->loop_father->header == e->dest
856 && e->dest->loop_father->latch == act)
858 if (act->loop_father == e->dest->loop_father
859 || act->loop_father == e->dest->loop_father->outer)
861 edges[act->index + 1][n_edges[act->index + 1]++] = e;
864 depth = find_common_loop (act->loop_father, e->dest->loop_father)->depth + 1;
865 if (depth == act->loop_father->depth)
866 cloop = act->loop_father;
868 cloop = act->loop_father->pred[depth];
869 i = cloop->num + last_basic_block;
870 edges[i][n_edges[i]++] = e;
873 /* Compute dfs numbering, starting from loop headers, and mark found
876 for (i = 0; i < last_basic_block + loops->num; i++)
880 mr[i] = last_basic_block + loops->num;
885 for (i = 0; i < loops->num; i++)
886 if (loops->parray[i])
888 stack[stack_top] = loops->parray[i]->header->index + 1;
889 estack[stack_top] = NULL;
897 idx = stack[stack_top - 1];
899 dfs_in[idx] = tick++;
903 e = edges[idx][--n_edges[idx]];
904 sidx = e->dest->loop_father->header == e->dest
905 ? e->dest->loop_father->num + last_basic_block
906 : e->dest->index + 1;
909 if (!closed[mri[sidx]])
911 if (mr[sidx] < mr[idx])
914 mri[idx] = mri[sidx];
917 if (mr[sidx] <= dfs_in[idx])
918 e->flags |= EDGE_IRREDUCIBLE_LOOP;
922 if (dfs_in[sidx] < 0)
924 stack[stack_top] = sidx;
925 estack[stack_top] = e;
929 if (dfs_in[sidx] < mr[idx])
931 mr[idx] = dfs_in[sidx];
934 e->flags |= EDGE_IRREDUCIBLE_LOOP;
939 e = estack[stack_top - 1];
943 /* Propagate information back. */
944 sidx = stack[stack_top - 1];
945 if (mr[sidx] > mr[idx])
948 mri[sidx] = mri[idx];
950 if (mr[idx] <= dfs_in[sidx])
951 e->flags |= EDGE_IRREDUCIBLE_LOOP;
953 /* Mark the block if relevant. */
954 if (idx && idx <= last_basic_block && mr[idx] <= dfs_in[idx])
955 BASIC_BLOCK (idx - 1)->flags |= BB_IRREDUCIBLE_LOOP;
965 for (i = 0; i < last_basic_block + loops->num; i++)
969 loops->state |= LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS;
972 /* Counts number of insns inside LOOP. */
974 num_loop_insns (struct loop *loop)
976 basic_block *bbs, bb;
977 unsigned i, ninsns = 0;
980 bbs = get_loop_body (loop);
981 for (i = 0; i < loop->num_nodes; i++)
985 for (insn = bb->head; insn != bb->end; insn = NEXT_INSN (insn))
994 /* Counts number of insns executed on average per iteration LOOP. */
996 average_num_loop_insns (struct loop *loop)
998 basic_block *bbs, bb;
999 unsigned i, binsns, ninsns, ratio;
1003 bbs = get_loop_body (loop);
1004 for (i = 0; i < loop->num_nodes; i++)
1009 for (insn = bb->head; insn != bb->end; insn = NEXT_INSN (insn))
1013 ratio = loop->header->frequency == 0
1015 : (bb->frequency * BB_FREQ_MAX) / loop->header->frequency;
1016 ninsns += binsns * ratio;
1020 ninsns /= BB_FREQ_MAX;
1022 ninsns = 1; /* To avoid division by zero. */
1027 /* Returns expected number of LOOP iterations.
1028 Compute upper bound on number of iterations in case they do not fit integer
1029 to help loop peeling heuristics. Use exact counts if at all possible. */
1031 expected_loop_iterations (const struct loop *loop)
1035 if (loop->header->count)
1037 gcov_type count_in, count_latch, expected;
1042 for (e = loop->header->pred; e; e = e->pred_next)
1043 if (e->src == loop->latch)
1044 count_latch = e->count;
1046 count_in += e->count;
1051 expected = (count_latch + count_in - 1) / count_in;
1053 /* Avoid overflows. */
1054 return (expected > REG_BR_PROB_BASE ? REG_BR_PROB_BASE : expected);
1058 int freq_in, freq_latch;
1063 for (e = loop->header->pred; e; e = e->pred_next)
1064 if (e->src == loop->latch)
1065 freq_latch = EDGE_FREQUENCY (e);
1067 freq_in += EDGE_FREQUENCY (e);
1072 return (freq_latch + freq_in - 1) / freq_in;