OSDN Git Service

* gcc.dg/pr48235.c: Add dg-require-effective-target freorder.
[pf3gnuchains/gcc-fork.git] / gcc / profile.c
index 883515d..fd37748 100644 (file)
@@ -1,6 +1,6 @@
 /* Calculate branch probabilities, and basic block execution counts.
    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
+   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
    based on some ideas from Dain Samples of UC Berkeley.
@@ -59,7 +59,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "regs.h"
 #include "expr.h"
 #include "function.h"
-#include "toplev.h"
+#include "basic-block.h"
+#include "diagnostic-core.h"
 #include "coverage.h"
 #include "value-prof.h"
 #include "tree.h"
@@ -71,9 +72,6 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "profile.h"
 
-/* Hooks for profiling.  */
-static struct profile_hooks* profile_hooks;
-
 struct bb_info {
   unsigned int count_valid : 1;
 
@@ -100,7 +98,6 @@ static int total_num_blocks_created;
 static int total_num_passes;
 static int total_num_times_called;
 static int total_hist_br_prob[20];
-static int total_num_never_executed;
 static int total_num_branches;
 
 /* Forward declarations.  */
@@ -141,7 +138,7 @@ instrument_edges (struct edge_list *el)
                fprintf (dump_file, "Edge %d to %d instrumented%s\n",
                         e->src->index, e->dest->index,
                         EDGE_CRITICAL_P (e) ? " (and split)" : "");
-             (profile_hooks->gen_edge_profiler) (num_instr_edges++, e);
+             gimple_gen_edge_profiler (num_instr_edges++, e);
            }
        }
     }
@@ -202,31 +199,31 @@ instrument_values (histogram_values values)
       switch (hist->type)
        {
        case HIST_TYPE_INTERVAL:
-         (profile_hooks->gen_interval_profiler) (hist, t, 0);
+         gimple_gen_interval_profiler (hist, t, 0);
          break;
 
        case HIST_TYPE_POW2:
-         (profile_hooks->gen_pow2_profiler) (hist, t, 0);
+         gimple_gen_pow2_profiler (hist, t, 0);
          break;
 
        case HIST_TYPE_SINGLE_VALUE:
-         (profile_hooks->gen_one_value_profiler) (hist, t, 0);
+         gimple_gen_one_value_profiler (hist, t, 0);
          break;
 
        case HIST_TYPE_CONST_DELTA:
-         (profile_hooks->gen_const_delta_profiler) (hist, t, 0);
+         gimple_gen_const_delta_profiler (hist, t, 0);
          break;
 
        case HIST_TYPE_INDIR_CALL:
-         (profile_hooks->gen_ic_profiler) (hist, t, 0);
+         gimple_gen_ic_profiler (hist, t, 0);
          break;
 
        case HIST_TYPE_AVERAGE:
-         (profile_hooks->gen_average_profiler) (hist, t, 0);
+         gimple_gen_average_profiler (hist, t, 0);
          break;
 
        case HIST_TYPE_IOR:
-         (profile_hooks->gen_ior_profiler) (hist, t, 0);
+         gimple_gen_ior_profiler (hist, t, 0);
          break;
 
        default:
@@ -277,8 +274,20 @@ is_edge_inconsistent (VEC(edge,gc) *edges)
     {
       if (!EDGE_INFO (e)->ignore)
         {
-          if (e->count < 0)
-            return true;
+          if (e->count < 0
+             && (!(e->flags & EDGE_FAKE)
+                 || !block_ends_with_call_p (e->src)))
+           {
+             if (dump_file)
+               {
+                 fprintf (dump_file,
+                          "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC,
+                          e->src->index, e->dest->index, e->count);
+                 dump_bb (e->src, dump_file, 0);
+                 dump_bb (e->dest, dump_file, 0);
+               }
+              return true;
+           }
         }
     }
   return false;
@@ -307,20 +316,59 @@ static bool
 is_inconsistent (void)
 {
   basic_block bb;
+  bool inconsistent = false;
   FOR_EACH_BB (bb)
     {
-      if (is_edge_inconsistent (bb->preds))
-        return true;
-      if (is_edge_inconsistent (bb->succs))
-        return true;
-      if ( bb->count != sum_edge_counts (bb->preds)
-         || (bb->count != sum_edge_counts (bb->succs) &&
-             !(find_edge (bb, EXIT_BLOCK_PTR) != NULL &&
-               block_ends_with_call_p (bb))))
-        return true;
+      inconsistent |= is_edge_inconsistent (bb->preds);
+      if (!dump_file && inconsistent)
+       return true;
+      inconsistent |= is_edge_inconsistent (bb->succs);
+      if (!dump_file && inconsistent)
+       return true;
+      if (bb->count < 0)
+        {
+         if (dump_file)
+           {
+             fprintf (dump_file, "BB %i count is negative "
+                      HOST_WIDEST_INT_PRINT_DEC,
+                      bb->index,
+                      bb->count);
+             dump_bb (bb, dump_file, 0);
+           }
+         inconsistent = true;
+       }
+      if (bb->count != sum_edge_counts (bb->preds))
+        {
+         if (dump_file)
+           {
+             fprintf (dump_file, "BB %i count does not match sum of incoming edges "
+                      HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
+                      bb->index,
+                      bb->count,
+                      sum_edge_counts (bb->preds));
+             dump_bb (bb, dump_file, 0);
+           }
+         inconsistent = true;
+       }
+      if (bb->count != sum_edge_counts (bb->succs) &&
+          ! (find_edge (bb, EXIT_BLOCK_PTR) != NULL && block_ends_with_call_p (bb)))
+       {
+         if (dump_file)
+           {
+             fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
+                      HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
+                      bb->index,
+                      bb->count,
+                      sum_edge_counts (bb->succs));
+             dump_bb (bb, dump_file, 0);
+           }
+         inconsistent = true;
+       }
+      if (!dump_file && inconsistent)
+       return true;
     }
 
-  return false;
+  return inconsistent;
 }
 
 /* Set each basic block count to the sum of its outgoing edge counts */
@@ -361,8 +409,17 @@ read_profile_edge_counts (gcov_type *exec_counts)
                e->count = exec_counts[exec_counts_pos++];
                if (e->count > profile_info->sum_max)
                  {
-                   error ("corrupted profile info: edge from %i to %i exceeds maximal count",
-                          bb->index, e->dest->index);
+                   if (flag_profile_correction)
+                     {
+                       static bool informed = 0;
+                       if (!informed)
+                         inform (input_location,
+                                 "corrupted profile info: edge count exceeds maximal count");
+                       informed = 1;
+                     }
+                   else
+                     error ("corrupted profile info: edge from %i to %i exceeds maximal count",
+                            bb->index, e->dest->index);
                  }
              }
            else
@@ -396,7 +453,6 @@ compute_branch_probabilities (void)
   int changes;
   int passes;
   int hist_br_prob[20];
-  int num_never_executed;
   int num_branches;
   gcov_type *exec_counts = get_exec_counts ();
   int inconsistent = 0;
@@ -596,7 +652,6 @@ compute_branch_probabilities (void)
 
   for (i = 0; i < 20; i++)
     hist_br_prob[i] = 0;
-  num_never_executed = 0;
   num_branches = 0;
 
   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
@@ -690,7 +745,7 @@ compute_branch_probabilities (void)
          if (bb->index >= NUM_FIXED_BLOCKS
              && block_ends_with_condjump_p (bb)
              && EDGE_COUNT (bb->succs) >= 2)
-           num_branches++, num_never_executed;
+           num_branches++;
        }
     }
   counts_to_freqs ();
@@ -699,8 +754,6 @@ compute_branch_probabilities (void)
   if (dump_file)
     {
       fprintf (dump_file, "%d branches\n", num_branches);
-      fprintf (dump_file, "%d branches never executed\n",
-              num_never_executed);
       if (num_branches)
        for (i = 0; i < 10; i++)
          fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
@@ -708,7 +761,6 @@ compute_branch_probabilities (void)
                   5 * i, 5 * i + 5);
 
       total_num_branches += num_branches;
-      total_num_never_executed += num_never_executed;
       for (i = 0; i < 20; i++)
        total_hist_br_prob[i] += hist_br_prob[i];
 
@@ -730,7 +782,7 @@ compute_value_histograms (histogram_values values)
   gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
   gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
   gcov_type *aact_count;
+
   for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
     n_histogram_counters[t] = 0;
 
@@ -801,7 +853,7 @@ output_location (char const *file_name, int line,
       return;
     }
 
-  name_differs = !prev_file_name || strcmp (file_name, prev_file_name);
+  name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name);
   line_differs = prev_line != line;
 
   if (name_differs || line_differs)
@@ -890,7 +942,9 @@ branch_prob (void)
          /* It may happen that there are compiler generated statements
             without a locus at all.  Go through the basic block from the
             last to the first statement looking for a locus.  */
-         for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
+         for (gsi = gsi_last_nondebug_bb (bb);
+              !gsi_end_p (gsi);
+              gsi_prev_nondebug (&gsi))
            {
              last = gsi_stmt (gsi);
              if (gimple_has_location (last))
@@ -898,8 +952,8 @@ branch_prob (void)
            }
 
          /* Edge with goto locus might get wrong coverage info unless
-            it is the only edge out of BB.   
-            Don't do that when the locuses match, so 
+            it is the only edge out of BB.
+            Don't do that when the locuses match, so
             if (blah) goto something;
             is not computed twice.  */
          if (last
@@ -909,10 +963,12 @@ branch_prob (void)
              && (LOCATION_FILE (e->goto_locus)
                  != LOCATION_FILE (gimple_location (last))
                  || (LOCATION_LINE (e->goto_locus)
-                     != LOCATION_LINE (gimple_location  (last)))))
+                     != LOCATION_LINE (gimple_location (last)))))
            {
              basic_block new_bb = split_edge (e);
-             single_succ_edge (new_bb)->goto_locus = e->goto_locus;
+             edge ne = single_succ_edge (new_bb);
+             ne->goto_locus = e->goto_locus;
+             ne->goto_block = e->goto_block;
            }
          if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
               && e->dest != EXIT_BLOCK_PTR)
@@ -1067,20 +1123,17 @@ branch_prob (void)
   /* Line numbers.  */
   if (coverage_begin_output ())
     {
-      gcov_position_t offset;
-
       /* Initialize the output.  */
       output_location (NULL, 0, NULL, NULL);
 
       FOR_EACH_BB (bb)
        {
          gimple_stmt_iterator gsi;
-
-         offset = 0;
+         gcov_position_t offset = 0;
 
          if (bb == ENTRY_BLOCK_PTR->next_bb)
            {
-             expanded_location curr_location = 
+             expanded_location curr_location =
                expand_location (DECL_SOURCE_LOCATION (current_function_decl));
              output_location (curr_location.file, curr_location.line,
                               &offset, bb);
@@ -1094,15 +1147,14 @@ branch_prob (void)
                                 &offset, bb);
            }
 
-         /* Notice GOTO expressions we eliminated while constructing the
-            CFG.  */
+         /* Notice GOTO expressions eliminated while constructing the CFG.  */
          if (single_succ_p (bb)
              && single_succ_edge (bb)->goto_locus != UNKNOWN_LOCATION)
            {
-             location_t curr_location = single_succ_edge (bb)->goto_locus;
-             /* ??? The FILE/LINE API is inconsistent for these cases.  */
-             output_location (LOCATION_FILE (curr_location),
-                              LOCATION_LINE (curr_location), &offset, bb);
+             expanded_location curr_location
+               = expand_location (single_succ_edge (bb)->goto_locus);
+             output_location (curr_location.file, curr_location.line,
+                              &offset, bb);
            }
 
          if (offset)
@@ -1120,7 +1172,7 @@ branch_prob (void)
 #undef BB_TO_GCOV_INDEX
 
   if (flag_profile_values)
-    find_values_to_profile (&values);
+    gimple_find_values_to_profile (&values);
 
   if (flag_branch_probabilities)
     {
@@ -1137,7 +1189,7 @@ branch_prob (void)
     {
       unsigned n_instrumented;
 
-      profile_hooks->init_edge_profiler ();
+      gimple_init_edge_profiler ();
 
       n_instrumented = instrument_edges (el);
 
@@ -1280,7 +1332,6 @@ init_branch_prob (void)
   total_num_passes = 0;
   total_num_times_called = 0;
   total_num_branches = 0;
-  total_num_never_executed = 0;
   for (i = 0; i < 20; i++)
     total_hist_br_prob[i] = 0;
 }
@@ -1311,8 +1362,6 @@ end_branch_prob (void)
                 / total_num_times_called);
       fprintf (dump_file, "Total number of branches: %d\n",
               total_num_branches);
-      fprintf (dump_file, "Total number of branches never executed: %d\n",
-              total_num_never_executed);
       if (total_num_branches)
        {
          int i;
@@ -1325,11 +1374,3 @@ end_branch_prob (void)
     }
 }
 
-/* Set up hooks to enable tree-based profiling.  */
-
-void
-tree_register_profile_hooks (void)
-{
-  gcc_assert (current_ir_type () == IR_GIMPLE);
-  profile_hooks = &tree_profile_hooks;
-}