1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2013 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"
59 #include "basic-block.h"
60 #include "diagnostic-core.h"
62 #include "value-prof.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 /* Counter working set information computed from the current counter
88 summary. Not initialized unless profile_info summary is non-NULL. */
89 static gcov_working_set_t gcov_working_sets
[NUM_GCOV_WORKING_SETS
];
91 /* Collect statistics on the performance of this pass for the entire source
94 static int total_num_blocks
;
95 static int total_num_edges
;
96 static int total_num_edges_ignored
;
97 static int total_num_edges_instrumented
;
98 static int total_num_blocks_created
;
99 static int total_num_passes
;
100 static int total_num_times_called
;
101 static int total_hist_br_prob
[20];
102 static int total_num_branches
;
104 /* Forward declarations. */
105 static void find_spanning_tree (struct edge_list
*);
107 /* Add edge instrumentation code to the entire insn chain.
109 F is the first insn of the chain.
110 NUM_BLOCKS is the number of basic blocks found in F. */
113 instrument_edges (struct edge_list
*el
)
115 unsigned num_instr_edges
= 0;
116 int num_edges
= NUM_EDGES (el
);
119 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
124 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
126 struct edge_info
*inf
= EDGE_INFO (e
);
128 if (!inf
->ignore
&& !inf
->on_tree
)
130 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
132 fprintf (dump_file
, "Edge %d to %d instrumented%s\n",
133 e
->src
->index
, e
->dest
->index
,
134 EDGE_CRITICAL_P (e
) ? " (and split)" : "");
135 gimple_gen_edge_profiler (num_instr_edges
++, e
);
140 total_num_blocks_created
+= num_edges
;
142 fprintf (dump_file
, "%d edges instrumented\n", num_instr_edges
);
143 return num_instr_edges
;
146 /* Add code to measure histograms for values in list VALUES. */
148 instrument_values (histogram_values values
)
152 /* Emit code to generate the histograms before the insns. */
154 for (i
= 0; i
< values
.length (); i
++)
156 histogram_value hist
= values
[i
];
157 unsigned t
= COUNTER_FOR_HIST_TYPE (hist
->type
);
159 if (!coverage_counter_alloc (t
, hist
->n_counters
))
164 case HIST_TYPE_INTERVAL
:
165 gimple_gen_interval_profiler (hist
, t
, 0);
169 gimple_gen_pow2_profiler (hist
, t
, 0);
172 case HIST_TYPE_SINGLE_VALUE
:
173 gimple_gen_one_value_profiler (hist
, t
, 0);
176 case HIST_TYPE_CONST_DELTA
:
177 gimple_gen_const_delta_profiler (hist
, t
, 0);
180 case HIST_TYPE_INDIR_CALL
:
181 gimple_gen_ic_profiler (hist
, t
, 0);
184 case HIST_TYPE_AVERAGE
:
185 gimple_gen_average_profiler (hist
, t
, 0);
189 gimple_gen_ior_profiler (hist
, t
, 0);
192 case HIST_TYPE_TIME_PROFILE
:
194 basic_block bb
= split_edge (single_succ_edge (ENTRY_BLOCK_PTR
));
195 gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
197 gimple_gen_time_profiler (t
, 0, gsi
);
208 /* Fill the working set information into the profile_info structure. */
211 get_working_sets (void)
213 unsigned ws_ix
, pctinc
, pct
;
214 gcov_working_set_t
*ws_info
;
219 compute_working_sets (profile_info
, gcov_working_sets
);
223 fprintf (dump_file
, "Counter working sets:\n");
224 /* Multiply the percentage by 100 to avoid float. */
225 pctinc
= 100 * 100 / NUM_GCOV_WORKING_SETS
;
226 for (ws_ix
= 0, pct
= pctinc
; ws_ix
< NUM_GCOV_WORKING_SETS
;
227 ws_ix
++, pct
+= pctinc
)
229 if (ws_ix
== NUM_GCOV_WORKING_SETS
- 1)
231 ws_info
= &gcov_working_sets
[ws_ix
];
232 /* Print out the percentage using int arithmatic to avoid float. */
233 fprintf (dump_file
, "\t\t%u.%02u%%: num counts=%u, min counter="
234 HOST_WIDEST_INT_PRINT_DEC
"\n",
235 pct
/ 100, pct
- (pct
/ 100 * 100),
236 ws_info
->num_counters
,
237 (HOST_WIDEST_INT
)ws_info
->min_counter
);
242 /* Given a the desired percentage of the full profile (sum_all from the
243 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
244 the corresponding working set information. If an exact match for
245 the percentage isn't found, the closest value is used. */
248 find_working_set (unsigned pct_times_10
)
253 gcc_assert (pct_times_10
<= 1000);
254 if (pct_times_10
>= 999)
255 return &gcov_working_sets
[NUM_GCOV_WORKING_SETS
- 1];
256 i
= pct_times_10
* NUM_GCOV_WORKING_SETS
/ 1000;
258 return &gcov_working_sets
[0];
259 return &gcov_working_sets
[i
- 1];
262 /* Computes hybrid profile for all matching entries in da_file.
264 CFG_CHECKSUM is the precomputed checksum for the CFG. */
267 get_exec_counts (unsigned cfg_checksum
, unsigned lineno_checksum
)
269 unsigned num_edges
= 0;
273 /* Count the edges to be (possibly) instrumented. */
274 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
279 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
280 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
284 counts
= get_coverage_counts (GCOV_COUNTER_ARCS
, num_edges
, cfg_checksum
,
285 lineno_checksum
, &profile_info
);
291 if (dump_file
&& profile_info
)
292 fprintf (dump_file
, "Merged %u profiles with maximal count %u.\n",
293 profile_info
->runs
, (unsigned) profile_info
->sum_max
);
300 is_edge_inconsistent (vec
<edge
, va_gc
> *edges
)
304 FOR_EACH_EDGE (e
, ei
, edges
)
306 if (!EDGE_INFO (e
)->ignore
)
309 && (!(e
->flags
& EDGE_FAKE
)
310 || !block_ends_with_call_p (e
->src
)))
315 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC
,
316 e
->src
->index
, e
->dest
->index
, e
->count
);
317 dump_bb (dump_file
, e
->src
, 0, TDF_DETAILS
);
318 dump_bb (dump_file
, e
->dest
, 0, TDF_DETAILS
);
328 correct_negative_edge_counts (void)
334 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
336 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
344 /* Check consistency.
345 Return true if inconsistency is found. */
347 is_inconsistent (void)
350 bool inconsistent
= false;
353 inconsistent
|= is_edge_inconsistent (bb
->preds
);
354 if (!dump_file
&& inconsistent
)
356 inconsistent
|= is_edge_inconsistent (bb
->succs
);
357 if (!dump_file
&& inconsistent
)
363 fprintf (dump_file
, "BB %i count is negative "
364 HOST_WIDEST_INT_PRINT_DEC
,
367 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
371 if (bb
->count
!= sum_edge_counts (bb
->preds
))
375 fprintf (dump_file
, "BB %i count does not match sum of incoming edges "
376 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
379 sum_edge_counts (bb
->preds
));
380 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
384 if (bb
->count
!= sum_edge_counts (bb
->succs
) &&
385 ! (find_edge (bb
, EXIT_BLOCK_PTR
) != NULL
&& block_ends_with_call_p (bb
)))
389 fprintf (dump_file
, "BB %i count does not match sum of outgoing edges "
390 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
393 sum_edge_counts (bb
->succs
));
394 dump_bb (dump_file
, bb
, 0, TDF_DETAILS
);
398 if (!dump_file
&& inconsistent
)
405 /* Set each basic block count to the sum of its outgoing edge counts */
410 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
412 bb
->count
= sum_edge_counts (bb
->succs
);
413 gcc_assert (bb
->count
>= 0);
417 /* Reads profile data and returns total number of edge counts read */
419 read_profile_edge_counts (gcov_type
*exec_counts
)
423 int exec_counts_pos
= 0;
424 /* For each edge not on the spanning tree, set its execution count from
426 /* The first count in the .da file is the number of times that the function
427 was entered. This is the exec_count for block zero. */
429 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
434 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
435 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
440 e
->count
= exec_counts
[exec_counts_pos
++];
441 if (e
->count
> profile_info
->sum_max
)
443 if (flag_profile_correction
)
445 static bool informed
= 0;
446 if (dump_enabled_p () && !informed
)
447 dump_printf_loc (MSG_NOTE
, input_location
,
448 "corrupted profile info: edge count"
449 " exceeds maximal count\n");
453 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
454 bb
->index
, e
->dest
->index
);
460 EDGE_INFO (e
)->count_valid
= 1;
461 BB_INFO (bb
)->succ_count
--;
462 BB_INFO (e
->dest
)->pred_count
--;
465 fprintf (dump_file
, "\nRead edge from %i to %i, count:",
466 bb
->index
, e
->dest
->index
);
467 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
468 (HOST_WIDEST_INT
) e
->count
);
476 #define OVERLAP_BASE 10000
478 /* Compare the static estimated profile to the actual profile, and
479 return the "degree of overlap" measure between them.
481 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
482 the sum of each basic block's minimum relative weights between
483 two profiles. And overlap of OVERLAP_BASE means two profiles are
487 compute_frequency_overlap (void)
489 gcov_type count_total
= 0, freq_total
= 0;
493 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
495 count_total
+= bb
->count
;
496 freq_total
+= bb
->frequency
;
499 if (count_total
== 0 || freq_total
== 0)
502 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
503 overlap
+= MIN (bb
->count
* OVERLAP_BASE
/ count_total
,
504 bb
->frequency
* OVERLAP_BASE
/ freq_total
);
509 /* Compute the branch probabilities for the various branches.
510 Annotate them accordingly.
512 CFG_CHECKSUM is the precomputed checksum for the CFG. */
515 compute_branch_probabilities (unsigned cfg_checksum
, unsigned lineno_checksum
)
522 int hist_br_prob
[20];
524 gcov_type
*exec_counts
= get_exec_counts (cfg_checksum
, lineno_checksum
);
525 int inconsistent
= 0;
527 /* Very simple sanity checks so we catch bugs in our profiling code. */
530 if (profile_info
->run_max
* profile_info
->runs
< profile_info
->sum_max
)
532 error ("corrupted profile info: run_max * runs < sum_max");
536 if (profile_info
->sum_all
< profile_info
->sum_max
)
538 error ("corrupted profile info: sum_all is smaller than sum_max");
542 /* Attach extra info block to each bb. */
543 alloc_aux_for_blocks (sizeof (struct bb_info
));
544 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
549 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
550 if (!EDGE_INFO (e
)->ignore
)
551 BB_INFO (bb
)->succ_count
++;
552 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
553 if (!EDGE_INFO (e
)->ignore
)
554 BB_INFO (bb
)->pred_count
++;
557 /* Avoid predicting entry on exit nodes. */
558 BB_INFO (EXIT_BLOCK_PTR
)->succ_count
= 2;
559 BB_INFO (ENTRY_BLOCK_PTR
)->pred_count
= 2;
561 num_edges
= read_profile_edge_counts (exec_counts
);
564 fprintf (dump_file
, "\n%d edge counts read\n", num_edges
);
566 /* For every block in the file,
567 - if every exit/entrance edge has a known count, then set the block count
568 - if the block count is known, and every exit/entrance edge but one has
569 a known execution count, then set the count of the remaining edge
571 As edge counts are set, decrement the succ/pred count, but don't delete
572 the edge, that way we can easily tell when all edges are known, or only
573 one edge is unknown. */
575 /* The order that the basic blocks are iterated through is important.
576 Since the code that finds spanning trees starts with block 0, low numbered
577 edges are put on the spanning tree in preference to high numbered edges.
578 Hence, most instrumented edges are at the end. Graph solving works much
579 faster if we propagate numbers from the end to the start.
581 This takes an average of slightly more than 3 passes. */
589 FOR_BB_BETWEEN (bb
, EXIT_BLOCK_PTR
, NULL
, prev_bb
)
591 struct bb_info
*bi
= BB_INFO (bb
);
592 if (! bi
->count_valid
)
594 if (bi
->succ_count
== 0)
600 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
606 else if (bi
->pred_count
== 0)
612 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
621 if (bi
->succ_count
== 1)
627 /* One of the counts will be invalid, but it is zero,
628 so adding it in also doesn't hurt. */
629 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
632 /* Search for the invalid edge, and set its count. */
633 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
634 if (! EDGE_INFO (e
)->count_valid
&& ! EDGE_INFO (e
)->ignore
)
637 /* Calculate count for remaining edge by conservation. */
638 total
= bb
->count
- total
;
641 EDGE_INFO (e
)->count_valid
= 1;
645 BB_INFO (e
->dest
)->pred_count
--;
648 if (bi
->pred_count
== 1)
654 /* One of the counts will be invalid, but it is zero,
655 so adding it in also doesn't hurt. */
656 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
659 /* Search for the invalid edge, and set its count. */
660 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
661 if (!EDGE_INFO (e
)->count_valid
&& !EDGE_INFO (e
)->ignore
)
664 /* Calculate count for remaining edge by conservation. */
665 total
= bb
->count
- total
+ e
->count
;
668 EDGE_INFO (e
)->count_valid
= 1;
672 BB_INFO (e
->src
)->succ_count
--;
680 int overlap
= compute_frequency_overlap ();
681 gimple_dump_cfg (dump_file
, dump_flags
);
682 fprintf (dump_file
, "Static profile overlap: %d.%d%%\n",
683 overlap
/ (OVERLAP_BASE
/ 100),
684 overlap
% (OVERLAP_BASE
/ 100));
687 total_num_passes
+= passes
;
689 fprintf (dump_file
, "Graph solving took %d passes.\n\n", passes
);
691 /* If the graph has been correctly solved, every block will have a
692 succ and pred count of zero. */
695 gcc_assert (!BB_INFO (bb
)->succ_count
&& !BB_INFO (bb
)->pred_count
);
698 /* Check for inconsistent basic block counts */
699 inconsistent
= is_inconsistent ();
703 if (flag_profile_correction
)
705 /* Inconsistency detected. Make it flow-consistent. */
706 static int informed
= 0;
707 if (dump_enabled_p () && informed
== 0)
710 dump_printf_loc (MSG_NOTE
, input_location
,
711 "correcting inconsistent profile data\n");
713 correct_negative_edge_counts ();
714 /* Set bb counts to the sum of the outgoing edge counts */
717 fprintf (dump_file
, "\nCalling mcf_smooth_cfg\n");
721 error ("corrupted profile info: profile data is not flow-consistent");
724 /* For every edge, calculate its branch probability and add a reg_note
725 to the branch insn to indicate this. */
727 for (i
= 0; i
< 20; i
++)
731 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
738 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
739 bb
->index
, (int)bb
->count
);
742 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
744 /* Function may return twice in the cased the called function is
745 setjmp or calls fork, but we can't represent this by extra
746 edge from the entry, since extra edge from the exit is
747 already present. We get negative frequency from the entry
750 && e
->dest
== EXIT_BLOCK_PTR
)
751 || (e
->count
> bb
->count
752 && e
->dest
!= EXIT_BLOCK_PTR
))
754 if (block_ends_with_call_p (bb
))
755 e
->count
= e
->count
< 0 ? 0 : bb
->count
;
757 if (e
->count
< 0 || e
->count
> bb
->count
)
759 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
760 e
->src
->index
, e
->dest
->index
,
762 e
->count
= bb
->count
/ 2;
767 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
768 e
->probability
= GCOV_COMPUTE_SCALE (e
->count
, bb
->count
);
769 if (bb
->index
>= NUM_FIXED_BLOCKS
770 && block_ends_with_condjump_p (bb
)
771 && EDGE_COUNT (bb
->succs
) >= 2)
777 /* Find the branch edge. It is possible that we do have fake
779 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
780 if (!(e
->flags
& (EDGE_FAKE
| EDGE_FALLTHRU
)))
783 prob
= e
->probability
;
784 index
= prob
* 20 / REG_BR_PROB_BASE
;
788 hist_br_prob
[index
]++;
793 /* As a last resort, distribute the probabilities evenly.
794 Use simple heuristics that if there are normal edges,
795 give all abnormals frequency of 0, otherwise distribute the
796 frequency over abnormals (this is the case of noreturn
798 else if (profile_status
== PROFILE_ABSENT
)
802 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
803 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
807 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
808 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
809 e
->probability
= REG_BR_PROB_BASE
/ total
;
815 total
+= EDGE_COUNT (bb
->succs
);
816 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
817 e
->probability
= REG_BR_PROB_BASE
/ total
;
819 if (bb
->index
>= NUM_FIXED_BLOCKS
820 && block_ends_with_condjump_p (bb
)
821 && EDGE_COUNT (bb
->succs
) >= 2)
826 profile_status
= PROFILE_READ
;
827 compute_function_frequency ();
831 fprintf (dump_file
, "%d branches\n", num_branches
);
833 for (i
= 0; i
< 10; i
++)
834 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
835 (hist_br_prob
[i
] + hist_br_prob
[19-i
]) * 100 / num_branches
,
838 total_num_branches
+= num_branches
;
839 for (i
= 0; i
< 20; i
++)
840 total_hist_br_prob
[i
] += hist_br_prob
[i
];
842 fputc ('\n', dump_file
);
843 fputc ('\n', dump_file
);
846 free_aux_for_blocks ();
849 /* Load value histograms values whose description is stored in VALUES array
852 CFG_CHECKSUM is the precomputed checksum for the CFG. */
855 compute_value_histograms (histogram_values values
, unsigned cfg_checksum
,
856 unsigned lineno_checksum
)
858 unsigned i
, j
, t
, any
;
859 unsigned n_histogram_counters
[GCOV_N_VALUE_COUNTERS
];
860 gcov_type
*histogram_counts
[GCOV_N_VALUE_COUNTERS
];
861 gcov_type
*act_count
[GCOV_N_VALUE_COUNTERS
];
862 gcov_type
*aact_count
;
863 struct cgraph_node
*node
;
865 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
866 n_histogram_counters
[t
] = 0;
868 for (i
= 0; i
< values
.length (); i
++)
870 histogram_value hist
= values
[i
];
871 n_histogram_counters
[(int) hist
->type
] += hist
->n_counters
;
875 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
877 if (!n_histogram_counters
[t
])
879 histogram_counts
[t
] = NULL
;
883 histogram_counts
[t
] =
884 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t
),
885 n_histogram_counters
[t
], cfg_checksum
,
886 lineno_checksum
, NULL
);
887 if (histogram_counts
[t
])
889 act_count
[t
] = histogram_counts
[t
];
894 for (i
= 0; i
< values
.length (); i
++)
896 histogram_value hist
= values
[i
];
897 gimple stmt
= hist
->hvalue
.stmt
;
899 t
= (int) hist
->type
;
901 aact_count
= act_count
[t
];
904 act_count
[t
] += hist
->n_counters
;
906 gimple_add_histogram_value (cfun
, stmt
, hist
);
907 hist
->hvalue
.counters
= XNEWVEC (gcov_type
, hist
->n_counters
);
908 for (j
= 0; j
< hist
->n_counters
; j
++)
910 hist
->hvalue
.counters
[j
] = aact_count
[j
];
912 hist
->hvalue
.counters
[j
] = 0;
914 /* Time profiler counter is not related to any statement,
915 so that we have to read the counter and set the value to
916 the corresponding call graph node. */
917 if (hist
->type
== HIST_TYPE_TIME_PROFILE
)
919 node
= cgraph_get_node (hist
->fun
->decl
);
921 node
->tp_first_run
= hist
->hvalue
.counters
[0];
924 fprintf (dump_file
, "Read tp_first_run: %d\n", node
->tp_first_run
);
928 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
929 free (histogram_counts
[t
]);
932 /* When passed NULL as file_name, initialize.
933 When passed something else, output the necessary commands to change
934 line to LINE and offset to FILE_NAME. */
936 output_location (char const *file_name
, int line
,
937 gcov_position_t
*offset
, basic_block bb
)
939 static char const *prev_file_name
;
940 static int prev_line
;
941 bool name_differs
, line_differs
;
945 prev_file_name
= NULL
;
950 name_differs
= !prev_file_name
|| filename_cmp (file_name
, prev_file_name
);
951 line_differs
= prev_line
!= line
;
953 if (name_differs
|| line_differs
)
957 *offset
= gcov_write_tag (GCOV_TAG_LINES
);
958 gcov_write_unsigned (bb
->index
);
959 name_differs
= line_differs
=true;
962 /* If this is a new source file, then output the
963 file's name to the .bb file. */
966 prev_file_name
= file_name
;
967 gcov_write_unsigned (0);
968 gcov_write_string (prev_file_name
);
972 gcov_write_unsigned (line
);
978 /* Instrument and/or analyze program behavior based on program the CFG.
980 This function creates a representation of the control flow graph (of
981 the function being compiled) that is suitable for the instrumentation
982 of edges and/or converting measured edge counts to counts on the
985 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
986 the flow graph that are needed to reconstruct the dynamic behavior of the
987 flow graph. This data is written to the gcno file for gcov.
989 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
990 information from the gcda file containing edge count information from
991 previous executions of the function being compiled. In this case, the
992 control flow graph is annotated with actual execution counts by
993 compute_branch_probabilities().
995 Main entry point of this file. */
1002 unsigned num_edges
, ignored_edges
;
1003 unsigned num_instrumented
;
1004 struct edge_list
*el
;
1005 histogram_values values
= histogram_values ();
1006 unsigned cfg_checksum
, lineno_checksum
;
1008 total_num_times_called
++;
1010 flow_call_edges_add (NULL
);
1011 add_noreturn_fake_exit_edges ();
1013 /* We can't handle cyclic regions constructed using abnormal edges.
1014 To avoid these we replace every source of abnormal edge by a fake
1015 edge from entry node and every destination by fake edge to exit.
1016 This keeps graph acyclic and our calculation exact for all normal
1017 edges except for exit and entrance ones.
1019 We also add fake exit edges for each call and asm statement in the
1020 basic, since it may not return. */
1024 int need_exit_edge
= 0, need_entry_edge
= 0;
1025 int have_exit_edge
= 0, have_entry_edge
= 0;
1029 /* Functions returning multiple times are not handled by extra edges.
1030 Instead we simply allow negative counts on edges from exit to the
1031 block past call and corresponding probabilities. We can't go
1032 with the extra edges because that would result in flowgraph that
1033 needs to have fake edges outside the spanning tree. */
1035 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1037 gimple_stmt_iterator gsi
;
1040 /* It may happen that there are compiler generated statements
1041 without a locus at all. Go through the basic block from the
1042 last to the first statement looking for a locus. */
1043 for (gsi
= gsi_last_nondebug_bb (bb
);
1045 gsi_prev_nondebug (&gsi
))
1047 last
= gsi_stmt (gsi
);
1048 if (gimple_has_location (last
))
1052 /* Edge with goto locus might get wrong coverage info unless
1053 it is the only edge out of BB.
1054 Don't do that when the locuses match, so
1055 if (blah) goto something;
1056 is not computed twice. */
1058 && gimple_has_location (last
)
1059 && LOCATION_LOCUS (e
->goto_locus
) != UNKNOWN_LOCATION
1060 && !single_succ_p (bb
)
1061 && (LOCATION_FILE (e
->goto_locus
)
1062 != LOCATION_FILE (gimple_location (last
))
1063 || (LOCATION_LINE (e
->goto_locus
)
1064 != LOCATION_LINE (gimple_location (last
)))))
1066 basic_block new_bb
= split_edge (e
);
1067 edge ne
= single_succ_edge (new_bb
);
1068 ne
->goto_locus
= e
->goto_locus
;
1070 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1071 && e
->dest
!= EXIT_BLOCK_PTR
)
1073 if (e
->dest
== EXIT_BLOCK_PTR
)
1076 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1078 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1079 && e
->src
!= ENTRY_BLOCK_PTR
)
1080 need_entry_edge
= 1;
1081 if (e
->src
== ENTRY_BLOCK_PTR
)
1082 have_entry_edge
= 1;
1085 if (need_exit_edge
&& !have_exit_edge
)
1088 fprintf (dump_file
, "Adding fake exit edge to bb %i\n",
1090 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
1092 if (need_entry_edge
&& !have_entry_edge
)
1095 fprintf (dump_file
, "Adding fake entry edge to bb %i\n",
1097 make_edge (ENTRY_BLOCK_PTR
, bb
, EDGE_FAKE
);
1098 /* Avoid bbs that have both fake entry edge and also some
1099 exit edge. One of those edges wouldn't be added to the
1100 spanning tree, but we can't instrument any of them. */
1101 if (have_exit_edge
|| need_exit_edge
)
1103 gimple_stmt_iterator gsi
;
1107 gsi
= gsi_after_labels (bb
);
1108 gcc_checking_assert (!gsi_end_p (gsi
));
1109 first
= gsi_stmt (gsi
);
1110 if (is_gimple_debug (first
))
1112 gsi_next_nondebug (&gsi
);
1113 gcc_checking_assert (!gsi_end_p (gsi
));
1114 first
= gsi_stmt (gsi
);
1116 /* Don't split the bbs containing __builtin_setjmp_receiver
1117 or __builtin_setjmp_dispatcher calls. These are very
1118 special and don't expect anything to be inserted before
1120 if (is_gimple_call (first
)
1121 && (((fndecl
= gimple_call_fndecl (first
)) != NULL
1122 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
1123 && (DECL_FUNCTION_CODE (fndecl
)
1124 == BUILT_IN_SETJMP_RECEIVER
1125 || (DECL_FUNCTION_CODE (fndecl
)
1126 == BUILT_IN_SETJMP_DISPATCHER
)))
1127 || gimple_call_flags (first
) & ECF_RETURNS_TWICE
))
1131 fprintf (dump_file
, "Splitting bb %i after labels\n",
1133 split_block_after_labels (bb
);
1138 el
= create_edge_list ();
1139 num_edges
= NUM_EDGES (el
);
1140 alloc_aux_for_edges (sizeof (struct edge_info
));
1142 /* The basic blocks are expected to be numbered sequentially. */
1146 for (i
= 0 ; i
< num_edges
; i
++)
1148 edge e
= INDEX_EDGE (el
, i
);
1151 /* Mark edges we've replaced by fake edges above as ignored. */
1152 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1153 && e
->src
!= ENTRY_BLOCK_PTR
&& e
->dest
!= EXIT_BLOCK_PTR
)
1155 EDGE_INFO (e
)->ignore
= 1;
1160 /* Create spanning tree from basic block graph, mark each edge that is
1161 on the spanning tree. We insert as many abnormal and critical edges
1162 as possible to minimize number of edge splits necessary. */
1164 find_spanning_tree (el
);
1166 /* Fake edges that are not on the tree will not be instrumented, so
1167 mark them ignored. */
1168 for (num_instrumented
= i
= 0; i
< num_edges
; i
++)
1170 edge e
= INDEX_EDGE (el
, i
);
1171 struct edge_info
*inf
= EDGE_INFO (e
);
1173 if (inf
->ignore
|| inf
->on_tree
)
1175 else if (e
->flags
& EDGE_FAKE
)
1184 total_num_blocks
+= n_basic_blocks
;
1186 fprintf (dump_file
, "%d basic blocks\n", n_basic_blocks
);
1188 total_num_edges
+= num_edges
;
1190 fprintf (dump_file
, "%d edges\n", num_edges
);
1192 total_num_edges_ignored
+= ignored_edges
;
1194 fprintf (dump_file
, "%d ignored edges\n", ignored_edges
);
1196 total_num_edges_instrumented
+= num_instrumented
;
1198 fprintf (dump_file
, "%d instrumentation edges\n", num_instrumented
);
1200 /* Compute two different checksums. Note that we want to compute
1201 the checksum in only once place, since it depends on the shape
1202 of the control flow which can change during
1203 various transformations. */
1204 cfg_checksum
= coverage_compute_cfg_checksum ();
1205 lineno_checksum
= coverage_compute_lineno_checksum ();
1207 /* Write the data from which gcov can reconstruct the basic block
1208 graph and function line numbers (the gcno file). */
1209 if (coverage_begin_function (lineno_checksum
, cfg_checksum
))
1211 gcov_position_t offset
;
1213 /* Basic block flags */
1214 offset
= gcov_write_tag (GCOV_TAG_BLOCKS
);
1215 for (i
= 0; i
!= (unsigned) (n_basic_blocks
); i
++)
1216 gcov_write_unsigned (0);
1217 gcov_write_length (offset
);
1220 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
1225 offset
= gcov_write_tag (GCOV_TAG_ARCS
);
1226 gcov_write_unsigned (bb
->index
);
1228 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1230 struct edge_info
*i
= EDGE_INFO (e
);
1233 unsigned flag_bits
= 0;
1236 flag_bits
|= GCOV_ARC_ON_TREE
;
1237 if (e
->flags
& EDGE_FAKE
)
1238 flag_bits
|= GCOV_ARC_FAKE
;
1239 if (e
->flags
& EDGE_FALLTHRU
)
1240 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1241 /* On trees we don't have fallthru flags, but we can
1242 recompute them from CFG shape. */
1243 if (e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)
1244 && e
->src
->next_bb
== e
->dest
)
1245 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1247 gcov_write_unsigned (e
->dest
->index
);
1248 gcov_write_unsigned (flag_bits
);
1252 gcov_write_length (offset
);
1256 /* Initialize the output. */
1257 output_location (NULL
, 0, NULL
, NULL
);
1261 gimple_stmt_iterator gsi
;
1262 gcov_position_t offset
= 0;
1264 if (bb
== ENTRY_BLOCK_PTR
->next_bb
)
1266 expanded_location curr_location
=
1267 expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
1268 output_location (curr_location
.file
, curr_location
.line
,
1272 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1274 gimple stmt
= gsi_stmt (gsi
);
1275 if (gimple_has_location (stmt
))
1276 output_location (gimple_filename (stmt
), gimple_lineno (stmt
),
1280 /* Notice GOTO expressions eliminated while constructing the CFG. */
1281 if (single_succ_p (bb
)
1282 && LOCATION_LOCUS (single_succ_edge (bb
)->goto_locus
)
1283 != UNKNOWN_LOCATION
)
1285 expanded_location curr_location
1286 = expand_location (single_succ_edge (bb
)->goto_locus
);
1287 output_location (curr_location
.file
, curr_location
.line
,
1293 /* A file of NULL indicates the end of run. */
1294 gcov_write_unsigned (0);
1295 gcov_write_string (NULL
);
1296 gcov_write_length (offset
);
1301 if (flag_profile_values
)
1302 gimple_find_values_to_profile (&values
);
1304 if (flag_branch_probabilities
)
1306 compute_branch_probabilities (cfg_checksum
, lineno_checksum
);
1307 if (flag_profile_values
)
1308 compute_value_histograms (values
, cfg_checksum
, lineno_checksum
);
1311 remove_fake_edges ();
1313 /* For each edge not on the spanning tree, add counting code. */
1314 if (profile_arc_flag
1315 && coverage_counter_alloc (GCOV_COUNTER_ARCS
, num_instrumented
))
1317 unsigned n_instrumented
;
1319 gimple_init_edge_profiler ();
1321 n_instrumented
= instrument_edges (el
);
1323 gcc_assert (n_instrumented
== num_instrumented
);
1325 if (flag_profile_values
)
1326 instrument_values (values
);
1328 /* Commit changes done by instrumentation. */
1329 gsi_commit_edge_inserts ();
1332 free_aux_for_edges ();
1335 free_edge_list (el
);
1336 coverage_end_function (lineno_checksum
, cfg_checksum
);
1339 /* Union find algorithm implementation for the basic blocks using
1343 find_group (basic_block bb
)
1345 basic_block group
= bb
, bb1
;
1347 while ((basic_block
) group
->aux
!= group
)
1348 group
= (basic_block
) group
->aux
;
1350 /* Compress path. */
1351 while ((basic_block
) bb
->aux
!= group
)
1353 bb1
= (basic_block
) bb
->aux
;
1354 bb
->aux
= (void *) group
;
1361 union_groups (basic_block bb1
, basic_block bb2
)
1363 basic_block bb1g
= find_group (bb1
);
1364 basic_block bb2g
= find_group (bb2
);
1366 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1367 this code is unlikely going to be performance problem anyway. */
1368 gcc_assert (bb1g
!= bb2g
);
1373 /* This function searches all of the edges in the program flow graph, and puts
1374 as many bad edges as possible onto the spanning tree. Bad edges include
1375 abnormals edges, which can't be instrumented at the moment. Since it is
1376 possible for fake edges to form a cycle, we will have to develop some
1377 better way in the future. Also put critical edges to the tree, since they
1378 are more expensive to instrument. */
1381 find_spanning_tree (struct edge_list
*el
)
1384 int num_edges
= NUM_EDGES (el
);
1387 /* We use aux field for standard union-find algorithm. */
1388 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1391 /* Add fake edge exit to entry we can't instrument. */
1392 union_groups (EXIT_BLOCK_PTR
, ENTRY_BLOCK_PTR
);
1394 /* First add all abnormal edges to the tree unless they form a cycle. Also
1395 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1396 setting return value from function. */
1397 for (i
= 0; i
< num_edges
; i
++)
1399 edge e
= INDEX_EDGE (el
, i
);
1400 if (((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
))
1401 || e
->dest
== EXIT_BLOCK_PTR
)
1402 && !EDGE_INFO (e
)->ignore
1403 && (find_group (e
->src
) != find_group (e
->dest
)))
1406 fprintf (dump_file
, "Abnormal edge %d to %d put to tree\n",
1407 e
->src
->index
, e
->dest
->index
);
1408 EDGE_INFO (e
)->on_tree
= 1;
1409 union_groups (e
->src
, e
->dest
);
1413 /* Now insert all critical edges to the tree unless they form a cycle. */
1414 for (i
= 0; i
< num_edges
; i
++)
1416 edge e
= INDEX_EDGE (el
, i
);
1417 if (EDGE_CRITICAL_P (e
) && !EDGE_INFO (e
)->ignore
1418 && find_group (e
->src
) != find_group (e
->dest
))
1421 fprintf (dump_file
, "Critical edge %d to %d put to tree\n",
1422 e
->src
->index
, e
->dest
->index
);
1423 EDGE_INFO (e
)->on_tree
= 1;
1424 union_groups (e
->src
, e
->dest
);
1428 /* And now the rest. */
1429 for (i
= 0; i
< num_edges
; i
++)
1431 edge e
= INDEX_EDGE (el
, i
);
1432 if (!EDGE_INFO (e
)->ignore
1433 && find_group (e
->src
) != find_group (e
->dest
))
1436 fprintf (dump_file
, "Normal edge %d to %d put to tree\n",
1437 e
->src
->index
, e
->dest
->index
);
1438 EDGE_INFO (e
)->on_tree
= 1;
1439 union_groups (e
->src
, e
->dest
);
1443 clear_aux_for_blocks ();
1446 /* Perform file-level initialization for branch-prob processing. */
1449 init_branch_prob (void)
1453 total_num_blocks
= 0;
1454 total_num_edges
= 0;
1455 total_num_edges_ignored
= 0;
1456 total_num_edges_instrumented
= 0;
1457 total_num_blocks_created
= 0;
1458 total_num_passes
= 0;
1459 total_num_times_called
= 0;
1460 total_num_branches
= 0;
1461 for (i
= 0; i
< 20; i
++)
1462 total_hist_br_prob
[i
] = 0;
1465 /* Performs file-level cleanup after branch-prob processing
1469 end_branch_prob (void)
1473 fprintf (dump_file
, "\n");
1474 fprintf (dump_file
, "Total number of blocks: %d\n",
1476 fprintf (dump_file
, "Total number of edges: %d\n", total_num_edges
);
1477 fprintf (dump_file
, "Total number of ignored edges: %d\n",
1478 total_num_edges_ignored
);
1479 fprintf (dump_file
, "Total number of instrumented edges: %d\n",
1480 total_num_edges_instrumented
);
1481 fprintf (dump_file
, "Total number of blocks created: %d\n",
1482 total_num_blocks_created
);
1483 fprintf (dump_file
, "Total number of graph solution passes: %d\n",
1485 if (total_num_times_called
!= 0)
1486 fprintf (dump_file
, "Average number of graph solution passes: %d\n",
1487 (total_num_passes
+ (total_num_times_called
>> 1))
1488 / total_num_times_called
);
1489 fprintf (dump_file
, "Total number of branches: %d\n",
1490 total_num_branches
);
1491 if (total_num_branches
)
1495 for (i
= 0; i
< 10; i
++)
1496 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
1497 (total_hist_br_prob
[i
] + total_hist_br_prob
[19-i
]) * 100
1498 / total_num_branches
, 5*i
, 5*i
+5);