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