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, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains low level functions to manipulate the CFG and
23 analyze it. All other modules should not transform the data structure
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"
60 #include "diagnostic-core.h"
65 #include "tree-pass.h"
68 #include "alloc-pool.h"
71 #include "tree-flow.h"
73 /* The obstack on which the flow graph components are allocated. */
75 struct bitmap_obstack reg_obstack
;
77 void debug_flow_info (void);
78 static void free_edge (edge
);
80 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
82 /* Called once at initialization time. */
85 init_flow (struct function
*the_fun
)
88 the_fun
->cfg
= ggc_alloc_cleared_control_flow_graph ();
89 n_edges_for_function (the_fun
) = 0;
90 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
)
91 = ggc_alloc_cleared_basic_block_def ();
92 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
)->index
= ENTRY_BLOCK
;
93 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
)
94 = ggc_alloc_cleared_basic_block_def ();
95 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
)->index
= EXIT_BLOCK
;
96 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
)->next_bb
97 = EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
);
98 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
)->prev_bb
99 = ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
);
102 /* Helper function for remove_edge and clear_edges. Frees edge structure
103 without actually unlinking it from the pred/succ lists. */
106 free_edge (edge e ATTRIBUTE_UNUSED
)
112 /* Free the memory associated with the edge structures. */
123 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
125 VEC_truncate (edge
, bb
->succs
, 0);
126 VEC_truncate (edge
, bb
->preds
, 0);
129 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
131 VEC_truncate (edge
, EXIT_BLOCK_PTR
->preds
, 0);
132 VEC_truncate (edge
, ENTRY_BLOCK_PTR
->succs
, 0);
134 gcc_assert (!n_edges
);
137 /* Allocate memory for basic_block. */
143 bb
= ggc_alloc_cleared_basic_block_def ();
147 /* Link block B to chain after AFTER. */
149 link_block (basic_block b
, basic_block after
)
151 b
->next_bb
= after
->next_bb
;
154 b
->next_bb
->prev_bb
= b
;
157 /* Unlink block B from chain. */
159 unlink_block (basic_block b
)
161 b
->next_bb
->prev_bb
= b
->prev_bb
;
162 b
->prev_bb
->next_bb
= b
->next_bb
;
167 /* Sequentially order blocks and compact the arrays. */
169 compact_blocks (void)
173 SET_BASIC_BLOCK (ENTRY_BLOCK
, ENTRY_BLOCK_PTR
);
174 SET_BASIC_BLOCK (EXIT_BLOCK
, EXIT_BLOCK_PTR
);
177 df_compact_blocks ();
182 i
= NUM_FIXED_BLOCKS
;
185 SET_BASIC_BLOCK (i
, bb
);
189 gcc_assert (i
== n_basic_blocks
);
191 for (; i
< last_basic_block
; i
++)
192 SET_BASIC_BLOCK (i
, NULL
);
194 last_basic_block
= n_basic_blocks
;
197 /* Remove block B from the basic block array. */
200 expunge_block (basic_block b
)
203 SET_BASIC_BLOCK (b
->index
, NULL
);
205 /* We should be able to ggc_free here, but we are not.
206 The dead SSA_NAMES are left pointing to dead statements that are pointing
207 to dead basic blocks making garbage collector to die.
208 We should be able to release all dead SSA_NAMES and at the same time we should
209 clear out BB pointer of dead statements consistently. */
212 /* Connect E to E->src. */
217 VEC_safe_push (edge
, gc
, e
->src
->succs
, e
);
218 df_mark_solutions_dirty ();
221 /* Connect E to E->dest. */
224 connect_dest (edge e
)
226 basic_block dest
= e
->dest
;
227 VEC_safe_push (edge
, gc
, dest
->preds
, e
);
228 e
->dest_idx
= EDGE_COUNT (dest
->preds
) - 1;
229 df_mark_solutions_dirty ();
232 /* Disconnect edge E from E->src. */
235 disconnect_src (edge e
)
237 basic_block src
= e
->src
;
241 for (ei
= ei_start (src
->succs
); (tmp
= ei_safe_edge (ei
)); )
245 VEC_unordered_remove (edge
, src
->succs
, ei
.index
);
252 df_mark_solutions_dirty ();
256 /* Disconnect edge E from E->dest. */
259 disconnect_dest (edge e
)
261 basic_block dest
= e
->dest
;
262 unsigned int dest_idx
= e
->dest_idx
;
264 VEC_unordered_remove (edge
, dest
->preds
, dest_idx
);
266 /* If we removed an edge in the middle of the edge vector, we need
267 to update dest_idx of the edge that moved into the "hole". */
268 if (dest_idx
< EDGE_COUNT (dest
->preds
))
269 EDGE_PRED (dest
, dest_idx
)->dest_idx
= dest_idx
;
270 df_mark_solutions_dirty ();
273 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
274 created edge. Use this only if you are sure that this edge can't
275 possibly already exist. */
278 unchecked_make_edge (basic_block src
, basic_block dst
, int flags
)
281 e
= ggc_alloc_cleared_edge_def ();
291 execute_on_growing_pred (e
);
295 /* Create an edge connecting SRC and DST with FLAGS optionally using
296 edge cache CACHE. Return the new edge, NULL if already exist. */
299 cached_make_edge (sbitmap edge_cache
, basic_block src
, basic_block dst
, int flags
)
301 if (edge_cache
== NULL
302 || src
== ENTRY_BLOCK_PTR
303 || dst
== EXIT_BLOCK_PTR
)
304 return make_edge (src
, dst
, flags
);
306 /* Does the requested edge already exist? */
307 if (! TEST_BIT (edge_cache
, dst
->index
))
309 /* The edge does not exist. Create one and update the
311 SET_BIT (edge_cache
, dst
->index
);
312 return unchecked_make_edge (src
, dst
, flags
);
315 /* At this point, we know that the requested edge exists. Adjust
316 flags if necessary. */
319 edge e
= find_edge (src
, dst
);
326 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
327 created edge or NULL if already exist. */
330 make_edge (basic_block src
, basic_block dest
, int flags
)
332 edge e
= find_edge (src
, dest
);
334 /* Make sure we don't add duplicate edges. */
341 return unchecked_make_edge (src
, dest
, flags
);
344 /* Create an edge connecting SRC to DEST and set probability by knowing
345 that it is the single edge leaving SRC. */
348 make_single_succ_edge (basic_block src
, basic_block dest
, int flags
)
350 edge e
= make_edge (src
, dest
, flags
);
352 e
->probability
= REG_BR_PROB_BASE
;
353 e
->count
= src
->count
;
357 /* This function will remove an edge from the flow graph. */
360 remove_edge_raw (edge e
)
362 remove_predictions_associated_with_edge (e
);
363 execute_on_shrinking_pred (e
);
368 /* This is probably not needed, but it doesn't hurt. */
369 redirect_edge_var_map_clear (e
);
374 /* Redirect an edge's successor from one block to another. */
377 redirect_edge_succ (edge e
, basic_block new_succ
)
379 execute_on_shrinking_pred (e
);
385 /* Reconnect the edge to the new successor block. */
388 execute_on_growing_pred (e
);
391 /* Like previous but avoid possible duplicate edge. */
394 redirect_edge_succ_nodup (edge e
, basic_block new_succ
)
398 s
= find_edge (e
->src
, new_succ
);
401 s
->flags
|= e
->flags
;
402 s
->probability
+= e
->probability
;
403 if (s
->probability
> REG_BR_PROB_BASE
)
404 s
->probability
= REG_BR_PROB_BASE
;
405 s
->count
+= e
->count
;
407 redirect_edge_var_map_dup (s
, e
);
411 redirect_edge_succ (e
, new_succ
);
416 /* Redirect an edge's predecessor from one block to another. */
419 redirect_edge_pred (edge e
, basic_block new_pred
)
425 /* Reconnect the edge to the new predecessor block. */
429 /* Clear all basic block flags, with the exception of partitioning and
432 clear_bb_flags (void)
436 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
437 bb
->flags
= (BB_PARTITION (bb
)
438 | (bb
->flags
& (BB_DISABLE_SCHEDULE
+ BB_RTL
+ BB_NON_LOCAL_GOTO_TARGET
)));
441 /* Check the consistency of profile information. We can't do that
442 in verify_flow_info, as the counts may get invalid for incompletely
443 solved graphs, later eliminating of conditionals or roundoff errors.
444 It is still practical to have them reported for debugging of simple
447 check_bb_profile (basic_block bb
, FILE * file
)
454 if (profile_status
== PROFILE_ABSENT
)
457 if (bb
!= EXIT_BLOCK_PTR
)
459 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
460 sum
+= e
->probability
;
461 if (EDGE_COUNT (bb
->succs
) && abs (sum
- REG_BR_PROB_BASE
) > 100)
462 fprintf (file
, "Invalid sum of outgoing probabilities %.1f%%\n",
463 sum
* 100.0 / REG_BR_PROB_BASE
);
465 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
467 if (EDGE_COUNT (bb
->succs
)
468 && (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100))
469 fprintf (file
, "Invalid sum of outgoing counts %i, should be %i\n",
470 (int) lsum
, (int) bb
->count
);
472 if (bb
!= ENTRY_BLOCK_PTR
)
475 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
476 sum
+= EDGE_FREQUENCY (e
);
477 if (abs (sum
- bb
->frequency
) > 100)
479 "Invalid sum of incoming frequencies %i, should be %i\n",
482 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
484 if (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100)
485 fprintf (file
, "Invalid sum of incoming counts %i, should be %i\n",
486 (int) lsum
, (int) bb
->count
);
490 /* Write information about registers and basic blocks into FILE.
491 This is part of making a debugging dump. */
494 dump_regset (regset r
, FILE *outf
)
497 reg_set_iterator rsi
;
501 fputs (" (nil)", outf
);
505 EXECUTE_IF_SET_IN_REG_SET (r
, 0, i
, rsi
)
507 fprintf (outf
, " %d", i
);
508 if (i
< FIRST_PSEUDO_REGISTER
)
509 fprintf (outf
, " [%s]",
514 /* Print a human-readable representation of R on the standard error
515 stream. This function is designed to be used from within the
519 debug_regset (regset r
)
521 dump_regset (r
, stderr
);
525 /* Emit basic block information for BB. HEADER is true if the user wants
526 the generic information and the predecessors, FOOTER is true if they want
527 the successors. FLAGS is the dump flags of interest; TDF_DETAILS emit
528 global register liveness information. PREFIX is put in front of every
529 line. The output is emitted to FILE. */
531 dump_bb_info (basic_block bb
, bool header
, bool footer
, int flags
,
532 const char *prefix
, FILE *file
)
539 fprintf (file
, "\n%sBasic block %d ", prefix
, bb
->index
);
541 fprintf (file
, ", prev %d", bb
->prev_bb
->index
);
543 fprintf (file
, ", next %d", bb
->next_bb
->index
);
544 fprintf (file
, ", loop_depth %d, count ", bb
->loop_depth
);
545 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, bb
->count
);
546 fprintf (file
, ", freq %i", bb
->frequency
);
547 /* Both maybe_hot_bb_p & probably_never_executed_bb_p functions
548 crash without cfun. */
549 if (cfun
&& maybe_hot_bb_p (bb
))
550 fputs (", maybe hot", file
);
551 if (cfun
&& probably_never_executed_bb_p (bb
))
552 fputs (", probably never executed", file
);
555 fprintf (file
, "%sPredecessors: ", prefix
);
556 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
557 dump_edge_info (file
, e
, 0);
559 if ((flags
& TDF_DETAILS
)
560 && (bb
->flags
& BB_RTL
)
564 df_dump_top (bb
, file
);
570 fprintf (file
, "\n%sSuccessors: ", prefix
);
571 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
572 dump_edge_info (file
, e
, 1);
574 if ((flags
& TDF_DETAILS
)
575 && (bb
->flags
& BB_RTL
)
579 df_dump_bottom (bb
, file
);
586 /* Dump the register info to FILE. */
589 dump_reg_info (FILE *file
)
591 unsigned int i
, max
= max_reg_num ();
592 if (reload_completed
)
595 if (reg_info_p_size
< max
)
596 max
= reg_info_p_size
;
598 fprintf (file
, "%d registers.\n", max
);
599 for (i
= FIRST_PSEUDO_REGISTER
; i
< max
; i
++)
601 enum reg_class rclass
, altclass
;
603 if (regstat_n_sets_and_refs
)
604 fprintf (file
, "\nRegister %d used %d times across %d insns",
605 i
, REG_N_REFS (i
), REG_LIVE_LENGTH (i
));
607 fprintf (file
, "\nRegister %d used %d times across %d insns",
608 i
, DF_REG_USE_COUNT (i
) + DF_REG_DEF_COUNT (i
), REG_LIVE_LENGTH (i
));
610 if (REG_BASIC_BLOCK (i
) >= NUM_FIXED_BLOCKS
)
611 fprintf (file
, " in block %d", REG_BASIC_BLOCK (i
));
612 if (regstat_n_sets_and_refs
)
613 fprintf (file
, "; set %d time%s", REG_N_SETS (i
),
614 (REG_N_SETS (i
) == 1) ? "" : "s");
616 fprintf (file
, "; set %d time%s", DF_REG_DEF_COUNT (i
),
617 (DF_REG_DEF_COUNT (i
) == 1) ? "" : "s");
618 if (regno_reg_rtx
[i
] != NULL
&& REG_USERVAR_P (regno_reg_rtx
[i
]))
619 fputs ("; user var", file
);
620 if (REG_N_DEATHS (i
) != 1)
621 fprintf (file
, "; dies in %d places", REG_N_DEATHS (i
));
622 if (REG_N_CALLS_CROSSED (i
) == 1)
623 fputs ("; crosses 1 call", file
);
624 else if (REG_N_CALLS_CROSSED (i
))
625 fprintf (file
, "; crosses %d calls", REG_N_CALLS_CROSSED (i
));
626 if (REG_FREQ_CALLS_CROSSED (i
))
627 fprintf (file
, "; crosses call with %d frequency", REG_FREQ_CALLS_CROSSED (i
));
628 if (regno_reg_rtx
[i
] != NULL
629 && PSEUDO_REGNO_BYTES (i
) != UNITS_PER_WORD
)
630 fprintf (file
, "; %d bytes", PSEUDO_REGNO_BYTES (i
));
632 rclass
= reg_preferred_class (i
);
633 altclass
= reg_alternate_class (i
);
634 if (rclass
!= GENERAL_REGS
|| altclass
!= ALL_REGS
)
636 if (altclass
== ALL_REGS
|| rclass
== ALL_REGS
)
637 fprintf (file
, "; pref %s", reg_class_names
[(int) rclass
]);
638 else if (altclass
== NO_REGS
)
639 fprintf (file
, "; %s or none", reg_class_names
[(int) rclass
]);
641 fprintf (file
, "; pref %s, else %s",
642 reg_class_names
[(int) rclass
],
643 reg_class_names
[(int) altclass
]);
646 if (regno_reg_rtx
[i
] != NULL
&& REG_POINTER (regno_reg_rtx
[i
]))
647 fputs ("; pointer", file
);
654 dump_flow_info (FILE *file
, int flags
)
658 /* There are no pseudo registers after reload. Don't dump them. */
659 if (reg_info_p_size
&& (flags
& TDF_DETAILS
) != 0)
660 dump_reg_info (file
);
662 fprintf (file
, "\n%d basic blocks, %d edges.\n", n_basic_blocks
, n_edges
);
665 dump_bb_info (bb
, true, true, flags
, "", file
);
666 check_bb_profile (bb
, file
);
673 debug_flow_info (void)
675 dump_flow_info (stderr
, TDF_DETAILS
);
679 dump_edge_info (FILE *file
, edge e
, int do_succ
)
681 basic_block side
= (do_succ
? e
->dest
: e
->src
);
682 /* both ENTRY_BLOCK_PTR & EXIT_BLOCK_PTR depend upon cfun. */
683 if (cfun
&& side
== ENTRY_BLOCK_PTR
)
684 fputs (" ENTRY", file
);
685 else if (cfun
&& side
== EXIT_BLOCK_PTR
)
686 fputs (" EXIT", file
);
688 fprintf (file
, " %d", side
->index
);
691 fprintf (file
, " [%.1f%%] ", e
->probability
* 100.0 / REG_BR_PROB_BASE
);
695 fputs (" count:", file
);
696 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, e
->count
);
701 static const char * const bitnames
[] = {
702 "fallthru", "ab", "abcall", "eh", "fake", "dfs_back",
703 "can_fallthru", "irreducible", "sibcall", "loop_exit",
704 "true", "false", "exec"
707 int i
, flags
= e
->flags
;
710 for (i
= 0; flags
; i
++)
711 if (flags
& (1 << i
))
717 if (i
< (int) ARRAY_SIZE (bitnames
))
718 fputs (bitnames
[i
], file
);
720 fprintf (file
, "%d", i
);
728 /* Simple routines to easily allocate AUX fields of basic blocks. */
730 static struct obstack block_aux_obstack
;
731 static void *first_block_aux_obj
= 0;
732 static struct obstack edge_aux_obstack
;
733 static void *first_edge_aux_obj
= 0;
735 /* Allocate a memory block of SIZE as BB->aux. The obstack must
736 be first initialized by alloc_aux_for_blocks. */
739 alloc_aux_for_block (basic_block bb
, int size
)
741 /* Verify that aux field is clear. */
742 gcc_assert (!bb
->aux
&& first_block_aux_obj
);
743 bb
->aux
= obstack_alloc (&block_aux_obstack
, size
);
744 memset (bb
->aux
, 0, size
);
747 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
748 alloc_aux_for_block for each basic block. */
751 alloc_aux_for_blocks (int size
)
753 static int initialized
;
757 gcc_obstack_init (&block_aux_obstack
);
761 /* Check whether AUX data are still allocated. */
762 gcc_assert (!first_block_aux_obj
);
764 first_block_aux_obj
= obstack_alloc (&block_aux_obstack
, 0);
769 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
770 alloc_aux_for_block (bb
, size
);
774 /* Clear AUX pointers of all blocks. */
777 clear_aux_for_blocks (void)
781 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
785 /* Free data allocated in block_aux_obstack and clear AUX pointers
789 free_aux_for_blocks (void)
791 gcc_assert (first_block_aux_obj
);
792 obstack_free (&block_aux_obstack
, first_block_aux_obj
);
793 first_block_aux_obj
= NULL
;
795 clear_aux_for_blocks ();
798 /* Allocate a memory edge of SIZE as BB->aux. The obstack must
799 be first initialized by alloc_aux_for_edges. */
802 alloc_aux_for_edge (edge e
, int size
)
804 /* Verify that aux field is clear. */
805 gcc_assert (!e
->aux
&& first_edge_aux_obj
);
806 e
->aux
= obstack_alloc (&edge_aux_obstack
, size
);
807 memset (e
->aux
, 0, size
);
810 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
811 alloc_aux_for_edge for each basic edge. */
814 alloc_aux_for_edges (int size
)
816 static int initialized
;
820 gcc_obstack_init (&edge_aux_obstack
);
824 /* Check whether AUX data are still allocated. */
825 gcc_assert (!first_edge_aux_obj
);
827 first_edge_aux_obj
= obstack_alloc (&edge_aux_obstack
, 0);
832 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
837 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
838 alloc_aux_for_edge (e
, size
);
843 /* Clear AUX pointers of all edges. */
846 clear_aux_for_edges (void)
851 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
854 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
859 /* Free data allocated in edge_aux_obstack and clear AUX pointers
863 free_aux_for_edges (void)
865 gcc_assert (first_edge_aux_obj
);
866 obstack_free (&edge_aux_obstack
, first_edge_aux_obj
);
867 first_edge_aux_obj
= NULL
;
869 clear_aux_for_edges ();
873 debug_bb (basic_block bb
)
875 dump_bb (bb
, stderr
, 0);
878 DEBUG_FUNCTION basic_block
881 basic_block bb
= BASIC_BLOCK (n
);
882 dump_bb (bb
, stderr
, 0);
886 /* Dumps cfg related information about basic block BB to FILE. */
889 dump_cfg_bb_info (FILE *file
, basic_block bb
)
894 static const char * const bb_bitnames
[] =
896 "new", "reachable", "irreducible_loop", "superblock",
897 "nosched", "hot", "cold", "dup", "xlabel", "rtl",
900 const unsigned n_bitnames
= sizeof (bb_bitnames
) / sizeof (char *);
903 fprintf (file
, "Basic block %d", bb
->index
);
904 for (i
= 0; i
< n_bitnames
; i
++)
905 if (bb
->flags
& (1 << i
))
912 fputs (bb_bitnames
[i
], file
);
918 fputs ("Predecessors: ", file
);
919 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
920 dump_edge_info (file
, e
, 0);
922 fprintf (file
, "\nSuccessors: ");
923 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
924 dump_edge_info (file
, e
, 1);
925 fputs ("\n\n", file
);
928 /* Dumps a brief description of cfg to FILE. */
931 brief_dump_cfg (FILE *file
)
937 dump_cfg_bb_info (file
, bb
);
941 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
942 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
943 redirected to destination of TAKEN_EDGE.
945 This function may leave the profile inconsistent in the case TAKEN_EDGE
946 frequency or count is believed to be lower than FREQUENCY or COUNT
949 update_bb_profile_for_threading (basic_block bb
, int edge_frequency
,
950 gcov_type count
, edge taken_edge
)
960 fprintf (dump_file
, "bb %i count became negative after threading",
965 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
966 Watch for overflows. */
968 prob
= edge_frequency
* REG_BR_PROB_BASE
/ bb
->frequency
;
971 if (prob
> taken_edge
->probability
)
974 fprintf (dump_file
, "Jump threading proved probability of edge "
975 "%i->%i too small (it is %i, should be %i).\n",
976 taken_edge
->src
->index
, taken_edge
->dest
->index
,
977 taken_edge
->probability
, prob
);
978 prob
= taken_edge
->probability
;
981 /* Now rescale the probabilities. */
982 taken_edge
->probability
-= prob
;
983 prob
= REG_BR_PROB_BASE
- prob
;
984 bb
->frequency
-= edge_frequency
;
985 if (bb
->frequency
< 0)
990 fprintf (dump_file
, "Edge frequencies of bb %i has been reset, "
991 "frequency of block should end up being 0, it is %i\n",
992 bb
->index
, bb
->frequency
);
993 EDGE_SUCC (bb
, 0)->probability
= REG_BR_PROB_BASE
;
994 ei
= ei_start (bb
->succs
);
996 for (; (c
= ei_safe_edge (ei
)); ei_next (&ei
))
999 else if (prob
!= REG_BR_PROB_BASE
)
1001 int scale
= RDIV (65536 * REG_BR_PROB_BASE
, prob
);
1003 FOR_EACH_EDGE (c
, ei
, bb
->succs
)
1005 /* Protect from overflow due to additional scaling. */
1006 if (c
->probability
> prob
)
1007 c
->probability
= REG_BR_PROB_BASE
;
1010 c
->probability
= RDIV (c
->probability
* scale
, 65536);
1011 if (c
->probability
> REG_BR_PROB_BASE
)
1012 c
->probability
= REG_BR_PROB_BASE
;
1017 gcc_assert (bb
== taken_edge
->src
);
1018 taken_edge
->count
-= count
;
1019 if (taken_edge
->count
< 0)
1022 fprintf (dump_file
, "edge %i->%i count became negative after threading",
1023 taken_edge
->src
->index
, taken_edge
->dest
->index
);
1024 taken_edge
->count
= 0;
1028 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
1029 by NUM/DEN, in int arithmetic. May lose some accuracy. */
1031 scale_bbs_frequencies_int (basic_block
*bbs
, int nbbs
, int num
, int den
)
1038 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
1039 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
1040 and still safely fit in int during calculations. */
1046 num
= RDIV (1000 * num
, den
);
1049 if (num
> 100 * den
)
1052 for (i
= 0; i
< nbbs
; i
++)
1055 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* num
, den
);
1056 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
1057 if (bbs
[i
]->frequency
> BB_FREQ_MAX
)
1058 bbs
[i
]->frequency
= BB_FREQ_MAX
;
1059 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* num
, den
);
1060 FOR_EACH_EDGE (e
, ei
, bbs
[i
]->succs
)
1061 e
->count
= RDIV (e
->count
* num
, den
);
1065 /* numbers smaller than this value are safe to multiply without getting
1067 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (HOST_WIDEST_INT) * 4 - 1))
1069 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
1070 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
1071 function but considerably slower. */
1073 scale_bbs_frequencies_gcov_type (basic_block
*bbs
, int nbbs
, gcov_type num
,
1078 gcov_type fraction
= RDIV (num
* 65536, den
);
1080 gcc_assert (fraction
>= 0);
1082 if (num
< MAX_SAFE_MULTIPLIER
)
1083 for (i
= 0; i
< nbbs
; i
++)
1086 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* num
, den
);
1087 if (bbs
[i
]->count
<= MAX_SAFE_MULTIPLIER
)
1088 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* num
, den
);
1090 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* fraction
, 65536);
1091 FOR_EACH_EDGE (e
, ei
, bbs
[i
]->succs
)
1092 if (bbs
[i
]->count
<= MAX_SAFE_MULTIPLIER
)
1093 e
->count
= RDIV (e
->count
* num
, den
);
1095 e
->count
= RDIV (e
->count
* fraction
, 65536);
1098 for (i
= 0; i
< nbbs
; i
++)
1101 if (sizeof (gcov_type
) > sizeof (int))
1102 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* num
, den
);
1104 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* fraction
, 65536);
1105 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* fraction
, 65536);
1106 FOR_EACH_EDGE (e
, ei
, bbs
[i
]->succs
)
1107 e
->count
= RDIV (e
->count
* fraction
, 65536);
1111 /* Data structures used to maintain mapping between basic blocks and
1113 static htab_t bb_original
;
1114 static htab_t bb_copy
;
1116 /* And between loops and copies. */
1117 static htab_t loop_copy
;
1118 static alloc_pool original_copy_bb_pool
;
1120 struct htab_bb_copy_original_entry
1122 /* Block we are attaching info to. */
1124 /* Index of original or copy (depending on the hashtable) */
1129 bb_copy_original_hash (const void *p
)
1131 const struct htab_bb_copy_original_entry
*data
1132 = ((const struct htab_bb_copy_original_entry
*)p
);
1134 return data
->index1
;
1137 bb_copy_original_eq (const void *p
, const void *q
)
1139 const struct htab_bb_copy_original_entry
*data
1140 = ((const struct htab_bb_copy_original_entry
*)p
);
1141 const struct htab_bb_copy_original_entry
*data2
1142 = ((const struct htab_bb_copy_original_entry
*)q
);
1144 return data
->index1
== data2
->index1
;
1147 /* Initialize the data structures to maintain mapping between blocks
1150 initialize_original_copy_tables (void)
1152 gcc_assert (!original_copy_bb_pool
);
1153 original_copy_bb_pool
1154 = create_alloc_pool ("original_copy",
1155 sizeof (struct htab_bb_copy_original_entry
), 10);
1156 bb_original
= htab_create (10, bb_copy_original_hash
,
1157 bb_copy_original_eq
, NULL
);
1158 bb_copy
= htab_create (10, bb_copy_original_hash
, bb_copy_original_eq
, NULL
);
1159 loop_copy
= htab_create (10, bb_copy_original_hash
, bb_copy_original_eq
, NULL
);
1162 /* Free the data structures to maintain mapping between blocks and
1165 free_original_copy_tables (void)
1167 gcc_assert (original_copy_bb_pool
);
1168 htab_delete (bb_copy
);
1169 htab_delete (bb_original
);
1170 htab_delete (loop_copy
);
1171 free_alloc_pool (original_copy_bb_pool
);
1175 original_copy_bb_pool
= NULL
;
1178 /* Removes the value associated with OBJ from table TAB. */
1181 copy_original_table_clear (htab_t tab
, unsigned obj
)
1184 struct htab_bb_copy_original_entry key
, *elt
;
1186 if (!original_copy_bb_pool
)
1190 slot
= htab_find_slot (tab
, &key
, NO_INSERT
);
1194 elt
= (struct htab_bb_copy_original_entry
*) *slot
;
1195 htab_clear_slot (tab
, slot
);
1196 pool_free (original_copy_bb_pool
, elt
);
1199 /* Sets the value associated with OBJ in table TAB to VAL.
1200 Do nothing when data structures are not initialized. */
1203 copy_original_table_set (htab_t tab
, unsigned obj
, unsigned val
)
1205 struct htab_bb_copy_original_entry
**slot
;
1206 struct htab_bb_copy_original_entry key
;
1208 if (!original_copy_bb_pool
)
1212 slot
= (struct htab_bb_copy_original_entry
**)
1213 htab_find_slot (tab
, &key
, INSERT
);
1216 *slot
= (struct htab_bb_copy_original_entry
*)
1217 pool_alloc (original_copy_bb_pool
);
1218 (*slot
)->index1
= obj
;
1220 (*slot
)->index2
= val
;
1223 /* Set original for basic block. Do nothing when data structures are not
1224 initialized so passes not needing this don't need to care. */
1226 set_bb_original (basic_block bb
, basic_block original
)
1228 copy_original_table_set (bb_original
, bb
->index
, original
->index
);
1231 /* Get the original basic block. */
1233 get_bb_original (basic_block bb
)
1235 struct htab_bb_copy_original_entry
*entry
;
1236 struct htab_bb_copy_original_entry key
;
1238 gcc_assert (original_copy_bb_pool
);
1240 key
.index1
= bb
->index
;
1241 entry
= (struct htab_bb_copy_original_entry
*) htab_find (bb_original
, &key
);
1243 return BASIC_BLOCK (entry
->index2
);
1248 /* Set copy for basic block. Do nothing when data structures are not
1249 initialized so passes not needing this don't need to care. */
1251 set_bb_copy (basic_block bb
, basic_block copy
)
1253 copy_original_table_set (bb_copy
, bb
->index
, copy
->index
);
1256 /* Get the copy of basic block. */
1258 get_bb_copy (basic_block bb
)
1260 struct htab_bb_copy_original_entry
*entry
;
1261 struct htab_bb_copy_original_entry key
;
1263 gcc_assert (original_copy_bb_pool
);
1265 key
.index1
= bb
->index
;
1266 entry
= (struct htab_bb_copy_original_entry
*) htab_find (bb_copy
, &key
);
1268 return BASIC_BLOCK (entry
->index2
);
1273 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1274 initialized so passes not needing this don't need to care. */
1277 set_loop_copy (struct loop
*loop
, struct loop
*copy
)
1280 copy_original_table_clear (loop_copy
, loop
->num
);
1282 copy_original_table_set (loop_copy
, loop
->num
, copy
->num
);
1285 /* Get the copy of LOOP. */
1288 get_loop_copy (struct loop
*loop
)
1290 struct htab_bb_copy_original_entry
*entry
;
1291 struct htab_bb_copy_original_entry key
;
1293 gcc_assert (original_copy_bb_pool
);
1295 key
.index1
= loop
->num
;
1296 entry
= (struct htab_bb_copy_original_entry
*) htab_find (loop_copy
, &key
);
1298 return get_loop (entry
->index2
);