[SFN] Introduce -gstatement-frontiers option, enable debug markers
[official-gcc.git] / gcc / symbol-summary.h
blobe672c818b746a783f420c93f3c67fd9f9494a82b
1 /* Callgraph summary data structure.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
3 Contributed by Martin Liska
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_SYMBOL_SUMMARY_H
22 #define GCC_SYMBOL_SUMMARY_H
24 /* We want to pass just pointer types as argument for function_summary
25 template class. */
27 template <class T>
28 class function_summary
30 private:
31 function_summary();
34 template <class T>
35 class GTY((user)) function_summary <T *>
37 public:
38 /* Default construction takes SYMTAB as an argument. */
39 function_summary (symbol_table *symtab, bool ggc = false): m_ggc (ggc),
40 m_insertion_enabled (true), m_released (false), m_map (13, ggc),
41 m_symtab (symtab)
43 m_symtab_insertion_hook =
44 symtab->add_cgraph_insertion_hook
45 (function_summary::symtab_insertion, this);
47 m_symtab_removal_hook =
48 symtab->add_cgraph_removal_hook
49 (function_summary::symtab_removal, this);
50 m_symtab_duplication_hook =
51 symtab->add_cgraph_duplication_hook
52 (function_summary::symtab_duplication, this);
55 /* Destructor. */
56 virtual ~function_summary ()
58 release ();
61 /* Destruction method that can be called for GGT purpose. */
62 void release ()
64 if (m_released)
65 return;
67 m_symtab->remove_cgraph_insertion_hook (m_symtab_insertion_hook);
68 m_symtab->remove_cgraph_removal_hook (m_symtab_removal_hook);
69 m_symtab->remove_cgraph_duplication_hook (m_symtab_duplication_hook);
71 /* Release all summaries. */
72 typedef typename hash_map <map_hash, T *>::iterator map_iterator;
73 for (map_iterator it = m_map.begin (); it != m_map.end (); ++it)
74 release ((*it).second);
76 m_released = true;
79 /* Traverses all summarys with a function F called with
80 ARG as argument. */
81 template<typename Arg, bool (*f)(const T &, Arg)>
82 void traverse (Arg a) const
84 m_map.traverse <f> (a);
87 /* Basic implementation of insert operation. */
88 virtual void insert (cgraph_node *, T *) {}
90 /* Basic implementation of removal operation. */
91 virtual void remove (cgraph_node *, T *) {}
93 /* Basic implementation of duplication operation. */
94 virtual void duplicate (cgraph_node *, cgraph_node *, T *, T *) {}
96 /* Allocates new data that are stored within map. */
97 T* allocate_new ()
99 /* Call gcc_internal_because we do not want to call finalizer for
100 a type T. We call dtor explicitly. */
101 return m_ggc ? new (ggc_internal_alloc (sizeof (T))) T () : new T () ;
104 /* Release an item that is stored within map. */
105 void release (T *item)
107 if (m_ggc)
109 item->~T ();
110 ggc_free (item);
112 else
113 delete item;
116 /* Getter for summary callgraph node pointer. */
117 T* get (cgraph_node *node)
119 gcc_checking_assert (node->summary_uid);
120 return get (node->summary_uid);
123 /* Return number of elements handled by data structure. */
124 size_t elements ()
126 return m_map.elements ();
129 /* Return true if a summary for the given NODE already exists. */
130 bool exists (cgraph_node *node)
132 return m_map.get (node->summary_uid) != NULL;
135 /* Enable insertion hook invocation. */
136 void enable_insertion_hook ()
138 m_insertion_enabled = true;
141 /* Enable insertion hook invocation. */
142 void disable_insertion_hook ()
144 m_insertion_enabled = false;
147 /* Symbol insertion hook that is registered to symbol table. */
148 static void symtab_insertion (cgraph_node *node, void *data)
150 gcc_checking_assert (node->summary_uid);
151 function_summary *summary = (function_summary <T *> *) (data);
153 if (summary->m_insertion_enabled)
154 summary->insert (node, summary->get (node));
157 /* Symbol removal hook that is registered to symbol table. */
158 static void symtab_removal (cgraph_node *node, void *data)
160 gcc_checking_assert (node->summary_uid);
161 function_summary *summary = (function_summary <T *> *) (data);
163 int summary_uid = node->summary_uid;
164 T **v = summary->m_map.get (summary_uid);
166 if (v)
168 summary->remove (node, *v);
169 summary->release (*v);
170 summary->m_map.remove (summary_uid);
174 /* Symbol duplication hook that is registered to symbol table. */
175 static void symtab_duplication (cgraph_node *node, cgraph_node *node2,
176 void *data)
178 function_summary *summary = (function_summary <T *> *) (data);
179 T **v = summary->m_map.get (node->summary_uid);
181 gcc_checking_assert (node2->summary_uid > 0);
183 if (v)
185 /* This load is necessary, because we insert a new value! */
186 T *data = *v;
187 T *duplicate = summary->allocate_new ();
188 summary->m_map.put (node2->summary_uid, duplicate);
189 summary->duplicate (node, node2, data, duplicate);
193 protected:
194 /* Indication if we use ggc summary. */
195 bool m_ggc;
197 private:
198 typedef int_hash <int, 0, -1> map_hash;
200 /* Getter for summary callgraph ID. */
201 T* get (int uid)
203 bool existed;
204 T **v = &m_map.get_or_insert (uid, &existed);
205 if (!existed)
206 *v = allocate_new ();
208 return *v;
211 /* Indicates if insertion hook is enabled. */
212 bool m_insertion_enabled;
213 /* Indicates if the summary is released. */
214 bool m_released;
215 /* Main summary store, where summary ID is used as key. */
216 hash_map <map_hash, T *> m_map;
217 /* Internal summary insertion hook pointer. */
218 cgraph_node_hook_list *m_symtab_insertion_hook;
219 /* Internal summary removal hook pointer. */
220 cgraph_node_hook_list *m_symtab_removal_hook;
221 /* Internal summary duplication hook pointer. */
222 cgraph_2node_hook_list *m_symtab_duplication_hook;
223 /* Symbol table the summary is registered to. */
224 symbol_table *m_symtab;
226 template <typename U> friend void gt_ggc_mx (function_summary <U *> * const &);
227 template <typename U> friend void gt_pch_nx (function_summary <U *> * const &);
228 template <typename U> friend void gt_pch_nx (function_summary <U *> * const &,
229 gt_pointer_operator, void *);
232 template <typename T>
233 void
234 gt_ggc_mx(function_summary<T *>* const &summary)
236 gcc_checking_assert (summary->m_ggc);
237 gt_ggc_mx (&summary->m_map);
240 template <typename T>
241 void
242 gt_pch_nx(function_summary<T *>* const &summary)
244 gcc_checking_assert (summary->m_ggc);
245 gt_pch_nx (&summary->m_map);
248 template <typename T>
249 void
250 gt_pch_nx(function_summary<T *>* const& summary, gt_pointer_operator op,
251 void *cookie)
253 gcc_checking_assert (summary->m_ggc);
254 gt_pch_nx (&summary->m_map, op, cookie);
257 /* An impossible class templated by non-pointers so, which makes sure that only
258 summaries gathering pointers can be created. */
260 template <class T>
261 class call_summary
263 private:
264 call_summary();
267 /* Class to store auxiliary information about call graph edges. */
269 template <class T>
270 class GTY((user)) call_summary <T *>
272 public:
273 /* Default construction takes SYMTAB as an argument. */
274 call_summary (symbol_table *symtab, bool ggc = false): m_ggc (ggc),
275 m_map (13, ggc), m_released (false), m_symtab (symtab)
277 m_symtab_removal_hook =
278 symtab->add_edge_removal_hook
279 (call_summary::symtab_removal, this);
280 m_symtab_duplication_hook =
281 symtab->add_edge_duplication_hook
282 (call_summary::symtab_duplication, this);
285 /* Destructor. */
286 virtual ~call_summary ()
288 release ();
291 /* Destruction method that can be called for GGT purpose. */
292 void release ()
294 if (m_released)
295 return;
297 m_symtab->remove_edge_removal_hook (m_symtab_removal_hook);
298 m_symtab->remove_edge_duplication_hook (m_symtab_duplication_hook);
300 /* Release all summaries. */
301 typedef typename hash_map <map_hash, T *>::iterator map_iterator;
302 for (map_iterator it = m_map.begin (); it != m_map.end (); ++it)
303 release ((*it).second);
305 m_released = true;
308 /* Traverses all summarys with a function F called with
309 ARG as argument. */
310 template<typename Arg, bool (*f)(const T &, Arg)>
311 void traverse (Arg a) const
313 m_map.traverse <f> (a);
316 /* Basic implementation of removal operation. */
317 virtual void remove (cgraph_edge *, T *) {}
319 /* Basic implementation of duplication operation. */
320 virtual void duplicate (cgraph_edge *, cgraph_edge *, T *, T *) {}
322 /* Allocates new data that are stored within map. */
323 T* allocate_new ()
325 /* Call gcc_internal_because we do not want to call finalizer for
326 a type T. We call dtor explicitly. */
327 return m_ggc ? new (ggc_internal_alloc (sizeof (T))) T () : new T () ;
330 /* Release an item that is stored within map. */
331 void release (T *item)
333 if (m_ggc)
335 item->~T ();
336 ggc_free (item);
338 else
339 delete item;
342 /* Getter for summary callgraph edge pointer. */
343 T* get (cgraph_edge *edge)
345 return get (hashable_uid (edge));
348 /* Return number of elements handled by data structure. */
349 size_t elements ()
351 return m_map.elements ();
354 /* Return true if a summary for the given EDGE already exists. */
355 bool exists (cgraph_edge *edge)
357 return m_map.get (hashable_uid (edge)) != NULL;
360 /* Symbol removal hook that is registered to symbol table. */
361 static void symtab_removal (cgraph_edge *edge, void *data)
363 call_summary *summary = (call_summary <T *> *) (data);
365 int h_uid = summary->hashable_uid (edge);
366 T **v = summary->m_map.get (h_uid);
368 if (v)
370 summary->remove (edge, *v);
371 summary->release (*v);
372 summary->m_map.remove (h_uid);
376 /* Symbol duplication hook that is registered to symbol table. */
377 static void symtab_duplication (cgraph_edge *edge1, cgraph_edge *edge2,
378 void *data)
380 call_summary *summary = (call_summary <T *> *) (data);
381 T **v = summary->m_map.get (summary->hashable_uid (edge1));
383 if (v)
385 /* This load is necessary, because we insert a new value! */
386 T *data = *v;
387 T *duplicate = summary->allocate_new ();
388 summary->m_map.put (summary->hashable_uid (edge2), duplicate);
389 summary->duplicate (edge1, edge2, data, duplicate);
393 protected:
394 /* Indication if we use ggc summary. */
395 bool m_ggc;
397 private:
398 typedef int_hash <int, 0, -1> map_hash;
400 /* Getter for summary callgraph ID. */
401 T* get (int uid)
403 bool existed;
404 T **v = &m_map.get_or_insert (uid, &existed);
405 if (!existed)
406 *v = allocate_new ();
408 return *v;
411 /* Get a hashable uid of EDGE. */
412 int hashable_uid (cgraph_edge *edge)
414 /* Edge uids start at zero which our hash_map does not like. */
415 return edge->uid + 1;
418 /* Main summary store, where summary ID is used as key. */
419 hash_map <map_hash, T *> m_map;
420 /* Internal summary removal hook pointer. */
421 cgraph_edge_hook_list *m_symtab_removal_hook;
422 /* Internal summary duplication hook pointer. */
423 cgraph_2edge_hook_list *m_symtab_duplication_hook;
424 /* Indicates if the summary is released. */
425 bool m_released;
426 /* Symbol table the summary is registered to. */
427 symbol_table *m_symtab;
429 template <typename U> friend void gt_ggc_mx (call_summary <U *> * const &);
430 template <typename U> friend void gt_pch_nx (call_summary <U *> * const &);
431 template <typename U> friend void gt_pch_nx (call_summary <U *> * const &,
432 gt_pointer_operator, void *);
435 template <typename T>
436 void
437 gt_ggc_mx(call_summary<T *>* const &summary)
439 gcc_checking_assert (summary->m_ggc);
440 gt_ggc_mx (&summary->m_map);
443 template <typename T>
444 void
445 gt_pch_nx(call_summary<T *>* const &summary)
447 gcc_checking_assert (summary->m_ggc);
448 gt_pch_nx (&summary->m_map);
451 template <typename T>
452 void
453 gt_pch_nx(call_summary<T *>* const& summary, gt_pointer_operator op,
454 void *cookie)
456 gcc_checking_assert (summary->m_ggc);
457 gt_pch_nx (&summary->m_map, op, cookie);
460 #endif /* GCC_SYMBOL_SUMMARY_H */