gcc/ChangeLog:
[official-gcc.git] / gcc / profile-count.c
blob51c3b74fefa1d3d14e734f33547d4cba9b20ccf4
1 /* Profile counter container type.
2 Copyright (C) 2017 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "profile-count.h"
25 #include "options.h"
26 #include "tree.h"
27 #include "basic-block.h"
28 #include "cfg.h"
29 #include "function.h"
30 #include "gimple.h"
31 #include "data-streamer.h"
32 #include "cgraph.h"
33 #include "wide-int.h"
34 #include "sreal.h"
36 /* Dump THIS to F. */
38 void
39 profile_count::dump (FILE *f) const
41 if (!initialized_p ())
42 fprintf (f, "uninitialized");
43 else
45 fprintf (f, "%" PRId64, m_val);
46 if (m_quality == profile_guessed_local)
47 fprintf (f, " (estimated locally)");
48 else if (m_quality == profile_guessed_global0)
49 fprintf (f, " (estimated locally, globally 0)");
50 else if (m_quality == profile_adjusted)
51 fprintf (f, " (adjusted)");
52 else if (m_quality == profile_afdo)
53 fprintf (f, " (auto FDO)");
54 else if (m_quality == profile_guessed)
55 fprintf (f, " (guessed)");
59 /* Dump THIS to stderr. */
61 void
62 profile_count::debug () const
64 dump (stderr);
65 fprintf (stderr, "\n");
68 /* Return true if THIS differs from OTHER; tolerate small diferences. */
70 bool
71 profile_count::differs_from_p (profile_count other) const
73 gcc_checking_assert (compatible_p (other));
74 if (!initialized_p () || !other.initialized_p ())
75 return false;
76 if ((uint64_t)m_val - (uint64_t)other.m_val < 100
77 || (uint64_t)other.m_val - (uint64_t)m_val < 100)
78 return false;
79 if (!other.m_val)
80 return true;
81 int64_t ratio = (int64_t)m_val * 100 / other.m_val;
82 return ratio < 99 || ratio > 101;
85 /* Stream THIS from IB. */
87 profile_count
88 profile_count::stream_in (struct lto_input_block *ib)
90 profile_count ret;
91 ret.m_val = streamer_read_gcov_count (ib);
92 ret.m_quality = (profile_quality) streamer_read_uhwi (ib);
93 return ret;
96 /* Stream THIS to OB. */
98 void
99 profile_count::stream_out (struct output_block *ob)
101 streamer_write_gcov_count (ob, m_val);
102 streamer_write_uhwi (ob, m_quality);
105 /* Stream THIS to OB. */
107 void
108 profile_count::stream_out (struct lto_output_stream *ob)
110 streamer_write_gcov_count_stream (ob, m_val);
111 streamer_write_uhwi_stream (ob, m_quality);
114 /* Dump THIS to F. */
116 void
117 profile_probability::dump (FILE *f) const
119 if (!initialized_p ())
120 fprintf (f, "uninitialized");
121 else
123 /* Make difference between 0.00 as a roundoff error and actual 0.
124 Similarly for 1. */
125 if (m_val == 0)
126 fprintf (f, "never");
127 else if (m_val == max_probability)
128 fprintf (f, "always");
129 else
130 fprintf (f, "%3.1f%%", (double)m_val * 100 / max_probability);
131 if (m_quality == profile_adjusted)
132 fprintf (f, " (adjusted)");
133 else if (m_quality == profile_afdo)
134 fprintf (f, " (auto FDO)");
135 else if (m_quality == profile_guessed)
136 fprintf (f, " (guessed)");
140 /* Dump THIS to stderr. */
142 void
143 profile_probability::debug () const
145 dump (stderr);
146 fprintf (stderr, "\n");
149 /* Return true if THIS differs from OTHER; tolerate small diferences. */
151 bool
152 profile_probability::differs_from_p (profile_probability other) const
154 if (!initialized_p () || !other.initialized_p ())
155 return false;
156 if ((uint64_t)m_val - (uint64_t)other.m_val < max_probability / 1000
157 || (uint64_t)other.m_val - (uint64_t)max_probability < 1000)
158 return false;
159 if (!other.m_val)
160 return true;
161 int64_t ratio = (int64_t)m_val * 100 / other.m_val;
162 return ratio < 99 || ratio > 101;
165 /* Return true if THIS differs significantly from OTHER. */
167 bool
168 profile_probability::differs_lot_from_p (profile_probability other) const
170 if (!initialized_p () || !other.initialized_p ())
171 return false;
172 uint32_t d = m_val > other.m_val ? m_val - other.m_val : other.m_val - m_val;
173 return d > max_probability / 2;
176 /* Stream THIS from IB. */
178 profile_probability
179 profile_probability::stream_in (struct lto_input_block *ib)
181 profile_probability ret;
182 ret.m_val = streamer_read_uhwi (ib);
183 ret.m_quality = (profile_quality) streamer_read_uhwi (ib);
184 return ret;
187 /* Stream THIS to OB. */
189 void
190 profile_probability::stream_out (struct output_block *ob)
192 streamer_write_uhwi (ob, m_val);
193 streamer_write_uhwi (ob, m_quality);
196 /* Stream THIS to OB. */
198 void
199 profile_probability::stream_out (struct lto_output_stream *ob)
201 streamer_write_uhwi_stream (ob, m_val);
202 streamer_write_uhwi_stream (ob, m_quality);
205 /* Compute RES=(a*b + c/2)/c capping and return false if overflow happened. */
207 bool
208 slow_safe_scale_64bit (uint64_t a, uint64_t b, uint64_t c, uint64_t *res)
210 FIXED_WIDE_INT (128) tmp = a;
211 bool overflow;
212 tmp = wi::udiv_floor (wi::umul (tmp, b, &overflow) + (c / 2), c);
213 gcc_checking_assert (!overflow);
214 if (wi::fits_uhwi_p (tmp))
216 *res = tmp.to_uhwi ();
217 return true;
219 *res = (uint64_t) -1;
220 return false;
223 /* Return count as frequency within FUN scaled in range 0 to REG_FREQ_MAX
224 Used for legacy code and should not be used anymore. */
227 profile_count::to_frequency (struct function *fun) const
229 if (!initialized_p ())
230 return BB_FREQ_MAX;
231 if (*this == profile_count::zero ())
232 return 0;
233 gcc_assert (REG_BR_PROB_BASE == BB_FREQ_MAX
234 && fun->cfg->count_max.initialized_p ());
235 profile_probability prob = probability_in (fun->cfg->count_max);
236 if (!prob.initialized_p ())
237 return REG_BR_PROB_BASE;
238 return prob.to_reg_br_prob_base ();
241 /* Return count as frequency within FUN scaled in range 0 to CGRAPH_FREQ_MAX
242 where CGRAPH_FREQ_BASE means that count equals to entry block count.
243 Used for legacy code and should not be used anymore. */
246 profile_count::to_cgraph_frequency (profile_count entry_bb_count) const
248 if (!initialized_p ())
249 return CGRAPH_FREQ_BASE;
250 if (*this == profile_count::zero ())
251 return 0;
252 gcc_checking_assert (entry_bb_count.initialized_p ());
253 uint64_t scale;
254 if (!safe_scale_64bit (!entry_bb_count.m_val ? m_val + 1 : m_val,
255 CGRAPH_FREQ_BASE, MAX (1, entry_bb_count.m_val), &scale))
256 return CGRAPH_FREQ_MAX;
257 return MIN (scale, CGRAPH_FREQ_MAX);
260 /* Return THIS/IN as sreal value. */
262 sreal
263 profile_count::to_sreal_scale (profile_count in, bool *known) const
265 if (!initialized_p ())
267 if (known)
268 *known = false;
269 return CGRAPH_FREQ_BASE;
271 if (known)
272 *known = true;
273 if (*this == profile_count::zero ())
274 return 0;
275 gcc_checking_assert (in.initialized_p ());
277 if (!in.m_val)
279 if (!m_val)
280 return 1;
281 return m_val * 4;
283 return (sreal)m_val / (sreal)in.m_val;
286 /* We want to scale profile across function boundary from NUM to DEN.
287 Take care of the side case when DEN is zeros. We still want to behave
288 sanely here which means
289 - scale to profile_count::zero () if NUM is profile_count::zero
290 - do not affect anything if NUM == DEN
291 - preserve counter value but adjust quality in other cases. */
293 void
294 profile_count::adjust_for_ipa_scaling (profile_count *num,
295 profile_count *den)
297 /* Scaling is no-op if NUM and DEN are the same. */
298 if (*num == *den)
299 return;
300 /* Scaling to zero is always zeor. */
301 if (*num == profile_count::zero ())
302 return;
303 /* If den is non-zero we are safe. */
304 if (den->force_nonzero () == *den)
305 return;
306 /* Force both to non-zero so we do not push profiles to 0 when
307 both num == 0 and den == 0. */
308 *den = den->force_nonzero ();
309 *num = num->force_nonzero ();