Current state of work.
[official-gcc.git] / gcc / cfgloop.c
bloba93e70657ad91c8c985efba885eca0bb4ef7609d
1 /* Natural loop discovery code for GNU compiler.
2 Copyright (C) 2000, 2001, 2003, 2004 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 "toplev.h"
29 #include "cfgloop.h"
30 #include "flags.h"
31 #include "tree.h"
32 #include "tree-flow.h"
34 /* Ratio of frequencies of edges so that one of more latch edges is
35 considered to belong to inner loop with same header. */
36 #define HEAVY_EDGE_RATIO 8
38 #define HEADER_BLOCK(B) (* (int *) (B)->aux)
39 #define LATCH_EDGE(E) (*(int *) (E)->aux)
41 static void flow_loops_cfg_dump (const struct loops *, FILE *);
42 static void flow_loop_entry_edges_find (struct loop *);
43 static void flow_loop_exit_edges_find (struct loop *);
44 static int flow_loop_nodes_find (basic_block, struct loop *);
45 static void flow_loop_pre_header_scan (struct loop *);
46 static basic_block flow_loop_pre_header_find (basic_block);
47 static int flow_loop_level_compute (struct loop *);
48 static int flow_loops_level_compute (struct loops *);
49 static void establish_preds (struct loop *);
50 static void canonicalize_loop_headers (void);
51 static bool glb_enum_p (basic_block, void *);
53 /* Dump loop related CFG information. */
55 static void
56 flow_loops_cfg_dump (const struct loops *loops, FILE *file)
58 int i;
59 basic_block bb;
61 if (! loops->num || ! file)
62 return;
64 FOR_EACH_BB (bb)
66 edge succ;
67 unsigned ix;
69 fprintf (file, ";; %d succs { ", bb->index);
70 FOR_EACH_EDGE (succ, bb->succ, ix)
71 fprintf (file, "%d ", succ->dest->index);
72 fprintf (file, "}\n");
75 /* Dump the DFS node order. */
76 if (loops->cfg.dfs_order)
78 fputs (";; DFS order: ", file);
79 for (i = 0; i < n_basic_blocks; i++)
80 fprintf (file, "%d ", loops->cfg.dfs_order[i]);
82 fputs ("\n", file);
85 /* Dump the reverse completion node order. */
86 if (loops->cfg.rc_order)
88 fputs (";; RC order: ", file);
89 for (i = 0; i < n_basic_blocks; i++)
90 fprintf (file, "%d ", loops->cfg.rc_order[i]);
92 fputs ("\n", file);
96 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
98 bool
99 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
101 return loop->depth > outer->depth
102 && loop->pred[outer->depth] == outer;
105 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
106 loops within LOOP. */
108 struct loop *
109 superloop_at_depth (struct loop *loop, unsigned depth)
111 if (depth > (unsigned) loop->depth)
112 abort ();
114 if (depth == (unsigned) loop->depth)
115 return loop;
117 return loop->pred[depth];
120 /* Dump the loop information specified by LOOP to the stream FILE
121 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
123 void
124 flow_loop_dump (const struct loop *loop, FILE *file,
125 void (*loop_dump_aux) (const struct loop *, FILE *, int),
126 int verbose)
128 basic_block *bbs;
129 unsigned i;
131 if (! loop || ! loop->header)
132 return;
134 fprintf (file, ";;\n;; Loop %d:%s\n", loop->num,
135 loop->invalid ? " invalid" : "");
137 fprintf (file, ";; header %d, latch %d, pre-header %d\n",
138 loop->header->index, loop->latch->index,
139 loop->pre_header ? loop->pre_header->index : -1);
140 fprintf (file, ";; depth %d, level %d, outer %ld\n",
141 loop->depth, loop->level,
142 (long) (loop->outer ? loop->outer->num : -1));
144 if (loop->pre_header_edges)
145 flow_edge_list_print (";; pre-header edges", loop->pre_header_edges,
146 loop->num_pre_header_edges, file);
148 flow_edge_list_print (";; entry edges", loop->entry_edges,
149 loop->num_entries, file);
150 fprintf (file, ";; nodes:");
151 bbs = get_loop_body (loop);
152 for (i = 0; i < loop->num_nodes; i++)
153 fprintf (file, " %d", bbs[i]->index);
154 free (bbs);
155 fprintf (file, "\n");
156 flow_edge_list_print (";; exit edges", loop->exit_edges,
157 loop->num_exits, file);
159 if (loop_dump_aux)
160 loop_dump_aux (loop, file, verbose);
163 /* Dump the loop information specified by LOOPS to the stream FILE,
164 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
166 void
167 flow_loops_dump (const struct loops *loops, FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
169 int i;
170 int num_loops;
172 num_loops = loops->num;
173 if (! num_loops || ! file)
174 return;
176 fprintf (file, ";; %d loops found, %d levels\n",
177 num_loops, loops->levels);
179 for (i = 0; i < num_loops; i++)
181 struct loop *loop = loops->parray[i];
183 if (!loop)
184 continue;
186 flow_loop_dump (loop, file, loop_dump_aux, verbose);
189 if (verbose)
190 flow_loops_cfg_dump (loops, file);
193 /* Free data allocated for LOOP. */
194 void
195 flow_loop_free (struct loop *loop)
197 if (loop->pre_header_edges)
198 free (loop->pre_header_edges);
199 if (loop->entry_edges)
200 free (loop->entry_edges);
201 if (loop->exit_edges)
202 free (loop->exit_edges);
203 if (loop->pred)
204 free (loop->pred);
205 free (loop);
208 /* Free all the memory allocated for LOOPS. */
210 void
211 flow_loops_free (struct loops *loops)
213 if (loops->parray)
215 unsigned i;
217 if (! loops->num)
218 abort ();
220 /* Free the loop descriptors. */
221 for (i = 0; i < loops->num; i++)
223 struct loop *loop = loops->parray[i];
225 if (!loop)
226 continue;
228 flow_loop_free (loop);
231 free (loops->parray);
232 loops->parray = NULL;
234 if (loops->cfg.dfs_order)
235 free (loops->cfg.dfs_order);
236 if (loops->cfg.rc_order)
237 free (loops->cfg.rc_order);
242 /* Find the entry edges into the LOOP. */
244 static void
245 flow_loop_entry_edges_find (struct loop *loop)
247 edge e;
248 unsigned ix;
249 int num_entries;
251 num_entries = 0;
252 FOR_EACH_EDGE (e, loop->header->pred, ix)
254 if (flow_loop_outside_edge_p (loop, e))
255 num_entries++;
258 if (! num_entries)
259 abort ();
261 loop->entry_edges = xmalloc (num_entries * sizeof (edge *));
263 num_entries = 0;
264 FOR_EACH_EDGE (e, loop->header->pred, ix)
266 if (flow_loop_outside_edge_p (loop, e))
267 loop->entry_edges[num_entries++] = e;
270 loop->num_entries = num_entries;
273 /* Find the exit edges from the LOOP. */
275 static void
276 flow_loop_exit_edges_find (struct loop *loop)
278 edge e;
279 basic_block node, *bbs;
280 unsigned num_exits, i, ix;
282 loop->exit_edges = NULL;
283 loop->num_exits = 0;
285 /* Check all nodes within the loop to see if there are any
286 successors not in the loop. Note that a node may have multiple
287 exiting edges. */
288 num_exits = 0;
289 bbs = get_loop_body (loop);
290 for (i = 0; i < loop->num_nodes; i++)
292 node = bbs[i];
294 FOR_EACH_EDGE (e, node->succ, ix)
296 basic_block dest = e->dest;
298 if (!flow_bb_inside_loop_p (loop, dest))
299 num_exits++;
303 if (! num_exits)
305 free (bbs);
306 return;
309 loop->exit_edges = xmalloc (num_exits * sizeof (edge *));
311 /* Store all exiting edges into an array. */
312 num_exits = 0;
313 for (i = 0; i < loop->num_nodes; i++)
315 node = bbs[i];
316 FOR_EACH_EDGE (e, node->succ, ix)
318 basic_block dest = e->dest;
320 if (!flow_bb_inside_loop_p (loop, dest))
321 loop->exit_edges[num_exits++] = e;
324 free (bbs);
325 loop->num_exits = num_exits;
328 /* Find the nodes contained within the LOOP with header HEADER.
329 Return the number of nodes within the loop. */
331 static int
332 flow_loop_nodes_find (basic_block header, struct loop *loop)
334 basic_block *stack;
335 int sp;
336 int num_nodes = 1;
338 header->loop_father = loop;
339 header->loop_depth = loop->depth;
341 if (loop->latch->loop_father != loop)
343 stack = xmalloc (n_basic_blocks * sizeof (basic_block));
344 sp = 0;
345 num_nodes++;
346 stack[sp++] = loop->latch;
347 loop->latch->loop_father = loop;
348 loop->latch->loop_depth = loop->depth;
350 while (sp)
352 basic_block node;
353 edge e;
354 unsigned ix;
356 node = stack[--sp];
358 FOR_EACH_EDGE (e, node->pred, ix)
360 basic_block ancestor = e->src;
362 if (ancestor != ENTRY_BLOCK_PTR
363 && ancestor->loop_father != loop)
365 ancestor->loop_father = loop;
366 ancestor->loop_depth = loop->depth;
367 num_nodes++;
368 stack[sp++] = ancestor;
372 free (stack);
374 return num_nodes;
377 /* Find the root node of the loop pre-header extended basic block and
378 the edges along the trace from the root node to the loop header. */
380 static void
381 flow_loop_pre_header_scan (struct loop *loop)
383 int num;
384 basic_block ebb;
385 edge e;
387 loop->num_pre_header_edges = 0;
388 if (loop->num_entries != 1)
389 return;
391 ebb = loop->entry_edges[0]->src;
392 if (ebb == ENTRY_BLOCK_PTR)
393 return;
395 /* Count number of edges along trace from loop header to
396 root of pre-header extended basic block. Usually this is
397 only one or two edges. */
398 for (num = 1;
399 EDGE_0 (ebb->pred)->src != ENTRY_BLOCK_PTR && EDGE_COUNT (ebb->pred) == 1;
400 num++)
401 ebb = EDGE_0 (ebb->pred)->src;
403 loop->pre_header_edges = xmalloc (num * sizeof (edge));
404 loop->num_pre_header_edges = num;
406 /* Store edges in order that they are followed. The source of the first edge
407 is the root node of the pre-header extended basic block and the
408 destination of the last last edge is the loop header. */
409 for (e = loop->entry_edges[0]; num; e = e->src->pred)
410 loop->pre_header_edges[--num] = e;
413 /* Return the block for the pre-header of the loop with header
414 HEADER. Return NULL if there is no pre-header. */
416 static basic_block
417 flow_loop_pre_header_find (basic_block header)
419 basic_block pre_header;
420 edge e;
421 unsigned ix;
423 /* If block p is a predecessor of the header and is the only block
424 that the header does not dominate, then it is the pre-header. */
425 pre_header = NULL;
426 FOR_EACH_EDGE (e, header->pred, ix)
428 basic_block node = e->src;
430 if (node != ENTRY_BLOCK_PTR
431 && ! dominated_by_p (CDI_DOMINATORS, node, header))
433 if (pre_header == NULL)
434 pre_header = node;
435 else
437 /* There are multiple edges into the header from outside
438 the loop so there is no pre-header block. */
439 pre_header = NULL;
440 break;
445 return pre_header;
448 static void
449 establish_preds (struct loop *loop)
451 struct loop *ploop, *father = loop->outer;
453 loop->depth = father->depth + 1;
454 if (loop->pred)
455 free (loop->pred);
456 loop->pred = xmalloc (sizeof (struct loop *) * loop->depth);
457 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
458 loop->pred[father->depth] = father;
460 for (ploop = loop->inner; ploop; ploop = ploop->next)
461 establish_preds (ploop);
464 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
465 added loop. If LOOP has some children, take care of that their
466 pred field will be initialized correctly. */
468 void
469 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
471 loop->next = father->inner;
472 father->inner = loop;
473 loop->outer = father;
475 establish_preds (loop);
478 /* Remove LOOP from the loop hierarchy tree. */
480 void
481 flow_loop_tree_node_remove (struct loop *loop)
483 struct loop *prev, *father;
485 father = loop->outer;
486 loop->outer = NULL;
488 /* Remove loop from the list of sons. */
489 if (father->inner == loop)
490 father->inner = loop->next;
491 else
493 for (prev = father->inner; prev->next != loop; prev = prev->next);
494 prev->next = loop->next;
497 loop->depth = -1;
498 free (loop->pred);
499 loop->pred = NULL;
502 /* Helper function to compute loop nesting depth and enclosed loop level
503 for the natural loop specified by LOOP. Returns the loop level. */
505 static int
506 flow_loop_level_compute (struct loop *loop)
508 struct loop *inner;
509 int level = 1;
511 if (! loop)
512 return 0;
514 /* Traverse loop tree assigning depth and computing level as the
515 maximum level of all the inner loops of this loop. The loop
516 level is equivalent to the height of the loop in the loop tree
517 and corresponds to the number of enclosed loop levels (including
518 itself). */
519 for (inner = loop->inner; inner; inner = inner->next)
521 int ilevel = flow_loop_level_compute (inner) + 1;
523 if (ilevel > level)
524 level = ilevel;
527 loop->level = level;
528 return level;
531 /* Compute the loop nesting depth and enclosed loop level for the loop
532 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
533 level. */
535 static int
536 flow_loops_level_compute (struct loops *loops)
538 return flow_loop_level_compute (loops->tree_root);
541 /* Scan a single natural loop specified by LOOP collecting information
542 about it specified by FLAGS. */
545 flow_loop_scan (struct loop *loop, int flags)
547 if (flags & LOOP_ENTRY_EDGES)
549 /* Find edges which enter the loop header.
550 Note that the entry edges should only
551 enter the header of a natural loop. */
552 flow_loop_entry_edges_find (loop);
555 if (flags & LOOP_EXIT_EDGES)
557 /* Find edges which exit the loop. */
558 flow_loop_exit_edges_find (loop);
561 if (flags & LOOP_PRE_HEADER)
563 /* Look to see if the loop has a pre-header node. */
564 loop->pre_header = flow_loop_pre_header_find (loop->header);
566 /* Find the blocks within the extended basic block of
567 the loop pre-header. */
568 flow_loop_pre_header_scan (loop);
571 return 1;
574 /* A callback to update latch and header info for basic block JUMP created
575 by redirecting an edge. */
577 static void
578 update_latch_info (basic_block jump)
580 alloc_aux_for_block (jump, sizeof (int));
581 HEADER_BLOCK (jump) = 0;
582 alloc_aux_for_edge (EDGE_0 (jump->pred), sizeof (int));
583 LATCH_EDGE (EDGE_0 (jump->pred)) = 0;
586 /* A callback for make_forwarder block, to redirect all edges except for
587 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
588 whether to redirect it. */
590 static edge mfb_kj_edge;
591 static bool
592 mfb_keep_just (edge e)
594 return e != mfb_kj_edge;
597 /* A callback for make_forwarder block, to redirect the latch edges into an
598 entry part. E is the edge for that we should decide whether to redirect
599 it. */
601 static bool
602 mfb_keep_nonlatch (edge e)
604 return LATCH_EDGE (e);
607 /* Takes care of merging natural loops with shared headers. */
609 static void
610 canonicalize_loop_headers (void)
612 basic_block header;
613 edge e;
614 unsigned ix;
616 /* Compute the dominators. */
617 calculate_dominance_info (CDI_DOMINATORS);
619 alloc_aux_for_blocks (sizeof (int));
620 alloc_aux_for_edges (sizeof (int));
622 /* Split blocks so that each loop has only single latch. */
623 FOR_EACH_BB (header)
625 int num_latches = 0;
626 int have_abnormal_edge = 0;
628 FOR_EACH_EDGE (e, header->pred, ix)
630 basic_block latch = e->src;
632 if (e->flags & EDGE_ABNORMAL)
633 have_abnormal_edge = 1;
635 if (latch != ENTRY_BLOCK_PTR
636 && dominated_by_p (CDI_DOMINATORS, latch, header))
638 num_latches++;
639 LATCH_EDGE (e) = 1;
642 if (have_abnormal_edge)
643 HEADER_BLOCK (header) = 0;
644 else
645 HEADER_BLOCK (header) = num_latches;
648 free_dominance_info (CDI_DOMINATORS);
650 if (HEADER_BLOCK (EDGE_0 (ENTRY_BLOCK_PTR->succ)->dest))
652 basic_block bb;
654 /* We could not redirect edges freely here. On the other hand,
655 we can simply split the edge from entry block. */
656 bb = split_edge (EDGE_0 (ENTRY_BLOCK_PTR->succ));
658 alloc_aux_for_edge (EDGE_0 (bb->succ), sizeof (int));
659 LATCH_EDGE (EDGE_0 (bb->succ)) = 0;
660 alloc_aux_for_block (bb, sizeof (int));
661 HEADER_BLOCK (bb) = 0;
664 FOR_EACH_BB (header)
666 int max_freq, is_heavy;
667 edge heavy, tmp_edge;
669 if (HEADER_BLOCK (header) <= 1)
670 continue;
672 /* Find a heavy edge. */
673 is_heavy = 1;
674 heavy = NULL;
675 max_freq = 0;
677 FOR_EACH_EDGE (e, header->pred, ix)
678 if (LATCH_EDGE (e) &&
679 EDGE_FREQUENCY (e) > max_freq)
680 max_freq = EDGE_FREQUENCY (e);
682 FOR_EACH_EDGE (e, header->pred, ix)
683 if (LATCH_EDGE (e) &&
684 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
686 if (heavy)
688 is_heavy = 0;
689 break;
691 else
692 heavy = e;
695 if (is_heavy)
697 /* Split out the heavy edge, and create inner loop for it. */
698 mfb_kj_edge = heavy;
699 tmp_edge = make_forwarder_block (header, mfb_keep_just,
700 update_latch_info);
701 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
702 HEADER_BLOCK (tmp_edge->dest) = 1;
703 alloc_aux_for_edge (tmp_edge, sizeof (int));
704 LATCH_EDGE (tmp_edge) = 0;
705 HEADER_BLOCK (header)--;
708 if (HEADER_BLOCK (header) > 1)
710 /* Create a new latch block. */
711 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
712 update_latch_info);
713 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
714 HEADER_BLOCK (tmp_edge->src) = 0;
715 HEADER_BLOCK (tmp_edge->dest) = 1;
716 alloc_aux_for_edge (tmp_edge, sizeof (int));
717 LATCH_EDGE (tmp_edge) = 1;
721 free_aux_for_blocks ();
722 free_aux_for_edges ();
725 /* Find all the natural loops in the function and save in LOOPS structure and
726 recalculate loop_depth information in basic block structures. FLAGS
727 controls which loop information is collected. Return the number of natural
728 loops found. */
731 flow_loops_find (struct loops *loops, int flags)
733 int i;
734 int b;
735 int num_loops;
736 edge e;
737 sbitmap headers;
738 int *dfs_order;
739 int *rc_order;
740 basic_block header;
741 basic_block bb;
742 unsigned ix;
744 /* This function cannot be repeatedly called with different
745 flags to build up the loop information. The loop tree
746 must always be built if this function is called. */
747 if (! (flags & LOOP_TREE))
748 abort ();
750 memset (loops, 0, sizeof *loops);
752 /* Taking care of this degenerate case makes the rest of
753 this code simpler. */
754 if (n_basic_blocks == 0)
755 return 0;
757 dfs_order = NULL;
758 rc_order = NULL;
760 /* Join loops with shared headers. */
761 canonicalize_loop_headers ();
763 /* Compute the dominators. */
764 calculate_dominance_info (CDI_DOMINATORS);
766 /* Count the number of loop headers. This should be the
767 same as the number of natural loops. */
768 headers = sbitmap_alloc (last_basic_block);
769 sbitmap_zero (headers);
771 num_loops = 0;
772 FOR_EACH_BB (header)
774 int more_latches = 0;
776 header->loop_depth = 0;
778 /* If we have an abnormal predecessor, do not consider the
779 loop (not worth the problems). */
780 FOR_EACH_EDGE (e, header->pred, ix)
781 if (e->flags & EDGE_ABNORMAL)
782 break;
783 if (e)
784 continue;
786 FOR_EACH_EDGE (e, header->pred, ix)
788 basic_block latch = e->src;
790 if (e->flags & EDGE_ABNORMAL)
791 abort ();
793 /* Look for back edges where a predecessor is dominated
794 by this block. A natural loop has a single entry
795 node (header) that dominates all the nodes in the
796 loop. It also has single back edge to the header
797 from a latch node. */
798 if (latch != ENTRY_BLOCK_PTR
799 && dominated_by_p (CDI_DOMINATORS, latch, header))
801 /* Shared headers should be eliminated by now. */
802 if (more_latches)
803 abort ();
804 more_latches = 1;
805 SET_BIT (headers, header->index);
806 num_loops++;
811 /* Allocate loop structures. */
812 loops->parray = xcalloc (num_loops + 1, sizeof (struct loop *));
814 /* Dummy loop containing whole function. */
815 loops->parray[0] = xcalloc (1, sizeof (struct loop));
816 loops->parray[0]->next = NULL;
817 loops->parray[0]->inner = NULL;
818 loops->parray[0]->outer = NULL;
819 loops->parray[0]->depth = 0;
820 loops->parray[0]->pred = NULL;
821 loops->parray[0]->num_nodes = n_basic_blocks + 2;
822 loops->parray[0]->latch = EXIT_BLOCK_PTR;
823 loops->parray[0]->header = ENTRY_BLOCK_PTR;
824 ENTRY_BLOCK_PTR->loop_father = loops->parray[0];
825 EXIT_BLOCK_PTR->loop_father = loops->parray[0];
827 loops->tree_root = loops->parray[0];
829 /* Find and record information about all the natural loops
830 in the CFG. */
831 loops->num = 1;
832 FOR_EACH_BB (bb)
833 bb->loop_father = loops->tree_root;
835 if (num_loops)
837 /* Compute depth first search order of the CFG so that outer
838 natural loops will be found before inner natural loops. */
839 dfs_order = xmalloc (n_basic_blocks * sizeof (int));
840 rc_order = xmalloc (n_basic_blocks * sizeof (int));
841 flow_depth_first_order_compute (dfs_order, rc_order);
843 /* Save CFG derived information to avoid recomputing it. */
844 loops->cfg.dfs_order = dfs_order;
845 loops->cfg.rc_order = rc_order;
847 num_loops = 1;
849 for (b = 0; b < n_basic_blocks; b++)
851 struct loop *loop;
853 /* Search the nodes of the CFG in reverse completion order
854 so that we can find outer loops first. */
855 if (!TEST_BIT (headers, rc_order[b]))
856 continue;
858 header = BASIC_BLOCK (rc_order[b]);
860 loop = loops->parray[num_loops] = xcalloc (1, sizeof (struct loop));
862 loop->header = header;
863 loop->num = num_loops;
864 num_loops++;
866 /* Look for the latch for this header block. */
867 FOR_EACH_EDGE (e, header->pred, ix)
869 basic_block latch = e->src;
871 if (latch != ENTRY_BLOCK_PTR
872 && dominated_by_p (CDI_DOMINATORS, latch, header))
874 loop->latch = latch;
875 break;
879 flow_loop_tree_node_add (header->loop_father, loop);
880 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
883 /* Assign the loop nesting depth and enclosed loop level for each
884 loop. */
885 loops->levels = flow_loops_level_compute (loops);
887 /* Scan the loops. */
888 for (i = 1; i < num_loops; i++)
889 flow_loop_scan (loops->parray[i], flags);
891 loops->num = num_loops;
893 else
895 free_dominance_info (CDI_DOMINATORS);
898 sbitmap_free (headers);
900 loops->state = 0;
901 #ifdef ENABLE_CHECKING
902 verify_flow_info ();
903 verify_loop_structure (loops);
904 #endif
906 return loops->num;
909 /* Update the information regarding the loops in the CFG
910 specified by LOOPS. */
913 flow_loops_update (struct loops *loops, int flags)
915 /* One day we may want to update the current loop data. For now
916 throw away the old stuff and rebuild what we need. */
917 if (loops->parray)
918 flow_loops_free (loops);
920 return flow_loops_find (loops, flags);
923 /* Return nonzero if basic block BB belongs to LOOP. */
924 bool
925 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
927 struct loop *source_loop;
929 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
930 return 0;
932 source_loop = bb->loop_father;
933 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
936 /* Return nonzero if edge E enters header of LOOP from outside of LOOP. */
938 bool
939 flow_loop_outside_edge_p (const struct loop *loop, edge e)
941 if (e->dest != loop->header)
942 abort ();
943 return !flow_bb_inside_loop_p (loop, e->src);
946 /* Enumeration predicate for get_loop_body. */
947 static bool
948 glb_enum_p (basic_block bb, void *glb_header)
950 return bb != (basic_block) glb_header;
953 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
954 order against direction of edges from latch. Specially, if
955 header != latch, latch is the 1-st block. */
956 basic_block *
957 get_loop_body (const struct loop *loop)
959 basic_block *tovisit, bb;
960 unsigned tv = 0;
962 if (!loop->num_nodes)
963 abort ();
965 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
966 tovisit[tv++] = loop->header;
968 if (loop->latch == EXIT_BLOCK_PTR)
970 /* There may be blocks unreachable from EXIT_BLOCK. */
971 if (loop->num_nodes != (unsigned) n_basic_blocks + 2)
972 abort ();
973 FOR_EACH_BB (bb)
974 tovisit[tv++] = bb;
975 tovisit[tv++] = EXIT_BLOCK_PTR;
977 else if (loop->latch != loop->header)
979 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
980 tovisit + 1, loop->num_nodes - 1,
981 loop->header) + 1;
984 if (tv != loop->num_nodes)
985 abort ();
986 return tovisit;
989 /* Fills dominance descendants inside LOOP of the basic block BB into
990 array TOVISIT from index *TV. */
992 static void
993 fill_sons_in_loop (const struct loop *loop, basic_block bb,
994 basic_block *tovisit, int *tv)
996 basic_block son, postpone = NULL;
998 tovisit[(*tv)++] = bb;
999 for (son = first_dom_son (CDI_DOMINATORS, bb);
1000 son;
1001 son = next_dom_son (CDI_DOMINATORS, son))
1003 if (!flow_bb_inside_loop_p (loop, son))
1004 continue;
1006 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
1008 postpone = son;
1009 continue;
1011 fill_sons_in_loop (loop, son, tovisit, tv);
1014 if (postpone)
1015 fill_sons_in_loop (loop, postpone, tovisit, tv);
1018 /* Gets body of a LOOP (that must be different from the outermost loop)
1019 sorted by dominance relation. Additionally, if a basic block s dominates
1020 the latch, then only blocks dominated by s are be after it. */
1022 basic_block *
1023 get_loop_body_in_dom_order (const struct loop *loop)
1025 basic_block *tovisit;
1026 int tv;
1028 if (!loop->num_nodes)
1029 abort ();
1031 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
1033 if (loop->latch == EXIT_BLOCK_PTR)
1034 abort ();
1036 tv = 0;
1037 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
1039 if (tv != (int) loop->num_nodes)
1040 abort ();
1042 return tovisit;
1045 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
1046 edge *
1047 get_loop_exit_edges (const struct loop *loop, unsigned int *n_edges)
1049 edge *edges, e;
1050 unsigned i, n;
1051 basic_block * body;
1052 unsigned ix;
1054 if (loop->latch == EXIT_BLOCK_PTR)
1055 abort ();
1057 body = get_loop_body (loop);
1058 n = 0;
1059 for (i = 0; i < loop->num_nodes; i++)
1060 FOR_EACH_EDGE (e, body[i]->succ, ix)
1061 if (!flow_bb_inside_loop_p (loop, e->dest))
1062 n++;
1063 edges = xmalloc (n * sizeof (edge));
1064 *n_edges = n;
1065 n = 0;
1066 for (i = 0; i < loop->num_nodes; i++)
1067 FOR_EACH_EDGE (e, body[i]->succ, ix)
1068 if (!flow_bb_inside_loop_p (loop, e->dest))
1069 edges[n++] = e;
1070 free (body);
1072 return edges;
1075 /* Counts the number of conditional branches inside LOOP. */
1077 unsigned
1078 num_loop_branches (const struct loop *loop)
1080 unsigned i, n;
1081 basic_block * body;
1083 if (loop->latch == EXIT_BLOCK_PTR)
1084 abort ();
1086 body = get_loop_body (loop);
1087 n = 0;
1088 for (i = 0; i < loop->num_nodes; i++)
1089 if (EDGE_COUNT (body[i]->succ) >= 2)
1090 n++;
1091 free (body);
1093 return n;
1096 /* Adds basic block BB to LOOP. */
1097 void
1098 add_bb_to_loop (basic_block bb, struct loop *loop)
1100 int i;
1102 bb->loop_father = loop;
1103 bb->loop_depth = loop->depth;
1104 loop->num_nodes++;
1105 for (i = 0; i < loop->depth; i++)
1106 loop->pred[i]->num_nodes++;
1109 /* Remove basic block BB from loops. */
1110 void
1111 remove_bb_from_loops (basic_block bb)
1113 int i;
1114 struct loop *loop = bb->loop_father;
1116 loop->num_nodes--;
1117 for (i = 0; i < loop->depth; i++)
1118 loop->pred[i]->num_nodes--;
1119 bb->loop_father = NULL;
1120 bb->loop_depth = 0;
1123 /* Finds nearest common ancestor in loop tree for given loops. */
1124 struct loop *
1125 find_common_loop (struct loop *loop_s, struct loop *loop_d)
1127 if (!loop_s) return loop_d;
1128 if (!loop_d) return loop_s;
1130 if (loop_s->depth < loop_d->depth)
1131 loop_d = loop_d->pred[loop_s->depth];
1132 else if (loop_s->depth > loop_d->depth)
1133 loop_s = loop_s->pred[loop_d->depth];
1135 while (loop_s != loop_d)
1137 loop_s = loop_s->outer;
1138 loop_d = loop_d->outer;
1140 return loop_s;
1143 /* Cancels the LOOP; it must be innermost one. */
1144 void
1145 cancel_loop (struct loops *loops, struct loop *loop)
1147 basic_block *bbs;
1148 unsigned i;
1150 if (loop->inner)
1151 abort ();
1153 /* Move blocks up one level (they should be removed as soon as possible). */
1154 bbs = get_loop_body (loop);
1155 for (i = 0; i < loop->num_nodes; i++)
1156 bbs[i]->loop_father = loop->outer;
1158 /* Remove the loop from structure. */
1159 flow_loop_tree_node_remove (loop);
1161 /* Remove loop from loops array. */
1162 loops->parray[loop->num] = NULL;
1164 /* Free loop data. */
1165 flow_loop_free (loop);
1168 /* Cancels LOOP and all its subloops. */
1169 void
1170 cancel_loop_tree (struct loops *loops, struct loop *loop)
1172 while (loop->inner)
1173 cancel_loop_tree (loops, loop->inner);
1174 cancel_loop (loops, loop);
1177 /* Checks that LOOPS are all right:
1178 -- sizes of loops are all right
1179 -- results of get_loop_body really belong to the loop
1180 -- loop header have just single entry edge and single latch edge
1181 -- loop latches have only single successor that is header of their loop
1182 -- irreducible loops are correctly marked
1184 void
1185 verify_loop_structure (struct loops *loops)
1187 unsigned *sizes, i, j;
1188 sbitmap irreds;
1189 basic_block *bbs, bb;
1190 struct loop *loop;
1191 int err = 0;
1192 edge e;
1193 unsigned ix;
1195 /* Check sizes. */
1196 sizes = xcalloc (loops->num, sizeof (int));
1197 sizes[0] = 2;
1199 FOR_EACH_BB (bb)
1200 for (loop = bb->loop_father; loop; loop = loop->outer)
1201 sizes[loop->num]++;
1203 for (i = 0; i < loops->num; i++)
1205 if (!loops->parray[i])
1206 continue;
1208 if (loops->parray[i]->num_nodes != sizes[i])
1210 error ("Size of loop %d should be %d, not %d.",
1211 i, sizes[i], loops->parray[i]->num_nodes);
1212 err = 1;
1216 free (sizes);
1218 /* Check get_loop_body. */
1219 for (i = 1; i < loops->num; i++)
1221 loop = loops->parray[i];
1222 if (!loop)
1223 continue;
1224 bbs = get_loop_body (loop);
1226 for (j = 0; j < loop->num_nodes; j++)
1227 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1229 error ("Bb %d do not belong to loop %d.",
1230 bbs[j]->index, i);
1231 err = 1;
1233 free (bbs);
1236 /* Check headers and latches. */
1237 for (i = 1; i < loops->num; i++)
1239 loop = loops->parray[i];
1240 if (!loop)
1241 continue;
1243 if ((loops->state & LOOPS_HAVE_PREHEADERS)
1244 && EDGE_COUNT (loop->header->pred) != 2)
1246 error ("Loop %d's header does not have exactly 2 entries.", i);
1247 err = 1;
1249 if (loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1251 if (EDGE_COUNT (loop->latch->succ) != 1)
1253 error ("Loop %d's latch does not have exactly 1 successor.", i);
1254 err = 1;
1256 if (EDGE_0 (loop->latch->succ)->dest != loop->header)
1258 error ("Loop %d's latch does not have header as successor.", i);
1259 err = 1;
1261 if (loop->latch->loop_father != loop)
1263 error ("Loop %d's latch does not belong directly to it.", i);
1264 err = 1;
1267 if (loop->header->loop_father != loop)
1269 error ("Loop %d's header does not belong directly to it.", i);
1270 err = 1;
1272 if ((loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1273 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1275 error ("Loop %d's latch is marked as part of irreducible region.", i);
1276 err = 1;
1280 /* Check irreducible loops. */
1281 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1283 /* Record old info. */
1284 irreds = sbitmap_alloc (last_basic_block);
1285 FOR_EACH_BB (bb)
1287 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1288 SET_BIT (irreds, bb->index);
1289 else
1290 RESET_BIT (irreds, bb->index);
1291 FOR_EACH_EDGE (e, bb->succ, ix)
1292 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1293 e->flags |= EDGE_ALL_FLAGS + 1;
1296 /* Recount it. */
1297 mark_irreducible_loops (loops);
1299 /* Compare. */
1300 FOR_EACH_BB (bb)
1302 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1303 && !TEST_BIT (irreds, bb->index))
1305 error ("Basic block %d should be marked irreducible.", bb->index);
1306 err = 1;
1308 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1309 && TEST_BIT (irreds, bb->index))
1311 error ("Basic block %d should not be marked irreducible.", bb->index);
1312 err = 1;
1314 FOR_EACH_EDGE (e, bb->succ, ix)
1316 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1317 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1319 error ("Edge from %d to %d should be marked irreducible.",
1320 e->src->index, e->dest->index);
1321 err = 1;
1323 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1324 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1326 error ("Edge from %d to %d should not be marked irreducible.",
1327 e->src->index, e->dest->index);
1328 err = 1;
1330 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1333 free (irreds);
1336 if (err)
1337 abort ();
1340 /* Returns latch edge of LOOP. */
1341 edge
1342 loop_latch_edge (const struct loop *loop)
1344 edge e;
1345 unsigned ix;
1347 FOR_EACH_EDGE (e, loop->header->pred, ix)
1348 if (e->src == loop->latch)
1349 break;
1351 return e;
1354 /* Returns preheader edge of LOOP. */
1355 edge
1356 loop_preheader_edge (const struct loop *loop)
1358 edge e;
1359 unsigned ix;
1361 FOR_EACH_EDGE (e, loop->header->pred, ix)
1362 if (e->src != loop->latch)
1363 break;
1365 return e;