OSDN Git Service

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