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