A scaled cutoff can exceed the width of gcov_unsigned_t.  Truncating it
makes a forced nonzero count too small.  Keep the full cutoff and cap the
result at the largest representable profile count.

Regression tested on aarch64-linux-gnu with no new regressions.

gcc/ChangeLog:

        * profile-count.cc (profile_count::force_nonzero): Use a full width
        integer for the cutoff and cap the result.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
---
 gcc/profile-count.cc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/gcc/profile-count.cc b/gcc/profile-count.cc
index e00d11b6f02..979886c2611 100644
--- a/gcc/profile-count.cc
+++ b/gcc/profile-count.cc
@@ -587,8 +587,10 @@ profile_count::force_nonzero () const
      cutoff and we perform count = count * num / den where num is non-zero
      and den is 0.   If profile was scaled by large factor, forcing value
      to 1 would lead to large scale factor.  */
-  gcov_unsigned_t small = profile_info ? profile_info->cutoff / 2 + 1
+  /* The scaled cutoff can exceed the range of gcov_unsigned_t.  */
+  gcov_type_unsigned small = profile_info ? profile_info->cutoff / 2 + 1
                          : 1;
+  small = MIN (small, max_count);
   if (ret.m_val < small)
     {
       ret.m_val = small;
-- 
2.34.1

Reply via email to