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