1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
6 based on some ideas from Dain Samples of UC Berkeley.
7 Further mangling by Bob Manson, Cygnus Support.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* Generate basic block profile instrumentation and auxiliary files.
26 Profile generation is optimized, so that not all arcs in the basic
27 block graph need instrumenting. First, the BB graph is closed with
28 one entry (function start), and one exit (function exit). Any
29 ABNORMAL_EDGE cannot be instrumented (because there is no control
30 path to place the code). We close the graph by inserting fake
31 EDGE_FAKE edges to the EXIT_BLOCK, from the sources of abnormal
32 edges that do not go to the exit_block. We ignore such abnormal
33 edges. Naturally these fake edges are never directly traversed,
34 and so *cannot* be directly instrumented. Some other graph
35 massaging is done. To optimize the instrumentation we generate the
36 BB minimal span tree, only edges that are not on the span tree
37 (plus the entry point) need instrumenting. From that information
38 all other edge counts can be deduced. By construction all fake
39 edges must be on the spanning tree. We also attempt to place
40 EDGE_CRITICAL edges on the spanning tree.
42 The auxiliary files generated are <dumpbase>.gcno (at compile time)
43 and <dumpbase>.gcda (at run time). The format is
44 described in full in gcov-io.h. */
46 /* ??? Register allocation should use basic block execution counts to
47 give preference to the most commonly executed blocks. */
49 /* ??? Should calculate branch probabilities before instrumenting code, since
50 then we can use arc counts to help decide which arcs to instrument. */
54 #include "coretypes.h"
61 #include "basic-block.h"
62 #include "diagnostic-core.h"
64 #include "value-prof.h"
66 #include "tree-flow.h"
73 unsigned int count_valid
: 1;
75 /* Number of successor and predecessor edges. */
80 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
83 /* Counter summary from the last set of coverage counts read. */
85 const struct gcov_ctr_summary
*profile_info
;
87 /* Number of data points in the working set summary array. Using 128
88 provides information for at least every 1% increment of the total
89 profile size. The last entry is hardwired to 99.9% of the total. */
90 #define NUM_GCOV_WORKING_SETS 128
92 /* Counter working set information computed from the current counter
93 summary. Not initialized unless profile_info summary is non-NULL. */
94 static gcov_working_set_t gcov_working_sets
[NUM_GCOV_WORKING_SETS
];
96 /* Collect statistics on the performance of this pass for the entire source
99 static int total_num_blocks
;
100 static int total_num_edges
;
101 static int total_num_edges_ignored
;
102 static int total_num_edges_instrumented
;
103 static int total_num_blocks_created
;
104 static int total_num_passes
;
105 static int total_num_times_called
;
106 static int total_hist_br_prob
[20];
107 static int total_num_branches
;
109 /* Forward declarations. */
110 static void find_spanning_tree (struct edge_list
*);
112 /* Add edge instrumentation code to the entire insn chain.
114 F is the first insn of the chain.
115 NUM_BLOCKS is the number of basic blocks found in F. */
118 instrument_edges (struct edge_list
*el
)
120 unsigned num_instr_edges
= 0;
121 int num_edges
= NUM_EDGES (el
);
124 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
129 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
131 struct edge_info
*inf
= EDGE_INFO (e
);
133 if (!inf
->ignore
&& !inf
->on_tree
)
135 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
137 fprintf (dump_file
, "Edge %d to %d instrumented%s\n",
138 e
->src
->index
, e
->dest
->index
,
139 EDGE_CRITICAL_P (e
) ? " (and split)" : "");
140 gimple_gen_edge_profiler (num_instr_edges
++, e
);
145 total_num_blocks_created
+= num_edges
;
147 fprintf (dump_file
, "%d edges instrumented\n", num_instr_edges
);
148 return num_instr_edges
;
151 /* Add code to measure histograms for values in list VALUES. */
153 instrument_values (histogram_values values
)
157 /* Emit code to generate the histograms before the insns. */
159 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
161 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
162 unsigned t
= COUNTER_FOR_HIST_TYPE (hist
->type
);
164 if (!coverage_counter_alloc (t
, hist
->n_counters
))
169 case HIST_TYPE_INTERVAL
:
170 gimple_gen_interval_profiler (hist
, t
, 0);
174 gimple_gen_pow2_profiler (hist
, t
, 0);
177 case HIST_TYPE_SINGLE_VALUE
:
178 gimple_gen_one_value_profiler (hist
, t
, 0);
181 case HIST_TYPE_CONST_DELTA
:
182 gimple_gen_const_delta_profiler (hist
, t
, 0);
185 case HIST_TYPE_INDIR_CALL
:
186 gimple_gen_ic_profiler (hist
, t
, 0);
189 case HIST_TYPE_AVERAGE
:
190 gimple_gen_average_profiler (hist
, t
, 0);
194 gimple_gen_ior_profiler (hist
, t
, 0);
204 /* Compute the working set information from the counter histogram in
205 the profile summary. This is an array of information corresponding to a
206 range of percentages of the total execution count (sum_all), and includes
207 the number of counters required to cover that working set percentage and
208 the minimum counter value in that working set. */
211 compute_working_sets (void)
213 gcov_type working_set_cum_values
[NUM_GCOV_WORKING_SETS
];
214 gcov_type ws_cum_hotness_incr
;
215 gcov_type cum
, tmp_cum
;
216 const gcov_bucket_type
*histo_bucket
;
217 unsigned ws_ix
, c_num
, count
, pctinc
, pct
;
219 gcov_working_set_t
*ws_info
;
224 /* Compute the amount of sum_all that the cumulative hotness grows
225 by in each successive working set entry, which depends on the
226 number of working set entries. */
227 ws_cum_hotness_incr
= profile_info
->sum_all
/ NUM_GCOV_WORKING_SETS
;
229 /* Next fill in an array of the cumulative hotness values corresponding
230 to each working set summary entry we are going to compute below.
231 Skip 0% statistics, which can be extrapolated from the
232 rest of the summary data. */
233 cum
= ws_cum_hotness_incr
;
234 for (ws_ix
= 0; ws_ix
< NUM_GCOV_WORKING_SETS
;
235 ws_ix
++, cum
+= ws_cum_hotness_incr
)
236 working_set_cum_values
[ws_ix
] = cum
;
237 /* The last summary entry is reserved for (roughly) 99.9% of the
238 working set. Divide by 1024 so it becomes a shift, which gives
239 almost exactly 99.9%. */
240 working_set_cum_values
[NUM_GCOV_WORKING_SETS
-1]
241 = profile_info
->sum_all
- profile_info
->sum_all
/1024;
243 /* Next, walk through the histogram in decending order of hotness
244 and compute the statistics for the working set summary array.
245 As histogram entries are accumulated, we check to see which
246 working set entries have had their expected cum_value reached
247 and fill them in, walking the working set entries in increasing
248 size of cum_value. */
249 ws_ix
= 0; /* The current entry into the working set array. */
250 cum
= 0; /* The current accumulated counter sum. */
251 count
= 0; /* The current accumulated count of block counters. */
252 for (h_ix
= GCOV_HISTOGRAM_SIZE
- 1;
253 h_ix
>= 0 && ws_ix
< NUM_GCOV_WORKING_SETS
; h_ix
--)
255 histo_bucket
= &profile_info
->histogram
[h_ix
];
257 /* If we haven't reached the required cumulative counter value for
258 the current working set percentage, simply accumulate this histogram
259 entry into the running sums and continue to the next histogram
261 if (cum
+ histo_bucket
->cum_value
< working_set_cum_values
[ws_ix
])
263 cum
+= histo_bucket
->cum_value
;
264 count
+= histo_bucket
->num_counters
;
268 /* If adding the current histogram entry's cumulative counter value
269 causes us to exceed the current working set size, then estimate
270 how many of this histogram entry's counter values are required to
271 reach the working set size, and fill in working set entries
272 as we reach their expected cumulative value. */
273 for (c_num
= 0, tmp_cum
= cum
;
274 c_num
< histo_bucket
->num_counters
&& ws_ix
< NUM_GCOV_WORKING_SETS
;
278 /* If we haven't reached the last histogram entry counter, add
279 in the minimum value again. This will underestimate the
280 cumulative sum so far, because many of the counter values in this
281 entry may have been larger than the minimum. We could add in the
282 average value every time, but that would require an expensive
284 if (c_num
+ 1 < histo_bucket
->num_counters
)
285 tmp_cum
+= histo_bucket
->min_value
;
286 /* If we have reached the last histogram entry counter, then add
287 in the entire cumulative value. */
289 tmp_cum
= cum
+ histo_bucket
->cum_value
;
291 /* Next walk through successive working set entries and fill in
292 the statistics for any whose size we have reached by accumulating
293 this histogram counter. */
294 while (tmp_cum
>= working_set_cum_values
[ws_ix
]
295 && ws_ix
< NUM_GCOV_WORKING_SETS
)
297 gcov_working_sets
[ws_ix
].num_counters
= count
;
298 gcov_working_sets
[ws_ix
].min_counter
299 = histo_bucket
->min_value
;
303 /* Finally, update the running cumulative value since we were
304 using a temporary above. */
305 cum
+= histo_bucket
->cum_value
;
307 gcc_assert (ws_ix
== NUM_GCOV_WORKING_SETS
);
311 fprintf (dump_file
, "Counter working sets:\n");
312 /* Multiply the percentage by 100 to avoid float. */
313 pctinc
= 100 * 100 / NUM_GCOV_WORKING_SETS
;
314 for (ws_ix
= 0, pct
= pctinc
; ws_ix
< NUM_GCOV_WORKING_SETS
;
315 ws_ix
++, pct
+= pctinc
)
317 if (ws_ix
== NUM_GCOV_WORKING_SETS
- 1)
319 ws_info
= &gcov_working_sets
[ws_ix
];
320 /* Print out the percentage using int arithmatic to avoid float. */
321 fprintf (dump_file
, "\t\t%u.%02u%%: num counts=%u, min counter="
322 HOST_WIDEST_INT_PRINT_DEC
"\n",
323 pct
/ 100, pct
- (pct
/ 100 * 100),
324 ws_info
->num_counters
,
325 (HOST_WIDEST_INT
)ws_info
->min_counter
);
330 /* Given a the desired percentage of the full profile (sum_all from the
331 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
332 the corresponding working set information. If an exact match for
333 the percentage isn't found, the closest value is used. */
336 find_working_set (unsigned pct_times_10
)
341 gcc_assert (pct_times_10
<= 1000);
342 if (pct_times_10
>= 999)
343 return &gcov_working_sets
[NUM_GCOV_WORKING_SETS
- 1];
344 i
= pct_times_10
* NUM_GCOV_WORKING_SETS
/ 1000;
346 return &gcov_working_sets
[0];
347 return &gcov_working_sets
[i
- 1];
350 /* Computes hybrid profile for all matching entries in da_file.
352 CFG_CHECKSUM is the precomputed checksum for the CFG. */
355 get_exec_counts (unsigned cfg_checksum
, unsigned lineno_checksum
)
357 unsigned num_edges
= 0;
361 /* Count the edges to be (possibly) instrumented. */
362 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
367 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
368 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
372 counts
= get_coverage_counts (GCOV_COUNTER_ARCS
, num_edges
, cfg_checksum
,
373 lineno_checksum
, &profile_info
);
377 compute_working_sets();
379 if (dump_file
&& profile_info
)
380 fprintf(dump_file
, "Merged %u profiles with maximal count %u.\n",
381 profile_info
->runs
, (unsigned) profile_info
->sum_max
);
388 is_edge_inconsistent (VEC(edge
,gc
) *edges
)
392 FOR_EACH_EDGE (e
, ei
, edges
)
394 if (!EDGE_INFO (e
)->ignore
)
397 && (!(e
->flags
& EDGE_FAKE
)
398 || !block_ends_with_call_p (e
->src
)))
403 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC
,
404 e
->src
->index
, e
->dest
->index
, e
->count
);
405 dump_bb (dump_file
, e
->src
, 0, TDF_DETAILS
);
406 dump_bb (dump_file
, e
->dest
, 0, TDF_DETAILS
);
416 correct_negative_edge_counts (void)
422 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
424 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
432 /* Check consistency.
433 Return true if inconsistency is found. */
435 is_inconsistent (void)
438 bool inconsistent
= false;
441 inconsistent
|= is_edge_inconsistent (bb
->preds
);
442 if (!dump_file
&& inconsistent
)
444 inconsistent
|= is_edge_inconsistent (bb
->succs
);
445 if (!dump_file
&& inconsistent
)
451 fprintf (dump_file
, "BB %i count is negative "
452 HOST_WIDEST_INT_PRINT_DEC
,
455 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
459 if (bb
->count
!= sum_edge_counts (bb
->preds
))
463 fprintf (dump_file
, "BB %i count does not match sum of incoming edges "
464 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
467 sum_edge_counts (bb
->preds
));
468 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
472 if (bb
->count
!= sum_edge_counts (bb
->succs
) &&
473 ! (find_edge (bb
, EXIT_BLOCK_PTR
) != NULL
&& block_ends_with_call_p (bb
)))
477 fprintf (dump_file
, "BB %i count does not match sum of outgoing edges "
478 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
481 sum_edge_counts (bb
->succs
));
482 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
486 if (!dump_file
&& inconsistent
)
493 /* Set each basic block count to the sum of its outgoing edge counts */
498 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
500 bb
->count
= sum_edge_counts (bb
->succs
);
501 gcc_assert (bb
->count
>= 0);
505 /* Reads profile data and returns total number of edge counts read */
507 read_profile_edge_counts (gcov_type
*exec_counts
)
511 int exec_counts_pos
= 0;
512 /* For each edge not on the spanning tree, set its execution count from
514 /* The first count in the .da file is the number of times that the function
515 was entered. This is the exec_count for block zero. */
517 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
522 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
523 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
528 e
->count
= exec_counts
[exec_counts_pos
++];
529 if (e
->count
> profile_info
->sum_max
)
531 if (flag_profile_correction
)
533 static bool informed
= 0;
535 inform (input_location
,
536 "corrupted profile info: edge count exceeds maximal count");
540 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
541 bb
->index
, e
->dest
->index
);
547 EDGE_INFO (e
)->count_valid
= 1;
548 BB_INFO (bb
)->succ_count
--;
549 BB_INFO (e
->dest
)->pred_count
--;
552 fprintf (dump_file
, "\nRead edge from %i to %i, count:",
553 bb
->index
, e
->dest
->index
);
554 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
555 (HOST_WIDEST_INT
) e
->count
);
563 #define OVERLAP_BASE 10000
565 /* Compare the static estimated profile to the actual profile, and
566 return the "degree of overlap" measure between them.
568 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
569 the sum of each basic block's minimum relative weights between
570 two profiles. And overlap of OVERLAP_BASE means two profiles are
574 compute_frequency_overlap (void)
576 gcov_type count_total
= 0, freq_total
= 0;
580 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
582 count_total
+= bb
->count
;
583 freq_total
+= bb
->frequency
;
586 if (count_total
== 0 || freq_total
== 0)
589 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
590 overlap
+= MIN (bb
->count
* OVERLAP_BASE
/ count_total
,
591 bb
->frequency
* OVERLAP_BASE
/ freq_total
);
596 /* Compute the branch probabilities for the various branches.
597 Annotate them accordingly.
599 CFG_CHECKSUM is the precomputed checksum for the CFG. */
602 compute_branch_probabilities (unsigned cfg_checksum
, unsigned lineno_checksum
)
609 int hist_br_prob
[20];
611 gcov_type
*exec_counts
= get_exec_counts (cfg_checksum
, lineno_checksum
);
612 int inconsistent
= 0;
614 /* Very simple sanity checks so we catch bugs in our profiling code. */
617 if (profile_info
->run_max
* profile_info
->runs
< profile_info
->sum_max
)
619 error ("corrupted profile info: run_max * runs < sum_max");
623 if (profile_info
->sum_all
< profile_info
->sum_max
)
625 error ("corrupted profile info: sum_all is smaller than sum_max");
629 /* Attach extra info block to each bb. */
630 alloc_aux_for_blocks (sizeof (struct bb_info
));
631 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
636 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
637 if (!EDGE_INFO (e
)->ignore
)
638 BB_INFO (bb
)->succ_count
++;
639 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
640 if (!EDGE_INFO (e
)->ignore
)
641 BB_INFO (bb
)->pred_count
++;
644 /* Avoid predicting entry on exit nodes. */
645 BB_INFO (EXIT_BLOCK_PTR
)->succ_count
= 2;
646 BB_INFO (ENTRY_BLOCK_PTR
)->pred_count
= 2;
648 num_edges
= read_profile_edge_counts (exec_counts
);
651 fprintf (dump_file
, "\n%d edge counts read\n", num_edges
);
653 /* For every block in the file,
654 - if every exit/entrance edge has a known count, then set the block count
655 - if the block count is known, and every exit/entrance edge but one has
656 a known execution count, then set the count of the remaining edge
658 As edge counts are set, decrement the succ/pred count, but don't delete
659 the edge, that way we can easily tell when all edges are known, or only
660 one edge is unknown. */
662 /* The order that the basic blocks are iterated through is important.
663 Since the code that finds spanning trees starts with block 0, low numbered
664 edges are put on the spanning tree in preference to high numbered edges.
665 Hence, most instrumented edges are at the end. Graph solving works much
666 faster if we propagate numbers from the end to the start.
668 This takes an average of slightly more than 3 passes. */
676 FOR_BB_BETWEEN (bb
, EXIT_BLOCK_PTR
, NULL
, prev_bb
)
678 struct bb_info
*bi
= BB_INFO (bb
);
679 if (! bi
->count_valid
)
681 if (bi
->succ_count
== 0)
687 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
693 else if (bi
->pred_count
== 0)
699 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
708 if (bi
->succ_count
== 1)
714 /* One of the counts will be invalid, but it is zero,
715 so adding it in also doesn't hurt. */
716 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
719 /* Search for the invalid edge, and set its count. */
720 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
721 if (! EDGE_INFO (e
)->count_valid
&& ! EDGE_INFO (e
)->ignore
)
724 /* Calculate count for remaining edge by conservation. */
725 total
= bb
->count
- total
;
728 EDGE_INFO (e
)->count_valid
= 1;
732 BB_INFO (e
->dest
)->pred_count
--;
735 if (bi
->pred_count
== 1)
741 /* One of the counts will be invalid, but it is zero,
742 so adding it in also doesn't hurt. */
743 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
746 /* Search for the invalid edge, and set its count. */
747 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
748 if (!EDGE_INFO (e
)->count_valid
&& !EDGE_INFO (e
)->ignore
)
751 /* Calculate count for remaining edge by conservation. */
752 total
= bb
->count
- total
+ e
->count
;
755 EDGE_INFO (e
)->count_valid
= 1;
759 BB_INFO (e
->src
)->succ_count
--;
767 int overlap
= compute_frequency_overlap ();
768 gimple_dump_cfg (dump_file
, dump_flags
);
769 fprintf (dump_file
, "Static profile overlap: %d.%d%%\n",
770 overlap
/ (OVERLAP_BASE
/ 100),
771 overlap
% (OVERLAP_BASE
/ 100));
774 total_num_passes
+= passes
;
776 fprintf (dump_file
, "Graph solving took %d passes.\n\n", passes
);
778 /* If the graph has been correctly solved, every block will have a
779 succ and pred count of zero. */
782 gcc_assert (!BB_INFO (bb
)->succ_count
&& !BB_INFO (bb
)->pred_count
);
785 /* Check for inconsistent basic block counts */
786 inconsistent
= is_inconsistent ();
790 if (flag_profile_correction
)
792 /* Inconsistency detected. Make it flow-consistent. */
793 static int informed
= 0;
797 inform (input_location
, "correcting inconsistent profile data");
799 correct_negative_edge_counts ();
800 /* Set bb counts to the sum of the outgoing edge counts */
803 fprintf (dump_file
, "\nCalling mcf_smooth_cfg\n");
807 error ("corrupted profile info: profile data is not flow-consistent");
810 /* For every edge, calculate its branch probability and add a reg_note
811 to the branch insn to indicate this. */
813 for (i
= 0; i
< 20; i
++)
817 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
824 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
825 bb
->index
, (int)bb
->count
);
828 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
830 /* Function may return twice in the cased the called function is
831 setjmp or calls fork, but we can't represent this by extra
832 edge from the entry, since extra edge from the exit is
833 already present. We get negative frequency from the entry
836 && e
->dest
== EXIT_BLOCK_PTR
)
837 || (e
->count
> bb
->count
838 && e
->dest
!= EXIT_BLOCK_PTR
))
840 if (block_ends_with_call_p (bb
))
841 e
->count
= e
->count
< 0 ? 0 : bb
->count
;
843 if (e
->count
< 0 || e
->count
> bb
->count
)
845 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
846 e
->src
->index
, e
->dest
->index
,
848 e
->count
= bb
->count
/ 2;
853 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
854 e
->probability
= (e
->count
* REG_BR_PROB_BASE
+ bb
->count
/ 2) / bb
->count
;
855 if (bb
->index
>= NUM_FIXED_BLOCKS
856 && block_ends_with_condjump_p (bb
)
857 && EDGE_COUNT (bb
->succs
) >= 2)
863 /* Find the branch edge. It is possible that we do have fake
865 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
866 if (!(e
->flags
& (EDGE_FAKE
| EDGE_FALLTHRU
)))
869 prob
= e
->probability
;
870 index
= prob
* 20 / REG_BR_PROB_BASE
;
874 hist_br_prob
[index
]++;
879 /* As a last resort, distribute the probabilities evenly.
880 Use simple heuristics that if there are normal edges,
881 give all abnormals frequency of 0, otherwise distribute the
882 frequency over abnormals (this is the case of noreturn
884 else if (profile_status
== PROFILE_ABSENT
)
888 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
889 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
893 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
894 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
895 e
->probability
= REG_BR_PROB_BASE
/ total
;
901 total
+= EDGE_COUNT (bb
->succs
);
902 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
903 e
->probability
= REG_BR_PROB_BASE
/ total
;
905 if (bb
->index
>= NUM_FIXED_BLOCKS
906 && block_ends_with_condjump_p (bb
)
907 && EDGE_COUNT (bb
->succs
) >= 2)
912 profile_status
= PROFILE_READ
;
913 compute_function_frequency ();
917 fprintf (dump_file
, "%d branches\n", num_branches
);
919 for (i
= 0; i
< 10; i
++)
920 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
921 (hist_br_prob
[i
] + hist_br_prob
[19-i
]) * 100 / num_branches
,
924 total_num_branches
+= num_branches
;
925 for (i
= 0; i
< 20; i
++)
926 total_hist_br_prob
[i
] += hist_br_prob
[i
];
928 fputc ('\n', dump_file
);
929 fputc ('\n', dump_file
);
932 free_aux_for_blocks ();
935 /* Load value histograms values whose description is stored in VALUES array
938 CFG_CHECKSUM is the precomputed checksum for the CFG. */
941 compute_value_histograms (histogram_values values
, unsigned cfg_checksum
,
942 unsigned lineno_checksum
)
944 unsigned i
, j
, t
, any
;
945 unsigned n_histogram_counters
[GCOV_N_VALUE_COUNTERS
];
946 gcov_type
*histogram_counts
[GCOV_N_VALUE_COUNTERS
];
947 gcov_type
*act_count
[GCOV_N_VALUE_COUNTERS
];
948 gcov_type
*aact_count
;
950 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
951 n_histogram_counters
[t
] = 0;
953 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
955 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
956 n_histogram_counters
[(int) hist
->type
] += hist
->n_counters
;
960 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
962 if (!n_histogram_counters
[t
])
964 histogram_counts
[t
] = NULL
;
968 histogram_counts
[t
] =
969 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t
),
970 n_histogram_counters
[t
], cfg_checksum
,
971 lineno_checksum
, NULL
);
972 if (histogram_counts
[t
])
974 act_count
[t
] = histogram_counts
[t
];
979 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
981 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
982 gimple stmt
= hist
->hvalue
.stmt
;
984 t
= (int) hist
->type
;
986 aact_count
= act_count
[t
];
987 act_count
[t
] += hist
->n_counters
;
989 gimple_add_histogram_value (cfun
, stmt
, hist
);
990 hist
->hvalue
.counters
= XNEWVEC (gcov_type
, hist
->n_counters
);
991 for (j
= 0; j
< hist
->n_counters
; j
++)
992 hist
->hvalue
.counters
[j
] = aact_count
[j
];
995 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
996 free (histogram_counts
[t
]);
999 /* When passed NULL as file_name, initialize.
1000 When passed something else, output the necessary commands to change
1001 line to LINE and offset to FILE_NAME. */
1003 output_location (char const *file_name
, int line
,
1004 gcov_position_t
*offset
, basic_block bb
)
1006 static char const *prev_file_name
;
1007 static int prev_line
;
1008 bool name_differs
, line_differs
;
1012 prev_file_name
= NULL
;
1017 name_differs
= !prev_file_name
|| filename_cmp (file_name
, prev_file_name
);
1018 line_differs
= prev_line
!= line
;
1020 if (name_differs
|| line_differs
)
1024 *offset
= gcov_write_tag (GCOV_TAG_LINES
);
1025 gcov_write_unsigned (bb
->index
);
1026 name_differs
= line_differs
=true;
1029 /* If this is a new source file, then output the
1030 file's name to the .bb file. */
1033 prev_file_name
= file_name
;
1034 gcov_write_unsigned (0);
1035 gcov_write_string (prev_file_name
);
1039 gcov_write_unsigned (line
);
1045 /* Instrument and/or analyze program behavior based on program the CFG.
1047 This function creates a representation of the control flow graph (of
1048 the function being compiled) that is suitable for the instrumentation
1049 of edges and/or converting measured edge counts to counts on the
1052 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
1053 the flow graph that are needed to reconstruct the dynamic behavior of the
1054 flow graph. This data is written to the gcno file for gcov.
1056 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
1057 information from the gcda file containing edge count information from
1058 previous executions of the function being compiled. In this case, the
1059 control flow graph is annotated with actual execution counts by
1060 compute_branch_probabilities().
1062 Main entry point of this file. */
1069 unsigned num_edges
, ignored_edges
;
1070 unsigned num_instrumented
;
1071 struct edge_list
*el
;
1072 histogram_values values
= NULL
;
1073 unsigned cfg_checksum
, lineno_checksum
;
1075 total_num_times_called
++;
1077 flow_call_edges_add (NULL
);
1078 add_noreturn_fake_exit_edges ();
1080 /* We can't handle cyclic regions constructed using abnormal edges.
1081 To avoid these we replace every source of abnormal edge by a fake
1082 edge from entry node and every destination by fake edge to exit.
1083 This keeps graph acyclic and our calculation exact for all normal
1084 edges except for exit and entrance ones.
1086 We also add fake exit edges for each call and asm statement in the
1087 basic, since it may not return. */
1091 int need_exit_edge
= 0, need_entry_edge
= 0;
1092 int have_exit_edge
= 0, have_entry_edge
= 0;
1096 /* Functions returning multiple times are not handled by extra edges.
1097 Instead we simply allow negative counts on edges from exit to the
1098 block past call and corresponding probabilities. We can't go
1099 with the extra edges because that would result in flowgraph that
1100 needs to have fake edges outside the spanning tree. */
1102 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1104 gimple_stmt_iterator gsi
;
1107 /* It may happen that there are compiler generated statements
1108 without a locus at all. Go through the basic block from the
1109 last to the first statement looking for a locus. */
1110 for (gsi
= gsi_last_nondebug_bb (bb
);
1112 gsi_prev_nondebug (&gsi
))
1114 last
= gsi_stmt (gsi
);
1115 if (gimple_has_location (last
))
1119 /* Edge with goto locus might get wrong coverage info unless
1120 it is the only edge out of BB.
1121 Don't do that when the locuses match, so
1122 if (blah) goto something;
1123 is not computed twice. */
1125 && gimple_has_location (last
)
1126 && e
->goto_locus
!= UNKNOWN_LOCATION
1127 && !single_succ_p (bb
)
1128 && (LOCATION_FILE (e
->goto_locus
)
1129 != LOCATION_FILE (gimple_location (last
))
1130 || (LOCATION_LINE (e
->goto_locus
)
1131 != LOCATION_LINE (gimple_location (last
)))))
1133 basic_block new_bb
= split_edge (e
);
1134 edge ne
= single_succ_edge (new_bb
);
1135 ne
->goto_locus
= e
->goto_locus
;
1136 ne
->goto_block
= e
->goto_block
;
1138 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1139 && e
->dest
!= EXIT_BLOCK_PTR
)
1141 if (e
->dest
== EXIT_BLOCK_PTR
)
1144 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1146 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1147 && e
->src
!= ENTRY_BLOCK_PTR
)
1148 need_entry_edge
= 1;
1149 if (e
->src
== ENTRY_BLOCK_PTR
)
1150 have_entry_edge
= 1;
1153 if (need_exit_edge
&& !have_exit_edge
)
1156 fprintf (dump_file
, "Adding fake exit edge to bb %i\n",
1158 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
1160 if (need_entry_edge
&& !have_entry_edge
)
1163 fprintf (dump_file
, "Adding fake entry edge to bb %i\n",
1165 make_edge (ENTRY_BLOCK_PTR
, bb
, EDGE_FAKE
);
1166 /* Avoid bbs that have both fake entry edge and also some
1167 exit edge. One of those edges wouldn't be added to the
1168 spanning tree, but we can't instrument any of them. */
1169 if (have_exit_edge
|| need_exit_edge
)
1171 gimple_stmt_iterator gsi
;
1175 gsi
= gsi_after_labels (bb
);
1176 gcc_checking_assert (!gsi_end_p (gsi
));
1177 first
= gsi_stmt (gsi
);
1178 if (is_gimple_debug (first
))
1180 gsi_next_nondebug (&gsi
);
1181 gcc_checking_assert (!gsi_end_p (gsi
));
1182 first
= gsi_stmt (gsi
);
1184 /* Don't split the bbs containing __builtin_setjmp_receiver
1185 or __builtin_setjmp_dispatcher calls. These are very
1186 special and don't expect anything to be inserted before
1188 if (!is_gimple_call (first
)
1189 || (fndecl
= gimple_call_fndecl (first
)) == NULL
1190 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
1191 || (DECL_FUNCTION_CODE (fndecl
) != BUILT_IN_SETJMP_RECEIVER
1192 && (DECL_FUNCTION_CODE (fndecl
)
1193 != BUILT_IN_SETJMP_DISPATCHER
)))
1196 fprintf (dump_file
, "Splitting bb %i after labels\n",
1198 split_block_after_labels (bb
);
1204 el
= create_edge_list ();
1205 num_edges
= NUM_EDGES (el
);
1206 alloc_aux_for_edges (sizeof (struct edge_info
));
1208 /* The basic blocks are expected to be numbered sequentially. */
1212 for (i
= 0 ; i
< num_edges
; i
++)
1214 edge e
= INDEX_EDGE (el
, i
);
1217 /* Mark edges we've replaced by fake edges above as ignored. */
1218 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1219 && e
->src
!= ENTRY_BLOCK_PTR
&& e
->dest
!= EXIT_BLOCK_PTR
)
1221 EDGE_INFO (e
)->ignore
= 1;
1226 /* Create spanning tree from basic block graph, mark each edge that is
1227 on the spanning tree. We insert as many abnormal and critical edges
1228 as possible to minimize number of edge splits necessary. */
1230 find_spanning_tree (el
);
1232 /* Fake edges that are not on the tree will not be instrumented, so
1233 mark them ignored. */
1234 for (num_instrumented
= i
= 0; i
< num_edges
; i
++)
1236 edge e
= INDEX_EDGE (el
, i
);
1237 struct edge_info
*inf
= EDGE_INFO (e
);
1239 if (inf
->ignore
|| inf
->on_tree
)
1241 else if (e
->flags
& EDGE_FAKE
)
1250 total_num_blocks
+= n_basic_blocks
;
1252 fprintf (dump_file
, "%d basic blocks\n", n_basic_blocks
);
1254 total_num_edges
+= num_edges
;
1256 fprintf (dump_file
, "%d edges\n", num_edges
);
1258 total_num_edges_ignored
+= ignored_edges
;
1260 fprintf (dump_file
, "%d ignored edges\n", ignored_edges
);
1262 total_num_edges_instrumented
+= num_instrumented
;
1264 fprintf (dump_file
, "%d instrumentation edges\n", num_instrumented
);
1266 /* Compute two different checksums. Note that we want to compute
1267 the checksum in only once place, since it depends on the shape
1268 of the control flow which can change during
1269 various transformations. */
1270 cfg_checksum
= coverage_compute_cfg_checksum ();
1271 lineno_checksum
= coverage_compute_lineno_checksum ();
1273 /* Write the data from which gcov can reconstruct the basic block
1274 graph and function line numbers (the gcno file). */
1275 if (coverage_begin_function (lineno_checksum
, cfg_checksum
))
1277 gcov_position_t offset
;
1279 /* Basic block flags */
1280 offset
= gcov_write_tag (GCOV_TAG_BLOCKS
);
1281 for (i
= 0; i
!= (unsigned) (n_basic_blocks
); i
++)
1282 gcov_write_unsigned (0);
1283 gcov_write_length (offset
);
1286 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
1291 offset
= gcov_write_tag (GCOV_TAG_ARCS
);
1292 gcov_write_unsigned (bb
->index
);
1294 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1296 struct edge_info
*i
= EDGE_INFO (e
);
1299 unsigned flag_bits
= 0;
1302 flag_bits
|= GCOV_ARC_ON_TREE
;
1303 if (e
->flags
& EDGE_FAKE
)
1304 flag_bits
|= GCOV_ARC_FAKE
;
1305 if (e
->flags
& EDGE_FALLTHRU
)
1306 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1307 /* On trees we don't have fallthru flags, but we can
1308 recompute them from CFG shape. */
1309 if (e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)
1310 && e
->src
->next_bb
== e
->dest
)
1311 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1313 gcov_write_unsigned (e
->dest
->index
);
1314 gcov_write_unsigned (flag_bits
);
1318 gcov_write_length (offset
);
1322 /* Initialize the output. */
1323 output_location (NULL
, 0, NULL
, NULL
);
1327 gimple_stmt_iterator gsi
;
1328 gcov_position_t offset
= 0;
1330 if (bb
== ENTRY_BLOCK_PTR
->next_bb
)
1332 expanded_location curr_location
=
1333 expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
1334 output_location (curr_location
.file
, curr_location
.line
,
1338 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1340 gimple stmt
= gsi_stmt (gsi
);
1341 if (gimple_has_location (stmt
))
1342 output_location (gimple_filename (stmt
), gimple_lineno (stmt
),
1346 /* Notice GOTO expressions eliminated while constructing the CFG. */
1347 if (single_succ_p (bb
)
1348 && single_succ_edge (bb
)->goto_locus
!= UNKNOWN_LOCATION
)
1350 expanded_location curr_location
1351 = expand_location (single_succ_edge (bb
)->goto_locus
);
1352 output_location (curr_location
.file
, curr_location
.line
,
1358 /* A file of NULL indicates the end of run. */
1359 gcov_write_unsigned (0);
1360 gcov_write_string (NULL
);
1361 gcov_write_length (offset
);
1366 if (flag_profile_values
)
1367 gimple_find_values_to_profile (&values
);
1369 if (flag_branch_probabilities
)
1371 compute_branch_probabilities (cfg_checksum
, lineno_checksum
);
1372 if (flag_profile_values
)
1373 compute_value_histograms (values
, cfg_checksum
, lineno_checksum
);
1376 remove_fake_edges ();
1378 /* For each edge not on the spanning tree, add counting code. */
1379 if (profile_arc_flag
1380 && coverage_counter_alloc (GCOV_COUNTER_ARCS
, num_instrumented
))
1382 unsigned n_instrumented
;
1384 gimple_init_edge_profiler ();
1386 n_instrumented
= instrument_edges (el
);
1388 gcc_assert (n_instrumented
== num_instrumented
);
1390 if (flag_profile_values
)
1391 instrument_values (values
);
1393 /* Commit changes done by instrumentation. */
1394 gsi_commit_edge_inserts ();
1397 free_aux_for_edges ();
1399 VEC_free (histogram_value
, heap
, values
);
1400 free_edge_list (el
);
1401 coverage_end_function (lineno_checksum
, cfg_checksum
);
1404 /* Union find algorithm implementation for the basic blocks using
1408 find_group (basic_block bb
)
1410 basic_block group
= bb
, bb1
;
1412 while ((basic_block
) group
->aux
!= group
)
1413 group
= (basic_block
) group
->aux
;
1415 /* Compress path. */
1416 while ((basic_block
) bb
->aux
!= group
)
1418 bb1
= (basic_block
) bb
->aux
;
1419 bb
->aux
= (void *) group
;
1426 union_groups (basic_block bb1
, basic_block bb2
)
1428 basic_block bb1g
= find_group (bb1
);
1429 basic_block bb2g
= find_group (bb2
);
1431 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1432 this code is unlikely going to be performance problem anyway. */
1433 gcc_assert (bb1g
!= bb2g
);
1438 /* This function searches all of the edges in the program flow graph, and puts
1439 as many bad edges as possible onto the spanning tree. Bad edges include
1440 abnormals edges, which can't be instrumented at the moment. Since it is
1441 possible for fake edges to form a cycle, we will have to develop some
1442 better way in the future. Also put critical edges to the tree, since they
1443 are more expensive to instrument. */
1446 find_spanning_tree (struct edge_list
*el
)
1449 int num_edges
= NUM_EDGES (el
);
1452 /* We use aux field for standard union-find algorithm. */
1453 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1456 /* Add fake edge exit to entry we can't instrument. */
1457 union_groups (EXIT_BLOCK_PTR
, ENTRY_BLOCK_PTR
);
1459 /* First add all abnormal edges to the tree unless they form a cycle. Also
1460 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1461 setting return value from function. */
1462 for (i
= 0; i
< num_edges
; i
++)
1464 edge e
= INDEX_EDGE (el
, i
);
1465 if (((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
))
1466 || e
->dest
== EXIT_BLOCK_PTR
)
1467 && !EDGE_INFO (e
)->ignore
1468 && (find_group (e
->src
) != find_group (e
->dest
)))
1471 fprintf (dump_file
, "Abnormal edge %d to %d put to tree\n",
1472 e
->src
->index
, e
->dest
->index
);
1473 EDGE_INFO (e
)->on_tree
= 1;
1474 union_groups (e
->src
, e
->dest
);
1478 /* Now insert all critical edges to the tree unless they form a cycle. */
1479 for (i
= 0; i
< num_edges
; i
++)
1481 edge e
= INDEX_EDGE (el
, i
);
1482 if (EDGE_CRITICAL_P (e
) && !EDGE_INFO (e
)->ignore
1483 && find_group (e
->src
) != find_group (e
->dest
))
1486 fprintf (dump_file
, "Critical edge %d to %d put to tree\n",
1487 e
->src
->index
, e
->dest
->index
);
1488 EDGE_INFO (e
)->on_tree
= 1;
1489 union_groups (e
->src
, e
->dest
);
1493 /* And now the rest. */
1494 for (i
= 0; i
< num_edges
; i
++)
1496 edge e
= INDEX_EDGE (el
, i
);
1497 if (!EDGE_INFO (e
)->ignore
1498 && find_group (e
->src
) != find_group (e
->dest
))
1501 fprintf (dump_file
, "Normal edge %d to %d put to tree\n",
1502 e
->src
->index
, e
->dest
->index
);
1503 EDGE_INFO (e
)->on_tree
= 1;
1504 union_groups (e
->src
, e
->dest
);
1508 clear_aux_for_blocks ();
1511 /* Perform file-level initialization for branch-prob processing. */
1514 init_branch_prob (void)
1518 total_num_blocks
= 0;
1519 total_num_edges
= 0;
1520 total_num_edges_ignored
= 0;
1521 total_num_edges_instrumented
= 0;
1522 total_num_blocks_created
= 0;
1523 total_num_passes
= 0;
1524 total_num_times_called
= 0;
1525 total_num_branches
= 0;
1526 for (i
= 0; i
< 20; i
++)
1527 total_hist_br_prob
[i
] = 0;
1530 /* Performs file-level cleanup after branch-prob processing
1534 end_branch_prob (void)
1538 fprintf (dump_file
, "\n");
1539 fprintf (dump_file
, "Total number of blocks: %d\n",
1541 fprintf (dump_file
, "Total number of edges: %d\n", total_num_edges
);
1542 fprintf (dump_file
, "Total number of ignored edges: %d\n",
1543 total_num_edges_ignored
);
1544 fprintf (dump_file
, "Total number of instrumented edges: %d\n",
1545 total_num_edges_instrumented
);
1546 fprintf (dump_file
, "Total number of blocks created: %d\n",
1547 total_num_blocks_created
);
1548 fprintf (dump_file
, "Total number of graph solution passes: %d\n",
1550 if (total_num_times_called
!= 0)
1551 fprintf (dump_file
, "Average number of graph solution passes: %d\n",
1552 (total_num_passes
+ (total_num_times_called
>> 1))
1553 / total_num_times_called
);
1554 fprintf (dump_file
, "Total number of branches: %d\n",
1555 total_num_branches
);
1556 if (total_num_branches
)
1560 for (i
= 0; i
< 10; i
++)
1561 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
1562 (total_hist_br_prob
[i
] + total_hist_br_prob
[19-i
]) * 100
1563 / total_num_branches
, 5*i
, 5*i
+5);