profile-count.h 37 KB

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