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 Free Software Foundation, Inc.
4 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
5 based on some ideas from Dain Samples of UC Berkeley.
6 Further mangling by Bob Manson, Cygnus Support.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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"
64 #include "value-prof.h"
67 #include "tree-flow.h"
69 /* Hooks for profiling. */
70 static struct profile_hooks* profile_hooks;
72 /* File for profiling debug output. */
74 profile_dump_file (void) {
75 return profile_hooks->profile_dump_file ();
78 /* Additional information about the edges we need. */
80 unsigned int count_valid : 1;
82 /* Is on the spanning tree. */
83 unsigned int on_tree : 1;
85 /* Pretend this edge does not exist (it is abnormal and we've
86 inserted a fake to compensate). */
87 unsigned int ignore : 1;
91 unsigned int count_valid : 1;
93 /* Number of successor and predecessor edges. */
98 #define EDGE_INFO(e) ((struct edge_info *) (e)->aux)
99 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
101 /* Counter summary from the last set of coverage counts read. */
103 const struct gcov_ctr_summary *profile_info;
105 /* Collect statistics on the performance of this pass for the entire source
108 static int total_num_blocks;
109 static int total_num_edges;
110 static int total_num_edges_ignored;
111 static int total_num_edges_instrumented;
112 static int total_num_blocks_created;
113 static int total_num_passes;
114 static int total_num_times_called;
115 static int total_hist_br_prob[20];
116 static int total_num_never_executed;
117 static int total_num_branches;
119 /* Forward declarations. */
120 static void find_spanning_tree (struct edge_list *);
121 static unsigned instrument_edges (struct edge_list *);
122 static void instrument_values (histogram_values);
123 static void compute_branch_probabilities (void);
124 static void compute_value_histograms (histogram_values);
125 static gcov_type * get_exec_counts (void);
126 static basic_block find_group (basic_block);
127 static void union_groups (basic_block, basic_block);
130 /* Add edge instrumentation code to the entire insn chain.
132 F is the first insn of the chain.
133 NUM_BLOCKS is the number of basic blocks found in F. */
136 instrument_edges (struct edge_list *el)
138 unsigned num_instr_edges = 0;
139 int num_edges = NUM_EDGES (el);
142 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
147 FOR_EACH_EDGE (e, ei, bb->succs)
149 struct edge_info *inf = EDGE_INFO (e);
151 if (!inf->ignore && !inf->on_tree)
153 if (e->flags & EDGE_ABNORMAL)
156 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
157 e->src->index, e->dest->index,
158 EDGE_CRITICAL_P (e) ? " (and split)" : "");
159 (profile_hooks->gen_edge_profiler) (num_instr_edges++, e);
164 total_num_blocks_created += num_edges;
166 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
167 return num_instr_edges;
170 /* Add code to measure histograms for values in list VALUES. */
172 instrument_values (histogram_values values)
176 /* Emit code to generate the histograms before the insns. */
178 for (i = 0; i < VEC_length (histogram_value, values); i++)
180 histogram_value hist = VEC_index (histogram_value, values, i);
183 case HIST_TYPE_INTERVAL:
184 t = GCOV_COUNTER_V_INTERVAL;
188 t = GCOV_COUNTER_V_POW2;
191 case HIST_TYPE_SINGLE_VALUE:
192 t = GCOV_COUNTER_V_SINGLE;
195 case HIST_TYPE_CONST_DELTA:
196 t = GCOV_COUNTER_V_DELTA;
202 if (!coverage_counter_alloc (t, hist->n_counters))
207 case HIST_TYPE_INTERVAL:
208 (profile_hooks->gen_interval_profiler) (hist, t, 0);
212 (profile_hooks->gen_pow2_profiler) (hist, t, 0);
215 case HIST_TYPE_SINGLE_VALUE:
216 (profile_hooks->gen_one_value_profiler) (hist, t, 0);
219 case HIST_TYPE_CONST_DELTA:
220 (profile_hooks->gen_const_delta_profiler) (hist, t, 0);
227 VEC_free (histogram_value, values);
231 /* Computes hybrid profile for all matching entries in da_file. */
234 get_exec_counts (void)
236 unsigned num_edges = 0;
240 /* Count the edges to be (possibly) instrumented. */
241 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
246 FOR_EACH_EDGE (e, ei, bb->succs)
247 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
251 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, &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);
263 /* Compute the branch probabilities for the various branches.
264 Annotate them accordingly. */
267 compute_branch_probabilities (void)
274 int hist_br_prob[20];
275 int num_never_executed;
277 gcov_type *exec_counts = get_exec_counts ();
278 int exec_counts_pos = 0;
280 /* Very simple sanity checks so we catch bugs in our profiling code. */
283 if (profile_info->run_max * profile_info->runs < profile_info->sum_max)
285 error ("corrupted profile info: run_max * runs < sum_max");
289 if (profile_info->sum_all < profile_info->sum_max)
291 error ("corrupted profile info: sum_all is smaller than sum_max");
296 /* Attach extra info block to each bb. */
298 alloc_aux_for_blocks (sizeof (struct bb_info));
299 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
304 FOR_EACH_EDGE (e, ei, bb->succs)
305 if (!EDGE_INFO (e)->ignore)
306 BB_INFO (bb)->succ_count++;
307 FOR_EACH_EDGE (e, ei, bb->preds)
308 if (!EDGE_INFO (e)->ignore)
309 BB_INFO (bb)->pred_count++;
312 /* Avoid predicting entry on exit nodes. */
313 BB_INFO (EXIT_BLOCK_PTR)->succ_count = 2;
314 BB_INFO (ENTRY_BLOCK_PTR)->pred_count = 2;
316 /* For each edge not on the spanning tree, set its execution count from
319 /* The first count in the .da file is the number of times that the function
320 was entered. This is the exec_count for block zero. */
322 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
327 FOR_EACH_EDGE (e, ei, bb->succs)
328 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
333 e->count = exec_counts[exec_counts_pos++];
334 if (e->count > profile_info->sum_max)
336 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
337 bb->index, e->dest->index);
343 EDGE_INFO (e)->count_valid = 1;
344 BB_INFO (bb)->succ_count--;
345 BB_INFO (e->dest)->pred_count--;
348 fprintf (dump_file, "\nRead edge from %i to %i, count:",
349 bb->index, e->dest->index);
350 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
351 (HOST_WIDEST_INT) e->count);
357 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
359 /* For every block in the file,
360 - if every exit/entrance edge has a known count, then set the block count
361 - if the block count is known, and every exit/entrance edge but one has
362 a known execution count, then set the count of the remaining edge
364 As edge counts are set, decrement the succ/pred count, but don't delete
365 the edge, that way we can easily tell when all edges are known, or only
366 one edge is unknown. */
368 /* The order that the basic blocks are iterated through is important.
369 Since the code that finds spanning trees starts with block 0, low numbered
370 edges are put on the spanning tree in preference to high numbered edges.
371 Hence, most instrumented edges are at the end. Graph solving works much
372 faster if we propagate numbers from the end to the start.
374 This takes an average of slightly more than 3 passes. */
382 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR, NULL, prev_bb)
384 struct bb_info *bi = BB_INFO (bb);
385 if (! bi->count_valid)
387 if (bi->succ_count == 0)
393 FOR_EACH_EDGE (e, ei, bb->succs)
399 else if (bi->pred_count == 0)
405 FOR_EACH_EDGE (e, ei, bb->preds)
414 if (bi->succ_count == 1)
420 /* One of the counts will be invalid, but it is zero,
421 so adding it in also doesn't hurt. */
422 FOR_EACH_EDGE (e, ei, bb->succs)
425 /* Seedgeh for the invalid edge, and set its count. */
426 FOR_EACH_EDGE (e, ei, bb->succs)
427 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
430 /* Calculate count for remaining edge by conservation. */
431 total = bb->count - total;
435 EDGE_INFO (e)->count_valid = 1;
439 BB_INFO (e->dest)->pred_count--;
442 if (bi->pred_count == 1)
448 /* One of the counts will be invalid, but it is zero,
449 so adding it in also doesn't hurt. */
450 FOR_EACH_EDGE (e, ei, bb->preds)
453 /* Search for the invalid edge, and set its count. */
454 FOR_EACH_EDGE (e, ei, bb->preds)
455 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
458 /* Calculate count for remaining edge by conservation. */
459 total = bb->count - total + e->count;
463 EDGE_INFO (e)->count_valid = 1;
467 BB_INFO (e->src)->succ_count--;
474 dump_flow_info (dump_file);
476 total_num_passes += passes;
478 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
480 /* If the graph has been correctly solved, every block will have a
481 succ and pred count of zero. */
484 if (BB_INFO (bb)->succ_count || BB_INFO (bb)->pred_count)
488 /* For every edge, calculate its branch probability and add a reg_note
489 to the branch insn to indicate this. */
491 for (i = 0; i < 20; i++)
493 num_never_executed = 0;
496 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
504 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
505 bb->index, (int)bb->count);
508 FOR_EACH_EDGE (e, ei, bb->succs)
510 /* Function may return twice in the cased the called function is
511 setjmp or calls fork, but we can't represent this by extra
512 edge from the entry, since extra edge from the exit is
513 already present. We get negative frequency from the entry
516 && e->dest == EXIT_BLOCK_PTR)
517 || (e->count > bb->count
518 && e->dest != EXIT_BLOCK_PTR))
520 if (block_ends_with_call_p (bb))
521 e->count = e->count < 0 ? 0 : bb->count;
523 if (e->count < 0 || e->count > bb->count)
525 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
526 e->src->index, e->dest->index,
528 e->count = bb->count / 2;
533 FOR_EACH_EDGE (e, ei, bb->succs)
534 e->probability = (e->count * REG_BR_PROB_BASE + bb->count / 2) / bb->count;
536 && block_ends_with_condjump_p (bb)
537 && EDGE_COUNT (bb->succs) >= 2)
543 /* Find the branch edge. It is possible that we do have fake
545 FOR_EACH_EDGE (e, ei, bb->succs)
546 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
549 prob = e->probability;
550 index = prob * 20 / REG_BR_PROB_BASE;
554 hist_br_prob[index]++;
556 /* Do this for RTL only. */
559 note = find_reg_note (BB_END (bb), REG_BR_PROB, 0);
560 /* There may be already note put by some other pass, such
561 as builtin_expect expander. */
563 XEXP (note, 0) = GEN_INT (prob);
565 REG_NOTES (BB_END (bb))
566 = gen_rtx_EXPR_LIST (REG_BR_PROB, GEN_INT (prob),
567 REG_NOTES (BB_END (bb)));
572 /* Otherwise try to preserve the existing REG_BR_PROB probabilities
573 tree based profile guessing put into code. BB can be the
574 ENTRY_BLOCK, and it can have multiple (fake) successors in
575 EH cases, but it still has no code; don't crash in this case. */
576 else if (profile_status == PROFILE_ABSENT
578 && EDGE_COUNT (bb->succs) > 1
580 && (note = find_reg_note (BB_END (bb), REG_BR_PROB, 0)))
582 int prob = INTVAL (XEXP (note, 0));
584 BRANCH_EDGE (bb)->probability = prob;
585 FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob;
587 /* As a last resort, distribute the probabilities evenly.
588 Use simple heuristics that if there are normal edges,
589 give all abnormals frequency of 0, otherwise distribute the
590 frequency over abnormals (this is the case of noreturn
592 else if (profile_status == PROFILE_ABSENT)
596 FOR_EACH_EDGE (e, ei, bb->succs)
597 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
601 FOR_EACH_EDGE (e, ei, bb->succs)
602 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
603 e->probability = REG_BR_PROB_BASE / total;
609 total += EDGE_COUNT (bb->succs);
610 FOR_EACH_EDGE (e, ei, bb->succs)
611 e->probability = REG_BR_PROB_BASE / total;
614 && block_ends_with_condjump_p (bb)
615 && EDGE_COUNT (bb->succs) >= 2)
616 num_branches++, num_never_executed;
623 fprintf (dump_file, "%d branches\n", num_branches);
624 fprintf (dump_file, "%d branches never executed\n",
627 for (i = 0; i < 10; i++)
628 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
629 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
632 total_num_branches += num_branches;
633 total_num_never_executed += num_never_executed;
634 for (i = 0; i < 20; i++)
635 total_hist_br_prob[i] += hist_br_prob[i];
637 fputc ('\n', dump_file);
638 fputc ('\n', dump_file);
641 free_aux_for_blocks ();
644 /* Load value histograms values whose description is stored in VALUES array
648 compute_value_histograms (histogram_values values)
650 unsigned i, j, t, any;
651 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
652 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
653 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
654 gcov_type *aact_count;
655 histogram_value hist;
657 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
658 n_histogram_counters[t] = 0;
660 for (i = 0; i < VEC_length (histogram_value, values); i++)
662 hist = VEC_index (histogram_value, values, i);
663 n_histogram_counters[(int) hist->type] += hist->n_counters;
667 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
669 if (!n_histogram_counters[t])
671 histogram_counts[t] = NULL;
675 histogram_counts[t] =
676 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
677 n_histogram_counters[t], NULL);
678 if (histogram_counts[t])
680 act_count[t] = histogram_counts[t];
685 for (i = 0; i < VEC_length (histogram_value, values); i++)
687 rtx hist_list = NULL_RTX;
689 hist = VEC_index (histogram_value, values, i);
690 t = (int) hist->type;
692 aact_count = act_count[t];
693 act_count[t] += hist->n_counters;
697 for (j = hist->n_counters; j > 0; j--)
698 hist_list = alloc_EXPR_LIST (0, GEN_INT (aact_count[j - 1]),
700 hist_list = alloc_EXPR_LIST (0,
701 copy_rtx (hist->hvalue.rtl.value), hist_list);
702 hist_list = alloc_EXPR_LIST (0, GEN_INT (hist->type), hist_list);
703 REG_NOTES (hist->hvalue.rtl.insn) =
704 alloc_EXPR_LIST (REG_VALUE_PROFILE, hist_list,
705 REG_NOTES (hist->hvalue.rtl.insn));
709 tree stmt = hist->hvalue.tree.stmt;
710 stmt_ann_t ann = get_stmt_ann (stmt);
711 hist->hvalue.tree.next = ann->histograms;
712 ann->histograms = hist;
713 hist->hvalue.tree.counters =
714 xmalloc (sizeof (gcov_type) * hist->n_counters);
715 for (j = 0; j < hist->n_counters; j++)
716 hist->hvalue.tree.counters[j] = aact_count[j];
720 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
721 if (histogram_counts[t])
722 free (histogram_counts[t]);
725 #define BB_TO_GCOV_INDEX(bb) ((bb)->index + 1)
726 /* When passed NULL as file_name, initialize.
727 When passed something else, output the necessary commands to change
728 line to LINE and offset to FILE_NAME. */
730 output_location (char const *file_name, int line,
731 gcov_position_t *offset, basic_block bb)
733 static char const *prev_file_name;
734 static int prev_line;
735 bool name_differs, line_differs;
739 prev_file_name = NULL;
744 name_differs = !prev_file_name || strcmp (file_name, prev_file_name);
745 line_differs = prev_line != line;
747 if (name_differs || line_differs)
751 *offset = gcov_write_tag (GCOV_TAG_LINES);
752 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
753 name_differs = line_differs=true;
756 /* If this is a new source file, then output the
757 file's name to the .bb file. */
760 prev_file_name = file_name;
761 gcov_write_unsigned (0);
762 gcov_write_string (prev_file_name);
766 gcov_write_unsigned (line);
772 /* Instrument and/or analyze program behavior based on program flow graph.
773 In either case, this function builds a flow graph for the function being
774 compiled. The flow graph is stored in BB_GRAPH.
776 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
777 the flow graph that are needed to reconstruct the dynamic behavior of the
780 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
781 information from a data file containing edge count information from previous
782 executions of the function being compiled. In this case, the flow graph is
783 annotated with actual execution counts, which are later propagated into the
784 rtl for optimization purposes.
786 Main entry point of this file. */
793 unsigned num_edges, ignored_edges;
794 unsigned num_instrumented;
795 struct edge_list *el;
796 histogram_values values = NULL;
798 total_num_times_called++;
800 flow_call_edges_add (NULL);
801 add_noreturn_fake_exit_edges ();
803 /* We can't handle cyclic regions constructed using abnormal edges.
804 To avoid these we replace every source of abnormal edge by a fake
805 edge from entry node and every destination by fake edge to exit.
806 This keeps graph acyclic and our calculation exact for all normal
807 edges except for exit and entrance ones.
809 We also add fake exit edges for each call and asm statement in the
810 basic, since it may not return. */
814 int need_exit_edge = 0, need_entry_edge = 0;
815 int have_exit_edge = 0, have_entry_edge = 0;
819 /* Functions returning multiple times are not handled by extra edges.
820 Instead we simply allow negative counts on edges from exit to the
821 block past call and corresponding probabilities. We can't go
822 with the extra edges because that would result in flowgraph that
823 needs to have fake edges outside the spanning tree. */
825 FOR_EACH_EDGE (e, ei, bb->succs)
827 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
828 && e->dest != EXIT_BLOCK_PTR)
830 if (e->dest == EXIT_BLOCK_PTR)
833 FOR_EACH_EDGE (e, ei, bb->preds)
835 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
836 && e->src != ENTRY_BLOCK_PTR)
838 if (e->src == ENTRY_BLOCK_PTR)
842 if (need_exit_edge && !have_exit_edge)
845 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
847 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
849 if (need_entry_edge && !have_entry_edge)
852 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
854 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FAKE);
858 el = create_edge_list ();
859 num_edges = NUM_EDGES (el);
860 alloc_aux_for_edges (sizeof (struct edge_info));
862 /* The basic blocks are expected to be numbered sequentially. */
866 for (i = 0 ; i < num_edges ; i++)
868 edge e = INDEX_EDGE (el, i);
871 /* Mark edges we've replaced by fake edges above as ignored. */
872 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
873 && e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR)
875 EDGE_INFO (e)->ignore = 1;
880 /* Create spanning tree from basic block graph, mark each edge that is
881 on the spanning tree. We insert as many abnormal and critical edges
882 as possible to minimize number of edge splits necessary. */
884 find_spanning_tree (el);
886 /* Fake edges that are not on the tree will not be instrumented, so
887 mark them ignored. */
888 for (num_instrumented = i = 0; i < num_edges; i++)
890 edge e = INDEX_EDGE (el, i);
891 struct edge_info *inf = EDGE_INFO (e);
893 if (inf->ignore || inf->on_tree)
895 else if (e->flags & EDGE_FAKE)
904 total_num_blocks += n_basic_blocks + 2;
906 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks);
908 total_num_edges += num_edges;
910 fprintf (dump_file, "%d edges\n", num_edges);
912 total_num_edges_ignored += ignored_edges;
914 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
916 /* Write the data from which gcov can reconstruct the basic block
919 /* Basic block flags */
920 if (coverage_begin_output ())
922 gcov_position_t offset;
924 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
925 for (i = 0; i != (unsigned) (n_basic_blocks + 2); i++)
926 gcov_write_unsigned (0);
927 gcov_write_length (offset);
930 /* Keep all basic block indexes nonnegative in the gcov output.
931 Index 0 is used for entry block, last index is for exit block.
933 ENTRY_BLOCK_PTR->index = -1;
934 EXIT_BLOCK_PTR->index = last_basic_block;
937 if (coverage_begin_output ())
939 gcov_position_t offset;
941 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
946 offset = gcov_write_tag (GCOV_TAG_ARCS);
947 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
949 FOR_EACH_EDGE (e, ei, bb->succs)
951 struct edge_info *i = EDGE_INFO (e);
954 unsigned flag_bits = 0;
957 flag_bits |= GCOV_ARC_ON_TREE;
958 if (e->flags & EDGE_FAKE)
959 flag_bits |= GCOV_ARC_FAKE;
960 if (e->flags & EDGE_FALLTHRU)
961 flag_bits |= GCOV_ARC_FALLTHROUGH;
962 /* On trees we don't have fallthru flags, but we can
963 recompute them from CFG shape. */
965 && e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
966 && e->src->next_bb == e->dest)
967 flag_bits |= GCOV_ARC_FALLTHROUGH;
969 gcov_write_unsigned (BB_TO_GCOV_INDEX (e->dest));
970 gcov_write_unsigned (flag_bits);
974 gcov_write_length (offset);
979 if (coverage_begin_output ())
981 /* Initialize the output. */
982 output_location (NULL, 0, NULL, NULL);
986 gcov_position_t offset;
990 rtx insn = BB_HEAD (bb);
991 int ignore_next_note = 0;
995 /* We are looking for line number notes. Search backward
996 before basic block to find correct ones. */
997 insn = prev_nonnote_insn (insn);
1001 insn = NEXT_INSN (insn);
1003 while (insn != BB_END (bb))
1007 /* Must ignore the line number notes that
1008 immediately follow the end of an inline function
1009 to avoid counting it twice. There is a note
1010 before the call, and one after the call. */
1011 if (NOTE_LINE_NUMBER (insn)
1012 == NOTE_INSN_REPEATED_LINE_NUMBER)
1013 ignore_next_note = 1;
1014 else if (NOTE_LINE_NUMBER (insn) <= 0)
1016 else if (ignore_next_note)
1017 ignore_next_note = 0;
1020 expanded_location s;
1021 NOTE_EXPANDED_LOCATION (s, insn);
1022 output_location (s.file, s.line, &offset, bb);
1025 insn = NEXT_INSN (insn);
1030 /* A file of NULL indicates the end of run. */
1031 gcov_write_unsigned (0);
1032 gcov_write_string (NULL);
1033 gcov_write_length (offset);
1039 gcov_position_t offset;
1043 block_stmt_iterator bsi;
1047 if (bb == ENTRY_BLOCK_PTR->next_bb)
1049 expanded_location curr_location =
1050 expand_location (DECL_SOURCE_LOCATION
1051 (current_function_decl));
1052 output_location (curr_location.file, curr_location.line,
1056 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1058 tree stmt = bsi_stmt (bsi);
1059 if (EXPR_HAS_LOCATION (stmt))
1060 output_location (EXPR_FILENAME (stmt),
1065 /* Notice GOTO expressions we eliminated while constructing the
1067 if (single_succ_p (bb) && single_succ_edge (bb)->goto_locus)
1069 /* ??? source_locus type is marked deprecated in input.h. */
1070 source_locus curr_location = single_succ_edge (bb)->goto_locus;
1071 /* ??? The FILE/LINE API is inconsistent for these cases. */
1072 #ifdef USE_MAPPED_LOCATION
1073 output_location (LOCATION_FILE (curr_location),
1074 LOCATION_LINE (curr_location),
1077 output_location (curr_location->file, curr_location->line,
1084 /* A file of NULL indicates the end of run. */
1085 gcov_write_unsigned (0);
1086 gcov_write_string (NULL);
1087 gcov_write_length (offset);
1093 ENTRY_BLOCK_PTR->index = ENTRY_BLOCK;
1094 EXIT_BLOCK_PTR->index = EXIT_BLOCK;
1095 #undef BB_TO_GCOV_INDEX
1097 if (flag_profile_values)
1098 find_values_to_profile (&values);
1100 if (flag_branch_probabilities)
1102 compute_branch_probabilities ();
1103 if (flag_profile_values)
1104 compute_value_histograms (values);
1107 remove_fake_edges ();
1109 /* For each edge not on the spanning tree, add counting code. */
1110 if (profile_arc_flag
1111 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1113 unsigned n_instrumented;
1115 profile_hooks->init_edge_profiler ();
1117 n_instrumented = instrument_edges (el);
1119 if (n_instrumented != num_instrumented)
1122 if (flag_profile_values)
1123 instrument_values (values);
1125 /* Commit changes done by instrumentation. */
1127 bsi_commit_edge_inserts ();
1130 commit_edge_insertions_watch_calls ();
1131 allocate_reg_info (max_reg_num (), FALSE, FALSE);
1135 free_aux_for_edges ();
1139 /* Re-merge split basic blocks and the mess introduced by
1140 insert_insn_on_edge. */
1141 cleanup_cfg (profile_arc_flag ? CLEANUP_EXPENSIVE : 0);
1142 if (profile_dump_file())
1143 dump_flow_info (profile_dump_file());
1146 free_edge_list (el);
1147 if (flag_branch_probabilities)
1148 profile_status = PROFILE_READ;
1151 /* Union find algorithm implementation for the basic blocks using
1155 find_group (basic_block bb)
1157 basic_block group = bb, bb1;
1159 while ((basic_block) group->aux != group)
1160 group = (basic_block) group->aux;
1162 /* Compress path. */
1163 while ((basic_block) bb->aux != group)
1165 bb1 = (basic_block) bb->aux;
1166 bb->aux = (void *) group;
1173 union_groups (basic_block bb1, basic_block bb2)
1175 basic_block bb1g = find_group (bb1);
1176 basic_block bb2g = find_group (bb2);
1178 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1179 this code is unlikely going to be performance problem anyway. */
1186 /* This function searches all of the edges in the program flow graph, and puts
1187 as many bad edges as possible onto the spanning tree. Bad edges include
1188 abnormals edges, which can't be instrumented at the moment. Since it is
1189 possible for fake edges to form a cycle, we will have to develop some
1190 better way in the future. Also put critical edges to the tree, since they
1191 are more expensive to instrument. */
1194 find_spanning_tree (struct edge_list *el)
1197 int num_edges = NUM_EDGES (el);
1200 /* We use aux field for standard union-find algorithm. */
1201 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1204 /* Add fake edge exit to entry we can't instrument. */
1205 union_groups (EXIT_BLOCK_PTR, ENTRY_BLOCK_PTR);
1207 /* First add all abnormal edges to the tree unless they form a cycle. Also
1208 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1209 setting return value from function. */
1210 for (i = 0; i < num_edges; i++)
1212 edge e = INDEX_EDGE (el, i);
1213 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1214 || e->dest == EXIT_BLOCK_PTR)
1215 && !EDGE_INFO (e)->ignore
1216 && (find_group (e->src) != find_group (e->dest)))
1219 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1220 e->src->index, e->dest->index);
1221 EDGE_INFO (e)->on_tree = 1;
1222 union_groups (e->src, e->dest);
1226 /* Now insert all critical edges to the tree unless they form a cycle. */
1227 for (i = 0; i < num_edges; i++)
1229 edge e = INDEX_EDGE (el, i);
1230 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1231 && find_group (e->src) != find_group (e->dest))
1234 fprintf (dump_file, "Critical edge %d to %d put to tree\n",
1235 e->src->index, e->dest->index);
1236 EDGE_INFO (e)->on_tree = 1;
1237 union_groups (e->src, e->dest);
1241 /* And now the rest. */
1242 for (i = 0; i < num_edges; i++)
1244 edge e = INDEX_EDGE (el, i);
1245 if (!EDGE_INFO (e)->ignore
1246 && find_group (e->src) != find_group (e->dest))
1249 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1250 e->src->index, e->dest->index);
1251 EDGE_INFO (e)->on_tree = 1;
1252 union_groups (e->src, e->dest);
1256 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1260 /* Perform file-level initialization for branch-prob processing. */
1263 init_branch_prob (void)
1267 total_num_blocks = 0;
1268 total_num_edges = 0;
1269 total_num_edges_ignored = 0;
1270 total_num_edges_instrumented = 0;
1271 total_num_blocks_created = 0;
1272 total_num_passes = 0;
1273 total_num_times_called = 0;
1274 total_num_branches = 0;
1275 total_num_never_executed = 0;
1276 for (i = 0; i < 20; i++)
1277 total_hist_br_prob[i] = 0;
1280 /* Performs file-level cleanup after branch-prob processing
1284 end_branch_prob (void)
1288 fprintf (dump_file, "\n");
1289 fprintf (dump_file, "Total number of blocks: %d\n",
1291 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1292 fprintf (dump_file, "Total number of ignored edges: %d\n",
1293 total_num_edges_ignored);
1294 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1295 total_num_edges_instrumented);
1296 fprintf (dump_file, "Total number of blocks created: %d\n",
1297 total_num_blocks_created);
1298 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1300 if (total_num_times_called != 0)
1301 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1302 (total_num_passes + (total_num_times_called >> 1))
1303 / total_num_times_called);
1304 fprintf (dump_file, "Total number of branches: %d\n",
1305 total_num_branches);
1306 fprintf (dump_file, "Total number of branches never executed: %d\n",
1307 total_num_never_executed);
1308 if (total_num_branches)
1312 for (i = 0; i < 10; i++)
1313 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1314 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1315 / total_num_branches, 5*i, 5*i+5);
1320 /* Set up hooks to enable tree-based profiling. */
1323 tree_register_profile_hooks (void)
1325 profile_hooks = &tree_profile_hooks;
1330 /* Set up hooks to enable RTL-based profiling. */
1333 rtl_register_profile_hooks (void)
1335 profile_hooks = &rtl_profile_hooks;