profile-count.h 37 KB

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