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
*,
50 static rtx
count_strange_loop_iterations (rtx
, rtx
, enum rtx_code
,
51 int, rtx
, enum machine_mode
);
53 /* Checks whether BB is executed exactly once in each LOOP iteration. */
55 just_once_each_iteration_p (struct loops
*loops
, struct loop
*loop
, basic_block bb
)
57 /* It must be executed at least once each iteration. */
58 if (!dominated_by_p (loops
->cfg
.dom
, loop
->latch
, bb
))
62 if (bb
->loop_father
!= loop
)
65 /* But this was not enough. We might have some irreducible loop here. */
66 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
73 /* Unmarks modified registers; helper to blocks_invariant_registers. */
75 unmark_altered (rtx what
, rtx by ATTRIBUTE_UNUSED
, regset regs
)
77 if (GET_CODE (what
) == SUBREG
)
78 what
= SUBREG_REG (what
);
81 CLEAR_REGNO_REG_SET (regs
, REGNO (what
));
84 /* Marks registers that are invariant inside blocks BBS. */
86 blocks_invariant_registers (basic_block
*bbs
, int nbbs
, regset regs
)
91 for (i
= 0; i
< max_reg_num (); i
++)
92 SET_REGNO_REG_SET (regs
, i
);
93 for (i
= 0; i
< nbbs
; i
++)
94 for (insn
= bbs
[i
]->head
;
95 insn
!= NEXT_INSN (bbs
[i
]->end
);
96 insn
= NEXT_INSN (insn
))
98 note_stores (PATTERN (insn
),
99 (void (*) (rtx
, rtx
, void *)) unmark_altered
,
103 /* Unmarks modified registers; helper to blocks_single_set_registers. */
104 struct unmark_altered_insn_data
111 unmark_altered_insn (rtx what
, rtx by ATTRIBUTE_UNUSED
,
112 struct unmark_altered_insn_data
*data
)
116 if (GET_CODE (what
) == SUBREG
)
117 what
= SUBREG_REG (what
);
121 if (data
->regs
[rn
] == data
->insn
)
123 data
->regs
[rn
] = NULL
;
126 /* Marks registers that have just single simple set in BBS; the relevant
127 insn is returned in REGS. */
129 blocks_single_set_registers (basic_block
*bbs
, int nbbs
, rtx
*regs
)
133 struct unmark_altered_insn_data data
;
135 for (i
= 0; i
< max_reg_num (); i
++)
138 for (i
= 0; i
< nbbs
; i
++)
139 for (insn
= bbs
[i
]->head
;
140 insn
!= NEXT_INSN (bbs
[i
]->end
);
141 insn
= NEXT_INSN (insn
))
143 rtx set
= single_set (insn
);
146 if (!REG_P (SET_DEST (set
)))
148 regs
[REGNO (SET_DEST (set
))] = insn
;
152 for (i
= 0; i
< nbbs
; i
++)
153 for (insn
= bbs
[i
]->head
;
154 insn
!= NEXT_INSN (bbs
[i
]->end
);
155 insn
= NEXT_INSN (insn
))
160 note_stores (PATTERN (insn
),
161 (void (*) (rtx
, rtx
, void *)) unmark_altered_insn
,
166 /* Helper for invariant_rtx_wrto_regs_p. */
168 invariant_rtx_wrto_regs_p_helper (rtx
*expr
, regset invariant_regs
)
170 switch (GET_CODE (*expr
))
174 case UNSPEC_VOLATILE
:
185 return MEM_VOLATILE_P (*expr
);
188 /* If the memory is not constant, assume it is modified. If it is
189 constant, we still have to check the address. */
190 return !RTX_UNCHANGING_P (*expr
);
193 return !REGNO_REG_SET_P (invariant_regs
, REGNO (*expr
));
200 /* Checks that EXPR is invariant provided that INVARIANT_REGS are invariant. */
202 invariant_rtx_wrto_regs_p (rtx expr
, regset invariant_regs
)
204 return !for_each_rtx (&expr
, (rtx_function
) invariant_rtx_wrto_regs_p_helper
,
208 /* Checks whether CONDITION is a simple comparison in that one of operands
209 is register and the other one is invariant in the LOOP. Fills var, lim
210 and cond fields in DESC. */
212 simple_condition_p (struct loop
*loop ATTRIBUTE_UNUSED
, rtx condition
,
213 regset invariant_regs
, struct loop_desc
*desc
)
217 /* Check condition. */
218 switch (GET_CODE (condition
))
235 /* Of integers or pointers. */
236 if (GET_MODE_CLASS (GET_MODE (XEXP (condition
, 0))) != MODE_INT
237 && GET_MODE_CLASS (GET_MODE (XEXP (condition
, 0))) != MODE_PARTIAL_INT
)
240 /* One of operands must be a simple register. */
241 op0
= XEXP (condition
, 0);
242 op1
= XEXP (condition
, 1);
244 /* One of operands must be invariant. */
245 if (invariant_rtx_wrto_regs_p (op0
, invariant_regs
))
247 /* And the other one must be a register. */
253 desc
->cond
= swap_condition (GET_CODE (condition
));
254 if (desc
->cond
== UNKNOWN
)
259 /* Check the other operand. */
260 if (!invariant_rtx_wrto_regs_p (op1
, invariant_regs
))
268 desc
->cond
= GET_CODE (condition
);
273 /* Checks whether DESC->var is incremented/decremented exactly once each
274 iteration. Fills in DESC->stride and returns block in that DESC->var is
277 simple_increment (struct loops
*loops
, struct loop
*loop
,
278 rtx
*simple_increment_regs
, struct loop_desc
*desc
)
280 rtx mod_insn
, set
, set_src
, set_add
;
283 /* Find insn that modifies var. */
284 mod_insn
= simple_increment_regs
[REGNO (desc
->var
)];
287 mod_bb
= BLOCK_FOR_INSN (mod_insn
);
289 /* Check that it is executed exactly once each iteration. */
290 if (!just_once_each_iteration_p (loops
, loop
, mod_bb
))
293 /* mod_insn must be a simple increment/decrement. */
294 set
= single_set (mod_insn
);
297 if (!rtx_equal_p (SET_DEST (set
), desc
->var
))
300 set_src
= find_reg_equal_equiv_note (mod_insn
);
302 set_src
= SET_SRC (set
);
303 if (GET_CODE (set_src
) != PLUS
)
305 if (!rtx_equal_p (XEXP (set_src
, 0), desc
->var
))
308 /* Set desc->stride. */
309 set_add
= XEXP (set_src
, 1);
310 if (CONSTANT_P (set_add
))
311 desc
->stride
= set_add
;
318 /* Tries to find initial value of VAR in INSN. This value must be invariant
319 wrto INVARIANT_REGS. If SET_INSN is not NULL, insn in that var is set is
322 variable_initial_value (rtx insn
, regset invariant_regs
, rtx var
, rtx
*set_insn
)
327 /* Go back through cfg. */
328 bb
= BLOCK_FOR_INSN (insn
);
331 for (; insn
!= bb
->head
; insn
= PREV_INSN (insn
))
334 note_stores (PATTERN (insn
),
335 (void (*) (rtx
, rtx
, void *)) unmark_altered
,
337 if (modified_between_p (var
, PREV_INSN (insn
), NEXT_INSN (insn
)))
341 if (insn
!= bb
->head
)
343 /* We found place where var is set. */
348 set
= single_set (insn
);
351 set_dest
= SET_DEST (set
);
352 if (!rtx_equal_p (set_dest
, var
))
355 note
= find_reg_equal_equiv_note (insn
);
356 if (note
&& GET_CODE (XEXP (note
, 0)) != EXPR_LIST
)
357 val
= XEXP (note
, 0);
360 if (!invariant_rtx_wrto_regs_p (val
, invariant_regs
))
369 if (bb
->pred
->pred_next
|| bb
->pred
->src
== ENTRY_BLOCK_PTR
)
379 /* Returns list of definitions of initial value of VAR at Edge. */
381 variable_initial_values (edge e
, rtx var
)
384 regset invariant_regs
;
385 regset_head invariant_regs_head
;
388 invariant_regs
= INITIALIZE_REG_SET (invariant_regs_head
);
389 for (i
= 0; i
< max_reg_num (); i
++)
390 SET_REGNO_REG_SET (invariant_regs
, i
);
392 list
= alloc_EXPR_LIST (0, copy_rtx (var
), NULL
);
394 if (e
->src
== ENTRY_BLOCK_PTR
)
397 set_insn
= e
->src
->end
;
399 && (var
= variable_initial_value (set_insn
, invariant_regs
, var
, &set_insn
)))
400 list
= alloc_EXPR_LIST (0, copy_rtx (var
), list
);
402 FREE_REG_SET (invariant_regs
);
406 /* Counts constant number of iterations of the loop described by DESC;
407 returns false if impossible. */
409 constant_iterations (struct loop_desc
*desc
, unsigned HOST_WIDE_INT
*niter
,
415 test
= test_for_iteration (desc
, 0);
416 if (test
== const0_rtx
)
419 *may_be_zero
= false;
423 *may_be_zero
= (test
!= const_true_rtx
);
425 /* It would make a little sense to check every with every when we
426 know that all but the first alternative are simply registers. */
427 for (ainit
= desc
->var_alts
; ainit
; ainit
= XEXP (ainit
, 1))
429 alim
= XEXP (desc
->lim_alts
, 0);
430 if (!(expr
= count_loop_iterations (desc
, XEXP (ainit
, 0), alim
)))
432 if (GET_CODE (expr
) == CONST_INT
)
434 *niter
= INTVAL (expr
);
438 for (alim
= XEXP (desc
->lim_alts
, 1); alim
; alim
= XEXP (alim
, 1))
440 ainit
= XEXP (desc
->var_alts
, 0);
441 if (!(expr
= count_loop_iterations (desc
, ainit
, XEXP (alim
, 0))))
443 if (GET_CODE (expr
) == CONST_INT
)
445 *niter
= INTVAL (expr
);
453 /* Attempts to determine a number of iterations of a "strange" loop.
454 Its induction variable starts with value INIT, is compared by COND
455 with LIM. If POSTINCR, it is incremented after the test. It is incremented
456 by STRIDE each iteration and iterates in MODE.
458 By "strange" we mean loops where induction variable increases in the wrong
459 direction wrto comparison, i.e. for (i = 6; i > 5; i++). */
461 count_strange_loop_iterations (rtx init
, rtx lim
, enum rtx_code cond
,
462 int postincr
, rtx stride
, enum machine_mode mode
)
464 rtx rqmt
, n_to_wrap
, before_wrap
, after_wrap
;
465 rtx mode_min
, mode_max
;
469 init
= simplify_gen_binary (PLUS
, mode
, init
, stride
);
471 /* If we are able to prove that we don't pass the first test, we are
473 rqmt
= simplify_gen_relational (cond
, SImode
, mode
, init
, lim
);
474 if (rqmt
== const0_rtx
)
477 /* And if we don't know we pass it, the things are too complicated for us. */
478 if (rqmt
!= const_true_rtx
)
487 size
= GET_MODE_BITSIZE (mode
);
488 mode_min
= GEN_INT (-((unsigned HOST_WIDEST_INT
) 1 << (size
- 1)));
489 mode_max
= GEN_INT (((unsigned HOST_WIDEST_INT
) 1 << (size
- 1)) - 1);
498 mode_min
= const0_rtx
;
499 mode_max
= simplify_gen_binary (MINUS
, mode
, const0_rtx
, const1_rtx
);
509 /* This iterates once, as init == lim. */
512 /* The behavior is undefined in signed cases. Never mind, we still
513 try to behave sanely. */
518 if (INTVAL (stride
) <= 0)
520 n_to_wrap
= simplify_gen_binary (MINUS
, mode
, mode_max
, copy_rtx (init
));
521 n_to_wrap
= simplify_gen_binary (UDIV
, mode
, n_to_wrap
, stride
);
522 before_wrap
= simplify_gen_binary (MULT
, mode
,
523 copy_rtx (n_to_wrap
), stride
);
524 before_wrap
= simplify_gen_binary (PLUS
, mode
,
525 before_wrap
, copy_rtx (init
));
526 after_wrap
= simplify_gen_binary (PLUS
, mode
,
527 before_wrap
, stride
);
528 if (GET_CODE (after_wrap
) != CONST_INT
)
530 after_wrap
= simplify_gen_binary (PLUS
, mode
, mode_min
, stride
);
531 after_wrap
= simplify_gen_binary (MINUS
, mode
, after_wrap
, const1_rtx
);
539 if (INTVAL (stride
) >= 0)
541 stride
= simplify_gen_unary (NEG
, mode
, stride
, mode
);
542 n_to_wrap
= simplify_gen_binary (MINUS
, mode
, copy_rtx (init
), mode_min
);
543 n_to_wrap
= simplify_gen_binary (UDIV
, mode
, n_to_wrap
, stride
);
544 before_wrap
= simplify_gen_binary (MULT
, mode
,
545 copy_rtx (n_to_wrap
), stride
);
546 before_wrap
= simplify_gen_binary (MINUS
, mode
,
547 copy_rtx (init
), before_wrap
);
548 after_wrap
= simplify_gen_binary (MINUS
, mode
,
549 before_wrap
, stride
);
550 if (GET_CODE (after_wrap
) != CONST_INT
)
552 after_wrap
= simplify_gen_binary (MINUS
, mode
, mode_max
, stride
);
553 after_wrap
= simplify_gen_binary (PLUS
, mode
, after_wrap
, const1_rtx
);
560 /* If this is const_true_rtx and we did not take a conservative aproximation
561 of after_wrap above, we might iterate the calculation (but of course we
562 would have to take care about infinite cases). Ignore this for now. */
563 rqmt
= simplify_gen_relational (cond
, SImode
, mode
, after_wrap
, lim
);
564 if (rqmt
!= const0_rtx
)
567 return simplify_gen_binary (PLUS
, mode
, n_to_wrap
, const1_rtx
);
570 /* Return RTX expression representing number of iterations of loop as bounded
571 by test described by DESC (in the case loop really has multiple exit
572 edges, fewer iterations may happen in the practice).
574 Return NULL if it is unknown. Additionally the value may be invalid for
575 paradoxical loop (lets define paradoxical loops as loops whose test is
576 failing at -1th iteration, for instance "for (i=5;i<1;i++);").
578 These cases needs to be either cared by copying the loop test in the front
579 of loop or keeping the test in first iteration of loop.
581 When INIT/LIM are set, they are used instead of var/lim of DESC. */
583 count_loop_iterations (struct loop_desc
*desc
, rtx init
, rtx lim
)
585 enum rtx_code cond
= desc
->cond
;
586 rtx stride
= desc
->stride
;
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 init
= copy_rtx (init
? init
: desc
->var
);
595 lim
= copy_rtx (lim
? lim
: desc
->lim
);
597 /* Ensure that we always handle the condition to stay inside loop. */
599 cond
= reverse_condition (cond
);
601 /* Compute absolute value of the difference of initial and final value. */
602 if (INTVAL (stride
) > 0)
604 /* Handle strange tests specially. */
605 if (cond
== EQ
|| cond
== GE
|| cond
== GT
|| cond
== GEU
607 return count_strange_loop_iterations (init
, lim
, cond
, desc
->postincr
,
608 stride
, GET_MODE (desc
->var
));
609 exp
= simplify_gen_binary (MINUS
, GET_MODE (desc
->var
),
614 /* Bypass nonsensical tests. */
615 if (cond
== EQ
|| cond
== LE
|| cond
== LT
|| cond
== LEU
617 return count_strange_loop_iterations (init
, lim
, cond
, desc
->postincr
,
618 stride
, GET_MODE (desc
->var
));
619 exp
= simplify_gen_binary (MINUS
, GET_MODE (desc
->var
),
621 stride
= simplify_gen_unary (NEG
, GET_MODE (desc
->var
),
622 stride
, GET_MODE (desc
->var
));
625 /* Normalize difference so the value is always first examined
626 and later incremented. */
629 exp
= simplify_gen_binary (MINUS
, GET_MODE (desc
->var
),
632 /* Determine delta caused by exit condition. */
636 /* For NE tests, make sure that the iteration variable won't miss
637 the final value. If EXP mod STRIDE is not zero, then the
638 iteration variable will overflow before the loop exits, and we
639 can not calculate the number of iterations easily. */
640 if (stride
!= const1_rtx
641 && (simplify_gen_binary (UMOD
, GET_MODE (desc
->var
), exp
, stride
)
654 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
661 if (stride
!= const1_rtx
)
663 /* Number of iterations is now (EXP + STRIDE - 1 / STRIDE),
664 but we need to take care for overflows. */
666 mod
= simplify_gen_binary (UMOD
, GET_MODE (desc
->var
), exp
, stride
);
668 /* This is dirty trick. When we can't compute number of iterations
669 to be constant, we simply ignore the possible overflow, as
670 runtime unroller always use power of 2 amounts and does not
671 care about possible lost bits. */
673 if (GET_CODE (mod
) != CONST_INT
)
675 rtx stridem1
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
676 stride
, constm1_rtx
);
677 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
679 exp
= simplify_gen_binary (UDIV
, GET_MODE (desc
->var
), exp
, stride
);
683 exp
= simplify_gen_binary (UDIV
, GET_MODE (desc
->var
), exp
, stride
);
684 if (mod
!= const0_rtx
)
685 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
),
692 fprintf (rtl_dump_file
, "; Number of iterations: ");
693 print_simple_rtl (rtl_dump_file
, exp
);
694 fprintf (rtl_dump_file
, "\n");
700 /* Return simplified RTX expression representing the value of test
701 described of DESC at given iteration of loop. */
704 test_for_iteration (struct loop_desc
*desc
, unsigned HOST_WIDE_INT iter
)
706 enum rtx_code cond
= desc
->cond
;
707 rtx exp
= XEXP (desc
->var_alts
, 0);
710 /* Give up on floating point modes and friends. It can be possible to do
711 the job for constant loop bounds, but it is probably not worthwhile. */
712 if (!INTEGRAL_MODE_P (GET_MODE (desc
->var
)))
715 /* Ensure that we always handle the condition to stay inside loop. */
717 cond
= reverse_condition (cond
);
719 /* Compute the value of induction variable. */
720 addval
= simplify_gen_binary (MULT
, GET_MODE (desc
->var
),
722 gen_int_mode (desc
->postincr
724 GET_MODE (desc
->var
)));
725 exp
= simplify_gen_binary (PLUS
, GET_MODE (desc
->var
), exp
, addval
);
726 /* Test at given condition. */
727 exp
= simplify_gen_relational (cond
, SImode
,
728 GET_MODE (desc
->var
), exp
, desc
->lim
);
732 fprintf (rtl_dump_file
, "; Conditional to continue loop at "
733 HOST_WIDE_INT_PRINT_UNSIGNED
"th iteration: ", iter
);
734 print_simple_rtl (rtl_dump_file
, exp
);
735 fprintf (rtl_dump_file
, "\n");
741 /* Tests whether exit at EXIT_EDGE from LOOP is simple. Returns simple loop
742 description joined to it in in DESC. INVARIANT_REGS and SINGLE_SET_REGS
743 are results of blocks_{invariant,single_set}_regs over BODY. */
745 simple_loop_exit_p (struct loops
*loops
, struct loop
*loop
, edge exit_edge
,
746 regset invariant_regs
, rtx
*single_set_regs
,
747 struct loop_desc
*desc
)
749 basic_block mod_bb
, exit_bb
;
754 exit_bb
= exit_edge
->src
;
756 fallthru_out
= (exit_edge
->flags
& EDGE_FALLTHRU
);
761 /* It must be tested (at least) once during any iteration. */
762 if (!dominated_by_p (loops
->cfg
.dom
, loop
->latch
, exit_bb
))
765 /* It must end in a simple conditional jump. */
766 if (!any_condjump_p (exit_bb
->end
))
773 desc
->out_edge
= exit_edge
;
776 /* Condition must be a simple comparison in that one of operands
777 is register and the other one is invariant. */
778 if (!(condition
= get_condition (exit_bb
->end
, NULL
)))
781 if (!simple_condition_p (loop
, condition
, invariant_regs
, desc
))
784 /* Var must be simply incremented or decremented in exactly one insn that
785 is executed just once every iteration. */
786 if (!(mod_bb
= simple_increment (loops
, loop
, single_set_regs
, desc
)))
789 /* OK, it is simple loop. Now just fill in remaining info. */
790 desc
->postincr
= !dominated_by_p (loops
->cfg
.dom
, exit_bb
, mod_bb
);
791 desc
->neg
= !fallthru_out
;
793 /* Find initial value of var and alternative values for lim. */
794 e
= loop_preheader_edge (loop
);
795 desc
->var_alts
= variable_initial_values (e
, desc
->var
);
796 desc
->lim_alts
= variable_initial_values (e
, desc
->lim
);
798 /* Number of iterations. */
800 constant_iterations (desc
, &desc
->niter
, &desc
->may_be_zero
);
801 if (!desc
->const_iter
&& !count_loop_iterations (desc
, NULL
, NULL
))
806 /* Tests whether LOOP is simple for loop. Returns simple loop description
809 simple_loop_p (struct loops
*loops
, struct loop
*loop
, struct loop_desc
*desc
)
814 struct loop_desc act
;
816 regset invariant_regs
;
817 regset_head invariant_regs_head
;
818 rtx
*single_set_regs
;
821 body
= get_loop_body (loop
);
823 invariant_regs
= INITIALIZE_REG_SET (invariant_regs_head
);
824 single_set_regs
= xmalloc (max_reg_num () * sizeof (rtx
));
826 blocks_invariant_registers (body
, loop
->num_nodes
, invariant_regs
);
827 blocks_single_set_registers (body
, loop
->num_nodes
, single_set_regs
);
830 for (i
= 0; i
< loop
->num_nodes
; i
++)
832 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
833 if (!flow_bb_inside_loop_p (loop
, e
->dest
)
834 && simple_loop_exit_p (loops
, loop
, e
,
835 invariant_regs
, single_set_regs
, &act
))
837 /* Prefer constant iterations; the less the better. */
840 else if (!act
.const_iter
841 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
846 if (body
[i
]->succ
&& body
[i
]->succ
->succ_next
)
849 desc
->n_branches
= n_branches
;
851 if (rtl_dump_file
&& any
)
853 fprintf (rtl_dump_file
, "; Simple loop %i\n", loop
->num
);
855 fprintf (rtl_dump_file
,
856 "; does postincrement after loop exit condition\n");
858 fprintf (rtl_dump_file
, "; Induction variable:");
859 print_simple_rtl (rtl_dump_file
, desc
->var
);
860 fputc ('\n', rtl_dump_file
);
862 fprintf (rtl_dump_file
, "; Initial values:");
863 print_simple_rtl (rtl_dump_file
, desc
->var_alts
);
864 fputc ('\n', rtl_dump_file
);
866 fprintf (rtl_dump_file
, "; Stride:");
867 print_simple_rtl (rtl_dump_file
, desc
->stride
);
868 fputc ('\n', rtl_dump_file
);
870 fprintf (rtl_dump_file
, "; Compared with:");
871 print_simple_rtl (rtl_dump_file
, desc
->lim
);
872 fputc ('\n', rtl_dump_file
);
874 fprintf (rtl_dump_file
, "; Alternative values:");
875 print_simple_rtl (rtl_dump_file
, desc
->lim_alts
);
876 fputc ('\n', rtl_dump_file
);
878 fprintf (rtl_dump_file
, "; Exit condition:");
880 fprintf (rtl_dump_file
, "(negated)");
881 fprintf (rtl_dump_file
, "%s\n", GET_RTX_NAME (desc
->cond
));
883 fprintf (rtl_dump_file
, "; Number of branches:");
884 fprintf (rtl_dump_file
, "%d\n", desc
->n_branches
);
886 fputc ('\n', rtl_dump_file
);
890 FREE_REG_SET (invariant_regs
);
891 free (single_set_regs
);
895 /* Marks blocks and edges that are part of non-recognized loops; i.e. we
896 throw away all latch edges and mark blocks inside any remaining cycle.
897 Everything is a bit complicated due to fact we do not want to do this
898 for parts of cycles that only "pass" through some loop -- i.e. for
899 each cycle, we want to mark blocks that belong directly to innermost
900 loop containing the whole cycle. */
902 mark_irreducible_loops (struct loops
*loops
)
904 int *dfs_in
, *closed
, *mr
, *mri
, *n_edges
, *stack
;
909 int stack_top
, tick
, depth
;
912 /* Reset the flags. */
913 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
915 act
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
916 for (e
= act
->succ
; e
; e
= e
->succ_next
)
917 e
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
920 /* The first last_basic_block + 1 entries are for real blocks (including
921 entry); then we have loops->num - 1 fake blocks for loops to that we
922 assign edges leading from loops (fake loop 0 is not interesting). */
923 dfs_in
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
924 closed
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
925 mr
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
926 mri
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
927 n_edges
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
928 edges
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (edge
*));
929 stack
= xmalloc ((n_basic_blocks
+ loops
->num
) * sizeof (int));
930 estack
= xmalloc ((n_basic_blocks
+ loops
->num
) * sizeof (edge
));
932 /* Create the edge lists. */
933 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
935 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
936 for (e
= act
->succ
; e
; e
= e
->succ_next
)
938 /* Ignore edges to exit. */
939 if (e
->dest
== EXIT_BLOCK_PTR
)
941 /* And latch edges. */
942 if (e
->dest
->loop_father
->header
== e
->dest
943 && e
->dest
->loop_father
->latch
== act
)
945 /* Edges inside a single loop should be left where they are. Edges
946 to subloop headers should lead to representative of the subloop,
947 but from the same place. */
948 if (act
->loop_father
== e
->dest
->loop_father
949 || act
->loop_father
== e
->dest
->loop_father
->outer
)
951 n_edges
[act
->index
+ 1]++;
954 /* Edges exiting loops remain. They should lead from representative
955 of the son of nearest common ancestor of the loops in that
957 depth
= find_common_loop (act
->loop_father
, e
->dest
->loop_father
)->depth
+ 1;
958 if (depth
== act
->loop_father
->depth
)
959 cloop
= act
->loop_father
;
961 cloop
= act
->loop_father
->pred
[depth
];
962 n_edges
[cloop
->num
+ last_basic_block
]++;
965 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
967 edges
[i
] = xmalloc (n_edges
[i
] * sizeof (edge
));
971 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
972 for (e
= act
->succ
; e
; e
= e
->succ_next
)
974 if (e
->dest
== EXIT_BLOCK_PTR
)
976 if (e
->dest
->loop_father
->header
== e
->dest
977 && e
->dest
->loop_father
->latch
== act
)
979 if (act
->loop_father
== e
->dest
->loop_father
980 || act
->loop_father
== e
->dest
->loop_father
->outer
)
982 edges
[act
->index
+ 1][n_edges
[act
->index
+ 1]++] = e
;
985 depth
= find_common_loop (act
->loop_father
, e
->dest
->loop_father
)->depth
+ 1;
986 if (depth
== act
->loop_father
->depth
)
987 cloop
= act
->loop_father
;
989 cloop
= act
->loop_father
->pred
[depth
];
990 i
= cloop
->num
+ last_basic_block
;
991 edges
[i
][n_edges
[i
]++] = e
;
994 /* Compute dfs numbering, starting from loop headers, and mark found
997 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
1001 mr
[i
] = last_basic_block
+ loops
->num
;
1006 for (i
= 0; i
< loops
->num
; i
++)
1007 if (loops
->parray
[i
])
1009 stack
[stack_top
] = loops
->parray
[i
]->header
->index
+ 1;
1010 estack
[stack_top
] = NULL
;
1018 idx
= stack
[stack_top
- 1];
1019 if (dfs_in
[idx
] < 0)
1020 dfs_in
[idx
] = tick
++;
1022 while (n_edges
[idx
])
1024 e
= edges
[idx
][--n_edges
[idx
]];
1025 sidx
= e
->dest
->loop_father
->header
== e
->dest
1026 ? e
->dest
->loop_father
->num
+ last_basic_block
1027 : e
->dest
->index
+ 1;
1030 if (!closed
[mri
[sidx
]])
1032 if (mr
[sidx
] < mr
[idx
])
1035 mri
[idx
] = mri
[sidx
];
1038 if (mr
[sidx
] <= dfs_in
[idx
])
1039 e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1043 if (dfs_in
[sidx
] < 0)
1045 stack
[stack_top
] = sidx
;
1046 estack
[stack_top
] = e
;
1050 if (dfs_in
[sidx
] < mr
[idx
])
1052 mr
[idx
] = dfs_in
[sidx
];
1055 e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1060 e
= estack
[stack_top
- 1];
1064 /* Propagate information back. */
1065 sidx
= stack
[stack_top
- 1];
1066 if (mr
[sidx
] > mr
[idx
])
1069 mri
[sidx
] = mri
[idx
];
1071 if (mr
[idx
] <= dfs_in
[sidx
])
1072 e
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
1074 /* Mark the block if relevant. */
1075 if (idx
&& idx
<= last_basic_block
&& mr
[idx
] <= dfs_in
[idx
])
1076 BASIC_BLOCK (idx
- 1)->flags
|= BB_IRREDUCIBLE_LOOP
;
1086 for (i
= 0; i
< last_basic_block
+ loops
->num
; i
++)
1090 loops
->state
|= LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
;
1093 /* Counts number of insns inside LOOP. */
1095 num_loop_insns (struct loop
*loop
)
1097 basic_block
*bbs
, bb
;
1098 unsigned i
, ninsns
= 0;
1101 bbs
= get_loop_body (loop
);
1102 for (i
= 0; i
< loop
->num_nodes
; i
++)
1106 for (insn
= bb
->head
; insn
!= bb
->end
; insn
= NEXT_INSN (insn
))
1115 /* Counts number of insns executed on average per iteration LOOP. */
1117 average_num_loop_insns (struct loop
*loop
)
1119 basic_block
*bbs
, bb
;
1120 unsigned i
, binsns
, ninsns
, ratio
;
1124 bbs
= get_loop_body (loop
);
1125 for (i
= 0; i
< loop
->num_nodes
; i
++)
1130 for (insn
= bb
->head
; insn
!= bb
->end
; insn
= NEXT_INSN (insn
))
1134 ratio
= loop
->header
->frequency
== 0
1136 : (bb
->frequency
* BB_FREQ_MAX
) / loop
->header
->frequency
;
1137 ninsns
+= binsns
* ratio
;
1141 ninsns
/= BB_FREQ_MAX
;
1143 ninsns
= 1; /* To avoid division by zero. */
1148 /* Returns expected number of LOOP iterations.
1149 Compute upper bound on number of iterations in case they do not fit integer
1150 to help loop peeling heuristics. Use exact counts if at all possible. */
1152 expected_loop_iterations (const struct loop
*loop
)
1156 if (loop
->header
->count
)
1158 gcov_type count_in
, count_latch
, expected
;
1163 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1164 if (e
->src
== loop
->latch
)
1165 count_latch
= e
->count
;
1167 count_in
+= e
->count
;
1172 expected
= (count_latch
+ count_in
- 1) / count_in
;
1174 /* Avoid overflows. */
1175 return (expected
> REG_BR_PROB_BASE
? REG_BR_PROB_BASE
: expected
);
1179 int freq_in
, freq_latch
;
1184 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
1185 if (e
->src
== loop
->latch
)
1186 freq_latch
= EDGE_FREQUENCY (e
);
1188 freq_in
+= EDGE_FREQUENCY (e
);
1193 return (freq_latch
+ freq_in
- 1) / freq_in
;