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"
39 profile_count::dump (FILE *f
) const
41 if (!initialized_p ())
42 fprintf (f
, "uninitialized");
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_guessed_global0adjusted
)
51 fprintf (f
, " (estimated locally, globally 0 adjusted)");
52 else if (m_quality
== profile_adjusted
)
53 fprintf (f
, " (adjusted)");
54 else if (m_quality
== profile_afdo
)
55 fprintf (f
, " (auto FDO)");
56 else if (m_quality
== profile_guessed
)
57 fprintf (f
, " (guessed)");
61 /* Dump THIS to stderr. */
64 profile_count::debug () const
67 fprintf (stderr
, "\n");
70 /* Return true if THIS differs from OTHER; tolerate small diferences. */
73 profile_count::differs_from_p (profile_count other
) const
75 gcc_checking_assert (compatible_p (other
));
76 if (!initialized_p () || !other
.initialized_p ())
78 if ((uint64_t)m_val
- (uint64_t)other
.m_val
< 100
79 || (uint64_t)other
.m_val
- (uint64_t)m_val
< 100)
83 int64_t ratio
= (int64_t)m_val
* 100 / other
.m_val
;
84 return ratio
< 99 || ratio
> 101;
87 /* Stream THIS from IB. */
90 profile_count::stream_in (struct lto_input_block
*ib
)
93 ret
.m_val
= streamer_read_gcov_count (ib
);
94 ret
.m_quality
= (profile_quality
) streamer_read_uhwi (ib
);
98 /* Stream THIS to OB. */
101 profile_count::stream_out (struct output_block
*ob
)
103 streamer_write_gcov_count (ob
, m_val
);
104 streamer_write_uhwi (ob
, m_quality
);
107 /* Stream THIS to OB. */
110 profile_count::stream_out (struct lto_output_stream
*ob
)
112 streamer_write_gcov_count_stream (ob
, m_val
);
113 streamer_write_uhwi_stream (ob
, m_quality
);
116 /* Dump THIS to F. */
119 profile_probability::dump (FILE *f
) const
121 if (!initialized_p ())
122 fprintf (f
, "uninitialized");
125 /* Make difference between 0.00 as a roundoff error and actual 0.
128 fprintf (f
, "never");
129 else if (m_val
== max_probability
)
130 fprintf (f
, "always");
132 fprintf (f
, "%3.1f%%", (double)m_val
* 100 / max_probability
);
133 if (m_quality
== profile_adjusted
)
134 fprintf (f
, " (adjusted)");
135 else if (m_quality
== profile_afdo
)
136 fprintf (f
, " (auto FDO)");
137 else if (m_quality
== profile_guessed
)
138 fprintf (f
, " (guessed)");
142 /* Dump THIS to stderr. */
145 profile_probability::debug () const
148 fprintf (stderr
, "\n");
151 /* Return true if THIS differs from OTHER; tolerate small diferences. */
154 profile_probability::differs_from_p (profile_probability other
) const
156 if (!initialized_p () || !other
.initialized_p ())
158 if ((uint64_t)m_val
- (uint64_t)other
.m_val
< max_probability
/ 1000
159 || (uint64_t)other
.m_val
- (uint64_t)max_probability
< 1000)
163 int64_t ratio
= (int64_t)m_val
* 100 / other
.m_val
;
164 return ratio
< 99 || ratio
> 101;
167 /* Return true if THIS differs significantly from OTHER. */
170 profile_probability::differs_lot_from_p (profile_probability other
) const
172 if (!initialized_p () || !other
.initialized_p ())
174 uint32_t d
= m_val
> other
.m_val
? m_val
- other
.m_val
: other
.m_val
- m_val
;
175 return d
> max_probability
/ 2;
178 /* Stream THIS from IB. */
181 profile_probability::stream_in (struct lto_input_block
*ib
)
183 profile_probability ret
;
184 ret
.m_val
= streamer_read_uhwi (ib
);
185 ret
.m_quality
= (profile_quality
) streamer_read_uhwi (ib
);
189 /* Stream THIS to OB. */
192 profile_probability::stream_out (struct output_block
*ob
)
194 streamer_write_uhwi (ob
, m_val
);
195 streamer_write_uhwi (ob
, m_quality
);
198 /* Stream THIS to OB. */
201 profile_probability::stream_out (struct lto_output_stream
*ob
)
203 streamer_write_uhwi_stream (ob
, m_val
);
204 streamer_write_uhwi_stream (ob
, m_quality
);
207 /* Compute RES=(a*b + c/2)/c capping and return false if overflow happened. */
210 slow_safe_scale_64bit (uint64_t a
, uint64_t b
, uint64_t c
, uint64_t *res
)
212 FIXED_WIDE_INT (128) tmp
= a
;
214 tmp
= wi::udiv_floor (wi::umul (tmp
, b
, &overflow
) + (c
/ 2), c
);
215 gcc_checking_assert (!overflow
);
216 if (wi::fits_uhwi_p (tmp
))
218 *res
= tmp
.to_uhwi ();
221 *res
= (uint64_t) -1;
225 /* Return count as frequency within FUN scaled in range 0 to REG_FREQ_MAX
226 Used for legacy code and should not be used anymore. */
229 profile_count::to_frequency (struct function
*fun
) const
231 if (!initialized_p ())
233 if (*this == profile_count::zero ())
235 gcc_assert (REG_BR_PROB_BASE
== BB_FREQ_MAX
236 && fun
->cfg
->count_max
.initialized_p ());
237 profile_probability prob
= probability_in (fun
->cfg
->count_max
);
238 if (!prob
.initialized_p ())
239 return REG_BR_PROB_BASE
;
240 return prob
.to_reg_br_prob_base ();
243 /* Return count as frequency within FUN scaled in range 0 to CGRAPH_FREQ_MAX
244 where CGRAPH_FREQ_BASE means that count equals to entry block count.
245 Used for legacy code and should not be used anymore. */
248 profile_count::to_cgraph_frequency (profile_count entry_bb_count
) const
250 if (!initialized_p () || !entry_bb_count
.initialized_p ())
251 return CGRAPH_FREQ_BASE
;
252 if (*this == profile_count::zero ())
254 gcc_checking_assert (entry_bb_count
.initialized_p ());
256 if (!safe_scale_64bit (!entry_bb_count
.m_val
? m_val
+ 1 : m_val
,
257 CGRAPH_FREQ_BASE
, MAX (1, entry_bb_count
.m_val
), &scale
))
258 return CGRAPH_FREQ_MAX
;
259 return MIN (scale
, CGRAPH_FREQ_MAX
);
262 /* Return THIS/IN as sreal value. */
265 profile_count::to_sreal_scale (profile_count in
, bool *known
) const
267 if (!initialized_p () || !in
.initialized_p ())
275 if (*this == profile_count::zero ())
284 return (sreal
)m_val
/ (sreal
)in
.m_val
;
287 /* We want to scale profile across function boundary from NUM to DEN.
288 Take care of the side case when DEN is zeros. We still want to behave
289 sanely here which means
290 - scale to profile_count::zero () if NUM is profile_count::zero
291 - do not affect anything if NUM == DEN
292 - preserve counter value but adjust quality in other cases. */
295 profile_count::adjust_for_ipa_scaling (profile_count
*num
,
298 /* Scaling is no-op if NUM and DEN are the same. */
301 /* Scaling to zero is always zero. */
302 if (*num
== profile_count::zero ())
304 /* If den is non-zero we are safe. */
305 if (den
->force_nonzero () == *den
)
307 /* Force both to non-zero so we do not push profiles to 0 when
308 both num == 0 and den == 0. */
309 *den
= den
->force_nonzero ();
310 *num
= num
->force_nonzero ();
313 /* THIS is a count of bb which is known to be executed IPA times.
314 Combine this information into bb counter. This means returning IPA
315 if it is nonzero, not changing anything if IPA is uninitialized
316 and if IPA is zero, turning THIS into corresponding local profile with
319 profile_count::combine_with_ipa_count (profile_count ipa
)
322 if (ipa
.nonzero_p ())
324 if (!ipa
.initialized_p () || *this == profile_count::zero ())
326 if (ipa
== profile_count::zero ())
327 return this->global0 ();
328 return this->global0adjusted ();
331 /* The profiling runtime uses gcov_type, which is usually 64bit integer.
332 Conversions back and forth are used to read the coverage and get it
333 into internal representation. */
335 profile_count::from_gcov_type (gcov_type v
)
338 gcc_checking_assert (v
>= 0);
339 if (dump_file
&& v
>= (gcov_type
)max_count
)
341 "Capping gcov count %" PRId64
" to max_count %" PRId64
"\n",
342 (int64_t) v
, (int64_t) max_count
);
343 ret
.m_val
= MIN (v
, (gcov_type
)max_count
);
344 ret
.m_quality
= profile_precise
;
349 /* COUNT1 times event happens with *THIS probability, COUNT2 times OTHER
350 happens with COUNT2 probablity. Return probablity that either *THIS or
354 profile_probability::combine_with_count (profile_count count1
,
355 profile_probability other
,
356 profile_count count2
) const
358 /* If probabilities are same, we are done.
359 If counts are nonzero we can distribute accordingly. In remaining
360 cases just avreage the values and hope for the best. */
361 if (*this == other
|| count1
== count2
362 || (count2
== profile_count::zero ()
363 && !(count1
== profile_count::zero ())))
365 if (count1
== profile_count::zero () && !(count2
== profile_count::zero ()))
367 else if (count1
.nonzero_p () || count2
.nonzero_p ())
368 return *this * count1
.probability_in (count1
+ count2
)
369 + other
* count2
.probability_in (count1
+ count2
);
371 return *this * profile_probability::even ()
372 + other
* profile_probability::even ();