1 /* Profile counter container type.
2 Copyright (C) 2017-2018 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
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
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/>. */
23 #include "coretypes.h"
24 #include "profile-count.h"
27 #include "basic-block.h"
31 #include "data-streamer.h"
36 /* Get a string describing QUALITY. */
39 profile_quality_as_string (enum profile_quality quality
)
45 case profile_uninitialized
:
46 return "uninitialized";
47 case profile_guessed_local
:
48 return "guessed_local";
49 case profile_guessed_global0
:
50 return "guessed_global0";
51 case profile_guessed_global0adjusted
:
52 return "guessed_global0adjusted";
57 case profile_adjusted
:
67 profile_count::dump (FILE *f
) const
69 if (!initialized_p ())
70 fprintf (f
, "uninitialized");
73 fprintf (f
, "%" PRId64
, m_val
);
74 if (m_quality
== profile_guessed_local
)
75 fprintf (f
, " (estimated locally)");
76 else if (m_quality
== profile_guessed_global0
)
77 fprintf (f
, " (estimated locally, globally 0)");
78 else if (m_quality
== profile_guessed_global0adjusted
)
79 fprintf (f
, " (estimated locally, globally 0 adjusted)");
80 else if (m_quality
== profile_adjusted
)
81 fprintf (f
, " (adjusted)");
82 else if (m_quality
== profile_afdo
)
83 fprintf (f
, " (auto FDO)");
84 else if (m_quality
== profile_guessed
)
85 fprintf (f
, " (guessed)");
89 /* Dump THIS to stderr. */
92 profile_count::debug () const
95 fprintf (stderr
, "\n");
98 /* Return true if THIS differs from OTHER; tolerate small diferences. */
101 profile_count::differs_from_p (profile_count other
) const
103 gcc_checking_assert (compatible_p (other
));
104 if (!initialized_p () || !other
.initialized_p ())
106 if ((uint64_t)m_val
- (uint64_t)other
.m_val
< 100
107 || (uint64_t)other
.m_val
- (uint64_t)m_val
< 100)
111 int64_t ratio
= (int64_t)m_val
* 100 / other
.m_val
;
112 return ratio
< 99 || ratio
> 101;
115 /* Stream THIS from IB. */
118 profile_count::stream_in (struct lto_input_block
*ib
)
121 ret
.m_val
= streamer_read_gcov_count (ib
);
122 ret
.m_quality
= (profile_quality
) streamer_read_uhwi (ib
);
126 /* Stream THIS to OB. */
129 profile_count::stream_out (struct output_block
*ob
)
131 streamer_write_gcov_count (ob
, m_val
);
132 streamer_write_uhwi (ob
, m_quality
);
135 /* Stream THIS to OB. */
138 profile_count::stream_out (struct lto_output_stream
*ob
)
140 streamer_write_gcov_count_stream (ob
, m_val
);
141 streamer_write_uhwi_stream (ob
, m_quality
);
144 /* Dump THIS to F. */
147 profile_probability::dump (FILE *f
) const
149 if (!initialized_p ())
150 fprintf (f
, "uninitialized");
153 /* Make difference between 0.00 as a roundoff error and actual 0.
156 fprintf (f
, "never");
157 else if (m_val
== max_probability
)
158 fprintf (f
, "always");
160 fprintf (f
, "%3.1f%%", (double)m_val
* 100 / max_probability
);
161 if (m_quality
== profile_adjusted
)
162 fprintf (f
, " (adjusted)");
163 else if (m_quality
== profile_afdo
)
164 fprintf (f
, " (auto FDO)");
165 else if (m_quality
== profile_guessed
)
166 fprintf (f
, " (guessed)");
170 /* Dump THIS to stderr. */
173 profile_probability::debug () const
176 fprintf (stderr
, "\n");
179 /* Return true if THIS differs from OTHER; tolerate small diferences. */
182 profile_probability::differs_from_p (profile_probability other
) const
184 if (!initialized_p () || !other
.initialized_p ())
186 if ((uint64_t)m_val
- (uint64_t)other
.m_val
< max_probability
/ 1000
187 || (uint64_t)other
.m_val
- (uint64_t)max_probability
< 1000)
191 int64_t ratio
= (int64_t)m_val
* 100 / other
.m_val
;
192 return ratio
< 99 || ratio
> 101;
195 /* Return true if THIS differs significantly from OTHER. */
198 profile_probability::differs_lot_from_p (profile_probability other
) const
200 if (!initialized_p () || !other
.initialized_p ())
202 uint32_t d
= m_val
> other
.m_val
? m_val
- other
.m_val
: other
.m_val
- m_val
;
203 return d
> max_probability
/ 2;
206 /* Stream THIS from IB. */
209 profile_probability::stream_in (struct lto_input_block
*ib
)
211 profile_probability ret
;
212 ret
.m_val
= streamer_read_uhwi (ib
);
213 ret
.m_quality
= (profile_quality
) streamer_read_uhwi (ib
);
217 /* Stream THIS to OB. */
220 profile_probability::stream_out (struct output_block
*ob
)
222 streamer_write_uhwi (ob
, m_val
);
223 streamer_write_uhwi (ob
, m_quality
);
226 /* Stream THIS to OB. */
229 profile_probability::stream_out (struct lto_output_stream
*ob
)
231 streamer_write_uhwi_stream (ob
, m_val
);
232 streamer_write_uhwi_stream (ob
, m_quality
);
235 /* Compute RES=(a*b + c/2)/c capping and return false if overflow happened. */
238 slow_safe_scale_64bit (uint64_t a
, uint64_t b
, uint64_t c
, uint64_t *res
)
240 FIXED_WIDE_INT (128) tmp
= a
;
241 wi::overflow_type overflow
;
242 tmp
= wi::udiv_floor (wi::umul (tmp
, b
, &overflow
) + (c
/ 2), c
);
243 gcc_checking_assert (!overflow
);
244 if (wi::fits_uhwi_p (tmp
))
246 *res
= tmp
.to_uhwi ();
249 *res
= (uint64_t) -1;
253 /* Return count as frequency within FUN scaled in range 0 to REG_FREQ_MAX
254 Used for legacy code and should not be used anymore. */
257 profile_count::to_frequency (struct function
*fun
) const
259 if (!initialized_p ())
261 if (*this == profile_count::zero ())
263 gcc_assert (REG_BR_PROB_BASE
== BB_FREQ_MAX
264 && fun
->cfg
->count_max
.initialized_p ());
265 profile_probability prob
= probability_in (fun
->cfg
->count_max
);
266 if (!prob
.initialized_p ())
267 return REG_BR_PROB_BASE
;
268 return prob
.to_reg_br_prob_base ();
271 /* Return count as frequency within FUN scaled in range 0 to CGRAPH_FREQ_MAX
272 where CGRAPH_FREQ_BASE means that count equals to entry block count.
273 Used for legacy code and should not be used anymore. */
276 profile_count::to_cgraph_frequency (profile_count entry_bb_count
) const
278 if (!initialized_p () || !entry_bb_count
.initialized_p ())
279 return CGRAPH_FREQ_BASE
;
280 if (*this == profile_count::zero ())
282 gcc_checking_assert (entry_bb_count
.initialized_p ());
284 if (!safe_scale_64bit (!entry_bb_count
.m_val
? m_val
+ 1 : m_val
,
285 CGRAPH_FREQ_BASE
, MAX (1, entry_bb_count
.m_val
), &scale
))
286 return CGRAPH_FREQ_MAX
;
287 return MIN (scale
, CGRAPH_FREQ_MAX
);
290 /* Return THIS/IN as sreal value. */
293 profile_count::to_sreal_scale (profile_count in
, bool *known
) const
295 if (!initialized_p () || !in
.initialized_p ())
303 if (*this == profile_count::zero ())
312 return (sreal
)m_val
/ (sreal
)in
.m_val
;
315 /* We want to scale profile across function boundary from NUM to DEN.
316 Take care of the side case when DEN is zeros. We still want to behave
317 sanely here which means
318 - scale to profile_count::zero () if NUM is profile_count::zero
319 - do not affect anything if NUM == DEN
320 - preserve counter value but adjust quality in other cases. */
323 profile_count::adjust_for_ipa_scaling (profile_count
*num
,
326 /* Scaling is no-op if NUM and DEN are the same. */
329 /* Scaling to zero is always zero. */
330 if (*num
== profile_count::zero ())
332 /* If den is non-zero we are safe. */
333 if (den
->force_nonzero () == *den
)
335 /* Force both to non-zero so we do not push profiles to 0 when
336 both num == 0 and den == 0. */
337 *den
= den
->force_nonzero ();
338 *num
= num
->force_nonzero ();
341 /* THIS is a count of bb which is known to be executed IPA times.
342 Combine this information into bb counter. This means returning IPA
343 if it is nonzero, not changing anything if IPA is uninitialized
344 and if IPA is zero, turning THIS into corresponding local profile with
347 profile_count::combine_with_ipa_count (profile_count ipa
)
350 if (ipa
.nonzero_p ())
352 if (!ipa
.initialized_p () || *this == profile_count::zero ())
354 if (ipa
== profile_count::zero ())
355 return this->global0 ();
356 return this->global0adjusted ();
359 /* The profiling runtime uses gcov_type, which is usually 64bit integer.
360 Conversions back and forth are used to read the coverage and get it
361 into internal representation. */
363 profile_count::from_gcov_type (gcov_type v
)
366 gcc_checking_assert (v
>= 0);
367 if (dump_file
&& v
>= (gcov_type
)max_count
)
369 "Capping gcov count %" PRId64
" to max_count %" PRId64
"\n",
370 (int64_t) v
, (int64_t) max_count
);
371 ret
.m_val
= MIN (v
, (gcov_type
)max_count
);
372 ret
.m_quality
= profile_precise
;
377 /* COUNT1 times event happens with *THIS probability, COUNT2 times OTHER
378 happens with COUNT2 probablity. Return probablity that either *THIS or
382 profile_probability::combine_with_count (profile_count count1
,
383 profile_probability other
,
384 profile_count count2
) const
386 /* If probabilities are same, we are done.
387 If counts are nonzero we can distribute accordingly. In remaining
388 cases just avreage the values and hope for the best. */
389 if (*this == other
|| count1
== count2
390 || (count2
== profile_count::zero ()
391 && !(count1
== profile_count::zero ())))
393 if (count1
== profile_count::zero () && !(count2
== profile_count::zero ()))
395 else if (count1
.nonzero_p () || count2
.nonzero_p ())
396 return *this * count1
.probability_in (count1
+ count2
)
397 + other
* count2
.probability_in (count1
+ count2
);
399 return *this * profile_probability::even ()
400 + other
* profile_probability::even ();