]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/profile-count.h
RISC-V: Add testcase for pr114734
[thirdparty/gcc.git] / gcc / profile-count.h
CommitLineData
3995f3a2 1/* Profile counter container type.
a945c346 2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3995f3a2
JH
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#ifndef GCC_PROFILE_COUNT_H
22#define GCC_PROFILE_COUNT_H
23
e7a74006 24struct function;
99b1c316 25struct profile_count;
f5b25e15 26class sreal;
e7a74006 27
5ef4d61d
AK
28/* Quality of the profile count. Because gengtype does not support enums
29 inside of classes, this is in global namespace. */
357067f2 30enum profile_quality {
17594687 31 /* Uninitialized value. */
e48dc99e 32 UNINITIALIZED_PROFILE,
03105885 33
e7a74006 34 /* Profile is based on static branch prediction heuristics and may
67914693 35 or may not match reality. It is local to function and cannot be compared
e7a74006
JH
36 inter-procedurally. Never used by probabilities (they are always local).
37 */
e48dc99e 38 GUESSED_LOCAL,
03105885 39
e7a74006 40 /* Profile was read by feedback and was 0, we used local heuristics to guess
956d615d 41 better. This is the case of functions not run in profile feedback.
e7a74006 42 Never used by probabilities. */
e48dc99e 43 GUESSED_GLOBAL0,
e7a74006 44
e48dc99e
ML
45 /* Same as GUESSED_GLOBAL0 but global count is adjusted 0. */
46 GUESSED_GLOBAL0_ADJUSTED,
e7a74006 47
4e9a497f 48 /* Profile is based on static branch prediction heuristics. It may or may
e7a74006
JH
49 not reflect the reality but it can be compared interprocedurally
50 (for example, we inlined function w/o profile feedback into function
51 with feedback and propagated from that).
956d615d 52 Never used by probabilities. */
e48dc99e 53 GUESSED,
03105885 54
4e9a497f 55 /* Profile was determined by autofdo. */
e48dc99e 56 AFDO,
03105885 57
5ef4d61d 58 /* Profile was originally based on feedback but it was adjusted
4e9a497f
JH
59 by code duplicating optimization. It may not precisely reflect the
60 particular code path. */
e48dc99e 61 ADJUSTED,
03105885 62
4e9a497f
JH
63 /* Profile was read from profile feedback or determined by accurate static
64 method. */
e48dc99e 65 PRECISE
4e9a497f 66};
3995f3a2 67
4a4412b9 68extern const char *profile_quality_as_string (enum profile_quality);
d276406a
ML
69extern bool parse_profile_quality (const char *value,
70 profile_quality *quality);
4a4412b9 71
3995f3a2
JH
72/* The base value for branch probability notes and edge probabilities. */
73#define REG_BR_PROB_BASE 10000
74
75#define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
76
9588ea78
JH
77bool slow_safe_scale_64bit (uint64_t a, uint64_t b, uint64_t c, uint64_t *res);
78
79/* Compute RES=(a*b + c/2)/c capping and return false if overflow happened. */
80
81inline bool
82safe_scale_64bit (uint64_t a, uint64_t b, uint64_t c, uint64_t *res)
83{
84#if (GCC_VERSION >= 5000)
85 uint64_t tmp;
86 if (!__builtin_mul_overflow (a, b, &tmp)
87 && !__builtin_add_overflow (tmp, c/2, &tmp))
88 {
89 *res = tmp / c;
90 return true;
91 }
92 if (c == 1)
93 {
94 *res = (uint64_t) -1;
95 return false;
96 }
97#else
98 if (a < ((uint64_t)1 << 31)
99 && b < ((uint64_t)1 << 31)
100 && c < ((uint64_t)1 << 31))
83d502cf
JH
101 {
102 *res = (a * b + (c / 2)) / c;
103 return true;
104 }
9588ea78
JH
105#endif
106 return slow_safe_scale_64bit (a, b, c, res);
107}
108
5ef4d61d 109/* Data type to hold probabilities. It implements fixed point arithmetics
357067f2
JH
110 with capping so probability is always in range [0,1] and scaling requiring
111 values greater than 1 needs to be represented otherwise.
112
113 In addition to actual value the quality of profile is tracked and propagated
e48dc99e 114 through all operations. Special value UNINITIALIZED_PROFILE is used for probabilities
956d615d 115 that has not been determined yet (for example because of
357067f2
JH
116 -fno-guess-branch-probability)
117
118 Typically probabilities are derived from profile feedback (via
119 probability_in_gcov_type), autoFDO or guessed statically and then propagated
120 thorough the compilation.
121
122 Named probabilities are available:
123 - never (0 probability)
124 - guessed_never
125 - very_unlikely (1/2000 probability)
956d615d 126 - unlikely (1/5 probability)
357067f2
JH
127 - even (1/2 probability)
128 - likely (4/5 probability)
129 - very_likely (1999/2000 probability)
130 - guessed_always
131 - always
132
133 Named probabilities except for never/always are assumed to be statically
5ef4d61d
AK
134 guessed and thus not necessarily accurate. The difference between never
135 and guessed_never is that the first one should be used only in case that
357067f2
JH
136 well behaving program will very likely not execute the "never" path.
137 For example if the path is going to abort () call or it exception handling.
138
5ef4d61d 139 Always and guessed_always probabilities are symmetric.
357067f2
JH
140
141 For legacy code we support conversion to/from REG_BR_PROB_BASE based fixpoint
5ef4d61d 142 integer arithmetics. Once the code is converted to branch probabilities,
357067f2
JH
143 these conversions will probably go away because they are lossy.
144*/
145
146class GTY((user)) profile_probability
147{
e7a74006 148 static const int n_bits = 29;
83d502cf
JH
149 /* We can technically use ((uint32_t) 1 << (n_bits - 1)) - 2 but that
150 will lead to harder multiplication sequences. */
151 static const uint32_t max_probability = (uint32_t) 1 << (n_bits - 2);
9588ea78
JH
152 static const uint32_t uninitialized_probability
153 = ((uint32_t) 1 << (n_bits - 1)) - 1;
357067f2 154
e7a74006
JH
155 uint32_t m_val : 29;
156 enum profile_quality m_quality : 3;
357067f2 157
99b1c316 158 friend struct profile_count;
357067f2 159public:
d276406a 160 profile_probability (): m_val (uninitialized_probability),
e48dc99e 161 m_quality (GUESSED)
d276406a
ML
162 {}
163
164 profile_probability (uint32_t val, profile_quality quality):
165 m_val (val), m_quality (quality)
166 {}
357067f2
JH
167
168 /* Named probabilities. */
169 static profile_probability never ()
170 {
171 profile_probability ret;
172 ret.m_val = 0;
e48dc99e 173 ret.m_quality = PRECISE;
357067f2
JH
174 return ret;
175 }
03105885 176
357067f2
JH
177 static profile_probability guessed_never ()
178 {
179 profile_probability ret;
180 ret.m_val = 0;
e48dc99e 181 ret.m_quality = GUESSED;
357067f2
JH
182 return ret;
183 }
03105885 184
357067f2
JH
185 static profile_probability very_unlikely ()
186 {
187 /* Be consistent with PROB_VERY_UNLIKELY in predict.h. */
9f55aee9 188 profile_probability r = guessed_always () / 2000;
357067f2
JH
189 r.m_val--;
190 return r;
191 }
03105885 192
357067f2
JH
193 static profile_probability unlikely ()
194 {
195 /* Be consistent with PROB_VERY_LIKELY in predict.h. */
9f55aee9 196 profile_probability r = guessed_always () / 5;
357067f2
JH
197 r.m_val--;
198 return r;
199 }
03105885 200
357067f2
JH
201 static profile_probability even ()
202 {
9f55aee9 203 return guessed_always () / 2;
357067f2 204 }
03105885 205
357067f2
JH
206 static profile_probability very_likely ()
207 {
a4c3f08d 208 return always () - very_unlikely ();
357067f2 209 }
03105885 210
357067f2
JH
211 static profile_probability likely ()
212 {
a4c3f08d 213 return always () - unlikely ();
357067f2 214 }
2e406f07
JH
215 /* Return true when value is not zero and can be used for scaling. */
216 bool nonzero_p () const
217 {
218 return initialized_p () && m_val != 0;
219 }
03105885 220
357067f2
JH
221 static profile_probability guessed_always ()
222 {
223 profile_probability ret;
224 ret.m_val = max_probability;
e48dc99e 225 ret.m_quality = GUESSED;
357067f2
JH
226 return ret;
227 }
03105885 228
357067f2
JH
229 static profile_probability always ()
230 {
231 profile_probability ret;
232 ret.m_val = max_probability;
e48dc99e 233 ret.m_quality = PRECISE;
357067f2
JH
234 return ret;
235 }
03105885 236
357067f2
JH
237 /* Probabilities which has not been initialized. Either because
238 initialization did not happen yet or because profile is unknown. */
239 static profile_probability uninitialized ()
240 {
241 profile_probability c;
242 c.m_val = uninitialized_probability;
e48dc99e 243 c.m_quality = GUESSED;
357067f2
JH
244 return c;
245 }
246
357067f2
JH
247 /* Return true if value has been initialized. */
248 bool initialized_p () const
249 {
250 return m_val != uninitialized_probability;
251 }
03105885 252
357067f2
JH
253 /* Return true if value can be trusted. */
254 bool reliable_p () const
255 {
e48dc99e 256 return m_quality >= ADJUSTED;
357067f2
JH
257 }
258
259 /* Conversion from and to REG_BR_PROB_BASE integer fixpoint arithmetics.
5ef4d61d 260 this is mostly to support legacy code and should go away. */
357067f2
JH
261 static profile_probability from_reg_br_prob_base (int v)
262 {
263 profile_probability ret;
264 gcc_checking_assert (v >= 0 && v <= REG_BR_PROB_BASE);
83d502cf 265 ret.m_val = RDIV (v * (uint64_t) max_probability, REG_BR_PROB_BASE);
e48dc99e 266 ret.m_quality = GUESSED;
357067f2
JH
267 return ret;
268 }
03105885 269
845bb366
JH
270 /* Return THIS with quality set to ADJUSTED. */
271 profile_probability adjusted () const
272 {
273 profile_probability ret = *this;
274 if (!initialized_p ())
275 return *this;
276 ret.m_quality = ADJUSTED;
277 return ret;
278 }
279
357067f2
JH
280 int to_reg_br_prob_base () const
281 {
282 gcc_checking_assert (initialized_p ());
83d502cf 283 return RDIV (m_val * (uint64_t) REG_BR_PROB_BASE, max_probability);
357067f2
JH
284 }
285
5fa396ad
JH
286 /* Conversion to and from RTL representation of profile probabilities. */
287 static profile_probability from_reg_br_prob_note (int v)
288 {
289 profile_probability ret;
e7a74006
JH
290 ret.m_val = ((unsigned int)v) / 8;
291 ret.m_quality = (enum profile_quality)(v & 7);
5fa396ad
JH
292 return ret;
293 }
03105885 294
5fa396ad
JH
295 int to_reg_br_prob_note () const
296 {
297 gcc_checking_assert (initialized_p ());
e7a74006 298 int ret = m_val * 8 + m_quality;
a4c3f08d 299 gcc_checking_assert (from_reg_br_prob_note (ret) == *this);
5fa396ad
JH
300 return ret;
301 }
302
357067f2
JH
303 /* Return VAL1/VAL2. */
304 static profile_probability probability_in_gcov_type
305 (gcov_type val1, gcov_type val2)
306 {
307 profile_probability ret;
308 gcc_checking_assert (val1 >= 0 && val2 > 0);
309 if (val1 > val2)
310 ret.m_val = max_probability;
311 else
83d502cf
JH
312 {
313 uint64_t tmp;
314 safe_scale_64bit (val1, max_probability, val2, &tmp);
315 gcc_checking_assert (tmp <= max_probability);
316 ret.m_val = tmp;
317 }
e48dc99e 318 ret.m_quality = PRECISE;
357067f2
JH
319 return ret;
320 }
321
322 /* Basic operations. */
323 bool operator== (const profile_probability &other) const
324 {
325 return m_val == other.m_val && m_quality == other.m_quality;
326 }
03105885 327
357067f2
JH
328 profile_probability operator+ (const profile_probability &other) const
329 {
a4c3f08d 330 if (other == never ())
357067f2 331 return *this;
a4c3f08d 332 if (*this == never ())
357067f2
JH
333 return other;
334 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 335 return uninitialized ();
357067f2
JH
336
337 profile_probability ret;
338 ret.m_val = MIN ((uint32_t)(m_val + other.m_val), max_probability);
339 ret.m_quality = MIN (m_quality, other.m_quality);
340 return ret;
341 }
03105885 342
357067f2
JH
343 profile_probability &operator+= (const profile_probability &other)
344 {
a4c3f08d 345 if (other == never ())
357067f2 346 return *this;
a4c3f08d 347 if (*this == never ())
357067f2
JH
348 {
349 *this = other;
350 return *this;
351 }
352 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 353 return *this = uninitialized ();
357067f2
JH
354 else
355 {
356 m_val = MIN ((uint32_t)(m_val + other.m_val), max_probability);
5ef4d61d 357 m_quality = MIN (m_quality, other.m_quality);
357067f2
JH
358 }
359 return *this;
360 }
03105885 361
357067f2
JH
362 profile_probability operator- (const profile_probability &other) const
363 {
a4c3f08d
ML
364 if (*this == never ()
365 || other == never ())
357067f2
JH
366 return *this;
367 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 368 return uninitialized ();
357067f2
JH
369 profile_probability ret;
370 ret.m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
371 ret.m_quality = MIN (m_quality, other.m_quality);
372 return ret;
373 }
03105885 374
357067f2
JH
375 profile_probability &operator-= (const profile_probability &other)
376 {
a4c3f08d
ML
377 if (*this == never ()
378 || other == never ())
357067f2
JH
379 return *this;
380 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 381 return *this = uninitialized ();
357067f2
JH
382 else
383 {
384 m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
5ef4d61d 385 m_quality = MIN (m_quality, other.m_quality);
357067f2
JH
386 }
387 return *this;
388 }
03105885 389
357067f2
JH
390 profile_probability operator* (const profile_probability &other) const
391 {
a4c3f08d
ML
392 if (*this == never ()
393 || other == never ())
394 return never ();
357067f2 395 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 396 return uninitialized ();
357067f2
JH
397 profile_probability ret;
398 ret.m_val = RDIV ((uint64_t)m_val * other.m_val, max_probability);
e48dc99e 399 ret.m_quality = MIN (MIN (m_quality, other.m_quality), ADJUSTED);
357067f2
JH
400 return ret;
401 }
03105885 402
357067f2
JH
403 profile_probability &operator*= (const profile_probability &other)
404 {
a4c3f08d
ML
405 if (*this == never ()
406 || other == never ())
407 return *this = never ();
357067f2 408 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 409 return *this = uninitialized ();
357067f2
JH
410 else
411 {
5ef4d61d 412 m_val = RDIV ((uint64_t)m_val * other.m_val, max_probability);
e48dc99e 413 m_quality = MIN (MIN (m_quality, other.m_quality), ADJUSTED);
357067f2
JH
414 }
415 return *this;
416 }
03105885 417
357067f2
JH
418 profile_probability operator/ (const profile_probability &other) const
419 {
a4c3f08d
ML
420 if (*this == never ())
421 return never ();
357067f2 422 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 423 return uninitialized ();
357067f2 424 profile_probability ret;
97c07987 425 /* If we get probability above 1, mark it as unreliable and return 1. */
357067f2 426 if (m_val >= other.m_val)
97c07987
JH
427 {
428 ret.m_val = max_probability;
429 ret.m_quality = MIN (MIN (m_quality, other.m_quality),
e48dc99e 430 GUESSED);
97c07987
JH
431 return ret;
432 }
357067f2
JH
433 else if (!m_val)
434 ret.m_val = 0;
435 else
436 {
437 gcc_checking_assert (other.m_val);
5ef4d61d 438 ret.m_val = MIN (RDIV ((uint64_t)m_val * max_probability,
357067f2
JH
439 other.m_val),
440 max_probability);
441 }
e48dc99e 442 ret.m_quality = MIN (MIN (m_quality, other.m_quality), ADJUSTED);
357067f2
JH
443 return ret;
444 }
03105885 445
357067f2
JH
446 profile_probability &operator/= (const profile_probability &other)
447 {
a4c3f08d
ML
448 if (*this == never ())
449 return *this = never ();
357067f2 450 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 451 return *this = uninitialized ();
357067f2
JH
452 else
453 {
97c07987
JH
454 /* If we get probability above 1, mark it as unreliable
455 and return 1. */
357067f2 456 if (m_val > other.m_val)
97c07987
JH
457 {
458 m_val = max_probability;
459 m_quality = MIN (MIN (m_quality, other.m_quality),
e48dc99e 460 GUESSED);
97c07987
JH
461 return *this;
462 }
357067f2
JH
463 else if (!m_val)
464 ;
465 else
466 {
467 gcc_checking_assert (other.m_val);
5ef4d61d 468 m_val = MIN (RDIV ((uint64_t)m_val * max_probability,
357067f2
JH
469 other.m_val),
470 max_probability);
471 }
e48dc99e 472 m_quality = MIN (MIN (m_quality, other.m_quality), ADJUSTED);
357067f2
JH
473 }
474 return *this;
475 }
476
f5c517f0
JJ
477 /* Split *THIS (ORIG) probability into 2 probabilities, such that
478 the returned one (FIRST) is *THIS * CPROB and *THIS is
479 adjusted (SECOND) so that FIRST + FIRST.invert () * SECOND
480 == ORIG. This is useful e.g. when splitting a conditional
481 branch like:
482 if (cond)
483 goto lab; // ORIG probability
484 into
485 if (cond1)
486 goto lab; // FIRST = ORIG * CPROB probability
487 if (cond2)
488 goto lab; // SECOND probability
489 such that the overall probability of jumping to lab remains
490 the same. CPROB gives the relative probability between the
491 branches. */
492 profile_probability split (const profile_probability &cprob)
493 {
494 profile_probability ret = *this * cprob;
495 /* The following is equivalent to:
f0dbeec7
JH
496 *this = cprob.invert () * *this / ret.invert ();
497 Avoid scaling when overall outcome is supposed to be always.
956d615d 498 Without knowing that one is inverse of other, the result would be
f0dbeec7 499 conservative. */
a4c3f08d 500 if (!(*this == always ()))
f0dbeec7 501 *this = (*this - ret) / ret.invert ();
f5c517f0
JJ
502 return ret;
503 }
504
357067f2
JH
505 gcov_type apply (gcov_type val) const
506 {
a4c3f08d 507 if (*this == uninitialized ())
357067f2
JH
508 return val / 2;
509 return RDIV (val * m_val, max_probability);
510 }
511
512 /* Return 1-*THIS. */
513 profile_probability invert () const
514 {
a4c3f08d 515 return always() - *this;
357067f2
JH
516 }
517
2f70a979
JH
518 /* Return THIS with quality dropped to GUESSED. */
519 profile_probability guessed () const
520 {
521 profile_probability ret = *this;
e48dc99e 522 ret.m_quality = GUESSED;
2f70a979
JH
523 return ret;
524 }
525
526 /* Return THIS with quality dropped to AFDO. */
527 profile_probability afdo () const
528 {
529 profile_probability ret = *this;
e48dc99e 530 ret.m_quality = AFDO;
2f70a979
JH
531 return ret;
532 }
533
357067f2
JH
534 /* Return *THIS * NUM / DEN. */
535 profile_probability apply_scale (int64_t num, int64_t den) const
536 {
a4c3f08d 537 if (*this == never ())
357067f2
JH
538 return *this;
539 if (!initialized_p ())
a4c3f08d 540 return uninitialized ();
357067f2 541 profile_probability ret;
83d502cf
JH
542 uint64_t tmp;
543 safe_scale_64bit (m_val, num, den, &tmp);
544 ret.m_val = MIN (tmp, max_probability);
e48dc99e 545 ret.m_quality = MIN (m_quality, ADJUSTED);
357067f2
JH
546 return ret;
547 }
548
2e406f07
JH
549 /* Return *THIS * NUM / DEN. */
550 profile_probability apply_scale (profile_probability num,
551 profile_probability den) const
552 {
553 if (*this == never ())
554 return *this;
555 if (num == never ())
556 return num;
557 if (!initialized_p () || !num.initialized_p () || !den.initialized_p ())
558 return uninitialized ();
559 if (num == den)
560 return *this;
561 gcc_checking_assert (den.m_val);
562
563 profile_probability ret;
564 uint64_t val;
565 safe_scale_64bit (m_val, num.m_val, den.m_val, &val);
566 ret.m_val = MIN (val, max_probability);
567 ret.m_quality = MIN (MIN (MIN (m_quality, ADJUSTED),
568 num.m_quality), den.m_quality);
569 return ret;
570 }
571
357067f2
JH
572 /* Return true when the probability of edge is reliable.
573
956d615d 574 The profile guessing code is good at predicting branch outcome (i.e.
357067f2
JH
575 taken/not taken), that is predicted right slightly over 75% of time.
576 It is however notoriously poor on predicting the probability itself.
577 In general the profile appear a lot flatter (with probabilities closer
578 to 50%) than the reality so it is bad idea to use it to drive optimization
579 such as those disabling dynamic branch prediction for well predictable
580 branches.
581
582 There are two exceptions - edges leading to noreturn edges and edges
583 predicted by number of iterations heuristics are predicted well. This macro
584 should be able to distinguish those, but at the moment it simply check for
585 noreturn heuristic that is only one giving probability over 99% or bellow
586 1%. In future we might want to propagate reliability information across the
587 CFG if we find this information useful on multiple places. */
357067f2
JH
588 bool probably_reliable_p () const
589 {
e48dc99e 590 if (m_quality >= ADJUSTED)
357067f2
JH
591 return true;
592 if (!initialized_p ())
593 return false;
594 return m_val < max_probability / 100
595 || m_val > max_probability - max_probability / 100;
596 }
597
598 /* Return false if profile_probability is bogus. */
599 bool verify () const
600 {
e48dc99e 601 gcc_checking_assert (m_quality != UNINITIALIZED_PROFILE);
357067f2 602 if (m_val == uninitialized_probability)
e48dc99e
ML
603 return m_quality == GUESSED;
604 else if (m_quality < GUESSED)
e7a74006
JH
605 return false;
606 return m_val <= max_probability;
357067f2
JH
607 }
608
956d615d 609 /* Comparisons are three-state and conservative. False is returned if
67914693 610 the inequality cannot be decided. */
357067f2
JH
611 bool operator< (const profile_probability &other) const
612 {
613 return initialized_p () && other.initialized_p () && m_val < other.m_val;
614 }
03105885 615
357067f2
JH
616 bool operator> (const profile_probability &other) const
617 {
618 return initialized_p () && other.initialized_p () && m_val > other.m_val;
619 }
620
621 bool operator<= (const profile_probability &other) const
622 {
623 return initialized_p () && other.initialized_p () && m_val <= other.m_val;
624 }
03105885 625
357067f2
JH
626 bool operator>= (const profile_probability &other) const
627 {
628 return initialized_p () && other.initialized_p () && m_val >= other.m_val;
629 }
630
9f55aee9
ML
631 profile_probability operator* (int64_t num) const
632 {
633 return apply_scale (num, 1);
634 }
635
268b5c81 636 profile_probability operator*= (int64_t num)
9f55aee9 637 {
268b5c81
ML
638 *this = apply_scale (num, 1);
639 return *this;
9f55aee9
ML
640 }
641
642 profile_probability operator/ (int64_t den) const
643 {
644 return apply_scale (1, den);
645 }
646
268b5c81 647 profile_probability operator/= (int64_t den)
9f55aee9 648 {
268b5c81
ML
649 *this = apply_scale (1, den);
650 return *this;
9f55aee9
ML
651 }
652
7ed98195
JH
653 /* Compute n-th power. */
654 profile_probability pow (int) const;
655
656 /* Compute sware root. */
657 profile_probability sqrt () const;
658
d276406a
ML
659 /* Get the value of the count. */
660 uint32_t value () const { return m_val; }
661
662 /* Get the quality of the count. */
663 enum profile_quality quality () const { return m_quality; }
664
357067f2
JH
665 /* Output THIS to F. */
666 void dump (FILE *f) const;
667
df704591
ML
668 /* Output THIS to BUFFER. */
669 void dump (char *buffer) const;
670
357067f2
JH
671 /* Print THIS to stderr. */
672 void debug () const;
673
674 /* Return true if THIS is known to differ significantly from OTHER. */
675 bool differs_from_p (profile_probability other) const;
03105885 676
357067f2
JH
677 /* Return if difference is greater than 50%. */
678 bool differs_lot_from_p (profile_probability other) const;
03105885 679
97c07987 680 /* COUNT1 times event happens with *THIS probability, COUNT2 times OTHER
956d615d 681 happens with COUNT2 probability. Return probability that either *THIS or
97c07987
JH
682 OTHER happens. */
683 profile_probability combine_with_count (profile_count count1,
684 profile_probability other,
685 profile_count count2) const;
357067f2 686
f5b25e15
JH
687 /* Return probability as sreal. */
688 sreal to_sreal () const;
357067f2 689 /* LTO streaming support. */
99b1c316 690 static profile_probability stream_in (class lto_input_block *);
357067f2
JH
691 void stream_out (struct output_block *);
692 void stream_out (struct lto_output_stream *);
693};
694
e7a74006
JH
695/* Main data type to hold profile counters in GCC. Profile counts originate
696 either from profile feedback, static profile estimation or both. We do not
697 perform whole program profile propagation and thus profile estimation
698 counters are often local to function, while counters from profile feedback
699 (or special cases of profile estimation) can be used inter-procedurally.
700
701 There are 3 basic types
702 1) local counters which are result of intra-procedural static profile
703 estimation.
704 2) ipa counters which are result of profile feedback or special case
705 of static profile estimation (such as in function main).
956d615d 706 3) counters which counts as 0 inter-procedurally (because given function
e7a74006
JH
707 was never run in train feedback) but they hold local static profile
708 estimate.
709
67914693 710 Counters of type 1 and 3 cannot be mixed with counters of different type
e7a74006
JH
711 within operation (because whole function should use one type of counter)
712 with exception that global zero mix in most operations where outcome is
713 well defined.
714
715 To take local counter and use it inter-procedurally use ipa member function
956d615d 716 which strips information irrelevant at the inter-procedural level.
e7a74006
JH
717
718 Counters are 61bit integers representing number of executions during the
719 train run or normalized frequency within the function.
720
3995f3a2
JH
721 As the profile is maintained during the compilation, many adjustments are
722 made. Not all transformations can be made precisely, most importantly
723 when code is being duplicated. It also may happen that part of CFG has
724 profile counts known while other do not - for example when LTO optimizing
725 partly profiled program or when profile was lost due to COMDAT merging.
726
b69d9ac6 727 For this reason profile_count tracks more information than
3995f3a2
JH
728 just unsigned integer and it is also ready for profile mismatches.
729 The API of this data type represent operations that are natural
730 on profile counts - sum, difference and operation with scales and
731 probabilities. All operations are safe by never getting negative counts
732 and they do end up in uninitialized scale if any of the parameters is
733 uninitialized.
734
956d615d 735 All comparisons that are three state and handling of probabilities. Thus
3995f3a2
JH
736 a < b is not equal to !(a >= b).
737
738 The following pre-defined counts are available:
739
740 profile_count::zero () for code that is known to execute zero times at
741 runtime (this can be detected statically i.e. for paths leading to
742 abort ();
743 profile_count::one () for code that is known to execute once (such as
744 main () function
745 profile_count::uninitialized () for unknown execution count.
746
747 */
748
6c1dae73 749struct GTY(()) profile_count
3995f3a2 750{
650fe732 751public:
4e9a497f 752 /* Use 62bit to hold basic block counters. Should be at least
3995f3a2
JH
753 64bit. Although a counter cannot be negative, we use a signed
754 type to hold various extra stages. */
755
e7a74006 756 static const int n_bits = 61;
4e9a497f 757 static const uint64_t max_count = ((uint64_t) 1 << n_bits) - 2;
907050e3 758private:
4e9a497f
JH
759 static const uint64_t uninitialized_count = ((uint64_t) 1 << n_bits) - 1;
760
21f657a4
RE
761#if defined (__arm__) && (__GNUC__ >= 6 && __GNUC__ <= 8)
762 /* Work-around for PR88469. A bug in the gcc-6/7/8 PCS layout code
763 incorrectly detects the alignment of a structure where the only
764 64-bit aligned object is a bit-field. We force the alignment of
765 the entire field to mitigate this. */
766#define UINT64_BIT_FIELD_ALIGN __attribute__ ((aligned(8)))
767#else
768#define UINT64_BIT_FIELD_ALIGN
769#endif
770 uint64_t UINT64_BIT_FIELD_ALIGN m_val : n_bits;
771#undef UINT64_BIT_FIELD_ALIGN
e7a74006 772 enum profile_quality m_quality : 3;
db51f624 773public:
e7a74006
JH
774
775 /* Return true if both values can meaningfully appear in single function
776 body. We have either all counters in function local or global, otherwise
777 operations between them are not really defined well. */
778 bool compatible_p (const profile_count other) const
779 {
780 if (!initialized_p () || !other.initialized_p ())
781 return true;
a4c3f08d
ML
782 if (*this == zero ()
783 || other == zero ())
e7a74006 784 return true;
db51f624
JH
785 /* Do not allow nonzero global profile together with local guesses
786 that are globally0. */
787 if (ipa ().nonzero_p ()
788 && !(other.ipa () == other))
789 return false;
790 if (other.ipa ().nonzero_p ()
791 && !(ipa () == *this))
792 return false;
793
e7a74006
JH
794 return ipa_p () == other.ipa_p ();
795 }
db51f624 796
3995f3a2
JH
797 /* Used for counters which are expected to be never executed. */
798 static profile_count zero ()
799 {
800 return from_gcov_type (0);
801 }
03105885 802
517048ce
JH
803 static profile_count adjusted_zero ()
804 {
805 profile_count c;
806 c.m_val = 0;
e48dc99e 807 c.m_quality = ADJUSTED;
517048ce
JH
808 return c;
809 }
03105885 810
36d95aa8
JH
811 static profile_count guessed_zero ()
812 {
813 profile_count c;
814 c.m_val = 0;
e48dc99e 815 c.m_quality = GUESSED;
36d95aa8
JH
816 return c;
817 }
03105885 818
3995f3a2
JH
819 static profile_count one ()
820 {
821 return from_gcov_type (1);
822 }
03105885 823
3995f3a2
JH
824 /* Value of counters which has not been initialized. Either because
825 initialization did not happen yet or because profile is unknown. */
826 static profile_count uninitialized ()
827 {
828 profile_count c;
4e9a497f 829 c.m_val = uninitialized_count;
e48dc99e 830 c.m_quality = GUESSED_LOCAL;
3995f3a2
JH
831 return c;
832 }
833
3995f3a2
JH
834 /* Conversion to gcov_type is lossy. */
835 gcov_type to_gcov_type () const
836 {
837 gcc_checking_assert (initialized_p ());
838 return m_val;
839 }
840
841 /* Return true if value has been initialized. */
842 bool initialized_p () const
843 {
4e9a497f 844 return m_val != uninitialized_count;
3995f3a2 845 }
03105885 846
3995f3a2
JH
847 /* Return true if value can be trusted. */
848 bool reliable_p () const
849 {
e48dc99e 850 return m_quality >= ADJUSTED;
3995f3a2 851 }
03105885 852
956d615d 853 /* Return true if value can be operated inter-procedurally. */
e7a74006
JH
854 bool ipa_p () const
855 {
e48dc99e 856 return !initialized_p () || m_quality >= GUESSED_GLOBAL0;
e7a74006 857 }
03105885 858
d1b9a572
ML
859 /* Return true if quality of profile is precise. */
860 bool precise_p () const
861 {
e48dc99e 862 return m_quality == PRECISE;
d1b9a572 863 }
3995f3a2 864
d276406a 865 /* Get the value of the count. */
07bd2703 866 uint64_t value () const { return m_val; }
d276406a 867
4a4412b9
DM
868 /* Get the quality of the count. */
869 enum profile_quality quality () const { return m_quality; }
870
b5db8b44
JH
871 /* When merging basic blocks, the two different profile counts are unified.
872 Return true if this can be done without losing info about profile.
873 The only case we care about here is when first BB contains something
874 that makes it terminate in a way not visible in CFG. */
875 bool ok_for_merging (profile_count other) const
876 {
e48dc99e
ML
877 if (m_quality < ADJUSTED
878 || other.m_quality < ADJUSTED)
b5db8b44
JH
879 return true;
880 return !(other < *this);
881 }
882
883 /* When merging two BBs with different counts, pick common count that looks
884 most representative. */
885 profile_count merge (profile_count other) const
886 {
887 if (*this == other || !other.initialized_p ()
888 || m_quality > other.m_quality)
889 return *this;
890 if (other.m_quality > m_quality
891 || other > *this)
892 return other;
893 return *this;
894 }
895
3995f3a2
JH
896 /* Basic operations. */
897 bool operator== (const profile_count &other) const
898 {
4e9a497f 899 return m_val == other.m_val && m_quality == other.m_quality;
3995f3a2 900 }
03105885 901
3995f3a2
JH
902 profile_count operator+ (const profile_count &other) const
903 {
a4c3f08d 904 if (other == zero ())
3995f3a2 905 return *this;
a4c3f08d 906 if (*this == zero ())
3995f3a2
JH
907 return other;
908 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 909 return uninitialized ();
3995f3a2
JH
910
911 profile_count ret;
e7a74006 912 gcc_checking_assert (compatible_p (other));
d5a3b4af
JJ
913 uint64_t ret_val = m_val + other.m_val;
914 ret.m_val = MIN (ret_val, max_count);
4e9a497f 915 ret.m_quality = MIN (m_quality, other.m_quality);
3995f3a2
JH
916 return ret;
917 }
03105885 918
3995f3a2
JH
919 profile_count &operator+= (const profile_count &other)
920 {
a4c3f08d 921 if (other == zero ())
3995f3a2 922 return *this;
a4c3f08d 923 if (*this == zero ())
3995f3a2
JH
924 {
925 *this = other;
926 return *this;
927 }
928 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 929 return *this = uninitialized ();
3995f3a2 930 else
4e9a497f 931 {
e7a74006 932 gcc_checking_assert (compatible_p (other));
d5a3b4af
JJ
933 uint64_t ret_val = m_val + other.m_val;
934 m_val = MIN (ret_val, max_count);
5ef4d61d 935 m_quality = MIN (m_quality, other.m_quality);
4e9a497f 936 }
3995f3a2
JH
937 return *this;
938 }
03105885 939
3995f3a2
JH
940 profile_count operator- (const profile_count &other) const
941 {
a4c3f08d 942 if (*this == zero () || other == zero ())
3995f3a2
JH
943 return *this;
944 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 945 return uninitialized ();
e7a74006 946 gcc_checking_assert (compatible_p (other));
3995f3a2 947 profile_count ret;
4e9a497f
JH
948 ret.m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
949 ret.m_quality = MIN (m_quality, other.m_quality);
3995f3a2
JH
950 return ret;
951 }
03105885 952
3995f3a2
JH
953 profile_count &operator-= (const profile_count &other)
954 {
a4c3f08d 955 if (*this == zero () || other == zero ())
3995f3a2
JH
956 return *this;
957 if (!initialized_p () || !other.initialized_p ())
a4c3f08d 958 return *this = uninitialized ();
3995f3a2 959 else
4e9a497f 960 {
e7a74006 961 gcc_checking_assert (compatible_p (other));
d5a3b4af 962 m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
5ef4d61d 963 m_quality = MIN (m_quality, other.m_quality);
4e9a497f 964 }
3995f3a2
JH
965 return *this;
966 }
967
968 /* Return false if profile_count is bogus. */
969 bool verify () const
970 {
e48dc99e
ML
971 gcc_checking_assert (m_quality != UNINITIALIZED_PROFILE);
972 return m_val != uninitialized_count || m_quality == GUESSED_LOCAL;
3995f3a2
JH
973 }
974
956d615d 975 /* Comparisons are three-state and conservative. False is returned if
67914693 976 the inequality cannot be decided. */
3995f3a2
JH
977 bool operator< (const profile_count &other) const
978 {
e7a74006
JH
979 if (!initialized_p () || !other.initialized_p ())
980 return false;
a4c3f08d
ML
981 if (*this == zero ())
982 return !(other == zero ());
983 if (other == zero ())
e7a74006
JH
984 return false;
985 gcc_checking_assert (compatible_p (other));
986 return m_val < other.m_val;
3995f3a2 987 }
03105885 988
3995f3a2
JH
989 bool operator> (const profile_count &other) const
990 {
e7a74006
JH
991 if (!initialized_p () || !other.initialized_p ())
992 return false;
a4c3f08d 993 if (*this == zero ())
e7a74006 994 return false;
a4c3f08d
ML
995 if (other == zero ())
996 return !(*this == zero ());
e7a74006 997 gcc_checking_assert (compatible_p (other));
3995f3a2
JH
998 return initialized_p () && other.initialized_p () && m_val > other.m_val;
999 }
03105885 1000
3995f3a2
JH
1001 bool operator< (const gcov_type other) const
1002 {
e7a74006 1003 gcc_checking_assert (ipa_p ());
4e9a497f 1004 gcc_checking_assert (other >= 0);
51b74457 1005 return ipa ().initialized_p () && ipa ().m_val < (uint64_t) other;
3995f3a2 1006 }
03105885 1007
3995f3a2
JH
1008 bool operator> (const gcov_type other) const
1009 {
e7a74006 1010 gcc_checking_assert (ipa_p ());
4e9a497f 1011 gcc_checking_assert (other >= 0);
51b74457 1012 return ipa ().initialized_p () && ipa ().m_val > (uint64_t) other;
3995f3a2
JH
1013 }
1014
1015 bool operator<= (const profile_count &other) const
1016 {
e7a74006
JH
1017 if (!initialized_p () || !other.initialized_p ())
1018 return false;
a4c3f08d 1019 if (*this == zero ())
e7a74006 1020 return true;
a4c3f08d
ML
1021 if (other == zero ())
1022 return (*this == zero ());
e7a74006
JH
1023 gcc_checking_assert (compatible_p (other));
1024 return m_val <= other.m_val;
3995f3a2 1025 }
03105885 1026
3995f3a2
JH
1027 bool operator>= (const profile_count &other) const
1028 {
e7a74006
JH
1029 if (!initialized_p () || !other.initialized_p ())
1030 return false;
a4c3f08d 1031 if (other == zero ())
e7a74006 1032 return true;
a4c3f08d
ML
1033 if (*this == zero ())
1034 return (other == zero ());
e7a74006
JH
1035 gcc_checking_assert (compatible_p (other));
1036 return m_val >= other.m_val;
3995f3a2 1037 }
03105885 1038
3995f3a2
JH
1039 bool operator<= (const gcov_type other) const
1040 {
e7a74006 1041 gcc_checking_assert (ipa_p ());
4e9a497f 1042 gcc_checking_assert (other >= 0);
51b74457 1043 return ipa ().initialized_p () && ipa ().m_val <= (uint64_t) other;
3995f3a2 1044 }
03105885 1045
3995f3a2
JH
1046 bool operator>= (const gcov_type other) const
1047 {
e7a74006 1048 gcc_checking_assert (ipa_p ());
4e9a497f 1049 gcc_checking_assert (other >= 0);
51b74457 1050 return ipa ().initialized_p () && ipa ().m_val >= (uint64_t) other;
3995f3a2 1051 }
03105885 1052
9f55aee9
ML
1053 profile_count operator* (int64_t num) const
1054 {
1055 return apply_scale (num, 1);
1056 }
1057
268b5c81 1058 profile_count operator*= (int64_t num)
9f55aee9 1059 {
268b5c81
ML
1060 *this = apply_scale (num, 1);
1061 return *this;
9f55aee9
ML
1062 }
1063
1064 profile_count operator/ (int64_t den) const
1065 {
1066 return apply_scale (1, den);
1067 }
1068
268b5c81 1069 profile_count operator/= (int64_t den)
9f55aee9 1070 {
268b5c81
ML
1071 *this = apply_scale (1, den);
1072 return *this;
9f55aee9
ML
1073 }
1074
e7a74006
JH
1075 /* Return true when value is not zero and can be used for scaling.
1076 This is different from *this > 0 because that requires counter to
1077 be IPA. */
1078 bool nonzero_p () const
1079 {
1080 return initialized_p () && m_val != 0;
1081 }
1082
956d615d 1083 /* Make counter forcibly nonzero. */
e7a74006
JH
1084 profile_count force_nonzero () const
1085 {
1086 if (!initialized_p ())
1087 return *this;
1088 profile_count ret = *this;
1089 if (ret.m_val == 0)
97c07987
JH
1090 {
1091 ret.m_val = 1;
e48dc99e 1092 ret.m_quality = MIN (m_quality, ADJUSTED);
97c07987 1093 }
e7a74006
JH
1094 return ret;
1095 }
1096
1097 profile_count max (profile_count other) const
1098 {
97dd1ee8
JH
1099 profile_count val = *this;
1100
1101 /* Always prefer nonzero IPA counts over local counts. */
1102 if (ipa ().nonzero_p () || other.ipa ().nonzero_p ())
1103 {
1104 val = ipa ();
1105 other = other.ipa ();
1106 }
e7a74006
JH
1107 if (!initialized_p ())
1108 return other;
1109 if (!other.initialized_p ())
1110 return *this;
a4c3f08d 1111 if (*this == zero ())
e7a74006 1112 return other;
a4c3f08d 1113 if (other == zero ())
e7a74006
JH
1114 return *this;
1115 gcc_checking_assert (compatible_p (other));
97dd1ee8
JH
1116 if (val.m_val < other.m_val || (m_val == other.m_val
1117 && val.m_quality < other.m_quality))
e7a74006
JH
1118 return other;
1119 return *this;
1120 }
3995f3a2
JH
1121
1122 /* PROB is a probability in scale 0...REG_BR_PROB_BASE. Scale counter
1123 accordingly. */
1124 profile_count apply_probability (int prob) const
1125 {
1126 gcc_checking_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
eaee472d 1127 if (m_val == 0)
aea5e79a 1128 return *this;
3995f3a2 1129 if (!initialized_p ())
a4c3f08d 1130 return uninitialized ();
3995f3a2 1131 profile_count ret;
d5a3b4af
JJ
1132 uint64_t tmp;
1133 safe_scale_64bit (m_val, prob, REG_BR_PROB_BASE, &tmp);
1134 ret.m_val = tmp;
e48dc99e 1135 ret.m_quality = MIN (m_quality, ADJUSTED);
357067f2
JH
1136 return ret;
1137 }
1138
1139 /* Scale counter according to PROB. */
1140 profile_count apply_probability (profile_probability prob) const
1141 {
499b8079 1142 if (*this == zero () || prob == profile_probability::always ())
357067f2
JH
1143 return *this;
1144 if (prob == profile_probability::never ())
a4c3f08d 1145 return zero ();
499b8079 1146 if (!initialized_p () || !prob.initialized_p ())
a4c3f08d 1147 return uninitialized ();
357067f2 1148 profile_count ret;
83d502cf
JH
1149 uint64_t tmp;
1150 safe_scale_64bit (m_val, prob.m_val, profile_probability::max_probability,
1151 &tmp);
1152 ret.m_val = tmp;
357067f2 1153 ret.m_quality = MIN (m_quality, prob.m_quality);
3995f3a2
JH
1154 return ret;
1155 }
03105885 1156
3995f3a2
JH
1157 /* Return *THIS * NUM / DEN. */
1158 profile_count apply_scale (int64_t num, int64_t den) const
1159 {
eaee472d 1160 if (m_val == 0)
aea5e79a 1161 return *this;
3995f3a2 1162 if (!initialized_p ())
a4c3f08d 1163 return uninitialized ();
3995f3a2 1164 profile_count ret;
83d502cf
JH
1165 uint64_t tmp;
1166
eaee472d 1167 gcc_checking_assert (num >= 0 && den > 0);
83d502cf
JH
1168 safe_scale_64bit (m_val, num, den, &tmp);
1169 ret.m_val = MIN (tmp, max_count);
e48dc99e 1170 ret.m_quality = MIN (m_quality, ADJUSTED);
3995f3a2
JH
1171 return ret;
1172 }
03105885 1173
3995f3a2
JH
1174 profile_count apply_scale (profile_count num, profile_count den) const
1175 {
a4c3f08d 1176 if (*this == zero ())
eaee472d 1177 return *this;
a4c3f08d 1178 if (num == zero ())
eaee472d 1179 return num;
3995f3a2 1180 if (!initialized_p () || !num.initialized_p () || !den.initialized_p ())
a4c3f08d 1181 return uninitialized ();
4e9a497f
JH
1182 if (num == den)
1183 return *this;
8e7d1486 1184 gcc_checking_assert (den.m_val);
4e9a497f
JH
1185
1186 profile_count ret;
9588ea78
JH
1187 uint64_t val;
1188 safe_scale_64bit (m_val, num.m_val, den.m_val, &val);
1189 ret.m_val = MIN (val, max_count);
e48dc99e 1190 ret.m_quality = MIN (MIN (MIN (m_quality, ADJUSTED),
e7a74006 1191 num.m_quality), den.m_quality);
97dd1ee8
JH
1192 /* Be sure that ret is not local if num is global.
1193 Also ensure that ret is not global0 when num is global. */
1194 if (num.ipa_p ())
1195 ret.m_quality = MAX (ret.m_quality,
1196 num == num.ipa () ? GUESSED : num.m_quality);
e7a74006
JH
1197 return ret;
1198 }
1199
1200 /* Return THIS with quality dropped to GUESSED_LOCAL. */
1201 profile_count guessed_local () const
1202 {
1203 profile_count ret = *this;
1204 if (!initialized_p ())
1205 return *this;
e48dc99e 1206 ret.m_quality = GUESSED_LOCAL;
e7a74006
JH
1207 return ret;
1208 }
1209
517048ce 1210 /* We know that profile is globally 0 but keep local profile if present. */
e7a74006
JH
1211 profile_count global0 () const
1212 {
1213 profile_count ret = *this;
1214 if (!initialized_p ())
1215 return *this;
e48dc99e 1216 ret.m_quality = GUESSED_GLOBAL0;
3995f3a2
JH
1217 return ret;
1218 }
1219
517048ce
JH
1220 /* We know that profile is globally adjusted 0 but keep local profile
1221 if present. */
1222 profile_count global0adjusted () const
1223 {
1224 profile_count ret = *this;
1225 if (!initialized_p ())
1226 return *this;
e48dc99e 1227 ret.m_quality = GUESSED_GLOBAL0_ADJUSTED;
517048ce
JH
1228 return ret;
1229 }
1230
2f70a979
JH
1231 /* Return THIS with quality dropped to GUESSED. */
1232 profile_count guessed () const
1233 {
1234 profile_count ret = *this;
e48dc99e 1235 ret.m_quality = MIN (ret.m_quality, GUESSED);
2f70a979
JH
1236 return ret;
1237 }
1238
956d615d
JJ
1239 /* Return variant of profile count which is always safe to compare
1240 across functions. */
e7a74006
JH
1241 profile_count ipa () const
1242 {
e48dc99e 1243 if (m_quality > GUESSED_GLOBAL0_ADJUSTED)
e7a74006 1244 return *this;
e48dc99e 1245 if (m_quality == GUESSED_GLOBAL0)
a4c3f08d 1246 return zero ();
e48dc99e 1247 if (m_quality == GUESSED_GLOBAL0_ADJUSTED)
a4c3f08d
ML
1248 return adjusted_zero ();
1249 return uninitialized ();
e7a74006
JH
1250 }
1251
2f70a979
JH
1252 /* Return THIS with quality dropped to AFDO. */
1253 profile_count afdo () const
1254 {
1255 profile_count ret = *this;
e48dc99e 1256 ret.m_quality = AFDO;
2f70a979
JH
1257 return ret;
1258 }
1259
3995f3a2
JH
1260 /* Return probability of event with counter THIS within event with counter
1261 OVERALL. */
357067f2 1262 profile_probability probability_in (const profile_count overall) const
3995f3a2 1263 {
a4c3f08d
ML
1264 if (*this == zero ()
1265 && !(overall == zero ()))
357067f2
JH
1266 return profile_probability::never ();
1267 if (!initialized_p () || !overall.initialized_p ()
1268 || !overall.m_val)
1269 return profile_probability::uninitialized ();
e48dc99e 1270 if (*this == overall && m_quality == PRECISE)
97c07987 1271 return profile_probability::always ();
357067f2 1272 profile_probability ret;
b49d29d7 1273 gcc_checking_assert (compatible_p (overall));
e0185719 1274
e7a74006 1275 if (overall.m_val < m_val)
97c07987
JH
1276 {
1277 ret.m_val = profile_probability::max_probability;
e48dc99e 1278 ret.m_quality = GUESSED;
97c07987
JH
1279 return ret;
1280 }
357067f2 1281 else
5ef4d61d 1282 ret.m_val = RDIV (m_val * profile_probability::max_probability,
357067f2 1283 overall.m_val);
97c07987 1284 ret.m_quality = MIN (MAX (MIN (m_quality, overall.m_quality),
e48dc99e 1285 GUESSED), ADJUSTED);
357067f2 1286 return ret;
3995f3a2
JH
1287 }
1288
aa6741ef
JH
1289 /* Return true if profile count is very large, so we risk overflows
1290 with loop transformations. */
1291 bool
1292 very_large_p ()
1293 {
1294 if (!initialized_p ())
1295 return false;
1296 return m_val > max_count / 65536;
1297 }
1298
e7a74006
JH
1299 int to_frequency (struct function *fun) const;
1300 int to_cgraph_frequency (profile_count entry_bb_count) const;
41f0e819 1301 sreal to_sreal_scale (profile_count in, bool *known = NULL) const;
e7a74006 1302
3995f3a2 1303 /* Output THIS to F. */
3cce8d98 1304 void dump (FILE *f, struct function *fun = NULL) const;
3995f3a2
JH
1305
1306 /* Print THIS to stderr. */
1307 void debug () const;
1308
1309 /* Return true if THIS is known to differ significantly from OTHER. */
1310 bool differs_from_p (profile_count other) const;
1311
e4373d41
JH
1312 /* We want to scale profile across function boundary from NUM to DEN.
1313 Take care of the side case when NUM and DEN are zeros of incompatible
1314 kinds. */
1315 static void adjust_for_ipa_scaling (profile_count *num, profile_count *den);
1316
517048ce
JH
1317 /* THIS is a count of bb which is known to be executed IPA times.
1318 Combine this information into bb counter. This means returning IPA
1319 if it is nonzero, not changing anything if IPA is uninitialized
1320 and if IPA is zero, turning THIS into corresponding local profile with
1321 global0. */
1322 profile_count combine_with_ipa_count (profile_count ipa);
1323
b49d29d7
JH
1324 /* Same as combine_with_ipa_count but inside function with count IPA2. */
1325 profile_count combine_with_ipa_count_within
1326 (profile_count ipa, profile_count ipa2);
1327
f6422f23
JH
1328 /* The profiling runtime uses gcov_type, which is usually 64bit integer.
1329 Conversions back and forth are used to read the coverage and get it
1330 into internal representation. */
d276406a 1331 static profile_count from_gcov_type (gcov_type v,
e48dc99e 1332 profile_quality quality = PRECISE);
f6422f23 1333
3995f3a2 1334 /* LTO streaming support. */
99b1c316 1335 static profile_count stream_in (class lto_input_block *);
3995f3a2
JH
1336 void stream_out (struct output_block *);
1337 void stream_out (struct lto_output_stream *);
1338};
1339#endif