]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/profile-count.c
Put the CL into the right dir.
[thirdparty/gcc.git] / gcc / profile-count.c
CommitLineData
db9cef39 1/* Profile counter container type.
fbd26352 2 Copyright (C) 2017-2019 Free Software Foundation, Inc.
db9cef39 3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
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"
db9cef39 28#include "function.h"
2515797e 29#include "cfg.h"
db9cef39 30#include "gimple.h"
31#include "data-streamer.h"
32#include "cgraph.h"
4cb91b78 33#include "wide-int.h"
7c343235 34#include "sreal.h"
db9cef39 35
6b33947d 36/* Names from profile_quality enum values. */
37
38const char *profile_quality_names[] =
39{
40 "uninitialized",
41 "guessed_local",
42 "guessed_global0",
43 "guessed_global0adjusted",
44 "guessed",
45 "afdo",
46 "adjusted",
47 "precise"
48};
49
9dcf2a11 50/* Get a string describing QUALITY. */
51
52const char *
53profile_quality_as_string (enum profile_quality quality)
54{
6b33947d 55 return profile_quality_names[quality];
56}
57
58/* Parse VALUE as profile quality and return true when a valid QUALITY. */
59
60bool
61parse_profile_quality (const char *value, profile_quality *quality)
62{
63 for (unsigned i = 0; i < ARRAY_SIZE (profile_quality_names); i++)
64 if (strcmp (profile_quality_names[i], value) == 0)
65 {
66 *quality = (profile_quality)i;
67 return true;
68 }
69
70 return false;
9dcf2a11 71}
72
6b33947d 73/* Display names from profile_quality enum values. */
74
75const char *profile_quality_display_names[] =
76{
77 NULL,
78 "estimated locally",
79 "estimated locally, globally 0",
80 "estimated locally, globally 0 adjusted",
81 "adjusted",
82 "auto FDO",
83 "guessed",
84 "precise"
85};
86
720cfc43 87/* Dump THIS to F. */
88
db9cef39 89void
90profile_count::dump (FILE *f) const
91{
92 if (!initialized_p ())
93 fprintf (f, "uninitialized");
94 else
6b33947d 95 fprintf (f, "%" PRId64 " (%s)", m_val,
96 profile_quality_display_names[m_quality]);
db9cef39 97}
98
720cfc43 99/* Dump THIS to stderr. */
100
db9cef39 101void
102profile_count::debug () const
103{
104 dump (stderr);
720cfc43 105 fprintf (stderr, "\n");
db9cef39 106}
107
720cfc43 108/* Return true if THIS differs from OTHER; tolerate small diferences. */
109
db9cef39 110bool
111profile_count::differs_from_p (profile_count other) const
112{
205ce1aa 113 gcc_checking_assert (compatible_p (other));
db9cef39 114 if (!initialized_p () || !other.initialized_p ())
115 return false;
720cfc43 116 if ((uint64_t)m_val - (uint64_t)other.m_val < 100
117 || (uint64_t)other.m_val - (uint64_t)m_val < 100)
db9cef39 118 return false;
119 if (!other.m_val)
120 return true;
720cfc43 121 int64_t ratio = (int64_t)m_val * 100 / other.m_val;
db9cef39 122 return ratio < 99 || ratio > 101;
123}
124
720cfc43 125/* Stream THIS from IB. */
126
db9cef39 127profile_count
2e966e2a 128profile_count::stream_in (class lto_input_block *ib)
db9cef39 129{
130 profile_count ret;
131 ret.m_val = streamer_read_gcov_count (ib);
720cfc43 132 ret.m_quality = (profile_quality) streamer_read_uhwi (ib);
db9cef39 133 return ret;
134}
135
720cfc43 136/* Stream THIS to OB. */
137
db9cef39 138void
139profile_count::stream_out (struct output_block *ob)
140{
141 streamer_write_gcov_count (ob, m_val);
51547b02 142 streamer_write_uhwi (ob, m_quality);
db9cef39 143}
144
720cfc43 145/* Stream THIS to OB. */
146
db9cef39 147void
148profile_count::stream_out (struct lto_output_stream *ob)
149{
150 streamer_write_gcov_count_stream (ob, m_val);
51547b02 151 streamer_write_uhwi_stream (ob, m_quality);
db9cef39 152}
720cfc43 153
154/* Dump THIS to F. */
155
156void
157profile_probability::dump (FILE *f) const
158{
159 if (!initialized_p ())
160 fprintf (f, "uninitialized");
161 else
162 {
163 /* Make difference between 0.00 as a roundoff error and actual 0.
164 Similarly for 1. */
165 if (m_val == 0)
166 fprintf (f, "never");
167 else if (m_val == max_probability)
168 fprintf (f, "always");
169 else
170 fprintf (f, "%3.1f%%", (double)m_val * 100 / max_probability);
5eb3f719 171 if (m_quality == ADJUSTED)
720cfc43 172 fprintf (f, " (adjusted)");
5eb3f719 173 else if (m_quality == AFDO)
720cfc43 174 fprintf (f, " (auto FDO)");
5eb3f719 175 else if (m_quality == GUESSED)
720cfc43 176 fprintf (f, " (guessed)");
177 }
178}
179
180/* Dump THIS to stderr. */
181
182void
183profile_probability::debug () const
184{
185 dump (stderr);
186 fprintf (stderr, "\n");
187}
188
189/* Return true if THIS differs from OTHER; tolerate small diferences. */
190
191bool
192profile_probability::differs_from_p (profile_probability other) const
193{
194 if (!initialized_p () || !other.initialized_p ())
195 return false;
9c0c95d8 196 if ((uint64_t)m_val - (uint64_t)other.m_val < max_probability / 1000
197 || (uint64_t)other.m_val - (uint64_t)max_probability < 1000)
720cfc43 198 return false;
199 if (!other.m_val)
200 return true;
9c0c95d8 201 int64_t ratio = (int64_t)m_val * 100 / other.m_val;
720cfc43 202 return ratio < 99 || ratio > 101;
203}
204
205/* Return true if THIS differs significantly from OTHER. */
206
207bool
208profile_probability::differs_lot_from_p (profile_probability other) const
209{
210 if (!initialized_p () || !other.initialized_p ())
211 return false;
212 uint32_t d = m_val > other.m_val ? m_val - other.m_val : other.m_val - m_val;
213 return d > max_probability / 2;
214}
215
216/* Stream THIS from IB. */
217
218profile_probability
2e966e2a 219profile_probability::stream_in (class lto_input_block *ib)
720cfc43 220{
221 profile_probability ret;
222 ret.m_val = streamer_read_uhwi (ib);
223 ret.m_quality = (profile_quality) streamer_read_uhwi (ib);
224 return ret;
225}
226
227/* Stream THIS to OB. */
228
229void
230profile_probability::stream_out (struct output_block *ob)
231{
232 streamer_write_uhwi (ob, m_val);
233 streamer_write_uhwi (ob, m_quality);
234}
235
236/* Stream THIS to OB. */
237
238void
239profile_probability::stream_out (struct lto_output_stream *ob)
240{
241 streamer_write_uhwi_stream (ob, m_val);
242 streamer_write_uhwi_stream (ob, m_quality);
243}
4cb91b78 244
245/* Compute RES=(a*b + c/2)/c capping and return false if overflow happened. */
246
247bool
248slow_safe_scale_64bit (uint64_t a, uint64_t b, uint64_t c, uint64_t *res)
249{
250 FIXED_WIDE_INT (128) tmp = a;
30b5769f 251 wi::overflow_type overflow;
4cb91b78 252 tmp = wi::udiv_floor (wi::umul (tmp, b, &overflow) + (c / 2), c);
253 gcc_checking_assert (!overflow);
254 if (wi::fits_uhwi_p (tmp))
255 {
256 *res = tmp.to_uhwi ();
257 return true;
258 }
259 *res = (uint64_t) -1;
260 return false;
261}
205ce1aa 262
263/* Return count as frequency within FUN scaled in range 0 to REG_FREQ_MAX
264 Used for legacy code and should not be used anymore. */
265
266int
267profile_count::to_frequency (struct function *fun) const
268{
269 if (!initialized_p ())
270 return BB_FREQ_MAX;
3451e82b 271 if (*this == zero ())
205ce1aa 272 return 0;
273 gcc_assert (REG_BR_PROB_BASE == BB_FREQ_MAX
274 && fun->cfg->count_max.initialized_p ());
275 profile_probability prob = probability_in (fun->cfg->count_max);
276 if (!prob.initialized_p ())
277 return REG_BR_PROB_BASE;
278 return prob.to_reg_br_prob_base ();
279}
280
281/* Return count as frequency within FUN scaled in range 0 to CGRAPH_FREQ_MAX
282 where CGRAPH_FREQ_BASE means that count equals to entry block count.
283 Used for legacy code and should not be used anymore. */
284
285int
286profile_count::to_cgraph_frequency (profile_count entry_bb_count) const
287{
ed0831a9 288 if (!initialized_p () || !entry_bb_count.initialized_p ())
205ce1aa 289 return CGRAPH_FREQ_BASE;
3451e82b 290 if (*this == zero ())
205ce1aa 291 return 0;
292 gcc_checking_assert (entry_bb_count.initialized_p ());
293 uint64_t scale;
294 if (!safe_scale_64bit (!entry_bb_count.m_val ? m_val + 1 : m_val,
295 CGRAPH_FREQ_BASE, MAX (1, entry_bb_count.m_val), &scale))
296 return CGRAPH_FREQ_MAX;
297 return MIN (scale, CGRAPH_FREQ_MAX);
298}
371858d4 299
7c343235 300/* Return THIS/IN as sreal value. */
301
302sreal
303profile_count::to_sreal_scale (profile_count in, bool *known) const
304{
ed0831a9 305 if (!initialized_p () || !in.initialized_p ())
7c343235 306 {
307 if (known)
308 *known = false;
e1364c65 309 return 1;
7c343235 310 }
311 if (known)
312 *known = true;
3451e82b 313 if (*this == zero ())
7c343235 314 return 0;
7c343235 315
316 if (!in.m_val)
317 {
318 if (!m_val)
319 return 1;
320 return m_val * 4;
321 }
322 return (sreal)m_val / (sreal)in.m_val;
323}
324
371858d4 325/* We want to scale profile across function boundary from NUM to DEN.
326 Take care of the side case when DEN is zeros. We still want to behave
327 sanely here which means
328 - scale to profile_count::zero () if NUM is profile_count::zero
329 - do not affect anything if NUM == DEN
330 - preserve counter value but adjust quality in other cases. */
331
332void
333profile_count::adjust_for_ipa_scaling (profile_count *num,
334 profile_count *den)
335{
336 /* Scaling is no-op if NUM and DEN are the same. */
337 if (*num == *den)
338 return;
ed0831a9 339 /* Scaling to zero is always zero. */
3451e82b 340 if (*num == zero ())
371858d4 341 return;
342 /* If den is non-zero we are safe. */
343 if (den->force_nonzero () == *den)
344 return;
345 /* Force both to non-zero so we do not push profiles to 0 when
346 both num == 0 and den == 0. */
347 *den = den->force_nonzero ();
348 *num = num->force_nonzero ();
349}
ed0831a9 350
351/* THIS is a count of bb which is known to be executed IPA times.
352 Combine this information into bb counter. This means returning IPA
353 if it is nonzero, not changing anything if IPA is uninitialized
354 and if IPA is zero, turning THIS into corresponding local profile with
355 global0. */
1569cde1 356
ed0831a9 357profile_count
358profile_count::combine_with_ipa_count (profile_count ipa)
359{
360 ipa = ipa.ipa ();
361 if (ipa.nonzero_p ())
362 return ipa;
3451e82b 363 if (!ipa.initialized_p () || *this == zero ())
ed0831a9 364 return *this;
3451e82b 365 if (ipa == zero ())
ed0831a9 366 return this->global0 ();
367 return this->global0adjusted ();
368}
a22775d8 369
370/* The profiling runtime uses gcov_type, which is usually 64bit integer.
371 Conversions back and forth are used to read the coverage and get it
372 into internal representation. */
1569cde1 373
a22775d8 374profile_count
6b33947d 375profile_count::from_gcov_type (gcov_type v, profile_quality quality)
a22775d8 376 {
377 profile_count ret;
378 gcc_checking_assert (v >= 0);
379 if (dump_file && v >= (gcov_type)max_count)
380 fprintf (dump_file,
381 "Capping gcov count %" PRId64 " to max_count %" PRId64 "\n",
382 (int64_t) v, (int64_t) max_count);
383 ret.m_val = MIN (v, (gcov_type)max_count);
6b33947d 384 ret.m_quality = quality;
a22775d8 385 return ret;
386 }
387
5b94633f 388/* COUNT1 times event happens with *THIS probability, COUNT2 times OTHER
389 happens with COUNT2 probablity. Return probablity that either *THIS or
390 OTHER happens. */
391
392profile_probability
393profile_probability::combine_with_count (profile_count count1,
394 profile_probability other,
395 profile_count count2) const
396{
397 /* If probabilities are same, we are done.
398 If counts are nonzero we can distribute accordingly. In remaining
399 cases just avreage the values and hope for the best. */
400 if (*this == other || count1 == count2
401 || (count2 == profile_count::zero ()
402 && !(count1 == profile_count::zero ())))
403 return *this;
404 if (count1 == profile_count::zero () && !(count2 == profile_count::zero ()))
405 return other;
406 else if (count1.nonzero_p () || count2.nonzero_p ())
407 return *this * count1.probability_in (count1 + count2)
408 + other * count2.probability_in (count1 + count2);
409 else
3451e82b 410 return *this * even () + other * even ();
5b94633f 411}