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
34 /* Function summary is a helper class that is used to associate a data structure
35 related to a callgraph node. Typical usage can be seen in IPA passes which
36 create a temporary pass-related structures. The summary class registers
37 hooks that are triggered when a new node is inserted, duplicated and deleted.
38 A user of a summary class can ovewrite virtual methods than are triggered by
39 the summary if such hook is triggered. Apart from a callgraph node, the user
40 is given a data structure tied to the node.
42 The function summary class can work both with a heap-allocated memory and
43 a memory gained by garbage collected memory. */
46 class GTY((user
)) function_summary
<T
*>
49 /* Default construction takes SYMTAB as an argument. */
50 function_summary (symbol_table
*symtab
, bool ggc
= false);
53 virtual ~function_summary ()
58 /* Destruction method that can be called for GGT purpose. */
61 /* Traverses all summarys with a function F called with
63 template<typename Arg
, bool (*f
)(const T
&, Arg
)>
64 void traverse (Arg a
) const
66 m_map
.traverse
<f
> (a
);
69 /* Basic implementation of insert operation. */
70 virtual void insert (cgraph_node
*, T
*) {}
72 /* Basic implementation of removal operation. */
73 virtual void remove (cgraph_node
*, T
*) {}
75 /* Basic implementation of duplication operation. */
76 virtual void duplicate (cgraph_node
*, cgraph_node
*, T
*, T
*) {}
78 /* Allocates new data that are stored within map. */
81 /* Call gcc_internal_because we do not want to call finalizer for
82 a type T. We call dtor explicitly. */
83 return m_ggc
? new (ggc_internal_alloc (sizeof (T
))) T () : new T () ;
86 /* Release an item that is stored within map. */
87 void release (T
*item
);
89 /* Getter for summary callgraph node pointer. If a summary for a node
90 does not exist it will be created. */
91 T
* get_create (cgraph_node
*node
)
93 return get (node
->get_uid (), true);
96 /* Getter for summary callgraph node pointer. */
97 T
* get (cgraph_node
*node
)
99 return get (node
->get_uid (), false);
102 /* Remove node from summary. */
103 void remove (cgraph_node
*node
)
105 int uid
= node
->get_uid ();
106 T
**v
= m_map
.get (uid
);
114 /* Return number of elements handled by data structure. */
117 return m_map
.elements ();
120 /* Return true if a summary for the given NODE already exists. */
121 bool exists (cgraph_node
*node
)
123 return m_map
.get (node
->get_uid ()) != NULL
;
126 /* Enable insertion hook invocation. */
127 void enable_insertion_hook ()
129 m_insertion_enabled
= true;
132 /* Enable insertion hook invocation. */
133 void disable_insertion_hook ()
135 m_insertion_enabled
= false;
138 /* Symbol insertion hook that is registered to symbol table. */
139 static void symtab_insertion (cgraph_node
*node
, void *data
);
141 /* Symbol removal hook that is registered to symbol table. */
142 static void symtab_removal (cgraph_node
*node
, void *data
);
144 /* Symbol duplication hook that is registered to symbol table. */
145 static void symtab_duplication (cgraph_node
*node
, cgraph_node
*node2
,
149 /* Indication if we use ggc summary. */
153 typedef int_hash
<int, 0, -1> map_hash
;
155 /* Getter for summary callgraph ID. */
156 T
*get (int uid
, bool lazy_insert
);
158 /* Indicates if insertion hook is enabled. */
159 bool m_insertion_enabled
;
160 /* Indicates if the summary is released. */
162 /* Main summary store, where summary ID is used as key. */
163 hash_map
<map_hash
, T
*> m_map
;
164 /* Internal summary insertion hook pointer. */
165 cgraph_node_hook_list
*m_symtab_insertion_hook
;
166 /* Internal summary removal hook pointer. */
167 cgraph_node_hook_list
*m_symtab_removal_hook
;
168 /* Internal summary duplication hook pointer. */
169 cgraph_2node_hook_list
*m_symtab_duplication_hook
;
170 /* Symbol table the summary is registered to. */
171 symbol_table
*m_symtab
;
173 template <typename U
> friend void gt_ggc_mx (function_summary
<U
*> * const &);
174 template <typename U
> friend void gt_pch_nx (function_summary
<U
*> * const &);
175 template <typename U
> friend void gt_pch_nx (function_summary
<U
*> * const &,
176 gt_pointer_operator
, void *);
179 template <typename T
>
180 function_summary
<T
*>::function_summary (symbol_table
*symtab
, bool ggc
):
181 m_ggc (ggc
), m_insertion_enabled (true), m_released (false), m_map (13, ggc
),
184 m_symtab_insertion_hook
185 = symtab
->add_cgraph_insertion_hook (function_summary::symtab_insertion
,
188 m_symtab_removal_hook
189 = symtab
->add_cgraph_removal_hook (function_summary::symtab_removal
, this);
190 m_symtab_duplication_hook
191 = symtab
->add_cgraph_duplication_hook (function_summary::symtab_duplication
,
195 template <typename T
>
197 function_summary
<T
*>::release ()
202 m_symtab
->remove_cgraph_insertion_hook (m_symtab_insertion_hook
);
203 m_symtab
->remove_cgraph_removal_hook (m_symtab_removal_hook
);
204 m_symtab
->remove_cgraph_duplication_hook (m_symtab_duplication_hook
);
206 /* Release all summaries. */
207 typedef typename hash_map
<map_hash
, T
*>::iterator map_iterator
;
208 for (map_iterator it
= m_map
.begin (); it
!= m_map
.end (); ++it
)
209 release ((*it
).second
);
214 template <typename T
>
216 function_summary
<T
*>::release (T
*item
)
227 template <typename T
>
229 function_summary
<T
*>::symtab_insertion (cgraph_node
*node
, void *data
)
231 gcc_checking_assert (node
->get_uid ());
232 function_summary
*summary
= (function_summary
<T
*> *) (data
);
234 if (summary
->m_insertion_enabled
)
235 summary
->insert (node
, summary
->get_create (node
));
238 template <typename T
>
240 function_summary
<T
*>::symtab_removal (cgraph_node
*node
, void *data
)
242 gcc_checking_assert (node
->get_uid ());
243 function_summary
*summary
= (function_summary
<T
*> *) (data
);
245 int uid
= node
->get_uid ();
246 T
**v
= summary
->m_map
.get (uid
);
250 summary
->remove (node
, *v
);
255 summary
->m_map
.remove (uid
);
259 template <typename T
>
261 function_summary
<T
*>::symtab_duplication (cgraph_node
*node
,
262 cgraph_node
*node2
, void *data
)
264 function_summary
*summary
= (function_summary
<T
*> *) (data
);
265 T
*v
= summary
->get (node
);
269 /* This load is necessary, because we insert a new value! */
270 T
*duplicate
= summary
->allocate_new ();
271 summary
->m_map
.put (node2
->get_uid (), duplicate
);
272 summary
->duplicate (node
, node2
, v
, duplicate
);
276 template <typename T
>
278 function_summary
<T
*>::get (int uid
, bool lazy_insert
)
280 gcc_checking_assert (uid
> 0);
285 T
**v
= &m_map
.get_or_insert (uid
, &existed
);
287 *v
= allocate_new ();
293 T
**v
= m_map
.get (uid
);
294 return v
== NULL
? NULL
: *v
;
298 template <typename T
>
300 gt_ggc_mx(function_summary
<T
*>* const &summary
)
302 gcc_checking_assert (summary
->m_ggc
);
303 gt_ggc_mx (&summary
->m_map
);
306 template <typename T
>
308 gt_pch_nx(function_summary
<T
*>* const &summary
)
310 gcc_checking_assert (summary
->m_ggc
);
311 gt_pch_nx (&summary
->m_map
);
314 template <typename T
>
316 gt_pch_nx(function_summary
<T
*>* const& summary
, gt_pointer_operator op
,
319 gcc_checking_assert (summary
->m_ggc
);
320 gt_pch_nx (&summary
->m_map
, op
, cookie
);
323 /* An impossible class templated by non-pointers so, which makes sure that only
324 summaries gathering pointers can be created. */
333 /* Class to store auxiliary information about call graph edges. */
336 class GTY((user
)) call_summary
<T
*>
339 /* Default construction takes SYMTAB as an argument. */
340 call_summary (symbol_table
*symtab
, bool ggc
= false): m_ggc (ggc
),
341 m_initialize_when_cloning (false), m_map (13, ggc
), m_released (false),
344 m_symtab_removal_hook
=
345 symtab
->add_edge_removal_hook
346 (call_summary::symtab_removal
, this);
347 m_symtab_duplication_hook
=
348 symtab
->add_edge_duplication_hook
349 (call_summary::symtab_duplication
, this);
353 virtual ~call_summary ()
358 /* Destruction method that can be called for GGT purpose. */
361 /* Traverses all summarys with a function F called with
363 template<typename Arg
, bool (*f
)(const T
&, Arg
)>
364 void traverse (Arg a
) const
366 m_map
.traverse
<f
> (a
);
369 /* Basic implementation of removal operation. */
370 virtual void remove (cgraph_edge
*, T
*) {}
372 /* Basic implementation of duplication operation. */
373 virtual void duplicate (cgraph_edge
*, cgraph_edge
*, T
*, T
*) {}
375 /* Allocates new data that are stored within map. */
378 /* Call gcc_internal_because we do not want to call finalizer for
379 a type T. We call dtor explicitly. */
380 return m_ggc
? new (ggc_internal_alloc (sizeof (T
))) T () : new T () ;
383 /* Release an item that is stored within map. */
384 void release (T
*item
);
386 /* Getter for summary callgraph edge pointer.
387 If a summary for an edge does not exist, it will be created. */
388 T
* get_create (cgraph_edge
*edge
)
390 return get (edge
->get_uid (), true);
393 /* Getter for summary callgraph edge pointer. */
394 T
* get (cgraph_edge
*edge
)
396 return get (edge
->get_uid (), false);
399 /* Remove edge from summary. */
400 void remove (cgraph_edge
*edge
)
402 int uid
= edge
->get_uid ();
403 T
**v
= m_map
.get (uid
);
411 /* Return number of elements handled by data structure. */
414 return m_map
.elements ();
417 /* Return true if a summary for the given EDGE already exists. */
418 bool exists (cgraph_edge
*edge
)
420 return m_map
.get (edge
->get_uid ()) != NULL
;
423 /* Symbol removal hook that is registered to symbol table. */
424 static void symtab_removal (cgraph_edge
*edge
, void *data
);
426 /* Symbol duplication hook that is registered to symbol table. */
427 static void symtab_duplication (cgraph_edge
*edge1
, cgraph_edge
*edge2
,
431 /* Indication if we use ggc summary. */
434 /* Initialize summary for an edge that is cloned. */
435 bool m_initialize_when_cloning
;
438 typedef int_hash
<int, 0, -1> map_hash
;
440 /* Getter for summary callgraph ID. */
441 T
*get (int uid
, bool lazy_insert
);
443 /* Main summary store, where summary ID is used as key. */
444 hash_map
<map_hash
, T
*> m_map
;
445 /* Internal summary removal hook pointer. */
446 cgraph_edge_hook_list
*m_symtab_removal_hook
;
447 /* Internal summary duplication hook pointer. */
448 cgraph_2edge_hook_list
*m_symtab_duplication_hook
;
449 /* Indicates if the summary is released. */
451 /* Symbol table the summary is registered to. */
452 symbol_table
*m_symtab
;
454 template <typename U
> friend void gt_ggc_mx (call_summary
<U
*> * const &);
455 template <typename U
> friend void gt_pch_nx (call_summary
<U
*> * const &);
456 template <typename U
> friend void gt_pch_nx (call_summary
<U
*> * const &,
457 gt_pointer_operator
, void *);
460 template <typename T
>
462 call_summary
<T
*>::get (int uid
, bool lazy_insert
)
464 gcc_checking_assert (uid
> 0);
469 T
**v
= &m_map
.get_or_insert (uid
, &existed
);
471 *v
= allocate_new ();
477 T
**v
= m_map
.get (uid
);
478 return v
== NULL
? NULL
: *v
;
482 template <typename T
>
484 call_summary
<T
*>::release ()
489 m_symtab
->remove_edge_removal_hook (m_symtab_removal_hook
);
490 m_symtab
->remove_edge_duplication_hook (m_symtab_duplication_hook
);
492 /* Release all summaries. */
493 typedef typename hash_map
<map_hash
, T
*>::iterator map_iterator
;
494 for (map_iterator it
= m_map
.begin (); it
!= m_map
.end (); ++it
)
495 release ((*it
).second
);
500 template <typename T
>
502 call_summary
<T
*>::release (T
*item
)
513 template <typename T
>
515 call_summary
<T
*>::symtab_removal (cgraph_edge
*edge
, void *data
)
517 call_summary
*summary
= (call_summary
<T
*> *) (data
);
519 int h_uid
= edge
->get_uid ();
520 T
**v
= summary
->m_map
.get (h_uid
);
524 summary
->remove (edge
, *v
);
525 summary
->release (*v
);
526 summary
->m_map
.remove (h_uid
);
530 template <typename T
>
532 call_summary
<T
*>::symtab_duplication (cgraph_edge
*edge1
,
533 cgraph_edge
*edge2
, void *data
)
535 call_summary
*summary
= (call_summary
<T
*> *) (data
);
536 T
*edge1_summary
= NULL
;
538 if (summary
->m_initialize_when_cloning
)
539 edge1_summary
= summary
->get_create (edge1
);
542 T
**v
= summary
->m_map
.get (edge1
->get_uid ());
545 /* This load is necessary, because we insert a new value! */
552 T
*duplicate
= summary
->allocate_new ();
553 summary
->m_map
.put (edge2
->get_uid (), duplicate
);
554 summary
->duplicate (edge1
, edge2
, edge1_summary
, duplicate
);
558 template <typename T
>
560 gt_ggc_mx(call_summary
<T
*>* const &summary
)
562 gcc_checking_assert (summary
->m_ggc
);
563 gt_ggc_mx (&summary
->m_map
);
566 template <typename T
>
568 gt_pch_nx(call_summary
<T
*>* const &summary
)
570 gcc_checking_assert (summary
->m_ggc
);
571 gt_pch_nx (&summary
->m_map
);
574 template <typename T
>
576 gt_pch_nx(call_summary
<T
*>* const& summary
, gt_pointer_operator op
,
579 gcc_checking_assert (summary
->m_ggc
);
580 gt_pch_nx (&summary
->m_map
, op
, cookie
);
583 #endif /* GCC_SYMBOL_SUMMARY_H */