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
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"
62 #include "basic-block.h"
63 #include "diagnostic-core.h"
65 #include "value-prof.h"
68 #include "tree-flow.h"
71 #include "tree-pass.h"
76 unsigned int count_valid
: 1;
78 /* Number of successor and predecessor edges. */
83 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
86 /* Counter summary from the last set of coverage counts read. */
88 const struct gcov_ctr_summary
*profile_info
;
90 /* Collect statistics on the performance of this pass for the entire source
93 static int total_num_blocks
;
94 static int total_num_edges
;
95 static int total_num_edges_ignored
;
96 static int total_num_edges_instrumented
;
97 static int total_num_blocks_created
;
98 static int total_num_passes
;
99 static int total_num_times_called
;
100 static int total_hist_br_prob
[20];
101 static int total_num_branches
;
103 /* Forward declarations. */
104 static void find_spanning_tree (struct edge_list
*);
105 static unsigned instrument_edges (struct edge_list
*);
106 static void instrument_values (histogram_values
);
107 static void compute_branch_probabilities (void);
108 static void compute_value_histograms (histogram_values
);
109 static gcov_type
* get_exec_counts (void);
110 static basic_block
find_group (basic_block
);
111 static void union_groups (basic_block
, basic_block
);
113 /* Add edge instrumentation code to the entire insn chain.
115 F is the first insn of the chain.
116 NUM_BLOCKS is the number of basic blocks found in F. */
119 instrument_edges (struct edge_list
*el
)
121 unsigned num_instr_edges
= 0;
122 int num_edges
= NUM_EDGES (el
);
125 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
130 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
132 struct edge_info
*inf
= EDGE_INFO (e
);
134 if (!inf
->ignore
&& !inf
->on_tree
)
136 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
138 fprintf (dump_file
, "Edge %d to %d instrumented%s\n",
139 e
->src
->index
, e
->dest
->index
,
140 EDGE_CRITICAL_P (e
) ? " (and split)" : "");
141 gimple_gen_edge_profiler (num_instr_edges
++, e
);
146 total_num_blocks_created
+= num_edges
;
148 fprintf (dump_file
, "%d edges instrumented\n", num_instr_edges
);
149 return num_instr_edges
;
152 /* Add code to measure histograms for values in list VALUES. */
154 instrument_values (histogram_values values
)
158 /* Emit code to generate the histograms before the insns. */
160 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
162 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
165 case HIST_TYPE_INTERVAL
:
166 t
= GCOV_COUNTER_V_INTERVAL
;
170 t
= GCOV_COUNTER_V_POW2
;
173 case HIST_TYPE_SINGLE_VALUE
:
174 t
= GCOV_COUNTER_V_SINGLE
;
177 case HIST_TYPE_CONST_DELTA
:
178 t
= GCOV_COUNTER_V_DELTA
;
181 case HIST_TYPE_INDIR_CALL
:
182 t
= GCOV_COUNTER_V_INDIR
;
185 case HIST_TYPE_AVERAGE
:
186 t
= GCOV_COUNTER_AVERAGE
;
190 t
= GCOV_COUNTER_IOR
;
196 if (!coverage_counter_alloc (t
, hist
->n_counters
))
201 case HIST_TYPE_INTERVAL
:
202 gimple_gen_interval_profiler (hist
, t
, 0);
206 gimple_gen_pow2_profiler (hist
, t
, 0);
209 case HIST_TYPE_SINGLE_VALUE
:
210 gimple_gen_one_value_profiler (hist
, t
, 0);
213 case HIST_TYPE_CONST_DELTA
:
214 gimple_gen_const_delta_profiler (hist
, t
, 0);
217 case HIST_TYPE_INDIR_CALL
:
218 gimple_gen_ic_profiler (hist
, t
, 0);
221 case HIST_TYPE_AVERAGE
:
222 gimple_gen_average_profiler (hist
, t
, 0);
226 gimple_gen_ior_profiler (hist
, t
, 0);
236 /* Computes hybrid profile for all matching entries in da_file. */
239 get_exec_counts (void)
241 unsigned num_edges
= 0;
245 /* Count the edges to be (possibly) instrumented. */
246 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
251 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
252 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
256 counts
= get_coverage_counts (GCOV_COUNTER_ARCS
, num_edges
, &profile_info
);
260 if (dump_file
&& profile_info
)
261 fprintf(dump_file
, "Merged %u profiles with maximal count %u.\n",
262 profile_info
->runs
, (unsigned) profile_info
->sum_max
);
269 is_edge_inconsistent (VEC(edge
,gc
) *edges
)
273 FOR_EACH_EDGE (e
, ei
, edges
)
275 if (!EDGE_INFO (e
)->ignore
)
278 && (!(e
->flags
& EDGE_FAKE
)
279 || !block_ends_with_call_p (e
->src
)))
284 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC
,
285 e
->src
->index
, e
->dest
->index
, e
->count
);
286 dump_bb (e
->src
, dump_file
, 0);
287 dump_bb (e
->dest
, dump_file
, 0);
297 correct_negative_edge_counts (void)
303 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
305 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
313 /* Check consistency.
314 Return true if inconsistency is found. */
316 is_inconsistent (void)
319 bool inconsistent
= false;
322 inconsistent
|= is_edge_inconsistent (bb
->preds
);
323 if (!dump_file
&& inconsistent
)
325 inconsistent
|= is_edge_inconsistent (bb
->succs
);
326 if (!dump_file
&& inconsistent
)
332 fprintf (dump_file
, "BB %i count is negative "
333 HOST_WIDEST_INT_PRINT_DEC
,
336 dump_bb (bb
, dump_file
, 0);
340 if (bb
->count
!= sum_edge_counts (bb
->preds
))
344 fprintf (dump_file
, "BB %i count does not match sum of incoming edges "
345 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
348 sum_edge_counts (bb
->preds
));
349 dump_bb (bb
, dump_file
, 0);
353 if (bb
->count
!= sum_edge_counts (bb
->succs
) &&
354 ! (find_edge (bb
, EXIT_BLOCK_PTR
) != NULL
&& block_ends_with_call_p (bb
)))
358 fprintf (dump_file
, "BB %i count does not match sum of outgoing edges "
359 HOST_WIDEST_INT_PRINT_DEC
" should be " HOST_WIDEST_INT_PRINT_DEC
,
362 sum_edge_counts (bb
->succs
));
363 dump_bb (bb
, dump_file
, 0);
367 if (!dump_file
&& inconsistent
)
374 /* Set each basic block count to the sum of its outgoing edge counts */
379 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
381 bb
->count
= sum_edge_counts (bb
->succs
);
382 gcc_assert (bb
->count
>= 0);
386 /* Reads profile data and returns total number of edge counts read */
388 read_profile_edge_counts (gcov_type
*exec_counts
)
392 int exec_counts_pos
= 0;
393 /* For each edge not on the spanning tree, set its execution count from
395 /* The first count in the .da file is the number of times that the function
396 was entered. This is the exec_count for block zero. */
398 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
403 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
404 if (!EDGE_INFO (e
)->ignore
&& !EDGE_INFO (e
)->on_tree
)
409 e
->count
= exec_counts
[exec_counts_pos
++];
410 if (e
->count
> profile_info
->sum_max
)
412 if (flag_profile_correction
)
414 static bool informed
= 0;
416 inform (input_location
,
417 "corrupted profile info: edge count exceeds maximal count");
421 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
422 bb
->index
, e
->dest
->index
);
428 EDGE_INFO (e
)->count_valid
= 1;
429 BB_INFO (bb
)->succ_count
--;
430 BB_INFO (e
->dest
)->pred_count
--;
433 fprintf (dump_file
, "\nRead edge from %i to %i, count:",
434 bb
->index
, e
->dest
->index
);
435 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
,
436 (HOST_WIDEST_INT
) e
->count
);
444 /* Compute the branch probabilities for the various branches.
445 Annotate them accordingly. */
448 compute_branch_probabilities (void)
455 int hist_br_prob
[20];
457 gcov_type
*exec_counts
= get_exec_counts ();
458 int inconsistent
= 0;
460 /* Very simple sanity checks so we catch bugs in our profiling code. */
463 if (profile_info
->run_max
* profile_info
->runs
< profile_info
->sum_max
)
465 error ("corrupted profile info: run_max * runs < sum_max");
469 if (profile_info
->sum_all
< profile_info
->sum_max
)
471 error ("corrupted profile info: sum_all is smaller than sum_max");
475 /* Attach extra info block to each bb. */
476 alloc_aux_for_blocks (sizeof (struct bb_info
));
477 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
482 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
483 if (!EDGE_INFO (e
)->ignore
)
484 BB_INFO (bb
)->succ_count
++;
485 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
486 if (!EDGE_INFO (e
)->ignore
)
487 BB_INFO (bb
)->pred_count
++;
490 /* Avoid predicting entry on exit nodes. */
491 BB_INFO (EXIT_BLOCK_PTR
)->succ_count
= 2;
492 BB_INFO (ENTRY_BLOCK_PTR
)->pred_count
= 2;
494 num_edges
= read_profile_edge_counts (exec_counts
);
497 fprintf (dump_file
, "\n%d edge counts read\n", num_edges
);
499 /* For every block in the file,
500 - if every exit/entrance edge has a known count, then set the block count
501 - if the block count is known, and every exit/entrance edge but one has
502 a known execution count, then set the count of the remaining edge
504 As edge counts are set, decrement the succ/pred count, but don't delete
505 the edge, that way we can easily tell when all edges are known, or only
506 one edge is unknown. */
508 /* The order that the basic blocks are iterated through is important.
509 Since the code that finds spanning trees starts with block 0, low numbered
510 edges are put on the spanning tree in preference to high numbered edges.
511 Hence, most instrumented edges are at the end. Graph solving works much
512 faster if we propagate numbers from the end to the start.
514 This takes an average of slightly more than 3 passes. */
522 FOR_BB_BETWEEN (bb
, EXIT_BLOCK_PTR
, NULL
, prev_bb
)
524 struct bb_info
*bi
= BB_INFO (bb
);
525 if (! bi
->count_valid
)
527 if (bi
->succ_count
== 0)
533 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
539 else if (bi
->pred_count
== 0)
545 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
554 if (bi
->succ_count
== 1)
560 /* One of the counts will be invalid, but it is zero,
561 so adding it in also doesn't hurt. */
562 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
565 /* Search for the invalid edge, and set its count. */
566 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
567 if (! EDGE_INFO (e
)->count_valid
&& ! EDGE_INFO (e
)->ignore
)
570 /* Calculate count for remaining edge by conservation. */
571 total
= bb
->count
- total
;
574 EDGE_INFO (e
)->count_valid
= 1;
578 BB_INFO (e
->dest
)->pred_count
--;
581 if (bi
->pred_count
== 1)
587 /* One of the counts will be invalid, but it is zero,
588 so adding it in also doesn't hurt. */
589 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
592 /* Search for the invalid edge, and set its count. */
593 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
594 if (!EDGE_INFO (e
)->count_valid
&& !EDGE_INFO (e
)->ignore
)
597 /* Calculate count for remaining edge by conservation. */
598 total
= bb
->count
- total
+ e
->count
;
601 EDGE_INFO (e
)->count_valid
= 1;
605 BB_INFO (e
->src
)->succ_count
--;
612 dump_flow_info (dump_file
, dump_flags
);
614 total_num_passes
+= passes
;
616 fprintf (dump_file
, "Graph solving took %d passes.\n\n", passes
);
618 /* If the graph has been correctly solved, every block will have a
619 succ and pred count of zero. */
622 gcc_assert (!BB_INFO (bb
)->succ_count
&& !BB_INFO (bb
)->pred_count
);
625 /* Check for inconsistent basic block counts */
626 inconsistent
= is_inconsistent ();
630 if (flag_profile_correction
)
632 /* Inconsistency detected. Make it flow-consistent. */
633 static int informed
= 0;
637 inform (input_location
, "correcting inconsistent profile data");
639 correct_negative_edge_counts ();
640 /* Set bb counts to the sum of the outgoing edge counts */
643 fprintf (dump_file
, "\nCalling mcf_smooth_cfg\n");
647 error ("corrupted profile info: profile data is not flow-consistent");
650 /* For every edge, calculate its branch probability and add a reg_note
651 to the branch insn to indicate this. */
653 for (i
= 0; i
< 20; i
++)
657 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
664 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
665 bb
->index
, (int)bb
->count
);
668 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
670 /* Function may return twice in the cased the called function is
671 setjmp or calls fork, but we can't represent this by extra
672 edge from the entry, since extra edge from the exit is
673 already present. We get negative frequency from the entry
676 && e
->dest
== EXIT_BLOCK_PTR
)
677 || (e
->count
> bb
->count
678 && e
->dest
!= EXIT_BLOCK_PTR
))
680 if (block_ends_with_call_p (bb
))
681 e
->count
= e
->count
< 0 ? 0 : bb
->count
;
683 if (e
->count
< 0 || e
->count
> bb
->count
)
685 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
686 e
->src
->index
, e
->dest
->index
,
688 e
->count
= bb
->count
/ 2;
693 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
694 e
->probability
= (e
->count
* REG_BR_PROB_BASE
+ bb
->count
/ 2) / bb
->count
;
695 if (bb
->index
>= NUM_FIXED_BLOCKS
696 && block_ends_with_condjump_p (bb
)
697 && EDGE_COUNT (bb
->succs
) >= 2)
703 /* Find the branch edge. It is possible that we do have fake
705 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
706 if (!(e
->flags
& (EDGE_FAKE
| EDGE_FALLTHRU
)))
709 prob
= e
->probability
;
710 index
= prob
* 20 / REG_BR_PROB_BASE
;
714 hist_br_prob
[index
]++;
719 /* As a last resort, distribute the probabilities evenly.
720 Use simple heuristics that if there are normal edges,
721 give all abnormals frequency of 0, otherwise distribute the
722 frequency over abnormals (this is the case of noreturn
724 else if (profile_status
== PROFILE_ABSENT
)
728 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
729 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
733 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
734 if (!(e
->flags
& (EDGE_COMPLEX
| EDGE_FAKE
)))
735 e
->probability
= REG_BR_PROB_BASE
/ total
;
741 total
+= EDGE_COUNT (bb
->succs
);
742 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
743 e
->probability
= REG_BR_PROB_BASE
/ total
;
745 if (bb
->index
>= NUM_FIXED_BLOCKS
746 && block_ends_with_condjump_p (bb
)
747 && EDGE_COUNT (bb
->succs
) >= 2)
752 profile_status
= PROFILE_READ
;
756 fprintf (dump_file
, "%d branches\n", num_branches
);
758 for (i
= 0; i
< 10; i
++)
759 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
760 (hist_br_prob
[i
] + hist_br_prob
[19-i
]) * 100 / num_branches
,
763 total_num_branches
+= num_branches
;
764 for (i
= 0; i
< 20; i
++)
765 total_hist_br_prob
[i
] += hist_br_prob
[i
];
767 fputc ('\n', dump_file
);
768 fputc ('\n', dump_file
);
771 free_aux_for_blocks ();
774 /* Load value histograms values whose description is stored in VALUES array
778 compute_value_histograms (histogram_values values
)
780 unsigned i
, j
, t
, any
;
781 unsigned n_histogram_counters
[GCOV_N_VALUE_COUNTERS
];
782 gcov_type
*histogram_counts
[GCOV_N_VALUE_COUNTERS
];
783 gcov_type
*act_count
[GCOV_N_VALUE_COUNTERS
];
784 gcov_type
*aact_count
;
786 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
787 n_histogram_counters
[t
] = 0;
789 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
791 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
792 n_histogram_counters
[(int) hist
->type
] += hist
->n_counters
;
796 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
798 if (!n_histogram_counters
[t
])
800 histogram_counts
[t
] = NULL
;
804 histogram_counts
[t
] =
805 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t
),
806 n_histogram_counters
[t
], NULL
);
807 if (histogram_counts
[t
])
809 act_count
[t
] = histogram_counts
[t
];
814 for (i
= 0; i
< VEC_length (histogram_value
, values
); i
++)
816 histogram_value hist
= VEC_index (histogram_value
, values
, i
);
817 gimple stmt
= hist
->hvalue
.stmt
;
819 t
= (int) hist
->type
;
821 aact_count
= act_count
[t
];
822 act_count
[t
] += hist
->n_counters
;
824 gimple_add_histogram_value (cfun
, stmt
, hist
);
825 hist
->hvalue
.counters
= XNEWVEC (gcov_type
, hist
->n_counters
);
826 for (j
= 0; j
< hist
->n_counters
; j
++)
827 hist
->hvalue
.counters
[j
] = aact_count
[j
];
830 for (t
= 0; t
< GCOV_N_VALUE_COUNTERS
; t
++)
831 if (histogram_counts
[t
])
832 free (histogram_counts
[t
]);
835 /* The entry basic block will be moved around so that it has index=1,
836 there is nothing at index 0 and the exit is at n_basic_block. */
837 #define BB_TO_GCOV_INDEX(bb) ((bb)->index - 1)
838 /* When passed NULL as file_name, initialize.
839 When passed something else, output the necessary commands to change
840 line to LINE and offset to FILE_NAME. */
842 output_location (char const *file_name
, int line
,
843 gcov_position_t
*offset
, basic_block bb
)
845 static char const *prev_file_name
;
846 static int prev_line
;
847 bool name_differs
, line_differs
;
851 prev_file_name
= NULL
;
856 name_differs
= !prev_file_name
|| strcmp (file_name
, prev_file_name
);
857 line_differs
= prev_line
!= line
;
859 if (name_differs
|| line_differs
)
863 *offset
= gcov_write_tag (GCOV_TAG_LINES
);
864 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb
));
865 name_differs
= line_differs
=true;
868 /* If this is a new source file, then output the
869 file's name to the .bb file. */
872 prev_file_name
= file_name
;
873 gcov_write_unsigned (0);
874 gcov_write_string (prev_file_name
);
878 gcov_write_unsigned (line
);
884 /* Instrument and/or analyze program behavior based on program flow graph.
885 In either case, this function builds a flow graph for the function being
886 compiled. The flow graph is stored in BB_GRAPH.
888 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
889 the flow graph that are needed to reconstruct the dynamic behavior of the
892 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
893 information from a data file containing edge count information from previous
894 executions of the function being compiled. In this case, the flow graph is
895 annotated with actual execution counts, which are later propagated into the
896 rtl for optimization purposes.
898 Main entry point of this file. */
905 unsigned num_edges
, ignored_edges
;
906 unsigned num_instrumented
;
907 struct edge_list
*el
;
908 histogram_values values
= NULL
;
910 total_num_times_called
++;
912 flow_call_edges_add (NULL
);
913 add_noreturn_fake_exit_edges ();
915 /* We can't handle cyclic regions constructed using abnormal edges.
916 To avoid these we replace every source of abnormal edge by a fake
917 edge from entry node and every destination by fake edge to exit.
918 This keeps graph acyclic and our calculation exact for all normal
919 edges except for exit and entrance ones.
921 We also add fake exit edges for each call and asm statement in the
922 basic, since it may not return. */
926 int need_exit_edge
= 0, need_entry_edge
= 0;
927 int have_exit_edge
= 0, have_entry_edge
= 0;
931 /* Functions returning multiple times are not handled by extra edges.
932 Instead we simply allow negative counts on edges from exit to the
933 block past call and corresponding probabilities. We can't go
934 with the extra edges because that would result in flowgraph that
935 needs to have fake edges outside the spanning tree. */
937 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
939 gimple_stmt_iterator gsi
;
942 /* It may happen that there are compiler generated statements
943 without a locus at all. Go through the basic block from the
944 last to the first statement looking for a locus. */
945 for (gsi
= gsi_last_nondebug_bb (bb
);
947 gsi_prev_nondebug (&gsi
))
949 last
= gsi_stmt (gsi
);
950 if (gimple_has_location (last
))
954 /* Edge with goto locus might get wrong coverage info unless
955 it is the only edge out of BB.
956 Don't do that when the locuses match, so
957 if (blah) goto something;
958 is not computed twice. */
960 && gimple_has_location (last
)
961 && e
->goto_locus
!= UNKNOWN_LOCATION
962 && !single_succ_p (bb
)
963 && (LOCATION_FILE (e
->goto_locus
)
964 != LOCATION_FILE (gimple_location (last
))
965 || (LOCATION_LINE (e
->goto_locus
)
966 != LOCATION_LINE (gimple_location (last
)))))
968 basic_block new_bb
= split_edge (e
);
969 edge ne
= single_succ_edge (new_bb
);
970 ne
->goto_locus
= e
->goto_locus
;
971 ne
->goto_block
= e
->goto_block
;
973 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
974 && e
->dest
!= EXIT_BLOCK_PTR
)
976 if (e
->dest
== EXIT_BLOCK_PTR
)
979 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
981 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
982 && e
->src
!= ENTRY_BLOCK_PTR
)
984 if (e
->src
== ENTRY_BLOCK_PTR
)
988 if (need_exit_edge
&& !have_exit_edge
)
991 fprintf (dump_file
, "Adding fake exit edge to bb %i\n",
993 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
995 if (need_entry_edge
&& !have_entry_edge
)
998 fprintf (dump_file
, "Adding fake entry edge to bb %i\n",
1000 make_edge (ENTRY_BLOCK_PTR
, bb
, EDGE_FAKE
);
1004 el
= create_edge_list ();
1005 num_edges
= NUM_EDGES (el
);
1006 alloc_aux_for_edges (sizeof (struct edge_info
));
1008 /* The basic blocks are expected to be numbered sequentially. */
1012 for (i
= 0 ; i
< num_edges
; i
++)
1014 edge e
= INDEX_EDGE (el
, i
);
1017 /* Mark edges we've replaced by fake edges above as ignored. */
1018 if ((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
))
1019 && e
->src
!= ENTRY_BLOCK_PTR
&& e
->dest
!= EXIT_BLOCK_PTR
)
1021 EDGE_INFO (e
)->ignore
= 1;
1026 /* Create spanning tree from basic block graph, mark each edge that is
1027 on the spanning tree. We insert as many abnormal and critical edges
1028 as possible to minimize number of edge splits necessary. */
1030 find_spanning_tree (el
);
1032 /* Fake edges that are not on the tree will not be instrumented, so
1033 mark them ignored. */
1034 for (num_instrumented
= i
= 0; i
< num_edges
; i
++)
1036 edge e
= INDEX_EDGE (el
, i
);
1037 struct edge_info
*inf
= EDGE_INFO (e
);
1039 if (inf
->ignore
|| inf
->on_tree
)
1041 else if (e
->flags
& EDGE_FAKE
)
1050 total_num_blocks
+= n_basic_blocks
;
1052 fprintf (dump_file
, "%d basic blocks\n", n_basic_blocks
);
1054 total_num_edges
+= num_edges
;
1056 fprintf (dump_file
, "%d edges\n", num_edges
);
1058 total_num_edges_ignored
+= ignored_edges
;
1060 fprintf (dump_file
, "%d ignored edges\n", ignored_edges
);
1062 /* Write the data from which gcov can reconstruct the basic block
1065 /* Basic block flags */
1066 if (coverage_begin_output ())
1068 gcov_position_t offset
;
1070 offset
= gcov_write_tag (GCOV_TAG_BLOCKS
);
1071 for (i
= 0; i
!= (unsigned) (n_basic_blocks
); i
++)
1072 gcov_write_unsigned (0);
1073 gcov_write_length (offset
);
1076 /* Keep all basic block indexes nonnegative in the gcov output.
1077 Index 0 is used for entry block, last index is for exit block.
1079 ENTRY_BLOCK_PTR
->index
= 1;
1080 EXIT_BLOCK_PTR
->index
= last_basic_block
;
1083 if (coverage_begin_output ())
1085 gcov_position_t offset
;
1087 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
1092 offset
= gcov_write_tag (GCOV_TAG_ARCS
);
1093 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb
));
1095 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1097 struct edge_info
*i
= EDGE_INFO (e
);
1100 unsigned flag_bits
= 0;
1103 flag_bits
|= GCOV_ARC_ON_TREE
;
1104 if (e
->flags
& EDGE_FAKE
)
1105 flag_bits
|= GCOV_ARC_FAKE
;
1106 if (e
->flags
& EDGE_FALLTHRU
)
1107 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1108 /* On trees we don't have fallthru flags, but we can
1109 recompute them from CFG shape. */
1110 if (e
->flags
& (EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
)
1111 && e
->src
->next_bb
== e
->dest
)
1112 flag_bits
|= GCOV_ARC_FALLTHROUGH
;
1114 gcov_write_unsigned (BB_TO_GCOV_INDEX (e
->dest
));
1115 gcov_write_unsigned (flag_bits
);
1119 gcov_write_length (offset
);
1124 if (coverage_begin_output ())
1126 gcov_position_t offset
;
1128 /* Initialize the output. */
1129 output_location (NULL
, 0, NULL
, NULL
);
1133 gimple_stmt_iterator gsi
;
1137 if (bb
== ENTRY_BLOCK_PTR
->next_bb
)
1139 expanded_location curr_location
=
1140 expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
1141 output_location (curr_location
.file
, curr_location
.line
,
1145 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1147 gimple stmt
= gsi_stmt (gsi
);
1148 if (gimple_has_location (stmt
))
1149 output_location (gimple_filename (stmt
), gimple_lineno (stmt
),
1153 /* Notice GOTO expressions we eliminated while constructing the
1155 if (single_succ_p (bb
)
1156 && single_succ_edge (bb
)->goto_locus
!= UNKNOWN_LOCATION
)
1158 location_t curr_location
= single_succ_edge (bb
)->goto_locus
;
1159 /* ??? The FILE/LINE API is inconsistent for these cases. */
1160 output_location (LOCATION_FILE (curr_location
),
1161 LOCATION_LINE (curr_location
), &offset
, bb
);
1166 /* A file of NULL indicates the end of run. */
1167 gcov_write_unsigned (0);
1168 gcov_write_string (NULL
);
1169 gcov_write_length (offset
);
1174 ENTRY_BLOCK_PTR
->index
= ENTRY_BLOCK
;
1175 EXIT_BLOCK_PTR
->index
= EXIT_BLOCK
;
1176 #undef BB_TO_GCOV_INDEX
1178 if (flag_profile_values
)
1179 gimple_find_values_to_profile (&values
);
1181 if (flag_branch_probabilities
)
1183 compute_branch_probabilities ();
1184 if (flag_profile_values
)
1185 compute_value_histograms (values
);
1188 remove_fake_edges ();
1190 /* For each edge not on the spanning tree, add counting code. */
1191 if (profile_arc_flag
1192 && coverage_counter_alloc (GCOV_COUNTER_ARCS
, num_instrumented
))
1194 unsigned n_instrumented
;
1196 gimple_init_edge_profiler ();
1198 n_instrumented
= instrument_edges (el
);
1200 gcc_assert (n_instrumented
== num_instrumented
);
1202 if (flag_profile_values
)
1203 instrument_values (values
);
1205 /* Commit changes done by instrumentation. */
1206 gsi_commit_edge_inserts ();
1209 free_aux_for_edges ();
1211 VEC_free (histogram_value
, heap
, values
);
1212 free_edge_list (el
);
1213 coverage_end_function ();
1216 /* Union find algorithm implementation for the basic blocks using
1220 find_group (basic_block bb
)
1222 basic_block group
= bb
, bb1
;
1224 while ((basic_block
) group
->aux
!= group
)
1225 group
= (basic_block
) group
->aux
;
1227 /* Compress path. */
1228 while ((basic_block
) bb
->aux
!= group
)
1230 bb1
= (basic_block
) bb
->aux
;
1231 bb
->aux
= (void *) group
;
1238 union_groups (basic_block bb1
, basic_block bb2
)
1240 basic_block bb1g
= find_group (bb1
);
1241 basic_block bb2g
= find_group (bb2
);
1243 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1244 this code is unlikely going to be performance problem anyway. */
1245 gcc_assert (bb1g
!= bb2g
);
1250 /* This function searches all of the edges in the program flow graph, and puts
1251 as many bad edges as possible onto the spanning tree. Bad edges include
1252 abnormals edges, which can't be instrumented at the moment. Since it is
1253 possible for fake edges to form a cycle, we will have to develop some
1254 better way in the future. Also put critical edges to the tree, since they
1255 are more expensive to instrument. */
1258 find_spanning_tree (struct edge_list
*el
)
1261 int num_edges
= NUM_EDGES (el
);
1264 /* We use aux field for standard union-find algorithm. */
1265 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1268 /* Add fake edge exit to entry we can't instrument. */
1269 union_groups (EXIT_BLOCK_PTR
, ENTRY_BLOCK_PTR
);
1271 /* First add all abnormal edges to the tree unless they form a cycle. Also
1272 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1273 setting return value from function. */
1274 for (i
= 0; i
< num_edges
; i
++)
1276 edge e
= INDEX_EDGE (el
, i
);
1277 if (((e
->flags
& (EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
| EDGE_FAKE
))
1278 || e
->dest
== EXIT_BLOCK_PTR
)
1279 && !EDGE_INFO (e
)->ignore
1280 && (find_group (e
->src
) != find_group (e
->dest
)))
1283 fprintf (dump_file
, "Abnormal edge %d to %d put to tree\n",
1284 e
->src
->index
, e
->dest
->index
);
1285 EDGE_INFO (e
)->on_tree
= 1;
1286 union_groups (e
->src
, e
->dest
);
1290 /* Now insert all critical edges to the tree unless they form a cycle. */
1291 for (i
= 0; i
< num_edges
; i
++)
1293 edge e
= INDEX_EDGE (el
, i
);
1294 if (EDGE_CRITICAL_P (e
) && !EDGE_INFO (e
)->ignore
1295 && find_group (e
->src
) != find_group (e
->dest
))
1298 fprintf (dump_file
, "Critical edge %d to %d put to tree\n",
1299 e
->src
->index
, e
->dest
->index
);
1300 EDGE_INFO (e
)->on_tree
= 1;
1301 union_groups (e
->src
, e
->dest
);
1305 /* And now the rest. */
1306 for (i
= 0; i
< num_edges
; i
++)
1308 edge e
= INDEX_EDGE (el
, i
);
1309 if (!EDGE_INFO (e
)->ignore
1310 && find_group (e
->src
) != find_group (e
->dest
))
1313 fprintf (dump_file
, "Normal edge %d to %d put to tree\n",
1314 e
->src
->index
, e
->dest
->index
);
1315 EDGE_INFO (e
)->on_tree
= 1;
1316 union_groups (e
->src
, e
->dest
);
1320 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
, NULL
, next_bb
)
1324 /* Perform file-level initialization for branch-prob processing. */
1327 init_branch_prob (void)
1331 total_num_blocks
= 0;
1332 total_num_edges
= 0;
1333 total_num_edges_ignored
= 0;
1334 total_num_edges_instrumented
= 0;
1335 total_num_blocks_created
= 0;
1336 total_num_passes
= 0;
1337 total_num_times_called
= 0;
1338 total_num_branches
= 0;
1339 for (i
= 0; i
< 20; i
++)
1340 total_hist_br_prob
[i
] = 0;
1343 /* Performs file-level cleanup after branch-prob processing
1347 end_branch_prob (void)
1351 fprintf (dump_file
, "\n");
1352 fprintf (dump_file
, "Total number of blocks: %d\n",
1354 fprintf (dump_file
, "Total number of edges: %d\n", total_num_edges
);
1355 fprintf (dump_file
, "Total number of ignored edges: %d\n",
1356 total_num_edges_ignored
);
1357 fprintf (dump_file
, "Total number of instrumented edges: %d\n",
1358 total_num_edges_instrumented
);
1359 fprintf (dump_file
, "Total number of blocks created: %d\n",
1360 total_num_blocks_created
);
1361 fprintf (dump_file
, "Total number of graph solution passes: %d\n",
1363 if (total_num_times_called
!= 0)
1364 fprintf (dump_file
, "Average number of graph solution passes: %d\n",
1365 (total_num_passes
+ (total_num_times_called
>> 1))
1366 / total_num_times_called
);
1367 fprintf (dump_file
, "Total number of branches: %d\n",
1368 total_num_branches
);
1369 if (total_num_branches
)
1373 for (i
= 0; i
< 10; i
++)
1374 fprintf (dump_file
, "%d%% branches in range %d-%d%%\n",
1375 (total_hist_br_prob
[i
] + total_hist_br_prob
[19-i
]) * 100
1376 / total_num_branches
, 5*i
, 5*i
+5);