1 /* Callgraph summary data structure.
2 Copyright (C) 2014-2018 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
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
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
28 class function_summary
35 class GTY((user
)) function_summary
<T
*>
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
),
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);
56 virtual ~function_summary ()
61 /* Destruction method that can be called for GGT purpose. */
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
);
79 /* Traverses all summarys with a function F called with
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. */
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
)
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. */
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
);
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
,
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);
185 /* This load is necessary, because we insert a new value! */
187 T
*duplicate
= summary
->allocate_new ();
188 summary
->m_map
.put (node2
->summary_uid
, duplicate
);
189 summary
->duplicate (node
, node2
, data
, duplicate
);
194 /* Indication if we use ggc summary. */
198 typedef int_hash
<int, 0, -1> map_hash
;
200 /* Getter for summary callgraph ID. */
204 T
**v
= &m_map
.get_or_insert (uid
, &existed
);
206 *v
= allocate_new ();
211 /* Indicates if insertion hook is enabled. */
212 bool m_insertion_enabled
;
213 /* Indicates if the summary is 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
>
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
>
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
>
250 gt_pch_nx(function_summary
<T
*>* const& summary
, gt_pointer_operator op
,
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. */
267 /* Class to store auxiliary information about call graph edges. */
270 class GTY((user
)) call_summary
<T
*>
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);
286 virtual ~call_summary ()
291 /* Destruction method that can be called for GGT purpose. */
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
);
308 /* Traverses all summarys with a function F called with
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. */
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
)
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. */
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
);
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
,
380 call_summary
*summary
= (call_summary
<T
*> *) (data
);
381 T
**v
= summary
->m_map
.get (summary
->hashable_uid (edge1
));
385 /* This load is necessary, because we insert a new value! */
387 T
*duplicate
= summary
->allocate_new ();
388 summary
->m_map
.put (summary
->hashable_uid (edge2
), duplicate
);
389 summary
->duplicate (edge1
, edge2
, data
, duplicate
);
394 /* Indication if we use ggc summary. */
398 typedef int_hash
<int, 0, -1> map_hash
;
400 /* Getter for summary callgraph ID. */
404 T
**v
= &m_map
.get_or_insert (uid
, &existed
);
406 *v
= allocate_new ();
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. */
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
>
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
>
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
>
453 gt_pch_nx(call_summary
<T
*>* const& summary
, gt_pointer_operator op
,
456 gcc_checking_assert (summary
->m_ggc
);
457 gt_pch_nx (&summary
->m_map
, op
, cookie
);
460 #endif /* GCC_SYMBOL_SUMMARY_H */