symbol-summary.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* Callgraph summary data structure.
  2. Copyright (C) 2014-2018 Free Software Foundation, Inc.
  3. Contributed by Martin Liska
  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_SYMBOL_SUMMARY_H
  17. #define GCC_SYMBOL_SUMMARY_H
  18. /* We want to pass just pointer types as argument for function_summary
  19. template class. */
  20. template <class T>
  21. class function_summary
  22. {
  23. private:
  24. function_summary();
  25. };
  26. template <class T>
  27. class GTY((user)) function_summary <T *>
  28. {
  29. public:
  30. /* Default construction takes SYMTAB as an argument. */
  31. function_summary (symbol_table *symtab, bool ggc = false): m_ggc (ggc),
  32. m_insertion_enabled (true), m_released (false), m_map (13, ggc),
  33. m_symtab (symtab)
  34. {
  35. m_symtab_insertion_hook =
  36. symtab->add_cgraph_insertion_hook
  37. (function_summary::symtab_insertion, this);
  38. m_symtab_removal_hook =
  39. symtab->add_cgraph_removal_hook
  40. (function_summary::symtab_removal, this);
  41. m_symtab_duplication_hook =
  42. symtab->add_cgraph_duplication_hook
  43. (function_summary::symtab_duplication, this);
  44. }
  45. /* Destructor. */
  46. virtual ~function_summary ()
  47. {
  48. release ();
  49. }
  50. /* Destruction method that can be called for GGT purpose. */
  51. void release ()
  52. {
  53. if (m_released)
  54. return;
  55. m_symtab->remove_cgraph_insertion_hook (m_symtab_insertion_hook);
  56. m_symtab->remove_cgraph_removal_hook (m_symtab_removal_hook);
  57. m_symtab->remove_cgraph_duplication_hook (m_symtab_duplication_hook);
  58. /* Release all summaries. */
  59. typedef typename hash_map <map_hash, T *>::iterator map_iterator;
  60. for (map_iterator it = m_map.begin (); it != m_map.end (); ++it)
  61. release ((*it).second);
  62. m_released = true;
  63. }
  64. /* Traverses all summarys with a function F called with
  65. ARG as argument. */
  66. template<typename Arg, bool (*f)(const T &, Arg)>
  67. void traverse (Arg a) const
  68. {
  69. m_map.traverse <f> (a);
  70. }
  71. /* Basic implementation of insert operation. */
  72. virtual void insert (cgraph_node *, T *) {}
  73. /* Basic implementation of removal operation. */
  74. virtual void remove (cgraph_node *, T *) {}
  75. /* Basic implementation of duplication operation. */
  76. virtual void duplicate (cgraph_node *, cgraph_node *, T *, T *) {}
  77. /* Allocates new data that are stored within map. */
  78. T* allocate_new ()
  79. {
  80. /* Call gcc_internal_because we do not want to call finalizer for
  81. a type T. We call dtor explicitly. */
  82. return m_ggc ? new (ggc_internal_alloc (sizeof (T))) T () : new T () ;
  83. }
  84. /* Release an item that is stored within map. */
  85. void release (T *item)
  86. {
  87. if (m_ggc)
  88. {
  89. item->~T ();
  90. ggc_free (item);
  91. }
  92. else
  93. delete item;
  94. }
  95. /* Getter for summary callgraph node pointer. */
  96. T* get (cgraph_node *node)
  97. {
  98. gcc_checking_assert (node->summary_uid);
  99. return get (node->summary_uid);
  100. }
  101. /* Return number of elements handled by data structure. */
  102. size_t elements ()
  103. {
  104. return m_map.elements ();
  105. }
  106. /* Return true if a summary for the given NODE already exists. */
  107. bool exists (cgraph_node *node)
  108. {
  109. return m_map.get (node->summary_uid) != NULL;
  110. }
  111. /* Enable insertion hook invocation. */
  112. void enable_insertion_hook ()
  113. {
  114. m_insertion_enabled = true;
  115. }
  116. /* Enable insertion hook invocation. */
  117. void disable_insertion_hook ()
  118. {
  119. m_insertion_enabled = false;
  120. }
  121. /* Symbol insertion hook that is registered to symbol table. */
  122. static void symtab_insertion (cgraph_node *node, void *data)
  123. {
  124. gcc_checking_assert (node->summary_uid);
  125. function_summary *summary = (function_summary <T *> *) (data);
  126. if (summary->m_insertion_enabled)
  127. summary->insert (node, summary->get (node));
  128. }
  129. /* Symbol removal hook that is registered to symbol table. */
  130. static void symtab_removal (cgraph_node *node, void *data)
  131. {
  132. gcc_checking_assert (node->summary_uid);
  133. function_summary *summary = (function_summary <T *> *) (data);
  134. int summary_uid = node->summary_uid;
  135. T **v = summary->m_map.get (summary_uid);
  136. if (v)
  137. {
  138. summary->remove (node, *v);
  139. summary->release (*v);
  140. summary->m_map.remove (summary_uid);
  141. }
  142. }
  143. /* Symbol duplication hook that is registered to symbol table. */
  144. static void symtab_duplication (cgraph_node *node, cgraph_node *node2,
  145. void *data)
  146. {
  147. function_summary *summary = (function_summary <T *> *) (data);
  148. T **v = summary->m_map.get (node->summary_uid);
  149. gcc_checking_assert (node2->summary_uid > 0);
  150. if (v)
  151. {
  152. /* This load is necessary, because we insert a new value! */
  153. T *data = *v;
  154. T *duplicate = summary->allocate_new ();
  155. summary->m_map.put (node2->summary_uid, duplicate);
  156. summary->duplicate (node, node2, data, duplicate);
  157. }
  158. }
  159. protected:
  160. /* Indication if we use ggc summary. */
  161. bool m_ggc;
  162. private:
  163. typedef int_hash <int, 0, -1> map_hash;
  164. /* Getter for summary callgraph ID. */
  165. T* get (int uid)
  166. {
  167. bool existed;
  168. T **v = &m_map.get_or_insert (uid, &existed);
  169. if (!existed)
  170. *v = allocate_new ();
  171. return *v;
  172. }
  173. /* Indicates if insertion hook is enabled. */
  174. bool m_insertion_enabled;
  175. /* Indicates if the summary is released. */
  176. bool m_released;
  177. /* Main summary store, where summary ID is used as key. */
  178. hash_map <map_hash, T *> m_map;
  179. /* Internal summary insertion hook pointer. */
  180. cgraph_node_hook_list *m_symtab_insertion_hook;
  181. /* Internal summary removal hook pointer. */
  182. cgraph_node_hook_list *m_symtab_removal_hook;
  183. /* Internal summary duplication hook pointer. */
  184. cgraph_2node_hook_list *m_symtab_duplication_hook;
  185. /* Symbol table the summary is registered to. */
  186. symbol_table *m_symtab;
  187. template <typename U> friend void gt_ggc_mx (function_summary <U *> * const &);
  188. template <typename U> friend void gt_pch_nx (function_summary <U *> * const &);
  189. template <typename U> friend void gt_pch_nx (function_summary <U *> * const &,
  190. gt_pointer_operator, void *);
  191. };
  192. template <typename T>
  193. void
  194. gt_ggc_mx(function_summary<T *>* const &summary)
  195. {
  196. gcc_checking_assert (summary->m_ggc);
  197. gt_ggc_mx (&summary->m_map);
  198. }
  199. template <typename T>
  200. void
  201. gt_pch_nx(function_summary<T *>* const &summary)
  202. {
  203. gcc_checking_assert (summary->m_ggc);
  204. gt_pch_nx (&summary->m_map);
  205. }
  206. template <typename T>
  207. void
  208. gt_pch_nx(function_summary<T *>* const& summary, gt_pointer_operator op,
  209. void *cookie)
  210. {
  211. gcc_checking_assert (summary->m_ggc);
  212. gt_pch_nx (&summary->m_map, op, cookie);
  213. }
  214. /* An impossible class templated by non-pointers so, which makes sure that only
  215. summaries gathering pointers can be created. */
  216. template <class T>
  217. class call_summary
  218. {
  219. private:
  220. call_summary();
  221. };
  222. /* Class to store auxiliary information about call graph edges. */
  223. template <class T>
  224. class GTY((user)) call_summary <T *>
  225. {
  226. public:
  227. /* Default construction takes SYMTAB as an argument. */
  228. call_summary (symbol_table *symtab, bool ggc = false): m_ggc (ggc),
  229. m_map (13, ggc), m_released (false), m_symtab (symtab)
  230. {
  231. m_symtab_removal_hook =
  232. symtab->add_edge_removal_hook
  233. (call_summary::symtab_removal, this);
  234. m_symtab_duplication_hook =
  235. symtab->add_edge_duplication_hook
  236. (call_summary::symtab_duplication, this);
  237. }
  238. /* Destructor. */
  239. virtual ~call_summary ()
  240. {
  241. release ();
  242. }
  243. /* Destruction method that can be called for GGT purpose. */
  244. void release ()
  245. {
  246. if (m_released)
  247. return;
  248. m_symtab->remove_edge_removal_hook (m_symtab_removal_hook);
  249. m_symtab->remove_edge_duplication_hook (m_symtab_duplication_hook);
  250. /* Release all summaries. */
  251. typedef typename hash_map <map_hash, T *>::iterator map_iterator;
  252. for (map_iterator it = m_map.begin (); it != m_map.end (); ++it)
  253. release ((*it).second);
  254. m_released = true;
  255. }
  256. /* Traverses all summarys with a function F called with
  257. ARG as argument. */
  258. template<typename Arg, bool (*f)(const T &, Arg)>
  259. void traverse (Arg a) const
  260. {
  261. m_map.traverse <f> (a);
  262. }
  263. /* Basic implementation of removal operation. */
  264. virtual void remove (cgraph_edge *, T *) {}
  265. /* Basic implementation of duplication operation. */
  266. virtual void duplicate (cgraph_edge *, cgraph_edge *, T *, T *) {}
  267. /* Allocates new data that are stored within map. */
  268. T* allocate_new ()
  269. {
  270. /* Call gcc_internal_because we do not want to call finalizer for
  271. a type T. We call dtor explicitly. */
  272. return m_ggc ? new (ggc_internal_alloc (sizeof (T))) T () : new T () ;
  273. }
  274. /* Release an item that is stored within map. */
  275. void release (T *item)
  276. {
  277. if (m_ggc)
  278. {
  279. item->~T ();
  280. ggc_free (item);
  281. }
  282. else
  283. delete item;
  284. }
  285. /* Getter for summary callgraph edge pointer. */
  286. T* get (cgraph_edge *edge)
  287. {
  288. return get (hashable_uid (edge));
  289. }
  290. /* Return number of elements handled by data structure. */
  291. size_t elements ()
  292. {
  293. return m_map.elements ();
  294. }
  295. /* Return true if a summary for the given EDGE already exists. */
  296. bool exists (cgraph_edge *edge)
  297. {
  298. return m_map.get (hashable_uid (edge)) != NULL;
  299. }
  300. /* Symbol removal hook that is registered to symbol table. */
  301. static void symtab_removal (cgraph_edge *edge, void *data)
  302. {
  303. call_summary *summary = (call_summary <T *> *) (data);
  304. int h_uid = summary->hashable_uid (edge);
  305. T **v = summary->m_map.get (h_uid);
  306. if (v)
  307. {
  308. summary->remove (edge, *v);
  309. summary->release (*v);
  310. summary->m_map.remove (h_uid);
  311. }
  312. }
  313. /* Symbol duplication hook that is registered to symbol table. */
  314. static void symtab_duplication (cgraph_edge *edge1, cgraph_edge *edge2,
  315. void *data)
  316. {
  317. call_summary *summary = (call_summary <T *> *) (data);
  318. T **v = summary->m_map.get (summary->hashable_uid (edge1));
  319. if (v)
  320. {
  321. /* This load is necessary, because we insert a new value! */
  322. T *data = *v;
  323. T *duplicate = summary->allocate_new ();
  324. summary->m_map.put (summary->hashable_uid (edge2), duplicate);
  325. summary->duplicate (edge1, edge2, data, duplicate);
  326. }
  327. }
  328. protected:
  329. /* Indication if we use ggc summary. */
  330. bool m_ggc;
  331. private:
  332. typedef int_hash <int, 0, -1> map_hash;
  333. /* Getter for summary callgraph ID. */
  334. T* get (int uid)
  335. {
  336. bool existed;
  337. T **v = &m_map.get_or_insert (uid, &existed);
  338. if (!existed)
  339. *v = allocate_new ();
  340. return *v;
  341. }
  342. /* Get a hashable uid of EDGE. */
  343. int hashable_uid (cgraph_edge *edge)
  344. {
  345. /* Edge uids start at zero which our hash_map does not like. */
  346. return edge->uid + 1;
  347. }
  348. /* Main summary store, where summary ID is used as key. */
  349. hash_map <map_hash, T *> m_map;
  350. /* Internal summary removal hook pointer. */
  351. cgraph_edge_hook_list *m_symtab_removal_hook;
  352. /* Internal summary duplication hook pointer. */
  353. cgraph_2edge_hook_list *m_symtab_duplication_hook;
  354. /* Indicates if the summary is released. */
  355. bool m_released;
  356. /* Symbol table the summary is registered to. */
  357. symbol_table *m_symtab;
  358. template <typename U> friend void gt_ggc_mx (call_summary <U *> * const &);
  359. template <typename U> friend void gt_pch_nx (call_summary <U *> * const &);
  360. template <typename U> friend void gt_pch_nx (call_summary <U *> * const &,
  361. gt_pointer_operator, void *);
  362. };
  363. template <typename T>
  364. void
  365. gt_ggc_mx(call_summary<T *>* const &summary)
  366. {
  367. gcc_checking_assert (summary->m_ggc);
  368. gt_ggc_mx (&summary->m_map);
  369. }
  370. template <typename T>
  371. void
  372. gt_pch_nx(call_summary<T *>* const &summary)
  373. {
  374. gcc_checking_assert (summary->m_ggc);
  375. gt_pch_nx (&summary->m_map);
  376. }
  377. template <typename T>
  378. void
  379. gt_pch_nx(call_summary<T *>* const& summary, gt_pointer_operator op,
  380. void *cookie)
  381. {
  382. gcc_checking_assert (summary->m_ggc);
  383. gt_pch_nx (&summary->m_map, op, cookie);
  384. }
  385. #endif /* GCC_SYMBOL_SUMMARY_H */