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 (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. */
51 flow_loops_cfg_dump (FILE *file
)
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. */
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)
83 superloop_at_depth (struct loop
*loop
, unsigned depth
)
85 gcc_assert (depth
<= (unsigned) loop
->depth
);
87 if (depth
== (unsigned) loop
->depth
)
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. */
97 flow_loop_dump (const struct loop
*loop
, FILE *file
,
98 void (*loop_dump_aux
) (const struct loop
*, FILE *, int),
104 if (! loop
|| ! loop
->header
)
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
);
119 fprintf (file
, "\n");
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. */
129 flow_loops_dump (FILE *file
, void (*loop_dump_aux
) (const struct loop
*, FILE *, int), int verbose
)
134 if (!current_loops
|| ! file
)
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
);
145 flow_loops_cfg_dump (file
);
148 /* Free data allocated for LOOP. */
150 flow_loop_free (struct loop
*loop
)
157 /* Free all the memory allocated for LOOPS. */
160 flow_loops_free (struct loops
*loops
)
167 /* Free the loop descriptors. */
168 for (i
= 0; VEC_iterate (loop_p
, loops
->larray
, i
, loop
); i
++)
173 flow_loop_free (loop
);
176 VEC_free (loop_p
, heap
, loops
->larray
);
177 loops
->larray
= NULL
;
181 /* Find the nodes contained within the LOOP with header HEADER.
182 Return the number of nodes within the loop. */
185 flow_loop_nodes_find (basic_block header
, struct loop
*loop
)
191 header
->loop_father
= loop
;
192 header
->loop_depth
= loop
->depth
;
194 if (loop
->latch
->loop_father
!= loop
)
196 stack
= XNEWVEC (basic_block
, n_basic_blocks
);
199 stack
[sp
++] = loop
->latch
;
200 loop
->latch
->loop_father
= loop
;
201 loop
->latch
->loop_depth
= loop
->depth
;
211 FOR_EACH_EDGE (e
, ei
, node
->preds
)
213 basic_block ancestor
= e
->src
;
215 if (ancestor
!= ENTRY_BLOCK_PTR
216 && ancestor
->loop_father
!= loop
)
218 ancestor
->loop_father
= loop
;
219 ancestor
->loop_depth
= loop
->depth
;
221 stack
[sp
++] = ancestor
;
230 /* For each loop that has just a single exit, record the exit edge. */
233 mark_single_exit_loops (void)
240 FOR_EACH_LOOP (li
, loop
, 0)
242 set_single_exit (loop
, NULL
);
248 if (bb
->loop_father
== current_loops
->tree_root
)
250 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
252 if (e
->dest
== EXIT_BLOCK_PTR
)
255 if (flow_bb_inside_loop_p (bb
->loop_father
, e
->dest
))
258 for (loop
= bb
->loop_father
;
259 loop
!= e
->dest
->loop_father
;
262 /* If we have already seen an exit, mark this by the edge that
263 surely does not occur as any exit. */
264 if (single_exit (loop
))
265 set_single_exit (loop
, single_succ_edge (ENTRY_BLOCK_PTR
));
267 set_single_exit (loop
, e
);
272 FOR_EACH_LOOP (li
, loop
, 0)
274 if (single_exit (loop
) == single_succ_edge (ENTRY_BLOCK_PTR
))
275 set_single_exit (loop
, NULL
);
278 current_loops
->state
|= LOOPS_HAVE_MARKED_SINGLE_EXITS
;
282 establish_preds (struct loop
*loop
)
284 struct loop
*ploop
, *father
= loop
->outer
;
286 loop
->depth
= father
->depth
+ 1;
288 /* Remember the current loop depth if it is the largest seen so far. */
289 cfun
->max_loop_depth
= MAX (cfun
->max_loop_depth
, loop
->depth
);
293 loop
->pred
= XNEWVEC (struct loop
*, loop
->depth
);
294 memcpy (loop
->pred
, father
->pred
, sizeof (struct loop
*) * father
->depth
);
295 loop
->pred
[father
->depth
] = father
;
297 for (ploop
= loop
->inner
; ploop
; ploop
= ploop
->next
)
298 establish_preds (ploop
);
301 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
302 added loop. If LOOP has some children, take care of that their
303 pred field will be initialized correctly. */
306 flow_loop_tree_node_add (struct loop
*father
, struct loop
*loop
)
308 loop
->next
= father
->inner
;
309 father
->inner
= loop
;
310 loop
->outer
= father
;
312 establish_preds (loop
);
315 /* Remove LOOP from the loop hierarchy tree. */
318 flow_loop_tree_node_remove (struct loop
*loop
)
320 struct loop
*prev
, *father
;
322 father
= loop
->outer
;
325 /* Remove loop from the list of sons. */
326 if (father
->inner
== loop
)
327 father
->inner
= loop
->next
;
330 for (prev
= father
->inner
; prev
->next
!= loop
; prev
= prev
->next
);
331 prev
->next
= loop
->next
;
339 /* A callback to update latch and header info for basic block JUMP created
340 by redirecting an edge. */
343 update_latch_info (basic_block jump
)
345 alloc_aux_for_block (jump
, sizeof (int));
346 HEADER_BLOCK (jump
) = 0;
347 alloc_aux_for_edge (single_pred_edge (jump
), sizeof (int));
348 LATCH_EDGE (single_pred_edge (jump
)) = 0;
349 set_immediate_dominator (CDI_DOMINATORS
, jump
, single_pred (jump
));
352 /* A callback for make_forwarder block, to redirect all edges except for
353 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
354 whether to redirect it. */
356 static edge mfb_kj_edge
;
358 mfb_keep_just (edge e
)
360 return e
!= mfb_kj_edge
;
363 /* A callback for make_forwarder block, to redirect the latch edges into an
364 entry part. E is the edge for that we should decide whether to redirect
368 mfb_keep_nonlatch (edge e
)
370 return LATCH_EDGE (e
);
373 /* Takes care of merging natural loops with shared headers. */
376 canonicalize_loop_headers (void)
381 alloc_aux_for_blocks (sizeof (int));
382 alloc_aux_for_edges (sizeof (int));
384 /* Split blocks so that each loop has only single latch. */
389 int have_abnormal_edge
= 0;
391 FOR_EACH_EDGE (e
, ei
, header
->preds
)
393 basic_block latch
= e
->src
;
395 if (e
->flags
& EDGE_ABNORMAL
)
396 have_abnormal_edge
= 1;
398 if (latch
!= ENTRY_BLOCK_PTR
399 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
405 if (have_abnormal_edge
)
406 HEADER_BLOCK (header
) = 0;
408 HEADER_BLOCK (header
) = num_latches
;
411 if (HEADER_BLOCK (single_succ (ENTRY_BLOCK_PTR
)))
415 /* We could not redirect edges freely here. On the other hand,
416 we can simply split the edge from entry block. */
417 bb
= split_edge (single_succ_edge (ENTRY_BLOCK_PTR
));
419 alloc_aux_for_edge (single_succ_edge (bb
), sizeof (int));
420 LATCH_EDGE (single_succ_edge (bb
)) = 0;
421 alloc_aux_for_block (bb
, sizeof (int));
422 HEADER_BLOCK (bb
) = 0;
427 int max_freq
, is_heavy
;
428 edge heavy
, tmp_edge
;
431 if (HEADER_BLOCK (header
) <= 1)
434 /* Find a heavy edge. */
438 FOR_EACH_EDGE (e
, ei
, header
->preds
)
439 if (LATCH_EDGE (e
) &&
440 EDGE_FREQUENCY (e
) > max_freq
)
441 max_freq
= EDGE_FREQUENCY (e
);
442 FOR_EACH_EDGE (e
, ei
, header
->preds
)
443 if (LATCH_EDGE (e
) &&
444 EDGE_FREQUENCY (e
) >= max_freq
/ HEAVY_EDGE_RATIO
)
457 /* Split out the heavy edge, and create inner loop for it. */
459 tmp_edge
= make_forwarder_block (header
, mfb_keep_just
,
461 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
462 HEADER_BLOCK (tmp_edge
->dest
) = 1;
463 alloc_aux_for_edge (tmp_edge
, sizeof (int));
464 LATCH_EDGE (tmp_edge
) = 0;
465 HEADER_BLOCK (header
)--;
468 if (HEADER_BLOCK (header
) > 1)
470 /* Create a new latch block. */
471 tmp_edge
= make_forwarder_block (header
, mfb_keep_nonlatch
,
473 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
474 HEADER_BLOCK (tmp_edge
->src
) = 0;
475 HEADER_BLOCK (tmp_edge
->dest
) = 1;
476 alloc_aux_for_edge (tmp_edge
, sizeof (int));
477 LATCH_EDGE (tmp_edge
) = 1;
481 free_aux_for_blocks ();
482 free_aux_for_edges ();
484 #ifdef ENABLE_CHECKING
485 verify_dominators (CDI_DOMINATORS
);
489 /* Find all the natural loops in the function and save in LOOPS structure and
490 recalculate loop_depth information in basic block structures.
491 Return the number of natural loops found. */
494 flow_loops_find (struct loops
*loops
)
506 memset (loops
, 0, sizeof *loops
);
508 /* We are going to recount the maximum loop depth,
509 so throw away the last count. */
510 cfun
->max_loop_depth
= 0;
512 /* Taking care of this degenerate case makes the rest of
513 this code simpler. */
514 if (n_basic_blocks
== NUM_FIXED_BLOCKS
)
520 /* Ensure that the dominators are computed. */
521 calculate_dominance_info (CDI_DOMINATORS
);
523 /* Join loops with shared headers. */
524 canonicalize_loop_headers ();
526 /* Count the number of loop headers. This should be the
527 same as the number of natural loops. */
528 headers
= sbitmap_alloc (last_basic_block
);
529 sbitmap_zero (headers
);
535 int more_latches
= 0;
537 header
->loop_depth
= 0;
539 /* If we have an abnormal predecessor, do not consider the
540 loop (not worth the problems). */
541 FOR_EACH_EDGE (e
, ei
, header
->preds
)
542 if (e
->flags
& EDGE_ABNORMAL
)
547 FOR_EACH_EDGE (e
, ei
, header
->preds
)
549 basic_block latch
= e
->src
;
551 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
553 /* Look for back edges where a predecessor is dominated
554 by this block. A natural loop has a single entry
555 node (header) that dominates all the nodes in the
556 loop. It also has single back edge to the header
557 from a latch node. */
558 if (latch
!= ENTRY_BLOCK_PTR
559 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
561 /* Shared headers should be eliminated by now. */
562 gcc_assert (!more_latches
);
564 SET_BIT (headers
, header
->index
);
570 /* Allocate loop structures. */
571 loops
->larray
= VEC_alloc (loop_p
, heap
, num_loops
+ 1);
573 /* Dummy loop containing whole function. */
574 root
= XCNEW (struct loop
);
575 root
->num_nodes
= n_basic_blocks
;
576 root
->latch
= EXIT_BLOCK_PTR
;
577 root
->header
= ENTRY_BLOCK_PTR
;
578 ENTRY_BLOCK_PTR
->loop_father
= root
;
579 EXIT_BLOCK_PTR
->loop_father
= root
;
581 VEC_quick_push (loop_p
, loops
->larray
, root
);
582 loops
->tree_root
= root
;
584 /* Find and record information about all the natural loops
587 bb
->loop_father
= loops
->tree_root
;
591 /* Compute depth first search order of the CFG so that outer
592 natural loops will be found before inner natural loops. */
593 dfs_order
= XNEWVEC (int, n_basic_blocks
);
594 rc_order
= XNEWVEC (int, n_basic_blocks
);
595 pre_and_rev_post_order_compute (dfs_order
, rc_order
, false);
599 for (b
= 0; b
< n_basic_blocks
- NUM_FIXED_BLOCKS
; b
++)
604 /* Search the nodes of the CFG in reverse completion order
605 so that we can find outer loops first. */
606 if (!TEST_BIT (headers
, rc_order
[b
]))
609 header
= BASIC_BLOCK (rc_order
[b
]);
611 loop
= XCNEW (struct loop
);
612 VEC_quick_push (loop_p
, loops
->larray
, loop
);
614 loop
->header
= header
;
615 loop
->num
= num_loops
;
618 /* Look for the latch for this header block. */
619 FOR_EACH_EDGE (e
, ei
, header
->preds
)
621 basic_block latch
= e
->src
;
623 if (latch
!= ENTRY_BLOCK_PTR
624 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
631 flow_loop_tree_node_add (header
->loop_father
, loop
);
632 loop
->num_nodes
= flow_loop_nodes_find (loop
->header
, loop
);
639 sbitmap_free (headers
);
642 return VEC_length (loop_p
, loops
->larray
);
645 /* Return nonzero if basic block BB belongs to LOOP. */
647 flow_bb_inside_loop_p (const struct loop
*loop
, const basic_block bb
)
649 struct loop
*source_loop
;
651 if (bb
== ENTRY_BLOCK_PTR
|| bb
== EXIT_BLOCK_PTR
)
654 source_loop
= bb
->loop_father
;
655 return loop
== source_loop
|| flow_loop_nested_p (loop
, source_loop
);
658 /* Enumeration predicate for get_loop_body. */
660 glb_enum_p (basic_block bb
, void *glb_header
)
662 return bb
!= (basic_block
) glb_header
;
665 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
666 order against direction of edges from latch. Specially, if
667 header != latch, latch is the 1-st block. */
669 get_loop_body (const struct loop
*loop
)
671 basic_block
*tovisit
, bb
;
674 gcc_assert (loop
->num_nodes
);
676 tovisit
= XCNEWVEC (basic_block
, loop
->num_nodes
);
677 tovisit
[tv
++] = loop
->header
;
679 if (loop
->latch
== EXIT_BLOCK_PTR
)
681 /* There may be blocks unreachable from EXIT_BLOCK. */
682 gcc_assert (loop
->num_nodes
== (unsigned) n_basic_blocks
);
685 tovisit
[tv
++] = EXIT_BLOCK_PTR
;
687 else if (loop
->latch
!= loop
->header
)
689 tv
= dfs_enumerate_from (loop
->latch
, 1, glb_enum_p
,
690 tovisit
+ 1, loop
->num_nodes
- 1,
694 gcc_assert (tv
== loop
->num_nodes
);
698 /* Fills dominance descendants inside LOOP of the basic block BB into
699 array TOVISIT from index *TV. */
702 fill_sons_in_loop (const struct loop
*loop
, basic_block bb
,
703 basic_block
*tovisit
, int *tv
)
705 basic_block son
, postpone
= NULL
;
707 tovisit
[(*tv
)++] = bb
;
708 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
710 son
= next_dom_son (CDI_DOMINATORS
, son
))
712 if (!flow_bb_inside_loop_p (loop
, son
))
715 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, son
))
720 fill_sons_in_loop (loop
, son
, tovisit
, tv
);
724 fill_sons_in_loop (loop
, postpone
, tovisit
, tv
);
727 /* Gets body of a LOOP (that must be different from the outermost loop)
728 sorted by dominance relation. Additionally, if a basic block s dominates
729 the latch, then only blocks dominated by s are be after it. */
732 get_loop_body_in_dom_order (const struct loop
*loop
)
734 basic_block
*tovisit
;
737 gcc_assert (loop
->num_nodes
);
739 tovisit
= XCNEWVEC (basic_block
, loop
->num_nodes
);
741 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
744 fill_sons_in_loop (loop
, loop
->header
, tovisit
, &tv
);
746 gcc_assert (tv
== (int) loop
->num_nodes
);
751 /* Get body of a LOOP in breadth first sort order. */
754 get_loop_body_in_bfs_order (const struct loop
*loop
)
762 gcc_assert (loop
->num_nodes
);
763 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
765 blocks
= XCNEWVEC (basic_block
, loop
->num_nodes
);
766 visited
= BITMAP_ALLOC (NULL
);
769 while (i
< loop
->num_nodes
)
774 if (!bitmap_bit_p (visited
, bb
->index
))
776 /* This basic block is now visited */
777 bitmap_set_bit (visited
, bb
->index
);
781 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
783 if (flow_bb_inside_loop_p (loop
, e
->dest
))
785 if (!bitmap_bit_p (visited
, e
->dest
->index
))
787 bitmap_set_bit (visited
, e
->dest
->index
);
788 blocks
[i
++] = e
->dest
;
793 gcc_assert (i
>= vc
);
798 BITMAP_FREE (visited
);
802 /* Returns the list of the exit edges of a LOOP. */
805 get_loop_exit_edges (const struct loop
*loop
)
807 VEC (edge
, heap
) *edges
= NULL
;
813 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
815 body
= get_loop_body (loop
);
816 for (i
= 0; i
< loop
->num_nodes
; i
++)
817 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
818 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
819 VEC_safe_push (edge
, heap
, edges
, e
);
825 /* Counts the number of conditional branches inside LOOP. */
828 num_loop_branches (const struct loop
*loop
)
833 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
835 body
= get_loop_body (loop
);
837 for (i
= 0; i
< loop
->num_nodes
; i
++)
838 if (EDGE_COUNT (body
[i
]->succs
) >= 2)
845 /* Adds basic block BB to LOOP. */
847 add_bb_to_loop (basic_block bb
, struct loop
*loop
)
851 gcc_assert (bb
->loop_father
== NULL
);
852 bb
->loop_father
= loop
;
853 bb
->loop_depth
= loop
->depth
;
855 for (i
= 0; i
< loop
->depth
; i
++)
856 loop
->pred
[i
]->num_nodes
++;
859 /* Remove basic block BB from loops. */
861 remove_bb_from_loops (basic_block bb
)
864 struct loop
*loop
= bb
->loop_father
;
866 gcc_assert (loop
!= NULL
);
868 for (i
= 0; i
< loop
->depth
; i
++)
869 loop
->pred
[i
]->num_nodes
--;
870 bb
->loop_father
= NULL
;
874 /* Finds nearest common ancestor in loop tree for given loops. */
876 find_common_loop (struct loop
*loop_s
, struct loop
*loop_d
)
878 if (!loop_s
) return loop_d
;
879 if (!loop_d
) return loop_s
;
881 if (loop_s
->depth
< loop_d
->depth
)
882 loop_d
= loop_d
->pred
[loop_s
->depth
];
883 else if (loop_s
->depth
> loop_d
->depth
)
884 loop_s
= loop_s
->pred
[loop_d
->depth
];
886 while (loop_s
!= loop_d
)
888 loop_s
= loop_s
->outer
;
889 loop_d
= loop_d
->outer
;
894 /* Removes LOOP from structures and frees its data. */
897 delete_loop (struct loop
*loop
)
899 /* Remove the loop from structure. */
900 flow_loop_tree_node_remove (loop
);
902 /* Remove loop from loops array. */
903 VEC_replace (loop_p
, current_loops
->larray
, loop
->num
, NULL
);
905 /* Free loop data. */
906 flow_loop_free (loop
);
909 /* Cancels the LOOP; it must be innermost one. */
912 cancel_loop (struct loop
*loop
)
917 gcc_assert (!loop
->inner
);
919 /* Move blocks up one level (they should be removed as soon as possible). */
920 bbs
= get_loop_body (loop
);
921 for (i
= 0; i
< loop
->num_nodes
; i
++)
922 bbs
[i
]->loop_father
= loop
->outer
;
927 /* Cancels LOOP and all its subloops. */
929 cancel_loop_tree (struct loop
*loop
)
932 cancel_loop_tree (loop
->inner
);
936 /* Checks that information about loops is correct
937 -- sizes of loops are all right
938 -- results of get_loop_body really belong to the loop
939 -- loop header have just single entry edge and single latch edge
940 -- loop latches have only single successor that is header of their loop
941 -- irreducible loops are correctly marked
944 verify_loop_structure (void)
946 unsigned *sizes
, i
, j
;
948 basic_block
*bbs
, bb
;
952 unsigned num
= number_of_loops ();
956 sizes
= XCNEWVEC (unsigned, num
);
960 for (loop
= bb
->loop_father
; loop
; loop
= loop
->outer
)
963 FOR_EACH_LOOP (li
, loop
, LI_INCLUDE_ROOT
)
967 if (loop
->num_nodes
!= sizes
[i
])
969 error ("size of loop %d should be %d, not %d",
970 i
, sizes
[i
], loop
->num_nodes
);
975 /* Check get_loop_body. */
976 FOR_EACH_LOOP (li
, loop
, 0)
978 bbs
= get_loop_body (loop
);
980 for (j
= 0; j
< loop
->num_nodes
; j
++)
981 if (!flow_bb_inside_loop_p (loop
, bbs
[j
]))
983 error ("bb %d do not belong to loop %d",
984 bbs
[j
]->index
, loop
->num
);
990 /* Check headers and latches. */
991 FOR_EACH_LOOP (li
, loop
, 0)
995 if ((current_loops
->state
& LOOPS_HAVE_PREHEADERS
)
996 && EDGE_COUNT (loop
->header
->preds
) != 2)
998 error ("loop %d's header does not have exactly 2 entries", i
);
1001 if (current_loops
->state
& LOOPS_HAVE_SIMPLE_LATCHES
)
1003 if (!single_succ_p (loop
->latch
))
1005 error ("loop %d's latch does not have exactly 1 successor", i
);
1008 if (single_succ (loop
->latch
) != loop
->header
)
1010 error ("loop %d's latch does not have header as successor", i
);
1013 if (loop
->latch
->loop_father
!= loop
)
1015 error ("loop %d's latch does not belong directly to it", i
);
1019 if (loop
->header
->loop_father
!= loop
)
1021 error ("loop %d's header does not belong directly to it", i
);
1024 if ((current_loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1025 && (loop_latch_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
))
1027 error ("loop %d's latch is marked as part of irreducible region", i
);
1032 /* Check irreducible loops. */
1033 if (current_loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1035 /* Record old info. */
1036 irreds
= sbitmap_alloc (last_basic_block
);
1040 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1041 SET_BIT (irreds
, bb
->index
);
1043 RESET_BIT (irreds
, bb
->index
);
1044 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1045 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1046 e
->flags
|= EDGE_ALL_FLAGS
+ 1;
1050 mark_irreducible_loops ();
1057 if ((bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1058 && !TEST_BIT (irreds
, bb
->index
))
1060 error ("basic block %d should be marked irreducible", bb
->index
);
1063 else if (!(bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1064 && TEST_BIT (irreds
, bb
->index
))
1066 error ("basic block %d should not be marked irreducible", bb
->index
);
1069 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1071 if ((e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1072 && !(e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1074 error ("edge from %d to %d should be marked irreducible",
1075 e
->src
->index
, e
->dest
->index
);
1078 else if (!(e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1079 && (e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1081 error ("edge from %d to %d should not be marked irreducible",
1082 e
->src
->index
, e
->dest
->index
);
1085 e
->flags
&= ~(EDGE_ALL_FLAGS
+ 1);
1091 /* Check the single_exit. */
1092 if (current_loops
->state
& LOOPS_HAVE_MARKED_SINGLE_EXITS
)
1094 memset (sizes
, 0, sizeof (unsigned) * num
);
1098 if (bb
->loop_father
== current_loops
->tree_root
)
1100 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1102 if (e
->dest
== EXIT_BLOCK_PTR
)
1105 if (flow_bb_inside_loop_p (bb
->loop_father
, e
->dest
))
1108 for (loop
= bb
->loop_father
;
1109 loop
!= e
->dest
->loop_father
;
1113 if (single_exit (loop
)
1114 && single_exit (loop
) != e
)
1116 error ("wrong single exit %d->%d recorded for loop %d",
1117 single_exit (loop
)->src
->index
,
1118 single_exit (loop
)->dest
->index
,
1120 error ("right exit is %d->%d",
1121 e
->src
->index
, e
->dest
->index
);
1128 FOR_EACH_LOOP (li
, loop
, 0)
1133 && !single_exit (loop
))
1135 error ("single exit not recorded for loop %d", loop
->num
);
1140 && single_exit (loop
))
1142 error ("loop %d should not have single exit (%d -> %d)",
1144 single_exit (loop
)->src
->index
,
1145 single_exit (loop
)->dest
->index
);
1156 /* Returns latch edge of LOOP. */
1158 loop_latch_edge (const struct loop
*loop
)
1160 return find_edge (loop
->latch
, loop
->header
);
1163 /* Returns preheader edge of LOOP. */
1165 loop_preheader_edge (const struct loop
*loop
)
1170 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1171 if (e
->src
!= loop
->latch
)
1177 /* Returns true if E is an exit of LOOP. */
1180 loop_exit_edge_p (const struct loop
*loop
, edge e
)
1182 return (flow_bb_inside_loop_p (loop
, e
->src
)
1183 && !flow_bb_inside_loop_p (loop
, e
->dest
));
1186 /* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
1187 or more than one exit. */
1190 single_exit (const struct loop
*loop
)
1192 return loop
->single_exit_
;
1195 /* Records E as a single exit edge of LOOP. */
1198 set_single_exit (struct loop
*loop
, edge e
)
1200 loop
->single_exit_
= e
;