PR c/53037
[official-gcc.git] / gcc / profile-count.h
blobf16bbcb2b9f77cf7838f95df3305d627302622d4
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
10 version.
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
15 for more details.
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
24 struct function;
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. */
45 profile_guessed = 2,
46 /* Profile was determined by autofdo. */
47 profile_afdo = 3,
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. */
51 profile_adjusted = 4,
52 /* Profile was read from profile feedback or determined by accurate static
53 method. */
54 profile_precise = 5
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. */
66 inline bool
67 safe_scale_64bit (uint64_t a, uint64_t b, uint64_t c, uint64_t *res)
69 #if (GCC_VERSION >= 5000)
70 uint64_t tmp;
71 if (!__builtin_mul_overflow (a, b, &tmp)
72 && !__builtin_add_overflow (tmp, c/2, &tmp))
74 *res = tmp / c;
75 return true;
77 if (c == 1)
79 *res = (uint64_t) -1;
80 return false;
82 #else
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;
88 return true;
90 #endif
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)
109 - guessed_never
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)
115 - guessed_always
116 - always
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;
140 uint32_t m_val : 29;
141 enum profile_quality m_quality : 3;
143 friend class profile_count;
144 public:
146 /* Named probabilities. */
147 static profile_probability never ()
149 profile_probability ret;
150 ret.m_val = 0;
151 ret.m_quality = profile_precise;
152 return ret;
154 static profile_probability guessed_never ()
156 profile_probability ret;
157 ret.m_val = 0;
158 ret.m_quality = profile_guessed;
159 return ret;
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);
166 r.m_val--;
167 return r;
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);
174 r.m_val--;
175 return r;
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;
194 return ret;
196 static profile_probability always ()
198 profile_probability ret;
199 ret.m_val = max_probability;
200 ret.m_quality = profile_precise;
201 return ret;
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;
210 return c;
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;
233 return ret;
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);
247 return ret;
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)
254 == *this);
255 return 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);
264 if (val1 > val2)
265 ret.m_val = max_probability;
266 else
268 uint64_t tmp;
269 safe_scale_64bit (val1, max_probability, val2, &tmp);
270 gcc_checking_assert (tmp <= max_probability);
271 ret.m_val = tmp;
273 ret.m_quality = profile_precise;
274 return ret;
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 ())
285 return *this;
286 if (*this == profile_probability::never ())
287 return other;
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);
294 return ret;
296 profile_probability &operator+= (const profile_probability &other)
298 if (other == profile_probability::never ())
299 return *this;
300 if (*this == profile_probability::never ())
302 *this = other;
303 return *this;
305 if (!initialized_p () || !other.initialized_p ())
306 return *this = profile_probability::uninitialized ();
307 else
309 m_val = MIN ((uint32_t)(m_val + other.m_val), max_probability);
310 m_quality = MIN (m_quality, other.m_quality);
312 return *this;
314 profile_probability operator- (const profile_probability &other) const
316 if (*this == profile_probability::never ()
317 || other == profile_probability::never ())
318 return *this;
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);
324 return ret;
326 profile_probability &operator-= (const profile_probability &other)
328 if (*this == profile_probability::never ()
329 || other == profile_probability::never ())
330 return *this;
331 if (!initialized_p () || !other.initialized_p ())
332 return *this = profile_probability::uninitialized ();
333 else
335 m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
336 m_quality = MIN (m_quality, other.m_quality);
338 return *this;
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);
350 return ret;
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 ();
359 else
361 m_val = RDIV ((uint64_t)m_val * other.m_val, max_probability);
362 m_quality = MIN (m_quality, other.m_quality);
364 return *this;
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;
375 else if (!m_val)
376 ret.m_val = 0;
377 else
379 gcc_checking_assert (other.m_val);
380 ret.m_val = MIN (RDIV ((uint64_t)m_val * max_probability,
381 other.m_val),
382 max_probability);
384 ret.m_quality = MIN (m_quality, other.m_quality);
385 return ret;
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 ();
393 else
395 if (m_val > other.m_val)
396 m_val = max_probability;
397 else if (!m_val)
399 else
401 gcc_checking_assert (other.m_val);
402 m_val = MIN (RDIV ((uint64_t)m_val * max_probability,
403 other.m_val),
404 max_probability);
406 m_quality = MIN (m_quality, other.m_quality);
408 return *this;
411 gcov_type apply (gcov_type val) const
413 if (*this == profile_probability::uninitialized ())
414 return val / 2;
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;
429 return ret;
432 /* Return THIS with quality dropped to AFDO. */
433 profile_probability afdo () const
435 profile_probability ret = *this;
436 ret.m_quality = profile_afdo;
437 return ret;
440 profile_probability combine_with_freq (int freq1, profile_probability other,
441 int freq2) const
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;
454 else
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);
458 return ret;
461 /* Return *THIS * NUM / DEN. */
462 profile_probability apply_scale (int64_t num, int64_t den) const
464 if (*this == profile_probability::never ())
465 return *this;
466 if (!initialized_p ())
467 return profile_probability::uninitialized ();
468 profile_probability ret;
469 uint64_t tmp;
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);
473 return ret;
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
484 branches.
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)
496 return true;
497 if (!initialized_p ())
498 return false;
499 return m_val < max_probability / 100
500 || m_val > max_probability - max_probability / 100;
503 /* Return false if profile_probability is bogus. */
504 bool verify () const
506 if (m_val == uninitialized_probability)
507 return m_quality == profile_guessed;
508 else if (m_quality < profile_guessed)
509 return false;
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. */
537 void debug () const;
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
558 estimation.
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
563 estimate.
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
568 well defined.
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
588 uninitialized.
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
597 abort ();
598 profile_count::one () for code that is known to execute once (such as
599 main () function
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 ())
623 return true;
624 if (*this == profile_count::zero ()
625 || other == profile_count::zero ())
626 return true;
627 return ipa_p () == other.ipa_p ();
629 public:
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 ()
638 profile_count c;
639 c.m_val = 0;
640 c.m_quality = profile_guessed;
641 return c;
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 ()
651 profile_count c;
652 c.m_val = uninitialized_count;
653 c.m_quality = profile_guessed_local;
654 return c;
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)
662 profile_count ret;
663 gcc_checking_assert (v >= 0 && (uint64_t) v <= max_count);
664 ret.m_val = v;
665 ret.m_quality = profile_precise;
666 return ret;
669 /* Conversion to gcov_type is lossy. */
670 gcov_type to_gcov_type () const
672 gcc_checking_assert (initialized_p ());
673 return m_val;
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. */
687 bool ipa_p () const
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)
700 return true;
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)
710 return *this;
711 if (other.m_quality > m_quality
712 || other > *this)
713 return other;
714 return *this;
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 ())
725 return *this;
726 if (*this == profile_count::zero ())
727 return other;
728 if (!initialized_p () || !other.initialized_p ())
729 return profile_count::uninitialized ();
731 profile_count ret;
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);
735 return ret;
737 profile_count &operator+= (const profile_count &other)
739 if (other == profile_count::zero ())
740 return *this;
741 if (*this == profile_count::zero ())
743 *this = other;
744 return *this;
746 if (!initialized_p () || !other.initialized_p ())
747 return *this = profile_count::uninitialized ();
748 else
750 gcc_checking_assert (compatible_p (other));
751 m_val += other.m_val;
752 m_quality = MIN (m_quality, other.m_quality);
754 return *this;
756 profile_count operator- (const profile_count &other) const
758 if (*this == profile_count::zero () || other == profile_count::zero ())
759 return *this;
760 if (!initialized_p () || !other.initialized_p ())
761 return profile_count::uninitialized ();
762 gcc_checking_assert (compatible_p (other));
763 profile_count ret;
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);
766 return ret;
768 profile_count &operator-= (const profile_count &other)
770 if (*this == profile_count::zero () || other == profile_count::zero ())
771 return *this;
772 if (!initialized_p () || !other.initialized_p ())
773 return *this = profile_count::uninitialized ();
774 else
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);
780 return *this;
783 /* Return false if profile_count is bogus. */
784 bool verify () const
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 ())
794 return false;
795 if (*this == profile_count::zero ())
796 return !(other == profile_count::zero ());
797 if (other == profile_count::zero ())
798 return false;
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 ())
805 return false;
806 if (*this == profile_count::zero ())
807 return false;
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 ())
829 return false;
830 if (*this == profile_count::zero ())
831 return true;
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 ())
840 return false;
841 if (other == profile_count::zero ())
842 return true;
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
862 be IPA. */
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 ())
872 return *this;
873 profile_count ret = *this;
874 if (ret.m_val == 0)
875 ret.m_val = 1;
876 return ret;
879 profile_count max (profile_count other) const
881 if (!initialized_p ())
882 return other;
883 if (!other.initialized_p ())
884 return *this;
885 if (*this == profile_count::zero ())
886 return other;
887 if (other == profile_count::zero ())
888 return *this;
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))
892 return other;
893 return *this;
896 /* PROB is a probability in scale 0...REG_BR_PROB_BASE. Scale counter
897 accordingly. */
898 profile_count apply_probability (int prob) const
900 gcc_checking_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
901 if (m_val == 0)
902 return *this;
903 if (!initialized_p ())
904 return profile_count::uninitialized ();
905 profile_count ret;
906 ret.m_val = RDIV (m_val * prob, REG_BR_PROB_BASE);
907 ret.m_quality = MIN (m_quality, profile_adjusted);
908 return ret;
911 /* Scale counter according to PROB. */
912 profile_count apply_probability (profile_probability prob) const
914 if (*this == profile_count::zero ())
915 return *this;
916 if (prob == profile_probability::never ())
917 return profile_count::zero ();
918 if (!initialized_p ())
919 return profile_count::uninitialized ();
920 profile_count ret;
921 uint64_t tmp;
922 safe_scale_64bit (m_val, prob.m_val, profile_probability::max_probability,
923 &tmp);
924 ret.m_val = tmp;
925 ret.m_quality = MIN (m_quality, prob.m_quality);
926 return ret;
928 /* Return *THIS * NUM / DEN. */
929 profile_count apply_scale (int64_t num, int64_t den) const
931 if (m_val == 0)
932 return *this;
933 if (!initialized_p ())
934 return profile_count::uninitialized ();
935 profile_count ret;
936 uint64_t tmp;
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);
942 return ret;
944 profile_count apply_scale (profile_count num, profile_count den) const
946 if (*this == profile_count::zero ())
947 return *this;
948 if (num == profile_count::zero ())
949 return num;
950 if (!initialized_p () || !num.initialized_p () || !den.initialized_p ())
951 return profile_count::uninitialized ();
952 if (num == den)
953 return *this;
954 gcc_checking_assert (den.m_val);
956 profile_count ret;
957 uint64_t 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);
964 return ret;
967 /* Return THIS with quality dropped to GUESSED_LOCAL. */
968 profile_count guessed_local () const
970 profile_count ret = *this;
971 if (!initialized_p ())
972 return *this;
973 ret.m_quality = profile_guessed_local;
974 return ret;
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 ())
982 return *this;
983 ret.m_quality = profile_guessed_global0;
984 return ret;
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);
992 return ret;
995 /* Return variant of profile counte which is always safe to compare
996 acorss functions. */
997 profile_count ipa () const
999 if (m_quality > profile_guessed_global0)
1000 return *this;
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;
1011 return ret;
1014 /* Return probability of event with counter THIS within event with counter
1015 OVERALL. */
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 ()
1021 || !overall.m_val)
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;
1028 else
1029 ret.m_val = RDIV (m_val * profile_probability::max_probability,
1030 overall.m_val);
1031 ret.m_quality = MAX (MIN (m_quality, overall.m_quality), profile_guessed);
1032 return ret;
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 *);
1052 #endif