Update count_scale for AutoFDO to prevent over-scale.
[official-gcc.git] / gcc-4_8 / gcc / profile.c
blobd976a17392fc805273f88df08a2de4e087a413c7
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2013 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Generate basic block profile instrumentation and auxiliary files.
24 Profile generation is optimized, so that not all arcs in the basic
25 block graph need instrumenting. First, the BB graph is closed with
26 one entry (function start), and one exit (function exit). Any
27 ABNORMAL_EDGE cannot be instrumented (because there is no control
28 path to place the code). We close the graph by inserting fake
29 EDGE_FAKE edges to the EXIT_BLOCK, from the sources of abnormal
30 edges that do not go to the exit_block. We ignore such abnormal
31 edges. Naturally these fake edges are never directly traversed,
32 and so *cannot* be directly instrumented. Some other graph
33 massaging is done. To optimize the instrumentation we generate the
34 BB minimal span tree, only edges that are not on the span tree
35 (plus the entry point) need instrumenting. From that information
36 all other edge counts can be deduced. By construction all fake
37 edges must be on the spanning tree. We also attempt to place
38 EDGE_CRITICAL edges on the spanning tree.
40 The auxiliary files generated are <dumpbase>.gcno (at compile time)
41 and <dumpbase>.gcda (at run time). The format is
42 described in full in gcov-io.h. */
44 /* ??? Register allocation should use basic block execution counts to
45 give preference to the most commonly executed blocks. */
47 /* ??? Should calculate branch probabilities before instrumenting code, since
48 then we can use arc counts to help decide which arcs to instrument. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "rtl.h"
55 #include "flags.h"
56 #include "regs.h"
57 #include "expr.h"
58 #include "function.h"
59 #include "basic-block.h"
60 #include "diagnostic-core.h"
61 #include "coverage.h"
62 #include "value-prof.h"
63 #include "tree.h"
64 #include "tree-flow.h"
65 #include "cfgloop.h"
66 #include "dumpfile.h"
67 #include "params.h"
69 #include "profile.h"
71 struct bb_info {
72 unsigned int count_valid : 1;
74 /* Number of successor and predecessor edges. */
75 gcov_type succ_count;
76 gcov_type pred_count;
79 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
82 /* Counter summary from the last set of coverage counts read. */
84 const struct gcov_ctr_summary *profile_info;
86 /* Counter working set information computed from the current counter
87 summary. Not initialized unless profile_info summary is non-NULL. */
88 static gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
90 /* Collect statistics on the performance of this pass for the entire source
91 file. */
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 void add_working_set (gcov_working_set_t *set) {
104 int i = 0;
105 for (; i < NUM_GCOV_WORKING_SETS; i++)
106 gcov_working_sets[i] = set[i];
109 /* Forward declarations. */
110 static void find_spanning_tree (struct edge_list *);
112 /* Add edge instrumentation code to the entire insn chain.
114 F is the first insn of the chain.
115 NUM_BLOCKS is the number of basic blocks found in F. */
117 static unsigned
118 instrument_edges (struct edge_list *el)
120 unsigned num_instr_edges = 0;
121 int num_edges = NUM_EDGES (el);
122 basic_block bb;
124 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
126 edge e;
127 edge_iterator ei;
129 FOR_EACH_EDGE (e, ei, bb->succs)
131 struct edge_info *inf = EDGE_INFO (e);
133 if (!inf->ignore && !inf->on_tree)
135 gcc_assert (!(e->flags & EDGE_ABNORMAL));
136 if (dump_file)
137 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
138 e->src->index, e->dest->index,
139 EDGE_CRITICAL_P (e) ? " (and split)" : "");
140 gimple_gen_edge_profiler (num_instr_edges++, e);
145 total_num_blocks_created += num_edges;
146 if (dump_file)
147 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
148 return num_instr_edges;
151 /* Add code to measure histograms for values in list VALUES. */
152 static void
153 instrument_values (histogram_values values)
155 unsigned i;
157 /* Emit code to generate the histograms before the insns. */
159 for (i = 0; i < values.length (); i++)
161 histogram_value hist = values[i];
162 unsigned t = COUNTER_FOR_HIST_TYPE (hist->type);
164 /* See condition in gimple_gen_ic_func_topn_profiler */
165 if (t == GCOV_COUNTER_ICALL_TOPNV
166 && (DECL_STATIC_CONSTRUCTOR (current_function_decl)
167 || DECL_STATIC_CONSTRUCTOR (current_function_decl)
168 || DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (
169 current_function_decl)))
170 continue;
172 if (!coverage_counter_alloc (t, hist->n_counters))
173 continue;
175 switch (hist->type)
177 case HIST_TYPE_INTERVAL:
178 gimple_gen_interval_profiler (hist, t, 0);
179 break;
181 case HIST_TYPE_POW2:
182 gimple_gen_pow2_profiler (hist, t, 0);
183 break;
185 case HIST_TYPE_SINGLE_VALUE:
186 gimple_gen_one_value_profiler (hist, t, 0);
187 break;
189 case HIST_TYPE_CONST_DELTA:
190 gimple_gen_const_delta_profiler (hist, t, 0);
191 break;
193 case HIST_TYPE_INDIR_CALL:
194 case HIST_TYPE_INDIR_CALL_TOPN:
195 gimple_gen_ic_profiler (hist, t, 0);
196 break;
198 case HIST_TYPE_AVERAGE:
199 gimple_gen_average_profiler (hist, t, 0);
200 break;
202 case HIST_TYPE_IOR:
203 gimple_gen_ior_profiler (hist, t, 0);
204 break;
206 default:
207 gcc_unreachable ();
213 /* Fill the working set information into the profile_info structure. */
215 void
216 get_working_sets (void)
218 unsigned ws_ix, pctinc, pct;
219 gcov_working_set_t *ws_info;
221 if (!profile_info)
222 return;
224 compute_working_sets (profile_info, gcov_working_sets);
226 if (dump_file)
228 fprintf (dump_file, "Counter working sets:\n");
229 /* Multiply the percentage by 100 to avoid float. */
230 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
231 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
232 ws_ix++, pct += pctinc)
234 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
235 pct = 9990;
236 ws_info = &gcov_working_sets[ws_ix];
237 /* Print out the percentage using int arithmatic to avoid float. */
238 fprintf (dump_file, "\t\t%u.%02u%%: num counts=%u, min counter="
239 HOST_WIDEST_INT_PRINT_DEC "\n",
240 pct / 100, pct - (pct / 100 * 100),
241 ws_info->num_counters,
242 (HOST_WIDEST_INT)ws_info->min_counter);
247 /* Given a the desired percentage of the full profile (sum_all from the
248 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
249 the corresponding working set information. If an exact match for
250 the percentage isn't found, the closest value is used. */
252 gcov_working_set_t *
253 find_working_set (unsigned pct_times_10)
255 unsigned i;
256 if (!profile_info)
257 return NULL;
258 gcc_assert (pct_times_10 <= 1000);
259 if (pct_times_10 >= 999)
260 return &gcov_working_sets[NUM_GCOV_WORKING_SETS - 1];
261 i = pct_times_10 * NUM_GCOV_WORKING_SETS / 1000;
262 if (!i)
263 return &gcov_working_sets[0];
264 return &gcov_working_sets[i - 1];
267 /* Computes hybrid profile for all matching entries in da_file.
269 CFG_CHECKSUM is the precomputed checksum for the CFG. */
271 static gcov_type *
272 get_exec_counts (unsigned cfg_checksum, unsigned lineno_checksum)
274 unsigned num_edges = 0;
275 basic_block bb;
276 gcov_type *counts;
278 /* Count the edges to be (possibly) instrumented. */
279 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
281 edge e;
282 edge_iterator ei;
284 FOR_EACH_EDGE (e, ei, bb->succs)
285 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
286 num_edges++;
289 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, cfg_checksum,
290 lineno_checksum, &profile_info);
291 if (!counts)
292 return NULL;
294 get_working_sets();
296 if (dump_file && profile_info)
297 fprintf(dump_file, "Merged %u profiles with maximal count %u.\n",
298 profile_info->runs, (unsigned) profile_info->sum_max);
300 return counts;
304 static bool
305 is_edge_inconsistent (vec<edge, va_gc> *edges)
307 edge e;
308 edge_iterator ei;
309 FOR_EACH_EDGE (e, ei, edges)
311 if (!EDGE_INFO (e)->ignore)
313 if (e->count < 0
314 && (!(e->flags & EDGE_FAKE)
315 || e->src == ENTRY_BLOCK_PTR
316 || !block_ends_with_call_p (e->src)))
318 if (dump_file)
320 fprintf (dump_file,
321 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC,
322 e->src->index, e->dest->index, e->count);
323 dump_bb (dump_file, e->src, 0, TDF_DETAILS);
324 dump_bb (dump_file, e->dest, 0, TDF_DETAILS);
326 return true;
330 return false;
333 static void
334 correct_negative_edge_counts (void)
336 basic_block bb;
337 edge e;
338 edge_iterator ei;
340 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
342 FOR_EACH_EDGE (e, ei, bb->succs)
344 if (e->count < 0)
345 e->count = 0;
350 /* Check consistency.
351 Return true if inconsistency is found. */
352 static bool
353 is_inconsistent (void)
355 basic_block bb;
356 bool inconsistent = false;
357 FOR_EACH_BB (bb)
359 inconsistent |= is_edge_inconsistent (bb->preds);
360 if (!dump_file && inconsistent)
361 return true;
362 inconsistent |= is_edge_inconsistent (bb->succs);
363 if (!dump_file && inconsistent)
364 return true;
365 if (bb->count < 0)
367 if (dump_file)
369 fprintf (dump_file, "BB %i count is negative "
370 HOST_WIDEST_INT_PRINT_DEC,
371 bb->index,
372 bb->count);
373 dump_bb (dump_file, bb, 0, TDF_DETAILS);
375 inconsistent = true;
377 if (bb->count != sum_edge_counts (bb->preds))
379 if (dump_file)
381 fprintf (dump_file, "BB %i count does not match sum of incoming edges "
382 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
383 bb->index,
384 bb->count,
385 sum_edge_counts (bb->preds));
386 dump_bb (dump_file, bb, 0, TDF_DETAILS);
388 inconsistent = true;
390 if (bb->count != sum_edge_counts (bb->succs) &&
391 ! (find_edge (bb, EXIT_BLOCK_PTR) != NULL && block_ends_with_call_p (bb)))
393 if (dump_file)
395 fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
396 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
397 bb->index,
398 bb->count,
399 sum_edge_counts (bb->succs));
400 dump_bb (dump_file, bb, 0, TDF_DETAILS);
402 inconsistent = true;
404 if (!dump_file && inconsistent)
405 return true;
408 return inconsistent;
411 /* Set each basic block count to the sum of its outgoing edge counts */
412 static void
413 set_bb_counts (void)
415 basic_block bb;
416 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
418 bb->count = sum_edge_counts (bb->succs);
419 gcc_assert (bb->count >= 0);
423 /* Reads profile data and returns total number of edge counts read */
424 static int
425 read_profile_edge_counts (gcov_type *exec_counts)
427 basic_block bb;
428 int num_edges = 0;
429 int exec_counts_pos = 0;
430 /* For each edge not on the spanning tree, set its execution count from
431 the .da file. */
432 /* The first count in the .da file is the number of times that the function
433 was entered. This is the exec_count for block zero. */
435 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
437 edge e;
438 edge_iterator ei;
440 FOR_EACH_EDGE (e, ei, bb->succs)
441 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
443 num_edges++;
444 if (exec_counts)
446 e->count = exec_counts[exec_counts_pos++];
447 if (e->count > profile_info->sum_max)
449 if (flag_profile_correction)
451 static bool informed = 0;
452 if (dump_enabled_p () && !informed)
453 dump_printf_loc (MSG_NOTE, input_location,
454 "corrupted profile info: edge count exceeds maximal count");
455 informed = 1;
457 else
458 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
459 bb->index, e->dest->index);
462 else
463 e->count = 0;
465 EDGE_INFO (e)->count_valid = 1;
466 BB_INFO (bb)->succ_count--;
467 BB_INFO (e->dest)->pred_count--;
468 if (dump_file)
470 fprintf (dump_file, "\nRead edge from %i to %i, count:",
471 bb->index, e->dest->index);
472 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
473 (HOST_WIDEST_INT) e->count);
478 return num_edges;
481 #define OVERLAP_BASE 10000
483 /* Compare the static estimated profile to the actual profile, and
484 return the "degree of overlap" measure between them.
486 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
487 the sum of each basic block's minimum relative weights between
488 two profiles. And overlap of OVERLAP_BASE means two profiles are
489 identical. */
491 static int
492 compute_frequency_overlap (void)
494 gcov_type count_total = 0, freq_total = 0;
495 int overlap = 0;
496 basic_block bb;
498 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
500 count_total += bb->count;
501 freq_total += bb->frequency;
504 if (count_total == 0 || freq_total == 0)
505 return 0;
507 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
508 overlap += MIN (bb->count * OVERLAP_BASE / count_total,
509 bb->frequency * OVERLAP_BASE / freq_total);
511 return overlap;
514 /* Compute the branch probabilities for the various branches.
515 Annotate them accordingly.
517 CFG_CHECKSUM is the precomputed checksum for the CFG. */
519 static void
520 compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
522 basic_block bb;
523 int i;
524 int num_edges = 0;
525 int changes;
526 int passes;
527 int hist_br_prob[20];
528 int num_branches;
529 gcov_type *exec_counts = get_exec_counts (cfg_checksum, lineno_checksum);
530 int inconsistent = 0;
532 /* Very simple sanity checks so we catch bugs in our profiling code. */
533 if (!profile_info)
534 return;
535 if (profile_info->run_max * profile_info->runs < profile_info->sum_max)
537 error ("corrupted profile info: run_max * runs < sum_max");
538 exec_counts = NULL;
541 if (profile_info->sum_all < profile_info->sum_max)
543 error ("corrupted profile info: sum_all is smaller than sum_max");
544 exec_counts = NULL;
547 /* Attach extra info block to each bb. */
548 alloc_aux_for_blocks (sizeof (struct bb_info));
549 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
551 edge e;
552 edge_iterator ei;
554 FOR_EACH_EDGE (e, ei, bb->succs)
555 if (!EDGE_INFO (e)->ignore)
556 BB_INFO (bb)->succ_count++;
557 FOR_EACH_EDGE (e, ei, bb->preds)
558 if (!EDGE_INFO (e)->ignore)
559 BB_INFO (bb)->pred_count++;
562 /* Avoid predicting entry on exit nodes. */
563 BB_INFO (EXIT_BLOCK_PTR)->succ_count = 2;
564 BB_INFO (ENTRY_BLOCK_PTR)->pred_count = 2;
566 num_edges = read_profile_edge_counts (exec_counts);
568 if (dump_file)
569 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
571 /* For every block in the file,
572 - if every exit/entrance edge has a known count, then set the block count
573 - if the block count is known, and every exit/entrance edge but one has
574 a known execution count, then set the count of the remaining edge
576 As edge counts are set, decrement the succ/pred count, but don't delete
577 the edge, that way we can easily tell when all edges are known, or only
578 one edge is unknown. */
580 /* The order that the basic blocks are iterated through is important.
581 Since the code that finds spanning trees starts with block 0, low numbered
582 edges are put on the spanning tree in preference to high numbered edges.
583 Hence, most instrumented edges are at the end. Graph solving works much
584 faster if we propagate numbers from the end to the start.
586 This takes an average of slightly more than 3 passes. */
588 changes = 1;
589 passes = 0;
590 while (changes)
592 passes++;
593 changes = 0;
594 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR, NULL, prev_bb)
596 struct bb_info *bi = BB_INFO (bb);
597 if (! bi->count_valid)
599 if (bi->succ_count == 0)
601 edge e;
602 edge_iterator ei;
603 gcov_type total = 0;
605 FOR_EACH_EDGE (e, ei, bb->succs)
606 total += e->count;
607 bb->count = total;
608 bi->count_valid = 1;
609 changes = 1;
611 else if (bi->pred_count == 0)
613 edge e;
614 edge_iterator ei;
615 gcov_type total = 0;
617 FOR_EACH_EDGE (e, ei, bb->preds)
618 total += e->count;
619 bb->count = total;
620 bi->count_valid = 1;
621 changes = 1;
624 if (bi->count_valid)
626 if (bi->succ_count == 1)
628 edge e;
629 edge_iterator ei;
630 gcov_type total = 0;
632 /* One of the counts will be invalid, but it is zero,
633 so adding it in also doesn't hurt. */
634 FOR_EACH_EDGE (e, ei, bb->succs)
635 total += e->count;
637 /* Search for the invalid edge, and set its count. */
638 FOR_EACH_EDGE (e, ei, bb->succs)
639 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
640 break;
642 /* Calculate count for remaining edge by conservation. */
643 total = bb->count - total;
645 gcc_assert (e);
646 EDGE_INFO (e)->count_valid = 1;
647 e->count = total;
648 bi->succ_count--;
650 BB_INFO (e->dest)->pred_count--;
651 changes = 1;
653 if (bi->pred_count == 1)
655 edge e;
656 edge_iterator ei;
657 gcov_type total = 0;
659 /* One of the counts will be invalid, but it is zero,
660 so adding it in also doesn't hurt. */
661 FOR_EACH_EDGE (e, ei, bb->preds)
662 total += e->count;
664 /* Search for the invalid edge, and set its count. */
665 FOR_EACH_EDGE (e, ei, bb->preds)
666 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
667 break;
669 /* Calculate count for remaining edge by conservation. */
670 total = bb->count - total + e->count;
672 gcc_assert (e);
673 EDGE_INFO (e)->count_valid = 1;
674 e->count = total;
675 bi->pred_count--;
677 BB_INFO (e->src)->succ_count--;
678 changes = 1;
683 if (dump_file)
685 int overlap = compute_frequency_overlap ();
686 gimple_dump_cfg (dump_file, dump_flags);
687 fprintf (dump_file, "Static profile overlap: %d.%d%%\n",
688 overlap / (OVERLAP_BASE / 100),
689 overlap % (OVERLAP_BASE / 100));
692 total_num_passes += passes;
693 if (dump_file)
694 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
696 /* If the graph has been correctly solved, every block will have a
697 succ and pred count of zero. */
698 FOR_EACH_BB (bb)
700 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
703 /* Check for inconsistent basic block counts */
704 inconsistent = is_inconsistent ();
706 if (inconsistent)
708 if (flag_profile_correction)
710 /* Inconsistency detected. Make it flow-consistent. */
711 static int informed = 0;
712 if (dump_enabled_p () && informed == 0)
714 informed = 1;
715 dump_printf_loc (MSG_NOTE, input_location,
716 "correcting inconsistent profile data");
718 correct_negative_edge_counts ();
719 /* Set bb counts to the sum of the outgoing edge counts */
720 set_bb_counts ();
721 if (dump_file)
722 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n");
723 mcf_smooth_cfg ();
725 else
726 error ("corrupted profile info: profile data is not flow-consistent");
729 /* For every edge, calculate its branch probability and add a reg_note
730 to the branch insn to indicate this. */
732 for (i = 0; i < 20; i++)
733 hist_br_prob[i] = 0;
734 num_branches = 0;
736 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
738 edge e;
739 edge_iterator ei;
741 if (bb->count < 0)
743 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
744 bb->index, (int)bb->count);
745 bb->count = 0;
747 FOR_EACH_EDGE (e, ei, bb->succs)
749 /* Function may return twice in the cased the called function is
750 setjmp or calls fork, but we can't represent this by extra
751 edge from the entry, since extra edge from the exit is
752 already present. We get negative frequency from the entry
753 point. */
754 if ((e->count < 0
755 && e->dest == EXIT_BLOCK_PTR)
756 || (e->count > bb->count
757 && e->dest != EXIT_BLOCK_PTR))
759 if (block_ends_with_call_p (bb))
760 e->count = e->count < 0 ? 0 : bb->count;
762 if (e->count < 0 || e->count > bb->count)
764 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
765 e->src->index, e->dest->index,
766 (int)e->count);
767 e->count = bb->count / 2;
770 if (bb->count)
772 FOR_EACH_EDGE (e, ei, bb->succs)
773 e->probability = (e->count * REG_BR_PROB_BASE + bb->count / 2) / bb->count;
774 if (bb->index >= NUM_FIXED_BLOCKS
775 && block_ends_with_condjump_p (bb)
776 && EDGE_COUNT (bb->succs) >= 2)
778 int prob;
779 edge e;
780 int index;
782 /* Find the branch edge. It is possible that we do have fake
783 edges here. */
784 FOR_EACH_EDGE (e, ei, bb->succs)
785 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
786 break;
788 prob = e->probability;
789 index = prob * 20 / REG_BR_PROB_BASE;
791 if (index == 20)
792 index = 19;
793 hist_br_prob[index]++;
795 num_branches++;
798 /* As a last resort, distribute the probabilities evenly.
799 Use simple heuristics that if there are normal edges,
800 give all abnormals frequency of 0, otherwise distribute the
801 frequency over abnormals (this is the case of noreturn
802 calls). */
803 else if (profile_status == PROFILE_ABSENT)
805 int total = 0;
807 FOR_EACH_EDGE (e, ei, bb->succs)
808 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
809 total ++;
810 if (total)
812 FOR_EACH_EDGE (e, ei, bb->succs)
813 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
814 e->probability = REG_BR_PROB_BASE / total;
815 else
816 e->probability = 0;
818 else
820 total += EDGE_COUNT (bb->succs);
821 FOR_EACH_EDGE (e, ei, bb->succs)
822 e->probability = REG_BR_PROB_BASE / total;
824 if (bb->index >= NUM_FIXED_BLOCKS
825 && block_ends_with_condjump_p (bb)
826 && EDGE_COUNT (bb->succs) >= 2)
827 num_branches++;
830 counts_to_freqs ();
831 profile_status = PROFILE_READ;
832 compute_function_frequency ();
834 if (dump_file)
836 fprintf (dump_file, "%d branches\n", num_branches);
837 if (num_branches)
838 for (i = 0; i < 10; i++)
839 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
840 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
841 5 * i, 5 * i + 5);
843 total_num_branches += num_branches;
844 for (i = 0; i < 20; i++)
845 total_hist_br_prob[i] += hist_br_prob[i];
847 fputc ('\n', dump_file);
848 fputc ('\n', dump_file);
851 free_aux_for_blocks ();
854 /* Load value histograms values whose description is stored in VALUES array
855 from .gcda file.
857 CFG_CHECKSUM is the precomputed checksum for the CFG. */
859 static void
860 compute_value_histograms (histogram_values values, unsigned cfg_checksum,
861 unsigned lineno_checksum)
863 unsigned i, j, t, any;
864 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
865 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
866 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
867 gcov_type *aact_count;
868 bool warned[GCOV_N_VALUE_COUNTERS];
869 static const char *const ctr_names[] = GCOV_COUNTER_NAMES;
871 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
873 n_histogram_counters[t] = 0;
874 warned[t] = 0;
877 for (i = 0; i < values.length (); i++)
879 histogram_value hist = values[i];
880 n_histogram_counters[(int) hist->type] += hist->n_counters;
883 any = 0;
884 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
886 if (!n_histogram_counters[t])
888 histogram_counts[t] = NULL;
889 continue;
892 histogram_counts[t] =
893 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
894 n_histogram_counters[t], cfg_checksum,
895 lineno_checksum, NULL);
896 if (histogram_counts[t])
897 any = 1;
898 act_count[t] = histogram_counts[t];
900 if (!any)
901 return;
903 for (i = 0; i < values.length (); i++)
905 histogram_value hist = values[i];
906 gimple stmt = hist->hvalue.stmt;
908 t = (int) hist->type;
910 aact_count = act_count[t];
911 /* If the counter cannot be found in gcda file, skip this
912 histogram and give a warning. */
913 if (aact_count == 0)
915 if (!warned[t])
916 warning (0, "cannot find %s counters in function %s.",
917 ctr_names[COUNTER_FOR_HIST_TYPE(t)],
918 IDENTIFIER_POINTER (
919 DECL_ASSEMBLER_NAME (current_function_decl)));
920 hist->n_counters = 0;
921 warned[t] = true;
922 continue;
924 act_count[t] += hist->n_counters;
926 gimple_add_histogram_value (cfun, stmt, hist);
927 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
928 for (j = 0; j < hist->n_counters; j++)
929 hist->hvalue.counters[j] = aact_count[j];
932 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
933 free (histogram_counts[t]);
936 /* When passed NULL as file_name, initialize.
937 When passed something else, output the necessary commands to change
938 line to LINE and offset to FILE_NAME. */
939 static void
940 output_location (char const *file_name, int line,
941 gcov_position_t *offset, basic_block bb)
943 static char const *prev_file_name;
944 static int prev_line;
945 bool name_differs, line_differs;
947 if (!file_name)
949 prev_file_name = NULL;
950 prev_line = -1;
951 return;
954 name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name);
955 line_differs = prev_line != line;
957 if (name_differs || line_differs)
959 if (!*offset)
961 *offset = gcov_write_tag (GCOV_TAG_LINES);
962 gcov_write_unsigned (bb->index);
963 name_differs = line_differs=true;
966 /* If this is a new source file, then output the
967 file's name to the .bb file. */
968 if (name_differs)
970 prev_file_name = file_name;
971 gcov_write_unsigned (0);
972 gcov_write_string (prev_file_name);
974 if (line_differs)
976 gcov_write_unsigned (line);
977 prev_line = line;
982 /* Instrument and/or analyze program behavior based on program the CFG.
984 This function creates a representation of the control flow graph (of
985 the function being compiled) that is suitable for the instrumentation
986 of edges and/or converting measured edge counts to counts on the
987 complete CFG.
989 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
990 the flow graph that are needed to reconstruct the dynamic behavior of the
991 flow graph. This data is written to the gcno file for gcov.
993 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
994 information from the gcda file containing edge count information from
995 previous executions of the function being compiled. In this case, the
996 control flow graph is annotated with actual execution counts by
997 compute_branch_probabilities().
999 Main entry point of this file. */
1001 void
1002 branch_prob (void)
1004 basic_block bb;
1005 unsigned i;
1006 unsigned num_edges, ignored_edges;
1007 unsigned num_instrumented;
1008 struct edge_list *el;
1009 histogram_values values = histogram_values();
1010 unsigned cfg_checksum, lineno_checksum;
1012 total_num_times_called++;
1014 flow_call_edges_add (NULL);
1015 add_noreturn_fake_exit_edges ();
1017 /* We can't handle cyclic regions constructed using abnormal edges.
1018 To avoid these we replace every source of abnormal edge by a fake
1019 edge from entry node and every destination by fake edge to exit.
1020 This keeps graph acyclic and our calculation exact for all normal
1021 edges except for exit and entrance ones.
1023 We also add fake exit edges for each call and asm statement in the
1024 basic, since it may not return. */
1026 FOR_EACH_BB (bb)
1028 int need_exit_edge = 0, need_entry_edge = 0;
1029 int have_exit_edge = 0, have_entry_edge = 0;
1030 edge e;
1031 edge_iterator ei;
1033 /* Functions returning multiple times are not handled by extra edges.
1034 Instead we simply allow negative counts on edges from exit to the
1035 block past call and corresponding probabilities. We can't go
1036 with the extra edges because that would result in flowgraph that
1037 needs to have fake edges outside the spanning tree. */
1039 FOR_EACH_EDGE (e, ei, bb->succs)
1041 gimple_stmt_iterator gsi;
1042 gimple last = NULL;
1044 /* It may happen that there are compiler generated statements
1045 without a locus at all. Go through the basic block from the
1046 last to the first statement looking for a locus. */
1047 for (gsi = gsi_last_nondebug_bb (bb);
1048 !gsi_end_p (gsi);
1049 gsi_prev_nondebug (&gsi))
1051 last = gsi_stmt (gsi);
1052 if (gimple_has_location (last))
1053 break;
1056 /* Edge with goto locus might get wrong coverage info unless
1057 it is the only edge out of BB.
1058 Don't do that when the locuses match, so
1059 if (blah) goto something;
1060 is not computed twice. */
1061 if (last
1062 && gimple_has_location (last)
1063 && LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
1064 && !single_succ_p (bb)
1065 && (LOCATION_FILE (e->goto_locus)
1066 != LOCATION_FILE (gimple_location (last))
1067 || (LOCATION_LINE (e->goto_locus)
1068 != LOCATION_LINE (gimple_location (last)))))
1070 basic_block new_bb = split_edge (e);
1071 edge ne = single_succ_edge (new_bb);
1072 ne->goto_locus = e->goto_locus;
1074 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1075 && e->dest != EXIT_BLOCK_PTR)
1076 need_exit_edge = 1;
1077 if (e->dest == EXIT_BLOCK_PTR)
1078 have_exit_edge = 1;
1080 FOR_EACH_EDGE (e, ei, bb->preds)
1082 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1083 && e->src != ENTRY_BLOCK_PTR)
1084 need_entry_edge = 1;
1085 if (e->src == ENTRY_BLOCK_PTR)
1086 have_entry_edge = 1;
1089 if (need_exit_edge && !have_exit_edge)
1091 if (dump_file)
1092 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
1093 bb->index);
1094 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
1096 if (need_entry_edge && !have_entry_edge)
1098 if (dump_file)
1099 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
1100 bb->index);
1101 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FAKE);
1102 /* Avoid bbs that have both fake entry edge and also some
1103 exit edge. One of those edges wouldn't be added to the
1104 spanning tree, but we can't instrument any of them. */
1105 if (have_exit_edge || need_exit_edge)
1107 gimple_stmt_iterator gsi;
1108 gimple first;
1109 tree fndecl;
1111 gsi = gsi_after_labels (bb);
1112 gcc_checking_assert (!gsi_end_p (gsi));
1113 first = gsi_stmt (gsi);
1114 if (is_gimple_debug (first))
1116 gsi_next_nondebug (&gsi);
1117 gcc_checking_assert (!gsi_end_p (gsi));
1118 first = gsi_stmt (gsi);
1120 /* Don't split the bbs containing __builtin_setjmp_receiver
1121 or __builtin_setjmp_dispatcher calls. These are very
1122 special and don't expect anything to be inserted before
1123 them. */
1124 if (!is_gimple_call (first)
1125 || (fndecl = gimple_call_fndecl (first)) == NULL
1126 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL
1127 || (DECL_FUNCTION_CODE (fndecl) != BUILT_IN_SETJMP_RECEIVER
1128 && (DECL_FUNCTION_CODE (fndecl)
1129 != BUILT_IN_SETJMP_DISPATCHER)))
1131 if (dump_file)
1132 fprintf (dump_file, "Splitting bb %i after labels\n",
1133 bb->index);
1134 split_block_after_labels (bb);
1140 el = create_edge_list ();
1141 num_edges = NUM_EDGES (el);
1142 alloc_aux_for_edges (sizeof (struct edge_info));
1144 /* The basic blocks are expected to be numbered sequentially. */
1145 compact_blocks ();
1147 ignored_edges = 0;
1148 for (i = 0 ; i < num_edges ; i++)
1150 edge e = INDEX_EDGE (el, i);
1151 e->count = 0;
1153 /* Mark edges we've replaced by fake edges above as ignored. */
1154 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1155 && e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR)
1157 EDGE_INFO (e)->ignore = 1;
1158 ignored_edges++;
1162 /* Create spanning tree from basic block graph, mark each edge that is
1163 on the spanning tree. We insert as many abnormal and critical edges
1164 as possible to minimize number of edge splits necessary. */
1166 find_spanning_tree (el);
1168 /* Fake edges that are not on the tree will not be instrumented, so
1169 mark them ignored. */
1170 for (num_instrumented = i = 0; i < num_edges; i++)
1172 edge e = INDEX_EDGE (el, i);
1173 struct edge_info *inf = EDGE_INFO (e);
1175 if (inf->ignore || inf->on_tree)
1176 /*NOP*/;
1177 else if (e->flags & EDGE_FAKE)
1179 inf->ignore = 1;
1180 ignored_edges++;
1182 else
1183 num_instrumented++;
1186 total_num_blocks += n_basic_blocks;
1187 if (dump_file)
1188 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks);
1190 total_num_edges += num_edges;
1191 if (dump_file)
1192 fprintf (dump_file, "%d edges\n", num_edges);
1194 total_num_edges_ignored += ignored_edges;
1195 if (dump_file)
1196 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
1198 total_num_edges_instrumented += num_instrumented;
1199 if (dump_file)
1200 fprintf (dump_file, "%d instrumentation edges\n", num_instrumented);
1202 /* Compute two different checksums. Note that we want to compute
1203 the checksum in only once place, since it depends on the shape
1204 of the control flow which can change during
1205 various transformations. */
1206 cfg_checksum = coverage_compute_cfg_checksum ();
1207 lineno_checksum = coverage_compute_lineno_checksum ();
1209 /* Write the data from which gcov can reconstruct the basic block
1210 graph and function line numbers (the gcno file). */
1211 if (coverage_begin_function (lineno_checksum, cfg_checksum))
1213 gcov_position_t offset;
1215 /* Basic block flags */
1216 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
1217 for (i = 0; i != (unsigned) (n_basic_blocks); i++)
1218 gcov_write_unsigned (0);
1219 gcov_write_length (offset);
1221 /* Arcs */
1222 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1224 edge e;
1225 edge_iterator ei;
1227 offset = gcov_write_tag (GCOV_TAG_ARCS);
1228 gcov_write_unsigned (bb->index);
1230 FOR_EACH_EDGE (e, ei, bb->succs)
1232 struct edge_info *i = EDGE_INFO (e);
1233 if (!i->ignore)
1235 unsigned flag_bits = 0;
1237 if (i->on_tree)
1238 flag_bits |= GCOV_ARC_ON_TREE;
1239 if (e->flags & EDGE_FAKE)
1240 flag_bits |= GCOV_ARC_FAKE;
1241 if (e->flags & EDGE_FALLTHRU)
1242 flag_bits |= GCOV_ARC_FALLTHROUGH;
1243 /* On trees we don't have fallthru flags, but we can
1244 recompute them from CFG shape. */
1245 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
1246 && e->src->next_bb == e->dest)
1247 flag_bits |= GCOV_ARC_FALLTHROUGH;
1249 gcov_write_unsigned (e->dest->index);
1250 gcov_write_unsigned (flag_bits);
1254 gcov_write_length (offset);
1257 /* Line numbers. */
1258 /* Initialize the output. */
1259 output_location (NULL, 0, NULL, NULL);
1261 FOR_EACH_BB (bb)
1263 gimple_stmt_iterator gsi;
1264 gcov_position_t offset = 0;
1266 if (bb == ENTRY_BLOCK_PTR->next_bb)
1268 expanded_location curr_location =
1269 expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1270 output_location (curr_location.file, curr_location.line,
1271 &offset, bb);
1274 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1276 gimple stmt = gsi_stmt (gsi);
1277 if (gimple_has_location (stmt))
1278 output_location (gimple_filename (stmt), gimple_lineno (stmt),
1279 &offset, bb);
1282 /* Notice GOTO expressions eliminated while constructing the CFG. */
1283 if (single_succ_p (bb)
1284 && LOCATION_LOCUS (single_succ_edge (bb)->goto_locus)
1285 != UNKNOWN_LOCATION)
1287 expanded_location curr_location
1288 = expand_location (single_succ_edge (bb)->goto_locus);
1289 output_location (curr_location.file, curr_location.line,
1290 &offset, bb);
1293 if (offset)
1295 /* A file of NULL indicates the end of run. */
1296 gcov_write_unsigned (0);
1297 gcov_write_string (NULL);
1298 gcov_write_length (offset);
1303 if (flag_profile_values)
1304 gimple_find_values_to_profile (&values);
1306 if (flag_branch_probabilities && !flag_auto_profile)
1308 compute_branch_probabilities (cfg_checksum, lineno_checksum);
1309 if (flag_profile_values)
1310 compute_value_histograms (values, cfg_checksum, lineno_checksum);
1313 remove_fake_edges ();
1315 /* For each edge not on the spanning tree, add counting code. */
1316 if (profile_arc_flag
1317 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1319 unsigned n_instrumented;
1321 gimple_init_edge_profiler ();
1323 n_instrumented = instrument_edges (el);
1325 gcc_assert (n_instrumented == num_instrumented);
1327 if (flag_profile_values)
1328 instrument_values (values);
1330 /* Commit changes done by instrumentation. */
1331 gsi_commit_edge_inserts ();
1333 if (flag_profile_generate_sampling
1334 || PARAM_VALUE (PARAM_COVERAGE_EXEC_ONCE))
1335 add_sampling_to_edge_counters ();
1338 free_aux_for_edges ();
1340 values.release ();
1341 free_edge_list (el);
1342 coverage_end_function (lineno_checksum, cfg_checksum);
1345 /* Union find algorithm implementation for the basic blocks using
1346 aux fields. */
1348 static basic_block
1349 find_group (basic_block bb)
1351 basic_block group = bb, bb1;
1353 while ((basic_block) group->aux != group)
1354 group = (basic_block) group->aux;
1356 /* Compress path. */
1357 while ((basic_block) bb->aux != group)
1359 bb1 = (basic_block) bb->aux;
1360 bb->aux = (void *) group;
1361 bb = bb1;
1363 return group;
1366 static void
1367 union_groups (basic_block bb1, basic_block bb2)
1369 basic_block bb1g = find_group (bb1);
1370 basic_block bb2g = find_group (bb2);
1372 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1373 this code is unlikely going to be performance problem anyway. */
1374 gcc_assert (bb1g != bb2g);
1376 bb1g->aux = bb2g;
1379 /* This function searches all of the edges in the program flow graph, and puts
1380 as many bad edges as possible onto the spanning tree. Bad edges include
1381 abnormals edges, which can't be instrumented at the moment. Since it is
1382 possible for fake edges to form a cycle, we will have to develop some
1383 better way in the future. Also put critical edges to the tree, since they
1384 are more expensive to instrument. */
1386 static void
1387 find_spanning_tree (struct edge_list *el)
1389 int i;
1390 int num_edges = NUM_EDGES (el);
1391 basic_block bb;
1393 /* We use aux field for standard union-find algorithm. */
1394 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1395 bb->aux = bb;
1397 /* Add fake edge exit to entry we can't instrument. */
1398 union_groups (EXIT_BLOCK_PTR, ENTRY_BLOCK_PTR);
1400 /* First add all abnormal edges to the tree unless they form a cycle. Also
1401 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1402 setting return value from function. */
1403 for (i = 0; i < num_edges; i++)
1405 edge e = INDEX_EDGE (el, i);
1406 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1407 || e->dest == EXIT_BLOCK_PTR)
1408 && !EDGE_INFO (e)->ignore
1409 && (find_group (e->src) != find_group (e->dest)))
1411 if (dump_file)
1412 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1413 e->src->index, e->dest->index);
1414 EDGE_INFO (e)->on_tree = 1;
1415 union_groups (e->src, e->dest);
1419 /* Now insert all critical edges to the tree unless they form a cycle. */
1420 for (i = 0; i < num_edges; i++)
1422 edge e = INDEX_EDGE (el, i);
1423 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1424 && find_group (e->src) != find_group (e->dest))
1426 if (dump_file)
1427 fprintf (dump_file, "Critical edge %d to %d put to tree\n",
1428 e->src->index, e->dest->index);
1429 EDGE_INFO (e)->on_tree = 1;
1430 union_groups (e->src, e->dest);
1434 /* And now the rest. */
1435 for (i = 0; i < num_edges; i++)
1437 edge e = INDEX_EDGE (el, i);
1438 if (!EDGE_INFO (e)->ignore
1439 && find_group (e->src) != find_group (e->dest))
1441 if (dump_file)
1442 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1443 e->src->index, e->dest->index);
1444 EDGE_INFO (e)->on_tree = 1;
1445 union_groups (e->src, e->dest);
1449 clear_aux_for_blocks ();
1452 /* Perform file-level initialization for branch-prob processing. */
1454 void
1455 init_branch_prob (void)
1457 int i;
1459 total_num_blocks = 0;
1460 total_num_edges = 0;
1461 total_num_edges_ignored = 0;
1462 total_num_edges_instrumented = 0;
1463 total_num_blocks_created = 0;
1464 total_num_passes = 0;
1465 total_num_times_called = 0;
1466 total_num_branches = 0;
1467 for (i = 0; i < 20; i++)
1468 total_hist_br_prob[i] = 0;
1471 /* Performs file-level cleanup after branch-prob processing
1472 is completed. */
1474 void
1475 end_branch_prob (void)
1477 if (dump_file)
1479 fprintf (dump_file, "\n");
1480 fprintf (dump_file, "Total number of blocks: %d\n",
1481 total_num_blocks);
1482 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1483 fprintf (dump_file, "Total number of ignored edges: %d\n",
1484 total_num_edges_ignored);
1485 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1486 total_num_edges_instrumented);
1487 fprintf (dump_file, "Total number of blocks created: %d\n",
1488 total_num_blocks_created);
1489 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1490 total_num_passes);
1491 if (total_num_times_called != 0)
1492 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1493 (total_num_passes + (total_num_times_called >> 1))
1494 / total_num_times_called);
1495 fprintf (dump_file, "Total number of branches: %d\n",
1496 total_num_branches);
1497 if (total_num_branches)
1499 int i;
1501 for (i = 0; i < 10; i++)
1502 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1503 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1504 / total_num_branches, 5*i, 5*i+5);