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 */
104 NULL
, /* head_tree */
108 NULL
, /* local_set */
109 NULL
, /* cond_local_set */
110 NULL
, /* global_live_at_start */
111 NULL
, /* global_live_at_end */
113 EXIT_BLOCK
, /* index */
121 void debug_flow_info
PARAMS ((void));
122 static void free_edge
PARAMS ((edge
));
124 /* Called once at initialization time. */
129 static int initialized
;
131 first_deleted_edge
= 0;
132 first_deleted_block
= 0;
137 gcc_obstack_init (&flow_obstack
);
138 flow_firstobj
= (char *) obstack_alloc (&flow_obstack
, 0);
143 obstack_free (&flow_obstack
, flow_firstobj
);
144 flow_firstobj
= (char *) obstack_alloc (&flow_obstack
, 0);
148 /* Helper function for remove_edge and clear_edges. Frees edge structure
149 without actually unlinking it from the pred/succ lists. */
156 memset (e
, 0, sizeof *e
);
157 e
->succ_next
= first_deleted_edge
;
158 first_deleted_edge
= e
;
161 /* Free the memory associated with the edge structures. */
169 for (i
= 0; i
< n_basic_blocks
; ++i
)
171 basic_block bb
= BASIC_BLOCK (i
);
176 edge next
= e
->succ_next
;
186 e
= ENTRY_BLOCK_PTR
->succ
;
189 edge next
= e
->succ_next
;
195 EXIT_BLOCK_PTR
->pred
= NULL
;
196 ENTRY_BLOCK_PTR
->succ
= NULL
;
202 /* Allocate memory for basic_block. */
209 if (first_deleted_block
)
211 bb
= first_deleted_block
;
212 first_deleted_block
= (basic_block
) bb
->succ
;
217 bb
= (basic_block
) obstack_alloc (&flow_obstack
, sizeof *bb
);
218 memset (bb
, 0, sizeof *bb
);
223 /* Remove block B from the basic block array and compact behind it. */
226 expunge_block_nocompact (b
)
229 /* Invalidate data to make bughunting easier. */
230 memset (b
, 0, sizeof *b
);
232 b
->succ
= (edge
) first_deleted_block
;
233 first_deleted_block
= (basic_block
) b
;
240 int i
, n
= n_basic_blocks
;
242 for (i
= b
->index
; i
+ 1 < n
; ++i
)
244 basic_block x
= BASIC_BLOCK (i
+ 1);
250 basic_block_info
->num_elements
--;
252 expunge_block_nocompact (b
);
255 /* Create an edge connecting SRC and DST with FLAGS optionally using
256 edge cache CACHE. Return the new edge, NULL if already exist. */
259 cached_make_edge (edge_cache
, src
, dst
, flags
)
261 basic_block src
, dst
;
267 /* Don't bother with edge cache for ENTRY or EXIT, if there aren't that
268 many edges to them, or we didn't allocate memory for it. */
269 use_edge_cache
= (edge_cache
270 && src
!= ENTRY_BLOCK_PTR
&& dst
!= EXIT_BLOCK_PTR
);
272 /* Make sure we don't add duplicate edges. */
273 switch (use_edge_cache
)
276 /* Quick test for non-existence of the edge. */
277 if (! TEST_BIT (edge_cache
[src
->index
], dst
->index
))
280 /* The edge exists; early exit if no work to do. */
286 for (e
= src
->succ
; e
; e
= e
->succ_next
)
295 if (first_deleted_edge
)
297 e
= first_deleted_edge
;
298 first_deleted_edge
= e
->succ_next
;
302 e
= (edge
) obstack_alloc (&flow_obstack
, sizeof *e
);
303 memset (e
, 0, sizeof *e
);
307 e
->succ_next
= src
->succ
;
308 e
->pred_next
= dst
->pred
;
317 SET_BIT (edge_cache
[src
->index
], dst
->index
);
322 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
323 created edge or NULL if already exist. */
326 make_edge (src
, dest
, flags
)
327 basic_block src
, dest
;
330 return cached_make_edge (NULL
, src
, dest
, flags
);
333 /* Create an edge connecting SRC to DEST and set probability by knowing
334 that it is the single edge leaving SRC. */
337 make_single_succ_edge (src
, dest
, flags
)
338 basic_block src
, dest
;
341 edge e
= make_edge (src
, dest
, flags
);
343 e
->probability
= REG_BR_PROB_BASE
;
344 e
->count
= src
->count
;
348 /* This function will remove an edge from the flow graph. */
354 edge last_pred
= NULL
;
355 edge last_succ
= NULL
;
357 basic_block src
, dest
;
361 for (tmp
= src
->succ
; tmp
&& tmp
!= e
; tmp
= tmp
->succ_next
)
367 last_succ
->succ_next
= e
->succ_next
;
369 src
->succ
= e
->succ_next
;
371 for (tmp
= dest
->pred
; tmp
&& tmp
!= e
; tmp
= tmp
->pred_next
)
377 last_pred
->pred_next
= e
->pred_next
;
379 dest
->pred
= e
->pred_next
;
384 /* Redirect an edge's successor from one block to another. */
387 redirect_edge_succ (e
, new_succ
)
389 basic_block new_succ
;
393 /* Disconnect the edge from the old successor block. */
394 for (pe
= &e
->dest
->pred
; *pe
!= e
; pe
= &(*pe
)->pred_next
)
396 *pe
= (*pe
)->pred_next
;
398 /* Reconnect the edge to the new successor block. */
399 e
->pred_next
= new_succ
->pred
;
404 /* Like previous but avoid possible duplicate edge. */
407 redirect_edge_succ_nodup (e
, new_succ
)
409 basic_block new_succ
;
413 /* Check whether the edge is already present. */
414 for (s
= e
->src
->succ
; s
; s
= s
->succ_next
)
415 if (s
->dest
== new_succ
&& s
!= e
)
420 s
->flags
|= e
->flags
;
421 s
->probability
+= e
->probability
;
422 s
->count
+= e
->count
;
427 redirect_edge_succ (e
, new_succ
);
432 /* Redirect an edge's predecessor from one block to another. */
435 redirect_edge_pred (e
, new_pred
)
437 basic_block new_pred
;
441 /* Disconnect the edge from the old predecessor block. */
442 for (pe
= &e
->src
->succ
; *pe
!= e
; pe
= &(*pe
)->succ_next
)
445 *pe
= (*pe
)->succ_next
;
447 /* Reconnect the edge to the new predecessor block. */
448 e
->succ_next
= new_pred
->succ
;
457 ENTRY_BLOCK_PTR
->flags
= 0;
458 EXIT_BLOCK_PTR
->flags
= 0;
459 for (i
= 0; i
< n_basic_blocks
; i
++)
460 BASIC_BLOCK (i
)->flags
= 0;
464 dump_flow_info (file
)
468 static const char * const reg_class_names
[] = REG_CLASS_NAMES
;
470 fprintf (file
, "%d registers.\n", max_regno
);
471 for (i
= FIRST_PSEUDO_REGISTER
; i
< max_regno
; i
++)
474 enum reg_class
class, altclass
;
476 fprintf (file
, "\nRegister %d used %d times across %d insns",
477 i
, REG_N_REFS (i
), REG_LIVE_LENGTH (i
));
478 if (REG_BASIC_BLOCK (i
) >= 0)
479 fprintf (file
, " in block %d", REG_BASIC_BLOCK (i
));
481 fprintf (file
, "; set %d time%s", REG_N_SETS (i
),
482 (REG_N_SETS (i
) == 1) ? "" : "s");
483 if (regno_reg_rtx
[i
] != NULL
&& REG_USERVAR_P (regno_reg_rtx
[i
]))
484 fprintf (file
, "; user var");
485 if (REG_N_DEATHS (i
) != 1)
486 fprintf (file
, "; dies in %d places", REG_N_DEATHS (i
));
487 if (REG_N_CALLS_CROSSED (i
) == 1)
488 fprintf (file
, "; crosses 1 call");
489 else if (REG_N_CALLS_CROSSED (i
))
490 fprintf (file
, "; crosses %d calls", REG_N_CALLS_CROSSED (i
));
491 if (regno_reg_rtx
[i
] != NULL
492 && PSEUDO_REGNO_BYTES (i
) != UNITS_PER_WORD
)
493 fprintf (file
, "; %d bytes", PSEUDO_REGNO_BYTES (i
));
495 class = reg_preferred_class (i
);
496 altclass
= reg_alternate_class (i
);
497 if (class != GENERAL_REGS
|| altclass
!= ALL_REGS
)
499 if (altclass
== ALL_REGS
|| class == ALL_REGS
)
500 fprintf (file
, "; pref %s", reg_class_names
[(int) class]);
501 else if (altclass
== NO_REGS
)
502 fprintf (file
, "; %s or none", reg_class_names
[(int) class]);
504 fprintf (file
, "; pref %s, else %s",
505 reg_class_names
[(int) class],
506 reg_class_names
[(int) altclass
]);
509 if (regno_reg_rtx
[i
] != NULL
&& REG_POINTER (regno_reg_rtx
[i
]))
510 fprintf (file
, "; pointer");
511 fprintf (file
, ".\n");
514 fprintf (file
, "\n%d basic blocks, %d edges.\n", n_basic_blocks
, n_edges
);
515 for (i
= 0; i
< n_basic_blocks
; i
++)
517 basic_block bb
= BASIC_BLOCK (i
);
522 fprintf (file
, "\nBasic block %d: first insn %d, last %d, ",
523 i
, INSN_UID (bb
->head
), INSN_UID (bb
->end
));
524 fprintf (file
, "loop_depth %d, count ", bb
->loop_depth
);
525 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, bb
->count
);
526 fprintf (file
, ", freq %i.\n", bb
->frequency
);
528 fprintf (file
, "Predecessors: ");
529 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
530 dump_edge_info (file
, e
, 0);
532 fprintf (file
, "\nSuccessors: ");
533 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
534 dump_edge_info (file
, e
, 1);
536 fprintf (file
, "\nRegisters live at start:");
537 dump_regset (bb
->global_live_at_start
, file
);
539 fprintf (file
, "\nRegisters live at end:");
540 dump_regset (bb
->global_live_at_end
, file
);
544 /* Check the consistency of profile information. We can't do that
545 in verify_flow_info, as the counts may get invalid for incompletely
546 solved graphs, later elliminating of conditionals or roundoff errors.
547 It is still practical to have them reported for debugging of simple
550 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
551 sum
+= e
->probability
;
552 if (bb
->succ
&& abs (sum
- REG_BR_PROB_BASE
) > 100)
553 fprintf (file
, "Invalid sum of outgoing probabilities %.1f%%\n",
554 sum
* 100.0 / REG_BR_PROB_BASE
);
556 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
557 sum
+= EDGE_FREQUENCY (e
);
558 if (abs (sum
- bb
->frequency
) > 100)
560 "Invalid sum of incomming frequencies %i, should be %i\n",
563 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
565 if (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100)
566 fprintf (file
, "Invalid sum of incomming counts %i, should be %i\n",
567 (int)lsum
, (int)bb
->count
);
569 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
571 if (bb
->succ
&& (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100))
572 fprintf (file
, "Invalid sum of incomming counts %i, should be %i\n",
573 (int)lsum
, (int)bb
->count
);
582 dump_flow_info (stderr
);
586 dump_edge_info (file
, e
, do_succ
)
591 basic_block side
= (do_succ
? e
->dest
: e
->src
);
593 if (side
== ENTRY_BLOCK_PTR
)
594 fputs (" ENTRY", file
);
595 else if (side
== EXIT_BLOCK_PTR
)
596 fputs (" EXIT", file
);
598 fprintf (file
, " %d", side
->index
);
601 fprintf (file
, " [%.1f%%] ", e
->probability
* 100.0 / REG_BR_PROB_BASE
);
605 fprintf (file
, " count:");
606 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, e
->count
);
611 static const char * const bitnames
[]
612 = {"fallthru", "ab", "abcall", "eh", "fake", "dfs_back"};
614 int i
, flags
= e
->flags
;
617 for (i
= 0; flags
; i
++)
618 if (flags
& (1 << i
))
624 if (i
< (int) ARRAY_SIZE (bitnames
))
625 fputs (bitnames
[i
], file
);
627 fprintf (file
, "%d", i
);
635 /* Simple routines to easily allocate AUX fields of basic blocks. */
637 static struct obstack block_aux_obstack
;
638 static void *first_block_aux_obj
= 0;
639 static struct obstack edge_aux_obstack
;
640 static void *first_edge_aux_obj
= 0;
642 /* Allocate an memory block of SIZE as BB->aux. The obstack must
643 be first initialized by alloc_aux_for_blocks. */
646 alloc_aux_for_block (bb
, size
)
650 /* Verify that aux field is clear. */
651 if (bb
->aux
|| !first_block_aux_obj
)
653 bb
->aux
= obstack_alloc (&block_aux_obstack
, size
);
654 memset (bb
->aux
, 0, size
);
657 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
658 alloc_aux_for_block for each basic block. */
661 alloc_aux_for_blocks (size
)
664 static int initialized
;
668 gcc_obstack_init (&block_aux_obstack
);
672 /* Check whether AUX data are still allocated. */
673 else if (first_block_aux_obj
)
675 first_block_aux_obj
= (char *) obstack_alloc (&block_aux_obstack
, 0);
680 for (i
= 0; i
< n_basic_blocks
; i
++)
681 alloc_aux_for_block (BASIC_BLOCK (i
), size
);
683 alloc_aux_for_block (ENTRY_BLOCK_PTR
, size
);
684 alloc_aux_for_block (EXIT_BLOCK_PTR
, size
);
688 /* Clear AUX pointers of all blocks. */
691 clear_aux_for_blocks ()
695 for (i
= 0; i
< n_basic_blocks
; i
++)
696 BASIC_BLOCK (i
)->aux
= NULL
;
698 ENTRY_BLOCK_PTR
->aux
= NULL
;
699 EXIT_BLOCK_PTR
->aux
= NULL
;
702 /* Free data allocated in block_aux_obstack and clear AUX pointers
706 free_aux_for_blocks ()
708 if (!first_block_aux_obj
)
710 obstack_free (&block_aux_obstack
, first_block_aux_obj
);
711 first_block_aux_obj
= NULL
;
713 clear_aux_for_blocks ();
716 /* Allocate an memory edge of SIZE as BB->aux. The obstack must
717 be first initialized by alloc_aux_for_edges. */
720 alloc_aux_for_edge (e
, size
)
724 /* Verify that aux field is clear. */
725 if (e
->aux
|| !first_edge_aux_obj
)
727 e
->aux
= obstack_alloc (&edge_aux_obstack
, size
);
728 memset (e
->aux
, 0, size
);
731 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
732 alloc_aux_for_edge for each basic edge. */
735 alloc_aux_for_edges (size
)
738 static int initialized
;
742 gcc_obstack_init (&edge_aux_obstack
);
746 /* Check whether AUX data are still allocated. */
747 else if (first_edge_aux_obj
)
750 first_edge_aux_obj
= (char *) obstack_alloc (&edge_aux_obstack
, 0);
754 for (i
= -1; i
< n_basic_blocks
; i
++)
760 bb
= BASIC_BLOCK (i
);
762 bb
= ENTRY_BLOCK_PTR
;
764 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
765 alloc_aux_for_edge (e
, size
);
770 /* Clear AUX pointers of all edges. */
773 clear_aux_for_edges ()
777 for (i
= -1; i
< n_basic_blocks
; i
++)
783 bb
= BASIC_BLOCK (i
);
785 bb
= ENTRY_BLOCK_PTR
;
787 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
792 /* Free data allocated in edge_aux_obstack and clear AUX pointers
796 free_aux_for_edges ()
798 if (!first_edge_aux_obj
)
800 obstack_free (&edge_aux_obstack
, first_edge_aux_obj
);
801 first_edge_aux_obj
= NULL
;
803 clear_aux_for_edges ();