* gcc.dg/compat/struct-layout-1_generate.c (dg_options): New. Moved
[official-gcc.git] / gcc / profile.c
blob0ece72519d91e971579a2659720e35748180e352
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
5 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
6 based on some ideas from Dain Samples of UC Berkeley.
7 Further mangling by Bob Manson, Cygnus Support.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* Generate basic block profile instrumentation and auxiliary files.
26 Profile generation is optimized, so that not all arcs in the basic
27 block graph need instrumenting. First, the BB graph is closed with
28 one entry (function start), and one exit (function exit). Any
29 ABNORMAL_EDGE cannot be instrumented (because there is no control
30 path to place the code). We close the graph by inserting fake
31 EDGE_FAKE edges to the EXIT_BLOCK, from the sources of abnormal
32 edges that do not go to the exit_block. We ignore such abnormal
33 edges. Naturally these fake edges are never directly traversed,
34 and so *cannot* be directly instrumented. Some other graph
35 massaging is done. To optimize the instrumentation we generate the
36 BB minimal span tree, only edges that are not on the span tree
37 (plus the entry point) need instrumenting. From that information
38 all other edge counts can be deduced. By construction all fake
39 edges must be on the spanning tree. We also attempt to place
40 EDGE_CRITICAL edges on the spanning tree.
42 The auxiliary files generated are <dumpbase>.gcno (at compile time)
43 and <dumpbase>.gcda (at run time). The format is
44 described in full in gcov-io.h. */
46 /* ??? Register allocation should use basic block execution counts to
47 give preference to the most commonly executed blocks. */
49 /* ??? Should calculate branch probabilities before instrumenting code, since
50 then we can use arc counts to help decide which arcs to instrument. */
52 #include "config.h"
53 #include "system.h"
54 #include "coretypes.h"
55 #include "tm.h"
56 #include "rtl.h"
57 #include "flags.h"
58 #include "output.h"
59 #include "regs.h"
60 #include "expr.h"
61 #include "function.h"
62 #include "toplev.h"
63 #include "coverage.h"
64 #include "value-prof.h"
65 #include "tree.h"
66 #include "cfghooks.h"
67 #include "tree-flow.h"
68 #include "timevar.h"
69 #include "cfgloop.h"
70 #include "tree-pass.h"
72 #include "profile.h"
74 /* Hooks for profiling. */
75 static struct profile_hooks* profile_hooks;
77 struct bb_info {
78 unsigned int count_valid : 1;
80 /* Number of successor and predecessor edges. */
81 gcov_type succ_count;
82 gcov_type pred_count;
85 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
88 /* Counter summary from the last set of coverage counts read. */
90 const struct gcov_ctr_summary *profile_info;
92 /* Collect statistics on the performance of this pass for the entire source
93 file. */
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_never_executed;
104 static int total_num_branches;
106 /* Forward declarations. */
107 static void find_spanning_tree (struct edge_list *);
108 static unsigned instrument_edges (struct edge_list *);
109 static void instrument_values (histogram_values);
110 static void compute_branch_probabilities (void);
111 static void compute_value_histograms (histogram_values);
112 static gcov_type * get_exec_counts (void);
113 static basic_block find_group (basic_block);
114 static void union_groups (basic_block, basic_block);
116 /* Add edge instrumentation code to the entire insn chain.
118 F is the first insn of the chain.
119 NUM_BLOCKS is the number of basic blocks found in F. */
121 static unsigned
122 instrument_edges (struct edge_list *el)
124 unsigned num_instr_edges = 0;
125 int num_edges = NUM_EDGES (el);
126 basic_block bb;
128 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
130 edge e;
131 edge_iterator ei;
133 FOR_EACH_EDGE (e, ei, bb->succs)
135 struct edge_info *inf = EDGE_INFO (e);
137 if (!inf->ignore && !inf->on_tree)
139 gcc_assert (!(e->flags & EDGE_ABNORMAL));
140 if (dump_file)
141 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
142 e->src->index, e->dest->index,
143 EDGE_CRITICAL_P (e) ? " (and split)" : "");
144 (profile_hooks->gen_edge_profiler) (num_instr_edges++, e);
149 total_num_blocks_created += num_edges;
150 if (dump_file)
151 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
152 return num_instr_edges;
155 /* Add code to measure histograms for values in list VALUES. */
156 static void
157 instrument_values (histogram_values values)
159 unsigned i, t;
161 /* Emit code to generate the histograms before the insns. */
163 for (i = 0; i < VEC_length (histogram_value, values); i++)
165 histogram_value hist = VEC_index (histogram_value, values, i);
166 switch (hist->type)
168 case HIST_TYPE_INTERVAL:
169 t = GCOV_COUNTER_V_INTERVAL;
170 break;
172 case HIST_TYPE_POW2:
173 t = GCOV_COUNTER_V_POW2;
174 break;
176 case HIST_TYPE_SINGLE_VALUE:
177 t = GCOV_COUNTER_V_SINGLE;
178 break;
180 case HIST_TYPE_CONST_DELTA:
181 t = GCOV_COUNTER_V_DELTA;
182 break;
184 case HIST_TYPE_INDIR_CALL:
185 t = GCOV_COUNTER_V_INDIR;
186 break;
188 case HIST_TYPE_AVERAGE:
189 t = GCOV_COUNTER_AVERAGE;
190 break;
192 case HIST_TYPE_IOR:
193 t = GCOV_COUNTER_IOR;
194 break;
196 default:
197 gcc_unreachable ();
199 if (!coverage_counter_alloc (t, hist->n_counters))
200 continue;
202 switch (hist->type)
204 case HIST_TYPE_INTERVAL:
205 (profile_hooks->gen_interval_profiler) (hist, t, 0);
206 break;
208 case HIST_TYPE_POW2:
209 (profile_hooks->gen_pow2_profiler) (hist, t, 0);
210 break;
212 case HIST_TYPE_SINGLE_VALUE:
213 (profile_hooks->gen_one_value_profiler) (hist, t, 0);
214 break;
216 case HIST_TYPE_CONST_DELTA:
217 (profile_hooks->gen_const_delta_profiler) (hist, t, 0);
218 break;
220 case HIST_TYPE_INDIR_CALL:
221 (profile_hooks->gen_ic_profiler) (hist, t, 0);
222 break;
224 case HIST_TYPE_AVERAGE:
225 (profile_hooks->gen_average_profiler) (hist, t, 0);
226 break;
228 case HIST_TYPE_IOR:
229 (profile_hooks->gen_ior_profiler) (hist, t, 0);
230 break;
232 default:
233 gcc_unreachable ();
239 /* Computes hybrid profile for all matching entries in da_file. */
241 static gcov_type *
242 get_exec_counts (void)
244 unsigned num_edges = 0;
245 basic_block bb;
246 gcov_type *counts;
248 /* Count the edges to be (possibly) instrumented. */
249 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
251 edge e;
252 edge_iterator ei;
254 FOR_EACH_EDGE (e, ei, bb->succs)
255 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
256 num_edges++;
259 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, &profile_info);
260 if (!counts)
261 return NULL;
263 if (dump_file && profile_info)
264 fprintf(dump_file, "Merged %u profiles with maximal count %u.\n",
265 profile_info->runs, (unsigned) profile_info->sum_max);
267 return counts;
271 static bool
272 is_edge_inconsistent (VEC(edge,gc) *edges)
274 edge e;
275 edge_iterator ei;
276 FOR_EACH_EDGE (e, ei, edges)
278 if (!EDGE_INFO (e)->ignore)
280 if (e->count < 0
281 && ((!e->flags & EDGE_FAKE)
282 || !block_ends_with_call_p (e->src)))
284 if (dump_file)
286 fprintf (dump_file,
287 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC,
288 e->src->index, e->dest->index, e->count);
289 dump_bb (e->src, dump_file, 0);
290 dump_bb (e->dest, dump_file, 0);
292 return true;
296 return false;
299 static void
300 correct_negative_edge_counts (void)
302 basic_block bb;
303 edge e;
304 edge_iterator ei;
306 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
308 FOR_EACH_EDGE (e, ei, bb->succs)
310 if (e->count < 0)
311 e->count = 0;
316 /* Check consistency.
317 Return true if inconsistency is found. */
318 static bool
319 is_inconsistent (void)
321 basic_block bb;
322 bool inconsistent = false;
323 FOR_EACH_BB (bb)
325 inconsistent |= is_edge_inconsistent (bb->preds);
326 if (!dump_file && inconsistent)
327 return true;
328 inconsistent |= is_edge_inconsistent (bb->succs);
329 if (!dump_file && inconsistent)
330 return true;
331 if (bb->count < 0)
333 if (dump_file)
335 fprintf (dump_file, "BB %i count is negative "
336 HOST_WIDEST_INT_PRINT_DEC,
337 bb->index,
338 bb->count);
339 dump_bb (bb, dump_file, 0);
341 inconsistent = true;
343 if (bb->count != sum_edge_counts (bb->preds))
345 if (dump_file)
347 fprintf (dump_file, "BB %i count does not match sum of incomming edges "
348 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
349 bb->index,
350 bb->count,
351 sum_edge_counts (bb->preds));
352 dump_bb (bb, dump_file, 0);
354 inconsistent = true;
356 if (bb->count != sum_edge_counts (bb->succs) &&
357 ! (find_edge (bb, EXIT_BLOCK_PTR) != NULL && block_ends_with_call_p (bb)))
359 if (dump_file)
361 fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
362 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
363 bb->index,
364 bb->count,
365 sum_edge_counts (bb->succs));
366 dump_bb (bb, dump_file, 0);
368 inconsistent = true;
370 if (!dump_file && inconsistent)
371 return true;
374 return inconsistent;
377 /* Set each basic block count to the sum of its outgoing edge counts */
378 static void
379 set_bb_counts (void)
381 basic_block bb;
382 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
384 bb->count = sum_edge_counts (bb->succs);
385 gcc_assert (bb->count >= 0);
389 /* Reads profile data and returns total number of edge counts read */
390 static int
391 read_profile_edge_counts (gcov_type *exec_counts)
393 basic_block bb;
394 int num_edges = 0;
395 int exec_counts_pos = 0;
396 /* For each edge not on the spanning tree, set its execution count from
397 the .da file. */
398 /* The first count in the .da file is the number of times that the function
399 was entered. This is the exec_count for block zero. */
401 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
403 edge e;
404 edge_iterator ei;
406 FOR_EACH_EDGE (e, ei, bb->succs)
407 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
409 num_edges++;
410 if (exec_counts)
412 e->count = exec_counts[exec_counts_pos++];
413 if (e->count > profile_info->sum_max)
415 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
416 bb->index, e->dest->index);
419 else
420 e->count = 0;
422 EDGE_INFO (e)->count_valid = 1;
423 BB_INFO (bb)->succ_count--;
424 BB_INFO (e->dest)->pred_count--;
425 if (dump_file)
427 fprintf (dump_file, "\nRead edge from %i to %i, count:",
428 bb->index, e->dest->index);
429 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
430 (HOST_WIDEST_INT) e->count);
435 return num_edges;
438 /* Compute the branch probabilities for the various branches.
439 Annotate them accordingly. */
441 static void
442 compute_branch_probabilities (void)
444 basic_block bb;
445 int i;
446 int num_edges = 0;
447 int changes;
448 int passes;
449 int hist_br_prob[20];
450 int num_never_executed;
451 int num_branches;
452 gcov_type *exec_counts = get_exec_counts ();
453 int inconsistent = 0;
455 /* Very simple sanity checks so we catch bugs in our profiling code. */
456 if (!profile_info)
457 return;
458 if (profile_info->run_max * profile_info->runs < profile_info->sum_max)
460 error ("corrupted profile info: run_max * runs < sum_max");
461 exec_counts = NULL;
464 if (profile_info->sum_all < profile_info->sum_max)
466 error ("corrupted profile info: sum_all is smaller than sum_max");
467 exec_counts = NULL;
470 /* Attach extra info block to each bb. */
471 alloc_aux_for_blocks (sizeof (struct bb_info));
472 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
474 edge e;
475 edge_iterator ei;
477 FOR_EACH_EDGE (e, ei, bb->succs)
478 if (!EDGE_INFO (e)->ignore)
479 BB_INFO (bb)->succ_count++;
480 FOR_EACH_EDGE (e, ei, bb->preds)
481 if (!EDGE_INFO (e)->ignore)
482 BB_INFO (bb)->pred_count++;
485 /* Avoid predicting entry on exit nodes. */
486 BB_INFO (EXIT_BLOCK_PTR)->succ_count = 2;
487 BB_INFO (ENTRY_BLOCK_PTR)->pred_count = 2;
489 num_edges = read_profile_edge_counts (exec_counts);
491 if (dump_file)
492 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
494 /* For every block in the file,
495 - if every exit/entrance edge has a known count, then set the block count
496 - if the block count is known, and every exit/entrance edge but one has
497 a known execution count, then set the count of the remaining edge
499 As edge counts are set, decrement the succ/pred count, but don't delete
500 the edge, that way we can easily tell when all edges are known, or only
501 one edge is unknown. */
503 /* The order that the basic blocks are iterated through is important.
504 Since the code that finds spanning trees starts with block 0, low numbered
505 edges are put on the spanning tree in preference to high numbered edges.
506 Hence, most instrumented edges are at the end. Graph solving works much
507 faster if we propagate numbers from the end to the start.
509 This takes an average of slightly more than 3 passes. */
511 changes = 1;
512 passes = 0;
513 while (changes)
515 passes++;
516 changes = 0;
517 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR, NULL, prev_bb)
519 struct bb_info *bi = BB_INFO (bb);
520 if (! bi->count_valid)
522 if (bi->succ_count == 0)
524 edge e;
525 edge_iterator ei;
526 gcov_type total = 0;
528 FOR_EACH_EDGE (e, ei, bb->succs)
529 total += e->count;
530 bb->count = total;
531 bi->count_valid = 1;
532 changes = 1;
534 else if (bi->pred_count == 0)
536 edge e;
537 edge_iterator ei;
538 gcov_type total = 0;
540 FOR_EACH_EDGE (e, ei, bb->preds)
541 total += e->count;
542 bb->count = total;
543 bi->count_valid = 1;
544 changes = 1;
547 if (bi->count_valid)
549 if (bi->succ_count == 1)
551 edge e;
552 edge_iterator ei;
553 gcov_type total = 0;
555 /* One of the counts will be invalid, but it is zero,
556 so adding it in also doesn't hurt. */
557 FOR_EACH_EDGE (e, ei, bb->succs)
558 total += e->count;
560 /* Search for the invalid edge, and set its count. */
561 FOR_EACH_EDGE (e, ei, bb->succs)
562 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
563 break;
565 /* Calculate count for remaining edge by conservation. */
566 total = bb->count - total;
568 gcc_assert (e);
569 EDGE_INFO (e)->count_valid = 1;
570 e->count = total;
571 bi->succ_count--;
573 BB_INFO (e->dest)->pred_count--;
574 changes = 1;
576 if (bi->pred_count == 1)
578 edge e;
579 edge_iterator ei;
580 gcov_type total = 0;
582 /* One of the counts will be invalid, but it is zero,
583 so adding it in also doesn't hurt. */
584 FOR_EACH_EDGE (e, ei, bb->preds)
585 total += e->count;
587 /* Search for the invalid edge, and set its count. */
588 FOR_EACH_EDGE (e, ei, bb->preds)
589 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
590 break;
592 /* Calculate count for remaining edge by conservation. */
593 total = bb->count - total + e->count;
595 gcc_assert (e);
596 EDGE_INFO (e)->count_valid = 1;
597 e->count = total;
598 bi->pred_count--;
600 BB_INFO (e->src)->succ_count--;
601 changes = 1;
606 if (dump_file)
607 dump_flow_info (dump_file, dump_flags);
609 total_num_passes += passes;
610 if (dump_file)
611 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
613 /* If the graph has been correctly solved, every block will have a
614 succ and pred count of zero. */
615 FOR_EACH_BB (bb)
617 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
620 /* Check for inconsistent basic block counts */
621 inconsistent = is_inconsistent ();
623 if (inconsistent)
625 if (flag_profile_correction)
627 /* Inconsistency detected. Make it flow-consistent. */
628 static int informed = 0;
629 if (informed == 0)
631 informed = 1;
632 inform (input_location, "correcting inconsistent profile data");
634 correct_negative_edge_counts ();
635 /* Set bb counts to the sum of the outgoing edge counts */
636 set_bb_counts ();
637 if (dump_file)
638 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n");
639 mcf_smooth_cfg ();
641 else
642 error ("corrupted profile info: profile data is not flow-consistent");
645 /* For every edge, calculate its branch probability and add a reg_note
646 to the branch insn to indicate this. */
648 for (i = 0; i < 20; i++)
649 hist_br_prob[i] = 0;
650 num_never_executed = 0;
651 num_branches = 0;
653 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
655 edge e;
656 edge_iterator ei;
658 if (bb->count < 0)
660 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
661 bb->index, (int)bb->count);
662 bb->count = 0;
664 FOR_EACH_EDGE (e, ei, bb->succs)
666 /* Function may return twice in the cased the called function is
667 setjmp or calls fork, but we can't represent this by extra
668 edge from the entry, since extra edge from the exit is
669 already present. We get negative frequency from the entry
670 point. */
671 if ((e->count < 0
672 && e->dest == EXIT_BLOCK_PTR)
673 || (e->count > bb->count
674 && e->dest != EXIT_BLOCK_PTR))
676 if (block_ends_with_call_p (bb))
677 e->count = e->count < 0 ? 0 : bb->count;
679 if (e->count < 0 || e->count > bb->count)
681 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
682 e->src->index, e->dest->index,
683 (int)e->count);
684 e->count = bb->count / 2;
687 if (bb->count)
689 FOR_EACH_EDGE (e, ei, bb->succs)
690 e->probability = (e->count * REG_BR_PROB_BASE + bb->count / 2) / bb->count;
691 if (bb->index >= NUM_FIXED_BLOCKS
692 && block_ends_with_condjump_p (bb)
693 && EDGE_COUNT (bb->succs) >= 2)
695 int prob;
696 edge e;
697 int index;
699 /* Find the branch edge. It is possible that we do have fake
700 edges here. */
701 FOR_EACH_EDGE (e, ei, bb->succs)
702 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
703 break;
705 prob = e->probability;
706 index = prob * 20 / REG_BR_PROB_BASE;
708 if (index == 20)
709 index = 19;
710 hist_br_prob[index]++;
712 num_branches++;
715 /* As a last resort, distribute the probabilities evenly.
716 Use simple heuristics that if there are normal edges,
717 give all abnormals frequency of 0, otherwise distribute the
718 frequency over abnormals (this is the case of noreturn
719 calls). */
720 else if (profile_status == PROFILE_ABSENT)
722 int total = 0;
724 FOR_EACH_EDGE (e, ei, bb->succs)
725 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
726 total ++;
727 if (total)
729 FOR_EACH_EDGE (e, ei, bb->succs)
730 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
731 e->probability = REG_BR_PROB_BASE / total;
732 else
733 e->probability = 0;
735 else
737 total += EDGE_COUNT (bb->succs);
738 FOR_EACH_EDGE (e, ei, bb->succs)
739 e->probability = REG_BR_PROB_BASE / total;
741 if (bb->index >= NUM_FIXED_BLOCKS
742 && block_ends_with_condjump_p (bb)
743 && EDGE_COUNT (bb->succs) >= 2)
744 num_branches++, num_never_executed;
747 counts_to_freqs ();
748 profile_status = PROFILE_READ;
750 if (dump_file)
752 fprintf (dump_file, "%d branches\n", num_branches);
753 fprintf (dump_file, "%d branches never executed\n",
754 num_never_executed);
755 if (num_branches)
756 for (i = 0; i < 10; i++)
757 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
758 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
759 5 * i, 5 * i + 5);
761 total_num_branches += num_branches;
762 total_num_never_executed += num_never_executed;
763 for (i = 0; i < 20; i++)
764 total_hist_br_prob[i] += hist_br_prob[i];
766 fputc ('\n', dump_file);
767 fputc ('\n', dump_file);
770 free_aux_for_blocks ();
773 /* Load value histograms values whose description is stored in VALUES array
774 from .gcda file. */
776 static void
777 compute_value_histograms (histogram_values values)
779 unsigned i, j, t, any;
780 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
781 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
782 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
783 gcov_type *aact_count;
785 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
786 n_histogram_counters[t] = 0;
788 for (i = 0; i < VEC_length (histogram_value, values); i++)
790 histogram_value hist = VEC_index (histogram_value, values, i);
791 n_histogram_counters[(int) hist->type] += hist->n_counters;
794 any = 0;
795 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
797 if (!n_histogram_counters[t])
799 histogram_counts[t] = NULL;
800 continue;
803 histogram_counts[t] =
804 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
805 n_histogram_counters[t], NULL);
806 if (histogram_counts[t])
807 any = 1;
808 act_count[t] = histogram_counts[t];
810 if (!any)
811 return;
813 for (i = 0; i < VEC_length (histogram_value, values); i++)
815 histogram_value hist = VEC_index (histogram_value, values, i);
816 gimple stmt = hist->hvalue.stmt;
818 t = (int) hist->type;
820 aact_count = act_count[t];
821 act_count[t] += hist->n_counters;
823 gimple_add_histogram_value (cfun, stmt, hist);
824 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
825 for (j = 0; j < hist->n_counters; j++)
826 hist->hvalue.counters[j] = aact_count[j];
829 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
830 if (histogram_counts[t])
831 free (histogram_counts[t]);
834 /* The entry basic block will be moved around so that it has index=1,
835 there is nothing at index 0 and the exit is at n_basic_block. */
836 #define BB_TO_GCOV_INDEX(bb) ((bb)->index - 1)
837 /* When passed NULL as file_name, initialize.
838 When passed something else, output the necessary commands to change
839 line to LINE and offset to FILE_NAME. */
840 static void
841 output_location (char const *file_name, int line,
842 gcov_position_t *offset, basic_block bb)
844 static char const *prev_file_name;
845 static int prev_line;
846 bool name_differs, line_differs;
848 if (!file_name)
850 prev_file_name = NULL;
851 prev_line = -1;
852 return;
855 name_differs = !prev_file_name || strcmp (file_name, prev_file_name);
856 line_differs = prev_line != line;
858 if (name_differs || line_differs)
860 if (!*offset)
862 *offset = gcov_write_tag (GCOV_TAG_LINES);
863 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
864 name_differs = line_differs=true;
867 /* If this is a new source file, then output the
868 file's name to the .bb file. */
869 if (name_differs)
871 prev_file_name = file_name;
872 gcov_write_unsigned (0);
873 gcov_write_string (prev_file_name);
875 if (line_differs)
877 gcov_write_unsigned (line);
878 prev_line = line;
883 /* Instrument and/or analyze program behavior based on program flow graph.
884 In either case, this function builds a flow graph for the function being
885 compiled. The flow graph is stored in BB_GRAPH.
887 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
888 the flow graph that are needed to reconstruct the dynamic behavior of the
889 flow graph.
891 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
892 information from a data file containing edge count information from previous
893 executions of the function being compiled. In this case, the flow graph is
894 annotated with actual execution counts, which are later propagated into the
895 rtl for optimization purposes.
897 Main entry point of this file. */
899 void
900 branch_prob (void)
902 basic_block bb;
903 unsigned i;
904 unsigned num_edges, ignored_edges;
905 unsigned num_instrumented;
906 struct edge_list *el;
907 histogram_values values = NULL;
909 total_num_times_called++;
911 flow_call_edges_add (NULL);
912 add_noreturn_fake_exit_edges ();
914 /* We can't handle cyclic regions constructed using abnormal edges.
915 To avoid these we replace every source of abnormal edge by a fake
916 edge from entry node and every destination by fake edge to exit.
917 This keeps graph acyclic and our calculation exact for all normal
918 edges except for exit and entrance ones.
920 We also add fake exit edges for each call and asm statement in the
921 basic, since it may not return. */
923 FOR_EACH_BB (bb)
925 int need_exit_edge = 0, need_entry_edge = 0;
926 int have_exit_edge = 0, have_entry_edge = 0;
927 edge e;
928 edge_iterator ei;
930 /* Functions returning multiple times are not handled by extra edges.
931 Instead we simply allow negative counts on edges from exit to the
932 block past call and corresponding probabilities. We can't go
933 with the extra edges because that would result in flowgraph that
934 needs to have fake edges outside the spanning tree. */
936 FOR_EACH_EDGE (e, ei, bb->succs)
938 gimple_stmt_iterator gsi;
939 gimple last = NULL;
941 /* It may happen that there are compiler generated statements
942 without a locus at all. Go through the basic block from the
943 last to the first statement looking for a locus. */
944 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
946 last = gsi_stmt (gsi);
947 if (gimple_has_location (last))
948 break;
951 /* Edge with goto locus might get wrong coverage info unless
952 it is the only edge out of BB.
953 Don't do that when the locuses match, so
954 if (blah) goto something;
955 is not computed twice. */
956 if (last
957 && gimple_has_location (last)
958 && e->goto_locus != UNKNOWN_LOCATION
959 && !single_succ_p (bb)
960 && (LOCATION_FILE (e->goto_locus)
961 != LOCATION_FILE (gimple_location (last))
962 || (LOCATION_LINE (e->goto_locus)
963 != LOCATION_LINE (gimple_location (last)))))
965 basic_block new_bb = split_edge (e);
966 single_succ_edge (new_bb)->goto_locus = e->goto_locus;
968 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
969 && e->dest != EXIT_BLOCK_PTR)
970 need_exit_edge = 1;
971 if (e->dest == EXIT_BLOCK_PTR)
972 have_exit_edge = 1;
974 FOR_EACH_EDGE (e, ei, bb->preds)
976 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
977 && e->src != ENTRY_BLOCK_PTR)
978 need_entry_edge = 1;
979 if (e->src == ENTRY_BLOCK_PTR)
980 have_entry_edge = 1;
983 if (need_exit_edge && !have_exit_edge)
985 if (dump_file)
986 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
987 bb->index);
988 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
990 if (need_entry_edge && !have_entry_edge)
992 if (dump_file)
993 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
994 bb->index);
995 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FAKE);
999 el = create_edge_list ();
1000 num_edges = NUM_EDGES (el);
1001 alloc_aux_for_edges (sizeof (struct edge_info));
1003 /* The basic blocks are expected to be numbered sequentially. */
1004 compact_blocks ();
1006 ignored_edges = 0;
1007 for (i = 0 ; i < num_edges ; i++)
1009 edge e = INDEX_EDGE (el, i);
1010 e->count = 0;
1012 /* Mark edges we've replaced by fake edges above as ignored. */
1013 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1014 && e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR)
1016 EDGE_INFO (e)->ignore = 1;
1017 ignored_edges++;
1021 /* Create spanning tree from basic block graph, mark each edge that is
1022 on the spanning tree. We insert as many abnormal and critical edges
1023 as possible to minimize number of edge splits necessary. */
1025 find_spanning_tree (el);
1027 /* Fake edges that are not on the tree will not be instrumented, so
1028 mark them ignored. */
1029 for (num_instrumented = i = 0; i < num_edges; i++)
1031 edge e = INDEX_EDGE (el, i);
1032 struct edge_info *inf = EDGE_INFO (e);
1034 if (inf->ignore || inf->on_tree)
1035 /*NOP*/;
1036 else if (e->flags & EDGE_FAKE)
1038 inf->ignore = 1;
1039 ignored_edges++;
1041 else
1042 num_instrumented++;
1045 total_num_blocks += n_basic_blocks;
1046 if (dump_file)
1047 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks);
1049 total_num_edges += num_edges;
1050 if (dump_file)
1051 fprintf (dump_file, "%d edges\n", num_edges);
1053 total_num_edges_ignored += ignored_edges;
1054 if (dump_file)
1055 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
1057 /* Write the data from which gcov can reconstruct the basic block
1058 graph. */
1060 /* Basic block flags */
1061 if (coverage_begin_output ())
1063 gcov_position_t offset;
1065 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
1066 for (i = 0; i != (unsigned) (n_basic_blocks); i++)
1067 gcov_write_unsigned (0);
1068 gcov_write_length (offset);
1071 /* Keep all basic block indexes nonnegative in the gcov output.
1072 Index 0 is used for entry block, last index is for exit block.
1074 ENTRY_BLOCK_PTR->index = 1;
1075 EXIT_BLOCK_PTR->index = last_basic_block;
1077 /* Arcs */
1078 if (coverage_begin_output ())
1080 gcov_position_t offset;
1082 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
1084 edge e;
1085 edge_iterator ei;
1087 offset = gcov_write_tag (GCOV_TAG_ARCS);
1088 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
1090 FOR_EACH_EDGE (e, ei, bb->succs)
1092 struct edge_info *i = EDGE_INFO (e);
1093 if (!i->ignore)
1095 unsigned flag_bits = 0;
1097 if (i->on_tree)
1098 flag_bits |= GCOV_ARC_ON_TREE;
1099 if (e->flags & EDGE_FAKE)
1100 flag_bits |= GCOV_ARC_FAKE;
1101 if (e->flags & EDGE_FALLTHRU)
1102 flag_bits |= GCOV_ARC_FALLTHROUGH;
1103 /* On trees we don't have fallthru flags, but we can
1104 recompute them from CFG shape. */
1105 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
1106 && e->src->next_bb == e->dest)
1107 flag_bits |= GCOV_ARC_FALLTHROUGH;
1109 gcov_write_unsigned (BB_TO_GCOV_INDEX (e->dest));
1110 gcov_write_unsigned (flag_bits);
1114 gcov_write_length (offset);
1118 /* Line numbers. */
1119 if (coverage_begin_output ())
1121 gcov_position_t offset;
1123 /* Initialize the output. */
1124 output_location (NULL, 0, NULL, NULL);
1126 FOR_EACH_BB (bb)
1128 gimple_stmt_iterator gsi;
1130 offset = 0;
1132 if (bb == ENTRY_BLOCK_PTR->next_bb)
1134 expanded_location curr_location =
1135 expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1136 output_location (curr_location.file, curr_location.line,
1137 &offset, bb);
1140 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1142 gimple stmt = gsi_stmt (gsi);
1143 if (gimple_has_location (stmt))
1144 output_location (gimple_filename (stmt), gimple_lineno (stmt),
1145 &offset, bb);
1148 /* Notice GOTO expressions we eliminated while constructing the
1149 CFG. */
1150 if (single_succ_p (bb)
1151 && single_succ_edge (bb)->goto_locus != UNKNOWN_LOCATION)
1153 location_t curr_location = single_succ_edge (bb)->goto_locus;
1154 /* ??? The FILE/LINE API is inconsistent for these cases. */
1155 output_location (LOCATION_FILE (curr_location),
1156 LOCATION_LINE (curr_location), &offset, bb);
1159 if (offset)
1161 /* A file of NULL indicates the end of run. */
1162 gcov_write_unsigned (0);
1163 gcov_write_string (NULL);
1164 gcov_write_length (offset);
1169 ENTRY_BLOCK_PTR->index = ENTRY_BLOCK;
1170 EXIT_BLOCK_PTR->index = EXIT_BLOCK;
1171 #undef BB_TO_GCOV_INDEX
1173 if (flag_profile_values)
1174 find_values_to_profile (&values);
1176 if (flag_branch_probabilities)
1178 compute_branch_probabilities ();
1179 if (flag_profile_values)
1180 compute_value_histograms (values);
1183 remove_fake_edges ();
1185 /* For each edge not on the spanning tree, add counting code. */
1186 if (profile_arc_flag
1187 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1189 unsigned n_instrumented;
1191 profile_hooks->init_edge_profiler ();
1193 n_instrumented = instrument_edges (el);
1195 gcc_assert (n_instrumented == num_instrumented);
1197 if (flag_profile_values)
1198 instrument_values (values);
1200 /* Commit changes done by instrumentation. */
1201 gsi_commit_edge_inserts ();
1204 free_aux_for_edges ();
1206 VEC_free (histogram_value, heap, values);
1207 free_edge_list (el);
1208 coverage_end_function ();
1211 /* Union find algorithm implementation for the basic blocks using
1212 aux fields. */
1214 static basic_block
1215 find_group (basic_block bb)
1217 basic_block group = bb, bb1;
1219 while ((basic_block) group->aux != group)
1220 group = (basic_block) group->aux;
1222 /* Compress path. */
1223 while ((basic_block) bb->aux != group)
1225 bb1 = (basic_block) bb->aux;
1226 bb->aux = (void *) group;
1227 bb = bb1;
1229 return group;
1232 static void
1233 union_groups (basic_block bb1, basic_block bb2)
1235 basic_block bb1g = find_group (bb1);
1236 basic_block bb2g = find_group (bb2);
1238 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1239 this code is unlikely going to be performance problem anyway. */
1240 gcc_assert (bb1g != bb2g);
1242 bb1g->aux = bb2g;
1245 /* This function searches all of the edges in the program flow graph, and puts
1246 as many bad edges as possible onto the spanning tree. Bad edges include
1247 abnormals edges, which can't be instrumented at the moment. Since it is
1248 possible for fake edges to form a cycle, we will have to develop some
1249 better way in the future. Also put critical edges to the tree, since they
1250 are more expensive to instrument. */
1252 static void
1253 find_spanning_tree (struct edge_list *el)
1255 int i;
1256 int num_edges = NUM_EDGES (el);
1257 basic_block bb;
1259 /* We use aux field for standard union-find algorithm. */
1260 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1261 bb->aux = bb;
1263 /* Add fake edge exit to entry we can't instrument. */
1264 union_groups (EXIT_BLOCK_PTR, ENTRY_BLOCK_PTR);
1266 /* First add all abnormal edges to the tree unless they form a cycle. Also
1267 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1268 setting return value from function. */
1269 for (i = 0; i < num_edges; i++)
1271 edge e = INDEX_EDGE (el, i);
1272 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1273 || e->dest == EXIT_BLOCK_PTR)
1274 && !EDGE_INFO (e)->ignore
1275 && (find_group (e->src) != find_group (e->dest)))
1277 if (dump_file)
1278 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1279 e->src->index, e->dest->index);
1280 EDGE_INFO (e)->on_tree = 1;
1281 union_groups (e->src, e->dest);
1285 /* Now insert all critical edges to the tree unless they form a cycle. */
1286 for (i = 0; i < num_edges; i++)
1288 edge e = INDEX_EDGE (el, i);
1289 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1290 && find_group (e->src) != find_group (e->dest))
1292 if (dump_file)
1293 fprintf (dump_file, "Critical edge %d to %d put to tree\n",
1294 e->src->index, e->dest->index);
1295 EDGE_INFO (e)->on_tree = 1;
1296 union_groups (e->src, e->dest);
1300 /* And now the rest. */
1301 for (i = 0; i < num_edges; i++)
1303 edge e = INDEX_EDGE (el, i);
1304 if (!EDGE_INFO (e)->ignore
1305 && find_group (e->src) != find_group (e->dest))
1307 if (dump_file)
1308 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1309 e->src->index, e->dest->index);
1310 EDGE_INFO (e)->on_tree = 1;
1311 union_groups (e->src, e->dest);
1315 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1316 bb->aux = NULL;
1319 /* Perform file-level initialization for branch-prob processing. */
1321 void
1322 init_branch_prob (void)
1324 int i;
1326 total_num_blocks = 0;
1327 total_num_edges = 0;
1328 total_num_edges_ignored = 0;
1329 total_num_edges_instrumented = 0;
1330 total_num_blocks_created = 0;
1331 total_num_passes = 0;
1332 total_num_times_called = 0;
1333 total_num_branches = 0;
1334 total_num_never_executed = 0;
1335 for (i = 0; i < 20; i++)
1336 total_hist_br_prob[i] = 0;
1339 /* Performs file-level cleanup after branch-prob processing
1340 is completed. */
1342 void
1343 end_branch_prob (void)
1345 if (dump_file)
1347 fprintf (dump_file, "\n");
1348 fprintf (dump_file, "Total number of blocks: %d\n",
1349 total_num_blocks);
1350 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1351 fprintf (dump_file, "Total number of ignored edges: %d\n",
1352 total_num_edges_ignored);
1353 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1354 total_num_edges_instrumented);
1355 fprintf (dump_file, "Total number of blocks created: %d\n",
1356 total_num_blocks_created);
1357 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1358 total_num_passes);
1359 if (total_num_times_called != 0)
1360 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1361 (total_num_passes + (total_num_times_called >> 1))
1362 / total_num_times_called);
1363 fprintf (dump_file, "Total number of branches: %d\n",
1364 total_num_branches);
1365 fprintf (dump_file, "Total number of branches never executed: %d\n",
1366 total_num_never_executed);
1367 if (total_num_branches)
1369 int i;
1371 for (i = 0; i < 10; i++)
1372 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1373 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1374 / total_num_branches, 5*i, 5*i+5);
1379 /* Set up hooks to enable tree-based profiling. */
1381 void
1382 tree_register_profile_hooks (void)
1384 gcc_assert (current_ir_type () == IR_GIMPLE);
1385 profile_hooks = &tree_profile_hooks;