From 8d71d7cd025d5b0b62a8cb9fd99c38f1b37fcc7d Mon Sep 17 00:00:00 2001
From: Jan Hubicka <hubicka@ucw.cz>
Date: Fri, 16 Jun 2017 21:02:11 +0200
Subject: [PATCH] predict.c (force_edge_cold): Handle declaring edges
 impossible more aggresively.

	* predict.c (force_edge_cold): Handle declaring edges impossible
	more aggresively.

From-SVN: r249276
---
 gcc/ChangeLog |  5 +++++
 gcc/predict.c | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 44ef8579998e..b22b567358ef 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2017-06-16  Jan Hubicka  <hubicka@ucw.cz>
+
+	* predict.c (force_edge_cold): Handle declaring edges impossible
+	more aggresively.
+
 2017-06-16  Jan Hubicka  <hubicka@ucw.cz>
 
 	* tree-ssa-loop-ivcanon.c (remove_exits_and_undefined_stmts): Update
diff --git a/gcc/predict.c b/gcc/predict.c
index 1cb7dc501036..60d1a094d106 100644
--- a/gcc/predict.c
+++ b/gcc/predict.c
@@ -3968,6 +3968,7 @@ force_edge_cold (edge e, bool impossible)
   profile_count old_count = e->count;
   int old_probability = e->probability;
   int prob_scale = REG_BR_PROB_BASE;
+  bool uninitialized_exit = false;
 
   /* If edge is already improbably or cold, just return.  */
   if (e->probability <= (impossible ? PROB_VERY_UNLIKELY : 0)
@@ -3978,6 +3979,8 @@ force_edge_cold (edge e, bool impossible)
       {
 	if (e2->count.initialized_p ())
 	  count_sum += e2->count;
+	else
+	  uninitialized_exit = true;
 	prob_sum += e2->probability;
       }
 
@@ -3989,7 +3992,7 @@ force_edge_cold (edge e, bool impossible)
 	 = MIN (e->probability, impossible ? 0 : PROB_VERY_UNLIKELY);
       if (impossible)
 	e->count = profile_count::zero ();
-      if (old_probability)
+      else if (old_probability)
 	e->count = e->count.apply_scale (e->probability, old_probability);
       else
         e->count = e->count.apply_scale (1, REG_BR_PROB_BASE);
@@ -4016,6 +4019,34 @@ force_edge_cold (edge e, bool impossible)
   else
     {
       e->probability = REG_BR_PROB_BASE;
+      if (e->src->count == profile_count::zero ())
+	return;
+      if (count_sum == profile_count::zero () && !uninitialized_exit
+	  && impossible)
+	{
+	  bool found = false;
+	  for (gimple_stmt_iterator gsi = gsi_start_bb (e->src);
+	       !gsi_end_p (gsi); gsi_next (&gsi))
+	    {
+	      if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
+		{
+		  found = true;
+	          break;
+		}
+	    }
+	  if (!found)
+	    {
+	      if (dump_file && (dump_flags & TDF_DETAILS))
+		fprintf (dump_file,
+			 "Making bb %i impossible and dropping count to 0.\n",
+			 e->src->index);
+	      e->count = profile_count::zero ();
+	      e->src->count = profile_count::zero ();
+	      FOR_EACH_EDGE (e2, ei, e->src->preds)
+		force_edge_cold (e2, impossible);
+	      return;
+	    }
+	}
 
       /* If we did not adjusting, the source basic block has no likely edeges
  	 leaving other direction. In that case force that bb cold, too.
-- 
GitLab