1 /* Natural loop discovery code for GNU compiler.
2 Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "basic-block.h"
29 #include "diagnostic-core.h"
32 #include "tree-flow.h"
33 #include "pointer-set.h"
37 static void flow_loops_cfg_dump (FILE *);
39 /* Dump loop related CFG information. */
42 flow_loops_cfg_dump (FILE *file
)
54 fprintf (file
, ";; %d succs { ", bb
->index
);
55 FOR_EACH_EDGE (succ
, ei
, bb
->succs
)
56 fprintf (file
, "%d ", succ
->dest
->index
);
57 fprintf (file
, "}\n");
61 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
64 flow_loop_nested_p (const struct loop
*outer
, const struct loop
*loop
)
66 unsigned odepth
= loop_depth (outer
);
68 return (loop_depth (loop
) > odepth
69 && VEC_index (loop_p
, loop
->superloops
, odepth
) == outer
);
72 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
76 superloop_at_depth (struct loop
*loop
, unsigned depth
)
78 unsigned ldepth
= loop_depth (loop
);
80 gcc_assert (depth
<= ldepth
);
85 return VEC_index (loop_p
, loop
->superloops
, depth
);
88 /* Returns the list of the latch edges of LOOP. */
90 static VEC (edge
, heap
) *
91 get_loop_latch_edges (const struct loop
*loop
)
95 VEC (edge
, heap
) *ret
= NULL
;
97 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
99 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, loop
->header
))
100 VEC_safe_push (edge
, heap
, ret
, e
);
106 /* Dump the loop information specified by LOOP to the stream FILE
107 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
110 flow_loop_dump (const struct loop
*loop
, FILE *file
,
111 void (*loop_dump_aux
) (const struct loop
*, FILE *, int),
116 VEC (edge
, heap
) *latches
;
119 if (! loop
|| ! loop
->header
)
122 fprintf (file
, ";;\n;; Loop %d\n", loop
->num
);
124 fprintf (file
, ";; header %d, ", loop
->header
->index
);
126 fprintf (file
, "latch %d\n", loop
->latch
->index
);
129 fprintf (file
, "multiple latches:");
130 latches
= get_loop_latch_edges (loop
);
131 FOR_EACH_VEC_ELT (edge
, latches
, i
, e
)
132 fprintf (file
, " %d", e
->src
->index
);
133 VEC_free (edge
, heap
, latches
);
134 fprintf (file
, "\n");
137 fprintf (file
, ";; depth %d, outer %ld\n",
138 loop_depth (loop
), (long) (loop_outer (loop
)
139 ? loop_outer (loop
)->num
: -1));
141 fprintf (file
, ";; nodes:");
142 bbs
= get_loop_body (loop
);
143 for (i
= 0; i
< loop
->num_nodes
; i
++)
144 fprintf (file
, " %d", bbs
[i
]->index
);
146 fprintf (file
, "\n");
149 loop_dump_aux (loop
, file
, verbose
);
152 /* Dump the loop information about loops to the stream FILE,
153 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
156 flow_loops_dump (FILE *file
, void (*loop_dump_aux
) (const struct loop
*, FILE *, int), int verbose
)
161 if (!current_loops
|| ! file
)
164 fprintf (file
, ";; %d loops found\n", number_of_loops ());
166 FOR_EACH_LOOP (li
, loop
, LI_INCLUDE_ROOT
)
168 flow_loop_dump (loop
, file
, loop_dump_aux
, verbose
);
172 flow_loops_cfg_dump (file
);
175 /* Free data allocated for LOOP. */
178 flow_loop_free (struct loop
*loop
)
180 struct loop_exit
*exit
, *next
;
182 VEC_free (loop_p
, gc
, loop
->superloops
);
184 /* Break the list of the loop exit records. They will be freed when the
185 corresponding edge is rescanned or removed, and this avoids
186 accessing the (already released) head of the list stored in the
188 for (exit
= loop
->exits
->next
; exit
!= loop
->exits
; exit
= next
)
195 ggc_free (loop
->exits
);
199 /* Free all the memory allocated for LOOPS. */
202 flow_loops_free (struct loops
*loops
)
209 /* Free the loop descriptors. */
210 FOR_EACH_VEC_ELT (loop_p
, loops
->larray
, i
, loop
)
215 flow_loop_free (loop
);
218 VEC_free (loop_p
, gc
, loops
->larray
);
222 /* Find the nodes contained within the LOOP with header HEADER.
223 Return the number of nodes within the loop. */
226 flow_loop_nodes_find (basic_block header
, struct loop
*loop
)
228 VEC (basic_block
, heap
) *stack
= NULL
;
231 edge_iterator latch_ei
;
233 header
->loop_father
= loop
;
235 FOR_EACH_EDGE (latch
, latch_ei
, loop
->header
->preds
)
237 if (latch
->src
->loop_father
== loop
238 || !dominated_by_p (CDI_DOMINATORS
, latch
->src
, loop
->header
))
242 VEC_safe_push (basic_block
, heap
, stack
, latch
->src
);
243 latch
->src
->loop_father
= loop
;
245 while (!VEC_empty (basic_block
, stack
))
251 node
= VEC_pop (basic_block
, stack
);
253 FOR_EACH_EDGE (e
, ei
, node
->preds
)
255 basic_block ancestor
= e
->src
;
257 if (ancestor
->loop_father
!= loop
)
259 ancestor
->loop_father
= loop
;
261 VEC_safe_push (basic_block
, heap
, stack
, ancestor
);
266 VEC_free (basic_block
, heap
, stack
);
271 /* Records the vector of superloops of the loop LOOP, whose immediate
272 superloop is FATHER. */
275 establish_preds (struct loop
*loop
, struct loop
*father
)
278 unsigned depth
= loop_depth (father
) + 1;
281 VEC_truncate (loop_p
, loop
->superloops
, 0);
282 VEC_reserve (loop_p
, gc
, loop
->superloops
, depth
);
283 FOR_EACH_VEC_ELT (loop_p
, father
->superloops
, i
, ploop
)
284 VEC_quick_push (loop_p
, loop
->superloops
, ploop
);
285 VEC_quick_push (loop_p
, loop
->superloops
, father
);
287 for (ploop
= loop
->inner
; ploop
; ploop
= ploop
->next
)
288 establish_preds (ploop
, loop
);
291 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
292 added loop. If LOOP has some children, take care of that their
293 pred field will be initialized correctly. */
296 flow_loop_tree_node_add (struct loop
*father
, struct loop
*loop
)
298 loop
->next
= father
->inner
;
299 father
->inner
= loop
;
301 establish_preds (loop
, father
);
304 /* Remove LOOP from the loop hierarchy tree. */
307 flow_loop_tree_node_remove (struct loop
*loop
)
309 struct loop
*prev
, *father
;
311 father
= loop_outer (loop
);
313 /* Remove loop from the list of sons. */
314 if (father
->inner
== loop
)
315 father
->inner
= loop
->next
;
318 for (prev
= father
->inner
; prev
->next
!= loop
; prev
= prev
->next
)
320 prev
->next
= loop
->next
;
323 VEC_truncate (loop_p
, loop
->superloops
, 0);
326 /* Allocates and returns new loop structure. */
331 struct loop
*loop
= ggc_alloc_cleared_loop ();
333 loop
->exits
= ggc_alloc_cleared_loop_exit ();
334 loop
->exits
->next
= loop
->exits
->prev
= loop
->exits
;
335 loop
->can_be_parallel
= false;
340 /* Initializes loops structure LOOPS, reserving place for NUM_LOOPS loops
341 (including the root of the loop tree). */
344 init_loops_structure (struct loops
*loops
, unsigned num_loops
)
348 memset (loops
, 0, sizeof *loops
);
349 loops
->larray
= VEC_alloc (loop_p
, gc
, num_loops
);
351 /* Dummy loop containing whole function. */
352 root
= alloc_loop ();
353 root
->num_nodes
= n_basic_blocks
;
354 root
->latch
= EXIT_BLOCK_PTR
;
355 root
->header
= ENTRY_BLOCK_PTR
;
356 ENTRY_BLOCK_PTR
->loop_father
= root
;
357 EXIT_BLOCK_PTR
->loop_father
= root
;
359 VEC_quick_push (loop_p
, loops
->larray
, root
);
360 loops
->tree_root
= root
;
363 /* Find all the natural loops in the function and save in LOOPS structure and
364 recalculate loop_father information in basic block structures.
365 Return the number of natural loops found. */
368 flow_loops_find (struct loops
*loops
)
379 /* Ensure that the dominators are computed. */
380 calculate_dominance_info (CDI_DOMINATORS
);
382 /* Taking care of this degenerate case makes the rest of
383 this code simpler. */
384 if (n_basic_blocks
== NUM_FIXED_BLOCKS
)
386 init_loops_structure (loops
, 1);
393 /* Count the number of loop headers. This should be the
394 same as the number of natural loops. */
395 headers
= sbitmap_alloc (last_basic_block
);
396 sbitmap_zero (headers
);
403 /* If we have an abnormal predecessor, do not consider the
404 loop (not worth the problems). */
405 if (bb_has_abnormal_pred (header
))
408 FOR_EACH_EDGE (e
, ei
, header
->preds
)
410 basic_block latch
= e
->src
;
412 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
414 /* Look for back edges where a predecessor is dominated
415 by this block. A natural loop has a single entry
416 node (header) that dominates all the nodes in the
417 loop. It also has single back edge to the header
418 from a latch node. */
419 if (latch
!= ENTRY_BLOCK_PTR
420 && dominated_by_p (CDI_DOMINATORS
, latch
, header
))
422 /* Shared headers should be eliminated by now. */
423 SET_BIT (headers
, header
->index
);
429 /* Allocate loop structures. */
430 init_loops_structure (loops
, num_loops
+ 1);
432 /* Find and record information about all the natural loops
435 bb
->loop_father
= loops
->tree_root
;
439 /* Compute depth first search order of the CFG so that outer
440 natural loops will be found before inner natural loops. */
441 dfs_order
= XNEWVEC (int, n_basic_blocks
);
442 rc_order
= XNEWVEC (int, n_basic_blocks
);
443 pre_and_rev_post_order_compute (dfs_order
, rc_order
, false);
447 for (b
= 0; b
< n_basic_blocks
- NUM_FIXED_BLOCKS
; b
++)
452 /* Search the nodes of the CFG in reverse completion order
453 so that we can find outer loops first. */
454 if (!TEST_BIT (headers
, rc_order
[b
]))
457 header
= BASIC_BLOCK (rc_order
[b
]);
459 loop
= alloc_loop ();
460 VEC_quick_push (loop_p
, loops
->larray
, loop
);
462 loop
->header
= header
;
463 loop
->num
= num_loops
;
466 flow_loop_tree_node_add (header
->loop_father
, loop
);
467 loop
->num_nodes
= flow_loop_nodes_find (loop
->header
, loop
);
469 /* Look for the latch for this header block, if it has just a
471 FOR_EACH_EDGE (e
, ei
, header
->preds
)
473 basic_block latch
= e
->src
;
475 if (flow_bb_inside_loop_p (loop
, latch
))
477 if (loop
->latch
!= NULL
)
479 /* More than one latch edge. */
492 sbitmap_free (headers
);
495 return VEC_length (loop_p
, loops
->larray
);
498 /* Ratio of frequencies of edges so that one of more latch edges is
499 considered to belong to inner loop with same header. */
500 #define HEAVY_EDGE_RATIO 8
502 /* Minimum number of samples for that we apply
503 find_subloop_latch_edge_by_profile heuristics. */
504 #define HEAVY_EDGE_MIN_SAMPLES 10
506 /* If the profile info is available, finds an edge in LATCHES that much more
507 frequent than the remaining edges. Returns such an edge, or NULL if we do
510 We do not use guessed profile here, only the measured one. The guessed
511 profile is usually too flat and unreliable for this (and it is mostly based
512 on the loop structure of the program, so it does not make much sense to
513 derive the loop structure from it). */
516 find_subloop_latch_edge_by_profile (VEC (edge
, heap
) *latches
)
520 gcov_type mcount
= 0, tcount
= 0;
522 FOR_EACH_VEC_ELT (edge
, latches
, i
, e
)
524 if (e
->count
> mcount
)
532 if (tcount
< HEAVY_EDGE_MIN_SAMPLES
533 || (tcount
- mcount
) * HEAVY_EDGE_RATIO
> tcount
)
538 "Found latch edge %d -> %d using profile information.\n",
539 me
->src
->index
, me
->dest
->index
);
543 /* Among LATCHES, guesses a latch edge of LOOP corresponding to subloop, based
544 on the structure of induction variables. Returns this edge, or NULL if we
547 We are quite conservative, and look just for an obvious simple innermost
548 loop (which is the case where we would lose the most performance by not
549 disambiguating the loop). More precisely, we look for the following
550 situation: The source of the chosen latch edge dominates sources of all
551 the other latch edges. Additionally, the header does not contain a phi node
552 such that the argument from the chosen edge is equal to the argument from
556 find_subloop_latch_edge_by_ivs (struct loop
*loop ATTRIBUTE_UNUSED
, VEC (edge
, heap
) *latches
)
558 edge e
, latch
= VEC_index (edge
, latches
, 0);
561 gimple_stmt_iterator psi
;
565 /* Find the candidate for the latch edge. */
566 for (i
= 1; VEC_iterate (edge
, latches
, i
, e
); i
++)
567 if (dominated_by_p (CDI_DOMINATORS
, latch
->src
, e
->src
))
570 /* Verify that it dominates all the latch edges. */
571 FOR_EACH_VEC_ELT (edge
, latches
, i
, e
)
572 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, latch
->src
))
575 /* Check for a phi node that would deny that this is a latch edge of
577 for (psi
= gsi_start_phis (loop
->header
); !gsi_end_p (psi
); gsi_next (&psi
))
579 phi
= gsi_stmt (psi
);
580 lop
= PHI_ARG_DEF_FROM_EDGE (phi
, latch
);
582 /* Ignore the values that are not changed inside the subloop. */
583 if (TREE_CODE (lop
) != SSA_NAME
584 || SSA_NAME_DEF_STMT (lop
) == phi
)
586 bb
= gimple_bb (SSA_NAME_DEF_STMT (lop
));
587 if (!bb
|| !flow_bb_inside_loop_p (loop
, bb
))
590 FOR_EACH_VEC_ELT (edge
, latches
, i
, e
)
592 && PHI_ARG_DEF_FROM_EDGE (phi
, e
) == lop
)
598 "Found latch edge %d -> %d using iv structure.\n",
599 latch
->src
->index
, latch
->dest
->index
);
603 /* If we can determine that one of the several latch edges of LOOP behaves
604 as a latch edge of a separate subloop, returns this edge. Otherwise
608 find_subloop_latch_edge (struct loop
*loop
)
610 VEC (edge
, heap
) *latches
= get_loop_latch_edges (loop
);
613 if (VEC_length (edge
, latches
) > 1)
615 latch
= find_subloop_latch_edge_by_profile (latches
);
618 /* We consider ivs to guess the latch edge only in SSA. Perhaps we
619 should use cfghook for this, but it is hard to imagine it would
620 be useful elsewhere. */
621 && current_ir_type () == IR_GIMPLE
)
622 latch
= find_subloop_latch_edge_by_ivs (loop
, latches
);
625 VEC_free (edge
, heap
, latches
);
629 /* Callback for make_forwarder_block. Returns true if the edge E is marked
630 in the set MFB_REIS_SET. */
632 static struct pointer_set_t
*mfb_reis_set
;
634 mfb_redirect_edges_in_set (edge e
)
636 return pointer_set_contains (mfb_reis_set
, e
);
639 /* Creates a subloop of LOOP with latch edge LATCH. */
642 form_subloop (struct loop
*loop
, edge latch
)
646 struct loop
*new_loop
;
648 mfb_reis_set
= pointer_set_create ();
649 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
652 pointer_set_insert (mfb_reis_set
, e
);
654 new_entry
= make_forwarder_block (loop
->header
, mfb_redirect_edges_in_set
,
656 pointer_set_destroy (mfb_reis_set
);
658 loop
->header
= new_entry
->src
;
660 /* Find the blocks and subloops that belong to the new loop, and add it to
661 the appropriate place in the loop tree. */
662 new_loop
= alloc_loop ();
663 new_loop
->header
= new_entry
->dest
;
664 new_loop
->latch
= latch
->src
;
665 add_loop (new_loop
, loop
);
668 /* Make all the latch edges of LOOP to go to a single forwarder block --
669 a new latch of LOOP. */
672 merge_latch_edges (struct loop
*loop
)
674 VEC (edge
, heap
) *latches
= get_loop_latch_edges (loop
);
678 gcc_assert (VEC_length (edge
, latches
) > 0);
680 if (VEC_length (edge
, latches
) == 1)
681 loop
->latch
= VEC_index (edge
, latches
, 0)->src
;
685 fprintf (dump_file
, "Merged latch edges of loop %d\n", loop
->num
);
687 mfb_reis_set
= pointer_set_create ();
688 FOR_EACH_VEC_ELT (edge
, latches
, i
, e
)
689 pointer_set_insert (mfb_reis_set
, e
);
690 latch
= make_forwarder_block (loop
->header
, mfb_redirect_edges_in_set
,
692 pointer_set_destroy (mfb_reis_set
);
694 loop
->header
= latch
->dest
;
695 loop
->latch
= latch
->src
;
698 VEC_free (edge
, heap
, latches
);
701 /* LOOP may have several latch edges. Transform it into (possibly several)
702 loops with single latch edge. */
705 disambiguate_multiple_latches (struct loop
*loop
)
709 /* We eliminate the multiple latches by splitting the header to the forwarder
710 block F and the rest R, and redirecting the edges. There are two cases:
712 1) If there is a latch edge E that corresponds to a subloop (we guess
713 that based on profile -- if it is taken much more often than the
714 remaining edges; and on trees, using the information about induction
715 variables of the loops), we redirect E to R, all the remaining edges to
716 F, then rescan the loops and try again for the outer loop.
717 2) If there is no such edge, we redirect all latch edges to F, and the
718 entry edges to R, thus making F the single latch of the loop. */
721 fprintf (dump_file
, "Disambiguating loop %d with multiple latches\n",
724 /* During latch merging, we may need to redirect the entry edges to a new
725 block. This would cause problems if the entry edge was the one from the
726 entry block. To avoid having to handle this case specially, split
728 e
= find_edge (ENTRY_BLOCK_PTR
, loop
->header
);
734 e
= find_subloop_latch_edge (loop
);
738 form_subloop (loop
, e
);
741 merge_latch_edges (loop
);
744 /* Split loops with multiple latch edges. */
747 disambiguate_loops_with_multiple_latches (void)
752 FOR_EACH_LOOP (li
, loop
, 0)
755 disambiguate_multiple_latches (loop
);
759 /* Return nonzero if basic block BB belongs to LOOP. */
761 flow_bb_inside_loop_p (const struct loop
*loop
, const_basic_block bb
)
763 struct loop
*source_loop
;
765 if (bb
== ENTRY_BLOCK_PTR
|| bb
== EXIT_BLOCK_PTR
)
768 source_loop
= bb
->loop_father
;
769 return loop
== source_loop
|| flow_loop_nested_p (loop
, source_loop
);
772 /* Enumeration predicate for get_loop_body_with_size. */
774 glb_enum_p (const_basic_block bb
, const void *glb_loop
)
776 const struct loop
*const loop
= (const struct loop
*) glb_loop
;
777 return (bb
!= loop
->header
778 && dominated_by_p (CDI_DOMINATORS
, bb
, loop
->header
));
781 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
782 order against direction of edges from latch. Specially, if
783 header != latch, latch is the 1-st block. LOOP cannot be the fake
784 loop tree root, and its size must be at most MAX_SIZE. The blocks
785 in the LOOP body are stored to BODY, and the size of the LOOP is
789 get_loop_body_with_size (const struct loop
*loop
, basic_block
*body
,
792 return dfs_enumerate_from (loop
->header
, 1, glb_enum_p
,
793 body
, max_size
, loop
);
796 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
797 order against direction of edges from latch. Specially, if
798 header != latch, latch is the 1-st block. */
801 get_loop_body (const struct loop
*loop
)
803 basic_block
*body
, bb
;
806 gcc_assert (loop
->num_nodes
);
808 body
= XNEWVEC (basic_block
, loop
->num_nodes
);
810 if (loop
->latch
== EXIT_BLOCK_PTR
)
812 /* There may be blocks unreachable from EXIT_BLOCK, hence we need to
813 special-case the fake loop that contains the whole function. */
814 gcc_assert (loop
->num_nodes
== (unsigned) n_basic_blocks
);
815 body
[tv
++] = loop
->header
;
816 body
[tv
++] = EXIT_BLOCK_PTR
;
821 tv
= get_loop_body_with_size (loop
, body
, loop
->num_nodes
);
823 gcc_assert (tv
== loop
->num_nodes
);
827 /* Fills dominance descendants inside LOOP of the basic block BB into
828 array TOVISIT from index *TV. */
831 fill_sons_in_loop (const struct loop
*loop
, basic_block bb
,
832 basic_block
*tovisit
, int *tv
)
834 basic_block son
, postpone
= NULL
;
836 tovisit
[(*tv
)++] = bb
;
837 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
839 son
= next_dom_son (CDI_DOMINATORS
, son
))
841 if (!flow_bb_inside_loop_p (loop
, son
))
844 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, son
))
849 fill_sons_in_loop (loop
, son
, tovisit
, tv
);
853 fill_sons_in_loop (loop
, postpone
, tovisit
, tv
);
856 /* Gets body of a LOOP (that must be different from the outermost loop)
857 sorted by dominance relation. Additionally, if a basic block s dominates
858 the latch, then only blocks dominated by s are be after it. */
861 get_loop_body_in_dom_order (const struct loop
*loop
)
863 basic_block
*tovisit
;
866 gcc_assert (loop
->num_nodes
);
868 tovisit
= XNEWVEC (basic_block
, loop
->num_nodes
);
870 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
873 fill_sons_in_loop (loop
, loop
->header
, tovisit
, &tv
);
875 gcc_assert (tv
== (int) loop
->num_nodes
);
880 /* Gets body of a LOOP sorted via provided BB_COMPARATOR. */
883 get_loop_body_in_custom_order (const struct loop
*loop
,
884 int (*bb_comparator
) (const void *, const void *))
886 basic_block
*bbs
= get_loop_body (loop
);
888 qsort (bbs
, loop
->num_nodes
, sizeof (basic_block
), bb_comparator
);
893 /* Get body of a LOOP in breadth first sort order. */
896 get_loop_body_in_bfs_order (const struct loop
*loop
)
904 gcc_assert (loop
->num_nodes
);
905 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
907 blocks
= XNEWVEC (basic_block
, loop
->num_nodes
);
908 visited
= BITMAP_ALLOC (NULL
);
911 while (i
< loop
->num_nodes
)
916 if (bitmap_set_bit (visited
, bb
->index
))
917 /* This basic block is now visited */
920 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
922 if (flow_bb_inside_loop_p (loop
, e
->dest
))
924 if (bitmap_set_bit (visited
, e
->dest
->index
))
925 blocks
[i
++] = e
->dest
;
929 gcc_assert (i
>= vc
);
934 BITMAP_FREE (visited
);
938 /* Hash function for struct loop_exit. */
941 loop_exit_hash (const void *ex
)
943 const struct loop_exit
*const exit
= (const struct loop_exit
*) ex
;
945 return htab_hash_pointer (exit
->e
);
948 /* Equality function for struct loop_exit. Compares with edge. */
951 loop_exit_eq (const void *ex
, const void *e
)
953 const struct loop_exit
*const exit
= (const struct loop_exit
*) ex
;
958 /* Frees the list of loop exit descriptions EX. */
961 loop_exit_free (void *ex
)
963 struct loop_exit
*exit
= (struct loop_exit
*) ex
, *next
;
965 for (; exit
; exit
= next
)
969 exit
->next
->prev
= exit
->prev
;
970 exit
->prev
->next
= exit
->next
;
976 /* Returns the list of records for E as an exit of a loop. */
978 static struct loop_exit
*
979 get_exit_descriptions (edge e
)
981 return (struct loop_exit
*) htab_find_with_hash (current_loops
->exits
, e
,
982 htab_hash_pointer (e
));
985 /* Updates the lists of loop exits in that E appears.
986 If REMOVED is true, E is being removed, and we
987 just remove it from the lists of exits.
988 If NEW_EDGE is true and E is not a loop exit, we
989 do not try to remove it from loop exit lists. */
992 rescan_loop_exit (edge e
, bool new_edge
, bool removed
)
995 struct loop_exit
*exits
= NULL
, *exit
;
996 struct loop
*aloop
, *cloop
;
998 if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
1002 && e
->src
->loop_father
!= NULL
1003 && e
->dest
->loop_father
!= NULL
1004 && !flow_bb_inside_loop_p (e
->src
->loop_father
, e
->dest
))
1006 cloop
= find_common_loop (e
->src
->loop_father
, e
->dest
->loop_father
);
1007 for (aloop
= e
->src
->loop_father
;
1009 aloop
= loop_outer (aloop
))
1011 exit
= ggc_alloc_loop_exit ();
1014 exit
->next
= aloop
->exits
->next
;
1015 exit
->prev
= aloop
->exits
;
1016 exit
->next
->prev
= exit
;
1017 exit
->prev
->next
= exit
;
1019 exit
->next_e
= exits
;
1024 if (!exits
&& new_edge
)
1027 slot
= htab_find_slot_with_hash (current_loops
->exits
, e
,
1028 htab_hash_pointer (e
),
1029 exits
? INSERT
: NO_INSERT
);
1036 loop_exit_free (*slot
);
1040 htab_clear_slot (current_loops
->exits
, slot
);
1043 /* For each loop, record list of exit edges, and start maintaining these
1047 record_loop_exits (void)
1056 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
1058 loops_state_set (LOOPS_HAVE_RECORDED_EXITS
);
1060 gcc_assert (current_loops
->exits
== NULL
);
1061 current_loops
->exits
= htab_create_ggc (2 * number_of_loops (),
1062 loop_exit_hash
, loop_exit_eq
,
1067 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1069 rescan_loop_exit (e
, true, false);
1074 /* Dumps information about the exit in *SLOT to FILE.
1075 Callback for htab_traverse. */
1078 dump_recorded_exit (void **slot
, void *file
)
1080 struct loop_exit
*exit
= (struct loop_exit
*) *slot
;
1084 for (; exit
!= NULL
; exit
= exit
->next_e
)
1087 fprintf ((FILE*) file
, "Edge %d->%d exits %u loops\n",
1088 e
->src
->index
, e
->dest
->index
, n
);
1093 /* Dumps the recorded exits of loops to FILE. */
1095 extern void dump_recorded_exits (FILE *);
1097 dump_recorded_exits (FILE *file
)
1099 if (!current_loops
->exits
)
1101 htab_traverse (current_loops
->exits
, dump_recorded_exit
, file
);
1104 /* Releases lists of loop exits. */
1107 release_recorded_exits (void)
1109 gcc_assert (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
));
1110 htab_delete (current_loops
->exits
);
1111 current_loops
->exits
= NULL
;
1112 loops_state_clear (LOOPS_HAVE_RECORDED_EXITS
);
1115 /* Returns the list of the exit edges of a LOOP. */
1118 get_loop_exit_edges (const struct loop
*loop
)
1120 VEC (edge
, heap
) *edges
= NULL
;
1125 struct loop_exit
*exit
;
1127 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
1129 /* If we maintain the lists of exits, use them. Otherwise we must
1130 scan the body of the loop. */
1131 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
1133 for (exit
= loop
->exits
->next
; exit
->e
; exit
= exit
->next
)
1134 VEC_safe_push (edge
, heap
, edges
, exit
->e
);
1138 body
= get_loop_body (loop
);
1139 for (i
= 0; i
< loop
->num_nodes
; i
++)
1140 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
1142 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
1143 VEC_safe_push (edge
, heap
, edges
, e
);
1151 /* Counts the number of conditional branches inside LOOP. */
1154 num_loop_branches (const struct loop
*loop
)
1159 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
1161 body
= get_loop_body (loop
);
1163 for (i
= 0; i
< loop
->num_nodes
; i
++)
1164 if (EDGE_COUNT (body
[i
]->succs
) >= 2)
1171 /* Adds basic block BB to LOOP. */
1173 add_bb_to_loop (basic_block bb
, struct loop
*loop
)
1180 gcc_assert (bb
->loop_father
== NULL
);
1181 bb
->loop_father
= loop
;
1183 FOR_EACH_VEC_ELT (loop_p
, loop
->superloops
, i
, ploop
)
1186 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1188 rescan_loop_exit (e
, true, false);
1190 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1192 rescan_loop_exit (e
, true, false);
1196 /* Remove basic block BB from loops. */
1198 remove_bb_from_loops (basic_block bb
)
1201 struct loop
*loop
= bb
->loop_father
;
1206 gcc_assert (loop
!= NULL
);
1208 FOR_EACH_VEC_ELT (loop_p
, loop
->superloops
, i
, ploop
)
1210 bb
->loop_father
= NULL
;
1212 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1214 rescan_loop_exit (e
, false, true);
1216 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1218 rescan_loop_exit (e
, false, true);
1222 /* Finds nearest common ancestor in loop tree for given loops. */
1224 find_common_loop (struct loop
*loop_s
, struct loop
*loop_d
)
1226 unsigned sdepth
, ddepth
;
1228 if (!loop_s
) return loop_d
;
1229 if (!loop_d
) return loop_s
;
1231 sdepth
= loop_depth (loop_s
);
1232 ddepth
= loop_depth (loop_d
);
1234 if (sdepth
< ddepth
)
1235 loop_d
= VEC_index (loop_p
, loop_d
->superloops
, sdepth
);
1236 else if (sdepth
> ddepth
)
1237 loop_s
= VEC_index (loop_p
, loop_s
->superloops
, ddepth
);
1239 while (loop_s
!= loop_d
)
1241 loop_s
= loop_outer (loop_s
);
1242 loop_d
= loop_outer (loop_d
);
1247 /* Removes LOOP from structures and frees its data. */
1250 delete_loop (struct loop
*loop
)
1252 /* Remove the loop from structure. */
1253 flow_loop_tree_node_remove (loop
);
1255 /* Remove loop from loops array. */
1256 VEC_replace (loop_p
, current_loops
->larray
, loop
->num
, NULL
);
1258 /* Free loop data. */
1259 flow_loop_free (loop
);
1262 /* Cancels the LOOP; it must be innermost one. */
1265 cancel_loop (struct loop
*loop
)
1269 struct loop
*outer
= loop_outer (loop
);
1271 gcc_assert (!loop
->inner
);
1273 /* Move blocks up one level (they should be removed as soon as possible). */
1274 bbs
= get_loop_body (loop
);
1275 for (i
= 0; i
< loop
->num_nodes
; i
++)
1276 bbs
[i
]->loop_father
= outer
;
1282 /* Cancels LOOP and all its subloops. */
1284 cancel_loop_tree (struct loop
*loop
)
1287 cancel_loop_tree (loop
->inner
);
1291 /* Checks that information about loops is correct
1292 -- sizes of loops are all right
1293 -- results of get_loop_body really belong to the loop
1294 -- loop header have just single entry edge and single latch edge
1295 -- loop latches have only single successor that is header of their loop
1296 -- irreducible loops are correctly marked
1297 -- the cached loop depth and loop father of each bb is correct
1300 verify_loop_structure (void)
1302 unsigned *sizes
, i
, j
;
1304 basic_block
*bbs
, bb
;
1308 unsigned num
= number_of_loops ();
1310 struct loop_exit
*exit
, *mexit
;
1311 bool dom_available
= dom_info_available_p (CDI_DOMINATORS
);
1312 sbitmap visited
= sbitmap_alloc (last_basic_block
);
1314 /* We need up-to-date dominators, compute or verify them. */
1316 calculate_dominance_info (CDI_DOMINATORS
);
1318 verify_dominators (CDI_DOMINATORS
);
1321 sizes
= XCNEWVEC (unsigned, num
);
1325 for (loop
= bb
->loop_father
; loop
; loop
= loop_outer (loop
))
1328 FOR_EACH_LOOP (li
, loop
, LI_INCLUDE_ROOT
)
1332 if (loop
->num_nodes
!= sizes
[i
])
1334 error ("size of loop %d should be %d, not %d",
1335 i
, sizes
[i
], loop
->num_nodes
);
1340 /* Check get_loop_body. */
1341 FOR_EACH_LOOP (li
, loop
, 0)
1343 bbs
= get_loop_body (loop
);
1345 for (j
= 0; j
< loop
->num_nodes
; j
++)
1346 if (!flow_bb_inside_loop_p (loop
, bbs
[j
]))
1348 error ("bb %d do not belong to loop %d",
1349 bbs
[j
]->index
, loop
->num
);
1354 sbitmap_zero (visited
);
1355 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
1357 bbs
= get_loop_body (loop
);
1359 for (j
= 0; j
< loop
->num_nodes
; j
++)
1363 /* Ignore this block if it is in an inner loop. */
1364 if (TEST_BIT (visited
, bb
->index
))
1366 SET_BIT (visited
, bb
->index
);
1368 if (bb
->loop_father
!= loop
)
1370 error ("bb %d has father loop %d, should be loop %d",
1371 bb
->index
, bb
->loop_father
->num
, loop
->num
);
1378 /* Check headers and latches. */
1379 FOR_EACH_LOOP (li
, loop
, 0)
1383 if (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
)
1384 && EDGE_COUNT (loop
->header
->preds
) != 2)
1386 error ("loop %d%'s header does not have exactly 2 entries", i
);
1389 if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES
))
1391 if (!single_succ_p (loop
->latch
))
1393 error ("loop %d%'s latch does not have exactly 1 successor", i
);
1396 if (single_succ (loop
->latch
) != loop
->header
)
1398 error ("loop %d%'s latch does not have header as successor", i
);
1401 if (loop
->latch
->loop_father
!= loop
)
1403 error ("loop %d%'s latch does not belong directly to it", i
);
1407 if (loop
->header
->loop_father
!= loop
)
1409 error ("loop %d%'s header does not belong directly to it", i
);
1412 if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
1413 && (loop_latch_edge (loop
)->flags
& EDGE_IRREDUCIBLE_LOOP
))
1415 error ("loop %d%'s latch is marked as part of irreducible region", i
);
1420 /* Check irreducible loops. */
1421 if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
))
1423 /* Record old info. */
1424 irreds
= sbitmap_alloc (last_basic_block
);
1428 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1429 SET_BIT (irreds
, bb
->index
);
1431 RESET_BIT (irreds
, bb
->index
);
1432 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1433 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1434 e
->flags
|= EDGE_ALL_FLAGS
+ 1;
1438 mark_irreducible_loops ();
1445 if ((bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1446 && !TEST_BIT (irreds
, bb
->index
))
1448 error ("basic block %d should be marked irreducible", bb
->index
);
1451 else if (!(bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1452 && TEST_BIT (irreds
, bb
->index
))
1454 error ("basic block %d should not be marked irreducible", bb
->index
);
1457 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1459 if ((e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1460 && !(e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1462 error ("edge from %d to %d should be marked irreducible",
1463 e
->src
->index
, e
->dest
->index
);
1466 else if (!(e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
1467 && (e
->flags
& (EDGE_ALL_FLAGS
+ 1)))
1469 error ("edge from %d to %d should not be marked irreducible",
1470 e
->src
->index
, e
->dest
->index
);
1473 e
->flags
&= ~(EDGE_ALL_FLAGS
+ 1);
1479 /* Check the recorded loop exits. */
1480 FOR_EACH_LOOP (li
, loop
, 0)
1482 if (!loop
->exits
|| loop
->exits
->e
!= NULL
)
1484 error ("corrupted head of the exits list of loop %d",
1490 /* Check that the list forms a cycle, and all elements except
1491 for the head are nonnull. */
1492 for (mexit
= loop
->exits
, exit
= mexit
->next
, i
= 0;
1493 exit
->e
&& exit
!= mexit
;
1497 mexit
= mexit
->next
;
1500 if (exit
!= loop
->exits
)
1502 error ("corrupted exits list of loop %d", loop
->num
);
1507 if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
1509 if (loop
->exits
->next
!= loop
->exits
)
1511 error ("nonempty exits list of loop %d, but exits are not recorded",
1518 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
1520 unsigned n_exits
= 0, eloops
;
1522 memset (sizes
, 0, sizeof (unsigned) * num
);
1526 if (bb
->loop_father
== current_loops
->tree_root
)
1528 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1530 if (flow_bb_inside_loop_p (bb
->loop_father
, e
->dest
))
1534 exit
= get_exit_descriptions (e
);
1537 error ("exit %d->%d not recorded",
1538 e
->src
->index
, e
->dest
->index
);
1542 for (; exit
; exit
= exit
->next_e
)
1545 for (loop
= bb
->loop_father
;
1546 loop
!= e
->dest
->loop_father
;
1547 loop
= loop_outer (loop
))
1555 error ("wrong list of exited loops for edge %d->%d",
1556 e
->src
->index
, e
->dest
->index
);
1562 if (n_exits
!= htab_elements (current_loops
->exits
))
1564 error ("too many loop exits recorded");
1568 FOR_EACH_LOOP (li
, loop
, 0)
1571 for (exit
= loop
->exits
->next
; exit
->e
; exit
= exit
->next
)
1573 if (eloops
!= sizes
[loop
->num
])
1575 error ("%d exits recorded for loop %d (having %d exits)",
1576 eloops
, loop
->num
, sizes
[loop
->num
]);
1584 sbitmap_free (visited
);
1587 free_dominance_info (CDI_DOMINATORS
);
1590 /* Returns latch edge of LOOP. */
1592 loop_latch_edge (const struct loop
*loop
)
1594 return find_edge (loop
->latch
, loop
->header
);
1597 /* Returns preheader edge of LOOP. */
1599 loop_preheader_edge (const struct loop
*loop
)
1604 gcc_assert (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS
));
1606 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1607 if (e
->src
!= loop
->latch
)
1613 /* Returns true if E is an exit of LOOP. */
1616 loop_exit_edge_p (const struct loop
*loop
, const_edge e
)
1618 return (flow_bb_inside_loop_p (loop
, e
->src
)
1619 && !flow_bb_inside_loop_p (loop
, e
->dest
));
1622 /* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
1623 or more than one exit. If loops do not have the exits recorded, NULL
1624 is returned always. */
1627 single_exit (const struct loop
*loop
)
1629 struct loop_exit
*exit
= loop
->exits
->next
;
1631 if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
1634 if (exit
->e
&& exit
->next
== loop
->exits
)
1640 /* Returns true when BB has an incoming edge exiting LOOP. */
1643 loop_exits_to_bb_p (struct loop
*loop
, basic_block bb
)
1648 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1649 if (loop_exit_edge_p (loop
, e
))
1655 /* Returns true when BB has an outgoing edge exiting LOOP. */
1658 loop_exits_from_bb_p (struct loop
*loop
, basic_block bb
)
1663 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1664 if (loop_exit_edge_p (loop
, e
))