From 499b8079a6419bb8082de062ec30772296c6700c Mon Sep 17 00:00:00 2001
From: Jan Hubicka <jh@suse.cz>
Date: Thu, 27 Jul 2023 15:57:54 +0200
Subject: [PATCH] Fix profile_count::apply_probability

profile_count::apply_probability misses check for uninitialized probability which leads
to completely random results on applying uninitialized probability to initialized scale.
This can make difference when i.e. inlining -fno-guess-branch-probability function
to -fguess-branch-probability one.

gcc/ChangeLog:

	* profile-count.h (profile_count::apply_probability): Fix
	handling of uninitialized probabilities, optimize scaling
	by probability 1.
---
 gcc/profile-count.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/profile-count.h b/gcc/profile-count.h
index bf1136782a3b..88a6431c21a0 100644
--- a/gcc/profile-count.h
+++ b/gcc/profile-count.h
@@ -1129,11 +1129,11 @@ public:
   /* Scale counter according to PROB.  */
   profile_count apply_probability (profile_probability prob) const
     {
-      if (*this == zero ())
+      if (*this == zero () || prob == profile_probability::always ())
 	return *this;
       if (prob == profile_probability::never ())
 	return zero ();
-      if (!initialized_p ())
+      if (!initialized_p () || !prob.initialized_p ())
 	return uninitialized ();
       profile_count ret;
       uint64_t tmp;
-- 
GitLab