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