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
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_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. */
62 profile_count::debug () const
65 fprintf (stderr
, "\n");
68 /* Return true if THIS differs from OTHER; tolerate small diferences. */
71 profile_count::differs_from_p (profile_count other
) const
73 gcc_checking_assert (compatible_p (other
));
74 if (!initialized_p () || !other
.initialized_p ())
76 if ((uint64_t)m_val
- (uint64_t)other
.m_val
< 100
77 || (uint64_t)other
.m_val
- (uint64_t)m_val
< 100)
81 int64_t ratio
= (int64_t)m_val
* 100 / other
.m_val
;
82 return ratio
< 99 || ratio
> 101;
85 /* Stream THIS from IB. */
88 profile_count::stream_in (struct lto_input_block
*ib
)
91 ret
.m_val
= streamer_read_gcov_count (ib
);
92 ret
.m_quality
= (profile_quality
) streamer_read_uhwi (ib
);
96 /* Stream THIS to OB. */
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. */
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. */
117 profile_probability::dump (FILE *f
) const
119 if (!initialized_p ())
120 fprintf (f
, "uninitialized");
123 /* Make difference between 0.00 as a roundoff error and actual 0.
126 fprintf (f
, "never");
127 else if (m_val
== max_probability
)
128 fprintf (f
, "always");
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. */
143 profile_probability::debug () const
146 fprintf (stderr
, "\n");
149 /* Return true if THIS differs from OTHER; tolerate small diferences. */
152 profile_probability::differs_from_p (profile_probability other
) const
154 if (!initialized_p () || !other
.initialized_p ())
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)
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. */
168 profile_probability::differs_lot_from_p (profile_probability other
) const
170 if (!initialized_p () || !other
.initialized_p ())
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. */
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
);
187 /* Stream THIS to OB. */
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. */
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. */
208 slow_safe_scale_64bit (uint64_t a
, uint64_t b
, uint64_t c
, uint64_t *res
)
210 FIXED_WIDE_INT (128) tmp
= a
;
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 ();
219 *res
= (uint64_t) -1;
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 ())
231 if (*this == profile_count::zero ())
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 ())
252 gcc_checking_assert (entry_bb_count
.initialized_p ());
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. */
263 profile_count::to_sreal_scale (profile_count in
, bool *known
) const
265 if (!initialized_p ())
269 return CGRAPH_FREQ_BASE
;
273 if (*this == profile_count::zero ())
275 gcc_checking_assert (in
.initialized_p ());
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. */
294 profile_count::adjust_for_ipa_scaling (profile_count
*num
,
297 /* Scaling is no-op if NUM and DEN are the same. */
300 /* Scaling to zero is always zeor. */
301 if (*num
== profile_count::zero ())
303 /* If den is non-zero we are safe. */
304 if (den
->force_nonzero () == *den
)
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 ();