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
))
963 HOST_WIDE_INT otr_token
;
964 ipa_polymorphic_call_context context
;
966 get_polymorphic_call_info (caller
->decl
,
968 &otr_type
, &otr_token
,
971 /* Only record types can have virtual calls. */
972 gcc_assert (TREE_CODE (otr_type
) == RECORD_TYPE
);
973 edge
->indirect_info
->polymorphic
= true;
974 edge
->indirect_info
->param_index
= -1;
975 edge
->indirect_info
->otr_token
= otr_token
;
976 edge
->indirect_info
->otr_type
= otr_type
;
977 edge
->indirect_info
->outer_type
= context
.outer_type
;
978 edge
->indirect_info
->offset
= context
.offset
;
979 edge
->indirect_info
->maybe_in_construction
980 = context
.maybe_in_construction
;
981 edge
->indirect_info
->maybe_derived_type
= context
.maybe_derived_type
;
984 edge
->next_callee
= caller
->indirect_calls
;
985 if (caller
->indirect_calls
)
986 caller
->indirect_calls
->prev_callee
= edge
;
987 caller
->indirect_calls
= edge
;
992 /* Remove the edge E from the list of the callers of the callee. */
995 cgraph_edge_remove_callee (struct cgraph_edge
*e
)
997 gcc_assert (!e
->indirect_unknown_callee
);
999 e
->prev_caller
->next_caller
= e
->next_caller
;
1001 e
->next_caller
->prev_caller
= e
->prev_caller
;
1002 if (!e
->prev_caller
)
1003 e
->callee
->callers
= e
->next_caller
;
1006 /* Remove the edge E from the list of the callees of the caller. */
1009 cgraph_edge_remove_caller (struct cgraph_edge
*e
)
1012 e
->prev_callee
->next_callee
= e
->next_callee
;
1014 e
->next_callee
->prev_callee
= e
->prev_callee
;
1015 if (!e
->prev_callee
)
1017 if (e
->indirect_unknown_callee
)
1018 e
->caller
->indirect_calls
= e
->next_callee
;
1020 e
->caller
->callees
= e
->next_callee
;
1022 if (e
->caller
->call_site_hash
)
1023 htab_remove_elt_with_hash (e
->caller
->call_site_hash
,
1025 htab_hash_pointer (e
->call_stmt
));
1028 /* Put the edge onto the free list. */
1031 cgraph_free_edge (struct cgraph_edge
*e
)
1035 if (e
->indirect_info
)
1036 ggc_free (e
->indirect_info
);
1038 /* Clear out the edge so we do not dangle pointers. */
1039 memset (e
, 0, sizeof (*e
));
1041 NEXT_FREE_EDGE (e
) = free_edges
;
1045 /* Remove the edge E in the cgraph. */
1048 cgraph_remove_edge (struct cgraph_edge
*e
)
1050 /* Call all edge removal hooks. */
1051 cgraph_call_edge_removal_hooks (e
);
1053 if (!e
->indirect_unknown_callee
)
1054 /* Remove from callers list of the callee. */
1055 cgraph_edge_remove_callee (e
);
1057 /* Remove from callees list of the callers. */
1058 cgraph_edge_remove_caller (e
);
1060 /* Put the edge onto the free list. */
1061 cgraph_free_edge (e
);
1064 /* Set callee of call graph edge E and add it to the corresponding set of
1068 cgraph_set_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1070 e
->prev_caller
= NULL
;
1072 n
->callers
->prev_caller
= e
;
1073 e
->next_caller
= n
->callers
;
1078 /* Turn edge E into speculative call calling N2. Update
1079 the profile so the direct call is taken COUNT times
1082 At clone materialization time, the indirect call E will
1085 if (call_dest == N2)
1090 At this time the function just creates the direct call,
1091 the referencd representing the if conditional and attaches
1092 them all to the orginal indirect call statement.
1094 Return direct edge created. */
1096 struct cgraph_edge
*
1097 cgraph_turn_edge_to_speculative (struct cgraph_edge
*e
,
1098 struct cgraph_node
*n2
,
1099 gcov_type direct_count
,
1100 int direct_frequency
)
1102 struct cgraph_node
*n
= e
->caller
;
1103 struct ipa_ref
*ref
;
1104 struct cgraph_edge
*e2
;
1108 fprintf (dump_file
, "Indirect call -> speculative call"
1109 " %s/%i => %s/%i\n",
1110 xstrdup (n
->name ()), n
->order
,
1111 xstrdup (n2
->name ()), n2
->order
);
1113 e
->speculative
= true;
1114 e2
= cgraph_create_edge (n
, n2
, e
->call_stmt
, direct_count
, direct_frequency
);
1115 initialize_inline_failed (e2
);
1116 e2
->speculative
= true;
1117 if (TREE_NOTHROW (n2
->decl
))
1118 e2
->can_throw_external
= false;
1120 e2
->can_throw_external
= e
->can_throw_external
;
1121 e2
->lto_stmt_uid
= e
->lto_stmt_uid
;
1122 e
->count
-= e2
->count
;
1123 e
->frequency
-= e2
->frequency
;
1124 cgraph_call_edge_duplication_hooks (e
, e2
);
1125 ref
= ipa_record_reference (n
, n2
,
1126 IPA_REF_ADDR
, e
->call_stmt
);
1127 ref
->lto_stmt_uid
= e
->lto_stmt_uid
;
1128 ref
->speculative
= e
->speculative
;
1129 cgraph_mark_address_taken_node (n2
);
1133 /* Speculative call consist of three components:
1134 1) an indirect edge representing the original call
1135 2) an direct edge representing the new call
1136 3) ADDR_EXPR reference representing the speculative check.
1137 All three components are attached to single statement (the indirect
1138 call) and if one of them exists, all of them must exist.
1140 Given speculative call edge E, return all three components.
1144 cgraph_speculative_call_info (struct cgraph_edge
*e
,
1145 struct cgraph_edge
*&direct
,
1146 struct cgraph_edge
*&indirect
,
1147 struct ipa_ref
*&reference
)
1149 struct ipa_ref
*ref
;
1151 struct cgraph_edge
*e2
;
1153 if (!e
->indirect_unknown_callee
)
1154 for (e2
= e
->caller
->indirect_calls
;
1155 e2
->call_stmt
!= e
->call_stmt
|| e2
->lto_stmt_uid
!= e
->lto_stmt_uid
;
1156 e2
= e2
->next_callee
)
1161 /* We can take advantage of the call stmt hash. */
1164 e
= cgraph_edge (e
->caller
, e2
->call_stmt
);
1165 gcc_assert (e
->speculative
&& !e
->indirect_unknown_callee
);
1168 for (e
= e
->caller
->callees
;
1169 e2
->call_stmt
!= e
->call_stmt
1170 || e2
->lto_stmt_uid
!= e
->lto_stmt_uid
;
1174 gcc_assert (e
->speculative
&& e2
->speculative
);
1179 for (i
= 0; ipa_ref_list_reference_iterate (&e
->caller
->ref_list
,
1181 if (ref
->speculative
1182 && ((ref
->stmt
&& ref
->stmt
== e
->call_stmt
)
1183 || (!ref
->stmt
&& ref
->lto_stmt_uid
== e
->lto_stmt_uid
)))
1189 /* Speculative edge always consist of all three components - direct edge,
1190 indirect and reference. */
1192 gcc_assert (e
&& e2
&& ref
);
1195 /* Redirect callee of E to N. The function does not update underlying
1199 cgraph_redirect_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1201 /* Remove from callers list of the current callee. */
1202 cgraph_edge_remove_callee (e
);
1204 /* Insert to callers list of the new callee. */
1205 cgraph_set_edge_callee (e
, n
);
1208 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1209 Remove the speculative call sequence and return edge representing the call.
1210 It is up to caller to redirect the call as appropriate. */
1212 struct cgraph_edge
*
1213 cgraph_resolve_speculation (struct cgraph_edge
*edge
, tree callee_decl
)
1215 struct cgraph_edge
*e2
;
1216 struct ipa_ref
*ref
;
1218 gcc_assert (edge
->speculative
);
1219 cgraph_speculative_call_info (edge
, e2
, edge
, ref
);
1221 || !symtab_semantically_equivalent_p (ref
->referred
,
1222 symtab_get_node (callee_decl
)))
1228 fprintf (dump_file
, "Speculative indirect call %s/%i => %s/%i has "
1229 "turned out to have contradicting known target ",
1230 xstrdup (edge
->caller
->name ()), edge
->caller
->order
,
1231 xstrdup (e2
->callee
->name ()), e2
->callee
->order
);
1232 print_generic_expr (dump_file
, callee_decl
, 0);
1233 fprintf (dump_file
, "\n");
1237 fprintf (dump_file
, "Removing speculative call %s/%i => %s/%i\n",
1238 xstrdup (edge
->caller
->name ()), edge
->caller
->order
,
1239 xstrdup (e2
->callee
->name ()), e2
->callee
->order
);
1245 struct cgraph_edge
*tmp
= edge
;
1247 fprintf (dump_file
, "Speculative call turned into direct call.\n");
1250 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1251 in the functions inlined through it. */
1253 edge
->count
+= e2
->count
;
1254 edge
->frequency
+= e2
->frequency
;
1255 if (edge
->frequency
> CGRAPH_FREQ_MAX
)
1256 edge
->frequency
= CGRAPH_FREQ_MAX
;
1257 edge
->speculative
= false;
1258 e2
->speculative
= false;
1259 ipa_remove_reference (ref
);
1260 if (e2
->indirect_unknown_callee
|| e2
->inline_failed
)
1261 cgraph_remove_edge (e2
);
1263 cgraph_remove_node_and_inline_clones (e2
->callee
, NULL
);
1264 if (edge
->caller
->call_site_hash
)
1265 cgraph_update_edge_in_call_site_hash (edge
);
1269 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1270 CALLEE. DELTA is an integer constant that is to be added to the this
1271 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1273 struct cgraph_edge
*
1274 cgraph_make_edge_direct (struct cgraph_edge
*edge
, struct cgraph_node
*callee
)
1276 gcc_assert (edge
->indirect_unknown_callee
);
1278 /* If we are redirecting speculative call, make it non-speculative. */
1279 if (edge
->indirect_unknown_callee
&& edge
->speculative
)
1281 edge
= cgraph_resolve_speculation (edge
, callee
->decl
);
1283 /* On successful speculation just return the pre existing direct edge. */
1284 if (!edge
->indirect_unknown_callee
)
1288 edge
->indirect_unknown_callee
= 0;
1289 ggc_free (edge
->indirect_info
);
1290 edge
->indirect_info
= NULL
;
1292 /* Get the edge out of the indirect edge list. */
1293 if (edge
->prev_callee
)
1294 edge
->prev_callee
->next_callee
= edge
->next_callee
;
1295 if (edge
->next_callee
)
1296 edge
->next_callee
->prev_callee
= edge
->prev_callee
;
1297 if (!edge
->prev_callee
)
1298 edge
->caller
->indirect_calls
= edge
->next_callee
;
1300 /* Put it into the normal callee list */
1301 edge
->prev_callee
= NULL
;
1302 edge
->next_callee
= edge
->caller
->callees
;
1303 if (edge
->caller
->callees
)
1304 edge
->caller
->callees
->prev_callee
= edge
;
1305 edge
->caller
->callees
= edge
;
1307 /* Insert to callers list of the new callee. */
1308 cgraph_set_edge_callee (edge
, callee
);
1310 if (edge
->call_stmt
)
1311 edge
->call_stmt_cannot_inline_p
1312 = !gimple_check_call_matching_types (edge
->call_stmt
, callee
->decl
,
1315 /* We need to re-determine the inlining status of the edge. */
1316 initialize_inline_failed (edge
);
1320 /* If necessary, change the function declaration in the call statement
1321 associated with E so that it corresponds to the edge callee. */
1324 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge
*e
)
1326 tree decl
= gimple_call_fndecl (e
->call_stmt
);
1328 gimple_stmt_iterator gsi
;
1329 #ifdef ENABLE_CHECKING
1330 struct cgraph_node
*node
;
1335 struct cgraph_edge
*e2
;
1337 struct ipa_ref
*ref
;
1339 cgraph_speculative_call_info (e
, e
, e2
, ref
);
1340 /* If there already is an direct call (i.e. as a result of inliner's
1341 substitution), forget about speculating. */
1343 e
= cgraph_resolve_speculation (e
, decl
);
1344 /* If types do not match, speculation was likely wrong.
1345 The direct edge was posisbly redirected to the clone with a different
1346 signature. We did not update the call statement yet, so compare it
1347 with the reference that still points to the proper type. */
1348 else if (!gimple_check_call_matching_types (e
->call_stmt
,
1349 ref
->referred
->decl
,
1353 fprintf (dump_file
, "Not expanding speculative call of %s/%i -> %s/%i\n"
1355 xstrdup (e
->caller
->name ()),
1357 xstrdup (e
->callee
->name ()),
1359 e
= cgraph_resolve_speculation (e
, NULL
);
1360 /* We are producing the final function body and will throw away the
1361 callgraph edges really soon. Reset the counts/frequencies to
1362 keep verifier happy in the case of roundoff errors. */
1363 e
->count
= gimple_bb (e
->call_stmt
)->count
;
1364 e
->frequency
= compute_call_stmt_bb_frequency
1365 (e
->caller
->decl
, gimple_bb (e
->call_stmt
));
1367 /* Expand speculation into GIMPLE code. */
1372 "Expanding speculative call of %s/%i -> %s/%i count:"
1373 HOST_WIDEST_INT_PRINT_DEC
"\n",
1374 xstrdup (e
->caller
->name ()),
1376 xstrdup (e
->callee
->name ()),
1378 (HOST_WIDEST_INT
)e
->count
);
1379 gcc_assert (e2
->speculative
);
1380 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
1381 new_stmt
= gimple_ic (e
->call_stmt
, cgraph (ref
->referred
),
1382 e
->count
|| e2
->count
1383 ? RDIV (e
->count
* REG_BR_PROB_BASE
,
1384 e
->count
+ e2
->count
)
1385 : e
->frequency
|| e2
->frequency
1386 ? RDIV (e
->frequency
* REG_BR_PROB_BASE
,
1387 e
->frequency
+ e2
->frequency
)
1388 : REG_BR_PROB_BASE
/ 2,
1389 e
->count
, e
->count
+ e2
->count
);
1390 e
->speculative
= false;
1391 cgraph_set_call_stmt_including_clones (e
->caller
, e
->call_stmt
,
1393 e
->frequency
= compute_call_stmt_bb_frequency
1394 (e
->caller
->decl
, gimple_bb (e
->call_stmt
));
1395 e2
->frequency
= compute_call_stmt_bb_frequency
1396 (e2
->caller
->decl
, gimple_bb (e2
->call_stmt
));
1397 e2
->speculative
= false;
1398 ref
->speculative
= false;
1400 /* Indirect edges are not both in the call site hash.
1402 if (e
->caller
->call_site_hash
)
1403 cgraph_update_edge_in_call_site_hash (e2
);
1405 /* Continue redirecting E to proper target. */
1409 if (e
->indirect_unknown_callee
1410 || decl
== e
->callee
->decl
)
1411 return e
->call_stmt
;
1413 #ifdef ENABLE_CHECKING
1416 node
= cgraph_get_node (decl
);
1417 gcc_assert (!node
|| !node
->clone
.combined_args_to_skip
);
1421 if (cgraph_dump_file
)
1423 fprintf (cgraph_dump_file
, "updating call of %s/%i -> %s/%i: ",
1424 xstrdup (e
->caller
->name ()), e
->caller
->order
,
1425 xstrdup (e
->callee
->name ()), e
->callee
->order
);
1426 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1427 if (e
->callee
->clone
.combined_args_to_skip
)
1429 fprintf (cgraph_dump_file
, " combined args to skip: ");
1430 dump_bitmap (cgraph_dump_file
,
1431 e
->callee
->clone
.combined_args_to_skip
);
1435 if (e
->callee
->clone
.combined_args_to_skip
)
1440 = gimple_call_copy_skip_args (e
->call_stmt
,
1441 e
->callee
->clone
.combined_args_to_skip
);
1442 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1443 gimple_call_set_fntype (new_stmt
, gimple_call_fntype (e
->call_stmt
));
1445 if (gimple_vdef (new_stmt
)
1446 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1447 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1449 gsi
= gsi_for_stmt (e
->call_stmt
);
1450 gsi_replace (&gsi
, new_stmt
, false);
1451 /* We need to defer cleaning EH info on the new statement to
1452 fixup-cfg. We may not have dominator information at this point
1453 and thus would end up with unreachable blocks and have no way
1454 to communicate that we need to run CFG cleanup then. */
1455 lp_nr
= lookup_stmt_eh_lp (e
->call_stmt
);
1458 remove_stmt_from_eh_lp (e
->call_stmt
);
1459 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
1464 new_stmt
= e
->call_stmt
;
1465 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1466 update_stmt (new_stmt
);
1469 cgraph_set_call_stmt_including_clones (e
->caller
, e
->call_stmt
, new_stmt
, false);
1471 if (cgraph_dump_file
)
1473 fprintf (cgraph_dump_file
, " updated to:");
1474 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1479 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1480 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1481 of OLD_STMT if it was previously call statement.
1482 If NEW_STMT is NULL, the call has been dropped without any
1486 cgraph_update_edges_for_call_stmt_node (struct cgraph_node
*node
,
1487 gimple old_stmt
, tree old_call
,
1490 tree new_call
= (new_stmt
&& is_gimple_call (new_stmt
))
1491 ? gimple_call_fndecl (new_stmt
) : 0;
1493 /* We are seeing indirect calls, then there is nothing to update. */
1494 if (!new_call
&& !old_call
)
1496 /* See if we turned indirect call into direct call or folded call to one builtin
1497 into different builtin. */
1498 if (old_call
!= new_call
)
1500 struct cgraph_edge
*e
= cgraph_edge (node
, old_stmt
);
1501 struct cgraph_edge
*ne
= NULL
;
1507 /* See if the edge is already there and has the correct callee. It
1508 might be so because of indirect inlining has already updated
1509 it. We also might've cloned and redirected the edge. */
1510 if (new_call
&& e
->callee
)
1512 struct cgraph_node
*callee
= e
->callee
;
1515 if (callee
->decl
== new_call
1516 || callee
->former_clone_of
== new_call
)
1518 callee
= callee
->clone_of
;
1522 /* Otherwise remove edge and create new one; we can't simply redirect
1523 since function has changed, so inline plan and other information
1524 attached to edge is invalid. */
1526 frequency
= e
->frequency
;
1527 cgraph_remove_edge (e
);
1531 /* We are seeing new direct call; compute profile info based on BB. */
1532 basic_block bb
= gimple_bb (new_stmt
);
1534 frequency
= compute_call_stmt_bb_frequency (current_function_decl
,
1540 ne
= cgraph_create_edge (node
, cgraph_get_create_node (new_call
),
1541 new_stmt
, count
, frequency
);
1542 gcc_assert (ne
->inline_failed
);
1545 /* We only updated the call stmt; update pointer in cgraph edge.. */
1546 else if (old_stmt
!= new_stmt
)
1547 cgraph_set_call_stmt (cgraph_edge (node
, old_stmt
), new_stmt
);
1550 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1551 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1552 of OLD_STMT before it was updated (updating can happen inplace). */
1555 cgraph_update_edges_for_call_stmt (gimple old_stmt
, tree old_decl
, gimple new_stmt
)
1557 struct cgraph_node
*orig
= cgraph_get_node (cfun
->decl
);
1558 struct cgraph_node
*node
;
1560 gcc_checking_assert (orig
);
1561 cgraph_update_edges_for_call_stmt_node (orig
, old_stmt
, old_decl
, new_stmt
);
1563 for (node
= orig
->clones
; node
!= orig
;)
1565 cgraph_update_edges_for_call_stmt_node (node
, old_stmt
, old_decl
, new_stmt
);
1567 node
= node
->clones
;
1568 else if (node
->next_sibling_clone
)
1569 node
= node
->next_sibling_clone
;
1572 while (node
!= orig
&& !node
->next_sibling_clone
)
1573 node
= node
->clone_of
;
1575 node
= node
->next_sibling_clone
;
1581 /* Remove all callees from the node. */
1584 cgraph_node_remove_callees (struct cgraph_node
*node
)
1586 struct cgraph_edge
*e
, *f
;
1588 /* It is sufficient to remove the edges from the lists of callers of
1589 the callees. The callee list of the node can be zapped with one
1591 for (e
= node
->callees
; e
; e
= f
)
1594 cgraph_call_edge_removal_hooks (e
);
1595 if (!e
->indirect_unknown_callee
)
1596 cgraph_edge_remove_callee (e
);
1597 cgraph_free_edge (e
);
1599 for (e
= node
->indirect_calls
; e
; e
= f
)
1602 cgraph_call_edge_removal_hooks (e
);
1603 if (!e
->indirect_unknown_callee
)
1604 cgraph_edge_remove_callee (e
);
1605 cgraph_free_edge (e
);
1607 node
->indirect_calls
= NULL
;
1608 node
->callees
= NULL
;
1609 if (node
->call_site_hash
)
1611 htab_delete (node
->call_site_hash
);
1612 node
->call_site_hash
= NULL
;
1616 /* Remove all callers from the node. */
1619 cgraph_node_remove_callers (struct cgraph_node
*node
)
1621 struct cgraph_edge
*e
, *f
;
1623 /* It is sufficient to remove the edges from the lists of callees of
1624 the callers. The caller list of the node can be zapped with one
1626 for (e
= node
->callers
; e
; e
= f
)
1629 cgraph_call_edge_removal_hooks (e
);
1630 cgraph_edge_remove_caller (e
);
1631 cgraph_free_edge (e
);
1633 node
->callers
= NULL
;
1636 /* Helper function for cgraph_release_function_body and free_lang_data.
1637 It releases body from function DECL without having to inspect its
1638 possibly non-existent symtab node. */
1641 release_function_body (tree decl
)
1643 if (DECL_STRUCT_FUNCTION (decl
))
1645 push_cfun (DECL_STRUCT_FUNCTION (decl
));
1649 cfun
->curr_properties
&= ~PROP_loops
;
1650 loop_optimizer_finalize ();
1652 if (cfun
->gimple_df
)
1655 delete_tree_cfg_annotations ();
1660 gcc_assert (dom_computed
[0] == DOM_NONE
);
1661 gcc_assert (dom_computed
[1] == DOM_NONE
);
1665 if (cfun
->value_histograms
)
1668 gimple_set_body (decl
, NULL
);
1669 /* Struct function hangs a lot of data that would leak if we didn't
1670 removed all pointers to it. */
1671 ggc_free (DECL_STRUCT_FUNCTION (decl
));
1672 DECL_STRUCT_FUNCTION (decl
) = NULL
;
1674 DECL_SAVED_TREE (decl
) = NULL
;
1677 /* Release memory used to represent body of function NODE.
1678 Use this only for functions that are released before being translated to
1679 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1680 are free'd in final.c via free_after_compilation(). */
1683 cgraph_release_function_body (struct cgraph_node
*node
)
1685 node
->ipa_transforms_to_apply
.release ();
1686 if (!node
->used_as_abstract_origin
&& cgraph_state
!= CGRAPH_STATE_PARSING
)
1688 DECL_RESULT (node
->decl
) = NULL
;
1689 DECL_ARGUMENTS (node
->decl
) = NULL
;
1691 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1692 of its associated function function declaration because it's
1693 needed to emit debug info later. */
1694 if (!node
->used_as_abstract_origin
&& DECL_INITIAL (node
->decl
))
1695 DECL_INITIAL (node
->decl
) = error_mark_node
;
1696 release_function_body (node
->decl
);
1697 if (node
->lto_file_data
)
1698 lto_free_function_in_decl_state_for_node (node
);
1701 /* Remove the node from cgraph. */
1704 cgraph_remove_node (struct cgraph_node
*node
)
1706 struct cgraph_node
*n
;
1707 int uid
= node
->uid
;
1709 cgraph_call_node_removal_hooks (node
);
1710 cgraph_node_remove_callers (node
);
1711 cgraph_node_remove_callees (node
);
1712 node
->ipa_transforms_to_apply
.release ();
1714 /* Incremental inlining access removed nodes stored in the postorder list.
1716 node
->force_output
= false;
1717 node
->forced_by_abi
= false;
1718 for (n
= node
->nested
; n
; n
= n
->next_nested
)
1720 node
->nested
= NULL
;
1723 struct cgraph_node
**node2
= &node
->origin
->nested
;
1725 while (*node2
!= node
)
1726 node2
= &(*node2
)->next_nested
;
1727 *node2
= node
->next_nested
;
1729 symtab_unregister_node (node
);
1730 if (node
->prev_sibling_clone
)
1731 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
1732 else if (node
->clone_of
)
1733 node
->clone_of
->clones
= node
->next_sibling_clone
;
1734 if (node
->next_sibling_clone
)
1735 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
1738 struct cgraph_node
*n
, *next
;
1742 for (n
= node
->clones
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
1743 n
->clone_of
= node
->clone_of
;
1744 n
->clone_of
= node
->clone_of
;
1745 n
->next_sibling_clone
= node
->clone_of
->clones
;
1746 if (node
->clone_of
->clones
)
1747 node
->clone_of
->clones
->prev_sibling_clone
= n
;
1748 node
->clone_of
->clones
= node
->clones
;
1752 /* We are removing node with clones. This makes clones inconsistent,
1753 but assume they will be removed subsequently and just keep clone
1754 tree intact. This can happen in unreachable function removal since
1755 we remove unreachable functions in random order, not by bottom-up
1756 walk of clone trees. */
1757 for (n
= node
->clones
; n
; n
= next
)
1759 next
= n
->next_sibling_clone
;
1760 n
->next_sibling_clone
= NULL
;
1761 n
->prev_sibling_clone
= NULL
;
1767 /* While all the clones are removed after being proceeded, the function
1768 itself is kept in the cgraph even after it is compiled. Check whether
1769 we are done with this body and reclaim it proactively if this is the case.
1771 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
1773 n
= cgraph_get_node (node
->decl
);
1775 || (!n
->clones
&& !n
->clone_of
&& !n
->global
.inlined_to
1776 && (cgraph_global_info_ready
1777 && (TREE_ASM_WRITTEN (n
->decl
)
1778 || DECL_EXTERNAL (n
->decl
)
1780 || (!flag_wpa
&& n
->in_other_partition
)))))
1781 cgraph_release_function_body (node
);
1785 if (node
->call_site_hash
)
1787 htab_delete (node
->call_site_hash
);
1788 node
->call_site_hash
= NULL
;
1792 /* Clear out the node to NULL all pointers and add the node to the free
1794 memset (node
, 0, sizeof (*node
));
1795 node
->type
= SYMTAB_FUNCTION
;
1797 SET_NEXT_FREE_NODE (node
, free_nodes
);
1801 /* Likewise indicate that a node is having address taken. */
1804 cgraph_mark_address_taken_node (struct cgraph_node
*node
)
1806 /* Indirect inlining can figure out that all uses of the address are
1808 if (node
->global
.inlined_to
)
1810 gcc_assert (cfun
->after_inlining
);
1811 gcc_assert (node
->callers
->indirect_inlining_edge
);
1814 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1815 IPA_REF_ADDR reference exists (and thus it should be set on node
1816 representing alias we take address of) and as a test whether address
1817 of the object was taken (and thus it should be set on node alias is
1818 referring to). We should remove the first use and the remove the
1820 node
->address_taken
= 1;
1821 node
= cgraph_function_or_thunk_node (node
, NULL
);
1822 node
->address_taken
= 1;
1825 /* Return local info for the compiled function. */
1827 struct cgraph_local_info
*
1828 cgraph_local_info (tree decl
)
1830 struct cgraph_node
*node
;
1832 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1833 node
= cgraph_get_node (decl
);
1836 return &node
->local
;
1839 /* Return local info for the compiled function. */
1841 struct cgraph_global_info
*
1842 cgraph_global_info (tree decl
)
1844 struct cgraph_node
*node
;
1846 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
&& cgraph_global_info_ready
);
1847 node
= cgraph_get_node (decl
);
1850 return &node
->global
;
1853 /* Return local info for the compiled function. */
1855 struct cgraph_rtl_info
*
1856 cgraph_rtl_info (tree decl
)
1858 struct cgraph_node
*node
;
1860 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1861 node
= cgraph_get_node (decl
);
1863 || (decl
!= current_function_decl
1864 && !TREE_ASM_WRITTEN (node
->decl
)))
1869 /* Return a string describing the failure REASON. */
1872 cgraph_inline_failed_string (cgraph_inline_failed_t reason
)
1875 #define DEFCIFCODE(code, string) string,
1877 static const char *cif_string_table
[CIF_N_REASONS
] = {
1878 #include "cif-code.def"
1881 /* Signedness of an enum type is implementation defined, so cast it
1882 to unsigned before testing. */
1883 gcc_assert ((unsigned) reason
< CIF_N_REASONS
);
1884 return cif_string_table
[reason
];
1887 /* Names used to print out the availability enum. */
1888 const char * const cgraph_availability_names
[] =
1889 {"unset", "not_available", "overwritable", "available", "local"};
1892 /* Dump call graph node NODE to file F. */
1895 dump_cgraph_node (FILE *f
, struct cgraph_node
*node
)
1897 struct cgraph_edge
*edge
;
1898 int indirect_calls_count
= 0;
1900 dump_symtab_base (f
, node
);
1902 if (node
->global
.inlined_to
)
1903 fprintf (f
, " Function %s/%i is inline copy in %s/%i\n",
1904 xstrdup (node
->name ()),
1906 xstrdup (node
->global
.inlined_to
->name ()),
1907 node
->global
.inlined_to
->order
);
1909 fprintf (f
, " Clone of %s/%i\n",
1910 node
->clone_of
->asm_name (),
1911 node
->clone_of
->order
);
1912 if (cgraph_function_flags_ready
)
1913 fprintf (f
, " Availability: %s\n",
1914 cgraph_availability_names
[cgraph_function_body_availability (node
)]);
1916 if (node
->profile_id
)
1917 fprintf (f
, " Profile id: %i\n",
1919 fprintf (f
, " First run: %i\n", node
->tp_first_run
);
1920 fprintf (f
, " Function flags:");
1922 fprintf (f
, " executed "HOST_WIDEST_INT_PRINT_DEC
"x",
1923 (HOST_WIDEST_INT
)node
->count
);
1925 fprintf (f
, " nested in: %s", node
->origin
->asm_name ());
1926 if (gimple_has_body_p (node
->decl
))
1927 fprintf (f
, " body");
1929 fprintf (f
, " process");
1930 if (node
->local
.local
)
1931 fprintf (f
, " local");
1932 if (node
->local
.redefined_extern_inline
)
1933 fprintf (f
, " redefined_extern_inline");
1934 if (node
->only_called_at_startup
)
1935 fprintf (f
, " only_called_at_startup");
1936 if (node
->only_called_at_exit
)
1937 fprintf (f
, " only_called_at_exit");
1939 fprintf (f
, " tm_clone");
1943 if (node
->thunk
.thunk_p
)
1945 fprintf (f
, " Thunk");
1946 if (node
->thunk
.alias
)
1947 fprintf (f
, " of %s (asm: %s)",
1948 lang_hooks
.decl_printable_name (node
->thunk
.alias
, 2),
1949 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->thunk
.alias
)));
1950 fprintf (f
, " fixed offset %i virtual value %i has "
1951 "virtual offset %i)\n",
1952 (int)node
->thunk
.fixed_offset
,
1953 (int)node
->thunk
.virtual_value
,
1954 (int)node
->thunk
.virtual_offset_p
);
1956 if (node
->alias
&& node
->thunk
.alias
1957 && DECL_P (node
->thunk
.alias
))
1959 fprintf (f
, " Alias of %s",
1960 lang_hooks
.decl_printable_name (node
->thunk
.alias
, 2));
1961 if (DECL_ASSEMBLER_NAME_SET_P (node
->thunk
.alias
))
1962 fprintf (f
, " (asm: %s)",
1963 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->thunk
.alias
)));
1967 fprintf (f
, " Called by: ");
1969 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
1971 fprintf (f
, "%s/%i ", edge
->caller
->asm_name (),
1972 edge
->caller
->order
);
1974 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
1975 (HOST_WIDEST_INT
)edge
->count
);
1976 if (edge
->frequency
)
1977 fprintf (f
, "(%.2f per call) ",
1978 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
1979 if (edge
->speculative
)
1980 fprintf (f
, "(speculative) ");
1981 if (!edge
->inline_failed
)
1982 fprintf (f
, "(inlined) ");
1983 if (edge
->indirect_inlining_edge
)
1984 fprintf (f
, "(indirect_inlining) ");
1985 if (edge
->can_throw_external
)
1986 fprintf (f
, "(can throw external) ");
1989 fprintf (f
, "\n Calls: ");
1990 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1992 fprintf (f
, "%s/%i ", edge
->callee
->asm_name (),
1993 edge
->callee
->order
);
1994 if (edge
->speculative
)
1995 fprintf (f
, "(speculative) ");
1996 if (!edge
->inline_failed
)
1997 fprintf (f
, "(inlined) ");
1998 if (edge
->indirect_inlining_edge
)
1999 fprintf (f
, "(indirect_inlining) ");
2001 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
2002 (HOST_WIDEST_INT
)edge
->count
);
2003 if (edge
->frequency
)
2004 fprintf (f
, "(%.2f per call) ",
2005 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
2006 if (edge
->can_throw_external
)
2007 fprintf (f
, "(can throw external) ");
2011 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
2012 indirect_calls_count
++;
2013 if (indirect_calls_count
)
2014 fprintf (f
, " Has %i outgoing edges for indirect calls.\n",
2015 indirect_calls_count
);
2019 /* Dump call graph node NODE to stderr. */
2022 debug_cgraph_node (struct cgraph_node
*node
)
2024 dump_cgraph_node (stderr
, node
);
2028 /* Dump the callgraph to file F. */
2031 dump_cgraph (FILE *f
)
2033 struct cgraph_node
*node
;
2035 fprintf (f
, "callgraph:\n\n");
2036 FOR_EACH_FUNCTION (node
)
2037 dump_cgraph_node (f
, node
);
2041 /* Dump the call graph to stderr. */
2046 dump_cgraph (stderr
);
2049 /* Return true when the DECL can possibly be inlined. */
2051 cgraph_function_possibly_inlined_p (tree decl
)
2053 if (!cgraph_global_info_ready
)
2054 return !DECL_UNINLINABLE (decl
);
2055 return DECL_POSSIBLY_INLINED (decl
);
2058 /* NODE is no longer nested function; update cgraph accordingly. */
2060 cgraph_unnest_node (struct cgraph_node
*node
)
2062 struct cgraph_node
**node2
= &node
->origin
->nested
;
2063 gcc_assert (node
->origin
);
2065 while (*node2
!= node
)
2066 node2
= &(*node2
)->next_nested
;
2067 *node2
= node
->next_nested
;
2068 node
->origin
= NULL
;
2071 /* Return function availability. See cgraph.h for description of individual
2074 cgraph_function_body_availability (struct cgraph_node
*node
)
2076 enum availability avail
;
2077 if (!node
->analyzed
)
2078 avail
= AVAIL_NOT_AVAILABLE
;
2079 else if (node
->local
.local
)
2080 avail
= AVAIL_LOCAL
;
2081 else if (node
->alias
&& node
->weakref
)
2082 cgraph_function_or_thunk_node (node
, &avail
);
2083 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node
->decl
)))
2084 avail
= AVAIL_OVERWRITABLE
;
2085 else if (!node
->externally_visible
)
2086 avail
= AVAIL_AVAILABLE
;
2087 /* Inline functions are safe to be analyzed even if their symbol can
2088 be overwritten at runtime. It is not meaningful to enforce any sane
2089 behaviour on replacing inline function by different body. */
2090 else if (DECL_DECLARED_INLINE_P (node
->decl
))
2091 avail
= AVAIL_AVAILABLE
;
2093 /* If the function can be overwritten, return OVERWRITABLE. Take
2094 care at least of two notable extensions - the COMDAT functions
2095 used to share template instantiations in C++ (this is symmetric
2096 to code cp_cannot_inline_tree_fn and probably shall be shared and
2097 the inlinability hooks completely eliminated).
2099 ??? Does the C++ one definition rule allow us to always return
2100 AVAIL_AVAILABLE here? That would be good reason to preserve this
2103 else if (decl_replaceable_p (node
->decl
)
2104 && !DECL_EXTERNAL (node
->decl
))
2105 avail
= AVAIL_OVERWRITABLE
;
2106 else avail
= AVAIL_AVAILABLE
;
2111 /* Worker for cgraph_node_can_be_local_p. */
2113 cgraph_node_cannot_be_local_p_1 (struct cgraph_node
*node
,
2114 void *data ATTRIBUTE_UNUSED
)
2116 return !(!node
->force_output
2117 && ((DECL_COMDAT (node
->decl
)
2118 && !node
->forced_by_abi
2119 && !symtab_used_from_object_file_p (node
)
2120 && !node
->same_comdat_group
)
2121 || !node
->externally_visible
));
2124 /* Return true if NODE can be made local for API change.
2125 Extern inline functions and C++ COMDAT functions can be made local
2126 at the expense of possible code size growth if function is used in multiple
2127 compilation units. */
2129 cgraph_node_can_be_local_p (struct cgraph_node
*node
)
2131 return (!node
->address_taken
2132 && !cgraph_for_node_and_aliases (node
,
2133 cgraph_node_cannot_be_local_p_1
,
2137 /* Call calback on NODE, thunks and aliases associated to NODE.
2138 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2142 cgraph_for_node_thunks_and_aliases (struct cgraph_node
*node
,
2143 bool (*callback
) (struct cgraph_node
*, void *),
2145 bool include_overwritable
)
2147 struct cgraph_edge
*e
;
2149 struct ipa_ref
*ref
;
2151 if (callback (node
, data
))
2153 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2154 if (e
->caller
->thunk
.thunk_p
2155 && (include_overwritable
2156 || cgraph_function_body_availability (e
->caller
) > AVAIL_OVERWRITABLE
))
2157 if (cgraph_for_node_thunks_and_aliases (e
->caller
, callback
, data
,
2158 include_overwritable
))
2160 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
2161 if (ref
->use
== IPA_REF_ALIAS
)
2163 struct cgraph_node
*alias
= ipa_ref_referring_node (ref
);
2164 if (include_overwritable
2165 || cgraph_function_body_availability (alias
) > AVAIL_OVERWRITABLE
)
2166 if (cgraph_for_node_thunks_and_aliases (alias
, callback
, data
,
2167 include_overwritable
))
2173 /* Call calback on NODE and aliases associated to NODE.
2174 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2178 cgraph_for_node_and_aliases (struct cgraph_node
*node
,
2179 bool (*callback
) (struct cgraph_node
*, void *),
2181 bool include_overwritable
)
2184 struct ipa_ref
*ref
;
2186 if (callback (node
, data
))
2188 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
, i
, ref
); i
++)
2189 if (ref
->use
== IPA_REF_ALIAS
)
2191 struct cgraph_node
*alias
= ipa_ref_referring_node (ref
);
2192 if (include_overwritable
2193 || cgraph_function_body_availability (alias
) > AVAIL_OVERWRITABLE
)
2194 if (cgraph_for_node_and_aliases (alias
, callback
, data
,
2195 include_overwritable
))
2201 /* Worker to bring NODE local. */
2204 cgraph_make_node_local_1 (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2206 gcc_checking_assert (cgraph_node_can_be_local_p (node
));
2207 if (DECL_COMDAT (node
->decl
) || DECL_EXTERNAL (node
->decl
))
2209 symtab_make_decl_local (node
->decl
);
2211 node
->externally_visible
= false;
2212 node
->forced_by_abi
= false;
2213 node
->local
.local
= true;
2214 node
->unique_name
= (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
2215 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
2216 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
2217 gcc_assert (cgraph_function_body_availability (node
) == AVAIL_LOCAL
);
2222 /* Bring NODE local. */
2225 cgraph_make_node_local (struct cgraph_node
*node
)
2227 cgraph_for_node_thunks_and_aliases (node
, cgraph_make_node_local_1
,
2231 /* Worker to set nothrow flag. */
2234 cgraph_set_nothrow_flag_1 (struct cgraph_node
*node
, void *data
)
2236 struct cgraph_edge
*e
;
2238 TREE_NOTHROW (node
->decl
) = data
!= NULL
;
2241 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2242 e
->can_throw_external
= false;
2246 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2247 if any to NOTHROW. */
2250 cgraph_set_nothrow_flag (struct cgraph_node
*node
, bool nothrow
)
2252 cgraph_for_node_thunks_and_aliases (node
, cgraph_set_nothrow_flag_1
,
2253 (void *)(size_t)nothrow
, false);
2256 /* Worker to set const flag. */
2259 cgraph_set_const_flag_1 (struct cgraph_node
*node
, void *data
)
2261 /* Static constructors and destructors without a side effect can be
2263 if (data
&& !((size_t)data
& 2))
2265 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2266 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2267 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2268 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2270 TREE_READONLY (node
->decl
) = data
!= NULL
;
2271 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = ((size_t)data
& 2) != 0;
2275 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2276 if any to READONLY. */
2279 cgraph_set_const_flag (struct cgraph_node
*node
, bool readonly
, bool looping
)
2281 cgraph_for_node_thunks_and_aliases (node
, cgraph_set_const_flag_1
,
2282 (void *)(size_t)(readonly
+ (int)looping
* 2),
2286 /* Worker to set pure flag. */
2289 cgraph_set_pure_flag_1 (struct cgraph_node
*node
, void *data
)
2291 /* Static constructors and destructors without a side effect can be
2293 if (data
&& !((size_t)data
& 2))
2295 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2296 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2297 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2298 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2300 DECL_PURE_P (node
->decl
) = data
!= NULL
;
2301 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = ((size_t)data
& 2) != 0;
2305 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2309 cgraph_set_pure_flag (struct cgraph_node
*node
, bool pure
, bool looping
)
2311 cgraph_for_node_thunks_and_aliases (node
, cgraph_set_pure_flag_1
,
2312 (void *)(size_t)(pure
+ (int)looping
* 2),
2316 /* Return true when NODE can not return or throw and thus
2317 it is safe to ignore its side effects for IPA analysis. */
2320 cgraph_node_cannot_return (struct cgraph_node
*node
)
2322 int flags
= flags_from_decl_or_type (node
->decl
);
2323 if (!flag_exceptions
)
2324 return (flags
& ECF_NORETURN
) != 0;
2326 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2327 == (ECF_NORETURN
| ECF_NOTHROW
));
2330 /* Return true when call of E can not lead to return from caller
2331 and thus it is safe to ignore its side effects for IPA analysis
2332 when computing side effects of the caller.
2333 FIXME: We could actually mark all edges that have no reaching
2334 patch to EXIT_BLOCK_PTR or throw to get better results. */
2336 cgraph_edge_cannot_lead_to_return (struct cgraph_edge
*e
)
2338 if (cgraph_node_cannot_return (e
->caller
))
2340 if (e
->indirect_unknown_callee
)
2342 int flags
= e
->indirect_info
->ecf_flags
;
2343 if (!flag_exceptions
)
2344 return (flags
& ECF_NORETURN
) != 0;
2346 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2347 == (ECF_NORETURN
| ECF_NOTHROW
));
2350 return cgraph_node_cannot_return (e
->callee
);
2353 /* Return true when function NODE can be removed from callgraph
2354 if all direct calls are eliminated. */
2357 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node
*node
)
2359 gcc_assert (!node
->global
.inlined_to
);
2360 /* Extern inlines can always go, we will use the external definition. */
2361 if (DECL_EXTERNAL (node
->decl
))
2363 /* When function is needed, we can not remove it. */
2364 if (node
->force_output
|| node
->used_from_other_partition
)
2366 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
2367 || DECL_STATIC_DESTRUCTOR (node
->decl
))
2369 /* Only COMDAT functions can be removed if externally visible. */
2370 if (node
->externally_visible
2371 && (!DECL_COMDAT (node
->decl
)
2372 || node
->forced_by_abi
2373 || symtab_used_from_object_file_p (node
)))
2378 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2381 nonremovable_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2383 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node
);
2386 /* Return true when function NODE and its aliases can be removed from callgraph
2387 if all direct calls are eliminated. */
2390 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node
*node
)
2392 /* Extern inlines can always go, we will use the external definition. */
2393 if (DECL_EXTERNAL (node
->decl
))
2395 if (node
->address_taken
)
2397 return !cgraph_for_node_and_aliases (node
, nonremovable_p
, NULL
, true);
2400 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2403 used_from_object_file_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2405 return symtab_used_from_object_file_p (node
);
2408 /* Return true when function NODE can be expected to be removed
2409 from program when direct calls in this compilation unit are removed.
2411 As a special case COMDAT functions are
2412 cgraph_can_remove_if_no_direct_calls_p while the are not
2413 cgraph_only_called_directly_p (it is possible they are called from other
2416 This function behaves as cgraph_only_called_directly_p because eliminating
2417 all uses of COMDAT function does not make it necessarily disappear from
2418 the program unless we are compiling whole program or we do LTO. In this
2419 case we know we win since dynamic linking will not really discard the
2420 linkonce section. */
2423 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node
*node
)
2425 gcc_assert (!node
->global
.inlined_to
);
2426 if (cgraph_for_node_and_aliases (node
, used_from_object_file_p
, NULL
, true))
2428 if (!in_lto_p
&& !flag_whole_program
)
2429 return cgraph_only_called_directly_p (node
);
2432 if (DECL_EXTERNAL (node
->decl
))
2434 return cgraph_can_remove_if_no_direct_calls_p (node
);
2439 /* Worker for cgraph_only_called_directly_p. */
2442 cgraph_not_only_called_directly_p_1 (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
2444 return !cgraph_only_called_directly_or_aliased_p (node
);
2447 /* Return true when function NODE and all its aliases are only called
2449 i.e. it is not externally visible, address was not taken and
2450 it is not used in any other non-standard way. */
2453 cgraph_only_called_directly_p (struct cgraph_node
*node
)
2455 gcc_assert (cgraph_function_or_thunk_node (node
, NULL
) == node
);
2456 return !cgraph_for_node_and_aliases (node
, cgraph_not_only_called_directly_p_1
,
2461 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2464 collect_callers_of_node_1 (struct cgraph_node
*node
, void *data
)
2466 vec
<cgraph_edge_p
> *redirect_callers
= (vec
<cgraph_edge_p
> *)data
;
2467 struct cgraph_edge
*cs
;
2468 enum availability avail
;
2469 cgraph_function_or_thunk_node (node
, &avail
);
2471 if (avail
> AVAIL_OVERWRITABLE
)
2472 for (cs
= node
->callers
; cs
!= NULL
; cs
= cs
->next_caller
)
2473 if (!cs
->indirect_inlining_edge
)
2474 redirect_callers
->safe_push (cs
);
2478 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2479 (i.e. are not overwritable). */
2482 collect_callers_of_node (struct cgraph_node
*node
)
2484 vec
<cgraph_edge_p
> redirect_callers
= vNULL
;
2485 cgraph_for_node_and_aliases (node
, collect_callers_of_node_1
,
2486 &redirect_callers
, false);
2487 return redirect_callers
;
2490 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2492 clone_of_p (struct cgraph_node
*node
, struct cgraph_node
*node2
)
2494 node
= cgraph_function_or_thunk_node (node
, NULL
);
2495 node2
= cgraph_function_or_thunk_node (node2
, NULL
);
2496 while (node
!= node2
&& node2
)
2497 node2
= node2
->clone_of
;
2498 return node2
!= NULL
;
2501 /* Verify edge E count and frequency. */
2504 verify_edge_count_and_frequency (struct cgraph_edge
*e
)
2506 bool error_found
= false;
2509 error ("caller edge count is negative");
2512 if (e
->frequency
< 0)
2514 error ("caller edge frequency is negative");
2517 if (e
->frequency
> CGRAPH_FREQ_MAX
)
2519 error ("caller edge frequency is too large");
2522 if (gimple_has_body_p (e
->caller
->decl
)
2523 && !e
->caller
->global
.inlined_to
2525 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2526 Remove this once edges are actually removed from the function at that time. */
2528 || (inline_edge_summary_vec
.exists ()
2529 && ((inline_edge_summary_vec
.length () <= (unsigned) e
->uid
)
2530 || !inline_edge_summary (e
)->predicate
)))
2532 != compute_call_stmt_bb_frequency (e
->caller
->decl
,
2533 gimple_bb (e
->call_stmt
))))
2535 error ("caller edge frequency %i does not match BB frequency %i",
2537 compute_call_stmt_bb_frequency (e
->caller
->decl
,
2538 gimple_bb (e
->call_stmt
)));
2544 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2546 cgraph_debug_gimple_stmt (struct function
*this_cfun
, gimple stmt
)
2548 bool fndecl_was_null
= false;
2549 /* debug_gimple_stmt needs correct cfun */
2550 if (cfun
!= this_cfun
)
2551 set_cfun (this_cfun
);
2552 /* ...and an actual current_function_decl */
2553 if (!current_function_decl
)
2555 current_function_decl
= this_cfun
->decl
;
2556 fndecl_was_null
= true;
2558 debug_gimple_stmt (stmt
);
2559 if (fndecl_was_null
)
2560 current_function_decl
= NULL
;
2563 /* Verify that call graph edge E corresponds to DECL from the associated
2564 statement. Return true if the verification should fail. */
2567 verify_edge_corresponds_to_fndecl (struct cgraph_edge
*e
, tree decl
)
2569 struct cgraph_node
*node
;
2571 if (!decl
|| e
->callee
->global
.inlined_to
)
2573 if (cgraph_state
== CGRAPH_LTO_STREAMING
)
2575 node
= cgraph_get_node (decl
);
2577 /* We do not know if a node from a different partition is an alias or what it
2578 aliases and therefore cannot do the former_clone_of check reliably. */
2579 if (!node
|| node
->in_other_partition
|| e
->callee
->in_other_partition
)
2581 node
= cgraph_function_or_thunk_node (node
, NULL
);
2583 if (e
->callee
->former_clone_of
!= node
->decl
2584 /* IPA-CP sometimes redirect edge to clone and then back to the former
2585 function. This ping-pong has to go, eventually. */
2586 && (node
!= cgraph_function_or_thunk_node (e
->callee
, NULL
))
2587 && !clone_of_p (cgraph_function_or_thunk_node (node
, NULL
), e
->callee
))
2593 /* Verify cgraph nodes of given cgraph node. */
2595 verify_cgraph_node (struct cgraph_node
*node
)
2597 struct cgraph_edge
*e
;
2598 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (node
->decl
);
2599 basic_block this_block
;
2600 gimple_stmt_iterator gsi
;
2601 bool error_found
= false;
2606 timevar_push (TV_CGRAPH_VERIFY
);
2607 error_found
|= verify_symtab_base (node
);
2608 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2611 error ("aux field set for edge %s->%s",
2612 identifier_to_locale (e
->caller
->name ()),
2613 identifier_to_locale (e
->callee
->name ()));
2616 if (node
->count
< 0)
2618 error ("execution count is negative");
2621 if (node
->global
.inlined_to
&& node
->same_comdat_group
)
2623 error ("inline clone in same comdat group list");
2626 if (!node
->definition
&& !node
->in_other_partition
&& node
->local
.local
)
2628 error ("local symbols must be defined");
2631 if (node
->global
.inlined_to
&& node
->externally_visible
)
2633 error ("externally visible inline clone");
2636 if (node
->global
.inlined_to
&& node
->address_taken
)
2638 error ("inline clone with address taken");
2641 if (node
->global
.inlined_to
&& node
->force_output
)
2643 error ("inline clone is forced to output");
2646 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2650 error ("aux field set for indirect edge from %s",
2651 identifier_to_locale (e
->caller
->name ()));
2654 if (!e
->indirect_unknown_callee
2655 || !e
->indirect_info
)
2657 error ("An indirect edge from %s is not marked as indirect or has "
2658 "associated indirect_info, the corresponding statement is: ",
2659 identifier_to_locale (e
->caller
->name ()));
2660 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2664 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2666 if (verify_edge_count_and_frequency (e
))
2668 if (!e
->inline_failed
)
2670 if (node
->global
.inlined_to
2671 != (e
->caller
->global
.inlined_to
2672 ? e
->caller
->global
.inlined_to
: e
->caller
))
2674 error ("inlined_to pointer is wrong");
2677 if (node
->callers
->next_caller
)
2679 error ("multiple inline callers");
2684 if (node
->global
.inlined_to
)
2686 error ("inlined_to pointer set for noninline callers");
2690 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2691 if (verify_edge_count_and_frequency (e
))
2693 if (!node
->callers
&& node
->global
.inlined_to
)
2695 error ("inlined_to pointer is set but no predecessors found");
2698 if (node
->global
.inlined_to
== node
)
2700 error ("inlined_to pointer refers to itself");
2706 struct cgraph_node
*n
;
2707 for (n
= node
->clone_of
->clones
; n
; n
= n
->next_sibling_clone
)
2712 error ("node has wrong clone_of");
2718 struct cgraph_node
*n
;
2719 for (n
= node
->clones
; n
; n
= n
->next_sibling_clone
)
2720 if (n
->clone_of
!= node
)
2724 error ("node has wrong clone list");
2728 if ((node
->prev_sibling_clone
|| node
->next_sibling_clone
) && !node
->clone_of
)
2730 error ("node is in clone list but it is not clone");
2733 if (!node
->prev_sibling_clone
&& node
->clone_of
&& node
->clone_of
->clones
!= node
)
2735 error ("node has wrong prev_clone pointer");
2738 if (node
->prev_sibling_clone
&& node
->prev_sibling_clone
->next_sibling_clone
!= node
)
2740 error ("double linked list of clones corrupted");
2744 if (node
->analyzed
&& node
->alias
)
2746 bool ref_found
= false;
2748 struct ipa_ref
*ref
;
2752 error ("Alias has call edges");
2755 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
,
2757 if (ref
->use
!= IPA_REF_ALIAS
)
2759 error ("Alias has non-alias reference");
2764 error ("Alias has more than one alias reference");
2771 error ("Analyzed alias has no reference");
2775 if (node
->analyzed
&& node
->thunk
.thunk_p
)
2779 error ("No edge out of thunk node");
2782 else if (node
->callees
->next_callee
)
2784 error ("More than one edge out of thunk node");
2787 if (gimple_has_body_p (node
->decl
))
2789 error ("Thunk is not supposed to have body");
2793 else if (node
->analyzed
&& gimple_has_body_p (node
->decl
)
2794 && !TREE_ASM_WRITTEN (node
->decl
)
2795 && (!DECL_EXTERNAL (node
->decl
) || node
->global
.inlined_to
)
2800 pointer_set_t
*stmts
= pointer_set_create ();
2802 struct ipa_ref
*ref
;
2804 /* Reach the trees by walking over the CFG, and note the
2805 enclosing basic-blocks in the call edges. */
2806 FOR_EACH_BB_FN (this_block
, this_cfun
)
2808 for (gsi
= gsi_start_phis (this_block
);
2809 !gsi_end_p (gsi
); gsi_next (&gsi
))
2810 pointer_set_insert (stmts
, gsi_stmt (gsi
));
2811 for (gsi
= gsi_start_bb (this_block
);
2815 gimple stmt
= gsi_stmt (gsi
);
2816 pointer_set_insert (stmts
, stmt
);
2817 if (is_gimple_call (stmt
))
2819 struct cgraph_edge
*e
= cgraph_edge (node
, stmt
);
2820 tree decl
= gimple_call_fndecl (stmt
);
2825 error ("shared call_stmt:");
2826 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
2829 if (!e
->indirect_unknown_callee
)
2831 if (verify_edge_corresponds_to_fndecl (e
, decl
))
2833 error ("edge points to wrong declaration:");
2834 debug_tree (e
->callee
->decl
);
2835 fprintf (stderr
," Instead of:");
2842 error ("an indirect edge with unknown callee "
2843 "corresponding to a call_stmt with "
2844 "a known declaration:");
2846 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2852 error ("missing callgraph edge for call stmt:");
2853 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
2860 ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
);
2862 if (ref
->stmt
&& !pointer_set_contains (stmts
, ref
->stmt
))
2864 error ("reference to dead statement");
2865 cgraph_debug_gimple_stmt (this_cfun
, ref
->stmt
);
2868 pointer_set_destroy (stmts
);
2871 /* No CFG available?! */
2874 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2878 error ("edge %s->%s has no corresponding call_stmt",
2879 identifier_to_locale (e
->caller
->name ()),
2880 identifier_to_locale (e
->callee
->name ()));
2881 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2886 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2888 if (!e
->aux
&& !e
->speculative
)
2890 error ("an indirect edge from %s has no corresponding call_stmt",
2891 identifier_to_locale (e
->caller
->name ()));
2892 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2900 dump_cgraph_node (stderr
, node
);
2901 internal_error ("verify_cgraph_node failed");
2903 timevar_pop (TV_CGRAPH_VERIFY
);
2906 /* Verify whole cgraph structure. */
2908 verify_cgraph (void)
2910 struct cgraph_node
*node
;
2915 FOR_EACH_FUNCTION (node
)
2916 verify_cgraph_node (node
);
2919 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2920 Walk through thunk, too.
2921 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2923 struct cgraph_node
*
2924 cgraph_function_node (struct cgraph_node
*node
, enum availability
*availability
)
2928 node
= cgraph_function_or_thunk_node (node
, availability
);
2929 if (node
->thunk
.thunk_p
)
2931 node
= node
->callees
->callee
;
2934 enum availability a
;
2935 a
= cgraph_function_body_availability (node
);
2936 if (a
< *availability
)
2939 node
= cgraph_function_or_thunk_node (node
, availability
);
2941 } while (node
&& node
->thunk
.thunk_p
);
2945 /* When doing LTO, read NODE's body from disk if it is not already present. */
2948 cgraph_get_body (struct cgraph_node
*node
)
2950 struct lto_file_decl_data
*file_data
;
2951 const char *data
, *name
;
2953 tree decl
= node
->decl
;
2955 if (DECL_RESULT (decl
))
2958 gcc_assert (in_lto_p
);
2960 file_data
= node
->lto_file_data
;
2961 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2963 /* We may have renamed the declaration, e.g., a static function. */
2964 name
= lto_get_decl_name_mapping (file_data
, name
);
2966 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
2970 dump_cgraph_node (stderr
, node
);
2971 fatal_error ("%s: section %s is missing",
2972 file_data
->file_name
,
2976 gcc_assert (DECL_STRUCT_FUNCTION (decl
) == NULL
);
2978 lto_input_function_body (file_data
, node
, data
);
2979 lto_stats
.num_function_bodies
++;
2980 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
2982 lto_free_function_in_decl_state_for_node (node
);
2986 /* Verify if the type of the argument matches that of the function
2987 declaration. If we cannot verify this or there is a mismatch,
2991 gimple_check_call_args (gimple stmt
, tree fndecl
, bool args_count_match
)
2994 unsigned int i
, nargs
;
2996 /* Calls to internal functions always match their signature. */
2997 if (gimple_call_internal_p (stmt
))
3000 nargs
= gimple_call_num_args (stmt
);
3002 /* Get argument types for verification. */
3004 parms
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3006 parms
= TYPE_ARG_TYPES (gimple_call_fntype (stmt
));
3008 /* Verify if the type of the argument matches that of the function
3009 declaration. If we cannot verify this or there is a mismatch,
3011 if (fndecl
&& DECL_ARGUMENTS (fndecl
))
3013 for (i
= 0, p
= DECL_ARGUMENTS (fndecl
);
3015 i
++, p
= DECL_CHAIN (p
))
3018 /* We cannot distinguish a varargs function from the case
3019 of excess parameters, still deferring the inlining decision
3020 to the callee is possible. */
3023 arg
= gimple_call_arg (stmt
, i
);
3024 if (p
== error_mark_node
3025 || arg
== error_mark_node
3026 || (!types_compatible_p (DECL_ARG_TYPE (p
), TREE_TYPE (arg
))
3027 && !fold_convertible_p (DECL_ARG_TYPE (p
), arg
)))
3030 if (args_count_match
&& p
)
3035 for (i
= 0, p
= parms
; i
< nargs
; i
++, p
= TREE_CHAIN (p
))
3038 /* If this is a varargs function defer inlining decision
3042 arg
= gimple_call_arg (stmt
, i
);
3043 if (TREE_VALUE (p
) == error_mark_node
3044 || arg
== error_mark_node
3045 || TREE_CODE (TREE_VALUE (p
)) == VOID_TYPE
3046 || (!types_compatible_p (TREE_VALUE (p
), TREE_TYPE (arg
))
3047 && !fold_convertible_p (TREE_VALUE (p
), arg
)))
3059 /* Verify if the type of the argument and lhs of CALL_STMT matches
3060 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3061 true, the arg count needs to be the same.
3062 If we cannot verify this or there is a mismatch, return false. */
3065 gimple_check_call_matching_types (gimple call_stmt
, tree callee
,
3066 bool args_count_match
)
3070 if ((DECL_RESULT (callee
)
3071 && !DECL_BY_REFERENCE (DECL_RESULT (callee
))
3072 && (lhs
= gimple_call_lhs (call_stmt
)) != NULL_TREE
3073 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee
)),
3075 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee
)), lhs
))
3076 || !gimple_check_call_args (call_stmt
, callee
, args_count_match
))
3081 #include "gt-cgraph.h"