From 9532b75d548aee18929396dd3f5b05ffab1f32f5 Mon Sep 17 00:00:00 2001
From: chengbin <bin.cheng@linux.alibaba.com>
Date: Thu, 16 Aug 2018 12:09:48 +0800
Subject: [PATCH 2/4] Handle ZERO profile count/prob as a general value for
 different qualities. Don't invert uninitialized profile probability.

---
 gcc/expmed.c        |  2 +-
 gcc/profile-count.h | 16 ++++++++++------
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/gcc/expmed.c b/gcc/expmed.c
index 444d6a82f98..6e95f3c76eb 100644
--- a/gcc/expmed.c
+++ b/gcc/expmed.c
@@ -6166,7 +6166,7 @@ emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
   emit_move_insn (target, trueval);
   label = gen_label_rtx ();
   do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX, NULL,
-			   label, profile_probability::uninitialized ());
+			   label, profile_probability::always ());
 
   emit_move_insn (target, falseval);
   emit_label (label);
diff --git a/gcc/profile-count.h b/gcc/profile-count.h
index f4d0c340a0a..7003d030d58 100644
--- a/gcc/profile-count.h
+++ b/gcc/profile-count.h
@@ -200,11 +200,11 @@ public:
       ret.m_quality = profile_guessed;
       return ret;
     }
-  static profile_probability always ()
+  static profile_probability always (enum profile_quality q = profile_precise)
     {
       profile_probability ret;
       ret.m_val = max_probability;
-      ret.m_quality = profile_precise;
+      ret.m_quality = q;
       return ret;
     }
   /* Probabilities which has not been initialized. Either because
@@ -284,7 +284,8 @@ public:
   /* Basic operations.  */
   bool operator== (const profile_probability &other) const
     {
-      return m_val == other.m_val && m_quality == other.m_quality;
+      return (m_val == other.m_val
+	      && (m_quality == other.m_quality || m_val == 0));
     }
   profile_probability operator+ (const profile_probability &other) const
     {
@@ -459,10 +460,12 @@ public:
       return RDIV (val * m_val, max_probability);
     }
 
-  /* Return 1-*THIS.  */
+  /* Return 1-*THIS.  It's meaningless to invert an uninitialized value.  */
   profile_probability invert () const
     {
-      return profile_probability::always() - *this;
+      if (! initialized_p ())
+	return *this;
+      return profile_probability::always(m_quality) - *this;
     }
 
   /* Return THIS with quality dropped to GUESSED.  */
@@ -754,7 +757,8 @@ public:
   /* Basic operations.  */
   bool operator== (const profile_count &other) const
     {
-      return m_val == other.m_val && m_quality == other.m_quality;
+      return (m_val == other.m_val
+	      && (m_quality == other.m_quality || m_val == 0));
     }
   profile_count operator+ (const profile_count &other) const
     {
-- 
2.14.4.44.g2045bb6

