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
)
133 if (!current_loops
|| ! file
)
136 fprintf (file
, ";; %d loops found\n", current_loops
->num
);
138 for (i
= 0; i
< current_loops
->num
; i
++)
140 struct loop
*loop
= current_loops
->parray
[i
];
145 flow_loop_dump (loop
, file
, loop_dump_aux
, verbose
);
149 flow_loops_cfg_dump (file
);
152 /* Free data allocated for LOOP. */
154 flow_loop_free (struct loop
*loop
)
161 /* Free all the memory allocated for LOOPS. */
164 flow_loops_free (struct loops
*loops
)
170 gcc_assert (loops
->num
);
172 /* Free the loop descriptors. */
173 for (i
= 0; i
< loops
->num
; i
++)
175 struct loop
*loop
= loops
->parray
[i
];
180 flow_loop_free (loop
);
183 free (loops
->parray
);
184 loops
->parray
= NULL
;
188 /* Find the nodes contained within the LOOP with header HEADER.
189 Return the number of nodes within the loop. */
192 flow_loop_nodes_find (basic_block header
, struct loop
*loop
)
198 header
->loop_father
= loop
;
199 header
->loop_depth
= loop
->depth
;
201 if (loop
->latch
->loop_father
!= loop
)
203 stack
= XNEWVEC (basic_block
, n_basic_blocks
);
206 stack
[sp
++] = loop
->latch
;
207 loop
->latch
->loop_father
= loop
;
208 loop
->latch
->loop_depth
= loop
->depth
;
218 FOR_EACH_EDGE (e
, ei
, node
->preds
)
220 basic_block ancestor
= e
->src
;
222 if (ancestor
!= ENTRY_BLOCK_PTR
223 && ancestor
->loop_father
!= loop
)
225 ancestor
->loop_father
= loop
;
226 ancestor
->loop_depth
= loop
->depth
;
228 stack
[sp
++] = ancestor
;
237 /* For each loop that has just a single exit, record the exit edge. */
240 mark_single_exit_loops (void)
247 for (i
= 1; i
< current_loops
->num
; i
++)
249 loop
= current_loops
->parray
[i
];
251 set_single_exit (loop
, NULL
);
257 if (bb
->loop_father
== current_loops
->tree_root
)
259 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
261 if (e
->dest
== EXIT_BLOCK_PTR
)
264 if (flow_bb_inside_loop_p (bb
->loop_father
, e
->dest
))
267 for (loop
= bb
->loop_father
;
268 loop
!= e
->dest
->loop_father
;
271 /* If we have already seen an exit, mark this by the edge that
272 surely does not occur as any exit. */
273 if (single_exit (loop
))
274 set_single_exit (loop
, single_succ_edge (ENTRY_BLOCK_PTR
));
276 set_single_exit (loop
, e
);
281 for (i
= 1; i
< current_loops
->num
; i
++)
283 loop
= current_loops
->parray
[i
];
287 if (single_exit (loop
) == single_succ_edge (ENTRY_BLOCK_PTR
))
288 set_single_exit (loop
, NULL
);
291 current_loops
->state
|= LOOPS_HAVE_MARKED_SINGLE_EXITS
;
295 establish_preds (struct loop
*loop
)
297 struct loop
*ploop
, *father
= loop
->outer
;
299 loop
->depth
= father
->depth
+ 1;
301 /* Remember the current loop depth if it is the largest seen so far. */
302 cfun
->max_loop_depth
= MAX (cfun
->max_loop_depth
, loop
->depth
);
306 loop
->pred
= XNEWVEC (struct loop
*, loop
->depth
);
307 memcpy (loop
->pred
, father
->pred
, sizeof (struct loop
*) * father
->depth
);
308 loop
->pred
[father
->depth
] = father
;
310 for (ploop
= loop
->inner
; ploop
; ploop
= ploop
->next
)
311 establish_preds (ploop
);
314 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
315 added loop. If LOOP has some children, take care of that their
316 pred field will be initialized correctly. */
319 flow_loop_tree_node_add (struct loop
*father
, struct loop
*loop
)
321 loop
->next
= father
->inner
;
322 father
->inner
= loop
;
323 loop
->outer
= father
;
325 establish_preds (loop
);
328 /* Remove LOOP from the loop hierarchy tree. */
331 flow_loop_tree_node_remove (struct loop
*loop
)
333 struct loop
*prev
, *father
;
335 father
= loop
->outer
;
338 /* Remove loop from the list of sons. */
339 if (father
->inner
== loop
)
340 father
->inner
= loop
->next
;
343 for (prev
= father
->inner
; prev
->next
!= loop
; prev
= prev
->next
);
344 prev
->next
= loop
->next
;
352 /* A callback to update latch and header info for basic block JUMP created
353 by redirecting an edge. */
356 update_latch_info (basic_block jump
)
358 alloc_aux_for_block (jump
, sizeof (int));
359 HEADER_BLOCK (jump
) = 0;
360 alloc_aux_for_edge (single_pred_edge (jump
), sizeof (int));
361 LATCH_EDGE (single_pred_edge (jump
)) = 0;
362 set_immediate_dominator (CDI_DOMINATORS
, jump
, single_pred (jump
));
365 /* A callback for make_forwarder block, to redirect all edges except for
366 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
367 whether to redirect it. */
369 static edge mfb_kj_edge
;
371 mfb_keep_just (edge e
)
373 return e
!= mfb_kj_edge
;
376 /* A callback for make_forwarder block, to redirect the latch edges into an
377 entry part. E is the edge for that we should decide whether to redirect
381 mfb_keep_nonlatch (edge e
)
383 return LATCH_EDGE (e
);
386 /* Takes care of merging natural loops with shared headers. */
389 canonicalize_loop_headers (void)
394 alloc_aux_for_blocks (sizeof (int));
395 alloc_aux_for_edges (sizeof (int));
397 /* Split blocks so that each loop has only single latch. */
402 int have_abnormal_edge
= 0;
404 FOR_EACH_EDGE (e
, ei
, header
->preds
)
406 basic_block latch
= e
->src
;
408 if (e
->flags
& EDGE_ABNORMAL
)
409 have_abnormal_edge
= 1;
411 if (latch
!= ENTRY_BLOCK_PTR
412 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
418 if (have_abnormal_edge
)
419 HEADER_BLOCK (header
) = 0;
421 HEADER_BLOCK (header
) = num_latches
;
424 if (HEADER_BLOCK (single_succ (ENTRY_BLOCK_PTR
)))
428 /* We could not redirect edges freely here. On the other hand,
429 we can simply split the edge from entry block. */
430 bb
= split_edge (single_succ_edge (ENTRY_BLOCK_PTR
));
432 alloc_aux_for_edge (single_succ_edge (bb
), sizeof (int));
433 LATCH_EDGE (single_succ_edge (bb
)) = 0;
434 alloc_aux_for_block (bb
, sizeof (int));
435 HEADER_BLOCK (bb
) = 0;
440 int max_freq
, is_heavy
;
441 edge heavy
, tmp_edge
;
444 if (HEADER_BLOCK (header
) <= 1)
447 /* Find a heavy edge. */
451 FOR_EACH_EDGE (e
, ei
, header
->preds
)
452 if (LATCH_EDGE (e
) &&
453 EDGE_FREQUENCY (e
) > max_freq
)
454 max_freq
= EDGE_FREQUENCY (e
);
455 FOR_EACH_EDGE (e
, ei
, header
->preds
)
456 if (LATCH_EDGE (e
) &&
457 EDGE_FREQUENCY (e
) >= max_freq
/ HEAVY_EDGE_RATIO
)
470 /* Split out the heavy edge, and create inner loop for it. */
472 tmp_edge
= make_forwarder_block (header
, mfb_keep_just
,
474 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
475 HEADER_BLOCK (tmp_edge
->dest
) = 1;
476 alloc_aux_for_edge (tmp_edge
, sizeof (int));
477 LATCH_EDGE (tmp_edge
) = 0;
478 HEADER_BLOCK (header
)--;
481 if (HEADER_BLOCK (header
) > 1)
483 /* Create a new latch block. */
484 tmp_edge
= make_forwarder_block (header
, mfb_keep_nonlatch
,
486 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
487 HEADER_BLOCK (tmp_edge
->src
) = 0;
488 HEADER_BLOCK (tmp_edge
->dest
) = 1;
489 alloc_aux_for_edge (tmp_edge
, sizeof (int));
490 LATCH_EDGE (tmp_edge
) = 1;
494 free_aux_for_blocks ();
495 free_aux_for_edges ();
497 #ifdef ENABLE_CHECKING
498 verify_dominators (CDI_DOMINATORS
);
502 /* Initialize all the parallel_p fields of the loops structure to true. */
505 initialize_loops_parallel_p (struct loops
*loops
)
509 for (i
= 0; i
< loops
->num
; i
++)
511 struct loop
*loop
= loops
->parray
[i
];
512 loop
->parallel_p
= true;
516 /* Find all the natural loops in the function and save in LOOPS structure and
517 recalculate loop_depth information in basic block structures.
518 Return the number of natural loops found. */
521 flow_loops_find (struct loops
*loops
)
532 memset (loops
, 0, sizeof *loops
);
534 /* We are going to recount the maximum loop depth,
535 so throw away the last count. */
536 cfun
->max_loop_depth
= 0;
538 /* Taking care of this degenerate case makes the rest of
539 this code simpler. */
540 if (n_basic_blocks
== NUM_FIXED_BLOCKS
)
546 /* Ensure that the dominators are computed. */
547 calculate_dominance_info (CDI_DOMINATORS
);
549 /* Join loops with shared headers. */
550 canonicalize_loop_headers ();
552 /* Count the number of loop headers. This should be the
553 same as the number of natural loops. */
554 headers
= sbitmap_alloc (last_basic_block
);
555 sbitmap_zero (headers
);
561 int more_latches
= 0;
563 header
->loop_depth
= 0;
565 /* If we have an abnormal predecessor, do not consider the
566 loop (not worth the problems). */
567 FOR_EACH_EDGE (e
, ei
, header
->preds
)
568 if (e
->flags
& EDGE_ABNORMAL
)
573 FOR_EACH_EDGE (e
, ei
, header
->preds
)
575 basic_block latch
= e
->src
;
577 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
579 /* Look for back edges where a predecessor is dominated
580 by this block. A natural loop has a single entry
581 node (header) that dominates all the nodes in the
582 loop. It also has single back edge to the header
583 from a latch node. */
584 if (latch
!= ENTRY_BLOCK_PTR
585 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
587 /* Shared headers should be eliminated by now. */
588 gcc_assert (!more_latches
);
590 SET_BIT (headers
, header
->index
);
596 /* Allocate loop structures. */
597 loops
->parray
= XCNEWVEC (struct loop
*, num_loops
+ 1);
599 /* Dummy loop containing whole function. */
600 loops
->parray
[0] = XCNEW (struct loop
);
601 loops
->parray
[0]->next
= NULL
;
602 loops
->parray
[0]->inner
= NULL
;
603 loops
->parray
[0]->outer
= NULL
;
604 loops
->parray
[0]->depth
= 0;
605 loops
->parray
[0]->pred
= NULL
;
606 loops
->parray
[0]->num_nodes
= n_basic_blocks
;
607 loops
->parray
[0]->latch
= EXIT_BLOCK_PTR
;
608 loops
->parray
[0]->header
= ENTRY_BLOCK_PTR
;
609 ENTRY_BLOCK_PTR
->loop_father
= loops
->parray
[0];
610 EXIT_BLOCK_PTR
->loop_father
= loops
->parray
[0];
612 loops
->tree_root
= loops
->parray
[0];
614 /* Find and record information about all the natural loops
618 bb
->loop_father
= loops
->tree_root
;
622 /* Compute depth first search order of the CFG so that outer
623 natural loops will be found before inner natural loops. */
624 dfs_order
= XNEWVEC (int, n_basic_blocks
);
625 rc_order
= XNEWVEC (int, n_basic_blocks
);
626 pre_and_rev_post_order_compute (dfs_order
, rc_order
, false);
630 for (b
= 0; b
< n_basic_blocks
- NUM_FIXED_BLOCKS
; b
++)
635 /* Search the nodes of the CFG in reverse completion order
636 so that we can find outer loops first. */
637 if (!TEST_BIT (headers
, rc_order
[b
]))
640 header
= BASIC_BLOCK (rc_order
[b
]);
642 loop
= loops
->parray
[num_loops
] = XCNEW (struct loop
);
644 loop
->header
= header
;
645 loop
->num
= num_loops
;
648 /* Look for the latch for this header block. */
649 FOR_EACH_EDGE (e
, ei
, header
->preds
)
651 basic_block latch
= e
->src
;
653 if (latch
!= ENTRY_BLOCK_PTR
654 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
661 flow_loop_tree_node_add (header
->loop_father
, loop
);
662 loop
->num_nodes
= flow_loop_nodes_find (loop
->header
, loop
);
665 loops
->num
= num_loops
;
666 initialize_loops_parallel_p (loops
);
672 sbitmap_free (headers
);
678 /* Return nonzero if basic block BB belongs to LOOP. */
680 flow_bb_inside_loop_p (const struct loop
*loop
, const basic_block bb
)
682 struct loop
*source_loop
;
684 if (bb
== ENTRY_BLOCK_PTR
|| bb
== EXIT_BLOCK_PTR
)
687 source_loop
= bb
->loop_father
;
688 return loop
== source_loop
|| flow_loop_nested_p (loop
, source_loop
);
691 /* Enumeration predicate for get_loop_body. */
693 glb_enum_p (basic_block bb
, void *glb_header
)
695 return bb
!= (basic_block
) glb_header
;
698 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
699 order against direction of edges from latch. Specially, if
700 header != latch, latch is the 1-st block. */
702 get_loop_body (const struct loop
*loop
)
704 basic_block
*tovisit
, bb
;
707 gcc_assert (loop
->num_nodes
);
709 tovisit
= XCNEWVEC (basic_block
, loop
->num_nodes
);
710 tovisit
[tv
++] = loop
->header
;
712 if (loop
->latch
== EXIT_BLOCK_PTR
)
714 /* There may be blocks unreachable from EXIT_BLOCK. */
715 gcc_assert (loop
->num_nodes
== (unsigned) n_basic_blocks
);
718 tovisit
[tv
++] = EXIT_BLOCK_PTR
;
720 else if (loop
->latch
!= loop
->header
)
722 tv
= dfs_enumerate_from (loop
->latch
, 1, glb_enum_p
,
723 tovisit
+ 1, loop
->num_nodes
- 1,
727 gcc_assert (tv
== loop
->num_nodes
);
731 /* Fills dominance descendants inside LOOP of the basic block BB into
732 array TOVISIT from index *TV. */
735 fill_sons_in_loop (const struct loop
*loop
, basic_block bb
,
736 basic_block
*tovisit
, int *tv
)
738 basic_block son
, postpone
= NULL
;
740 tovisit
[(*tv
)++] = bb
;
741 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
743 son
= next_dom_son (CDI_DOMINATORS
, son
))
745 if (!flow_bb_inside_loop_p (loop
, son
))
748 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, son
))
753 fill_sons_in_loop (loop
, son
, tovisit
, tv
);
757 fill_sons_in_loop (loop
, postpone
, tovisit
, tv
);
760 /* Gets body of a LOOP (that must be different from the outermost loop)
761 sorted by dominance relation. Additionally, if a basic block s dominates
762 the latch, then only blocks dominated by s are be after it. */
765 get_loop_body_in_dom_order (const struct loop
*loop
)
767 basic_block
*tovisit
;
770 gcc_assert (loop
->num_nodes
);
772 tovisit
= XCNEWVEC (basic_block
, loop
->num_nodes
);
774 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
777 fill_sons_in_loop (loop
, loop
->header
, tovisit
, &tv
);
779 gcc_assert (tv
== (int) loop
->num_nodes
);
784 /* Get body of a LOOP in breadth first sort order. */
787 get_loop_body_in_bfs_order (const struct loop
*loop
)
795 gcc_assert (loop
->num_nodes
);
796 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
798 blocks
= XCNEWVEC (basic_block
, loop
->num_nodes
);
799 visited
= BITMAP_ALLOC (NULL
);
802 while (i
< loop
->num_nodes
)
807 if (!bitmap_bit_p (visited
, bb
->index
))
809 /* This basic block is now visited */
810 bitmap_set_bit (visited
, bb
->index
);
814 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
816 if (flow_bb_inside_loop_p (loop
, e
->dest
))
818 if (!bitmap_bit_p (visited
, e
->dest
->index
))
820 bitmap_set_bit (visited
, e
->dest
->index
);
821 blocks
[i
++] = e
->dest
;
826 gcc_assert (i
>= vc
);
831 BITMAP_FREE (visited
);
835 /* Returns the list of the exit edges of a LOOP. */
838 get_loop_exit_edges (const struct loop
*loop
)
840 VEC (edge
, heap
) *edges
= NULL
;
846 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
848 body
= get_loop_body (loop
);
849 for (i
= 0; i
< loop
->num_nodes
; i
++)
850 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
851 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
852 VEC_safe_push (edge
, heap
, edges
, e
);
858 /* Counts the number of conditional branches inside LOOP. */
861 num_loop_branches (const struct loop
*loop
)
866 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
868 body
= get_loop_body (loop
);
870 for (i
= 0; i
< loop
->num_nodes
; i
++)
871 if (EDGE_COUNT (body
[i
]->succs
) >= 2)
878 /* Adds basic block BB to LOOP. */
880 add_bb_to_loop (basic_block bb
, struct loop
*loop
)
884 gcc_assert (bb
->loop_father
== NULL
);
885 bb
->loop_father
= loop
;
886 bb
->loop_depth
= loop
->depth
;
888 for (i
= 0; i
< loop
->depth
; i
++)
889 loop
->pred
[i
]->num_nodes
++;
892 /* Remove basic block BB from loops. */
894 remove_bb_from_loops (basic_block bb
)
897 struct loop
*loop
= bb
->loop_father
;
899 gcc_assert (loop
!= NULL
);
901 for (i
= 0; i
< loop
->depth
; i
++)
902 loop
->pred
[i
]->num_nodes
--;
903 bb
->loop_father
= NULL
;
907 /* Finds nearest common ancestor in loop tree for given loops. */
909 find_common_loop (struct loop
*loop_s
, struct loop
*loop_d
)
911 if (!loop_s
) return loop_d
;
912 if (!loop_d
) return loop_s
;
914 if (loop_s
->depth
< loop_d
->depth
)
915 loop_d
= loop_d
->pred
[loop_s
->depth
];
916 else if (loop_s
->depth
> loop_d
->depth
)
917 loop_s
= loop_s
->pred
[loop_d
->depth
];
919 while (loop_s
!= loop_d
)
921 loop_s
= loop_s
->outer
;
922 loop_d
= loop_d
->outer
;
927 /* Cancels the LOOP; it must be innermost one. */
930 cancel_loop (struct loop
*loop
)
935 gcc_assert (!loop
->inner
);
937 /* Move blocks up one level (they should be removed as soon as possible). */
938 bbs
= get_loop_body (loop
);
939 for (i
= 0; i
< loop
->num_nodes
; i
++)
940 bbs
[i
]->loop_father
= loop
->outer
;
942 /* Remove the loop from structure. */
943 flow_loop_tree_node_remove (loop
);
945 /* Remove loop from loops array. */
946 current_loops
->parray
[loop
->num
] = NULL
;
948 /* Free loop data. */
949 flow_loop_free (loop
);
952 /* Cancels LOOP and all its subloops. */
954 cancel_loop_tree (struct loop
*loop
)
957 cancel_loop_tree (loop
->inner
);
961 /* Checks that information about loops is correct
962 -- sizes of loops are all right
963 -- results of get_loop_body really belong to the loop
964 -- loop header have just single entry edge and single latch edge
965 -- loop latches have only single successor that is header of their loop
966 -- irreducible loops are correctly marked
969 verify_loop_structure (void)
971 unsigned *sizes
, i
, j
;
973 basic_block
*bbs
, bb
;
979 sizes
= XCNEWVEC (unsigned, current_loops
->num
);
983 for (loop
= bb
->loop_father
; loop
; loop
= loop
->outer
)
986 for (i
= 0; i
< current_loops
->num
; i
++)
988 if (!current_loops
->parray
[i
])
991 if (current_loops
->parray
[i
]->num_nodes
!= sizes
[i
])
993 error ("size of loop %d should be %d, not %d",
994 i
, sizes
[i
], current_loops
->parray
[i
]->num_nodes
);
999 /* Check get_loop_body. */
1000 for (i
= 1; i
< current_loops
->num
; i
++)
1002 loop
= current_loops
->parray
[i
];
1005 bbs
= get_loop_body (loop
);
1007 for (j
= 0; j
< loop
->num_nodes
; j
++)
1008 if (!flow_bb_inside_loop_p (loop
, bbs
[j
]))
1010 error ("bb %d do not belong to loop %d",
1017 /* Check headers and latches. */
1018 for (i
= 1; i
< current_loops
->num
; i
++)
1020 loop
= current_loops
->parray
[i
];
1024 if ((current_loops
->state
& LOOPS_HAVE_PREHEADERS
)
1025 && EDGE_COUNT (loop
->header
->preds
) != 2)
1027 error ("loop %d's header does not have exactly 2 entries", i
);
1030 if (current_loops
->state
& LOOPS_HAVE_SIMPLE_LATCHES
)
1032 if (!single_succ_p (loop
->latch
))
1034 error ("loop %d's latch does not have exactly 1 successor", i
);
1037 if (single_succ (loop
->latch
) != loop
->header
)
1039 error ("loop %d's latch does not have header as successor", i
);
1042 if (loop
->latch
->loop_father
!= loop
)
1044 error ("loop %d's latch does not belong directly to it", i
);
1048 if (loop
->header
->loop_father
!= loop
)
1050 error ("loop %d's header does not belong directly to it", i
);
1053 if ((current_loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1054 && (loop_latch_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
))
1056 error ("loop %d's latch is marked as part of irreducible region", i
);
1061 /* Check irreducible loops. */
1062 if (current_loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1064 /* Record old info. */
1065 irreds
= sbitmap_alloc (last_basic_block
);
1069 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1070 SET_BIT (irreds
, bb
->index
);
1072 RESET_BIT (irreds
, bb
->index
);
1073 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1074 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1075 e
->flags
|= EDGE_ALL_FLAGS
+ 1;
1079 mark_irreducible_loops ();
1086 if ((bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1087 && !TEST_BIT (irreds
, bb
->index
))
1089 error ("basic block %d should be marked irreducible", bb
->index
);
1092 else if (!(bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1093 && TEST_BIT (irreds
, bb
->index
))
1095 error ("basic block %d should not be marked irreducible", bb
->index
);
1098 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1100 if ((e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1101 && !(e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1103 error ("edge from %d to %d should be marked irreducible",
1104 e
->src
->index
, e
->dest
->index
);
1107 else if (!(e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1108 && (e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1110 error ("edge from %d to %d should not be marked irreducible",
1111 e
->src
->index
, e
->dest
->index
);
1114 e
->flags
&= ~(EDGE_ALL_FLAGS
+ 1);
1120 /* Check the single_exit. */
1121 if (current_loops
->state
& LOOPS_HAVE_MARKED_SINGLE_EXITS
)
1123 memset (sizes
, 0, sizeof (unsigned) * current_loops
->num
);
1127 if (bb
->loop_father
== current_loops
->tree_root
)
1129 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1131 if (e
->dest
== EXIT_BLOCK_PTR
)
1134 if (flow_bb_inside_loop_p (bb
->loop_father
, e
->dest
))
1137 for (loop
= bb
->loop_father
;
1138 loop
!= e
->dest
->loop_father
;
1142 if (single_exit (loop
)
1143 && single_exit (loop
) != e
)
1145 error ("wrong single exit %d->%d recorded for loop %d",
1146 single_exit (loop
)->src
->index
,
1147 single_exit (loop
)->dest
->index
,
1149 error ("right exit is %d->%d",
1150 e
->src
->index
, e
->dest
->index
);
1157 for (i
= 1; i
< current_loops
->num
; i
++)
1159 loop
= current_loops
->parray
[i
];
1164 && !single_exit (loop
))
1166 error ("single exit not recorded for loop %d", loop
->num
);
1171 && single_exit (loop
))
1173 error ("loop %d should not have single exit (%d -> %d)",
1175 single_exit (loop
)->src
->index
,
1176 single_exit (loop
)->dest
->index
);
1187 /* Returns latch edge of LOOP. */
1189 loop_latch_edge (const struct loop
*loop
)
1191 return find_edge (loop
->latch
, loop
->header
);
1194 /* Returns preheader edge of LOOP. */
1196 loop_preheader_edge (const struct loop
*loop
)
1201 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1202 if (e
->src
!= loop
->latch
)
1208 /* Returns true if E is an exit of LOOP. */
1211 loop_exit_edge_p (const struct loop
*loop
, edge e
)
1213 return (flow_bb_inside_loop_p (loop
, e
->src
)
1214 && !flow_bb_inside_loop_p (loop
, e
->dest
));
1217 /* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
1218 or more than one exit. */
1221 single_exit (const struct loop
*loop
)
1223 return loop
->single_exit_
;
1226 /* Records E as a single exit edge of LOOP. */
1229 set_single_exit (struct loop
*loop
, edge e
)
1231 loop
->single_exit_
= e
;