re PR target/83862 (powerpc: ICE in signbit testcase)
[official-gcc.git] / gcc / profile-count.h
blob03e6635f0da12df7eb4db92b0ac3d8134cb2499b
1 /* Profile counter container type.
2 Copyright (C) 2017-2018 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 = 1,
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 = 2,
39 /* Same as profile_guessed_global0 but global count is adjusted 0. */
40 profile_guessed_global0adjusted = 3,
42 /* Profile is based on static branch prediction heuristics. It may or may
43 not reflect the reality but it can be compared interprocedurally
44 (for example, we inlined function w/o profile feedback into function
45 with feedback and propagated from that).
46 Never used by probablities. */
47 profile_guessed = 4,
48 /* Profile was determined by autofdo. */
49 profile_afdo = 5,
50 /* Profile was originally based on feedback but it was adjusted
51 by code duplicating optimization. It may not precisely reflect the
52 particular code path. */
53 profile_adjusted = 6,
54 /* Profile was read from profile feedback or determined by accurate static
55 method. */
56 profile_precise = 7
59 /* The base value for branch probability notes and edge probabilities. */
60 #define REG_BR_PROB_BASE 10000
62 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
64 bool slow_safe_scale_64bit (uint64_t a, uint64_t b, uint64_t c, uint64_t *res);
66 /* Compute RES=(a*b + c/2)/c capping and return false if overflow happened. */
68 inline bool
69 safe_scale_64bit (uint64_t a, uint64_t b, uint64_t c, uint64_t *res)
71 #if (GCC_VERSION >= 5000)
72 uint64_t tmp;
73 if (!__builtin_mul_overflow (a, b, &tmp)
74 && !__builtin_add_overflow (tmp, c/2, &tmp))
76 *res = tmp / c;
77 return true;
79 if (c == 1)
81 *res = (uint64_t) -1;
82 return false;
84 #else
85 if (a < ((uint64_t)1 << 31)
86 && b < ((uint64_t)1 << 31)
87 && c < ((uint64_t)1 << 31))
89 *res = (a * b + (c / 2)) / c;
90 return true;
92 #endif
93 return slow_safe_scale_64bit (a, b, c, res);
96 /* Data type to hold probabilities. It implements fixed point arithmetics
97 with capping so probability is always in range [0,1] and scaling requiring
98 values greater than 1 needs to be represented otherwise.
100 In addition to actual value the quality of profile is tracked and propagated
101 through all operations. Special value UNINITIALIZED is used for probabilities
102 that has not been determined yet (for example bacause of
103 -fno-guess-branch-probability)
105 Typically probabilities are derived from profile feedback (via
106 probability_in_gcov_type), autoFDO or guessed statically and then propagated
107 thorough the compilation.
109 Named probabilities are available:
110 - never (0 probability)
111 - guessed_never
112 - very_unlikely (1/2000 probability)
113 - unlikely (1/5 probablity)
114 - even (1/2 probability)
115 - likely (4/5 probability)
116 - very_likely (1999/2000 probability)
117 - guessed_always
118 - always
120 Named probabilities except for never/always are assumed to be statically
121 guessed and thus not necessarily accurate. The difference between never
122 and guessed_never is that the first one should be used only in case that
123 well behaving program will very likely not execute the "never" path.
124 For example if the path is going to abort () call or it exception handling.
126 Always and guessed_always probabilities are symmetric.
128 For legacy code we support conversion to/from REG_BR_PROB_BASE based fixpoint
129 integer arithmetics. Once the code is converted to branch probabilities,
130 these conversions will probably go away because they are lossy.
133 class GTY((user)) profile_probability
135 static const int n_bits = 29;
136 /* We can technically use ((uint32_t) 1 << (n_bits - 1)) - 2 but that
137 will lead to harder multiplication sequences. */
138 static const uint32_t max_probability = (uint32_t) 1 << (n_bits - 2);
139 static const uint32_t uninitialized_probability
140 = ((uint32_t) 1 << (n_bits - 1)) - 1;
142 uint32_t m_val : 29;
143 enum profile_quality m_quality : 3;
145 friend class profile_count;
146 public:
148 /* Named probabilities. */
149 static profile_probability never ()
151 profile_probability ret;
152 ret.m_val = 0;
153 ret.m_quality = profile_precise;
154 return ret;
156 static profile_probability guessed_never ()
158 profile_probability ret;
159 ret.m_val = 0;
160 ret.m_quality = profile_guessed;
161 return ret;
163 static profile_probability very_unlikely ()
165 /* Be consistent with PROB_VERY_UNLIKELY in predict.h. */
166 profile_probability r
167 = profile_probability::always ().apply_scale (1, 2000);
168 r.m_val--;
169 return r;
171 static profile_probability unlikely ()
173 /* Be consistent with PROB_VERY_LIKELY in predict.h. */
174 profile_probability r
175 = profile_probability::always ().apply_scale (1, 5);
176 r.m_val--;
177 return r;
179 static profile_probability even ()
181 return profile_probability::always ().apply_scale (1, 2);
183 static profile_probability very_likely ()
185 return profile_probability::always () - very_unlikely ();
187 static profile_probability likely ()
189 return profile_probability::always () - unlikely ();
191 static profile_probability guessed_always ()
193 profile_probability ret;
194 ret.m_val = max_probability;
195 ret.m_quality = profile_guessed;
196 return ret;
198 static profile_probability always ()
200 profile_probability ret;
201 ret.m_val = max_probability;
202 ret.m_quality = profile_precise;
203 return ret;
205 /* Probabilities which has not been initialized. Either because
206 initialization did not happen yet or because profile is unknown. */
207 static profile_probability uninitialized ()
209 profile_probability c;
210 c.m_val = uninitialized_probability;
211 c.m_quality = profile_guessed;
212 return c;
216 /* Return true if value has been initialized. */
217 bool initialized_p () const
219 return m_val != uninitialized_probability;
221 /* Return true if value can be trusted. */
222 bool reliable_p () const
224 return m_quality >= profile_adjusted;
227 /* Conversion from and to REG_BR_PROB_BASE integer fixpoint arithmetics.
228 this is mostly to support legacy code and should go away. */
229 static profile_probability from_reg_br_prob_base (int v)
231 profile_probability ret;
232 gcc_checking_assert (v >= 0 && v <= REG_BR_PROB_BASE);
233 ret.m_val = RDIV (v * (uint64_t) max_probability, REG_BR_PROB_BASE);
234 ret.m_quality = profile_guessed;
235 return ret;
237 int to_reg_br_prob_base () const
239 gcc_checking_assert (initialized_p ());
240 return RDIV (m_val * (uint64_t) REG_BR_PROB_BASE, max_probability);
243 /* Conversion to and from RTL representation of profile probabilities. */
244 static profile_probability from_reg_br_prob_note (int v)
246 profile_probability ret;
247 ret.m_val = ((unsigned int)v) / 8;
248 ret.m_quality = (enum profile_quality)(v & 7);
249 return ret;
251 int to_reg_br_prob_note () const
253 gcc_checking_assert (initialized_p ());
254 int ret = m_val * 8 + m_quality;
255 gcc_checking_assert (profile_probability::from_reg_br_prob_note (ret)
256 == *this);
257 return ret;
260 /* Return VAL1/VAL2. */
261 static profile_probability probability_in_gcov_type
262 (gcov_type val1, gcov_type val2)
264 profile_probability ret;
265 gcc_checking_assert (val1 >= 0 && val2 > 0);
266 if (val1 > val2)
267 ret.m_val = max_probability;
268 else
270 uint64_t tmp;
271 safe_scale_64bit (val1, max_probability, val2, &tmp);
272 gcc_checking_assert (tmp <= max_probability);
273 ret.m_val = tmp;
275 ret.m_quality = profile_precise;
276 return ret;
279 /* Basic operations. */
280 bool operator== (const profile_probability &other) const
282 return m_val == other.m_val && m_quality == other.m_quality;
284 profile_probability operator+ (const profile_probability &other) const
286 if (other == profile_probability::never ())
287 return *this;
288 if (*this == profile_probability::never ())
289 return other;
290 if (!initialized_p () || !other.initialized_p ())
291 return profile_probability::uninitialized ();
293 profile_probability ret;
294 ret.m_val = MIN ((uint32_t)(m_val + other.m_val), max_probability);
295 ret.m_quality = MIN (m_quality, other.m_quality);
296 return ret;
298 profile_probability &operator+= (const profile_probability &other)
300 if (other == profile_probability::never ())
301 return *this;
302 if (*this == profile_probability::never ())
304 *this = other;
305 return *this;
307 if (!initialized_p () || !other.initialized_p ())
308 return *this = profile_probability::uninitialized ();
309 else
311 m_val = MIN ((uint32_t)(m_val + other.m_val), max_probability);
312 m_quality = MIN (m_quality, other.m_quality);
314 return *this;
316 profile_probability operator- (const profile_probability &other) const
318 if (*this == profile_probability::never ()
319 || other == profile_probability::never ())
320 return *this;
321 if (!initialized_p () || !other.initialized_p ())
322 return profile_probability::uninitialized ();
323 profile_probability ret;
324 ret.m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
325 ret.m_quality = MIN (m_quality, other.m_quality);
326 return ret;
328 profile_probability &operator-= (const profile_probability &other)
330 if (*this == profile_probability::never ()
331 || other == profile_probability::never ())
332 return *this;
333 if (!initialized_p () || !other.initialized_p ())
334 return *this = profile_probability::uninitialized ();
335 else
337 m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
338 m_quality = MIN (m_quality, other.m_quality);
340 return *this;
342 profile_probability operator* (const profile_probability &other) const
344 if (*this == profile_probability::never ()
345 || other == profile_probability::never ())
346 return profile_probability::never ();
347 if (!initialized_p () || !other.initialized_p ())
348 return profile_probability::uninitialized ();
349 profile_probability ret;
350 ret.m_val = RDIV ((uint64_t)m_val * other.m_val, max_probability);
351 ret.m_quality = MIN (m_quality, other.m_quality);
352 return ret;
354 profile_probability &operator*= (const profile_probability &other)
356 if (*this == profile_probability::never ()
357 || other == profile_probability::never ())
358 return *this = profile_probability::never ();
359 if (!initialized_p () || !other.initialized_p ())
360 return *this = profile_probability::uninitialized ();
361 else
363 m_val = RDIV ((uint64_t)m_val * other.m_val, max_probability);
364 m_quality = MIN (m_quality, other.m_quality);
366 return *this;
368 profile_probability operator/ (const profile_probability &other) const
370 if (*this == profile_probability::never ())
371 return profile_probability::never ();
372 if (!initialized_p () || !other.initialized_p ())
373 return profile_probability::uninitialized ();
374 profile_probability ret;
375 if (m_val >= other.m_val)
376 ret.m_val = max_probability;
377 else if (!m_val)
378 ret.m_val = 0;
379 else
381 gcc_checking_assert (other.m_val);
382 ret.m_val = MIN (RDIV ((uint64_t)m_val * max_probability,
383 other.m_val),
384 max_probability);
386 ret.m_quality = MIN (m_quality, other.m_quality);
387 return ret;
389 profile_probability &operator/= (const profile_probability &other)
391 if (*this == profile_probability::never ())
392 return *this = profile_probability::never ();
393 if (!initialized_p () || !other.initialized_p ())
394 return *this = profile_probability::uninitialized ();
395 else
397 if (m_val > other.m_val)
398 m_val = max_probability;
399 else if (!m_val)
401 else
403 gcc_checking_assert (other.m_val);
404 m_val = MIN (RDIV ((uint64_t)m_val * max_probability,
405 other.m_val),
406 max_probability);
408 m_quality = MIN (m_quality, other.m_quality);
410 return *this;
413 gcov_type apply (gcov_type val) const
415 if (*this == profile_probability::uninitialized ())
416 return val / 2;
417 return RDIV (val * m_val, max_probability);
420 /* Return 1-*THIS. */
421 profile_probability invert () const
423 return profile_probability::always() - *this;
426 /* Return THIS with quality dropped to GUESSED. */
427 profile_probability guessed () const
429 profile_probability ret = *this;
430 ret.m_quality = profile_guessed;
431 return ret;
434 /* Return THIS with quality dropped to AFDO. */
435 profile_probability afdo () const
437 profile_probability ret = *this;
438 ret.m_quality = profile_afdo;
439 return ret;
442 profile_probability combine_with_freq (int freq1, profile_probability other,
443 int freq2) const
445 profile_probability ret;
447 if (*this == profile_probability::uninitialized ()
448 || other == profile_probability::uninitialized ())
449 return profile_probability::uninitialized ();
451 gcc_checking_assert (freq1 >= 0 && freq2 >= 0);
452 if (!freq1 && !freq2)
454 ret.m_val = (m_val + other.m_val) / 2;
456 else
457 ret.m_val = RDIV (m_val * (uint64_t) freq1
458 + other.m_val * (uint64_t) freq2, freq1 + freq2);
459 ret.m_quality = MIN (m_quality, other.m_quality);
460 return ret;
463 /* Return *THIS * NUM / DEN. */
464 profile_probability apply_scale (int64_t num, int64_t den) const
466 if (*this == profile_probability::never ())
467 return *this;
468 if (!initialized_p ())
469 return profile_probability::uninitialized ();
470 profile_probability ret;
471 uint64_t tmp;
472 safe_scale_64bit (m_val, num, den, &tmp);
473 ret.m_val = MIN (tmp, max_probability);
474 ret.m_quality = MIN (m_quality, profile_adjusted);
475 return ret;
478 /* Return true when the probability of edge is reliable.
480 The profile guessing code is good at predicting branch outcome (ie.
481 taken/not taken), that is predicted right slightly over 75% of time.
482 It is however notoriously poor on predicting the probability itself.
483 In general the profile appear a lot flatter (with probabilities closer
484 to 50%) than the reality so it is bad idea to use it to drive optimization
485 such as those disabling dynamic branch prediction for well predictable
486 branches.
488 There are two exceptions - edges leading to noreturn edges and edges
489 predicted by number of iterations heuristics are predicted well. This macro
490 should be able to distinguish those, but at the moment it simply check for
491 noreturn heuristic that is only one giving probability over 99% or bellow
492 1%. In future we might want to propagate reliability information across the
493 CFG if we find this information useful on multiple places. */
495 bool probably_reliable_p () const
497 if (m_quality >= profile_adjusted)
498 return true;
499 if (!initialized_p ())
500 return false;
501 return m_val < max_probability / 100
502 || m_val > max_probability - max_probability / 100;
505 /* Return false if profile_probability is bogus. */
506 bool verify () const
508 gcc_checking_assert (profile_guessed_local <= m_quality
509 && m_quality <= profile_precise);
510 if (m_val == uninitialized_probability)
511 return m_quality == profile_guessed;
512 else if (m_quality < profile_guessed)
513 return false;
514 return m_val <= max_probability;
517 /* Comparsions are three-state and conservative. False is returned if
518 the inequality can not be decided. */
519 bool operator< (const profile_probability &other) const
521 return initialized_p () && other.initialized_p () && m_val < other.m_val;
523 bool operator> (const profile_probability &other) const
525 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;
532 bool operator>= (const profile_probability &other) const
534 return initialized_p () && other.initialized_p () && m_val >= other.m_val;
537 /* Output THIS to F. */
538 void dump (FILE *f) const;
540 /* Print THIS to stderr. */
541 void debug () const;
543 /* Return true if THIS is known to differ significantly from OTHER. */
544 bool differs_from_p (profile_probability other) const;
545 /* Return if difference is greater than 50%. */
546 bool differs_lot_from_p (profile_probability other) const;
548 /* LTO streaming support. */
549 static profile_probability stream_in (struct lto_input_block *);
550 void stream_out (struct output_block *);
551 void stream_out (struct lto_output_stream *);
554 /* Main data type to hold profile counters in GCC. Profile counts originate
555 either from profile feedback, static profile estimation or both. We do not
556 perform whole program profile propagation and thus profile estimation
557 counters are often local to function, while counters from profile feedback
558 (or special cases of profile estimation) can be used inter-procedurally.
560 There are 3 basic types
561 1) local counters which are result of intra-procedural static profile
562 estimation.
563 2) ipa counters which are result of profile feedback or special case
564 of static profile estimation (such as in function main).
565 3) counters which counts as 0 inter-procedurally (beause given function
566 was never run in train feedback) but they hold local static profile
567 estimate.
569 Counters of type 1 and 3 can not be mixed with counters of different type
570 within operation (because whole function should use one type of counter)
571 with exception that global zero mix in most operations where outcome is
572 well defined.
574 To take local counter and use it inter-procedurally use ipa member function
575 which strips information irelevant at the inter-procedural level.
577 Counters are 61bit integers representing number of executions during the
578 train run or normalized frequency within the function.
580 As the profile is maintained during the compilation, many adjustments are
581 made. Not all transformations can be made precisely, most importantly
582 when code is being duplicated. It also may happen that part of CFG has
583 profile counts known while other do not - for example when LTO optimizing
584 partly profiled program or when profile was lost due to COMDAT merging.
586 For this reason profile_count tracks more information than
587 just unsigned integer and it is also ready for profile mismatches.
588 The API of this data type represent operations that are natural
589 on profile counts - sum, difference and operation with scales and
590 probabilities. All operations are safe by never getting negative counts
591 and they do end up in uninitialized scale if any of the parameters is
592 uninitialized.
594 All comparsions that are three state and handling of probabilities. Thus
595 a < b is not equal to !(a >= b).
597 The following pre-defined counts are available:
599 profile_count::zero () for code that is known to execute zero times at
600 runtime (this can be detected statically i.e. for paths leading to
601 abort ();
602 profile_count::one () for code that is known to execute once (such as
603 main () function
604 profile_count::uninitialized () for unknown execution count.
608 class sreal;
610 class GTY(()) profile_count
612 public:
613 /* Use 62bit to hold basic block counters. Should be at least
614 64bit. Although a counter cannot be negative, we use a signed
615 type to hold various extra stages. */
617 static const int n_bits = 61;
618 private:
619 static const uint64_t max_count = ((uint64_t) 1 << n_bits) - 2;
620 static const uint64_t uninitialized_count = ((uint64_t) 1 << n_bits) - 1;
622 uint64_t m_val : n_bits;
623 enum profile_quality m_quality : 3;
625 /* Return true if both values can meaningfully appear in single function
626 body. We have either all counters in function local or global, otherwise
627 operations between them are not really defined well. */
628 bool compatible_p (const profile_count other) const
630 if (!initialized_p () || !other.initialized_p ())
631 return true;
632 if (*this == profile_count::zero ()
633 || other == profile_count::zero ())
634 return true;
635 return ipa_p () == other.ipa_p ();
637 public:
639 /* Used for counters which are expected to be never executed. */
640 static profile_count zero ()
642 return from_gcov_type (0);
644 static profile_count adjusted_zero ()
646 profile_count c;
647 c.m_val = 0;
648 c.m_quality = profile_adjusted;
649 return c;
651 static profile_count guessed_zero ()
653 profile_count c;
654 c.m_val = 0;
655 c.m_quality = profile_guessed;
656 return c;
658 static profile_count one ()
660 return from_gcov_type (1);
662 /* Value of counters which has not been initialized. Either because
663 initialization did not happen yet or because profile is unknown. */
664 static profile_count uninitialized ()
666 profile_count c;
667 c.m_val = uninitialized_count;
668 c.m_quality = profile_guessed_local;
669 return c;
672 /* Conversion to gcov_type is lossy. */
673 gcov_type to_gcov_type () const
675 gcc_checking_assert (initialized_p ());
676 return m_val;
679 /* Return true if value has been initialized. */
680 bool initialized_p () const
682 return m_val != uninitialized_count;
684 /* Return true if value can be trusted. */
685 bool reliable_p () const
687 return m_quality >= profile_adjusted;
689 /* Return true if vlaue can be operated inter-procedurally. */
690 bool ipa_p () const
692 return !initialized_p () || m_quality >= profile_guessed_global0;
694 /* Return true if quality of profile is precise. */
695 bool precise_p () const
697 return m_quality == profile_precise;
700 /* When merging basic blocks, the two different profile counts are unified.
701 Return true if this can be done without losing info about profile.
702 The only case we care about here is when first BB contains something
703 that makes it terminate in a way not visible in CFG. */
704 bool ok_for_merging (profile_count other) const
706 if (m_quality < profile_adjusted
707 || other.m_quality < profile_adjusted)
708 return true;
709 return !(other < *this);
712 /* When merging two BBs with different counts, pick common count that looks
713 most representative. */
714 profile_count merge (profile_count other) const
716 if (*this == other || !other.initialized_p ()
717 || m_quality > other.m_quality)
718 return *this;
719 if (other.m_quality > m_quality
720 || other > *this)
721 return other;
722 return *this;
725 /* Basic operations. */
726 bool operator== (const profile_count &other) const
728 return m_val == other.m_val && m_quality == other.m_quality;
730 profile_count operator+ (const profile_count &other) const
732 if (other == profile_count::zero ())
733 return *this;
734 if (*this == profile_count::zero ())
735 return other;
736 if (!initialized_p () || !other.initialized_p ())
737 return profile_count::uninitialized ();
739 profile_count ret;
740 gcc_checking_assert (compatible_p (other));
741 ret.m_val = m_val + other.m_val;
742 ret.m_quality = MIN (m_quality, other.m_quality);
743 return ret;
745 profile_count &operator+= (const profile_count &other)
747 if (other == profile_count::zero ())
748 return *this;
749 if (*this == profile_count::zero ())
751 *this = other;
752 return *this;
754 if (!initialized_p () || !other.initialized_p ())
755 return *this = profile_count::uninitialized ();
756 else
758 gcc_checking_assert (compatible_p (other));
759 m_val += other.m_val;
760 m_quality = MIN (m_quality, other.m_quality);
762 return *this;
764 profile_count operator- (const profile_count &other) const
766 if (*this == profile_count::zero () || other == profile_count::zero ())
767 return *this;
768 if (!initialized_p () || !other.initialized_p ())
769 return profile_count::uninitialized ();
770 gcc_checking_assert (compatible_p (other));
771 profile_count ret;
772 ret.m_val = m_val >= other.m_val ? m_val - other.m_val : 0;
773 ret.m_quality = MIN (m_quality, other.m_quality);
774 return ret;
776 profile_count &operator-= (const profile_count &other)
778 if (*this == profile_count::zero () || other == profile_count::zero ())
779 return *this;
780 if (!initialized_p () || !other.initialized_p ())
781 return *this = profile_count::uninitialized ();
782 else
784 gcc_checking_assert (compatible_p (other));
785 m_val = m_val >= other.m_val ? m_val - other.m_val: 0;
786 m_quality = MIN (m_quality, other.m_quality);
788 return *this;
791 /* Return false if profile_count is bogus. */
792 bool verify () const
794 gcc_checking_assert (profile_guessed_local <= m_quality
795 && m_quality <= profile_precise);
796 return m_val != uninitialized_count || m_quality == profile_guessed_local;
799 /* Comparsions are three-state and conservative. False is returned if
800 the inequality can not be decided. */
801 bool operator< (const profile_count &other) const
803 if (!initialized_p () || !other.initialized_p ())
804 return false;
805 if (*this == profile_count::zero ())
806 return !(other == profile_count::zero ());
807 if (other == profile_count::zero ())
808 return false;
809 gcc_checking_assert (compatible_p (other));
810 return m_val < other.m_val;
812 bool operator> (const profile_count &other) const
814 if (!initialized_p () || !other.initialized_p ())
815 return false;
816 if (*this == profile_count::zero ())
817 return false;
818 if (other == profile_count::zero ())
819 return !(*this == profile_count::zero ());
820 gcc_checking_assert (compatible_p (other));
821 return initialized_p () && other.initialized_p () && m_val > other.m_val;
823 bool operator< (const gcov_type other) const
825 gcc_checking_assert (ipa_p ());
826 gcc_checking_assert (other >= 0);
827 return initialized_p () && m_val < (uint64_t) other;
829 bool operator> (const gcov_type other) const
831 gcc_checking_assert (ipa_p ());
832 gcc_checking_assert (other >= 0);
833 return initialized_p () && m_val > (uint64_t) other;
836 bool operator<= (const profile_count &other) const
838 if (!initialized_p () || !other.initialized_p ())
839 return false;
840 if (*this == profile_count::zero ())
841 return true;
842 if (other == profile_count::zero ())
843 return (*this == profile_count::zero ());
844 gcc_checking_assert (compatible_p (other));
845 return m_val <= other.m_val;
847 bool operator>= (const profile_count &other) const
849 if (!initialized_p () || !other.initialized_p ())
850 return false;
851 if (other == profile_count::zero ())
852 return true;
853 if (*this == profile_count::zero ())
854 return !(other == profile_count::zero ());
855 gcc_checking_assert (compatible_p (other));
856 return m_val >= other.m_val;
858 bool operator<= (const gcov_type other) const
860 gcc_checking_assert (ipa_p ());
861 gcc_checking_assert (other >= 0);
862 return initialized_p () && m_val <= (uint64_t) other;
864 bool operator>= (const gcov_type other) const
866 gcc_checking_assert (ipa_p ());
867 gcc_checking_assert (other >= 0);
868 return initialized_p () && m_val >= (uint64_t) other;
870 /* Return true when value is not zero and can be used for scaling.
871 This is different from *this > 0 because that requires counter to
872 be IPA. */
873 bool nonzero_p () const
875 return initialized_p () && m_val != 0;
878 /* Make counter forcingly nonzero. */
879 profile_count force_nonzero () const
881 if (!initialized_p ())
882 return *this;
883 profile_count ret = *this;
884 if (ret.m_val == 0)
885 ret.m_val = 1;
886 return ret;
889 profile_count max (profile_count other) const
891 if (!initialized_p ())
892 return other;
893 if (!other.initialized_p ())
894 return *this;
895 if (*this == profile_count::zero ())
896 return other;
897 if (other == profile_count::zero ())
898 return *this;
899 gcc_checking_assert (compatible_p (other));
900 if (m_val < other.m_val || (m_val == other.m_val
901 && m_quality < other.m_quality))
902 return other;
903 return *this;
906 /* PROB is a probability in scale 0...REG_BR_PROB_BASE. Scale counter
907 accordingly. */
908 profile_count apply_probability (int prob) const
910 gcc_checking_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
911 if (m_val == 0)
912 return *this;
913 if (!initialized_p ())
914 return profile_count::uninitialized ();
915 profile_count ret;
916 ret.m_val = RDIV (m_val * prob, REG_BR_PROB_BASE);
917 ret.m_quality = MIN (m_quality, profile_adjusted);
918 return ret;
921 /* Scale counter according to PROB. */
922 profile_count apply_probability (profile_probability prob) const
924 if (*this == profile_count::zero ())
925 return *this;
926 if (prob == profile_probability::never ())
927 return profile_count::zero ();
928 if (!initialized_p ())
929 return profile_count::uninitialized ();
930 profile_count ret;
931 uint64_t tmp;
932 safe_scale_64bit (m_val, prob.m_val, profile_probability::max_probability,
933 &tmp);
934 ret.m_val = tmp;
935 ret.m_quality = MIN (m_quality, prob.m_quality);
936 return ret;
938 /* Return *THIS * NUM / DEN. */
939 profile_count apply_scale (int64_t num, int64_t den) const
941 if (m_val == 0)
942 return *this;
943 if (!initialized_p ())
944 return profile_count::uninitialized ();
945 profile_count ret;
946 uint64_t tmp;
948 gcc_checking_assert (num >= 0 && den > 0);
949 safe_scale_64bit (m_val, num, den, &tmp);
950 ret.m_val = MIN (tmp, max_count);
951 ret.m_quality = MIN (m_quality, profile_adjusted);
952 return ret;
954 profile_count apply_scale (profile_count num, profile_count den) const
956 if (*this == profile_count::zero ())
957 return *this;
958 if (num == profile_count::zero ())
959 return num;
960 if (!initialized_p () || !num.initialized_p () || !den.initialized_p ())
961 return profile_count::uninitialized ();
962 if (num == den)
963 return *this;
964 gcc_checking_assert (den.m_val);
966 profile_count ret;
967 uint64_t val;
968 safe_scale_64bit (m_val, num.m_val, den.m_val, &val);
969 ret.m_val = MIN (val, max_count);
970 ret.m_quality = MIN (MIN (MIN (m_quality, profile_adjusted),
971 num.m_quality), den.m_quality);
972 if (num.ipa_p () && !ret.ipa_p ())
973 ret.m_quality = MIN (num.m_quality, profile_guessed);
974 return ret;
977 /* Return THIS with quality dropped to GUESSED_LOCAL. */
978 profile_count guessed_local () const
980 profile_count ret = *this;
981 if (!initialized_p ())
982 return *this;
983 ret.m_quality = profile_guessed_local;
984 return ret;
987 /* We know that profile is globally 0 but keep local profile if present. */
988 profile_count global0 () const
990 profile_count ret = *this;
991 if (!initialized_p ())
992 return *this;
993 ret.m_quality = profile_guessed_global0;
994 return ret;
997 /* We know that profile is globally adjusted 0 but keep local profile
998 if present. */
999 profile_count global0adjusted () const
1001 profile_count ret = *this;
1002 if (!initialized_p ())
1003 return *this;
1004 ret.m_quality = profile_guessed_global0adjusted;
1005 return ret;
1008 /* Return THIS with quality dropped to GUESSED. */
1009 profile_count guessed () const
1011 profile_count ret = *this;
1012 ret.m_quality = MIN (ret.m_quality, profile_guessed);
1013 return ret;
1016 /* Return variant of profile counte which is always safe to compare
1017 acorss functions. */
1018 profile_count ipa () const
1020 if (m_quality > profile_guessed_global0adjusted)
1021 return *this;
1022 if (m_quality == profile_guessed_global0)
1023 return profile_count::zero ();
1024 if (m_quality == profile_guessed_global0adjusted)
1025 return profile_count::adjusted_zero ();
1026 return profile_count::uninitialized ();
1029 /* Return THIS with quality dropped to AFDO. */
1030 profile_count afdo () const
1032 profile_count ret = *this;
1033 ret.m_quality = profile_afdo;
1034 return ret;
1037 /* Return probability of event with counter THIS within event with counter
1038 OVERALL. */
1039 profile_probability probability_in (const profile_count overall) const
1041 if (*this == profile_count::zero ())
1042 return profile_probability::never ();
1043 if (!initialized_p () || !overall.initialized_p ()
1044 || !overall.m_val)
1045 return profile_probability::uninitialized ();
1046 profile_probability ret;
1047 gcc_checking_assert (compatible_p (overall));
1049 if (overall.m_val < m_val)
1050 ret.m_val = profile_probability::max_probability;
1051 else
1052 ret.m_val = RDIV (m_val * profile_probability::max_probability,
1053 overall.m_val);
1054 ret.m_quality = MAX (MIN (m_quality, overall.m_quality), profile_guessed);
1055 return ret;
1058 int to_frequency (struct function *fun) const;
1059 int to_cgraph_frequency (profile_count entry_bb_count) const;
1060 sreal to_sreal_scale (profile_count in, bool *known = NULL) const;
1062 /* Output THIS to F. */
1063 void dump (FILE *f) const;
1065 /* Print THIS to stderr. */
1066 void debug () const;
1068 /* Return true if THIS is known to differ significantly from OTHER. */
1069 bool differs_from_p (profile_count other) const;
1071 /* We want to scale profile across function boundary from NUM to DEN.
1072 Take care of the side case when NUM and DEN are zeros of incompatible
1073 kinds. */
1074 static void adjust_for_ipa_scaling (profile_count *num, profile_count *den);
1076 /* THIS is a count of bb which is known to be executed IPA times.
1077 Combine this information into bb counter. This means returning IPA
1078 if it is nonzero, not changing anything if IPA is uninitialized
1079 and if IPA is zero, turning THIS into corresponding local profile with
1080 global0. */
1081 profile_count combine_with_ipa_count (profile_count ipa);
1083 /* The profiling runtime uses gcov_type, which is usually 64bit integer.
1084 Conversions back and forth are used to read the coverage and get it
1085 into internal representation. */
1086 static profile_count from_gcov_type (gcov_type v);
1088 /* LTO streaming support. */
1089 static profile_count stream_in (struct lto_input_block *);
1090 void stream_out (struct output_block *);
1091 void stream_out (struct lto_output_stream *);
1093 #endif