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"
67 #include "tree-flow.h"
70 #include "tree-pass.h"
75 unsigned int count_valid
: 1;
77 /* Number of successor and predecessor edges. */
82 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
85 /* Counter summary from the last set of coverage counts read. */
87 const struct gcov_ctr_summary
*profile_info
;
89 /* Collect statistics on the performance of this pass for the entire source
92 static int total_num_blocks
;
93 static int total_num_edges
;
94 static int total_num_edges_ignored
;
95 static int total_num_edges_instrumented
;
96 static int total_num_blocks_created
;
97 static int total_num_passes
;
98 static int total_num_times_called
;
99 static int total_hist_br_prob
[20];
100 static int total_num_branches
;
102 /* Forward declarations. */
103 static void find_spanning_tree (struct edge_list
*);
105 /* Add edge instrumentation code to the entire insn chain.
107 F is the first insn of the chain.
108 NUM_BLOCKS is the number of basic blocks found in F. */
111 instrument_edges (struct edge_list
*el
)
113 unsigned num_instr_edges
= 0;
114 int num_edges
= NUM_EDGES (el
);
117 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
122 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
124 struct edge_info
*inf
= EDGE_INFO (e
);
126 if (!inf
->ignore
&& !inf
->on_tree
)
128 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
130 fprintf (dump_file
, "Edge %d to %d instrumented%s\n",
131 e
->src
->index
, e
->dest
->index
,
132 EDGE_CRITICAL_P (e
) ? " (and split)" : "");
133 gimple_gen_edge_profiler (num_instr_edges
++, e
);
138 total_num_blocks_created
+= num_edges
;
140 fprintf (dump_file
, "%d edges instrumented\n", num_instr_edges
);
141 return num_instr_edges
;
144 /* Add code to measure histograms for values in list VALUES. */
146 instrument_values (histogram_values values
)
150 /* Emit code to generate the histograms before the insns. */
152 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
154 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
157 case HIST_TYPE_INTERVAL
:
158 t
= GCOV_COUNTER_V_INTERVAL
;
162 t
= GCOV_COUNTER_V_POW2
;
165 case HIST_TYPE_SINGLE_VALUE
:
166 t
= GCOV_COUNTER_V_SINGLE
;
169 case HIST_TYPE_CONST_DELTA
:
170 t
= GCOV_COUNTER_V_DELTA
;
173 case HIST_TYPE_INDIR_CALL
:
174 t
= GCOV_COUNTER_V_INDIR
;
177 case HIST_TYPE_AVERAGE
:
178 t
= GCOV_COUNTER_AVERAGE
;
182 t
= GCOV_COUNTER_IOR
;
188 if (!coverage_counter_alloc (t
, hist
->n_counters
))
193 case HIST_TYPE_INTERVAL
:
194 gimple_gen_interval_profiler (hist
, t
, 0);
198 gimple_gen_pow2_profiler (hist
, t
, 0);
201 case HIST_TYPE_SINGLE_VALUE
:
202 gimple_gen_one_value_profiler (hist
, t
, 0);
205 case HIST_TYPE_CONST_DELTA
:
206 gimple_gen_const_delta_profiler (hist
, t
, 0);
209 case HIST_TYPE_INDIR_CALL
:
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);
228 /* Computes hybrid profile for all matching entries in da_file.
230 CFG_CHECKSUM is the precomputed checksum for the CFG. */
233 get_exec_counts (unsigned cfg_checksum
, unsigned lineno_checksum
)
235 unsigned num_edges
= 0;
239 /* Count the edges to be (possibly) instrumented. */
240 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
245 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
246 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
250 counts
= get_coverage_counts (GCOV_COUNTER_ARCS
, num_edges
, cfg_checksum
,
251 lineno_checksum
, &profile_info
);
255 if (dump_file
&& profile_info
)
256 fprintf(dump_file
, "Merged %u profiles with maximal count %u.\n",
257 profile_info
->runs
, (unsigned) profile_info
->sum_max
);
264 is_edge_inconsistent (VEC(edge
,gc
) *edges
)
268 FOR_EACH_EDGE (e
, ei
, edges
)
270 if (!EDGE_INFO (e
)->ignore
)
273 && (!(e
->flags
& EDGE_FAKE
)
274 || !block_ends_with_call_p (e
->src
)))
279 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC
,
280 e
->src
->index
, e
->dest
->index
, e
->count
);
281 dump_bb (e
->src
, dump_file
, 0);
282 dump_bb (e
->dest
, dump_file
, 0);
292 correct_negative_edge_counts (void)
298 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
300 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
308 /* Check consistency.
309 Return true if inconsistency is found. */
311 is_inconsistent (void)
314 bool inconsistent
= false;
317 inconsistent
|= is_edge_inconsistent (bb
->preds
);
318 if (!dump_file
&& inconsistent
)
320 inconsistent
|= is_edge_inconsistent (bb
->succs
);
321 if (!dump_file
&& inconsistent
)
327 fprintf (dump_file
, "BB %i count is negative "
328 HOST_WIDEST_INT_PRINT_DEC
,
331 dump_bb (bb
, dump_file
, 0);
335 if (bb
->count
!= sum_edge_counts (bb
->preds
))
339 fprintf (dump_file
, "BB %i count does not match sum of incoming edges "
340 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
343 sum_edge_counts (bb
->preds
));
344 dump_bb (bb
, dump_file
, 0);
348 if (bb
->count
!= sum_edge_counts (bb
->succs
) &&
349 ! (find_edge (bb
, EXIT_BLOCK_PTR
) != NULL
&& block_ends_with_call_p (bb
)))
353 fprintf (dump_file
, "BB %i count does not match sum of outgoing edges "
354 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
357 sum_edge_counts (bb
->succs
));
358 dump_bb (bb
, dump_file
, 0);
362 if (!dump_file
&& inconsistent
)
369 /* Set each basic block count to the sum of its outgoing edge counts */
374 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
376 bb
->count
= sum_edge_counts (bb
->succs
);
377 gcc_assert (bb
->count
>= 0);
381 /* Reads profile data and returns total number of edge counts read */
383 read_profile_edge_counts (gcov_type
*exec_counts
)
387 int exec_counts_pos
= 0;
388 /* For each edge not on the spanning tree, set its execution count from
390 /* The first count in the .da file is the number of times that the function
391 was entered. This is the exec_count for block zero. */
393 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
398 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
399 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
404 e
->count
= exec_counts
[exec_counts_pos
++];
405 if (e
->count
> profile_info
->sum_max
)
407 if (flag_profile_correction
)
409 static bool informed
= 0;
411 inform (input_location
,
412 "corrupted profile info: edge count exceeds maximal count");
416 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
417 bb
->index
, e
->dest
->index
);
423 EDGE_INFO (e
)->count_valid
= 1;
424 BB_INFO (bb
)->succ_count
--;
425 BB_INFO (e
->dest
)->pred_count
--;
428 fprintf (dump_file
, "\nRead edge from %i to %i, count:",
429 bb
->index
, e
->dest
->index
);
430 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
431 (HOST_WIDEST_INT
) e
->count
);
439 #define OVERLAP_BASE 10000
441 /* Compare the static estimated profile to the actual profile, and
442 return the "degree of overlap" measure between them.
444 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
445 the sum of each basic block's minimum relative weights between
446 two profiles. And overlap of OVERLAP_BASE means two profiles are
450 compute_frequency_overlap (void)
452 gcov_type count_total
= 0, freq_total
= 0;
456 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
458 count_total
+= bb
->count
;
459 freq_total
+= bb
->frequency
;
462 if (count_total
== 0 || freq_total
== 0)
465 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
466 overlap
+= MIN (bb
->count
* OVERLAP_BASE
/ count_total
,
467 bb
->frequency
* OVERLAP_BASE
/ freq_total
);
472 /* Compute the branch probabilities for the various branches.
473 Annotate them accordingly.
475 CFG_CHECKSUM is the precomputed checksum for the CFG. */
478 compute_branch_probabilities (unsigned cfg_checksum
, unsigned lineno_checksum
)
485 int hist_br_prob
[20];
487 gcov_type
*exec_counts
= get_exec_counts (cfg_checksum
, lineno_checksum
);
488 int inconsistent
= 0;
490 /* Very simple sanity checks so we catch bugs in our profiling code. */
493 if (profile_info
->run_max
* profile_info
->runs
< profile_info
->sum_max
)
495 error ("corrupted profile info: run_max * runs < sum_max");
499 if (profile_info
->sum_all
< profile_info
->sum_max
)
501 error ("corrupted profile info: sum_all is smaller than sum_max");
505 /* Attach extra info block to each bb. */
506 alloc_aux_for_blocks (sizeof (struct bb_info
));
507 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
512 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
513 if (!EDGE_INFO (e
)->ignore
)
514 BB_INFO (bb
)->succ_count
++;
515 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
516 if (!EDGE_INFO (e
)->ignore
)
517 BB_INFO (bb
)->pred_count
++;
520 /* Avoid predicting entry on exit nodes. */
521 BB_INFO (EXIT_BLOCK_PTR
)->succ_count
= 2;
522 BB_INFO (ENTRY_BLOCK_PTR
)->pred_count
= 2;
524 num_edges
= read_profile_edge_counts (exec_counts
);
527 fprintf (dump_file
, "\n%d edge counts read\n", num_edges
);
529 /* For every block in the file,
530 - if every exit/entrance edge has a known count, then set the block count
531 - if the block count is known, and every exit/entrance edge but one has
532 a known execution count, then set the count of the remaining edge
534 As edge counts are set, decrement the succ/pred count, but don't delete
535 the edge, that way we can easily tell when all edges are known, or only
536 one edge is unknown. */
538 /* The order that the basic blocks are iterated through is important.
539 Since the code that finds spanning trees starts with block 0, low numbered
540 edges are put on the spanning tree in preference to high numbered edges.
541 Hence, most instrumented edges are at the end. Graph solving works much
542 faster if we propagate numbers from the end to the start.
544 This takes an average of slightly more than 3 passes. */
552 FOR_BB_BETWEEN (bb
, EXIT_BLOCK_PTR
, NULL
, prev_bb
)
554 struct bb_info
*bi
= BB_INFO (bb
);
555 if (! bi
->count_valid
)
557 if (bi
->succ_count
== 0)
563 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
569 else if (bi
->pred_count
== 0)
575 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
584 if (bi
->succ_count
== 1)
590 /* One of the counts will be invalid, but it is zero,
591 so adding it in also doesn't hurt. */
592 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
595 /* Search for the invalid edge, and set its count. */
596 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
597 if (! EDGE_INFO (e
)->count_valid
&& ! EDGE_INFO (e
)->ignore
)
600 /* Calculate count for remaining edge by conservation. */
601 total
= bb
->count
- total
;
604 EDGE_INFO (e
)->count_valid
= 1;
608 BB_INFO (e
->dest
)->pred_count
--;
611 if (bi
->pred_count
== 1)
617 /* One of the counts will be invalid, but it is zero,
618 so adding it in also doesn't hurt. */
619 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
622 /* Search for the invalid edge, and set its count. */
623 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
624 if (!EDGE_INFO (e
)->count_valid
&& !EDGE_INFO (e
)->ignore
)
627 /* Calculate count for remaining edge by conservation. */
628 total
= bb
->count
- total
+ e
->count
;
631 EDGE_INFO (e
)->count_valid
= 1;
635 BB_INFO (e
->src
)->succ_count
--;
643 int overlap
= compute_frequency_overlap ();
644 dump_flow_info (dump_file
, dump_flags
);
645 fprintf (dump_file
, "Static profile overlap: %d.%d%%\n",
646 overlap
/ (OVERLAP_BASE
/ 100),
647 overlap
% (OVERLAP_BASE
/ 100));
650 total_num_passes
+= passes
;
652 fprintf (dump_file
, "Graph solving took %d passes.\n\n", passes
);
654 /* If the graph has been correctly solved, every block will have a
655 succ and pred count of zero. */
658 gcc_assert (!BB_INFO (bb
)->succ_count
&& !BB_INFO (bb
)->pred_count
);
661 /* Check for inconsistent basic block counts */
662 inconsistent
= is_inconsistent ();
666 if (flag_profile_correction
)
668 /* Inconsistency detected. Make it flow-consistent. */
669 static int informed
= 0;
673 inform (input_location
, "correcting inconsistent profile data");
675 correct_negative_edge_counts ();
676 /* Set bb counts to the sum of the outgoing edge counts */
679 fprintf (dump_file
, "\nCalling mcf_smooth_cfg\n");
683 error ("corrupted profile info: profile data is not flow-consistent");
686 /* For every edge, calculate its branch probability and add a reg_note
687 to the branch insn to indicate this. */
689 for (i
= 0; i
< 20; i
++)
693 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
700 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
701 bb
->index
, (int)bb
->count
);
704 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
706 /* Function may return twice in the cased the called function is
707 setjmp or calls fork, but we can't represent this by extra
708 edge from the entry, since extra edge from the exit is
709 already present. We get negative frequency from the entry
712 && e
->dest
== EXIT_BLOCK_PTR
)
713 || (e
->count
> bb
->count
714 && e
->dest
!= EXIT_BLOCK_PTR
))
716 if (block_ends_with_call_p (bb
))
717 e
->count
= e
->count
< 0 ? 0 : bb
->count
;
719 if (e
->count
< 0 || e
->count
> bb
->count
)
721 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
722 e
->src
->index
, e
->dest
->index
,
724 e
->count
= bb
->count
/ 2;
729 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
730 e
->probability
= (e
->count
* REG_BR_PROB_BASE
+ bb
->count
/ 2) / bb
->count
;
731 if (bb
->index
>= NUM_FIXED_BLOCKS
732 && block_ends_with_condjump_p (bb
)
733 && EDGE_COUNT (bb
->succs
) >= 2)
739 /* Find the branch edge. It is possible that we do have fake
741 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
742 if (!(e
->flags
& (EDGE_FAKE
| EDGE_FALLTHRU
)))
745 prob
= e
->probability
;
746 index
= prob
* 20 / REG_BR_PROB_BASE
;
750 hist_br_prob
[index
]++;
755 /* As a last resort, distribute the probabilities evenly.
756 Use simple heuristics that if there are normal edges,
757 give all abnormals frequency of 0, otherwise distribute the
758 frequency over abnormals (this is the case of noreturn
760 else if (profile_status
== PROFILE_ABSENT
)
764 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
765 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
769 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
770 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
771 e
->probability
= REG_BR_PROB_BASE
/ total
;
777 total
+= EDGE_COUNT (bb
->succs
);
778 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
779 e
->probability
= REG_BR_PROB_BASE
/ total
;
781 if (bb
->index
>= NUM_FIXED_BLOCKS
782 && block_ends_with_condjump_p (bb
)
783 && EDGE_COUNT (bb
->succs
) >= 2)
788 profile_status
= PROFILE_READ
;
789 compute_function_frequency ();
793 fprintf (dump_file
, "%d branches\n", num_branches
);
795 for (i
= 0; i
< 10; i
++)
796 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
797 (hist_br_prob
[i
] + hist_br_prob
[19-i
]) * 100 / num_branches
,
800 total_num_branches
+= num_branches
;
801 for (i
= 0; i
< 20; i
++)
802 total_hist_br_prob
[i
] += hist_br_prob
[i
];
804 fputc ('\n', dump_file
);
805 fputc ('\n', dump_file
);
808 free_aux_for_blocks ();
811 /* Load value histograms values whose description is stored in VALUES array
814 CFG_CHECKSUM is the precomputed checksum for the CFG. */
817 compute_value_histograms (histogram_values values
, unsigned cfg_checksum
,
818 unsigned lineno_checksum
)
820 unsigned i
, j
, t
, any
;
821 unsigned n_histogram_counters
[GCOV_N_VALUE_COUNTERS
];
822 gcov_type
*histogram_counts
[GCOV_N_VALUE_COUNTERS
];
823 gcov_type
*act_count
[GCOV_N_VALUE_COUNTERS
];
824 gcov_type
*aact_count
;
826 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
827 n_histogram_counters
[t
] = 0;
829 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
831 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
832 n_histogram_counters
[(int) hist
->type
] += hist
->n_counters
;
836 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
838 if (!n_histogram_counters
[t
])
840 histogram_counts
[t
] = NULL
;
844 histogram_counts
[t
] =
845 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t
),
846 n_histogram_counters
[t
], cfg_checksum
,
847 lineno_checksum
, NULL
);
848 if (histogram_counts
[t
])
850 act_count
[t
] = histogram_counts
[t
];
855 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
857 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
858 gimple stmt
= hist
->hvalue
.stmt
;
860 t
= (int) hist
->type
;
862 aact_count
= act_count
[t
];
863 act_count
[t
] += hist
->n_counters
;
865 gimple_add_histogram_value (cfun
, stmt
, hist
);
866 hist
->hvalue
.counters
= XNEWVEC (gcov_type
, hist
->n_counters
);
867 for (j
= 0; j
< hist
->n_counters
; j
++)
868 hist
->hvalue
.counters
[j
] = aact_count
[j
];
871 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
872 free (histogram_counts
[t
]);
875 /* The entry basic block will be moved around so that it has index=1,
876 there is nothing at index 0 and the exit is at n_basic_block. */
877 #define BB_TO_GCOV_INDEX(bb) ((bb)->index - 1)
878 /* When passed NULL as file_name, initialize.
879 When passed something else, output the necessary commands to change
880 line to LINE and offset to FILE_NAME. */
882 output_location (char const *file_name
, int line
,
883 gcov_position_t
*offset
, basic_block bb
)
885 static char const *prev_file_name
;
886 static int prev_line
;
887 bool name_differs
, line_differs
;
891 prev_file_name
= NULL
;
896 name_differs
= !prev_file_name
|| filename_cmp (file_name
, prev_file_name
);
897 line_differs
= prev_line
!= line
;
899 if (name_differs
|| line_differs
)
903 *offset
= gcov_write_tag (GCOV_TAG_LINES
);
904 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb
));
905 name_differs
= line_differs
=true;
908 /* If this is a new source file, then output the
909 file's name to the .bb file. */
912 prev_file_name
= file_name
;
913 gcov_write_unsigned (0);
914 gcov_write_string (prev_file_name
);
918 gcov_write_unsigned (line
);
924 /* Instrument and/or analyze program behavior based on program flow graph.
925 In either case, this function builds a flow graph for the function being
926 compiled. The flow graph is stored in BB_GRAPH.
928 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
929 the flow graph that are needed to reconstruct the dynamic behavior of the
932 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
933 information from a data file containing edge count information from previous
934 executions of the function being compiled. In this case, the flow graph is
935 annotated with actual execution counts, which are later propagated into the
936 rtl for optimization purposes.
938 Main entry point of this file. */
945 unsigned num_edges
, ignored_edges
;
946 unsigned num_instrumented
;
947 struct edge_list
*el
;
948 histogram_values values
= NULL
;
949 unsigned cfg_checksum
, lineno_checksum
;
951 total_num_times_called
++;
953 flow_call_edges_add (NULL
);
954 add_noreturn_fake_exit_edges ();
956 /* We can't handle cyclic regions constructed using abnormal edges.
957 To avoid these we replace every source of abnormal edge by a fake
958 edge from entry node and every destination by fake edge to exit.
959 This keeps graph acyclic and our calculation exact for all normal
960 edges except for exit and entrance ones.
962 We also add fake exit edges for each call and asm statement in the
963 basic, since it may not return. */
967 int need_exit_edge
= 0, need_entry_edge
= 0;
968 int have_exit_edge
= 0, have_entry_edge
= 0;
972 /* Functions returning multiple times are not handled by extra edges.
973 Instead we simply allow negative counts on edges from exit to the
974 block past call and corresponding probabilities. We can't go
975 with the extra edges because that would result in flowgraph that
976 needs to have fake edges outside the spanning tree. */
978 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
980 gimple_stmt_iterator gsi
;
983 /* It may happen that there are compiler generated statements
984 without a locus at all. Go through the basic block from the
985 last to the first statement looking for a locus. */
986 for (gsi
= gsi_last_nondebug_bb (bb
);
988 gsi_prev_nondebug (&gsi
))
990 last
= gsi_stmt (gsi
);
991 if (gimple_has_location (last
))
995 /* Edge with goto locus might get wrong coverage info unless
996 it is the only edge out of BB.
997 Don't do that when the locuses match, so
998 if (blah) goto something;
999 is not computed twice. */
1001 && gimple_has_location (last
)
1002 && e
->goto_locus
!= UNKNOWN_LOCATION
1003 && !single_succ_p (bb
)
1004 && (LOCATION_FILE (e
->goto_locus
)
1005 != LOCATION_FILE (gimple_location (last
))
1006 || (LOCATION_LINE (e
->goto_locus
)
1007 != LOCATION_LINE (gimple_location (last
)))))
1009 basic_block new_bb
= split_edge (e
);
1010 edge ne
= single_succ_edge (new_bb
);
1011 ne
->goto_locus
= e
->goto_locus
;
1012 ne
->goto_block
= e
->goto_block
;
1014 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1015 && e
->dest
!= EXIT_BLOCK_PTR
)
1017 if (e
->dest
== EXIT_BLOCK_PTR
)
1020 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1022 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1023 && e
->src
!= ENTRY_BLOCK_PTR
)
1024 need_entry_edge
= 1;
1025 if (e
->src
== ENTRY_BLOCK_PTR
)
1026 have_entry_edge
= 1;
1029 if (need_exit_edge
&& !have_exit_edge
)
1032 fprintf (dump_file
, "Adding fake exit edge to bb %i\n",
1034 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
1036 if (need_entry_edge
&& !have_entry_edge
)
1039 fprintf (dump_file
, "Adding fake entry edge to bb %i\n",
1041 make_edge (ENTRY_BLOCK_PTR
, bb
, EDGE_FAKE
);
1042 /* Avoid bbs that have both fake entry edge and also some
1043 exit edge. One of those edges wouldn't be added to the
1044 spanning tree, but we can't instrument any of them. */
1045 if (have_exit_edge
|| need_exit_edge
)
1047 gimple_stmt_iterator gsi
;
1051 gsi
= gsi_after_labels (bb
);
1052 gcc_checking_assert (!gsi_end_p (gsi
));
1053 first
= gsi_stmt (gsi
);
1054 if (is_gimple_debug (first
))
1056 gsi_next_nondebug (&gsi
);
1057 gcc_checking_assert (!gsi_end_p (gsi
));
1058 first
= gsi_stmt (gsi
);
1060 /* Don't split the bbs containing __builtin_setjmp_receiver
1061 or __builtin_setjmp_dispatcher calls. These are very
1062 special and don't expect anything to be inserted before
1064 if (!is_gimple_call (first
)
1065 || (fndecl
= gimple_call_fndecl (first
)) == NULL
1066 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
1067 || (DECL_FUNCTION_CODE (fndecl
) != BUILT_IN_SETJMP_RECEIVER
1068 && (DECL_FUNCTION_CODE (fndecl
)
1069 != BUILT_IN_SETJMP_DISPATCHER
)))
1072 fprintf (dump_file
, "Splitting bb %i after labels\n",
1074 split_block_after_labels (bb
);
1080 el
= create_edge_list ();
1081 num_edges
= NUM_EDGES (el
);
1082 alloc_aux_for_edges (sizeof (struct edge_info
));
1084 /* The basic blocks are expected to be numbered sequentially. */
1088 for (i
= 0 ; i
< num_edges
; i
++)
1090 edge e
= INDEX_EDGE (el
, i
);
1093 /* Mark edges we've replaced by fake edges above as ignored. */
1094 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1095 && e
->src
!= ENTRY_BLOCK_PTR
&& e
->dest
!= EXIT_BLOCK_PTR
)
1097 EDGE_INFO (e
)->ignore
= 1;
1102 /* Create spanning tree from basic block graph, mark each edge that is
1103 on the spanning tree. We insert as many abnormal and critical edges
1104 as possible to minimize number of edge splits necessary. */
1106 find_spanning_tree (el
);
1108 /* Fake edges that are not on the tree will not be instrumented, so
1109 mark them ignored. */
1110 for (num_instrumented
= i
= 0; i
< num_edges
; i
++)
1112 edge e
= INDEX_EDGE (el
, i
);
1113 struct edge_info
*inf
= EDGE_INFO (e
);
1115 if (inf
->ignore
|| inf
->on_tree
)
1117 else if (e
->flags
& EDGE_FAKE
)
1126 total_num_blocks
+= n_basic_blocks
;
1128 fprintf (dump_file
, "%d basic blocks\n", n_basic_blocks
);
1130 total_num_edges
+= num_edges
;
1132 fprintf (dump_file
, "%d edges\n", num_edges
);
1134 total_num_edges_ignored
+= ignored_edges
;
1136 fprintf (dump_file
, "%d ignored edges\n", ignored_edges
);
1139 /* Compute two different checksums. Note that we want to compute
1140 the checksum in only once place, since it depends on the shape
1141 of the control flow which can change during
1142 various transformations. */
1143 cfg_checksum
= coverage_compute_cfg_checksum ();
1144 lineno_checksum
= coverage_compute_lineno_checksum ();
1146 /* Write the data from which gcov can reconstruct the basic block
1147 graph and function line numbers */
1149 if (coverage_begin_function (lineno_checksum
, cfg_checksum
))
1151 gcov_position_t offset
;
1153 /* Basic block flags */
1154 offset
= gcov_write_tag (GCOV_TAG_BLOCKS
);
1155 for (i
= 0; i
!= (unsigned) (n_basic_blocks
); i
++)
1156 gcov_write_unsigned (0);
1157 gcov_write_length (offset
);
1159 /* Keep all basic block indexes nonnegative in the gcov output.
1160 Index 0 is used for entry block, last index is for exit
1162 ENTRY_BLOCK_PTR
->index
= 1;
1163 EXIT_BLOCK_PTR
->index
= last_basic_block
;
1166 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
1171 offset
= gcov_write_tag (GCOV_TAG_ARCS
);
1172 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb
));
1174 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1176 struct edge_info
*i
= EDGE_INFO (e
);
1179 unsigned flag_bits
= 0;
1182 flag_bits
|= GCOV_ARC_ON_TREE
;
1183 if (e
->flags
& EDGE_FAKE
)
1184 flag_bits
|= GCOV_ARC_FAKE
;
1185 if (e
->flags
& EDGE_FALLTHRU
)
1186 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1187 /* On trees we don't have fallthru flags, but we can
1188 recompute them from CFG shape. */
1189 if (e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)
1190 && e
->src
->next_bb
== e
->dest
)
1191 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1193 gcov_write_unsigned (BB_TO_GCOV_INDEX (e
->dest
));
1194 gcov_write_unsigned (flag_bits
);
1198 gcov_write_length (offset
);
1201 ENTRY_BLOCK_PTR
->index
= ENTRY_BLOCK
;
1202 EXIT_BLOCK_PTR
->index
= EXIT_BLOCK
;
1205 /* Initialize the output. */
1206 output_location (NULL
, 0, NULL
, NULL
);
1210 gimple_stmt_iterator gsi
;
1211 gcov_position_t offset
= 0;
1213 if (bb
== ENTRY_BLOCK_PTR
->next_bb
)
1215 expanded_location curr_location
=
1216 expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
1217 output_location (curr_location
.file
, curr_location
.line
,
1221 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1223 gimple stmt
= gsi_stmt (gsi
);
1224 if (gimple_has_location (stmt
))
1225 output_location (gimple_filename (stmt
), gimple_lineno (stmt
),
1229 /* Notice GOTO expressions eliminated while constructing the CFG. */
1230 if (single_succ_p (bb
)
1231 && single_succ_edge (bb
)->goto_locus
!= UNKNOWN_LOCATION
)
1233 expanded_location curr_location
1234 = expand_location (single_succ_edge (bb
)->goto_locus
);
1235 output_location (curr_location
.file
, curr_location
.line
,
1241 /* A file of NULL indicates the end of run. */
1242 gcov_write_unsigned (0);
1243 gcov_write_string (NULL
);
1244 gcov_write_length (offset
);
1249 #undef BB_TO_GCOV_INDEX
1251 if (flag_profile_values
)
1252 gimple_find_values_to_profile (&values
);
1254 if (flag_branch_probabilities
)
1256 compute_branch_probabilities (cfg_checksum
, lineno_checksum
);
1257 if (flag_profile_values
)
1258 compute_value_histograms (values
, cfg_checksum
, lineno_checksum
);
1261 remove_fake_edges ();
1263 /* For each edge not on the spanning tree, add counting code. */
1264 if (profile_arc_flag
1265 && coverage_counter_alloc (GCOV_COUNTER_ARCS
, num_instrumented
))
1267 unsigned n_instrumented
;
1269 gimple_init_edge_profiler ();
1271 n_instrumented
= instrument_edges (el
);
1273 gcc_assert (n_instrumented
== num_instrumented
);
1275 if (flag_profile_values
)
1276 instrument_values (values
);
1278 /* Commit changes done by instrumentation. */
1279 gsi_commit_edge_inserts ();
1282 free_aux_for_edges ();
1284 VEC_free (histogram_value
, heap
, values
);
1285 free_edge_list (el
);
1286 coverage_end_function (lineno_checksum
, cfg_checksum
);
1289 /* Union find algorithm implementation for the basic blocks using
1293 find_group (basic_block bb
)
1295 basic_block group
= bb
, bb1
;
1297 while ((basic_block
) group
->aux
!= group
)
1298 group
= (basic_block
) group
->aux
;
1300 /* Compress path. */
1301 while ((basic_block
) bb
->aux
!= group
)
1303 bb1
= (basic_block
) bb
->aux
;
1304 bb
->aux
= (void *) group
;
1311 union_groups (basic_block bb1
, basic_block bb2
)
1313 basic_block bb1g
= find_group (bb1
);
1314 basic_block bb2g
= find_group (bb2
);
1316 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1317 this code is unlikely going to be performance problem anyway. */
1318 gcc_assert (bb1g
!= bb2g
);
1323 /* This function searches all of the edges in the program flow graph, and puts
1324 as many bad edges as possible onto the spanning tree. Bad edges include
1325 abnormals edges, which can't be instrumented at the moment. Since it is
1326 possible for fake edges to form a cycle, we will have to develop some
1327 better way in the future. Also put critical edges to the tree, since they
1328 are more expensive to instrument. */
1331 find_spanning_tree (struct edge_list
*el
)
1334 int num_edges
= NUM_EDGES (el
);
1337 /* We use aux field for standard union-find algorithm. */
1338 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1341 /* Add fake edge exit to entry we can't instrument. */
1342 union_groups (EXIT_BLOCK_PTR
, ENTRY_BLOCK_PTR
);
1344 /* First add all abnormal edges to the tree unless they form a cycle. Also
1345 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1346 setting return value from function. */
1347 for (i
= 0; i
< num_edges
; i
++)
1349 edge e
= INDEX_EDGE (el
, i
);
1350 if (((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
))
1351 || e
->dest
== EXIT_BLOCK_PTR
)
1352 && !EDGE_INFO (e
)->ignore
1353 && (find_group (e
->src
) != find_group (e
->dest
)))
1356 fprintf (dump_file
, "Abnormal edge %d to %d put to tree\n",
1357 e
->src
->index
, e
->dest
->index
);
1358 EDGE_INFO (e
)->on_tree
= 1;
1359 union_groups (e
->src
, e
->dest
);
1363 /* Now insert all critical edges to the tree unless they form a cycle. */
1364 for (i
= 0; i
< num_edges
; i
++)
1366 edge e
= INDEX_EDGE (el
, i
);
1367 if (EDGE_CRITICAL_P (e
) && !EDGE_INFO (e
)->ignore
1368 && find_group (e
->src
) != find_group (e
->dest
))
1371 fprintf (dump_file
, "Critical edge %d to %d put to tree\n",
1372 e
->src
->index
, e
->dest
->index
);
1373 EDGE_INFO (e
)->on_tree
= 1;
1374 union_groups (e
->src
, e
->dest
);
1378 /* And now the rest. */
1379 for (i
= 0; i
< num_edges
; i
++)
1381 edge e
= INDEX_EDGE (el
, i
);
1382 if (!EDGE_INFO (e
)->ignore
1383 && find_group (e
->src
) != find_group (e
->dest
))
1386 fprintf (dump_file
, "Normal edge %d to %d put to tree\n",
1387 e
->src
->index
, e
->dest
->index
);
1388 EDGE_INFO (e
)->on_tree
= 1;
1389 union_groups (e
->src
, e
->dest
);
1393 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1397 /* Perform file-level initialization for branch-prob processing. */
1400 init_branch_prob (void)
1404 total_num_blocks
= 0;
1405 total_num_edges
= 0;
1406 total_num_edges_ignored
= 0;
1407 total_num_edges_instrumented
= 0;
1408 total_num_blocks_created
= 0;
1409 total_num_passes
= 0;
1410 total_num_times_called
= 0;
1411 total_num_branches
= 0;
1412 for (i
= 0; i
< 20; i
++)
1413 total_hist_br_prob
[i
] = 0;
1416 /* Performs file-level cleanup after branch-prob processing
1420 end_branch_prob (void)
1424 fprintf (dump_file
, "\n");
1425 fprintf (dump_file
, "Total number of blocks: %d\n",
1427 fprintf (dump_file
, "Total number of edges: %d\n", total_num_edges
);
1428 fprintf (dump_file
, "Total number of ignored edges: %d\n",
1429 total_num_edges_ignored
);
1430 fprintf (dump_file
, "Total number of instrumented edges: %d\n",
1431 total_num_edges_instrumented
);
1432 fprintf (dump_file
, "Total number of blocks created: %d\n",
1433 total_num_blocks_created
);
1434 fprintf (dump_file
, "Total number of graph solution passes: %d\n",
1436 if (total_num_times_called
!= 0)
1437 fprintf (dump_file
, "Average number of graph solution passes: %d\n",
1438 (total_num_passes
+ (total_num_times_called
>> 1))
1439 / total_num_times_called
);
1440 fprintf (dump_file
, "Total number of branches: %d\n",
1441 total_num_branches
);
1442 if (total_num_branches
)
1446 for (i
= 0; i
< 10; i
++)
1447 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
1448 (total_hist_br_prob
[i
] + total_hist_br_prob
[19-i
]) * 100
1449 / total_num_branches
, 5*i
, 5*i
+5);