2012-09-11 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / profile.c
bloba5029a1037ba728deda9ef8c1f4d70b3934e940f
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
6 based on some ideas from Dain Samples of UC Berkeley.
7 Further mangling by Bob Manson, Cygnus Support.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* Generate basic block profile instrumentation and auxiliary files.
26 Profile generation is optimized, so that not all arcs in the basic
27 block graph need instrumenting. First, the BB graph is closed with
28 one entry (function start), and one exit (function exit). Any
29 ABNORMAL_EDGE cannot be instrumented (because there is no control
30 path to place the code). We close the graph by inserting fake
31 EDGE_FAKE edges to the EXIT_BLOCK, from the sources of abnormal
32 edges that do not go to the exit_block. We ignore such abnormal
33 edges. Naturally these fake edges are never directly traversed,
34 and so *cannot* be directly instrumented. Some other graph
35 massaging is done. To optimize the instrumentation we generate the
36 BB minimal span tree, only edges that are not on the span tree
37 (plus the entry point) need instrumenting. From that information
38 all other edge counts can be deduced. By construction all fake
39 edges must be on the spanning tree. We also attempt to place
40 EDGE_CRITICAL edges on the spanning tree.
42 The auxiliary files generated are <dumpbase>.gcno (at compile time)
43 and <dumpbase>.gcda (at run time). The format is
44 described in full in gcov-io.h. */
46 /* ??? Register allocation should use basic block execution counts to
47 give preference to the most commonly executed blocks. */
49 /* ??? Should calculate branch probabilities before instrumenting code, since
50 then we can use arc counts to help decide which arcs to instrument. */
52 #include "config.h"
53 #include "system.h"
54 #include "coretypes.h"
55 #include "tm.h"
56 #include "rtl.h"
57 #include "flags.h"
58 #include "regs.h"
59 #include "expr.h"
60 #include "function.h"
61 #include "basic-block.h"
62 #include "diagnostic-core.h"
63 #include "coverage.h"
64 #include "value-prof.h"
65 #include "tree.h"
66 #include "tree-flow.h"
67 #include "cfgloop.h"
68 #include "dumpfile.h"
70 #include "profile.h"
72 struct bb_info {
73 unsigned int count_valid : 1;
75 /* Number of successor and predecessor edges. */
76 gcov_type succ_count;
77 gcov_type pred_count;
80 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
83 /* Counter summary from the last set of coverage counts read. */
85 const struct gcov_ctr_summary *profile_info;
87 /* Number of data points in the working set summary array. Using 128
88 provides information for at least every 1% increment of the total
89 profile size. The last entry is hardwired to 99.9% of the total. */
90 #define NUM_GCOV_WORKING_SETS 128
92 /* Counter working set information computed from the current counter
93 summary. Not initialized unless profile_info summary is non-NULL. */
94 static gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
96 /* Collect statistics on the performance of this pass for the entire source
97 file. */
99 static int total_num_blocks;
100 static int total_num_edges;
101 static int total_num_edges_ignored;
102 static int total_num_edges_instrumented;
103 static int total_num_blocks_created;
104 static int total_num_passes;
105 static int total_num_times_called;
106 static int total_hist_br_prob[20];
107 static int total_num_branches;
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 < VEC_length (histogram_value, values); i++)
161 histogram_value hist = VEC_index (histogram_value, values, i);
162 unsigned t = COUNTER_FOR_HIST_TYPE (hist->type);
164 if (!coverage_counter_alloc (t, hist->n_counters))
165 continue;
167 switch (hist->type)
169 case HIST_TYPE_INTERVAL:
170 gimple_gen_interval_profiler (hist, t, 0);
171 break;
173 case HIST_TYPE_POW2:
174 gimple_gen_pow2_profiler (hist, t, 0);
175 break;
177 case HIST_TYPE_SINGLE_VALUE:
178 gimple_gen_one_value_profiler (hist, t, 0);
179 break;
181 case HIST_TYPE_CONST_DELTA:
182 gimple_gen_const_delta_profiler (hist, t, 0);
183 break;
185 case HIST_TYPE_INDIR_CALL:
186 gimple_gen_ic_profiler (hist, t, 0);
187 break;
189 case HIST_TYPE_AVERAGE:
190 gimple_gen_average_profiler (hist, t, 0);
191 break;
193 case HIST_TYPE_IOR:
194 gimple_gen_ior_profiler (hist, t, 0);
195 break;
197 default:
198 gcc_unreachable ();
204 /* Compute the working set information from the counter histogram in
205 the profile summary. This is an array of information corresponding to a
206 range of percentages of the total execution count (sum_all), and includes
207 the number of counters required to cover that working set percentage and
208 the minimum counter value in that working set. */
210 static void
211 compute_working_sets (void)
213 gcov_type working_set_cum_values[NUM_GCOV_WORKING_SETS];
214 gcov_type ws_cum_hotness_incr;
215 gcov_type cum, tmp_cum;
216 const gcov_bucket_type *histo_bucket;
217 unsigned ws_ix, c_num, count, pctinc, pct;
218 int h_ix;
219 gcov_working_set_t *ws_info;
221 if (!profile_info)
222 return;
224 /* Compute the amount of sum_all that the cumulative hotness grows
225 by in each successive working set entry, which depends on the
226 number of working set entries. */
227 ws_cum_hotness_incr = profile_info->sum_all / NUM_GCOV_WORKING_SETS;
229 /* Next fill in an array of the cumulative hotness values corresponding
230 to each working set summary entry we are going to compute below.
231 Skip 0% statistics, which can be extrapolated from the
232 rest of the summary data. */
233 cum = ws_cum_hotness_incr;
234 for (ws_ix = 0; ws_ix < NUM_GCOV_WORKING_SETS;
235 ws_ix++, cum += ws_cum_hotness_incr)
236 working_set_cum_values[ws_ix] = cum;
237 /* The last summary entry is reserved for (roughly) 99.9% of the
238 working set. Divide by 1024 so it becomes a shift, which gives
239 almost exactly 99.9%. */
240 working_set_cum_values[NUM_GCOV_WORKING_SETS-1]
241 = profile_info->sum_all - profile_info->sum_all/1024;
243 /* Next, walk through the histogram in decending order of hotness
244 and compute the statistics for the working set summary array.
245 As histogram entries are accumulated, we check to see which
246 working set entries have had their expected cum_value reached
247 and fill them in, walking the working set entries in increasing
248 size of cum_value. */
249 ws_ix = 0; /* The current entry into the working set array. */
250 cum = 0; /* The current accumulated counter sum. */
251 count = 0; /* The current accumulated count of block counters. */
252 for (h_ix = GCOV_HISTOGRAM_SIZE - 1;
253 h_ix >= 0 && ws_ix < NUM_GCOV_WORKING_SETS; h_ix--)
255 histo_bucket = &profile_info->histogram[h_ix];
257 /* If we haven't reached the required cumulative counter value for
258 the current working set percentage, simply accumulate this histogram
259 entry into the running sums and continue to the next histogram
260 entry. */
261 if (cum + histo_bucket->cum_value < working_set_cum_values[ws_ix])
263 cum += histo_bucket->cum_value;
264 count += histo_bucket->num_counters;
265 continue;
268 /* If adding the current histogram entry's cumulative counter value
269 causes us to exceed the current working set size, then estimate
270 how many of this histogram entry's counter values are required to
271 reach the working set size, and fill in working set entries
272 as we reach their expected cumulative value. */
273 for (c_num = 0, tmp_cum = cum;
274 c_num < histo_bucket->num_counters && ws_ix < NUM_GCOV_WORKING_SETS;
275 c_num++)
277 count++;
278 /* If we haven't reached the last histogram entry counter, add
279 in the minimum value again. This will underestimate the
280 cumulative sum so far, because many of the counter values in this
281 entry may have been larger than the minimum. We could add in the
282 average value every time, but that would require an expensive
283 divide operation. */
284 if (c_num + 1 < histo_bucket->num_counters)
285 tmp_cum += histo_bucket->min_value;
286 /* If we have reached the last histogram entry counter, then add
287 in the entire cumulative value. */
288 else
289 tmp_cum = cum + histo_bucket->cum_value;
291 /* Next walk through successive working set entries and fill in
292 the statistics for any whose size we have reached by accumulating
293 this histogram counter. */
294 while (tmp_cum >= working_set_cum_values[ws_ix]
295 && ws_ix < NUM_GCOV_WORKING_SETS)
297 gcov_working_sets[ws_ix].num_counters = count;
298 gcov_working_sets[ws_ix].min_counter
299 = histo_bucket->min_value;
300 ws_ix++;
303 /* Finally, update the running cumulative value since we were
304 using a temporary above. */
305 cum += histo_bucket->cum_value;
307 gcc_assert (ws_ix == NUM_GCOV_WORKING_SETS);
309 if (dump_file)
311 fprintf (dump_file, "Counter working sets:\n");
312 /* Multiply the percentage by 100 to avoid float. */
313 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
314 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
315 ws_ix++, pct += pctinc)
317 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
318 pct = 9990;
319 ws_info = &gcov_working_sets[ws_ix];
320 /* Print out the percentage using int arithmatic to avoid float. */
321 fprintf (dump_file, "\t\t%u.%02u%%: num counts=%u, min counter="
322 HOST_WIDEST_INT_PRINT_DEC "\n",
323 pct / 100, pct - (pct / 100 * 100),
324 ws_info->num_counters,
325 (HOST_WIDEST_INT)ws_info->min_counter);
330 /* Given a the desired percentage of the full profile (sum_all from the
331 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
332 the corresponding working set information. If an exact match for
333 the percentage isn't found, the closest value is used. */
335 gcov_working_set_t *
336 find_working_set (unsigned pct_times_10)
338 unsigned i;
339 if (!profile_info)
340 return NULL;
341 gcc_assert (pct_times_10 <= 1000);
342 if (pct_times_10 >= 999)
343 return &gcov_working_sets[NUM_GCOV_WORKING_SETS - 1];
344 i = pct_times_10 * NUM_GCOV_WORKING_SETS / 1000;
345 if (!i)
346 return &gcov_working_sets[0];
347 return &gcov_working_sets[i - 1];
350 /* Computes hybrid profile for all matching entries in da_file.
352 CFG_CHECKSUM is the precomputed checksum for the CFG. */
354 static gcov_type *
355 get_exec_counts (unsigned cfg_checksum, unsigned lineno_checksum)
357 unsigned num_edges = 0;
358 basic_block bb;
359 gcov_type *counts;
361 /* Count the edges to be (possibly) instrumented. */
362 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
364 edge e;
365 edge_iterator ei;
367 FOR_EACH_EDGE (e, ei, bb->succs)
368 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
369 num_edges++;
372 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, cfg_checksum,
373 lineno_checksum, &profile_info);
374 if (!counts)
375 return NULL;
377 compute_working_sets();
379 if (dump_file && profile_info)
380 fprintf(dump_file, "Merged %u profiles with maximal count %u.\n",
381 profile_info->runs, (unsigned) profile_info->sum_max);
383 return counts;
387 static bool
388 is_edge_inconsistent (VEC(edge,gc) *edges)
390 edge e;
391 edge_iterator ei;
392 FOR_EACH_EDGE (e, ei, edges)
394 if (!EDGE_INFO (e)->ignore)
396 if (e->count < 0
397 && (!(e->flags & EDGE_FAKE)
398 || !block_ends_with_call_p (e->src)))
400 if (dump_file)
402 fprintf (dump_file,
403 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC,
404 e->src->index, e->dest->index, e->count);
405 dump_bb (dump_file, e->src, 0, TDF_DETAILS);
406 dump_bb (dump_file, e->dest, 0, TDF_DETAILS);
408 return true;
412 return false;
415 static void
416 correct_negative_edge_counts (void)
418 basic_block bb;
419 edge e;
420 edge_iterator ei;
422 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
424 FOR_EACH_EDGE (e, ei, bb->succs)
426 if (e->count < 0)
427 e->count = 0;
432 /* Check consistency.
433 Return true if inconsistency is found. */
434 static bool
435 is_inconsistent (void)
437 basic_block bb;
438 bool inconsistent = false;
439 FOR_EACH_BB (bb)
441 inconsistent |= is_edge_inconsistent (bb->preds);
442 if (!dump_file && inconsistent)
443 return true;
444 inconsistent |= is_edge_inconsistent (bb->succs);
445 if (!dump_file && inconsistent)
446 return true;
447 if (bb->count < 0)
449 if (dump_file)
451 fprintf (dump_file, "BB %i count is negative "
452 HOST_WIDEST_INT_PRINT_DEC,
453 bb->index,
454 bb->count);
455 dump_bb (dump_file, bb, 0, TDF_DETAILS);
457 inconsistent = true;
459 if (bb->count != sum_edge_counts (bb->preds))
461 if (dump_file)
463 fprintf (dump_file, "BB %i count does not match sum of incoming edges "
464 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
465 bb->index,
466 bb->count,
467 sum_edge_counts (bb->preds));
468 dump_bb (dump_file, bb, 0, TDF_DETAILS);
470 inconsistent = true;
472 if (bb->count != sum_edge_counts (bb->succs) &&
473 ! (find_edge (bb, EXIT_BLOCK_PTR) != NULL && block_ends_with_call_p (bb)))
475 if (dump_file)
477 fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
478 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
479 bb->index,
480 bb->count,
481 sum_edge_counts (bb->succs));
482 dump_bb (dump_file, bb, 0, TDF_DETAILS);
484 inconsistent = true;
486 if (!dump_file && inconsistent)
487 return true;
490 return inconsistent;
493 /* Set each basic block count to the sum of its outgoing edge counts */
494 static void
495 set_bb_counts (void)
497 basic_block bb;
498 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
500 bb->count = sum_edge_counts (bb->succs);
501 gcc_assert (bb->count >= 0);
505 /* Reads profile data and returns total number of edge counts read */
506 static int
507 read_profile_edge_counts (gcov_type *exec_counts)
509 basic_block bb;
510 int num_edges = 0;
511 int exec_counts_pos = 0;
512 /* For each edge not on the spanning tree, set its execution count from
513 the .da file. */
514 /* The first count in the .da file is the number of times that the function
515 was entered. This is the exec_count for block zero. */
517 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
519 edge e;
520 edge_iterator ei;
522 FOR_EACH_EDGE (e, ei, bb->succs)
523 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
525 num_edges++;
526 if (exec_counts)
528 e->count = exec_counts[exec_counts_pos++];
529 if (e->count > profile_info->sum_max)
531 if (flag_profile_correction)
533 static bool informed = 0;
534 if (!informed)
535 inform (input_location,
536 "corrupted profile info: edge count exceeds maximal count");
537 informed = 1;
539 else
540 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
541 bb->index, e->dest->index);
544 else
545 e->count = 0;
547 EDGE_INFO (e)->count_valid = 1;
548 BB_INFO (bb)->succ_count--;
549 BB_INFO (e->dest)->pred_count--;
550 if (dump_file)
552 fprintf (dump_file, "\nRead edge from %i to %i, count:",
553 bb->index, e->dest->index);
554 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
555 (HOST_WIDEST_INT) e->count);
560 return num_edges;
563 #define OVERLAP_BASE 10000
565 /* Compare the static estimated profile to the actual profile, and
566 return the "degree of overlap" measure between them.
568 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
569 the sum of each basic block's minimum relative weights between
570 two profiles. And overlap of OVERLAP_BASE means two profiles are
571 identical. */
573 static int
574 compute_frequency_overlap (void)
576 gcov_type count_total = 0, freq_total = 0;
577 int overlap = 0;
578 basic_block bb;
580 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
582 count_total += bb->count;
583 freq_total += bb->frequency;
586 if (count_total == 0 || freq_total == 0)
587 return 0;
589 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
590 overlap += MIN (bb->count * OVERLAP_BASE / count_total,
591 bb->frequency * OVERLAP_BASE / freq_total);
593 return overlap;
596 /* Compute the branch probabilities for the various branches.
597 Annotate them accordingly.
599 CFG_CHECKSUM is the precomputed checksum for the CFG. */
601 static void
602 compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
604 basic_block bb;
605 int i;
606 int num_edges = 0;
607 int changes;
608 int passes;
609 int hist_br_prob[20];
610 int num_branches;
611 gcov_type *exec_counts = get_exec_counts (cfg_checksum, lineno_checksum);
612 int inconsistent = 0;
614 /* Very simple sanity checks so we catch bugs in our profiling code. */
615 if (!profile_info)
616 return;
617 if (profile_info->run_max * profile_info->runs < profile_info->sum_max)
619 error ("corrupted profile info: run_max * runs < sum_max");
620 exec_counts = NULL;
623 if (profile_info->sum_all < profile_info->sum_max)
625 error ("corrupted profile info: sum_all is smaller than sum_max");
626 exec_counts = NULL;
629 /* Attach extra info block to each bb. */
630 alloc_aux_for_blocks (sizeof (struct bb_info));
631 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
633 edge e;
634 edge_iterator ei;
636 FOR_EACH_EDGE (e, ei, bb->succs)
637 if (!EDGE_INFO (e)->ignore)
638 BB_INFO (bb)->succ_count++;
639 FOR_EACH_EDGE (e, ei, bb->preds)
640 if (!EDGE_INFO (e)->ignore)
641 BB_INFO (bb)->pred_count++;
644 /* Avoid predicting entry on exit nodes. */
645 BB_INFO (EXIT_BLOCK_PTR)->succ_count = 2;
646 BB_INFO (ENTRY_BLOCK_PTR)->pred_count = 2;
648 num_edges = read_profile_edge_counts (exec_counts);
650 if (dump_file)
651 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
653 /* For every block in the file,
654 - if every exit/entrance edge has a known count, then set the block count
655 - if the block count is known, and every exit/entrance edge but one has
656 a known execution count, then set the count of the remaining edge
658 As edge counts are set, decrement the succ/pred count, but don't delete
659 the edge, that way we can easily tell when all edges are known, or only
660 one edge is unknown. */
662 /* The order that the basic blocks are iterated through is important.
663 Since the code that finds spanning trees starts with block 0, low numbered
664 edges are put on the spanning tree in preference to high numbered edges.
665 Hence, most instrumented edges are at the end. Graph solving works much
666 faster if we propagate numbers from the end to the start.
668 This takes an average of slightly more than 3 passes. */
670 changes = 1;
671 passes = 0;
672 while (changes)
674 passes++;
675 changes = 0;
676 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR, NULL, prev_bb)
678 struct bb_info *bi = BB_INFO (bb);
679 if (! bi->count_valid)
681 if (bi->succ_count == 0)
683 edge e;
684 edge_iterator ei;
685 gcov_type total = 0;
687 FOR_EACH_EDGE (e, ei, bb->succs)
688 total += e->count;
689 bb->count = total;
690 bi->count_valid = 1;
691 changes = 1;
693 else if (bi->pred_count == 0)
695 edge e;
696 edge_iterator ei;
697 gcov_type total = 0;
699 FOR_EACH_EDGE (e, ei, bb->preds)
700 total += e->count;
701 bb->count = total;
702 bi->count_valid = 1;
703 changes = 1;
706 if (bi->count_valid)
708 if (bi->succ_count == 1)
710 edge e;
711 edge_iterator ei;
712 gcov_type total = 0;
714 /* One of the counts will be invalid, but it is zero,
715 so adding it in also doesn't hurt. */
716 FOR_EACH_EDGE (e, ei, bb->succs)
717 total += e->count;
719 /* Search for the invalid edge, and set its count. */
720 FOR_EACH_EDGE (e, ei, bb->succs)
721 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
722 break;
724 /* Calculate count for remaining edge by conservation. */
725 total = bb->count - total;
727 gcc_assert (e);
728 EDGE_INFO (e)->count_valid = 1;
729 e->count = total;
730 bi->succ_count--;
732 BB_INFO (e->dest)->pred_count--;
733 changes = 1;
735 if (bi->pred_count == 1)
737 edge e;
738 edge_iterator ei;
739 gcov_type total = 0;
741 /* One of the counts will be invalid, but it is zero,
742 so adding it in also doesn't hurt. */
743 FOR_EACH_EDGE (e, ei, bb->preds)
744 total += e->count;
746 /* Search for the invalid edge, and set its count. */
747 FOR_EACH_EDGE (e, ei, bb->preds)
748 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
749 break;
751 /* Calculate count for remaining edge by conservation. */
752 total = bb->count - total + e->count;
754 gcc_assert (e);
755 EDGE_INFO (e)->count_valid = 1;
756 e->count = total;
757 bi->pred_count--;
759 BB_INFO (e->src)->succ_count--;
760 changes = 1;
765 if (dump_file)
767 int overlap = compute_frequency_overlap ();
768 gimple_dump_cfg (dump_file, dump_flags);
769 fprintf (dump_file, "Static profile overlap: %d.%d%%\n",
770 overlap / (OVERLAP_BASE / 100),
771 overlap % (OVERLAP_BASE / 100));
774 total_num_passes += passes;
775 if (dump_file)
776 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
778 /* If the graph has been correctly solved, every block will have a
779 succ and pred count of zero. */
780 FOR_EACH_BB (bb)
782 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
785 /* Check for inconsistent basic block counts */
786 inconsistent = is_inconsistent ();
788 if (inconsistent)
790 if (flag_profile_correction)
792 /* Inconsistency detected. Make it flow-consistent. */
793 static int informed = 0;
794 if (informed == 0)
796 informed = 1;
797 inform (input_location, "correcting inconsistent profile data");
799 correct_negative_edge_counts ();
800 /* Set bb counts to the sum of the outgoing edge counts */
801 set_bb_counts ();
802 if (dump_file)
803 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n");
804 mcf_smooth_cfg ();
806 else
807 error ("corrupted profile info: profile data is not flow-consistent");
810 /* For every edge, calculate its branch probability and add a reg_note
811 to the branch insn to indicate this. */
813 for (i = 0; i < 20; i++)
814 hist_br_prob[i] = 0;
815 num_branches = 0;
817 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
819 edge e;
820 edge_iterator ei;
822 if (bb->count < 0)
824 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
825 bb->index, (int)bb->count);
826 bb->count = 0;
828 FOR_EACH_EDGE (e, ei, bb->succs)
830 /* Function may return twice in the cased the called function is
831 setjmp or calls fork, but we can't represent this by extra
832 edge from the entry, since extra edge from the exit is
833 already present. We get negative frequency from the entry
834 point. */
835 if ((e->count < 0
836 && e->dest == EXIT_BLOCK_PTR)
837 || (e->count > bb->count
838 && e->dest != EXIT_BLOCK_PTR))
840 if (block_ends_with_call_p (bb))
841 e->count = e->count < 0 ? 0 : bb->count;
843 if (e->count < 0 || e->count > bb->count)
845 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
846 e->src->index, e->dest->index,
847 (int)e->count);
848 e->count = bb->count / 2;
851 if (bb->count)
853 FOR_EACH_EDGE (e, ei, bb->succs)
854 e->probability = (e->count * REG_BR_PROB_BASE + bb->count / 2) / bb->count;
855 if (bb->index >= NUM_FIXED_BLOCKS
856 && block_ends_with_condjump_p (bb)
857 && EDGE_COUNT (bb->succs) >= 2)
859 int prob;
860 edge e;
861 int index;
863 /* Find the branch edge. It is possible that we do have fake
864 edges here. */
865 FOR_EACH_EDGE (e, ei, bb->succs)
866 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
867 break;
869 prob = e->probability;
870 index = prob * 20 / REG_BR_PROB_BASE;
872 if (index == 20)
873 index = 19;
874 hist_br_prob[index]++;
876 num_branches++;
879 /* As a last resort, distribute the probabilities evenly.
880 Use simple heuristics that if there are normal edges,
881 give all abnormals frequency of 0, otherwise distribute the
882 frequency over abnormals (this is the case of noreturn
883 calls). */
884 else if (profile_status == PROFILE_ABSENT)
886 int total = 0;
888 FOR_EACH_EDGE (e, ei, bb->succs)
889 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
890 total ++;
891 if (total)
893 FOR_EACH_EDGE (e, ei, bb->succs)
894 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
895 e->probability = REG_BR_PROB_BASE / total;
896 else
897 e->probability = 0;
899 else
901 total += EDGE_COUNT (bb->succs);
902 FOR_EACH_EDGE (e, ei, bb->succs)
903 e->probability = REG_BR_PROB_BASE / total;
905 if (bb->index >= NUM_FIXED_BLOCKS
906 && block_ends_with_condjump_p (bb)
907 && EDGE_COUNT (bb->succs) >= 2)
908 num_branches++;
911 counts_to_freqs ();
912 profile_status = PROFILE_READ;
913 compute_function_frequency ();
915 if (dump_file)
917 fprintf (dump_file, "%d branches\n", num_branches);
918 if (num_branches)
919 for (i = 0; i < 10; i++)
920 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
921 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
922 5 * i, 5 * i + 5);
924 total_num_branches += num_branches;
925 for (i = 0; i < 20; i++)
926 total_hist_br_prob[i] += hist_br_prob[i];
928 fputc ('\n', dump_file);
929 fputc ('\n', dump_file);
932 free_aux_for_blocks ();
935 /* Load value histograms values whose description is stored in VALUES array
936 from .gcda file.
938 CFG_CHECKSUM is the precomputed checksum for the CFG. */
940 static void
941 compute_value_histograms (histogram_values values, unsigned cfg_checksum,
942 unsigned lineno_checksum)
944 unsigned i, j, t, any;
945 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
946 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
947 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
948 gcov_type *aact_count;
950 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
951 n_histogram_counters[t] = 0;
953 for (i = 0; i < VEC_length (histogram_value, values); i++)
955 histogram_value hist = VEC_index (histogram_value, values, i);
956 n_histogram_counters[(int) hist->type] += hist->n_counters;
959 any = 0;
960 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
962 if (!n_histogram_counters[t])
964 histogram_counts[t] = NULL;
965 continue;
968 histogram_counts[t] =
969 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
970 n_histogram_counters[t], cfg_checksum,
971 lineno_checksum, NULL);
972 if (histogram_counts[t])
973 any = 1;
974 act_count[t] = histogram_counts[t];
976 if (!any)
977 return;
979 for (i = 0; i < VEC_length (histogram_value, values); i++)
981 histogram_value hist = VEC_index (histogram_value, values, i);
982 gimple stmt = hist->hvalue.stmt;
984 t = (int) hist->type;
986 aact_count = act_count[t];
987 act_count[t] += hist->n_counters;
989 gimple_add_histogram_value (cfun, stmt, hist);
990 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
991 for (j = 0; j < hist->n_counters; j++)
992 hist->hvalue.counters[j] = aact_count[j];
995 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
996 free (histogram_counts[t]);
999 /* When passed NULL as file_name, initialize.
1000 When passed something else, output the necessary commands to change
1001 line to LINE and offset to FILE_NAME. */
1002 static void
1003 output_location (char const *file_name, int line,
1004 gcov_position_t *offset, basic_block bb)
1006 static char const *prev_file_name;
1007 static int prev_line;
1008 bool name_differs, line_differs;
1010 if (!file_name)
1012 prev_file_name = NULL;
1013 prev_line = -1;
1014 return;
1017 name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name);
1018 line_differs = prev_line != line;
1020 if (name_differs || line_differs)
1022 if (!*offset)
1024 *offset = gcov_write_tag (GCOV_TAG_LINES);
1025 gcov_write_unsigned (bb->index);
1026 name_differs = line_differs=true;
1029 /* If this is a new source file, then output the
1030 file's name to the .bb file. */
1031 if (name_differs)
1033 prev_file_name = file_name;
1034 gcov_write_unsigned (0);
1035 gcov_write_string (prev_file_name);
1037 if (line_differs)
1039 gcov_write_unsigned (line);
1040 prev_line = line;
1045 /* Instrument and/or analyze program behavior based on program the CFG.
1047 This function creates a representation of the control flow graph (of
1048 the function being compiled) that is suitable for the instrumentation
1049 of edges and/or converting measured edge counts to counts on the
1050 complete CFG.
1052 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
1053 the flow graph that are needed to reconstruct the dynamic behavior of the
1054 flow graph. This data is written to the gcno file for gcov.
1056 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
1057 information from the gcda file containing edge count information from
1058 previous executions of the function being compiled. In this case, the
1059 control flow graph is annotated with actual execution counts by
1060 compute_branch_probabilities().
1062 Main entry point of this file. */
1064 void
1065 branch_prob (void)
1067 basic_block bb;
1068 unsigned i;
1069 unsigned num_edges, ignored_edges;
1070 unsigned num_instrumented;
1071 struct edge_list *el;
1072 histogram_values values = NULL;
1073 unsigned cfg_checksum, lineno_checksum;
1075 total_num_times_called++;
1077 flow_call_edges_add (NULL);
1078 add_noreturn_fake_exit_edges ();
1080 /* We can't handle cyclic regions constructed using abnormal edges.
1081 To avoid these we replace every source of abnormal edge by a fake
1082 edge from entry node and every destination by fake edge to exit.
1083 This keeps graph acyclic and our calculation exact for all normal
1084 edges except for exit and entrance ones.
1086 We also add fake exit edges for each call and asm statement in the
1087 basic, since it may not return. */
1089 FOR_EACH_BB (bb)
1091 int need_exit_edge = 0, need_entry_edge = 0;
1092 int have_exit_edge = 0, have_entry_edge = 0;
1093 edge e;
1094 edge_iterator ei;
1096 /* Functions returning multiple times are not handled by extra edges.
1097 Instead we simply allow negative counts on edges from exit to the
1098 block past call and corresponding probabilities. We can't go
1099 with the extra edges because that would result in flowgraph that
1100 needs to have fake edges outside the spanning tree. */
1102 FOR_EACH_EDGE (e, ei, bb->succs)
1104 gimple_stmt_iterator gsi;
1105 gimple last = NULL;
1107 /* It may happen that there are compiler generated statements
1108 without a locus at all. Go through the basic block from the
1109 last to the first statement looking for a locus. */
1110 for (gsi = gsi_last_nondebug_bb (bb);
1111 !gsi_end_p (gsi);
1112 gsi_prev_nondebug (&gsi))
1114 last = gsi_stmt (gsi);
1115 if (gimple_has_location (last))
1116 break;
1119 /* Edge with goto locus might get wrong coverage info unless
1120 it is the only edge out of BB.
1121 Don't do that when the locuses match, so
1122 if (blah) goto something;
1123 is not computed twice. */
1124 if (last
1125 && gimple_has_location (last)
1126 && e->goto_locus != UNKNOWN_LOCATION
1127 && !single_succ_p (bb)
1128 && (LOCATION_FILE (e->goto_locus)
1129 != LOCATION_FILE (gimple_location (last))
1130 || (LOCATION_LINE (e->goto_locus)
1131 != LOCATION_LINE (gimple_location (last)))))
1133 basic_block new_bb = split_edge (e);
1134 edge ne = single_succ_edge (new_bb);
1135 ne->goto_locus = e->goto_locus;
1136 ne->goto_block = e->goto_block;
1138 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1139 && e->dest != EXIT_BLOCK_PTR)
1140 need_exit_edge = 1;
1141 if (e->dest == EXIT_BLOCK_PTR)
1142 have_exit_edge = 1;
1144 FOR_EACH_EDGE (e, ei, bb->preds)
1146 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1147 && e->src != ENTRY_BLOCK_PTR)
1148 need_entry_edge = 1;
1149 if (e->src == ENTRY_BLOCK_PTR)
1150 have_entry_edge = 1;
1153 if (need_exit_edge && !have_exit_edge)
1155 if (dump_file)
1156 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
1157 bb->index);
1158 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
1160 if (need_entry_edge && !have_entry_edge)
1162 if (dump_file)
1163 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
1164 bb->index);
1165 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FAKE);
1166 /* Avoid bbs that have both fake entry edge and also some
1167 exit edge. One of those edges wouldn't be added to the
1168 spanning tree, but we can't instrument any of them. */
1169 if (have_exit_edge || need_exit_edge)
1171 gimple_stmt_iterator gsi;
1172 gimple first;
1173 tree fndecl;
1175 gsi = gsi_after_labels (bb);
1176 gcc_checking_assert (!gsi_end_p (gsi));
1177 first = gsi_stmt (gsi);
1178 if (is_gimple_debug (first))
1180 gsi_next_nondebug (&gsi);
1181 gcc_checking_assert (!gsi_end_p (gsi));
1182 first = gsi_stmt (gsi);
1184 /* Don't split the bbs containing __builtin_setjmp_receiver
1185 or __builtin_setjmp_dispatcher calls. These are very
1186 special and don't expect anything to be inserted before
1187 them. */
1188 if (!is_gimple_call (first)
1189 || (fndecl = gimple_call_fndecl (first)) == NULL
1190 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL
1191 || (DECL_FUNCTION_CODE (fndecl) != BUILT_IN_SETJMP_RECEIVER
1192 && (DECL_FUNCTION_CODE (fndecl)
1193 != BUILT_IN_SETJMP_DISPATCHER)))
1195 if (dump_file)
1196 fprintf (dump_file, "Splitting bb %i after labels\n",
1197 bb->index);
1198 split_block_after_labels (bb);
1204 el = create_edge_list ();
1205 num_edges = NUM_EDGES (el);
1206 alloc_aux_for_edges (sizeof (struct edge_info));
1208 /* The basic blocks are expected to be numbered sequentially. */
1209 compact_blocks ();
1211 ignored_edges = 0;
1212 for (i = 0 ; i < num_edges ; i++)
1214 edge e = INDEX_EDGE (el, i);
1215 e->count = 0;
1217 /* Mark edges we've replaced by fake edges above as ignored. */
1218 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1219 && e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR)
1221 EDGE_INFO (e)->ignore = 1;
1222 ignored_edges++;
1226 /* Create spanning tree from basic block graph, mark each edge that is
1227 on the spanning tree. We insert as many abnormal and critical edges
1228 as possible to minimize number of edge splits necessary. */
1230 find_spanning_tree (el);
1232 /* Fake edges that are not on the tree will not be instrumented, so
1233 mark them ignored. */
1234 for (num_instrumented = i = 0; i < num_edges; i++)
1236 edge e = INDEX_EDGE (el, i);
1237 struct edge_info *inf = EDGE_INFO (e);
1239 if (inf->ignore || inf->on_tree)
1240 /*NOP*/;
1241 else if (e->flags & EDGE_FAKE)
1243 inf->ignore = 1;
1244 ignored_edges++;
1246 else
1247 num_instrumented++;
1250 total_num_blocks += n_basic_blocks;
1251 if (dump_file)
1252 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks);
1254 total_num_edges += num_edges;
1255 if (dump_file)
1256 fprintf (dump_file, "%d edges\n", num_edges);
1258 total_num_edges_ignored += ignored_edges;
1259 if (dump_file)
1260 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
1262 total_num_edges_instrumented += num_instrumented;
1263 if (dump_file)
1264 fprintf (dump_file, "%d instrumentation edges\n", num_instrumented);
1266 /* Compute two different checksums. Note that we want to compute
1267 the checksum in only once place, since it depends on the shape
1268 of the control flow which can change during
1269 various transformations. */
1270 cfg_checksum = coverage_compute_cfg_checksum ();
1271 lineno_checksum = coverage_compute_lineno_checksum ();
1273 /* Write the data from which gcov can reconstruct the basic block
1274 graph and function line numbers (the gcno file). */
1275 if (coverage_begin_function (lineno_checksum, cfg_checksum))
1277 gcov_position_t offset;
1279 /* Basic block flags */
1280 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
1281 for (i = 0; i != (unsigned) (n_basic_blocks); i++)
1282 gcov_write_unsigned (0);
1283 gcov_write_length (offset);
1285 /* Arcs */
1286 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1288 edge e;
1289 edge_iterator ei;
1291 offset = gcov_write_tag (GCOV_TAG_ARCS);
1292 gcov_write_unsigned (bb->index);
1294 FOR_EACH_EDGE (e, ei, bb->succs)
1296 struct edge_info *i = EDGE_INFO (e);
1297 if (!i->ignore)
1299 unsigned flag_bits = 0;
1301 if (i->on_tree)
1302 flag_bits |= GCOV_ARC_ON_TREE;
1303 if (e->flags & EDGE_FAKE)
1304 flag_bits |= GCOV_ARC_FAKE;
1305 if (e->flags & EDGE_FALLTHRU)
1306 flag_bits |= GCOV_ARC_FALLTHROUGH;
1307 /* On trees we don't have fallthru flags, but we can
1308 recompute them from CFG shape. */
1309 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
1310 && e->src->next_bb == e->dest)
1311 flag_bits |= GCOV_ARC_FALLTHROUGH;
1313 gcov_write_unsigned (e->dest->index);
1314 gcov_write_unsigned (flag_bits);
1318 gcov_write_length (offset);
1321 /* Line numbers. */
1322 /* Initialize the output. */
1323 output_location (NULL, 0, NULL, NULL);
1325 FOR_EACH_BB (bb)
1327 gimple_stmt_iterator gsi;
1328 gcov_position_t offset = 0;
1330 if (bb == ENTRY_BLOCK_PTR->next_bb)
1332 expanded_location curr_location =
1333 expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1334 output_location (curr_location.file, curr_location.line,
1335 &offset, bb);
1338 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1340 gimple stmt = gsi_stmt (gsi);
1341 if (gimple_has_location (stmt))
1342 output_location (gimple_filename (stmt), gimple_lineno (stmt),
1343 &offset, bb);
1346 /* Notice GOTO expressions eliminated while constructing the CFG. */
1347 if (single_succ_p (bb)
1348 && single_succ_edge (bb)->goto_locus != UNKNOWN_LOCATION)
1350 expanded_location curr_location
1351 = expand_location (single_succ_edge (bb)->goto_locus);
1352 output_location (curr_location.file, curr_location.line,
1353 &offset, bb);
1356 if (offset)
1358 /* A file of NULL indicates the end of run. */
1359 gcov_write_unsigned (0);
1360 gcov_write_string (NULL);
1361 gcov_write_length (offset);
1366 if (flag_profile_values)
1367 gimple_find_values_to_profile (&values);
1369 if (flag_branch_probabilities)
1371 compute_branch_probabilities (cfg_checksum, lineno_checksum);
1372 if (flag_profile_values)
1373 compute_value_histograms (values, cfg_checksum, lineno_checksum);
1376 remove_fake_edges ();
1378 /* For each edge not on the spanning tree, add counting code. */
1379 if (profile_arc_flag
1380 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1382 unsigned n_instrumented;
1384 gimple_init_edge_profiler ();
1386 n_instrumented = instrument_edges (el);
1388 gcc_assert (n_instrumented == num_instrumented);
1390 if (flag_profile_values)
1391 instrument_values (values);
1393 /* Commit changes done by instrumentation. */
1394 gsi_commit_edge_inserts ();
1397 free_aux_for_edges ();
1399 VEC_free (histogram_value, heap, values);
1400 free_edge_list (el);
1401 coverage_end_function (lineno_checksum, cfg_checksum);
1404 /* Union find algorithm implementation for the basic blocks using
1405 aux fields. */
1407 static basic_block
1408 find_group (basic_block bb)
1410 basic_block group = bb, bb1;
1412 while ((basic_block) group->aux != group)
1413 group = (basic_block) group->aux;
1415 /* Compress path. */
1416 while ((basic_block) bb->aux != group)
1418 bb1 = (basic_block) bb->aux;
1419 bb->aux = (void *) group;
1420 bb = bb1;
1422 return group;
1425 static void
1426 union_groups (basic_block bb1, basic_block bb2)
1428 basic_block bb1g = find_group (bb1);
1429 basic_block bb2g = find_group (bb2);
1431 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1432 this code is unlikely going to be performance problem anyway. */
1433 gcc_assert (bb1g != bb2g);
1435 bb1g->aux = bb2g;
1438 /* This function searches all of the edges in the program flow graph, and puts
1439 as many bad edges as possible onto the spanning tree. Bad edges include
1440 abnormals edges, which can't be instrumented at the moment. Since it is
1441 possible for fake edges to form a cycle, we will have to develop some
1442 better way in the future. Also put critical edges to the tree, since they
1443 are more expensive to instrument. */
1445 static void
1446 find_spanning_tree (struct edge_list *el)
1448 int i;
1449 int num_edges = NUM_EDGES (el);
1450 basic_block bb;
1452 /* We use aux field for standard union-find algorithm. */
1453 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1454 bb->aux = bb;
1456 /* Add fake edge exit to entry we can't instrument. */
1457 union_groups (EXIT_BLOCK_PTR, ENTRY_BLOCK_PTR);
1459 /* First add all abnormal edges to the tree unless they form a cycle. Also
1460 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1461 setting return value from function. */
1462 for (i = 0; i < num_edges; i++)
1464 edge e = INDEX_EDGE (el, i);
1465 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1466 || e->dest == EXIT_BLOCK_PTR)
1467 && !EDGE_INFO (e)->ignore
1468 && (find_group (e->src) != find_group (e->dest)))
1470 if (dump_file)
1471 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1472 e->src->index, e->dest->index);
1473 EDGE_INFO (e)->on_tree = 1;
1474 union_groups (e->src, e->dest);
1478 /* Now insert all critical edges to the tree unless they form a cycle. */
1479 for (i = 0; i < num_edges; i++)
1481 edge e = INDEX_EDGE (el, i);
1482 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1483 && find_group (e->src) != find_group (e->dest))
1485 if (dump_file)
1486 fprintf (dump_file, "Critical edge %d to %d put to tree\n",
1487 e->src->index, e->dest->index);
1488 EDGE_INFO (e)->on_tree = 1;
1489 union_groups (e->src, e->dest);
1493 /* And now the rest. */
1494 for (i = 0; i < num_edges; i++)
1496 edge e = INDEX_EDGE (el, i);
1497 if (!EDGE_INFO (e)->ignore
1498 && find_group (e->src) != find_group (e->dest))
1500 if (dump_file)
1501 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1502 e->src->index, e->dest->index);
1503 EDGE_INFO (e)->on_tree = 1;
1504 union_groups (e->src, e->dest);
1508 clear_aux_for_blocks ();
1511 /* Perform file-level initialization for branch-prob processing. */
1513 void
1514 init_branch_prob (void)
1516 int i;
1518 total_num_blocks = 0;
1519 total_num_edges = 0;
1520 total_num_edges_ignored = 0;
1521 total_num_edges_instrumented = 0;
1522 total_num_blocks_created = 0;
1523 total_num_passes = 0;
1524 total_num_times_called = 0;
1525 total_num_branches = 0;
1526 for (i = 0; i < 20; i++)
1527 total_hist_br_prob[i] = 0;
1530 /* Performs file-level cleanup after branch-prob processing
1531 is completed. */
1533 void
1534 end_branch_prob (void)
1536 if (dump_file)
1538 fprintf (dump_file, "\n");
1539 fprintf (dump_file, "Total number of blocks: %d\n",
1540 total_num_blocks);
1541 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1542 fprintf (dump_file, "Total number of ignored edges: %d\n",
1543 total_num_edges_ignored);
1544 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1545 total_num_edges_instrumented);
1546 fprintf (dump_file, "Total number of blocks created: %d\n",
1547 total_num_blocks_created);
1548 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1549 total_num_passes);
1550 if (total_num_times_called != 0)
1551 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1552 (total_num_passes + (total_num_times_called >> 1))
1553 / total_num_times_called);
1554 fprintf (dump_file, "Total number of branches: %d\n",
1555 total_num_branches);
1556 if (total_num_branches)
1558 int i;
1560 for (i = 0; i < 10; i++)
1561 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1562 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1563 / total_num_branches, 5*i, 5*i+5);