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