]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/profile.c
2015-06-25 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / profile.c
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2015 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.
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
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.
39
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. */
43
44 /* ??? Register allocation should use basic block execution counts to
45 give preference to the most commonly executed blocks. */
46
47 /* ??? Should calculate branch probabilities before instrumenting code, since
48 then we can use arc counts to help decide which arcs to instrument. */
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "rtl.h"
55 #include "flags.h"
56 #include "regs.h"
57 #include "symtab.h"
58 #include "hard-reg-set.h"
59 #include "function.h"
60 #include "alias.h"
61 #include "tree.h"
62 #include "insn-config.h"
63 #include "expmed.h"
64 #include "dojump.h"
65 #include "explow.h"
66 #include "calls.h"
67 #include "emit-rtl.h"
68 #include "varasm.h"
69 #include "stmt.h"
70 #include "expr.h"
71 #include "predict.h"
72 #include "dominance.h"
73 #include "cfg.h"
74 #include "cfganal.h"
75 #include "basic-block.h"
76 #include "diagnostic-core.h"
77 #include "coverage.h"
78 #include "value-prof.h"
79 #include "fold-const.h"
80 #include "tree-ssa-alias.h"
81 #include "internal-fn.h"
82 #include "gimple-expr.h"
83 #include "gimple.h"
84 #include "gimple-iterator.h"
85 #include "tree-cfg.h"
86 #include "cfgloop.h"
87 #include "dumpfile.h"
88 #include "cgraph.h"
89
90 #include "profile.h"
91
92 struct bb_profile_info {
93 unsigned int count_valid : 1;
94
95 /* Number of successor and predecessor edges. */
96 gcov_type succ_count;
97 gcov_type pred_count;
98 };
99
100 #define BB_INFO(b) ((struct bb_profile_info *) (b)->aux)
101
102
103 /* Counter summary from the last set of coverage counts read. */
104
105 const struct gcov_ctr_summary *profile_info;
106
107 /* Counter working set information computed from the current counter
108 summary. Not initialized unless profile_info summary is non-NULL. */
109 static gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
110
111 /* Collect statistics on the performance of this pass for the entire source
112 file. */
113
114 static int total_num_blocks;
115 static int total_num_edges;
116 static int total_num_edges_ignored;
117 static int total_num_edges_instrumented;
118 static int total_num_blocks_created;
119 static int total_num_passes;
120 static int total_num_times_called;
121 static int total_hist_br_prob[20];
122 static int total_num_branches;
123
124 /* Helper function to update gcov_working_sets. */
125
126 void add_working_set (gcov_working_set_t *set) {
127 int i = 0;
128 for (; i < NUM_GCOV_WORKING_SETS; i++)
129 gcov_working_sets[i] = set[i];
130 }
131
132 /* Forward declarations. */
133 static void find_spanning_tree (struct edge_list *);
134
135 /* Add edge instrumentation code to the entire insn chain.
136
137 F is the first insn of the chain.
138 NUM_BLOCKS is the number of basic blocks found in F. */
139
140 static unsigned
141 instrument_edges (struct edge_list *el)
142 {
143 unsigned num_instr_edges = 0;
144 int num_edges = NUM_EDGES (el);
145 basic_block bb;
146
147 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
148 {
149 edge e;
150 edge_iterator ei;
151
152 FOR_EACH_EDGE (e, ei, bb->succs)
153 {
154 struct edge_profile_info *inf = EDGE_INFO (e);
155
156 if (!inf->ignore && !inf->on_tree)
157 {
158 gcc_assert (!(e->flags & EDGE_ABNORMAL));
159 if (dump_file)
160 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
161 e->src->index, e->dest->index,
162 EDGE_CRITICAL_P (e) ? " (and split)" : "");
163 gimple_gen_edge_profiler (num_instr_edges++, e);
164 }
165 }
166 }
167
168 total_num_blocks_created += num_edges;
169 if (dump_file)
170 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
171 return num_instr_edges;
172 }
173
174 /* Add code to measure histograms for values in list VALUES. */
175 static void
176 instrument_values (histogram_values values)
177 {
178 unsigned i;
179
180 /* Emit code to generate the histograms before the insns. */
181
182 for (i = 0; i < values.length (); i++)
183 {
184 histogram_value hist = values[i];
185 unsigned t = COUNTER_FOR_HIST_TYPE (hist->type);
186
187 if (!coverage_counter_alloc (t, hist->n_counters))
188 continue;
189
190 switch (hist->type)
191 {
192 case HIST_TYPE_INTERVAL:
193 gimple_gen_interval_profiler (hist, t, 0);
194 break;
195
196 case HIST_TYPE_POW2:
197 gimple_gen_pow2_profiler (hist, t, 0);
198 break;
199
200 case HIST_TYPE_SINGLE_VALUE:
201 gimple_gen_one_value_profiler (hist, t, 0);
202 break;
203
204 case HIST_TYPE_CONST_DELTA:
205 gimple_gen_const_delta_profiler (hist, t, 0);
206 break;
207
208 case HIST_TYPE_INDIR_CALL:
209 case HIST_TYPE_INDIR_CALL_TOPN:
210 gimple_gen_ic_profiler (hist, t, 0);
211 break;
212
213 case HIST_TYPE_AVERAGE:
214 gimple_gen_average_profiler (hist, t, 0);
215 break;
216
217 case HIST_TYPE_IOR:
218 gimple_gen_ior_profiler (hist, t, 0);
219 break;
220
221 case HIST_TYPE_TIME_PROFILE:
222 {
223 basic_block bb =
224 split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
225 gimple_stmt_iterator gsi = gsi_start_bb (bb);
226
227 gimple_gen_time_profiler (t, 0, gsi);
228 break;
229 }
230
231 default:
232 gcc_unreachable ();
233 }
234 }
235 }
236 \f
237
238 /* Fill the working set information into the profile_info structure. */
239
240 void
241 get_working_sets (void)
242 {
243 unsigned ws_ix, pctinc, pct;
244 gcov_working_set_t *ws_info;
245
246 if (!profile_info)
247 return;
248
249 compute_working_sets (profile_info, gcov_working_sets);
250
251 if (dump_file)
252 {
253 fprintf (dump_file, "Counter working sets:\n");
254 /* Multiply the percentage by 100 to avoid float. */
255 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
256 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
257 ws_ix++, pct += pctinc)
258 {
259 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
260 pct = 9990;
261 ws_info = &gcov_working_sets[ws_ix];
262 /* Print out the percentage using int arithmatic to avoid float. */
263 fprintf (dump_file, "\t\t%u.%02u%%: num counts=%u, min counter="
264 "%" PRId64 "\n",
265 pct / 100, pct - (pct / 100 * 100),
266 ws_info->num_counters,
267 (int64_t)ws_info->min_counter);
268 }
269 }
270 }
271
272 /* Given a the desired percentage of the full profile (sum_all from the
273 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
274 the corresponding working set information. If an exact match for
275 the percentage isn't found, the closest value is used. */
276
277 gcov_working_set_t *
278 find_working_set (unsigned pct_times_10)
279 {
280 unsigned i;
281 if (!profile_info)
282 return NULL;
283 gcc_assert (pct_times_10 <= 1000);
284 if (pct_times_10 >= 999)
285 return &gcov_working_sets[NUM_GCOV_WORKING_SETS - 1];
286 i = pct_times_10 * NUM_GCOV_WORKING_SETS / 1000;
287 if (!i)
288 return &gcov_working_sets[0];
289 return &gcov_working_sets[i - 1];
290 }
291
292 /* Computes hybrid profile for all matching entries in da_file.
293
294 CFG_CHECKSUM is the precomputed checksum for the CFG. */
295
296 static gcov_type *
297 get_exec_counts (unsigned cfg_checksum, unsigned lineno_checksum)
298 {
299 unsigned num_edges = 0;
300 basic_block bb;
301 gcov_type *counts;
302
303 /* Count the edges to be (possibly) instrumented. */
304 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
305 {
306 edge e;
307 edge_iterator ei;
308
309 FOR_EACH_EDGE (e, ei, bb->succs)
310 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
311 num_edges++;
312 }
313
314 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, cfg_checksum,
315 lineno_checksum, &profile_info);
316 if (!counts)
317 return NULL;
318
319 get_working_sets ();
320
321 if (dump_file && profile_info)
322 fprintf (dump_file, "Merged %u profiles with maximal count %u.\n",
323 profile_info->runs, (unsigned) profile_info->sum_max);
324
325 return counts;
326 }
327
328
329 static bool
330 is_edge_inconsistent (vec<edge, va_gc> *edges)
331 {
332 edge e;
333 edge_iterator ei;
334 FOR_EACH_EDGE (e, ei, edges)
335 {
336 if (!EDGE_INFO (e)->ignore)
337 {
338 if (e->count < 0
339 && (!(e->flags & EDGE_FAKE)
340 || !block_ends_with_call_p (e->src)))
341 {
342 if (dump_file)
343 {
344 fprintf (dump_file,
345 "Edge %i->%i is inconsistent, count%" PRId64,
346 e->src->index, e->dest->index, e->count);
347 dump_bb (dump_file, e->src, 0, TDF_DETAILS);
348 dump_bb (dump_file, e->dest, 0, TDF_DETAILS);
349 }
350 return true;
351 }
352 }
353 }
354 return false;
355 }
356
357 static void
358 correct_negative_edge_counts (void)
359 {
360 basic_block bb;
361 edge e;
362 edge_iterator ei;
363
364 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
365 {
366 FOR_EACH_EDGE (e, ei, bb->succs)
367 {
368 if (e->count < 0)
369 e->count = 0;
370 }
371 }
372 }
373
374 /* Check consistency.
375 Return true if inconsistency is found. */
376 static bool
377 is_inconsistent (void)
378 {
379 basic_block bb;
380 bool inconsistent = false;
381 FOR_EACH_BB_FN (bb, cfun)
382 {
383 inconsistent |= is_edge_inconsistent (bb->preds);
384 if (!dump_file && inconsistent)
385 return true;
386 inconsistent |= is_edge_inconsistent (bb->succs);
387 if (!dump_file && inconsistent)
388 return true;
389 if (bb->count < 0)
390 {
391 if (dump_file)
392 {
393 fprintf (dump_file, "BB %i count is negative "
394 "%" PRId64,
395 bb->index,
396 bb->count);
397 dump_bb (dump_file, bb, 0, TDF_DETAILS);
398 }
399 inconsistent = true;
400 }
401 if (bb->count != sum_edge_counts (bb->preds))
402 {
403 if (dump_file)
404 {
405 fprintf (dump_file, "BB %i count does not match sum of incoming edges "
406 "%" PRId64" should be %" PRId64,
407 bb->index,
408 bb->count,
409 sum_edge_counts (bb->preds));
410 dump_bb (dump_file, bb, 0, TDF_DETAILS);
411 }
412 inconsistent = true;
413 }
414 if (bb->count != sum_edge_counts (bb->succs) &&
415 ! (find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)) != NULL
416 && block_ends_with_call_p (bb)))
417 {
418 if (dump_file)
419 {
420 fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
421 "%" PRId64" should be %" PRId64,
422 bb->index,
423 bb->count,
424 sum_edge_counts (bb->succs));
425 dump_bb (dump_file, bb, 0, TDF_DETAILS);
426 }
427 inconsistent = true;
428 }
429 if (!dump_file && inconsistent)
430 return true;
431 }
432
433 return inconsistent;
434 }
435
436 /* Set each basic block count to the sum of its outgoing edge counts */
437 static void
438 set_bb_counts (void)
439 {
440 basic_block bb;
441 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
442 {
443 bb->count = sum_edge_counts (bb->succs);
444 gcc_assert (bb->count >= 0);
445 }
446 }
447
448 /* Reads profile data and returns total number of edge counts read */
449 static int
450 read_profile_edge_counts (gcov_type *exec_counts)
451 {
452 basic_block bb;
453 int num_edges = 0;
454 int exec_counts_pos = 0;
455 /* For each edge not on the spanning tree, set its execution count from
456 the .da file. */
457 /* The first count in the .da file is the number of times that the function
458 was entered. This is the exec_count for block zero. */
459
460 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
461 {
462 edge e;
463 edge_iterator ei;
464
465 FOR_EACH_EDGE (e, ei, bb->succs)
466 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
467 {
468 num_edges++;
469 if (exec_counts)
470 {
471 e->count = exec_counts[exec_counts_pos++];
472 if (e->count > profile_info->sum_max)
473 {
474 if (flag_profile_correction)
475 {
476 static bool informed = 0;
477 if (dump_enabled_p () && !informed)
478 dump_printf_loc (MSG_NOTE, input_location,
479 "corrupted profile info: edge count"
480 " exceeds maximal count\n");
481 informed = 1;
482 }
483 else
484 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
485 bb->index, e->dest->index);
486 }
487 }
488 else
489 e->count = 0;
490
491 EDGE_INFO (e)->count_valid = 1;
492 BB_INFO (bb)->succ_count--;
493 BB_INFO (e->dest)->pred_count--;
494 if (dump_file)
495 {
496 fprintf (dump_file, "\nRead edge from %i to %i, count:",
497 bb->index, e->dest->index);
498 fprintf (dump_file, "%" PRId64,
499 (int64_t) e->count);
500 }
501 }
502 }
503
504 return num_edges;
505 }
506
507 #define OVERLAP_BASE 10000
508
509 /* Compare the static estimated profile to the actual profile, and
510 return the "degree of overlap" measure between them.
511
512 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
513 the sum of each basic block's minimum relative weights between
514 two profiles. And overlap of OVERLAP_BASE means two profiles are
515 identical. */
516
517 static int
518 compute_frequency_overlap (void)
519 {
520 gcov_type count_total = 0, freq_total = 0;
521 int overlap = 0;
522 basic_block bb;
523
524 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
525 {
526 count_total += bb->count;
527 freq_total += bb->frequency;
528 }
529
530 if (count_total == 0 || freq_total == 0)
531 return 0;
532
533 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
534 overlap += MIN (bb->count * OVERLAP_BASE / count_total,
535 bb->frequency * OVERLAP_BASE / freq_total);
536
537 return overlap;
538 }
539
540 /* Compute the branch probabilities for the various branches.
541 Annotate them accordingly.
542
543 CFG_CHECKSUM is the precomputed checksum for the CFG. */
544
545 static void
546 compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
547 {
548 basic_block bb;
549 int i;
550 int num_edges = 0;
551 int changes;
552 int passes;
553 int hist_br_prob[20];
554 int num_branches;
555 gcov_type *exec_counts = get_exec_counts (cfg_checksum, lineno_checksum);
556 int inconsistent = 0;
557
558 /* Very simple sanity checks so we catch bugs in our profiling code. */
559 if (!profile_info)
560 return;
561
562 if (profile_info->sum_all < profile_info->sum_max)
563 {
564 error ("corrupted profile info: sum_all is smaller than sum_max");
565 exec_counts = NULL;
566 }
567
568 /* Attach extra info block to each bb. */
569 alloc_aux_for_blocks (sizeof (struct bb_profile_info));
570 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
571 {
572 edge e;
573 edge_iterator ei;
574
575 FOR_EACH_EDGE (e, ei, bb->succs)
576 if (!EDGE_INFO (e)->ignore)
577 BB_INFO (bb)->succ_count++;
578 FOR_EACH_EDGE (e, ei, bb->preds)
579 if (!EDGE_INFO (e)->ignore)
580 BB_INFO (bb)->pred_count++;
581 }
582
583 /* Avoid predicting entry on exit nodes. */
584 BB_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->succ_count = 2;
585 BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (cfun))->pred_count = 2;
586
587 num_edges = read_profile_edge_counts (exec_counts);
588
589 if (dump_file)
590 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
591
592 /* For every block in the file,
593 - if every exit/entrance edge has a known count, then set the block count
594 - if the block count is known, and every exit/entrance edge but one has
595 a known execution count, then set the count of the remaining edge
596
597 As edge counts are set, decrement the succ/pred count, but don't delete
598 the edge, that way we can easily tell when all edges are known, or only
599 one edge is unknown. */
600
601 /* The order that the basic blocks are iterated through is important.
602 Since the code that finds spanning trees starts with block 0, low numbered
603 edges are put on the spanning tree in preference to high numbered edges.
604 Hence, most instrumented edges are at the end. Graph solving works much
605 faster if we propagate numbers from the end to the start.
606
607 This takes an average of slightly more than 3 passes. */
608
609 changes = 1;
610 passes = 0;
611 while (changes)
612 {
613 passes++;
614 changes = 0;
615 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, prev_bb)
616 {
617 struct bb_profile_info *bi = BB_INFO (bb);
618 if (! bi->count_valid)
619 {
620 if (bi->succ_count == 0)
621 {
622 edge e;
623 edge_iterator ei;
624 gcov_type total = 0;
625
626 FOR_EACH_EDGE (e, ei, bb->succs)
627 total += e->count;
628 bb->count = total;
629 bi->count_valid = 1;
630 changes = 1;
631 }
632 else if (bi->pred_count == 0)
633 {
634 edge e;
635 edge_iterator ei;
636 gcov_type total = 0;
637
638 FOR_EACH_EDGE (e, ei, bb->preds)
639 total += e->count;
640 bb->count = total;
641 bi->count_valid = 1;
642 changes = 1;
643 }
644 }
645 if (bi->count_valid)
646 {
647 if (bi->succ_count == 1)
648 {
649 edge e;
650 edge_iterator ei;
651 gcov_type total = 0;
652
653 /* One of the counts will be invalid, but it is zero,
654 so adding it in also doesn't hurt. */
655 FOR_EACH_EDGE (e, ei, bb->succs)
656 total += e->count;
657
658 /* Search for the invalid edge, and set its count. */
659 FOR_EACH_EDGE (e, ei, bb->succs)
660 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
661 break;
662
663 /* Calculate count for remaining edge by conservation. */
664 total = bb->count - total;
665
666 gcc_assert (e);
667 EDGE_INFO (e)->count_valid = 1;
668 e->count = total;
669 bi->succ_count--;
670
671 BB_INFO (e->dest)->pred_count--;
672 changes = 1;
673 }
674 if (bi->pred_count == 1)
675 {
676 edge e;
677 edge_iterator ei;
678 gcov_type total = 0;
679
680 /* One of the counts will be invalid, but it is zero,
681 so adding it in also doesn't hurt. */
682 FOR_EACH_EDGE (e, ei, bb->preds)
683 total += e->count;
684
685 /* Search for the invalid edge, and set its count. */
686 FOR_EACH_EDGE (e, ei, bb->preds)
687 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
688 break;
689
690 /* Calculate count for remaining edge by conservation. */
691 total = bb->count - total + e->count;
692
693 gcc_assert (e);
694 EDGE_INFO (e)->count_valid = 1;
695 e->count = total;
696 bi->pred_count--;
697
698 BB_INFO (e->src)->succ_count--;
699 changes = 1;
700 }
701 }
702 }
703 }
704 if (dump_file)
705 {
706 int overlap = compute_frequency_overlap ();
707 gimple_dump_cfg (dump_file, dump_flags);
708 fprintf (dump_file, "Static profile overlap: %d.%d%%\n",
709 overlap / (OVERLAP_BASE / 100),
710 overlap % (OVERLAP_BASE / 100));
711 }
712
713 total_num_passes += passes;
714 if (dump_file)
715 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
716
717 /* If the graph has been correctly solved, every block will have a
718 succ and pred count of zero. */
719 FOR_EACH_BB_FN (bb, cfun)
720 {
721 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
722 }
723
724 /* Check for inconsistent basic block counts */
725 inconsistent = is_inconsistent ();
726
727 if (inconsistent)
728 {
729 if (flag_profile_correction)
730 {
731 /* Inconsistency detected. Make it flow-consistent. */
732 static int informed = 0;
733 if (dump_enabled_p () && informed == 0)
734 {
735 informed = 1;
736 dump_printf_loc (MSG_NOTE, input_location,
737 "correcting inconsistent profile data\n");
738 }
739 correct_negative_edge_counts ();
740 /* Set bb counts to the sum of the outgoing edge counts */
741 set_bb_counts ();
742 if (dump_file)
743 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n");
744 mcf_smooth_cfg ();
745 }
746 else
747 error ("corrupted profile info: profile data is not flow-consistent");
748 }
749
750 /* For every edge, calculate its branch probability and add a reg_note
751 to the branch insn to indicate this. */
752
753 for (i = 0; i < 20; i++)
754 hist_br_prob[i] = 0;
755 num_branches = 0;
756
757 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
758 {
759 edge e;
760 edge_iterator ei;
761
762 if (bb->count < 0)
763 {
764 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
765 bb->index, (int)bb->count);
766 bb->count = 0;
767 }
768 FOR_EACH_EDGE (e, ei, bb->succs)
769 {
770 /* Function may return twice in the cased the called function is
771 setjmp or calls fork, but we can't represent this by extra
772 edge from the entry, since extra edge from the exit is
773 already present. We get negative frequency from the entry
774 point. */
775 if ((e->count < 0
776 && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
777 || (e->count > bb->count
778 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)))
779 {
780 if (block_ends_with_call_p (bb))
781 e->count = e->count < 0 ? 0 : bb->count;
782 }
783 if (e->count < 0 || e->count > bb->count)
784 {
785 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
786 e->src->index, e->dest->index,
787 (int)e->count);
788 e->count = bb->count / 2;
789 }
790 }
791 if (bb->count)
792 {
793 FOR_EACH_EDGE (e, ei, bb->succs)
794 e->probability = GCOV_COMPUTE_SCALE (e->count, bb->count);
795 if (bb->index >= NUM_FIXED_BLOCKS
796 && block_ends_with_condjump_p (bb)
797 && EDGE_COUNT (bb->succs) >= 2)
798 {
799 int prob;
800 edge e;
801 int index;
802
803 /* Find the branch edge. It is possible that we do have fake
804 edges here. */
805 FOR_EACH_EDGE (e, ei, bb->succs)
806 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
807 break;
808
809 prob = e->probability;
810 index = prob * 20 / REG_BR_PROB_BASE;
811
812 if (index == 20)
813 index = 19;
814 hist_br_prob[index]++;
815
816 num_branches++;
817 }
818 }
819 /* As a last resort, distribute the probabilities evenly.
820 Use simple heuristics that if there are normal edges,
821 give all abnormals frequency of 0, otherwise distribute the
822 frequency over abnormals (this is the case of noreturn
823 calls). */
824 else if (profile_status_for_fn (cfun) == PROFILE_ABSENT)
825 {
826 int total = 0;
827
828 FOR_EACH_EDGE (e, ei, bb->succs)
829 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
830 total ++;
831 if (total)
832 {
833 FOR_EACH_EDGE (e, ei, bb->succs)
834 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
835 e->probability = REG_BR_PROB_BASE / total;
836 else
837 e->probability = 0;
838 }
839 else
840 {
841 total += EDGE_COUNT (bb->succs);
842 FOR_EACH_EDGE (e, ei, bb->succs)
843 e->probability = REG_BR_PROB_BASE / total;
844 }
845 if (bb->index >= NUM_FIXED_BLOCKS
846 && block_ends_with_condjump_p (bb)
847 && EDGE_COUNT (bb->succs) >= 2)
848 num_branches++;
849 }
850 }
851 counts_to_freqs ();
852 profile_status_for_fn (cfun) = PROFILE_READ;
853 compute_function_frequency ();
854
855 if (dump_file)
856 {
857 fprintf (dump_file, "%d branches\n", num_branches);
858 if (num_branches)
859 for (i = 0; i < 10; i++)
860 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
861 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
862 5 * i, 5 * i + 5);
863
864 total_num_branches += num_branches;
865 for (i = 0; i < 20; i++)
866 total_hist_br_prob[i] += hist_br_prob[i];
867
868 fputc ('\n', dump_file);
869 fputc ('\n', dump_file);
870 }
871
872 free_aux_for_blocks ();
873 }
874
875 /* Load value histograms values whose description is stored in VALUES array
876 from .gcda file.
877
878 CFG_CHECKSUM is the precomputed checksum for the CFG. */
879
880 static void
881 compute_value_histograms (histogram_values values, unsigned cfg_checksum,
882 unsigned lineno_checksum)
883 {
884 unsigned i, j, t, any;
885 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
886 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
887 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
888 gcov_type *aact_count;
889 struct cgraph_node *node;
890
891 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
892 n_histogram_counters[t] = 0;
893
894 for (i = 0; i < values.length (); i++)
895 {
896 histogram_value hist = values[i];
897 n_histogram_counters[(int) hist->type] += hist->n_counters;
898 }
899
900 any = 0;
901 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
902 {
903 if (!n_histogram_counters[t])
904 {
905 histogram_counts[t] = NULL;
906 continue;
907 }
908
909 histogram_counts[t] =
910 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
911 n_histogram_counters[t], cfg_checksum,
912 lineno_checksum, NULL);
913 if (histogram_counts[t])
914 any = 1;
915 act_count[t] = histogram_counts[t];
916 }
917 if (!any)
918 return;
919
920 for (i = 0; i < values.length (); i++)
921 {
922 histogram_value hist = values[i];
923 gimple stmt = hist->hvalue.stmt;
924
925 t = (int) hist->type;
926
927 aact_count = act_count[t];
928
929 if (act_count[t])
930 act_count[t] += hist->n_counters;
931
932 gimple_add_histogram_value (cfun, stmt, hist);
933 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
934 for (j = 0; j < hist->n_counters; j++)
935 if (aact_count)
936 hist->hvalue.counters[j] = aact_count[j];
937 else
938 hist->hvalue.counters[j] = 0;
939
940 /* Time profiler counter is not related to any statement,
941 so that we have to read the counter and set the value to
942 the corresponding call graph node. */
943 if (hist->type == HIST_TYPE_TIME_PROFILE)
944 {
945 node = cgraph_node::get (hist->fun->decl);
946 node->tp_first_run = hist->hvalue.counters[0];
947
948 if (dump_file)
949 fprintf (dump_file, "Read tp_first_run: %d\n", node->tp_first_run);
950 }
951 }
952
953 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
954 free (histogram_counts[t]);
955 }
956
957 /* When passed NULL as file_name, initialize.
958 When passed something else, output the necessary commands to change
959 line to LINE and offset to FILE_NAME. */
960 static void
961 output_location (char const *file_name, int line,
962 gcov_position_t *offset, basic_block bb)
963 {
964 static char const *prev_file_name;
965 static int prev_line;
966 bool name_differs, line_differs;
967
968 if (!file_name)
969 {
970 prev_file_name = NULL;
971 prev_line = -1;
972 return;
973 }
974
975 name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name);
976 line_differs = prev_line != line;
977
978 if (name_differs || line_differs)
979 {
980 if (!*offset)
981 {
982 *offset = gcov_write_tag (GCOV_TAG_LINES);
983 gcov_write_unsigned (bb->index);
984 name_differs = line_differs=true;
985 }
986
987 /* If this is a new source file, then output the
988 file's name to the .bb file. */
989 if (name_differs)
990 {
991 prev_file_name = file_name;
992 gcov_write_unsigned (0);
993 gcov_write_string (prev_file_name);
994 }
995 if (line_differs)
996 {
997 gcov_write_unsigned (line);
998 prev_line = line;
999 }
1000 }
1001 }
1002
1003 /* Instrument and/or analyze program behavior based on program the CFG.
1004
1005 This function creates a representation of the control flow graph (of
1006 the function being compiled) that is suitable for the instrumentation
1007 of edges and/or converting measured edge counts to counts on the
1008 complete CFG.
1009
1010 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
1011 the flow graph that are needed to reconstruct the dynamic behavior of the
1012 flow graph. This data is written to the gcno file for gcov.
1013
1014 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
1015 information from the gcda file containing edge count information from
1016 previous executions of the function being compiled. In this case, the
1017 control flow graph is annotated with actual execution counts by
1018 compute_branch_probabilities().
1019
1020 Main entry point of this file. */
1021
1022 void
1023 branch_prob (void)
1024 {
1025 basic_block bb;
1026 unsigned i;
1027 unsigned num_edges, ignored_edges;
1028 unsigned num_instrumented;
1029 struct edge_list *el;
1030 histogram_values values = histogram_values ();
1031 unsigned cfg_checksum, lineno_checksum;
1032
1033 total_num_times_called++;
1034
1035 flow_call_edges_add (NULL);
1036 add_noreturn_fake_exit_edges ();
1037
1038 /* We can't handle cyclic regions constructed using abnormal edges.
1039 To avoid these we replace every source of abnormal edge by a fake
1040 edge from entry node and every destination by fake edge to exit.
1041 This keeps graph acyclic and our calculation exact for all normal
1042 edges except for exit and entrance ones.
1043
1044 We also add fake exit edges for each call and asm statement in the
1045 basic, since it may not return. */
1046
1047 FOR_EACH_BB_FN (bb, cfun)
1048 {
1049 int need_exit_edge = 0, need_entry_edge = 0;
1050 int have_exit_edge = 0, have_entry_edge = 0;
1051 edge e;
1052 edge_iterator ei;
1053
1054 /* Functions returning multiple times are not handled by extra edges.
1055 Instead we simply allow negative counts on edges from exit to the
1056 block past call and corresponding probabilities. We can't go
1057 with the extra edges because that would result in flowgraph that
1058 needs to have fake edges outside the spanning tree. */
1059
1060 FOR_EACH_EDGE (e, ei, bb->succs)
1061 {
1062 gimple_stmt_iterator gsi;
1063 gimple last = NULL;
1064
1065 /* It may happen that there are compiler generated statements
1066 without a locus at all. Go through the basic block from the
1067 last to the first statement looking for a locus. */
1068 for (gsi = gsi_last_nondebug_bb (bb);
1069 !gsi_end_p (gsi);
1070 gsi_prev_nondebug (&gsi))
1071 {
1072 last = gsi_stmt (gsi);
1073 if (gimple_has_location (last))
1074 break;
1075 }
1076
1077 /* Edge with goto locus might get wrong coverage info unless
1078 it is the only edge out of BB.
1079 Don't do that when the locuses match, so
1080 if (blah) goto something;
1081 is not computed twice. */
1082 if (last
1083 && gimple_has_location (last)
1084 && LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
1085 && !single_succ_p (bb)
1086 && (LOCATION_FILE (e->goto_locus)
1087 != LOCATION_FILE (gimple_location (last))
1088 || (LOCATION_LINE (e->goto_locus)
1089 != LOCATION_LINE (gimple_location (last)))))
1090 {
1091 basic_block new_bb = split_edge (e);
1092 edge ne = single_succ_edge (new_bb);
1093 ne->goto_locus = e->goto_locus;
1094 }
1095 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1096 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1097 need_exit_edge = 1;
1098 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1099 have_exit_edge = 1;
1100 }
1101 FOR_EACH_EDGE (e, ei, bb->preds)
1102 {
1103 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1104 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1105 need_entry_edge = 1;
1106 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1107 have_entry_edge = 1;
1108 }
1109
1110 if (need_exit_edge && !have_exit_edge)
1111 {
1112 if (dump_file)
1113 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
1114 bb->index);
1115 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
1116 }
1117 if (need_entry_edge && !have_entry_edge)
1118 {
1119 if (dump_file)
1120 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
1121 bb->index);
1122 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FAKE);
1123 /* Avoid bbs that have both fake entry edge and also some
1124 exit edge. One of those edges wouldn't be added to the
1125 spanning tree, but we can't instrument any of them. */
1126 if (have_exit_edge || need_exit_edge)
1127 {
1128 gimple_stmt_iterator gsi;
1129 gimple first;
1130
1131 gsi = gsi_start_nondebug_after_labels_bb (bb);
1132 gcc_checking_assert (!gsi_end_p (gsi));
1133 first = gsi_stmt (gsi);
1134 /* Don't split the bbs containing __builtin_setjmp_receiver
1135 or ABNORMAL_DISPATCHER calls. These are very
1136 special and don't expect anything to be inserted before
1137 them. */
1138 if (is_gimple_call (first)
1139 && (gimple_call_builtin_p (first, BUILT_IN_SETJMP_RECEIVER)
1140 || (gimple_call_flags (first) & ECF_RETURNS_TWICE)
1141 || (gimple_call_internal_p (first)
1142 && (gimple_call_internal_fn (first)
1143 == IFN_ABNORMAL_DISPATCHER))))
1144 continue;
1145
1146 if (dump_file)
1147 fprintf (dump_file, "Splitting bb %i after labels\n",
1148 bb->index);
1149 split_block_after_labels (bb);
1150 }
1151 }
1152 }
1153
1154 el = create_edge_list ();
1155 num_edges = NUM_EDGES (el);
1156 alloc_aux_for_edges (sizeof (struct edge_profile_info));
1157
1158 /* The basic blocks are expected to be numbered sequentially. */
1159 compact_blocks ();
1160
1161 ignored_edges = 0;
1162 for (i = 0 ; i < num_edges ; i++)
1163 {
1164 edge e = INDEX_EDGE (el, i);
1165 e->count = 0;
1166
1167 /* Mark edges we've replaced by fake edges above as ignored. */
1168 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1169 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1170 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1171 {
1172 EDGE_INFO (e)->ignore = 1;
1173 ignored_edges++;
1174 }
1175 }
1176
1177 /* Create spanning tree from basic block graph, mark each edge that is
1178 on the spanning tree. We insert as many abnormal and critical edges
1179 as possible to minimize number of edge splits necessary. */
1180
1181 find_spanning_tree (el);
1182
1183 /* Fake edges that are not on the tree will not be instrumented, so
1184 mark them ignored. */
1185 for (num_instrumented = i = 0; i < num_edges; i++)
1186 {
1187 edge e = INDEX_EDGE (el, i);
1188 struct edge_profile_info *inf = EDGE_INFO (e);
1189
1190 if (inf->ignore || inf->on_tree)
1191 /*NOP*/;
1192 else if (e->flags & EDGE_FAKE)
1193 {
1194 inf->ignore = 1;
1195 ignored_edges++;
1196 }
1197 else
1198 num_instrumented++;
1199 }
1200
1201 total_num_blocks += n_basic_blocks_for_fn (cfun);
1202 if (dump_file)
1203 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks_for_fn (cfun));
1204
1205 total_num_edges += num_edges;
1206 if (dump_file)
1207 fprintf (dump_file, "%d edges\n", num_edges);
1208
1209 total_num_edges_ignored += ignored_edges;
1210 if (dump_file)
1211 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
1212
1213 total_num_edges_instrumented += num_instrumented;
1214 if (dump_file)
1215 fprintf (dump_file, "%d instrumentation edges\n", num_instrumented);
1216
1217 /* Compute two different checksums. Note that we want to compute
1218 the checksum in only once place, since it depends on the shape
1219 of the control flow which can change during
1220 various transformations. */
1221 cfg_checksum = coverage_compute_cfg_checksum (cfun);
1222 lineno_checksum = coverage_compute_lineno_checksum ();
1223
1224 /* Write the data from which gcov can reconstruct the basic block
1225 graph and function line numbers (the gcno file). */
1226 if (coverage_begin_function (lineno_checksum, cfg_checksum))
1227 {
1228 gcov_position_t offset;
1229
1230 /* Basic block flags */
1231 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
1232 for (i = 0; i != (unsigned) (n_basic_blocks_for_fn (cfun)); i++)
1233 gcov_write_unsigned (0);
1234 gcov_write_length (offset);
1235
1236 /* Arcs */
1237 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
1238 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1239 {
1240 edge e;
1241 edge_iterator ei;
1242
1243 offset = gcov_write_tag (GCOV_TAG_ARCS);
1244 gcov_write_unsigned (bb->index);
1245
1246 FOR_EACH_EDGE (e, ei, bb->succs)
1247 {
1248 struct edge_profile_info *i = EDGE_INFO (e);
1249 if (!i->ignore)
1250 {
1251 unsigned flag_bits = 0;
1252
1253 if (i->on_tree)
1254 flag_bits |= GCOV_ARC_ON_TREE;
1255 if (e->flags & EDGE_FAKE)
1256 flag_bits |= GCOV_ARC_FAKE;
1257 if (e->flags & EDGE_FALLTHRU)
1258 flag_bits |= GCOV_ARC_FALLTHROUGH;
1259 /* On trees we don't have fallthru flags, but we can
1260 recompute them from CFG shape. */
1261 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
1262 && e->src->next_bb == e->dest)
1263 flag_bits |= GCOV_ARC_FALLTHROUGH;
1264
1265 gcov_write_unsigned (e->dest->index);
1266 gcov_write_unsigned (flag_bits);
1267 }
1268 }
1269
1270 gcov_write_length (offset);
1271 }
1272
1273 /* Line numbers. */
1274 /* Initialize the output. */
1275 output_location (NULL, 0, NULL, NULL);
1276
1277 FOR_EACH_BB_FN (bb, cfun)
1278 {
1279 gimple_stmt_iterator gsi;
1280 gcov_position_t offset = 0;
1281
1282 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
1283 {
1284 expanded_location curr_location =
1285 expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1286 output_location (curr_location.file, curr_location.line,
1287 &offset, bb);
1288 }
1289
1290 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1291 {
1292 gimple stmt = gsi_stmt (gsi);
1293 if (gimple_has_location (stmt))
1294 output_location (gimple_filename (stmt), gimple_lineno (stmt),
1295 &offset, bb);
1296 }
1297
1298 /* Notice GOTO expressions eliminated while constructing the CFG. */
1299 if (single_succ_p (bb)
1300 && LOCATION_LOCUS (single_succ_edge (bb)->goto_locus)
1301 != UNKNOWN_LOCATION)
1302 {
1303 expanded_location curr_location
1304 = expand_location (single_succ_edge (bb)->goto_locus);
1305 output_location (curr_location.file, curr_location.line,
1306 &offset, bb);
1307 }
1308
1309 if (offset)
1310 {
1311 /* A file of NULL indicates the end of run. */
1312 gcov_write_unsigned (0);
1313 gcov_write_string (NULL);
1314 gcov_write_length (offset);
1315 }
1316 }
1317 }
1318
1319 if (flag_profile_values)
1320 gimple_find_values_to_profile (&values);
1321
1322 if (flag_branch_probabilities)
1323 {
1324 compute_branch_probabilities (cfg_checksum, lineno_checksum);
1325 if (flag_profile_values)
1326 compute_value_histograms (values, cfg_checksum, lineno_checksum);
1327 }
1328
1329 remove_fake_edges ();
1330
1331 /* For each edge not on the spanning tree, add counting code. */
1332 if (profile_arc_flag
1333 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1334 {
1335 unsigned n_instrumented;
1336
1337 gimple_init_edge_profiler ();
1338
1339 n_instrumented = instrument_edges (el);
1340
1341 gcc_assert (n_instrumented == num_instrumented);
1342
1343 if (flag_profile_values)
1344 instrument_values (values);
1345
1346 /* Commit changes done by instrumentation. */
1347 gsi_commit_edge_inserts ();
1348 }
1349
1350 free_aux_for_edges ();
1351
1352 values.release ();
1353 free_edge_list (el);
1354 coverage_end_function (lineno_checksum, cfg_checksum);
1355 }
1356 \f
1357 /* Union find algorithm implementation for the basic blocks using
1358 aux fields. */
1359
1360 static basic_block
1361 find_group (basic_block bb)
1362 {
1363 basic_block group = bb, bb1;
1364
1365 while ((basic_block) group->aux != group)
1366 group = (basic_block) group->aux;
1367
1368 /* Compress path. */
1369 while ((basic_block) bb->aux != group)
1370 {
1371 bb1 = (basic_block) bb->aux;
1372 bb->aux = (void *) group;
1373 bb = bb1;
1374 }
1375 return group;
1376 }
1377
1378 static void
1379 union_groups (basic_block bb1, basic_block bb2)
1380 {
1381 basic_block bb1g = find_group (bb1);
1382 basic_block bb2g = find_group (bb2);
1383
1384 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1385 this code is unlikely going to be performance problem anyway. */
1386 gcc_assert (bb1g != bb2g);
1387
1388 bb1g->aux = bb2g;
1389 }
1390 \f
1391 /* This function searches all of the edges in the program flow graph, and puts
1392 as many bad edges as possible onto the spanning tree. Bad edges include
1393 abnormals edges, which can't be instrumented at the moment. Since it is
1394 possible for fake edges to form a cycle, we will have to develop some
1395 better way in the future. Also put critical edges to the tree, since they
1396 are more expensive to instrument. */
1397
1398 static void
1399 find_spanning_tree (struct edge_list *el)
1400 {
1401 int i;
1402 int num_edges = NUM_EDGES (el);
1403 basic_block bb;
1404
1405 /* We use aux field for standard union-find algorithm. */
1406 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
1407 bb->aux = bb;
1408
1409 /* Add fake edge exit to entry we can't instrument. */
1410 union_groups (EXIT_BLOCK_PTR_FOR_FN (cfun), ENTRY_BLOCK_PTR_FOR_FN (cfun));
1411
1412 /* First add all abnormal edges to the tree unless they form a cycle. Also
1413 add all edges to the exit block to avoid inserting profiling code behind
1414 setting return value from function. */
1415 for (i = 0; i < num_edges; i++)
1416 {
1417 edge e = INDEX_EDGE (el, i);
1418 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1419 || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1420 && !EDGE_INFO (e)->ignore
1421 && (find_group (e->src) != find_group (e->dest)))
1422 {
1423 if (dump_file)
1424 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1425 e->src->index, e->dest->index);
1426 EDGE_INFO (e)->on_tree = 1;
1427 union_groups (e->src, e->dest);
1428 }
1429 }
1430
1431 /* Now insert all critical edges to the tree unless they form a cycle. */
1432 for (i = 0; i < num_edges; i++)
1433 {
1434 edge e = INDEX_EDGE (el, i);
1435 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1436 && find_group (e->src) != find_group (e->dest))
1437 {
1438 if (dump_file)
1439 fprintf (dump_file, "Critical edge %d to %d put to tree\n",
1440 e->src->index, e->dest->index);
1441 EDGE_INFO (e)->on_tree = 1;
1442 union_groups (e->src, e->dest);
1443 }
1444 }
1445
1446 /* And now the rest. */
1447 for (i = 0; i < num_edges; i++)
1448 {
1449 edge e = INDEX_EDGE (el, i);
1450 if (!EDGE_INFO (e)->ignore
1451 && find_group (e->src) != find_group (e->dest))
1452 {
1453 if (dump_file)
1454 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1455 e->src->index, e->dest->index);
1456 EDGE_INFO (e)->on_tree = 1;
1457 union_groups (e->src, e->dest);
1458 }
1459 }
1460
1461 clear_aux_for_blocks ();
1462 }
1463 \f
1464 /* Perform file-level initialization for branch-prob processing. */
1465
1466 void
1467 init_branch_prob (void)
1468 {
1469 int i;
1470
1471 total_num_blocks = 0;
1472 total_num_edges = 0;
1473 total_num_edges_ignored = 0;
1474 total_num_edges_instrumented = 0;
1475 total_num_blocks_created = 0;
1476 total_num_passes = 0;
1477 total_num_times_called = 0;
1478 total_num_branches = 0;
1479 for (i = 0; i < 20; i++)
1480 total_hist_br_prob[i] = 0;
1481 }
1482
1483 /* Performs file-level cleanup after branch-prob processing
1484 is completed. */
1485
1486 void
1487 end_branch_prob (void)
1488 {
1489 if (dump_file)
1490 {
1491 fprintf (dump_file, "\n");
1492 fprintf (dump_file, "Total number of blocks: %d\n",
1493 total_num_blocks);
1494 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1495 fprintf (dump_file, "Total number of ignored edges: %d\n",
1496 total_num_edges_ignored);
1497 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1498 total_num_edges_instrumented);
1499 fprintf (dump_file, "Total number of blocks created: %d\n",
1500 total_num_blocks_created);
1501 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1502 total_num_passes);
1503 if (total_num_times_called != 0)
1504 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1505 (total_num_passes + (total_num_times_called >> 1))
1506 / total_num_times_called);
1507 fprintf (dump_file, "Total number of branches: %d\n",
1508 total_num_branches);
1509 if (total_num_branches)
1510 {
1511 int i;
1512
1513 for (i = 0; i < 10; i++)
1514 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1515 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1516 / total_num_branches, 5*i, 5*i+5);
1517 }
1518 }
1519 }