1 /* Natural loop discovery code for GNU compiler.
2 Copyright (C) 2000, 2001, 2003, 2004 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"
27 #include "basic-block.h"
32 #include "tree-flow.h"
34 /* Ratio of frequencies of edges so that one of more latch edges is
35 considered to belong to inner loop with same header. */
36 #define HEAVY_EDGE_RATIO 8
38 #define HEADER_BLOCK(B) (* (int *) (B)->aux)
39 #define LATCH_EDGE(E) (*(int *) (E)->aux)
41 static void flow_loops_cfg_dump (const struct loops
*, FILE *);
42 static void flow_loop_entry_edges_find (struct loop
*);
43 static void flow_loop_exit_edges_find (struct loop
*);
44 static int flow_loop_nodes_find (basic_block
, struct loop
*);
45 static void flow_loop_pre_header_scan (struct loop
*);
46 static basic_block
flow_loop_pre_header_find (basic_block
);
47 static int flow_loop_level_compute (struct loop
*);
48 static int flow_loops_level_compute (struct loops
*);
49 static void establish_preds (struct loop
*);
50 static void canonicalize_loop_headers (void);
51 static bool glb_enum_p (basic_block
, void *);
53 /* Dump loop related CFG information. */
56 flow_loops_cfg_dump (const struct loops
*loops
, FILE *file
)
61 if (! loops
->num
|| ! file
)
68 fprintf (file
, ";; %d succs { ", bb
->index
);
69 for (succ
= bb
->succ
; succ
; succ
= succ
->succ_next
)
70 fprintf (file
, "%d ", succ
->dest
->index
);
71 fprintf (file
, "}\n");
74 /* Dump the DFS node order. */
75 if (loops
->cfg
.dfs_order
)
77 fputs (";; DFS order: ", file
);
78 for (i
= 0; i
< n_basic_blocks
; i
++)
79 fprintf (file
, "%d ", loops
->cfg
.dfs_order
[i
]);
84 /* Dump the reverse completion node order. */
85 if (loops
->cfg
.rc_order
)
87 fputs (";; RC order: ", file
);
88 for (i
= 0; i
< n_basic_blocks
; i
++)
89 fprintf (file
, "%d ", loops
->cfg
.rc_order
[i
]);
95 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
98 flow_loop_nested_p (const struct loop
*outer
, const struct loop
*loop
)
100 return loop
->depth
> outer
->depth
101 && loop
->pred
[outer
->depth
] == outer
;
104 /* Dump the loop information specified by LOOP to the stream FILE
105 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
108 flow_loop_dump (const struct loop
*loop
, FILE *file
,
109 void (*loop_dump_aux
) (const struct loop
*, FILE *, int),
115 if (! loop
|| ! loop
->header
)
118 fprintf (file
, ";;\n;; Loop %d:%s\n", loop
->num
,
119 loop
->invalid
? " invalid" : "");
121 fprintf (file
, ";; header %d, latch %d, pre-header %d\n",
122 loop
->header
->index
, loop
->latch
->index
,
123 loop
->pre_header
? loop
->pre_header
->index
: -1);
124 fprintf (file
, ";; depth %d, level %d, outer %ld\n",
125 loop
->depth
, loop
->level
,
126 (long) (loop
->outer
? loop
->outer
->num
: -1));
128 if (loop
->pre_header_edges
)
129 flow_edge_list_print (";; pre-header edges", loop
->pre_header_edges
,
130 loop
->num_pre_header_edges
, file
);
132 flow_edge_list_print (";; entry edges", loop
->entry_edges
,
133 loop
->num_entries
, file
);
134 fprintf (file
, ";; nodes:");
135 bbs
= get_loop_body (loop
);
136 for (i
= 0; i
< loop
->num_nodes
; i
++)
137 fprintf (file
, " %d", bbs
[i
]->index
);
139 fprintf (file
, "\n");
140 flow_edge_list_print (";; exit edges", loop
->exit_edges
,
141 loop
->num_exits
, file
);
144 loop_dump_aux (loop
, file
, verbose
);
147 /* Dump the loop information specified by LOOPS to the stream FILE,
148 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
151 flow_loops_dump (const struct loops
*loops
, FILE *file
, void (*loop_dump_aux
) (const struct loop
*, FILE *, int), int verbose
)
156 num_loops
= loops
->num
;
157 if (! num_loops
|| ! file
)
160 fprintf (file
, ";; %d loops found, %d levels\n",
161 num_loops
, loops
->levels
);
163 for (i
= 0; i
< num_loops
; i
++)
165 struct loop
*loop
= loops
->parray
[i
];
170 flow_loop_dump (loop
, file
, loop_dump_aux
, verbose
);
174 flow_loops_cfg_dump (loops
, file
);
177 /* Free data allocated for LOOP. */
179 flow_loop_free (struct loop
*loop
)
181 if (loop
->pre_header_edges
)
182 free (loop
->pre_header_edges
);
183 if (loop
->entry_edges
)
184 free (loop
->entry_edges
);
185 if (loop
->exit_edges
)
186 free (loop
->exit_edges
);
192 /* Free all the memory allocated for LOOPS. */
195 flow_loops_free (struct loops
*loops
)
204 /* Free the loop descriptors. */
205 for (i
= 0; i
< loops
->num
; i
++)
207 struct loop
*loop
= loops
->parray
[i
];
212 flow_loop_free (loop
);
215 free (loops
->parray
);
216 loops
->parray
= NULL
;
218 if (loops
->cfg
.dfs_order
)
219 free (loops
->cfg
.dfs_order
);
220 if (loops
->cfg
.rc_order
)
221 free (loops
->cfg
.rc_order
);
226 /* Find the entry edges into the LOOP. */
229 flow_loop_entry_edges_find (struct loop
*loop
)
235 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
237 if (flow_loop_outside_edge_p (loop
, e
))
244 loop
->entry_edges
= xmalloc (num_entries
* sizeof (edge
*));
247 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
249 if (flow_loop_outside_edge_p (loop
, e
))
250 loop
->entry_edges
[num_entries
++] = e
;
253 loop
->num_entries
= num_entries
;
256 /* Find the exit edges from the LOOP. */
259 flow_loop_exit_edges_find (struct loop
*loop
)
262 basic_block node
, *bbs
;
263 unsigned num_exits
, i
;
265 loop
->exit_edges
= NULL
;
268 /* Check all nodes within the loop to see if there are any
269 successors not in the loop. Note that a node may have multiple
272 bbs
= get_loop_body (loop
);
273 for (i
= 0; i
< loop
->num_nodes
; i
++)
276 for (e
= node
->succ
; e
; e
= e
->succ_next
)
278 basic_block dest
= e
->dest
;
280 if (!flow_bb_inside_loop_p (loop
, dest
))
291 loop
->exit_edges
= xmalloc (num_exits
* sizeof (edge
*));
293 /* Store all exiting edges into an array. */
295 for (i
= 0; i
< loop
->num_nodes
; i
++)
298 for (e
= node
->succ
; e
; e
= e
->succ_next
)
300 basic_block dest
= e
->dest
;
302 if (!flow_bb_inside_loop_p (loop
, dest
))
303 loop
->exit_edges
[num_exits
++] = e
;
307 loop
->num_exits
= num_exits
;
310 /* Find the nodes contained within the LOOP with header HEADER.
311 Return the number of nodes within the loop. */
314 flow_loop_nodes_find (basic_block header
, struct loop
*loop
)
320 header
->loop_father
= loop
;
321 header
->loop_depth
= loop
->depth
;
323 if (loop
->latch
->loop_father
!= loop
)
325 stack
= xmalloc (n_basic_blocks
* sizeof (basic_block
));
328 stack
[sp
++] = loop
->latch
;
329 loop
->latch
->loop_father
= loop
;
330 loop
->latch
->loop_depth
= loop
->depth
;
339 for (e
= node
->pred
; e
; e
= e
->pred_next
)
341 basic_block ancestor
= e
->src
;
343 if (ancestor
!= ENTRY_BLOCK_PTR
344 && ancestor
->loop_father
!= loop
)
346 ancestor
->loop_father
= loop
;
347 ancestor
->loop_depth
= loop
->depth
;
349 stack
[sp
++] = ancestor
;
358 /* Find the root node of the loop pre-header extended basic block and
359 the edges along the trace from the root node to the loop header. */
362 flow_loop_pre_header_scan (struct loop
*loop
)
368 loop
->num_pre_header_edges
= 0;
369 if (loop
->num_entries
!= 1)
372 ebb
= loop
->entry_edges
[0]->src
;
373 if (ebb
== ENTRY_BLOCK_PTR
)
376 /* Count number of edges along trace from loop header to
377 root of pre-header extended basic block. Usually this is
378 only one or two edges. */
379 for (num
= 1; ebb
->pred
->src
!= ENTRY_BLOCK_PTR
&& ! ebb
->pred
->pred_next
;
381 ebb
= ebb
->pred
->src
;
383 loop
->pre_header_edges
= xmalloc (num
* sizeof (edge
));
384 loop
->num_pre_header_edges
= num
;
386 /* Store edges in order that they are followed. The source of the first edge
387 is the root node of the pre-header extended basic block and the
388 destination of the last last edge is the loop header. */
389 for (e
= loop
->entry_edges
[0]; num
; e
= e
->src
->pred
)
390 loop
->pre_header_edges
[--num
] = e
;
393 /* Return the block for the pre-header of the loop with header
394 HEADER. Return NULL if there is no pre-header. */
397 flow_loop_pre_header_find (basic_block header
)
399 basic_block pre_header
;
402 /* If block p is a predecessor of the header and is the only block
403 that the header does not dominate, then it is the pre-header. */
405 for (e
= header
->pred
; e
; e
= e
->pred_next
)
407 basic_block node
= e
->src
;
409 if (node
!= ENTRY_BLOCK_PTR
410 && ! dominated_by_p (CDI_DOMINATORS
, node
, header
))
412 if (pre_header
== NULL
)
416 /* There are multiple edges into the header from outside
417 the loop so there is no pre-header block. */
428 establish_preds (struct loop
*loop
)
430 struct loop
*ploop
, *father
= loop
->outer
;
432 loop
->depth
= father
->depth
+ 1;
435 loop
->pred
= xmalloc (sizeof (struct loop
*) * loop
->depth
);
436 memcpy (loop
->pred
, father
->pred
, sizeof (struct loop
*) * father
->depth
);
437 loop
->pred
[father
->depth
] = father
;
439 for (ploop
= loop
->inner
; ploop
; ploop
= ploop
->next
)
440 establish_preds (ploop
);
443 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
444 added loop. If LOOP has some children, take care of that their
445 pred field will be initialized correctly. */
448 flow_loop_tree_node_add (struct loop
*father
, struct loop
*loop
)
450 loop
->next
= father
->inner
;
451 father
->inner
= loop
;
452 loop
->outer
= father
;
454 establish_preds (loop
);
457 /* Remove LOOP from the loop hierarchy tree. */
460 flow_loop_tree_node_remove (struct loop
*loop
)
462 struct loop
*prev
, *father
;
464 father
= loop
->outer
;
467 /* Remove loop from the list of sons. */
468 if (father
->inner
== loop
)
469 father
->inner
= loop
->next
;
472 for (prev
= father
->inner
; prev
->next
!= loop
; prev
= prev
->next
);
473 prev
->next
= loop
->next
;
481 /* Helper function to compute loop nesting depth and enclosed loop level
482 for the natural loop specified by LOOP. Returns the loop level. */
485 flow_loop_level_compute (struct loop
*loop
)
493 /* Traverse loop tree assigning depth and computing level as the
494 maximum level of all the inner loops of this loop. The loop
495 level is equivalent to the height of the loop in the loop tree
496 and corresponds to the number of enclosed loop levels (including
498 for (inner
= loop
->inner
; inner
; inner
= inner
->next
)
500 int ilevel
= flow_loop_level_compute (inner
) + 1;
510 /* Compute the loop nesting depth and enclosed loop level for the loop
511 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
515 flow_loops_level_compute (struct loops
*loops
)
517 return flow_loop_level_compute (loops
->tree_root
);
520 /* Scan a single natural loop specified by LOOP collecting information
521 about it specified by FLAGS. */
524 flow_loop_scan (struct loop
*loop
, int flags
)
526 if (flags
& LOOP_ENTRY_EDGES
)
528 /* Find edges which enter the loop header.
529 Note that the entry edges should only
530 enter the header of a natural loop. */
531 flow_loop_entry_edges_find (loop
);
534 if (flags
& LOOP_EXIT_EDGES
)
536 /* Find edges which exit the loop. */
537 flow_loop_exit_edges_find (loop
);
540 if (flags
& LOOP_PRE_HEADER
)
542 /* Look to see if the loop has a pre-header node. */
543 loop
->pre_header
= flow_loop_pre_header_find (loop
->header
);
545 /* Find the blocks within the extended basic block of
546 the loop pre-header. */
547 flow_loop_pre_header_scan (loop
);
553 /* A callback to update latch and header info for basic block JUMP created
554 by redirecting an edge. */
557 update_latch_info (basic_block jump
)
559 alloc_aux_for_block (jump
, sizeof (int));
560 HEADER_BLOCK (jump
) = 0;
561 alloc_aux_for_edge (jump
->pred
, sizeof (int));
562 LATCH_EDGE (jump
->pred
) = 0;
565 /* A callback for make_forwarder block, to redirect all edges except for
566 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
567 whether to redirect it. */
569 static edge mfb_kj_edge
;
571 mfb_keep_just (edge e
)
573 return e
!= mfb_kj_edge
;
576 /* A callback for make_forwarder block, to redirect the latch edges into an
577 entry part. E is the edge for that we should decide whether to redirect
581 mfb_keep_nonlatch (edge e
)
583 return LATCH_EDGE (e
);
586 /* Takes care of merging natural loops with shared headers. */
589 canonicalize_loop_headers (void)
594 /* Compute the dominators. */
595 calculate_dominance_info (CDI_DOMINATORS
);
597 alloc_aux_for_blocks (sizeof (int));
598 alloc_aux_for_edges (sizeof (int));
600 /* Split blocks so that each loop has only single latch. */
604 int have_abnormal_edge
= 0;
606 for (e
= header
->pred
; e
; e
= e
->pred_next
)
608 basic_block latch
= e
->src
;
610 if (e
->flags
& EDGE_ABNORMAL
)
611 have_abnormal_edge
= 1;
613 if (latch
!= ENTRY_BLOCK_PTR
614 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
620 if (have_abnormal_edge
)
621 HEADER_BLOCK (header
) = 0;
623 HEADER_BLOCK (header
) = num_latches
;
626 free_dominance_info (CDI_DOMINATORS
);
628 if (HEADER_BLOCK (ENTRY_BLOCK_PTR
->succ
->dest
))
632 /* We could not redirect edges freely here. On the other hand,
633 we can simply split the edge from entry block. */
634 bb
= split_edge (ENTRY_BLOCK_PTR
->succ
);
636 alloc_aux_for_edge (bb
->succ
, sizeof (int));
637 LATCH_EDGE (bb
->succ
) = 0;
638 alloc_aux_for_block (bb
, sizeof (int));
639 HEADER_BLOCK (bb
) = 0;
644 int max_freq
, is_heavy
;
645 edge heavy
, tmp_edge
;
647 if (HEADER_BLOCK (header
) <= 1)
650 /* Find a heavy edge. */
654 for (e
= header
->pred
; e
; e
= e
->pred_next
)
655 if (LATCH_EDGE (e
) &&
656 EDGE_FREQUENCY (e
) > max_freq
)
657 max_freq
= EDGE_FREQUENCY (e
);
658 for (e
= header
->pred
; e
; e
= e
->pred_next
)
659 if (LATCH_EDGE (e
) &&
660 EDGE_FREQUENCY (e
) >= max_freq
/ HEAVY_EDGE_RATIO
)
673 /* Split out the heavy edge, and create inner loop for it. */
675 tmp_edge
= make_forwarder_block (header
, mfb_keep_just
,
677 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
678 HEADER_BLOCK (tmp_edge
->dest
) = 1;
679 alloc_aux_for_edge (tmp_edge
, sizeof (int));
680 LATCH_EDGE (tmp_edge
) = 0;
681 HEADER_BLOCK (header
)--;
684 if (HEADER_BLOCK (header
) > 1)
686 /* Create a new latch block. */
687 tmp_edge
= make_forwarder_block (header
, mfb_keep_nonlatch
,
689 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
690 HEADER_BLOCK (tmp_edge
->src
) = 0;
691 HEADER_BLOCK (tmp_edge
->dest
) = 1;
692 alloc_aux_for_edge (tmp_edge
, sizeof (int));
693 LATCH_EDGE (tmp_edge
) = 1;
697 free_aux_for_blocks ();
698 free_aux_for_edges ();
701 /* Find all the natural loops in the function and save in LOOPS structure and
702 recalculate loop_depth information in basic block structures. FLAGS
703 controls which loop information is collected. Return the number of natural
707 flow_loops_find (struct loops
*loops
, int flags
)
719 /* This function cannot be repeatedly called with different
720 flags to build up the loop information. The loop tree
721 must always be built if this function is called. */
722 if (! (flags
& LOOP_TREE
))
725 memset (loops
, 0, sizeof *loops
);
727 /* Taking care of this degenerate case makes the rest of
728 this code simpler. */
729 if (n_basic_blocks
== 0)
735 /* Join loops with shared headers. */
736 canonicalize_loop_headers ();
738 /* Compute the dominators. */
739 calculate_dominance_info (CDI_DOMINATORS
);
741 /* Count the number of loop headers. This should be the
742 same as the number of natural loops. */
743 headers
= sbitmap_alloc (last_basic_block
);
744 sbitmap_zero (headers
);
749 int more_latches
= 0;
751 header
->loop_depth
= 0;
753 /* If we have an abnormal predecessor, do not consider the
754 loop (not worth the problems). */
755 for (e
= header
->pred
; e
; e
= e
->pred_next
)
756 if (e
->flags
& EDGE_ABNORMAL
)
761 for (e
= header
->pred
; e
; e
= e
->pred_next
)
763 basic_block latch
= e
->src
;
765 if (e
->flags
& EDGE_ABNORMAL
)
768 /* Look for back edges where a predecessor is dominated
769 by this block. A natural loop has a single entry
770 node (header) that dominates all the nodes in the
771 loop. It also has single back edge to the header
772 from a latch node. */
773 if (latch
!= ENTRY_BLOCK_PTR
774 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
776 /* Shared headers should be eliminated by now. */
780 SET_BIT (headers
, header
->index
);
786 /* Allocate loop structures. */
787 loops
->parray
= xcalloc (num_loops
+ 1, sizeof (struct loop
*));
789 /* Dummy loop containing whole function. */
790 loops
->parray
[0] = xcalloc (1, sizeof (struct loop
));
791 loops
->parray
[0]->next
= NULL
;
792 loops
->parray
[0]->inner
= NULL
;
793 loops
->parray
[0]->outer
= NULL
;
794 loops
->parray
[0]->depth
= 0;
795 loops
->parray
[0]->pred
= NULL
;
796 loops
->parray
[0]->num_nodes
= n_basic_blocks
+ 2;
797 loops
->parray
[0]->latch
= EXIT_BLOCK_PTR
;
798 loops
->parray
[0]->header
= ENTRY_BLOCK_PTR
;
799 ENTRY_BLOCK_PTR
->loop_father
= loops
->parray
[0];
800 EXIT_BLOCK_PTR
->loop_father
= loops
->parray
[0];
802 loops
->tree_root
= loops
->parray
[0];
804 /* Find and record information about all the natural loops
808 bb
->loop_father
= loops
->tree_root
;
812 /* Compute depth first search order of the CFG so that outer
813 natural loops will be found before inner natural loops. */
814 dfs_order
= xmalloc (n_basic_blocks
* sizeof (int));
815 rc_order
= xmalloc (n_basic_blocks
* sizeof (int));
816 flow_depth_first_order_compute (dfs_order
, rc_order
);
818 /* Save CFG derived information to avoid recomputing it. */
819 loops
->cfg
.dfs_order
= dfs_order
;
820 loops
->cfg
.rc_order
= rc_order
;
824 for (b
= 0; b
< n_basic_blocks
; b
++)
828 /* Search the nodes of the CFG in reverse completion order
829 so that we can find outer loops first. */
830 if (!TEST_BIT (headers
, rc_order
[b
]))
833 header
= BASIC_BLOCK (rc_order
[b
]);
835 loop
= loops
->parray
[num_loops
] = xcalloc (1, sizeof (struct loop
));
837 loop
->header
= header
;
838 loop
->num
= num_loops
;
841 /* Look for the latch for this header block. */
842 for (e
= header
->pred
; e
; e
= e
->pred_next
)
844 basic_block latch
= e
->src
;
846 if (latch
!= ENTRY_BLOCK_PTR
847 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
854 flow_loop_tree_node_add (header
->loop_father
, loop
);
855 loop
->num_nodes
= flow_loop_nodes_find (loop
->header
, loop
);
858 /* Assign the loop nesting depth and enclosed loop level for each
860 loops
->levels
= flow_loops_level_compute (loops
);
862 /* Scan the loops. */
863 for (i
= 1; i
< num_loops
; i
++)
864 flow_loop_scan (loops
->parray
[i
], flags
);
866 loops
->num
= num_loops
;
870 free_dominance_info (CDI_DOMINATORS
);
873 sbitmap_free (headers
);
876 #ifdef ENABLE_CHECKING
878 verify_loop_structure (loops
);
884 /* Update the information regarding the loops in the CFG
885 specified by LOOPS. */
888 flow_loops_update (struct loops
*loops
, int flags
)
890 /* One day we may want to update the current loop data. For now
891 throw away the old stuff and rebuild what we need. */
893 flow_loops_free (loops
);
895 return flow_loops_find (loops
, flags
);
898 /* Return nonzero if basic block BB belongs to LOOP. */
900 flow_bb_inside_loop_p (const struct loop
*loop
, const basic_block bb
)
902 struct loop
*source_loop
;
904 if (bb
== ENTRY_BLOCK_PTR
|| bb
== EXIT_BLOCK_PTR
)
907 source_loop
= bb
->loop_father
;
908 return loop
== source_loop
|| flow_loop_nested_p (loop
, source_loop
);
911 /* Return nonzero if edge E enters header of LOOP from outside of LOOP. */
914 flow_loop_outside_edge_p (const struct loop
*loop
, edge e
)
916 if (e
->dest
!= loop
->header
)
918 return !flow_bb_inside_loop_p (loop
, e
->src
);
921 /* Enumeration predicate for get_loop_body. */
923 glb_enum_p (basic_block bb
, void *glb_header
)
925 return bb
!= (basic_block
) glb_header
;
928 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
929 order against direction of edges from latch. Specially, if
930 header != latch, latch is the 1-st block. */
932 get_loop_body (const struct loop
*loop
)
934 basic_block
*tovisit
, bb
;
937 if (!loop
->num_nodes
)
940 tovisit
= xcalloc (loop
->num_nodes
, sizeof (basic_block
));
941 tovisit
[tv
++] = loop
->header
;
943 if (loop
->latch
== EXIT_BLOCK_PTR
)
945 /* There may be blocks unreachable from EXIT_BLOCK. */
946 if (loop
->num_nodes
!= (unsigned) n_basic_blocks
+ 2)
950 tovisit
[tv
++] = EXIT_BLOCK_PTR
;
952 else if (loop
->latch
!= loop
->header
)
954 tv
= dfs_enumerate_from (loop
->latch
, 1, glb_enum_p
,
955 tovisit
+ 1, loop
->num_nodes
- 1,
959 if (tv
!= loop
->num_nodes
)
964 /* Fills dominance descendants inside LOOP of the basic block BB into
965 array TOVISIT from index *TV. */
968 fill_sons_in_loop (const struct loop
*loop
, basic_block bb
,
969 basic_block
*tovisit
, int *tv
)
971 basic_block son
, postpone
= NULL
;
973 tovisit
[(*tv
)++] = bb
;
974 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
976 son
= next_dom_son (CDI_DOMINATORS
, son
))
978 if (!flow_bb_inside_loop_p (loop
, son
))
981 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, son
))
986 fill_sons_in_loop (loop
, son
, tovisit
, tv
);
990 fill_sons_in_loop (loop
, postpone
, tovisit
, tv
);
993 /* Gets body of a LOOP (that must be different from the outermost loop)
994 sorted by dominance relation. Additionally, if a basic block s dominates
995 the latch, then only blocks dominated by s are be after it. */
998 get_loop_body_in_dom_order (const struct loop
*loop
)
1000 basic_block
*tovisit
;
1003 if (!loop
->num_nodes
)
1006 tovisit
= xcalloc (loop
->num_nodes
, sizeof (basic_block
));
1008 if (loop
->latch
== EXIT_BLOCK_PTR
)
1012 fill_sons_in_loop (loop
, loop
->header
, tovisit
, &tv
);
1014 if (tv
!= (int) loop
->num_nodes
)
1020 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
1022 get_loop_exit_edges (const struct loop
*loop
, unsigned int *n_edges
)
1028 if (loop
->latch
== EXIT_BLOCK_PTR
)
1031 body
= get_loop_body (loop
);
1033 for (i
= 0; i
< loop
->num_nodes
; i
++)
1034 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
1035 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
1037 edges
= xmalloc (n
* sizeof (edge
));
1040 for (i
= 0; i
< loop
->num_nodes
; i
++)
1041 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
1042 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
1049 /* Counts the number of conditional branches inside LOOP. */
1052 num_loop_branches (const struct loop
*loop
)
1057 if (loop
->latch
== EXIT_BLOCK_PTR
)
1060 body
= get_loop_body (loop
);
1062 for (i
= 0; i
< loop
->num_nodes
; i
++)
1063 if (body
[i
]->succ
&& body
[i
]->succ
->succ_next
)
1070 /* Adds basic block BB to LOOP. */
1072 add_bb_to_loop (basic_block bb
, struct loop
*loop
)
1076 bb
->loop_father
= loop
;
1077 bb
->loop_depth
= loop
->depth
;
1079 for (i
= 0; i
< loop
->depth
; i
++)
1080 loop
->pred
[i
]->num_nodes
++;
1083 /* Remove basic block BB from loops. */
1085 remove_bb_from_loops (basic_block bb
)
1088 struct loop
*loop
= bb
->loop_father
;
1091 for (i
= 0; i
< loop
->depth
; i
++)
1092 loop
->pred
[i
]->num_nodes
--;
1093 bb
->loop_father
= NULL
;
1097 /* Finds nearest common ancestor in loop tree for given loops. */
1099 find_common_loop (struct loop
*loop_s
, struct loop
*loop_d
)
1101 if (!loop_s
) return loop_d
;
1102 if (!loop_d
) return loop_s
;
1104 if (loop_s
->depth
< loop_d
->depth
)
1105 loop_d
= loop_d
->pred
[loop_s
->depth
];
1106 else if (loop_s
->depth
> loop_d
->depth
)
1107 loop_s
= loop_s
->pred
[loop_d
->depth
];
1109 while (loop_s
!= loop_d
)
1111 loop_s
= loop_s
->outer
;
1112 loop_d
= loop_d
->outer
;
1117 /* Cancels the LOOP; it must be innermost one. */
1119 cancel_loop (struct loops
*loops
, struct loop
*loop
)
1127 /* Move blocks up one level (they should be removed as soon as possible). */
1128 bbs
= get_loop_body (loop
);
1129 for (i
= 0; i
< loop
->num_nodes
; i
++)
1130 bbs
[i
]->loop_father
= loop
->outer
;
1132 /* Remove the loop from structure. */
1133 flow_loop_tree_node_remove (loop
);
1135 /* Remove loop from loops array. */
1136 loops
->parray
[loop
->num
] = NULL
;
1138 /* Free loop data. */
1139 flow_loop_free (loop
);
1142 /* Cancels LOOP and all its subloops. */
1144 cancel_loop_tree (struct loops
*loops
, struct loop
*loop
)
1147 cancel_loop_tree (loops
, loop
->inner
);
1148 cancel_loop (loops
, loop
);
1151 /* Checks that LOOPS are all right:
1152 -- sizes of loops are all right
1153 -- results of get_loop_body really belong to the loop
1154 -- loop header have just single entry edge and single latch edge
1155 -- loop latches have only single successor that is header of their loop
1156 -- irreducible loops are correctly marked
1159 verify_loop_structure (struct loops
*loops
)
1161 unsigned *sizes
, i
, j
;
1163 basic_block
*bbs
, bb
;
1169 sizes
= xcalloc (loops
->num
, sizeof (int));
1173 for (loop
= bb
->loop_father
; loop
; loop
= loop
->outer
)
1176 for (i
= 0; i
< loops
->num
; i
++)
1178 if (!loops
->parray
[i
])
1181 if (loops
->parray
[i
]->num_nodes
!= sizes
[i
])
1183 error ("Size of loop %d should be %d, not %d.",
1184 i
, sizes
[i
], loops
->parray
[i
]->num_nodes
);
1191 /* Check get_loop_body. */
1192 for (i
= 1; i
< loops
->num
; i
++)
1194 loop
= loops
->parray
[i
];
1197 bbs
= get_loop_body (loop
);
1199 for (j
= 0; j
< loop
->num_nodes
; j
++)
1200 if (!flow_bb_inside_loop_p (loop
, bbs
[j
]))
1202 error ("Bb %d do not belong to loop %d.",
1209 /* Check headers and latches. */
1210 for (i
= 1; i
< loops
->num
; i
++)
1212 loop
= loops
->parray
[i
];
1216 if ((loops
->state
& LOOPS_HAVE_PREHEADERS
)
1217 && (!loop
->header
->pred
->pred_next
1218 || loop
->header
->pred
->pred_next
->pred_next
))
1220 error ("Loop %d's header does not have exactly 2 entries.", i
);
1223 if (loops
->state
& LOOPS_HAVE_SIMPLE_LATCHES
)
1225 if (!loop
->latch
->succ
1226 || loop
->latch
->succ
->succ_next
)
1228 error ("Loop %d's latch does not have exactly 1 successor.", i
);
1231 if (loop
->latch
->succ
->dest
!= loop
->header
)
1233 error ("Loop %d's latch does not have header as successor.", i
);
1236 if (loop
->latch
->loop_father
!= loop
)
1238 error ("Loop %d's latch does not belong directly to it.", i
);
1242 if (loop
->header
->loop_father
!= loop
)
1244 error ("Loop %d's header does not belong directly to it.", i
);
1247 if ((loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1248 && (loop_latch_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
))
1250 error ("Loop %d's latch is marked as part of irreducible region.", i
);
1255 /* Check irreducible loops. */
1256 if (loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1258 /* Record old info. */
1259 irreds
= sbitmap_alloc (last_basic_block
);
1262 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1263 SET_BIT (irreds
, bb
->index
);
1265 RESET_BIT (irreds
, bb
->index
);
1266 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1267 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1268 e
->flags
|= EDGE_ALL_FLAGS
+ 1;
1272 mark_irreducible_loops (loops
);
1277 if ((bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1278 && !TEST_BIT (irreds
, bb
->index
))
1280 error ("Basic block %d should be marked irreducible.", bb
->index
);
1283 else if (!(bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1284 && TEST_BIT (irreds
, bb
->index
))
1286 error ("Basic block %d should not be marked irreducible.", bb
->index
);
1289 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1291 if ((e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1292 && !(e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1294 error ("Edge from %d to %d should be marked irreducible.",
1295 e
->src
->index
, e
->dest
->index
);
1298 else if (!(e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1299 && (e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1301 error ("Edge from %d to %d should not be marked irreducible.",
1302 e
->src
->index
, e
->dest
->index
);
1305 e
->flags
&= ~(EDGE_ALL_FLAGS
+ 1);
1315 /* Returns latch edge of LOOP. */
1317 loop_latch_edge (const struct loop
*loop
)
1321 for (e
= loop
->header
->pred
; e
->src
!= loop
->latch
; e
= e
->pred_next
)
1327 /* Returns preheader edge of LOOP. */
1329 loop_preheader_edge (const struct loop
*loop
)
1333 for (e
= loop
->header
->pred
; e
->src
== loop
->latch
; e
= e
->pred_next
)