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
48 #include "hard-reg-set.h"
49 #include "basic-block.h"
59 /* The obstack on which the flow graph components are allocated. */
61 struct obstack flow_obstack;
62 static char *flow_firstobj;
64 /* Number of basic blocks in the current function. */
68 /* Number of edges in the current function. */
72 /* First edge in the deleted edges chain. */
74 edge first_deleted_edge;
75 static basic_block first_deleted_block;
77 /* The basic block array. */
79 varray_type basic_block_info;
81 /* The special entry and exit blocks. */
83 struct basic_block_def entry_exit_blocks[2]
91 NULL, /* cond_local_set */
92 NULL, /* global_live_at_start */
93 NULL, /* global_live_at_end */
95 ENTRY_BLOCK, /* index */
97 EXIT_BLOCK_PTR, /* next_bb */
106 NULL, /* head_tree */
110 NULL, /* local_set */
111 NULL, /* cond_local_set */
112 NULL, /* global_live_at_start */
113 NULL, /* global_live_at_end */
115 EXIT_BLOCK, /* index */
116 ENTRY_BLOCK_PTR, /* prev_bb */
125 void debug_flow_info PARAMS ((void));
126 static void free_edge PARAMS ((edge));
128 /* Called once at initialization time. */
133 static int initialized;
135 first_deleted_edge = 0;
136 first_deleted_block = 0;
141 gcc_obstack_init (&flow_obstack);
142 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
147 obstack_free (&flow_obstack, flow_firstobj);
148 flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
152 /* Helper function for remove_edge and clear_edges. Frees edge structure
153 without actually unlinking it from the pred/succ lists. */
160 memset (e, 0, sizeof *e);
161 e->succ_next = first_deleted_edge;
162 first_deleted_edge = e;
165 /* Free the memory associated with the edge structures. */
173 for (i = 0; i < n_basic_blocks; ++i)
175 basic_block bb = BASIC_BLOCK (i);
180 edge next = e->succ_next;
190 e = ENTRY_BLOCK_PTR->succ;
193 edge next = e->succ_next;
199 EXIT_BLOCK_PTR->pred = NULL;
200 ENTRY_BLOCK_PTR->succ = NULL;
206 /* Allocate memory for basic_block. */
213 if (first_deleted_block)
215 bb = first_deleted_block;
216 first_deleted_block = (basic_block) bb->succ;
221 bb = (basic_block) obstack_alloc (&flow_obstack, sizeof *bb);
222 memset (bb, 0, sizeof *bb);
227 /* Link block B to chain after AFTER. */
229 link_block (b, after)
230 basic_block b, after;
232 b->next_bb = after->next_bb;
235 b->next_bb->prev_bb = b;
238 /* Unlink block B from chain. */
243 b->next_bb->prev_bb = b->prev_bb;
244 b->prev_bb->next_bb = b->next_bb;
248 /* Remove block B from the basic block array and compact behind it. */
251 expunge_block_nocompact (b)
256 /* Invalidate data to make bughunting easier. */
257 memset (b, 0, sizeof *b);
259 b->succ = (edge) first_deleted_block;
260 first_deleted_block = (basic_block) b;
267 int i, n = n_basic_blocks;
269 for (i = b->index; i + 1 < n; ++i)
271 basic_block x = BASIC_BLOCK (i + 1);
277 basic_block_info->num_elements--;
279 expunge_block_nocompact (b);
282 /* Create an edge connecting SRC and DST with FLAGS optionally using
283 edge cache CACHE. Return the new edge, NULL if already exist. */
286 cached_make_edge (edge_cache, src, dst, flags)
288 basic_block src, dst;
294 /* Don't bother with edge cache for ENTRY or EXIT, if there aren't that
295 many edges to them, or we didn't allocate memory for it. */
296 use_edge_cache = (edge_cache
297 && src != ENTRY_BLOCK_PTR && dst != EXIT_BLOCK_PTR);
299 /* Make sure we don't add duplicate edges. */
300 switch (use_edge_cache)
303 /* Quick test for non-existence of the edge. */
304 if (! TEST_BIT (edge_cache[src->index], dst->index))
307 /* The edge exists; early exit if no work to do. */
313 for (e = src->succ; e; e = e->succ_next)
322 if (first_deleted_edge)
324 e = first_deleted_edge;
325 first_deleted_edge = e->succ_next;
329 e = (edge) obstack_alloc (&flow_obstack, sizeof *e);
330 memset (e, 0, sizeof *e);
334 e->succ_next = src->succ;
335 e->pred_next = dst->pred;
344 SET_BIT (edge_cache[src->index], dst->index);
349 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
350 created edge or NULL if already exist. */
353 make_edge (src, dest, flags)
354 basic_block src, dest;
357 return cached_make_edge (NULL, src, dest, flags);
360 /* Create an edge connecting SRC to DEST and set probability by knowing
361 that it is the single edge leaving SRC. */
364 make_single_succ_edge (src, dest, flags)
365 basic_block src, dest;
368 edge e = make_edge (src, dest, flags);
370 e->probability = REG_BR_PROB_BASE;
371 e->count = src->count;
375 /* This function will remove an edge from the flow graph. */
381 edge last_pred = NULL;
382 edge last_succ = NULL;
384 basic_block src, dest;
388 for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
394 last_succ->succ_next = e->succ_next;
396 src->succ = e->succ_next;
398 for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
404 last_pred->pred_next = e->pred_next;
406 dest->pred = e->pred_next;
411 /* Redirect an edge's successor from one block to another. */
414 redirect_edge_succ (e, new_succ)
416 basic_block new_succ;
420 /* Disconnect the edge from the old successor block. */
421 for (pe = &e->dest->pred; *pe != e; pe = &(*pe)->pred_next)
423 *pe = (*pe)->pred_next;
425 /* Reconnect the edge to the new successor block. */
426 e->pred_next = new_succ->pred;
431 /* Like previous but avoid possible duplicate edge. */
434 redirect_edge_succ_nodup (e, new_succ)
436 basic_block new_succ;
440 /* Check whether the edge is already present. */
441 for (s = e->src->succ; s; s = s->succ_next)
442 if (s->dest == new_succ && s != e)
447 s->flags |= e->flags;
448 s->probability += e->probability;
449 s->count += e->count;
454 redirect_edge_succ (e, new_succ);
459 /* Redirect an edge's predecessor from one block to another. */
462 redirect_edge_pred (e, new_pred)
464 basic_block new_pred;
468 /* Disconnect the edge from the old predecessor block. */
469 for (pe = &e->src->succ; *pe != e; pe = &(*pe)->succ_next)
472 *pe = (*pe)->succ_next;
474 /* Reconnect the edge to the new predecessor block. */
475 e->succ_next = new_pred->succ;
484 ENTRY_BLOCK_PTR->flags = 0;
485 EXIT_BLOCK_PTR->flags = 0;
486 for (i = 0; i < n_basic_blocks; i++)
487 BASIC_BLOCK (i)->flags = 0;
491 dump_flow_info (file)
495 static const char * const reg_class_names[] = REG_CLASS_NAMES;
497 fprintf (file, "%d registers.\n", max_regno);
498 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
501 enum reg_class class, altclass;
503 fprintf (file, "\nRegister %d used %d times across %d insns",
504 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
505 if (REG_BASIC_BLOCK (i) >= 0)
506 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
508 fprintf (file, "; set %d time%s", REG_N_SETS (i),
509 (REG_N_SETS (i) == 1) ? "" : "s");
510 if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
511 fprintf (file, "; user var");
512 if (REG_N_DEATHS (i) != 1)
513 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
514 if (REG_N_CALLS_CROSSED (i) == 1)
515 fprintf (file, "; crosses 1 call");
516 else if (REG_N_CALLS_CROSSED (i))
517 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
518 if (regno_reg_rtx[i] != NULL
519 && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
520 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
522 class = reg_preferred_class (i);
523 altclass = reg_alternate_class (i);
524 if (class != GENERAL_REGS || altclass != ALL_REGS)
526 if (altclass == ALL_REGS || class == ALL_REGS)
527 fprintf (file, "; pref %s", reg_class_names[(int) class]);
528 else if (altclass == NO_REGS)
529 fprintf (file, "; %s or none", reg_class_names[(int) class]);
531 fprintf (file, "; pref %s, else %s",
532 reg_class_names[(int) class],
533 reg_class_names[(int) altclass]);
536 if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
537 fprintf (file, "; pointer");
538 fprintf (file, ".\n");
541 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
542 for (i = 0; i < n_basic_blocks; i++)
544 basic_block bb = BASIC_BLOCK (i);
549 fprintf (file, "\nBasic block %d: first insn %d, last %d, ",
550 i, INSN_UID (bb->head), INSN_UID (bb->end));
551 fprintf (file, "prev %d, next %d, ",
552 bb->prev_bb->index, bb->next_bb->index);
553 fprintf (file, "loop_depth %d, count ", bb->loop_depth);
554 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
555 fprintf (file, ", freq %i", bb->frequency);
556 if (maybe_hot_bb_p (bb))
557 fprintf (file, ", maybe hot");
558 if (probably_never_executed_bb_p (bb))
559 fprintf (file, ", probably never executed");
560 fprintf (file, ".\n", bb->frequency);
562 fprintf (file, "Predecessors: ");
563 for (e = bb->pred; e; e = e->pred_next)
564 dump_edge_info (file, e, 0);
566 fprintf (file, "\nSuccessors: ");
567 for (e = bb->succ; e; e = e->succ_next)
568 dump_edge_info (file, e, 1);
570 fprintf (file, "\nRegisters live at start:");
571 dump_regset (bb->global_live_at_start, file);
573 fprintf (file, "\nRegisters live at end:");
574 dump_regset (bb->global_live_at_end, file);
578 /* Check the consistency of profile information. We can't do that
579 in verify_flow_info, as the counts may get invalid for incompletely
580 solved graphs, later elliminating of conditionals or roundoff errors.
581 It is still practical to have them reported for debugging of simple
584 for (e = bb->succ; e; e = e->succ_next)
585 sum += e->probability;
586 if (bb->succ && abs (sum - REG_BR_PROB_BASE) > 100)
587 fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
588 sum * 100.0 / REG_BR_PROB_BASE);
590 for (e = bb->pred; e; e = e->pred_next)
591 sum += EDGE_FREQUENCY (e);
592 if (abs (sum - bb->frequency) > 100)
594 "Invalid sum of incomming frequencies %i, should be %i\n",
597 for (e = bb->pred; e; e = e->pred_next)
599 if (lsum - bb->count > 100 || lsum - bb->count < -100)
600 fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
601 (int)lsum, (int)bb->count);
603 for (e = bb->succ; e; e = e->succ_next)
605 if (bb->succ && (lsum - bb->count > 100 || lsum - bb->count < -100))
606 fprintf (file, "Invalid sum of incomming counts %i, should be %i\n",
607 (int)lsum, (int)bb->count);
616 dump_flow_info (stderr);
620 dump_edge_info (file, e, do_succ)
625 basic_block side = (do_succ ? e->dest : e->src);
627 if (side == ENTRY_BLOCK_PTR)
628 fputs (" ENTRY", file);
629 else if (side == EXIT_BLOCK_PTR)
630 fputs (" EXIT", file);
632 fprintf (file, " %d", side->index);
635 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
639 fprintf (file, " count:");
640 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
645 static const char * const bitnames[]
646 = {"fallthru", "ab", "abcall", "eh", "fake", "dfs_back", "can_fallthru"};
648 int i, flags = e->flags;
651 for (i = 0; flags; i++)
652 if (flags & (1 << i))
658 if (i < (int) ARRAY_SIZE (bitnames))
659 fputs (bitnames[i], file);
661 fprintf (file, "%d", i);
669 /* Simple routines to easily allocate AUX fields of basic blocks. */
671 static struct obstack block_aux_obstack;
672 static void *first_block_aux_obj = 0;
673 static struct obstack edge_aux_obstack;
674 static void *first_edge_aux_obj = 0;
676 /* Allocate an memory block of SIZE as BB->aux. The obstack must
677 be first initialized by alloc_aux_for_blocks. */
680 alloc_aux_for_block (bb, size)
684 /* Verify that aux field is clear. */
685 if (bb->aux || !first_block_aux_obj)
687 bb->aux = obstack_alloc (&block_aux_obstack, size);
688 memset (bb->aux, 0, size);
691 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
692 alloc_aux_for_block for each basic block. */
695 alloc_aux_for_blocks (size)
698 static int initialized;
702 gcc_obstack_init (&block_aux_obstack);
706 /* Check whether AUX data are still allocated. */
707 else if (first_block_aux_obj)
709 first_block_aux_obj = (char *) obstack_alloc (&block_aux_obstack, 0);
714 for (i = 0; i < n_basic_blocks; i++)
715 alloc_aux_for_block (BASIC_BLOCK (i), size);
717 alloc_aux_for_block (ENTRY_BLOCK_PTR, size);
718 alloc_aux_for_block (EXIT_BLOCK_PTR, size);
722 /* Clear AUX pointers of all blocks. */
725 clear_aux_for_blocks ()
729 for (i = 0; i < n_basic_blocks; i++)
730 BASIC_BLOCK (i)->aux = NULL;
732 ENTRY_BLOCK_PTR->aux = NULL;
733 EXIT_BLOCK_PTR->aux = NULL;
736 /* Free data allocated in block_aux_obstack and clear AUX pointers
740 free_aux_for_blocks ()
742 if (!first_block_aux_obj)
744 obstack_free (&block_aux_obstack, first_block_aux_obj);
745 first_block_aux_obj = NULL;
747 clear_aux_for_blocks ();
750 /* Allocate an memory edge of SIZE as BB->aux. The obstack must
751 be first initialized by alloc_aux_for_edges. */
754 alloc_aux_for_edge (e, size)
758 /* Verify that aux field is clear. */
759 if (e->aux || !first_edge_aux_obj)
761 e->aux = obstack_alloc (&edge_aux_obstack, size);
762 memset (e->aux, 0, size);
765 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
766 alloc_aux_for_edge for each basic edge. */
769 alloc_aux_for_edges (size)
772 static int initialized;
776 gcc_obstack_init (&edge_aux_obstack);
780 /* Check whether AUX data are still allocated. */
781 else if (first_edge_aux_obj)
784 first_edge_aux_obj = (char *) obstack_alloc (&edge_aux_obstack, 0);
788 for (i = -1; i < n_basic_blocks; i++)
794 bb = BASIC_BLOCK (i);
796 bb = ENTRY_BLOCK_PTR;
798 for (e = bb->succ; e; e = e->succ_next)
799 alloc_aux_for_edge (e, size);
804 /* Clear AUX pointers of all edges. */
807 clear_aux_for_edges ()
811 for (i = -1; i < n_basic_blocks; i++)
817 bb = BASIC_BLOCK (i);
819 bb = ENTRY_BLOCK_PTR;
821 for (e = bb->succ; e; e = e->succ_next)
826 /* Free data allocated in edge_aux_obstack and clear AUX pointers
830 free_aux_for_edges ()
832 if (!first_edge_aux_obj)
834 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
835 first_edge_aux_obj = NULL;
837 clear_aux_for_edges ();