symbol-summary.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Callgraph summary data structure.
  2. Copyright (C) 2014-2015 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_map (13, ggc), m_insertion_enabled (true), m_symtab (symtab)
  33. {
  34. #ifdef ENABLE_CHECKING
  35. cgraph_node *node;
  36. FOR_EACH_FUNCTION (node)
  37. {
  38. gcc_checking_assert (node->summary_uid > 0);
  39. }
  40. #endif
  41. m_symtab_insertion_hook =
  42. symtab->add_cgraph_insertion_hook
  43. (function_summary::symtab_insertion, this);
  44. m_symtab_removal_hook =
  45. symtab->add_cgraph_removal_hook
  46. (function_summary::symtab_removal, this);
  47. m_symtab_duplication_hook =
  48. symtab->add_cgraph_duplication_hook
  49. (function_summary::symtab_duplication, this);
  50. }
  51. /* Destructor. */
  52. virtual ~function_summary ()
  53. {
  54. release ();
  55. }
  56. /* Destruction method that can be called for GGT purpose. */
  57. void release ()
  58. {
  59. if (m_symtab_insertion_hook)
  60. m_symtab->remove_cgraph_insertion_hook (m_symtab_insertion_hook);
  61. if (m_symtab_removal_hook)
  62. m_symtab->remove_cgraph_removal_hook (m_symtab_removal_hook);
  63. if (m_symtab_duplication_hook)
  64. m_symtab->remove_cgraph_duplication_hook (m_symtab_duplication_hook);
  65. m_symtab_insertion_hook = NULL;
  66. m_symtab_removal_hook = NULL;
  67. m_symtab_duplication_hook = NULL;
  68. /* Release all summaries. */
  69. typedef typename hash_map <int, T *, summary_hashmap_traits>::iterator map_iterator;
  70. for (map_iterator it = m_map.begin (); it != m_map.end (); ++it)
  71. release ((*it).second);
  72. }
  73. /* Traverses all summarys with a function F called with
  74. ARG as argument. */
  75. template<typename Arg, bool (*f)(const T &, Arg)>
  76. void traverse (Arg a) const
  77. {
  78. m_map.traverse <f> (a);
  79. }
  80. /* Basic implementation of insert operation. */
  81. virtual void insert (cgraph_node *, T *) {}
  82. /* Basic implementation of removal operation. */
  83. virtual void remove (cgraph_node *, T *) {}
  84. /* Basic implementation of duplication operation. */
  85. virtual void duplicate (cgraph_node *, cgraph_node *, T *, T *) {}
  86. /* Allocates new data that are stored within map. */
  87. T* allocate_new ()
  88. {
  89. return m_ggc ? new (ggc_alloc <T> ()) T() : new T () ;
  90. }
  91. /* Release an item that is stored within map. */
  92. void release (T *item)
  93. {
  94. if (m_ggc)
  95. {
  96. item->~T ();
  97. ggc_free (item);
  98. }
  99. else
  100. delete item;
  101. }
  102. /* Getter for summary callgraph node pointer. */
  103. T* get (cgraph_node *node)
  104. {
  105. return get (node->summary_uid);
  106. }
  107. /* Return number of elements handled by data structure. */
  108. size_t elements ()
  109. {
  110. return m_map.elements ();
  111. }
  112. /* Enable insertion hook invocation. */
  113. void enable_insertion_hook ()
  114. {
  115. m_insertion_enabled = true;
  116. }
  117. /* Enable insertion hook invocation. */
  118. void disable_insertion_hook ()
  119. {
  120. m_insertion_enabled = false;
  121. }
  122. /* Symbol insertion hook that is registered to symbol table. */
  123. static void symtab_insertion (cgraph_node *node, void *data)
  124. {
  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. if (!summary->m_ggc)
  140. delete (*v);
  141. summary->m_map.remove (summary_uid);
  142. }
  143. }
  144. /* Symbol duplication hook that is registered to symbol table. */
  145. static void symtab_duplication (cgraph_node *node, cgraph_node *node2,
  146. void *data)
  147. {
  148. function_summary *summary = (function_summary <T *> *) (data);
  149. T **v = summary->m_map.get (node->summary_uid);
  150. gcc_checking_assert (node2->summary_uid > 0);
  151. if (v)
  152. {
  153. /* This load is necessary, because we insert a new value! */
  154. T *data = *v;
  155. T *duplicate = summary->allocate_new ();
  156. summary->m_map.put (node2->summary_uid, duplicate);
  157. summary->duplicate (node, node2, data, duplicate);
  158. }
  159. }
  160. protected:
  161. /* Indication if we use ggc summary. */
  162. bool m_ggc;
  163. private:
  164. struct summary_hashmap_traits: default_hashmap_traits
  165. {
  166. static const int deleted_value = -1;
  167. static const int empty_value = 0;
  168. static hashval_t
  169. hash (const int v)
  170. {
  171. return (hashval_t)v;
  172. }
  173. template<typename Type>
  174. static bool
  175. is_deleted (Type &e)
  176. {
  177. return e.m_key == deleted_value;
  178. }
  179. template<typename Type>
  180. static bool
  181. is_empty (Type &e)
  182. {
  183. return e.m_key == empty_value;
  184. }
  185. template<typename Type>
  186. static void
  187. mark_deleted (Type &e)
  188. {
  189. e.m_key = deleted_value;
  190. }
  191. template<typename Type>
  192. static void
  193. mark_empty (Type &e)
  194. {
  195. e.m_key = empty_value;
  196. }
  197. };
  198. /* Getter for summary callgraph ID. */
  199. T* get (int uid)
  200. {
  201. bool existed;
  202. T **v = &m_map.get_or_insert (uid, &existed);
  203. if (!existed)
  204. *v = allocate_new ();
  205. return *v;
  206. }
  207. /* Main summary store, where summary ID is used as key. */
  208. hash_map <int, T *, summary_hashmap_traits> m_map;
  209. /* Internal summary insertion hook pointer. */
  210. cgraph_node_hook_list *m_symtab_insertion_hook;
  211. /* Internal summary removal hook pointer. */
  212. cgraph_node_hook_list *m_symtab_removal_hook;
  213. /* Internal summary duplication hook pointer. */
  214. cgraph_2node_hook_list *m_symtab_duplication_hook;
  215. /* Indicates if insertion hook is enabled. */
  216. bool m_insertion_enabled;
  217. /* Symbol table the summary is registered to. */
  218. symbol_table *m_symtab;
  219. template <typename U> friend void gt_ggc_mx (function_summary <U *> * const &);
  220. template <typename U> friend void gt_pch_nx (function_summary <U *> * const &);
  221. template <typename U> friend void gt_pch_nx (function_summary <U *> * const &,
  222. gt_pointer_operator, void *);
  223. };
  224. template <typename T>
  225. void
  226. gt_ggc_mx(function_summary<T *>* const &summary)
  227. {
  228. gcc_checking_assert (summary->m_ggc);
  229. gt_ggc_mx (&summary->m_map);
  230. }
  231. template <typename T>
  232. void
  233. gt_pch_nx(function_summary<T *>* const &summary)
  234. {
  235. gcc_checking_assert (summary->m_ggc);
  236. gt_pch_nx (&summary->m_map);
  237. }
  238. template <typename T>
  239. void
  240. gt_pch_nx(function_summary<T *>* const& summary, gt_pointer_operator op,
  241. void *cookie)
  242. {
  243. gcc_checking_assert (summary->m_ggc);
  244. gt_pch_nx (&summary->m_map, op, cookie);
  245. }
  246. #endif /* GCC_SYMBOL_SUMMARY_H */