i386: move alignment defaults to processor_costs.
[official-gcc.git] / gcc / profile.c
blobcb51e0d4c51feba9942528f5e3ff8ba92c8c1976
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2018 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 "backend.h"
54 #include "rtl.h"
55 #include "tree.h"
56 #include "gimple.h"
57 #include "cfghooks.h"
58 #include "cgraph.h"
59 #include "coverage.h"
60 #include "diagnostic-core.h"
61 #include "cfganal.h"
62 #include "value-prof.h"
63 #include "gimple-iterator.h"
64 #include "tree-cfg.h"
65 #include "dumpfile.h"
66 #include "cfgloop.h"
68 #include "profile.h"
70 /* Map from BBs/edges to gcov counters. */
71 vec<gcov_type> bb_gcov_counts;
72 hash_map<edge,gcov_type> *edge_gcov_counts;
74 struct bb_profile_info {
75 unsigned int count_valid : 1;
77 /* Number of successor and predecessor edges. */
78 gcov_type succ_count;
79 gcov_type pred_count;
82 #define BB_INFO(b) ((struct bb_profile_info *) (b)->aux)
85 /* Counter summary from the last set of coverage counts read. */
87 const gcov_summary *profile_info;
89 /* Counter working set information computed from the current counter
90 summary. Not initialized unless profile_info summary is non-NULL. */
91 static gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
93 /* Collect statistics on the performance of this pass for the entire source
94 file. */
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_branches;
106 /* Helper function to update gcov_working_sets. */
108 void add_working_set (gcov_working_set_t *set) {
109 int i = 0;
110 for (; i < NUM_GCOV_WORKING_SETS; i++)
111 gcov_working_sets[i] = set[i];
114 /* Forward declarations. */
115 static void find_spanning_tree (struct edge_list *);
117 /* Add edge instrumentation code to the entire insn chain.
119 F is the first insn of the chain.
120 NUM_BLOCKS is the number of basic blocks found in F. */
122 static unsigned
123 instrument_edges (struct edge_list *el)
125 unsigned num_instr_edges = 0;
126 int num_edges = NUM_EDGES (el);
127 basic_block bb;
129 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
131 edge e;
132 edge_iterator ei;
134 FOR_EACH_EDGE (e, ei, bb->succs)
136 struct edge_profile_info *inf = EDGE_INFO (e);
138 if (!inf->ignore && !inf->on_tree)
140 gcc_assert (!(e->flags & EDGE_ABNORMAL));
141 if (dump_file)
142 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
143 e->src->index, e->dest->index,
144 EDGE_CRITICAL_P (e) ? " (and split)" : "");
145 gimple_gen_edge_profiler (num_instr_edges++, e);
150 total_num_blocks_created += num_edges;
151 if (dump_file)
152 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
153 return num_instr_edges;
156 /* Add code to measure histograms for values in list VALUES. */
157 static void
158 instrument_values (histogram_values values)
160 unsigned i;
162 /* Emit code to generate the histograms before the insns. */
164 for (i = 0; i < values.length (); i++)
166 histogram_value hist = values[i];
167 unsigned t = COUNTER_FOR_HIST_TYPE (hist->type);
169 if (!coverage_counter_alloc (t, hist->n_counters))
170 continue;
172 switch (hist->type)
174 case HIST_TYPE_INTERVAL:
175 gimple_gen_interval_profiler (hist, t, 0);
176 break;
178 case HIST_TYPE_POW2:
179 gimple_gen_pow2_profiler (hist, t, 0);
180 break;
182 case HIST_TYPE_SINGLE_VALUE:
183 gimple_gen_one_value_profiler (hist, t, 0);
184 break;
186 case HIST_TYPE_INDIR_CALL:
187 case HIST_TYPE_INDIR_CALL_TOPN:
188 gimple_gen_ic_profiler (hist, t, 0);
189 break;
191 case HIST_TYPE_AVERAGE:
192 gimple_gen_average_profiler (hist, t, 0);
193 break;
195 case HIST_TYPE_IOR:
196 gimple_gen_ior_profiler (hist, t, 0);
197 break;
199 case HIST_TYPE_TIME_PROFILE:
200 gimple_gen_time_profiler (t, 0);
201 break;
203 default:
204 gcc_unreachable ();
210 /* Fill the working set information into the profile_info structure. */
212 void
213 get_working_sets (void)
215 unsigned ws_ix, pctinc, pct;
216 gcov_working_set_t *ws_info;
218 if (!profile_info)
219 return;
221 compute_working_sets (profile_info, gcov_working_sets);
223 if (dump_file)
225 fprintf (dump_file, "Counter working sets:\n");
226 /* Multiply the percentage by 100 to avoid float. */
227 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
228 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
229 ws_ix++, pct += pctinc)
231 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
232 pct = 9990;
233 ws_info = &gcov_working_sets[ws_ix];
234 /* Print out the percentage using int arithmatic to avoid float. */
235 fprintf (dump_file, "\t\t%u.%02u%%: num counts=%u, min counter="
236 "%" PRId64 "\n",
237 pct / 100, pct - (pct / 100 * 100),
238 ws_info->num_counters,
239 (int64_t)ws_info->min_counter);
244 /* Given a the desired percentage of the full profile (sum_all from the
245 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
246 the corresponding working set information. If an exact match for
247 the percentage isn't found, the closest value is used. */
249 gcov_working_set_t *
250 find_working_set (unsigned pct_times_10)
252 unsigned i;
253 if (!profile_info)
254 return NULL;
255 gcc_assert (pct_times_10 <= 1000);
256 if (pct_times_10 >= 999)
257 return &gcov_working_sets[NUM_GCOV_WORKING_SETS - 1];
258 i = pct_times_10 * NUM_GCOV_WORKING_SETS / 1000;
259 if (!i)
260 return &gcov_working_sets[0];
261 return &gcov_working_sets[i - 1];
264 /* Computes hybrid profile for all matching entries in da_file.
266 CFG_CHECKSUM is the precomputed checksum for the CFG. */
268 static gcov_type *
269 get_exec_counts (unsigned cfg_checksum, unsigned lineno_checksum)
271 unsigned num_edges = 0;
272 basic_block bb;
273 gcov_type *counts;
275 /* Count the edges to be (possibly) instrumented. */
276 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
278 edge e;
279 edge_iterator ei;
281 FOR_EACH_EDGE (e, ei, bb->succs)
282 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
283 num_edges++;
286 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, cfg_checksum,
287 lineno_checksum, &profile_info);
288 if (!counts)
289 return NULL;
291 get_working_sets ();
293 if (dump_file && profile_info)
294 fprintf (dump_file, "Merged %u profiles with maximal count %u.\n",
295 profile_info->runs, (unsigned) profile_info->sum_max);
297 return counts;
301 static bool
302 is_edge_inconsistent (vec<edge, va_gc> *edges)
304 edge e;
305 edge_iterator ei;
306 FOR_EACH_EDGE (e, ei, edges)
308 if (!EDGE_INFO (e)->ignore)
310 if (edge_gcov_count (e) < 0
311 && (!(e->flags & EDGE_FAKE)
312 || !block_ends_with_call_p (e->src)))
314 if (dump_file)
316 fprintf (dump_file,
317 "Edge %i->%i is inconsistent, count%" PRId64,
318 e->src->index, e->dest->index, edge_gcov_count (e));
319 dump_bb (dump_file, e->src, 0, TDF_DETAILS);
320 dump_bb (dump_file, e->dest, 0, TDF_DETAILS);
322 return true;
326 return false;
329 static void
330 correct_negative_edge_counts (void)
332 basic_block bb;
333 edge e;
334 edge_iterator ei;
336 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
338 FOR_EACH_EDGE (e, ei, bb->succs)
340 if (edge_gcov_count (e) < 0)
341 edge_gcov_count (e) = 0;
346 /* Check consistency.
347 Return true if inconsistency is found. */
348 static bool
349 is_inconsistent (void)
351 basic_block bb;
352 bool inconsistent = false;
353 FOR_EACH_BB_FN (bb, cfun)
355 inconsistent |= is_edge_inconsistent (bb->preds);
356 if (!dump_file && inconsistent)
357 return true;
358 inconsistent |= is_edge_inconsistent (bb->succs);
359 if (!dump_file && inconsistent)
360 return true;
361 if (bb_gcov_count (bb) < 0)
363 if (dump_file)
365 fprintf (dump_file, "BB %i count is negative "
366 "%" PRId64,
367 bb->index,
368 bb_gcov_count (bb));
369 dump_bb (dump_file, bb, 0, TDF_DETAILS);
371 inconsistent = true;
373 if (bb_gcov_count (bb) != sum_edge_counts (bb->preds))
375 if (dump_file)
377 fprintf (dump_file, "BB %i count does not match sum of incoming edges "
378 "%" PRId64" should be %" PRId64,
379 bb->index,
380 bb_gcov_count (bb),
381 sum_edge_counts (bb->preds));
382 dump_bb (dump_file, bb, 0, TDF_DETAILS);
384 inconsistent = true;
386 if (bb_gcov_count (bb) != sum_edge_counts (bb->succs) &&
387 ! (find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)) != NULL
388 && block_ends_with_call_p (bb)))
390 if (dump_file)
392 fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
393 "%" PRId64" should be %" PRId64,
394 bb->index,
395 bb_gcov_count (bb),
396 sum_edge_counts (bb->succs));
397 dump_bb (dump_file, bb, 0, TDF_DETAILS);
399 inconsistent = true;
401 if (!dump_file && inconsistent)
402 return true;
405 return inconsistent;
408 /* Set each basic block count to the sum of its outgoing edge counts */
409 static void
410 set_bb_counts (void)
412 basic_block bb;
413 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
415 bb_gcov_count (bb) = sum_edge_counts (bb->succs);
416 gcc_assert (bb_gcov_count (bb) >= 0);
420 /* Reads profile data and returns total number of edge counts read */
421 static int
422 read_profile_edge_counts (gcov_type *exec_counts)
424 basic_block bb;
425 int num_edges = 0;
426 int exec_counts_pos = 0;
427 /* For each edge not on the spanning tree, set its execution count from
428 the .da file. */
429 /* The first count in the .da file is the number of times that the function
430 was entered. This is the exec_count for block zero. */
432 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
434 edge e;
435 edge_iterator ei;
437 FOR_EACH_EDGE (e, ei, bb->succs)
438 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
440 num_edges++;
441 if (exec_counts)
443 edge_gcov_count (e) = exec_counts[exec_counts_pos++];
444 if (edge_gcov_count (e) > profile_info->sum_max)
446 if (flag_profile_correction)
448 static bool informed = 0;
449 if (dump_enabled_p () && !informed)
451 dump_location_t loc
452 = dump_location_t::from_location_t
453 (input_location);
454 dump_printf_loc (MSG_NOTE, loc,
455 "corrupted profile info: edge count"
456 " exceeds maximal count\n");
458 informed = 1;
460 else
461 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
462 bb->index, e->dest->index);
465 else
466 edge_gcov_count (e) = 0;
468 EDGE_INFO (e)->count_valid = 1;
469 BB_INFO (bb)->succ_count--;
470 BB_INFO (e->dest)->pred_count--;
471 if (dump_file)
473 fprintf (dump_file, "\nRead edge from %i to %i, count:",
474 bb->index, e->dest->index);
475 fprintf (dump_file, "%" PRId64,
476 (int64_t) edge_gcov_count (e));
481 return num_edges;
485 /* Compute the branch probabilities for the various branches.
486 Annotate them accordingly.
488 CFG_CHECKSUM is the precomputed checksum for the CFG. */
490 static void
491 compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
493 basic_block bb;
494 int i;
495 int num_edges = 0;
496 int changes;
497 int passes;
498 int hist_br_prob[20];
499 int num_branches;
500 gcov_type *exec_counts = get_exec_counts (cfg_checksum, lineno_checksum);
501 int inconsistent = 0;
503 /* Very simple sanity checks so we catch bugs in our profiling code. */
504 if (!profile_info)
506 if (dump_file)
507 fprintf (dump_file, "Profile info is missing; giving up\n");
508 return;
511 bb_gcov_counts.safe_grow_cleared (last_basic_block_for_fn (cfun));
512 edge_gcov_counts = new hash_map<edge,gcov_type>;
514 if (profile_info->sum_all < profile_info->sum_max)
516 error ("corrupted profile info: sum_all is smaller than sum_max");
517 exec_counts = NULL;
520 /* Attach extra info block to each bb. */
521 alloc_aux_for_blocks (sizeof (struct bb_profile_info));
522 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
524 edge e;
525 edge_iterator ei;
527 FOR_EACH_EDGE (e, ei, bb->succs)
528 if (!EDGE_INFO (e)->ignore)
529 BB_INFO (bb)->succ_count++;
530 FOR_EACH_EDGE (e, ei, bb->preds)
531 if (!EDGE_INFO (e)->ignore)
532 BB_INFO (bb)->pred_count++;
535 /* Avoid predicting entry on exit nodes. */
536 BB_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->succ_count = 2;
537 BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (cfun))->pred_count = 2;
539 num_edges = read_profile_edge_counts (exec_counts);
541 if (dump_file)
542 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
544 /* For every block in the file,
545 - if every exit/entrance edge has a known count, then set the block count
546 - if the block count is known, and every exit/entrance edge but one has
547 a known execution count, then set the count of the remaining edge
549 As edge counts are set, decrement the succ/pred count, but don't delete
550 the edge, that way we can easily tell when all edges are known, or only
551 one edge is unknown. */
553 /* The order that the basic blocks are iterated through is important.
554 Since the code that finds spanning trees starts with block 0, low numbered
555 edges are put on the spanning tree in preference to high numbered edges.
556 Hence, most instrumented edges are at the end. Graph solving works much
557 faster if we propagate numbers from the end to the start.
559 This takes an average of slightly more than 3 passes. */
561 changes = 1;
562 passes = 0;
563 while (changes)
565 passes++;
566 changes = 0;
567 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, prev_bb)
569 struct bb_profile_info *bi = BB_INFO (bb);
570 if (! bi->count_valid)
572 if (bi->succ_count == 0)
574 edge e;
575 edge_iterator ei;
576 gcov_type total = 0;
578 FOR_EACH_EDGE (e, ei, bb->succs)
579 total += edge_gcov_count (e);
580 bb_gcov_count (bb) = total;
581 bi->count_valid = 1;
582 changes = 1;
584 else if (bi->pred_count == 0)
586 edge e;
587 edge_iterator ei;
588 gcov_type total = 0;
590 FOR_EACH_EDGE (e, ei, bb->preds)
591 total += edge_gcov_count (e);
592 bb_gcov_count (bb) = total;
593 bi->count_valid = 1;
594 changes = 1;
597 if (bi->count_valid)
599 if (bi->succ_count == 1)
601 edge e;
602 edge_iterator ei;
603 gcov_type total = 0;
605 /* One of the counts will be invalid, but it is zero,
606 so adding it in also doesn't hurt. */
607 FOR_EACH_EDGE (e, ei, bb->succs)
608 total += edge_gcov_count (e);
610 /* Search for the invalid edge, and set its count. */
611 FOR_EACH_EDGE (e, ei, bb->succs)
612 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
613 break;
615 /* Calculate count for remaining edge by conservation. */
616 total = bb_gcov_count (bb) - total;
618 gcc_assert (e);
619 EDGE_INFO (e)->count_valid = 1;
620 edge_gcov_count (e) = total;
621 bi->succ_count--;
623 BB_INFO (e->dest)->pred_count--;
624 changes = 1;
626 if (bi->pred_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->preds)
635 total += edge_gcov_count (e);
637 /* Search for the invalid edge, and set its count. */
638 FOR_EACH_EDGE (e, ei, bb->preds)
639 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
640 break;
642 /* Calculate count for remaining edge by conservation. */
643 total = bb_gcov_count (bb) - total + edge_gcov_count (e);
645 gcc_assert (e);
646 EDGE_INFO (e)->count_valid = 1;
647 edge_gcov_count (e) = total;
648 bi->pred_count--;
650 BB_INFO (e->src)->succ_count--;
651 changes = 1;
657 total_num_passes += passes;
658 if (dump_file)
659 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
661 /* If the graph has been correctly solved, every block will have a
662 succ and pred count of zero. */
663 FOR_EACH_BB_FN (bb, cfun)
665 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
668 /* Check for inconsistent basic block counts */
669 inconsistent = is_inconsistent ();
671 if (inconsistent)
673 if (flag_profile_correction)
675 /* Inconsistency detected. Make it flow-consistent. */
676 static int informed = 0;
677 if (dump_enabled_p () && informed == 0)
679 informed = 1;
680 dump_printf_loc (MSG_NOTE,
681 dump_location_t::from_location_t (input_location),
682 "correcting inconsistent profile data\n");
684 correct_negative_edge_counts ();
685 /* Set bb counts to the sum of the outgoing edge counts */
686 set_bb_counts ();
687 if (dump_file)
688 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n");
689 mcf_smooth_cfg ();
691 else
692 error ("corrupted profile info: profile data is not flow-consistent");
695 /* For every edge, calculate its branch probability and add a reg_note
696 to the branch insn to indicate this. */
698 for (i = 0; i < 20; i++)
699 hist_br_prob[i] = 0;
700 num_branches = 0;
702 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
704 edge e;
705 edge_iterator ei;
707 if (bb_gcov_count (bb) < 0)
709 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
710 bb->index, (int)bb_gcov_count (bb));
711 bb_gcov_count (bb) = 0;
713 FOR_EACH_EDGE (e, ei, bb->succs)
715 /* Function may return twice in the cased the called function is
716 setjmp or calls fork, but we can't represent this by extra
717 edge from the entry, since extra edge from the exit is
718 already present. We get negative frequency from the entry
719 point. */
720 if ((edge_gcov_count (e) < 0
721 && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
722 || (edge_gcov_count (e) > bb_gcov_count (bb)
723 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)))
725 if (block_ends_with_call_p (bb))
726 edge_gcov_count (e) = edge_gcov_count (e) < 0
727 ? 0 : bb_gcov_count (bb);
729 if (edge_gcov_count (e) < 0
730 || edge_gcov_count (e) > bb_gcov_count (bb))
732 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
733 e->src->index, e->dest->index,
734 (int)edge_gcov_count (e));
735 edge_gcov_count (e) = bb_gcov_count (bb) / 2;
738 if (bb_gcov_count (bb))
740 FOR_EACH_EDGE (e, ei, bb->succs)
741 e->probability = profile_probability::probability_in_gcov_type
742 (edge_gcov_count (e), bb_gcov_count (bb));
743 if (bb->index >= NUM_FIXED_BLOCKS
744 && block_ends_with_condjump_p (bb)
745 && EDGE_COUNT (bb->succs) >= 2)
747 int prob;
748 edge e;
749 int index;
751 /* Find the branch edge. It is possible that we do have fake
752 edges here. */
753 FOR_EACH_EDGE (e, ei, bb->succs)
754 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
755 break;
757 prob = e->probability.to_reg_br_prob_base ();
758 index = prob * 20 / REG_BR_PROB_BASE;
760 if (index == 20)
761 index = 19;
762 hist_br_prob[index]++;
764 num_branches++;
767 /* As a last resort, distribute the probabilities evenly.
768 Use simple heuristics that if there are normal edges,
769 give all abnormals frequency of 0, otherwise distribute the
770 frequency over abnormals (this is the case of noreturn
771 calls). */
772 else if (profile_status_for_fn (cfun) == PROFILE_ABSENT)
774 int total = 0;
776 FOR_EACH_EDGE (e, ei, bb->succs)
777 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
778 total ++;
779 if (total)
781 FOR_EACH_EDGE (e, ei, bb->succs)
782 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
783 e->probability
784 = profile_probability::guessed_always ().apply_scale (1, total);
785 else
786 e->probability = profile_probability::never ();
788 else
790 total += EDGE_COUNT (bb->succs);
791 FOR_EACH_EDGE (e, ei, bb->succs)
792 e->probability
793 = profile_probability::guessed_always ().apply_scale (1, total);
795 if (bb->index >= NUM_FIXED_BLOCKS
796 && block_ends_with_condjump_p (bb)
797 && EDGE_COUNT (bb->succs) >= 2)
798 num_branches++;
802 /* If we have real data, use them! */
803 if (bb_gcov_count (ENTRY_BLOCK_PTR_FOR_FN (cfun))
804 || !flag_guess_branch_prob)
805 FOR_ALL_BB_FN (bb, cfun)
806 bb->count = profile_count::from_gcov_type (bb_gcov_count (bb));
807 /* If function was not trained, preserve local estimates including statically
808 determined zero counts. */
809 else
810 FOR_ALL_BB_FN (bb, cfun)
811 if (!(bb->count == profile_count::zero ()))
812 bb->count = bb->count.global0 ();
814 bb_gcov_counts.release ();
815 delete edge_gcov_counts;
816 edge_gcov_counts = NULL;
818 update_max_bb_count ();
820 if (dump_file)
822 fprintf (dump_file, "%d branches\n", num_branches);
823 if (num_branches)
824 for (i = 0; i < 10; i++)
825 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
826 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
827 5 * i, 5 * i + 5);
829 total_num_branches += num_branches;
830 for (i = 0; i < 20; i++)
831 total_hist_br_prob[i] += hist_br_prob[i];
833 fputc ('\n', dump_file);
834 fputc ('\n', dump_file);
837 free_aux_for_blocks ();
840 /* Load value histograms values whose description is stored in VALUES array
841 from .gcda file.
843 CFG_CHECKSUM is the precomputed checksum for the CFG. */
845 static void
846 compute_value_histograms (histogram_values values, unsigned cfg_checksum,
847 unsigned lineno_checksum)
849 unsigned i, j, t, any;
850 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
851 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
852 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
853 gcov_type *aact_count;
854 struct cgraph_node *node;
856 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
857 n_histogram_counters[t] = 0;
859 for (i = 0; i < values.length (); i++)
861 histogram_value hist = values[i];
862 n_histogram_counters[(int) hist->type] += hist->n_counters;
865 any = 0;
866 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
868 if (!n_histogram_counters[t])
870 histogram_counts[t] = NULL;
871 continue;
874 histogram_counts[t] =
875 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
876 n_histogram_counters[t], cfg_checksum,
877 lineno_checksum, NULL);
878 if (histogram_counts[t])
879 any = 1;
880 act_count[t] = histogram_counts[t];
882 if (!any)
883 return;
885 for (i = 0; i < values.length (); i++)
887 histogram_value hist = values[i];
888 gimple *stmt = hist->hvalue.stmt;
890 t = (int) hist->type;
892 aact_count = act_count[t];
894 if (act_count[t])
895 act_count[t] += hist->n_counters;
897 gimple_add_histogram_value (cfun, stmt, hist);
898 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
899 for (j = 0; j < hist->n_counters; j++)
900 if (aact_count)
901 hist->hvalue.counters[j] = aact_count[j];
902 else
903 hist->hvalue.counters[j] = 0;
905 /* Time profiler counter is not related to any statement,
906 so that we have to read the counter and set the value to
907 the corresponding call graph node. */
908 if (hist->type == HIST_TYPE_TIME_PROFILE)
910 node = cgraph_node::get (hist->fun->decl);
911 node->tp_first_run = hist->hvalue.counters[0];
913 if (dump_file)
914 fprintf (dump_file, "Read tp_first_run: %d\n", node->tp_first_run);
918 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
919 free (histogram_counts[t]);
922 /* Location triplet which records a location. */
923 struct location_triplet
925 const char *filename;
926 int lineno;
927 int bb_index;
930 /* Traits class for streamed_locations hash set below. */
932 struct location_triplet_hash : typed_noop_remove <location_triplet>
934 typedef location_triplet value_type;
935 typedef location_triplet compare_type;
937 static hashval_t
938 hash (const location_triplet &ref)
940 inchash::hash hstate (0);
941 if (ref.filename)
942 hstate.add_int (strlen (ref.filename));
943 hstate.add_int (ref.lineno);
944 hstate.add_int (ref.bb_index);
945 return hstate.end ();
948 static bool
949 equal (const location_triplet &ref1, const location_triplet &ref2)
951 return ref1.lineno == ref2.lineno
952 && ref1.bb_index == ref2.bb_index
953 && ref1.filename != NULL
954 && ref2.filename != NULL
955 && strcmp (ref1.filename, ref2.filename) == 0;
958 static void
959 mark_deleted (location_triplet &ref)
961 ref.lineno = -1;
964 static void
965 mark_empty (location_triplet &ref)
967 ref.lineno = -2;
970 static bool
971 is_deleted (const location_triplet &ref)
973 return ref.lineno == -1;
976 static bool
977 is_empty (const location_triplet &ref)
979 return ref.lineno == -2;
986 /* When passed NULL as file_name, initialize.
987 When passed something else, output the necessary commands to change
988 line to LINE and offset to FILE_NAME. */
989 static void
990 output_location (hash_set<location_triplet_hash> *streamed_locations,
991 char const *file_name, int line,
992 gcov_position_t *offset, basic_block bb)
994 static char const *prev_file_name;
995 static int prev_line;
996 bool name_differs, line_differs;
998 location_triplet triplet;
999 triplet.filename = file_name;
1000 triplet.lineno = line;
1001 triplet.bb_index = bb ? bb->index : 0;
1003 if (streamed_locations->add (triplet))
1004 return;
1006 if (!file_name)
1008 prev_file_name = NULL;
1009 prev_line = -1;
1010 return;
1013 name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name);
1014 line_differs = prev_line != line;
1016 if (!*offset)
1018 *offset = gcov_write_tag (GCOV_TAG_LINES);
1019 gcov_write_unsigned (bb->index);
1020 name_differs = line_differs = true;
1023 /* If this is a new source file, then output the
1024 file's name to the .bb file. */
1025 if (name_differs)
1027 prev_file_name = file_name;
1028 gcov_write_unsigned (0);
1029 gcov_write_filename (prev_file_name);
1031 if (line_differs)
1033 gcov_write_unsigned (line);
1034 prev_line = line;
1038 /* Helper for qsort so edges get sorted from highest frequency to smallest.
1039 This controls the weight for minimal spanning tree algorithm */
1040 static int
1041 compare_freqs (const void *p1, const void *p2)
1043 const_edge e1 = *(const const_edge *)p1;
1044 const_edge e2 = *(const const_edge *)p2;
1046 /* Critical edges needs to be split which introduce extra control flow.
1047 Make them more heavy. */
1048 int m1 = EDGE_CRITICAL_P (e1) ? 2 : 1;
1049 int m2 = EDGE_CRITICAL_P (e2) ? 2 : 1;
1051 if (EDGE_FREQUENCY (e1) * m1 + m1 != EDGE_FREQUENCY (e2) * m2 + m2)
1052 return EDGE_FREQUENCY (e2) * m2 + m2 - EDGE_FREQUENCY (e1) * m1 - m1;
1053 /* Stabilize sort. */
1054 if (e1->src->index != e2->src->index)
1055 return e2->src->index - e1->src->index;
1056 return e2->dest->index - e1->dest->index;
1059 /* Instrument and/or analyze program behavior based on program the CFG.
1061 This function creates a representation of the control flow graph (of
1062 the function being compiled) that is suitable for the instrumentation
1063 of edges and/or converting measured edge counts to counts on the
1064 complete CFG.
1066 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
1067 the flow graph that are needed to reconstruct the dynamic behavior of the
1068 flow graph. This data is written to the gcno file for gcov.
1070 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
1071 information from the gcda file containing edge count information from
1072 previous executions of the function being compiled. In this case, the
1073 control flow graph is annotated with actual execution counts by
1074 compute_branch_probabilities().
1076 Main entry point of this file. */
1078 void
1079 branch_prob (void)
1081 basic_block bb;
1082 unsigned i;
1083 unsigned num_edges, ignored_edges;
1084 unsigned num_instrumented;
1085 struct edge_list *el;
1086 histogram_values values = histogram_values ();
1087 unsigned cfg_checksum, lineno_checksum;
1089 total_num_times_called++;
1091 flow_call_edges_add (NULL);
1092 add_noreturn_fake_exit_edges ();
1094 hash_set <location_triplet_hash> streamed_locations;
1096 /* We can't handle cyclic regions constructed using abnormal edges.
1097 To avoid these we replace every source of abnormal edge by a fake
1098 edge from entry node and every destination by fake edge to exit.
1099 This keeps graph acyclic and our calculation exact for all normal
1100 edges except for exit and entrance ones.
1102 We also add fake exit edges for each call and asm statement in the
1103 basic, since it may not return. */
1105 FOR_EACH_BB_FN (bb, cfun)
1107 int need_exit_edge = 0, need_entry_edge = 0;
1108 int have_exit_edge = 0, have_entry_edge = 0;
1109 edge e;
1110 edge_iterator ei;
1112 /* Functions returning multiple times are not handled by extra edges.
1113 Instead we simply allow negative counts on edges from exit to the
1114 block past call and corresponding probabilities. We can't go
1115 with the extra edges because that would result in flowgraph that
1116 needs to have fake edges outside the spanning tree. */
1118 FOR_EACH_EDGE (e, ei, bb->succs)
1120 gimple_stmt_iterator gsi;
1121 gimple *last = NULL;
1123 /* It may happen that there are compiler generated statements
1124 without a locus at all. Go through the basic block from the
1125 last to the first statement looking for a locus. */
1126 for (gsi = gsi_last_nondebug_bb (bb);
1127 !gsi_end_p (gsi);
1128 gsi_prev_nondebug (&gsi))
1130 last = gsi_stmt (gsi);
1131 if (!RESERVED_LOCATION_P (gimple_location (last)))
1132 break;
1135 /* Edge with goto locus might get wrong coverage info unless
1136 it is the only edge out of BB.
1137 Don't do that when the locuses match, so
1138 if (blah) goto something;
1139 is not computed twice. */
1140 if (last
1141 && gimple_has_location (last)
1142 && !RESERVED_LOCATION_P (e->goto_locus)
1143 && !single_succ_p (bb)
1144 && (LOCATION_FILE (e->goto_locus)
1145 != LOCATION_FILE (gimple_location (last))
1146 || (LOCATION_LINE (e->goto_locus)
1147 != LOCATION_LINE (gimple_location (last)))))
1149 basic_block new_bb = split_edge (e);
1150 edge ne = single_succ_edge (new_bb);
1151 ne->goto_locus = e->goto_locus;
1153 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1154 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1155 need_exit_edge = 1;
1156 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1157 have_exit_edge = 1;
1159 FOR_EACH_EDGE (e, ei, bb->preds)
1161 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1162 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1163 need_entry_edge = 1;
1164 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1165 have_entry_edge = 1;
1168 if (need_exit_edge && !have_exit_edge)
1170 if (dump_file)
1171 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
1172 bb->index);
1173 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
1175 if (need_entry_edge && !have_entry_edge)
1177 if (dump_file)
1178 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
1179 bb->index);
1180 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FAKE);
1181 /* Avoid bbs that have both fake entry edge and also some
1182 exit edge. One of those edges wouldn't be added to the
1183 spanning tree, but we can't instrument any of them. */
1184 if (have_exit_edge || need_exit_edge)
1186 gimple_stmt_iterator gsi;
1187 gimple *first;
1189 gsi = gsi_start_nondebug_after_labels_bb (bb);
1190 gcc_checking_assert (!gsi_end_p (gsi));
1191 first = gsi_stmt (gsi);
1192 /* Don't split the bbs containing __builtin_setjmp_receiver
1193 or ABNORMAL_DISPATCHER calls. These are very
1194 special and don't expect anything to be inserted before
1195 them. */
1196 if (is_gimple_call (first)
1197 && (gimple_call_builtin_p (first, BUILT_IN_SETJMP_RECEIVER)
1198 || (gimple_call_flags (first) & ECF_RETURNS_TWICE)
1199 || (gimple_call_internal_p (first)
1200 && (gimple_call_internal_fn (first)
1201 == IFN_ABNORMAL_DISPATCHER))))
1202 continue;
1204 if (dump_file)
1205 fprintf (dump_file, "Splitting bb %i after labels\n",
1206 bb->index);
1207 split_block_after_labels (bb);
1212 el = create_edge_list ();
1213 num_edges = NUM_EDGES (el);
1214 qsort (el->index_to_edge, num_edges, sizeof (edge), compare_freqs);
1215 alloc_aux_for_edges (sizeof (struct edge_profile_info));
1217 /* The basic blocks are expected to be numbered sequentially. */
1218 compact_blocks ();
1220 ignored_edges = 0;
1221 for (i = 0 ; i < num_edges ; i++)
1223 edge e = INDEX_EDGE (el, i);
1225 /* Mark edges we've replaced by fake edges above as ignored. */
1226 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1227 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1228 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1230 EDGE_INFO (e)->ignore = 1;
1231 ignored_edges++;
1235 /* Create spanning tree from basic block graph, mark each edge that is
1236 on the spanning tree. We insert as many abnormal and critical edges
1237 as possible to minimize number of edge splits necessary. */
1239 find_spanning_tree (el);
1241 /* Fake edges that are not on the tree will not be instrumented, so
1242 mark them ignored. */
1243 for (num_instrumented = i = 0; i < num_edges; i++)
1245 edge e = INDEX_EDGE (el, i);
1246 struct edge_profile_info *inf = EDGE_INFO (e);
1248 if (inf->ignore || inf->on_tree)
1249 /*NOP*/;
1250 else if (e->flags & EDGE_FAKE)
1252 inf->ignore = 1;
1253 ignored_edges++;
1255 else
1256 num_instrumented++;
1259 total_num_blocks += n_basic_blocks_for_fn (cfun);
1260 if (dump_file)
1261 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks_for_fn (cfun));
1263 total_num_edges += num_edges;
1264 if (dump_file)
1265 fprintf (dump_file, "%d edges\n", num_edges);
1267 total_num_edges_ignored += ignored_edges;
1268 if (dump_file)
1269 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
1271 total_num_edges_instrumented += num_instrumented;
1272 if (dump_file)
1273 fprintf (dump_file, "%d instrumentation edges\n", num_instrumented);
1275 /* Compute two different checksums. Note that we want to compute
1276 the checksum in only once place, since it depends on the shape
1277 of the control flow which can change during
1278 various transformations. */
1279 cfg_checksum = coverage_compute_cfg_checksum (cfun);
1280 lineno_checksum = coverage_compute_lineno_checksum ();
1282 /* Write the data from which gcov can reconstruct the basic block
1283 graph and function line numbers (the gcno file). */
1284 if (coverage_begin_function (lineno_checksum, cfg_checksum))
1286 gcov_position_t offset;
1288 /* Basic block flags */
1289 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
1290 gcov_write_unsigned (n_basic_blocks_for_fn (cfun));
1291 gcov_write_length (offset);
1293 /* Arcs */
1294 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
1295 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1297 edge e;
1298 edge_iterator ei;
1300 offset = gcov_write_tag (GCOV_TAG_ARCS);
1301 gcov_write_unsigned (bb->index);
1303 FOR_EACH_EDGE (e, ei, bb->succs)
1305 struct edge_profile_info *i = EDGE_INFO (e);
1306 if (!i->ignore)
1308 unsigned flag_bits = 0;
1310 if (i->on_tree)
1311 flag_bits |= GCOV_ARC_ON_TREE;
1312 if (e->flags & EDGE_FAKE)
1313 flag_bits |= GCOV_ARC_FAKE;
1314 if (e->flags & EDGE_FALLTHRU)
1315 flag_bits |= GCOV_ARC_FALLTHROUGH;
1316 /* On trees we don't have fallthru flags, but we can
1317 recompute them from CFG shape. */
1318 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
1319 && e->src->next_bb == e->dest)
1320 flag_bits |= GCOV_ARC_FALLTHROUGH;
1322 gcov_write_unsigned (e->dest->index);
1323 gcov_write_unsigned (flag_bits);
1327 gcov_write_length (offset);
1330 /* Line numbers. */
1331 /* Initialize the output. */
1332 output_location (&streamed_locations, NULL, 0, NULL, NULL);
1334 hash_set<int_hash <location_t, 0, 2> > seen_locations;
1336 FOR_EACH_BB_FN (bb, cfun)
1338 gimple_stmt_iterator gsi;
1339 gcov_position_t offset = 0;
1341 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
1343 location_t loc = DECL_SOURCE_LOCATION (current_function_decl);
1344 seen_locations.add (loc);
1345 expanded_location curr_location = expand_location (loc);
1346 output_location (&streamed_locations, curr_location.file,
1347 curr_location.line, &offset, bb);
1350 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1352 gimple *stmt = gsi_stmt (gsi);
1353 location_t loc = gimple_location (stmt);
1354 if (!RESERVED_LOCATION_P (loc))
1356 seen_locations.add (loc);
1357 output_location (&streamed_locations, gimple_filename (stmt),
1358 gimple_lineno (stmt), &offset, bb);
1362 /* Notice GOTO expressions eliminated while constructing the CFG.
1363 It's hard to distinguish such expression, but goto_locus should
1364 not be any of already seen location. */
1365 location_t loc;
1366 if (single_succ_p (bb)
1367 && (loc = single_succ_edge (bb)->goto_locus)
1368 && !RESERVED_LOCATION_P (loc)
1369 && !seen_locations.contains (loc))
1371 expanded_location curr_location = expand_location (loc);
1372 output_location (&streamed_locations, curr_location.file,
1373 curr_location.line, &offset, bb);
1376 if (offset)
1378 /* A file of NULL indicates the end of run. */
1379 gcov_write_unsigned (0);
1380 gcov_write_string (NULL);
1381 gcov_write_length (offset);
1386 if (flag_profile_values)
1387 gimple_find_values_to_profile (&values);
1389 if (flag_branch_probabilities)
1391 compute_branch_probabilities (cfg_checksum, lineno_checksum);
1392 if (flag_profile_values)
1393 compute_value_histograms (values, cfg_checksum, lineno_checksum);
1396 remove_fake_edges ();
1398 /* For each edge not on the spanning tree, add counting code. */
1399 if (profile_arc_flag
1400 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1402 unsigned n_instrumented;
1404 gimple_init_gcov_profiler ();
1406 n_instrumented = instrument_edges (el);
1408 gcc_assert (n_instrumented == num_instrumented);
1410 if (flag_profile_values)
1411 instrument_values (values);
1413 /* Commit changes done by instrumentation. */
1414 gsi_commit_edge_inserts ();
1417 free_aux_for_edges ();
1419 values.release ();
1420 free_edge_list (el);
1421 coverage_end_function (lineno_checksum, cfg_checksum);
1422 if (flag_branch_probabilities && profile_info)
1424 struct loop *loop;
1425 if (dump_file && (dump_flags & TDF_DETAILS))
1426 report_predictor_hitrates ();
1427 profile_status_for_fn (cfun) = PROFILE_READ;
1429 /* At this moment we have precise loop iteration count estimates.
1430 Record them to loop structure before the profile gets out of date. */
1431 FOR_EACH_LOOP (loop, 0)
1432 if (loop->header->count > 0)
1434 gcov_type nit = expected_loop_iterations_unbounded (loop);
1435 widest_int bound = gcov_type_to_wide_int (nit);
1436 loop->any_estimate = false;
1437 record_niter_bound (loop, bound, true, false);
1439 compute_function_frequency ();
1443 /* Union find algorithm implementation for the basic blocks using
1444 aux fields. */
1446 static basic_block
1447 find_group (basic_block bb)
1449 basic_block group = bb, bb1;
1451 while ((basic_block) group->aux != group)
1452 group = (basic_block) group->aux;
1454 /* Compress path. */
1455 while ((basic_block) bb->aux != group)
1457 bb1 = (basic_block) bb->aux;
1458 bb->aux = (void *) group;
1459 bb = bb1;
1461 return group;
1464 static void
1465 union_groups (basic_block bb1, basic_block bb2)
1467 basic_block bb1g = find_group (bb1);
1468 basic_block bb2g = find_group (bb2);
1470 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1471 this code is unlikely going to be performance problem anyway. */
1472 gcc_assert (bb1g != bb2g);
1474 bb1g->aux = bb2g;
1477 /* This function searches all of the edges in the program flow graph, and puts
1478 as many bad edges as possible onto the spanning tree. Bad edges include
1479 abnormals edges, which can't be instrumented at the moment. Since it is
1480 possible for fake edges to form a cycle, we will have to develop some
1481 better way in the future. Also put critical edges to the tree, since they
1482 are more expensive to instrument. */
1484 static void
1485 find_spanning_tree (struct edge_list *el)
1487 int i;
1488 int num_edges = NUM_EDGES (el);
1489 basic_block bb;
1491 /* We use aux field for standard union-find algorithm. */
1492 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
1493 bb->aux = bb;
1495 /* Add fake edge exit to entry we can't instrument. */
1496 union_groups (EXIT_BLOCK_PTR_FOR_FN (cfun), ENTRY_BLOCK_PTR_FOR_FN (cfun));
1498 /* First add all abnormal edges to the tree unless they form a cycle. Also
1499 add all edges to the exit block to avoid inserting profiling code behind
1500 setting return value from function. */
1501 for (i = 0; i < num_edges; i++)
1503 edge e = INDEX_EDGE (el, i);
1504 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1505 || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1506 && !EDGE_INFO (e)->ignore
1507 && (find_group (e->src) != find_group (e->dest)))
1509 if (dump_file)
1510 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1511 e->src->index, e->dest->index);
1512 EDGE_INFO (e)->on_tree = 1;
1513 union_groups (e->src, e->dest);
1517 /* And now the rest. Edge list is sorted according to frequencies and
1518 thus we will produce minimal spanning tree. */
1519 for (i = 0; i < num_edges; i++)
1521 edge e = INDEX_EDGE (el, i);
1522 if (!EDGE_INFO (e)->ignore
1523 && find_group (e->src) != find_group (e->dest))
1525 if (dump_file)
1526 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1527 e->src->index, e->dest->index);
1528 EDGE_INFO (e)->on_tree = 1;
1529 union_groups (e->src, e->dest);
1533 clear_aux_for_blocks ();
1536 /* Perform file-level initialization for branch-prob processing. */
1538 void
1539 init_branch_prob (void)
1541 int i;
1543 total_num_blocks = 0;
1544 total_num_edges = 0;
1545 total_num_edges_ignored = 0;
1546 total_num_edges_instrumented = 0;
1547 total_num_blocks_created = 0;
1548 total_num_passes = 0;
1549 total_num_times_called = 0;
1550 total_num_branches = 0;
1551 for (i = 0; i < 20; i++)
1552 total_hist_br_prob[i] = 0;
1555 /* Performs file-level cleanup after branch-prob processing
1556 is completed. */
1558 void
1559 end_branch_prob (void)
1561 if (dump_file)
1563 fprintf (dump_file, "\n");
1564 fprintf (dump_file, "Total number of blocks: %d\n",
1565 total_num_blocks);
1566 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1567 fprintf (dump_file, "Total number of ignored edges: %d\n",
1568 total_num_edges_ignored);
1569 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1570 total_num_edges_instrumented);
1571 fprintf (dump_file, "Total number of blocks created: %d\n",
1572 total_num_blocks_created);
1573 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1574 total_num_passes);
1575 if (total_num_times_called != 0)
1576 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1577 (total_num_passes + (total_num_times_called >> 1))
1578 / total_num_times_called);
1579 fprintf (dump_file, "Total number of branches: %d\n",
1580 total_num_branches);
1581 if (total_num_branches)
1583 int i;
1585 for (i = 0; i < 10; i++)
1586 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1587 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1588 / total_num_branches, 5*i, 5*i+5);