profile-count.h 37 KB

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