gcc/ChangeLog:
[official-gcc.git] / gcc / cfgloop.c
blobbd9e6d351da42ccbcda8bb2d4dc4eb6e19a7e98c
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 (FILE *);
44 static void establish_preds (struct loop *);
45 static void canonicalize_loop_headers (void);
46 static bool glb_enum_p (basic_block, void *);
48 /* Dump loop related CFG information. */
50 static void
51 flow_loops_cfg_dump (FILE *file)
53 basic_block bb;
55 if (!file)
56 return;
58 FOR_EACH_BB (bb)
60 edge succ;
61 edge_iterator ei;
63 fprintf (file, ";; %d succs { ", bb->index);
64 FOR_EACH_EDGE (succ, ei, bb->succs)
65 fprintf (file, "%d ", succ->dest->index);
66 fprintf (file, "}\n");
70 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
72 bool
73 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
75 return (loop->depth > outer->depth
76 && loop->pred[outer->depth] == outer);
79 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
80 loops within LOOP. */
82 struct loop *
83 superloop_at_depth (struct loop *loop, unsigned depth)
85 gcc_assert (depth <= (unsigned) loop->depth);
87 if (depth == (unsigned) loop->depth)
88 return loop;
90 return loop->pred[depth];
93 /* Dump the loop information specified by LOOP to the stream FILE
94 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
96 void
97 flow_loop_dump (const struct loop *loop, FILE *file,
98 void (*loop_dump_aux) (const struct loop *, FILE *, int),
99 int verbose)
101 basic_block *bbs;
102 unsigned i;
104 if (! loop || ! loop->header)
105 return;
107 fprintf (file, ";;\n;; Loop %d\n", loop->num);
109 fprintf (file, ";; header %d, latch %d\n",
110 loop->header->index, loop->latch->index);
111 fprintf (file, ";; depth %d, outer %ld\n",
112 loop->depth, (long) (loop->outer ? loop->outer->num : -1));
114 fprintf (file, ";; nodes:");
115 bbs = get_loop_body (loop);
116 for (i = 0; i < loop->num_nodes; i++)
117 fprintf (file, " %d", bbs[i]->index);
118 free (bbs);
119 fprintf (file, "\n");
121 if (loop_dump_aux)
122 loop_dump_aux (loop, file, verbose);
125 /* Dump the loop information about loops to the stream FILE,
126 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
128 void
129 flow_loops_dump (FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
131 loop_iterator li;
132 struct loop *loop;
134 if (!current_loops || ! file)
135 return;
137 fprintf (file, ";; %d loops found\n", number_of_loops ());
139 FOR_EACH_LOOP (li, loop, LI_INCLUDE_ROOT)
141 flow_loop_dump (loop, file, loop_dump_aux, verbose);
144 if (verbose)
145 flow_loops_cfg_dump (file);
148 /* Free data allocated for LOOP. */
149 void
150 flow_loop_free (struct loop *loop)
152 struct loop_exit *exit, *next;
154 if (loop->pred)
155 free (loop->pred);
157 /* Break the list of the loop exit records. They will be freed when the
158 corresponding edge is rescanned or removed, and this avoids
159 accessing the (already released) head of the list stored in the
160 loop structure. */
161 for (exit = loop->exits.next; exit != &loop->exits; exit = next)
163 next = exit->next;
164 exit->next = exit;
165 exit->prev = exit;
168 free (loop);
171 /* Free all the memory allocated for LOOPS. */
173 void
174 flow_loops_free (struct loops *loops)
176 if (loops->larray)
178 unsigned i;
179 loop_p loop;
181 /* Free the loop descriptors. */
182 for (i = 0; VEC_iterate (loop_p, loops->larray, i, loop); i++)
184 if (!loop)
185 continue;
187 flow_loop_free (loop);
190 VEC_free (loop_p, heap, loops->larray);
191 loops->larray = NULL;
195 /* Find the nodes contained within the LOOP with header HEADER.
196 Return the number of nodes within the loop. */
199 flow_loop_nodes_find (basic_block header, struct loop *loop)
201 basic_block *stack;
202 int sp;
203 int num_nodes = 1;
205 header->loop_father = loop;
206 header->loop_depth = loop->depth;
208 if (loop->latch->loop_father != loop)
210 stack = XNEWVEC (basic_block, n_basic_blocks);
211 sp = 0;
212 num_nodes++;
213 stack[sp++] = loop->latch;
214 loop->latch->loop_father = loop;
215 loop->latch->loop_depth = loop->depth;
217 while (sp)
219 basic_block node;
220 edge e;
221 edge_iterator ei;
223 node = stack[--sp];
225 FOR_EACH_EDGE (e, ei, node->preds)
227 basic_block ancestor = e->src;
229 if (ancestor != ENTRY_BLOCK_PTR
230 && ancestor->loop_father != loop)
232 ancestor->loop_father = loop;
233 ancestor->loop_depth = loop->depth;
234 num_nodes++;
235 stack[sp++] = ancestor;
239 free (stack);
241 return num_nodes;
244 static void
245 establish_preds (struct loop *loop)
247 struct loop *ploop, *father = loop->outer;
249 loop->depth = father->depth + 1;
251 /* Remember the current loop depth if it is the largest seen so far. */
252 cfun->max_loop_depth = MAX (cfun->max_loop_depth, loop->depth);
254 if (loop->pred)
255 free (loop->pred);
256 loop->pred = XNEWVEC (struct loop *, loop->depth);
257 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
258 loop->pred[father->depth] = father;
260 for (ploop = loop->inner; ploop; ploop = ploop->next)
261 establish_preds (ploop);
264 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
265 added loop. If LOOP has some children, take care of that their
266 pred field will be initialized correctly. */
268 void
269 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
271 loop->next = father->inner;
272 father->inner = loop;
273 loop->outer = father;
275 establish_preds (loop);
278 /* Remove LOOP from the loop hierarchy tree. */
280 void
281 flow_loop_tree_node_remove (struct loop *loop)
283 struct loop *prev, *father;
285 father = loop->outer;
286 loop->outer = NULL;
288 /* Remove loop from the list of sons. */
289 if (father->inner == loop)
290 father->inner = loop->next;
291 else
293 for (prev = father->inner; prev->next != loop; prev = prev->next);
294 prev->next = loop->next;
297 loop->depth = -1;
298 free (loop->pred);
299 loop->pred = NULL;
302 /* A callback to update latch and header info for basic block JUMP created
303 by redirecting an edge. */
305 static void
306 update_latch_info (basic_block jump)
308 alloc_aux_for_block (jump, sizeof (int));
309 HEADER_BLOCK (jump) = 0;
310 alloc_aux_for_edge (single_pred_edge (jump), sizeof (int));
311 LATCH_EDGE (single_pred_edge (jump)) = 0;
312 set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
315 /* A callback for make_forwarder block, to redirect all edges except for
316 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
317 whether to redirect it. */
319 static edge mfb_kj_edge;
320 static bool
321 mfb_keep_just (edge e)
323 return e != mfb_kj_edge;
326 /* A callback for make_forwarder block, to redirect the latch edges into an
327 entry part. E is the edge for that we should decide whether to redirect
328 it. */
330 static bool
331 mfb_keep_nonlatch (edge e)
333 return LATCH_EDGE (e);
336 /* Takes care of merging natural loops with shared headers. */
338 static void
339 canonicalize_loop_headers (void)
341 basic_block header;
342 edge e;
344 alloc_aux_for_blocks (sizeof (int));
345 alloc_aux_for_edges (sizeof (int));
347 /* Split blocks so that each loop has only single latch. */
348 FOR_EACH_BB (header)
350 edge_iterator ei;
351 int num_latches = 0;
352 int have_abnormal_edge = 0;
354 FOR_EACH_EDGE (e, ei, header->preds)
356 basic_block latch = e->src;
358 if (e->flags & EDGE_ABNORMAL)
359 have_abnormal_edge = 1;
361 if (latch != ENTRY_BLOCK_PTR
362 && dominated_by_p (CDI_DOMINATORS, latch, header))
364 num_latches++;
365 LATCH_EDGE (e) = 1;
368 if (have_abnormal_edge)
369 HEADER_BLOCK (header) = 0;
370 else
371 HEADER_BLOCK (header) = num_latches;
374 if (HEADER_BLOCK (single_succ (ENTRY_BLOCK_PTR)))
376 basic_block bb;
378 /* We could not redirect edges freely here. On the other hand,
379 we can simply split the edge from entry block. */
380 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
382 alloc_aux_for_edge (single_succ_edge (bb), sizeof (int));
383 LATCH_EDGE (single_succ_edge (bb)) = 0;
384 alloc_aux_for_block (bb, sizeof (int));
385 HEADER_BLOCK (bb) = 0;
388 FOR_EACH_BB (header)
390 int max_freq, is_heavy;
391 edge heavy, tmp_edge;
392 edge_iterator ei;
394 if (HEADER_BLOCK (header) <= 1)
395 continue;
397 /* Find a heavy edge. */
398 is_heavy = 1;
399 heavy = NULL;
400 max_freq = 0;
401 FOR_EACH_EDGE (e, ei, header->preds)
402 if (LATCH_EDGE (e) &&
403 EDGE_FREQUENCY (e) > max_freq)
404 max_freq = EDGE_FREQUENCY (e);
405 FOR_EACH_EDGE (e, ei, header->preds)
406 if (LATCH_EDGE (e) &&
407 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
409 if (heavy)
411 is_heavy = 0;
412 break;
414 else
415 heavy = e;
418 if (is_heavy)
420 /* Split out the heavy edge, and create inner loop for it. */
421 mfb_kj_edge = heavy;
422 tmp_edge = make_forwarder_block (header, mfb_keep_just,
423 update_latch_info);
424 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
425 HEADER_BLOCK (tmp_edge->dest) = 1;
426 alloc_aux_for_edge (tmp_edge, sizeof (int));
427 LATCH_EDGE (tmp_edge) = 0;
428 HEADER_BLOCK (header)--;
431 if (HEADER_BLOCK (header) > 1)
433 /* Create a new latch block. */
434 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
435 update_latch_info);
436 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
437 HEADER_BLOCK (tmp_edge->src) = 0;
438 HEADER_BLOCK (tmp_edge->dest) = 1;
439 alloc_aux_for_edge (tmp_edge, sizeof (int));
440 LATCH_EDGE (tmp_edge) = 1;
444 free_aux_for_blocks ();
445 free_aux_for_edges ();
447 #ifdef ENABLE_CHECKING
448 verify_dominators (CDI_DOMINATORS);
449 #endif
452 /* Allocates and returns new loop structure. */
454 struct loop *
455 alloc_loop (void)
457 struct loop *loop = XCNEW (struct loop);
459 loop->exits.next = loop->exits.prev = &loop->exits;
460 return loop;
463 /* Find all the natural loops in the function and save in LOOPS structure and
464 recalculate loop_depth information in basic block structures.
465 Return the number of natural loops found. */
468 flow_loops_find (struct loops *loops)
470 int b;
471 int num_loops;
472 edge e;
473 sbitmap headers;
474 int *dfs_order;
475 int *rc_order;
476 basic_block header;
477 basic_block bb;
478 struct loop *root;
480 memset (loops, 0, sizeof *loops);
482 /* We are going to recount the maximum loop depth,
483 so throw away the last count. */
484 cfun->max_loop_depth = 0;
486 /* Taking care of this degenerate case makes the rest of
487 this code simpler. */
488 if (n_basic_blocks == NUM_FIXED_BLOCKS)
489 return 0;
491 dfs_order = NULL;
492 rc_order = NULL;
494 /* Ensure that the dominators are computed. */
495 calculate_dominance_info (CDI_DOMINATORS);
497 /* Join loops with shared headers. */
498 canonicalize_loop_headers ();
500 /* Count the number of loop headers. This should be the
501 same as the number of natural loops. */
502 headers = sbitmap_alloc (last_basic_block);
503 sbitmap_zero (headers);
505 num_loops = 0;
506 FOR_EACH_BB (header)
508 edge_iterator ei;
509 int more_latches = 0;
511 header->loop_depth = 0;
513 /* If we have an abnormal predecessor, do not consider the
514 loop (not worth the problems). */
515 FOR_EACH_EDGE (e, ei, header->preds)
516 if (e->flags & EDGE_ABNORMAL)
517 break;
518 if (e)
519 continue;
521 FOR_EACH_EDGE (e, ei, header->preds)
523 basic_block latch = e->src;
525 gcc_assert (!(e->flags & EDGE_ABNORMAL));
527 /* Look for back edges where a predecessor is dominated
528 by this block. A natural loop has a single entry
529 node (header) that dominates all the nodes in the
530 loop. It also has single back edge to the header
531 from a latch node. */
532 if (latch != ENTRY_BLOCK_PTR
533 && dominated_by_p (CDI_DOMINATORS, latch, header))
535 /* Shared headers should be eliminated by now. */
536 gcc_assert (!more_latches);
537 more_latches = 1;
538 SET_BIT (headers, header->index);
539 num_loops++;
544 /* Allocate loop structures. */
545 loops->larray = VEC_alloc (loop_p, heap, num_loops + 1);
547 /* Dummy loop containing whole function. */
548 root = alloc_loop ();
549 root->num_nodes = n_basic_blocks;
550 root->latch = EXIT_BLOCK_PTR;
551 root->header = ENTRY_BLOCK_PTR;
552 ENTRY_BLOCK_PTR->loop_father = root;
553 EXIT_BLOCK_PTR->loop_father = root;
555 VEC_quick_push (loop_p, loops->larray, root);
556 loops->tree_root = root;
558 /* Find and record information about all the natural loops
559 in the CFG. */
560 FOR_EACH_BB (bb)
561 bb->loop_father = loops->tree_root;
563 if (num_loops)
565 /* Compute depth first search order of the CFG so that outer
566 natural loops will be found before inner natural loops. */
567 dfs_order = XNEWVEC (int, n_basic_blocks);
568 rc_order = XNEWVEC (int, n_basic_blocks);
569 pre_and_rev_post_order_compute (dfs_order, rc_order, false);
571 num_loops = 1;
573 for (b = 0; b < n_basic_blocks - NUM_FIXED_BLOCKS; b++)
575 struct loop *loop;
576 edge_iterator ei;
578 /* Search the nodes of the CFG in reverse completion order
579 so that we can find outer loops first. */
580 if (!TEST_BIT (headers, rc_order[b]))
581 continue;
583 header = BASIC_BLOCK (rc_order[b]);
585 loop = alloc_loop ();
586 VEC_quick_push (loop_p, loops->larray, loop);
588 loop->header = header;
589 loop->num = num_loops;
590 num_loops++;
592 /* Look for the latch for this header block. */
593 FOR_EACH_EDGE (e, ei, header->preds)
595 basic_block latch = e->src;
597 if (latch != ENTRY_BLOCK_PTR
598 && dominated_by_p (CDI_DOMINATORS, latch, header))
600 loop->latch = latch;
601 break;
605 flow_loop_tree_node_add (header->loop_father, loop);
606 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
609 free (dfs_order);
610 free (rc_order);
613 sbitmap_free (headers);
615 loops->exits = NULL;
616 loops->state = 0;
617 return VEC_length (loop_p, loops->larray);
620 /* Return nonzero if basic block BB belongs to LOOP. */
621 bool
622 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
624 struct loop *source_loop;
626 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
627 return 0;
629 source_loop = bb->loop_father;
630 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
633 /* Enumeration predicate for get_loop_body. */
634 static bool
635 glb_enum_p (basic_block bb, void *glb_header)
637 return bb != (basic_block) glb_header;
640 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
641 order against direction of edges from latch. Specially, if
642 header != latch, latch is the 1-st block. */
643 basic_block *
644 get_loop_body (const struct loop *loop)
646 basic_block *tovisit, bb;
647 unsigned tv = 0;
649 gcc_assert (loop->num_nodes);
651 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
652 tovisit[tv++] = loop->header;
654 if (loop->latch == EXIT_BLOCK_PTR)
656 /* There may be blocks unreachable from EXIT_BLOCK. */
657 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks);
658 FOR_EACH_BB (bb)
659 tovisit[tv++] = bb;
660 tovisit[tv++] = EXIT_BLOCK_PTR;
662 else if (loop->latch != loop->header)
664 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
665 tovisit + 1, loop->num_nodes - 1,
666 loop->header) + 1;
669 gcc_assert (tv == loop->num_nodes);
670 return tovisit;
673 /* Fills dominance descendants inside LOOP of the basic block BB into
674 array TOVISIT from index *TV. */
676 static void
677 fill_sons_in_loop (const struct loop *loop, basic_block bb,
678 basic_block *tovisit, int *tv)
680 basic_block son, postpone = NULL;
682 tovisit[(*tv)++] = bb;
683 for (son = first_dom_son (CDI_DOMINATORS, bb);
684 son;
685 son = next_dom_son (CDI_DOMINATORS, son))
687 if (!flow_bb_inside_loop_p (loop, son))
688 continue;
690 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
692 postpone = son;
693 continue;
695 fill_sons_in_loop (loop, son, tovisit, tv);
698 if (postpone)
699 fill_sons_in_loop (loop, postpone, tovisit, tv);
702 /* Gets body of a LOOP (that must be different from the outermost loop)
703 sorted by dominance relation. Additionally, if a basic block s dominates
704 the latch, then only blocks dominated by s are be after it. */
706 basic_block *
707 get_loop_body_in_dom_order (const struct loop *loop)
709 basic_block *tovisit;
710 int tv;
712 gcc_assert (loop->num_nodes);
714 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
716 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
718 tv = 0;
719 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
721 gcc_assert (tv == (int) loop->num_nodes);
723 return tovisit;
726 /* Get body of a LOOP in breadth first sort order. */
728 basic_block *
729 get_loop_body_in_bfs_order (const struct loop *loop)
731 basic_block *blocks;
732 basic_block bb;
733 bitmap visited;
734 unsigned int i = 0;
735 unsigned int vc = 1;
737 gcc_assert (loop->num_nodes);
738 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
740 blocks = XCNEWVEC (basic_block, loop->num_nodes);
741 visited = BITMAP_ALLOC (NULL);
743 bb = loop->header;
744 while (i < loop->num_nodes)
746 edge e;
747 edge_iterator ei;
749 if (!bitmap_bit_p (visited, bb->index))
751 /* This basic block is now visited */
752 bitmap_set_bit (visited, bb->index);
753 blocks[i++] = bb;
756 FOR_EACH_EDGE (e, ei, bb->succs)
758 if (flow_bb_inside_loop_p (loop, e->dest))
760 if (!bitmap_bit_p (visited, e->dest->index))
762 bitmap_set_bit (visited, e->dest->index);
763 blocks[i++] = e->dest;
768 gcc_assert (i >= vc);
770 bb = blocks[vc++];
773 BITMAP_FREE (visited);
774 return blocks;
777 /* Hash function for struct loop_exit. */
779 static hashval_t
780 loop_exit_hash (const void *ex)
782 struct loop_exit *exit = (struct loop_exit *) ex;
784 return htab_hash_pointer (exit->e);
787 /* Equality function for struct loop_exit. Compares with edge. */
789 static int
790 loop_exit_eq (const void *ex, const void *e)
792 struct loop_exit *exit = (struct loop_exit *) ex;
794 return exit->e == e;
797 /* Frees the list of loop exit descriptions EX. */
799 static void
800 loop_exit_free (void *ex)
802 struct loop_exit *exit = (struct loop_exit *) ex, *next;
804 for (; exit; exit = next)
806 next = exit->next_e;
808 exit->next->prev = exit->prev;
809 exit->prev->next = exit->next;
811 free (exit);
815 /* Returns the list of records for E as an exit of a loop. */
817 static struct loop_exit *
818 get_exit_descriptions (edge e)
820 return htab_find_with_hash (current_loops->exits, e,
821 htab_hash_pointer (e));
824 /* Updates the lists of loop exits in that E appears.
825 If REMOVED is true, E is being removed, and we
826 just remove it from the lists of exits.
827 If NEW_EDGE is true and E is not a loop exit, we
828 do not try to remove it from loop exit lists. */
830 void
831 rescan_loop_exit (edge e, bool new_edge, bool removed)
833 void **slot;
834 struct loop_exit *exits = NULL, *exit;
835 struct loop *aloop, *cloop;
837 if ((current_loops->state & LOOPS_HAVE_RECORDED_EXITS) == 0)
838 return;
840 if (!removed
841 && e->src->loop_father != NULL
842 && e->dest->loop_father != NULL
843 && !flow_bb_inside_loop_p (e->src->loop_father, e->dest))
845 cloop = find_common_loop (e->src->loop_father, e->dest->loop_father);
846 for (aloop = e->src->loop_father;
847 aloop != cloop;
848 aloop = aloop->outer)
850 exit = XNEW (struct loop_exit);
851 exit->e = e;
853 exit->next = aloop->exits.next;
854 exit->prev = &aloop->exits;
855 exit->next->prev = exit;
856 exit->prev->next = exit;
858 exit->next_e = exits;
859 exits = exit;
863 if (!exits && new_edge)
864 return;
866 slot = htab_find_slot_with_hash (current_loops->exits, e,
867 htab_hash_pointer (e),
868 exits ? INSERT : NO_INSERT);
869 if (!slot)
870 return;
872 if (exits)
874 if (*slot)
875 loop_exit_free (*slot);
876 *slot = exits;
878 else
879 htab_clear_slot (current_loops->exits, slot);
882 /* For each loop, record list of exit edges, and start maintaining these
883 lists. */
885 void
886 record_loop_exits (void)
888 basic_block bb;
889 edge_iterator ei;
890 edge e;
892 if (current_loops->state & LOOPS_HAVE_RECORDED_EXITS)
893 return;
894 current_loops->state |= LOOPS_HAVE_RECORDED_EXITS;
896 gcc_assert (current_loops->exits == NULL);
897 current_loops->exits = htab_create (2 * number_of_loops (),
898 loop_exit_hash,
899 loop_exit_eq,
900 loop_exit_free);
902 FOR_EACH_BB (bb)
904 FOR_EACH_EDGE (e, ei, bb->succs)
906 rescan_loop_exit (e, true, false);
911 /* Dumps information about the exit in *SLOT to FILE.
912 Callback for htab_traverse. */
914 static int
915 dump_recorded_exit (void **slot, void *file)
917 struct loop_exit *exit = *slot;
918 unsigned n = 0;
919 edge e = exit->e;
921 for (; exit != NULL; exit = exit->next_e)
922 n++;
924 fprintf (file, "Edge %d->%d exits %u loops\n",
925 e->src->index, e->dest->index, n);
927 return 1;
930 /* Dumps the recorded exits of loops to FILE. */
932 extern void dump_recorded_exits (FILE *);
933 void
934 dump_recorded_exits (FILE *file)
936 if (!current_loops->exits)
937 return;
938 htab_traverse (current_loops->exits, dump_recorded_exit, file);
941 /* Releases lists of loop exits. */
943 void
944 release_recorded_exits (void)
946 gcc_assert (current_loops->state & LOOPS_HAVE_RECORDED_EXITS);
947 htab_delete (current_loops->exits);
948 current_loops->exits = NULL;
949 current_loops->state &= ~LOOPS_HAVE_RECORDED_EXITS;
952 /* Returns the list of the exit edges of a LOOP. */
954 VEC (edge, heap) *
955 get_loop_exit_edges (const struct loop *loop)
957 VEC (edge, heap) *edges = NULL;
958 edge e;
959 unsigned i;
960 basic_block *body;
961 edge_iterator ei;
962 struct loop_exit *exit;
964 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
966 /* If we maintain the lists of exits, use them. Otherwise we must
967 scan the body of the loop. */
968 if (current_loops->state & LOOPS_HAVE_RECORDED_EXITS)
970 for (exit = loop->exits.next; exit->e; exit = exit->next)
971 VEC_safe_push (edge, heap, edges, exit->e);
973 else
975 body = get_loop_body (loop);
976 for (i = 0; i < loop->num_nodes; i++)
977 FOR_EACH_EDGE (e, ei, body[i]->succs)
979 if (!flow_bb_inside_loop_p (loop, e->dest))
980 VEC_safe_push (edge, heap, edges, e);
982 free (body);
985 return edges;
988 /* Counts the number of conditional branches inside LOOP. */
990 unsigned
991 num_loop_branches (const struct loop *loop)
993 unsigned i, n;
994 basic_block * body;
996 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
998 body = get_loop_body (loop);
999 n = 0;
1000 for (i = 0; i < loop->num_nodes; i++)
1001 if (EDGE_COUNT (body[i]->succs) >= 2)
1002 n++;
1003 free (body);
1005 return n;
1008 /* Adds basic block BB to LOOP. */
1009 void
1010 add_bb_to_loop (basic_block bb, struct loop *loop)
1012 int i;
1013 edge_iterator ei;
1014 edge e;
1016 gcc_assert (bb->loop_father == NULL);
1017 bb->loop_father = loop;
1018 bb->loop_depth = loop->depth;
1019 loop->num_nodes++;
1020 for (i = 0; i < loop->depth; i++)
1021 loop->pred[i]->num_nodes++;
1023 FOR_EACH_EDGE (e, ei, bb->succs)
1025 rescan_loop_exit (e, true, false);
1027 FOR_EACH_EDGE (e, ei, bb->preds)
1029 rescan_loop_exit (e, true, false);
1033 /* Remove basic block BB from loops. */
1034 void
1035 remove_bb_from_loops (basic_block bb)
1037 int i;
1038 struct loop *loop = bb->loop_father;
1039 edge_iterator ei;
1040 edge e;
1042 gcc_assert (loop != NULL);
1043 loop->num_nodes--;
1044 for (i = 0; i < loop->depth; i++)
1045 loop->pred[i]->num_nodes--;
1046 bb->loop_father = NULL;
1047 bb->loop_depth = 0;
1049 FOR_EACH_EDGE (e, ei, bb->succs)
1051 rescan_loop_exit (e, false, true);
1053 FOR_EACH_EDGE (e, ei, bb->preds)
1055 rescan_loop_exit (e, false, true);
1059 /* Finds nearest common ancestor in loop tree for given loops. */
1060 struct loop *
1061 find_common_loop (struct loop *loop_s, struct loop *loop_d)
1063 if (!loop_s) return loop_d;
1064 if (!loop_d) return loop_s;
1066 if (loop_s->depth < loop_d->depth)
1067 loop_d = loop_d->pred[loop_s->depth];
1068 else if (loop_s->depth > loop_d->depth)
1069 loop_s = loop_s->pred[loop_d->depth];
1071 while (loop_s != loop_d)
1073 loop_s = loop_s->outer;
1074 loop_d = loop_d->outer;
1076 return loop_s;
1079 /* Removes LOOP from structures and frees its data. */
1081 void
1082 delete_loop (struct loop *loop)
1084 /* Remove the loop from structure. */
1085 flow_loop_tree_node_remove (loop);
1087 /* Remove loop from loops array. */
1088 VEC_replace (loop_p, current_loops->larray, loop->num, NULL);
1090 /* Free loop data. */
1091 flow_loop_free (loop);
1094 /* Cancels the LOOP; it must be innermost one. */
1096 static void
1097 cancel_loop (struct loop *loop)
1099 basic_block *bbs;
1100 unsigned i;
1102 gcc_assert (!loop->inner);
1104 /* Move blocks up one level (they should be removed as soon as possible). */
1105 bbs = get_loop_body (loop);
1106 for (i = 0; i < loop->num_nodes; i++)
1107 bbs[i]->loop_father = loop->outer;
1109 delete_loop (loop);
1112 /* Cancels LOOP and all its subloops. */
1113 void
1114 cancel_loop_tree (struct loop *loop)
1116 while (loop->inner)
1117 cancel_loop_tree (loop->inner);
1118 cancel_loop (loop);
1121 /* Checks that information about loops is correct
1122 -- sizes of loops are all right
1123 -- results of get_loop_body really belong to the loop
1124 -- loop header have just single entry edge and single latch edge
1125 -- loop latches have only single successor that is header of their loop
1126 -- irreducible loops are correctly marked
1128 void
1129 verify_loop_structure (void)
1131 unsigned *sizes, i, j;
1132 sbitmap irreds;
1133 basic_block *bbs, bb;
1134 struct loop *loop;
1135 int err = 0;
1136 edge e;
1137 unsigned num = number_of_loops ();
1138 loop_iterator li;
1139 struct loop_exit *exit, *mexit;
1141 /* Check sizes. */
1142 sizes = XCNEWVEC (unsigned, num);
1143 sizes[0] = 2;
1145 FOR_EACH_BB (bb)
1146 for (loop = bb->loop_father; loop; loop = loop->outer)
1147 sizes[loop->num]++;
1149 FOR_EACH_LOOP (li, loop, LI_INCLUDE_ROOT)
1151 i = loop->num;
1153 if (loop->num_nodes != sizes[i])
1155 error ("size of loop %d should be %d, not %d",
1156 i, sizes[i], loop->num_nodes);
1157 err = 1;
1161 /* Check get_loop_body. */
1162 FOR_EACH_LOOP (li, loop, 0)
1164 bbs = get_loop_body (loop);
1166 for (j = 0; j < loop->num_nodes; j++)
1167 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1169 error ("bb %d do not belong to loop %d",
1170 bbs[j]->index, loop->num);
1171 err = 1;
1173 free (bbs);
1176 /* Check headers and latches. */
1177 FOR_EACH_LOOP (li, loop, 0)
1179 i = loop->num;
1181 if ((current_loops->state & LOOPS_HAVE_PREHEADERS)
1182 && EDGE_COUNT (loop->header->preds) != 2)
1184 error ("loop %d's header does not have exactly 2 entries", i);
1185 err = 1;
1187 if (current_loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1189 if (!single_succ_p (loop->latch))
1191 error ("loop %d's latch does not have exactly 1 successor", i);
1192 err = 1;
1194 if (single_succ (loop->latch) != loop->header)
1196 error ("loop %d's latch does not have header as successor", i);
1197 err = 1;
1199 if (loop->latch->loop_father != loop)
1201 error ("loop %d's latch does not belong directly to it", i);
1202 err = 1;
1205 if (loop->header->loop_father != loop)
1207 error ("loop %d's header does not belong directly to it", i);
1208 err = 1;
1210 if ((current_loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1211 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1213 error ("loop %d's latch is marked as part of irreducible region", i);
1214 err = 1;
1218 /* Check irreducible loops. */
1219 if (current_loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1221 /* Record old info. */
1222 irreds = sbitmap_alloc (last_basic_block);
1223 FOR_EACH_BB (bb)
1225 edge_iterator ei;
1226 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1227 SET_BIT (irreds, bb->index);
1228 else
1229 RESET_BIT (irreds, bb->index);
1230 FOR_EACH_EDGE (e, ei, bb->succs)
1231 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1232 e->flags |= EDGE_ALL_FLAGS + 1;
1235 /* Recount it. */
1236 mark_irreducible_loops ();
1238 /* Compare. */
1239 FOR_EACH_BB (bb)
1241 edge_iterator ei;
1243 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1244 && !TEST_BIT (irreds, bb->index))
1246 error ("basic block %d should be marked irreducible", bb->index);
1247 err = 1;
1249 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1250 && TEST_BIT (irreds, bb->index))
1252 error ("basic block %d should not be marked irreducible", bb->index);
1253 err = 1;
1255 FOR_EACH_EDGE (e, ei, bb->succs)
1257 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1258 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1260 error ("edge from %d to %d should be marked irreducible",
1261 e->src->index, e->dest->index);
1262 err = 1;
1264 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1265 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1267 error ("edge from %d to %d should not be marked irreducible",
1268 e->src->index, e->dest->index);
1269 err = 1;
1271 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1274 free (irreds);
1277 /* Check the recorded loop exits. */
1278 FOR_EACH_LOOP (li, loop, 0)
1280 if (loop->exits.e != NULL)
1282 error ("corrupted head of the exits list of loop %d",
1283 loop->num);
1284 err = 1;
1286 else
1288 /* Check that the list forms a cycle, and all elements except
1289 for the head are nonnull. */
1290 for (mexit = &loop->exits, exit = mexit->next, i = 0;
1291 exit->e && exit != mexit;
1292 exit = exit->next)
1294 if (i++ & 1)
1295 mexit = mexit->next;
1298 if (exit != &loop->exits)
1300 error ("corrupted exits list of loop %d", loop->num);
1301 err = 1;
1305 if ((current_loops->state & LOOPS_HAVE_RECORDED_EXITS) == 0)
1307 if (loop->exits.next != &loop->exits)
1309 error ("nonempty exits list of loop %d, but exits are not recorded",
1310 loop->num);
1311 err = 1;
1316 if (current_loops->state & LOOPS_HAVE_RECORDED_EXITS)
1318 unsigned n_exits = 0, eloops;
1320 memset (sizes, 0, sizeof (unsigned) * num);
1321 FOR_EACH_BB (bb)
1323 edge_iterator ei;
1324 if (bb->loop_father == current_loops->tree_root)
1325 continue;
1326 FOR_EACH_EDGE (e, ei, bb->succs)
1328 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
1329 continue;
1331 n_exits++;
1332 exit = get_exit_descriptions (e);
1333 if (!exit)
1335 error ("Exit %d->%d not recorded",
1336 e->src->index, e->dest->index);
1337 err = 1;
1339 eloops = 0;
1340 for (; exit; exit = exit->next_e)
1341 eloops++;
1343 for (loop = bb->loop_father;
1344 loop != e->dest->loop_father;
1345 loop = loop->outer)
1347 eloops--;
1348 sizes[loop->num]++;
1351 if (eloops != 0)
1353 error ("Wrong list of exited loops for edge %d->%d",
1354 e->src->index, e->dest->index);
1355 err = 1;
1360 if (n_exits != htab_elements (current_loops->exits))
1362 error ("Too many loop exits recorded");
1363 err = 1;
1366 FOR_EACH_LOOP (li, loop, 0)
1368 eloops = 0;
1369 for (exit = loop->exits.next; exit->e; exit = exit->next)
1370 eloops++;
1371 if (eloops != sizes[loop->num])
1373 error ("%d exits recorded for loop %d (having %d exits)",
1374 eloops, loop->num, sizes[loop->num]);
1375 err = 1;
1380 gcc_assert (!err);
1382 free (sizes);
1385 /* Returns latch edge of LOOP. */
1386 edge
1387 loop_latch_edge (const struct loop *loop)
1389 return find_edge (loop->latch, loop->header);
1392 /* Returns preheader edge of LOOP. */
1393 edge
1394 loop_preheader_edge (const struct loop *loop)
1396 edge e;
1397 edge_iterator ei;
1399 FOR_EACH_EDGE (e, ei, loop->header->preds)
1400 if (e->src != loop->latch)
1401 break;
1403 return e;
1406 /* Returns true if E is an exit of LOOP. */
1408 bool
1409 loop_exit_edge_p (const struct loop *loop, edge e)
1411 return (flow_bb_inside_loop_p (loop, e->src)
1412 && !flow_bb_inside_loop_p (loop, e->dest));
1415 /* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
1416 or more than one exit. If loops do not have the exits recorded, NULL
1417 is returned always. */
1419 edge
1420 single_exit (const struct loop *loop)
1422 struct loop_exit *exit = loop->exits.next;
1424 if ((current_loops->state & LOOPS_HAVE_RECORDED_EXITS) == 0)
1425 return NULL;
1427 if (exit->e && exit->next == &loop->exits)
1428 return exit->e;
1429 else
1430 return NULL;