crontab: Don't build snapshot for 3.4.x anymore.
[official-gcc.git] / gcc / cfgloop.c
blob7c06a3af1fee4170a11a2d6e3643098dcc3aa866
1 /* Natural loop discovery code for GNU compiler.
2 Copyright (C) 2000, 2001, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, 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 "obstack.h"
28 #include "function.h"
29 #include "basic-block.h"
30 #include "toplev.h"
31 #include "cfgloop.h"
32 #include "flags.h"
33 #include "tree.h"
34 #include "tree-flow.h"
36 /* Ratio of frequencies of edges so that one of more latch edges is
37 considered to belong to inner loop with same header. */
38 #define HEAVY_EDGE_RATIO 8
40 #define HEADER_BLOCK(B) (* (int *) (B)->aux)
41 #define LATCH_EDGE(E) (*(int *) (E)->aux)
43 static void flow_loops_cfg_dump (const struct loops *, FILE *);
44 static int flow_loop_level_compute (struct loop *);
45 static void flow_loops_level_compute (struct loops *);
46 static void establish_preds (struct loop *);
47 static void canonicalize_loop_headers (void);
48 static bool glb_enum_p (basic_block, void *);
50 /* Dump loop related CFG information. */
52 static void
53 flow_loops_cfg_dump (const struct loops *loops, FILE *file)
55 int i;
56 basic_block bb;
58 if (! loops->num || ! file)
59 return;
61 FOR_EACH_BB (bb)
63 edge succ;
64 edge_iterator ei;
66 fprintf (file, ";; %d succs { ", bb->index);
67 FOR_EACH_EDGE (succ, ei, bb->succs)
68 fprintf (file, "%d ", succ->dest->index);
69 fprintf (file, "}\n");
72 /* Dump the DFS node order. */
73 if (loops->cfg.dfs_order)
75 fputs (";; DFS order: ", file);
76 for (i = NUM_FIXED_BLOCKS; i < n_basic_blocks; i++)
77 fprintf (file, "%d ", loops->cfg.dfs_order[i]);
79 fputs ("\n", file);
82 /* Dump the reverse completion node order. */
83 if (loops->cfg.rc_order)
85 fputs (";; RC order: ", file);
86 for (i = NUM_FIXED_BLOCKS; i < n_basic_blocks; i++)
87 fprintf (file, "%d ", loops->cfg.rc_order[i]);
89 fputs ("\n", file);
93 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
95 bool
96 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
98 return (loop->depth > outer->depth
99 && loop->pred[outer->depth] == outer);
102 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
103 loops within LOOP. */
105 struct loop *
106 superloop_at_depth (struct loop *loop, unsigned depth)
108 gcc_assert (depth <= (unsigned) loop->depth);
110 if (depth == (unsigned) loop->depth)
111 return loop;
113 return loop->pred[depth];
116 /* Dump the loop information specified by LOOP to the stream FILE
117 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
119 void
120 flow_loop_dump (const struct loop *loop, FILE *file,
121 void (*loop_dump_aux) (const struct loop *, FILE *, int),
122 int verbose)
124 basic_block *bbs;
125 unsigned i;
127 if (! loop || ! loop->header)
128 return;
130 fprintf (file, ";;\n;; Loop %d:%s\n", loop->num,
131 loop->invalid ? " invalid" : "");
133 fprintf (file, ";; header %d, latch %d\n",
134 loop->header->index, loop->latch->index);
135 fprintf (file, ";; depth %d, level %d, outer %ld\n",
136 loop->depth, loop->level,
137 (long) (loop->outer ? loop->outer->num : -1));
139 fprintf (file, ";; nodes:");
140 bbs = get_loop_body (loop);
141 for (i = 0; i < loop->num_nodes; i++)
142 fprintf (file, " %d", bbs[i]->index);
143 free (bbs);
144 fprintf (file, "\n");
146 if (loop_dump_aux)
147 loop_dump_aux (loop, file, verbose);
150 /* Dump the loop information specified by LOOPS to the stream FILE,
151 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
153 void
154 flow_loops_dump (const struct loops *loops, FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
156 int i;
157 int num_loops;
159 num_loops = loops->num;
160 if (! num_loops || ! file)
161 return;
163 fprintf (file, ";; %d loops found\n", num_loops);
165 for (i = 0; i < num_loops; i++)
167 struct loop *loop = loops->parray[i];
169 if (!loop)
170 continue;
172 flow_loop_dump (loop, file, loop_dump_aux, verbose);
175 if (verbose)
176 flow_loops_cfg_dump (loops, file);
179 /* Free data allocated for LOOP. */
180 void
181 flow_loop_free (struct loop *loop)
183 if (loop->pred)
184 free (loop->pred);
185 free (loop);
188 /* Free all the memory allocated for LOOPS. */
190 void
191 flow_loops_free (struct loops *loops)
193 if (loops->parray)
195 unsigned i;
197 gcc_assert (loops->num);
199 /* Free the loop descriptors. */
200 for (i = 0; i < loops->num; i++)
202 struct loop *loop = loops->parray[i];
204 if (!loop)
205 continue;
207 flow_loop_free (loop);
210 free (loops->parray);
211 loops->parray = NULL;
213 if (loops->cfg.dfs_order)
214 free (loops->cfg.dfs_order);
215 if (loops->cfg.rc_order)
216 free (loops->cfg.rc_order);
221 /* Find the nodes contained within the LOOP with header HEADER.
222 Return the number of nodes within the loop. */
225 flow_loop_nodes_find (basic_block header, struct loop *loop)
227 basic_block *stack;
228 int sp;
229 int num_nodes = 1;
231 header->loop_father = loop;
232 header->loop_depth = loop->depth;
234 if (loop->latch->loop_father != loop)
236 stack = XNEWVEC (basic_block, n_basic_blocks);
237 sp = 0;
238 num_nodes++;
239 stack[sp++] = loop->latch;
240 loop->latch->loop_father = loop;
241 loop->latch->loop_depth = loop->depth;
243 while (sp)
245 basic_block node;
246 edge e;
247 edge_iterator ei;
249 node = stack[--sp];
251 FOR_EACH_EDGE (e, ei, node->preds)
253 basic_block ancestor = e->src;
255 if (ancestor != ENTRY_BLOCK_PTR
256 && ancestor->loop_father != loop)
258 ancestor->loop_father = loop;
259 ancestor->loop_depth = loop->depth;
260 num_nodes++;
261 stack[sp++] = ancestor;
265 free (stack);
267 return num_nodes;
270 /* For each loop in the lOOPS tree that has just a single exit
271 record the exit edge. */
273 void
274 mark_single_exit_loops (struct loops *loops)
276 basic_block bb;
277 edge e;
278 struct loop *loop;
279 unsigned i;
281 for (i = 1; i < loops->num; i++)
283 loop = loops->parray[i];
284 if (loop)
285 loop->single_exit = NULL;
288 FOR_EACH_BB (bb)
290 edge_iterator ei;
291 if (bb->loop_father == loops->tree_root)
292 continue;
293 FOR_EACH_EDGE (e, ei, bb->succs)
295 if (e->dest == EXIT_BLOCK_PTR)
296 continue;
298 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
299 continue;
301 for (loop = bb->loop_father;
302 loop != e->dest->loop_father;
303 loop = loop->outer)
305 /* If we have already seen an exit, mark this by the edge that
306 surely does not occur as any exit. */
307 if (loop->single_exit)
308 loop->single_exit = single_succ_edge (ENTRY_BLOCK_PTR);
309 else
310 loop->single_exit = e;
315 for (i = 1; i < loops->num; i++)
317 loop = loops->parray[i];
318 if (!loop)
319 continue;
321 if (loop->single_exit == single_succ_edge (ENTRY_BLOCK_PTR))
322 loop->single_exit = NULL;
325 loops->state |= LOOPS_HAVE_MARKED_SINGLE_EXITS;
328 static void
329 establish_preds (struct loop *loop)
331 struct loop *ploop, *father = loop->outer;
333 loop->depth = father->depth + 1;
335 /* Remember the current loop depth if it is the largest seen so far. */
336 cfun->max_loop_depth = MAX (cfun->max_loop_depth, loop->depth);
338 if (loop->pred)
339 free (loop->pred);
340 loop->pred = XNEWVEC (struct loop *, loop->depth);
341 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
342 loop->pred[father->depth] = father;
344 for (ploop = loop->inner; ploop; ploop = ploop->next)
345 establish_preds (ploop);
348 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
349 added loop. If LOOP has some children, take care of that their
350 pred field will be initialized correctly. */
352 void
353 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
355 loop->next = father->inner;
356 father->inner = loop;
357 loop->outer = father;
359 establish_preds (loop);
362 /* Remove LOOP from the loop hierarchy tree. */
364 void
365 flow_loop_tree_node_remove (struct loop *loop)
367 struct loop *prev, *father;
369 father = loop->outer;
370 loop->outer = NULL;
372 /* Remove loop from the list of sons. */
373 if (father->inner == loop)
374 father->inner = loop->next;
375 else
377 for (prev = father->inner; prev->next != loop; prev = prev->next);
378 prev->next = loop->next;
381 loop->depth = -1;
382 free (loop->pred);
383 loop->pred = NULL;
386 /* Helper function to compute loop nesting depth and enclosed loop level
387 for the natural loop specified by LOOP. Returns the loop level. */
389 static int
390 flow_loop_level_compute (struct loop *loop)
392 struct loop *inner;
393 int level = 1;
395 if (! loop)
396 return 0;
398 /* Traverse loop tree assigning depth and computing level as the
399 maximum level of all the inner loops of this loop. The loop
400 level is equivalent to the height of the loop in the loop tree
401 and corresponds to the number of enclosed loop levels (including
402 itself). */
403 for (inner = loop->inner; inner; inner = inner->next)
405 int ilevel = flow_loop_level_compute (inner) + 1;
407 if (ilevel > level)
408 level = ilevel;
411 loop->level = level;
412 return level;
415 /* Compute the loop nesting depth and enclosed loop level for the loop
416 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
417 level. */
419 static void
420 flow_loops_level_compute (struct loops *loops)
422 flow_loop_level_compute (loops->tree_root);
425 /* A callback to update latch and header info for basic block JUMP created
426 by redirecting an edge. */
428 static void
429 update_latch_info (basic_block jump)
431 alloc_aux_for_block (jump, sizeof (int));
432 HEADER_BLOCK (jump) = 0;
433 alloc_aux_for_edge (single_pred_edge (jump), sizeof (int));
434 LATCH_EDGE (single_pred_edge (jump)) = 0;
435 set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
438 /* A callback for make_forwarder block, to redirect all edges except for
439 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
440 whether to redirect it. */
442 static edge mfb_kj_edge;
443 static bool
444 mfb_keep_just (edge e)
446 return e != mfb_kj_edge;
449 /* A callback for make_forwarder block, to redirect the latch edges into an
450 entry part. E is the edge for that we should decide whether to redirect
451 it. */
453 static bool
454 mfb_keep_nonlatch (edge e)
456 return LATCH_EDGE (e);
459 /* Takes care of merging natural loops with shared headers. */
461 static void
462 canonicalize_loop_headers (void)
464 basic_block header;
465 edge e;
467 alloc_aux_for_blocks (sizeof (int));
468 alloc_aux_for_edges (sizeof (int));
470 /* Split blocks so that each loop has only single latch. */
471 FOR_EACH_BB (header)
473 edge_iterator ei;
474 int num_latches = 0;
475 int have_abnormal_edge = 0;
477 FOR_EACH_EDGE (e, ei, header->preds)
479 basic_block latch = e->src;
481 if (e->flags & EDGE_ABNORMAL)
482 have_abnormal_edge = 1;
484 if (latch != ENTRY_BLOCK_PTR
485 && dominated_by_p (CDI_DOMINATORS, latch, header))
487 num_latches++;
488 LATCH_EDGE (e) = 1;
491 if (have_abnormal_edge)
492 HEADER_BLOCK (header) = 0;
493 else
494 HEADER_BLOCK (header) = num_latches;
497 if (HEADER_BLOCK (single_succ (ENTRY_BLOCK_PTR)))
499 basic_block bb;
501 /* We could not redirect edges freely here. On the other hand,
502 we can simply split the edge from entry block. */
503 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
505 alloc_aux_for_edge (single_succ_edge (bb), sizeof (int));
506 LATCH_EDGE (single_succ_edge (bb)) = 0;
507 alloc_aux_for_block (bb, sizeof (int));
508 HEADER_BLOCK (bb) = 0;
511 FOR_EACH_BB (header)
513 int max_freq, is_heavy;
514 edge heavy, tmp_edge;
515 edge_iterator ei;
517 if (HEADER_BLOCK (header) <= 1)
518 continue;
520 /* Find a heavy edge. */
521 is_heavy = 1;
522 heavy = NULL;
523 max_freq = 0;
524 FOR_EACH_EDGE (e, ei, header->preds)
525 if (LATCH_EDGE (e) &&
526 EDGE_FREQUENCY (e) > max_freq)
527 max_freq = EDGE_FREQUENCY (e);
528 FOR_EACH_EDGE (e, ei, header->preds)
529 if (LATCH_EDGE (e) &&
530 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
532 if (heavy)
534 is_heavy = 0;
535 break;
537 else
538 heavy = e;
541 if (is_heavy)
543 /* Split out the heavy edge, and create inner loop for it. */
544 mfb_kj_edge = heavy;
545 tmp_edge = make_forwarder_block (header, mfb_keep_just,
546 update_latch_info);
547 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
548 HEADER_BLOCK (tmp_edge->dest) = 1;
549 alloc_aux_for_edge (tmp_edge, sizeof (int));
550 LATCH_EDGE (tmp_edge) = 0;
551 HEADER_BLOCK (header)--;
554 if (HEADER_BLOCK (header) > 1)
556 /* Create a new latch block. */
557 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
558 update_latch_info);
559 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
560 HEADER_BLOCK (tmp_edge->src) = 0;
561 HEADER_BLOCK (tmp_edge->dest) = 1;
562 alloc_aux_for_edge (tmp_edge, sizeof (int));
563 LATCH_EDGE (tmp_edge) = 1;
567 free_aux_for_blocks ();
568 free_aux_for_edges ();
570 #ifdef ENABLE_CHECKING
571 verify_dominators (CDI_DOMINATORS);
572 #endif
575 /* Initialize all the parallel_p fields of the loops structure to true. */
577 static void
578 initialize_loops_parallel_p (struct loops *loops)
580 unsigned int i;
582 for (i = 0; i < loops->num; i++)
584 struct loop *loop = loops->parray[i];
585 loop->parallel_p = true;
589 /* Find all the natural loops in the function and save in LOOPS structure and
590 recalculate loop_depth information in basic block structures.
591 Return the number of natural loops found. */
594 flow_loops_find (struct loops *loops)
596 int b;
597 int num_loops;
598 edge e;
599 sbitmap headers;
600 int *dfs_order;
601 int *rc_order;
602 basic_block header;
603 basic_block bb;
605 memset (loops, 0, sizeof *loops);
607 /* We are going to recount the maximum loop depth,
608 so throw away the last count. */
609 cfun->max_loop_depth = 0;
611 /* Taking care of this degenerate case makes the rest of
612 this code simpler. */
613 if (n_basic_blocks == NUM_FIXED_BLOCKS)
614 return 0;
616 dfs_order = NULL;
617 rc_order = NULL;
619 /* Ensure that the dominators are computed. */
620 calculate_dominance_info (CDI_DOMINATORS);
622 /* Join loops with shared headers. */
623 canonicalize_loop_headers ();
625 /* Count the number of loop headers. This should be the
626 same as the number of natural loops. */
627 headers = sbitmap_alloc (last_basic_block);
628 sbitmap_zero (headers);
630 num_loops = 0;
631 FOR_EACH_BB (header)
633 edge_iterator ei;
634 int more_latches = 0;
636 header->loop_depth = 0;
638 /* If we have an abnormal predecessor, do not consider the
639 loop (not worth the problems). */
640 FOR_EACH_EDGE (e, ei, header->preds)
641 if (e->flags & EDGE_ABNORMAL)
642 break;
643 if (e)
644 continue;
646 FOR_EACH_EDGE (e, ei, header->preds)
648 basic_block latch = e->src;
650 gcc_assert (!(e->flags & EDGE_ABNORMAL));
652 /* Look for back edges where a predecessor is dominated
653 by this block. A natural loop has a single entry
654 node (header) that dominates all the nodes in the
655 loop. It also has single back edge to the header
656 from a latch node. */
657 if (latch != ENTRY_BLOCK_PTR
658 && dominated_by_p (CDI_DOMINATORS, latch, header))
660 /* Shared headers should be eliminated by now. */
661 gcc_assert (!more_latches);
662 more_latches = 1;
663 SET_BIT (headers, header->index);
664 num_loops++;
669 /* Allocate loop structures. */
670 loops->parray = XCNEWVEC (struct loop *, num_loops + 1);
672 /* Dummy loop containing whole function. */
673 loops->parray[0] = XCNEW (struct loop);
674 loops->parray[0]->next = NULL;
675 loops->parray[0]->inner = NULL;
676 loops->parray[0]->outer = NULL;
677 loops->parray[0]->depth = 0;
678 loops->parray[0]->pred = NULL;
679 loops->parray[0]->num_nodes = n_basic_blocks;
680 loops->parray[0]->latch = EXIT_BLOCK_PTR;
681 loops->parray[0]->header = ENTRY_BLOCK_PTR;
682 ENTRY_BLOCK_PTR->loop_father = loops->parray[0];
683 EXIT_BLOCK_PTR->loop_father = loops->parray[0];
685 loops->tree_root = loops->parray[0];
687 /* Find and record information about all the natural loops
688 in the CFG. */
689 loops->num = 1;
690 FOR_EACH_BB (bb)
691 bb->loop_father = loops->tree_root;
693 if (num_loops)
695 /* Compute depth first search order of the CFG so that outer
696 natural loops will be found before inner natural loops. */
697 dfs_order = XNEWVEC (int, n_basic_blocks);
698 rc_order = XNEWVEC (int, n_basic_blocks);
699 pre_and_rev_post_order_compute (dfs_order, rc_order, false);
701 /* Save CFG derived information to avoid recomputing it. */
702 loops->cfg.dfs_order = dfs_order;
703 loops->cfg.rc_order = rc_order;
705 num_loops = 1;
707 for (b = 0; b < n_basic_blocks - NUM_FIXED_BLOCKS; b++)
709 struct loop *loop;
710 edge_iterator ei;
712 /* Search the nodes of the CFG in reverse completion order
713 so that we can find outer loops first. */
714 if (!TEST_BIT (headers, rc_order[b]))
715 continue;
717 header = BASIC_BLOCK (rc_order[b]);
719 loop = loops->parray[num_loops] = XCNEW (struct loop);
721 loop->header = header;
722 loop->num = num_loops;
723 num_loops++;
725 /* Look for the latch for this header block. */
726 FOR_EACH_EDGE (e, ei, header->preds)
728 basic_block latch = e->src;
730 if (latch != ENTRY_BLOCK_PTR
731 && dominated_by_p (CDI_DOMINATORS, latch, header))
733 loop->latch = latch;
734 break;
738 flow_loop_tree_node_add (header->loop_father, loop);
739 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
742 /* Assign the loop nesting depth and enclosed loop level for each
743 loop. */
744 flow_loops_level_compute (loops);
746 loops->num = num_loops;
747 initialize_loops_parallel_p (loops);
750 sbitmap_free (headers);
752 loops->state = 0;
753 #ifdef ENABLE_CHECKING
754 verify_flow_info ();
755 verify_loop_structure (loops);
756 #endif
758 return loops->num;
761 /* Return nonzero if basic block BB belongs to LOOP. */
762 bool
763 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
765 struct loop *source_loop;
767 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
768 return 0;
770 source_loop = bb->loop_father;
771 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
774 /* Enumeration predicate for get_loop_body. */
775 static bool
776 glb_enum_p (basic_block bb, void *glb_header)
778 return bb != (basic_block) glb_header;
781 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
782 order against direction of edges from latch. Specially, if
783 header != latch, latch is the 1-st block. */
784 basic_block *
785 get_loop_body (const struct loop *loop)
787 basic_block *tovisit, bb;
788 unsigned tv = 0;
790 gcc_assert (loop->num_nodes);
792 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
793 tovisit[tv++] = loop->header;
795 if (loop->latch == EXIT_BLOCK_PTR)
797 /* There may be blocks unreachable from EXIT_BLOCK. */
798 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks);
799 FOR_EACH_BB (bb)
800 tovisit[tv++] = bb;
801 tovisit[tv++] = EXIT_BLOCK_PTR;
803 else if (loop->latch != loop->header)
805 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
806 tovisit + 1, loop->num_nodes - 1,
807 loop->header) + 1;
810 gcc_assert (tv == loop->num_nodes);
811 return tovisit;
814 /* Fills dominance descendants inside LOOP of the basic block BB into
815 array TOVISIT from index *TV. */
817 static void
818 fill_sons_in_loop (const struct loop *loop, basic_block bb,
819 basic_block *tovisit, int *tv)
821 basic_block son, postpone = NULL;
823 tovisit[(*tv)++] = bb;
824 for (son = first_dom_son (CDI_DOMINATORS, bb);
825 son;
826 son = next_dom_son (CDI_DOMINATORS, son))
828 if (!flow_bb_inside_loop_p (loop, son))
829 continue;
831 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
833 postpone = son;
834 continue;
836 fill_sons_in_loop (loop, son, tovisit, tv);
839 if (postpone)
840 fill_sons_in_loop (loop, postpone, tovisit, tv);
843 /* Gets body of a LOOP (that must be different from the outermost loop)
844 sorted by dominance relation. Additionally, if a basic block s dominates
845 the latch, then only blocks dominated by s are be after it. */
847 basic_block *
848 get_loop_body_in_dom_order (const struct loop *loop)
850 basic_block *tovisit;
851 int tv;
853 gcc_assert (loop->num_nodes);
855 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
857 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
859 tv = 0;
860 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
862 gcc_assert (tv == (int) loop->num_nodes);
864 return tovisit;
867 /* Get body of a LOOP in breadth first sort order. */
869 basic_block *
870 get_loop_body_in_bfs_order (const struct loop *loop)
872 basic_block *blocks;
873 basic_block bb;
874 bitmap visited;
875 unsigned int i = 0;
876 unsigned int vc = 1;
878 gcc_assert (loop->num_nodes);
879 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
881 blocks = XCNEWVEC (basic_block, loop->num_nodes);
882 visited = BITMAP_ALLOC (NULL);
884 bb = loop->header;
885 while (i < loop->num_nodes)
887 edge e;
888 edge_iterator ei;
890 if (!bitmap_bit_p (visited, bb->index))
892 /* This basic block is now visited */
893 bitmap_set_bit (visited, bb->index);
894 blocks[i++] = bb;
897 FOR_EACH_EDGE (e, ei, bb->succs)
899 if (flow_bb_inside_loop_p (loop, e->dest))
901 if (!bitmap_bit_p (visited, e->dest->index))
903 bitmap_set_bit (visited, e->dest->index);
904 blocks[i++] = e->dest;
909 gcc_assert (i >= vc);
911 bb = blocks[vc++];
914 BITMAP_FREE (visited);
915 return blocks;
918 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
919 edge *
920 get_loop_exit_edges (const struct loop *loop, unsigned int *num_edges)
922 edge *edges, e;
923 unsigned i, n;
924 basic_block * body;
925 edge_iterator ei;
927 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
929 body = get_loop_body (loop);
930 n = 0;
931 for (i = 0; i < loop->num_nodes; i++)
932 FOR_EACH_EDGE (e, ei, body[i]->succs)
933 if (!flow_bb_inside_loop_p (loop, e->dest))
934 n++;
935 edges = XNEWVEC (edge, n);
936 *num_edges = n;
937 n = 0;
938 for (i = 0; i < loop->num_nodes; i++)
939 FOR_EACH_EDGE (e, ei, body[i]->succs)
940 if (!flow_bb_inside_loop_p (loop, e->dest))
941 edges[n++] = e;
942 free (body);
944 return edges;
947 /* Counts the number of conditional branches inside LOOP. */
949 unsigned
950 num_loop_branches (const struct loop *loop)
952 unsigned i, n;
953 basic_block * body;
955 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
957 body = get_loop_body (loop);
958 n = 0;
959 for (i = 0; i < loop->num_nodes; i++)
960 if (EDGE_COUNT (body[i]->succs) >= 2)
961 n++;
962 free (body);
964 return n;
967 /* Adds basic block BB to LOOP. */
968 void
969 add_bb_to_loop (basic_block bb, struct loop *loop)
971 int i;
973 bb->loop_father = loop;
974 bb->loop_depth = loop->depth;
975 loop->num_nodes++;
976 for (i = 0; i < loop->depth; i++)
977 loop->pred[i]->num_nodes++;
980 /* Remove basic block BB from loops. */
981 void
982 remove_bb_from_loops (basic_block bb)
984 int i;
985 struct loop *loop = bb->loop_father;
987 loop->num_nodes--;
988 for (i = 0; i < loop->depth; i++)
989 loop->pred[i]->num_nodes--;
990 bb->loop_father = NULL;
991 bb->loop_depth = 0;
994 /* Finds nearest common ancestor in loop tree for given loops. */
995 struct loop *
996 find_common_loop (struct loop *loop_s, struct loop *loop_d)
998 if (!loop_s) return loop_d;
999 if (!loop_d) return loop_s;
1001 if (loop_s->depth < loop_d->depth)
1002 loop_d = loop_d->pred[loop_s->depth];
1003 else if (loop_s->depth > loop_d->depth)
1004 loop_s = loop_s->pred[loop_d->depth];
1006 while (loop_s != loop_d)
1008 loop_s = loop_s->outer;
1009 loop_d = loop_d->outer;
1011 return loop_s;
1014 /* Cancels the LOOP; it must be innermost one. */
1016 static void
1017 cancel_loop (struct loops *loops, struct loop *loop)
1019 basic_block *bbs;
1020 unsigned i;
1022 gcc_assert (!loop->inner);
1024 /* Move blocks up one level (they should be removed as soon as possible). */
1025 bbs = get_loop_body (loop);
1026 for (i = 0; i < loop->num_nodes; i++)
1027 bbs[i]->loop_father = loop->outer;
1029 /* Remove the loop from structure. */
1030 flow_loop_tree_node_remove (loop);
1032 /* Remove loop from loops array. */
1033 loops->parray[loop->num] = NULL;
1035 /* Free loop data. */
1036 flow_loop_free (loop);
1039 /* Cancels LOOP and all its subloops. */
1040 void
1041 cancel_loop_tree (struct loops *loops, struct loop *loop)
1043 while (loop->inner)
1044 cancel_loop_tree (loops, loop->inner);
1045 cancel_loop (loops, loop);
1048 /* Checks that LOOPS are all right:
1049 -- sizes of loops are all right
1050 -- results of get_loop_body really belong to the loop
1051 -- loop header have just single entry edge and single latch edge
1052 -- loop latches have only single successor that is header of their loop
1053 -- irreducible loops are correctly marked
1055 void
1056 verify_loop_structure (struct loops *loops)
1058 unsigned *sizes, i, j;
1059 sbitmap irreds;
1060 basic_block *bbs, bb;
1061 struct loop *loop;
1062 int err = 0;
1063 edge e;
1065 /* Check sizes. */
1066 sizes = XCNEWVEC (unsigned, loops->num);
1067 sizes[0] = 2;
1069 FOR_EACH_BB (bb)
1070 for (loop = bb->loop_father; loop; loop = loop->outer)
1071 sizes[loop->num]++;
1073 for (i = 0; i < loops->num; i++)
1075 if (!loops->parray[i])
1076 continue;
1078 if (loops->parray[i]->num_nodes != sizes[i])
1080 error ("size of loop %d should be %d, not %d",
1081 i, sizes[i], loops->parray[i]->num_nodes);
1082 err = 1;
1086 /* Check get_loop_body. */
1087 for (i = 1; i < loops->num; i++)
1089 loop = loops->parray[i];
1090 if (!loop)
1091 continue;
1092 bbs = get_loop_body (loop);
1094 for (j = 0; j < loop->num_nodes; j++)
1095 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1097 error ("bb %d do not belong to loop %d",
1098 bbs[j]->index, i);
1099 err = 1;
1101 free (bbs);
1104 /* Check headers and latches. */
1105 for (i = 1; i < loops->num; i++)
1107 loop = loops->parray[i];
1108 if (!loop)
1109 continue;
1111 if ((loops->state & LOOPS_HAVE_PREHEADERS)
1112 && EDGE_COUNT (loop->header->preds) != 2)
1114 error ("loop %d's header does not have exactly 2 entries", i);
1115 err = 1;
1117 if (loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1119 if (!single_succ_p (loop->latch))
1121 error ("loop %d's latch does not have exactly 1 successor", i);
1122 err = 1;
1124 if (single_succ (loop->latch) != loop->header)
1126 error ("loop %d's latch does not have header as successor", i);
1127 err = 1;
1129 if (loop->latch->loop_father != loop)
1131 error ("loop %d's latch does not belong directly to it", i);
1132 err = 1;
1135 if (loop->header->loop_father != loop)
1137 error ("loop %d's header does not belong directly to it", i);
1138 err = 1;
1140 if ((loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1141 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1143 error ("loop %d's latch is marked as part of irreducible region", i);
1144 err = 1;
1148 /* Check irreducible loops. */
1149 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1151 /* Record old info. */
1152 irreds = sbitmap_alloc (last_basic_block);
1153 FOR_EACH_BB (bb)
1155 edge_iterator ei;
1156 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1157 SET_BIT (irreds, bb->index);
1158 else
1159 RESET_BIT (irreds, bb->index);
1160 FOR_EACH_EDGE (e, ei, bb->succs)
1161 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1162 e->flags |= EDGE_ALL_FLAGS + 1;
1165 /* Recount it. */
1166 mark_irreducible_loops (loops);
1168 /* Compare. */
1169 FOR_EACH_BB (bb)
1171 edge_iterator ei;
1173 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1174 && !TEST_BIT (irreds, bb->index))
1176 error ("basic block %d should be marked irreducible", bb->index);
1177 err = 1;
1179 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1180 && TEST_BIT (irreds, bb->index))
1182 error ("basic block %d should not be marked irreducible", bb->index);
1183 err = 1;
1185 FOR_EACH_EDGE (e, ei, bb->succs)
1187 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1188 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1190 error ("edge from %d to %d should be marked irreducible",
1191 e->src->index, e->dest->index);
1192 err = 1;
1194 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1195 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1197 error ("edge from %d to %d should not be marked irreducible",
1198 e->src->index, e->dest->index);
1199 err = 1;
1201 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1204 free (irreds);
1207 /* Check the single_exit. */
1208 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
1210 memset (sizes, 0, sizeof (unsigned) * loops->num);
1211 FOR_EACH_BB (bb)
1213 edge_iterator ei;
1214 if (bb->loop_father == loops->tree_root)
1215 continue;
1216 FOR_EACH_EDGE (e, ei, bb->succs)
1218 if (e->dest == EXIT_BLOCK_PTR)
1219 continue;
1221 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
1222 continue;
1224 for (loop = bb->loop_father;
1225 loop != e->dest->loop_father;
1226 loop = loop->outer)
1228 sizes[loop->num]++;
1229 if (loop->single_exit
1230 && loop->single_exit != e)
1232 error ("wrong single exit %d->%d recorded for loop %d",
1233 loop->single_exit->src->index,
1234 loop->single_exit->dest->index,
1235 loop->num);
1236 error ("right exit is %d->%d",
1237 e->src->index, e->dest->index);
1238 err = 1;
1244 for (i = 1; i < loops->num; i++)
1246 loop = loops->parray[i];
1247 if (!loop)
1248 continue;
1250 if (sizes[i] == 1
1251 && !loop->single_exit)
1253 error ("single exit not recorded for loop %d", loop->num);
1254 err = 1;
1257 if (sizes[i] != 1
1258 && loop->single_exit)
1260 error ("loop %d should not have single exit (%d -> %d)",
1261 loop->num,
1262 loop->single_exit->src->index,
1263 loop->single_exit->dest->index);
1264 err = 1;
1269 gcc_assert (!err);
1271 free (sizes);
1274 /* Returns latch edge of LOOP. */
1275 edge
1276 loop_latch_edge (const struct loop *loop)
1278 return find_edge (loop->latch, loop->header);
1281 /* Returns preheader edge of LOOP. */
1282 edge
1283 loop_preheader_edge (const struct loop *loop)
1285 edge e;
1286 edge_iterator ei;
1288 FOR_EACH_EDGE (e, ei, loop->header->preds)
1289 if (e->src != loop->latch)
1290 break;
1292 return e;
1295 /* Returns true if E is an exit of LOOP. */
1297 bool
1298 loop_exit_edge_p (const struct loop *loop, edge e)
1300 return (flow_bb_inside_loop_p (loop, e->src)
1301 && !flow_bb_inside_loop_p (loop, e->dest));