gcc/ChangeLog:
[official-gcc.git] / gcc / cfgloopanal.c
blobbe5b82effcf1193d150cc76734e925f7771d0c1d
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
9 version.
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
14 for more details.
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
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "cfgloop.h"
29 #include "expr.h"
30 #include "output.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 *,
41 bool *);
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,
47 struct loop_desc *);
48 static basic_block simple_increment (struct loops *, struct loop *, rtx *,
49 struct loop_desc *);
51 /* Checks whether BB is executed exactly once in each LOOP iteration. */
52 bool
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))
57 return false;
59 /* And just once. */
60 if (bb->loop_father != loop)
61 return false;
63 /* But this was not enough. We might have some irreducible loop here. */
64 if (bb->flags & BB_IRREDUCIBLE_LOOP)
65 return false;
67 return true;
71 /* Unmarks modified registers; helper to blocks_invariant_registers. */
72 static void
73 unmark_altered (rtx what, rtx by ATTRIBUTE_UNUSED, regset regs)
75 if (GET_CODE (what) == SUBREG)
76 what = SUBREG_REG (what);
77 if (!REG_P (what))
78 return;
79 CLEAR_REGNO_REG_SET (regs, REGNO (what));
82 /* Marks registers that are invariant inside blocks BBS. */
83 static void
84 blocks_invariant_registers (basic_block *bbs, int nbbs, regset regs)
86 rtx insn;
87 int i;
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))
95 if (INSN_P (insn))
96 note_stores (PATTERN (insn),
97 (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered,
98 regs);
101 /* Unmarks modified registers; helper to blocks_single_set_registers. */
102 struct unmark_altered_insn_data
104 rtx *regs;
105 rtx insn;
108 static void
109 unmark_altered_insn (rtx what, rtx by ATTRIBUTE_UNUSED,
110 struct unmark_altered_insn_data *data)
112 int rn;
114 if (GET_CODE (what) == SUBREG)
115 what = SUBREG_REG (what);
116 if (!REG_P (what))
117 return;
118 rn = REGNO (what);
119 if (data->regs[rn] == data->insn)
120 return;
121 data->regs[rn] = NULL;
124 /* Marks registers that have just single simple set in BBS; the relevant
125 insn is returned in REGS. */
126 static void
127 blocks_single_set_registers (basic_block *bbs, int nbbs, rtx *regs)
129 rtx insn;
130 int i;
131 struct unmark_altered_insn_data data;
133 for (i = 0; i < max_reg_num (); i++)
134 regs[i] = NULL;
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);
142 if (!set)
143 continue;
144 if (!REG_P (SET_DEST (set)))
145 continue;
146 regs[REGNO (SET_DEST (set))] = insn;
149 data.regs = regs;
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))
155 if (!INSN_P (insn))
156 continue;
157 data.insn = insn;
158 note_stores (PATTERN (insn),
159 (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered_insn,
160 &data);
164 /* Helper for invariant_rtx_wrto_regs_p. */
165 static int
166 invariant_rtx_wrto_regs_p_helper (rtx *expr, regset invariant_regs)
168 switch (GET_CODE (*expr))
170 case CC0:
171 case PC:
172 case UNSPEC_VOLATILE:
173 return 1;
175 case CONST_INT:
176 case CONST_DOUBLE:
177 case CONST:
178 case SYMBOL_REF:
179 case LABEL_REF:
180 return 0;
182 case ASM_OPERANDS:
183 return MEM_VOLATILE_P (*expr);
185 case MEM:
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);
190 case REG:
191 return !REGNO_REG_SET_P (invariant_regs, REGNO (*expr));
193 default:
194 return 0;
198 /* Checks that EXPR is invariant provided that INVARIANT_REGS are invariant. */
199 static bool
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,
203 invariant_regs);
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. */
209 static bool
210 simple_condition_p (struct loop *loop ATTRIBUTE_UNUSED, rtx condition,
211 regset invariant_regs, struct loop_desc *desc)
213 rtx op0, op1;
215 /* Check condition. */
216 switch (GET_CODE (condition))
218 case EQ:
219 case NE:
220 case LE:
221 case LT:
222 case GE:
223 case GT:
224 case GEU:
225 case GTU:
226 case LEU:
227 case LTU:
228 break;
229 default:
230 return false;
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)
236 return false;
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. */
246 if (!REG_P (op1))
247 return false;
248 desc->var = op1;
249 desc->lim = op0;
251 desc->cond = swap_condition (GET_CODE (condition));
252 if (desc->cond == UNKNOWN)
253 return false;
254 return true;
257 /* Check the other operand. */
258 if (!invariant_rtx_wrto_regs_p (op1, invariant_regs))
259 return false;
260 if (!REG_P (op0))
261 return false;
263 desc->var = op0;
264 desc->lim = op1;
266 desc->cond = GET_CODE (condition);
268 return true;
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
273 modified. */
274 static basic_block
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;
279 basic_block mod_bb;
281 /* Find insn that modifies var. */
282 mod_insn = simple_increment_regs[REGNO (desc->var)];
283 if (!mod_insn)
284 return NULL;
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))
289 return NULL;
291 /* mod_insn must be a simple increment/decrement. */
292 set = single_set (mod_insn);
293 if (!set)
294 abort ();
295 if (!rtx_equal_p (SET_DEST (set), desc->var))
296 abort ();
298 set_src = find_reg_equal_equiv_note (mod_insn);
299 if (!set_src)
300 set_src = SET_SRC (set);
301 if (GET_CODE (set_src) != PLUS)
302 return NULL;
303 if (!rtx_equal_p (XEXP (set_src, 0), desc->var))
304 return NULL;
306 /* Set desc->stride. */
307 set_add = XEXP (set_src, 1);
308 if (CONSTANT_P (set_add))
309 desc->stride = set_add;
310 else
311 return NULL;
313 return mod_bb;
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
318 placed here. */
319 static rtx
320 variable_initial_value (rtx insn, regset invariant_regs, rtx var, rtx *set_insn)
322 basic_block bb;
323 rtx set;
325 /* Go back through cfg. */
326 bb = BLOCK_FOR_INSN (insn);
327 while (1)
329 for (; insn != bb->head; insn = PREV_INSN (insn))
331 if (INSN_P (insn))
332 note_stores (PATTERN (insn),
333 (void (*) PARAMS ((rtx, rtx, void *))) unmark_altered,
334 invariant_regs);
335 if (modified_between_p (var, PREV_INSN (insn), NEXT_INSN (insn)))
336 break;
339 if (insn != bb->head)
341 /* We found place where var is set. */
342 rtx set_dest;
343 rtx val;
344 rtx note;
346 set = single_set (insn);
347 if (!set)
348 return NULL;
349 set_dest = SET_DEST (set);
350 if (!rtx_equal_p (set_dest, var))
351 return NULL;
353 note = find_reg_equal_equiv_note (insn);
354 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
355 val = XEXP (note, 0);
356 else
357 val = SET_SRC (set);
358 if (!invariant_rtx_wrto_regs_p (val, invariant_regs))
359 return NULL;
361 if (set_insn)
362 *set_insn = insn;
363 return val;
367 if (bb->pred->pred_next || bb->pred->src == ENTRY_BLOCK_PTR)
368 return NULL;
370 bb = bb->pred->src;
371 insn = bb->end;
374 return NULL;
377 /* Returns list of definitions of initial value of VAR at Edge. */
378 static rtx
379 variable_initial_values (edge e, rtx var)
381 rtx set_insn, list;
382 regset invariant_regs;
383 regset_head invariant_regs_head;
384 int i;
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)
393 return list;
395 set_insn = e->src->end;
396 while (REG_P (var)
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);
401 return list;
404 /* Counts constant number of iterations of the loop described by DESC;
405 returns false if impossible. */
406 static bool
407 constant_iterations (struct loop_desc *desc, unsigned HOST_WIDE_INT *niter,
408 bool *may_be_zero)
410 rtx test, expr;
411 rtx ainit, alim;
413 test = test_for_iteration (desc, 0);
414 if (test == const0_rtx)
416 *niter = 0;
417 *may_be_zero = false;
418 return true;
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)))
429 abort ();
430 if (GET_CODE (expr) == CONST_INT)
432 *niter = INTVAL (expr);
433 return true;
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))))
440 abort ();
441 if (GET_CODE (expr) == CONST_INT)
443 *niter = INTVAL (expr);
444 return true;
448 return false;
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;
468 rtx mod, exp;
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)))
473 return NULL;
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. */
479 if (desc->neg)
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
487 || cond == GTU)
488 return NULL;
489 exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
490 lim, init);
492 else
494 /* Bypass nonsensical tests. */
495 if (cond == EQ || cond == LE || cond == LT || cond == LEU
496 || cond == LTU)
497 return NULL;
498 exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
499 init, lim);
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. */
507 if (!desc->postincr)
508 exp = simplify_gen_binary (MINUS, GET_MODE (desc->var),
509 exp, stride);
511 /* Determine delta caused by exit condition. */
512 switch (cond)
514 case NE:
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)
521 != const0_rtx))
522 return NULL;
523 break;
524 case LT:
525 case GT:
526 case LTU:
527 case GTU:
528 break;
529 case LE:
530 case GE:
531 case LEU:
532 case GEU:
533 exp = simplify_gen_binary (PLUS, GET_MODE (desc->var),
534 exp, const1_rtx);
535 break;
536 default:
537 abort ();
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),
557 exp, stridem1);
558 exp = simplify_gen_binary (UDIV, GET_MODE (desc->var), exp, stride);
560 else
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),
565 exp, const1_rtx);
569 if (rtl_dump_file)
571 fprintf (rtl_dump_file, "; Number of iterations: ");
572 print_simple_rtl (rtl_dump_file, exp);
573 fprintf (rtl_dump_file, "\n");
576 return exp;
579 /* Return simplified RTX expression representing the value of test
580 described of DESC at given iteration of loop. */
582 static rtx
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);
587 rtx addval;
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)))
592 return NULL;
594 /* Ensure that we always handle the condition to stay inside loop. */
595 if (desc->neg)
596 cond = reverse_condition (cond);
598 /* Compute the value of induction variable. */
599 addval = simplify_gen_binary (MULT, GET_MODE (desc->var),
600 desc->stride,
601 gen_int_mode (desc->postincr
602 ? iter : iter + 1,
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);
609 if (rtl_dump_file)
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");
616 return exp;
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. */
623 static bool
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;
629 int fallthru_out;
630 rtx condition;
631 edge ei, e;
633 exit_bb = exit_edge->src;
635 fallthru_out = (exit_edge->flags & EDGE_FALLTHRU);
637 if (!exit_bb)
638 return false;
640 /* It must be tested (at least) once during any iteration. */
641 if (!dominated_by_p (loops->cfg.dom, loop->latch, exit_bb))
642 return false;
644 /* It must end in a simple conditional jump. */
645 if (!any_condjump_p (exit_bb->end))
646 return false;
648 ei = exit_bb->succ;
649 if (ei == exit_edge)
650 ei = ei->succ_next;
652 desc->out_edge = exit_edge;
653 desc->in_edge = ei;
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)))
658 return false;
660 if (!simple_condition_p (loop, condition, invariant_regs, desc))
661 return false;
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)))
666 return false;
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))
679 return false;
680 desc->const_iter =
681 constant_iterations (desc, &desc->niter, &desc->may_be_zero);
682 return true;
685 /* Tests whether LOOP is simple for loop. Returns simple loop description
686 in DESC. */
687 bool
688 simple_loop_p (struct loops *loops, struct loop *loop, struct loop_desc *desc)
690 unsigned i;
691 basic_block *body;
692 edge e;
693 struct loop_desc act;
694 bool any = false;
695 regset invariant_regs;
696 regset_head invariant_regs_head;
697 rtx *single_set_regs;
698 int n_branches;
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);
708 n_branches = 0;
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. */
717 if (!any)
718 any = true;
719 else if (!act.const_iter
720 || (desc->const_iter && act.niter >= desc->niter))
721 continue;
722 *desc = act;
725 if (body[i]->succ && body[i]->succ->succ_next)
726 n_branches++;
728 desc->n_branches = n_branches;
730 if (rtl_dump_file && any)
732 fprintf (rtl_dump_file, "; Simple loop %i\n", loop->num);
733 if (desc->postincr)
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:");
758 if (desc->neg)
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);
768 free (body);
769 FREE_REG_SET (invariant_regs);
770 free (single_set_regs);
771 return any;
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. */
780 void
781 mark_irreducible_loops (struct loops *loops)
783 int *dfs_in, *closed, *mr, *mri, *n_edges, *stack;
784 unsigned i;
785 edge **edges, e;
786 edge *estack;
787 basic_block act;
788 int stack_top, tick, depth;
789 struct loop *cloop;
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++)
813 n_edges[i] = 0;
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)
819 continue;
820 /* And latch edges. */
821 if (e->dest->loop_father->header == e->dest
822 && e->dest->loop_father->latch == act)
823 continue;
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]++;
831 continue;
833 /* Edges exiting loops remain. They should lead from representative
834 of the son of nearest common ancestor of the loops in that
835 act lays. */
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;
839 else
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));
847 n_edges[i] = 0;
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)
854 continue;
855 if (e->dest->loop_father->header == e->dest
856 && e->dest->loop_father->latch == act)
857 continue;
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;
862 continue;
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;
867 else
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
874 loops.*/
875 tick = 0;
876 for (i = 0; i < last_basic_block + loops->num; i++)
878 dfs_in[i] = -1;
879 closed[i] = 0;
880 mr[i] = last_basic_block + loops->num;
881 mri[i] = -1;
884 stack_top = 0;
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;
890 stack_top++;
893 while (stack_top)
895 int idx, sidx;
897 idx = stack[stack_top - 1];
898 if (dfs_in[idx] < 0)
899 dfs_in[idx] = tick++;
901 while (n_edges[idx])
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;
907 if (closed[sidx])
909 if (!closed[mri[sidx]])
911 if (mr[sidx] < mr[idx])
913 mr[idx] = mr[sidx];
914 mri[idx] = mri[sidx];
917 if (mr[sidx] <= dfs_in[idx])
918 e->flags |= EDGE_IRREDUCIBLE_LOOP;
920 continue;
922 if (dfs_in[sidx] < 0)
924 stack[stack_top] = sidx;
925 estack[stack_top] = e;
926 stack_top++;
927 goto next;
929 if (dfs_in[sidx] < mr[idx])
931 mr[idx] = dfs_in[sidx];
932 mri[idx] = sidx;
934 e->flags |= EDGE_IRREDUCIBLE_LOOP;
937 /* Return back. */
938 closed[idx] = 1;
939 e = estack[stack_top - 1];
940 stack_top--;
941 if (e)
943 /* Propagate information back. */
944 sidx = stack[stack_top - 1];
945 if (mr[sidx] > mr[idx])
947 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;
956 next:;
959 free (stack);
960 free (estack);
961 free (dfs_in);
962 free (closed);
963 free (mr);
964 free (mri);
965 for (i = 0; i < last_basic_block + loops->num; i++)
966 free (edges[i]);
967 free (edges);
968 free (n_edges);
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;
978 rtx insn;
980 bbs = get_loop_body (loop);
981 for (i = 0; i < loop->num_nodes; i++)
983 bb = bbs[i];
984 ninsns++;
985 for (insn = bb->head; insn != bb->end; insn = NEXT_INSN (insn))
986 if (INSN_P (insn))
987 ninsns++;
989 free(bbs);
991 return ninsns;
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;
1000 rtx insn;
1002 ninsns = 0;
1003 bbs = get_loop_body (loop);
1004 for (i = 0; i < loop->num_nodes; i++)
1006 bb = bbs[i];
1008 binsns = 1;
1009 for (insn = bb->head; insn != bb->end; insn = NEXT_INSN (insn))
1010 if (INSN_P (insn))
1011 binsns++;
1013 ratio = loop->header->frequency == 0
1014 ? BB_FREQ_MAX
1015 : (bb->frequency * BB_FREQ_MAX) / loop->header->frequency;
1016 ninsns += binsns * ratio;
1018 free(bbs);
1020 ninsns /= BB_FREQ_MAX;
1021 if (!ninsns)
1022 ninsns = 1; /* To avoid division by zero. */
1024 return ninsns;
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. */
1030 unsigned
1031 expected_loop_iterations (const struct loop *loop)
1033 edge e;
1035 if (loop->header->count)
1037 gcov_type count_in, count_latch, expected;
1039 count_in = 0;
1040 count_latch = 0;
1042 for (e = loop->header->pred; e; e = e->pred_next)
1043 if (e->src == loop->latch)
1044 count_latch = e->count;
1045 else
1046 count_in += e->count;
1048 if (count_in == 0)
1049 return 0;
1051 expected = (count_latch + count_in - 1) / count_in;
1053 /* Avoid overflows. */
1054 return (expected > REG_BR_PROB_BASE ? REG_BR_PROB_BASE : expected);
1056 else
1058 int freq_in, freq_latch;
1060 freq_in = 0;
1061 freq_latch = 0;
1063 for (e = loop->header->pred; e; e = e->pred_next)
1064 if (e->src == loop->latch)
1065 freq_latch = EDGE_FREQUENCY (e);
1066 else
1067 freq_in += EDGE_FREQUENCY (e);
1069 if (freq_in == 0)
1070 return 0;
1072 return (freq_latch + freq_in - 1) / freq_in;