* arm.c (arm_cpp_interwork): New variable.
[official-gcc.git] / gcc / cfgloop.c
blob3d13386ba4e1cd2038ef38b4598424ea605ebf93
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
9 version.
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
14 for more details.
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
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "toplev.h"
29 #include "cfgloop.h"
30 #include "flags.h"
31 #include "tree.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. */
55 static void
56 flow_loops_cfg_dump (const struct loops *loops, FILE *file)
58 int i;
59 basic_block bb;
61 if (! loops->num || ! file)
62 return;
64 FOR_EACH_BB (bb)
66 edge succ;
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]);
81 fputs ("\n", file);
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]);
91 fputs ("\n", file);
95 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
97 bool
98 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
100 return loop->depth > outer->depth
101 && loop->pred[outer->depth] == outer;
104 /* Dump the loop information specified by LOOP to the stream FILE
105 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
107 void
108 flow_loop_dump (const struct loop *loop, FILE *file,
109 void (*loop_dump_aux) (const struct loop *, FILE *, int),
110 int verbose)
112 basic_block *bbs;
113 unsigned i;
115 if (! loop || ! loop->header)
116 return;
118 fprintf (file, ";;\n;; Loop %d:%s\n", loop->num,
119 loop->invalid ? " invalid" : "");
121 fprintf (file, ";; header %d, latch %d, pre-header %d\n",
122 loop->header->index, loop->latch->index,
123 loop->pre_header ? loop->pre_header->index : -1);
124 fprintf (file, ";; depth %d, level %d, outer %ld\n",
125 loop->depth, loop->level,
126 (long) (loop->outer ? loop->outer->num : -1));
128 if (loop->pre_header_edges)
129 flow_edge_list_print (";; pre-header edges", loop->pre_header_edges,
130 loop->num_pre_header_edges, file);
132 flow_edge_list_print (";; entry edges", loop->entry_edges,
133 loop->num_entries, file);
134 fprintf (file, ";; nodes:");
135 bbs = get_loop_body (loop);
136 for (i = 0; i < loop->num_nodes; i++)
137 fprintf (file, " %d", bbs[i]->index);
138 free (bbs);
139 fprintf (file, "\n");
140 flow_edge_list_print (";; exit edges", loop->exit_edges,
141 loop->num_exits, file);
143 if (loop_dump_aux)
144 loop_dump_aux (loop, file, verbose);
147 /* Dump the loop information specified by LOOPS to the stream FILE,
148 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
150 void
151 flow_loops_dump (const struct loops *loops, FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
153 int i;
154 int num_loops;
156 num_loops = loops->num;
157 if (! num_loops || ! file)
158 return;
160 fprintf (file, ";; %d loops found, %d levels\n",
161 num_loops, loops->levels);
163 for (i = 0; i < num_loops; i++)
165 struct loop *loop = loops->parray[i];
167 if (!loop)
168 continue;
170 flow_loop_dump (loop, file, loop_dump_aux, verbose);
173 if (verbose)
174 flow_loops_cfg_dump (loops, file);
177 /* Free data allocated for LOOP. */
178 void
179 flow_loop_free (struct loop *loop)
181 if (loop->pre_header_edges)
182 free (loop->pre_header_edges);
183 if (loop->entry_edges)
184 free (loop->entry_edges);
185 if (loop->exit_edges)
186 free (loop->exit_edges);
187 if (loop->pred)
188 free (loop->pred);
189 free (loop);
192 /* Free all the memory allocated for LOOPS. */
194 void
195 flow_loops_free (struct loops *loops)
197 if (loops->parray)
199 unsigned i;
201 if (! loops->num)
202 abort ();
204 /* Free the loop descriptors. */
205 for (i = 0; i < loops->num; i++)
207 struct loop *loop = loops->parray[i];
209 if (!loop)
210 continue;
212 flow_loop_free (loop);
215 free (loops->parray);
216 loops->parray = NULL;
218 if (loops->cfg.dfs_order)
219 free (loops->cfg.dfs_order);
220 if (loops->cfg.rc_order)
221 free (loops->cfg.rc_order);
226 /* Find the entry edges into the LOOP. */
228 static void
229 flow_loop_entry_edges_find (struct loop *loop)
231 edge e;
232 int num_entries;
234 num_entries = 0;
235 for (e = loop->header->pred; e; e = e->pred_next)
237 if (flow_loop_outside_edge_p (loop, e))
238 num_entries++;
241 if (! num_entries)
242 abort ();
244 loop->entry_edges = xmalloc (num_entries * sizeof (edge *));
246 num_entries = 0;
247 for (e = loop->header->pred; e; e = e->pred_next)
249 if (flow_loop_outside_edge_p (loop, e))
250 loop->entry_edges[num_entries++] = e;
253 loop->num_entries = num_entries;
256 /* Find the exit edges from the LOOP. */
258 static void
259 flow_loop_exit_edges_find (struct loop *loop)
261 edge e;
262 basic_block node, *bbs;
263 unsigned num_exits, i;
265 loop->exit_edges = NULL;
266 loop->num_exits = 0;
268 /* Check all nodes within the loop to see if there are any
269 successors not in the loop. Note that a node may have multiple
270 exiting edges. */
271 num_exits = 0;
272 bbs = get_loop_body (loop);
273 for (i = 0; i < loop->num_nodes; i++)
275 node = bbs[i];
276 for (e = node->succ; e; e = e->succ_next)
278 basic_block dest = e->dest;
280 if (!flow_bb_inside_loop_p (loop, dest))
281 num_exits++;
285 if (! num_exits)
287 free (bbs);
288 return;
291 loop->exit_edges = xmalloc (num_exits * sizeof (edge *));
293 /* Store all exiting edges into an array. */
294 num_exits = 0;
295 for (i = 0; i < loop->num_nodes; i++)
297 node = bbs[i];
298 for (e = node->succ; e; e = e->succ_next)
300 basic_block dest = e->dest;
302 if (!flow_bb_inside_loop_p (loop, dest))
303 loop->exit_edges[num_exits++] = e;
306 free (bbs);
307 loop->num_exits = num_exits;
310 /* Find the nodes contained within the LOOP with header HEADER.
311 Return the number of nodes within the loop. */
313 static int
314 flow_loop_nodes_find (basic_block header, struct loop *loop)
316 basic_block *stack;
317 int sp;
318 int num_nodes = 1;
320 header->loop_father = loop;
321 header->loop_depth = loop->depth;
323 if (loop->latch->loop_father != loop)
325 stack = xmalloc (n_basic_blocks * sizeof (basic_block));
326 sp = 0;
327 num_nodes++;
328 stack[sp++] = loop->latch;
329 loop->latch->loop_father = loop;
330 loop->latch->loop_depth = loop->depth;
332 while (sp)
334 basic_block node;
335 edge e;
337 node = stack[--sp];
339 for (e = node->pred; e; e = e->pred_next)
341 basic_block ancestor = e->src;
343 if (ancestor != ENTRY_BLOCK_PTR
344 && ancestor->loop_father != loop)
346 ancestor->loop_father = loop;
347 ancestor->loop_depth = loop->depth;
348 num_nodes++;
349 stack[sp++] = ancestor;
353 free (stack);
355 return num_nodes;
358 /* Find the root node of the loop pre-header extended basic block and
359 the edges along the trace from the root node to the loop header. */
361 static void
362 flow_loop_pre_header_scan (struct loop *loop)
364 int num;
365 basic_block ebb;
366 edge e;
368 loop->num_pre_header_edges = 0;
369 if (loop->num_entries != 1)
370 return;
372 ebb = loop->entry_edges[0]->src;
373 if (ebb == ENTRY_BLOCK_PTR)
374 return;
376 /* Count number of edges along trace from loop header to
377 root of pre-header extended basic block. Usually this is
378 only one or two edges. */
379 for (num = 1; ebb->pred->src != ENTRY_BLOCK_PTR && ! ebb->pred->pred_next;
380 num++)
381 ebb = ebb->pred->src;
383 loop->pre_header_edges = xmalloc (num * sizeof (edge));
384 loop->num_pre_header_edges = num;
386 /* Store edges in order that they are followed. The source of the first edge
387 is the root node of the pre-header extended basic block and the
388 destination of the last last edge is the loop header. */
389 for (e = loop->entry_edges[0]; num; e = e->src->pred)
390 loop->pre_header_edges[--num] = e;
393 /* Return the block for the pre-header of the loop with header
394 HEADER. Return NULL if there is no pre-header. */
396 static basic_block
397 flow_loop_pre_header_find (basic_block header)
399 basic_block pre_header;
400 edge e;
402 /* If block p is a predecessor of the header and is the only block
403 that the header does not dominate, then it is the pre-header. */
404 pre_header = NULL;
405 for (e = header->pred; e; e = e->pred_next)
407 basic_block node = e->src;
409 if (node != ENTRY_BLOCK_PTR
410 && ! dominated_by_p (CDI_DOMINATORS, node, header))
412 if (pre_header == NULL)
413 pre_header = node;
414 else
416 /* There are multiple edges into the header from outside
417 the loop so there is no pre-header block. */
418 pre_header = NULL;
419 break;
424 return pre_header;
427 static void
428 establish_preds (struct loop *loop)
430 struct loop *ploop, *father = loop->outer;
432 loop->depth = father->depth + 1;
433 if (loop->pred)
434 free (loop->pred);
435 loop->pred = xmalloc (sizeof (struct loop *) * loop->depth);
436 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
437 loop->pred[father->depth] = father;
439 for (ploop = loop->inner; ploop; ploop = ploop->next)
440 establish_preds (ploop);
443 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
444 added loop. If LOOP has some children, take care of that their
445 pred field will be initialized correctly. */
447 void
448 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
450 loop->next = father->inner;
451 father->inner = loop;
452 loop->outer = father;
454 establish_preds (loop);
457 /* Remove LOOP from the loop hierarchy tree. */
459 void
460 flow_loop_tree_node_remove (struct loop *loop)
462 struct loop *prev, *father;
464 father = loop->outer;
465 loop->outer = NULL;
467 /* Remove loop from the list of sons. */
468 if (father->inner == loop)
469 father->inner = loop->next;
470 else
472 for (prev = father->inner; prev->next != loop; prev = prev->next);
473 prev->next = loop->next;
476 loop->depth = -1;
477 free (loop->pred);
478 loop->pred = NULL;
481 /* Helper function to compute loop nesting depth and enclosed loop level
482 for the natural loop specified by LOOP. Returns the loop level. */
484 static int
485 flow_loop_level_compute (struct loop *loop)
487 struct loop *inner;
488 int level = 1;
490 if (! loop)
491 return 0;
493 /* Traverse loop tree assigning depth and computing level as the
494 maximum level of all the inner loops of this loop. The loop
495 level is equivalent to the height of the loop in the loop tree
496 and corresponds to the number of enclosed loop levels (including
497 itself). */
498 for (inner = loop->inner; inner; inner = inner->next)
500 int ilevel = flow_loop_level_compute (inner) + 1;
502 if (ilevel > level)
503 level = ilevel;
506 loop->level = level;
507 return level;
510 /* Compute the loop nesting depth and enclosed loop level for the loop
511 hierarchy tree specified by LOOPS. Return the maximum enclosed loop
512 level. */
514 static int
515 flow_loops_level_compute (struct loops *loops)
517 return flow_loop_level_compute (loops->tree_root);
520 /* Scan a single natural loop specified by LOOP collecting information
521 about it specified by FLAGS. */
524 flow_loop_scan (struct loop *loop, int flags)
526 if (flags & LOOP_ENTRY_EDGES)
528 /* Find edges which enter the loop header.
529 Note that the entry edges should only
530 enter the header of a natural loop. */
531 flow_loop_entry_edges_find (loop);
534 if (flags & LOOP_EXIT_EDGES)
536 /* Find edges which exit the loop. */
537 flow_loop_exit_edges_find (loop);
540 if (flags & LOOP_PRE_HEADER)
542 /* Look to see if the loop has a pre-header node. */
543 loop->pre_header = flow_loop_pre_header_find (loop->header);
545 /* Find the blocks within the extended basic block of
546 the loop pre-header. */
547 flow_loop_pre_header_scan (loop);
550 return 1;
553 /* A callback to update latch and header info for basic block JUMP created
554 by redirecting an edge. */
556 static void
557 update_latch_info (basic_block jump)
559 alloc_aux_for_block (jump, sizeof (int));
560 HEADER_BLOCK (jump) = 0;
561 alloc_aux_for_edge (jump->pred, sizeof (int));
562 LATCH_EDGE (jump->pred) = 0;
565 /* A callback for make_forwarder block, to redirect all edges except for
566 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
567 whether to redirect it. */
569 static edge mfb_kj_edge;
570 static bool
571 mfb_keep_just (edge e)
573 return e != mfb_kj_edge;
576 /* A callback for make_forwarder block, to redirect the latch edges into an
577 entry part. E is the edge for that we should decide whether to redirect
578 it. */
580 static bool
581 mfb_keep_nonlatch (edge e)
583 return LATCH_EDGE (e);
586 /* Takes care of merging natural loops with shared headers. */
588 static void
589 canonicalize_loop_headers (void)
591 basic_block header;
592 edge e;
594 /* Compute the dominators. */
595 calculate_dominance_info (CDI_DOMINATORS);
597 alloc_aux_for_blocks (sizeof (int));
598 alloc_aux_for_edges (sizeof (int));
600 /* Split blocks so that each loop has only single latch. */
601 FOR_EACH_BB (header)
603 int num_latches = 0;
604 int have_abnormal_edge = 0;
606 for (e = header->pred; e; e = e->pred_next)
608 basic_block latch = e->src;
610 if (e->flags & EDGE_ABNORMAL)
611 have_abnormal_edge = 1;
613 if (latch != ENTRY_BLOCK_PTR
614 && dominated_by_p (CDI_DOMINATORS, latch, header))
616 num_latches++;
617 LATCH_EDGE (e) = 1;
620 if (have_abnormal_edge)
621 HEADER_BLOCK (header) = 0;
622 else
623 HEADER_BLOCK (header) = num_latches;
626 free_dominance_info (CDI_DOMINATORS);
628 if (HEADER_BLOCK (ENTRY_BLOCK_PTR->succ->dest))
630 basic_block bb;
632 /* We could not redirect edges freely here. On the other hand,
633 we can simply split the edge from entry block. */
634 bb = split_edge (ENTRY_BLOCK_PTR->succ);
636 alloc_aux_for_edge (bb->succ, sizeof (int));
637 LATCH_EDGE (bb->succ) = 0;
638 alloc_aux_for_block (bb, sizeof (int));
639 HEADER_BLOCK (bb) = 0;
642 FOR_EACH_BB (header)
644 int max_freq, is_heavy;
645 edge heavy, tmp_edge;
647 if (HEADER_BLOCK (header) <= 1)
648 continue;
650 /* Find a heavy edge. */
651 is_heavy = 1;
652 heavy = NULL;
653 max_freq = 0;
654 for (e = header->pred; e; e = e->pred_next)
655 if (LATCH_EDGE (e) &&
656 EDGE_FREQUENCY (e) > max_freq)
657 max_freq = EDGE_FREQUENCY (e);
658 for (e = header->pred; e; e = e->pred_next)
659 if (LATCH_EDGE (e) &&
660 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
662 if (heavy)
664 is_heavy = 0;
665 break;
667 else
668 heavy = e;
671 if (is_heavy)
673 /* Split out the heavy edge, and create inner loop for it. */
674 mfb_kj_edge = heavy;
675 tmp_edge = make_forwarder_block (header, mfb_keep_just,
676 update_latch_info);
677 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
678 HEADER_BLOCK (tmp_edge->dest) = 1;
679 alloc_aux_for_edge (tmp_edge, sizeof (int));
680 LATCH_EDGE (tmp_edge) = 0;
681 HEADER_BLOCK (header)--;
684 if (HEADER_BLOCK (header) > 1)
686 /* Create a new latch block. */
687 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
688 update_latch_info);
689 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
690 HEADER_BLOCK (tmp_edge->src) = 0;
691 HEADER_BLOCK (tmp_edge->dest) = 1;
692 alloc_aux_for_edge (tmp_edge, sizeof (int));
693 LATCH_EDGE (tmp_edge) = 1;
697 free_aux_for_blocks ();
698 free_aux_for_edges ();
701 /* Find all the natural loops in the function and save in LOOPS structure and
702 recalculate loop_depth information in basic block structures. FLAGS
703 controls which loop information is collected. Return the number of natural
704 loops found. */
707 flow_loops_find (struct loops *loops, int flags)
709 int i;
710 int b;
711 int num_loops;
712 edge e;
713 sbitmap headers;
714 int *dfs_order;
715 int *rc_order;
716 basic_block header;
717 basic_block bb;
719 /* This function cannot be repeatedly called with different
720 flags to build up the loop information. The loop tree
721 must always be built if this function is called. */
722 if (! (flags & LOOP_TREE))
723 abort ();
725 memset (loops, 0, sizeof *loops);
727 /* Taking care of this degenerate case makes the rest of
728 this code simpler. */
729 if (n_basic_blocks == 0)
730 return 0;
732 dfs_order = NULL;
733 rc_order = NULL;
735 /* Join loops with shared headers. */
736 canonicalize_loop_headers ();
738 /* Compute the dominators. */
739 calculate_dominance_info (CDI_DOMINATORS);
741 /* Count the number of loop headers. This should be the
742 same as the number of natural loops. */
743 headers = sbitmap_alloc (last_basic_block);
744 sbitmap_zero (headers);
746 num_loops = 0;
747 FOR_EACH_BB (header)
749 int more_latches = 0;
751 header->loop_depth = 0;
753 /* If we have an abnormal predecessor, do not consider the
754 loop (not worth the problems). */
755 for (e = header->pred; e; e = e->pred_next)
756 if (e->flags & EDGE_ABNORMAL)
757 break;
758 if (e)
759 continue;
761 for (e = header->pred; e; e = e->pred_next)
763 basic_block latch = e->src;
765 if (e->flags & EDGE_ABNORMAL)
766 abort ();
768 /* Look for back edges where a predecessor is dominated
769 by this block. A natural loop has a single entry
770 node (header) that dominates all the nodes in the
771 loop. It also has single back edge to the header
772 from a latch node. */
773 if (latch != ENTRY_BLOCK_PTR
774 && dominated_by_p (CDI_DOMINATORS, latch, header))
776 /* Shared headers should be eliminated by now. */
777 if (more_latches)
778 abort ();
779 more_latches = 1;
780 SET_BIT (headers, header->index);
781 num_loops++;
786 /* Allocate loop structures. */
787 loops->parray = xcalloc (num_loops + 1, sizeof (struct loop *));
789 /* Dummy loop containing whole function. */
790 loops->parray[0] = xcalloc (1, sizeof (struct loop));
791 loops->parray[0]->next = NULL;
792 loops->parray[0]->inner = NULL;
793 loops->parray[0]->outer = NULL;
794 loops->parray[0]->depth = 0;
795 loops->parray[0]->pred = NULL;
796 loops->parray[0]->num_nodes = n_basic_blocks + 2;
797 loops->parray[0]->latch = EXIT_BLOCK_PTR;
798 loops->parray[0]->header = ENTRY_BLOCK_PTR;
799 ENTRY_BLOCK_PTR->loop_father = loops->parray[0];
800 EXIT_BLOCK_PTR->loop_father = loops->parray[0];
802 loops->tree_root = loops->parray[0];
804 /* Find and record information about all the natural loops
805 in the CFG. */
806 loops->num = 1;
807 FOR_EACH_BB (bb)
808 bb->loop_father = loops->tree_root;
810 if (num_loops)
812 /* Compute depth first search order of the CFG so that outer
813 natural loops will be found before inner natural loops. */
814 dfs_order = xmalloc (n_basic_blocks * sizeof (int));
815 rc_order = xmalloc (n_basic_blocks * sizeof (int));
816 flow_depth_first_order_compute (dfs_order, rc_order);
818 /* Save CFG derived information to avoid recomputing it. */
819 loops->cfg.dfs_order = dfs_order;
820 loops->cfg.rc_order = rc_order;
822 num_loops = 1;
824 for (b = 0; b < n_basic_blocks; b++)
826 struct loop *loop;
828 /* Search the nodes of the CFG in reverse completion order
829 so that we can find outer loops first. */
830 if (!TEST_BIT (headers, rc_order[b]))
831 continue;
833 header = BASIC_BLOCK (rc_order[b]);
835 loop = loops->parray[num_loops] = xcalloc (1, sizeof (struct loop));
837 loop->header = header;
838 loop->num = num_loops;
839 num_loops++;
841 /* Look for the latch for this header block. */
842 for (e = header->pred; e; e = e->pred_next)
844 basic_block latch = e->src;
846 if (latch != ENTRY_BLOCK_PTR
847 && dominated_by_p (CDI_DOMINATORS, latch, header))
849 loop->latch = latch;
850 break;
854 flow_loop_tree_node_add (header->loop_father, loop);
855 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
858 /* Assign the loop nesting depth and enclosed loop level for each
859 loop. */
860 loops->levels = flow_loops_level_compute (loops);
862 /* Scan the loops. */
863 for (i = 1; i < num_loops; i++)
864 flow_loop_scan (loops->parray[i], flags);
866 loops->num = num_loops;
868 else
870 free_dominance_info (CDI_DOMINATORS);
873 sbitmap_free (headers);
875 loops->state = 0;
876 #ifdef ENABLE_CHECKING
877 verify_flow_info ();
878 verify_loop_structure (loops);
879 #endif
881 return loops->num;
884 /* Update the information regarding the loops in the CFG
885 specified by LOOPS. */
888 flow_loops_update (struct loops *loops, int flags)
890 /* One day we may want to update the current loop data. For now
891 throw away the old stuff and rebuild what we need. */
892 if (loops->parray)
893 flow_loops_free (loops);
895 return flow_loops_find (loops, flags);
898 /* Return nonzero if basic block BB belongs to LOOP. */
899 bool
900 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
902 struct loop *source_loop;
904 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
905 return 0;
907 source_loop = bb->loop_father;
908 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
911 /* Return nonzero if edge E enters header of LOOP from outside of LOOP. */
913 bool
914 flow_loop_outside_edge_p (const struct loop *loop, edge e)
916 if (e->dest != loop->header)
917 abort ();
918 return !flow_bb_inside_loop_p (loop, e->src);
921 /* Enumeration predicate for get_loop_body. */
922 static bool
923 glb_enum_p (basic_block bb, void *glb_header)
925 return bb != (basic_block) glb_header;
928 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
929 order against direction of edges from latch. Specially, if
930 header != latch, latch is the 1-st block. */
931 basic_block *
932 get_loop_body (const struct loop *loop)
934 basic_block *tovisit, bb;
935 unsigned tv = 0;
937 if (!loop->num_nodes)
938 abort ();
940 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
941 tovisit[tv++] = loop->header;
943 if (loop->latch == EXIT_BLOCK_PTR)
945 /* There may be blocks unreachable from EXIT_BLOCK. */
946 if (loop->num_nodes != (unsigned) n_basic_blocks + 2)
947 abort ();
948 FOR_EACH_BB (bb)
949 tovisit[tv++] = bb;
950 tovisit[tv++] = EXIT_BLOCK_PTR;
952 else if (loop->latch != loop->header)
954 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
955 tovisit + 1, loop->num_nodes - 1,
956 loop->header) + 1;
959 if (tv != loop->num_nodes)
960 abort ();
961 return tovisit;
964 /* Fills dominance descendants inside LOOP of the basic block BB into
965 array TOVISIT from index *TV. */
967 static void
968 fill_sons_in_loop (const struct loop *loop, basic_block bb,
969 basic_block *tovisit, int *tv)
971 basic_block son, postpone = NULL;
973 tovisit[(*tv)++] = bb;
974 for (son = first_dom_son (CDI_DOMINATORS, bb);
975 son;
976 son = next_dom_son (CDI_DOMINATORS, son))
978 if (!flow_bb_inside_loop_p (loop, son))
979 continue;
981 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
983 postpone = son;
984 continue;
986 fill_sons_in_loop (loop, son, tovisit, tv);
989 if (postpone)
990 fill_sons_in_loop (loop, postpone, tovisit, tv);
993 /* Gets body of a LOOP (that must be different from the outermost loop)
994 sorted by dominance relation. Additionally, if a basic block s dominates
995 the latch, then only blocks dominated by s are be after it. */
997 basic_block *
998 get_loop_body_in_dom_order (const struct loop *loop)
1000 basic_block *tovisit;
1001 int tv;
1003 if (!loop->num_nodes)
1004 abort ();
1006 tovisit = xcalloc (loop->num_nodes, sizeof (basic_block));
1008 if (loop->latch == EXIT_BLOCK_PTR)
1009 abort ();
1011 tv = 0;
1012 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
1014 if (tv != (int) loop->num_nodes)
1015 abort ();
1017 return tovisit;
1020 /* Gets exit edges of a LOOP, returning their number in N_EDGES. */
1021 edge *
1022 get_loop_exit_edges (const struct loop *loop, unsigned int *n_edges)
1024 edge *edges, e;
1025 unsigned i, n;
1026 basic_block * body;
1028 if (loop->latch == EXIT_BLOCK_PTR)
1029 abort ();
1031 body = get_loop_body (loop);
1032 n = 0;
1033 for (i = 0; i < loop->num_nodes; i++)
1034 for (e = body[i]->succ; e; e = e->succ_next)
1035 if (!flow_bb_inside_loop_p (loop, e->dest))
1036 n++;
1037 edges = xmalloc (n * sizeof (edge));
1038 *n_edges = n;
1039 n = 0;
1040 for (i = 0; i < loop->num_nodes; i++)
1041 for (e = body[i]->succ; e; e = e->succ_next)
1042 if (!flow_bb_inside_loop_p (loop, e->dest))
1043 edges[n++] = e;
1044 free (body);
1046 return edges;
1049 /* Counts the number of conditional branches inside LOOP. */
1051 unsigned
1052 num_loop_branches (const struct loop *loop)
1054 unsigned i, n;
1055 basic_block * body;
1057 if (loop->latch == EXIT_BLOCK_PTR)
1058 abort ();
1060 body = get_loop_body (loop);
1061 n = 0;
1062 for (i = 0; i < loop->num_nodes; i++)
1063 if (body[i]->succ && body[i]->succ->succ_next)
1064 n++;
1065 free (body);
1067 return n;
1070 /* Adds basic block BB to LOOP. */
1071 void
1072 add_bb_to_loop (basic_block bb, struct loop *loop)
1074 int i;
1076 bb->loop_father = loop;
1077 bb->loop_depth = loop->depth;
1078 loop->num_nodes++;
1079 for (i = 0; i < loop->depth; i++)
1080 loop->pred[i]->num_nodes++;
1083 /* Remove basic block BB from loops. */
1084 void
1085 remove_bb_from_loops (basic_block bb)
1087 int i;
1088 struct loop *loop = bb->loop_father;
1090 loop->num_nodes--;
1091 for (i = 0; i < loop->depth; i++)
1092 loop->pred[i]->num_nodes--;
1093 bb->loop_father = NULL;
1094 bb->loop_depth = 0;
1097 /* Finds nearest common ancestor in loop tree for given loops. */
1098 struct loop *
1099 find_common_loop (struct loop *loop_s, struct loop *loop_d)
1101 if (!loop_s) return loop_d;
1102 if (!loop_d) return loop_s;
1104 if (loop_s->depth < loop_d->depth)
1105 loop_d = loop_d->pred[loop_s->depth];
1106 else if (loop_s->depth > loop_d->depth)
1107 loop_s = loop_s->pred[loop_d->depth];
1109 while (loop_s != loop_d)
1111 loop_s = loop_s->outer;
1112 loop_d = loop_d->outer;
1114 return loop_s;
1117 /* Cancels the LOOP; it must be innermost one. */
1118 void
1119 cancel_loop (struct loops *loops, struct loop *loop)
1121 basic_block *bbs;
1122 unsigned i;
1124 if (loop->inner)
1125 abort ();
1127 /* Move blocks up one level (they should be removed as soon as possible). */
1128 bbs = get_loop_body (loop);
1129 for (i = 0; i < loop->num_nodes; i++)
1130 bbs[i]->loop_father = loop->outer;
1132 /* Remove the loop from structure. */
1133 flow_loop_tree_node_remove (loop);
1135 /* Remove loop from loops array. */
1136 loops->parray[loop->num] = NULL;
1138 /* Free loop data. */
1139 flow_loop_free (loop);
1142 /* Cancels LOOP and all its subloops. */
1143 void
1144 cancel_loop_tree (struct loops *loops, struct loop *loop)
1146 while (loop->inner)
1147 cancel_loop_tree (loops, loop->inner);
1148 cancel_loop (loops, loop);
1151 /* Checks that LOOPS are all right:
1152 -- sizes of loops are all right
1153 -- results of get_loop_body really belong to the loop
1154 -- loop header have just single entry edge and single latch edge
1155 -- loop latches have only single successor that is header of their loop
1156 -- irreducible loops are correctly marked
1158 void
1159 verify_loop_structure (struct loops *loops)
1161 unsigned *sizes, i, j;
1162 sbitmap irreds;
1163 basic_block *bbs, bb;
1164 struct loop *loop;
1165 int err = 0;
1166 edge e;
1168 /* Check sizes. */
1169 sizes = xcalloc (loops->num, sizeof (int));
1170 sizes[0] = 2;
1172 FOR_EACH_BB (bb)
1173 for (loop = bb->loop_father; loop; loop = loop->outer)
1174 sizes[loop->num]++;
1176 for (i = 0; i < loops->num; i++)
1178 if (!loops->parray[i])
1179 continue;
1181 if (loops->parray[i]->num_nodes != sizes[i])
1183 error ("Size of loop %d should be %d, not %d.",
1184 i, sizes[i], loops->parray[i]->num_nodes);
1185 err = 1;
1189 free (sizes);
1191 /* Check get_loop_body. */
1192 for (i = 1; i < loops->num; i++)
1194 loop = loops->parray[i];
1195 if (!loop)
1196 continue;
1197 bbs = get_loop_body (loop);
1199 for (j = 0; j < loop->num_nodes; j++)
1200 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1202 error ("Bb %d do not belong to loop %d.",
1203 bbs[j]->index, i);
1204 err = 1;
1206 free (bbs);
1209 /* Check headers and latches. */
1210 for (i = 1; i < loops->num; i++)
1212 loop = loops->parray[i];
1213 if (!loop)
1214 continue;
1216 if ((loops->state & LOOPS_HAVE_PREHEADERS)
1217 && (!loop->header->pred->pred_next
1218 || loop->header->pred->pred_next->pred_next))
1220 error ("Loop %d's header does not have exactly 2 entries.", i);
1221 err = 1;
1223 if (loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1225 if (!loop->latch->succ
1226 || loop->latch->succ->succ_next)
1228 error ("Loop %d's latch does not have exactly 1 successor.", i);
1229 err = 1;
1231 if (loop->latch->succ->dest != loop->header)
1233 error ("Loop %d's latch does not have header as successor.", i);
1234 err = 1;
1236 if (loop->latch->loop_father != loop)
1238 error ("Loop %d's latch does not belong directly to it.", i);
1239 err = 1;
1242 if (loop->header->loop_father != loop)
1244 error ("Loop %d's header does not belong directly to it.", i);
1245 err = 1;
1247 if ((loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1248 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1250 error ("Loop %d's latch is marked as part of irreducible region.", i);
1251 err = 1;
1255 /* Check irreducible loops. */
1256 if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1258 /* Record old info. */
1259 irreds = sbitmap_alloc (last_basic_block);
1260 FOR_EACH_BB (bb)
1262 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1263 SET_BIT (irreds, bb->index);
1264 else
1265 RESET_BIT (irreds, bb->index);
1266 for (e = bb->succ; e; e = e->succ_next)
1267 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1268 e->flags |= EDGE_ALL_FLAGS + 1;
1271 /* Recount it. */
1272 mark_irreducible_loops (loops);
1274 /* Compare. */
1275 FOR_EACH_BB (bb)
1277 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1278 && !TEST_BIT (irreds, bb->index))
1280 error ("Basic block %d should be marked irreducible.", bb->index);
1281 err = 1;
1283 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1284 && TEST_BIT (irreds, bb->index))
1286 error ("Basic block %d should not be marked irreducible.", bb->index);
1287 err = 1;
1289 for (e = bb->succ; e; e = e->succ_next)
1291 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1292 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1294 error ("Edge from %d to %d should be marked irreducible.",
1295 e->src->index, e->dest->index);
1296 err = 1;
1298 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1299 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1301 error ("Edge from %d to %d should not be marked irreducible.",
1302 e->src->index, e->dest->index);
1303 err = 1;
1305 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1308 free (irreds);
1311 if (err)
1312 abort ();
1315 /* Returns latch edge of LOOP. */
1316 edge
1317 loop_latch_edge (const struct loop *loop)
1319 edge e;
1321 for (e = loop->header->pred; e->src != loop->latch; e = e->pred_next)
1322 continue;
1324 return e;
1327 /* Returns preheader edge of LOOP. */
1328 edge
1329 loop_preheader_edge (const struct loop *loop)
1331 edge e;
1333 for (e = loop->header->pred; e->src == loop->latch; e = e->pred_next)
1334 continue;
1336 return e;