Index: params.def
===================================================================
--- params.def  (revision 203152)
+++ params.def  (working copy)
@@ -373,6 +373,12 @@ DEFPARAM(HOT_BB_FREQUENCY_FRACTION,
         "Select fraction of the maximal frequency of executions of
basic block in function given basic block needs to have to be
considered hot",
         1000, 0, 0)

+DEFPARAM(PARAM_MIN_HOT_RUN_RATIO,
+        "min-hot-run-ratio",
+         "The minimum ratio of profile runs to basic block execution count "
+         "for the block to be considered hot",
+        4, 0, 10000)
+
 DEFPARAM (PARAM_ALIGN_THRESHOLD,
          "align-threshold",
          "Select fraction of the maximal frequency of executions of
basic block in function given basic block get alignment",
Index: predict.c
===================================================================
--- predict.c   (revision 203152)
+++ predict.c   (working copy)
@@ -237,17 +237,31 @@ probably_never_executed (struct function *fun,
   gcc_checking_assert (fun);
   if (profile_status_for_function (fun) == PROFILE_READ)
     {
-      if ((count * 4 + profile_info->runs / 2) / profile_info->runs > 0)
+      int min_run_ratio = PARAM_VALUE (PARAM_MIN_HOT_RUN_RATIO);
+      if (RDIV (count * min_run_ratio, profile_info->runs) > 0)
        return false;
       if (!frequency)
        return true;
       if (!ENTRY_BLOCK_PTR->frequency)
        return false;
-      if (ENTRY_BLOCK_PTR->count && ENTRY_BLOCK_PTR->count < REG_BR_PROB_BASE)
+      if (ENTRY_BLOCK_PTR->count)
        {
-         return (RDIV (frequency * ENTRY_BLOCK_PTR->count,
-                       ENTRY_BLOCK_PTR->frequency)
-                 < REG_BR_PROB_BASE / 4);
+          gcov_type scaled_count
+              = frequency * ENTRY_BLOCK_PTR->count * min_run_ratio;
+          gcov_type computed_count;
+          /* Check for overflow, in which case ENTRY_BLOCK_PTR->count should
+             be large enough to do the division first without losing much
+             precision.  */
+          if (scaled_count/frequency/min_run_ratio != ENTRY_BLOCK_PTR->count)
+            {
+              computed_count = RDIV (ENTRY_BLOCK_PTR->count,
+                                     ENTRY_BLOCK_PTR->frequency);
+              computed_count *= frequency * min_run_ratio;
+            }
+          else
+            computed_count = RDIV (scaled_count, ENTRY_BLOCK_PTR->frequency);
+          if (RDIV (computed_count * min_run_ratio, profile_info->runs) > 0)
+            return false;
        }
       return true;
     }
@@ -2388,7 +2402,7 @@ tree_estimate_probability (void)
   pointer_map_destroy (bb_predictions);
   bb_predictions = NULL;

-  estimate_bb_frequencies ();
+  compute_bb_frequencies (false);
   free_dominance_info (CDI_POST_DOMINATORS);
   remove_fake_exit_edges ();
 }
@@ -2551,7 +2565,7 @@ typedef struct edge_info_def
 #define BLOCK_INFO(B)  ((block_info) (B)->aux)
 #define EDGE_INFO(E)   ((edge_info) (E)->aux)

-/* Helper function for estimate_bb_frequencies.
+/* Helper function for compute_bb_frequencies.
    Propagate the frequencies in blocks marked in
    TOVISIT, starting in HEAD.  */

@@ -2691,10 +2705,10 @@ propagate_freq (basic_block head, bitmap tovisit)
     }
 }

-/* Estimate probabilities of loopback edges in loops at same nest level.  */
+/* Compute frequencies in loops at same nest level.  */

 static void
-estimate_loops_at_level (struct loop *first_loop)
+compute_loops_at_level (struct loop *first_loop)
 {
   struct loop *loop;

@@ -2705,7 +2719,7 @@ static void
       unsigned i;
       bitmap tovisit = BITMAP_ALLOC (NULL);

-      estimate_loops_at_level (loop->inner);
+      compute_loops_at_level (loop->inner);

       /* Find current loop back edge and mark it.  */
       e = loop_latch_edge (loop);
@@ -2723,14 +2737,14 @@ static void
 /* Propagates frequencies through structure of loops.  */

 static void
-estimate_loops (void)
+compute_loops (void)
 {
   bitmap tovisit = BITMAP_ALLOC (NULL);
   basic_block bb;

-  /* Start by estimating the frequencies in the loops.  */
+  /* Start by computing the frequencies in the loops.  */
   if (number_of_loops (cfun) > 1)
-    estimate_loops_at_level (current_loops->tree_root->inner);
+    compute_loops_at_level (current_loops->tree_root->inner);

   /* Now propagate the frequencies through all the blocks.  */
   FOR_ALL_BB (bb)
@@ -2800,15 +2814,16 @@ expensive_function_p (int threshold)
   return false;
 }

-/* Estimate basic blocks frequency by given branch probabilities.  */
+/* Compute and propagate basic block frequencies using the given branch
+   probabilities.  */

 void
-estimate_bb_frequencies (void)
+compute_bb_frequencies (bool rebuild)
 {
   basic_block bb;
   sreal freq_max;

-  if (profile_status != PROFILE_READ || !counts_to_freqs ())
+  if (rebuild || profile_status != PROFILE_READ || !counts_to_freqs ())
     {
       static int real_values_initialized = 0;

@@ -2845,9 +2860,9 @@ void
            }
        }

-      /* First compute probabilities locally for each loop from innermost
+      /* First compute frequencies locally for each loop from innermost
          to outermost to examine probabilities for back edges.  */
-      estimate_loops ();
+      compute_loops ();

       memcpy (&freq_max, &real_zero, sizeof (real_zero));
       FOR_EACH_BB (bb)
@@ -3027,18 +3042,16 @@ void
 rebuild_frequencies (void)
 {
   timevar_push (TV_REBUILD_FREQUENCIES);
-  if (profile_status == PROFILE_GUESSED)
+  if (profile_status == PROFILE_GUESSED || profile_status == PROFILE_READ)
     {
       loop_optimizer_init (0);
       add_noreturn_fake_exit_edges ();
       mark_irreducible_loops ();
       connect_infinite_loops_to_exit ();
-      estimate_bb_frequencies ();
+      compute_bb_frequencies (true);
       remove_fake_exit_edges ();
       loop_optimizer_finalize ();
     }
-  else if (profile_status == PROFILE_READ)
-    counts_to_freqs ();
   else
     gcc_unreachable ();
   timevar_pop (TV_REBUILD_FREQUENCIES);
Index: predict.h
===================================================================
--- predict.h   (revision 203152)
+++ predict.h   (working copy)
@@ -37,7 +37,7 @@ enum prediction

 extern void predict_insn_def (rtx, enum br_predictor, enum prediction);
 extern int counts_to_freqs (void);
-extern void estimate_bb_frequencies (void);
+extern void compute_bb_frequencies (bool rebuild);
 extern const char *predictor_name (enum br_predictor);
 extern tree build_predict_expr (enum br_predictor, enum prediction);
 extern void tree_estimate_probability (void);
