* tree-vect-loop-manip.c (vect_do_peeling): Do not use
[official-gcc.git] / gcc / profile-count.h
blob90d1bc747eeaccda030326fac87525efc8c4353a
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 sreal;
606 class GTY(()) profile_count
608 /* Use 62bit to hold basic block counters. Should be at least
609 64bit. Although a counter cannot be negative, we use a signed
610 type to hold various extra stages. */
612 static const int n_bits = 61;
613 static const uint64_t max_count = ((uint64_t) 1 << n_bits) - 2;
614 static const uint64_t uninitialized_count = ((uint64_t) 1 << n_bits) - 1;
616 uint64_t m_val : n_bits;
617 enum profile_quality m_quality : 3;
619 /* Return true if both values can meaningfully appear in single function
620 body. We have either all counters in function local or global, otherwise
621 operations between them are not really defined well. */
622 bool compatible_p (const profile_count other) const
624 if (!initialized_p () || !other.initialized_p ())
625 return true;
626 if (*this == profile_count::zero ()
627 || other == profile_count::zero ())
628 return true;
629 return ipa_p () == other.ipa_p ();
631 public:
633 /* Used for counters which are expected to be never executed. */
634 static profile_count zero ()
636 return from_gcov_type (0);
638 static profile_count guessed_zero ()
640 profile_count c;
641 c.m_val = 0;
642 c.m_quality = profile_guessed;
643 return c;
645 static profile_count one ()
647 return from_gcov_type (1);
649 /* Value of counters which has not been initialized. Either because
650 initialization did not happen yet or because profile is unknown. */
651 static profile_count uninitialized ()
653 profile_count c;
654 c.m_val = uninitialized_count;
655 c.m_quality = profile_guessed_local;
656 return c;
659 /* The profiling runtime uses gcov_type, which is usually 64bit integer.
660 Conversions back and forth are used to read the coverage and get it
661 into internal representation. */
662 static profile_count from_gcov_type (gcov_type v)
664 profile_count ret;
665 gcc_checking_assert (v >= 0 && (uint64_t) v <= max_count);
666 ret.m_val = v;
667 ret.m_quality = profile_precise;
668 return ret;
671 /* Conversion to gcov_type is lossy. */
672 gcov_type to_gcov_type () const
674 gcc_checking_assert (initialized_p ());
675 return m_val;
678 /* Return true if value has been initialized. */
679 bool initialized_p () const
681 return m_val != uninitialized_count;
683 /* Return true if value can be trusted. */
684 bool reliable_p () const
686 return m_quality >= profile_adjusted;
688 /* Return true if vlaue can be operated inter-procedurally. */
689 bool ipa_p () const
691 return !initialized_p () || m_quality >= profile_guessed_global0;
694 /* When merging basic blocks, the two different profile counts are unified.
695 Return true if this can be done without losing info about profile.
696 The only case we care about here is when first BB contains something
697 that makes it terminate in a way not visible in CFG. */
698 bool ok_for_merging (profile_count other) const
700 if (m_quality < profile_adjusted
701 || other.m_quality < profile_adjusted)
702 return true;
703 return !(other < *this);
706 /* When merging two BBs with different counts, pick common count that looks
707 most representative. */
708 profile_count merge (profile_count other) const
710 if (*this == other || !other.initialized_p ()
711 || m_quality > other.m_quality)
712 return *this;
713 if (other.m_quality > m_quality
714 || other > *this)
715 return other;
716 return *this;
719 /* Basic operations. */
720 bool operator== (const profile_count &other) const
722 return m_val == other.m_val && m_quality == other.m_quality;
724 profile_count operator+ (const profile_count &other) const
726 if (other == profile_count::zero ())
727 return *this;
728 if (*this == profile_count::zero ())
729 return other;
730 if (!initialized_p () || !other.initialized_p ())
731 return profile_count::uninitialized ();
733 profile_count ret;
734 gcc_checking_assert (compatible_p (other));
735 ret.m_val = m_val + other.m_val;
736 ret.m_quality = MIN (m_quality, other.m_quality);
737 return ret;
739 profile_count &operator+= (const profile_count &other)
741 if (other == profile_count::zero ())
742 return *this;
743 if (*this == profile_count::zero ())
745 *this = other;
746 return *this;
748 if (!initialized_p () || !other.initialized_p ())
749 return *this = profile_count::uninitialized ();
750 else
752 gcc_checking_assert (compatible_p (other));
753 m_val += other.m_val;
754 m_quality = MIN (m_quality, other.m_quality);
756 return *this;
758 profile_count operator- (const profile_count &other) const
760 if (*this == profile_count::zero () || other == profile_count::zero ())
761 return *this;
762 if (!initialized_p () || !other.initialized_p ())
763 return profile_count::uninitialized ();
764 gcc_checking_assert (compatible_p (other));
765 profile_count ret;
766 ret.m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
767 ret.m_quality = MIN (m_quality, other.m_quality);
768 return ret;
770 profile_count &operator-= (const profile_count &other)
772 if (*this == profile_count::zero () || other == profile_count::zero ())
773 return *this;
774 if (!initialized_p () || !other.initialized_p ())
775 return *this = profile_count::uninitialized ();
776 else
778 gcc_checking_assert (compatible_p (other));
779 m_val = m_val >= other.m_val ? m_val - other.m_val: 0;
780 m_quality = MIN (m_quality, other.m_quality);
782 return *this;
785 /* Return false if profile_count is bogus. */
786 bool verify () const
788 return m_val != uninitialized_count || m_quality == profile_guessed_local;
791 /* Comparsions are three-state and conservative. False is returned if
792 the inequality can not be decided. */
793 bool operator< (const profile_count &other) const
795 if (!initialized_p () || !other.initialized_p ())
796 return false;
797 if (*this == profile_count::zero ())
798 return !(other == profile_count::zero ());
799 if (other == profile_count::zero ())
800 return false;
801 gcc_checking_assert (compatible_p (other));
802 return m_val < other.m_val;
804 bool operator> (const profile_count &other) const
806 if (!initialized_p () || !other.initialized_p ())
807 return false;
808 if (*this == profile_count::zero ())
809 return false;
810 if (other == profile_count::zero ())
811 return !(*this == profile_count::zero ());
812 gcc_checking_assert (compatible_p (other));
813 return initialized_p () && other.initialized_p () && m_val > other.m_val;
815 bool operator< (const gcov_type other) const
817 gcc_checking_assert (ipa_p ());
818 gcc_checking_assert (other >= 0);
819 return initialized_p () && m_val < (uint64_t) other;
821 bool operator> (const gcov_type other) const
823 gcc_checking_assert (ipa_p ());
824 gcc_checking_assert (other >= 0);
825 return initialized_p () && m_val > (uint64_t) other;
828 bool operator<= (const profile_count &other) const
830 if (!initialized_p () || !other.initialized_p ())
831 return false;
832 if (*this == profile_count::zero ())
833 return true;
834 if (other == profile_count::zero ())
835 return (*this == profile_count::zero ());
836 gcc_checking_assert (compatible_p (other));
837 return m_val <= other.m_val;
839 bool operator>= (const profile_count &other) const
841 if (!initialized_p () || !other.initialized_p ())
842 return false;
843 if (other == profile_count::zero ())
844 return true;
845 if (*this == profile_count::zero ())
846 return !(other == profile_count::zero ());
847 gcc_checking_assert (compatible_p (other));
848 return m_val >= other.m_val;
850 bool operator<= (const gcov_type other) const
852 gcc_checking_assert (ipa_p ());
853 gcc_checking_assert (other >= 0);
854 return initialized_p () && m_val <= (uint64_t) other;
856 bool operator>= (const gcov_type other) const
858 gcc_checking_assert (ipa_p ());
859 gcc_checking_assert (other >= 0);
860 return initialized_p () && m_val >= (uint64_t) other;
862 /* Return true when value is not zero and can be used for scaling.
863 This is different from *this > 0 because that requires counter to
864 be IPA. */
865 bool nonzero_p () const
867 return initialized_p () && m_val != 0;
870 /* Make counter forcingly nonzero. */
871 profile_count force_nonzero () const
873 if (!initialized_p ())
874 return *this;
875 profile_count ret = *this;
876 if (ret.m_val == 0)
877 ret.m_val = 1;
878 return ret;
881 profile_count max (profile_count other) const
883 if (!initialized_p ())
884 return other;
885 if (!other.initialized_p ())
886 return *this;
887 if (*this == profile_count::zero ())
888 return other;
889 if (other == profile_count::zero ())
890 return *this;
891 gcc_checking_assert (compatible_p (other));
892 if (m_val < other.m_val || (m_val == other.m_val
893 && m_quality < other.m_quality))
894 return other;
895 return *this;
898 /* PROB is a probability in scale 0...REG_BR_PROB_BASE. Scale counter
899 accordingly. */
900 profile_count apply_probability (int prob) const
902 gcc_checking_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
903 if (m_val == 0)
904 return *this;
905 if (!initialized_p ())
906 return profile_count::uninitialized ();
907 profile_count ret;
908 ret.m_val = RDIV (m_val * prob, REG_BR_PROB_BASE);
909 ret.m_quality = MIN (m_quality, profile_adjusted);
910 return ret;
913 /* Scale counter according to PROB. */
914 profile_count apply_probability (profile_probability prob) const
916 if (*this == profile_count::zero ())
917 return *this;
918 if (prob == profile_probability::never ())
919 return profile_count::zero ();
920 if (!initialized_p ())
921 return profile_count::uninitialized ();
922 profile_count ret;
923 uint64_t tmp;
924 safe_scale_64bit (m_val, prob.m_val, profile_probability::max_probability,
925 &tmp);
926 ret.m_val = tmp;
927 ret.m_quality = MIN (m_quality, prob.m_quality);
928 return ret;
930 /* Return *THIS * NUM / DEN. */
931 profile_count apply_scale (int64_t num, int64_t den) const
933 if (m_val == 0)
934 return *this;
935 if (!initialized_p ())
936 return profile_count::uninitialized ();
937 profile_count ret;
938 uint64_t tmp;
940 gcc_checking_assert (num >= 0 && den > 0);
941 safe_scale_64bit (m_val, num, den, &tmp);
942 ret.m_val = MIN (tmp, max_count);
943 ret.m_quality = MIN (m_quality, profile_adjusted);
944 return ret;
946 profile_count apply_scale (profile_count num, profile_count den) const
948 if (*this == profile_count::zero ())
949 return *this;
950 if (num == profile_count::zero ())
951 return num;
952 if (!initialized_p () || !num.initialized_p () || !den.initialized_p ())
953 return profile_count::uninitialized ();
954 if (num == den)
955 return *this;
956 gcc_checking_assert (den.m_val);
958 profile_count ret;
959 uint64_t val;
960 safe_scale_64bit (m_val, num.m_val, den.m_val, &val);
961 ret.m_val = MIN (val, max_count);
962 ret.m_quality = MIN (MIN (MIN (m_quality, profile_adjusted),
963 num.m_quality), den.m_quality);
964 if (num.ipa_p () && !ret.ipa_p ())
965 ret.m_quality = MIN (num.m_quality, profile_guessed);
966 return ret;
969 /* Return THIS with quality dropped to GUESSED_LOCAL. */
970 profile_count guessed_local () const
972 profile_count ret = *this;
973 if (!initialized_p ())
974 return *this;
975 ret.m_quality = profile_guessed_local;
976 return ret;
979 /* We know that profile is globally0 but keep local profile if present. */
980 profile_count global0 () const
982 profile_count ret = *this;
983 if (!initialized_p ())
984 return *this;
985 ret.m_quality = profile_guessed_global0;
986 return ret;
989 /* Return THIS with quality dropped to GUESSED. */
990 profile_count guessed () const
992 profile_count ret = *this;
993 ret.m_quality = MIN (ret.m_quality, profile_guessed);
994 return ret;
997 /* Return variant of profile counte which is always safe to compare
998 acorss functions. */
999 profile_count ipa () const
1001 if (m_quality > profile_guessed_global0)
1002 return *this;
1003 if (m_quality == profile_guessed_global0)
1004 return profile_count::zero ();
1005 return profile_count::uninitialized ();
1008 /* Return THIS with quality dropped to AFDO. */
1009 profile_count afdo () const
1011 profile_count ret = *this;
1012 ret.m_quality = profile_afdo;
1013 return ret;
1016 /* Return probability of event with counter THIS within event with counter
1017 OVERALL. */
1018 profile_probability probability_in (const profile_count overall) const
1020 if (*this == profile_count::zero ())
1021 return profile_probability::never ();
1022 if (!initialized_p () || !overall.initialized_p ()
1023 || !overall.m_val)
1024 return profile_probability::uninitialized ();
1025 profile_probability ret;
1026 gcc_checking_assert (compatible_p (overall));
1028 if (overall.m_val < m_val)
1029 ret.m_val = profile_probability::max_probability;
1030 else
1031 ret.m_val = RDIV (m_val * profile_probability::max_probability,
1032 overall.m_val);
1033 ret.m_quality = MAX (MIN (m_quality, overall.m_quality), profile_guessed);
1034 return ret;
1037 int to_frequency (struct function *fun) const;
1038 int to_cgraph_frequency (profile_count entry_bb_count) const;
1039 sreal to_sreal_scale (profile_count in, bool *known = NULL) const;
1041 /* Output THIS to F. */
1042 void dump (FILE *f) const;
1044 /* Print THIS to stderr. */
1045 void debug () const;
1047 /* Return true if THIS is known to differ significantly from OTHER. */
1048 bool differs_from_p (profile_count other) const;
1050 /* We want to scale profile across function boundary from NUM to DEN.
1051 Take care of the side case when NUM and DEN are zeros of incompatible
1052 kinds. */
1053 static void adjust_for_ipa_scaling (profile_count *num, profile_count *den);
1055 /* LTO streaming support. */
1056 static profile_count stream_in (struct lto_input_block *);
1057 void stream_out (struct output_block *);
1058 void stream_out (struct lto_output_stream *);
1060 #endif