simplify-rtx.c (simplify_rtx): Use simplify_subreg rather than simplify_gen_subreg.
[official-gcc.git] / gcc / cfgloop.c
blob5a58fb257c16ba9afa1ddfbba7e2156826b63d18
1 /* Natural loop discovery code for GNU compiler.
2 Copyright (C) 2000, 2001, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, 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 "obstack.h"
28 #include "function.h"
29 #include "basic-block.h"
30 #include "toplev.h"
31 #include "cfgloop.h"
32 #include "flags.h"
33 #include "tree.h"
34 #include "tree-flow.h"
36 /* Ratio of frequencies of edges so that one of more latch edges is
37 considered to belong to inner loop with same header. */
38 #define HEAVY_EDGE_RATIO 8
40 #define HEADER_BLOCK(B) (* (int *) (B)->aux)
41 #define LATCH_EDGE(E) (*(int *) (E)->aux)
43 static void flow_loops_cfg_dump (FILE *);
44 static void establish_preds (struct loop *);
45 static void canonicalize_loop_headers (void);
46 static bool glb_enum_p (basic_block, void *);
48 /* Dump loop related CFG information. */
50 static void
51 flow_loops_cfg_dump (FILE *file)
53 basic_block bb;
55 if (!file)
56 return;
58 FOR_EACH_BB (bb)
60 edge succ;
61 edge_iterator ei;
63 fprintf (file, ";; %d succs { ", bb->index);
64 FOR_EACH_EDGE (succ, ei, bb->succs)
65 fprintf (file, "%d ", succ->dest->index);
66 fprintf (file, "}\n");
70 /* Return nonzero if the nodes of LOOP are a subset of OUTER. */
72 bool
73 flow_loop_nested_p (const struct loop *outer, const struct loop *loop)
75 return (loop->depth > outer->depth
76 && loop->pred[outer->depth] == outer);
79 /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
80 loops within LOOP. */
82 struct loop *
83 superloop_at_depth (struct loop *loop, unsigned depth)
85 gcc_assert (depth <= (unsigned) loop->depth);
87 if (depth == (unsigned) loop->depth)
88 return loop;
90 return loop->pred[depth];
93 /* Dump the loop information specified by LOOP to the stream FILE
94 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
96 void
97 flow_loop_dump (const struct loop *loop, FILE *file,
98 void (*loop_dump_aux) (const struct loop *, FILE *, int),
99 int verbose)
101 basic_block *bbs;
102 unsigned i;
104 if (! loop || ! loop->header)
105 return;
107 fprintf (file, ";;\n;; Loop %d\n", loop->num);
109 fprintf (file, ";; header %d, latch %d\n",
110 loop->header->index, loop->latch->index);
111 fprintf (file, ";; depth %d, outer %ld\n",
112 loop->depth, (long) (loop->outer ? loop->outer->num : -1));
114 fprintf (file, ";; nodes:");
115 bbs = get_loop_body (loop);
116 for (i = 0; i < loop->num_nodes; i++)
117 fprintf (file, " %d", bbs[i]->index);
118 free (bbs);
119 fprintf (file, "\n");
121 if (loop_dump_aux)
122 loop_dump_aux (loop, file, verbose);
125 /* Dump the loop information about loops to the stream FILE,
126 using auxiliary dump callback function LOOP_DUMP_AUX if non null. */
128 void
129 flow_loops_dump (FILE *file, void (*loop_dump_aux) (const struct loop *, FILE *, int), int verbose)
131 unsigned i;
133 if (!current_loops || ! file)
134 return;
136 fprintf (file, ";; %d loops found\n", current_loops->num);
138 for (i = 0; i < current_loops->num; i++)
140 struct loop *loop = current_loops->parray[i];
142 if (!loop)
143 continue;
145 flow_loop_dump (loop, file, loop_dump_aux, verbose);
148 if (verbose)
149 flow_loops_cfg_dump (file);
152 /* Free data allocated for LOOP. */
153 void
154 flow_loop_free (struct loop *loop)
156 if (loop->pred)
157 free (loop->pred);
158 free (loop);
161 /* Free all the memory allocated for LOOPS. */
163 void
164 flow_loops_free (struct loops *loops)
166 if (loops->parray)
168 unsigned i;
170 gcc_assert (loops->num);
172 /* Free the loop descriptors. */
173 for (i = 0; i < loops->num; i++)
175 struct loop *loop = loops->parray[i];
177 if (!loop)
178 continue;
180 flow_loop_free (loop);
183 free (loops->parray);
184 loops->parray = NULL;
188 /* Find the nodes contained within the LOOP with header HEADER.
189 Return the number of nodes within the loop. */
192 flow_loop_nodes_find (basic_block header, struct loop *loop)
194 basic_block *stack;
195 int sp;
196 int num_nodes = 1;
198 header->loop_father = loop;
199 header->loop_depth = loop->depth;
201 if (loop->latch->loop_father != loop)
203 stack = XNEWVEC (basic_block, n_basic_blocks);
204 sp = 0;
205 num_nodes++;
206 stack[sp++] = loop->latch;
207 loop->latch->loop_father = loop;
208 loop->latch->loop_depth = loop->depth;
210 while (sp)
212 basic_block node;
213 edge e;
214 edge_iterator ei;
216 node = stack[--sp];
218 FOR_EACH_EDGE (e, ei, node->preds)
220 basic_block ancestor = e->src;
222 if (ancestor != ENTRY_BLOCK_PTR
223 && ancestor->loop_father != loop)
225 ancestor->loop_father = loop;
226 ancestor->loop_depth = loop->depth;
227 num_nodes++;
228 stack[sp++] = ancestor;
232 free (stack);
234 return num_nodes;
237 /* For each loop that has just a single exit, record the exit edge. */
239 void
240 mark_single_exit_loops (void)
242 basic_block bb;
243 edge e;
244 struct loop *loop;
245 unsigned i;
247 for (i = 1; i < current_loops->num; i++)
249 loop = current_loops->parray[i];
250 if (loop)
251 set_single_exit (loop, NULL);
254 FOR_EACH_BB (bb)
256 edge_iterator ei;
257 if (bb->loop_father == current_loops->tree_root)
258 continue;
259 FOR_EACH_EDGE (e, ei, bb->succs)
261 if (e->dest == EXIT_BLOCK_PTR)
262 continue;
264 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
265 continue;
267 for (loop = bb->loop_father;
268 loop != e->dest->loop_father;
269 loop = loop->outer)
271 /* If we have already seen an exit, mark this by the edge that
272 surely does not occur as any exit. */
273 if (single_exit (loop))
274 set_single_exit (loop, single_succ_edge (ENTRY_BLOCK_PTR));
275 else
276 set_single_exit (loop, e);
281 for (i = 1; i < current_loops->num; i++)
283 loop = current_loops->parray[i];
284 if (!loop)
285 continue;
287 if (single_exit (loop) == single_succ_edge (ENTRY_BLOCK_PTR))
288 set_single_exit (loop, NULL);
291 current_loops->state |= LOOPS_HAVE_MARKED_SINGLE_EXITS;
294 static void
295 establish_preds (struct loop *loop)
297 struct loop *ploop, *father = loop->outer;
299 loop->depth = father->depth + 1;
301 /* Remember the current loop depth if it is the largest seen so far. */
302 cfun->max_loop_depth = MAX (cfun->max_loop_depth, loop->depth);
304 if (loop->pred)
305 free (loop->pred);
306 loop->pred = XNEWVEC (struct loop *, loop->depth);
307 memcpy (loop->pred, father->pred, sizeof (struct loop *) * father->depth);
308 loop->pred[father->depth] = father;
310 for (ploop = loop->inner; ploop; ploop = ploop->next)
311 establish_preds (ploop);
314 /* Add LOOP to the loop hierarchy tree where FATHER is father of the
315 added loop. If LOOP has some children, take care of that their
316 pred field will be initialized correctly. */
318 void
319 flow_loop_tree_node_add (struct loop *father, struct loop *loop)
321 loop->next = father->inner;
322 father->inner = loop;
323 loop->outer = father;
325 establish_preds (loop);
328 /* Remove LOOP from the loop hierarchy tree. */
330 void
331 flow_loop_tree_node_remove (struct loop *loop)
333 struct loop *prev, *father;
335 father = loop->outer;
336 loop->outer = NULL;
338 /* Remove loop from the list of sons. */
339 if (father->inner == loop)
340 father->inner = loop->next;
341 else
343 for (prev = father->inner; prev->next != loop; prev = prev->next);
344 prev->next = loop->next;
347 loop->depth = -1;
348 free (loop->pred);
349 loop->pred = NULL;
352 /* A callback to update latch and header info for basic block JUMP created
353 by redirecting an edge. */
355 static void
356 update_latch_info (basic_block jump)
358 alloc_aux_for_block (jump, sizeof (int));
359 HEADER_BLOCK (jump) = 0;
360 alloc_aux_for_edge (single_pred_edge (jump), sizeof (int));
361 LATCH_EDGE (single_pred_edge (jump)) = 0;
362 set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
365 /* A callback for make_forwarder block, to redirect all edges except for
366 MFB_KJ_EDGE to the entry part. E is the edge for that we should decide
367 whether to redirect it. */
369 static edge mfb_kj_edge;
370 static bool
371 mfb_keep_just (edge e)
373 return e != mfb_kj_edge;
376 /* A callback for make_forwarder block, to redirect the latch edges into an
377 entry part. E is the edge for that we should decide whether to redirect
378 it. */
380 static bool
381 mfb_keep_nonlatch (edge e)
383 return LATCH_EDGE (e);
386 /* Takes care of merging natural loops with shared headers. */
388 static void
389 canonicalize_loop_headers (void)
391 basic_block header;
392 edge e;
394 alloc_aux_for_blocks (sizeof (int));
395 alloc_aux_for_edges (sizeof (int));
397 /* Split blocks so that each loop has only single latch. */
398 FOR_EACH_BB (header)
400 edge_iterator ei;
401 int num_latches = 0;
402 int have_abnormal_edge = 0;
404 FOR_EACH_EDGE (e, ei, header->preds)
406 basic_block latch = e->src;
408 if (e->flags & EDGE_ABNORMAL)
409 have_abnormal_edge = 1;
411 if (latch != ENTRY_BLOCK_PTR
412 && dominated_by_p (CDI_DOMINATORS, latch, header))
414 num_latches++;
415 LATCH_EDGE (e) = 1;
418 if (have_abnormal_edge)
419 HEADER_BLOCK (header) = 0;
420 else
421 HEADER_BLOCK (header) = num_latches;
424 if (HEADER_BLOCK (single_succ (ENTRY_BLOCK_PTR)))
426 basic_block bb;
428 /* We could not redirect edges freely here. On the other hand,
429 we can simply split the edge from entry block. */
430 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
432 alloc_aux_for_edge (single_succ_edge (bb), sizeof (int));
433 LATCH_EDGE (single_succ_edge (bb)) = 0;
434 alloc_aux_for_block (bb, sizeof (int));
435 HEADER_BLOCK (bb) = 0;
438 FOR_EACH_BB (header)
440 int max_freq, is_heavy;
441 edge heavy, tmp_edge;
442 edge_iterator ei;
444 if (HEADER_BLOCK (header) <= 1)
445 continue;
447 /* Find a heavy edge. */
448 is_heavy = 1;
449 heavy = NULL;
450 max_freq = 0;
451 FOR_EACH_EDGE (e, ei, header->preds)
452 if (LATCH_EDGE (e) &&
453 EDGE_FREQUENCY (e) > max_freq)
454 max_freq = EDGE_FREQUENCY (e);
455 FOR_EACH_EDGE (e, ei, header->preds)
456 if (LATCH_EDGE (e) &&
457 EDGE_FREQUENCY (e) >= max_freq / HEAVY_EDGE_RATIO)
459 if (heavy)
461 is_heavy = 0;
462 break;
464 else
465 heavy = e;
468 if (is_heavy)
470 /* Split out the heavy edge, and create inner loop for it. */
471 mfb_kj_edge = heavy;
472 tmp_edge = make_forwarder_block (header, mfb_keep_just,
473 update_latch_info);
474 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
475 HEADER_BLOCK (tmp_edge->dest) = 1;
476 alloc_aux_for_edge (tmp_edge, sizeof (int));
477 LATCH_EDGE (tmp_edge) = 0;
478 HEADER_BLOCK (header)--;
481 if (HEADER_BLOCK (header) > 1)
483 /* Create a new latch block. */
484 tmp_edge = make_forwarder_block (header, mfb_keep_nonlatch,
485 update_latch_info);
486 alloc_aux_for_block (tmp_edge->dest, sizeof (int));
487 HEADER_BLOCK (tmp_edge->src) = 0;
488 HEADER_BLOCK (tmp_edge->dest) = 1;
489 alloc_aux_for_edge (tmp_edge, sizeof (int));
490 LATCH_EDGE (tmp_edge) = 1;
494 free_aux_for_blocks ();
495 free_aux_for_edges ();
497 #ifdef ENABLE_CHECKING
498 verify_dominators (CDI_DOMINATORS);
499 #endif
502 /* Initialize all the parallel_p fields of the loops structure to true. */
504 static void
505 initialize_loops_parallel_p (struct loops *loops)
507 unsigned int i;
509 for (i = 0; i < loops->num; i++)
511 struct loop *loop = loops->parray[i];
512 loop->parallel_p = true;
516 /* Find all the natural loops in the function and save in LOOPS structure and
517 recalculate loop_depth information in basic block structures.
518 Return the number of natural loops found. */
521 flow_loops_find (struct loops *loops)
523 int b;
524 int num_loops;
525 edge e;
526 sbitmap headers;
527 int *dfs_order;
528 int *rc_order;
529 basic_block header;
530 basic_block bb;
532 memset (loops, 0, sizeof *loops);
534 /* We are going to recount the maximum loop depth,
535 so throw away the last count. */
536 cfun->max_loop_depth = 0;
538 /* Taking care of this degenerate case makes the rest of
539 this code simpler. */
540 if (n_basic_blocks == NUM_FIXED_BLOCKS)
541 return 0;
543 dfs_order = NULL;
544 rc_order = NULL;
546 /* Ensure that the dominators are computed. */
547 calculate_dominance_info (CDI_DOMINATORS);
549 /* Join loops with shared headers. */
550 canonicalize_loop_headers ();
552 /* Count the number of loop headers. This should be the
553 same as the number of natural loops. */
554 headers = sbitmap_alloc (last_basic_block);
555 sbitmap_zero (headers);
557 num_loops = 0;
558 FOR_EACH_BB (header)
560 edge_iterator ei;
561 int more_latches = 0;
563 header->loop_depth = 0;
565 /* If we have an abnormal predecessor, do not consider the
566 loop (not worth the problems). */
567 FOR_EACH_EDGE (e, ei, header->preds)
568 if (e->flags & EDGE_ABNORMAL)
569 break;
570 if (e)
571 continue;
573 FOR_EACH_EDGE (e, ei, header->preds)
575 basic_block latch = e->src;
577 gcc_assert (!(e->flags & EDGE_ABNORMAL));
579 /* Look for back edges where a predecessor is dominated
580 by this block. A natural loop has a single entry
581 node (header) that dominates all the nodes in the
582 loop. It also has single back edge to the header
583 from a latch node. */
584 if (latch != ENTRY_BLOCK_PTR
585 && dominated_by_p (CDI_DOMINATORS, latch, header))
587 /* Shared headers should be eliminated by now. */
588 gcc_assert (!more_latches);
589 more_latches = 1;
590 SET_BIT (headers, header->index);
591 num_loops++;
596 /* Allocate loop structures. */
597 loops->parray = XCNEWVEC (struct loop *, num_loops + 1);
599 /* Dummy loop containing whole function. */
600 loops->parray[0] = XCNEW (struct loop);
601 loops->parray[0]->next = NULL;
602 loops->parray[0]->inner = NULL;
603 loops->parray[0]->outer = NULL;
604 loops->parray[0]->depth = 0;
605 loops->parray[0]->pred = NULL;
606 loops->parray[0]->num_nodes = n_basic_blocks;
607 loops->parray[0]->latch = EXIT_BLOCK_PTR;
608 loops->parray[0]->header = ENTRY_BLOCK_PTR;
609 ENTRY_BLOCK_PTR->loop_father = loops->parray[0];
610 EXIT_BLOCK_PTR->loop_father = loops->parray[0];
612 loops->tree_root = loops->parray[0];
614 /* Find and record information about all the natural loops
615 in the CFG. */
616 loops->num = 1;
617 FOR_EACH_BB (bb)
618 bb->loop_father = loops->tree_root;
620 if (num_loops)
622 /* Compute depth first search order of the CFG so that outer
623 natural loops will be found before inner natural loops. */
624 dfs_order = XNEWVEC (int, n_basic_blocks);
625 rc_order = XNEWVEC (int, n_basic_blocks);
626 pre_and_rev_post_order_compute (dfs_order, rc_order, false);
628 num_loops = 1;
630 for (b = 0; b < n_basic_blocks - NUM_FIXED_BLOCKS; b++)
632 struct loop *loop;
633 edge_iterator ei;
635 /* Search the nodes of the CFG in reverse completion order
636 so that we can find outer loops first. */
637 if (!TEST_BIT (headers, rc_order[b]))
638 continue;
640 header = BASIC_BLOCK (rc_order[b]);
642 loop = loops->parray[num_loops] = XCNEW (struct loop);
644 loop->header = header;
645 loop->num = num_loops;
646 num_loops++;
648 /* Look for the latch for this header block. */
649 FOR_EACH_EDGE (e, ei, header->preds)
651 basic_block latch = e->src;
653 if (latch != ENTRY_BLOCK_PTR
654 && dominated_by_p (CDI_DOMINATORS, latch, header))
656 loop->latch = latch;
657 break;
661 flow_loop_tree_node_add (header->loop_father, loop);
662 loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
665 loops->num = num_loops;
666 initialize_loops_parallel_p (loops);
668 free (dfs_order);
669 free (rc_order);
672 sbitmap_free (headers);
674 loops->state = 0;
675 return loops->num;
678 /* Return nonzero if basic block BB belongs to LOOP. */
679 bool
680 flow_bb_inside_loop_p (const struct loop *loop, const basic_block bb)
682 struct loop *source_loop;
684 if (bb == ENTRY_BLOCK_PTR || bb == EXIT_BLOCK_PTR)
685 return 0;
687 source_loop = bb->loop_father;
688 return loop == source_loop || flow_loop_nested_p (loop, source_loop);
691 /* Enumeration predicate for get_loop_body. */
692 static bool
693 glb_enum_p (basic_block bb, void *glb_header)
695 return bb != (basic_block) glb_header;
698 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
699 order against direction of edges from latch. Specially, if
700 header != latch, latch is the 1-st block. */
701 basic_block *
702 get_loop_body (const struct loop *loop)
704 basic_block *tovisit, bb;
705 unsigned tv = 0;
707 gcc_assert (loop->num_nodes);
709 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
710 tovisit[tv++] = loop->header;
712 if (loop->latch == EXIT_BLOCK_PTR)
714 /* There may be blocks unreachable from EXIT_BLOCK. */
715 gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks);
716 FOR_EACH_BB (bb)
717 tovisit[tv++] = bb;
718 tovisit[tv++] = EXIT_BLOCK_PTR;
720 else if (loop->latch != loop->header)
722 tv = dfs_enumerate_from (loop->latch, 1, glb_enum_p,
723 tovisit + 1, loop->num_nodes - 1,
724 loop->header) + 1;
727 gcc_assert (tv == loop->num_nodes);
728 return tovisit;
731 /* Fills dominance descendants inside LOOP of the basic block BB into
732 array TOVISIT from index *TV. */
734 static void
735 fill_sons_in_loop (const struct loop *loop, basic_block bb,
736 basic_block *tovisit, int *tv)
738 basic_block son, postpone = NULL;
740 tovisit[(*tv)++] = bb;
741 for (son = first_dom_son (CDI_DOMINATORS, bb);
742 son;
743 son = next_dom_son (CDI_DOMINATORS, son))
745 if (!flow_bb_inside_loop_p (loop, son))
746 continue;
748 if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
750 postpone = son;
751 continue;
753 fill_sons_in_loop (loop, son, tovisit, tv);
756 if (postpone)
757 fill_sons_in_loop (loop, postpone, tovisit, tv);
760 /* Gets body of a LOOP (that must be different from the outermost loop)
761 sorted by dominance relation. Additionally, if a basic block s dominates
762 the latch, then only blocks dominated by s are be after it. */
764 basic_block *
765 get_loop_body_in_dom_order (const struct loop *loop)
767 basic_block *tovisit;
768 int tv;
770 gcc_assert (loop->num_nodes);
772 tovisit = XCNEWVEC (basic_block, loop->num_nodes);
774 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
776 tv = 0;
777 fill_sons_in_loop (loop, loop->header, tovisit, &tv);
779 gcc_assert (tv == (int) loop->num_nodes);
781 return tovisit;
784 /* Get body of a LOOP in breadth first sort order. */
786 basic_block *
787 get_loop_body_in_bfs_order (const struct loop *loop)
789 basic_block *blocks;
790 basic_block bb;
791 bitmap visited;
792 unsigned int i = 0;
793 unsigned int vc = 1;
795 gcc_assert (loop->num_nodes);
796 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
798 blocks = XCNEWVEC (basic_block, loop->num_nodes);
799 visited = BITMAP_ALLOC (NULL);
801 bb = loop->header;
802 while (i < loop->num_nodes)
804 edge e;
805 edge_iterator ei;
807 if (!bitmap_bit_p (visited, bb->index))
809 /* This basic block is now visited */
810 bitmap_set_bit (visited, bb->index);
811 blocks[i++] = bb;
814 FOR_EACH_EDGE (e, ei, bb->succs)
816 if (flow_bb_inside_loop_p (loop, e->dest))
818 if (!bitmap_bit_p (visited, e->dest->index))
820 bitmap_set_bit (visited, e->dest->index);
821 blocks[i++] = e->dest;
826 gcc_assert (i >= vc);
828 bb = blocks[vc++];
831 BITMAP_FREE (visited);
832 return blocks;
835 /* Returns the list of the exit edges of a LOOP. */
837 VEC (edge, heap) *
838 get_loop_exit_edges (const struct loop *loop)
840 VEC (edge, heap) *edges = NULL;
841 edge e;
842 unsigned i;
843 basic_block *body;
844 edge_iterator ei;
846 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
848 body = get_loop_body (loop);
849 for (i = 0; i < loop->num_nodes; i++)
850 FOR_EACH_EDGE (e, ei, body[i]->succs)
851 if (!flow_bb_inside_loop_p (loop, e->dest))
852 VEC_safe_push (edge, heap, edges, e);
853 free (body);
855 return edges;
858 /* Counts the number of conditional branches inside LOOP. */
860 unsigned
861 num_loop_branches (const struct loop *loop)
863 unsigned i, n;
864 basic_block * body;
866 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
868 body = get_loop_body (loop);
869 n = 0;
870 for (i = 0; i < loop->num_nodes; i++)
871 if (EDGE_COUNT (body[i]->succs) >= 2)
872 n++;
873 free (body);
875 return n;
878 /* Adds basic block BB to LOOP. */
879 void
880 add_bb_to_loop (basic_block bb, struct loop *loop)
882 int i;
884 gcc_assert (bb->loop_father == NULL);
885 bb->loop_father = loop;
886 bb->loop_depth = loop->depth;
887 loop->num_nodes++;
888 for (i = 0; i < loop->depth; i++)
889 loop->pred[i]->num_nodes++;
892 /* Remove basic block BB from loops. */
893 void
894 remove_bb_from_loops (basic_block bb)
896 int i;
897 struct loop *loop = bb->loop_father;
899 gcc_assert (loop != NULL);
900 loop->num_nodes--;
901 for (i = 0; i < loop->depth; i++)
902 loop->pred[i]->num_nodes--;
903 bb->loop_father = NULL;
904 bb->loop_depth = 0;
907 /* Finds nearest common ancestor in loop tree for given loops. */
908 struct loop *
909 find_common_loop (struct loop *loop_s, struct loop *loop_d)
911 if (!loop_s) return loop_d;
912 if (!loop_d) return loop_s;
914 if (loop_s->depth < loop_d->depth)
915 loop_d = loop_d->pred[loop_s->depth];
916 else if (loop_s->depth > loop_d->depth)
917 loop_s = loop_s->pred[loop_d->depth];
919 while (loop_s != loop_d)
921 loop_s = loop_s->outer;
922 loop_d = loop_d->outer;
924 return loop_s;
927 /* Cancels the LOOP; it must be innermost one. */
929 static void
930 cancel_loop (struct loop *loop)
932 basic_block *bbs;
933 unsigned i;
935 gcc_assert (!loop->inner);
937 /* Move blocks up one level (they should be removed as soon as possible). */
938 bbs = get_loop_body (loop);
939 for (i = 0; i < loop->num_nodes; i++)
940 bbs[i]->loop_father = loop->outer;
942 /* Remove the loop from structure. */
943 flow_loop_tree_node_remove (loop);
945 /* Remove loop from loops array. */
946 current_loops->parray[loop->num] = NULL;
948 /* Free loop data. */
949 flow_loop_free (loop);
952 /* Cancels LOOP and all its subloops. */
953 void
954 cancel_loop_tree (struct loop *loop)
956 while (loop->inner)
957 cancel_loop_tree (loop->inner);
958 cancel_loop (loop);
961 /* Checks that information about loops is correct
962 -- sizes of loops are all right
963 -- results of get_loop_body really belong to the loop
964 -- loop header have just single entry edge and single latch edge
965 -- loop latches have only single successor that is header of their loop
966 -- irreducible loops are correctly marked
968 void
969 verify_loop_structure (void)
971 unsigned *sizes, i, j;
972 sbitmap irreds;
973 basic_block *bbs, bb;
974 struct loop *loop;
975 int err = 0;
976 edge e;
978 /* Check sizes. */
979 sizes = XCNEWVEC (unsigned, current_loops->num);
980 sizes[0] = 2;
982 FOR_EACH_BB (bb)
983 for (loop = bb->loop_father; loop; loop = loop->outer)
984 sizes[loop->num]++;
986 for (i = 0; i < current_loops->num; i++)
988 if (!current_loops->parray[i])
989 continue;
991 if (current_loops->parray[i]->num_nodes != sizes[i])
993 error ("size of loop %d should be %d, not %d",
994 i, sizes[i], current_loops->parray[i]->num_nodes);
995 err = 1;
999 /* Check get_loop_body. */
1000 for (i = 1; i < current_loops->num; i++)
1002 loop = current_loops->parray[i];
1003 if (!loop)
1004 continue;
1005 bbs = get_loop_body (loop);
1007 for (j = 0; j < loop->num_nodes; j++)
1008 if (!flow_bb_inside_loop_p (loop, bbs[j]))
1010 error ("bb %d do not belong to loop %d",
1011 bbs[j]->index, i);
1012 err = 1;
1014 free (bbs);
1017 /* Check headers and latches. */
1018 for (i = 1; i < current_loops->num; i++)
1020 loop = current_loops->parray[i];
1021 if (!loop)
1022 continue;
1024 if ((current_loops->state & LOOPS_HAVE_PREHEADERS)
1025 && EDGE_COUNT (loop->header->preds) != 2)
1027 error ("loop %d's header does not have exactly 2 entries", i);
1028 err = 1;
1030 if (current_loops->state & LOOPS_HAVE_SIMPLE_LATCHES)
1032 if (!single_succ_p (loop->latch))
1034 error ("loop %d's latch does not have exactly 1 successor", i);
1035 err = 1;
1037 if (single_succ (loop->latch) != loop->header)
1039 error ("loop %d's latch does not have header as successor", i);
1040 err = 1;
1042 if (loop->latch->loop_father != loop)
1044 error ("loop %d's latch does not belong directly to it", i);
1045 err = 1;
1048 if (loop->header->loop_father != loop)
1050 error ("loop %d's header does not belong directly to it", i);
1051 err = 1;
1053 if ((current_loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1054 && (loop_latch_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP))
1056 error ("loop %d's latch is marked as part of irreducible region", i);
1057 err = 1;
1061 /* Check irreducible loops. */
1062 if (current_loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1064 /* Record old info. */
1065 irreds = sbitmap_alloc (last_basic_block);
1066 FOR_EACH_BB (bb)
1068 edge_iterator ei;
1069 if (bb->flags & BB_IRREDUCIBLE_LOOP)
1070 SET_BIT (irreds, bb->index);
1071 else
1072 RESET_BIT (irreds, bb->index);
1073 FOR_EACH_EDGE (e, ei, bb->succs)
1074 if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1075 e->flags |= EDGE_ALL_FLAGS + 1;
1078 /* Recount it. */
1079 mark_irreducible_loops ();
1081 /* Compare. */
1082 FOR_EACH_BB (bb)
1084 edge_iterator ei;
1086 if ((bb->flags & BB_IRREDUCIBLE_LOOP)
1087 && !TEST_BIT (irreds, bb->index))
1089 error ("basic block %d should be marked irreducible", bb->index);
1090 err = 1;
1092 else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
1093 && TEST_BIT (irreds, bb->index))
1095 error ("basic block %d should not be marked irreducible", bb->index);
1096 err = 1;
1098 FOR_EACH_EDGE (e, ei, bb->succs)
1100 if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
1101 && !(e->flags & (EDGE_ALL_FLAGS + 1)))
1103 error ("edge from %d to %d should be marked irreducible",
1104 e->src->index, e->dest->index);
1105 err = 1;
1107 else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
1108 && (e->flags & (EDGE_ALL_FLAGS + 1)))
1110 error ("edge from %d to %d should not be marked irreducible",
1111 e->src->index, e->dest->index);
1112 err = 1;
1114 e->flags &= ~(EDGE_ALL_FLAGS + 1);
1117 free (irreds);
1120 /* Check the single_exit. */
1121 if (current_loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
1123 memset (sizes, 0, sizeof (unsigned) * current_loops->num);
1124 FOR_EACH_BB (bb)
1126 edge_iterator ei;
1127 if (bb->loop_father == current_loops->tree_root)
1128 continue;
1129 FOR_EACH_EDGE (e, ei, bb->succs)
1131 if (e->dest == EXIT_BLOCK_PTR)
1132 continue;
1134 if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
1135 continue;
1137 for (loop = bb->loop_father;
1138 loop != e->dest->loop_father;
1139 loop = loop->outer)
1141 sizes[loop->num]++;
1142 if (single_exit (loop)
1143 && single_exit (loop) != e)
1145 error ("wrong single exit %d->%d recorded for loop %d",
1146 single_exit (loop)->src->index,
1147 single_exit (loop)->dest->index,
1148 loop->num);
1149 error ("right exit is %d->%d",
1150 e->src->index, e->dest->index);
1151 err = 1;
1157 for (i = 1; i < current_loops->num; i++)
1159 loop = current_loops->parray[i];
1160 if (!loop)
1161 continue;
1163 if (sizes[i] == 1
1164 && !single_exit (loop))
1166 error ("single exit not recorded for loop %d", loop->num);
1167 err = 1;
1170 if (sizes[i] != 1
1171 && single_exit (loop))
1173 error ("loop %d should not have single exit (%d -> %d)",
1174 loop->num,
1175 single_exit (loop)->src->index,
1176 single_exit (loop)->dest->index);
1177 err = 1;
1182 gcc_assert (!err);
1184 free (sizes);
1187 /* Returns latch edge of LOOP. */
1188 edge
1189 loop_latch_edge (const struct loop *loop)
1191 return find_edge (loop->latch, loop->header);
1194 /* Returns preheader edge of LOOP. */
1195 edge
1196 loop_preheader_edge (const struct loop *loop)
1198 edge e;
1199 edge_iterator ei;
1201 FOR_EACH_EDGE (e, ei, loop->header->preds)
1202 if (e->src != loop->latch)
1203 break;
1205 return e;
1208 /* Returns true if E is an exit of LOOP. */
1210 bool
1211 loop_exit_edge_p (const struct loop *loop, edge e)
1213 return (flow_bb_inside_loop_p (loop, e->src)
1214 && !flow_bb_inside_loop_p (loop, e->dest));
1217 /* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
1218 or more than one exit. */
1220 edge
1221 single_exit (const struct loop *loop)
1223 return loop->single_exit_;
1226 /* Records E as a single exit edge of LOOP. */
1228 void
1229 set_single_exit (struct loop *loop, edge e)
1231 loop->single_exit_ = e;