1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This file contains low level functions to manipulate the CFG and
23 analyze it. All other modules should not transform the datastructure
24 directly and use abstraction instead. The file is supposed to be
25 ordered bottom-up and should not contain any code dependent on a
26 particular intermediate language (RTL or trees).
28 Available functionality:
29 - Initialization/deallocation
30 init_flow, clear_edges
31 - Low level basic block manipulation
32 alloc_block, expunge_block
34 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
35 - Low level edge redirection (without updating instruction chain)
36 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
37 - Dumping and debugging
38 dump_flow_info, debug_flow_info, dump_edge_info
39 - Allocation of AUX fields for basic blocks
40 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
42 - Consistency checking
44 - Dumping and debugging
45 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
50 #include "coretypes.h"
54 #include "hard-reg-set.h"
55 #include "basic-block.h"
64 #include "alloc-pool.h"
66 /* The obstack on which the flow graph components are allocated. */
68 struct obstack flow_obstack
;
69 static char *flow_firstobj
;
71 /* Basic block object pool. */
73 static alloc_pool bb_pool
;
75 /* Edge object pool. */
77 static alloc_pool edge_pool
;
79 /* Number of basic blocks in the current function. */
83 /* First free basic block number. */
87 /* Number of edges in the current function. */
91 /* The basic block array. */
93 varray_type basic_block_info
;
95 /* The special entry and exit blocks. */
97 struct basic_block_def entry_exit_blocks
[2]
100 NULL
, /* head_tree */
104 NULL
, /* local_set */
105 NULL
, /* cond_local_set */
106 NULL
, /* global_live_at_start */
107 NULL
, /* global_live_at_end */
109 ENTRY_BLOCK
, /* index */
111 EXIT_BLOCK_PTR
, /* next_bb */
113 NULL
, /* loop_father */
121 NULL
, /* head_tree */
125 NULL
, /* local_set */
126 NULL
, /* cond_local_set */
127 NULL
, /* global_live_at_start */
128 NULL
, /* global_live_at_end */
130 EXIT_BLOCK
, /* index */
131 ENTRY_BLOCK_PTR
, /* prev_bb */
134 NULL
, /* loop_father */
141 void debug_flow_info
PARAMS ((void));
142 static void free_edge
PARAMS ((edge
));
144 /* Called once at initialization time. */
149 static int initialized
;
155 gcc_obstack_init (&flow_obstack
);
156 flow_firstobj
= (char *) obstack_alloc (&flow_obstack
, 0);
161 free_alloc_pool (bb_pool
);
162 free_alloc_pool (edge_pool
);
163 obstack_free (&flow_obstack
, flow_firstobj
);
164 flow_firstobj
= (char *) obstack_alloc (&flow_obstack
, 0);
166 bb_pool
= create_alloc_pool ("Basic block pool",
167 sizeof (struct basic_block_def
), 100);
168 edge_pool
= create_alloc_pool ("Edge pool",
169 sizeof (struct edge_def
), 100);
172 /* Helper function for remove_edge and clear_edges. Frees edge structure
173 without actually unlinking it from the pred/succ lists. */
180 pool_free (edge_pool
, e
);
183 /* Free the memory associated with the edge structures. */
197 edge next
= e
->succ_next
;
207 e
= ENTRY_BLOCK_PTR
->succ
;
210 edge next
= e
->succ_next
;
216 EXIT_BLOCK_PTR
->pred
= NULL
;
217 ENTRY_BLOCK_PTR
->succ
= NULL
;
223 /* Allocate memory for basic_block. */
229 bb
= pool_alloc (bb_pool
);
230 memset (bb
, 0, sizeof (*bb
));
234 /* Link block B to chain after AFTER. */
236 link_block (b
, after
)
237 basic_block b
, after
;
239 b
->next_bb
= after
->next_bb
;
242 b
->next_bb
->prev_bb
= b
;
245 /* Unlink block B from chain. */
250 b
->next_bb
->prev_bb
= b
->prev_bb
;
251 b
->prev_bb
->next_bb
= b
->next_bb
;
254 /* Sequentially order blocks and compact the arrays. */
264 BASIC_BLOCK (i
) = bb
;
269 if (i
!= n_basic_blocks
)
272 last_basic_block
= n_basic_blocks
;
275 /* Remove block B from the basic block array. */
282 BASIC_BLOCK (b
->index
) = NULL
;
284 pool_free (bb_pool
, b
);
287 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
288 created edge. Use this only if you are sure that this edge can't
289 possibly already exist. */
292 unchecked_make_edge (src
, dst
, flags
)
293 basic_block src
, dst
;
297 e
= pool_alloc (edge_pool
);
298 memset (e
, 0, sizeof (*e
));
301 e
->succ_next
= src
->succ
;
302 e
->pred_next
= dst
->pred
;
313 /* Create an edge connecting SRC and DST with FLAGS optionally using
314 edge cache CACHE. Return the new edge, NULL if already exist. */
317 cached_make_edge (edge_cache
, src
, dst
, flags
)
319 basic_block src
, dst
;
325 /* Don't bother with edge cache for ENTRY or EXIT, if there aren't that
326 many edges to them, or we didn't allocate memory for it. */
327 use_edge_cache
= (edge_cache
328 && src
!= ENTRY_BLOCK_PTR
&& dst
!= EXIT_BLOCK_PTR
);
330 /* Make sure we don't add duplicate edges. */
331 switch (use_edge_cache
)
334 /* Quick test for non-existence of the edge. */
335 if (! TEST_BIT (edge_cache
[src
->index
], dst
->index
))
338 /* The edge exists; early exit if no work to do. */
344 for (e
= src
->succ
; e
; e
= e
->succ_next
)
353 e
= unchecked_make_edge (src
, dst
, flags
);
356 SET_BIT (edge_cache
[src
->index
], dst
->index
);
361 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
362 created edge or NULL if already exist. */
365 make_edge (src
, dest
, flags
)
366 basic_block src
, dest
;
369 return cached_make_edge (NULL
, src
, dest
, flags
);
372 /* Create an edge connecting SRC to DEST and set probability by knowing
373 that it is the single edge leaving SRC. */
376 make_single_succ_edge (src
, dest
, flags
)
377 basic_block src
, dest
;
380 edge e
= make_edge (src
, dest
, flags
);
382 e
->probability
= REG_BR_PROB_BASE
;
383 e
->count
= src
->count
;
387 /* This function will remove an edge from the flow graph. */
393 edge last_pred
= NULL
;
394 edge last_succ
= NULL
;
396 basic_block src
, dest
;
400 for (tmp
= src
->succ
; tmp
&& tmp
!= e
; tmp
= tmp
->succ_next
)
406 last_succ
->succ_next
= e
->succ_next
;
408 src
->succ
= e
->succ_next
;
410 for (tmp
= dest
->pred
; tmp
&& tmp
!= e
; tmp
= tmp
->pred_next
)
416 last_pred
->pred_next
= e
->pred_next
;
418 dest
->pred
= e
->pred_next
;
423 /* Redirect an edge's successor from one block to another. */
426 redirect_edge_succ (e
, new_succ
)
428 basic_block new_succ
;
432 /* Disconnect the edge from the old successor block. */
433 for (pe
= &e
->dest
->pred
; *pe
!= e
; pe
= &(*pe
)->pred_next
)
435 *pe
= (*pe
)->pred_next
;
437 /* Reconnect the edge to the new successor block. */
438 e
->pred_next
= new_succ
->pred
;
443 /* Like previous but avoid possible duplicate edge. */
446 redirect_edge_succ_nodup (e
, new_succ
)
448 basic_block new_succ
;
452 /* Check whether the edge is already present. */
453 for (s
= e
->src
->succ
; s
; s
= s
->succ_next
)
454 if (s
->dest
== new_succ
&& s
!= e
)
459 s
->flags
|= e
->flags
;
460 s
->probability
+= e
->probability
;
461 if (s
->probability
> REG_BR_PROB_BASE
)
462 s
->probability
= REG_BR_PROB_BASE
;
463 s
->count
+= e
->count
;
468 redirect_edge_succ (e
, new_succ
);
473 /* Redirect an edge's predecessor from one block to another. */
476 redirect_edge_pred (e
, new_pred
)
478 basic_block new_pred
;
482 /* Disconnect the edge from the old predecessor block. */
483 for (pe
= &e
->src
->succ
; *pe
!= e
; pe
= &(*pe
)->succ_next
)
486 *pe
= (*pe
)->succ_next
;
488 /* Reconnect the edge to the new predecessor block. */
489 e
->succ_next
= new_pred
->succ
;
499 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
504 dump_flow_info (file
)
508 int max_regno
= max_reg_num ();
510 static const char * const reg_class_names
[] = REG_CLASS_NAMES
;
512 fprintf (file
, "%d registers.\n", max_regno
);
513 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
516 enum reg_class
class, altclass
;
518 fprintf (file
, "\nRegister %d used %d times across %d insns",
519 i
, REG_N_REFS (i
), REG_LIVE_LENGTH (i
));
520 if (REG_BASIC_BLOCK (i
) >= 0)
521 fprintf (file
, " in block %d", REG_BASIC_BLOCK (i
));
523 fprintf (file
, "; set %d time%s", REG_N_SETS (i
),
524 (REG_N_SETS (i
) == 1) ? "" : "s");
525 if (regno_reg_rtx
[i
] != NULL
&& REG_USERVAR_P (regno_reg_rtx
[i
]))
526 fprintf (file
, "; user var");
527 if (REG_N_DEATHS (i
) != 1)
528 fprintf (file
, "; dies in %d places", REG_N_DEATHS (i
));
529 if (REG_N_CALLS_CROSSED (i
) == 1)
530 fprintf (file
, "; crosses 1 call");
531 else if (REG_N_CALLS_CROSSED (i
))
532 fprintf (file
, "; crosses %d calls", REG_N_CALLS_CROSSED (i
));
533 if (regno_reg_rtx
[i
] != NULL
534 && PSEUDO_REGNO_BYTES (i
) != UNITS_PER_WORD
)
535 fprintf (file
, "; %d bytes", PSEUDO_REGNO_BYTES (i
));
537 class = reg_preferred_class (i
);
538 altclass
= reg_alternate_class (i
);
539 if (class != GENERAL_REGS
|| altclass
!= ALL_REGS
)
541 if (altclass
== ALL_REGS
|| class == ALL_REGS
)
542 fprintf (file
, "; pref %s", reg_class_names
[(int) class]);
543 else if (altclass
== NO_REGS
)
544 fprintf (file
, "; %s or none", reg_class_names
[(int) class]);
546 fprintf (file
, "; pref %s, else %s",
547 reg_class_names
[(int) class],
548 reg_class_names
[(int) altclass
]);
551 if (regno_reg_rtx
[i
] != NULL
&& REG_POINTER (regno_reg_rtx
[i
]))
552 fprintf (file
, "; pointer");
553 fprintf (file
, ".\n");
556 fprintf (file
, "\n%d basic blocks, %d edges.\n", n_basic_blocks
, n_edges
);
563 fprintf (file
, "\nBasic block %d: first insn %d, last %d, ",
564 bb
->index
, INSN_UID (bb
->head
), INSN_UID (bb
->end
));
565 fprintf (file
, "prev %d, next %d, ",
566 bb
->prev_bb
->index
, bb
->next_bb
->index
);
567 fprintf (file
, "loop_depth %d, count ", bb
->loop_depth
);
568 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, bb
->count
);
569 fprintf (file
, ", freq %i", bb
->frequency
);
570 if (maybe_hot_bb_p (bb
))
571 fprintf (file
, ", maybe hot");
572 if (probably_never_executed_bb_p (bb
))
573 fprintf (file
, ", probably never executed");
574 fprintf (file
, ".\n");
576 fprintf (file
, "Predecessors: ");
577 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
578 dump_edge_info (file
, e
, 0);
580 fprintf (file
, "\nSuccessors: ");
581 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
582 dump_edge_info (file
, e
, 1);
584 fprintf (file
, "\nRegisters live at start:");
585 dump_regset (bb
->global_live_at_start
, file
);
587 fprintf (file
, "\nRegisters live at end:");
588 dump_regset (bb
->global_live_at_end
, file
);
592 /* Check the consistency of profile information. We can't do that
593 in verify_flow_info, as the counts may get invalid for incompletely
594 solved graphs, later eliminating of conditionals or roundoff errors.
595 It is still practical to have them reported for debugging of simple
598 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
599 sum
+= e
->probability
;
600 if (bb
->succ
&& abs (sum
- REG_BR_PROB_BASE
) > 100)
601 fprintf (file
, "Invalid sum of outgoing probabilities %.1f%%\n",
602 sum
* 100.0 / REG_BR_PROB_BASE
);
604 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
605 sum
+= EDGE_FREQUENCY (e
);
606 if (abs (sum
- bb
->frequency
) > 100)
608 "Invalid sum of incomming frequencies %i, should be %i\n",
611 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
613 if (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100)
614 fprintf (file
, "Invalid sum of incomming counts %i, should be %i\n",
615 (int)lsum
, (int)bb
->count
);
617 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
619 if (bb
->succ
&& (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100))
620 fprintf (file
, "Invalid sum of incomming counts %i, should be %i\n",
621 (int)lsum
, (int)bb
->count
);
630 dump_flow_info (stderr
);
634 dump_edge_info (file
, e
, do_succ
)
639 basic_block side
= (do_succ
? e
->dest
: e
->src
);
641 if (side
== ENTRY_BLOCK_PTR
)
642 fputs (" ENTRY", file
);
643 else if (side
== EXIT_BLOCK_PTR
)
644 fputs (" EXIT", file
);
646 fprintf (file
, " %d", side
->index
);
649 fprintf (file
, " [%.1f%%] ", e
->probability
* 100.0 / REG_BR_PROB_BASE
);
653 fprintf (file
, " count:");
654 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, e
->count
);
659 static const char * const bitnames
[] = {
660 "fallthru", "ab", "abcall", "eh", "fake", "dfs_back",
661 "can_fallthru", "irreducible", "sibcall"
664 int i
, flags
= e
->flags
;
667 for (i
= 0; flags
; i
++)
668 if (flags
& (1 << i
))
674 if (i
< (int) ARRAY_SIZE (bitnames
))
675 fputs (bitnames
[i
], file
);
677 fprintf (file
, "%d", i
);
685 /* Simple routines to easily allocate AUX fields of basic blocks. */
687 static struct obstack block_aux_obstack
;
688 static void *first_block_aux_obj
= 0;
689 static struct obstack edge_aux_obstack
;
690 static void *first_edge_aux_obj
= 0;
692 /* Allocate a memory block of SIZE as BB->aux. The obstack must
693 be first initialized by alloc_aux_for_blocks. */
696 alloc_aux_for_block (bb
, size
)
700 /* Verify that aux field is clear. */
701 if (bb
->aux
|| !first_block_aux_obj
)
703 bb
->aux
= obstack_alloc (&block_aux_obstack
, size
);
704 memset (bb
->aux
, 0, size
);
707 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
708 alloc_aux_for_block for each basic block. */
711 alloc_aux_for_blocks (size
)
714 static int initialized
;
718 gcc_obstack_init (&block_aux_obstack
);
722 /* Check whether AUX data are still allocated. */
723 else if (first_block_aux_obj
)
725 first_block_aux_obj
= (char *) obstack_alloc (&block_aux_obstack
, 0);
730 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
731 alloc_aux_for_block (bb
, size
);
735 /* Clear AUX pointers of all blocks. */
738 clear_aux_for_blocks ()
742 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
746 /* Free data allocated in block_aux_obstack and clear AUX pointers
750 free_aux_for_blocks ()
752 if (!first_block_aux_obj
)
754 obstack_free (&block_aux_obstack
, first_block_aux_obj
);
755 first_block_aux_obj
= NULL
;
757 clear_aux_for_blocks ();
760 /* Allocate a memory edge of SIZE as BB->aux. The obstack must
761 be first initialized by alloc_aux_for_edges. */
764 alloc_aux_for_edge (e
, size
)
768 /* Verify that aux field is clear. */
769 if (e
->aux
|| !first_edge_aux_obj
)
771 e
->aux
= obstack_alloc (&edge_aux_obstack
, size
);
772 memset (e
->aux
, 0, size
);
775 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
776 alloc_aux_for_edge for each basic edge. */
779 alloc_aux_for_edges (size
)
782 static int initialized
;
786 gcc_obstack_init (&edge_aux_obstack
);
790 /* Check whether AUX data are still allocated. */
791 else if (first_edge_aux_obj
)
794 first_edge_aux_obj
= (char *) obstack_alloc (&edge_aux_obstack
, 0);
799 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
803 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
804 alloc_aux_for_edge (e
, size
);
809 /* Clear AUX pointers of all edges. */
812 clear_aux_for_edges ()
817 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
819 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
824 /* Free data allocated in edge_aux_obstack and clear AUX pointers
828 free_aux_for_edges ()
830 if (!first_edge_aux_obj
)
832 obstack_free (&edge_aux_obstack
, first_edge_aux_obj
);
833 first_edge_aux_obj
= NULL
;
835 clear_aux_for_edges ();
838 /* Verify the CFG consistency.
840 Currently it does following checks edge and basic block list correctness
841 and calls into IL dependent checking then. */
845 size_t *edge_checksum
;
846 int num_bb_notes
, err
= 0;
847 basic_block bb
, last_bb_seen
;
848 basic_block
*last_visited
;
850 last_visited
= (basic_block
*) xcalloc (last_basic_block
+ 2,
851 sizeof (basic_block
));
852 edge_checksum
= (size_t *) xcalloc (last_basic_block
+ 2, sizeof (size_t));
854 /* Check bb chain & numbers. */
855 last_bb_seen
= ENTRY_BLOCK_PTR
;
856 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
->next_bb
, NULL
, next_bb
)
858 if (bb
!= EXIT_BLOCK_PTR
859 && bb
!= BASIC_BLOCK (bb
->index
))
861 error ("bb %d on wrong place", bb
->index
);
865 if (bb
->prev_bb
!= last_bb_seen
)
867 error ("prev_bb of %d should be %d, not %d",
868 bb
->index
, last_bb_seen
->index
, bb
->prev_bb
->index
);
875 /* Now check the basic blocks (boundaries etc.) */
876 FOR_EACH_BB_REVERSE (bb
)
883 error ("verify_flow_info: Wrong count of block %i %i",
884 bb
->index
, (int)bb
->count
);
887 if (bb
->frequency
< 0)
889 error ("verify_flow_info: Wrong frequency of block %i %i",
890 bb
->index
, bb
->frequency
);
893 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
895 if (last_visited
[e
->dest
->index
+ 2] == bb
)
897 error ("verify_flow_info: Duplicate edge %i->%i",
898 e
->src
->index
, e
->dest
->index
);
901 if (e
->probability
< 0 || e
->probability
> REG_BR_PROB_BASE
)
903 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
904 e
->src
->index
, e
->dest
->index
, e
->probability
);
909 error ("verify_flow_info: Wrong count of edge %i->%i %i",
910 e
->src
->index
, e
->dest
->index
, (int)e
->count
);
914 last_visited
[e
->dest
->index
+ 2] = bb
;
916 if (e
->flags
& EDGE_FALLTHRU
)
921 error ("verify_flow_info: Basic block %d succ edge is corrupted",
923 fprintf (stderr
, "Predecessor: ");
924 dump_edge_info (stderr
, e
, 0);
925 fprintf (stderr
, "\nSuccessor: ");
926 dump_edge_info (stderr
, e
, 1);
927 fprintf (stderr
, "\n");
931 edge_checksum
[e
->dest
->index
+ 2] += (size_t) e
;
935 error ("Wrong amount of branch edges after unconditional jump %i", bb
->index
);
939 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
943 error ("basic block %d pred edge is corrupted", bb
->index
);
944 fputs ("Predecessor: ", stderr
);
945 dump_edge_info (stderr
, e
, 0);
946 fputs ("\nSuccessor: ", stderr
);
947 dump_edge_info (stderr
, e
, 1);
948 fputc ('\n', stderr
);
951 edge_checksum
[e
->dest
->index
+ 2] -= (size_t) e
;
955 /* Complete edge checksumming for ENTRY and EXIT. */
959 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
960 edge_checksum
[e
->dest
->index
+ 2] += (size_t) e
;
962 for (e
= EXIT_BLOCK_PTR
->pred
; e
; e
= e
->pred_next
)
963 edge_checksum
[e
->dest
->index
+ 2] -= (size_t) e
;
966 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
967 if (edge_checksum
[bb
->index
+ 2])
969 error ("basic block %i edge lists are corrupted", bb
->index
);
974 last_bb_seen
= ENTRY_BLOCK_PTR
;
978 free (edge_checksum
);
979 err
|= cfg_hooks
->cfgh_verify_flow_info ();
981 internal_error ("verify_flow_info failed");
984 /* Print out one basic block with live information at start and end. */
993 fprintf (outf
, ";; Basic block %d, loop depth %d, count ",
994 bb
->index
, bb
->loop_depth
);
995 fprintf (outf
, HOST_WIDEST_INT_PRINT_DEC
, (HOST_WIDEST_INT
) bb
->count
);
998 cfg_hooks
->dump_bb (bb
, outf
);
1000 fputs (";; Successors: ", outf
);
1001 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
1002 dump_edge_info (outf
, e
, 1);
1010 dump_bb (bb
, stderr
);
1017 basic_block bb
= BASIC_BLOCK (n
);
1018 dump_bb (bb
, stderr
);