https://gcc.gnu.org/g:69ac957ea56ad4b98baa8a6ca52ad0729b3358e0

commit r16-4141-g69ac957ea56ad4b98baa8a6ca52ad0729b3358e0
Author: Jan Hubicka <[email protected]>
Date:   Tue Sep 30 13:07:53 2025 +0200

    Fix overflow in ipa_profile_generate_summary
    
    With profile count scaling we now get overflow in 
ipa_profile_generate_summary
    which uses old macro GCOV_COMPUTE_SCALE that is not ready for very large
    counts.  This patch replaces remaining two uses of it by (somewhat 
elaborate)
    profile-count based equivalent which is overflow safe.
    
    gcc/ChangeLog:
    
            * basic-block.h (GCOV_COMPUTE_SCALE): Remove.
            * ipa-profile.cc (ipa_profile_generate_summary): Use
            profile-count scaling.
            * sched-rgn.cc (compute_trg_info): Likewise.

Diff:
---
 gcc/basic-block.h  | 5 -----
 gcc/ipa-profile.cc | 6 +++++-
 gcc/sched-rgn.cc   | 7 ++++++-
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/gcc/basic-block.h b/gcc/basic-block.h
index 049631de49b9..410a03c6a5af 100644
--- a/gcc/basic-block.h
+++ b/gcc/basic-block.h
@@ -293,11 +293,6 @@ enum cfg_bb_flags
 /* Return expected execution frequency of the edge E.  */
 #define EDGE_FREQUENCY(e)              e->count ().to_frequency (cfun)
 
-/* Compute a scale factor (or probability) suitable for scaling of
-   gcov_type values via apply_probability() and apply_scale().  */
-#define GCOV_COMPUTE_SCALE(num,den) \
-  ((den) ? RDIV ((num) * REG_BR_PROB_BASE, (den)) : REG_BR_PROB_BASE)
-
 /* Return nonzero if edge is critical.  */
 #define EDGE_CRITICAL_P(e)             (EDGE_COUNT ((e)->src->succs) >= 2 \
                                         && EDGE_COUNT ((e)->dest->preds) >= 2)
diff --git a/gcc/ipa-profile.cc b/gcc/ipa-profile.cc
index d4725ce62626..4d0a8c98fbdf 100644
--- a/gcc/ipa-profile.cc
+++ b/gcc/ipa-profile.cc
@@ -316,7 +316,11 @@ ipa_profile_generate_summary (void)
                              count = all;
                            }
                          speculative_call_target item (
-                           val, GCOV_COMPUTE_SCALE (count, all));
+                           val,
+                           profile_count::from_gcov_type (count)
+                             .probability_in
+                               (profile_count::from_gcov_type (all))
+                                .to_reg_br_prob_base ());
                          csum->speculative_call_targets.safe_push (item);
                        }
 
diff --git a/gcc/sched-rgn.cc b/gcc/sched-rgn.cc
index adae78449163..5382fde1c314 100644
--- a/gcc/sched-rgn.cc
+++ b/gcc/sched-rgn.cc
@@ -1532,7 +1532,12 @@ compute_trg_info (int trg)
          int tf = prob[trg], cf = prob[i];
 
          /* In CFGs with low probability edges TF can possibly be zero.  */
-         sp->src_prob = (tf ? GCOV_COMPUTE_SCALE (cf, tf) : 0);
+         sp->src_prob = (tf ?
+                         profile_count::from_gcov_type (cf)
+                           .probability_in
+                             (profile_count::from_gcov_type (tf))
+                               .to_reg_br_prob_base ()
+                         : 0);
          sp->is_valid = (sp->src_prob >= min_spec_prob);
        }

Reply via email to