1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2015 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Generate basic block profile instrumentation and auxiliary files.
24 Profile generation is optimized, so that not all arcs in the basic
25 block graph need instrumenting. First, the BB graph is closed with
26 one entry (function start), and one exit (function exit). Any
27 ABNORMAL_EDGE cannot be instrumented (because there is no control
28 path to place the code). We close the graph by inserting fake
29 EDGE_FAKE edges to the EXIT_BLOCK, from the sources of abnormal
30 edges that do not go to the exit_block. We ignore such abnormal
31 edges. Naturally these fake edges are never directly traversed,
32 and so *cannot* be directly instrumented. Some other graph
33 massaging is done. To optimize the instrumentation we generate the
34 BB minimal span tree, only edges that are not on the span tree
35 (plus the entry point) need instrumenting. From that information
36 all other edge counts can be deduced. By construction all fake
37 edges must be on the spanning tree. We also attempt to place
38 EDGE_CRITICAL edges on the spanning tree.
40 The auxiliary files generated are <dumpbase>.gcno (at compile time)
41 and <dumpbase>.gcda (at run time). The format is
42 described in full in gcov-io.h. */
44 /* ??? Register allocation should use basic block execution counts to
45 give preference to the most commonly executed blocks. */
47 /* ??? Should calculate branch probabilities before instrumenting code, since
48 then we can use arc counts to help decide which arcs to instrument. */
52 #include "coretypes.h"
58 #include "hard-reg-set.h"
62 #include "insn-config.h"
72 #include "dominance.h"
75 #include "basic-block.h"
76 #include "diagnostic-core.h"
78 #include "value-prof.h"
79 #include "fold-const.h"
80 #include "tree-ssa-alias.h"
81 #include "internal-fn.h"
82 #include "gimple-expr.h"
84 #include "gimple-iterator.h"
92 struct bb_profile_info
{
93 unsigned int count_valid
: 1;
95 /* Number of successor and predecessor edges. */
100 #define BB_INFO(b) ((struct bb_profile_info *) (b)->aux)
103 /* Counter summary from the last set of coverage counts read. */
105 const struct gcov_ctr_summary
*profile_info
;
107 /* Counter working set information computed from the current counter
108 summary. Not initialized unless profile_info summary is non-NULL. */
109 static gcov_working_set_t gcov_working_sets
[NUM_GCOV_WORKING_SETS
];
111 /* Collect statistics on the performance of this pass for the entire source
114 static int total_num_blocks
;
115 static int total_num_edges
;
116 static int total_num_edges_ignored
;
117 static int total_num_edges_instrumented
;
118 static int total_num_blocks_created
;
119 static int total_num_passes
;
120 static int total_num_times_called
;
121 static int total_hist_br_prob
[20];
122 static int total_num_branches
;
124 /* Helper function to update gcov_working_sets. */
126 void add_working_set (gcov_working_set_t
*set
) {
128 for (; i
< NUM_GCOV_WORKING_SETS
; i
++)
129 gcov_working_sets
[i
] = set
[i
];
132 /* Forward declarations. */
133 static void find_spanning_tree (struct edge_list
*);
135 /* Add edge instrumentation code to the entire insn chain.
137 F is the first insn of the chain.
138 NUM_BLOCKS is the number of basic blocks found in F. */
141 instrument_edges (struct edge_list
*el
)
143 unsigned num_instr_edges
= 0;
144 int num_edges
= NUM_EDGES (el
);
147 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
152 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
154 struct edge_profile_info
*inf
= EDGE_INFO (e
);
156 if (!inf
->ignore
&& !inf
->on_tree
)
158 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
160 fprintf (dump_file
, "Edge %d to %d instrumented%s\n",
161 e
->src
->index
, e
->dest
->index
,
162 EDGE_CRITICAL_P (e
) ? " (and split)" : "");
163 gimple_gen_edge_profiler (num_instr_edges
++, e
);
168 total_num_blocks_created
+= num_edges
;
170 fprintf (dump_file
, "%d edges instrumented\n", num_instr_edges
);
171 return num_instr_edges
;
174 /* Add code to measure histograms for values in list VALUES. */
176 instrument_values (histogram_values values
)
180 /* Emit code to generate the histograms before the insns. */
182 for (i
= 0; i
< values
.length (); i
++)
184 histogram_value hist
= values
[i
];
185 unsigned t
= COUNTER_FOR_HIST_TYPE (hist
->type
);
187 if (!coverage_counter_alloc (t
, hist
->n_counters
))
192 case HIST_TYPE_INTERVAL
:
193 gimple_gen_interval_profiler (hist
, t
, 0);
197 gimple_gen_pow2_profiler (hist
, t
, 0);
200 case HIST_TYPE_SINGLE_VALUE
:
201 gimple_gen_one_value_profiler (hist
, t
, 0);
204 case HIST_TYPE_CONST_DELTA
:
205 gimple_gen_const_delta_profiler (hist
, t
, 0);
208 case HIST_TYPE_INDIR_CALL
:
209 case HIST_TYPE_INDIR_CALL_TOPN
:
210 gimple_gen_ic_profiler (hist
, t
, 0);
213 case HIST_TYPE_AVERAGE
:
214 gimple_gen_average_profiler (hist
, t
, 0);
218 gimple_gen_ior_profiler (hist
, t
, 0);
221 case HIST_TYPE_TIME_PROFILE
:
224 split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
225 gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
227 gimple_gen_time_profiler (t
, 0, gsi
);
238 /* Fill the working set information into the profile_info structure. */
241 get_working_sets (void)
243 unsigned ws_ix
, pctinc
, pct
;
244 gcov_working_set_t
*ws_info
;
249 compute_working_sets (profile_info
, gcov_working_sets
);
253 fprintf (dump_file
, "Counter working sets:\n");
254 /* Multiply the percentage by 100 to avoid float. */
255 pctinc
= 100 * 100 / NUM_GCOV_WORKING_SETS
;
256 for (ws_ix
= 0, pct
= pctinc
; ws_ix
< NUM_GCOV_WORKING_SETS
;
257 ws_ix
++, pct
+= pctinc
)
259 if (ws_ix
== NUM_GCOV_WORKING_SETS
- 1)
261 ws_info
= &gcov_working_sets
[ws_ix
];
262 /* Print out the percentage using int arithmatic to avoid float. */
263 fprintf (dump_file
, "\t\t%u.%02u%%: num counts=%u, min counter="
265 pct
/ 100, pct
- (pct
/ 100 * 100),
266 ws_info
->num_counters
,
267 (int64_t)ws_info
->min_counter
);
272 /* Given a the desired percentage of the full profile (sum_all from the
273 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
274 the corresponding working set information. If an exact match for
275 the percentage isn't found, the closest value is used. */
278 find_working_set (unsigned pct_times_10
)
283 gcc_assert (pct_times_10
<= 1000);
284 if (pct_times_10
>= 999)
285 return &gcov_working_sets
[NUM_GCOV_WORKING_SETS
- 1];
286 i
= pct_times_10
* NUM_GCOV_WORKING_SETS
/ 1000;
288 return &gcov_working_sets
[0];
289 return &gcov_working_sets
[i
- 1];
292 /* Computes hybrid profile for all matching entries in da_file.
294 CFG_CHECKSUM is the precomputed checksum for the CFG. */
297 get_exec_counts (unsigned cfg_checksum
, unsigned lineno_checksum
)
299 unsigned num_edges
= 0;
303 /* Count the edges to be (possibly) instrumented. */
304 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
309 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
310 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
314 counts
= get_coverage_counts (GCOV_COUNTER_ARCS
, num_edges
, cfg_checksum
,
315 lineno_checksum
, &profile_info
);
321 if (dump_file
&& profile_info
)
322 fprintf (dump_file
, "Merged %u profiles with maximal count %u.\n",
323 profile_info
->runs
, (unsigned) profile_info
->sum_max
);
330 is_edge_inconsistent (vec
<edge
, va_gc
> *edges
)
334 FOR_EACH_EDGE (e
, ei
, edges
)
336 if (!EDGE_INFO (e
)->ignore
)
339 && (!(e
->flags
& EDGE_FAKE
)
340 || !block_ends_with_call_p (e
->src
)))
345 "Edge %i->%i is inconsistent, count%" PRId64
,
346 e
->src
->index
, e
->dest
->index
, e
->count
);
347 dump_bb (dump_file
, e
->src
, 0, TDF_DETAILS
);
348 dump_bb (dump_file
, e
->dest
, 0, TDF_DETAILS
);
358 correct_negative_edge_counts (void)
364 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
366 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
374 /* Check consistency.
375 Return true if inconsistency is found. */
377 is_inconsistent (void)
380 bool inconsistent
= false;
381 FOR_EACH_BB_FN (bb
, cfun
)
383 inconsistent
|= is_edge_inconsistent (bb
->preds
);
384 if (!dump_file
&& inconsistent
)
386 inconsistent
|= is_edge_inconsistent (bb
->succs
);
387 if (!dump_file
&& inconsistent
)
393 fprintf (dump_file
, "BB %i count is negative "
397 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
401 if (bb
->count
!= sum_edge_counts (bb
->preds
))
405 fprintf (dump_file
, "BB %i count does not match sum of incoming edges "
406 "%" PRId64
" should be %" PRId64
,
409 sum_edge_counts (bb
->preds
));
410 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
414 if (bb
->count
!= sum_edge_counts (bb
->succs
) &&
415 ! (find_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
)) != NULL
416 && block_ends_with_call_p (bb
)))
420 fprintf (dump_file
, "BB %i count does not match sum of outgoing edges "
421 "%" PRId64
" should be %" PRId64
,
424 sum_edge_counts (bb
->succs
));
425 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
429 if (!dump_file
&& inconsistent
)
436 /* Set each basic block count to the sum of its outgoing edge counts */
441 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
443 bb
->count
= sum_edge_counts (bb
->succs
);
444 gcc_assert (bb
->count
>= 0);
448 /* Reads profile data and returns total number of edge counts read */
450 read_profile_edge_counts (gcov_type
*exec_counts
)
454 int exec_counts_pos
= 0;
455 /* For each edge not on the spanning tree, set its execution count from
457 /* The first count in the .da file is the number of times that the function
458 was entered. This is the exec_count for block zero. */
460 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
465 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
466 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
471 e
->count
= exec_counts
[exec_counts_pos
++];
472 if (e
->count
> profile_info
->sum_max
)
474 if (flag_profile_correction
)
476 static bool informed
= 0;
477 if (dump_enabled_p () && !informed
)
478 dump_printf_loc (MSG_NOTE
, input_location
,
479 "corrupted profile info: edge count"
480 " exceeds maximal count\n");
484 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
485 bb
->index
, e
->dest
->index
);
491 EDGE_INFO (e
)->count_valid
= 1;
492 BB_INFO (bb
)->succ_count
--;
493 BB_INFO (e
->dest
)->pred_count
--;
496 fprintf (dump_file
, "\nRead edge from %i to %i, count:",
497 bb
->index
, e
->dest
->index
);
498 fprintf (dump_file
, "%" PRId64
,
507 #define OVERLAP_BASE 10000
509 /* Compare the static estimated profile to the actual profile, and
510 return the "degree of overlap" measure between them.
512 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
513 the sum of each basic block's minimum relative weights between
514 two profiles. And overlap of OVERLAP_BASE means two profiles are
518 compute_frequency_overlap (void)
520 gcov_type count_total
= 0, freq_total
= 0;
524 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
526 count_total
+= bb
->count
;
527 freq_total
+= bb
->frequency
;
530 if (count_total
== 0 || freq_total
== 0)
533 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
534 overlap
+= MIN (bb
->count
* OVERLAP_BASE
/ count_total
,
535 bb
->frequency
* OVERLAP_BASE
/ freq_total
);
540 /* Compute the branch probabilities for the various branches.
541 Annotate them accordingly.
543 CFG_CHECKSUM is the precomputed checksum for the CFG. */
546 compute_branch_probabilities (unsigned cfg_checksum
, unsigned lineno_checksum
)
553 int hist_br_prob
[20];
555 gcov_type
*exec_counts
= get_exec_counts (cfg_checksum
, lineno_checksum
);
556 int inconsistent
= 0;
558 /* Very simple sanity checks so we catch bugs in our profiling code. */
562 if (profile_info
->sum_all
< profile_info
->sum_max
)
564 error ("corrupted profile info: sum_all is smaller than sum_max");
568 /* Attach extra info block to each bb. */
569 alloc_aux_for_blocks (sizeof (struct bb_profile_info
));
570 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
575 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
576 if (!EDGE_INFO (e
)->ignore
)
577 BB_INFO (bb
)->succ_count
++;
578 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
579 if (!EDGE_INFO (e
)->ignore
)
580 BB_INFO (bb
)->pred_count
++;
583 /* Avoid predicting entry on exit nodes. */
584 BB_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun
))->succ_count
= 2;
585 BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (cfun
))->pred_count
= 2;
587 num_edges
= read_profile_edge_counts (exec_counts
);
590 fprintf (dump_file
, "\n%d edge counts read\n", num_edges
);
592 /* For every block in the file,
593 - if every exit/entrance edge has a known count, then set the block count
594 - if the block count is known, and every exit/entrance edge but one has
595 a known execution count, then set the count of the remaining edge
597 As edge counts are set, decrement the succ/pred count, but don't delete
598 the edge, that way we can easily tell when all edges are known, or only
599 one edge is unknown. */
601 /* The order that the basic blocks are iterated through is important.
602 Since the code that finds spanning trees starts with block 0, low numbered
603 edges are put on the spanning tree in preference to high numbered edges.
604 Hence, most instrumented edges are at the end. Graph solving works much
605 faster if we propagate numbers from the end to the start.
607 This takes an average of slightly more than 3 passes. */
615 FOR_BB_BETWEEN (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), NULL
, prev_bb
)
617 struct bb_profile_info
*bi
= BB_INFO (bb
);
618 if (! bi
->count_valid
)
620 if (bi
->succ_count
== 0)
626 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
632 else if (bi
->pred_count
== 0)
638 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
647 if (bi
->succ_count
== 1)
653 /* One of the counts will be invalid, but it is zero,
654 so adding it in also doesn't hurt. */
655 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
658 /* Search for the invalid edge, and set its count. */
659 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
660 if (! EDGE_INFO (e
)->count_valid
&& ! EDGE_INFO (e
)->ignore
)
663 /* Calculate count for remaining edge by conservation. */
664 total
= bb
->count
- total
;
667 EDGE_INFO (e
)->count_valid
= 1;
671 BB_INFO (e
->dest
)->pred_count
--;
674 if (bi
->pred_count
== 1)
680 /* One of the counts will be invalid, but it is zero,
681 so adding it in also doesn't hurt. */
682 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
685 /* Search for the invalid edge, and set its count. */
686 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
687 if (!EDGE_INFO (e
)->count_valid
&& !EDGE_INFO (e
)->ignore
)
690 /* Calculate count for remaining edge by conservation. */
691 total
= bb
->count
- total
+ e
->count
;
694 EDGE_INFO (e
)->count_valid
= 1;
698 BB_INFO (e
->src
)->succ_count
--;
706 int overlap
= compute_frequency_overlap ();
707 gimple_dump_cfg (dump_file
, dump_flags
);
708 fprintf (dump_file
, "Static profile overlap: %d.%d%%\n",
709 overlap
/ (OVERLAP_BASE
/ 100),
710 overlap
% (OVERLAP_BASE
/ 100));
713 total_num_passes
+= passes
;
715 fprintf (dump_file
, "Graph solving took %d passes.\n\n", passes
);
717 /* If the graph has been correctly solved, every block will have a
718 succ and pred count of zero. */
719 FOR_EACH_BB_FN (bb
, cfun
)
721 gcc_assert (!BB_INFO (bb
)->succ_count
&& !BB_INFO (bb
)->pred_count
);
724 /* Check for inconsistent basic block counts */
725 inconsistent
= is_inconsistent ();
729 if (flag_profile_correction
)
731 /* Inconsistency detected. Make it flow-consistent. */
732 static int informed
= 0;
733 if (dump_enabled_p () && informed
== 0)
736 dump_printf_loc (MSG_NOTE
, input_location
,
737 "correcting inconsistent profile data\n");
739 correct_negative_edge_counts ();
740 /* Set bb counts to the sum of the outgoing edge counts */
743 fprintf (dump_file
, "\nCalling mcf_smooth_cfg\n");
747 error ("corrupted profile info: profile data is not flow-consistent");
750 /* For every edge, calculate its branch probability and add a reg_note
751 to the branch insn to indicate this. */
753 for (i
= 0; i
< 20; i
++)
757 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
764 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
765 bb
->index
, (int)bb
->count
);
768 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
770 /* Function may return twice in the cased the called function is
771 setjmp or calls fork, but we can't represent this by extra
772 edge from the entry, since extra edge from the exit is
773 already present. We get negative frequency from the entry
776 && e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
777 || (e
->count
> bb
->count
778 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)))
780 if (block_ends_with_call_p (bb
))
781 e
->count
= e
->count
< 0 ? 0 : bb
->count
;
783 if (e
->count
< 0 || e
->count
> bb
->count
)
785 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
786 e
->src
->index
, e
->dest
->index
,
788 e
->count
= bb
->count
/ 2;
793 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
794 e
->probability
= GCOV_COMPUTE_SCALE (e
->count
, bb
->count
);
795 if (bb
->index
>= NUM_FIXED_BLOCKS
796 && block_ends_with_condjump_p (bb
)
797 && EDGE_COUNT (bb
->succs
) >= 2)
803 /* Find the branch edge. It is possible that we do have fake
805 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
806 if (!(e
->flags
& (EDGE_FAKE
| EDGE_FALLTHRU
)))
809 prob
= e
->probability
;
810 index
= prob
* 20 / REG_BR_PROB_BASE
;
814 hist_br_prob
[index
]++;
819 /* As a last resort, distribute the probabilities evenly.
820 Use simple heuristics that if there are normal edges,
821 give all abnormals frequency of 0, otherwise distribute the
822 frequency over abnormals (this is the case of noreturn
824 else if (profile_status_for_fn (cfun
) == PROFILE_ABSENT
)
828 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
829 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
833 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
834 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
835 e
->probability
= REG_BR_PROB_BASE
/ total
;
841 total
+= EDGE_COUNT (bb
->succs
);
842 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
843 e
->probability
= REG_BR_PROB_BASE
/ total
;
845 if (bb
->index
>= NUM_FIXED_BLOCKS
846 && block_ends_with_condjump_p (bb
)
847 && EDGE_COUNT (bb
->succs
) >= 2)
852 profile_status_for_fn (cfun
) = PROFILE_READ
;
853 compute_function_frequency ();
857 fprintf (dump_file
, "%d branches\n", num_branches
);
859 for (i
= 0; i
< 10; i
++)
860 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
861 (hist_br_prob
[i
] + hist_br_prob
[19-i
]) * 100 / num_branches
,
864 total_num_branches
+= num_branches
;
865 for (i
= 0; i
< 20; i
++)
866 total_hist_br_prob
[i
] += hist_br_prob
[i
];
868 fputc ('\n', dump_file
);
869 fputc ('\n', dump_file
);
872 free_aux_for_blocks ();
875 /* Load value histograms values whose description is stored in VALUES array
878 CFG_CHECKSUM is the precomputed checksum for the CFG. */
881 compute_value_histograms (histogram_values values
, unsigned cfg_checksum
,
882 unsigned lineno_checksum
)
884 unsigned i
, j
, t
, any
;
885 unsigned n_histogram_counters
[GCOV_N_VALUE_COUNTERS
];
886 gcov_type
*histogram_counts
[GCOV_N_VALUE_COUNTERS
];
887 gcov_type
*act_count
[GCOV_N_VALUE_COUNTERS
];
888 gcov_type
*aact_count
;
889 struct cgraph_node
*node
;
891 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
892 n_histogram_counters
[t
] = 0;
894 for (i
= 0; i
< values
.length (); i
++)
896 histogram_value hist
= values
[i
];
897 n_histogram_counters
[(int) hist
->type
] += hist
->n_counters
;
901 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
903 if (!n_histogram_counters
[t
])
905 histogram_counts
[t
] = NULL
;
909 histogram_counts
[t
] =
910 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t
),
911 n_histogram_counters
[t
], cfg_checksum
,
912 lineno_checksum
, NULL
);
913 if (histogram_counts
[t
])
915 act_count
[t
] = histogram_counts
[t
];
920 for (i
= 0; i
< values
.length (); i
++)
922 histogram_value hist
= values
[i
];
923 gimple stmt
= hist
->hvalue
.stmt
;
925 t
= (int) hist
->type
;
927 aact_count
= act_count
[t
];
930 act_count
[t
] += hist
->n_counters
;
932 gimple_add_histogram_value (cfun
, stmt
, hist
);
933 hist
->hvalue
.counters
= XNEWVEC (gcov_type
, hist
->n_counters
);
934 for (j
= 0; j
< hist
->n_counters
; j
++)
936 hist
->hvalue
.counters
[j
] = aact_count
[j
];
938 hist
->hvalue
.counters
[j
] = 0;
940 /* Time profiler counter is not related to any statement,
941 so that we have to read the counter and set the value to
942 the corresponding call graph node. */
943 if (hist
->type
== HIST_TYPE_TIME_PROFILE
)
945 node
= cgraph_node::get (hist
->fun
->decl
);
946 node
->tp_first_run
= hist
->hvalue
.counters
[0];
949 fprintf (dump_file
, "Read tp_first_run: %d\n", node
->tp_first_run
);
953 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
954 free (histogram_counts
[t
]);
957 /* When passed NULL as file_name, initialize.
958 When passed something else, output the necessary commands to change
959 line to LINE and offset to FILE_NAME. */
961 output_location (char const *file_name
, int line
,
962 gcov_position_t
*offset
, basic_block bb
)
964 static char const *prev_file_name
;
965 static int prev_line
;
966 bool name_differs
, line_differs
;
970 prev_file_name
= NULL
;
975 name_differs
= !prev_file_name
|| filename_cmp (file_name
, prev_file_name
);
976 line_differs
= prev_line
!= line
;
978 if (name_differs
|| line_differs
)
982 *offset
= gcov_write_tag (GCOV_TAG_LINES
);
983 gcov_write_unsigned (bb
->index
);
984 name_differs
= line_differs
=true;
987 /* If this is a new source file, then output the
988 file's name to the .bb file. */
991 prev_file_name
= file_name
;
992 gcov_write_unsigned (0);
993 gcov_write_string (prev_file_name
);
997 gcov_write_unsigned (line
);
1003 /* Instrument and/or analyze program behavior based on program the CFG.
1005 This function creates a representation of the control flow graph (of
1006 the function being compiled) that is suitable for the instrumentation
1007 of edges and/or converting measured edge counts to counts on the
1010 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
1011 the flow graph that are needed to reconstruct the dynamic behavior of the
1012 flow graph. This data is written to the gcno file for gcov.
1014 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
1015 information from the gcda file containing edge count information from
1016 previous executions of the function being compiled. In this case, the
1017 control flow graph is annotated with actual execution counts by
1018 compute_branch_probabilities().
1020 Main entry point of this file. */
1027 unsigned num_edges
, ignored_edges
;
1028 unsigned num_instrumented
;
1029 struct edge_list
*el
;
1030 histogram_values values
= histogram_values ();
1031 unsigned cfg_checksum
, lineno_checksum
;
1033 total_num_times_called
++;
1035 flow_call_edges_add (NULL
);
1036 add_noreturn_fake_exit_edges ();
1038 /* We can't handle cyclic regions constructed using abnormal edges.
1039 To avoid these we replace every source of abnormal edge by a fake
1040 edge from entry node and every destination by fake edge to exit.
1041 This keeps graph acyclic and our calculation exact for all normal
1042 edges except for exit and entrance ones.
1044 We also add fake exit edges for each call and asm statement in the
1045 basic, since it may not return. */
1047 FOR_EACH_BB_FN (bb
, cfun
)
1049 int need_exit_edge
= 0, need_entry_edge
= 0;
1050 int have_exit_edge
= 0, have_entry_edge
= 0;
1054 /* Functions returning multiple times are not handled by extra edges.
1055 Instead we simply allow negative counts on edges from exit to the
1056 block past call and corresponding probabilities. We can't go
1057 with the extra edges because that would result in flowgraph that
1058 needs to have fake edges outside the spanning tree. */
1060 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1062 gimple_stmt_iterator gsi
;
1065 /* It may happen that there are compiler generated statements
1066 without a locus at all. Go through the basic block from the
1067 last to the first statement looking for a locus. */
1068 for (gsi
= gsi_last_nondebug_bb (bb
);
1070 gsi_prev_nondebug (&gsi
))
1072 last
= gsi_stmt (gsi
);
1073 if (gimple_has_location (last
))
1077 /* Edge with goto locus might get wrong coverage info unless
1078 it is the only edge out of BB.
1079 Don't do that when the locuses match, so
1080 if (blah) goto something;
1081 is not computed twice. */
1083 && gimple_has_location (last
)
1084 && LOCATION_LOCUS (e
->goto_locus
) != UNKNOWN_LOCATION
1085 && !single_succ_p (bb
)
1086 && (LOCATION_FILE (e
->goto_locus
)
1087 != LOCATION_FILE (gimple_location (last
))
1088 || (LOCATION_LINE (e
->goto_locus
)
1089 != LOCATION_LINE (gimple_location (last
)))))
1091 basic_block new_bb
= split_edge (e
);
1092 edge ne
= single_succ_edge (new_bb
);
1093 ne
->goto_locus
= e
->goto_locus
;
1095 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1096 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1098 if (e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1101 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1103 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1104 && e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1105 need_entry_edge
= 1;
1106 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1107 have_entry_edge
= 1;
1110 if (need_exit_edge
&& !have_exit_edge
)
1113 fprintf (dump_file
, "Adding fake exit edge to bb %i\n",
1115 make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), EDGE_FAKE
);
1117 if (need_entry_edge
&& !have_entry_edge
)
1120 fprintf (dump_file
, "Adding fake entry edge to bb %i\n",
1122 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FAKE
);
1123 /* Avoid bbs that have both fake entry edge and also some
1124 exit edge. One of those edges wouldn't be added to the
1125 spanning tree, but we can't instrument any of them. */
1126 if (have_exit_edge
|| need_exit_edge
)
1128 gimple_stmt_iterator gsi
;
1131 gsi
= gsi_start_nondebug_after_labels_bb (bb
);
1132 gcc_checking_assert (!gsi_end_p (gsi
));
1133 first
= gsi_stmt (gsi
);
1134 /* Don't split the bbs containing __builtin_setjmp_receiver
1135 or ABNORMAL_DISPATCHER calls. These are very
1136 special and don't expect anything to be inserted before
1138 if (is_gimple_call (first
)
1139 && (gimple_call_builtin_p (first
, BUILT_IN_SETJMP_RECEIVER
)
1140 || (gimple_call_flags (first
) & ECF_RETURNS_TWICE
)
1141 || (gimple_call_internal_p (first
)
1142 && (gimple_call_internal_fn (first
)
1143 == IFN_ABNORMAL_DISPATCHER
))))
1147 fprintf (dump_file
, "Splitting bb %i after labels\n",
1149 split_block_after_labels (bb
);
1154 el
= create_edge_list ();
1155 num_edges
= NUM_EDGES (el
);
1156 alloc_aux_for_edges (sizeof (struct edge_profile_info
));
1158 /* The basic blocks are expected to be numbered sequentially. */
1162 for (i
= 0 ; i
< num_edges
; i
++)
1164 edge e
= INDEX_EDGE (el
, i
);
1167 /* Mark edges we've replaced by fake edges above as ignored. */
1168 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1169 && e
->src
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
)
1170 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1172 EDGE_INFO (e
)->ignore
= 1;
1177 /* Create spanning tree from basic block graph, mark each edge that is
1178 on the spanning tree. We insert as many abnormal and critical edges
1179 as possible to minimize number of edge splits necessary. */
1181 find_spanning_tree (el
);
1183 /* Fake edges that are not on the tree will not be instrumented, so
1184 mark them ignored. */
1185 for (num_instrumented
= i
= 0; i
< num_edges
; i
++)
1187 edge e
= INDEX_EDGE (el
, i
);
1188 struct edge_profile_info
*inf
= EDGE_INFO (e
);
1190 if (inf
->ignore
|| inf
->on_tree
)
1192 else if (e
->flags
& EDGE_FAKE
)
1201 total_num_blocks
+= n_basic_blocks_for_fn (cfun
);
1203 fprintf (dump_file
, "%d basic blocks\n", n_basic_blocks_for_fn (cfun
));
1205 total_num_edges
+= num_edges
;
1207 fprintf (dump_file
, "%d edges\n", num_edges
);
1209 total_num_edges_ignored
+= ignored_edges
;
1211 fprintf (dump_file
, "%d ignored edges\n", ignored_edges
);
1213 total_num_edges_instrumented
+= num_instrumented
;
1215 fprintf (dump_file
, "%d instrumentation edges\n", num_instrumented
);
1217 /* Compute two different checksums. Note that we want to compute
1218 the checksum in only once place, since it depends on the shape
1219 of the control flow which can change during
1220 various transformations. */
1221 cfg_checksum
= coverage_compute_cfg_checksum (cfun
);
1222 lineno_checksum
= coverage_compute_lineno_checksum ();
1224 /* Write the data from which gcov can reconstruct the basic block
1225 graph and function line numbers (the gcno file). */
1226 if (coverage_begin_function (lineno_checksum
, cfg_checksum
))
1228 gcov_position_t offset
;
1230 /* Basic block flags */
1231 offset
= gcov_write_tag (GCOV_TAG_BLOCKS
);
1232 for (i
= 0; i
!= (unsigned) (n_basic_blocks_for_fn (cfun
)); i
++)
1233 gcov_write_unsigned (0);
1234 gcov_write_length (offset
);
1237 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
),
1238 EXIT_BLOCK_PTR_FOR_FN (cfun
), next_bb
)
1243 offset
= gcov_write_tag (GCOV_TAG_ARCS
);
1244 gcov_write_unsigned (bb
->index
);
1246 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1248 struct edge_profile_info
*i
= EDGE_INFO (e
);
1251 unsigned flag_bits
= 0;
1254 flag_bits
|= GCOV_ARC_ON_TREE
;
1255 if (e
->flags
& EDGE_FAKE
)
1256 flag_bits
|= GCOV_ARC_FAKE
;
1257 if (e
->flags
& EDGE_FALLTHRU
)
1258 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1259 /* On trees we don't have fallthru flags, but we can
1260 recompute them from CFG shape. */
1261 if (e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)
1262 && e
->src
->next_bb
== e
->dest
)
1263 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1265 gcov_write_unsigned (e
->dest
->index
);
1266 gcov_write_unsigned (flag_bits
);
1270 gcov_write_length (offset
);
1274 /* Initialize the output. */
1275 output_location (NULL
, 0, NULL
, NULL
);
1277 FOR_EACH_BB_FN (bb
, cfun
)
1279 gimple_stmt_iterator gsi
;
1280 gcov_position_t offset
= 0;
1282 if (bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
)
1284 expanded_location curr_location
=
1285 expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
1286 output_location (curr_location
.file
, curr_location
.line
,
1290 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1292 gimple stmt
= gsi_stmt (gsi
);
1293 if (gimple_has_location (stmt
))
1294 output_location (gimple_filename (stmt
), gimple_lineno (stmt
),
1298 /* Notice GOTO expressions eliminated while constructing the CFG. */
1299 if (single_succ_p (bb
)
1300 && LOCATION_LOCUS (single_succ_edge (bb
)->goto_locus
)
1301 != UNKNOWN_LOCATION
)
1303 expanded_location curr_location
1304 = expand_location (single_succ_edge (bb
)->goto_locus
);
1305 output_location (curr_location
.file
, curr_location
.line
,
1311 /* A file of NULL indicates the end of run. */
1312 gcov_write_unsigned (0);
1313 gcov_write_string (NULL
);
1314 gcov_write_length (offset
);
1319 if (flag_profile_values
)
1320 gimple_find_values_to_profile (&values
);
1322 if (flag_branch_probabilities
)
1324 compute_branch_probabilities (cfg_checksum
, lineno_checksum
);
1325 if (flag_profile_values
)
1326 compute_value_histograms (values
, cfg_checksum
, lineno_checksum
);
1329 remove_fake_edges ();
1331 /* For each edge not on the spanning tree, add counting code. */
1332 if (profile_arc_flag
1333 && coverage_counter_alloc (GCOV_COUNTER_ARCS
, num_instrumented
))
1335 unsigned n_instrumented
;
1337 gimple_init_edge_profiler ();
1339 n_instrumented
= instrument_edges (el
);
1341 gcc_assert (n_instrumented
== num_instrumented
);
1343 if (flag_profile_values
)
1344 instrument_values (values
);
1346 /* Commit changes done by instrumentation. */
1347 gsi_commit_edge_inserts ();
1350 free_aux_for_edges ();
1353 free_edge_list (el
);
1354 coverage_end_function (lineno_checksum
, cfg_checksum
);
1357 /* Union find algorithm implementation for the basic blocks using
1361 find_group (basic_block bb
)
1363 basic_block group
= bb
, bb1
;
1365 while ((basic_block
) group
->aux
!= group
)
1366 group
= (basic_block
) group
->aux
;
1368 /* Compress path. */
1369 while ((basic_block
) bb
->aux
!= group
)
1371 bb1
= (basic_block
) bb
->aux
;
1372 bb
->aux
= (void *) group
;
1379 union_groups (basic_block bb1
, basic_block bb2
)
1381 basic_block bb1g
= find_group (bb1
);
1382 basic_block bb2g
= find_group (bb2
);
1384 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1385 this code is unlikely going to be performance problem anyway. */
1386 gcc_assert (bb1g
!= bb2g
);
1391 /* This function searches all of the edges in the program flow graph, and puts
1392 as many bad edges as possible onto the spanning tree. Bad edges include
1393 abnormals edges, which can't be instrumented at the moment. Since it is
1394 possible for fake edges to form a cycle, we will have to develop some
1395 better way in the future. Also put critical edges to the tree, since they
1396 are more expensive to instrument. */
1399 find_spanning_tree (struct edge_list
*el
)
1402 int num_edges
= NUM_EDGES (el
);
1405 /* We use aux field for standard union-find algorithm. */
1406 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), NULL
, next_bb
)
1409 /* Add fake edge exit to entry we can't instrument. */
1410 union_groups (EXIT_BLOCK_PTR_FOR_FN (cfun
), ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1412 /* First add all abnormal edges to the tree unless they form a cycle. Also
1413 add all edges to the exit block to avoid inserting profiling code behind
1414 setting return value from function. */
1415 for (i
= 0; i
< num_edges
; i
++)
1417 edge e
= INDEX_EDGE (el
, i
);
1418 if (((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
))
1419 || e
->dest
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1420 && !EDGE_INFO (e
)->ignore
1421 && (find_group (e
->src
) != find_group (e
->dest
)))
1424 fprintf (dump_file
, "Abnormal edge %d to %d put to tree\n",
1425 e
->src
->index
, e
->dest
->index
);
1426 EDGE_INFO (e
)->on_tree
= 1;
1427 union_groups (e
->src
, e
->dest
);
1431 /* Now insert all critical edges to the tree unless they form a cycle. */
1432 for (i
= 0; i
< num_edges
; i
++)
1434 edge e
= INDEX_EDGE (el
, i
);
1435 if (EDGE_CRITICAL_P (e
) && !EDGE_INFO (e
)->ignore
1436 && find_group (e
->src
) != find_group (e
->dest
))
1439 fprintf (dump_file
, "Critical edge %d to %d put to tree\n",
1440 e
->src
->index
, e
->dest
->index
);
1441 EDGE_INFO (e
)->on_tree
= 1;
1442 union_groups (e
->src
, e
->dest
);
1446 /* And now the rest. */
1447 for (i
= 0; i
< num_edges
; i
++)
1449 edge e
= INDEX_EDGE (el
, i
);
1450 if (!EDGE_INFO (e
)->ignore
1451 && find_group (e
->src
) != find_group (e
->dest
))
1454 fprintf (dump_file
, "Normal edge %d to %d put to tree\n",
1455 e
->src
->index
, e
->dest
->index
);
1456 EDGE_INFO (e
)->on_tree
= 1;
1457 union_groups (e
->src
, e
->dest
);
1461 clear_aux_for_blocks ();
1464 /* Perform file-level initialization for branch-prob processing. */
1467 init_branch_prob (void)
1471 total_num_blocks
= 0;
1472 total_num_edges
= 0;
1473 total_num_edges_ignored
= 0;
1474 total_num_edges_instrumented
= 0;
1475 total_num_blocks_created
= 0;
1476 total_num_passes
= 0;
1477 total_num_times_called
= 0;
1478 total_num_branches
= 0;
1479 for (i
= 0; i
< 20; i
++)
1480 total_hist_br_prob
[i
] = 0;
1483 /* Performs file-level cleanup after branch-prob processing
1487 end_branch_prob (void)
1491 fprintf (dump_file
, "\n");
1492 fprintf (dump_file
, "Total number of blocks: %d\n",
1494 fprintf (dump_file
, "Total number of edges: %d\n", total_num_edges
);
1495 fprintf (dump_file
, "Total number of ignored edges: %d\n",
1496 total_num_edges_ignored
);
1497 fprintf (dump_file
, "Total number of instrumented edges: %d\n",
1498 total_num_edges_instrumented
);
1499 fprintf (dump_file
, "Total number of blocks created: %d\n",
1500 total_num_blocks_created
);
1501 fprintf (dump_file
, "Total number of graph solution passes: %d\n",
1503 if (total_num_times_called
!= 0)
1504 fprintf (dump_file
, "Average number of graph solution passes: %d\n",
1505 (total_num_passes
+ (total_num_times_called
>> 1))
1506 / total_num_times_called
);
1507 fprintf (dump_file
, "Total number of branches: %d\n",
1508 total_num_branches
);
1509 if (total_num_branches
)
1513 for (i
= 0; i
< 10; i
++)
1514 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
1515 (total_hist_br_prob
[i
] + total_hist_br_prob
[19-i
]) * 100
1516 / total_num_branches
, 5*i
, 5*i
+5);