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 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 file generated is <dumpbase>.bbg. The format is
43 described in full in gcov-io.h. */
45 /* ??? Register allocation should use basic block execution counts to
46 give preference to the most commonly executed blocks. */
48 /* ??? Should calculate branch probabilities before instrumenting code, since
49 then we can use arc counts to help decide which arcs to instrument. */
53 #include "coretypes.h"
63 #include "value-prof.h"
66 /* Additional information about the edges we need. */
68 unsigned int count_valid : 1;
70 /* Is on the spanning tree. */
71 unsigned int on_tree : 1;
73 /* Pretend this edge does not exist (it is abnormal and we've
74 inserted a fake to compensate). */
75 unsigned int ignore : 1;
79 unsigned int count_valid : 1;
81 /* Number of successor and predecessor edges. */
86 #define EDGE_INFO(e) ((struct edge_info *) (e)->aux)
87 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
89 /* Counter summary from the last set of coverage counts read. */
91 const struct gcov_ctr_summary *profile_info;
93 /* Collect statistics on the performance of this pass for the entire source
96 static int total_num_blocks;
97 static int total_num_edges;
98 static int total_num_edges_ignored;
99 static int total_num_edges_instrumented;
100 static int total_num_blocks_created;
101 static int total_num_passes;
102 static int total_num_times_called;
103 static int total_hist_br_prob[20];
104 static int total_num_never_executed;
105 static int total_num_branches;
107 /* Forward declarations. */
108 static void find_spanning_tree (struct edge_list *);
109 static rtx gen_edge_profiler (int);
110 static rtx gen_interval_profiler (struct histogram_value *, unsigned,
112 static rtx gen_pow2_profiler (struct histogram_value *, unsigned, unsigned);
113 static rtx gen_one_value_profiler (struct histogram_value *, unsigned,
115 static rtx gen_const_delta_profiler (struct histogram_value *, unsigned,
117 static unsigned instrument_edges (struct edge_list *);
118 static void instrument_values (unsigned, struct histogram_value *);
119 static void compute_branch_probabilities (void);
120 static void compute_value_histograms (unsigned, struct histogram_value *);
121 static gcov_type * get_exec_counts (void);
122 static basic_block find_group (basic_block);
123 static void union_groups (basic_block, basic_block);
126 /* Add edge instrumentation code to the entire insn chain.
128 F is the first insn of the chain.
129 NUM_BLOCKS is the number of basic blocks found in F. */
132 instrument_edges (struct edge_list *el)
134 unsigned num_instr_edges = 0;
135 int num_edges = NUM_EDGES (el);
138 remove_fake_edges ();
140 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
144 for (e = bb->succ; e; e = e->succ_next)
146 struct edge_info *inf = EDGE_INFO (e);
148 if (!inf->ignore && !inf->on_tree)
152 if (e->flags & EDGE_ABNORMAL)
155 fprintf (rtl_dump_file, "Edge %d to %d instrumented%s\n",
156 e->src->index, e->dest->index,
157 EDGE_CRITICAL_P (e) ? " (and split)" : "");
158 edge_profile = gen_edge_profiler (num_instr_edges++);
159 insert_insn_on_edge (edge_profile, e);
160 rebuild_jump_labels (e->insns);
165 total_num_blocks_created += num_edges;
167 fprintf (rtl_dump_file, "%d edges instrumented\n", num_instr_edges);
168 return num_instr_edges;
171 /* Add code to measure histograms list of VALUES of length N_VALUES. */
173 instrument_values (unsigned n_values, struct histogram_value *values)
179 /* Emit code to generate the histograms before the insns. */
181 for (i = 0; i < n_values; i++)
183 e = split_block (BLOCK_FOR_INSN (values[i].insn),
184 PREV_INSN (values[i].insn));
185 switch (values[i].type)
187 case HIST_TYPE_INTERVAL:
188 t = GCOV_COUNTER_V_INTERVAL;
192 t = GCOV_COUNTER_V_POW2;
195 case HIST_TYPE_SINGLE_VALUE:
196 t = GCOV_COUNTER_V_SINGLE;
199 case HIST_TYPE_CONST_DELTA:
200 t = GCOV_COUNTER_V_DELTA;
206 if (!coverage_counter_alloc (t, values[i].n_counters))
209 switch (values[i].type)
211 case HIST_TYPE_INTERVAL:
212 sequence = gen_interval_profiler (values + i, t, 0);
216 sequence = gen_pow2_profiler (values + i, t, 0);
219 case HIST_TYPE_SINGLE_VALUE:
220 sequence = gen_one_value_profiler (values + i, t, 0);
223 case HIST_TYPE_CONST_DELTA:
224 sequence = gen_const_delta_profiler (values + i, t, 0);
231 safe_insert_insn_on_edge (sequence, e);
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)
249 for (e = bb->succ; e; e = e->succ_next)
250 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
254 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, &profile_info);
258 if (rtl_dump_file && profile_info)
259 fprintf(rtl_dump_file, "Merged %u profiles with maximal count %u.\n",
260 profile_info->runs, (unsigned) profile_info->sum_max);
266 /* Compute the branch probabilities for the various branches.
267 Annotate them accordingly. */
270 compute_branch_probabilities (void)
277 int hist_br_prob[20];
278 int num_never_executed;
280 gcov_type *exec_counts = get_exec_counts ();
281 int exec_counts_pos = 0;
283 /* Attach extra info block to each bb. */
285 alloc_aux_for_blocks (sizeof (struct bb_info));
286 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
290 for (e = bb->succ; e; e = e->succ_next)
291 if (!EDGE_INFO (e)->ignore)
292 BB_INFO (bb)->succ_count++;
293 for (e = bb->pred; e; e = e->pred_next)
294 if (!EDGE_INFO (e)->ignore)
295 BB_INFO (bb)->pred_count++;
298 /* Avoid predicting entry on exit nodes. */
299 BB_INFO (EXIT_BLOCK_PTR)->succ_count = 2;
300 BB_INFO (ENTRY_BLOCK_PTR)->pred_count = 2;
302 /* For each edge not on the spanning tree, set its execution count from
305 /* The first count in the .da file is the number of times that the function
306 was entered. This is the exec_count for block zero. */
308 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
311 for (e = bb->succ; e; e = e->succ_next)
312 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
317 e->count = exec_counts[exec_counts_pos++];
322 EDGE_INFO (e)->count_valid = 1;
323 BB_INFO (bb)->succ_count--;
324 BB_INFO (e->dest)->pred_count--;
327 fprintf (rtl_dump_file, "\nRead edge from %i to %i, count:",
328 bb->index, e->dest->index);
329 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC,
330 (HOST_WIDEST_INT) e->count);
336 fprintf (rtl_dump_file, "\n%d edge counts read\n", num_edges);
338 /* For every block in the file,
339 - if every exit/entrance edge has a known count, then set the block count
340 - if the block count is known, and every exit/entrance edge but one has
341 a known execution count, then set the count of the remaining edge
343 As edge counts are set, decrement the succ/pred count, but don't delete
344 the edge, that way we can easily tell when all edges are known, or only
345 one edge is unknown. */
347 /* The order that the basic blocks are iterated through is important.
348 Since the code that finds spanning trees starts with block 0, low numbered
349 edges are put on the spanning tree in preference to high numbered edges.
350 Hence, most instrumented edges are at the end. Graph solving works much
351 faster if we propagate numbers from the end to the start.
353 This takes an average of slightly more than 3 passes. */
361 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR, NULL, prev_bb)
363 struct bb_info *bi = BB_INFO (bb);
364 if (! bi->count_valid)
366 if (bi->succ_count == 0)
371 for (e = bb->succ; e; e = e->succ_next)
377 else if (bi->pred_count == 0)
382 for (e = bb->pred; e; e = e->pred_next)
391 if (bi->succ_count == 1)
396 /* One of the counts will be invalid, but it is zero,
397 so adding it in also doesn't hurt. */
398 for (e = bb->succ; e; e = e->succ_next)
401 /* Seedgeh for the invalid edge, and set its count. */
402 for (e = bb->succ; e; e = e->succ_next)
403 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
406 /* Calculate count for remaining edge by conservation. */
407 total = bb->count - total;
411 EDGE_INFO (e)->count_valid = 1;
415 BB_INFO (e->dest)->pred_count--;
418 if (bi->pred_count == 1)
423 /* One of the counts will be invalid, but it is zero,
424 so adding it in also doesn't hurt. */
425 for (e = bb->pred; e; e = e->pred_next)
428 /* Search for the invalid edge, and set its count. */
429 for (e = bb->pred; e; e = e->pred_next)
430 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
433 /* Calculate count for remaining edge by conservation. */
434 total = bb->count - total + e->count;
438 EDGE_INFO (e)->count_valid = 1;
442 BB_INFO (e->src)->succ_count--;
449 dump_flow_info (rtl_dump_file);
451 total_num_passes += passes;
453 fprintf (rtl_dump_file, "Graph solving took %d passes.\n\n", passes);
455 /* If the graph has been correctly solved, every block will have a
456 succ and pred count of zero. */
459 if (BB_INFO (bb)->succ_count || BB_INFO (bb)->pred_count)
463 /* For every edge, calculate its branch probability and add a reg_note
464 to the branch insn to indicate this. */
466 for (i = 0; i < 20; i++)
468 num_never_executed = 0;
471 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
478 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
479 bb->index, (int)bb->count);
482 for (e = bb->succ; e; e = e->succ_next)
484 /* Function may return twice in the cased the called fucntion is
485 setjmp or calls fork, but we can't represent this by extra
486 edge from the entry, since extra edge from the exit is
487 already present. We get negative frequency from the entry
490 && e->dest == EXIT_BLOCK_PTR)
491 || (e->count > bb->count
492 && e->dest != EXIT_BLOCK_PTR))
496 while (GET_CODE (insn) != CALL_INSN
498 && keep_with_call_p (insn))
499 insn = PREV_INSN (insn);
500 if (GET_CODE (insn) == CALL_INSN)
501 e->count = e->count < 0 ? 0 : bb->count;
503 if (e->count < 0 || e->count > bb->count)
505 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
506 e->src->index, e->dest->index,
508 e->count = bb->count / 2;
513 for (e = bb->succ; e; e = e->succ_next)
514 e->probability = (e->count * REG_BR_PROB_BASE + bb->count / 2) / bb->count;
516 && any_condjump_p (bb->end)
517 && bb->succ->succ_next)
523 /* Find the branch edge. It is possible that we do have fake
525 for (e = bb->succ; e->flags & (EDGE_FAKE | EDGE_FALLTHRU);
527 continue; /* Loop body has been intentionally left blank. */
529 prob = e->probability;
530 index = prob * 20 / REG_BR_PROB_BASE;
534 hist_br_prob[index]++;
536 note = find_reg_note (bb->end, REG_BR_PROB, 0);
537 /* There may be already note put by some other pass, such
538 as builtin_expect expander. */
540 XEXP (note, 0) = GEN_INT (prob);
543 = gen_rtx_EXPR_LIST (REG_BR_PROB, GEN_INT (prob),
544 REG_NOTES (bb->end));
548 /* Otherwise distribute the probabilities evenly so we get sane
549 sum. Use simple heuristics that if there are normal edges,
550 give all abnormals frequency of 0, otherwise distribute the
551 frequency over abnormals (this is the case of noreturn
557 for (e = bb->succ; e; e = e->succ_next)
558 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
562 for (e = bb->succ; e; e = e->succ_next)
563 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
564 e->probability = REG_BR_PROB_BASE / total;
570 for (e = bb->succ; e; e = e->succ_next)
572 for (e = bb->succ; e; e = e->succ_next)
573 e->probability = REG_BR_PROB_BASE / total;
576 && any_condjump_p (bb->end)
577 && bb->succ->succ_next)
578 num_branches++, num_never_executed;
584 fprintf (rtl_dump_file, "%d branches\n", num_branches);
585 fprintf (rtl_dump_file, "%d branches never executed\n",
588 for (i = 0; i < 10; i++)
589 fprintf (rtl_dump_file, "%d%% branches in range %d-%d%%\n",
590 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
593 total_num_branches += num_branches;
594 total_num_never_executed += num_never_executed;
595 for (i = 0; i < 20; i++)
596 total_hist_br_prob[i] += hist_br_prob[i];
598 fputc ('\n', rtl_dump_file);
599 fputc ('\n', rtl_dump_file);
602 free_aux_for_blocks ();
605 /* Load value histograms for N_VALUES values whose description is stored
606 in VALUES array from .da file. */
608 compute_value_histograms (unsigned n_values, struct histogram_value *values)
610 unsigned i, j, t, any;
611 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
612 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
613 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
614 gcov_type *aact_count;
616 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
617 n_histogram_counters[t] = 0;
619 for (i = 0; i < n_values; i++)
620 n_histogram_counters[(int) (values[i].type)] += values[i].n_counters;
623 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
625 histogram_counts[t] =
626 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
627 n_histogram_counters[t], &profile_info);
628 if (histogram_counts[t])
630 act_count[t] = histogram_counts[t];
635 for (i = 0; i < n_values; i++)
637 rtx hist_list = NULL_RTX;
638 t = (int) (values[i].type);
640 aact_count = act_count[t];
641 act_count[t] += values[i].n_counters;
642 for (j = values[i].n_counters; j > 0; j--)
643 hist_list = alloc_EXPR_LIST (0, GEN_INT (aact_count[j - 1]), hist_list);
644 hist_list = alloc_EXPR_LIST (0, copy_rtx (values[i].value), hist_list);
645 hist_list = alloc_EXPR_LIST (0, GEN_INT (values[i].type), hist_list);
646 REG_NOTES (values[i].insn) =
647 alloc_EXPR_LIST (REG_VALUE_PROFILE, hist_list,
648 REG_NOTES (values[i].insn));
651 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
652 if (histogram_counts[t])
653 free (histogram_counts[t]);
656 /* Instrument and/or analyze program behavior based on program flow graph.
657 In either case, this function builds a flow graph for the function being
658 compiled. The flow graph is stored in BB_GRAPH.
660 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
661 the flow graph that are needed to reconstruct the dynamic behavior of the
664 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
665 information from a data file containing edge count information from previous
666 executions of the function being compiled. In this case, the flow graph is
667 annotated with actual execution counts, which are later propagated into the
668 rtl for optimization purposes.
670 Main entry point of this file. */
677 unsigned num_edges, ignored_edges;
678 unsigned num_instrumented;
679 struct edge_list *el;
680 unsigned n_values = 0;
681 struct histogram_value *values = NULL;
683 total_num_times_called++;
685 flow_call_edges_add (NULL);
686 add_noreturn_fake_exit_edges ();
688 /* We can't handle cyclic regions constructed using abnormal edges.
689 To avoid these we replace every source of abnormal edge by a fake
690 edge from entry node and every destination by fake edge to exit.
691 This keeps graph acyclic and our calculation exact for all normal
692 edges except for exit and entrance ones.
694 We also add fake exit edges for each call and asm statement in the
695 basic, since it may not return. */
699 int need_exit_edge = 0, need_entry_edge = 0;
700 int have_exit_edge = 0, have_entry_edge = 0;
703 /* Functions returning multiple times are not handled by extra edges.
704 Instead we simply allow negative counts on edges from exit to the
705 block past call and corresponding probabilities. We can't go
706 with the extra edges because that would result in flowgraph that
707 needs to have fake edges outside the spanning tree. */
709 for (e = bb->succ; e; e = e->succ_next)
711 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
712 && e->dest != EXIT_BLOCK_PTR)
714 if (e->dest == EXIT_BLOCK_PTR)
717 for (e = bb->pred; e; e = e->pred_next)
719 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
720 && e->src != ENTRY_BLOCK_PTR)
722 if (e->src == ENTRY_BLOCK_PTR)
726 if (need_exit_edge && !have_exit_edge)
729 fprintf (rtl_dump_file, "Adding fake exit edge to bb %i\n",
731 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
733 if (need_entry_edge && !have_entry_edge)
736 fprintf (rtl_dump_file, "Adding fake entry edge to bb %i\n",
738 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FAKE);
742 el = create_edge_list ();
743 num_edges = NUM_EDGES (el);
744 alloc_aux_for_edges (sizeof (struct edge_info));
746 /* The basic blocks are expected to be numbered sequentially. */
750 for (i = 0 ; i < num_edges ; i++)
752 edge e = INDEX_EDGE (el, i);
755 /* Mark edges we've replaced by fake edges above as ignored. */
756 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
757 && e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR)
759 EDGE_INFO (e)->ignore = 1;
764 #ifdef ENABLE_CHECKING
768 /* Create spanning tree from basic block graph, mark each edge that is
769 on the spanning tree. We insert as many abnormal and critical edges
770 as possible to minimize number of edge splits necessary. */
772 find_spanning_tree (el);
774 /* Fake edges that are not on the tree will not be instrumented, so
775 mark them ignored. */
776 for (num_instrumented = i = 0; i < num_edges; i++)
778 edge e = INDEX_EDGE (el, i);
779 struct edge_info *inf = EDGE_INFO (e);
781 if (inf->ignore || inf->on_tree)
783 else if (e->flags & EDGE_FAKE)
792 total_num_blocks += n_basic_blocks + 2;
794 fprintf (rtl_dump_file, "%d basic blocks\n", n_basic_blocks);
796 total_num_edges += num_edges;
798 fprintf (rtl_dump_file, "%d edges\n", num_edges);
800 total_num_edges_ignored += ignored_edges;
802 fprintf (rtl_dump_file, "%d ignored edges\n", ignored_edges);
804 /* Write the data from which gcov can reconstruct the basic block
807 /* Basic block flags */
808 if (coverage_begin_output ())
810 gcov_position_t offset;
812 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
813 for (i = 0; i != (unsigned) (n_basic_blocks + 2); i++)
814 gcov_write_unsigned (0);
815 gcov_write_length (offset);
818 /* Keep all basic block indexes nonnegative in the gcov output.
819 Index 0 is used for entry block, last index is for exit block.
821 ENTRY_BLOCK_PTR->index = -1;
822 EXIT_BLOCK_PTR->index = last_basic_block;
823 #define BB_TO_GCOV_INDEX(bb) ((bb)->index + 1)
826 if (coverage_begin_output ())
828 gcov_position_t offset;
830 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
834 offset = gcov_write_tag (GCOV_TAG_ARCS);
835 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
837 for (e = bb->succ; e; e = e->succ_next)
839 struct edge_info *i = EDGE_INFO (e);
842 unsigned flag_bits = 0;
845 flag_bits |= GCOV_ARC_ON_TREE;
846 if (e->flags & EDGE_FAKE)
847 flag_bits |= GCOV_ARC_FAKE;
848 if (e->flags & EDGE_FALLTHRU)
849 flag_bits |= GCOV_ARC_FALLTHROUGH;
851 gcov_write_unsigned (BB_TO_GCOV_INDEX (e->dest));
852 gcov_write_unsigned (flag_bits);
856 gcov_write_length (offset);
861 if (coverage_begin_output ())
863 char const *prev_file_name = NULL;
864 gcov_position_t offset;
869 int ignore_next_note = 0;
873 /* We are looking for line number notes. Search backward
874 before basic block to find correct ones. */
875 insn = prev_nonnote_insn (insn);
879 insn = NEXT_INSN (insn);
881 while (insn != bb->end)
883 if (GET_CODE (insn) == NOTE)
885 /* Must ignore the line number notes that
886 immediately follow the end of an inline function
887 to avoid counting it twice. There is a note
888 before the call, and one after the call. */
889 if (NOTE_LINE_NUMBER (insn)
890 == NOTE_INSN_REPEATED_LINE_NUMBER)
891 ignore_next_note = 1;
892 else if (NOTE_LINE_NUMBER (insn) <= 0)
894 else if (ignore_next_note)
895 ignore_next_note = 0;
900 offset = gcov_write_tag (GCOV_TAG_LINES);
901 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
904 /* If this is a new source file, then output the
905 file's name to the .bb file. */
907 || strcmp (NOTE_SOURCE_FILE (insn),
910 prev_file_name = NOTE_SOURCE_FILE (insn);
911 gcov_write_unsigned (0);
912 gcov_write_string (prev_file_name);
914 gcov_write_unsigned (NOTE_LINE_NUMBER (insn));
917 insn = NEXT_INSN (insn);
922 /* A file of NULL indicates the end of run. */
923 gcov_write_unsigned (0);
924 gcov_write_string (NULL);
925 gcov_write_length (offset);
929 ENTRY_BLOCK_PTR->index = ENTRY_BLOCK;
930 EXIT_BLOCK_PTR->index = EXIT_BLOCK;
931 #undef BB_TO_GCOV_INDEX
933 if (flag_profile_values)
935 life_analysis (get_insns (), NULL, PROP_DEATH_NOTES);
936 find_values_to_profile (&n_values, &values);
937 allocate_reg_info (max_reg_num (), FALSE, FALSE);
940 if (flag_branch_probabilities)
942 compute_branch_probabilities ();
943 if (flag_profile_values)
944 compute_value_histograms (n_values, values);
947 /* For each edge not on the spanning tree, add counting code as rtl. */
949 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
951 unsigned n_instrumented = instrument_edges (el);
953 if (n_instrumented != num_instrumented)
956 if (flag_profile_values)
957 instrument_values (n_values, values);
959 /* Commit changes done by instrumentation. */
960 commit_edge_insertions_watch_calls ();
961 allocate_reg_info (max_reg_num (), FALSE, FALSE);
964 if (flag_profile_values)
965 count_or_remove_death_notes (NULL, 1);
966 remove_fake_edges ();
967 free_aux_for_edges ();
968 /* Re-merge split basic blocks and the mess introduced by
969 insert_insn_on_edge. */
970 cleanup_cfg (profile_arc_flag ? CLEANUP_EXPENSIVE : 0);
972 dump_flow_info (rtl_dump_file);
977 /* Union find algorithm implementation for the basic blocks using
981 find_group (basic_block bb)
983 basic_block group = bb, bb1;
985 while ((basic_block) group->aux != group)
986 group = (basic_block) group->aux;
989 while ((basic_block) bb->aux != group)
991 bb1 = (basic_block) bb->aux;
992 bb->aux = (void *) group;
999 union_groups (basic_block bb1, basic_block bb2)
1001 basic_block bb1g = find_group (bb1);
1002 basic_block bb2g = find_group (bb2);
1004 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1005 this code is unlikely going to be performance problem anyway. */
1012 /* This function searches all of the edges in the program flow graph, and puts
1013 as many bad edges as possible onto the spanning tree. Bad edges include
1014 abnormals edges, which can't be instrumented at the moment. Since it is
1015 possible for fake edges to form a cycle, we will have to develop some
1016 better way in the future. Also put critical edges to the tree, since they
1017 are more expensive to instrument. */
1020 find_spanning_tree (struct edge_list *el)
1023 int num_edges = NUM_EDGES (el);
1026 /* We use aux field for standard union-find algorithm. */
1027 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1030 /* Add fake edge exit to entry we can't instrument. */
1031 union_groups (EXIT_BLOCK_PTR, ENTRY_BLOCK_PTR);
1033 /* First add all abnormal edges to the tree unless they form a cycle. Also
1034 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1035 setting return value from function. */
1036 for (i = 0; i < num_edges; i++)
1038 edge e = INDEX_EDGE (el, i);
1039 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1040 || e->dest == EXIT_BLOCK_PTR)
1041 && !EDGE_INFO (e)->ignore
1042 && (find_group (e->src) != find_group (e->dest)))
1045 fprintf (rtl_dump_file, "Abnormal edge %d to %d put to tree\n",
1046 e->src->index, e->dest->index);
1047 EDGE_INFO (e)->on_tree = 1;
1048 union_groups (e->src, e->dest);
1052 /* Now insert all critical edges to the tree unless they form a cycle. */
1053 for (i = 0; i < num_edges; i++)
1055 edge e = INDEX_EDGE (el, i);
1056 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1057 && find_group (e->src) != find_group (e->dest))
1060 fprintf (rtl_dump_file, "Critical edge %d to %d put to tree\n",
1061 e->src->index, e->dest->index);
1062 EDGE_INFO (e)->on_tree = 1;
1063 union_groups (e->src, e->dest);
1067 /* And now the rest. */
1068 for (i = 0; i < num_edges; i++)
1070 edge e = INDEX_EDGE (el, i);
1071 if (!EDGE_INFO (e)->ignore
1072 && find_group (e->src) != find_group (e->dest))
1075 fprintf (rtl_dump_file, "Normal edge %d to %d put to tree\n",
1076 e->src->index, e->dest->index);
1077 EDGE_INFO (e)->on_tree = 1;
1078 union_groups (e->src, e->dest);
1082 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1086 /* Perform file-level initialization for branch-prob processing. */
1089 init_branch_prob (void)
1093 total_num_blocks = 0;
1094 total_num_edges = 0;
1095 total_num_edges_ignored = 0;
1096 total_num_edges_instrumented = 0;
1097 total_num_blocks_created = 0;
1098 total_num_passes = 0;
1099 total_num_times_called = 0;
1100 total_num_branches = 0;
1101 total_num_never_executed = 0;
1102 for (i = 0; i < 20; i++)
1103 total_hist_br_prob[i] = 0;
1106 /* Performs file-level cleanup after branch-prob processing
1110 end_branch_prob (void)
1114 fprintf (rtl_dump_file, "\n");
1115 fprintf (rtl_dump_file, "Total number of blocks: %d\n",
1117 fprintf (rtl_dump_file, "Total number of edges: %d\n", total_num_edges);
1118 fprintf (rtl_dump_file, "Total number of ignored edges: %d\n",
1119 total_num_edges_ignored);
1120 fprintf (rtl_dump_file, "Total number of instrumented edges: %d\n",
1121 total_num_edges_instrumented);
1122 fprintf (rtl_dump_file, "Total number of blocks created: %d\n",
1123 total_num_blocks_created);
1124 fprintf (rtl_dump_file, "Total number of graph solution passes: %d\n",
1126 if (total_num_times_called != 0)
1127 fprintf (rtl_dump_file, "Average number of graph solution passes: %d\n",
1128 (total_num_passes + (total_num_times_called >> 1))
1129 / total_num_times_called);
1130 fprintf (rtl_dump_file, "Total number of branches: %d\n",
1131 total_num_branches);
1132 fprintf (rtl_dump_file, "Total number of branches never executed: %d\n",
1133 total_num_never_executed);
1134 if (total_num_branches)
1138 for (i = 0; i < 10; i++)
1139 fprintf (rtl_dump_file, "%d%% branches in range %d-%d%%\n",
1140 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1141 / total_num_branches, 5*i, 5*i+5);
1147 /* Output instructions as RTL to increment the edge execution count. */
1150 gen_edge_profiler (int edgeno)
1152 rtx ref = coverage_counter_ref (GCOV_COUNTER_ARCS, edgeno);
1154 enum machine_mode mode = GET_MODE (ref);
1158 ref = validize_mem (ref);
1160 tmp = expand_simple_binop (mode, PLUS, ref, const1_rtx,
1161 ref, 0, OPTAB_WIDEN);
1164 emit_move_insn (copy_rtx (ref), tmp);
1166 sequence = get_insns ();
1171 /* Output instructions as RTL to increment the interval histogram counter.
1172 VALUE is the expression whose value is profiled. TAG is the tag of the
1173 section for counters, BASE is offset of the counter position. */
1176 gen_interval_profiler (struct histogram_value *value, unsigned tag,
1179 unsigned gcov_size = tree_low_cst (TYPE_SIZE (GCOV_TYPE_NODE), 1);
1180 enum machine_mode mode = mode_for_size (gcov_size, MODE_INT, 0);
1181 rtx mem_ref, tmp, tmp1, mr, val;
1183 rtx more_label = gen_label_rtx ();
1184 rtx less_label = gen_label_rtx ();
1185 rtx end_of_code_label = gen_label_rtx ();
1186 int per_counter = gcov_size / BITS_PER_UNIT;
1191 emit_insn (value->seq);
1193 mr = gen_reg_rtx (Pmode);
1195 tmp = coverage_counter_ref (tag, base);
1196 tmp = force_reg (Pmode, XEXP (tmp, 0));
1198 val = expand_simple_binop (value->mode, MINUS,
1199 copy_rtx (value->value),
1200 GEN_INT (value->hdata.intvl.int_start),
1201 NULL_RTX, 0, OPTAB_WIDEN);
1203 if (value->hdata.intvl.may_be_more)
1204 do_compare_rtx_and_jump (copy_rtx (val), GEN_INT (value->hdata.intvl.steps),
1205 GE, 0, value->mode, NULL_RTX, NULL_RTX, more_label);
1206 if (value->hdata.intvl.may_be_less)
1207 do_compare_rtx_and_jump (copy_rtx (val), const0_rtx, LT, 0, value->mode,
1208 NULL_RTX, NULL_RTX, less_label);
1210 /* We are in range. */
1211 tmp1 = expand_simple_binop (value->mode, MULT,
1212 copy_rtx (val), GEN_INT (per_counter),
1213 NULL_RTX, 0, OPTAB_WIDEN);
1214 tmp1 = expand_simple_binop (Pmode, PLUS, copy_rtx (tmp), tmp1, mr,
1217 emit_move_insn (copy_rtx (mr), tmp1);
1219 if (value->hdata.intvl.may_be_more
1220 || value->hdata.intvl.may_be_less)
1222 emit_jump_insn (gen_jump (end_of_code_label));
1226 /* Above the interval. */
1227 if (value->hdata.intvl.may_be_more)
1229 emit_label (more_label);
1230 tmp1 = expand_simple_binop (Pmode, PLUS, copy_rtx (tmp),
1231 GEN_INT (per_counter * value->hdata.intvl.steps),
1232 mr, 0, OPTAB_WIDEN);
1234 emit_move_insn (copy_rtx (mr), tmp1);
1235 if (value->hdata.intvl.may_be_less)
1237 emit_jump_insn (gen_jump (end_of_code_label));
1242 /* Below the interval. */
1243 if (value->hdata.intvl.may_be_less)
1245 emit_label (less_label);
1246 tmp1 = expand_simple_binop (Pmode, PLUS, copy_rtx (tmp),
1247 GEN_INT (per_counter * (value->hdata.intvl.steps
1248 + (value->hdata.intvl.may_be_more ? 1 : 0))),
1249 mr, 0, OPTAB_WIDEN);
1251 emit_move_insn (copy_rtx (mr), tmp1);
1254 if (value->hdata.intvl.may_be_more
1255 || value->hdata.intvl.may_be_less)
1256 emit_label (end_of_code_label);
1258 mem_ref = validize_mem (gen_rtx_MEM (mode, mr));
1260 tmp = expand_simple_binop (mode, PLUS, copy_rtx (mem_ref), const1_rtx,
1261 mem_ref, 0, OPTAB_WIDEN);
1264 emit_move_insn (copy_rtx (mem_ref), tmp);
1266 sequence = get_insns ();
1268 rebuild_jump_labels (sequence);
1272 /* Output instructions as RTL to increment the power of two histogram counter.
1273 VALUE is the expression whose value is profiled. TAG is the tag of the
1274 section for counters, BASE is offset of the counter position. */
1277 gen_pow2_profiler (struct histogram_value *value, unsigned tag, unsigned base)
1279 unsigned gcov_size = tree_low_cst (TYPE_SIZE (GCOV_TYPE_NODE), 1);
1280 enum machine_mode mode = mode_for_size (gcov_size, MODE_INT, 0);
1281 rtx mem_ref, tmp, mr, uval;
1283 rtx end_of_code_label = gen_label_rtx ();
1284 rtx loop_label = gen_label_rtx ();
1285 int per_counter = gcov_size / BITS_PER_UNIT;
1290 emit_insn (value->seq);
1292 mr = gen_reg_rtx (Pmode);
1293 tmp = coverage_counter_ref (tag, base);
1294 tmp = force_reg (Pmode, XEXP (tmp, 0));
1295 emit_move_insn (mr, tmp);
1297 uval = gen_reg_rtx (value->mode);
1298 emit_move_insn (uval, copy_rtx (value->value));
1300 /* Check for non-power of 2. */
1301 if (value->hdata.pow2.may_be_other)
1303 do_compare_rtx_and_jump (copy_rtx (uval), const0_rtx, LE, 0, value->mode,
1304 NULL_RTX, NULL_RTX, end_of_code_label);
1305 tmp = expand_simple_binop (value->mode, PLUS, copy_rtx (uval),
1306 constm1_rtx, NULL_RTX, 0, OPTAB_WIDEN);
1307 tmp = expand_simple_binop (value->mode, AND, copy_rtx (uval), tmp,
1308 NULL_RTX, 0, OPTAB_WIDEN);
1309 do_compare_rtx_and_jump (tmp, const0_rtx, NE, 0, value->mode, NULL_RTX,
1310 NULL_RTX, end_of_code_label);
1313 /* Count log_2(value). */
1314 emit_label (loop_label);
1316 tmp = expand_simple_binop (Pmode, PLUS, copy_rtx (mr), GEN_INT (per_counter), mr, 0, OPTAB_WIDEN);
1318 emit_move_insn (copy_rtx (mr), tmp);
1320 tmp = expand_simple_binop (value->mode, ASHIFTRT, copy_rtx (uval), const1_rtx,
1321 uval, 0, OPTAB_WIDEN);
1323 emit_move_insn (copy_rtx (uval), tmp);
1325 do_compare_rtx_and_jump (copy_rtx (uval), const0_rtx, NE, 0, value->mode,
1326 NULL_RTX, NULL_RTX, loop_label);
1328 /* Increase the counter. */
1329 emit_label (end_of_code_label);
1331 mem_ref = validize_mem (gen_rtx_MEM (mode, mr));
1333 tmp = expand_simple_binop (mode, PLUS, copy_rtx (mem_ref), const1_rtx,
1334 mem_ref, 0, OPTAB_WIDEN);
1337 emit_move_insn (copy_rtx (mem_ref), tmp);
1339 sequence = get_insns ();
1341 rebuild_jump_labels (sequence);
1345 /* Output instructions as RTL for code to find the most common value.
1346 VALUE is the expression whose value is profiled. TAG is the tag of the
1347 section for counters, BASE is offset of the counter position. */
1350 gen_one_value_profiler (struct histogram_value *value, unsigned tag,
1353 unsigned gcov_size = tree_low_cst (TYPE_SIZE (GCOV_TYPE_NODE), 1);
1354 enum machine_mode mode = mode_for_size (gcov_size, MODE_INT, 0);
1355 rtx stored_value_ref, counter_ref, all_ref, stored_value, counter, all;
1358 rtx same_label = gen_label_rtx ();
1359 rtx zero_label = gen_label_rtx ();
1360 rtx end_of_code_label = gen_label_rtx ();
1365 emit_insn (value->seq);
1367 stored_value_ref = coverage_counter_ref (tag, base);
1368 counter_ref = coverage_counter_ref (tag, base + 1);
1369 all_ref = coverage_counter_ref (tag, base + 2);
1370 stored_value = validize_mem (stored_value_ref);
1371 counter = validize_mem (counter_ref);
1372 all = validize_mem (all_ref);
1374 uval = gen_reg_rtx (mode);
1375 convert_move (uval, copy_rtx (value->value), 0);
1377 /* Check if the stored value matches. */
1378 do_compare_rtx_and_jump (copy_rtx (uval), copy_rtx (stored_value), EQ,
1379 0, mode, NULL_RTX, NULL_RTX, same_label);
1381 /* Does not match; check whether the counter is zero. */
1382 do_compare_rtx_and_jump (copy_rtx (counter), const0_rtx, EQ, 0, mode,
1383 NULL_RTX, NULL_RTX, zero_label);
1385 /* The counter is not zero yet. */
1386 tmp = expand_simple_binop (mode, PLUS, copy_rtx (counter), constm1_rtx,
1387 counter, 0, OPTAB_WIDEN);
1390 emit_move_insn (copy_rtx (counter), tmp);
1392 emit_jump_insn (gen_jump (end_of_code_label));
1395 emit_label (zero_label);
1396 /* Set new value. */
1397 emit_move_insn (copy_rtx (stored_value), copy_rtx (uval));
1399 emit_label (same_label);
1400 /* Increase the counter. */
1401 tmp = expand_simple_binop (mode, PLUS, copy_rtx (counter), const1_rtx,
1402 counter, 0, OPTAB_WIDEN);
1405 emit_move_insn (copy_rtx (counter), tmp);
1407 emit_label (end_of_code_label);
1409 /* Increase the counter of all executions; this seems redundant given
1410 that ve have counts for edges in cfg, but it may happen that some
1411 optimization will change the counts for the block (either because
1412 it is unable to update them correctly, or because it will duplicate
1413 the block or its part). */
1414 tmp = expand_simple_binop (mode, PLUS, copy_rtx (all), const1_rtx,
1415 all, 0, OPTAB_WIDEN);
1418 emit_move_insn (copy_rtx (all), tmp);
1419 sequence = get_insns ();
1421 rebuild_jump_labels (sequence);
1425 /* Output instructions as RTL for code to find the most common value of
1426 a difference between two evaluations of an expression.
1427 VALUE is the expression whose value is profiled. TAG is the tag of the
1428 section for counters, BASE is offset of the counter position. */
1431 gen_const_delta_profiler (struct histogram_value *value, unsigned tag,
1434 struct histogram_value one_value_delta;
1435 unsigned gcov_size = tree_low_cst (TYPE_SIZE (GCOV_TYPE_NODE), 1);
1436 enum machine_mode mode = mode_for_size (gcov_size, MODE_INT, 0);
1437 rtx stored_value_ref, stored_value, tmp, uval;
1443 emit_insn (value->seq);
1445 stored_value_ref = coverage_counter_ref (tag, base);
1446 stored_value = validize_mem (stored_value_ref);
1448 uval = gen_reg_rtx (mode);
1449 convert_move (uval, copy_rtx (value->value), 0);
1450 tmp = expand_simple_binop (mode, MINUS,
1451 copy_rtx (uval), copy_rtx (stored_value),
1452 NULL_RTX, 0, OPTAB_WIDEN);
1454 one_value_delta.value = tmp;
1455 one_value_delta.mode = mode;
1456 one_value_delta.seq = NULL_RTX;
1457 one_value_delta.insn = value->insn;
1458 one_value_delta.type = HIST_TYPE_SINGLE_VALUE;
1459 emit_insn (gen_one_value_profiler (&one_value_delta, tag, base + 1));
1461 emit_move_insn (copy_rtx (stored_value), uval);
1462 sequence = get_insns ();
1464 rebuild_jump_labels (sequence);