1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
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 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
28 #include "coretypes.h"
31 #include "tree-inline.h"
32 #include "langhooks.h"
39 #include "basic-block.h"
43 #include "gimple-iterator.h"
46 #include "gimple-ssa.h"
50 #include "value-prof.h"
52 #include "diagnostic-core.h"
54 #include "ipa-utils.h"
55 #include "lto-streamer.h"
56 #include "ipa-inline.h"
58 #include "gimple-pretty-print.h"
60 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
61 #include "tree-pass.h"
63 static void cgraph_node_remove_callers (struct cgraph_node
*node
);
64 static inline void cgraph_edge_remove_caller (struct cgraph_edge
*e
);
65 static inline void cgraph_edge_remove_callee (struct cgraph_edge
*e
);
67 /* Queue of cgraph nodes scheduled to be lowered. */
68 symtab_node
*x_cgraph_nodes_queue
;
69 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
71 /* Number of nodes in existence. */
74 /* Maximal uid used in cgraph nodes. */
77 /* Maximal uid used in cgraph edges. */
78 int cgraph_edge_max_uid
;
80 /* Set when whole unit has been analyzed so we can access global info. */
81 bool cgraph_global_info_ready
= false;
83 /* What state callgraph is in right now. */
84 enum cgraph_state cgraph_state
= CGRAPH_STATE_PARSING
;
86 /* Set when the cgraph is fully build and the basic flags are computed. */
87 bool cgraph_function_flags_ready
= false;
89 /* List of hooks triggered on cgraph_edge events. */
90 struct cgraph_edge_hook_list
{
91 cgraph_edge_hook hook
;
93 struct cgraph_edge_hook_list
*next
;
96 /* List of hooks triggered on cgraph_node events. */
97 struct cgraph_node_hook_list
{
98 cgraph_node_hook hook
;
100 struct cgraph_node_hook_list
*next
;
103 /* List of hooks triggered on events involving two cgraph_edges. */
104 struct cgraph_2edge_hook_list
{
105 cgraph_2edge_hook hook
;
107 struct cgraph_2edge_hook_list
*next
;
110 /* List of hooks triggered on events involving two cgraph_nodes. */
111 struct cgraph_2node_hook_list
{
112 cgraph_2node_hook hook
;
114 struct cgraph_2node_hook_list
*next
;
117 /* List of hooks triggered when an edge is removed. */
118 struct cgraph_edge_hook_list
*first_cgraph_edge_removal_hook
;
119 /* List of hooks triggered when a node is removed. */
120 struct cgraph_node_hook_list
*first_cgraph_node_removal_hook
;
121 /* List of hooks triggered when an edge is duplicated. */
122 struct cgraph_2edge_hook_list
*first_cgraph_edge_duplicated_hook
;
123 /* List of hooks triggered when a node is duplicated. */
124 struct cgraph_2node_hook_list
*first_cgraph_node_duplicated_hook
;
125 /* List of hooks triggered when an function is inserted. */
126 struct cgraph_node_hook_list
*first_cgraph_function_insertion_hook
;
128 /* Head of a linked list of unused (freed) call graph nodes.
129 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
130 static GTY(()) struct cgraph_node
*free_nodes
;
131 /* Head of a linked list of unused (freed) call graph edges.
132 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
133 static GTY(()) struct cgraph_edge
*free_edges
;
135 /* Did procss_same_body_aliases run? */
136 bool cpp_implicit_aliases_done
;
138 /* Map a cgraph_node to cgraph_function_version_info using this htab.
139 The cgraph_function_version_info has a THIS_NODE field that is the
140 corresponding cgraph_node.. */
142 static GTY((param_is (struct cgraph_function_version_info
))) htab_t
143 cgraph_fnver_htab
= NULL
;
145 /* Hash function for cgraph_fnver_htab. */
147 cgraph_fnver_htab_hash (const void *ptr
)
149 int uid
= ((const struct cgraph_function_version_info
*)ptr
)->this_node
->uid
;
150 return (hashval_t
)(uid
);
153 /* eq function for cgraph_fnver_htab. */
155 cgraph_fnver_htab_eq (const void *p1
, const void *p2
)
157 const struct cgraph_function_version_info
*n1
158 = (const struct cgraph_function_version_info
*)p1
;
159 const struct cgraph_function_version_info
*n2
160 = (const struct cgraph_function_version_info
*)p2
;
162 return n1
->this_node
->uid
== n2
->this_node
->uid
;
165 /* Mark as GC root all allocated nodes. */
166 static GTY(()) struct cgraph_function_version_info
*
167 version_info_node
= NULL
;
169 /* Get the cgraph_function_version_info node corresponding to node. */
170 struct cgraph_function_version_info
*
171 get_cgraph_node_version (struct cgraph_node
*node
)
173 struct cgraph_function_version_info
*ret
;
174 struct cgraph_function_version_info key
;
175 key
.this_node
= node
;
177 if (cgraph_fnver_htab
== NULL
)
180 ret
= (struct cgraph_function_version_info
*)
181 htab_find (cgraph_fnver_htab
, &key
);
186 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
187 corresponding to cgraph_node NODE. */
188 struct cgraph_function_version_info
*
189 insert_new_cgraph_node_version (struct cgraph_node
*node
)
193 version_info_node
= NULL
;
194 version_info_node
= ggc_alloc_cleared_cgraph_function_version_info ();
195 version_info_node
->this_node
= node
;
197 if (cgraph_fnver_htab
== NULL
)
198 cgraph_fnver_htab
= htab_create_ggc (2, cgraph_fnver_htab_hash
,
199 cgraph_fnver_htab_eq
, NULL
);
201 slot
= htab_find_slot (cgraph_fnver_htab
, version_info_node
, INSERT
);
202 gcc_assert (slot
!= NULL
);
203 *slot
= version_info_node
;
204 return version_info_node
;
207 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
208 DECL is a duplicate declaration. */
210 delete_function_version (tree decl
)
212 struct cgraph_node
*decl_node
= cgraph_get_node (decl
);
213 struct cgraph_function_version_info
*decl_v
= NULL
;
215 if (decl_node
== NULL
)
218 decl_v
= get_cgraph_node_version (decl_node
);
223 if (decl_v
->prev
!= NULL
)
224 decl_v
->prev
->next
= decl_v
->next
;
226 if (decl_v
->next
!= NULL
)
227 decl_v
->next
->prev
= decl_v
->prev
;
229 if (cgraph_fnver_htab
!= NULL
)
230 htab_remove_elt (cgraph_fnver_htab
, decl_v
);
232 cgraph_remove_node (decl_node
);
235 /* Record that DECL1 and DECL2 are semantically identical function
238 record_function_versions (tree decl1
, tree decl2
)
240 struct cgraph_node
*decl1_node
= cgraph_get_create_node (decl1
);
241 struct cgraph_node
*decl2_node
= cgraph_get_create_node (decl2
);
242 struct cgraph_function_version_info
*decl1_v
= NULL
;
243 struct cgraph_function_version_info
*decl2_v
= NULL
;
244 struct cgraph_function_version_info
*before
;
245 struct cgraph_function_version_info
*after
;
247 gcc_assert (decl1_node
!= NULL
&& decl2_node
!= NULL
);
248 decl1_v
= get_cgraph_node_version (decl1_node
);
249 decl2_v
= get_cgraph_node_version (decl2_node
);
251 if (decl1_v
!= NULL
&& decl2_v
!= NULL
)
255 decl1_v
= insert_new_cgraph_node_version (decl1_node
);
258 decl2_v
= insert_new_cgraph_node_version (decl2_node
);
260 /* Chain decl2_v and decl1_v. All semantically identical versions
261 will be chained together. */
266 while (before
->next
!= NULL
)
267 before
= before
->next
;
269 while (after
->prev
!= NULL
)
272 before
->next
= after
;
273 after
->prev
= before
;
276 /* Macros to access the next item in the list of free cgraph nodes and
278 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->next)
279 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
280 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
282 /* Register HOOK to be called with DATA on each removed edge. */
283 struct cgraph_edge_hook_list
*
284 cgraph_add_edge_removal_hook (cgraph_edge_hook hook
, void *data
)
286 struct cgraph_edge_hook_list
*entry
;
287 struct cgraph_edge_hook_list
**ptr
= &first_cgraph_edge_removal_hook
;
289 entry
= (struct cgraph_edge_hook_list
*) xmalloc (sizeof (*entry
));
299 /* Remove ENTRY from the list of hooks called on removing edges. */
301 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list
*entry
)
303 struct cgraph_edge_hook_list
**ptr
= &first_cgraph_edge_removal_hook
;
305 while (*ptr
!= entry
)
311 /* Call all edge removal hooks. */
313 cgraph_call_edge_removal_hooks (struct cgraph_edge
*e
)
315 struct cgraph_edge_hook_list
*entry
= first_cgraph_edge_removal_hook
;
318 entry
->hook (e
, entry
->data
);
323 /* Register HOOK to be called with DATA on each removed node. */
324 struct cgraph_node_hook_list
*
325 cgraph_add_node_removal_hook (cgraph_node_hook hook
, void *data
)
327 struct cgraph_node_hook_list
*entry
;
328 struct cgraph_node_hook_list
**ptr
= &first_cgraph_node_removal_hook
;
330 entry
= (struct cgraph_node_hook_list
*) xmalloc (sizeof (*entry
));
340 /* Remove ENTRY from the list of hooks called on removing nodes. */
342 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list
*entry
)
344 struct cgraph_node_hook_list
**ptr
= &first_cgraph_node_removal_hook
;
346 while (*ptr
!= entry
)
352 /* Call all node removal hooks. */
354 cgraph_call_node_removal_hooks (struct cgraph_node
*node
)
356 struct cgraph_node_hook_list
*entry
= first_cgraph_node_removal_hook
;
359 entry
->hook (node
, entry
->data
);
364 /* Register HOOK to be called with DATA on each inserted node. */
365 struct cgraph_node_hook_list
*
366 cgraph_add_function_insertion_hook (cgraph_node_hook hook
, void *data
)
368 struct cgraph_node_hook_list
*entry
;
369 struct cgraph_node_hook_list
**ptr
= &first_cgraph_function_insertion_hook
;
371 entry
= (struct cgraph_node_hook_list
*) xmalloc (sizeof (*entry
));
381 /* Remove ENTRY from the list of hooks called on inserted nodes. */
383 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list
*entry
)
385 struct cgraph_node_hook_list
**ptr
= &first_cgraph_function_insertion_hook
;
387 while (*ptr
!= entry
)
393 /* Call all node insertion hooks. */
395 cgraph_call_function_insertion_hooks (struct cgraph_node
*node
)
397 struct cgraph_node_hook_list
*entry
= first_cgraph_function_insertion_hook
;
400 entry
->hook (node
, entry
->data
);
405 /* Register HOOK to be called with DATA on each duplicated edge. */
406 struct cgraph_2edge_hook_list
*
407 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook
, void *data
)
409 struct cgraph_2edge_hook_list
*entry
;
410 struct cgraph_2edge_hook_list
**ptr
= &first_cgraph_edge_duplicated_hook
;
412 entry
= (struct cgraph_2edge_hook_list
*) xmalloc (sizeof (*entry
));
422 /* Remove ENTRY from the list of hooks called on duplicating edges. */
424 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list
*entry
)
426 struct cgraph_2edge_hook_list
**ptr
= &first_cgraph_edge_duplicated_hook
;
428 while (*ptr
!= entry
)
434 /* Call all edge duplication hooks. */
436 cgraph_call_edge_duplication_hooks (struct cgraph_edge
*cs1
,
437 struct cgraph_edge
*cs2
)
439 struct cgraph_2edge_hook_list
*entry
= first_cgraph_edge_duplicated_hook
;
442 entry
->hook (cs1
, cs2
, entry
->data
);
447 /* Register HOOK to be called with DATA on each duplicated node. */
448 struct cgraph_2node_hook_list
*
449 cgraph_add_node_duplication_hook (cgraph_2node_hook hook
, void *data
)
451 struct cgraph_2node_hook_list
*entry
;
452 struct cgraph_2node_hook_list
**ptr
= &first_cgraph_node_duplicated_hook
;
454 entry
= (struct cgraph_2node_hook_list
*) xmalloc (sizeof (*entry
));
464 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
466 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list
*entry
)
468 struct cgraph_2node_hook_list
**ptr
= &first_cgraph_node_duplicated_hook
;
470 while (*ptr
!= entry
)
476 /* Call all node duplication hooks. */
478 cgraph_call_node_duplication_hooks (struct cgraph_node
*node1
,
479 struct cgraph_node
*node2
)
481 struct cgraph_2node_hook_list
*entry
= first_cgraph_node_duplicated_hook
;
484 entry
->hook (node1
, node2
, entry
->data
);
489 /* Allocate new callgraph node. */
491 static inline struct cgraph_node
*
492 cgraph_allocate_node (void)
494 struct cgraph_node
*node
;
499 free_nodes
= NEXT_FREE_NODE (node
);
503 node
= ggc_alloc_cleared_cgraph_node ();
504 node
->uid
= cgraph_max_uid
++;
510 /* Allocate new callgraph node and insert it into basic data structures. */
513 cgraph_create_empty_node (void)
515 struct cgraph_node
*node
= cgraph_allocate_node ();
517 node
->type
= SYMTAB_FUNCTION
;
518 node
->frequency
= NODE_FREQUENCY_NORMAL
;
519 node
->count_materialization_scale
= REG_BR_PROB_BASE
;
524 /* Return cgraph node assigned to DECL. Create new one when needed. */
527 cgraph_create_node (tree decl
)
529 struct cgraph_node
*node
= cgraph_create_empty_node ();
530 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
533 symtab_register_node (node
);
535 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
537 node
->origin
= cgraph_get_create_node (DECL_CONTEXT (decl
));
538 node
->next_nested
= node
->origin
->nested
;
539 node
->origin
->nested
= node
;
544 /* Try to find a call graph node for declaration DECL and if it does not exist
545 or if it corresponds to an inline clone, create a new one. */
548 cgraph_get_create_node (tree decl
)
550 struct cgraph_node
*first_clone
= cgraph_get_node (decl
);
552 if (first_clone
&& !first_clone
->global
.inlined_to
)
555 struct cgraph_node
*node
= cgraph_create_node (decl
);
558 first_clone
->clone_of
= node
;
559 node
->clones
= first_clone
;
560 symtab_prevail_in_asm_name_hash (node
);
561 symtab_insert_node_to_hashtable (node
);
563 fprintf (dump_file
, "Introduced new external node "
564 "(%s/%i) and turned into root of the clone tree.\n",
565 xstrdup (node
->name ()), node
->order
);
568 fprintf (dump_file
, "Introduced new external node "
569 "(%s/%i).\n", xstrdup (node
->name ()),
574 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
575 the function body is associated with (not necessarily cgraph_node (DECL). */
578 cgraph_create_function_alias (tree alias
, tree target
)
580 struct cgraph_node
*alias_node
;
582 gcc_assert (TREE_CODE (target
) == FUNCTION_DECL
583 || TREE_CODE (target
) == IDENTIFIER_NODE
);
584 gcc_assert (TREE_CODE (alias
) == FUNCTION_DECL
);
585 alias_node
= cgraph_get_create_node (alias
);
586 gcc_assert (!alias_node
->definition
);
587 alias_node
->alias_target
= target
;
588 alias_node
->definition
= true;
589 alias_node
->alias
= true;
590 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
591 alias_node
->weakref
= true;
595 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
597 Same body aliases are output whenever the body of DECL is output,
598 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
601 cgraph_same_body_alias (struct cgraph_node
*decl_node ATTRIBUTE_UNUSED
, tree alias
, tree decl
)
603 struct cgraph_node
*n
;
604 #ifndef ASM_OUTPUT_DEF
605 /* If aliases aren't supported by the assembler, fail. */
608 /* Langhooks can create same body aliases of symbols not defined.
609 Those are useless. Drop them on the floor. */
610 if (cgraph_global_info_ready
)
613 n
= cgraph_create_function_alias (alias
, decl
);
614 n
->cpp_implicit_alias
= true;
615 if (cpp_implicit_aliases_done
)
616 symtab_resolve_alias (n
,
617 cgraph_get_node (decl
));
621 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
622 aliases DECL with an adjustments made into the first parameter.
623 See comments in thunk_adjust for detail on the parameters. */
626 cgraph_add_thunk (struct cgraph_node
*decl_node ATTRIBUTE_UNUSED
,
627 tree alias
, tree decl ATTRIBUTE_UNUSED
,
629 HOST_WIDE_INT fixed_offset
, HOST_WIDE_INT virtual_value
,
633 struct cgraph_node
*node
;
635 node
= cgraph_get_node (alias
);
638 gcc_assert (node
->definition
);
639 gcc_assert (!node
->alias
);
640 gcc_assert (!node
->thunk
.thunk_p
);
641 cgraph_remove_node (node
);
644 node
= cgraph_create_node (alias
);
645 gcc_checking_assert (!virtual_offset
646 || tree_to_double_int (virtual_offset
) ==
647 double_int::from_shwi (virtual_value
));
648 node
->thunk
.fixed_offset
= fixed_offset
;
649 node
->thunk
.this_adjusting
= this_adjusting
;
650 node
->thunk
.virtual_value
= virtual_value
;
651 node
->thunk
.virtual_offset_p
= virtual_offset
!= NULL
;
652 node
->thunk
.alias
= real_alias
;
653 node
->thunk
.thunk_p
= true;
654 node
->definition
= true;
659 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
660 Return NULL if there's no such node. */
663 cgraph_node_for_asm (tree asmname
)
665 /* We do not want to look at inline clones. */
666 for (symtab_node
*node
= symtab_node_for_asm (asmname
);
668 node
= node
->next_sharing_asm_name
)
670 cgraph_node
*cn
= dyn_cast
<cgraph_node
> (node
);
671 if (cn
&& !cn
->global
.inlined_to
)
677 /* Returns a hash value for X (which really is a cgraph_edge). */
680 edge_hash (const void *x
)
682 return htab_hash_pointer (((const struct cgraph_edge
*) x
)->call_stmt
);
685 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
688 edge_eq (const void *x
, const void *y
)
690 return ((const struct cgraph_edge
*) x
)->call_stmt
== y
;
693 /* Add call graph edge E to call site hash of its caller. */
696 cgraph_update_edge_in_call_site_hash (struct cgraph_edge
*e
)
699 slot
= htab_find_slot_with_hash (e
->caller
->call_site_hash
,
701 htab_hash_pointer (e
->call_stmt
),
706 /* Add call graph edge E to call site hash of its caller. */
709 cgraph_add_edge_to_call_site_hash (struct cgraph_edge
*e
)
712 /* There are two speculative edges for every statement (one direct,
713 one indirect); always hash the direct one. */
714 if (e
->speculative
&& e
->indirect_unknown_callee
)
716 slot
= htab_find_slot_with_hash (e
->caller
->call_site_hash
,
718 htab_hash_pointer (e
->call_stmt
),
722 gcc_assert (((struct cgraph_edge
*)*slot
)->speculative
);
727 gcc_assert (!*slot
|| e
->speculative
);
731 /* Return the callgraph edge representing the GIMPLE_CALL statement
735 cgraph_edge (struct cgraph_node
*node
, gimple call_stmt
)
737 struct cgraph_edge
*e
, *e2
;
740 if (node
->call_site_hash
)
741 return (struct cgraph_edge
*)
742 htab_find_with_hash (node
->call_site_hash
, call_stmt
,
743 htab_hash_pointer (call_stmt
));
745 /* This loop may turn out to be performance problem. In such case adding
746 hashtables into call nodes with very many edges is probably best
747 solution. It is not good idea to add pointer into CALL_EXPR itself
748 because we want to make possible having multiple cgraph nodes representing
749 different clones of the same body before the body is actually cloned. */
750 for (e
= node
->callees
; e
; e
= e
->next_callee
)
752 if (e
->call_stmt
== call_stmt
)
758 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
760 if (e
->call_stmt
== call_stmt
)
767 node
->call_site_hash
= htab_create_ggc (120, edge_hash
, edge_eq
, NULL
);
768 for (e2
= node
->callees
; e2
; e2
= e2
->next_callee
)
769 cgraph_add_edge_to_call_site_hash (e2
);
770 for (e2
= node
->indirect_calls
; e2
; e2
= e2
->next_callee
)
771 cgraph_add_edge_to_call_site_hash (e2
);
778 /* Change field call_stmt of edge E to NEW_STMT.
779 If UPDATE_SPECULATIVE and E is any component of speculative
780 edge, then update all components. */
783 cgraph_set_call_stmt (struct cgraph_edge
*e
, gimple new_stmt
,
784 bool update_speculative
)
788 /* Speculative edges has three component, update all of them
790 if (update_speculative
&& e
->speculative
)
792 struct cgraph_edge
*direct
, *indirect
;
795 cgraph_speculative_call_info (e
, direct
, indirect
, ref
);
796 cgraph_set_call_stmt (direct
, new_stmt
, false);
797 cgraph_set_call_stmt (indirect
, new_stmt
, false);
798 ref
->stmt
= new_stmt
;
802 /* Only direct speculative edges go to call_site_hash. */
803 if (e
->caller
->call_site_hash
804 && (!e
->speculative
|| !e
->indirect_unknown_callee
))
806 htab_remove_elt_with_hash (e
->caller
->call_site_hash
,
808 htab_hash_pointer (e
->call_stmt
));
811 e
->call_stmt
= new_stmt
;
812 if (e
->indirect_unknown_callee
813 && (decl
= gimple_call_fndecl (new_stmt
)))
815 /* Constant propagation (and possibly also inlining?) can turn an
816 indirect call into a direct one. */
817 struct cgraph_node
*new_callee
= cgraph_get_node (decl
);
819 gcc_checking_assert (new_callee
);
820 e
= cgraph_make_edge_direct (e
, new_callee
);
823 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
824 e
->can_throw_external
= stmt_can_throw_external (new_stmt
);
826 if (e
->caller
->call_site_hash
)
827 cgraph_add_edge_to_call_site_hash (e
);
830 /* Allocate a cgraph_edge structure and fill it with data according to the
831 parameters of which only CALLEE can be NULL (when creating an indirect call
834 static struct cgraph_edge
*
835 cgraph_create_edge_1 (struct cgraph_node
*caller
, struct cgraph_node
*callee
,
836 gimple call_stmt
, gcov_type count
, int freq
,
837 bool indir_unknown_callee
)
839 struct cgraph_edge
*edge
;
841 /* LTO does not actually have access to the call_stmt since these
842 have not been loaded yet. */
845 /* This is a rather expensive check possibly triggering
846 construction of call stmt hashtable. */
847 #ifdef ENABLE_CHECKING
848 struct cgraph_edge
*e
;
849 gcc_checking_assert (!(e
=cgraph_edge (caller
, call_stmt
)) || e
->speculative
);
852 gcc_assert (is_gimple_call (call_stmt
));
858 free_edges
= NEXT_FREE_EDGE (edge
);
862 edge
= ggc_alloc_cgraph_edge ();
863 edge
->uid
= cgraph_edge_max_uid
++;
867 edge
->caller
= caller
;
868 edge
->callee
= callee
;
869 edge
->prev_caller
= NULL
;
870 edge
->next_caller
= NULL
;
871 edge
->prev_callee
= NULL
;
872 edge
->next_callee
= NULL
;
873 edge
->lto_stmt_uid
= 0;
876 gcc_assert (count
>= 0);
877 edge
->frequency
= freq
;
878 gcc_assert (freq
>= 0);
879 gcc_assert (freq
<= CGRAPH_FREQ_MAX
);
881 edge
->call_stmt
= call_stmt
;
882 push_cfun (DECL_STRUCT_FUNCTION (caller
->decl
));
883 edge
->can_throw_external
884 = call_stmt
? stmt_can_throw_external (call_stmt
) : false;
887 && callee
&& callee
->decl
888 && !gimple_check_call_matching_types (call_stmt
, callee
->decl
,
890 edge
->call_stmt_cannot_inline_p
= true;
892 edge
->call_stmt_cannot_inline_p
= false;
894 edge
->indirect_info
= NULL
;
895 edge
->indirect_inlining_edge
= 0;
896 edge
->speculative
= false;
897 edge
->indirect_unknown_callee
= indir_unknown_callee
;
898 if (call_stmt
&& caller
->call_site_hash
)
899 cgraph_add_edge_to_call_site_hash (edge
);
904 /* Create edge from CALLER to CALLEE in the cgraph. */
907 cgraph_create_edge (struct cgraph_node
*caller
, struct cgraph_node
*callee
,
908 gimple call_stmt
, gcov_type count
, int freq
)
910 struct cgraph_edge
*edge
= cgraph_create_edge_1 (caller
, callee
, call_stmt
,
913 initialize_inline_failed (edge
);
915 edge
->next_caller
= callee
->callers
;
917 callee
->callers
->prev_caller
= edge
;
918 edge
->next_callee
= caller
->callees
;
920 caller
->callees
->prev_callee
= edge
;
921 caller
->callees
= edge
;
922 callee
->callers
= edge
;
927 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
929 struct cgraph_indirect_call_info
*
930 cgraph_allocate_init_indirect_info (void)
932 struct cgraph_indirect_call_info
*ii
;
934 ii
= ggc_alloc_cleared_cgraph_indirect_call_info ();
935 ii
->param_index
= -1;
939 /* Create an indirect edge with a yet-undetermined callee where the call
940 statement destination is a formal parameter of the caller with index
944 cgraph_create_indirect_edge (struct cgraph_node
*caller
, gimple call_stmt
,
946 gcov_type count
, int freq
)
948 struct cgraph_edge
*edge
= cgraph_create_edge_1 (caller
, NULL
, call_stmt
,
952 initialize_inline_failed (edge
);
954 edge
->indirect_info
= cgraph_allocate_init_indirect_info ();
955 edge
->indirect_info
->ecf_flags
= ecf_flags
;
957 /* Record polymorphic call info. */
959 && (target
= gimple_call_fn (call_stmt
))
960 && virtual_method_call_p (target
))
962 tree type
= obj_type_ref_class (target
);
965 /* Only record types can have virtual calls. */
966 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
967 edge
->indirect_info
->param_index
= -1;
968 edge
->indirect_info
->otr_token
969 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target
));
970 edge
->indirect_info
->otr_type
= type
;
971 edge
->indirect_info
->polymorphic
= 1;
974 edge
->next_callee
= caller
->indirect_calls
;
975 if (caller
->indirect_calls
)
976 caller
->indirect_calls
->prev_callee
= edge
;
977 caller
->indirect_calls
= edge
;
982 /* Remove the edge E from the list of the callers of the callee. */
985 cgraph_edge_remove_callee (struct cgraph_edge
*e
)
987 gcc_assert (!e
->indirect_unknown_callee
);
989 e
->prev_caller
->next_caller
= e
->next_caller
;
991 e
->next_caller
->prev_caller
= e
->prev_caller
;
993 e
->callee
->callers
= e
->next_caller
;
996 /* Remove the edge E from the list of the callees of the caller. */
999 cgraph_edge_remove_caller (struct cgraph_edge
*e
)
1002 e
->prev_callee
->next_callee
= e
->next_callee
;
1004 e
->next_callee
->prev_callee
= e
->prev_callee
;
1005 if (!e
->prev_callee
)
1007 if (e
->indirect_unknown_callee
)
1008 e
->caller
->indirect_calls
= e
->next_callee
;
1010 e
->caller
->callees
= e
->next_callee
;
1012 if (e
->caller
->call_site_hash
)
1013 htab_remove_elt_with_hash (e
->caller
->call_site_hash
,
1015 htab_hash_pointer (e
->call_stmt
));
1018 /* Put the edge onto the free list. */
1021 cgraph_free_edge (struct cgraph_edge
*e
)
1025 if (e
->indirect_info
)
1026 ggc_free (e
->indirect_info
);
1028 /* Clear out the edge so we do not dangle pointers. */
1029 memset (e
, 0, sizeof (*e
));
1031 NEXT_FREE_EDGE (e
) = free_edges
;
1035 /* Remove the edge E in the cgraph. */
1038 cgraph_remove_edge (struct cgraph_edge
*e
)
1040 /* Call all edge removal hooks. */
1041 cgraph_call_edge_removal_hooks (e
);
1043 if (!e
->indirect_unknown_callee
)
1044 /* Remove from callers list of the callee. */
1045 cgraph_edge_remove_callee (e
);
1047 /* Remove from callees list of the callers. */
1048 cgraph_edge_remove_caller (e
);
1050 /* Put the edge onto the free list. */
1051 cgraph_free_edge (e
);
1054 /* Set callee of call graph edge E and add it to the corresponding set of
1058 cgraph_set_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1060 e
->prev_caller
= NULL
;
1062 n
->callers
->prev_caller
= e
;
1063 e
->next_caller
= n
->callers
;
1068 /* Turn edge E into speculative call calling N2. Update
1069 the profile so the direct call is taken COUNT times
1072 At clone materialization time, the indirect call E will
1075 if (call_dest == N2)
1080 At this time the function just creates the direct call,
1081 the referencd representing the if conditional and attaches
1082 them all to the orginal indirect call statement.
1084 Return direct edge created. */
1086 struct cgraph_edge
*
1087 cgraph_turn_edge_to_speculative (struct cgraph_edge
*e
,
1088 struct cgraph_node
*n2
,
1089 gcov_type direct_count
,
1090 int direct_frequency
)
1092 struct cgraph_node
*n
= e
->caller
;
1093 struct ipa_ref
*ref
;
1094 struct cgraph_edge
*e2
;
1098 fprintf (dump_file
, "Indirect call -> speculative call"
1099 " %s/%i => %s/%i\n",
1100 xstrdup (n
->name ()), n
->order
,
1101 xstrdup (n2
->name ()), n2
->order
);
1103 e
->speculative
= true;
1104 e2
= cgraph_create_edge (n
, n2
, e
->call_stmt
, direct_count
, direct_frequency
);
1105 initialize_inline_failed (e2
);
1106 e2
->speculative
= true;
1107 if (TREE_NOTHROW (n2
->decl
))
1108 e2
->can_throw_external
= false;
1110 e2
->can_throw_external
= e
->can_throw_external
;
1111 e2
->lto_stmt_uid
= e
->lto_stmt_uid
;
1112 e
->count
-= e2
->count
;
1113 e
->frequency
-= e2
->frequency
;
1114 cgraph_call_edge_duplication_hooks (e
, e2
);
1115 ref
= ipa_record_reference (n
, n2
,
1116 IPA_REF_ADDR
, e
->call_stmt
);
1117 ref
->lto_stmt_uid
= e
->lto_stmt_uid
;
1118 ref
->speculative
= e
->speculative
;
1119 cgraph_mark_address_taken_node (n2
);
1123 /* Speculative call consist of three components:
1124 1) an indirect edge representing the original call
1125 2) an direct edge representing the new call
1126 3) ADDR_EXPR reference representing the speculative check.
1127 All three components are attached to single statement (the indirect
1128 call) and if one of them exists, all of them must exist.
1130 Given speculative call edge E, return all three components.
1134 cgraph_speculative_call_info (struct cgraph_edge
*e
,
1135 struct cgraph_edge
*&direct
,
1136 struct cgraph_edge
*&indirect
,
1137 struct ipa_ref
*&reference
)
1139 struct ipa_ref
*ref
;
1141 struct cgraph_edge
*e2
;
1143 if (!e
->indirect_unknown_callee
)
1144 for (e2
= e
->caller
->indirect_calls
;
1145 e2
->call_stmt
!= e
->call_stmt
|| e2
->lto_stmt_uid
!= e
->lto_stmt_uid
;
1146 e2
= e2
->next_callee
)
1151 /* We can take advantage of the call stmt hash. */
1154 e
= cgraph_edge (e
->caller
, e2
->call_stmt
);
1155 gcc_assert (e
->speculative
&& !e
->indirect_unknown_callee
);
1158 for (e
= e
->caller
->callees
;
1159 e2
->call_stmt
!= e
->call_stmt
1160 || e2
->lto_stmt_uid
!= e
->lto_stmt_uid
;
1164 gcc_assert (e
->speculative
&& e2
->speculative
);
1169 for (i
= 0; ipa_ref_list_reference_iterate (&e
->caller
->ref_list
,
1171 if (ref
->speculative
1172 && ((ref
->stmt
&& ref
->stmt
== e
->call_stmt
)
1173 || (!ref
->stmt
&& ref
->lto_stmt_uid
== e
->lto_stmt_uid
)))
1179 /* Speculative edge always consist of all three components - direct edge,
1180 indirect and reference. */
1182 gcc_assert (e
&& e2
&& ref
);
1185 /* Redirect callee of E to N. The function does not update underlying
1189 cgraph_redirect_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1191 /* Remove from callers list of the current callee. */
1192 cgraph_edge_remove_callee (e
);
1194 /* Insert to callers list of the new callee. */
1195 cgraph_set_edge_callee (e
, n
);
1198 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1199 Remove the speculative call sequence and return edge representing the call.
1200 It is up to caller to redirect the call as appropriate. */
1202 struct cgraph_edge
*
1203 cgraph_resolve_speculation (struct cgraph_edge
*edge
, tree callee_decl
)
1205 struct cgraph_edge
*e2
;
1206 struct ipa_ref
*ref
;
1208 gcc_assert (edge
->speculative
);
1209 cgraph_speculative_call_info (edge
, e2
, edge
, ref
);
1211 || !symtab_semantically_equivalent_p (ref
->referred
,
1212 symtab_get_node (callee_decl
)))
1218 fprintf (dump_file
, "Speculative indirect call %s/%i => %s/%i has "
1219 "turned out to have contradicting known target ",
1220 xstrdup (edge
->caller
->name ()), edge
->caller
->order
,
1221 xstrdup (e2
->callee
->name ()), e2
->callee
->order
);
1222 print_generic_expr (dump_file
, callee_decl
, 0);
1223 fprintf (dump_file
, "\n");
1227 fprintf (dump_file
, "Removing speculative call %s/%i => %s/%i\n",
1228 xstrdup (edge
->caller
->name ()), edge
->caller
->order
,
1229 xstrdup (e2
->callee
->name ()), e2
->callee
->order
);
1235 struct cgraph_edge
*tmp
= edge
;
1237 fprintf (dump_file
, "Speculative call turned into direct call.\n");
1240 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1241 in the functions inlined through it. */
1243 edge
->count
+= e2
->count
;
1244 edge
->frequency
+= e2
->frequency
;
1245 if (edge
->frequency
> CGRAPH_FREQ_MAX
)
1246 edge
->frequency
= CGRAPH_FREQ_MAX
;
1247 edge
->speculative
= false;
1248 e2
->speculative
= false;
1249 ipa_remove_reference (ref
);
1250 if (e2
->indirect_unknown_callee
|| e2
->inline_failed
)
1251 cgraph_remove_edge (e2
);
1253 cgraph_remove_node_and_inline_clones (e2
->callee
, NULL
);
1254 if (edge
->caller
->call_site_hash
)
1255 cgraph_update_edge_in_call_site_hash (edge
);
1259 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1260 CALLEE. DELTA is an integer constant that is to be added to the this
1261 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1263 struct cgraph_edge
*
1264 cgraph_make_edge_direct (struct cgraph_edge
*edge
, struct cgraph_node
*callee
)
1266 gcc_assert (edge
->indirect_unknown_callee
);
1268 /* If we are redirecting speculative call, make it non-speculative. */
1269 if (edge
->indirect_unknown_callee
&& edge
->speculative
)
1271 edge
= cgraph_resolve_speculation (edge
, callee
->decl
);
1273 /* On successful speculation just return the pre existing direct edge. */
1274 if (!edge
->indirect_unknown_callee
)
1278 edge
->indirect_unknown_callee
= 0;
1279 ggc_free (edge
->indirect_info
);
1280 edge
->indirect_info
= NULL
;
1282 /* Get the edge out of the indirect edge list. */
1283 if (edge
->prev_callee
)
1284 edge
->prev_callee
->next_callee
= edge
->next_callee
;
1285 if (edge
->next_callee
)
1286 edge
->next_callee
->prev_callee
= edge
->prev_callee
;
1287 if (!edge
->prev_callee
)
1288 edge
->caller
->indirect_calls
= edge
->next_callee
;
1290 /* Put it into the normal callee list */
1291 edge
->prev_callee
= NULL
;
1292 edge
->next_callee
= edge
->caller
->callees
;
1293 if (edge
->caller
->callees
)
1294 edge
->caller
->callees
->prev_callee
= edge
;
1295 edge
->caller
->callees
= edge
;
1297 /* Insert to callers list of the new callee. */
1298 cgraph_set_edge_callee (edge
, callee
);
1300 if (edge
->call_stmt
)
1301 edge
->call_stmt_cannot_inline_p
1302 = !gimple_check_call_matching_types (edge
->call_stmt
, callee
->decl
,
1305 /* We need to re-determine the inlining status of the edge. */
1306 initialize_inline_failed (edge
);
1310 /* If necessary, change the function declaration in the call statement
1311 associated with E so that it corresponds to the edge callee. */
1314 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge
*e
)
1316 tree decl
= gimple_call_fndecl (e
->call_stmt
);
1318 gimple_stmt_iterator gsi
;
1319 #ifdef ENABLE_CHECKING
1320 struct cgraph_node
*node
;
1325 struct cgraph_edge
*e2
;
1327 struct ipa_ref
*ref
;
1329 cgraph_speculative_call_info (e
, e
, e2
, ref
);
1330 /* If there already is an direct call (i.e. as a result of inliner's
1331 substitution), forget about speculating. */
1333 e
= cgraph_resolve_speculation (e
, decl
);
1334 /* If types do not match, speculation was likely wrong.
1335 The direct edge was posisbly redirected to the clone with a different
1336 signature. We did not update the call statement yet, so compare it
1337 with the reference that still points to the proper type. */
1338 else if (!gimple_check_call_matching_types (e
->call_stmt
,
1339 ref
->referred
->decl
,
1343 fprintf (dump_file
, "Not expanding speculative call of %s/%i -> %s/%i\n"
1345 xstrdup (e
->caller
->name ()),
1347 xstrdup (e
->callee
->name ()),
1349 e
= cgraph_resolve_speculation (e
, NULL
);
1350 /* We are producing the final function body and will throw away the
1351 callgraph edges really soon. Reset the counts/frequencies to
1352 keep verifier happy in the case of roundoff errors. */
1353 e
->count
= gimple_bb (e
->call_stmt
)->count
;
1354 e
->frequency
= compute_call_stmt_bb_frequency
1355 (e
->caller
->decl
, gimple_bb (e
->call_stmt
));
1357 /* Expand speculation into GIMPLE code. */
1362 "Expanding speculative call of %s/%i -> %s/%i count:"
1363 HOST_WIDEST_INT_PRINT_DEC
"\n",
1364 xstrdup (e
->caller
->name ()),
1366 xstrdup (e
->callee
->name ()),
1368 (HOST_WIDEST_INT
)e
->count
);
1369 gcc_assert (e2
->speculative
);
1370 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
1371 new_stmt
= gimple_ic (e
->call_stmt
, cgraph (ref
->referred
),
1372 e
->count
|| e2
->count
1373 ? RDIV (e
->count
* REG_BR_PROB_BASE
,
1374 e
->count
+ e2
->count
)
1375 : e
->frequency
|| e2
->frequency
1376 ? RDIV (e
->frequency
* REG_BR_PROB_BASE
,
1377 e
->frequency
+ e2
->frequency
)
1378 : REG_BR_PROB_BASE
/ 2,
1379 e
->count
, e
->count
+ e2
->count
);
1380 e
->speculative
= false;
1381 cgraph_set_call_stmt_including_clones (e
->caller
, e
->call_stmt
,
1383 e
->frequency
= compute_call_stmt_bb_frequency
1384 (e
->caller
->decl
, gimple_bb (e
->call_stmt
));
1385 e2
->frequency
= compute_call_stmt_bb_frequency
1386 (e2
->caller
->decl
, gimple_bb (e2
->call_stmt
));
1387 e2
->speculative
= false;
1388 ref
->speculative
= false;
1390 /* Indirect edges are not both in the call site hash.
1392 if (e
->caller
->call_site_hash
)
1393 cgraph_update_edge_in_call_site_hash (e2
);
1395 /* Continue redirecting E to proper target. */
1399 if (e
->indirect_unknown_callee
1400 || decl
== e
->callee
->decl
)
1401 return e
->call_stmt
;
1403 #ifdef ENABLE_CHECKING
1406 node
= cgraph_get_node (decl
);
1407 gcc_assert (!node
|| !node
->clone
.combined_args_to_skip
);
1411 if (cgraph_dump_file
)
1413 fprintf (cgraph_dump_file
, "updating call of %s/%i -> %s/%i: ",
1414 xstrdup (e
->caller
->name ()), e
->caller
->order
,
1415 xstrdup (e
->callee
->name ()), e
->callee
->order
);
1416 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1417 if (e
->callee
->clone
.combined_args_to_skip
)
1419 fprintf (cgraph_dump_file
, " combined args to skip: ");
1420 dump_bitmap (cgraph_dump_file
,
1421 e
->callee
->clone
.combined_args_to_skip
);
1425 if (e
->callee
->clone
.combined_args_to_skip
)
1430 = gimple_call_copy_skip_args (e
->call_stmt
,
1431 e
->callee
->clone
.combined_args_to_skip
);
1432 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1433 gimple_call_set_fntype (new_stmt
, gimple_call_fntype (e
->call_stmt
));
1435 if (gimple_vdef (new_stmt
)
1436 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1437 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1439 gsi
= gsi_for_stmt (e
->call_stmt
);
1440 gsi_replace (&gsi
, new_stmt
, false);
1441 /* We need to defer cleaning EH info on the new statement to
1442 fixup-cfg. We may not have dominator information at this point
1443 and thus would end up with unreachable blocks and have no way
1444 to communicate that we need to run CFG cleanup then. */
1445 lp_nr
= lookup_stmt_eh_lp (e
->call_stmt
);
1448 remove_stmt_from_eh_lp (e
->call_stmt
);
1449 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
1454 new_stmt
= e
->call_stmt
;
1455 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1456 update_stmt (new_stmt
);
1459 cgraph_set_call_stmt_including_clones (e
->caller
, e
->call_stmt
, new_stmt
, false);
1461 if (cgraph_dump_file
)
1463 fprintf (cgraph_dump_file
, " updated to:");
1464 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1469 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1470 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1471 of OLD_STMT if it was previously call statement.
1472 If NEW_STMT is NULL, the call has been dropped without any
1476 cgraph_update_edges_for_call_stmt_node (struct cgraph_node
*node
,
1477 gimple old_stmt
, tree old_call
,
1480 tree new_call
= (new_stmt
&& is_gimple_call (new_stmt
))
1481 ? gimple_call_fndecl (new_stmt
) : 0;
1483 /* We are seeing indirect calls, then there is nothing to update. */
1484 if (!new_call
&& !old_call
)
1486 /* See if we turned indirect call into direct call or folded call to one builtin
1487 into different builtin. */
1488 if (old_call
!= new_call
)
1490 struct cgraph_edge
*e
= cgraph_edge (node
, old_stmt
);
1491 struct cgraph_edge
*ne
= NULL
;
1497 /* See if the edge is already there and has the correct callee. It
1498 might be so because of indirect inlining has already updated
1499 it. We also might've cloned and redirected the edge. */
1500 if (new_call
&& e
->callee
)
1502 struct cgraph_node
*callee
= e
->callee
;
1505 if (callee
->decl
== new_call
1506 || callee
->former_clone_of
== new_call
)
1508 callee
= callee
->clone_of
;
1512 /* Otherwise remove edge and create new one; we can't simply redirect
1513 since function has changed, so inline plan and other information
1514 attached to edge is invalid. */
1516 frequency
= e
->frequency
;
1517 cgraph_remove_edge (e
);
1521 /* We are seeing new direct call; compute profile info based on BB. */
1522 basic_block bb
= gimple_bb (new_stmt
);
1524 frequency
= compute_call_stmt_bb_frequency (current_function_decl
,
1530 ne
= cgraph_create_edge (node
, cgraph_get_create_node (new_call
),
1531 new_stmt
, count
, frequency
);
1532 gcc_assert (ne
->inline_failed
);
1535 /* We only updated the call stmt; update pointer in cgraph edge.. */
1536 else if (old_stmt
!= new_stmt
)
1537 cgraph_set_call_stmt (cgraph_edge (node
, old_stmt
), new_stmt
);
1540 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1541 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1542 of OLD_STMT before it was updated (updating can happen inplace). */
1545 cgraph_update_edges_for_call_stmt (gimple old_stmt
, tree old_decl
, gimple new_stmt
)
1547 struct cgraph_node
*orig
= cgraph_get_node (cfun
->decl
);
1548 struct cgraph_node
*node
;
1550 gcc_checking_assert (orig
);
1551 cgraph_update_edges_for_call_stmt_node (orig
, old_stmt
, old_decl
, new_stmt
);
1553 for (node
= orig
->clones
; node
!= orig
;)
1555 cgraph_update_edges_for_call_stmt_node (node
, old_stmt
, old_decl
, new_stmt
);
1557 node
= node
->clones
;
1558 else if (node
->next_sibling_clone
)
1559 node
= node
->next_sibling_clone
;
1562 while (node
!= orig
&& !node
->next_sibling_clone
)
1563 node
= node
->clone_of
;
1565 node
= node
->next_sibling_clone
;
1571 /* Remove all callees from the node. */
1574 cgraph_node_remove_callees (struct cgraph_node
*node
)
1576 struct cgraph_edge
*e
, *f
;
1578 /* It is sufficient to remove the edges from the lists of callers of
1579 the callees. The callee list of the node can be zapped with one
1581 for (e
= node
->callees
; e
; e
= f
)
1584 cgraph_call_edge_removal_hooks (e
);
1585 if (!e
->indirect_unknown_callee
)
1586 cgraph_edge_remove_callee (e
);
1587 cgraph_free_edge (e
);
1589 for (e
= node
->indirect_calls
; e
; e
= f
)
1592 cgraph_call_edge_removal_hooks (e
);
1593 if (!e
->indirect_unknown_callee
)
1594 cgraph_edge_remove_callee (e
);
1595 cgraph_free_edge (e
);
1597 node
->indirect_calls
= NULL
;
1598 node
->callees
= NULL
;
1599 if (node
->call_site_hash
)
1601 htab_delete (node
->call_site_hash
);
1602 node
->call_site_hash
= NULL
;
1606 /* Remove all callers from the node. */
1609 cgraph_node_remove_callers (struct cgraph_node
*node
)
1611 struct cgraph_edge
*e
, *f
;
1613 /* It is sufficient to remove the edges from the lists of callees of
1614 the callers. The caller list of the node can be zapped with one
1616 for (e
= node
->callers
; e
; e
= f
)
1619 cgraph_call_edge_removal_hooks (e
);
1620 cgraph_edge_remove_caller (e
);
1621 cgraph_free_edge (e
);
1623 node
->callers
= NULL
;
1626 /* Helper function for cgraph_release_function_body and free_lang_data.
1627 It releases body from function DECL without having to inspect its
1628 possibly non-existent symtab node. */
1631 release_function_body (tree decl
)
1633 if (DECL_STRUCT_FUNCTION (decl
))
1635 push_cfun (DECL_STRUCT_FUNCTION (decl
));
1639 cfun
->curr_properties
&= ~PROP_loops
;
1640 loop_optimizer_finalize ();
1642 if (cfun
->gimple_df
)
1645 delete_tree_cfg_annotations ();
1650 gcc_assert (dom_computed
[0] == DOM_NONE
);
1651 gcc_assert (dom_computed
[1] == DOM_NONE
);
1655 if (cfun
->value_histograms
)
1658 gimple_set_body (decl
, NULL
);
1659 /* Struct function hangs a lot of data that would leak if we didn't
1660 removed all pointers to it. */
1661 ggc_free (DECL_STRUCT_FUNCTION (decl
));
1662 DECL_STRUCT_FUNCTION (decl
) = NULL
;
1664 DECL_SAVED_TREE (decl
) = NULL
;
1667 /* Release memory used to represent body of function NODE.
1668 Use this only for functions that are released before being translated to
1669 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1670 are free'd in final.c via free_after_compilation(). */
1673 cgraph_release_function_body (struct cgraph_node
*node
)
1675 node
->ipa_transforms_to_apply
.release ();
1676 if (!node
->used_as_abstract_origin
&& cgraph_state
!= CGRAPH_STATE_PARSING
)
1678 DECL_RESULT (node
->decl
) = NULL
;
1679 DECL_ARGUMENTS (node
->decl
) = NULL
;
1681 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1682 of its associated function function declaration because it's
1683 needed to emit debug info later. */
1684 if (!node
->used_as_abstract_origin
&& DECL_INITIAL (node
->decl
))
1685 DECL_INITIAL (node
->decl
) = error_mark_node
;
1686 release_function_body (node
->decl
);
1687 if (node
->lto_file_data
)
1688 lto_free_function_in_decl_state_for_node (node
);
1691 /* Remove the node from cgraph. */
1694 cgraph_remove_node (struct cgraph_node
*node
)
1696 struct cgraph_node
*n
;
1697 int uid
= node
->uid
;
1699 cgraph_call_node_removal_hooks (node
);
1700 cgraph_node_remove_callers (node
);
1701 cgraph_node_remove_callees (node
);
1702 node
->ipa_transforms_to_apply
.release ();
1704 /* Incremental inlining access removed nodes stored in the postorder list.
1706 node
->force_output
= false;
1707 node
->forced_by_abi
= false;
1708 for (n
= node
->nested
; n
; n
= n
->next_nested
)
1710 node
->nested
= NULL
;
1713 struct cgraph_node
**node2
= &node
->origin
->nested
;
1715 while (*node2
!= node
)
1716 node2
= &(*node2
)->next_nested
;
1717 *node2
= node
->next_nested
;
1719 symtab_unregister_node (node
);
1720 if (node
->prev_sibling_clone
)
1721 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
1722 else if (node
->clone_of
)
1723 node
->clone_of
->clones
= node
->next_sibling_clone
;
1724 if (node
->next_sibling_clone
)
1725 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
1728 struct cgraph_node
*n
, *next
;
1732 for (n
= node
->clones
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
1733 n
->clone_of
= node
->clone_of
;
1734 n
->clone_of
= node
->clone_of
;
1735 n
->next_sibling_clone
= node
->clone_of
->clones
;
1736 if (node
->clone_of
->clones
)
1737 node
->clone_of
->clones
->prev_sibling_clone
= n
;
1738 node
->clone_of
->clones
= node
->clones
;
1742 /* We are removing node with clones. This makes clones inconsistent,
1743 but assume they will be removed subsequently and just keep clone
1744 tree intact. This can happen in unreachable function removal since
1745 we remove unreachable functions in random order, not by bottom-up
1746 walk of clone trees. */
1747 for (n
= node
->clones
; n
; n
= next
)
1749 next
= n
->next_sibling_clone
;
1750 n
->next_sibling_clone
= NULL
;
1751 n
->prev_sibling_clone
= NULL
;
1757 /* While all the clones are removed after being proceeded, the function
1758 itself is kept in the cgraph even after it is compiled. Check whether
1759 we are done with this body and reclaim it proactively if this is the case.
1761 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
1763 n
= cgraph_get_node (node
->decl
);
1765 || (!n
->clones
&& !n
->clone_of
&& !n
->global
.inlined_to
1766 && (cgraph_global_info_ready
1767 && (TREE_ASM_WRITTEN (n
->decl
)
1768 || DECL_EXTERNAL (n
->decl
)
1770 || (!flag_wpa
&& n
->in_other_partition
)))))
1771 cgraph_release_function_body (node
);
1775 if (node
->call_site_hash
)
1777 htab_delete (node
->call_site_hash
);
1778 node
->call_site_hash
= NULL
;
1782 /* Clear out the node to NULL all pointers and add the node to the free
1784 memset (node
, 0, sizeof (*node
));
1785 node
->type
= SYMTAB_FUNCTION
;
1787 SET_NEXT_FREE_NODE (node
, free_nodes
);
1791 /* Likewise indicate that a node is having address taken. */
1794 cgraph_mark_address_taken_node (struct cgraph_node
*node
)
1796 /* Indirect inlining can figure out that all uses of the address are
1798 if (node
->global
.inlined_to
)
1800 gcc_assert (cfun
->after_inlining
);
1801 gcc_assert (node
->callers
->indirect_inlining_edge
);
1804 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1805 IPA_REF_ADDR reference exists (and thus it should be set on node
1806 representing alias we take address of) and as a test whether address
1807 of the object was taken (and thus it should be set on node alias is
1808 referring to). We should remove the first use and the remove the
1810 node
->address_taken
= 1;
1811 node
= cgraph_function_or_thunk_node (node
, NULL
);
1812 node
->address_taken
= 1;
1815 /* Return local info for the compiled function. */
1817 struct cgraph_local_info
*
1818 cgraph_local_info (tree decl
)
1820 struct cgraph_node
*node
;
1822 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1823 node
= cgraph_get_node (decl
);
1826 return &node
->local
;
1829 /* Return local info for the compiled function. */
1831 struct cgraph_global_info
*
1832 cgraph_global_info (tree decl
)
1834 struct cgraph_node
*node
;
1836 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
&& cgraph_global_info_ready
);
1837 node
= cgraph_get_node (decl
);
1840 return &node
->global
;
1843 /* Return local info for the compiled function. */
1845 struct cgraph_rtl_info
*
1846 cgraph_rtl_info (tree decl
)
1848 struct cgraph_node
*node
;
1850 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1851 node
= cgraph_get_node (decl
);
1853 || (decl
!= current_function_decl
1854 && !TREE_ASM_WRITTEN (node
->decl
)))
1859 /* Return a string describing the failure REASON. */
1862 cgraph_inline_failed_string (cgraph_inline_failed_t reason
)
1865 #define DEFCIFCODE(code, string) string,
1867 static const char *cif_string_table
[CIF_N_REASONS
] = {
1868 #include "cif-code.def"
1871 /* Signedness of an enum type is implementation defined, so cast it
1872 to unsigned before testing. */
1873 gcc_assert ((unsigned) reason
< CIF_N_REASONS
);
1874 return cif_string_table
[reason
];
1877 /* Names used to print out the availability enum. */
1878 const char * const cgraph_availability_names
[] =
1879 {"unset", "not_available", "overwritable", "available", "local"};
1882 /* Dump call graph node NODE to file F. */
1885 dump_cgraph_node (FILE *f
, struct cgraph_node
*node
)
1887 struct cgraph_edge
*edge
;
1888 int indirect_calls_count
= 0;
1890 dump_symtab_base (f
, node
);
1892 if (node
->global
.inlined_to
)
1893 fprintf (f
, " Function %s/%i is inline copy in %s/%i\n",
1894 xstrdup (node
->name ()),
1896 xstrdup (node
->global
.inlined_to
->name ()),
1897 node
->global
.inlined_to
->order
);
1899 fprintf (f
, " Clone of %s/%i\n",
1900 node
->clone_of
->asm_name (),
1901 node
->clone_of
->order
);
1902 if (cgraph_function_flags_ready
)
1903 fprintf (f
, " Availability: %s\n",
1904 cgraph_availability_names
[cgraph_function_body_availability (node
)]);
1906 if (node
->profile_id
)
1907 fprintf (f
, " Profile id: %i\n",
1909 fprintf (f
, " First run: %i\n", node
->tp_first_run
);
1910 fprintf (f
, " Function flags:");
1912 fprintf (f
, " executed "HOST_WIDEST_INT_PRINT_DEC
"x",
1913 (HOST_WIDEST_INT
)node
->count
);
1915 fprintf (f
, " nested in: %s", node
->origin
->asm_name ());
1916 if (gimple_has_body_p (node
->decl
))
1917 fprintf (f
, " body");
1919 fprintf (f
, " process");
1920 if (node
->local
.local
)
1921 fprintf (f
, " local");
1922 if (node
->local
.redefined_extern_inline
)
1923 fprintf (f
, " redefined_extern_inline");
1924 if (node
->only_called_at_startup
)
1925 fprintf (f
, " only_called_at_startup");
1926 if (node
->only_called_at_exit
)
1927 fprintf (f
, " only_called_at_exit");
1929 fprintf (f
, " tm_clone");
1933 if (node
->thunk
.thunk_p
)
1935 fprintf (f
, " Thunk");
1936 if (node
->thunk
.alias
)
1937 fprintf (f
, " of %s (asm: %s)",
1938 lang_hooks
.decl_printable_name (node
->thunk
.alias
, 2),
1939 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->thunk
.alias
)));
1940 fprintf (f
, " fixed offset %i virtual value %i has "
1941 "virtual offset %i)\n",
1942 (int)node
->thunk
.fixed_offset
,
1943 (int)node
->thunk
.virtual_value
,
1944 (int)node
->thunk
.virtual_offset_p
);
1946 if (node
->alias
&& node
->thunk
.alias
1947 && DECL_P (node
->thunk
.alias
))
1949 fprintf (f
, " Alias of %s",
1950 lang_hooks
.decl_printable_name (node
->thunk
.alias
, 2));
1951 if (DECL_ASSEMBLER_NAME_SET_P (node
->thunk
.alias
))
1952 fprintf (f
, " (asm: %s)",
1953 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->thunk
.alias
)));
1957 fprintf (f
, " Called by: ");
1959 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
1961 fprintf (f
, "%s/%i ", edge
->caller
->asm_name (),
1962 edge
->caller
->order
);
1964 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
1965 (HOST_WIDEST_INT
)edge
->count
);
1966 if (edge
->frequency
)
1967 fprintf (f
, "(%.2f per call) ",
1968 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
1969 if (edge
->speculative
)
1970 fprintf (f
, "(speculative) ");
1971 if (!edge
->inline_failed
)
1972 fprintf (f
, "(inlined) ");
1973 if (edge
->indirect_inlining_edge
)
1974 fprintf (f
, "(indirect_inlining) ");
1975 if (edge
->can_throw_external
)
1976 fprintf (f
, "(can throw external) ");
1979 fprintf (f
, "\n Calls: ");
1980 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1982 fprintf (f
, "%s/%i ", edge
->callee
->asm_name (),
1983 edge
->callee
->order
);
1984 if (edge
->speculative
)
1985 fprintf (f
, "(speculative) ");
1986 if (!edge
->inline_failed
)
1987 fprintf (f
, "(inlined) ");
1988 if (edge
->indirect_inlining_edge
)
1989 fprintf (f
, "(indirect_inlining) ");
1991 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
1992 (HOST_WIDEST_INT
)edge
->count
);
1993 if (edge
->frequency
)
1994 fprintf (f
, "(%.2f per call) ",
1995 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
1996 if (edge
->can_throw_external
)
1997 fprintf (f
, "(can throw external) ");
2001 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
2002 indirect_calls_count
++;
2003 if (indirect_calls_count
)
2004 fprintf (f
, " Has %i outgoing edges for indirect calls.\n",
2005 indirect_calls_count
);
2009 /* Dump call graph node NODE to stderr. */
2012 debug_cgraph_node (struct cgraph_node
*node
)
2014 dump_cgraph_node (stderr
, node
);
2018 /* Dump the callgraph to file F. */
2021 dump_cgraph (FILE *f
)
2023 struct cgraph_node
*node
;
2025 fprintf (f
, "callgraph:\n\n");
2026 FOR_EACH_FUNCTION (node
)
2027 dump_cgraph_node (f
, node
);
2031 /* Dump the call graph to stderr. */
2036 dump_cgraph (stderr
);
2039 /* Return true when the DECL can possibly be inlined. */
2041 cgraph_function_possibly_inlined_p (tree decl
)
2043 if (!cgraph_global_info_ready
)
2044 return !DECL_UNINLINABLE (decl
);
2045 return DECL_POSSIBLY_INLINED (decl
);
2048 /* NODE is no longer nested function; update cgraph accordingly. */
2050 cgraph_unnest_node (struct cgraph_node
*node
)
2052 struct cgraph_node
**node2
= &node
->origin
->nested
;
2053 gcc_assert (node
->origin
);
2055 while (*node2
!= node
)
2056 node2
= &(*node2
)->next_nested
;
2057 *node2
= node
->next_nested
;
2058 node
->origin
= NULL
;
2061 /* Return function availability. See cgraph.h for description of individual
2064 cgraph_function_body_availability (struct cgraph_node
*node
)
2066 enum availability avail
;
2067 if (!node
->analyzed
)
2068 avail
= AVAIL_NOT_AVAILABLE
;
2069 else if (node
->local
.local
)
2070 avail
= AVAIL_LOCAL
;
2071 else if (node
->alias
&& node
->weakref
)
2072 cgraph_function_or_thunk_node (node
, &avail
);
2073 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node
->decl
)))
2074 avail
= AVAIL_OVERWRITABLE
;
2075 else if (!node
->externally_visible
)
2076 avail
= AVAIL_AVAILABLE
;
2077 /* Inline functions are safe to be analyzed even if their symbol can
2078 be overwritten at runtime. It is not meaningful to enforce any sane
2079 behaviour on replacing inline function by different body. */
2080 else if (DECL_DECLARED_INLINE_P (node
->decl
))
2081 avail
= AVAIL_AVAILABLE
;
2083 /* If the function can be overwritten, return OVERWRITABLE. Take
2084 care at least of two notable extensions - the COMDAT functions
2085 used to share template instantiations in C++ (this is symmetric
2086 to code cp_cannot_inline_tree_fn and probably shall be shared and
2087 the inlinability hooks completely eliminated).
2089 ??? Does the C++ one definition rule allow us to always return
2090 AVAIL_AVAILABLE here? That would be good reason to preserve this
2093 else if (decl_replaceable_p (node
->decl
)
2094 && !DECL_EXTERNAL (node
->decl
))
2095 avail
= AVAIL_OVERWRITABLE
;
2096 else avail
= AVAIL_AVAILABLE
;
2101 /* Worker for cgraph_node_can_be_local_p. */
2103 cgraph_node_cannot_be_local_p_1 (struct cgraph_node
*node
,
2104 void *data ATTRIBUTE_UNUSED
)
2106 return !(!node
->force_output
2107 && ((DECL_COMDAT (node
->decl
)
2108 && !node
->forced_by_abi
2109 && !symtab_used_from_object_file_p (node
)
2110 && !node
->same_comdat_group
)
2111 || !node
->externally_visible
));
2114 /* Return true if NODE can be made local for API change.
2115 Extern inline functions and C++ COMDAT functions can be made local
2116 at the expense of possible code size growth if function is used in multiple
2117 compilation units. */
2119 cgraph_node_can_be_local_p (struct cgraph_node
*node
)
2121 return (!node
->address_taken
2122 && !cgraph_for_node_and_aliases (node
,
2123 cgraph_node_cannot_be_local_p_1
,
2127 /* Call calback on NODE, thunks and aliases associated to NODE.
2128 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2132 cgraph_for_node_thunks_and_aliases (struct cgraph_node
*node
,
2133 bool (*callback
) (struct cgraph_node
*, void *),
2135 bool include_overwritable
)
2137 struct cgraph_edge
*e
;
2139 struct ipa_ref
*ref
;
2141 if (callback (node
, data
))
2143 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2144 if (e
->caller
->thunk
.thunk_p
2145 && (include_overwritable
2146 || cgraph_function_body_availability (e
->caller
) > AVAIL_OVERWRITABLE
))
2147 if (cgraph_for_node_thunks_and_aliases (e
->caller
, callback
, data
,
2148 include_overwritable
))
2150 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
2151 if (ref
->use
== IPA_REF_ALIAS
)
2153 struct cgraph_node
*alias
= ipa_ref_referring_node (ref
);
2154 if (include_overwritable
2155 || cgraph_function_body_availability (alias
) > AVAIL_OVERWRITABLE
)
2156 if (cgraph_for_node_thunks_and_aliases (alias
, callback
, data
,
2157 include_overwritable
))
2163 /* Call calback on NODE and aliases associated to NODE.
2164 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2168 cgraph_for_node_and_aliases (struct cgraph_node
*node
,
2169 bool (*callback
) (struct cgraph_node
*, void *),
2171 bool include_overwritable
)
2174 struct ipa_ref
*ref
;
2176 if (callback (node
, data
))
2178 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
2179 if (ref
->use
== IPA_REF_ALIAS
)
2181 struct cgraph_node
*alias
= ipa_ref_referring_node (ref
);
2182 if (include_overwritable
2183 || cgraph_function_body_availability (alias
) > AVAIL_OVERWRITABLE
)
2184 if (cgraph_for_node_and_aliases (alias
, callback
, data
,
2185 include_overwritable
))
2191 /* Worker to bring NODE local. */
2194 cgraph_make_node_local_1 (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2196 gcc_checking_assert (cgraph_node_can_be_local_p (node
));
2197 if (DECL_COMDAT (node
->decl
) || DECL_EXTERNAL (node
->decl
))
2199 symtab_make_decl_local (node
->decl
);
2201 node
->externally_visible
= false;
2202 node
->forced_by_abi
= false;
2203 node
->local
.local
= true;
2204 node
->unique_name
= (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
2205 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
2206 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
2207 gcc_assert (cgraph_function_body_availability (node
) == AVAIL_LOCAL
);
2212 /* Bring NODE local. */
2215 cgraph_make_node_local (struct cgraph_node
*node
)
2217 cgraph_for_node_thunks_and_aliases (node
, cgraph_make_node_local_1
,
2221 /* Worker to set nothrow flag. */
2224 cgraph_set_nothrow_flag_1 (struct cgraph_node
*node
, void *data
)
2226 struct cgraph_edge
*e
;
2228 TREE_NOTHROW (node
->decl
) = data
!= NULL
;
2231 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2232 e
->can_throw_external
= false;
2236 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2237 if any to NOTHROW. */
2240 cgraph_set_nothrow_flag (struct cgraph_node
*node
, bool nothrow
)
2242 cgraph_for_node_thunks_and_aliases (node
, cgraph_set_nothrow_flag_1
,
2243 (void *)(size_t)nothrow
, false);
2246 /* Worker to set const flag. */
2249 cgraph_set_const_flag_1 (struct cgraph_node
*node
, void *data
)
2251 /* Static constructors and destructors without a side effect can be
2253 if (data
&& !((size_t)data
& 2))
2255 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2256 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2257 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2258 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2260 TREE_READONLY (node
->decl
) = data
!= NULL
;
2261 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = ((size_t)data
& 2) != 0;
2265 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2266 if any to READONLY. */
2269 cgraph_set_const_flag (struct cgraph_node
*node
, bool readonly
, bool looping
)
2271 cgraph_for_node_thunks_and_aliases (node
, cgraph_set_const_flag_1
,
2272 (void *)(size_t)(readonly
+ (int)looping
* 2),
2276 /* Worker to set pure flag. */
2279 cgraph_set_pure_flag_1 (struct cgraph_node
*node
, void *data
)
2281 /* Static constructors and destructors without a side effect can be
2283 if (data
&& !((size_t)data
& 2))
2285 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2286 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2287 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2288 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2290 DECL_PURE_P (node
->decl
) = data
!= NULL
;
2291 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = ((size_t)data
& 2) != 0;
2295 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2299 cgraph_set_pure_flag (struct cgraph_node
*node
, bool pure
, bool looping
)
2301 cgraph_for_node_thunks_and_aliases (node
, cgraph_set_pure_flag_1
,
2302 (void *)(size_t)(pure
+ (int)looping
* 2),
2306 /* Return true when NODE can not return or throw and thus
2307 it is safe to ignore its side effects for IPA analysis. */
2310 cgraph_node_cannot_return (struct cgraph_node
*node
)
2312 int flags
= flags_from_decl_or_type (node
->decl
);
2313 if (!flag_exceptions
)
2314 return (flags
& ECF_NORETURN
) != 0;
2316 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2317 == (ECF_NORETURN
| ECF_NOTHROW
));
2320 /* Return true when call of E can not lead to return from caller
2321 and thus it is safe to ignore its side effects for IPA analysis
2322 when computing side effects of the caller.
2323 FIXME: We could actually mark all edges that have no reaching
2324 patch to EXIT_BLOCK_PTR or throw to get better results. */
2326 cgraph_edge_cannot_lead_to_return (struct cgraph_edge
*e
)
2328 if (cgraph_node_cannot_return (e
->caller
))
2330 if (e
->indirect_unknown_callee
)
2332 int flags
= e
->indirect_info
->ecf_flags
;
2333 if (!flag_exceptions
)
2334 return (flags
& ECF_NORETURN
) != 0;
2336 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2337 == (ECF_NORETURN
| ECF_NOTHROW
));
2340 return cgraph_node_cannot_return (e
->callee
);
2343 /* Return true when function NODE can be removed from callgraph
2344 if all direct calls are eliminated. */
2347 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node
*node
)
2349 gcc_assert (!node
->global
.inlined_to
);
2350 /* Extern inlines can always go, we will use the external definition. */
2351 if (DECL_EXTERNAL (node
->decl
))
2353 /* When function is needed, we can not remove it. */
2354 if (node
->force_output
|| node
->used_from_other_partition
)
2356 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
2357 || DECL_STATIC_DESTRUCTOR (node
->decl
))
2359 /* Only COMDAT functions can be removed if externally visible. */
2360 if (node
->externally_visible
2361 && (!DECL_COMDAT (node
->decl
)
2362 || node
->forced_by_abi
2363 || symtab_used_from_object_file_p (node
)))
2368 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2371 nonremovable_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2373 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node
);
2376 /* Return true when function NODE and its aliases can be removed from callgraph
2377 if all direct calls are eliminated. */
2380 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node
*node
)
2382 /* Extern inlines can always go, we will use the external definition. */
2383 if (DECL_EXTERNAL (node
->decl
))
2385 if (node
->address_taken
)
2387 return !cgraph_for_node_and_aliases (node
, nonremovable_p
, NULL
, true);
2390 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2393 used_from_object_file_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2395 return symtab_used_from_object_file_p (node
);
2398 /* Return true when function NODE can be expected to be removed
2399 from program when direct calls in this compilation unit are removed.
2401 As a special case COMDAT functions are
2402 cgraph_can_remove_if_no_direct_calls_p while the are not
2403 cgraph_only_called_directly_p (it is possible they are called from other
2406 This function behaves as cgraph_only_called_directly_p because eliminating
2407 all uses of COMDAT function does not make it necessarily disappear from
2408 the program unless we are compiling whole program or we do LTO. In this
2409 case we know we win since dynamic linking will not really discard the
2410 linkonce section. */
2413 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node
*node
)
2415 gcc_assert (!node
->global
.inlined_to
);
2416 if (cgraph_for_node_and_aliases (node
, used_from_object_file_p
, NULL
, true))
2418 if (!in_lto_p
&& !flag_whole_program
)
2419 return cgraph_only_called_directly_p (node
);
2422 if (DECL_EXTERNAL (node
->decl
))
2424 return cgraph_can_remove_if_no_direct_calls_p (node
);
2429 /* Worker for cgraph_only_called_directly_p. */
2432 cgraph_not_only_called_directly_p_1 (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2434 return !cgraph_only_called_directly_or_aliased_p (node
);
2437 /* Return true when function NODE and all its aliases are only called
2439 i.e. it is not externally visible, address was not taken and
2440 it is not used in any other non-standard way. */
2443 cgraph_only_called_directly_p (struct cgraph_node
*node
)
2445 gcc_assert (cgraph_function_or_thunk_node (node
, NULL
) == node
);
2446 return !cgraph_for_node_and_aliases (node
, cgraph_not_only_called_directly_p_1
,
2451 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2454 collect_callers_of_node_1 (struct cgraph_node
*node
, void *data
)
2456 vec
<cgraph_edge_p
> *redirect_callers
= (vec
<cgraph_edge_p
> *)data
;
2457 struct cgraph_edge
*cs
;
2458 enum availability avail
;
2459 cgraph_function_or_thunk_node (node
, &avail
);
2461 if (avail
> AVAIL_OVERWRITABLE
)
2462 for (cs
= node
->callers
; cs
!= NULL
; cs
= cs
->next_caller
)
2463 if (!cs
->indirect_inlining_edge
)
2464 redirect_callers
->safe_push (cs
);
2468 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2469 (i.e. are not overwritable). */
2472 collect_callers_of_node (struct cgraph_node
*node
)
2474 vec
<cgraph_edge_p
> redirect_callers
= vNULL
;
2475 cgraph_for_node_and_aliases (node
, collect_callers_of_node_1
,
2476 &redirect_callers
, false);
2477 return redirect_callers
;
2480 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2482 clone_of_p (struct cgraph_node
*node
, struct cgraph_node
*node2
)
2484 node
= cgraph_function_or_thunk_node (node
, NULL
);
2485 node2
= cgraph_function_or_thunk_node (node2
, NULL
);
2486 while (node
!= node2
&& node2
)
2487 node2
= node2
->clone_of
;
2488 return node2
!= NULL
;
2491 /* Verify edge E count and frequency. */
2494 verify_edge_count_and_frequency (struct cgraph_edge
*e
)
2496 bool error_found
= false;
2499 error ("caller edge count is negative");
2502 if (e
->frequency
< 0)
2504 error ("caller edge frequency is negative");
2507 if (e
->frequency
> CGRAPH_FREQ_MAX
)
2509 error ("caller edge frequency is too large");
2512 if (gimple_has_body_p (e
->caller
->decl
)
2513 && !e
->caller
->global
.inlined_to
2515 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2516 Remove this once edges are actually removed from the function at that time. */
2518 || (inline_edge_summary_vec
.exists ()
2519 && ((inline_edge_summary_vec
.length () <= (unsigned) e
->uid
)
2520 || !inline_edge_summary (e
)->predicate
)))
2522 != compute_call_stmt_bb_frequency (e
->caller
->decl
,
2523 gimple_bb (e
->call_stmt
))))
2525 error ("caller edge frequency %i does not match BB frequency %i",
2527 compute_call_stmt_bb_frequency (e
->caller
->decl
,
2528 gimple_bb (e
->call_stmt
)));
2534 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2536 cgraph_debug_gimple_stmt (struct function
*this_cfun
, gimple stmt
)
2538 bool fndecl_was_null
= false;
2539 /* debug_gimple_stmt needs correct cfun */
2540 if (cfun
!= this_cfun
)
2541 set_cfun (this_cfun
);
2542 /* ...and an actual current_function_decl */
2543 if (!current_function_decl
)
2545 current_function_decl
= this_cfun
->decl
;
2546 fndecl_was_null
= true;
2548 debug_gimple_stmt (stmt
);
2549 if (fndecl_was_null
)
2550 current_function_decl
= NULL
;
2553 /* Verify that call graph edge E corresponds to DECL from the associated
2554 statement. Return true if the verification should fail. */
2557 verify_edge_corresponds_to_fndecl (struct cgraph_edge
*e
, tree decl
)
2559 struct cgraph_node
*node
;
2561 if (!decl
|| e
->callee
->global
.inlined_to
)
2563 if (cgraph_state
== CGRAPH_LTO_STREAMING
)
2565 node
= cgraph_get_node (decl
);
2567 /* We do not know if a node from a different partition is an alias or what it
2568 aliases and therefore cannot do the former_clone_of check reliably. */
2569 if (!node
|| node
->in_other_partition
|| e
->callee
->in_other_partition
)
2571 node
= cgraph_function_or_thunk_node (node
, NULL
);
2573 if (e
->callee
->former_clone_of
!= node
->decl
2574 /* IPA-CP sometimes redirect edge to clone and then back to the former
2575 function. This ping-pong has to go, eventually. */
2576 && (node
!= cgraph_function_or_thunk_node (e
->callee
, NULL
))
2577 && !clone_of_p (cgraph_function_or_thunk_node (node
, NULL
), e
->callee
))
2583 /* Verify cgraph nodes of given cgraph node. */
2585 verify_cgraph_node (struct cgraph_node
*node
)
2587 struct cgraph_edge
*e
;
2588 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (node
->decl
);
2589 basic_block this_block
;
2590 gimple_stmt_iterator gsi
;
2591 bool error_found
= false;
2596 timevar_push (TV_CGRAPH_VERIFY
);
2597 error_found
|= verify_symtab_base (node
);
2598 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2601 error ("aux field set for edge %s->%s",
2602 identifier_to_locale (e
->caller
->name ()),
2603 identifier_to_locale (e
->callee
->name ()));
2606 if (node
->count
< 0)
2608 error ("execution count is negative");
2611 if (node
->global
.inlined_to
&& node
->same_comdat_group
)
2613 error ("inline clone in same comdat group list");
2616 if (!node
->definition
&& !node
->in_other_partition
&& node
->local
.local
)
2618 error ("local symbols must be defined");
2621 if (node
->global
.inlined_to
&& node
->externally_visible
)
2623 error ("externally visible inline clone");
2626 if (node
->global
.inlined_to
&& node
->address_taken
)
2628 error ("inline clone with address taken");
2631 if (node
->global
.inlined_to
&& node
->force_output
)
2633 error ("inline clone is forced to output");
2636 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2640 error ("aux field set for indirect edge from %s",
2641 identifier_to_locale (e
->caller
->name ()));
2644 if (!e
->indirect_unknown_callee
2645 || !e
->indirect_info
)
2647 error ("An indirect edge from %s is not marked as indirect or has "
2648 "associated indirect_info, the corresponding statement is: ",
2649 identifier_to_locale (e
->caller
->name ()));
2650 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2654 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2656 if (verify_edge_count_and_frequency (e
))
2658 if (!e
->inline_failed
)
2660 if (node
->global
.inlined_to
2661 != (e
->caller
->global
.inlined_to
2662 ? e
->caller
->global
.inlined_to
: e
->caller
))
2664 error ("inlined_to pointer is wrong");
2667 if (node
->callers
->next_caller
)
2669 error ("multiple inline callers");
2674 if (node
->global
.inlined_to
)
2676 error ("inlined_to pointer set for noninline callers");
2680 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2681 if (verify_edge_count_and_frequency (e
))
2683 if (!node
->callers
&& node
->global
.inlined_to
)
2685 error ("inlined_to pointer is set but no predecessors found");
2688 if (node
->global
.inlined_to
== node
)
2690 error ("inlined_to pointer refers to itself");
2696 struct cgraph_node
*n
;
2697 for (n
= node
->clone_of
->clones
; n
; n
= n
->next_sibling_clone
)
2702 error ("node has wrong clone_of");
2708 struct cgraph_node
*n
;
2709 for (n
= node
->clones
; n
; n
= n
->next_sibling_clone
)
2710 if (n
->clone_of
!= node
)
2714 error ("node has wrong clone list");
2718 if ((node
->prev_sibling_clone
|| node
->next_sibling_clone
) && !node
->clone_of
)
2720 error ("node is in clone list but it is not clone");
2723 if (!node
->prev_sibling_clone
&& node
->clone_of
&& node
->clone_of
->clones
!= node
)
2725 error ("node has wrong prev_clone pointer");
2728 if (node
->prev_sibling_clone
&& node
->prev_sibling_clone
->next_sibling_clone
!= node
)
2730 error ("double linked list of clones corrupted");
2734 if (node
->analyzed
&& node
->alias
)
2736 bool ref_found
= false;
2738 struct ipa_ref
*ref
;
2742 error ("Alias has call edges");
2745 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
,
2747 if (ref
->use
!= IPA_REF_ALIAS
)
2749 error ("Alias has non-alias reference");
2754 error ("Alias has more than one alias reference");
2761 error ("Analyzed alias has no reference");
2765 if (node
->analyzed
&& node
->thunk
.thunk_p
)
2769 error ("No edge out of thunk node");
2772 else if (node
->callees
->next_callee
)
2774 error ("More than one edge out of thunk node");
2777 if (gimple_has_body_p (node
->decl
))
2779 error ("Thunk is not supposed to have body");
2783 else if (node
->analyzed
&& gimple_has_body_p (node
->decl
)
2784 && !TREE_ASM_WRITTEN (node
->decl
)
2785 && (!DECL_EXTERNAL (node
->decl
) || node
->global
.inlined_to
)
2790 pointer_set_t
*stmts
= pointer_set_create ();
2792 struct ipa_ref
*ref
;
2794 /* Reach the trees by walking over the CFG, and note the
2795 enclosing basic-blocks in the call edges. */
2796 FOR_EACH_BB_FN (this_block
, this_cfun
)
2798 for (gsi
= gsi_start_phis (this_block
);
2799 !gsi_end_p (gsi
); gsi_next (&gsi
))
2800 pointer_set_insert (stmts
, gsi_stmt (gsi
));
2801 for (gsi
= gsi_start_bb (this_block
);
2805 gimple stmt
= gsi_stmt (gsi
);
2806 pointer_set_insert (stmts
, stmt
);
2807 if (is_gimple_call (stmt
))
2809 struct cgraph_edge
*e
= cgraph_edge (node
, stmt
);
2810 tree decl
= gimple_call_fndecl (stmt
);
2815 error ("shared call_stmt:");
2816 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
2819 if (!e
->indirect_unknown_callee
)
2821 if (verify_edge_corresponds_to_fndecl (e
, decl
))
2823 error ("edge points to wrong declaration:");
2824 debug_tree (e
->callee
->decl
);
2825 fprintf (stderr
," Instead of:");
2832 error ("an indirect edge with unknown callee "
2833 "corresponding to a call_stmt with "
2834 "a known declaration:");
2836 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2842 error ("missing callgraph edge for call stmt:");
2843 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
2850 ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
);
2852 if (ref
->stmt
&& !pointer_set_contains (stmts
, ref
->stmt
))
2854 error ("reference to dead statement");
2855 cgraph_debug_gimple_stmt (this_cfun
, ref
->stmt
);
2858 pointer_set_destroy (stmts
);
2861 /* No CFG available?! */
2864 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2868 error ("edge %s->%s has no corresponding call_stmt",
2869 identifier_to_locale (e
->caller
->name ()),
2870 identifier_to_locale (e
->callee
->name ()));
2871 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2876 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2878 if (!e
->aux
&& !e
->speculative
)
2880 error ("an indirect edge from %s has no corresponding call_stmt",
2881 identifier_to_locale (e
->caller
->name ()));
2882 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2890 dump_cgraph_node (stderr
, node
);
2891 internal_error ("verify_cgraph_node failed");
2893 timevar_pop (TV_CGRAPH_VERIFY
);
2896 /* Verify whole cgraph structure. */
2898 verify_cgraph (void)
2900 struct cgraph_node
*node
;
2905 FOR_EACH_FUNCTION (node
)
2906 verify_cgraph_node (node
);
2909 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2910 Walk through thunk, too.
2911 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2913 struct cgraph_node
*
2914 cgraph_function_node (struct cgraph_node
*node
, enum availability
*availability
)
2918 node
= cgraph_function_or_thunk_node (node
, availability
);
2919 if (node
->thunk
.thunk_p
)
2921 node
= node
->callees
->callee
;
2924 enum availability a
;
2925 a
= cgraph_function_body_availability (node
);
2926 if (a
< *availability
)
2929 node
= cgraph_function_or_thunk_node (node
, availability
);
2931 } while (node
&& node
->thunk
.thunk_p
);
2935 /* When doing LTO, read NODE's body from disk if it is not already present. */
2938 cgraph_get_body (struct cgraph_node
*node
)
2940 struct lto_file_decl_data
*file_data
;
2941 const char *data
, *name
;
2943 tree decl
= node
->decl
;
2945 if (DECL_RESULT (decl
))
2948 gcc_assert (in_lto_p
);
2950 file_data
= node
->lto_file_data
;
2951 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2953 /* We may have renamed the declaration, e.g., a static function. */
2954 name
= lto_get_decl_name_mapping (file_data
, name
);
2956 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
2960 dump_cgraph_node (stderr
, node
);
2961 fatal_error ("%s: section %s is missing",
2962 file_data
->file_name
,
2966 gcc_assert (DECL_STRUCT_FUNCTION (decl
) == NULL
);
2968 lto_input_function_body (file_data
, node
, data
);
2969 lto_stats
.num_function_bodies
++;
2970 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
2972 lto_free_function_in_decl_state_for_node (node
);
2976 /* Verify if the type of the argument matches that of the function
2977 declaration. If we cannot verify this or there is a mismatch,
2981 gimple_check_call_args (gimple stmt
, tree fndecl
, bool args_count_match
)
2984 unsigned int i
, nargs
;
2986 /* Calls to internal functions always match their signature. */
2987 if (gimple_call_internal_p (stmt
))
2990 nargs
= gimple_call_num_args (stmt
);
2992 /* Get argument types for verification. */
2994 parms
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2996 parms
= TYPE_ARG_TYPES (gimple_call_fntype (stmt
));
2998 /* Verify if the type of the argument matches that of the function
2999 declaration. If we cannot verify this or there is a mismatch,
3001 if (fndecl
&& DECL_ARGUMENTS (fndecl
))
3003 for (i
= 0, p
= DECL_ARGUMENTS (fndecl
);
3005 i
++, p
= DECL_CHAIN (p
))
3008 /* We cannot distinguish a varargs function from the case
3009 of excess parameters, still deferring the inlining decision
3010 to the callee is possible. */
3013 arg
= gimple_call_arg (stmt
, i
);
3014 if (p
== error_mark_node
3015 || arg
== error_mark_node
3016 || (!types_compatible_p (DECL_ARG_TYPE (p
), TREE_TYPE (arg
))
3017 && !fold_convertible_p (DECL_ARG_TYPE (p
), arg
)))
3020 if (args_count_match
&& p
)
3025 for (i
= 0, p
= parms
; i
< nargs
; i
++, p
= TREE_CHAIN (p
))
3028 /* If this is a varargs function defer inlining decision
3032 arg
= gimple_call_arg (stmt
, i
);
3033 if (TREE_VALUE (p
) == error_mark_node
3034 || arg
== error_mark_node
3035 || TREE_CODE (TREE_VALUE (p
)) == VOID_TYPE
3036 || (!types_compatible_p (TREE_VALUE (p
), TREE_TYPE (arg
))
3037 && !fold_convertible_p (TREE_VALUE (p
), arg
)))
3049 /* Verify if the type of the argument and lhs of CALL_STMT matches
3050 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3051 true, the arg count needs to be the same.
3052 If we cannot verify this or there is a mismatch, return false. */
3055 gimple_check_call_matching_types (gimple call_stmt
, tree callee
,
3056 bool args_count_match
)
3060 if ((DECL_RESULT (callee
)
3061 && !DECL_BY_REFERENCE (DECL_RESULT (callee
))
3062 && (lhs
= gimple_call_lhs (call_stmt
)) != NULL_TREE
3063 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee
)),
3065 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee
)), lhs
))
3066 || !gimple_check_call_args (call_stmt
, callee
, args_count_match
))
3071 #include "gt-cgraph.h"