1 /* Natural loop analysis code for GNU compiler.
2 Copyright (C) 2002 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
PARAMS ((rtx
, rtx
, regset
));
34 static void blocks_invariant_registers
PARAMS ((basic_block
*, int, regset
));
35 static void unmark_altered_insn
PARAMS ((rtx
, rtx
, struct unmark_altered_insn_data
*));
36 static void blocks_single_set_registers
PARAMS ((basic_block
*, int, rtx
*));
37 static int invariant_rtx_wrto_regs_p_helper
PARAMS ((rtx
*, regset
));
38 static bool invariant_rtx_wrto_regs_p
PARAMS ((rtx
, regset
));
39 static rtx test_for_iteration
PARAMS ((struct loop_desc
*desc
,
40 unsigned HOST_WIDE_INT
));
41 static bool constant_iterations
PARAMS ((struct loop_desc
*,
42 unsigned HOST_WIDE_INT
*,
44 static bool simple_loop_exit_p
PARAMS ((struct loops
*, struct loop
*,
47 static rtx variable_initial_value
PARAMS ((rtx
, regset
, rtx
, rtx
*));
48 static rtx variable_initial_values
PARAMS ((edge
, rtx
));
49 static bool simple_condition_p
PARAMS ((struct loop
*, rtx
,
50 regset
, struct loop_desc
*));
51 static basic_block simple_increment
PARAMS ((struct loops
*, struct loop
*,
52 rtx
*, struct loop_desc
*));
54 /* Checks whether BB is executed exactly once in each LOOP iteration. */
56 just_once_each_iteration_p (loops
, loop
, bb
)
61 /* It must be executed at least once each iteration. */
62 if (!dominated_by_p (loops
->cfg
.dom
, loop
->latch
, bb
))
66 if (bb
->loop_father
!= loop
)
69 /* But this was not enough. We might have some irreducible loop here. */
70 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
77 /* Unmarks modified registers; helper to blocks_invariant_registers. */
79 unmark_altered (what
, by
, regs
)
81 rtx by ATTRIBUTE_UNUSED
;
84 if (GET_CODE (what
) == SUBREG
)
85 what
= SUBREG_REG (what
);
88 CLEAR_REGNO_REG_SET (regs
, REGNO (what
));
91 /* Marks registers that are invariant inside blocks BBS. */
93 blocks_invariant_registers (bbs
, nbbs
, regs
)
101 for (i
= 0; i
< max_reg_num (); i
++)
102 SET_REGNO_REG_SET (regs
, i
);
103 for (i
= 0; i
< nbbs
; i
++)
104 for (insn
= bbs
[i
]->head
;
105 insn
!= NEXT_INSN (bbs
[i
]->end
);
106 insn
= NEXT_INSN (insn
))
108 note_stores (PATTERN (insn
),
109 (void (*) PARAMS ((rtx
, rtx
, void *))) unmark_altered
,
113 /* Unmarks modified registers; helper to blocks_single_set_registers. */
114 struct unmark_altered_insn_data
121 unmark_altered_insn (what
, by
, data
)
123 rtx by ATTRIBUTE_UNUSED
;
124 struct unmark_altered_insn_data
*data
;
128 if (GET_CODE (what
) == SUBREG
)
129 what
= SUBREG_REG (what
);
133 if (data
->regs
[rn
] == data
->insn
)
135 data
->regs
[rn
] = NULL
;
138 /* Marks registers that have just single simple set in BBS; the relevant
139 insn is returned in REGS. */
141 blocks_single_set_registers (bbs
, nbbs
, regs
)
148 struct unmark_altered_insn_data data
;
150 for (i
= 0; i
< max_reg_num (); i
++)
153 for (i
= 0; i
< nbbs
; i
++)
154 for (insn
= bbs
[i
]->head
;
155 insn
!= NEXT_INSN (bbs
[i
]->end
);
156 insn
= NEXT_INSN (insn
))
158 rtx set
= single_set (insn
);
161 if (!REG_P (SET_DEST (set
)))
163 regs
[REGNO (SET_DEST (set
))] = insn
;
167 for (i
= 0; i
< nbbs
; i
++)
168 for (insn
= bbs
[i
]->head
;
169 insn
!= NEXT_INSN (bbs
[i
]->end
);
170 insn
= NEXT_INSN (insn
))
175 note_stores (PATTERN (insn
),
176 (void (*) PARAMS ((rtx
, rtx
, void *))) unmark_altered_insn
,
181 /* Helper for invariant_rtx_wrto_regs_p. */
183 invariant_rtx_wrto_regs_p_helper (expr
, invariant_regs
)
185 regset invariant_regs
;
187 switch (GET_CODE (*expr
))
191 case UNSPEC_VOLATILE
:
202 return MEM_VOLATILE_P (*expr
);
205 /* If the memory is not constant, assume it is modified. If it is
206 constant, we still have to check the address. */
207 return !RTX_UNCHANGING_P (*expr
);
210 return !REGNO_REG_SET_P (invariant_regs
, REGNO (*expr
));
217 /* Checks that EXPR is invariant provided that INVARIANT_REGS are invariant. */
219 invariant_rtx_wrto_regs_p (expr
, invariant_regs
)
221 regset invariant_regs
;
223 return !for_each_rtx (&expr
, (rtx_function
) invariant_rtx_wrto_regs_p_helper
,
227 /* Checks whether CONDITION is a simple comparison in that one of operands
228 is register and the other one is invariant in the LOOP. Fills var, lim
229 and cond fields in DESC. */
231 simple_condition_p (loop
, condition
, invariant_regs
, desc
)
232 struct loop
*loop ATTRIBUTE_UNUSED
;
234 regset invariant_regs
;
235 struct loop_desc
*desc
;
239 /* Check condition. */
240 switch (GET_CODE (condition
))
257 /* Of integers or pointers. */
258 if (GET_MODE_CLASS (GET_MODE (XEXP (condition
, 0))) != MODE_INT
259 && GET_MODE_CLASS (GET_MODE (XEXP (condition
, 0))) != MODE_PARTIAL_INT
)
262 /* One of operands must be a simple register. */
263 op0
= XEXP (condition
, 0);
264 op1
= XEXP (condition
, 1);
266 /* One of operands must be invariant. */
267 if (invariant_rtx_wrto_regs_p (op0
, invariant_regs
))
269 /* And the other one must be a register. */
275 desc
->cond
= swap_condition (GET_CODE (condition
));
276 if (desc
->cond
== UNKNOWN
)
281 /* Check the other operand. */
282 if (!invariant_rtx_wrto_regs_p (op1
, invariant_regs
))
290 desc
->cond
= GET_CODE (condition
);
295 /* Checks whether DESC->var is incremented/decremented exactly once each
296 iteration. Fills in DESC->stride and returns block in that DESC->var is
299 simple_increment (loops
, loop
, simple_increment_regs
, desc
)
302 rtx
*simple_increment_regs
;
303 struct loop_desc
*desc
;
305 rtx mod_insn
, set
, set_src
, set_add
;
308 /* Find insn that modifies var. */
309 mod_insn
= simple_increment_regs
[REGNO (desc
->var
)];
312 mod_bb
= BLOCK_FOR_INSN (mod_insn
);
314 /* Check that it is executed exactly once each iteration. */
315 if (!just_once_each_iteration_p (loops
, loop
, mod_bb
))
318 /* mod_insn must be a simple increment/decrement. */
319 set
= single_set (mod_insn
);
322 if (!rtx_equal_p (SET_DEST (set
), desc
->var
))
325 set_src
= find_reg_equal_equiv_note (mod_insn
);
327 set_src
= SET_SRC (set
);
328 if (GET_CODE (set_src
) != PLUS
)
330 if (!rtx_equal_p (XEXP (set_src
, 0), desc
->var
))
333 /* Set desc->stride. */
334 set_add
= XEXP (set_src
, 1);
335 if (CONSTANT_P (set_add
))
336 desc
->stride
= set_add
;
343 /* Tries to find initial value of VAR in INSN. This value must be invariant
344 wrto INVARIANT_REGS. If SET_INSN is not NULL, insn in that var is set is
347 variable_initial_value (insn
, invariant_regs
, var
, set_insn
)
349 regset invariant_regs
;
356 /* Go back through cfg. */
357 bb
= BLOCK_FOR_INSN (insn
);
360 for (; insn
!= bb
->head
; insn
= PREV_INSN (insn
))
362 if (modified_between_p (var
, PREV_INSN (insn
), NEXT_INSN (insn
)))
365 note_stores (PATTERN (insn
),
366 (void (*) PARAMS ((rtx
, rtx
, void *))) unmark_altered
,
370 if (insn
!= bb
->head
)
372 /* We found place where var is set. */
377 set
= single_set (insn
);
380 set_dest
= SET_DEST (set
);
381 if (!rtx_equal_p (set_dest
, var
))
384 note
= find_reg_equal_equiv_note (insn
);
385 if (note
&& GET_CODE (XEXP (note
, 0)) != EXPR_LIST
)
386 val
= XEXP (note
, 0);
389 if (!invariant_rtx_wrto_regs_p (val
, invariant_regs
))
398 if (bb
->pred
->pred_next
|| bb
->pred
->src
== ENTRY_BLOCK_PTR
)
408 /* Returns list of definitions of initial value of VAR at Edge. */
410 variable_initial_values (e
, var
)
415 regset invariant_regs
;
416 regset_head invariant_regs_head
;
419 invariant_regs
= INITIALIZE_REG_SET (invariant_regs_head
);
420 for (i
= 0; i
< max_reg_num (); i
++)
421 SET_REGNO_REG_SET (invariant_regs
, i
);
423 list
= alloc_EXPR_LIST (0, copy_rtx (var
), NULL
);
425 if (e
->src
== ENTRY_BLOCK_PTR
)
428 set_insn
= e
->src
->end
;
430 && (var
= variable_initial_value (set_insn
, invariant_regs
, var
, &set_insn
)))
431 list
= alloc_EXPR_LIST (0, copy_rtx (var
), list
);
433 FREE_REG_SET (invariant_regs
);
437 /* Counts constant number of iterations of the loop described by DESC;
438 returns false if impossible. */
440 constant_iterations (desc
, niter
, may_be_zero
)
441 struct loop_desc
*desc
;
442 unsigned HOST_WIDE_INT
*niter
;
448 test
= test_for_iteration (desc
, 0);
449 if (test
== const0_rtx
)
452 *may_be_zero
= false;
456 *may_be_zero
= (test
!= const_true_rtx
);
458 /* It would make a little sense to check every with every when we
459 know that all but the first alternative are simply registers. */
460 for (ainit
= desc
->var_alts
; ainit
; ainit
= XEXP (ainit
, 1))
462 alim
= XEXP (desc
->lim_alts
, 0);
463 if (!(expr
= count_loop_iterations (desc
, XEXP (ainit
, 0), alim
)))
465 if (GET_CODE (expr
) == CONST_INT
)
467 *niter
= INTVAL (expr
);
471 for (alim
= XEXP (desc
->lim_alts
, 1); alim
; alim
= XEXP (alim
, 1))
473 ainit
= XEXP (desc
->var_alts
, 0);
474 if (!(expr
= count_loop_iterations (desc
, ainit
, XEXP (alim
, 0))))
476 if (GET_CODE (expr
) == CONST_INT
)
478 *niter
= INTVAL (expr
);
486 /* Return RTX expression representing number of iterations of loop as bounded
487 by test described by DESC (in the case loop really has multiple exit
488 edges, fewer iterations may happen in the practice).
490 Return NULL if it is unknown. Additionally the value may be invalid for
491 paradoxical loop (lets define paradoxical loops as loops whose test is
492 failing at -1th iteration, for instance "for (i=5;i<1;i++);").
494 These cases needs to be either cared by copying the loop test in the front
495 of loop or keeping the test in first iteration of loop.
497 When INIT/LIM are set, they are used instead of var/lim of DESC. */
499 count_loop_iterations (desc
, init
, lim
)
500 struct loop_desc
*desc
;
504 enum rtx_code cond
= desc
->cond
;
505 rtx stride
= desc
->stride
;
508 /* Give up on floating point modes and friends. It can be possible to do
509 the job for constant loop bounds, but it is probably not worthwhile. */
510 if (!INTEGRAL_MODE_P (GET_MODE (desc
->var
)))
513 init
= copy_rtx (init
? init
: desc
->var
);
514 lim
= copy_rtx (lim
? lim
: desc
->lim
);
516 /* Ensure that we always handle the condition to stay inside loop. */
518 cond
= reverse_condition (cond
);
520 /* Compute absolute value of the difference of initial and final value. */
521 if (INTVAL (stride
) > 0)
523 /* Bypass nonsensical tests. */
524 if (cond
== EQ
|| cond
== GE
|| cond
== GT
|| cond
== GEU
527 exp
= simplify_gen_binary (MINUS
, GET_MODE (desc
->var
),
532 /* Bypass nonsensical tests. */
533 if (cond
== EQ
|| cond
== LE
|| cond
== LT
|| cond
== LEU
536 exp
= simplify_gen_binary (MINUS
, GET_MODE (desc
->var
),
538 stride
= simplify_gen_unary (NEG
, GET_MODE (desc
->var
),
539 stride
, GET_MODE (desc
->var
));
542 /* Normalize difference so the value is always first examined
543 and later incremented. */
546 exp
= simplify_gen_binary (MINUS
, GET_MODE (desc
->var
),
549 /* Determine delta caused by exit condition. */
553 /* For NE tests, make sure that the iteration variable won't miss
554 the final value. If EXP mod STRIDE is not zero, then the
555 iteration variable will overflow before the loop exits, and we
556 can not calculate the number of iterations easily. */
557 if (stride
!= const1_rtx
558 && (simplify_gen_binary (UMOD
, GET_MODE (desc
->var
), exp
, stride
)
571 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
578 if (stride
!= const1_rtx
)
580 /* Number of iterations is now (EXP + STRIDE - 1 / STRIDE),
581 but we need to take care for overflows. */
583 mod
= simplify_gen_binary (UMOD
, GET_MODE (desc
->var
), exp
, stride
);
585 /* This is dirty trick. When we can't compute number of iterations
586 to be constant, we simply ignore the possible overflow, as
587 runtime unroller always use power of 2 amounts and does not
588 care about possible lost bits. */
590 if (GET_CODE (mod
) != CONST_INT
)
592 rtx stridem1
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
593 stride
, constm1_rtx
);
594 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
596 exp
= simplify_gen_binary (UDIV
, GET_MODE (desc
->var
), exp
, stride
);
600 exp
= simplify_gen_binary (UDIV
, GET_MODE (desc
->var
), exp
, stride
);
601 if (mod
!= const0_rtx
)
602 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
609 fprintf (rtl_dump_file
, "; Number of iterations: ");
610 print_simple_rtl (rtl_dump_file
, exp
);
611 fprintf (rtl_dump_file
, "\n");
617 /* Return simplified RTX expression representing the value of test
618 described of DESC at given iteration of loop. */
621 test_for_iteration (desc
, iter
)
622 struct loop_desc
*desc
;
623 unsigned HOST_WIDE_INT iter
;
625 enum rtx_code cond
= desc
->cond
;
626 rtx exp
= XEXP (desc
->var_alts
, 0);
629 /* Give up on floating point modes and friends. It can be possible to do
630 the job for constant loop bounds, but it is probably not worthwhile. */
631 if (!INTEGRAL_MODE_P (GET_MODE (desc
->var
)))
634 /* Ensure that we always handle the condition to stay inside loop. */
636 cond
= reverse_condition (cond
);
638 /* Compute the value of induction variable. */
639 addval
= simplify_gen_binary (MULT
, GET_MODE (desc
->var
),
641 gen_int_mode (desc
->postincr
643 GET_MODE (desc
->var
)));
644 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
), exp
, addval
);
645 /* Test at given condition. */
646 exp
= simplify_gen_relational (cond
, SImode
,
647 GET_MODE (desc
->var
), exp
, desc
->lim
);
651 fprintf (rtl_dump_file
, "; Conditional to continue loop at "
652 HOST_WIDE_INT_PRINT_UNSIGNED
"th iteration: ", iter
);
653 print_simple_rtl (rtl_dump_file
, exp
);
654 fprintf (rtl_dump_file
, "\n");
660 /* Tests whether exit at EXIT_EDGE from LOOP is simple. Returns simple loop
661 description joined to it in in DESC. INVARIANT_REGS and SINGLE_SET_REGS
662 are results of blocks_{invariant,single_set}_regs over BODY. */
664 simple_loop_exit_p (loops
, loop
, exit_edge
, invariant_regs
, single_set_regs
, desc
)
668 struct loop_desc
*desc
;
669 regset invariant_regs
;
670 rtx
*single_set_regs
;
672 basic_block mod_bb
, exit_bb
;
677 exit_bb
= exit_edge
->src
;
679 fallthru_out
= (exit_edge
->flags
& EDGE_FALLTHRU
);
684 /* It must be tested (at least) once during any iteration. */
685 if (!dominated_by_p (loops
->cfg
.dom
, loop
->latch
, exit_bb
))
688 /* It must end in a simple conditional jump. */
689 if (!any_condjump_p (exit_bb
->end
))
696 desc
->out_edge
= exit_edge
;
699 /* Condition must be a simple comparison in that one of operands
700 is register and the other one is invariant. */
701 if (!(condition
= get_condition (exit_bb
->end
, NULL
)))
704 if (!simple_condition_p (loop
, condition
, invariant_regs
, desc
))
707 /* Var must be simply incremented or decremented in exactly one insn that
708 is executed just once every iteration. */
709 if (!(mod_bb
= simple_increment (loops
, loop
, single_set_regs
, desc
)))
712 /* OK, it is simple loop. Now just fill in remaining info. */
713 desc
->postincr
= !dominated_by_p (loops
->cfg
.dom
, exit_bb
, mod_bb
);
714 desc
->neg
= !fallthru_out
;
716 /* Find initial value of var and alternative values for lim. */
717 e
= loop_preheader_edge (loop
);
718 desc
->var_alts
= variable_initial_values (e
, desc
->var
);
719 desc
->lim_alts
= variable_initial_values (e
, desc
->lim
);
721 /* Number of iterations. */
722 if (!count_loop_iterations (desc
, NULL
, NULL
))
725 constant_iterations (desc
, &desc
->niter
, &desc
->may_be_zero
);
729 /* Tests whether LOOP is simple for loop. Returns simple loop description
732 simple_loop_p (loops
, loop
, desc
)
735 struct loop_desc
*desc
;
740 struct loop_desc act
;
742 regset invariant_regs
;
743 regset_head invariant_regs_head
;
744 rtx
*single_set_regs
;
747 body
= get_loop_body (loop
);
749 invariant_regs
= INITIALIZE_REG_SET (invariant_regs_head
);
750 single_set_regs
= xmalloc (max_reg_num () * sizeof (rtx
));
752 blocks_invariant_registers (body
, loop
->num_nodes
, invariant_regs
);
753 blocks_single_set_registers (body
, loop
->num_nodes
, single_set_regs
);
756 for (i
= 0; i
< loop
->num_nodes
; i
++)
758 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
759 if (!flow_bb_inside_loop_p (loop
, e
->dest
)
760 && simple_loop_exit_p (loops
, loop
, e
,
761 invariant_regs
, single_set_regs
, &act
))
763 /* Prefer constant iterations; the less the better. */
766 else if (!act
.const_iter
767 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
772 if (body
[i
]->succ
&& body
[i
]->succ
->succ_next
)
775 desc
->n_branches
= n_branches
;
777 if (rtl_dump_file
&& any
)
779 fprintf (rtl_dump_file
, "; Simple loop %i\n", loop
->num
);
781 fprintf (rtl_dump_file
,
782 "; does postincrement after loop exit condition\n");
784 fprintf (rtl_dump_file
, "; Induction variable:");
785 print_simple_rtl (rtl_dump_file
, desc
->var
);
786 fputc ('\n', rtl_dump_file
);
788 fprintf (rtl_dump_file
, "; Initial values:");
789 print_simple_rtl (rtl_dump_file
, desc
->var_alts
);
790 fputc ('\n', rtl_dump_file
);
792 fprintf (rtl_dump_file
, "; Stride:");
793 print_simple_rtl (rtl_dump_file
, desc
->stride
);
794 fputc ('\n', rtl_dump_file
);
796 fprintf (rtl_dump_file
, "; Compared with:");
797 print_simple_rtl (rtl_dump_file
, desc
->lim
);
798 fputc ('\n', rtl_dump_file
);
800 fprintf (rtl_dump_file
, "; Alternative values:");
801 print_simple_rtl (rtl_dump_file
, desc
->lim_alts
);
802 fputc ('\n', rtl_dump_file
);
804 fprintf (rtl_dump_file
, "; Exit condition:");
806 fprintf (rtl_dump_file
, "(negated)");
807 fprintf (rtl_dump_file
, "%s\n", GET_RTX_NAME (desc
->cond
));
809 fprintf (rtl_dump_file
, "; Number of branches:");
810 fprintf (rtl_dump_file
, "%d\n", desc
->n_branches
);
812 fputc ('\n', rtl_dump_file
);
816 FREE_REG_SET (invariant_regs
);
817 free (single_set_regs
);
821 /* Marks blocks and edges that are part of non-recognized loops; i.e. we
822 throw away all latch edges and mark blocks inside any remaining cycle.
823 Everything is a bit complicated due to fact we do not want to do this
824 for parts of cycles that only "pass" through some loop -- i.e. for
825 each cycle, we want to mark blocks that belong directly to innermost
826 loop containing the whole cycle. */
828 mark_irreducible_loops (loops
)
831 int *dfs_in
, *closed
, *mr
, *mri
, *n_edges
, *stack
;
836 int stack_top
, tick
, depth
;
839 /* Reset the flags. */
840 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
842 act
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
843 for (e
= act
->succ
; e
; e
= e
->succ_next
)
844 e
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
847 /* The first last_basic_block + 1 entries are for real blocks (including
848 entry); then we have loops->num - 1 fake blocks for loops to that we
849 assign edges leading from loops (fake loop 0 is not interesting). */
850 dfs_in
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
851 closed
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
852 mr
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
853 mri
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
854 n_edges
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
855 edges
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (edge
*));
856 stack
= xmalloc ((n_basic_blocks
+ loops
->num
) * sizeof (int));
857 estack
= xmalloc ((n_basic_blocks
+ loops
->num
) * sizeof (edge
));
859 /* Create the edge lists. */
860 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
862 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
863 for (e
= act
->succ
; e
; e
= e
->succ_next
)
865 /* Ignore edges to exit. */
866 if (e
->dest
== EXIT_BLOCK_PTR
)
868 /* And latch edges. */
869 if (e
->dest
->loop_father
->header
== e
->dest
870 && e
->dest
->loop_father
->latch
== act
)
872 /* Edges inside a single loop should be left where they are. Edges
873 to subloop headers should lead to representative of the subloop,
874 but from the same place. */
875 if (act
->loop_father
== e
->dest
->loop_father
876 || act
->loop_father
== e
->dest
->loop_father
->outer
)
878 n_edges
[act
->index
+ 1]++;
881 /* Edges exiting loops remain. They should lead from representative
882 of the son of nearest common ancestor of the loops in that
884 depth
= find_common_loop (act
->loop_father
, e
->dest
->loop_father
)->depth
+ 1;
885 if (depth
== act
->loop_father
->depth
)
886 cloop
= act
->loop_father
;
888 cloop
= act
->loop_father
->pred
[depth
];
889 n_edges
[cloop
->num
+ last_basic_block
]++;
892 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
894 edges
[i
] = xmalloc (n_edges
[i
] * sizeof (edge
));
898 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
899 for (e
= act
->succ
; e
; e
= e
->succ_next
)
901 if (e
->dest
== EXIT_BLOCK_PTR
)
903 if (e
->dest
->loop_father
->header
== e
->dest
904 && e
->dest
->loop_father
->latch
== act
)
906 if (act
->loop_father
== e
->dest
->loop_father
907 || act
->loop_father
== e
->dest
->loop_father
->outer
)
909 edges
[act
->index
+ 1][n_edges
[act
->index
+ 1]++] = e
;
912 depth
= find_common_loop (act
->loop_father
, e
->dest
->loop_father
)->depth
+ 1;
913 if (depth
== act
->loop_father
->depth
)
914 cloop
= act
->loop_father
;
916 cloop
= act
->loop_father
->pred
[depth
];
917 i
= cloop
->num
+ last_basic_block
;
918 edges
[i
][n_edges
[i
]++] = e
;
921 /* Compute dfs numbering, starting from loop headers, and mark found
924 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
928 mr
[i
] = last_basic_block
+ loops
->num
;
933 for (i
= 0; i
< loops
->num
; i
++)
934 if (loops
->parray
[i
])
936 stack
[stack_top
] = loops
->parray
[i
]->header
->index
+ 1;
937 estack
[stack_top
] = NULL
;
945 idx
= stack
[stack_top
- 1];
947 dfs_in
[idx
] = tick
++;
951 e
= edges
[idx
][--n_edges
[idx
]];
952 sidx
= e
->dest
->loop_father
->header
== e
->dest
953 ? e
->dest
->loop_father
->num
+ last_basic_block
954 : e
->dest
->index
+ 1;
957 if (!closed
[mri
[sidx
]])
959 if (mr
[sidx
] < mr
[idx
])
962 mri
[idx
] = mri
[sidx
];
965 if (mr
[sidx
] <= dfs_in
[idx
])
966 e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
970 if (dfs_in
[sidx
] < 0)
972 stack
[stack_top
] = sidx
;
973 estack
[stack_top
] = e
;
977 if (dfs_in
[sidx
] < mr
[idx
])
979 mr
[idx
] = dfs_in
[sidx
];
982 e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
987 e
= estack
[stack_top
- 1];
991 /* Propagate information back. */
992 sidx
= stack
[stack_top
- 1];
993 if (mr
[sidx
] > mr
[idx
])
996 mri
[sidx
] = mri
[idx
];
998 if (mr
[idx
] <= dfs_in
[sidx
])
999 e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1001 /* Mark the block if relevant. */
1002 if (idx
&& idx
<= last_basic_block
&& mr
[idx
] <= dfs_in
[idx
])
1003 BASIC_BLOCK (idx
- 1)->flags
|= BB_IRREDUCIBLE_LOOP
;
1013 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
1017 loops
->state
|= LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
;
1020 /* Counts number of insns inside LOOP. */
1022 num_loop_insns (loop
)
1025 basic_block
*bbs
, bb
;
1026 unsigned i
, ninsns
= 0;
1029 bbs
= get_loop_body (loop
);
1030 for (i
= 0; i
< loop
->num_nodes
; i
++)
1034 for (insn
= bb
->head
; insn
!= bb
->end
; insn
= NEXT_INSN (insn
))
1043 /* Counts number of insns executed on average per iteration LOOP. */
1045 average_num_loop_insns (loop
)
1048 basic_block
*bbs
, bb
;
1049 unsigned i
, binsns
, ninsns
, ratio
;
1053 bbs
= get_loop_body (loop
);
1054 for (i
= 0; i
< loop
->num_nodes
; i
++)
1059 for (insn
= bb
->head
; insn
!= bb
->end
; insn
= NEXT_INSN (insn
))
1063 ratio
= loop
->header
->frequency
== 0
1065 : (bb
->frequency
* BB_FREQ_MAX
) / loop
->header
->frequency
;
1066 ninsns
+= binsns
* ratio
;
1070 ninsns
/= BB_FREQ_MAX
;
1072 ninsns
= 1; /* To avoid division by zero. */
1077 /* Returns expected number of LOOP iterations.
1078 Compute upper bound on number of iterations in case they do not fit integer
1079 to help loop peeling heuristics. Use exact counts if at all possible. */
1081 expected_loop_iterations (loop
)
1082 const struct loop
*loop
;
1086 if (loop
->header
->count
)
1088 gcov_type count_in
, count_latch
, expected
;
1093 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1094 if (e
->src
== loop
->latch
)
1095 count_latch
= e
->count
;
1097 count_in
+= e
->count
;
1102 expected
= (count_latch
+ count_in
- 1) / count_in
;
1104 /* Avoid overflows. */
1105 return (expected
> REG_BR_PROB_BASE
? REG_BR_PROB_BASE
: expected
);
1109 int freq_in
, freq_latch
;
1114 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1115 if (e
->src
== loop
->latch
)
1116 freq_latch
= EDGE_FREQUENCY (e
);
1118 freq_in
+= EDGE_FREQUENCY (e
);
1123 return (freq_latch
+ freq_in
- 1) / freq_in
;