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
47 TODO: Document these "Available functionality" functions in the files
53 #include "coretypes.h"
57 #include "alloc-pool.h"
59 #include "basic-block.h"
61 #include "cfgloop.h" /* FIXME: For struct loop. */
65 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
67 /* Called once at initialization time. */
70 init_flow (struct function
*the_fun
)
73 the_fun
->cfg
= ggc_alloc_cleared_control_flow_graph ();
74 n_edges_for_function (the_fun
) = 0;
75 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
)
76 = ggc_alloc_cleared_basic_block_def ();
77 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
)->index
= ENTRY_BLOCK
;
78 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
)
79 = ggc_alloc_cleared_basic_block_def ();
80 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
)->index
= EXIT_BLOCK
;
81 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
)->next_bb
82 = EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
);
83 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun
)->prev_bb
84 = ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun
);
87 /* Helper function for remove_edge and clear_edges. Frees edge structure
88 without actually removing it from the pred/succ arrays. */
97 /* Free the memory associated with the edge structures. */
108 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
110 VEC_truncate (edge
, bb
->succs
, 0);
111 VEC_truncate (edge
, bb
->preds
, 0);
114 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
116 VEC_truncate (edge
, EXIT_BLOCK_PTR
->preds
, 0);
117 VEC_truncate (edge
, ENTRY_BLOCK_PTR
->succs
, 0);
119 gcc_assert (!n_edges
);
122 /* Allocate memory for basic_block. */
128 bb
= ggc_alloc_cleared_basic_block_def ();
132 /* Link block B to chain after AFTER. */
134 link_block (basic_block b
, basic_block after
)
136 b
->next_bb
= after
->next_bb
;
139 b
->next_bb
->prev_bb
= b
;
142 /* Unlink block B from chain. */
144 unlink_block (basic_block b
)
146 b
->next_bb
->prev_bb
= b
->prev_bb
;
147 b
->prev_bb
->next_bb
= b
->next_bb
;
152 /* Sequentially order blocks and compact the arrays. */
154 compact_blocks (void)
158 SET_BASIC_BLOCK (ENTRY_BLOCK
, ENTRY_BLOCK_PTR
);
159 SET_BASIC_BLOCK (EXIT_BLOCK
, EXIT_BLOCK_PTR
);
162 df_compact_blocks ();
167 i
= NUM_FIXED_BLOCKS
;
170 SET_BASIC_BLOCK (i
, bb
);
174 gcc_assert (i
== n_basic_blocks
);
176 for (; i
< last_basic_block
; i
++)
177 SET_BASIC_BLOCK (i
, NULL
);
179 last_basic_block
= n_basic_blocks
;
182 /* Remove block B from the basic block array. */
185 expunge_block (basic_block b
)
188 SET_BASIC_BLOCK (b
->index
, NULL
);
190 /* We should be able to ggc_free here, but we are not.
191 The dead SSA_NAMES are left pointing to dead statements that are pointing
192 to dead basic blocks making garbage collector to die.
193 We should be able to release all dead SSA_NAMES and at the same time we should
194 clear out BB pointer of dead statements consistently. */
197 /* Connect E to E->src. */
202 VEC_safe_push (edge
, gc
, e
->src
->succs
, e
);
203 df_mark_solutions_dirty ();
206 /* Connect E to E->dest. */
209 connect_dest (edge e
)
211 basic_block dest
= e
->dest
;
212 VEC_safe_push (edge
, gc
, dest
->preds
, e
);
213 e
->dest_idx
= EDGE_COUNT (dest
->preds
) - 1;
214 df_mark_solutions_dirty ();
217 /* Disconnect edge E from E->src. */
220 disconnect_src (edge e
)
222 basic_block src
= e
->src
;
226 for (ei
= ei_start (src
->succs
); (tmp
= ei_safe_edge (ei
)); )
230 VEC_unordered_remove (edge
, src
->succs
, ei
.index
);
231 df_mark_solutions_dirty ();
241 /* Disconnect edge E from E->dest. */
244 disconnect_dest (edge e
)
246 basic_block dest
= e
->dest
;
247 unsigned int dest_idx
= e
->dest_idx
;
249 VEC_unordered_remove (edge
, dest
->preds
, dest_idx
);
251 /* If we removed an edge in the middle of the edge vector, we need
252 to update dest_idx of the edge that moved into the "hole". */
253 if (dest_idx
< EDGE_COUNT (dest
->preds
))
254 EDGE_PRED (dest
, dest_idx
)->dest_idx
= dest_idx
;
255 df_mark_solutions_dirty ();
258 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
259 created edge. Use this only if you are sure that this edge can't
260 possibly already exist. */
263 unchecked_make_edge (basic_block src
, basic_block dst
, int flags
)
266 e
= ggc_alloc_cleared_edge_def ();
276 execute_on_growing_pred (e
);
280 /* Create an edge connecting SRC and DST with FLAGS optionally using
281 edge cache CACHE. Return the new edge, NULL if already exist. */
284 cached_make_edge (sbitmap edge_cache
, basic_block src
, basic_block dst
, int flags
)
286 if (edge_cache
== NULL
287 || src
== ENTRY_BLOCK_PTR
288 || dst
== EXIT_BLOCK_PTR
)
289 return make_edge (src
, dst
, flags
);
291 /* Does the requested edge already exist? */
292 if (! TEST_BIT (edge_cache
, dst
->index
))
294 /* The edge does not exist. Create one and update the
296 SET_BIT (edge_cache
, dst
->index
);
297 return unchecked_make_edge (src
, dst
, flags
);
300 /* At this point, we know that the requested edge exists. Adjust
301 flags if necessary. */
304 edge e
= find_edge (src
, dst
);
311 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
312 created edge or NULL if already exist. */
315 make_edge (basic_block src
, basic_block dest
, int flags
)
317 edge e
= find_edge (src
, dest
);
319 /* Make sure we don't add duplicate edges. */
326 return unchecked_make_edge (src
, dest
, flags
);
329 /* Create an edge connecting SRC to DEST and set probability by knowing
330 that it is the single edge leaving SRC. */
333 make_single_succ_edge (basic_block src
, basic_block dest
, int flags
)
335 edge e
= make_edge (src
, dest
, flags
);
337 e
->probability
= REG_BR_PROB_BASE
;
338 e
->count
= src
->count
;
342 /* This function will remove an edge from the flow graph. */
345 remove_edge_raw (edge e
)
347 remove_predictions_associated_with_edge (e
);
348 execute_on_shrinking_pred (e
);
356 /* Redirect an edge's successor from one block to another. */
359 redirect_edge_succ (edge e
, basic_block new_succ
)
361 execute_on_shrinking_pred (e
);
367 /* Reconnect the edge to the new successor block. */
370 execute_on_growing_pred (e
);
373 /* Redirect an edge's predecessor from one block to another. */
376 redirect_edge_pred (edge e
, basic_block new_pred
)
382 /* Reconnect the edge to the new predecessor block. */
386 /* Clear all basic block flags that do not have to be preserved. */
388 clear_bb_flags (void)
392 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
393 bb
->flags
&= BB_FLAGS_TO_PRESERVE
;
396 /* Check the consistency of profile information. We can't do that
397 in verify_flow_info, as the counts may get invalid for incompletely
398 solved graphs, later eliminating of conditionals or roundoff errors.
399 It is still practical to have them reported for debugging of simple
402 check_bb_profile (basic_block bb
, FILE * file
, int indent
, int flags
)
408 struct function
*fun
= DECL_STRUCT_FUNCTION (current_function_decl
);
409 char *s_indent
= (char *) alloca ((size_t) indent
+ 1);
410 memset ((void *) s_indent
, ' ', (size_t) indent
);
411 s_indent
[indent
] = '\0';
413 if (profile_status_for_function (fun
) == PROFILE_ABSENT
)
416 if (bb
!= EXIT_BLOCK_PTR_FOR_FUNCTION (fun
))
418 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
419 sum
+= e
->probability
;
420 if (EDGE_COUNT (bb
->succs
) && abs (sum
- REG_BR_PROB_BASE
) > 100)
421 fprintf (file
, "%s%sInvalid sum of outgoing probabilities %.1f%%\n",
422 (flags
& TDF_COMMENT
) ? ";; " : "", s_indent
,
423 sum
* 100.0 / REG_BR_PROB_BASE
);
425 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
427 if (EDGE_COUNT (bb
->succs
)
428 && (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100))
429 fprintf (file
, "%s%sInvalid sum of outgoing counts %i, should be %i\n",
430 (flags
& TDF_COMMENT
) ? ";; " : "", s_indent
,
431 (int) lsum
, (int) bb
->count
);
433 if (bb
!= ENTRY_BLOCK_PTR_FOR_FUNCTION (fun
))
436 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
437 sum
+= EDGE_FREQUENCY (e
);
438 if (abs (sum
- bb
->frequency
) > 100)
440 "%s%sInvalid sum of incoming frequencies %i, should be %i\n",
441 (flags
& TDF_COMMENT
) ? ";; " : "", s_indent
,
444 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
446 if (lsum
- bb
->count
> 100 || lsum
- bb
->count
< -100)
447 fprintf (file
, "%s%sInvalid sum of incoming counts %i, should be %i\n",
448 (flags
& TDF_COMMENT
) ? ";; " : "", s_indent
,
449 (int) lsum
, (int) bb
->count
);
454 dump_edge_info (FILE *file
, edge e
, int flags
, int do_succ
)
456 basic_block side
= (do_succ
? e
->dest
: e
->src
);
457 bool do_details
= false;
459 if ((flags
& TDF_DETAILS
) != 0
460 && (flags
& TDF_SLIM
) == 0)
463 /* ENTRY_BLOCK_PTR/EXIT_BLOCK_PTR depend on cfun.
464 Compare against ENTRY_BLOCK/EXIT_BLOCK to avoid that dependency. */
465 if (side
->index
== ENTRY_BLOCK
)
466 fputs (" ENTRY", file
);
467 else if (side
->index
== EXIT_BLOCK
)
468 fputs (" EXIT", file
);
470 fprintf (file
, " %d", side
->index
);
472 if (e
->probability
&& do_details
)
473 fprintf (file
, " [%.1f%%] ", e
->probability
* 100.0 / REG_BR_PROB_BASE
);
475 if (e
->count
&& do_details
)
477 fputs (" count:", file
);
478 fprintf (file
, HOST_WIDEST_INT_PRINT_DEC
, e
->count
);
481 if (e
->flags
&& do_details
)
483 static const char * const bitnames
[] =
485 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
486 #include "cfg-flags.def"
491 int i
, flags
= e
->flags
;
493 gcc_assert (e
->flags
<= EDGE_ALL_FLAGS
);
495 for (i
= 0; flags
; i
++)
496 if (flags
& (1 << i
))
502 fputs (bitnames
[i
], file
);
510 /* Simple routines to easily allocate AUX fields of basic blocks. */
512 static struct obstack block_aux_obstack
;
513 static void *first_block_aux_obj
= 0;
514 static struct obstack edge_aux_obstack
;
515 static void *first_edge_aux_obj
= 0;
517 /* Allocate a memory block of SIZE as BB->aux. The obstack must
518 be first initialized by alloc_aux_for_blocks. */
521 alloc_aux_for_block (basic_block bb
, int size
)
523 /* Verify that aux field is clear. */
524 gcc_assert (!bb
->aux
&& first_block_aux_obj
);
525 bb
->aux
= obstack_alloc (&block_aux_obstack
, size
);
526 memset (bb
->aux
, 0, size
);
529 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
530 alloc_aux_for_block for each basic block. */
533 alloc_aux_for_blocks (int size
)
535 static int initialized
;
539 gcc_obstack_init (&block_aux_obstack
);
543 /* Check whether AUX data are still allocated. */
544 gcc_assert (!first_block_aux_obj
);
546 first_block_aux_obj
= obstack_alloc (&block_aux_obstack
, 0);
552 alloc_aux_for_block (bb
, size
);
556 /* Clear AUX pointers of all blocks. */
559 clear_aux_for_blocks (void)
567 /* Free data allocated in block_aux_obstack and clear AUX pointers
571 free_aux_for_blocks (void)
573 gcc_assert (first_block_aux_obj
);
574 obstack_free (&block_aux_obstack
, first_block_aux_obj
);
575 first_block_aux_obj
= NULL
;
577 clear_aux_for_blocks ();
580 /* Allocate a memory edge of SIZE as E->aux. The obstack must
581 be first initialized by alloc_aux_for_edges. */
584 alloc_aux_for_edge (edge e
, int size
)
586 /* Verify that aux field is clear. */
587 gcc_assert (!e
->aux
&& first_edge_aux_obj
);
588 e
->aux
= obstack_alloc (&edge_aux_obstack
, size
);
589 memset (e
->aux
, 0, size
);
592 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
593 alloc_aux_for_edge for each basic edge. */
596 alloc_aux_for_edges (int size
)
598 static int initialized
;
602 gcc_obstack_init (&edge_aux_obstack
);
606 /* Check whether AUX data are still allocated. */
607 gcc_assert (!first_edge_aux_obj
);
609 first_edge_aux_obj
= obstack_alloc (&edge_aux_obstack
, 0);
614 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
619 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
620 alloc_aux_for_edge (e
, size
);
625 /* Clear AUX pointers of all edges. */
628 clear_aux_for_edges (void)
633 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
636 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
641 /* Free data allocated in edge_aux_obstack and clear AUX pointers
645 free_aux_for_edges (void)
647 gcc_assert (first_edge_aux_obj
);
648 obstack_free (&edge_aux_obstack
, first_edge_aux_obj
);
649 first_edge_aux_obj
= NULL
;
651 clear_aux_for_edges ();
655 debug_bb (basic_block bb
)
657 dump_bb (stderr
, bb
, 0, dump_flags
);
660 DEBUG_FUNCTION basic_block
663 basic_block bb
= BASIC_BLOCK (n
);
668 /* Dumps cfg related information about basic block BB to OUTF.
669 If HEADER is true, dump things that appear before the instructions
670 contained in BB. If FOOTER is true, dump things that appear after.
671 Flags are the TDF_* masks as documented in dumpfile.h.
672 NB: With TDF_DETAILS, it is assumed that cfun is available, so
673 that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */
676 dump_bb_info (FILE *outf
, basic_block bb
, int indent
, int flags
,
677 bool do_header
, bool do_footer
)
681 static const char * const bb_bitnames
[] =
683 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
684 #include "cfg-flags.def"
686 #undef DEF_BASIC_BLOCK_FLAG
688 const unsigned n_bitnames
= sizeof (bb_bitnames
) / sizeof (char *);
690 char *s_indent
= (char *) alloca ((size_t) indent
+ 1);
691 memset ((void *) s_indent
, ' ', (size_t) indent
);
692 s_indent
[indent
] = '\0';
694 gcc_assert (bb
->flags
<= BB_ALL_FLAGS
);
700 if (flags
& TDF_COMMENT
)
702 fprintf (outf
, "%sbasic block %d, loop depth %d",
703 s_indent
, bb
->index
, bb_loop_depth (bb
));
704 if (flags
& TDF_DETAILS
)
706 struct function
*fun
= DECL_STRUCT_FUNCTION (current_function_decl
);
707 fprintf (outf
, ", count " HOST_WIDEST_INT_PRINT_DEC
,
708 (HOST_WIDEST_INT
) bb
->count
);
709 fprintf (outf
, ", freq %i", bb
->frequency
);
710 if (maybe_hot_bb_p (fun
, bb
))
711 fputs (", maybe hot", outf
);
712 if (probably_never_executed_bb_p (fun
, bb
))
713 fputs (", probably never executed", outf
);
717 check_bb_profile (bb
, outf
, indent
, flags
);
719 if (flags
& TDF_DETAILS
)
721 if (flags
& TDF_COMMENT
)
723 fprintf (outf
, "%s prev block ", s_indent
);
725 fprintf (outf
, "%d", bb
->prev_bb
->index
);
727 fprintf (outf
, "(nil)");
728 fprintf (outf
, ", next block ");
730 fprintf (outf
, "%d", bb
->next_bb
->index
);
732 fprintf (outf
, "(nil)");
734 fputs (", flags:", outf
);
736 for (i
= 0; i
< n_bitnames
; i
++)
737 if (bb
->flags
& (1 << i
))
744 fputs (bb_bitnames
[i
], outf
);
751 if (flags
& TDF_COMMENT
)
753 fprintf (outf
, "%s pred: ", s_indent
);
755 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
759 if (flags
& TDF_COMMENT
)
761 fprintf (outf
, "%s ", s_indent
);
764 dump_edge_info (outf
, e
, flags
, 0);
773 if (flags
& TDF_COMMENT
)
775 fprintf (outf
, "%s succ: ", s_indent
);
777 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
781 if (flags
& TDF_COMMENT
)
783 fprintf (outf
, "%s ", s_indent
);
786 dump_edge_info (outf
, e
, flags
, 1);
794 /* Dumps a brief description of cfg to FILE. */
797 brief_dump_cfg (FILE *file
, int flags
)
803 dump_bb_info (file
, bb
, 0,
804 flags
& (TDF_COMMENT
| TDF_DETAILS
),
809 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
810 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
811 redirected to destination of TAKEN_EDGE.
813 This function may leave the profile inconsistent in the case TAKEN_EDGE
814 frequency or count is believed to be lower than FREQUENCY or COUNT
817 update_bb_profile_for_threading (basic_block bb
, int edge_frequency
,
818 gcov_type count
, edge taken_edge
)
828 fprintf (dump_file
, "bb %i count became negative after threading",
833 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
834 Watch for overflows. */
836 prob
= edge_frequency
* REG_BR_PROB_BASE
/ bb
->frequency
;
839 if (prob
> taken_edge
->probability
)
842 fprintf (dump_file
, "Jump threading proved probability of edge "
843 "%i->%i too small (it is %i, should be %i).\n",
844 taken_edge
->src
->index
, taken_edge
->dest
->index
,
845 taken_edge
->probability
, prob
);
846 prob
= taken_edge
->probability
;
849 /* Now rescale the probabilities. */
850 taken_edge
->probability
-= prob
;
851 prob
= REG_BR_PROB_BASE
- prob
;
852 bb
->frequency
-= edge_frequency
;
853 if (bb
->frequency
< 0)
858 fprintf (dump_file
, "Edge frequencies of bb %i has been reset, "
859 "frequency of block should end up being 0, it is %i\n",
860 bb
->index
, bb
->frequency
);
861 EDGE_SUCC (bb
, 0)->probability
= REG_BR_PROB_BASE
;
862 ei
= ei_start (bb
->succs
);
864 for (; (c
= ei_safe_edge (ei
)); ei_next (&ei
))
867 else if (prob
!= REG_BR_PROB_BASE
)
869 int scale
= RDIV (65536 * REG_BR_PROB_BASE
, prob
);
871 FOR_EACH_EDGE (c
, ei
, bb
->succs
)
873 /* Protect from overflow due to additional scaling. */
874 if (c
->probability
> prob
)
875 c
->probability
= REG_BR_PROB_BASE
;
878 c
->probability
= RDIV (c
->probability
* scale
, 65536);
879 if (c
->probability
> REG_BR_PROB_BASE
)
880 c
->probability
= REG_BR_PROB_BASE
;
885 gcc_assert (bb
== taken_edge
->src
);
886 taken_edge
->count
-= count
;
887 if (taken_edge
->count
< 0)
890 fprintf (dump_file
, "edge %i->%i count became negative after threading",
891 taken_edge
->src
->index
, taken_edge
->dest
->index
);
892 taken_edge
->count
= 0;
896 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
897 by NUM/DEN, in int arithmetic. May lose some accuracy. */
899 scale_bbs_frequencies_int (basic_block
*bbs
, int nbbs
, int num
, int den
)
906 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
907 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
908 and still safely fit in int during calculations. */
914 num
= RDIV (1000 * num
, den
);
920 for (i
= 0; i
< nbbs
; i
++)
923 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* num
, den
);
924 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
925 if (bbs
[i
]->frequency
> BB_FREQ_MAX
)
926 bbs
[i
]->frequency
= BB_FREQ_MAX
;
927 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* num
, den
);
928 FOR_EACH_EDGE (e
, ei
, bbs
[i
]->succs
)
929 e
->count
= RDIV (e
->count
* num
, den
);
933 /* numbers smaller than this value are safe to multiply without getting
935 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (HOST_WIDEST_INT) * 4 - 1))
937 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
938 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
939 function but considerably slower. */
941 scale_bbs_frequencies_gcov_type (basic_block
*bbs
, int nbbs
, gcov_type num
,
946 gcov_type fraction
= RDIV (num
* 65536, den
);
948 gcc_assert (fraction
>= 0);
950 if (num
< MAX_SAFE_MULTIPLIER
)
951 for (i
= 0; i
< nbbs
; i
++)
954 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* num
, den
);
955 if (bbs
[i
]->count
<= MAX_SAFE_MULTIPLIER
)
956 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* num
, den
);
958 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* fraction
, 65536);
959 FOR_EACH_EDGE (e
, ei
, bbs
[i
]->succs
)
960 if (bbs
[i
]->count
<= MAX_SAFE_MULTIPLIER
)
961 e
->count
= RDIV (e
->count
* num
, den
);
963 e
->count
= RDIV (e
->count
* fraction
, 65536);
966 for (i
= 0; i
< nbbs
; i
++)
969 if (sizeof (gcov_type
) > sizeof (int))
970 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* num
, den
);
972 bbs
[i
]->frequency
= RDIV (bbs
[i
]->frequency
* fraction
, 65536);
973 bbs
[i
]->count
= RDIV (bbs
[i
]->count
* fraction
, 65536);
974 FOR_EACH_EDGE (e
, ei
, bbs
[i
]->succs
)
975 e
->count
= RDIV (e
->count
* fraction
, 65536);
979 /* Data structures used to maintain mapping between basic blocks and
981 static htab_t bb_original
;
982 static htab_t bb_copy
;
984 /* And between loops and copies. */
985 static htab_t loop_copy
;
986 static alloc_pool original_copy_bb_pool
;
988 struct htab_bb_copy_original_entry
990 /* Block we are attaching info to. */
992 /* Index of original or copy (depending on the hashtable) */
997 bb_copy_original_hash (const void *p
)
999 const struct htab_bb_copy_original_entry
*data
1000 = ((const struct htab_bb_copy_original_entry
*)p
);
1002 return data
->index1
;
1005 bb_copy_original_eq (const void *p
, const void *q
)
1007 const struct htab_bb_copy_original_entry
*data
1008 = ((const struct htab_bb_copy_original_entry
*)p
);
1009 const struct htab_bb_copy_original_entry
*data2
1010 = ((const struct htab_bb_copy_original_entry
*)q
);
1012 return data
->index1
== data2
->index1
;
1015 /* Initialize the data structures to maintain mapping between blocks
1018 initialize_original_copy_tables (void)
1020 gcc_assert (!original_copy_bb_pool
);
1021 original_copy_bb_pool
1022 = create_alloc_pool ("original_copy",
1023 sizeof (struct htab_bb_copy_original_entry
), 10);
1024 bb_original
= htab_create (10, bb_copy_original_hash
,
1025 bb_copy_original_eq
, NULL
);
1026 bb_copy
= htab_create (10, bb_copy_original_hash
, bb_copy_original_eq
, NULL
);
1027 loop_copy
= htab_create (10, bb_copy_original_hash
, bb_copy_original_eq
, NULL
);
1030 /* Free the data structures to maintain mapping between blocks and
1033 free_original_copy_tables (void)
1035 gcc_assert (original_copy_bb_pool
);
1036 htab_delete (bb_copy
);
1037 htab_delete (bb_original
);
1038 htab_delete (loop_copy
);
1039 free_alloc_pool (original_copy_bb_pool
);
1043 original_copy_bb_pool
= NULL
;
1046 /* Removes the value associated with OBJ from table TAB. */
1049 copy_original_table_clear (htab_t tab
, unsigned obj
)
1052 struct htab_bb_copy_original_entry key
, *elt
;
1054 if (!original_copy_bb_pool
)
1058 slot
= htab_find_slot (tab
, &key
, NO_INSERT
);
1062 elt
= (struct htab_bb_copy_original_entry
*) *slot
;
1063 htab_clear_slot (tab
, slot
);
1064 pool_free (original_copy_bb_pool
, elt
);
1067 /* Sets the value associated with OBJ in table TAB to VAL.
1068 Do nothing when data structures are not initialized. */
1071 copy_original_table_set (htab_t tab
, unsigned obj
, unsigned val
)
1073 struct htab_bb_copy_original_entry
**slot
;
1074 struct htab_bb_copy_original_entry key
;
1076 if (!original_copy_bb_pool
)
1080 slot
= (struct htab_bb_copy_original_entry
**)
1081 htab_find_slot (tab
, &key
, INSERT
);
1084 *slot
= (struct htab_bb_copy_original_entry
*)
1085 pool_alloc (original_copy_bb_pool
);
1086 (*slot
)->index1
= obj
;
1088 (*slot
)->index2
= val
;
1091 /* Set original for basic block. Do nothing when data structures are not
1092 initialized so passes not needing this don't need to care. */
1094 set_bb_original (basic_block bb
, basic_block original
)
1096 copy_original_table_set (bb_original
, bb
->index
, original
->index
);
1099 /* Get the original basic block. */
1101 get_bb_original (basic_block bb
)
1103 struct htab_bb_copy_original_entry
*entry
;
1104 struct htab_bb_copy_original_entry key
;
1106 gcc_assert (original_copy_bb_pool
);
1108 key
.index1
= bb
->index
;
1109 entry
= (struct htab_bb_copy_original_entry
*) htab_find (bb_original
, &key
);
1111 return BASIC_BLOCK (entry
->index2
);
1116 /* Set copy for basic block. Do nothing when data structures are not
1117 initialized so passes not needing this don't need to care. */
1119 set_bb_copy (basic_block bb
, basic_block copy
)
1121 copy_original_table_set (bb_copy
, bb
->index
, copy
->index
);
1124 /* Get the copy of basic block. */
1126 get_bb_copy (basic_block bb
)
1128 struct htab_bb_copy_original_entry
*entry
;
1129 struct htab_bb_copy_original_entry key
;
1131 gcc_assert (original_copy_bb_pool
);
1133 key
.index1
= bb
->index
;
1134 entry
= (struct htab_bb_copy_original_entry
*) htab_find (bb_copy
, &key
);
1136 return BASIC_BLOCK (entry
->index2
);
1141 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1142 initialized so passes not needing this don't need to care. */
1145 set_loop_copy (struct loop
*loop
, struct loop
*copy
)
1148 copy_original_table_clear (loop_copy
, loop
->num
);
1150 copy_original_table_set (loop_copy
, loop
->num
, copy
->num
);
1153 /* Get the copy of LOOP. */
1156 get_loop_copy (struct loop
*loop
)
1158 struct htab_bb_copy_original_entry
*entry
;
1159 struct htab_bb_copy_original_entry key
;
1161 gcc_assert (original_copy_bb_pool
);
1163 key
.index1
= loop
->num
;
1164 entry
= (struct htab_bb_copy_original_entry
*) htab_find (loop_copy
, &key
);
1166 return get_loop (entry
->index2
);