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 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
105 loops within LOOP. */
108 superloop_at_depth (struct loop
*loop
, unsigned depth
)
110 if (depth
> (unsigned) loop
->depth
)
113 if (depth
== (unsigned) loop
->depth
)
116 return loop
->pred
[depth
];
119 /* Dump the loop information specified by LOOP to the stream FILE
120 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
123 flow_loop_dump (const struct loop
*loop
, FILE *file
,
124 void (*loop_dump_aux
) (const struct loop
*, FILE *, int),
130 if (! loop
|| ! loop
->header
)
133 fprintf (file
, ";;\n;; Loop %d:%s\n", loop
->num
,
134 loop
->invalid
? " invalid" : "");
136 fprintf (file
, ";; header %d, latch %d, pre-header %d\n",
137 loop
->header
->index
, loop
->latch
->index
,
138 loop
->pre_header
? loop
->pre_header
->index
: -1);
139 fprintf (file
, ";; depth %d, level %d, outer %ld\n",
140 loop
->depth
, loop
->level
,
141 (long) (loop
->outer
? loop
->outer
->num
: -1));
143 if (loop
->pre_header_edges
)
144 flow_edge_list_print (";; pre-header edges", loop
->pre_header_edges
,
145 loop
->num_pre_header_edges
, file
);
147 flow_edge_list_print (";; entry edges", loop
->entry_edges
,
148 loop
->num_entries
, file
);
149 fprintf (file
, ";; nodes:");
150 bbs
= get_loop_body (loop
);
151 for (i
= 0; i
< loop
->num_nodes
; i
++)
152 fprintf (file
, " %d", bbs
[i
]->index
);
154 fprintf (file
, "\n");
155 flow_edge_list_print (";; exit edges", loop
->exit_edges
,
156 loop
->num_exits
, file
);
159 loop_dump_aux (loop
, file
, verbose
);
162 /* Dump the loop information specified by LOOPS to the stream FILE,
163 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
166 flow_loops_dump (const struct loops
*loops
, FILE *file
, void (*loop_dump_aux
) (const struct loop
*, FILE *, int), int verbose
)
171 num_loops
= loops
->num
;
172 if (! num_loops
|| ! file
)
175 fprintf (file
, ";; %d loops found, %d levels\n",
176 num_loops
, loops
->levels
);
178 for (i
= 0; i
< num_loops
; i
++)
180 struct loop
*loop
= loops
->parray
[i
];
185 flow_loop_dump (loop
, file
, loop_dump_aux
, verbose
);
189 flow_loops_cfg_dump (loops
, file
);
192 /* Free data allocated for LOOP. */
194 flow_loop_free (struct loop
*loop
)
196 if (loop
->pre_header_edges
)
197 free (loop
->pre_header_edges
);
198 if (loop
->entry_edges
)
199 free (loop
->entry_edges
);
200 if (loop
->exit_edges
)
201 free (loop
->exit_edges
);
207 /* Free all the memory allocated for LOOPS. */
210 flow_loops_free (struct loops
*loops
)
219 /* Free the loop descriptors. */
220 for (i
= 0; i
< loops
->num
; i
++)
222 struct loop
*loop
= loops
->parray
[i
];
227 flow_loop_free (loop
);
230 free (loops
->parray
);
231 loops
->parray
= NULL
;
233 if (loops
->cfg
.dfs_order
)
234 free (loops
->cfg
.dfs_order
);
235 if (loops
->cfg
.rc_order
)
236 free (loops
->cfg
.rc_order
);
241 /* Find the entry edges into the LOOP. */
244 flow_loop_entry_edges_find (struct loop
*loop
)
250 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
252 if (flow_loop_outside_edge_p (loop
, e
))
259 loop
->entry_edges
= xmalloc (num_entries
* sizeof (edge
*));
262 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
264 if (flow_loop_outside_edge_p (loop
, e
))
265 loop
->entry_edges
[num_entries
++] = e
;
268 loop
->num_entries
= num_entries
;
271 /* Find the exit edges from the LOOP. */
274 flow_loop_exit_edges_find (struct loop
*loop
)
277 basic_block node
, *bbs
;
278 unsigned num_exits
, i
;
280 loop
->exit_edges
= NULL
;
283 /* Check all nodes within the loop to see if there are any
284 successors not in the loop. Note that a node may have multiple
287 bbs
= get_loop_body (loop
);
288 for (i
= 0; i
< loop
->num_nodes
; i
++)
291 for (e
= node
->succ
; e
; e
= e
->succ_next
)
293 basic_block dest
= e
->dest
;
295 if (!flow_bb_inside_loop_p (loop
, dest
))
306 loop
->exit_edges
= xmalloc (num_exits
* sizeof (edge
*));
308 /* Store all exiting edges into an array. */
310 for (i
= 0; i
< loop
->num_nodes
; i
++)
313 for (e
= node
->succ
; e
; e
= e
->succ_next
)
315 basic_block dest
= e
->dest
;
317 if (!flow_bb_inside_loop_p (loop
, dest
))
318 loop
->exit_edges
[num_exits
++] = e
;
322 loop
->num_exits
= num_exits
;
325 /* Find the nodes contained within the LOOP with header HEADER.
326 Return the number of nodes within the loop. */
329 flow_loop_nodes_find (basic_block header
, struct loop
*loop
)
335 header
->loop_father
= loop
;
336 header
->loop_depth
= loop
->depth
;
338 if (loop
->latch
->loop_father
!= loop
)
340 stack
= xmalloc (n_basic_blocks
* sizeof (basic_block
));
343 stack
[sp
++] = loop
->latch
;
344 loop
->latch
->loop_father
= loop
;
345 loop
->latch
->loop_depth
= loop
->depth
;
354 for (e
= node
->pred
; e
; e
= e
->pred_next
)
356 basic_block ancestor
= e
->src
;
358 if (ancestor
!= ENTRY_BLOCK_PTR
359 && ancestor
->loop_father
!= loop
)
361 ancestor
->loop_father
= loop
;
362 ancestor
->loop_depth
= loop
->depth
;
364 stack
[sp
++] = ancestor
;
373 /* Find the root node of the loop pre-header extended basic block and
374 the edges along the trace from the root node to the loop header. */
377 flow_loop_pre_header_scan (struct loop
*loop
)
383 loop
->num_pre_header_edges
= 0;
384 if (loop
->num_entries
!= 1)
387 ebb
= loop
->entry_edges
[0]->src
;
388 if (ebb
== ENTRY_BLOCK_PTR
)
391 /* Count number of edges along trace from loop header to
392 root of pre-header extended basic block. Usually this is
393 only one or two edges. */
394 for (num
= 1; ebb
->pred
->src
!= ENTRY_BLOCK_PTR
&& ! ebb
->pred
->pred_next
;
396 ebb
= ebb
->pred
->src
;
398 loop
->pre_header_edges
= xmalloc (num
* sizeof (edge
));
399 loop
->num_pre_header_edges
= num
;
401 /* Store edges in order that they are followed. The source of the first edge
402 is the root node of the pre-header extended basic block and the
403 destination of the last last edge is the loop header. */
404 for (e
= loop
->entry_edges
[0]; num
; e
= e
->src
->pred
)
405 loop
->pre_header_edges
[--num
] = e
;
408 /* Return the block for the pre-header of the loop with header
409 HEADER. Return NULL if there is no pre-header. */
412 flow_loop_pre_header_find (basic_block header
)
414 basic_block pre_header
;
417 /* If block p is a predecessor of the header and is the only block
418 that the header does not dominate, then it is the pre-header. */
420 for (e
= header
->pred
; e
; e
= e
->pred_next
)
422 basic_block node
= e
->src
;
424 if (node
!= ENTRY_BLOCK_PTR
425 && ! dominated_by_p (CDI_DOMINATORS
, node
, header
))
427 if (pre_header
== NULL
)
431 /* There are multiple edges into the header from outside
432 the loop so there is no pre-header block. */
443 establish_preds (struct loop
*loop
)
445 struct loop
*ploop
, *father
= loop
->outer
;
447 loop
->depth
= father
->depth
+ 1;
450 loop
->pred
= xmalloc (sizeof (struct loop
*) * loop
->depth
);
451 memcpy (loop
->pred
, father
->pred
, sizeof (struct loop
*) * father
->depth
);
452 loop
->pred
[father
->depth
] = father
;
454 for (ploop
= loop
->inner
; ploop
; ploop
= ploop
->next
)
455 establish_preds (ploop
);
458 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
459 added loop. If LOOP has some children, take care of that their
460 pred field will be initialized correctly. */
463 flow_loop_tree_node_add (struct loop
*father
, struct loop
*loop
)
465 loop
->next
= father
->inner
;
466 father
->inner
= loop
;
467 loop
->outer
= father
;
469 establish_preds (loop
);
472 /* Remove LOOP from the loop hierarchy tree. */
475 flow_loop_tree_node_remove (struct loop
*loop
)
477 struct loop
*prev
, *father
;
479 father
= loop
->outer
;
482 /* Remove loop from the list of sons. */
483 if (father
->inner
== loop
)
484 father
->inner
= loop
->next
;
487 for (prev
= father
->inner
; prev
->next
!= loop
; prev
= prev
->next
);
488 prev
->next
= loop
->next
;
496 /* Helper function to compute loop nesting depth and enclosed loop level
497 for the natural loop specified by LOOP. Returns the loop level. */
500 flow_loop_level_compute (struct loop
*loop
)
508 /* Traverse loop tree assigning depth and computing level as the
509 maximum level of all the inner loops of this loop. The loop
510 level is equivalent to the height of the loop in the loop tree
511 and corresponds to the number of enclosed loop levels (including
513 for (inner
= loop
->inner
; inner
; inner
= inner
->next
)
515 int ilevel
= flow_loop_level_compute (inner
) + 1;
525 /* Compute the loop nesting depth and enclosed loop level for the loop
526 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
530 flow_loops_level_compute (struct loops
*loops
)
532 return flow_loop_level_compute (loops
->tree_root
);
535 /* Scan a single natural loop specified by LOOP collecting information
536 about it specified by FLAGS. */
539 flow_loop_scan (struct loop
*loop
, int flags
)
541 if (flags
& LOOP_ENTRY_EDGES
)
543 /* Find edges which enter the loop header.
544 Note that the entry edges should only
545 enter the header of a natural loop. */
546 flow_loop_entry_edges_find (loop
);
549 if (flags
& LOOP_EXIT_EDGES
)
551 /* Find edges which exit the loop. */
552 flow_loop_exit_edges_find (loop
);
555 if (flags
& LOOP_PRE_HEADER
)
557 /* Look to see if the loop has a pre-header node. */
558 loop
->pre_header
= flow_loop_pre_header_find (loop
->header
);
560 /* Find the blocks within the extended basic block of
561 the loop pre-header. */
562 flow_loop_pre_header_scan (loop
);
568 /* A callback to update latch and header info for basic block JUMP created
569 by redirecting an edge. */
572 update_latch_info (basic_block jump
)
574 alloc_aux_for_block (jump
, sizeof (int));
575 HEADER_BLOCK (jump
) = 0;
576 alloc_aux_for_edge (jump
->pred
, sizeof (int));
577 LATCH_EDGE (jump
->pred
) = 0;
578 set_immediate_dominator (CDI_DOMINATORS
, jump
, jump
->pred
->src
);
581 /* A callback for make_forwarder block, to redirect all edges except for
582 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
583 whether to redirect it. */
585 static edge mfb_kj_edge
;
587 mfb_keep_just (edge e
)
589 return e
!= mfb_kj_edge
;
592 /* A callback for make_forwarder block, to redirect the latch edges into an
593 entry part. E is the edge for that we should decide whether to redirect
597 mfb_keep_nonlatch (edge e
)
599 return LATCH_EDGE (e
);
602 /* Takes care of merging natural loops with shared headers. */
605 canonicalize_loop_headers (void)
610 alloc_aux_for_blocks (sizeof (int));
611 alloc_aux_for_edges (sizeof (int));
613 /* Split blocks so that each loop has only single latch. */
617 int have_abnormal_edge
= 0;
619 for (e
= header
->pred
; e
; e
= e
->pred_next
)
621 basic_block latch
= e
->src
;
623 if (e
->flags
& EDGE_ABNORMAL
)
624 have_abnormal_edge
= 1;
626 if (latch
!= ENTRY_BLOCK_PTR
627 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
633 if (have_abnormal_edge
)
634 HEADER_BLOCK (header
) = 0;
636 HEADER_BLOCK (header
) = num_latches
;
639 if (HEADER_BLOCK (ENTRY_BLOCK_PTR
->succ
->dest
))
643 /* We could not redirect edges freely here. On the other hand,
644 we can simply split the edge from entry block. */
645 bb
= split_edge (ENTRY_BLOCK_PTR
->succ
);
647 alloc_aux_for_edge (bb
->succ
, sizeof (int));
648 LATCH_EDGE (bb
->succ
) = 0;
649 alloc_aux_for_block (bb
, sizeof (int));
650 HEADER_BLOCK (bb
) = 0;
655 int max_freq
, is_heavy
;
656 edge heavy
, tmp_edge
;
658 if (HEADER_BLOCK (header
) <= 1)
661 /* Find a heavy edge. */
665 for (e
= header
->pred
; e
; e
= e
->pred_next
)
666 if (LATCH_EDGE (e
) &&
667 EDGE_FREQUENCY (e
) > max_freq
)
668 max_freq
= EDGE_FREQUENCY (e
);
669 for (e
= header
->pred
; e
; e
= e
->pred_next
)
670 if (LATCH_EDGE (e
) &&
671 EDGE_FREQUENCY (e
) >= max_freq
/ HEAVY_EDGE_RATIO
)
684 /* Split out the heavy edge, and create inner loop for it. */
686 tmp_edge
= make_forwarder_block (header
, mfb_keep_just
,
688 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
689 HEADER_BLOCK (tmp_edge
->dest
) = 1;
690 alloc_aux_for_edge (tmp_edge
, sizeof (int));
691 LATCH_EDGE (tmp_edge
) = 0;
692 HEADER_BLOCK (header
)--;
695 if (HEADER_BLOCK (header
) > 1)
697 /* Create a new latch block. */
698 tmp_edge
= make_forwarder_block (header
, mfb_keep_nonlatch
,
700 alloc_aux_for_block (tmp_edge
->dest
, sizeof (int));
701 HEADER_BLOCK (tmp_edge
->src
) = 0;
702 HEADER_BLOCK (tmp_edge
->dest
) = 1;
703 alloc_aux_for_edge (tmp_edge
, sizeof (int));
704 LATCH_EDGE (tmp_edge
) = 1;
708 free_aux_for_blocks ();
709 free_aux_for_edges ();
711 #ifdef ENABLE_CHECKING
712 verify_dominators (CDI_DOMINATORS
);
716 /* Find all the natural loops in the function and save in LOOPS structure and
717 recalculate loop_depth information in basic block structures. FLAGS
718 controls which loop information is collected. Return the number of natural
722 flow_loops_find (struct loops
*loops
, int flags
)
734 /* This function cannot be repeatedly called with different
735 flags to build up the loop information. The loop tree
736 must always be built if this function is called. */
737 if (! (flags
& LOOP_TREE
))
740 memset (loops
, 0, sizeof *loops
);
742 /* Taking care of this degenerate case makes the rest of
743 this code simpler. */
744 if (n_basic_blocks
== 0)
750 /* Ensure that the dominators are computed. */
751 calculate_dominance_info (CDI_DOMINATORS
);
753 /* Join loops with shared headers. */
754 canonicalize_loop_headers ();
756 /* Count the number of loop headers. This should be the
757 same as the number of natural loops. */
758 headers
= sbitmap_alloc (last_basic_block
);
759 sbitmap_zero (headers
);
764 int more_latches
= 0;
766 header
->loop_depth
= 0;
768 /* If we have an abnormal predecessor, do not consider the
769 loop (not worth the problems). */
770 for (e
= header
->pred
; e
; e
= e
->pred_next
)
771 if (e
->flags
& EDGE_ABNORMAL
)
776 for (e
= header
->pred
; e
; e
= e
->pred_next
)
778 basic_block latch
= e
->src
;
780 if (e
->flags
& EDGE_ABNORMAL
)
783 /* Look for back edges where a predecessor is dominated
784 by this block. A natural loop has a single entry
785 node (header) that dominates all the nodes in the
786 loop. It also has single back edge to the header
787 from a latch node. */
788 if (latch
!= ENTRY_BLOCK_PTR
789 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
791 /* Shared headers should be eliminated by now. */
795 SET_BIT (headers
, header
->index
);
801 /* Allocate loop structures. */
802 loops
->parray
= xcalloc (num_loops
+ 1, sizeof (struct loop
*));
804 /* Dummy loop containing whole function. */
805 loops
->parray
[0] = xcalloc (1, sizeof (struct loop
));
806 loops
->parray
[0]->next
= NULL
;
807 loops
->parray
[0]->inner
= NULL
;
808 loops
->parray
[0]->outer
= NULL
;
809 loops
->parray
[0]->depth
= 0;
810 loops
->parray
[0]->pred
= NULL
;
811 loops
->parray
[0]->num_nodes
= n_basic_blocks
+ 2;
812 loops
->parray
[0]->latch
= EXIT_BLOCK_PTR
;
813 loops
->parray
[0]->header
= ENTRY_BLOCK_PTR
;
814 ENTRY_BLOCK_PTR
->loop_father
= loops
->parray
[0];
815 EXIT_BLOCK_PTR
->loop_father
= loops
->parray
[0];
817 loops
->tree_root
= loops
->parray
[0];
819 /* Find and record information about all the natural loops
823 bb
->loop_father
= loops
->tree_root
;
827 /* Compute depth first search order of the CFG so that outer
828 natural loops will be found before inner natural loops. */
829 dfs_order
= xmalloc (n_basic_blocks
* sizeof (int));
830 rc_order
= xmalloc (n_basic_blocks
* sizeof (int));
831 flow_depth_first_order_compute (dfs_order
, rc_order
);
833 /* Save CFG derived information to avoid recomputing it. */
834 loops
->cfg
.dfs_order
= dfs_order
;
835 loops
->cfg
.rc_order
= rc_order
;
839 for (b
= 0; b
< n_basic_blocks
; b
++)
843 /* Search the nodes of the CFG in reverse completion order
844 so that we can find outer loops first. */
845 if (!TEST_BIT (headers
, rc_order
[b
]))
848 header
= BASIC_BLOCK (rc_order
[b
]);
850 loop
= loops
->parray
[num_loops
] = xcalloc (1, sizeof (struct loop
));
852 loop
->header
= header
;
853 loop
->num
= num_loops
;
856 /* Look for the latch for this header block. */
857 for (e
= header
->pred
; e
; e
= e
->pred_next
)
859 basic_block latch
= e
->src
;
861 if (latch
!= ENTRY_BLOCK_PTR
862 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
869 flow_loop_tree_node_add (header
->loop_father
, loop
);
870 loop
->num_nodes
= flow_loop_nodes_find (loop
->header
, loop
);
873 /* Assign the loop nesting depth and enclosed loop level for each
875 loops
->levels
= flow_loops_level_compute (loops
);
877 /* Scan the loops. */
878 for (i
= 1; i
< num_loops
; i
++)
879 flow_loop_scan (loops
->parray
[i
], flags
);
881 loops
->num
= num_loops
;
884 sbitmap_free (headers
);
887 #ifdef ENABLE_CHECKING
889 verify_loop_structure (loops
);
895 /* Update the information regarding the loops in the CFG
896 specified by LOOPS. */
899 flow_loops_update (struct loops
*loops
, int flags
)
901 /* One day we may want to update the current loop data. For now
902 throw away the old stuff and rebuild what we need. */
904 flow_loops_free (loops
);
906 return flow_loops_find (loops
, flags
);
909 /* Return nonzero if basic block BB belongs to LOOP. */
911 flow_bb_inside_loop_p (const struct loop
*loop
, const basic_block bb
)
913 struct loop
*source_loop
;
915 if (bb
== ENTRY_BLOCK_PTR
|| bb
== EXIT_BLOCK_PTR
)
918 source_loop
= bb
->loop_father
;
919 return loop
== source_loop
|| flow_loop_nested_p (loop
, source_loop
);
922 /* Return nonzero if edge E enters header of LOOP from outside of LOOP. */
925 flow_loop_outside_edge_p (const struct loop
*loop
, edge e
)
927 if (e
->dest
!= loop
->header
)
929 return !flow_bb_inside_loop_p (loop
, e
->src
);
932 /* Enumeration predicate for get_loop_body. */
934 glb_enum_p (basic_block bb
, void *glb_header
)
936 return bb
!= (basic_block
) glb_header
;
939 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
940 order against direction of edges from latch. Specially, if
941 header != latch, latch is the 1-st block. */
943 get_loop_body (const struct loop
*loop
)
945 basic_block
*tovisit
, bb
;
948 if (!loop
->num_nodes
)
951 tovisit
= xcalloc (loop
->num_nodes
, sizeof (basic_block
));
952 tovisit
[tv
++] = loop
->header
;
954 if (loop
->latch
== EXIT_BLOCK_PTR
)
956 /* There may be blocks unreachable from EXIT_BLOCK. */
957 if (loop
->num_nodes
!= (unsigned) n_basic_blocks
+ 2)
961 tovisit
[tv
++] = EXIT_BLOCK_PTR
;
963 else if (loop
->latch
!= loop
->header
)
965 tv
= dfs_enumerate_from (loop
->latch
, 1, glb_enum_p
,
966 tovisit
+ 1, loop
->num_nodes
- 1,
970 if (tv
!= loop
->num_nodes
)
975 /* Fills dominance descendants inside LOOP of the basic block BB into
976 array TOVISIT from index *TV. */
979 fill_sons_in_loop (const struct loop
*loop
, basic_block bb
,
980 basic_block
*tovisit
, int *tv
)
982 basic_block son
, postpone
= NULL
;
984 tovisit
[(*tv
)++] = bb
;
985 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
987 son
= next_dom_son (CDI_DOMINATORS
, son
))
989 if (!flow_bb_inside_loop_p (loop
, son
))
992 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, son
))
997 fill_sons_in_loop (loop
, son
, tovisit
, tv
);
1001 fill_sons_in_loop (loop
, postpone
, tovisit
, tv
);
1004 /* Gets body of a LOOP (that must be different from the outermost loop)
1005 sorted by dominance relation. Additionally, if a basic block s dominates
1006 the latch, then only blocks dominated by s are be after it. */
1009 get_loop_body_in_dom_order (const struct loop
*loop
)
1011 basic_block
*tovisit
;
1014 if (!loop
->num_nodes
)
1017 tovisit
= xcalloc (loop
->num_nodes
, sizeof (basic_block
));
1019 if (loop
->latch
== EXIT_BLOCK_PTR
)
1023 fill_sons_in_loop (loop
, loop
->header
, tovisit
, &tv
);
1025 if (tv
!= (int) loop
->num_nodes
)
1031 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
1033 get_loop_exit_edges (const struct loop
*loop
, unsigned int *n_edges
)
1039 if (loop
->latch
== EXIT_BLOCK_PTR
)
1042 body
= get_loop_body (loop
);
1044 for (i
= 0; i
< loop
->num_nodes
; i
++)
1045 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
1046 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
1048 edges
= xmalloc (n
* sizeof (edge
));
1051 for (i
= 0; i
< loop
->num_nodes
; i
++)
1052 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
1053 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
1060 /* Counts the number of conditional branches inside LOOP. */
1063 num_loop_branches (const struct loop
*loop
)
1068 if (loop
->latch
== EXIT_BLOCK_PTR
)
1071 body
= get_loop_body (loop
);
1073 for (i
= 0; i
< loop
->num_nodes
; i
++)
1074 if (body
[i
]->succ
&& body
[i
]->succ
->succ_next
)
1081 /* Adds basic block BB to LOOP. */
1083 add_bb_to_loop (basic_block bb
, struct loop
*loop
)
1087 bb
->loop_father
= loop
;
1088 bb
->loop_depth
= loop
->depth
;
1090 for (i
= 0; i
< loop
->depth
; i
++)
1091 loop
->pred
[i
]->num_nodes
++;
1094 /* Remove basic block BB from loops. */
1096 remove_bb_from_loops (basic_block bb
)
1099 struct loop
*loop
= bb
->loop_father
;
1102 for (i
= 0; i
< loop
->depth
; i
++)
1103 loop
->pred
[i
]->num_nodes
--;
1104 bb
->loop_father
= NULL
;
1108 /* Finds nearest common ancestor in loop tree for given loops. */
1110 find_common_loop (struct loop
*loop_s
, struct loop
*loop_d
)
1112 if (!loop_s
) return loop_d
;
1113 if (!loop_d
) return loop_s
;
1115 if (loop_s
->depth
< loop_d
->depth
)
1116 loop_d
= loop_d
->pred
[loop_s
->depth
];
1117 else if (loop_s
->depth
> loop_d
->depth
)
1118 loop_s
= loop_s
->pred
[loop_d
->depth
];
1120 while (loop_s
!= loop_d
)
1122 loop_s
= loop_s
->outer
;
1123 loop_d
= loop_d
->outer
;
1128 /* Cancels the LOOP; it must be innermost one. */
1130 cancel_loop (struct loops
*loops
, struct loop
*loop
)
1138 /* Move blocks up one level (they should be removed as soon as possible). */
1139 bbs
= get_loop_body (loop
);
1140 for (i
= 0; i
< loop
->num_nodes
; i
++)
1141 bbs
[i
]->loop_father
= loop
->outer
;
1143 /* Remove the loop from structure. */
1144 flow_loop_tree_node_remove (loop
);
1146 /* Remove loop from loops array. */
1147 loops
->parray
[loop
->num
] = NULL
;
1149 /* Free loop data. */
1150 flow_loop_free (loop
);
1153 /* Cancels LOOP and all its subloops. */
1155 cancel_loop_tree (struct loops
*loops
, struct loop
*loop
)
1158 cancel_loop_tree (loops
, loop
->inner
);
1159 cancel_loop (loops
, loop
);
1162 /* Checks that LOOPS are all right:
1163 -- sizes of loops are all right
1164 -- results of get_loop_body really belong to the loop
1165 -- loop header have just single entry edge and single latch edge
1166 -- loop latches have only single successor that is header of their loop
1167 -- irreducible loops are correctly marked
1170 verify_loop_structure (struct loops
*loops
)
1172 unsigned *sizes
, i
, j
;
1174 basic_block
*bbs
, bb
;
1180 sizes
= xcalloc (loops
->num
, sizeof (int));
1184 for (loop
= bb
->loop_father
; loop
; loop
= loop
->outer
)
1187 for (i
= 0; i
< loops
->num
; i
++)
1189 if (!loops
->parray
[i
])
1192 if (loops
->parray
[i
]->num_nodes
!= sizes
[i
])
1194 error ("Size of loop %d should be %d, not %d.",
1195 i
, sizes
[i
], loops
->parray
[i
]->num_nodes
);
1202 /* Check get_loop_body. */
1203 for (i
= 1; i
< loops
->num
; i
++)
1205 loop
= loops
->parray
[i
];
1208 bbs
= get_loop_body (loop
);
1210 for (j
= 0; j
< loop
->num_nodes
; j
++)
1211 if (!flow_bb_inside_loop_p (loop
, bbs
[j
]))
1213 error ("Bb %d do not belong to loop %d.",
1220 /* Check headers and latches. */
1221 for (i
= 1; i
< loops
->num
; i
++)
1223 loop
= loops
->parray
[i
];
1227 if ((loops
->state
& LOOPS_HAVE_PREHEADERS
)
1228 && (!loop
->header
->pred
->pred_next
1229 || loop
->header
->pred
->pred_next
->pred_next
))
1231 error ("Loop %d's header does not have exactly 2 entries.", i
);
1234 if (loops
->state
& LOOPS_HAVE_SIMPLE_LATCHES
)
1236 if (!loop
->latch
->succ
1237 || loop
->latch
->succ
->succ_next
)
1239 error ("Loop %d's latch does not have exactly 1 successor.", i
);
1242 if (loop
->latch
->succ
->dest
!= loop
->header
)
1244 error ("Loop %d's latch does not have header as successor.", i
);
1247 if (loop
->latch
->loop_father
!= loop
)
1249 error ("Loop %d's latch does not belong directly to it.", i
);
1253 if (loop
->header
->loop_father
!= loop
)
1255 error ("Loop %d's header does not belong directly to it.", i
);
1258 if ((loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1259 && (loop_latch_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
))
1261 error ("Loop %d's latch is marked as part of irreducible region.", i
);
1266 /* Check irreducible loops. */
1267 if (loops
->state
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1269 /* Record old info. */
1270 irreds
= sbitmap_alloc (last_basic_block
);
1273 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1274 SET_BIT (irreds
, bb
->index
);
1276 RESET_BIT (irreds
, bb
->index
);
1277 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1278 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1279 e
->flags
|= EDGE_ALL_FLAGS
+ 1;
1283 mark_irreducible_loops (loops
);
1288 if ((bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1289 && !TEST_BIT (irreds
, bb
->index
))
1291 error ("Basic block %d should be marked irreducible.", bb
->index
);
1294 else if (!(bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1295 && TEST_BIT (irreds
, bb
->index
))
1297 error ("Basic block %d should not be marked irreducible.", bb
->index
);
1300 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1302 if ((e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1303 && !(e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1305 error ("Edge from %d to %d should be marked irreducible.",
1306 e
->src
->index
, e
->dest
->index
);
1309 else if (!(e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1310 && (e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1312 error ("Edge from %d to %d should not be marked irreducible.",
1313 e
->src
->index
, e
->dest
->index
);
1316 e
->flags
&= ~(EDGE_ALL_FLAGS
+ 1);
1326 /* Returns latch edge of LOOP. */
1328 loop_latch_edge (const struct loop
*loop
)
1332 for (e
= loop
->header
->pred
; e
->src
!= loop
->latch
; e
= e
->pred_next
)
1338 /* Returns preheader edge of LOOP. */
1340 loop_preheader_edge (const struct loop
*loop
)
1344 for (e
= loop
->header
->pred
; e
->src
== loop
->latch
; e
= e
->pred_next
)