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, 51 Franklin Street, Fifth Floor, 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_level_compute (struct loop *);
45 static void flow_loops_level_compute (struct loops *);
46 static void establish_preds (struct loop *);
47 static void canonicalize_loop_headers (void);
48 static bool glb_enum_p (basic_block, void *);
50 /* Dump loop related CFG information. */
53 flow_loops_cfg_dump (const struct loops *loops, FILE *file)
57 if (! loops->num || ! file)
65 fprintf (file, ";; %d succs { ", bb->index);
66 FOR_EACH_EDGE (succ, ei, bb->succs)
67 fprintf (file, "%d ", succ->dest->index);
68 fprintf (file, "}\n");
72 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
75 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
77 return (loop->depth > outer->depth
78 && loop->pred[outer->depth] == outer);
81 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
85 superloop_at_depth (struct loop *loop, unsigned depth)
87 gcc_assert (depth <= (unsigned) loop->depth);
89 if (depth == (unsigned) loop->depth)
92 return loop->pred[depth];
95 /* Dump the loop information specified by LOOP to the stream FILE
96 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
99 flow_loop_dump (const struct loop *loop, FILE *file,
100 void (*loop_dump_aux) (const struct loop *, FILE *, int),
106 if (! loop || ! loop->header)
109 fprintf (file, ";;\n;; Loop %d\n", loop->num);
111 fprintf (file, ";; header %d, latch %d\n",
112 loop->header->index, loop->latch->index);
113 fprintf (file, ";; depth %d, level %d, outer %ld\n",
114 loop->depth, loop->level,
115 (long) (loop->outer ? loop->outer->num : -1));
117 fprintf (file, ";; nodes:");
118 bbs = get_loop_body (loop);
119 for (i = 0; i < loop->num_nodes; i++)
120 fprintf (file, " %d", bbs[i]->index);
122 fprintf (file, "\n");
125 loop_dump_aux (loop, file, verbose);
128 /* Dump the loop information specified by LOOPS to the stream FILE,
129 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
132 flow_loops_dump (const struct loops *loops, FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
137 num_loops = loops->num;
138 if (! num_loops || ! file)
141 fprintf (file, ";; %d loops found\n", num_loops);
143 for (i = 0; i < num_loops; i++)
145 struct loop *loop = loops->parray[i];
150 flow_loop_dump (loop, file, loop_dump_aux, verbose);
154 flow_loops_cfg_dump (loops, file);
157 /* Free data allocated for LOOP. */
159 flow_loop_free (struct loop *loop)
166 /* Free all the memory allocated for LOOPS. */
169 flow_loops_free (struct loops *loops)
175 gcc_assert (loops->num);
177 /* Free the loop descriptors. */
178 for (i = 0; i < loops->num; i++)
180 struct loop *loop = loops->parray[i];
185 flow_loop_free (loop);
188 free (loops->parray);
189 loops->parray = NULL;
193 /* Find the nodes contained within the LOOP with header HEADER.
194 Return the number of nodes within the loop. */
197 flow_loop_nodes_find (basic_block header, struct loop *loop)
203 header->loop_father = loop;
204 header->loop_depth = loop->depth;
206 if (loop->latch->loop_father != loop)
208 stack = XNEWVEC (basic_block, n_basic_blocks);
211 stack[sp++] = loop->latch;
212 loop->latch->loop_father = loop;
213 loop->latch->loop_depth = loop->depth;
223 FOR_EACH_EDGE (e, ei, node->preds)
225 basic_block ancestor = e->src;
227 if (ancestor != ENTRY_BLOCK_PTR
228 && ancestor->loop_father != loop)
230 ancestor->loop_father = loop;
231 ancestor->loop_depth = loop->depth;
233 stack[sp++] = ancestor;
242 /* For each loop in the lOOPS tree that has just a single exit
243 record the exit edge. */
246 mark_single_exit_loops (struct loops *loops)
253 for (i = 1; i < loops->num; i++)
255 loop = loops->parray[i];
257 loop->single_exit = NULL;
263 if (bb->loop_father == loops->tree_root)
265 FOR_EACH_EDGE (e, ei, bb->succs)
267 if (e->dest == EXIT_BLOCK_PTR)
270 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
273 for (loop = bb->loop_father;
274 loop != e->dest->loop_father;
277 /* If we have already seen an exit, mark this by the edge that
278 surely does not occur as any exit. */
279 if (loop->single_exit)
280 loop->single_exit = single_succ_edge (ENTRY_BLOCK_PTR);
282 loop->single_exit = e;
287 for (i = 1; i < loops->num; i++)
289 loop = loops->parray[i];
293 if (loop->single_exit == single_succ_edge (ENTRY_BLOCK_PTR))
294 loop->single_exit = NULL;
297 loops->state |= LOOPS_HAVE_MARKED_SINGLE_EXITS;
301 establish_preds (struct loop *loop)
303 struct loop *ploop, *father = loop->outer;
305 loop->depth = father->depth + 1;
307 /* Remember the current loop depth if it is the largest seen so far. */
308 cfun->max_loop_depth = MAX (cfun->max_loop_depth, loop->depth);
312 loop->pred = XNEWVEC (struct loop *, loop->depth);
313 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
314 loop->pred[father->depth] = father;
316 for (ploop = loop->inner; ploop; ploop = ploop->next)
317 establish_preds (ploop);
320 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
321 added loop. If LOOP has some children, take care of that their
322 pred field will be initialized correctly. */
325 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
327 loop->next = father->inner;
328 father->inner = loop;
329 loop->outer = father;
331 establish_preds (loop);
334 /* Remove LOOP from the loop hierarchy tree. */
337 flow_loop_tree_node_remove (struct loop *loop)
339 struct loop *prev, *father;
341 father = loop->outer;
344 /* Remove loop from the list of sons. */
345 if (father->inner == loop)
346 father->inner = loop->next;
349 for (prev = father->inner; prev->next != loop; prev = prev->next);
350 prev->next = loop->next;
358 /* Helper function to compute loop nesting depth and enclosed loop level
359 for the natural loop specified by LOOP. Returns the loop level. */
362 flow_loop_level_compute (struct loop *loop)
370 /* Traverse loop tree assigning depth and computing level as the
371 maximum level of all the inner loops of this loop. The loop
372 level is equivalent to the height of the loop in the loop tree
373 and corresponds to the number of enclosed loop levels (including
375 for (inner = loop->inner; inner; inner = inner->next)
377 int ilevel = flow_loop_level_compute (inner) + 1;
387 /* Compute the loop nesting depth and enclosed loop level for the loop
388 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
392 flow_loops_level_compute (struct loops *loops)
394 flow_loop_level_compute (loops->tree_root);
397 /* A callback to update latch and header info for basic block JUMP created
398 by redirecting an edge. */
401 update_latch_info (basic_block jump)
403 alloc_aux_for_block (jump, sizeof (int));
404 HEADER_BLOCK (jump) = 0;
405 alloc_aux_for_edge (single_pred_edge (jump), sizeof (int));
406 LATCH_EDGE (single_pred_edge (jump)) = 0;
407 set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
410 /* A callback for make_forwarder block, to redirect all edges except for
411 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
412 whether to redirect it. */
414 static edge mfb_kj_edge;
416 mfb_keep_just (edge e)
418 return e != mfb_kj_edge;
421 /* A callback for make_forwarder block, to redirect the latch edges into an
422 entry part. E is the edge for that we should decide whether to redirect
426 mfb_keep_nonlatch (edge e)
428 return LATCH_EDGE (e);
431 /* Takes care of merging natural loops with shared headers. */
434 canonicalize_loop_headers (void)
439 alloc_aux_for_blocks (sizeof (int));
440 alloc_aux_for_edges (sizeof (int));
442 /* Split blocks so that each loop has only single latch. */
447 int have_abnormal_edge = 0;
449 FOR_EACH_EDGE (e, ei, header->preds)
451 basic_block latch = e->src;
453 if (e->flags & EDGE_ABNORMAL)
454 have_abnormal_edge = 1;
456 if (latch != ENTRY_BLOCK_PTR
457 && dominated_by_p (CDI_DOMINATORS, latch, header))
463 if (have_abnormal_edge)
464 HEADER_BLOCK (header) = 0;
466 HEADER_BLOCK (header) = num_latches;
469 if (HEADER_BLOCK (single_succ (ENTRY_BLOCK_PTR)))
473 /* We could not redirect edges freely here. On the other hand,
474 we can simply split the edge from entry block. */
475 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
477 alloc_aux_for_edge (single_succ_edge (bb), sizeof (int));
478 LATCH_EDGE (single_succ_edge (bb)) = 0;
479 alloc_aux_for_block (bb, sizeof (int));
480 HEADER_BLOCK (bb) = 0;
485 int max_freq, is_heavy;
486 edge heavy, tmp_edge;
489 if (HEADER_BLOCK (header) <= 1)
492 /* Find a heavy edge. */
496 FOR_EACH_EDGE (e, ei, header->preds)
497 if (LATCH_EDGE (e) &&
498 EDGE_FREQUENCY (e) > max_freq)
499 max_freq = EDGE_FREQUENCY (e);
500 FOR_EACH_EDGE (e, ei, header->preds)
501 if (LATCH_EDGE (e) &&
502 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
515 /* Split out the heavy edge, and create inner loop for it. */
517 tmp_edge = make_forwarder_block (header, mfb_keep_just,
519 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
520 HEADER_BLOCK (tmp_edge->dest) = 1;
521 alloc_aux_for_edge (tmp_edge, sizeof (int));
522 LATCH_EDGE (tmp_edge) = 0;
523 HEADER_BLOCK (header)--;
526 if (HEADER_BLOCK (header) > 1)
528 /* Create a new latch block. */
529 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
531 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
532 HEADER_BLOCK (tmp_edge->src) = 0;
533 HEADER_BLOCK (tmp_edge->dest) = 1;
534 alloc_aux_for_edge (tmp_edge, sizeof (int));
535 LATCH_EDGE (tmp_edge) = 1;
539 free_aux_for_blocks ();
540 free_aux_for_edges ();
542 #ifdef ENABLE_CHECKING
543 verify_dominators (CDI_DOMINATORS);
547 /* Initialize all the parallel_p fields of the loops structure to true. */
550 initialize_loops_parallel_p (struct loops *loops)
554 for (i = 0; i < loops->num; i++)
556 struct loop *loop = loops->parray[i];
557 loop->parallel_p = true;
561 /* Find all the natural loops in the function and save in LOOPS structure and
562 recalculate loop_depth information in basic block structures.
563 Return the number of natural loops found. */
566 flow_loops_find (struct loops *loops)
577 memset (loops, 0, sizeof *loops);
579 /* We are going to recount the maximum loop depth,
580 so throw away the last count. */
581 cfun->max_loop_depth = 0;
583 /* Taking care of this degenerate case makes the rest of
584 this code simpler. */
585 if (n_basic_blocks == NUM_FIXED_BLOCKS)
591 /* Ensure that the dominators are computed. */
592 calculate_dominance_info (CDI_DOMINATORS);
594 /* Join loops with shared headers. */
595 canonicalize_loop_headers ();
597 /* Count the number of loop headers. This should be the
598 same as the number of natural loops. */
599 headers = sbitmap_alloc (last_basic_block);
600 sbitmap_zero (headers);
606 int more_latches = 0;
608 header->loop_depth = 0;
610 /* If we have an abnormal predecessor, do not consider the
611 loop (not worth the problems). */
612 FOR_EACH_EDGE (e, ei, header->preds)
613 if (e->flags & EDGE_ABNORMAL)
618 FOR_EACH_EDGE (e, ei, header->preds)
620 basic_block latch = e->src;
622 gcc_assert (!(e->flags & EDGE_ABNORMAL));
624 /* Look for back edges where a predecessor is dominated
625 by this block. A natural loop has a single entry
626 node (header) that dominates all the nodes in the
627 loop. It also has single back edge to the header
628 from a latch node. */
629 if (latch != ENTRY_BLOCK_PTR
630 && dominated_by_p (CDI_DOMINATORS, latch, header))
632 /* Shared headers should be eliminated by now. */
633 gcc_assert (!more_latches);
635 SET_BIT (headers, header->index);
641 /* Allocate loop structures. */
642 loops->parray = XCNEWVEC (struct loop *, num_loops + 1);
644 /* Dummy loop containing whole function. */
645 loops->parray[0] = XCNEW (struct loop);
646 loops->parray[0]->next = NULL;
647 loops->parray[0]->inner = NULL;
648 loops->parray[0]->outer = NULL;
649 loops->parray[0]->depth = 0;
650 loops->parray[0]->pred = NULL;
651 loops->parray[0]->num_nodes = n_basic_blocks;
652 loops->parray[0]->latch = EXIT_BLOCK_PTR;
653 loops->parray[0]->header = ENTRY_BLOCK_PTR;
654 ENTRY_BLOCK_PTR->loop_father = loops->parray[0];
655 EXIT_BLOCK_PTR->loop_father = loops->parray[0];
657 loops->tree_root = loops->parray[0];
659 /* Find and record information about all the natural loops
663 bb->loop_father = loops->tree_root;
667 /* Compute depth first search order of the CFG so that outer
668 natural loops will be found before inner natural loops. */
669 dfs_order = XNEWVEC (int, n_basic_blocks);
670 rc_order = XNEWVEC (int, n_basic_blocks);
671 pre_and_rev_post_order_compute (dfs_order, rc_order, false);
675 for (b = 0; b < n_basic_blocks - NUM_FIXED_BLOCKS; b++)
680 /* Search the nodes of the CFG in reverse completion order
681 so that we can find outer loops first. */
682 if (!TEST_BIT (headers, rc_order[b]))
685 header = BASIC_BLOCK (rc_order[b]);
687 loop = loops->parray[num_loops] = XCNEW (struct loop);
689 loop->header = header;
690 loop->num = num_loops;
693 /* Look for the latch for this header block. */
694 FOR_EACH_EDGE (e, ei, header->preds)
696 basic_block latch = e->src;
698 if (latch != ENTRY_BLOCK_PTR
699 && dominated_by_p (CDI_DOMINATORS, latch, header))
706 flow_loop_tree_node_add (header->loop_father, loop);
707 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
710 /* Assign the loop nesting depth and enclosed loop level for each
712 flow_loops_level_compute (loops);
714 loops->num = num_loops;
715 initialize_loops_parallel_p (loops);
721 sbitmap_free (headers);
727 /* Return nonzero if basic block BB belongs to LOOP. */
729 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
731 struct loop *source_loop;
733 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
736 source_loop = bb->loop_father;
737 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
740 /* Enumeration predicate for get_loop_body. */
742 glb_enum_p (basic_block bb, void *glb_header)
744 return bb != (basic_block) glb_header;
747 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
748 order against direction of edges from latch. Specially, if
749 header != latch, latch is the 1-st block. */
751 get_loop_body (const struct loop *loop)
753 basic_block *tovisit, bb;
756 gcc_assert (loop->num_nodes);
758 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
759 tovisit[tv++] = loop->header;
761 if (loop->latch == EXIT_BLOCK_PTR)
763 /* There may be blocks unreachable from EXIT_BLOCK. */
764 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks);
767 tovisit[tv++] = EXIT_BLOCK_PTR;
769 else if (loop->latch != loop->header)
771 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
772 tovisit + 1, loop->num_nodes - 1,
776 gcc_assert (tv == loop->num_nodes);
780 /* Fills dominance descendants inside LOOP of the basic block BB into
781 array TOVISIT from index *TV. */
784 fill_sons_in_loop (const struct loop *loop, basic_block bb,
785 basic_block *tovisit, int *tv)
787 basic_block son, postpone = NULL;
789 tovisit[(*tv)++] = bb;
790 for (son = first_dom_son (CDI_DOMINATORS, bb);
792 son = next_dom_son (CDI_DOMINATORS, son))
794 if (!flow_bb_inside_loop_p (loop, son))
797 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
802 fill_sons_in_loop (loop, son, tovisit, tv);
806 fill_sons_in_loop (loop, postpone, tovisit, tv);
809 /* Gets body of a LOOP (that must be different from the outermost loop)
810 sorted by dominance relation. Additionally, if a basic block s dominates
811 the latch, then only blocks dominated by s are be after it. */
814 get_loop_body_in_dom_order (const struct loop *loop)
816 basic_block *tovisit;
819 gcc_assert (loop->num_nodes);
821 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
823 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
826 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
828 gcc_assert (tv == (int) loop->num_nodes);
833 /* Get body of a LOOP in breadth first sort order. */
836 get_loop_body_in_bfs_order (const struct loop *loop)
844 gcc_assert (loop->num_nodes);
845 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
847 blocks = XCNEWVEC (basic_block, loop->num_nodes);
848 visited = BITMAP_ALLOC (NULL);
851 while (i < loop->num_nodes)
856 if (!bitmap_bit_p (visited, bb->index))
858 /* This basic block is now visited */
859 bitmap_set_bit (visited, bb->index);
863 FOR_EACH_EDGE (e, ei, bb->succs)
865 if (flow_bb_inside_loop_p (loop, e->dest))
867 if (!bitmap_bit_p (visited, e->dest->index))
869 bitmap_set_bit (visited, e->dest->index);
870 blocks[i++] = e->dest;
875 gcc_assert (i >= vc);
880 BITMAP_FREE (visited);
884 /* Returns the list of the exit edges of a LOOP. */
887 get_loop_exit_edges (const struct loop *loop)
889 VEC (edge, heap) *edges = NULL;
895 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
897 body = get_loop_body (loop);
898 for (i = 0; i < loop->num_nodes; i++)
899 FOR_EACH_EDGE (e, ei, body[i]->succs)
900 if (!flow_bb_inside_loop_p (loop, e->dest))
901 VEC_safe_push (edge, heap, edges, e);
907 /* Counts the number of conditional branches inside LOOP. */
910 num_loop_branches (const struct loop *loop)
915 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
917 body = get_loop_body (loop);
919 for (i = 0; i < loop->num_nodes; i++)
920 if (EDGE_COUNT (body[i]->succs) >= 2)
927 /* Adds basic block BB to LOOP. */
929 add_bb_to_loop (basic_block bb, struct loop *loop)
933 gcc_assert (bb->loop_father == NULL);
934 bb->loop_father = loop;
935 bb->loop_depth = loop->depth;
937 for (i = 0; i < loop->depth; i++)
938 loop->pred[i]->num_nodes++;
941 /* Remove basic block BB from loops. */
943 remove_bb_from_loops (basic_block bb)
946 struct loop *loop = bb->loop_father;
948 gcc_assert (loop != NULL);
950 for (i = 0; i < loop->depth; i++)
951 loop->pred[i]->num_nodes--;
952 bb->loop_father = NULL;
956 /* Finds nearest common ancestor in loop tree for given loops. */
958 find_common_loop (struct loop *loop_s, struct loop *loop_d)
960 if (!loop_s) return loop_d;
961 if (!loop_d) return loop_s;
963 if (loop_s->depth < loop_d->depth)
964 loop_d = loop_d->pred[loop_s->depth];
965 else if (loop_s->depth > loop_d->depth)
966 loop_s = loop_s->pred[loop_d->depth];
968 while (loop_s != loop_d)
970 loop_s = loop_s->outer;
971 loop_d = loop_d->outer;
976 /* Cancels the LOOP; it must be innermost one. */
979 cancel_loop (struct loops *loops, struct loop *loop)
984 gcc_assert (!loop->inner);
986 /* Move blocks up one level (they should be removed as soon as possible). */
987 bbs = get_loop_body (loop);
988 for (i = 0; i < loop->num_nodes; i++)
989 bbs[i]->loop_father = loop->outer;
991 /* Remove the loop from structure. */
992 flow_loop_tree_node_remove (loop);
994 /* Remove loop from loops array. */
995 loops->parray[loop->num] = NULL;
997 /* Free loop data. */
998 flow_loop_free (loop);
1001 /* Cancels LOOP and all its subloops. */
1003 cancel_loop_tree (struct loops *loops, struct loop *loop)
1006 cancel_loop_tree (loops, loop->inner);
1007 cancel_loop (loops, loop);
1010 /* Checks that LOOPS are all right:
1011 -- sizes of loops are all right
1012 -- results of get_loop_body really belong to the loop
1013 -- loop header have just single entry edge and single latch edge
1014 -- loop latches have only single successor that is header of their loop
1015 -- irreducible loops are correctly marked
1018 verify_loop_structure (struct loops *loops)
1020 unsigned *sizes, i, j;
1022 basic_block *bbs, bb;
1028 sizes = XCNEWVEC (unsigned, loops->num);
1032 for (loop = bb->loop_father; loop; loop = loop->outer)
1035 for (i = 0; i < loops->num; i++)
1037 if (!loops->parray[i])
1040 if (loops->parray[i]->num_nodes != sizes[i])
1042 error ("size of loop %d should be %d, not %d",
1043 i, sizes[i], loops->parray[i]->num_nodes);
1048 /* Check get_loop_body. */
1049 for (i = 1; i < loops->num; i++)
1051 loop = loops->parray[i];
1054 bbs = get_loop_body (loop);
1056 for (j = 0; j < loop->num_nodes; j++)
1057 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1059 error ("bb %d do not belong to loop %d",
1066 /* Check headers and latches. */
1067 for (i = 1; i < loops->num; i++)
1069 loop = loops->parray[i];
1073 if ((loops->state & LOOPS_HAVE_PREHEADERS)
1074 && EDGE_COUNT (loop->header->preds) != 2)
1076 error ("loop %d's header does not have exactly 2 entries", i);
1079 if (loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1081 if (!single_succ_p (loop->latch))
1083 error ("loop %d's latch does not have exactly 1 successor", i);
1086 if (single_succ (loop->latch) != loop->header)
1088 error ("loop %d's latch does not have header as successor", i);
1091 if (loop->latch->loop_father != loop)
1093 error ("loop %d's latch does not belong directly to it", i);
1097 if (loop->header->loop_father != loop)
1099 error ("loop %d's header does not belong directly to it", i);
1102 if ((loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1103 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1105 error ("loop %d's latch is marked as part of irreducible region", i);
1110 /* Check irreducible loops. */
1111 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1113 /* Record old info. */
1114 irreds = sbitmap_alloc (last_basic_block);
1118 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1119 SET_BIT (irreds, bb->index);
1121 RESET_BIT (irreds, bb->index);
1122 FOR_EACH_EDGE (e, ei, bb->succs)
1123 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1124 e->flags |= EDGE_ALL_FLAGS + 1;
1128 mark_irreducible_loops (loops);
1135 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1136 && !TEST_BIT (irreds, bb->index))
1138 error ("basic block %d should be marked irreducible", bb->index);
1141 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1142 && TEST_BIT (irreds, bb->index))
1144 error ("basic block %d should not be marked irreducible", bb->index);
1147 FOR_EACH_EDGE (e, ei, bb->succs)
1149 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1150 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1152 error ("edge from %d to %d should be marked irreducible",
1153 e->src->index, e->dest->index);
1156 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1157 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1159 error ("edge from %d to %d should not be marked irreducible",
1160 e->src->index, e->dest->index);
1163 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1169 /* Check the single_exit. */
1170 if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
1172 memset (sizes, 0, sizeof (unsigned) * loops->num);
1176 if (bb->loop_father == loops->tree_root)
1178 FOR_EACH_EDGE (e, ei, bb->succs)
1180 if (e->dest == EXIT_BLOCK_PTR)
1183 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
1186 for (loop = bb->loop_father;
1187 loop != e->dest->loop_father;
1191 if (loop->single_exit
1192 && loop->single_exit != e)
1194 error ("wrong single exit %d->%d recorded for loop %d",
1195 loop->single_exit->src->index,
1196 loop->single_exit->dest->index,
1198 error ("right exit is %d->%d",
1199 e->src->index, e->dest->index);
1206 for (i = 1; i < loops->num; i++)
1208 loop = loops->parray[i];
1213 && !loop->single_exit)
1215 error ("single exit not recorded for loop %d", loop->num);
1220 && loop->single_exit)
1222 error ("loop %d should not have single exit (%d -> %d)",
1224 loop->single_exit->src->index,
1225 loop->single_exit->dest->index);
1236 /* Returns latch edge of LOOP. */
1238 loop_latch_edge (const struct loop *loop)
1240 return find_edge (loop->latch, loop->header);
1243 /* Returns preheader edge of LOOP. */
1245 loop_preheader_edge (const struct loop *loop)
1250 FOR_EACH_EDGE (e, ei, loop->header->preds)
1251 if (e->src != loop->latch)
1257 /* Returns true if E is an exit of LOOP. */
1260 loop_exit_edge_p (const struct loop *loop, edge e)
1262 return (flow_bb_inside_loop_p (loop, e->src)
1263 && !flow_bb_inside_loop_p (loop, e->dest));