]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix overflow in ipa_profile_generate_summary
authorJan Hubicka <hubicka@ucw.cz>
Tue, 30 Sep 2025 11:07:53 +0000 (13:07 +0200)
committerJan Hubicka <hubicka@ucw.cz>
Tue, 30 Sep 2025 11:09:46 +0000 (13:09 +0200)
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.

gcc/basic-block.h
gcc/ipa-profile.cc
gcc/sched-rgn.cc

index 049631de49b96b4a9bc84f9731a6f0164e69e203..410a03c6a5af807b2bcc7bd32850c98c09bbba33 100644 (file)
@@ -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)
index d4725ce626264a92ec4b4bd98bba7c8df00e6c00..4d0a8c98fbdf41e0189911fb008aa3b548ac47fb 100644 (file)
@@ -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);
                        }
 
index adae78449163389170853e176ac72ae2d8c2e207..5382fde1c31431ffb7816f26aab0cab87d3e97a0 100644 (file)
@@ -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);
        }