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
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
29 #include "basic-block.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_nodes_find (basic_block, struct loop *);
45 static int flow_loop_level_compute (struct loop *);
46 static void flow_loops_level_compute (struct loops *);
47 static void establish_preds (struct loop *);
48 static void canonicalize_loop_headers (void);
49 static bool glb_enum_p (basic_block, void *);
51 /* Dump loop related CFG information. */
54 flow_loops_cfg_dump (const struct loops *loops, FILE *file)
59 if (! loops->num || ! file)
67 fprintf (file, ";; %d succs { ", bb->index);
68 FOR_EACH_EDGE (succ, ei, bb->succs)
69 fprintf (file, "%d ", succ->dest->index);
70 fprintf (file, "}\n");
73 /* Dump the DFS node order. */
74 if (loops->cfg.dfs_order)
76 fputs (";; DFS order: ", file);
77 for (i = 0; i < n_basic_blocks; i++)
78 fprintf (file, "%d ", loops->cfg.dfs_order[i]);
83 /* Dump the reverse completion node order. */
84 if (loops->cfg.rc_order)
86 fputs (";; RC order: ", file);
87 for (i = 0; i < n_basic_blocks; i++)
88 fprintf (file, "%d ", loops->cfg.rc_order[i]);
94 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
97 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
99 return (loop->depth > outer->depth
100 && loop->pred[outer->depth] == outer);
103 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
104 loops within LOOP. */
107 superloop_at_depth (struct loop *loop, unsigned depth)
109 gcc_assert (depth <= (unsigned) loop->depth);
111 if (depth == (unsigned) loop->depth)
114 return loop->pred[depth];
117 /* Dump the loop information specified by LOOP to the stream FILE
118 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
121 flow_loop_dump (const struct loop *loop, FILE *file,
122 void (*loop_dump_aux) (const struct loop *, FILE *, int),
128 if (! loop || ! loop->header)
131 fprintf (file, ";;\n;; Loop %d:%s\n", loop->num,
132 loop->invalid ? " invalid" : "");
134 fprintf (file, ";; header %d, latch %d\n",
135 loop->header->index, loop->latch->index);
136 fprintf (file, ";; depth %d, level %d, outer %ld\n",
137 loop->depth, loop->level,
138 (long) (loop->outer ? loop->outer->num : -1));
140 fprintf (file, ";; nodes:");
141 bbs = get_loop_body (loop);
142 for (i = 0; i < loop->num_nodes; i++)
143 fprintf (file, " %d", bbs[i]->index);
145 fprintf (file, "\n");
148 loop_dump_aux (loop, file, verbose);
151 /* Dump the loop information specified by LOOPS to the stream FILE,
152 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
155 flow_loops_dump (const struct loops *loops, FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
160 num_loops = loops->num;
161 if (! num_loops || ! file)
164 fprintf (file, ";; %d loops found\n", num_loops);
166 for (i = 0; i < num_loops; i++)
168 struct loop *loop = loops->parray[i];
173 flow_loop_dump (loop, file, loop_dump_aux, verbose);
177 flow_loops_cfg_dump (loops, file);
180 /* Free data allocated for LOOP. */
182 flow_loop_free (struct loop *loop)
189 /* Free all the memory allocated for LOOPS. */
192 flow_loops_free (struct loops *loops)
198 gcc_assert (loops->num);
200 /* Free the loop descriptors. */
201 for (i = 0; i < loops->num; i++)
203 struct loop *loop = loops->parray[i];
208 flow_loop_free (loop);
211 free (loops->parray);
212 loops->parray = NULL;
214 if (loops->cfg.dfs_order)
215 free (loops->cfg.dfs_order);
216 if (loops->cfg.rc_order)
217 free (loops->cfg.rc_order);
222 /* Find the nodes contained within the LOOP with header HEADER.
223 Return the number of nodes within the loop. */
226 flow_loop_nodes_find (basic_block header, struct loop *loop)
232 header->loop_father = loop;
233 header->loop_depth = loop->depth;
235 if (loop->latch->loop_father != loop)
237 stack = xmalloc (n_basic_blocks * sizeof (basic_block));
240 stack[sp++] = loop->latch;
241 loop->latch->loop_father = loop;
242 loop->latch->loop_depth = loop->depth;
252 FOR_EACH_EDGE (e, ei, node->preds)
254 basic_block ancestor = e->src;
256 if (ancestor != ENTRY_BLOCK_PTR
257 && ancestor->loop_father != loop)
259 ancestor->loop_father = loop;
260 ancestor->loop_depth = loop->depth;
262 stack[sp++] = ancestor;
271 /* For each loop in the lOOPS tree that has just a single exit
272 record the exit edge. */
275 mark_single_exit_loops (struct loops *loops)
282 for (i = 1; i < loops->num; i++)
284 loop = loops->parray[i];
286 loop->single_exit = NULL;
292 if (bb->loop_father == loops->tree_root)
294 FOR_EACH_EDGE (e, ei, bb->succs)
296 if (e->dest == EXIT_BLOCK_PTR)
299 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
302 for (loop = bb->loop_father;
303 loop != e->dest->loop_father;
306 /* If we have already seen an exit, mark this by the edge that
307 surely does not occur as any exit. */
308 if (loop->single_exit)
309 loop->single_exit = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
311 loop->single_exit = e;
316 for (i = 1; i < loops->num; i++)
318 loop = loops->parray[i];
322 if (loop->single_exit == EDGE_SUCC (ENTRY_BLOCK_PTR, 0))
323 loop->single_exit = NULL;
326 loops->state |= LOOPS_HAVE_MARKED_SINGLE_EXITS;
330 establish_preds (struct loop *loop)
332 struct loop *ploop, *father = loop->outer;
334 loop->depth = father->depth + 1;
336 /* Remember the current loop depth if it is the largest seen so far. */
337 cfun->max_loop_depth = MAX (cfun->max_loop_depth, loop->depth);
341 loop->pred = xmalloc (sizeof (struct loop *) * loop->depth);
342 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
343 loop->pred[father->depth] = father;
345 for (ploop = loop->inner; ploop; ploop = ploop->next)
346 establish_preds (ploop);
349 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
350 added loop. If LOOP has some children, take care of that their
351 pred field will be initialized correctly. */
354 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
356 loop->next = father->inner;
357 father->inner = loop;
358 loop->outer = father;
360 establish_preds (loop);
363 /* Remove LOOP from the loop hierarchy tree. */
366 flow_loop_tree_node_remove (struct loop *loop)
368 struct loop *prev, *father;
370 father = loop->outer;
373 /* Remove loop from the list of sons. */
374 if (father->inner == loop)
375 father->inner = loop->next;
378 for (prev = father->inner; prev->next != loop; prev = prev->next);
379 prev->next = loop->next;
387 /* Helper function to compute loop nesting depth and enclosed loop level
388 for the natural loop specified by LOOP. Returns the loop level. */
391 flow_loop_level_compute (struct loop *loop)
399 /* Traverse loop tree assigning depth and computing level as the
400 maximum level of all the inner loops of this loop. The loop
401 level is equivalent to the height of the loop in the loop tree
402 and corresponds to the number of enclosed loop levels (including
404 for (inner = loop->inner; inner; inner = inner->next)
406 int ilevel = flow_loop_level_compute (inner) + 1;
416 /* Compute the loop nesting depth and enclosed loop level for the loop
417 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
421 flow_loops_level_compute (struct loops *loops)
423 flow_loop_level_compute (loops->tree_root);
426 /* A callback to update latch and header info for basic block JUMP created
427 by redirecting an edge. */
430 update_latch_info (basic_block jump)
432 alloc_aux_for_block (jump, sizeof (int));
433 HEADER_BLOCK (jump) = 0;
434 alloc_aux_for_edge (EDGE_PRED (jump, 0), sizeof (int));
435 LATCH_EDGE (EDGE_PRED (jump, 0)) = 0;
436 set_immediate_dominator (CDI_DOMINATORS, jump, EDGE_PRED (jump, 0)->src);
439 /* A callback for make_forwarder block, to redirect all edges except for
440 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
441 whether to redirect it. */
443 static edge mfb_kj_edge;
445 mfb_keep_just (edge e)
447 return e != mfb_kj_edge;
450 /* A callback for make_forwarder block, to redirect the latch edges into an
451 entry part. E is the edge for that we should decide whether to redirect
455 mfb_keep_nonlatch (edge e)
457 return LATCH_EDGE (e);
460 /* Takes care of merging natural loops with shared headers. */
463 canonicalize_loop_headers (void)
468 alloc_aux_for_blocks (sizeof (int));
469 alloc_aux_for_edges (sizeof (int));
471 /* Split blocks so that each loop has only single latch. */
476 int have_abnormal_edge = 0;
478 FOR_EACH_EDGE (e, ei, header->preds)
480 basic_block latch = e->src;
482 if (e->flags & EDGE_ABNORMAL)
483 have_abnormal_edge = 1;
485 if (latch != ENTRY_BLOCK_PTR
486 && dominated_by_p (CDI_DOMINATORS, latch, header))
492 if (have_abnormal_edge)
493 HEADER_BLOCK (header) = 0;
495 HEADER_BLOCK (header) = num_latches;
498 if (HEADER_BLOCK (EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest))
502 /* We could not redirect edges freely here. On the other hand,
503 we can simply split the edge from entry block. */
504 bb = split_edge (EDGE_SUCC (ENTRY_BLOCK_PTR, 0));
506 alloc_aux_for_edge (EDGE_SUCC (bb, 0), sizeof (int));
507 LATCH_EDGE (EDGE_SUCC (bb, 0)) = 0;
508 alloc_aux_for_block (bb, sizeof (int));
509 HEADER_BLOCK (bb) = 0;
514 int max_freq, is_heavy;
515 edge heavy, tmp_edge;
518 if (HEADER_BLOCK (header) <= 1)
521 /* Find a heavy edge. */
525 FOR_EACH_EDGE (e, ei, header->preds)
526 if (LATCH_EDGE (e) &&
527 EDGE_FREQUENCY (e) > max_freq)
528 max_freq = EDGE_FREQUENCY (e);
529 FOR_EACH_EDGE (e, ei, header->preds)
530 if (LATCH_EDGE (e) &&
531 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
544 /* Split out the heavy edge, and create inner loop for it. */
546 tmp_edge = make_forwarder_block (header, mfb_keep_just,
548 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
549 HEADER_BLOCK (tmp_edge->dest) = 1;
550 alloc_aux_for_edge (tmp_edge, sizeof (int));
551 LATCH_EDGE (tmp_edge) = 0;
552 HEADER_BLOCK (header)--;
555 if (HEADER_BLOCK (header) > 1)
557 /* Create a new latch block. */
558 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
560 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
561 HEADER_BLOCK (tmp_edge->src) = 0;
562 HEADER_BLOCK (tmp_edge->dest) = 1;
563 alloc_aux_for_edge (tmp_edge, sizeof (int));
564 LATCH_EDGE (tmp_edge) = 1;
568 free_aux_for_blocks ();
569 free_aux_for_edges ();
571 #ifdef ENABLE_CHECKING
572 verify_dominators (CDI_DOMINATORS);
576 /* Initialize all the parallel_p fields of the loops structure to true. */
579 initialize_loops_parallel_p (struct loops *loops)
583 for (i = 0; i < loops->num; i++)
585 struct loop *loop = loops->parray[i];
586 loop->parallel_p = true;
590 /* Find all the natural loops in the function and save in LOOPS structure and
591 recalculate loop_depth information in basic block structures.
592 Return the number of natural loops found. */
595 flow_loops_find (struct loops *loops)
606 memset (loops, 0, sizeof *loops);
608 /* We are going to recount the maximum loop depth,
609 so throw away the last count. */
610 cfun->max_loop_depth = 0;
612 /* Taking care of this degenerate case makes the rest of
613 this code simpler. */
614 if (n_basic_blocks == 0)
620 /* Ensure that the dominators are computed. */
621 calculate_dominance_info (CDI_DOMINATORS);
623 /* Join loops with shared headers. */
624 canonicalize_loop_headers ();
626 /* Count the number of loop headers. This should be the
627 same as the number of natural loops. */
628 headers = sbitmap_alloc (last_basic_block);
629 sbitmap_zero (headers);
635 int more_latches = 0;
637 header->loop_depth = 0;
639 /* If we have an abnormal predecessor, do not consider the
640 loop (not worth the problems). */
641 FOR_EACH_EDGE (e, ei, header->preds)
642 if (e->flags & EDGE_ABNORMAL)
647 FOR_EACH_EDGE (e, ei, header->preds)
649 basic_block latch = e->src;
651 gcc_assert (!(e->flags & EDGE_ABNORMAL));
653 /* Look for back edges where a predecessor is dominated
654 by this block. A natural loop has a single entry
655 node (header) that dominates all the nodes in the
656 loop. It also has single back edge to the header
657 from a latch node. */
658 if (latch != ENTRY_BLOCK_PTR
659 && dominated_by_p (CDI_DOMINATORS, latch, header))
661 /* Shared headers should be eliminated by now. */
662 gcc_assert (!more_latches);
664 SET_BIT (headers, header->index);
670 /* Allocate loop structures. */
671 loops->parray = xcalloc (num_loops + 1, sizeof (struct loop *));
673 /* Dummy loop containing whole function. */
674 loops->parray[0] = xcalloc (1, sizeof (struct loop));
675 loops->parray[0]->next = NULL;
676 loops->parray[0]->inner = NULL;
677 loops->parray[0]->outer = NULL;
678 loops->parray[0]->depth = 0;
679 loops->parray[0]->pred = NULL;
680 loops->parray[0]->num_nodes = n_basic_blocks + 2;
681 loops->parray[0]->latch = EXIT_BLOCK_PTR;
682 loops->parray[0]->header = ENTRY_BLOCK_PTR;
683 ENTRY_BLOCK_PTR->loop_father = loops->parray[0];
684 EXIT_BLOCK_PTR->loop_father = loops->parray[0];
686 loops->tree_root = loops->parray[0];
688 /* Find and record information about all the natural loops
692 bb->loop_father = loops->tree_root;
696 /* Compute depth first search order of the CFG so that outer
697 natural loops will be found before inner natural loops. */
698 dfs_order = xmalloc (n_basic_blocks * sizeof (int));
699 rc_order = xmalloc (n_basic_blocks * sizeof (int));
700 flow_depth_first_order_compute (dfs_order, rc_order);
702 /* Save CFG derived information to avoid recomputing it. */
703 loops->cfg.dfs_order = dfs_order;
704 loops->cfg.rc_order = rc_order;
708 for (b = 0; b < n_basic_blocks; b++)
713 /* Search the nodes of the CFG in reverse completion order
714 so that we can find outer loops first. */
715 if (!TEST_BIT (headers, rc_order[b]))
718 header = BASIC_BLOCK (rc_order[b]);
720 loop = loops->parray[num_loops] = xcalloc (1, sizeof (struct loop));
722 loop->header = header;
723 loop->num = num_loops;
726 /* Look for the latch for this header block. */
727 FOR_EACH_EDGE (e, ei, header->preds)
729 basic_block latch = e->src;
731 if (latch != ENTRY_BLOCK_PTR
732 && dominated_by_p (CDI_DOMINATORS, latch, header))
739 flow_loop_tree_node_add (header->loop_father, loop);
740 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
743 /* Assign the loop nesting depth and enclosed loop level for each
745 flow_loops_level_compute (loops);
747 loops->num = num_loops;
748 initialize_loops_parallel_p (loops);
751 sbitmap_free (headers);
754 #ifdef ENABLE_CHECKING
756 verify_loop_structure (loops);
762 /* Return nonzero if basic block BB belongs to LOOP. */
764 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
766 struct loop *source_loop;
768 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
771 source_loop = bb->loop_father;
772 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
775 /* Return nonzero if edge E enters header of LOOP from outside of LOOP. */
778 flow_loop_outside_edge_p (const struct loop *loop, edge e)
780 gcc_assert (e->dest == loop->header);
781 return !flow_bb_inside_loop_p (loop, e->src);
784 /* Enumeration predicate for get_loop_body. */
786 glb_enum_p (basic_block bb, void *glb_header)
788 return bb != (basic_block) glb_header;
791 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
792 order against direction of edges from latch. Specially, if
793 header != latch, latch is the 1-st block. */
795 get_loop_body (const struct loop *loop)
797 basic_block *tovisit, bb;
800 gcc_assert (loop->num_nodes);
802 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
803 tovisit[tv++] = loop->header;
805 if (loop->latch == EXIT_BLOCK_PTR)
807 /* There may be blocks unreachable from EXIT_BLOCK. */
808 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks + 2);
811 tovisit[tv++] = EXIT_BLOCK_PTR;
813 else if (loop->latch != loop->header)
815 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
816 tovisit + 1, loop->num_nodes - 1,
820 gcc_assert (tv == loop->num_nodes);
824 /* Fills dominance descendants inside LOOP of the basic block BB into
825 array TOVISIT from index *TV. */
828 fill_sons_in_loop (const struct loop *loop, basic_block bb,
829 basic_block *tovisit, int *tv)
831 basic_block son, postpone = NULL;
833 tovisit[(*tv)++] = bb;
834 for (son = first_dom_son (CDI_DOMINATORS, bb);
836 son = next_dom_son (CDI_DOMINATORS, son))
838 if (!flow_bb_inside_loop_p (loop, son))
841 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
846 fill_sons_in_loop (loop, son, tovisit, tv);
850 fill_sons_in_loop (loop, postpone, tovisit, tv);
853 /* Gets body of a LOOP (that must be different from the outermost loop)
854 sorted by dominance relation. Additionally, if a basic block s dominates
855 the latch, then only blocks dominated by s are be after it. */
858 get_loop_body_in_dom_order (const struct loop *loop)
860 basic_block *tovisit;
863 gcc_assert (loop->num_nodes);
865 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
867 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
870 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
872 gcc_assert (tv == (int) loop->num_nodes);
877 /* Get body of a LOOP in breadth first sort order. */
880 get_loop_body_in_bfs_order (const struct loop *loop)
888 gcc_assert (loop->num_nodes);
889 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
891 blocks = xcalloc (loop->num_nodes, sizeof (basic_block));
892 visited = BITMAP_ALLOC (NULL);
895 while (i < loop->num_nodes)
900 if (!bitmap_bit_p (visited, bb->index))
902 /* This basic block is now visited */
903 bitmap_set_bit (visited, bb->index);
907 FOR_EACH_EDGE (e, ei, bb->succs)
909 if (flow_bb_inside_loop_p (loop, e->dest))
911 if (!bitmap_bit_p (visited, e->dest->index))
913 bitmap_set_bit (visited, e->dest->index);
914 blocks[i++] = e->dest;
919 gcc_assert (i >= vc);
924 BITMAP_FREE (visited);
928 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
930 get_loop_exit_edges (const struct loop *loop, unsigned int *n_edges)
937 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
939 body = get_loop_body (loop);
941 for (i = 0; i < loop->num_nodes; i++)
942 FOR_EACH_EDGE (e, ei, body[i]->succs)
943 if (!flow_bb_inside_loop_p (loop, e->dest))
945 edges = xmalloc (n * sizeof (edge));
948 for (i = 0; i < loop->num_nodes; i++)
949 FOR_EACH_EDGE (e, ei, body[i]->succs)
950 if (!flow_bb_inside_loop_p (loop, e->dest))
957 /* Counts the number of conditional branches inside LOOP. */
960 num_loop_branches (const struct loop *loop)
965 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
967 body = get_loop_body (loop);
969 for (i = 0; i < loop->num_nodes; i++)
970 if (EDGE_COUNT (body[i]->succs) >= 2)
977 /* Adds basic block BB to LOOP. */
979 add_bb_to_loop (basic_block bb, struct loop *loop)
983 bb->loop_father = loop;
984 bb->loop_depth = loop->depth;
986 for (i = 0; i < loop->depth; i++)
987 loop->pred[i]->num_nodes++;
990 /* Remove basic block BB from loops. */
992 remove_bb_from_loops (basic_block bb)
995 struct loop *loop = bb->loop_father;
998 for (i = 0; i < loop->depth; i++)
999 loop->pred[i]->num_nodes--;
1000 bb->loop_father = NULL;
1004 /* Finds nearest common ancestor in loop tree for given loops. */
1006 find_common_loop (struct loop *loop_s, struct loop *loop_d)
1008 if (!loop_s) return loop_d;
1009 if (!loop_d) return loop_s;
1011 if (loop_s->depth < loop_d->depth)
1012 loop_d = loop_d->pred[loop_s->depth];
1013 else if (loop_s->depth > loop_d->depth)
1014 loop_s = loop_s->pred[loop_d->depth];
1016 while (loop_s != loop_d)
1018 loop_s = loop_s->outer;
1019 loop_d = loop_d->outer;
1024 /* Cancels the LOOP; it must be innermost one. */
1026 cancel_loop (struct loops *loops, struct loop *loop)
1031 gcc_assert (!loop->inner);
1033 /* Move blocks up one level (they should be removed as soon as possible). */
1034 bbs = get_loop_body (loop);
1035 for (i = 0; i < loop->num_nodes; i++)
1036 bbs[i]->loop_father = loop->outer;
1038 /* Remove the loop from structure. */
1039 flow_loop_tree_node_remove (loop);
1041 /* Remove loop from loops array. */
1042 loops->parray[loop->num] = NULL;
1044 /* Free loop data. */
1045 flow_loop_free (loop);
1048 /* Cancels LOOP and all its subloops. */
1050 cancel_loop_tree (struct loops *loops, struct loop *loop)
1053 cancel_loop_tree (loops, loop->inner);
1054 cancel_loop (loops, loop);
1057 /* Checks that LOOPS are all right:
1058 -- sizes of loops are all right
1059 -- results of get_loop_body really belong to the loop
1060 -- loop header have just single entry edge and single latch edge
1061 -- loop latches have only single successor that is header of their loop
1062 -- irreducible loops are correctly marked
1065 verify_loop_structure (struct loops *loops)
1067 unsigned *sizes, i, j;
1069 basic_block *bbs, bb;
1075 sizes = xcalloc (loops->num, sizeof (int));
1079 for (loop = bb->loop_father; loop; loop = loop->outer)
1082 for (i = 0; i < loops->num; i++)
1084 if (!loops->parray[i])
1087 if (loops->parray[i]->num_nodes != sizes[i])
1089 error ("Size of loop %d should be %d, not %d.",
1090 i, sizes[i], loops->parray[i]->num_nodes);
1095 /* Check get_loop_body. */
1096 for (i = 1; i < loops->num; i++)
1098 loop = loops->parray[i];
1101 bbs = get_loop_body (loop);
1103 for (j = 0; j < loop->num_nodes; j++)
1104 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1106 error ("Bb %d do not belong to loop %d.",
1113 /* Check headers and latches. */
1114 for (i = 1; i < loops->num; i++)
1116 loop = loops->parray[i];
1120 if ((loops->state & LOOPS_HAVE_PREHEADERS)
1121 && EDGE_COUNT (loop->header->preds) != 2)
1123 error ("Loop %d's header does not have exactly 2 entries.", i);
1126 if (loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1128 if (EDGE_COUNT (loop->latch->succs) != 1)
1130 error ("Loop %d's latch does not have exactly 1 successor.", i);
1133 if (EDGE_SUCC (loop->latch, 0)->dest != loop->header)
1135 error ("Loop %d's latch does not have header as successor.", i);
1138 if (loop->latch->loop_father != loop)
1140 error ("Loop %d's latch does not belong directly to it.", i);
1144 if (loop->header->loop_father != loop)
1146 error ("Loop %d's header does not belong directly to it.", i);
1149 if ((loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1150 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1152 error ("Loop %d's latch is marked as part of irreducible region.", i);
1157 /* Check irreducible loops. */
1158 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1160 /* Record old info. */
1161 irreds = sbitmap_alloc (last_basic_block);
1165 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1166 SET_BIT (irreds, bb->index);
1168 RESET_BIT (irreds, bb->index);
1169 FOR_EACH_EDGE (e, ei, bb->succs)
1170 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1171 e->flags |= EDGE_ALL_FLAGS + 1;
1175 mark_irreducible_loops (loops);
1182 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1183 && !TEST_BIT (irreds, bb->index))
1185 error ("Basic block %d should be marked irreducible.", bb->index);
1188 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1189 && TEST_BIT (irreds, bb->index))
1191 error ("Basic block %d should not be marked irreducible.", bb->index);
1194 FOR_EACH_EDGE (e, ei, bb->succs)
1196 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1197 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1199 error ("Edge from %d to %d should be marked irreducible.",
1200 e->src->index, e->dest->index);
1203 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1204 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1206 error ("Edge from %d to %d should not be marked irreducible.",
1207 e->src->index, e->dest->index);
1210 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1216 /* Check the single_exit. */
1217 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
1219 memset (sizes, 0, sizeof (unsigned) * loops->num);
1223 if (bb->loop_father == loops->tree_root)
1225 FOR_EACH_EDGE (e, ei, bb->succs)
1227 if (e->dest == EXIT_BLOCK_PTR)
1230 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
1233 for (loop = bb->loop_father;
1234 loop != e->dest->loop_father;
1238 if (loop->single_exit
1239 && loop->single_exit != e)
1241 error ("Wrong single exit %d->%d recorded for loop %d.",
1242 loop->single_exit->src->index,
1243 loop->single_exit->dest->index,
1245 error ("Right exit is %d->%d.",
1246 e->src->index, e->dest->index);
1253 for (i = 1; i < loops->num; i++)
1255 loop = loops->parray[i];
1260 && !loop->single_exit)
1262 error ("Single exit not recorded for loop %d.", loop->num);
1267 && loop->single_exit)
1269 error ("Loop %d should not have single exit (%d -> %d).",
1271 loop->single_exit->src->index,
1272 loop->single_exit->dest->index);
1283 /* Returns latch edge of LOOP. */
1285 loop_latch_edge (const struct loop *loop)
1287 return find_edge (loop->latch, loop->header);
1290 /* Returns preheader edge of LOOP. */
1292 loop_preheader_edge (const struct loop *loop)
1297 FOR_EACH_EDGE (e, ei, loop->header->preds)
1298 if (e->src != loop->latch)
1304 /* Returns true if E is an exit of LOOP. */
1307 loop_exit_edge_p (const struct loop *loop, edge e)
1309 return (flow_bb_inside_loop_p (loop, e->src)
1310 && !flow_bb_inside_loop_p (loop, e->dest));