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 /* Ratio of frequencies of edges so that one of more latch edges is
33 considered to belong to inner loop with same header. */
34 #define HEAVY_EDGE_RATIO 8
36 #define HEADER_BLOCK(B) (* (int *) (B)->aux)
37 #define LATCH_EDGE(E) (*(int *) (E)->aux)
39 static void flow_loops_cfg_dump (const struct loops
*, FILE *);
40 static void flow_loop_entry_edges_find (struct loop
*);
41 static void flow_loop_exit_edges_find (struct loop
*);
42 static int flow_loop_nodes_find (basic_block
, struct loop
*);
43 static void flow_loop_pre_header_scan (struct loop
*);
44 static basic_block
flow_loop_pre_header_find (basic_block
);
45 static int flow_loop_level_compute (struct loop
*);
46 static int flow_loops_level_compute (struct loops
*);
47 static void establish_preds (struct loop
*);
48 static void canonicalize_loop_headers (void);
49 static bool glb_enum_p (basic_block
, void *);
51 /* Dump loop related CFG information. */
54 flow_loops_cfg_dump (const struct loops
*loops
, FILE *file
)
59 if (! loops
->num
|| ! file
)
66 fprintf (file
, ";; %d succs { ", bb
->index
);
67 for (succ
= bb
->succ
; succ
; succ
= succ
->succ_next
)
68 fprintf (file
, "%d ", succ
->dest
->index
);
69 fprintf (file
, "}\n");
72 /* Dump the DFS node order. */
73 if (loops
->cfg
.dfs_order
)
75 fputs (";; DFS order: ", file
);
76 for (i
= 0; i
< n_basic_blocks
; i
++)
77 fprintf (file
, "%d ", loops
->cfg
.dfs_order
[i
]);
82 /* Dump the reverse completion node order. */
83 if (loops
->cfg
.rc_order
)
85 fputs (";; RC order: ", file
);
86 for (i
= 0; i
< n_basic_blocks
; i
++)
87 fprintf (file
, "%d ", loops
->cfg
.rc_order
[i
]);
93 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
96 flow_loop_nested_p (const struct loop
*outer
, const struct loop
*loop
)
98 return loop
->depth
> outer
->depth
99 && loop
->pred
[outer
->depth
] == outer
;
102 /* Dump the loop information specified by LOOP to the stream FILE
103 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
106 flow_loop_dump (const struct loop
*loop
, FILE *file
,
107 void (*loop_dump_aux
) (const struct loop
*, FILE *, int),
113 if (! loop
|| ! loop
->header
)
116 fprintf (file
, ";;\n;; Loop %d:%s\n", loop
->num
,
117 loop
->invalid
? " invalid" : "");
119 fprintf (file
, ";; header %d, latch %d, pre-header %d\n",
120 loop
->header
->index
, loop
->latch
->index
,
121 loop
->pre_header
? loop
->pre_header
->index
: -1);
122 fprintf (file
, ";; depth %d, level %d, outer %ld\n",
123 loop
->depth
, loop
->level
,
124 (long) (loop
->outer
? loop
->outer
->num
: -1));
126 if (loop
->pre_header_edges
)
127 flow_edge_list_print (";; pre-header edges", loop
->pre_header_edges
,
128 loop
->num_pre_header_edges
, file
);
130 flow_edge_list_print (";; entry edges", loop
->entry_edges
,
131 loop
->num_entries
, file
);
132 fprintf (file
, ";; nodes:");
133 bbs
= get_loop_body (loop
);
134 for (i
= 0; i
< loop
->num_nodes
; i
++)
135 fprintf (file
, " %d", bbs
[i
]->index
);
137 fprintf (file
, "\n");
138 flow_edge_list_print (";; exit edges", loop
->exit_edges
,
139 loop
->num_exits
, file
);
142 loop_dump_aux (loop
, file
, verbose
);
145 /* Dump the loop information specified by LOOPS to the stream FILE,
146 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
149 flow_loops_dump (const struct loops
*loops
, FILE *file
, void (*loop_dump_aux
) (const struct loop
*, FILE *, int), int verbose
)
154 num_loops
= loops
->num
;
155 if (! num_loops
|| ! file
)
158 fprintf (file
, ";; %d loops found, %d levels\n",
159 num_loops
, loops
->levels
);
161 for (i
= 0; i
< num_loops
; i
++)
163 struct loop
*loop
= loops
->parray
[i
];
168 flow_loop_dump (loop
, file
, loop_dump_aux
, verbose
);
172 flow_loops_cfg_dump (loops
, file
);
175 /* Free data allocated for LOOP. */
177 flow_loop_free (struct loop
*loop
)
179 if (loop
->pre_header_edges
)
180 free (loop
->pre_header_edges
);
181 if (loop
->entry_edges
)
182 free (loop
->entry_edges
);
183 if (loop
->exit_edges
)
184 free (loop
->exit_edges
);
190 /* Free all the memory allocated for LOOPS. */
193 flow_loops_free (struct loops
*loops
)
202 /* Free the loop descriptors. */
203 for (i
= 0; i
< loops
->num
; i
++)
205 struct loop
*loop
= loops
->parray
[i
];
210 flow_loop_free (loop
);
213 free (loops
->parray
);
214 loops
->parray
= NULL
;
216 if (loops
->cfg
.dfs_order
)
217 free (loops
->cfg
.dfs_order
);
218 if (loops
->cfg
.rc_order
)
219 free (loops
->cfg
.rc_order
);
224 /* Find the entry edges into the LOOP. */
227 flow_loop_entry_edges_find (struct loop
*loop
)
233 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
235 if (flow_loop_outside_edge_p (loop
, e
))
242 loop
->entry_edges
= xmalloc (num_entries
* sizeof (edge
*));
245 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
247 if (flow_loop_outside_edge_p (loop
, e
))
248 loop
->entry_edges
[num_entries
++] = e
;
251 loop
->num_entries
= num_entries
;
254 /* Find the exit edges from the LOOP. */
257 flow_loop_exit_edges_find (struct loop
*loop
)
260 basic_block node
, *bbs
;
261 unsigned num_exits
, i
;
263 loop
->exit_edges
= NULL
;
266 /* Check all nodes within the loop to see if there are any
267 successors not in the loop. Note that a node may have multiple
270 bbs
= get_loop_body (loop
);
271 for (i
= 0; i
< loop
->num_nodes
; i
++)
274 for (e
= node
->succ
; e
; e
= e
->succ_next
)
276 basic_block dest
= e
->dest
;
278 if (!flow_bb_inside_loop_p (loop
, dest
))
289 loop
->exit_edges
= xmalloc (num_exits
* sizeof (edge
*));
291 /* Store all exiting edges into an array. */
293 for (i
= 0; i
< loop
->num_nodes
; i
++)
296 for (e
= node
->succ
; e
; e
= e
->succ_next
)
298 basic_block dest
= e
->dest
;
300 if (!flow_bb_inside_loop_p (loop
, dest
))
301 loop
->exit_edges
[num_exits
++] = e
;
305 loop
->num_exits
= num_exits
;
308 /* Find the nodes contained within the LOOP with header HEADER.
309 Return the number of nodes within the loop. */
312 flow_loop_nodes_find (basic_block header
, struct loop
*loop
)
318 header
->loop_father
= loop
;
319 header
->loop_depth
= loop
->depth
;
321 if (loop
->latch
->loop_father
!= loop
)
323 stack
= xmalloc (n_basic_blocks
* sizeof (basic_block
));
326 stack
[sp
++] = loop
->latch
;
327 loop
->latch
->loop_father
= loop
;
328 loop
->latch
->loop_depth
= loop
->depth
;
337 for (e
= node
->pred
; e
; e
= e
->pred_next
)
339 basic_block ancestor
= e
->src
;
341 if (ancestor
!= ENTRY_BLOCK_PTR
342 && ancestor
->loop_father
!= loop
)
344 ancestor
->loop_father
= loop
;
345 ancestor
->loop_depth
= loop
->depth
;
347 stack
[sp
++] = ancestor
;
356 /* Find the root node of the loop pre-header extended basic block and
357 the edges along the trace from the root node to the loop header. */
360 flow_loop_pre_header_scan (struct loop
*loop
)
366 loop
->num_pre_header_edges
= 0;
367 if (loop
->num_entries
!= 1)
370 ebb
= loop
->entry_edges
[0]->src
;
371 if (ebb
== ENTRY_BLOCK_PTR
)
374 /* Count number of edges along trace from loop header to
375 root of pre-header extended basic block. Usually this is
376 only one or two edges. */
377 for (num
= 1; ebb
->pred
->src
!= ENTRY_BLOCK_PTR
&& ! ebb
->pred
->pred_next
;
379 ebb
= ebb
->pred
->src
;
381 loop
->pre_header_edges
= xmalloc (num
* sizeof (edge
));
382 loop
->num_pre_header_edges
= num
;
384 /* Store edges in order that they are followed. The source of the first edge
385 is the root node of the pre-header extended basic block and the
386 destination of the last last edge is the loop header. */
387 for (e
= loop
->entry_edges
[0]; num
; e
= e
->src
->pred
)
388 loop
->pre_header_edges
[--num
] = e
;
391 /* Return the block for the pre-header of the loop with header
392 HEADER. Return NULL if there is no pre-header. */
395 flow_loop_pre_header_find (basic_block header
)
397 basic_block pre_header
;
400 /* If block p is a predecessor of the header and is the only block
401 that the header does not dominate, then it is the pre-header. */
403 for (e
= header
->pred
; e
; e
= e
->pred_next
)
405 basic_block node
= e
->src
;
407 if (node
!= ENTRY_BLOCK_PTR
408 && ! dominated_by_p (CDI_DOMINATORS
, node
, header
))
410 if (pre_header
== NULL
)
414 /* There are multiple edges into the header from outside
415 the loop so there is no pre-header block. */
426 establish_preds (struct loop
*loop
)
428 struct loop
*ploop
, *father
= loop
->outer
;
430 loop
->depth
= father
->depth
+ 1;
433 loop
->pred
= xmalloc (sizeof (struct loop
*) * loop
->depth
);
434 memcpy (loop
->pred
, father
->pred
, sizeof (struct loop
*) * father
->depth
);
435 loop
->pred
[father
->depth
] = father
;
437 for (ploop
= loop
->inner
; ploop
; ploop
= ploop
->next
)
438 establish_preds (ploop
);
441 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
442 added loop. If LOOP has some children, take care of that their
443 pred field will be initialized correctly. */
446 flow_loop_tree_node_add (struct loop
*father
, struct loop
*loop
)
448 loop
->next
= father
->inner
;
449 father
->inner
= loop
;
450 loop
->outer
= father
;
452 establish_preds (loop
);
455 /* Remove LOOP from the loop hierarchy tree. */
458 flow_loop_tree_node_remove (struct loop
*loop
)
460 struct loop
*prev
, *father
;
462 father
= loop
->outer
;
465 /* Remove loop from the list of sons. */
466 if (father
->inner
== loop
)
467 father
->inner
= loop
->next
;
470 for (prev
= father
->inner
; prev
->next
!= loop
; prev
= prev
->next
);
471 prev
->next
= loop
->next
;
479 /* Helper function to compute loop nesting depth and enclosed loop level
480 for the natural loop specified by LOOP. Returns the loop level. */
483 flow_loop_level_compute (struct loop
*loop
)
491 /* Traverse loop tree assigning depth and computing level as the
492 maximum level of all the inner loops of this loop. The loop
493 level is equivalent to the height of the loop in the loop tree
494 and corresponds to the number of enclosed loop levels (including
496 for (inner
= loop
->inner
; inner
; inner
= inner
->next
)
498 int ilevel
= flow_loop_level_compute (inner
) + 1;
508 /* Compute the loop nesting depth and enclosed loop level for the loop
509 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
513 flow_loops_level_compute (struct loops
*loops
)
515 return flow_loop_level_compute (loops
->tree_root
);
518 /* Scan a single natural loop specified by LOOP collecting information
519 about it specified by FLAGS. */
522 flow_loop_scan (struct loop
*loop
, int flags
)
524 if (flags
& LOOP_ENTRY_EDGES
)
526 /* Find edges which enter the loop header.
527 Note that the entry edges should only
528 enter the header of a natural loop. */
529 flow_loop_entry_edges_find (loop
);
532 if (flags
& LOOP_EXIT_EDGES
)
534 /* Find edges which exit the loop. */
535 flow_loop_exit_edges_find (loop
);
538 if (flags
& LOOP_PRE_HEADER
)
540 /* Look to see if the loop has a pre-header node. */
541 loop
->pre_header
= flow_loop_pre_header_find (loop
->header
);
543 /* Find the blocks within the extended basic block of
544 the loop pre-header. */
545 flow_loop_pre_header_scan (loop
);
551 /* A callback to update latch and header info for basic block JUMP created
552 by redirecting an edge. */
555 update_latch_info (basic_block jump
)
557 alloc_aux_for_block (jump
, sizeof (int));
558 HEADER_BLOCK (jump
) = 0;
559 alloc_aux_for_edge (jump
->pred
, sizeof (int));
560 LATCH_EDGE (jump
->pred
) = 0;
563 /* A callback for make_forwarder block, to redirect all edges except for
564 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
565 whether to redirect it. */
567 static edge mfb_kj_edge
;
569 mfb_keep_just (edge e
)
571 return e
!= mfb_kj_edge
;
574 /* A callback for make_forwarder block, to redirect the latch edges into an
575 entry part. E is the edge for that we should decide whether to redirect
579 mfb_keep_nonlatch (edge e
)
581 return LATCH_EDGE (e
);
584 /* Takes care of merging natural loops with shared headers. */
587 canonicalize_loop_headers (void)
592 /* Compute the dominators. */
593 calculate_dominance_info (CDI_DOMINATORS
);
595 alloc_aux_for_blocks (sizeof (int));
596 alloc_aux_for_edges (sizeof (int));
598 /* Split blocks so that each loop has only single latch. */
602 int have_abnormal_edge
= 0;
604 for (e
= header
->pred
; e
; e
= e
->pred_next
)
606 basic_block latch
= e
->src
;
608 if (e
->flags
& EDGE_ABNORMAL
)
609 have_abnormal_edge
= 1;
611 if (latch
!= ENTRY_BLOCK_PTR
612 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
618 if (have_abnormal_edge
)
619 HEADER_BLOCK (header
) = 0;
621 HEADER_BLOCK (header
) = num_latches
;
624 free_dominance_info (CDI_DOMINATORS
);
626 if (HEADER_BLOCK (ENTRY_BLOCK_PTR
->succ
->dest
))
630 /* We could not redirect edges freely here. On the other hand,
631 we can simply split the edge from entry block. */
632 bb
= split_edge (ENTRY_BLOCK_PTR
->succ
);
634 alloc_aux_for_edge (bb
->succ
, sizeof (int));
635 LATCH_EDGE (bb
->succ
) = 0;
636 alloc_aux_for_block (bb
, sizeof (int));
637 HEADER_BLOCK (bb
) = 0;
642 int max_freq
, is_heavy
;
643 edge heavy
, tmp_edge
;
645 if (HEADER_BLOCK (header
) <= 1)
648 /* Find a heavy edge. */
652 for (e
= header
->pred
; e
; e
= e
->pred_next
)
653 if (LATCH_EDGE (e
) &&
654 EDGE_FREQUENCY (e
) > max_freq
)
655 max_freq
= EDGE_FREQUENCY (e
);
656 for (e
= header
->pred
; e
; e
= e
->pred_next
)
657 if (LATCH_EDGE (e
) &&
658 EDGE_FREQUENCY (e
) >= max_freq
/ HEAVY_EDGE_RATIO
)
671 /* Split out the heavy edge, and create inner loop for it. */
673 tmp_edge
= make_forwarder_block (header
, mfb_keep_just
,
675 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
676 HEADER_BLOCK (tmp_edge
->dest
) = 1;
677 alloc_aux_for_edge (tmp_edge
, sizeof (int));
678 LATCH_EDGE (tmp_edge
) = 0;
679 HEADER_BLOCK (header
)--;
682 if (HEADER_BLOCK (header
) > 1)
684 /* Create a new latch block. */
685 tmp_edge
= make_forwarder_block (header
, mfb_keep_nonlatch
,
687 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
688 HEADER_BLOCK (tmp_edge
->src
) = 0;
689 HEADER_BLOCK (tmp_edge
->dest
) = 1;
690 alloc_aux_for_edge (tmp_edge
, sizeof (int));
691 LATCH_EDGE (tmp_edge
) = 1;
695 free_aux_for_blocks ();
696 free_aux_for_edges ();
699 /* Find all the natural loops in the function and save in LOOPS structure and
700 recalculate loop_depth information in basic block structures. FLAGS
701 controls which loop information is collected. Return the number of natural
705 flow_loops_find (struct loops
*loops
, int flags
)
717 /* This function cannot be repeatedly called with different
718 flags to build up the loop information. The loop tree
719 must always be built if this function is called. */
720 if (! (flags
& LOOP_TREE
))
723 memset (loops
, 0, sizeof *loops
);
725 /* Taking care of this degenerate case makes the rest of
726 this code simpler. */
727 if (n_basic_blocks
== 0)
733 /* Join loops with shared headers. */
734 canonicalize_loop_headers ();
736 /* Compute the dominators. */
737 calculate_dominance_info (CDI_DOMINATORS
);
739 /* Count the number of loop headers. This should be the
740 same as the number of natural loops. */
741 headers
= sbitmap_alloc (last_basic_block
);
742 sbitmap_zero (headers
);
747 int more_latches
= 0;
749 header
->loop_depth
= 0;
751 /* If we have an abnormal predecessor, do not consider the
752 loop (not worth the problems). */
753 for (e
= header
->pred
; e
; e
= e
->pred_next
)
754 if (e
->flags
& EDGE_ABNORMAL
)
759 for (e
= header
->pred
; e
; e
= e
->pred_next
)
761 basic_block latch
= e
->src
;
763 if (e
->flags
& EDGE_ABNORMAL
)
766 /* Look for back edges where a predecessor is dominated
767 by this block. A natural loop has a single entry
768 node (header) that dominates all the nodes in the
769 loop. It also has single back edge to the header
770 from a latch node. */
771 if (latch
!= ENTRY_BLOCK_PTR
772 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
774 /* Shared headers should be eliminated by now. */
778 SET_BIT (headers
, header
->index
);
784 /* Allocate loop structures. */
785 loops
->parray
= xcalloc (num_loops
+ 1, sizeof (struct loop
*));
787 /* Dummy loop containing whole function. */
788 loops
->parray
[0] = xcalloc (1, sizeof (struct loop
));
789 loops
->parray
[0]->next
= NULL
;
790 loops
->parray
[0]->inner
= NULL
;
791 loops
->parray
[0]->outer
= NULL
;
792 loops
->parray
[0]->depth
= 0;
793 loops
->parray
[0]->pred
= NULL
;
794 loops
->parray
[0]->num_nodes
= n_basic_blocks
+ 2;
795 loops
->parray
[0]->latch
= EXIT_BLOCK_PTR
;
796 loops
->parray
[0]->header
= ENTRY_BLOCK_PTR
;
797 ENTRY_BLOCK_PTR
->loop_father
= loops
->parray
[0];
798 EXIT_BLOCK_PTR
->loop_father
= loops
->parray
[0];
800 loops
->tree_root
= loops
->parray
[0];
802 /* Find and record information about all the natural loops
806 bb
->loop_father
= loops
->tree_root
;
810 /* Compute depth first search order of the CFG so that outer
811 natural loops will be found before inner natural loops. */
812 dfs_order
= xmalloc (n_basic_blocks
* sizeof (int));
813 rc_order
= xmalloc (n_basic_blocks
* sizeof (int));
814 flow_depth_first_order_compute (dfs_order
, rc_order
);
816 /* Save CFG derived information to avoid recomputing it. */
817 loops
->cfg
.dfs_order
= dfs_order
;
818 loops
->cfg
.rc_order
= rc_order
;
822 for (b
= 0; b
< n_basic_blocks
; b
++)
826 /* Search the nodes of the CFG in reverse completion order
827 so that we can find outer loops first. */
828 if (!TEST_BIT (headers
, rc_order
[b
]))
831 header
= BASIC_BLOCK (rc_order
[b
]);
833 loop
= loops
->parray
[num_loops
] = xcalloc (1, sizeof (struct loop
));
835 loop
->header
= header
;
836 loop
->num
= num_loops
;
839 /* Look for the latch for this header block. */
840 for (e
= header
->pred
; e
; e
= e
->pred_next
)
842 basic_block latch
= e
->src
;
844 if (latch
!= ENTRY_BLOCK_PTR
845 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
852 flow_loop_tree_node_add (header
->loop_father
, loop
);
853 loop
->num_nodes
= flow_loop_nodes_find (loop
->header
, loop
);
856 /* Assign the loop nesting depth and enclosed loop level for each
858 loops
->levels
= flow_loops_level_compute (loops
);
860 /* Scan the loops. */
861 for (i
= 1; i
< num_loops
; i
++)
862 flow_loop_scan (loops
->parray
[i
], flags
);
864 loops
->num
= num_loops
;
868 free_dominance_info (CDI_DOMINATORS
);
871 sbitmap_free (headers
);
874 #ifdef ENABLE_CHECKING
876 verify_loop_structure (loops
);
882 /* Update the information regarding the loops in the CFG
883 specified by LOOPS. */
886 flow_loops_update (struct loops
*loops
, int flags
)
888 /* One day we may want to update the current loop data. For now
889 throw away the old stuff and rebuild what we need. */
891 flow_loops_free (loops
);
893 return flow_loops_find (loops
, flags
);
896 /* Return nonzero if basic block BB belongs to LOOP. */
898 flow_bb_inside_loop_p (const struct loop
*loop
, const basic_block bb
)
900 struct loop
*source_loop
;
902 if (bb
== ENTRY_BLOCK_PTR
|| bb
== EXIT_BLOCK_PTR
)
905 source_loop
= bb
->loop_father
;
906 return loop
== source_loop
|| flow_loop_nested_p (loop
, source_loop
);
909 /* Return nonzero if edge E enters header of LOOP from outside of LOOP. */
912 flow_loop_outside_edge_p (const struct loop
*loop
, edge e
)
914 if (e
->dest
!= loop
->header
)
916 return !flow_bb_inside_loop_p (loop
, e
->src
);
919 /* Enumeration predicate for get_loop_body. */
921 glb_enum_p (basic_block bb
, void *glb_header
)
923 return bb
!= (basic_block
) glb_header
;
926 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
927 order against direction of edges from latch. Specially, if
928 header != latch, latch is the 1-st block. */
930 get_loop_body (const struct loop
*loop
)
932 basic_block
*tovisit
, bb
;
935 if (!loop
->num_nodes
)
938 tovisit
= xcalloc (loop
->num_nodes
, sizeof (basic_block
));
939 tovisit
[tv
++] = loop
->header
;
941 if (loop
->latch
== EXIT_BLOCK_PTR
)
943 /* There may be blocks unreachable from EXIT_BLOCK. */
944 if (loop
->num_nodes
!= (unsigned) n_basic_blocks
+ 2)
948 tovisit
[tv
++] = EXIT_BLOCK_PTR
;
950 else if (loop
->latch
!= loop
->header
)
952 tv
= dfs_enumerate_from (loop
->latch
, 1, glb_enum_p
,
953 tovisit
+ 1, loop
->num_nodes
- 1,
957 if (tv
!= loop
->num_nodes
)
962 /* Fills dominance descendants inside LOOP of the basic block BB into
963 array TOVISIT from index *TV. */
966 fill_sons_in_loop (const struct loop
*loop
, basic_block bb
,
967 basic_block
*tovisit
, int *tv
)
969 basic_block son
, postpone
= NULL
;
971 tovisit
[(*tv
)++] = bb
;
972 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
974 son
= next_dom_son (CDI_DOMINATORS
, son
))
976 if (!flow_bb_inside_loop_p (loop
, son
))
979 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, son
))
984 fill_sons_in_loop (loop
, son
, tovisit
, tv
);
988 fill_sons_in_loop (loop
, postpone
, tovisit
, tv
);
991 /* Gets body of a LOOP (that must be different from the outermost loop)
992 sorted by dominance relation. Additionally, if a basic block s dominates
993 the latch, then only blocks dominated by s are be after it. */
996 get_loop_body_in_dom_order (const struct loop
*loop
)
998 basic_block
*tovisit
;
1001 if (!loop
->num_nodes
)
1004 tovisit
= xcalloc (loop
->num_nodes
, sizeof (basic_block
));
1006 if (loop
->latch
== EXIT_BLOCK_PTR
)
1010 fill_sons_in_loop (loop
, loop
->header
, tovisit
, &tv
);
1012 if (tv
!= (int) loop
->num_nodes
)
1018 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
1020 get_loop_exit_edges (const struct loop
*loop
, unsigned int *n_edges
)
1026 if (loop
->latch
== EXIT_BLOCK_PTR
)
1029 body
= get_loop_body (loop
);
1031 for (i
= 0; i
< loop
->num_nodes
; i
++)
1032 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
1033 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
1035 edges
= xmalloc (n
* sizeof (edge
));
1038 for (i
= 0; i
< loop
->num_nodes
; i
++)
1039 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
1040 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
1047 /* Counts the number of conditional branches inside LOOP. */
1050 num_loop_branches (const struct loop
*loop
)
1055 if (loop
->latch
== EXIT_BLOCK_PTR
)
1058 body
= get_loop_body (loop
);
1060 for (i
= 0; i
< loop
->num_nodes
; i
++)
1061 if (body
[i
]->succ
&& body
[i
]->succ
->succ_next
)
1068 /* Adds basic block BB to LOOP. */
1070 add_bb_to_loop (basic_block bb
, struct loop
*loop
)
1074 bb
->loop_father
= loop
;
1075 bb
->loop_depth
= loop
->depth
;
1077 for (i
= 0; i
< loop
->depth
; i
++)
1078 loop
->pred
[i
]->num_nodes
++;
1081 /* Remove basic block BB from loops. */
1083 remove_bb_from_loops (basic_block bb
)
1086 struct loop
*loop
= bb
->loop_father
;
1089 for (i
= 0; i
< loop
->depth
; i
++)
1090 loop
->pred
[i
]->num_nodes
--;
1091 bb
->loop_father
= NULL
;
1095 /* Finds nearest common ancestor in loop tree for given loops. */
1097 find_common_loop (struct loop
*loop_s
, struct loop
*loop_d
)
1099 if (!loop_s
) return loop_d
;
1100 if (!loop_d
) return loop_s
;
1102 if (loop_s
->depth
< loop_d
->depth
)
1103 loop_d
= loop_d
->pred
[loop_s
->depth
];
1104 else if (loop_s
->depth
> loop_d
->depth
)
1105 loop_s
= loop_s
->pred
[loop_d
->depth
];
1107 while (loop_s
!= loop_d
)
1109 loop_s
= loop_s
->outer
;
1110 loop_d
= loop_d
->outer
;
1115 /* Cancels the LOOP; it must be innermost one. */
1117 cancel_loop (struct loops
*loops
, struct loop
*loop
)
1125 /* Move blocks up one level (they should be removed as soon as possible). */
1126 bbs
= get_loop_body (loop
);
1127 for (i
= 0; i
< loop
->num_nodes
; i
++)
1128 bbs
[i
]->loop_father
= loop
->outer
;
1130 /* Remove the loop from structure. */
1131 flow_loop_tree_node_remove (loop
);
1133 /* Remove loop from loops array. */
1134 loops
->parray
[loop
->num
] = NULL
;
1136 /* Free loop data. */
1137 flow_loop_free (loop
);
1140 /* Cancels LOOP and all its subloops. */
1142 cancel_loop_tree (struct loops
*loops
, struct loop
*loop
)
1145 cancel_loop_tree (loops
, loop
->inner
);
1146 cancel_loop (loops
, loop
);
1149 /* Checks that LOOPS are all right:
1150 -- sizes of loops are all right
1151 -- results of get_loop_body really belong to the loop
1152 -- loop header have just single entry edge and single latch edge
1153 -- loop latches have only single successor that is header of their loop
1154 -- irreducible loops are correctly marked
1157 verify_loop_structure (struct loops
*loops
)
1159 unsigned *sizes
, i
, j
;
1161 basic_block
*bbs
, bb
;
1167 sizes
= xcalloc (loops
->num
, sizeof (int));
1171 for (loop
= bb
->loop_father
; loop
; loop
= loop
->outer
)
1174 for (i
= 0; i
< loops
->num
; i
++)
1176 if (!loops
->parray
[i
])
1179 if (loops
->parray
[i
]->num_nodes
!= sizes
[i
])
1181 error ("Size of loop %d should be %d, not %d.",
1182 i
, sizes
[i
], loops
->parray
[i
]->num_nodes
);
1189 /* Check get_loop_body. */
1190 for (i
= 1; i
< loops
->num
; i
++)
1192 loop
= loops
->parray
[i
];
1195 bbs
= get_loop_body (loop
);
1197 for (j
= 0; j
< loop
->num_nodes
; j
++)
1198 if (!flow_bb_inside_loop_p (loop
, bbs
[j
]))
1200 error ("Bb %d do not belong to loop %d.",
1207 /* Check headers and latches. */
1208 for (i
= 1; i
< loops
->num
; i
++)
1210 loop
= loops
->parray
[i
];
1214 if ((loops
->state
& LOOPS_HAVE_PREHEADERS
)
1215 && (!loop
->header
->pred
->pred_next
1216 || loop
->header
->pred
->pred_next
->pred_next
))
1218 error ("Loop %d's header does not have exactly 2 entries.", i
);
1221 if (loops
->state
& LOOPS_HAVE_SIMPLE_LATCHES
)
1223 if (!loop
->latch
->succ
1224 || loop
->latch
->succ
->succ_next
)
1226 error ("Loop %d's latch does not have exactly 1 successor.", i
);
1229 if (loop
->latch
->succ
->dest
!= loop
->header
)
1231 error ("Loop %d's latch does not have header as successor.", i
);
1234 if (loop
->latch
->loop_father
!= loop
)
1236 error ("Loop %d's latch does not belong directly to it.", i
);
1240 if (loop
->header
->loop_father
!= loop
)
1242 error ("Loop %d's header does not belong directly to it.", i
);
1245 if ((loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1246 && (loop_latch_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
))
1248 error ("Loop %d's latch is marked as part of irreducible region.", i
);
1253 /* Check irreducible loops. */
1254 if (loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1256 /* Record old info. */
1257 irreds
= sbitmap_alloc (last_basic_block
);
1260 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1261 SET_BIT (irreds
, bb
->index
);
1263 RESET_BIT (irreds
, bb
->index
);
1264 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1265 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1266 e
->flags
|= EDGE_ALL_FLAGS
+ 1;
1270 mark_irreducible_loops (loops
);
1275 if ((bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1276 && !TEST_BIT (irreds
, bb
->index
))
1278 error ("Basic block %d should be marked irreducible.", bb
->index
);
1281 else if (!(bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1282 && TEST_BIT (irreds
, bb
->index
))
1284 error ("Basic block %d should not be marked irreducible.", bb
->index
);
1287 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1289 if ((e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1290 && !(e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1292 error ("Edge from %d to %d should be marked irreducible.",
1293 e
->src
->index
, e
->dest
->index
);
1296 else if (!(e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1297 && (e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1299 error ("Edge from %d to %d should not be marked irreducible.",
1300 e
->src
->index
, e
->dest
->index
);
1303 e
->flags
&= ~(EDGE_ALL_FLAGS
+ 1);
1313 /* Returns latch edge of LOOP. */
1315 loop_latch_edge (const struct loop
*loop
)
1319 for (e
= loop
->header
->pred
; e
->src
!= loop
->latch
; e
= e
->pred_next
)
1325 /* Returns preheader edge of LOOP. */
1327 loop_preheader_edge (const struct loop
*loop
)
1331 for (e
= loop
->header
->pred
; e
->src
== loop
->latch
; e
= e
->pred_next
)