Fix typo in t-dimode
[official-gcc.git] / gcc / profile.c
blobd07002d265e68f38ea53a0ca4707a23565a8d980
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2021 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 gcov_summary *profile_info;
89 /* Collect statistics on the performance of this pass for the entire source
90 file. */
92 static int total_num_blocks;
93 static int total_num_edges;
94 static int total_num_edges_ignored;
95 static int total_num_edges_instrumented;
96 static int total_num_blocks_created;
97 static int total_num_passes;
98 static int total_num_times_called;
99 static int total_hist_br_prob[20];
100 static int total_num_branches;
102 /* Forward declarations. */
103 static void find_spanning_tree (struct edge_list *);
105 /* Add edge instrumentation code to the entire insn chain.
107 F is the first insn of the chain.
108 NUM_BLOCKS is the number of basic blocks found in F. */
110 static unsigned
111 instrument_edges (struct edge_list *el)
113 unsigned num_instr_edges = 0;
114 int num_edges = NUM_EDGES (el);
115 basic_block bb;
117 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
119 edge e;
120 edge_iterator ei;
122 FOR_EACH_EDGE (e, ei, bb->succs)
124 struct edge_profile_info *inf = EDGE_INFO (e);
126 if (!inf->ignore && !inf->on_tree)
128 gcc_assert (!(e->flags & EDGE_ABNORMAL));
129 if (dump_file)
130 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
131 e->src->index, e->dest->index,
132 EDGE_CRITICAL_P (e) ? " (and split)" : "");
133 gimple_gen_edge_profiler (num_instr_edges++, e);
138 total_num_blocks_created += num_edges;
139 if (dump_file)
140 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
141 return num_instr_edges;
144 /* Add code to measure histograms for values in list VALUES. */
145 static void
146 instrument_values (histogram_values values)
148 unsigned i;
150 /* Emit code to generate the histograms before the insns. */
152 for (i = 0; i < values.length (); i++)
154 histogram_value hist = values[i];
155 unsigned t = COUNTER_FOR_HIST_TYPE (hist->type);
157 if (!coverage_counter_alloc (t, hist->n_counters))
158 continue;
160 switch (hist->type)
162 case HIST_TYPE_INTERVAL:
163 gimple_gen_interval_profiler (hist, t);
164 break;
166 case HIST_TYPE_POW2:
167 gimple_gen_pow2_profiler (hist, t);
168 break;
170 case HIST_TYPE_TOPN_VALUES:
171 gimple_gen_topn_values_profiler (hist, t);
172 break;
174 case HIST_TYPE_INDIR_CALL:
175 gimple_gen_ic_profiler (hist, t);
176 break;
178 case HIST_TYPE_AVERAGE:
179 gimple_gen_average_profiler (hist, t);
180 break;
182 case HIST_TYPE_IOR:
183 gimple_gen_ior_profiler (hist, t);
184 break;
186 case HIST_TYPE_TIME_PROFILE:
187 gimple_gen_time_profiler (t);
188 break;
190 default:
191 gcc_unreachable ();
197 /* Computes hybrid profile for all matching entries in da_file.
199 CFG_CHECKSUM is the precomputed checksum for the CFG. */
201 static gcov_type *
202 get_exec_counts (unsigned cfg_checksum, unsigned lineno_checksum)
204 unsigned num_edges = 0;
205 basic_block bb;
206 gcov_type *counts;
208 /* Count the edges to be (possibly) instrumented. */
209 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
211 edge e;
212 edge_iterator ei;
214 FOR_EACH_EDGE (e, ei, bb->succs)
215 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
216 num_edges++;
219 counts = get_coverage_counts (GCOV_COUNTER_ARCS, cfg_checksum,
220 lineno_checksum, num_edges);
221 if (!counts)
222 return NULL;
224 return counts;
227 static bool
228 is_edge_inconsistent (vec<edge, va_gc> *edges)
230 edge e;
231 edge_iterator ei;
232 FOR_EACH_EDGE (e, ei, edges)
234 if (!EDGE_INFO (e)->ignore)
236 if (edge_gcov_count (e) < 0
237 && (!(e->flags & EDGE_FAKE)
238 || !block_ends_with_call_p (e->src)))
240 if (dump_file)
242 fprintf (dump_file,
243 "Edge %i->%i is inconsistent, count%" PRId64,
244 e->src->index, e->dest->index, edge_gcov_count (e));
245 dump_bb (dump_file, e->src, 0, TDF_DETAILS);
246 dump_bb (dump_file, e->dest, 0, TDF_DETAILS);
248 return true;
252 return false;
255 static void
256 correct_negative_edge_counts (void)
258 basic_block bb;
259 edge e;
260 edge_iterator ei;
262 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
264 FOR_EACH_EDGE (e, ei, bb->succs)
266 if (edge_gcov_count (e) < 0)
267 edge_gcov_count (e) = 0;
272 /* Check consistency.
273 Return true if inconsistency is found. */
274 static bool
275 is_inconsistent (void)
277 basic_block bb;
278 bool inconsistent = false;
279 FOR_EACH_BB_FN (bb, cfun)
281 inconsistent |= is_edge_inconsistent (bb->preds);
282 if (!dump_file && inconsistent)
283 return true;
284 inconsistent |= is_edge_inconsistent (bb->succs);
285 if (!dump_file && inconsistent)
286 return true;
287 if (bb_gcov_count (bb) < 0)
289 if (dump_file)
291 fprintf (dump_file, "BB %i count is negative "
292 "%" PRId64,
293 bb->index,
294 bb_gcov_count (bb));
295 dump_bb (dump_file, bb, 0, TDF_DETAILS);
297 inconsistent = true;
299 if (bb_gcov_count (bb) != sum_edge_counts (bb->preds))
301 if (dump_file)
303 fprintf (dump_file, "BB %i count does not match sum of incoming edges "
304 "%" PRId64" should be %" PRId64,
305 bb->index,
306 bb_gcov_count (bb),
307 sum_edge_counts (bb->preds));
308 dump_bb (dump_file, bb, 0, TDF_DETAILS);
310 inconsistent = true;
312 if (bb_gcov_count (bb) != sum_edge_counts (bb->succs) &&
313 ! (find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)) != NULL
314 && block_ends_with_call_p (bb)))
316 if (dump_file)
318 fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
319 "%" PRId64" should be %" PRId64,
320 bb->index,
321 bb_gcov_count (bb),
322 sum_edge_counts (bb->succs));
323 dump_bb (dump_file, bb, 0, TDF_DETAILS);
325 inconsistent = true;
327 if (!dump_file && inconsistent)
328 return true;
331 return inconsistent;
334 /* Set each basic block count to the sum of its outgoing edge counts */
335 static void
336 set_bb_counts (void)
338 basic_block bb;
339 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
341 bb_gcov_count (bb) = sum_edge_counts (bb->succs);
342 gcc_assert (bb_gcov_count (bb) >= 0);
346 /* Reads profile data and returns total number of edge counts read */
347 static int
348 read_profile_edge_counts (gcov_type *exec_counts)
350 basic_block bb;
351 int num_edges = 0;
352 int exec_counts_pos = 0;
353 /* For each edge not on the spanning tree, set its execution count from
354 the .da file. */
355 /* The first count in the .da file is the number of times that the function
356 was entered. This is the exec_count for block zero. */
358 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
360 edge e;
361 edge_iterator ei;
363 FOR_EACH_EDGE (e, ei, bb->succs)
364 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
366 num_edges++;
367 if (exec_counts)
368 edge_gcov_count (e) = exec_counts[exec_counts_pos++];
369 else
370 edge_gcov_count (e) = 0;
372 EDGE_INFO (e)->count_valid = 1;
373 BB_INFO (bb)->succ_count--;
374 BB_INFO (e->dest)->pred_count--;
375 if (dump_file)
377 fprintf (dump_file, "\nRead edge from %i to %i, count:",
378 bb->index, e->dest->index);
379 fprintf (dump_file, "%" PRId64,
380 (int64_t) edge_gcov_count (e));
385 return num_edges;
389 /* Compute the branch probabilities for the various branches.
390 Annotate them accordingly.
392 CFG_CHECKSUM is the precomputed checksum for the CFG. */
394 static void
395 compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
397 basic_block bb;
398 int i;
399 int num_edges = 0;
400 int changes;
401 int passes;
402 int hist_br_prob[20];
403 int num_branches;
404 gcov_type *exec_counts = get_exec_counts (cfg_checksum, lineno_checksum);
405 int inconsistent = 0;
407 /* Very simple sanity checks so we catch bugs in our profiling code. */
408 if (!profile_info)
410 if (dump_file)
411 fprintf (dump_file, "Profile info is missing; giving up\n");
412 return;
415 bb_gcov_counts.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
416 edge_gcov_counts = new hash_map<edge,gcov_type>;
418 /* Attach extra info block to each bb. */
419 alloc_aux_for_blocks (sizeof (struct bb_profile_info));
420 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
422 edge e;
423 edge_iterator ei;
425 FOR_EACH_EDGE (e, ei, bb->succs)
426 if (!EDGE_INFO (e)->ignore)
427 BB_INFO (bb)->succ_count++;
428 FOR_EACH_EDGE (e, ei, bb->preds)
429 if (!EDGE_INFO (e)->ignore)
430 BB_INFO (bb)->pred_count++;
433 /* Avoid predicting entry on exit nodes. */
434 BB_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->succ_count = 2;
435 BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (cfun))->pred_count = 2;
437 num_edges = read_profile_edge_counts (exec_counts);
439 if (dump_file)
440 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
442 /* For every block in the file,
443 - if every exit/entrance edge has a known count, then set the block count
444 - if the block count is known, and every exit/entrance edge but one has
445 a known execution count, then set the count of the remaining edge
447 As edge counts are set, decrement the succ/pred count, but don't delete
448 the edge, that way we can easily tell when all edges are known, or only
449 one edge is unknown. */
451 /* The order that the basic blocks are iterated through is important.
452 Since the code that finds spanning trees starts with block 0, low numbered
453 edges are put on the spanning tree in preference to high numbered edges.
454 Hence, most instrumented edges are at the end. Graph solving works much
455 faster if we propagate numbers from the end to the start.
457 This takes an average of slightly more than 3 passes. */
459 changes = 1;
460 passes = 0;
461 while (changes)
463 passes++;
464 changes = 0;
465 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, prev_bb)
467 struct bb_profile_info *bi = BB_INFO (bb);
468 if (! bi->count_valid)
470 if (bi->succ_count == 0)
472 edge e;
473 edge_iterator ei;
474 gcov_type total = 0;
476 FOR_EACH_EDGE (e, ei, bb->succs)
477 total += edge_gcov_count (e);
478 bb_gcov_count (bb) = total;
479 bi->count_valid = 1;
480 changes = 1;
482 else if (bi->pred_count == 0)
484 edge e;
485 edge_iterator ei;
486 gcov_type total = 0;
488 FOR_EACH_EDGE (e, ei, bb->preds)
489 total += edge_gcov_count (e);
490 bb_gcov_count (bb) = total;
491 bi->count_valid = 1;
492 changes = 1;
495 if (bi->count_valid)
497 if (bi->succ_count == 1)
499 edge e;
500 edge_iterator ei;
501 gcov_type total = 0;
503 /* One of the counts will be invalid, but it is zero,
504 so adding it in also doesn't hurt. */
505 FOR_EACH_EDGE (e, ei, bb->succs)
506 total += edge_gcov_count (e);
508 /* Search for the invalid edge, and set its count. */
509 FOR_EACH_EDGE (e, ei, bb->succs)
510 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
511 break;
513 /* Calculate count for remaining edge by conservation. */
514 total = bb_gcov_count (bb) - total;
516 gcc_assert (e);
517 EDGE_INFO (e)->count_valid = 1;
518 edge_gcov_count (e) = total;
519 bi->succ_count--;
521 BB_INFO (e->dest)->pred_count--;
522 changes = 1;
524 if (bi->pred_count == 1)
526 edge e;
527 edge_iterator ei;
528 gcov_type total = 0;
530 /* One of the counts will be invalid, but it is zero,
531 so adding it in also doesn't hurt. */
532 FOR_EACH_EDGE (e, ei, bb->preds)
533 total += edge_gcov_count (e);
535 /* Search for the invalid edge, and set its count. */
536 FOR_EACH_EDGE (e, ei, bb->preds)
537 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
538 break;
540 /* Calculate count for remaining edge by conservation. */
541 total = bb_gcov_count (bb) - total + edge_gcov_count (e);
543 gcc_assert (e);
544 EDGE_INFO (e)->count_valid = 1;
545 edge_gcov_count (e) = total;
546 bi->pred_count--;
548 BB_INFO (e->src)->succ_count--;
549 changes = 1;
555 total_num_passes += passes;
556 if (dump_file)
557 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
559 /* If the graph has been correctly solved, every block will have a
560 succ and pred count of zero. */
561 FOR_EACH_BB_FN (bb, cfun)
563 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
566 /* Check for inconsistent basic block counts */
567 inconsistent = is_inconsistent ();
569 if (inconsistent)
571 if (flag_profile_correction)
573 /* Inconsistency detected. Make it flow-consistent. */
574 static int informed = 0;
575 if (dump_enabled_p () && informed == 0)
577 informed = 1;
578 dump_printf_loc (MSG_NOTE,
579 dump_user_location_t::from_location_t (input_location),
580 "correcting inconsistent profile data\n");
582 correct_negative_edge_counts ();
583 /* Set bb counts to the sum of the outgoing edge counts */
584 set_bb_counts ();
585 if (dump_file)
586 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n");
587 mcf_smooth_cfg ();
589 else
590 error ("corrupted profile info: profile data is not flow-consistent");
593 /* For every edge, calculate its branch probability and add a reg_note
594 to the branch insn to indicate this. */
596 for (i = 0; i < 20; i++)
597 hist_br_prob[i] = 0;
598 num_branches = 0;
600 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
602 edge e;
603 edge_iterator ei;
605 if (bb_gcov_count (bb) < 0)
607 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
608 bb->index, (int)bb_gcov_count (bb));
609 bb_gcov_count (bb) = 0;
611 FOR_EACH_EDGE (e, ei, bb->succs)
613 /* Function may return twice in the cased the called function is
614 setjmp or calls fork, but we can't represent this by extra
615 edge from the entry, since extra edge from the exit is
616 already present. We get negative frequency from the entry
617 point. */
618 if ((edge_gcov_count (e) < 0
619 && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
620 || (edge_gcov_count (e) > bb_gcov_count (bb)
621 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)))
623 if (block_ends_with_call_p (bb))
624 edge_gcov_count (e) = edge_gcov_count (e) < 0
625 ? 0 : bb_gcov_count (bb);
627 if (edge_gcov_count (e) < 0
628 || edge_gcov_count (e) > bb_gcov_count (bb))
630 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
631 e->src->index, e->dest->index,
632 (int)edge_gcov_count (e));
633 edge_gcov_count (e) = bb_gcov_count (bb) / 2;
636 if (bb_gcov_count (bb))
638 bool set_to_guessed = false;
639 FOR_EACH_EDGE (e, ei, bb->succs)
641 bool prev_never = e->probability == profile_probability::never ();
642 e->probability = profile_probability::probability_in_gcov_type
643 (edge_gcov_count (e), bb_gcov_count (bb));
644 if (e->probability == profile_probability::never ()
645 && !prev_never
646 && flag_profile_partial_training)
647 set_to_guessed = true;
649 if (set_to_guessed)
650 FOR_EACH_EDGE (e, ei, bb->succs)
651 e->probability = e->probability.guessed ();
652 if (bb->index >= NUM_FIXED_BLOCKS
653 && block_ends_with_condjump_p (bb)
654 && EDGE_COUNT (bb->succs) >= 2)
656 int prob;
657 edge e;
658 int index;
660 /* Find the branch edge. It is possible that we do have fake
661 edges here. */
662 FOR_EACH_EDGE (e, ei, bb->succs)
663 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
664 break;
666 prob = e->probability.to_reg_br_prob_base ();
667 index = prob * 20 / REG_BR_PROB_BASE;
669 if (index == 20)
670 index = 19;
671 hist_br_prob[index]++;
673 num_branches++;
676 /* As a last resort, distribute the probabilities evenly.
677 Use simple heuristics that if there are normal edges,
678 give all abnormals frequency of 0, otherwise distribute the
679 frequency over abnormals (this is the case of noreturn
680 calls). */
681 else if (profile_status_for_fn (cfun) == PROFILE_ABSENT)
683 int total = 0;
685 FOR_EACH_EDGE (e, ei, bb->succs)
686 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
687 total ++;
688 if (total)
690 FOR_EACH_EDGE (e, ei, bb->succs)
691 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
692 e->probability
693 = profile_probability::guessed_always ().apply_scale (1, total);
694 else
695 e->probability = profile_probability::never ();
697 else
699 total += EDGE_COUNT (bb->succs);
700 FOR_EACH_EDGE (e, ei, bb->succs)
701 e->probability
702 = profile_probability::guessed_always ().apply_scale (1, total);
704 if (bb->index >= NUM_FIXED_BLOCKS
705 && block_ends_with_condjump_p (bb)
706 && EDGE_COUNT (bb->succs) >= 2)
707 num_branches++;
711 if (exec_counts
712 && (bb_gcov_count (ENTRY_BLOCK_PTR_FOR_FN (cfun))
713 || !flag_profile_partial_training))
714 profile_status_for_fn (cfun) = PROFILE_READ;
716 /* If we have real data, use them! */
717 if (bb_gcov_count (ENTRY_BLOCK_PTR_FOR_FN (cfun))
718 || !flag_guess_branch_prob)
719 FOR_ALL_BB_FN (bb, cfun)
720 if (bb_gcov_count (bb) || !flag_profile_partial_training)
721 bb->count = profile_count::from_gcov_type (bb_gcov_count (bb));
722 else
723 bb->count = profile_count::guessed_zero ();
724 /* If function was not trained, preserve local estimates including statically
725 determined zero counts. */
726 else if (profile_status_for_fn (cfun) == PROFILE_READ
727 && !flag_profile_partial_training)
728 FOR_ALL_BB_FN (bb, cfun)
729 if (!(bb->count == profile_count::zero ()))
730 bb->count = bb->count.global0 ();
732 bb_gcov_counts.release ();
733 delete edge_gcov_counts;
734 edge_gcov_counts = NULL;
736 update_max_bb_count ();
738 if (dump_file)
740 fprintf (dump_file, " Profile feedback for function");
741 fprintf (dump_file, ((profile_status_for_fn (cfun) == PROFILE_READ)
742 ? " is available \n"
743 : " is not available \n"));
745 fprintf (dump_file, "%d branches\n", num_branches);
746 if (num_branches)
747 for (i = 0; i < 10; i++)
748 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
749 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
750 5 * i, 5 * i + 5);
752 total_num_branches += num_branches;
753 for (i = 0; i < 20; i++)
754 total_hist_br_prob[i] += hist_br_prob[i];
756 fputc ('\n', dump_file);
757 fputc ('\n', dump_file);
760 free_aux_for_blocks ();
763 /* Sort the histogram value and count for TOPN and INDIR_CALL type. */
765 static void
766 sort_hist_values (histogram_value hist)
768 gcc_assert (hist->type == HIST_TYPE_TOPN_VALUES
769 || hist->type == HIST_TYPE_INDIR_CALL);
771 int counters = hist->hvalue.counters[1];
772 for (int i = 0; i < counters - 1; i++)
773 /* Hist value is organized as:
774 [total_executions, N, value1, counter1, ..., valueN, counterN]
775 Use decrease bubble sort to rearrange it. The sort starts from <value1,
776 counter1> and compares counter first. If counter is same, compares the
777 value, exchange it if small to keep stable. */
780 bool swapped = false;
781 for (int j = 0; j < counters - 1 - i; j++)
783 gcov_type *p = &hist->hvalue.counters[2 * j + 2];
784 if (p[1] < p[3] || (p[1] == p[3] && p[0] < p[2]))
786 std::swap (p[0], p[2]);
787 std::swap (p[1], p[3]);
788 swapped = true;
791 if (!swapped)
792 break;
795 /* Load value histograms values whose description is stored in VALUES array
796 from .gcda file.
798 CFG_CHECKSUM is the precomputed checksum for the CFG. */
800 static void
801 compute_value_histograms (histogram_values values, unsigned cfg_checksum,
802 unsigned lineno_checksum)
804 unsigned i, j, t, any;
805 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
806 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
807 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
808 gcov_type *aact_count;
809 struct cgraph_node *node;
811 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
812 n_histogram_counters[t] = 0;
814 for (i = 0; i < values.length (); i++)
816 histogram_value hist = values[i];
817 n_histogram_counters[(int) hist->type] += hist->n_counters;
820 any = 0;
821 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
823 if (!n_histogram_counters[t])
825 histogram_counts[t] = NULL;
826 continue;
829 histogram_counts[t] = get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
830 cfg_checksum,
831 lineno_checksum,
832 n_histogram_counters[t]);
833 if (histogram_counts[t])
834 any = 1;
835 act_count[t] = histogram_counts[t];
837 if (!any)
838 return;
840 for (i = 0; i < values.length (); i++)
842 histogram_value hist = values[i];
843 gimple *stmt = hist->hvalue.stmt;
845 t = (int) hist->type;
846 bool topn_p = (hist->type == HIST_TYPE_TOPN_VALUES
847 || hist->type == HIST_TYPE_INDIR_CALL);
849 /* TOP N counter uses variable number of counters. */
850 if (topn_p)
852 unsigned total_size;
853 if (act_count[t])
854 total_size = 2 + 2 * act_count[t][1];
855 else
856 total_size = 2;
857 gimple_add_histogram_value (cfun, stmt, hist);
858 hist->n_counters = total_size;
859 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
860 for (j = 0; j < hist->n_counters; j++)
861 if (act_count[t])
862 hist->hvalue.counters[j] = act_count[t][j];
863 else
864 hist->hvalue.counters[j] = 0;
865 act_count[t] += hist->n_counters;
866 sort_hist_values (hist);
868 else
870 aact_count = act_count[t];
872 if (act_count[t])
873 act_count[t] += hist->n_counters;
875 gimple_add_histogram_value (cfun, stmt, hist);
876 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
877 for (j = 0; j < hist->n_counters; j++)
878 if (aact_count)
879 hist->hvalue.counters[j] = aact_count[j];
880 else
881 hist->hvalue.counters[j] = 0;
884 /* Time profiler counter is not related to any statement,
885 so that we have to read the counter and set the value to
886 the corresponding call graph node. */
887 if (hist->type == HIST_TYPE_TIME_PROFILE)
889 node = cgraph_node::get (hist->fun->decl);
890 if (hist->hvalue.counters[0] >= 0
891 && hist->hvalue.counters[0] < INT_MAX / 2)
892 node->tp_first_run = hist->hvalue.counters[0];
893 else
895 if (flag_profile_correction)
896 error ("corrupted profile info: invalid time profile");
897 node->tp_first_run = 0;
900 /* Drop profile for -fprofile-reproducible=multithreaded. */
901 bool drop
902 = (flag_profile_reproducible == PROFILE_REPRODUCIBILITY_MULTITHREADED);
903 if (drop)
904 node->tp_first_run = 0;
906 if (dump_file)
907 fprintf (dump_file, "Read tp_first_run: %d%s\n", node->tp_first_run,
908 drop ? "; ignored because profile reproducibility is "
909 "multi-threaded" : "");
913 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
914 free (histogram_counts[t]);
917 /* Location triplet which records a location. */
918 struct location_triplet
920 const char *filename;
921 int lineno;
922 int bb_index;
925 /* Traits class for streamed_locations hash set below. */
927 struct location_triplet_hash : typed_noop_remove <location_triplet>
929 typedef location_triplet value_type;
930 typedef location_triplet compare_type;
932 static hashval_t
933 hash (const location_triplet &ref)
935 inchash::hash hstate (0);
936 if (ref.filename)
937 hstate.add_int (strlen (ref.filename));
938 hstate.add_int (ref.lineno);
939 hstate.add_int (ref.bb_index);
940 return hstate.end ();
943 static bool
944 equal (const location_triplet &ref1, const location_triplet &ref2)
946 return ref1.lineno == ref2.lineno
947 && ref1.bb_index == ref2.bb_index
948 && ref1.filename != NULL
949 && ref2.filename != NULL
950 && strcmp (ref1.filename, ref2.filename) == 0;
953 static void
954 mark_deleted (location_triplet &ref)
956 ref.lineno = -1;
959 static const bool empty_zero_p = false;
961 static void
962 mark_empty (location_triplet &ref)
964 ref.lineno = -2;
967 static bool
968 is_deleted (const location_triplet &ref)
970 return ref.lineno == -1;
973 static bool
974 is_empty (const location_triplet &ref)
976 return ref.lineno == -2;
983 /* When passed NULL as file_name, initialize.
984 When passed something else, output the necessary commands to change
985 line to LINE and offset to FILE_NAME. */
986 static void
987 output_location (hash_set<location_triplet_hash> *streamed_locations,
988 char const *file_name, int line,
989 gcov_position_t *offset, basic_block bb)
991 static char const *prev_file_name;
992 static int prev_line;
993 bool name_differs, line_differs;
995 location_triplet triplet;
996 triplet.filename = file_name;
997 triplet.lineno = line;
998 triplet.bb_index = bb ? bb->index : 0;
1000 if (streamed_locations->add (triplet))
1001 return;
1003 if (!file_name)
1005 prev_file_name = NULL;
1006 prev_line = -1;
1007 return;
1010 name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name);
1011 line_differs = prev_line != line;
1013 if (!*offset)
1015 *offset = gcov_write_tag (GCOV_TAG_LINES);
1016 gcov_write_unsigned (bb->index);
1017 name_differs = line_differs = true;
1020 /* If this is a new source file, then output the
1021 file's name to the .bb file. */
1022 if (name_differs)
1024 prev_file_name = file_name;
1025 gcov_write_unsigned (0);
1026 gcov_write_filename (prev_file_name);
1028 if (line_differs)
1030 gcov_write_unsigned (line);
1031 prev_line = line;
1035 /* Helper for qsort so edges get sorted from highest frequency to smallest.
1036 This controls the weight for minimal spanning tree algorithm */
1037 static int
1038 compare_freqs (const void *p1, const void *p2)
1040 const_edge e1 = *(const const_edge *)p1;
1041 const_edge e2 = *(const const_edge *)p2;
1043 /* Critical edges needs to be split which introduce extra control flow.
1044 Make them more heavy. */
1045 int m1 = EDGE_CRITICAL_P (e1) ? 2 : 1;
1046 int m2 = EDGE_CRITICAL_P (e2) ? 2 : 1;
1048 if (EDGE_FREQUENCY (e1) * m1 + m1 != EDGE_FREQUENCY (e2) * m2 + m2)
1049 return EDGE_FREQUENCY (e2) * m2 + m2 - EDGE_FREQUENCY (e1) * m1 - m1;
1050 /* Stabilize sort. */
1051 if (e1->src->index != e2->src->index)
1052 return e2->src->index - e1->src->index;
1053 return e2->dest->index - e1->dest->index;
1056 /* Only read execution count for thunks. */
1058 void
1059 read_thunk_profile (struct cgraph_node *node)
1061 tree old = current_function_decl;
1062 current_function_decl = node->decl;
1063 gcov_type *counts = get_coverage_counts (GCOV_COUNTER_ARCS, 0, 0, 1);
1064 if (counts)
1066 node->callees->count = node->count
1067 = profile_count::from_gcov_type (counts[0]);
1068 free (counts);
1070 current_function_decl = old;
1071 return;
1075 /* Instrument and/or analyze program behavior based on program the CFG.
1077 This function creates a representation of the control flow graph (of
1078 the function being compiled) that is suitable for the instrumentation
1079 of edges and/or converting measured edge counts to counts on the
1080 complete CFG.
1082 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
1083 the flow graph that are needed to reconstruct the dynamic behavior of the
1084 flow graph. This data is written to the gcno file for gcov.
1086 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
1087 information from the gcda file containing edge count information from
1088 previous executions of the function being compiled. In this case, the
1089 control flow graph is annotated with actual execution counts by
1090 compute_branch_probabilities().
1092 Main entry point of this file. */
1094 void
1095 branch_prob (bool thunk)
1097 basic_block bb;
1098 unsigned i;
1099 unsigned num_edges, ignored_edges;
1100 unsigned num_instrumented;
1101 struct edge_list *el;
1102 histogram_values values = histogram_values ();
1103 unsigned cfg_checksum, lineno_checksum;
1105 total_num_times_called++;
1107 flow_call_edges_add (NULL);
1108 add_noreturn_fake_exit_edges ();
1110 hash_set <location_triplet_hash> streamed_locations;
1112 if (!thunk)
1114 /* We can't handle cyclic regions constructed using abnormal edges.
1115 To avoid these we replace every source of abnormal edge by a fake
1116 edge from entry node and every destination by fake edge to exit.
1117 This keeps graph acyclic and our calculation exact for all normal
1118 edges except for exit and entrance ones.
1120 We also add fake exit edges for each call and asm statement in the
1121 basic, since it may not return. */
1123 FOR_EACH_BB_FN (bb, cfun)
1125 int need_exit_edge = 0, need_entry_edge = 0;
1126 int have_exit_edge = 0, have_entry_edge = 0;
1127 edge e;
1128 edge_iterator ei;
1130 /* Functions returning multiple times are not handled by extra edges.
1131 Instead we simply allow negative counts on edges from exit to the
1132 block past call and corresponding probabilities. We can't go
1133 with the extra edges because that would result in flowgraph that
1134 needs to have fake edges outside the spanning tree. */
1136 FOR_EACH_EDGE (e, ei, bb->succs)
1138 gimple_stmt_iterator gsi;
1139 gimple *last = NULL;
1141 /* It may happen that there are compiler generated statements
1142 without a locus at all. Go through the basic block from the
1143 last to the first statement looking for a locus. */
1144 for (gsi = gsi_last_nondebug_bb (bb);
1145 !gsi_end_p (gsi);
1146 gsi_prev_nondebug (&gsi))
1148 last = gsi_stmt (gsi);
1149 if (!RESERVED_LOCATION_P (gimple_location (last)))
1150 break;
1153 /* Edge with goto locus might get wrong coverage info unless
1154 it is the only edge out of BB.
1155 Don't do that when the locuses match, so
1156 if (blah) goto something;
1157 is not computed twice. */
1158 if (last
1159 && gimple_has_location (last)
1160 && !RESERVED_LOCATION_P (e->goto_locus)
1161 && !single_succ_p (bb)
1162 && (LOCATION_FILE (e->goto_locus)
1163 != LOCATION_FILE (gimple_location (last))
1164 || (LOCATION_LINE (e->goto_locus)
1165 != LOCATION_LINE (gimple_location (last)))))
1167 basic_block new_bb = split_edge (e);
1168 edge ne = single_succ_edge (new_bb);
1169 ne->goto_locus = e->goto_locus;
1171 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1172 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1173 need_exit_edge = 1;
1174 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1175 have_exit_edge = 1;
1177 FOR_EACH_EDGE (e, ei, bb->preds)
1179 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1180 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1181 need_entry_edge = 1;
1182 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1183 have_entry_edge = 1;
1186 if (need_exit_edge && !have_exit_edge)
1188 if (dump_file)
1189 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
1190 bb->index);
1191 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
1193 if (need_entry_edge && !have_entry_edge)
1195 if (dump_file)
1196 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
1197 bb->index);
1198 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FAKE);
1199 /* Avoid bbs that have both fake entry edge and also some
1200 exit edge. One of those edges wouldn't be added to the
1201 spanning tree, but we can't instrument any of them. */
1202 if (have_exit_edge || need_exit_edge)
1204 gimple_stmt_iterator gsi;
1205 gimple *first;
1207 gsi = gsi_start_nondebug_after_labels_bb (bb);
1208 gcc_checking_assert (!gsi_end_p (gsi));
1209 first = gsi_stmt (gsi);
1210 /* Don't split the bbs containing __builtin_setjmp_receiver
1211 or ABNORMAL_DISPATCHER calls. These are very
1212 special and don't expect anything to be inserted before
1213 them. */
1214 if (is_gimple_call (first)
1215 && (gimple_call_builtin_p (first, BUILT_IN_SETJMP_RECEIVER)
1216 || (gimple_call_flags (first) & ECF_RETURNS_TWICE)
1217 || (gimple_call_internal_p (first)
1218 && (gimple_call_internal_fn (first)
1219 == IFN_ABNORMAL_DISPATCHER))))
1220 continue;
1222 if (dump_file)
1223 fprintf (dump_file, "Splitting bb %i after labels\n",
1224 bb->index);
1225 split_block_after_labels (bb);
1231 el = create_edge_list ();
1232 num_edges = NUM_EDGES (el);
1233 qsort (el->index_to_edge, num_edges, sizeof (edge), compare_freqs);
1234 alloc_aux_for_edges (sizeof (struct edge_profile_info));
1236 /* The basic blocks are expected to be numbered sequentially. */
1237 compact_blocks ();
1239 ignored_edges = 0;
1240 for (i = 0 ; i < num_edges ; i++)
1242 edge e = INDEX_EDGE (el, i);
1244 /* Mark edges we've replaced by fake edges above as ignored. */
1245 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1246 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1247 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1249 EDGE_INFO (e)->ignore = 1;
1250 ignored_edges++;
1254 /* Create spanning tree from basic block graph, mark each edge that is
1255 on the spanning tree. We insert as many abnormal and critical edges
1256 as possible to minimize number of edge splits necessary. */
1258 if (!thunk)
1259 find_spanning_tree (el);
1260 else
1262 edge e;
1263 edge_iterator ei;
1264 /* Keep only edge from entry block to be instrumented. */
1265 FOR_EACH_BB_FN (bb, cfun)
1266 FOR_EACH_EDGE (e, ei, bb->succs)
1267 EDGE_INFO (e)->ignore = true;
1271 /* Fake edges that are not on the tree will not be instrumented, so
1272 mark them ignored. */
1273 for (num_instrumented = i = 0; i < num_edges; i++)
1275 edge e = INDEX_EDGE (el, i);
1276 struct edge_profile_info *inf = EDGE_INFO (e);
1278 if (inf->ignore || inf->on_tree)
1279 /*NOP*/;
1280 else if (e->flags & EDGE_FAKE)
1282 inf->ignore = 1;
1283 ignored_edges++;
1285 else
1286 num_instrumented++;
1289 total_num_blocks += n_basic_blocks_for_fn (cfun);
1290 if (dump_file)
1291 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks_for_fn (cfun));
1293 total_num_edges += num_edges;
1294 if (dump_file)
1295 fprintf (dump_file, "%d edges\n", num_edges);
1297 total_num_edges_ignored += ignored_edges;
1298 if (dump_file)
1299 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
1301 total_num_edges_instrumented += num_instrumented;
1302 if (dump_file)
1303 fprintf (dump_file, "%d instrumentation edges\n", num_instrumented);
1305 /* Dump function body before it's instrumented.
1306 It helps to debug gcov tool. */
1307 if (dump_file && (dump_flags & TDF_DETAILS))
1308 dump_function_to_file (cfun->decl, dump_file, dump_flags);
1310 /* Compute two different checksums. Note that we want to compute
1311 the checksum in only once place, since it depends on the shape
1312 of the control flow which can change during
1313 various transformations. */
1314 if (thunk)
1316 /* At stream in time we do not have CFG, so we cannot do checksums. */
1317 cfg_checksum = 0;
1318 lineno_checksum = 0;
1320 else
1322 cfg_checksum = coverage_compute_cfg_checksum (cfun);
1323 lineno_checksum = coverage_compute_lineno_checksum ();
1326 /* Write the data from which gcov can reconstruct the basic block
1327 graph and function line numbers (the gcno file). */
1328 if (coverage_begin_function (lineno_checksum, cfg_checksum))
1330 gcov_position_t offset;
1332 /* Basic block flags */
1333 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
1334 gcov_write_unsigned (n_basic_blocks_for_fn (cfun));
1335 gcov_write_length (offset);
1337 /* Arcs */
1338 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
1339 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1341 edge e;
1342 edge_iterator ei;
1344 offset = gcov_write_tag (GCOV_TAG_ARCS);
1345 gcov_write_unsigned (bb->index);
1347 FOR_EACH_EDGE (e, ei, bb->succs)
1349 struct edge_profile_info *i = EDGE_INFO (e);
1350 if (!i->ignore)
1352 unsigned flag_bits = 0;
1354 if (i->on_tree)
1355 flag_bits |= GCOV_ARC_ON_TREE;
1356 if (e->flags & EDGE_FAKE)
1357 flag_bits |= GCOV_ARC_FAKE;
1358 if (e->flags & EDGE_FALLTHRU)
1359 flag_bits |= GCOV_ARC_FALLTHROUGH;
1360 /* On trees we don't have fallthru flags, but we can
1361 recompute them from CFG shape. */
1362 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
1363 && e->src->next_bb == e->dest)
1364 flag_bits |= GCOV_ARC_FALLTHROUGH;
1366 gcov_write_unsigned (e->dest->index);
1367 gcov_write_unsigned (flag_bits);
1371 gcov_write_length (offset);
1374 /* Line numbers. */
1375 /* Initialize the output. */
1376 output_location (&streamed_locations, NULL, 0, NULL, NULL);
1378 hash_set<location_hash> seen_locations;
1380 FOR_EACH_BB_FN (bb, cfun)
1382 gimple_stmt_iterator gsi;
1383 gcov_position_t offset = 0;
1385 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
1387 location_t loc = DECL_SOURCE_LOCATION (current_function_decl);
1388 gcc_checking_assert (!RESERVED_LOCATION_P (loc));
1389 seen_locations.add (loc);
1390 expanded_location curr_location = expand_location (loc);
1391 output_location (&streamed_locations, curr_location.file,
1392 MAX (1, curr_location.line), &offset, bb);
1395 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1397 gimple *stmt = gsi_stmt (gsi);
1398 location_t loc = gimple_location (stmt);
1399 if (!RESERVED_LOCATION_P (loc))
1401 seen_locations.add (loc);
1402 output_location (&streamed_locations, gimple_filename (stmt),
1403 MAX (1, gimple_lineno (stmt)), &offset, bb);
1407 /* Notice GOTO expressions eliminated while constructing the CFG.
1408 It's hard to distinguish such expression, but goto_locus should
1409 not be any of already seen location. */
1410 location_t loc;
1411 if (single_succ_p (bb)
1412 && (loc = single_succ_edge (bb)->goto_locus)
1413 && !RESERVED_LOCATION_P (loc)
1414 && !seen_locations.contains (loc))
1416 expanded_location curr_location = expand_location (loc);
1417 output_location (&streamed_locations, curr_location.file,
1418 MAX (1, curr_location.line), &offset, bb);
1421 if (offset)
1423 /* A file of NULL indicates the end of run. */
1424 gcov_write_unsigned (0);
1425 gcov_write_string (NULL);
1426 gcov_write_length (offset);
1431 if (flag_profile_values)
1432 gimple_find_values_to_profile (&values);
1434 if (flag_branch_probabilities)
1436 compute_branch_probabilities (cfg_checksum, lineno_checksum);
1437 if (flag_profile_values)
1438 compute_value_histograms (values, cfg_checksum, lineno_checksum);
1441 remove_fake_edges ();
1443 /* For each edge not on the spanning tree, add counting code. */
1444 if (profile_arc_flag
1445 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1447 unsigned n_instrumented;
1449 gimple_init_gcov_profiler ();
1451 n_instrumented = instrument_edges (el);
1453 gcc_assert (n_instrumented == num_instrumented);
1455 if (flag_profile_values)
1456 instrument_values (values);
1458 /* Commit changes done by instrumentation. */
1459 gsi_commit_edge_inserts ();
1462 free_aux_for_edges ();
1464 values.release ();
1465 free_edge_list (el);
1466 coverage_end_function (lineno_checksum, cfg_checksum);
1467 if (flag_branch_probabilities
1468 && (profile_status_for_fn (cfun) == PROFILE_READ))
1470 if (dump_file && (dump_flags & TDF_DETAILS))
1471 report_predictor_hitrates ();
1473 /* At this moment we have precise loop iteration count estimates.
1474 Record them to loop structure before the profile gets out of date. */
1475 for (auto loop : loops_list (cfun, 0))
1476 if (loop->header->count > 0 && loop->header->count.reliable_p ())
1478 gcov_type nit = expected_loop_iterations_unbounded (loop);
1479 widest_int bound = gcov_type_to_wide_int (nit);
1480 loop->any_estimate = false;
1481 record_niter_bound (loop, bound, true, false);
1483 compute_function_frequency ();
1487 /* Union find algorithm implementation for the basic blocks using
1488 aux fields. */
1490 static basic_block
1491 find_group (basic_block bb)
1493 basic_block group = bb, bb1;
1495 while ((basic_block) group->aux != group)
1496 group = (basic_block) group->aux;
1498 /* Compress path. */
1499 while ((basic_block) bb->aux != group)
1501 bb1 = (basic_block) bb->aux;
1502 bb->aux = (void *) group;
1503 bb = bb1;
1505 return group;
1508 static void
1509 union_groups (basic_block bb1, basic_block bb2)
1511 basic_block bb1g = find_group (bb1);
1512 basic_block bb2g = find_group (bb2);
1514 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1515 this code is unlikely going to be performance problem anyway. */
1516 gcc_assert (bb1g != bb2g);
1518 bb1g->aux = bb2g;
1521 /* This function searches all of the edges in the program flow graph, and puts
1522 as many bad edges as possible onto the spanning tree. Bad edges include
1523 abnormals edges, which can't be instrumented at the moment. Since it is
1524 possible for fake edges to form a cycle, we will have to develop some
1525 better way in the future. Also put critical edges to the tree, since they
1526 are more expensive to instrument. */
1528 static void
1529 find_spanning_tree (struct edge_list *el)
1531 int i;
1532 int num_edges = NUM_EDGES (el);
1533 basic_block bb;
1535 /* We use aux field for standard union-find algorithm. */
1536 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
1537 bb->aux = bb;
1539 /* Add fake edge exit to entry we can't instrument. */
1540 union_groups (EXIT_BLOCK_PTR_FOR_FN (cfun), ENTRY_BLOCK_PTR_FOR_FN (cfun));
1542 /* First add all abnormal edges to the tree unless they form a cycle. Also
1543 add all edges to the exit block to avoid inserting profiling code behind
1544 setting return value from function. */
1545 for (i = 0; i < num_edges; i++)
1547 edge e = INDEX_EDGE (el, i);
1548 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1549 || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1550 && !EDGE_INFO (e)->ignore
1551 && (find_group (e->src) != find_group (e->dest)))
1553 if (dump_file)
1554 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1555 e->src->index, e->dest->index);
1556 EDGE_INFO (e)->on_tree = 1;
1557 union_groups (e->src, e->dest);
1561 /* And now the rest. Edge list is sorted according to frequencies and
1562 thus we will produce minimal spanning tree. */
1563 for (i = 0; i < num_edges; i++)
1565 edge e = INDEX_EDGE (el, i);
1566 if (!EDGE_INFO (e)->ignore
1567 && find_group (e->src) != find_group (e->dest))
1569 if (dump_file)
1570 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1571 e->src->index, e->dest->index);
1572 EDGE_INFO (e)->on_tree = 1;
1573 union_groups (e->src, e->dest);
1577 clear_aux_for_blocks ();
1580 /* Perform file-level initialization for branch-prob processing. */
1582 void
1583 init_branch_prob (void)
1585 int i;
1587 total_num_blocks = 0;
1588 total_num_edges = 0;
1589 total_num_edges_ignored = 0;
1590 total_num_edges_instrumented = 0;
1591 total_num_blocks_created = 0;
1592 total_num_passes = 0;
1593 total_num_times_called = 0;
1594 total_num_branches = 0;
1595 for (i = 0; i < 20; i++)
1596 total_hist_br_prob[i] = 0;
1599 /* Performs file-level cleanup after branch-prob processing
1600 is completed. */
1602 void
1603 end_branch_prob (void)
1605 if (dump_file)
1607 fprintf (dump_file, "\n");
1608 fprintf (dump_file, "Total number of blocks: %d\n",
1609 total_num_blocks);
1610 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1611 fprintf (dump_file, "Total number of ignored edges: %d\n",
1612 total_num_edges_ignored);
1613 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1614 total_num_edges_instrumented);
1615 fprintf (dump_file, "Total number of blocks created: %d\n",
1616 total_num_blocks_created);
1617 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1618 total_num_passes);
1619 if (total_num_times_called != 0)
1620 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1621 (total_num_passes + (total_num_times_called >> 1))
1622 / total_num_times_called);
1623 fprintf (dump_file, "Total number of branches: %d\n",
1624 total_num_branches);
1625 if (total_num_branches)
1627 int i;
1629 for (i = 0; i < 10; i++)
1630 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1631 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1632 / total_num_branches, 5*i, 5*i+5);