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