backport: ChangeLog.tuples: ChangeLog from gimple-tuples-branch.
[gcc.git] / gcc / profile.c
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.
8
9 This file is part of GCC.
10
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.
15
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.
20
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/>. */
24
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.
41
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. */
45
46 /* ??? Register allocation should use basic block execution counts to
47 give preference to the most commonly executed blocks. */
48
49 /* ??? Should calculate branch probabilities before instrumenting code, since
50 then we can use arc counts to help decide which arcs to instrument. */
51
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"
71
72 /* Hooks for profiling. */
73 static struct profile_hooks* profile_hooks;
74
75 /* Additional information about the edges we need. */
76 struct edge_info {
77 unsigned int count_valid : 1;
78
79 /* Is on the spanning tree. */
80 unsigned int on_tree : 1;
81
82 /* Pretend this edge does not exist (it is abnormal and we've
83 inserted a fake to compensate). */
84 unsigned int ignore : 1;
85 };
86
87 struct bb_info {
88 unsigned int count_valid : 1;
89
90 /* Number of successor and predecessor edges. */
91 gcov_type succ_count;
92 gcov_type pred_count;
93 };
94
95 #define EDGE_INFO(e) ((struct edge_info *) (e)->aux)
96 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
97
98
99 /* Counter summary from the last set of coverage counts read. */
100
101 const struct gcov_ctr_summary *profile_info;
102
103 /* Collect statistics on the performance of this pass for the entire source
104 file. */
105
106 static int total_num_blocks;
107 static int total_num_edges;
108 static int total_num_edges_ignored;
109 static int total_num_edges_instrumented;
110 static int total_num_blocks_created;
111 static int total_num_passes;
112 static int total_num_times_called;
113 static int total_hist_br_prob[20];
114 static int total_num_never_executed;
115 static int total_num_branches;
116
117 /* Forward declarations. */
118 static void find_spanning_tree (struct edge_list *);
119 static unsigned instrument_edges (struct edge_list *);
120 static void instrument_values (histogram_values);
121 static void compute_branch_probabilities (void);
122 static void compute_value_histograms (histogram_values);
123 static gcov_type * get_exec_counts (void);
124 static basic_block find_group (basic_block);
125 static void union_groups (basic_block, basic_block);
126
127 \f
128 /* Add edge instrumentation code to the entire insn chain.
129
130 F is the first insn of the chain.
131 NUM_BLOCKS is the number of basic blocks found in F. */
132
133 static unsigned
134 instrument_edges (struct edge_list *el)
135 {
136 unsigned num_instr_edges = 0;
137 int num_edges = NUM_EDGES (el);
138 basic_block bb;
139
140 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
141 {
142 edge e;
143 edge_iterator ei;
144
145 FOR_EACH_EDGE (e, ei, bb->succs)
146 {
147 struct edge_info *inf = EDGE_INFO (e);
148
149 if (!inf->ignore && !inf->on_tree)
150 {
151 gcc_assert (!(e->flags & EDGE_ABNORMAL));
152 if (dump_file)
153 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
154 e->src->index, e->dest->index,
155 EDGE_CRITICAL_P (e) ? " (and split)" : "");
156 (profile_hooks->gen_edge_profiler) (num_instr_edges++, e);
157 }
158 }
159 }
160
161 total_num_blocks_created += num_edges;
162 if (dump_file)
163 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
164 return num_instr_edges;
165 }
166
167 /* Add code to measure histograms for values in list VALUES. */
168 static void
169 instrument_values (histogram_values values)
170 {
171 unsigned i, t;
172
173 /* Emit code to generate the histograms before the insns. */
174
175 for (i = 0; i < VEC_length (histogram_value, values); i++)
176 {
177 histogram_value hist = VEC_index (histogram_value, values, i);
178 switch (hist->type)
179 {
180 case HIST_TYPE_INTERVAL:
181 t = GCOV_COUNTER_V_INTERVAL;
182 break;
183
184 case HIST_TYPE_POW2:
185 t = GCOV_COUNTER_V_POW2;
186 break;
187
188 case HIST_TYPE_SINGLE_VALUE:
189 t = GCOV_COUNTER_V_SINGLE;
190 break;
191
192 case HIST_TYPE_CONST_DELTA:
193 t = GCOV_COUNTER_V_DELTA;
194 break;
195
196 case HIST_TYPE_INDIR_CALL:
197 t = GCOV_COUNTER_V_INDIR;
198 break;
199
200 case HIST_TYPE_AVERAGE:
201 t = GCOV_COUNTER_AVERAGE;
202 break;
203
204 case HIST_TYPE_IOR:
205 t = GCOV_COUNTER_IOR;
206 break;
207
208 default:
209 gcc_unreachable ();
210 }
211 if (!coverage_counter_alloc (t, hist->n_counters))
212 continue;
213
214 switch (hist->type)
215 {
216 case HIST_TYPE_INTERVAL:
217 (profile_hooks->gen_interval_profiler) (hist, t, 0);
218 break;
219
220 case HIST_TYPE_POW2:
221 (profile_hooks->gen_pow2_profiler) (hist, t, 0);
222 break;
223
224 case HIST_TYPE_SINGLE_VALUE:
225 (profile_hooks->gen_one_value_profiler) (hist, t, 0);
226 break;
227
228 case HIST_TYPE_CONST_DELTA:
229 (profile_hooks->gen_const_delta_profiler) (hist, t, 0);
230 break;
231
232 case HIST_TYPE_INDIR_CALL:
233 (profile_hooks->gen_ic_profiler) (hist, t, 0);
234 break;
235
236 case HIST_TYPE_AVERAGE:
237 (profile_hooks->gen_average_profiler) (hist, t, 0);
238 break;
239
240 case HIST_TYPE_IOR:
241 (profile_hooks->gen_ior_profiler) (hist, t, 0);
242 break;
243
244 default:
245 gcc_unreachable ();
246 }
247 }
248 }
249 \f
250
251 /* Computes hybrid profile for all matching entries in da_file. */
252
253 static gcov_type *
254 get_exec_counts (void)
255 {
256 unsigned num_edges = 0;
257 basic_block bb;
258 gcov_type *counts;
259
260 /* Count the edges to be (possibly) instrumented. */
261 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
262 {
263 edge e;
264 edge_iterator ei;
265
266 FOR_EACH_EDGE (e, ei, bb->succs)
267 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
268 num_edges++;
269 }
270
271 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, &profile_info);
272 if (!counts)
273 return NULL;
274
275 if (dump_file && profile_info)
276 fprintf(dump_file, "Merged %u profiles with maximal count %u.\n",
277 profile_info->runs, (unsigned) profile_info->sum_max);
278
279 return counts;
280 }
281 \f
282
283 /* Compute the branch probabilities for the various branches.
284 Annotate them accordingly. */
285
286 static void
287 compute_branch_probabilities (void)
288 {
289 basic_block bb;
290 int i;
291 int num_edges = 0;
292 int changes;
293 int passes;
294 int hist_br_prob[20];
295 int num_never_executed;
296 int num_branches;
297 gcov_type *exec_counts = get_exec_counts ();
298 int exec_counts_pos = 0;
299
300 /* Very simple sanity checks so we catch bugs in our profiling code. */
301 if (profile_info)
302 {
303 if (profile_info->run_max * profile_info->runs < profile_info->sum_max)
304 {
305 error ("corrupted profile info: run_max * runs < sum_max");
306 exec_counts = NULL;
307 }
308
309 if (profile_info->sum_all < profile_info->sum_max)
310 {
311 error ("corrupted profile info: sum_all is smaller than sum_max");
312 exec_counts = NULL;
313 }
314 }
315
316 /* Attach extra info block to each bb. */
317
318 alloc_aux_for_blocks (sizeof (struct bb_info));
319 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
320 {
321 edge e;
322 edge_iterator ei;
323
324 FOR_EACH_EDGE (e, ei, bb->succs)
325 if (!EDGE_INFO (e)->ignore)
326 BB_INFO (bb)->succ_count++;
327 FOR_EACH_EDGE (e, ei, bb->preds)
328 if (!EDGE_INFO (e)->ignore)
329 BB_INFO (bb)->pred_count++;
330 }
331
332 /* Avoid predicting entry on exit nodes. */
333 BB_INFO (EXIT_BLOCK_PTR)->succ_count = 2;
334 BB_INFO (ENTRY_BLOCK_PTR)->pred_count = 2;
335
336 /* For each edge not on the spanning tree, set its execution count from
337 the .da file. */
338
339 /* The first count in the .da file is the number of times that the function
340 was entered. This is the exec_count for block zero. */
341
342 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
343 {
344 edge e;
345 edge_iterator ei;
346
347 FOR_EACH_EDGE (e, ei, bb->succs)
348 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
349 {
350 num_edges++;
351 if (exec_counts)
352 {
353 e->count = exec_counts[exec_counts_pos++];
354 if (e->count > profile_info->sum_max)
355 {
356 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
357 bb->index, e->dest->index);
358 }
359 }
360 else
361 e->count = 0;
362
363 EDGE_INFO (e)->count_valid = 1;
364 BB_INFO (bb)->succ_count--;
365 BB_INFO (e->dest)->pred_count--;
366 if (dump_file)
367 {
368 fprintf (dump_file, "\nRead edge from %i to %i, count:",
369 bb->index, e->dest->index);
370 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
371 (HOST_WIDEST_INT) e->count);
372 }
373 }
374 }
375
376 if (dump_file)
377 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
378
379 /* For every block in the file,
380 - if every exit/entrance edge has a known count, then set the block count
381 - if the block count is known, and every exit/entrance edge but one has
382 a known execution count, then set the count of the remaining edge
383
384 As edge counts are set, decrement the succ/pred count, but don't delete
385 the edge, that way we can easily tell when all edges are known, or only
386 one edge is unknown. */
387
388 /* The order that the basic blocks are iterated through is important.
389 Since the code that finds spanning trees starts with block 0, low numbered
390 edges are put on the spanning tree in preference to high numbered edges.
391 Hence, most instrumented edges are at the end. Graph solving works much
392 faster if we propagate numbers from the end to the start.
393
394 This takes an average of slightly more than 3 passes. */
395
396 changes = 1;
397 passes = 0;
398 while (changes)
399 {
400 passes++;
401 changes = 0;
402 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR, NULL, prev_bb)
403 {
404 struct bb_info *bi = BB_INFO (bb);
405 if (! bi->count_valid)
406 {
407 if (bi->succ_count == 0)
408 {
409 edge e;
410 edge_iterator ei;
411 gcov_type total = 0;
412
413 FOR_EACH_EDGE (e, ei, bb->succs)
414 total += e->count;
415 bb->count = total;
416 bi->count_valid = 1;
417 changes = 1;
418 }
419 else if (bi->pred_count == 0)
420 {
421 edge e;
422 edge_iterator ei;
423 gcov_type total = 0;
424
425 FOR_EACH_EDGE (e, ei, bb->preds)
426 total += e->count;
427 bb->count = total;
428 bi->count_valid = 1;
429 changes = 1;
430 }
431 }
432 if (bi->count_valid)
433 {
434 if (bi->succ_count == 1)
435 {
436 edge e;
437 edge_iterator ei;
438 gcov_type total = 0;
439
440 /* One of the counts will be invalid, but it is zero,
441 so adding it in also doesn't hurt. */
442 FOR_EACH_EDGE (e, ei, bb->succs)
443 total += e->count;
444
445 /* Search for the invalid edge, and set its count. */
446 FOR_EACH_EDGE (e, ei, bb->succs)
447 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
448 break;
449
450 /* Calculate count for remaining edge by conservation. */
451 total = bb->count - total;
452
453 gcc_assert (e);
454 EDGE_INFO (e)->count_valid = 1;
455 e->count = total;
456 bi->succ_count--;
457
458 BB_INFO (e->dest)->pred_count--;
459 changes = 1;
460 }
461 if (bi->pred_count == 1)
462 {
463 edge e;
464 edge_iterator ei;
465 gcov_type total = 0;
466
467 /* One of the counts will be invalid, but it is zero,
468 so adding it in also doesn't hurt. */
469 FOR_EACH_EDGE (e, ei, bb->preds)
470 total += e->count;
471
472 /* Search for the invalid edge, and set its count. */
473 FOR_EACH_EDGE (e, ei, bb->preds)
474 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
475 break;
476
477 /* Calculate count for remaining edge by conservation. */
478 total = bb->count - total + e->count;
479
480 gcc_assert (e);
481 EDGE_INFO (e)->count_valid = 1;
482 e->count = total;
483 bi->pred_count--;
484
485 BB_INFO (e->src)->succ_count--;
486 changes = 1;
487 }
488 }
489 }
490 }
491 if (dump_file)
492 dump_flow_info (dump_file, dump_flags);
493
494 total_num_passes += passes;
495 if (dump_file)
496 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
497
498 /* If the graph has been correctly solved, every block will have a
499 succ and pred count of zero. */
500 FOR_EACH_BB (bb)
501 {
502 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
503 }
504
505 /* For every edge, calculate its branch probability and add a reg_note
506 to the branch insn to indicate this. */
507
508 for (i = 0; i < 20; i++)
509 hist_br_prob[i] = 0;
510 num_never_executed = 0;
511 num_branches = 0;
512
513 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
514 {
515 edge e;
516 edge_iterator ei;
517
518 if (bb->count < 0)
519 {
520 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
521 bb->index, (int)bb->count);
522 bb->count = 0;
523 }
524 FOR_EACH_EDGE (e, ei, bb->succs)
525 {
526 /* Function may return twice in the cased the called function is
527 setjmp or calls fork, but we can't represent this by extra
528 edge from the entry, since extra edge from the exit is
529 already present. We get negative frequency from the entry
530 point. */
531 if ((e->count < 0
532 && e->dest == EXIT_BLOCK_PTR)
533 || (e->count > bb->count
534 && e->dest != EXIT_BLOCK_PTR))
535 {
536 if (block_ends_with_call_p (bb))
537 e->count = e->count < 0 ? 0 : bb->count;
538 }
539 if (e->count < 0 || e->count > bb->count)
540 {
541 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
542 e->src->index, e->dest->index,
543 (int)e->count);
544 e->count = bb->count / 2;
545 }
546 }
547 if (bb->count)
548 {
549 FOR_EACH_EDGE (e, ei, bb->succs)
550 e->probability = (e->count * REG_BR_PROB_BASE + bb->count / 2) / bb->count;
551 if (bb->index >= NUM_FIXED_BLOCKS
552 && block_ends_with_condjump_p (bb)
553 && EDGE_COUNT (bb->succs) >= 2)
554 {
555 int prob;
556 edge e;
557 int index;
558
559 /* Find the branch edge. It is possible that we do have fake
560 edges here. */
561 FOR_EACH_EDGE (e, ei, bb->succs)
562 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
563 break;
564
565 prob = e->probability;
566 index = prob * 20 / REG_BR_PROB_BASE;
567
568 if (index == 20)
569 index = 19;
570 hist_br_prob[index]++;
571
572 num_branches++;
573 }
574 }
575 /* As a last resort, distribute the probabilities evenly.
576 Use simple heuristics that if there are normal edges,
577 give all abnormals frequency of 0, otherwise distribute the
578 frequency over abnormals (this is the case of noreturn
579 calls). */
580 else if (profile_status == PROFILE_ABSENT)
581 {
582 int total = 0;
583
584 FOR_EACH_EDGE (e, ei, bb->succs)
585 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
586 total ++;
587 if (total)
588 {
589 FOR_EACH_EDGE (e, ei, bb->succs)
590 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
591 e->probability = REG_BR_PROB_BASE / total;
592 else
593 e->probability = 0;
594 }
595 else
596 {
597 total += EDGE_COUNT (bb->succs);
598 FOR_EACH_EDGE (e, ei, bb->succs)
599 e->probability = REG_BR_PROB_BASE / total;
600 }
601 if (bb->index >= NUM_FIXED_BLOCKS
602 && block_ends_with_condjump_p (bb)
603 && EDGE_COUNT (bb->succs) >= 2)
604 num_branches++, num_never_executed;
605 }
606 }
607 counts_to_freqs ();
608
609 if (dump_file)
610 {
611 fprintf (dump_file, "%d branches\n", num_branches);
612 fprintf (dump_file, "%d branches never executed\n",
613 num_never_executed);
614 if (num_branches)
615 for (i = 0; i < 10; i++)
616 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
617 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
618 5 * i, 5 * i + 5);
619
620 total_num_branches += num_branches;
621 total_num_never_executed += num_never_executed;
622 for (i = 0; i < 20; i++)
623 total_hist_br_prob[i] += hist_br_prob[i];
624
625 fputc ('\n', dump_file);
626 fputc ('\n', dump_file);
627 }
628
629 free_aux_for_blocks ();
630 }
631
632 /* Load value histograms values whose description is stored in VALUES array
633 from .gcda file. */
634
635 static void
636 compute_value_histograms (histogram_values values)
637 {
638 unsigned i, j, t, any;
639 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
640 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
641 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
642 gcov_type *aact_count;
643
644 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
645 n_histogram_counters[t] = 0;
646
647 for (i = 0; i < VEC_length (histogram_value, values); i++)
648 {
649 histogram_value hist = VEC_index (histogram_value, values, i);
650 n_histogram_counters[(int) hist->type] += hist->n_counters;
651 }
652
653 any = 0;
654 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
655 {
656 if (!n_histogram_counters[t])
657 {
658 histogram_counts[t] = NULL;
659 continue;
660 }
661
662 histogram_counts[t] =
663 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
664 n_histogram_counters[t], NULL);
665 if (histogram_counts[t])
666 any = 1;
667 act_count[t] = histogram_counts[t];
668 }
669 if (!any)
670 return;
671
672 for (i = 0; i < VEC_length (histogram_value, values); i++)
673 {
674 histogram_value hist = VEC_index (histogram_value, values, i);
675 gimple stmt = hist->hvalue.stmt;
676
677 t = (int) hist->type;
678
679 aact_count = act_count[t];
680 act_count[t] += hist->n_counters;
681
682 gimple_add_histogram_value (cfun, stmt, hist);
683 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
684 for (j = 0; j < hist->n_counters; j++)
685 hist->hvalue.counters[j] = aact_count[j];
686 }
687
688 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
689 if (histogram_counts[t])
690 free (histogram_counts[t]);
691 }
692
693 /* The entry basic block will be moved around so that it has index=1,
694 there is nothing at index 0 and the exit is at n_basic_block. */
695 #define BB_TO_GCOV_INDEX(bb) ((bb)->index - 1)
696 /* When passed NULL as file_name, initialize.
697 When passed something else, output the necessary commands to change
698 line to LINE and offset to FILE_NAME. */
699 static void
700 output_location (char const *file_name, int line,
701 gcov_position_t *offset, basic_block bb)
702 {
703 static char const *prev_file_name;
704 static int prev_line;
705 bool name_differs, line_differs;
706
707 if (!file_name)
708 {
709 prev_file_name = NULL;
710 prev_line = -1;
711 return;
712 }
713
714 name_differs = !prev_file_name || strcmp (file_name, prev_file_name);
715 line_differs = prev_line != line;
716
717 if (name_differs || line_differs)
718 {
719 if (!*offset)
720 {
721 *offset = gcov_write_tag (GCOV_TAG_LINES);
722 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
723 name_differs = line_differs=true;
724 }
725
726 /* If this is a new source file, then output the
727 file's name to the .bb file. */
728 if (name_differs)
729 {
730 prev_file_name = file_name;
731 gcov_write_unsigned (0);
732 gcov_write_string (prev_file_name);
733 }
734 if (line_differs)
735 {
736 gcov_write_unsigned (line);
737 prev_line = line;
738 }
739 }
740 }
741
742 /* Instrument and/or analyze program behavior based on program flow graph.
743 In either case, this function builds a flow graph for the function being
744 compiled. The flow graph is stored in BB_GRAPH.
745
746 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
747 the flow graph that are needed to reconstruct the dynamic behavior of the
748 flow graph.
749
750 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
751 information from a data file containing edge count information from previous
752 executions of the function being compiled. In this case, the flow graph is
753 annotated with actual execution counts, which are later propagated into the
754 rtl for optimization purposes.
755
756 Main entry point of this file. */
757
758 void
759 branch_prob (void)
760 {
761 basic_block bb;
762 unsigned i;
763 unsigned num_edges, ignored_edges;
764 unsigned num_instrumented;
765 struct edge_list *el;
766 histogram_values values = NULL;
767
768 total_num_times_called++;
769
770 flow_call_edges_add (NULL);
771 add_noreturn_fake_exit_edges ();
772
773 /* We can't handle cyclic regions constructed using abnormal edges.
774 To avoid these we replace every source of abnormal edge by a fake
775 edge from entry node and every destination by fake edge to exit.
776 This keeps graph acyclic and our calculation exact for all normal
777 edges except for exit and entrance ones.
778
779 We also add fake exit edges for each call and asm statement in the
780 basic, since it may not return. */
781
782 FOR_EACH_BB (bb)
783 {
784 int need_exit_edge = 0, need_entry_edge = 0;
785 int have_exit_edge = 0, have_entry_edge = 0;
786 edge e;
787 edge_iterator ei;
788
789 /* Functions returning multiple times are not handled by extra edges.
790 Instead we simply allow negative counts on edges from exit to the
791 block past call and corresponding probabilities. We can't go
792 with the extra edges because that would result in flowgraph that
793 needs to have fake edges outside the spanning tree. */
794
795 FOR_EACH_EDGE (e, ei, bb->succs)
796 {
797 gimple_stmt_iterator gsi;
798 gimple last = NULL;
799
800 /* It may happen that there are compiler generated statements
801 without a locus at all. Go through the basic block from the
802 last to the first statement looking for a locus. */
803 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
804 {
805 last = gsi_stmt (gsi);
806 if (gimple_has_location (last))
807 break;
808 }
809
810 /* Edge with goto locus might get wrong coverage info unless
811 it is the only edge out of BB.
812 Don't do that when the locuses match, so
813 if (blah) goto something;
814 is not computed twice. */
815 if (last
816 && gimple_has_location (last)
817 && e->goto_locus != UNKNOWN_LOCATION
818 && !single_succ_p (bb)
819 && (LOCATION_FILE (e->goto_locus)
820 != LOCATION_FILE (gimple_location (last))
821 || (LOCATION_LINE (e->goto_locus)
822 != LOCATION_LINE (gimple_location (last)))))
823 {
824 basic_block new = split_edge (e);
825 single_succ_edge (new)->goto_locus = e->goto_locus;
826 }
827 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
828 && e->dest != EXIT_BLOCK_PTR)
829 need_exit_edge = 1;
830 if (e->dest == EXIT_BLOCK_PTR)
831 have_exit_edge = 1;
832 }
833 FOR_EACH_EDGE (e, ei, bb->preds)
834 {
835 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
836 && e->src != ENTRY_BLOCK_PTR)
837 need_entry_edge = 1;
838 if (e->src == ENTRY_BLOCK_PTR)
839 have_entry_edge = 1;
840 }
841
842 if (need_exit_edge && !have_exit_edge)
843 {
844 if (dump_file)
845 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
846 bb->index);
847 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
848 }
849 if (need_entry_edge && !have_entry_edge)
850 {
851 if (dump_file)
852 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
853 bb->index);
854 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FAKE);
855 }
856 }
857
858 el = create_edge_list ();
859 num_edges = NUM_EDGES (el);
860 alloc_aux_for_edges (sizeof (struct edge_info));
861
862 /* The basic blocks are expected to be numbered sequentially. */
863 compact_blocks ();
864
865 ignored_edges = 0;
866 for (i = 0 ; i < num_edges ; i++)
867 {
868 edge e = INDEX_EDGE (el, i);
869 e->count = 0;
870
871 /* Mark edges we've replaced by fake edges above as ignored. */
872 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
873 && e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR)
874 {
875 EDGE_INFO (e)->ignore = 1;
876 ignored_edges++;
877 }
878 }
879
880 /* Create spanning tree from basic block graph, mark each edge that is
881 on the spanning tree. We insert as many abnormal and critical edges
882 as possible to minimize number of edge splits necessary. */
883
884 find_spanning_tree (el);
885
886 /* Fake edges that are not on the tree will not be instrumented, so
887 mark them ignored. */
888 for (num_instrumented = i = 0; i < num_edges; i++)
889 {
890 edge e = INDEX_EDGE (el, i);
891 struct edge_info *inf = EDGE_INFO (e);
892
893 if (inf->ignore || inf->on_tree)
894 /*NOP*/;
895 else if (e->flags & EDGE_FAKE)
896 {
897 inf->ignore = 1;
898 ignored_edges++;
899 }
900 else
901 num_instrumented++;
902 }
903
904 total_num_blocks += n_basic_blocks;
905 if (dump_file)
906 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks);
907
908 total_num_edges += num_edges;
909 if (dump_file)
910 fprintf (dump_file, "%d edges\n", num_edges);
911
912 total_num_edges_ignored += ignored_edges;
913 if (dump_file)
914 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
915
916 /* Write the data from which gcov can reconstruct the basic block
917 graph. */
918
919 /* Basic block flags */
920 if (coverage_begin_output ())
921 {
922 gcov_position_t offset;
923
924 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
925 for (i = 0; i != (unsigned) (n_basic_blocks); i++)
926 gcov_write_unsigned (0);
927 gcov_write_length (offset);
928 }
929
930 /* Keep all basic block indexes nonnegative in the gcov output.
931 Index 0 is used for entry block, last index is for exit block.
932 */
933 ENTRY_BLOCK_PTR->index = 1;
934 EXIT_BLOCK_PTR->index = last_basic_block;
935
936 /* Arcs */
937 if (coverage_begin_output ())
938 {
939 gcov_position_t offset;
940
941 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
942 {
943 edge e;
944 edge_iterator ei;
945
946 offset = gcov_write_tag (GCOV_TAG_ARCS);
947 gcov_write_unsigned (BB_TO_GCOV_INDEX (bb));
948
949 FOR_EACH_EDGE (e, ei, bb->succs)
950 {
951 struct edge_info *i = EDGE_INFO (e);
952 if (!i->ignore)
953 {
954 unsigned flag_bits = 0;
955
956 if (i->on_tree)
957 flag_bits |= GCOV_ARC_ON_TREE;
958 if (e->flags & EDGE_FAKE)
959 flag_bits |= GCOV_ARC_FAKE;
960 if (e->flags & EDGE_FALLTHRU)
961 flag_bits |= GCOV_ARC_FALLTHROUGH;
962 /* On trees we don't have fallthru flags, but we can
963 recompute them from CFG shape. */
964 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
965 && e->src->next_bb == e->dest)
966 flag_bits |= GCOV_ARC_FALLTHROUGH;
967
968 gcov_write_unsigned (BB_TO_GCOV_INDEX (e->dest));
969 gcov_write_unsigned (flag_bits);
970 }
971 }
972
973 gcov_write_length (offset);
974 }
975 }
976
977 /* Line numbers. */
978 if (coverage_begin_output ())
979 {
980 gcov_position_t offset;
981
982 /* Initialize the output. */
983 output_location (NULL, 0, NULL, NULL);
984
985 FOR_EACH_BB (bb)
986 {
987 gimple_stmt_iterator gsi;
988
989 offset = 0;
990
991 if (bb == ENTRY_BLOCK_PTR->next_bb)
992 {
993 expanded_location curr_location =
994 expand_location (DECL_SOURCE_LOCATION (current_function_decl));
995 output_location (curr_location.file, curr_location.line,
996 &offset, bb);
997 }
998
999 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1000 {
1001 gimple stmt = gsi_stmt (gsi);
1002 if (gimple_has_location (stmt))
1003 output_location (gimple_filename (stmt), gimple_lineno (stmt),
1004 &offset, bb);
1005 }
1006
1007 /* Notice GOTO expressions we eliminated while constructing the
1008 CFG. */
1009 if (single_succ_p (bb)
1010 && single_succ_edge (bb)->goto_locus != UNKNOWN_LOCATION)
1011 {
1012 location_t curr_location = single_succ_edge (bb)->goto_locus;
1013 /* ??? The FILE/LINE API is inconsistent for these cases. */
1014 output_location (LOCATION_FILE (curr_location),
1015 LOCATION_LINE (curr_location), &offset, bb);
1016 }
1017
1018 if (offset)
1019 {
1020 /* A file of NULL indicates the end of run. */
1021 gcov_write_unsigned (0);
1022 gcov_write_string (NULL);
1023 gcov_write_length (offset);
1024 }
1025 }
1026 }
1027
1028 ENTRY_BLOCK_PTR->index = ENTRY_BLOCK;
1029 EXIT_BLOCK_PTR->index = EXIT_BLOCK;
1030 #undef BB_TO_GCOV_INDEX
1031
1032 if (flag_profile_values)
1033 find_values_to_profile (&values);
1034
1035 if (flag_branch_probabilities)
1036 {
1037 compute_branch_probabilities ();
1038 if (flag_profile_values)
1039 compute_value_histograms (values);
1040 }
1041
1042 remove_fake_edges ();
1043
1044 /* For each edge not on the spanning tree, add counting code. */
1045 if (profile_arc_flag
1046 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1047 {
1048 unsigned n_instrumented;
1049
1050 profile_hooks->init_edge_profiler ();
1051
1052 n_instrumented = instrument_edges (el);
1053
1054 gcc_assert (n_instrumented == num_instrumented);
1055
1056 if (flag_profile_values)
1057 instrument_values (values);
1058
1059 /* Commit changes done by instrumentation. */
1060 gsi_commit_edge_inserts ();
1061 }
1062
1063 free_aux_for_edges ();
1064
1065 VEC_free (histogram_value, heap, values);
1066 free_edge_list (el);
1067 if (flag_branch_probabilities)
1068 profile_status = PROFILE_READ;
1069 coverage_end_function ();
1070 }
1071 \f
1072 /* Union find algorithm implementation for the basic blocks using
1073 aux fields. */
1074
1075 static basic_block
1076 find_group (basic_block bb)
1077 {
1078 basic_block group = bb, bb1;
1079
1080 while ((basic_block) group->aux != group)
1081 group = (basic_block) group->aux;
1082
1083 /* Compress path. */
1084 while ((basic_block) bb->aux != group)
1085 {
1086 bb1 = (basic_block) bb->aux;
1087 bb->aux = (void *) group;
1088 bb = bb1;
1089 }
1090 return group;
1091 }
1092
1093 static void
1094 union_groups (basic_block bb1, basic_block bb2)
1095 {
1096 basic_block bb1g = find_group (bb1);
1097 basic_block bb2g = find_group (bb2);
1098
1099 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1100 this code is unlikely going to be performance problem anyway. */
1101 gcc_assert (bb1g != bb2g);
1102
1103 bb1g->aux = bb2g;
1104 }
1105 \f
1106 /* This function searches all of the edges in the program flow graph, and puts
1107 as many bad edges as possible onto the spanning tree. Bad edges include
1108 abnormals edges, which can't be instrumented at the moment. Since it is
1109 possible for fake edges to form a cycle, we will have to develop some
1110 better way in the future. Also put critical edges to the tree, since they
1111 are more expensive to instrument. */
1112
1113 static void
1114 find_spanning_tree (struct edge_list *el)
1115 {
1116 int i;
1117 int num_edges = NUM_EDGES (el);
1118 basic_block bb;
1119
1120 /* We use aux field for standard union-find algorithm. */
1121 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1122 bb->aux = bb;
1123
1124 /* Add fake edge exit to entry we can't instrument. */
1125 union_groups (EXIT_BLOCK_PTR, ENTRY_BLOCK_PTR);
1126
1127 /* First add all abnormal edges to the tree unless they form a cycle. Also
1128 add all edges to EXIT_BLOCK_PTR to avoid inserting profiling code behind
1129 setting return value from function. */
1130 for (i = 0; i < num_edges; i++)
1131 {
1132 edge e = INDEX_EDGE (el, i);
1133 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1134 || e->dest == EXIT_BLOCK_PTR)
1135 && !EDGE_INFO (e)->ignore
1136 && (find_group (e->src) != find_group (e->dest)))
1137 {
1138 if (dump_file)
1139 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1140 e->src->index, e->dest->index);
1141 EDGE_INFO (e)->on_tree = 1;
1142 union_groups (e->src, e->dest);
1143 }
1144 }
1145
1146 /* Now insert all critical edges to the tree unless they form a cycle. */
1147 for (i = 0; i < num_edges; i++)
1148 {
1149 edge e = INDEX_EDGE (el, i);
1150 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1151 && find_group (e->src) != find_group (e->dest))
1152 {
1153 if (dump_file)
1154 fprintf (dump_file, "Critical edge %d to %d put to tree\n",
1155 e->src->index, e->dest->index);
1156 EDGE_INFO (e)->on_tree = 1;
1157 union_groups (e->src, e->dest);
1158 }
1159 }
1160
1161 /* And now the rest. */
1162 for (i = 0; i < num_edges; i++)
1163 {
1164 edge e = INDEX_EDGE (el, i);
1165 if (!EDGE_INFO (e)->ignore
1166 && find_group (e->src) != find_group (e->dest))
1167 {
1168 if (dump_file)
1169 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1170 e->src->index, e->dest->index);
1171 EDGE_INFO (e)->on_tree = 1;
1172 union_groups (e->src, e->dest);
1173 }
1174 }
1175
1176 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1177 bb->aux = NULL;
1178 }
1179 \f
1180 /* Perform file-level initialization for branch-prob processing. */
1181
1182 void
1183 init_branch_prob (void)
1184 {
1185 int i;
1186
1187 total_num_blocks = 0;
1188 total_num_edges = 0;
1189 total_num_edges_ignored = 0;
1190 total_num_edges_instrumented = 0;
1191 total_num_blocks_created = 0;
1192 total_num_passes = 0;
1193 total_num_times_called = 0;
1194 total_num_branches = 0;
1195 total_num_never_executed = 0;
1196 for (i = 0; i < 20; i++)
1197 total_hist_br_prob[i] = 0;
1198 }
1199
1200 /* Performs file-level cleanup after branch-prob processing
1201 is completed. */
1202
1203 void
1204 end_branch_prob (void)
1205 {
1206 if (dump_file)
1207 {
1208 fprintf (dump_file, "\n");
1209 fprintf (dump_file, "Total number of blocks: %d\n",
1210 total_num_blocks);
1211 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1212 fprintf (dump_file, "Total number of ignored edges: %d\n",
1213 total_num_edges_ignored);
1214 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1215 total_num_edges_instrumented);
1216 fprintf (dump_file, "Total number of blocks created: %d\n",
1217 total_num_blocks_created);
1218 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1219 total_num_passes);
1220 if (total_num_times_called != 0)
1221 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1222 (total_num_passes + (total_num_times_called >> 1))
1223 / total_num_times_called);
1224 fprintf (dump_file, "Total number of branches: %d\n",
1225 total_num_branches);
1226 fprintf (dump_file, "Total number of branches never executed: %d\n",
1227 total_num_never_executed);
1228 if (total_num_branches)
1229 {
1230 int i;
1231
1232 for (i = 0; i < 10; i++)
1233 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1234 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1235 / total_num_branches, 5*i, 5*i+5);
1236 }
1237 }
1238 }
1239
1240 /* Set up hooks to enable tree-based profiling. */
1241
1242 void
1243 tree_register_profile_hooks (void)
1244 {
1245 gcc_assert (current_ir_type () == IR_GIMPLE);
1246 profile_hooks = &tree_profile_hooks;
1247 }