]> git.ipfire.org Git - thirdparty/gcc.git/commit
Fix integer overflow in profile_count::probability_in
authorJan Hubicka <hubicka@ucw.cz>
Fri, 26 Sep 2025 10:39:07 +0000 (12:39 +0200)
committerJan Hubicka <hubicka@ucw.cz>
Fri, 26 Sep 2025 10:39:44 +0000 (12:39 +0200)
commit9f9c8d63a5822378e18f3f8f188df21c6104e18e
treebd1803fe63a303edd7285b6222e4d82964fa4ce3
parent1cf6cda0554406f3d3ca641de7c4e360953aeb22
Fix integer overflow in profile_count::probability_in

This patch fixes integer overflow in profile_count::probability_in which happens
for very large counts.  This was probably not that common in practice until
scaled AutoFDO profiles were intorduces.

This was introduced as cut&paste from profile_probability implementation.
I reviewed multiplicaitons in the file for safety and noticed that in some
cases the code is over-protective. In profile_probability::operator/ we alrady
scale that m_val <= other.m_val and thus we know result will be in the range
0...max_probability.  In profile_probability::apply_scale we deal with 30bit
value from profile_probability so no overflow can happen.

gcc/ChangeLog:

* profile-count.h (profile_probability::operator/): Do not cap
twice.
(profile_probability::operator/=): Likewise.
(profile_probability::apply_scale): Do not watch for overflow.
(profile_count::probability_in): Watch overflow.
gcc/profile-count.h