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/>. */
21 #ifndef GCC_PROFILE_COUNT_H
22 #define GCC_PROFILE_COUNT_H
26 /* Quality of the profile count. Because gengtype does not support enums
27 inside of classes, this is in global namespace. */
28 enum profile_quality
{
29 /* Profile is based on static branch prediction heuristics and may
30 or may not match reality. It is local to function and can not be compared
31 inter-procedurally. Never used by probabilities (they are always local).
33 profile_guessed_local
= 0,
34 /* Profile was read by feedback and was 0, we used local heuristics to guess
35 better. This is the case of functions not run in profile fedback.
36 Never used by probabilities. */
37 profile_guessed_global0
= 1,
40 /* Profile is based on static branch prediction heuristics. It may or may
41 not reflect the reality but it can be compared interprocedurally
42 (for example, we inlined function w/o profile feedback into function
43 with feedback and propagated from that).
44 Never used by probablities. */
46 /* Profile was determined by autofdo. */
48 /* Profile was originally based on feedback but it was adjusted
49 by code duplicating optimization. It may not precisely reflect the
50 particular code path. */
52 /* Profile was read from profile feedback or determined by accurate static
57 /* The base value for branch probability notes and edge probabilities. */
58 #define REG_BR_PROB_BASE 10000
60 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
62 bool slow_safe_scale_64bit (uint64_t a
, uint64_t b
, uint64_t c
, uint64_t *res
);
64 /* Compute RES=(a*b + c/2)/c capping and return false if overflow happened. */
67 safe_scale_64bit (uint64_t a
, uint64_t b
, uint64_t c
, uint64_t *res
)
69 #if (GCC_VERSION >= 5000)
71 if (!__builtin_mul_overflow (a
, b
, &tmp
)
72 && !__builtin_add_overflow (tmp
, c
/2, &tmp
))
83 if (a
< ((uint64_t)1 << 31)
84 && b
< ((uint64_t)1 << 31)
85 && c
< ((uint64_t)1 << 31))
87 *res
= (a
* b
+ (c
/ 2)) / c
;
91 return slow_safe_scale_64bit (a
, b
, c
, res
);
94 /* Data type to hold probabilities. It implements fixed point arithmetics
95 with capping so probability is always in range [0,1] and scaling requiring
96 values greater than 1 needs to be represented otherwise.
98 In addition to actual value the quality of profile is tracked and propagated
99 through all operations. Special value UNINITIALIZED is used for probabilities
100 that has not been determined yet (for example bacause of
101 -fno-guess-branch-probability)
103 Typically probabilities are derived from profile feedback (via
104 probability_in_gcov_type), autoFDO or guessed statically and then propagated
105 thorough the compilation.
107 Named probabilities are available:
108 - never (0 probability)
110 - very_unlikely (1/2000 probability)
111 - unlikely (1/5 probablity)
112 - even (1/2 probability)
113 - likely (4/5 probability)
114 - very_likely (1999/2000 probability)
118 Named probabilities except for never/always are assumed to be statically
119 guessed and thus not necessarily accurate. The difference between never
120 and guessed_never is that the first one should be used only in case that
121 well behaving program will very likely not execute the "never" path.
122 For example if the path is going to abort () call or it exception handling.
124 Always and guessed_always probabilities are symmetric.
126 For legacy code we support conversion to/from REG_BR_PROB_BASE based fixpoint
127 integer arithmetics. Once the code is converted to branch probabilities,
128 these conversions will probably go away because they are lossy.
131 class GTY((user
)) profile_probability
133 static const int n_bits
= 29;
134 /* We can technically use ((uint32_t) 1 << (n_bits - 1)) - 2 but that
135 will lead to harder multiplication sequences. */
136 static const uint32_t max_probability
= (uint32_t) 1 << (n_bits
- 2);
137 static const uint32_t uninitialized_probability
138 = ((uint32_t) 1 << (n_bits
- 1)) - 1;
141 enum profile_quality m_quality
: 3;
143 friend class profile_count
;
146 /* Named probabilities. */
147 static profile_probability
never ()
149 profile_probability ret
;
151 ret
.m_quality
= profile_precise
;
154 static profile_probability
guessed_never ()
156 profile_probability ret
;
158 ret
.m_quality
= profile_guessed
;
161 static profile_probability
very_unlikely ()
163 /* Be consistent with PROB_VERY_UNLIKELY in predict.h. */
164 profile_probability r
165 = profile_probability::always ().apply_scale (1, 2000);
169 static profile_probability
unlikely ()
171 /* Be consistent with PROB_VERY_LIKELY in predict.h. */
172 profile_probability r
173 = profile_probability::always ().apply_scale (1, 5);
177 static profile_probability
even ()
179 return profile_probability::always ().apply_scale (1, 2);
181 static profile_probability
very_likely ()
183 return profile_probability::always () - very_unlikely ();
185 static profile_probability
likely ()
187 return profile_probability::always () - unlikely ();
189 static profile_probability
guessed_always ()
191 profile_probability ret
;
192 ret
.m_val
= max_probability
;
193 ret
.m_quality
= profile_guessed
;
196 static profile_probability
always ()
198 profile_probability ret
;
199 ret
.m_val
= max_probability
;
200 ret
.m_quality
= profile_precise
;
203 /* Probabilities which has not been initialized. Either because
204 initialization did not happen yet or because profile is unknown. */
205 static profile_probability
uninitialized ()
207 profile_probability c
;
208 c
.m_val
= uninitialized_probability
;
209 c
.m_quality
= profile_guessed
;
214 /* Return true if value has been initialized. */
215 bool initialized_p () const
217 return m_val
!= uninitialized_probability
;
219 /* Return true if value can be trusted. */
220 bool reliable_p () const
222 return m_quality
>= profile_adjusted
;
225 /* Conversion from and to REG_BR_PROB_BASE integer fixpoint arithmetics.
226 this is mostly to support legacy code and should go away. */
227 static profile_probability
from_reg_br_prob_base (int v
)
229 profile_probability ret
;
230 gcc_checking_assert (v
>= 0 && v
<= REG_BR_PROB_BASE
);
231 ret
.m_val
= RDIV (v
* (uint64_t) max_probability
, REG_BR_PROB_BASE
);
232 ret
.m_quality
= profile_guessed
;
235 int to_reg_br_prob_base () const
237 gcc_checking_assert (initialized_p ());
238 return RDIV (m_val
* (uint64_t) REG_BR_PROB_BASE
, max_probability
);
241 /* Conversion to and from RTL representation of profile probabilities. */
242 static profile_probability
from_reg_br_prob_note (int v
)
244 profile_probability ret
;
245 ret
.m_val
= ((unsigned int)v
) / 8;
246 ret
.m_quality
= (enum profile_quality
)(v
& 7);
249 int to_reg_br_prob_note () const
251 gcc_checking_assert (initialized_p ());
252 int ret
= m_val
* 8 + m_quality
;
253 gcc_checking_assert (profile_probability::from_reg_br_prob_note (ret
)
258 /* Return VAL1/VAL2. */
259 static profile_probability probability_in_gcov_type
260 (gcov_type val1
, gcov_type val2
)
262 profile_probability ret
;
263 gcc_checking_assert (val1
>= 0 && val2
> 0);
265 ret
.m_val
= max_probability
;
269 safe_scale_64bit (val1
, max_probability
, val2
, &tmp
);
270 gcc_checking_assert (tmp
<= max_probability
);
273 ret
.m_quality
= profile_precise
;
277 /* Basic operations. */
278 bool operator== (const profile_probability
&other
) const
280 return m_val
== other
.m_val
&& m_quality
== other
.m_quality
;
282 profile_probability
operator+ (const profile_probability
&other
) const
284 if (other
== profile_probability::never ())
286 if (*this == profile_probability::never ())
288 if (!initialized_p () || !other
.initialized_p ())
289 return profile_probability::uninitialized ();
291 profile_probability ret
;
292 ret
.m_val
= MIN ((uint32_t)(m_val
+ other
.m_val
), max_probability
);
293 ret
.m_quality
= MIN (m_quality
, other
.m_quality
);
296 profile_probability
&operator+= (const profile_probability
&other
)
298 if (other
== profile_probability::never ())
300 if (*this == profile_probability::never ())
305 if (!initialized_p () || !other
.initialized_p ())
306 return *this = profile_probability::uninitialized ();
309 m_val
= MIN ((uint32_t)(m_val
+ other
.m_val
), max_probability
);
310 m_quality
= MIN (m_quality
, other
.m_quality
);
314 profile_probability
operator- (const profile_probability
&other
) const
316 if (*this == profile_probability::never ()
317 || other
== profile_probability::never ())
319 if (!initialized_p () || !other
.initialized_p ())
320 return profile_probability::uninitialized ();
321 profile_probability ret
;
322 ret
.m_val
= m_val
>= other
.m_val
? m_val
- other
.m_val
: 0;
323 ret
.m_quality
= MIN (m_quality
, other
.m_quality
);
326 profile_probability
&operator-= (const profile_probability
&other
)
328 if (*this == profile_probability::never ()
329 || other
== profile_probability::never ())
331 if (!initialized_p () || !other
.initialized_p ())
332 return *this = profile_probability::uninitialized ();
335 m_val
= m_val
>= other
.m_val
? m_val
- other
.m_val
: 0;
336 m_quality
= MIN (m_quality
, other
.m_quality
);
340 profile_probability
operator* (const profile_probability
&other
) const
342 if (*this == profile_probability::never ()
343 || other
== profile_probability::never ())
344 return profile_probability::never ();
345 if (!initialized_p () || !other
.initialized_p ())
346 return profile_probability::uninitialized ();
347 profile_probability ret
;
348 ret
.m_val
= RDIV ((uint64_t)m_val
* other
.m_val
, max_probability
);
349 ret
.m_quality
= MIN (m_quality
, other
.m_quality
);
352 profile_probability
&operator*= (const profile_probability
&other
)
354 if (*this == profile_probability::never ()
355 || other
== profile_probability::never ())
356 return *this = profile_probability::never ();
357 if (!initialized_p () || !other
.initialized_p ())
358 return *this = profile_probability::uninitialized ();
361 m_val
= RDIV ((uint64_t)m_val
* other
.m_val
, max_probability
);
362 m_quality
= MIN (m_quality
, other
.m_quality
);
366 profile_probability
operator/ (const profile_probability
&other
) const
368 if (*this == profile_probability::never ())
369 return profile_probability::never ();
370 if (!initialized_p () || !other
.initialized_p ())
371 return profile_probability::uninitialized ();
372 profile_probability ret
;
373 if (m_val
>= other
.m_val
)
374 ret
.m_val
= max_probability
;
379 gcc_checking_assert (other
.m_val
);
380 ret
.m_val
= MIN (RDIV ((uint64_t)m_val
* max_probability
,
384 ret
.m_quality
= MIN (m_quality
, other
.m_quality
);
387 profile_probability
&operator/= (const profile_probability
&other
)
389 if (*this == profile_probability::never ())
390 return *this = profile_probability::never ();
391 if (!initialized_p () || !other
.initialized_p ())
392 return *this = profile_probability::uninitialized ();
395 if (m_val
> other
.m_val
)
396 m_val
= max_probability
;
401 gcc_checking_assert (other
.m_val
);
402 m_val
= MIN (RDIV ((uint64_t)m_val
* max_probability
,
406 m_quality
= MIN (m_quality
, other
.m_quality
);
411 gcov_type
apply (gcov_type val
) const
413 if (*this == profile_probability::uninitialized ())
415 return RDIV (val
* m_val
, max_probability
);
418 /* Return 1-*THIS. */
419 profile_probability
invert () const
421 return profile_probability::always() - *this;
424 /* Return THIS with quality dropped to GUESSED. */
425 profile_probability
guessed () const
427 profile_probability ret
= *this;
428 ret
.m_quality
= profile_guessed
;
432 /* Return THIS with quality dropped to AFDO. */
433 profile_probability
afdo () const
435 profile_probability ret
= *this;
436 ret
.m_quality
= profile_afdo
;
440 profile_probability
combine_with_freq (int freq1
, profile_probability other
,
443 profile_probability ret
;
445 if (*this == profile_probability::uninitialized ()
446 || other
== profile_probability::uninitialized ())
447 return profile_probability::uninitialized ();
449 gcc_checking_assert (freq1
>= 0 && freq2
>= 0);
450 if (!freq1
&& !freq2
)
452 ret
.m_val
= (m_val
+ other
.m_val
) / 2;
455 ret
.m_val
= RDIV (m_val
* (uint64_t) freq1
456 + other
.m_val
* (uint64_t) freq2
, freq1
+ freq2
);
457 ret
.m_quality
= MIN (m_quality
, other
.m_quality
);
461 /* Return *THIS * NUM / DEN. */
462 profile_probability
apply_scale (int64_t num
, int64_t den
) const
464 if (*this == profile_probability::never ())
466 if (!initialized_p ())
467 return profile_probability::uninitialized ();
468 profile_probability ret
;
470 safe_scale_64bit (m_val
, num
, den
, &tmp
);
471 ret
.m_val
= MIN (tmp
, max_probability
);
472 ret
.m_quality
= MIN (m_quality
, profile_adjusted
);
476 /* Return true when the probability of edge is reliable.
478 The profile guessing code is good at predicting branch outcome (ie.
479 taken/not taken), that is predicted right slightly over 75% of time.
480 It is however notoriously poor on predicting the probability itself.
481 In general the profile appear a lot flatter (with probabilities closer
482 to 50%) than the reality so it is bad idea to use it to drive optimization
483 such as those disabling dynamic branch prediction for well predictable
486 There are two exceptions - edges leading to noreturn edges and edges
487 predicted by number of iterations heuristics are predicted well. This macro
488 should be able to distinguish those, but at the moment it simply check for
489 noreturn heuristic that is only one giving probability over 99% or bellow
490 1%. In future we might want to propagate reliability information across the
491 CFG if we find this information useful on multiple places. */
493 bool probably_reliable_p () const
495 if (m_quality
>= profile_adjusted
)
497 if (!initialized_p ())
499 return m_val
< max_probability
/ 100
500 || m_val
> max_probability
- max_probability
/ 100;
503 /* Return false if profile_probability is bogus. */
506 if (m_val
== uninitialized_probability
)
507 return m_quality
== profile_guessed
;
508 else if (m_quality
< profile_guessed
)
510 return m_val
<= max_probability
;
513 /* Comparsions are three-state and conservative. False is returned if
514 the inequality can not be decided. */
515 bool operator< (const profile_probability
&other
) const
517 return initialized_p () && other
.initialized_p () && m_val
< other
.m_val
;
519 bool operator> (const profile_probability
&other
) const
521 return initialized_p () && other
.initialized_p () && m_val
> other
.m_val
;
524 bool operator<= (const profile_probability
&other
) const
526 return initialized_p () && other
.initialized_p () && m_val
<= other
.m_val
;
528 bool operator>= (const profile_probability
&other
) const
530 return initialized_p () && other
.initialized_p () && m_val
>= other
.m_val
;
533 /* Output THIS to F. */
534 void dump (FILE *f
) const;
536 /* Print THIS to stderr. */
539 /* Return true if THIS is known to differ significantly from OTHER. */
540 bool differs_from_p (profile_probability other
) const;
541 /* Return if difference is greater than 50%. */
542 bool differs_lot_from_p (profile_probability other
) const;
544 /* LTO streaming support. */
545 static profile_probability
stream_in (struct lto_input_block
*);
546 void stream_out (struct output_block
*);
547 void stream_out (struct lto_output_stream
*);
550 /* Main data type to hold profile counters in GCC. Profile counts originate
551 either from profile feedback, static profile estimation or both. We do not
552 perform whole program profile propagation and thus profile estimation
553 counters are often local to function, while counters from profile feedback
554 (or special cases of profile estimation) can be used inter-procedurally.
556 There are 3 basic types
557 1) local counters which are result of intra-procedural static profile
559 2) ipa counters which are result of profile feedback or special case
560 of static profile estimation (such as in function main).
561 3) counters which counts as 0 inter-procedurally (beause given function
562 was never run in train feedback) but they hold local static profile
565 Counters of type 1 and 3 can not be mixed with counters of different type
566 within operation (because whole function should use one type of counter)
567 with exception that global zero mix in most operations where outcome is
570 To take local counter and use it inter-procedurally use ipa member function
571 which strips information irelevant at the inter-procedural level.
573 Counters are 61bit integers representing number of executions during the
574 train run or normalized frequency within the function.
576 As the profile is maintained during the compilation, many adjustments are
577 made. Not all transformations can be made precisely, most importantly
578 when code is being duplicated. It also may happen that part of CFG has
579 profile counts known while other do not - for example when LTO optimizing
580 partly profiled program or when profile was lost due to COMDAT merging.
582 For this reason profile_count tracks more information than
583 just unsigned integer and it is also ready for profile mismatches.
584 The API of this data type represent operations that are natural
585 on profile counts - sum, difference and operation with scales and
586 probabilities. All operations are safe by never getting negative counts
587 and they do end up in uninitialized scale if any of the parameters is
590 All comparsions that are three state and handling of probabilities. Thus
591 a < b is not equal to !(a >= b).
593 The following pre-defined counts are available:
595 profile_count::zero () for code that is known to execute zero times at
596 runtime (this can be detected statically i.e. for paths leading to
598 profile_count::one () for code that is known to execute once (such as
600 profile_count::uninitialized () for unknown execution count.
604 class GTY(()) profile_count
606 /* Use 62bit to hold basic block counters. Should be at least
607 64bit. Although a counter cannot be negative, we use a signed
608 type to hold various extra stages. */
610 static const int n_bits
= 61;
611 static const uint64_t max_count
= ((uint64_t) 1 << n_bits
) - 2;
612 static const uint64_t uninitialized_count
= ((uint64_t) 1 << n_bits
) - 1;
614 uint64_t m_val
: n_bits
;
615 enum profile_quality m_quality
: 3;
617 /* Return true if both values can meaningfully appear in single function
618 body. We have either all counters in function local or global, otherwise
619 operations between them are not really defined well. */
620 bool compatible_p (const profile_count other
) const
622 if (!initialized_p () || !other
.initialized_p ())
624 if (*this == profile_count::zero ()
625 || other
== profile_count::zero ())
627 return ipa_p () == other
.ipa_p ();
631 /* Used for counters which are expected to be never executed. */
632 static profile_count
zero ()
634 return from_gcov_type (0);
636 static profile_count
guessed_zero ()
640 c
.m_quality
= profile_guessed
;
643 static profile_count
one ()
645 return from_gcov_type (1);
647 /* Value of counters which has not been initialized. Either because
648 initialization did not happen yet or because profile is unknown. */
649 static profile_count
uninitialized ()
652 c
.m_val
= uninitialized_count
;
653 c
.m_quality
= profile_guessed_local
;
657 /* The profiling runtime uses gcov_type, which is usually 64bit integer.
658 Conversions back and forth are used to read the coverage and get it
659 into internal representation. */
660 static profile_count
from_gcov_type (gcov_type v
)
663 gcc_checking_assert (v
>= 0 && (uint64_t) v
<= max_count
);
665 ret
.m_quality
= profile_precise
;
669 /* Conversion to gcov_type is lossy. */
670 gcov_type
to_gcov_type () const
672 gcc_checking_assert (initialized_p ());
676 /* Return true if value has been initialized. */
677 bool initialized_p () const
679 return m_val
!= uninitialized_count
;
681 /* Return true if value can be trusted. */
682 bool reliable_p () const
684 return m_quality
>= profile_adjusted
;
686 /* Return true if vlaue can be operated inter-procedurally. */
689 return !initialized_p () || m_quality
>= profile_guessed_global0
;
692 /* When merging basic blocks, the two different profile counts are unified.
693 Return true if this can be done without losing info about profile.
694 The only case we care about here is when first BB contains something
695 that makes it terminate in a way not visible in CFG. */
696 bool ok_for_merging (profile_count other
) const
698 if (m_quality
< profile_adjusted
699 || other
.m_quality
< profile_adjusted
)
701 return !(other
< *this);
704 /* When merging two BBs with different counts, pick common count that looks
705 most representative. */
706 profile_count
merge (profile_count other
) const
708 if (*this == other
|| !other
.initialized_p ()
709 || m_quality
> other
.m_quality
)
711 if (other
.m_quality
> m_quality
717 /* Basic operations. */
718 bool operator== (const profile_count
&other
) const
720 return m_val
== other
.m_val
&& m_quality
== other
.m_quality
;
722 profile_count
operator+ (const profile_count
&other
) const
724 if (other
== profile_count::zero ())
726 if (*this == profile_count::zero ())
728 if (!initialized_p () || !other
.initialized_p ())
729 return profile_count::uninitialized ();
732 gcc_checking_assert (compatible_p (other
));
733 ret
.m_val
= m_val
+ other
.m_val
;
734 ret
.m_quality
= MIN (m_quality
, other
.m_quality
);
737 profile_count
&operator+= (const profile_count
&other
)
739 if (other
== profile_count::zero ())
741 if (*this == profile_count::zero ())
746 if (!initialized_p () || !other
.initialized_p ())
747 return *this = profile_count::uninitialized ();
750 gcc_checking_assert (compatible_p (other
));
751 m_val
+= other
.m_val
;
752 m_quality
= MIN (m_quality
, other
.m_quality
);
756 profile_count
operator- (const profile_count
&other
) const
758 if (*this == profile_count::zero () || other
== profile_count::zero ())
760 if (!initialized_p () || !other
.initialized_p ())
761 return profile_count::uninitialized ();
762 gcc_checking_assert (compatible_p (other
));
764 ret
.m_val
= m_val
>= other
.m_val
? m_val
- other
.m_val
: 0;
765 ret
.m_quality
= MIN (m_quality
, other
.m_quality
);
768 profile_count
&operator-= (const profile_count
&other
)
770 if (*this == profile_count::zero () || other
== profile_count::zero ())
772 if (!initialized_p () || !other
.initialized_p ())
773 return *this = profile_count::uninitialized ();
776 gcc_checking_assert (compatible_p (other
));
777 m_val
= m_val
>= other
.m_val
? m_val
- other
.m_val
: 0;
778 m_quality
= MIN (m_quality
, other
.m_quality
);
783 /* Return false if profile_count is bogus. */
786 return m_val
!= uninitialized_count
|| m_quality
== profile_guessed_local
;
789 /* Comparsions are three-state and conservative. False is returned if
790 the inequality can not be decided. */
791 bool operator< (const profile_count
&other
) const
793 if (!initialized_p () || !other
.initialized_p ())
795 if (*this == profile_count::zero ())
796 return !(other
== profile_count::zero ());
797 if (other
== profile_count::zero ())
799 gcc_checking_assert (compatible_p (other
));
800 return m_val
< other
.m_val
;
802 bool operator> (const profile_count
&other
) const
804 if (!initialized_p () || !other
.initialized_p ())
806 if (*this == profile_count::zero ())
808 if (other
== profile_count::zero ())
809 return !(*this == profile_count::zero ());
810 gcc_checking_assert (compatible_p (other
));
811 return initialized_p () && other
.initialized_p () && m_val
> other
.m_val
;
813 bool operator< (const gcov_type other
) const
815 gcc_checking_assert (ipa_p ());
816 gcc_checking_assert (other
>= 0);
817 return initialized_p () && m_val
< (uint64_t) other
;
819 bool operator> (const gcov_type other
) const
821 gcc_checking_assert (ipa_p ());
822 gcc_checking_assert (other
>= 0);
823 return initialized_p () && m_val
> (uint64_t) other
;
826 bool operator<= (const profile_count
&other
) const
828 if (!initialized_p () || !other
.initialized_p ())
830 if (*this == profile_count::zero ())
832 if (other
== profile_count::zero ())
833 return (*this == profile_count::zero ());
834 gcc_checking_assert (compatible_p (other
));
835 return m_val
<= other
.m_val
;
837 bool operator>= (const profile_count
&other
) const
839 if (!initialized_p () || !other
.initialized_p ())
841 if (other
== profile_count::zero ())
843 if (*this == profile_count::zero ())
844 return !(other
== profile_count::zero ());
845 gcc_checking_assert (compatible_p (other
));
846 return m_val
>= other
.m_val
;
848 bool operator<= (const gcov_type other
) const
850 gcc_checking_assert (ipa_p ());
851 gcc_checking_assert (other
>= 0);
852 return initialized_p () && m_val
<= (uint64_t) other
;
854 bool operator>= (const gcov_type other
) const
856 gcc_checking_assert (ipa_p ());
857 gcc_checking_assert (other
>= 0);
858 return initialized_p () && m_val
>= (uint64_t) other
;
860 /* Return true when value is not zero and can be used for scaling.
861 This is different from *this > 0 because that requires counter to
863 bool nonzero_p () const
865 return initialized_p () && m_val
!= 0;
868 /* Make counter forcingly nonzero. */
869 profile_count
force_nonzero () const
871 if (!initialized_p ())
873 profile_count ret
= *this;
879 profile_count
max (profile_count other
) const
881 if (!initialized_p ())
883 if (!other
.initialized_p ())
885 if (*this == profile_count::zero ())
887 if (other
== profile_count::zero ())
889 gcc_checking_assert (compatible_p (other
));
890 if (m_val
< other
.m_val
|| (m_val
== other
.m_val
891 && m_quality
< other
.m_quality
))
896 /* PROB is a probability in scale 0...REG_BR_PROB_BASE. Scale counter
898 profile_count
apply_probability (int prob
) const
900 gcc_checking_assert (prob
>= 0 && prob
<= REG_BR_PROB_BASE
);
903 if (!initialized_p ())
904 return profile_count::uninitialized ();
906 ret
.m_val
= RDIV (m_val
* prob
, REG_BR_PROB_BASE
);
907 ret
.m_quality
= MIN (m_quality
, profile_adjusted
);
911 /* Scale counter according to PROB. */
912 profile_count
apply_probability (profile_probability prob
) const
914 if (*this == profile_count::zero ())
916 if (prob
== profile_probability::never ())
917 return profile_count::zero ();
918 if (!initialized_p ())
919 return profile_count::uninitialized ();
922 safe_scale_64bit (m_val
, prob
.m_val
, profile_probability::max_probability
,
925 ret
.m_quality
= MIN (m_quality
, prob
.m_quality
);
928 /* Return *THIS * NUM / DEN. */
929 profile_count
apply_scale (int64_t num
, int64_t den
) const
933 if (!initialized_p ())
934 return profile_count::uninitialized ();
938 gcc_checking_assert (num
>= 0 && den
> 0);
939 safe_scale_64bit (m_val
, num
, den
, &tmp
);
940 ret
.m_val
= MIN (tmp
, max_count
);
941 ret
.m_quality
= MIN (m_quality
, profile_adjusted
);
944 profile_count
apply_scale (profile_count num
, profile_count den
) const
946 if (*this == profile_count::zero ())
948 if (num
== profile_count::zero ())
950 if (!initialized_p () || !num
.initialized_p () || !den
.initialized_p ())
951 return profile_count::uninitialized ();
954 gcc_checking_assert (den
.m_val
);
958 safe_scale_64bit (m_val
, num
.m_val
, den
.m_val
, &val
);
959 ret
.m_val
= MIN (val
, max_count
);
960 ret
.m_quality
= MIN (MIN (MIN (m_quality
, profile_adjusted
),
961 num
.m_quality
), den
.m_quality
);
962 if (num
.ipa_p () && !ret
.ipa_p ())
963 ret
.m_quality
= MIN (num
.m_quality
, profile_guessed
);
967 /* Return THIS with quality dropped to GUESSED_LOCAL. */
968 profile_count
guessed_local () const
970 profile_count ret
= *this;
971 if (!initialized_p ())
973 ret
.m_quality
= profile_guessed_local
;
977 /* We know that profile is globally0 but keep local profile if present. */
978 profile_count
global0 () const
980 profile_count ret
= *this;
981 if (!initialized_p ())
983 ret
.m_quality
= profile_guessed_global0
;
987 /* Return THIS with quality dropped to GUESSED. */
988 profile_count
guessed () const
990 profile_count ret
= *this;
991 ret
.m_quality
= MIN (ret
.m_quality
, profile_guessed
);
995 /* Return variant of profile counte which is always safe to compare
997 profile_count
ipa () const
999 if (m_quality
> profile_guessed_global0
)
1001 if (m_quality
== profile_guessed_global0
)
1002 return profile_count::zero ();
1003 return profile_count::uninitialized ();
1006 /* Return THIS with quality dropped to AFDO. */
1007 profile_count
afdo () const
1009 profile_count ret
= *this;
1010 ret
.m_quality
= profile_afdo
;
1014 /* Return probability of event with counter THIS within event with counter
1016 profile_probability
probability_in (const profile_count overall
) const
1018 if (*this == profile_count::zero ())
1019 return profile_probability::never ();
1020 if (!initialized_p () || !overall
.initialized_p ()
1022 return profile_probability::uninitialized ();
1023 profile_probability ret
;
1024 gcc_checking_assert (compatible_p (overall
));
1026 if (overall
.m_val
< m_val
)
1027 ret
.m_val
= profile_probability::max_probability
;
1029 ret
.m_val
= RDIV (m_val
* profile_probability::max_probability
,
1031 ret
.m_quality
= MAX (MIN (m_quality
, overall
.m_quality
), profile_guessed
);
1035 int to_frequency (struct function
*fun
) const;
1036 int to_cgraph_frequency (profile_count entry_bb_count
) const;
1038 /* Output THIS to F. */
1039 void dump (FILE *f
) const;
1041 /* Print THIS to stderr. */
1042 void debug () const;
1044 /* Return true if THIS is known to differ significantly from OTHER. */
1045 bool differs_from_p (profile_count other
) const;
1047 /* LTO streaming support. */
1048 static profile_count
stream_in (struct lto_input_block
*);
1049 void stream_out (struct output_block
*);
1050 void stream_out (struct lto_output_stream
*);