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