1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011, 2012 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains basic routines manipulating call graph
24 The call-graph is a data structure designed for intra-procedural optimization.
25 It represents a multi-graph where nodes are functions and edges are call sites. */
29 #include "coretypes.h"
32 #include "tree-inline.h"
33 #include "langhooks.h"
40 #include "basic-block.h"
45 #include "tree-dump.h"
46 #include "tree-flow.h"
47 #include "value-prof.h"
49 #include "diagnostic-core.h"
51 #include "ipa-utils.h"
52 #include "lto-streamer.h"
53 #include "ipa-inline.h"
55 #include "gimple-pretty-print.h"
57 static void cgraph_node_remove_callers (struct cgraph_node *node);
58 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
59 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
61 /* Queue of cgraph nodes scheduled to be lowered. */
62 symtab_node x_cgraph_nodes_queue;
63 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
65 /* Number of nodes in existence. */
68 /* Maximal uid used in cgraph nodes. */
71 /* Maximal uid used in cgraph edges. */
72 int cgraph_edge_max_uid;
74 /* Set when whole unit has been analyzed so we can access global info. */
75 bool cgraph_global_info_ready = false;
77 /* What state callgraph is in right now. */
78 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
80 /* Set when the cgraph is fully build and the basic flags are computed. */
81 bool cgraph_function_flags_ready = false;
83 /* List of hooks triggered on cgraph_edge events. */
84 struct cgraph_edge_hook_list {
85 cgraph_edge_hook hook;
87 struct cgraph_edge_hook_list *next;
90 /* List of hooks triggered on cgraph_node events. */
91 struct cgraph_node_hook_list {
92 cgraph_node_hook hook;
94 struct cgraph_node_hook_list *next;
97 /* List of hooks triggered on events involving two cgraph_edges. */
98 struct cgraph_2edge_hook_list {
99 cgraph_2edge_hook hook;
101 struct cgraph_2edge_hook_list *next;
104 /* List of hooks triggered on events involving two cgraph_nodes. */
105 struct cgraph_2node_hook_list {
106 cgraph_2node_hook hook;
108 struct cgraph_2node_hook_list *next;
111 /* List of hooks triggered when an edge is removed. */
112 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
113 /* List of hooks triggered when a node is removed. */
114 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
115 /* List of hooks triggered when an edge is duplicated. */
116 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
117 /* List of hooks triggered when a node is duplicated. */
118 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
119 /* List of hooks triggered when an function is inserted. */
120 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
122 /* Head of a linked list of unused (freed) call graph nodes.
123 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
124 static GTY(()) struct cgraph_node *free_nodes;
125 /* Head of a linked list of unused (freed) call graph edges.
126 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
127 static GTY(()) struct cgraph_edge *free_edges;
129 /* Did procss_same_body_aliases run? */
130 bool same_body_aliases_done;
132 /* Macros to access the next item in the list of free cgraph nodes and
134 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
135 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
136 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
138 /* Register HOOK to be called with DATA on each removed edge. */
139 struct cgraph_edge_hook_list *
140 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
142 struct cgraph_edge_hook_list *entry;
143 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
145 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
155 /* Remove ENTRY from the list of hooks called on removing edges. */
157 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
159 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
161 while (*ptr != entry)
167 /* Call all edge removal hooks. */
169 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
171 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
174 entry->hook (e, entry->data);
179 /* Register HOOK to be called with DATA on each removed node. */
180 struct cgraph_node_hook_list *
181 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
183 struct cgraph_node_hook_list *entry;
184 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
186 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
196 /* Remove ENTRY from the list of hooks called on removing nodes. */
198 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
200 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
202 while (*ptr != entry)
208 /* Call all node removal hooks. */
210 cgraph_call_node_removal_hooks (struct cgraph_node *node)
212 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
215 entry->hook (node, entry->data);
220 /* Register HOOK to be called with DATA on each inserted node. */
221 struct cgraph_node_hook_list *
222 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
224 struct cgraph_node_hook_list *entry;
225 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
227 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
237 /* Remove ENTRY from the list of hooks called on inserted nodes. */
239 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
241 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
243 while (*ptr != entry)
249 /* Call all node insertion hooks. */
251 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
253 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
256 entry->hook (node, entry->data);
261 /* Register HOOK to be called with DATA on each duplicated edge. */
262 struct cgraph_2edge_hook_list *
263 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
265 struct cgraph_2edge_hook_list *entry;
266 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
268 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
278 /* Remove ENTRY from the list of hooks called on duplicating edges. */
280 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
282 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
284 while (*ptr != entry)
290 /* Call all edge duplication hooks. */
292 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
293 struct cgraph_edge *cs2)
295 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
298 entry->hook (cs1, cs2, entry->data);
303 /* Register HOOK to be called with DATA on each duplicated node. */
304 struct cgraph_2node_hook_list *
305 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
307 struct cgraph_2node_hook_list *entry;
308 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
310 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
320 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
322 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
324 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
326 while (*ptr != entry)
332 /* Call all node duplication hooks. */
334 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
335 struct cgraph_node *node2)
337 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
340 entry->hook (node1, node2, entry->data);
345 /* Allocate new callgraph node. */
347 static inline struct cgraph_node *
348 cgraph_allocate_node (void)
350 struct cgraph_node *node;
355 free_nodes = NEXT_FREE_NODE (node);
359 node = ggc_alloc_cleared_cgraph_node ();
360 node->uid = cgraph_max_uid++;
366 /* Allocate new callgraph node and insert it into basic data structures. */
369 cgraph_create_empty_node (void)
371 struct cgraph_node *node = cgraph_allocate_node ();
373 node->symbol.type = SYMTAB_FUNCTION;
374 node->frequency = NODE_FREQUENCY_NORMAL;
375 node->count_materialization_scale = REG_BR_PROB_BASE;
380 /* Return cgraph node assigned to DECL. Create new one when needed. */
383 cgraph_create_node (tree decl)
385 struct cgraph_node *node = cgraph_create_empty_node ();
386 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
388 node->symbol.decl = decl;
389 symtab_register_node ((symtab_node) node);
391 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
393 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
394 node->next_nested = node->origin->nested;
395 node->origin->nested = node;
400 /* Try to find a call graph node for declaration DECL and if it does not exist,
404 cgraph_get_create_node (tree decl)
406 struct cgraph_node *node;
408 node = cgraph_get_node (decl);
412 return cgraph_create_node (decl);
415 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
416 the function body is associated with (not neccesarily cgraph_node (DECL). */
419 cgraph_create_function_alias (tree alias, tree decl)
421 struct cgraph_node *alias_node;
423 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
424 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
425 alias_node = cgraph_get_create_node (alias);
426 gcc_assert (!alias_node->local.finalized);
427 alias_node->thunk.alias = decl;
428 alias_node->local.finalized = true;
429 alias_node->alias = 1;
433 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
435 Same body aliases are output whenever the body of DECL is output,
436 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
439 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
441 struct cgraph_node *n;
442 #ifndef ASM_OUTPUT_DEF
443 /* If aliases aren't supported by the assembler, fail. */
446 /* Langhooks can create same body aliases of symbols not defined.
447 Those are useless. Drop them on the floor. */
448 if (cgraph_global_info_ready)
451 n = cgraph_create_function_alias (alias, decl);
452 n->same_body_alias = true;
453 if (same_body_aliases_done)
454 ipa_record_reference ((symtab_node)n, (symtab_node)cgraph_get_node (decl),
455 IPA_REF_ALIAS, NULL);
459 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
460 aliases DECL with an adjustments made into the first parameter.
461 See comments in thunk_adjust for detail on the parameters. */
464 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
465 tree alias, tree decl ATTRIBUTE_UNUSED,
467 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
471 struct cgraph_node *node;
473 node = cgraph_get_node (alias);
476 gcc_assert (node->local.finalized);
477 gcc_assert (!node->alias);
478 gcc_assert (!node->thunk.thunk_p);
479 cgraph_remove_node (node);
482 node = cgraph_create_node (alias);
483 gcc_checking_assert (!virtual_offset
484 || double_int_equal_p
485 (tree_to_double_int (virtual_offset),
486 shwi_to_double_int (virtual_value)));
487 node->thunk.fixed_offset = fixed_offset;
488 node->thunk.this_adjusting = this_adjusting;
489 node->thunk.virtual_value = virtual_value;
490 node->thunk.virtual_offset_p = virtual_offset != NULL;
491 node->thunk.alias = real_alias;
492 node->thunk.thunk_p = true;
493 node->local.finalized = true;
498 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
499 Return NULL if there's no such node. */
502 cgraph_node_for_asm (tree asmname)
504 symtab_node node = symtab_node_for_asm (asmname);
506 /* We do not want to look at inline clones. */
507 for (node = symtab_node_for_asm (asmname); node; node = node->symbol.next_sharing_asm_name)
508 if (symtab_function_p (node) && !cgraph(node)->global.inlined_to)
509 return cgraph (node);
513 /* Returns a hash value for X (which really is a die_struct). */
516 edge_hash (const void *x)
518 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
521 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
524 edge_eq (const void *x, const void *y)
526 return ((const struct cgraph_edge *) x)->call_stmt == y;
529 /* Add call graph edge E to call site hash of its caller. */
532 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
535 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
537 htab_hash_pointer (e->call_stmt),
543 /* Return the callgraph edge representing the GIMPLE_CALL statement
547 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
549 struct cgraph_edge *e, *e2;
552 if (node->call_site_hash)
553 return (struct cgraph_edge *)
554 htab_find_with_hash (node->call_site_hash, call_stmt,
555 htab_hash_pointer (call_stmt));
557 /* This loop may turn out to be performance problem. In such case adding
558 hashtables into call nodes with very many edges is probably best
559 solution. It is not good idea to add pointer into CALL_EXPR itself
560 because we want to make possible having multiple cgraph nodes representing
561 different clones of the same body before the body is actually cloned. */
562 for (e = node->callees; e; e = e->next_callee)
564 if (e->call_stmt == call_stmt)
570 for (e = node->indirect_calls; e; e = e->next_callee)
572 if (e->call_stmt == call_stmt)
579 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
580 for (e2 = node->callees; e2; e2 = e2->next_callee)
581 cgraph_add_edge_to_call_site_hash (e2);
582 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
583 cgraph_add_edge_to_call_site_hash (e2);
590 /* Change field call_stmt of edge E to NEW_STMT. */
593 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
597 if (e->caller->call_site_hash)
599 htab_remove_elt_with_hash (e->caller->call_site_hash,
601 htab_hash_pointer (e->call_stmt));
604 e->call_stmt = new_stmt;
605 if (e->indirect_unknown_callee
606 && (decl = gimple_call_fndecl (new_stmt)))
608 /* Constant propagation (and possibly also inlining?) can turn an
609 indirect call into a direct one. */
610 struct cgraph_node *new_callee = cgraph_get_node (decl);
612 gcc_checking_assert (new_callee);
613 cgraph_make_edge_direct (e, new_callee);
616 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
617 e->can_throw_external = stmt_can_throw_external (new_stmt);
619 if (e->caller->call_site_hash)
620 cgraph_add_edge_to_call_site_hash (e);
623 /* Allocate a cgraph_edge structure and fill it with data according to the
624 parameters of which only CALLEE can be NULL (when creating an indirect call
627 static struct cgraph_edge *
628 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
629 gimple call_stmt, gcov_type count, int freq)
631 struct cgraph_edge *edge;
633 /* LTO does not actually have access to the call_stmt since these
634 have not been loaded yet. */
637 /* This is a rather expensive check possibly triggering
638 construction of call stmt hashtable. */
639 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
641 gcc_assert (is_gimple_call (call_stmt));
647 free_edges = NEXT_FREE_EDGE (edge);
651 edge = ggc_alloc_cgraph_edge ();
652 edge->uid = cgraph_edge_max_uid++;
656 edge->caller = caller;
657 edge->callee = callee;
658 edge->prev_caller = NULL;
659 edge->next_caller = NULL;
660 edge->prev_callee = NULL;
661 edge->next_callee = NULL;
664 gcc_assert (count >= 0);
665 edge->frequency = freq;
666 gcc_assert (freq >= 0);
667 gcc_assert (freq <= CGRAPH_FREQ_MAX);
669 edge->call_stmt = call_stmt;
670 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
671 edge->can_throw_external
672 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
675 && callee && callee->symbol.decl
676 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl))
677 edge->call_stmt_cannot_inline_p = true;
679 edge->call_stmt_cannot_inline_p = false;
680 if (call_stmt && caller->call_site_hash)
681 cgraph_add_edge_to_call_site_hash (edge);
683 edge->indirect_info = NULL;
684 edge->indirect_inlining_edge = 0;
689 /* Create edge from CALLER to CALLEE in the cgraph. */
692 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
693 gimple call_stmt, gcov_type count, int freq)
695 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
698 edge->indirect_unknown_callee = 0;
699 initialize_inline_failed (edge);
701 edge->next_caller = callee->callers;
703 callee->callers->prev_caller = edge;
704 edge->next_callee = caller->callees;
706 caller->callees->prev_callee = edge;
707 caller->callees = edge;
708 callee->callers = edge;
713 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
715 struct cgraph_indirect_call_info *
716 cgraph_allocate_init_indirect_info (void)
718 struct cgraph_indirect_call_info *ii;
720 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
721 ii->param_index = -1;
725 /* Create an indirect edge with a yet-undetermined callee where the call
726 statement destination is a formal parameter of the caller with index
730 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
732 gcov_type count, int freq)
734 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
737 edge->indirect_unknown_callee = 1;
738 initialize_inline_failed (edge);
740 edge->indirect_info = cgraph_allocate_init_indirect_info ();
741 edge->indirect_info->ecf_flags = ecf_flags;
743 edge->next_callee = caller->indirect_calls;
744 if (caller->indirect_calls)
745 caller->indirect_calls->prev_callee = edge;
746 caller->indirect_calls = edge;
751 /* Remove the edge E from the list of the callers of the callee. */
754 cgraph_edge_remove_callee (struct cgraph_edge *e)
756 gcc_assert (!e->indirect_unknown_callee);
758 e->prev_caller->next_caller = e->next_caller;
760 e->next_caller->prev_caller = e->prev_caller;
762 e->callee->callers = e->next_caller;
765 /* Remove the edge E from the list of the callees of the caller. */
768 cgraph_edge_remove_caller (struct cgraph_edge *e)
771 e->prev_callee->next_callee = e->next_callee;
773 e->next_callee->prev_callee = e->prev_callee;
776 if (e->indirect_unknown_callee)
777 e->caller->indirect_calls = e->next_callee;
779 e->caller->callees = e->next_callee;
781 if (e->caller->call_site_hash)
782 htab_remove_elt_with_hash (e->caller->call_site_hash,
784 htab_hash_pointer (e->call_stmt));
787 /* Put the edge onto the free list. */
790 cgraph_free_edge (struct cgraph_edge *e)
794 /* Clear out the edge so we do not dangle pointers. */
795 memset (e, 0, sizeof (*e));
797 NEXT_FREE_EDGE (e) = free_edges;
801 /* Remove the edge E in the cgraph. */
804 cgraph_remove_edge (struct cgraph_edge *e)
806 /* Call all edge removal hooks. */
807 cgraph_call_edge_removal_hooks (e);
809 if (!e->indirect_unknown_callee)
810 /* Remove from callers list of the callee. */
811 cgraph_edge_remove_callee (e);
813 /* Remove from callees list of the callers. */
814 cgraph_edge_remove_caller (e);
816 /* Put the edge onto the free list. */
817 cgraph_free_edge (e);
820 /* Set callee of call graph edge E and add it to the corresponding set of
824 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
826 e->prev_caller = NULL;
828 n->callers->prev_caller = e;
829 e->next_caller = n->callers;
834 /* Redirect callee of E to N. The function does not update underlying
838 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
840 /* Remove from callers list of the current callee. */
841 cgraph_edge_remove_callee (e);
843 /* Insert to callers list of the new callee. */
844 cgraph_set_edge_callee (e, n);
847 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
848 CALLEE. DELTA is an integer constant that is to be added to the this
849 pointer (first parameter) to compensate for skipping a thunk adjustment. */
852 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
854 edge->indirect_unknown_callee = 0;
856 /* Get the edge out of the indirect edge list. */
857 if (edge->prev_callee)
858 edge->prev_callee->next_callee = edge->next_callee;
859 if (edge->next_callee)
860 edge->next_callee->prev_callee = edge->prev_callee;
861 if (!edge->prev_callee)
862 edge->caller->indirect_calls = edge->next_callee;
864 /* Put it into the normal callee list */
865 edge->prev_callee = NULL;
866 edge->next_callee = edge->caller->callees;
867 if (edge->caller->callees)
868 edge->caller->callees->prev_callee = edge;
869 edge->caller->callees = edge;
871 /* Insert to callers list of the new callee. */
872 cgraph_set_edge_callee (edge, callee);
875 edge->call_stmt_cannot_inline_p
876 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl);
878 /* We need to re-determine the inlining status of the edge. */
879 initialize_inline_failed (edge);
882 /* If necessary, change the function declaration in the call statement
883 associated with E so that it corresponds to the edge callee. */
886 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
888 tree decl = gimple_call_fndecl (e->call_stmt);
890 gimple_stmt_iterator gsi;
891 #ifdef ENABLE_CHECKING
892 struct cgraph_node *node;
895 if (e->indirect_unknown_callee
896 || decl == e->callee->symbol.decl)
899 #ifdef ENABLE_CHECKING
902 node = cgraph_get_node (decl);
903 gcc_assert (!node || !node->clone.combined_args_to_skip);
907 if (cgraph_dump_file)
909 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
910 xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
911 xstrdup (cgraph_node_name (e->callee)), e->callee->uid);
912 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
913 if (e->callee->clone.combined_args_to_skip)
915 fprintf (cgraph_dump_file, " combined args to skip: ");
916 dump_bitmap (cgraph_dump_file,
917 e->callee->clone.combined_args_to_skip);
921 if (e->callee->clone.combined_args_to_skip)
926 = gimple_call_copy_skip_args (e->call_stmt,
927 e->callee->clone.combined_args_to_skip);
928 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
930 if (gimple_vdef (new_stmt)
931 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
932 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
934 gsi = gsi_for_stmt (e->call_stmt);
935 gsi_replace (&gsi, new_stmt, false);
936 /* We need to defer cleaning EH info on the new statement to
937 fixup-cfg. We may not have dominator information at this point
938 and thus would end up with unreachable blocks and have no way
939 to communicate that we need to run CFG cleanup then. */
940 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
943 remove_stmt_from_eh_lp (e->call_stmt);
944 add_stmt_to_eh_lp (new_stmt, lp_nr);
949 new_stmt = e->call_stmt;
950 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
951 update_stmt (new_stmt);
954 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
956 if (cgraph_dump_file)
958 fprintf (cgraph_dump_file, " updated to:");
959 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
964 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
965 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
966 of OLD_STMT if it was previously call statement.
967 If NEW_STMT is NULL, the call has been dropped without any
971 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
972 gimple old_stmt, tree old_call,
975 tree new_call = (new_stmt && is_gimple_call (new_stmt))
976 ? gimple_call_fndecl (new_stmt) : 0;
978 /* We are seeing indirect calls, then there is nothing to update. */
979 if (!new_call && !old_call)
981 /* See if we turned indirect call into direct call or folded call to one builtin
982 into different builtin. */
983 if (old_call != new_call)
985 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
986 struct cgraph_edge *ne = NULL;
992 /* See if the edge is already there and has the correct callee. It
993 might be so because of indirect inlining has already updated
994 it. We also might've cloned and redirected the edge. */
995 if (new_call && e->callee)
997 struct cgraph_node *callee = e->callee;
1000 if (callee->symbol.decl == new_call
1001 || callee->former_clone_of == new_call)
1003 callee = callee->clone_of;
1007 /* Otherwise remove edge and create new one; we can't simply redirect
1008 since function has changed, so inline plan and other information
1009 attached to edge is invalid. */
1011 frequency = e->frequency;
1012 cgraph_remove_edge (e);
1016 /* We are seeing new direct call; compute profile info based on BB. */
1017 basic_block bb = gimple_bb (new_stmt);
1019 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1025 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1026 new_stmt, count, frequency);
1027 gcc_assert (ne->inline_failed);
1030 /* We only updated the call stmt; update pointer in cgraph edge.. */
1031 else if (old_stmt != new_stmt)
1032 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1035 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1036 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1037 of OLD_STMT before it was updated (updating can happen inplace). */
1040 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1042 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1043 struct cgraph_node *node;
1045 gcc_checking_assert (orig);
1046 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1048 for (node = orig->clones; node != orig;)
1050 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1052 node = node->clones;
1053 else if (node->next_sibling_clone)
1054 node = node->next_sibling_clone;
1057 while (node != orig && !node->next_sibling_clone)
1058 node = node->clone_of;
1060 node = node->next_sibling_clone;
1066 /* Remove all callees from the node. */
1069 cgraph_node_remove_callees (struct cgraph_node *node)
1071 struct cgraph_edge *e, *f;
1073 /* It is sufficient to remove the edges from the lists of callers of
1074 the callees. The callee list of the node can be zapped with one
1076 for (e = node->callees; e; e = f)
1079 cgraph_call_edge_removal_hooks (e);
1080 if (!e->indirect_unknown_callee)
1081 cgraph_edge_remove_callee (e);
1082 cgraph_free_edge (e);
1084 for (e = node->indirect_calls; e; e = f)
1087 cgraph_call_edge_removal_hooks (e);
1088 if (!e->indirect_unknown_callee)
1089 cgraph_edge_remove_callee (e);
1090 cgraph_free_edge (e);
1092 node->indirect_calls = NULL;
1093 node->callees = NULL;
1094 if (node->call_site_hash)
1096 htab_delete (node->call_site_hash);
1097 node->call_site_hash = NULL;
1101 /* Remove all callers from the node. */
1104 cgraph_node_remove_callers (struct cgraph_node *node)
1106 struct cgraph_edge *e, *f;
1108 /* It is sufficient to remove the edges from the lists of callees of
1109 the callers. The caller list of the node can be zapped with one
1111 for (e = node->callers; e; e = f)
1114 cgraph_call_edge_removal_hooks (e);
1115 cgraph_edge_remove_caller (e);
1116 cgraph_free_edge (e);
1118 node->callers = NULL;
1121 /* Release memory used to represent body of function NODE. */
1124 cgraph_release_function_body (struct cgraph_node *node)
1126 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1128 tree old_decl = current_function_decl;
1129 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1133 cfun->curr_properties &= ~PROP_loops;
1134 loop_optimizer_finalize ();
1136 if (cfun->gimple_df)
1138 current_function_decl = node->symbol.decl;
1140 delete_tree_cfg_annotations ();
1142 current_function_decl = old_decl;
1146 gcc_assert (dom_computed[0] == DOM_NONE);
1147 gcc_assert (dom_computed[1] == DOM_NONE);
1150 if (cfun->value_histograms)
1153 gimple_set_body (node->symbol.decl, NULL);
1154 VEC_free (ipa_opt_pass, heap,
1155 node->ipa_transforms_to_apply);
1156 /* Struct function hangs a lot of data that would leak if we didn't
1157 removed all pointers to it. */
1158 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1159 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1161 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1162 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1163 of its associated function function declaration because it's
1164 needed to emit debug info later. */
1165 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1166 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1169 /* Remove the node from cgraph. */
1172 cgraph_remove_node (struct cgraph_node *node)
1174 struct cgraph_node *n;
1175 int uid = node->uid;
1177 cgraph_call_node_removal_hooks (node);
1178 cgraph_node_remove_callers (node);
1179 cgraph_node_remove_callees (node);
1180 VEC_free (ipa_opt_pass, heap,
1181 node->ipa_transforms_to_apply);
1183 /* Incremental inlining access removed nodes stored in the postorder list.
1185 node->symbol.force_output = false;
1186 for (n = node->nested; n; n = n->next_nested)
1188 node->nested = NULL;
1191 struct cgraph_node **node2 = &node->origin->nested;
1193 while (*node2 != node)
1194 node2 = &(*node2)->next_nested;
1195 *node2 = node->next_nested;
1197 symtab_unregister_node ((symtab_node)node);
1198 if (node->prev_sibling_clone)
1199 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1200 else if (node->clone_of)
1201 node->clone_of->clones = node->next_sibling_clone;
1202 if (node->next_sibling_clone)
1203 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1206 struct cgraph_node *n, *next;
1210 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1211 n->clone_of = node->clone_of;
1212 n->clone_of = node->clone_of;
1213 n->next_sibling_clone = node->clone_of->clones;
1214 if (node->clone_of->clones)
1215 node->clone_of->clones->prev_sibling_clone = n;
1216 node->clone_of->clones = node->clones;
1220 /* We are removing node with clones. This makes clones inconsistent,
1221 but assume they will be removed subsequently and just keep clone
1222 tree intact. This can happen in unreachable function removal since
1223 we remove unreachable functions in random order, not by bottom-up
1224 walk of clone trees. */
1225 for (n = node->clones; n; n = next)
1227 next = n->next_sibling_clone;
1228 n->next_sibling_clone = NULL;
1229 n->prev_sibling_clone = NULL;
1235 /* While all the clones are removed after being proceeded, the function
1236 itself is kept in the cgraph even after it is compiled. Check whether
1237 we are done with this body and reclaim it proactively if this is the case.
1239 n = cgraph_get_node (node->symbol.decl);
1241 || (!n->clones && !n->clone_of && !n->global.inlined_to
1242 && (cgraph_global_info_ready
1243 && (TREE_ASM_WRITTEN (n->symbol.decl)
1244 || DECL_EXTERNAL (n->symbol.decl)
1246 || n->symbol.in_other_partition))))
1247 cgraph_release_function_body (node);
1249 node->symbol.decl = NULL;
1250 if (node->call_site_hash)
1252 htab_delete (node->call_site_hash);
1253 node->call_site_hash = NULL;
1257 /* Clear out the node to NULL all pointers and add the node to the free
1259 memset (node, 0, sizeof(*node));
1260 node->symbol.type = SYMTAB_FUNCTION;
1262 SET_NEXT_FREE_NODE (node, free_nodes);
1266 /* Likewise indicate that a node is having address taken. */
1269 cgraph_mark_address_taken_node (struct cgraph_node *node)
1271 gcc_assert (!node->global.inlined_to);
1272 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1273 IPA_REF_ADDR reference exists (and thus it should be set on node
1274 representing alias we take address of) and as a test whether address
1275 of the object was taken (and thus it should be set on node alias is
1276 referring to). We should remove the first use and the remove the
1278 node->symbol.address_taken = 1;
1279 node = cgraph_function_or_thunk_node (node, NULL);
1280 node->symbol.address_taken = 1;
1283 /* Return local info for the compiled function. */
1285 struct cgraph_local_info *
1286 cgraph_local_info (tree decl)
1288 struct cgraph_node *node;
1290 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1291 node = cgraph_get_node (decl);
1294 return &node->local;
1297 /* Return local info for the compiled function. */
1299 struct cgraph_global_info *
1300 cgraph_global_info (tree decl)
1302 struct cgraph_node *node;
1304 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1305 node = cgraph_get_node (decl);
1308 return &node->global;
1311 /* Return local info for the compiled function. */
1313 struct cgraph_rtl_info *
1314 cgraph_rtl_info (tree decl)
1316 struct cgraph_node *node;
1318 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1319 node = cgraph_get_node (decl);
1321 || (decl != current_function_decl
1322 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1327 /* Return a string describing the failure REASON. */
1330 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1333 #define DEFCIFCODE(code, string) string,
1335 static const char *cif_string_table[CIF_N_REASONS] = {
1336 #include "cif-code.def"
1339 /* Signedness of an enum type is implementation defined, so cast it
1340 to unsigned before testing. */
1341 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1342 return cif_string_table[reason];
1345 /* Names used to print out the availability enum. */
1346 const char * const cgraph_availability_names[] =
1347 {"unset", "not_available", "overwritable", "available", "local"};
1350 /* Dump call graph node NODE to file F. */
1353 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1355 struct cgraph_edge *edge;
1356 int indirect_calls_count = 0;
1358 dump_symtab_base (f, (symtab_node) node);
1360 if (node->global.inlined_to)
1361 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1362 xstrdup (cgraph_node_name (node)),
1364 xstrdup (cgraph_node_name (node->global.inlined_to)),
1365 node->global.inlined_to->symbol.order);
1367 fprintf (f, " Clone of %s/%i\n",
1368 cgraph_node_asm_name (node->clone_of),
1369 node->clone_of->symbol.order);
1370 if (cgraph_function_flags_ready)
1371 fprintf (f, " Availability: %s\n",
1372 cgraph_availability_names [cgraph_function_body_availability (node)]);
1374 fprintf (f, " Function flags:");
1376 fprintf (f, " analyzed");
1378 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1379 (HOST_WIDEST_INT)node->count);
1381 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1382 if (gimple_has_body_p (node->symbol.decl))
1383 fprintf (f, " body");
1385 fprintf (f, " process");
1386 if (node->local.local)
1387 fprintf (f, " local");
1388 if (node->local.finalized)
1389 fprintf (f, " finalized");
1390 if (node->local.redefined_extern_inline)
1391 fprintf (f, " redefined_extern_inline");
1392 if (node->only_called_at_startup)
1393 fprintf (f, " only_called_at_startup");
1394 if (node->only_called_at_exit)
1395 fprintf (f, " only_called_at_exit");
1396 else if (node->alias)
1397 fprintf (f, " alias");
1399 fprintf (f, " tm_clone");
1403 if (node->thunk.thunk_p)
1405 fprintf (f, " Thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1406 "virtual offset %i)\n",
1407 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1408 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1409 (int)node->thunk.fixed_offset,
1410 (int)node->thunk.virtual_value,
1411 (int)node->thunk.virtual_offset_p);
1413 if (node->alias && node->thunk.alias)
1415 fprintf (f, " Alias of %s",
1416 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1417 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1418 fprintf (f, " (asm: %s)",
1419 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1423 fprintf (f, " Called by: ");
1425 for (edge = node->callers; edge; edge = edge->next_caller)
1427 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1428 edge->caller->symbol.order);
1430 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1431 (HOST_WIDEST_INT)edge->count);
1432 if (edge->frequency)
1433 fprintf (f, "(%.2f per call) ",
1434 edge->frequency / (double)CGRAPH_FREQ_BASE);
1435 if (!edge->inline_failed)
1436 fprintf(f, "(inlined) ");
1437 if (edge->indirect_inlining_edge)
1438 fprintf(f, "(indirect_inlining) ");
1439 if (edge->can_throw_external)
1440 fprintf(f, "(can throw external) ");
1443 fprintf (f, "\n Calls: ");
1444 for (edge = node->callees; edge; edge = edge->next_callee)
1446 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1447 edge->callee->symbol.order);
1448 if (!edge->inline_failed)
1449 fprintf(f, "(inlined) ");
1450 if (edge->indirect_inlining_edge)
1451 fprintf(f, "(indirect_inlining) ");
1453 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1454 (HOST_WIDEST_INT)edge->count);
1455 if (edge->frequency)
1456 fprintf (f, "(%.2f per call) ",
1457 edge->frequency / (double)CGRAPH_FREQ_BASE);
1458 if (edge->can_throw_external)
1459 fprintf(f, "(can throw external) ");
1463 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1464 indirect_calls_count++;
1465 if (indirect_calls_count)
1466 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1467 indirect_calls_count);
1471 /* Dump call graph node NODE to stderr. */
1474 debug_cgraph_node (struct cgraph_node *node)
1476 dump_cgraph_node (stderr, node);
1480 /* Dump the callgraph to file F. */
1483 dump_cgraph (FILE *f)
1485 struct cgraph_node *node;
1487 fprintf (f, "callgraph:\n\n");
1488 FOR_EACH_FUNCTION (node)
1489 dump_cgraph_node (f, node);
1493 /* Dump the call graph to stderr. */
1498 dump_cgraph (stderr);
1501 /* Return true when the DECL can possibly be inlined. */
1503 cgraph_function_possibly_inlined_p (tree decl)
1505 if (!cgraph_global_info_ready)
1506 return !DECL_UNINLINABLE (decl);
1507 return DECL_POSSIBLY_INLINED (decl);
1510 /* NODE is no longer nested function; update cgraph accordingly. */
1512 cgraph_unnest_node (struct cgraph_node *node)
1514 struct cgraph_node **node2 = &node->origin->nested;
1515 gcc_assert (node->origin);
1517 while (*node2 != node)
1518 node2 = &(*node2)->next_nested;
1519 *node2 = node->next_nested;
1520 node->origin = NULL;
1523 /* Return function availability. See cgraph.h for description of individual
1526 cgraph_function_body_availability (struct cgraph_node *node)
1528 enum availability avail;
1529 gcc_assert (cgraph_function_flags_ready);
1530 if (!node->analyzed)
1531 avail = AVAIL_NOT_AVAILABLE;
1532 else if (node->local.local)
1533 avail = AVAIL_LOCAL;
1534 else if (!node->symbol.externally_visible)
1535 avail = AVAIL_AVAILABLE;
1536 /* Inline functions are safe to be analyzed even if their symbol can
1537 be overwritten at runtime. It is not meaningful to enforce any sane
1538 behaviour on replacing inline function by different body. */
1539 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1540 avail = AVAIL_AVAILABLE;
1542 /* If the function can be overwritten, return OVERWRITABLE. Take
1543 care at least of two notable extensions - the COMDAT functions
1544 used to share template instantiations in C++ (this is symmetric
1545 to code cp_cannot_inline_tree_fn and probably shall be shared and
1546 the inlinability hooks completely eliminated).
1548 ??? Does the C++ one definition rule allow us to always return
1549 AVAIL_AVAILABLE here? That would be good reason to preserve this
1552 else if (decl_replaceable_p (node->symbol.decl)
1553 && !DECL_EXTERNAL (node->symbol.decl))
1554 avail = AVAIL_OVERWRITABLE;
1555 else avail = AVAIL_AVAILABLE;
1560 /* Worker for cgraph_node_can_be_local_p. */
1562 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
1563 void *data ATTRIBUTE_UNUSED)
1565 return !(!node->symbol.force_output
1566 && ((DECL_COMDAT (node->symbol.decl)
1567 && !node->symbol.same_comdat_group)
1568 || !node->symbol.externally_visible));
1571 /* Return true if NODE can be made local for API change.
1572 Extern inline functions and C++ COMDAT functions can be made local
1573 at the expense of possible code size growth if function is used in multiple
1574 compilation units. */
1576 cgraph_node_can_be_local_p (struct cgraph_node *node)
1578 return (!node->symbol.address_taken
1579 && !cgraph_for_node_and_aliases (node,
1580 cgraph_node_cannot_be_local_p_1,
1584 /* Call calback on NODE, thunks and aliases asociated to NODE.
1585 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1589 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
1590 bool (*callback) (struct cgraph_node *, void *),
1592 bool include_overwritable)
1594 struct cgraph_edge *e;
1596 struct ipa_ref *ref;
1598 if (callback (node, data))
1600 for (e = node->callers; e; e = e->next_caller)
1601 if (e->caller->thunk.thunk_p
1602 && (include_overwritable
1603 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1604 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1605 include_overwritable))
1607 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1608 if (ref->use == IPA_REF_ALIAS)
1610 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1611 if (include_overwritable
1612 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1613 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1614 include_overwritable))
1620 /* Call calback on NODE and aliases asociated to NODE.
1621 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1625 cgraph_for_node_and_aliases (struct cgraph_node *node,
1626 bool (*callback) (struct cgraph_node *, void *),
1628 bool include_overwritable)
1631 struct ipa_ref *ref;
1633 if (callback (node, data))
1635 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1636 if (ref->use == IPA_REF_ALIAS)
1638 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1639 if (include_overwritable
1640 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1641 if (cgraph_for_node_and_aliases (alias, callback, data,
1642 include_overwritable))
1648 /* Worker to bring NODE local. */
1651 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1653 gcc_checking_assert (cgraph_node_can_be_local_p (node));
1654 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1656 symtab_make_decl_local (node->symbol.decl);
1658 node->symbol.externally_visible = false;
1659 node->local.local = true;
1660 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1661 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1666 /* Bring NODE local. */
1669 cgraph_make_node_local (struct cgraph_node *node)
1671 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
1675 /* Worker to set nothrow flag. */
1678 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
1680 struct cgraph_edge *e;
1682 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1685 for (e = node->callers; e; e = e->next_caller)
1686 e->can_throw_external = false;
1690 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1691 if any to NOTHROW. */
1694 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1696 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1697 (void *)(size_t)nothrow, false);
1700 /* Worker to set const flag. */
1703 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1705 /* Static constructors and destructors without a side effect can be
1707 if (data && !((size_t)data & 2))
1709 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1710 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1711 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1712 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1714 TREE_READONLY (node->symbol.decl) = data != NULL;
1715 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1719 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1720 if any to READONLY. */
1723 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1725 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
1726 (void *)(size_t)(readonly + (int)looping * 2),
1730 /* Worker to set pure flag. */
1733 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
1735 /* Static pureructors and destructors without a side effect can be
1737 if (data && !((size_t)data & 2))
1739 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1740 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1741 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1742 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1744 DECL_PURE_P (node->symbol.decl) = data != NULL;
1745 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1749 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
1753 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
1755 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1756 (void *)(size_t)(pure + (int)looping * 2),
1760 /* Data used by cgraph_propagate_frequency. */
1762 struct cgraph_propagate_frequency_data
1764 bool maybe_unlikely_executed;
1765 bool maybe_executed_once;
1766 bool only_called_at_startup;
1767 bool only_called_at_exit;
1770 /* Worker for cgraph_propagate_frequency_1. */
1773 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
1775 struct cgraph_propagate_frequency_data *d;
1776 struct cgraph_edge *edge;
1778 d = (struct cgraph_propagate_frequency_data *)data;
1779 for (edge = node->callers;
1780 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1781 || d->only_called_at_startup || d->only_called_at_exit);
1782 edge = edge->next_caller)
1784 if (edge->caller != node)
1786 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1787 /* It makes sense to put main() together with the static constructors.
1788 It will be executed for sure, but rest of functions called from
1789 main are definitely not at startup only. */
1790 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1791 d->only_called_at_startup = 0;
1792 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1794 if (!edge->frequency)
1796 switch (edge->caller->frequency)
1798 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
1800 case NODE_FREQUENCY_EXECUTED_ONCE:
1801 if (dump_file && (dump_flags & TDF_DETAILS))
1802 fprintf (dump_file, " Called by %s that is executed once\n",
1803 cgraph_node_name (edge->caller));
1804 d->maybe_unlikely_executed = false;
1805 if (inline_edge_summary (edge)->loop_depth)
1807 d->maybe_executed_once = false;
1808 if (dump_file && (dump_flags & TDF_DETAILS))
1809 fprintf (dump_file, " Called in loop\n");
1812 case NODE_FREQUENCY_HOT:
1813 case NODE_FREQUENCY_NORMAL:
1814 if (dump_file && (dump_flags & TDF_DETAILS))
1815 fprintf (dump_file, " Called by %s that is normal or hot\n",
1816 cgraph_node_name (edge->caller));
1817 d->maybe_unlikely_executed = false;
1818 d->maybe_executed_once = false;
1822 return edge != NULL;
1825 /* See if the frequency of NODE can be updated based on frequencies of its
1828 cgraph_propagate_frequency (struct cgraph_node *node)
1830 struct cgraph_propagate_frequency_data d = {true, true, true, true};
1831 bool changed = false;
1833 if (!node->local.local)
1835 gcc_assert (node->analyzed);
1836 if (dump_file && (dump_flags & TDF_DETAILS))
1837 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
1839 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
1841 if ((d.only_called_at_startup && !d.only_called_at_exit)
1842 && !node->only_called_at_startup)
1844 node->only_called_at_startup = true;
1846 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
1847 cgraph_node_name (node));
1850 if ((d.only_called_at_exit && !d.only_called_at_startup)
1851 && !node->only_called_at_exit)
1853 node->only_called_at_exit = true;
1855 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
1856 cgraph_node_name (node));
1859 /* These come either from profile or user hints; never update them. */
1860 if (node->frequency == NODE_FREQUENCY_HOT
1861 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1863 if (d.maybe_unlikely_executed)
1865 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
1867 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
1868 cgraph_node_name (node));
1871 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
1873 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1875 fprintf (dump_file, "Node %s promoted to executed once.\n",
1876 cgraph_node_name (node));
1882 /* Return true when NODE can not return or throw and thus
1883 it is safe to ignore its side effects for IPA analysis. */
1886 cgraph_node_cannot_return (struct cgraph_node *node)
1888 int flags = flags_from_decl_or_type (node->symbol.decl);
1889 if (!flag_exceptions)
1890 return (flags & ECF_NORETURN) != 0;
1892 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1893 == (ECF_NORETURN | ECF_NOTHROW));
1896 /* Return true when call of E can not lead to return from caller
1897 and thus it is safe to ignore its side effects for IPA analysis
1898 when computing side effects of the caller.
1899 FIXME: We could actually mark all edges that have no reaching
1900 patch to EXIT_BLOCK_PTR or throw to get better results. */
1902 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
1904 if (cgraph_node_cannot_return (e->caller))
1906 if (e->indirect_unknown_callee)
1908 int flags = e->indirect_info->ecf_flags;
1909 if (!flag_exceptions)
1910 return (flags & ECF_NORETURN) != 0;
1912 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1913 == (ECF_NORETURN | ECF_NOTHROW));
1916 return cgraph_node_cannot_return (e->callee);
1919 /* Return true when function NODE can be removed from callgraph
1920 if all direct calls are eliminated. */
1923 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
1925 gcc_assert (!node->global.inlined_to);
1926 /* Extern inlines can always go, we will use the external definition. */
1927 if (DECL_EXTERNAL (node->symbol.decl))
1929 /* When function is needed, we can not remove it. */
1930 if (node->symbol.force_output || node->symbol.used_from_other_partition)
1932 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1933 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1935 /* Only COMDAT functions can be removed if externally visible. */
1936 if (node->symbol.externally_visible
1937 && (!DECL_COMDAT (node->symbol.decl)
1938 || symtab_used_from_object_file_p ((symtab_node) node)))
1943 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1946 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1948 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
1951 /* Return true when function NODE and its aliases can be removed from callgraph
1952 if all direct calls are eliminated. */
1955 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
1957 /* Extern inlines can always go, we will use the external definition. */
1958 if (DECL_EXTERNAL (node->symbol.decl))
1960 if (node->symbol.address_taken)
1962 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
1965 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1968 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1970 return symtab_used_from_object_file_p ((symtab_node) node);
1973 /* Return true when function NODE can be expected to be removed
1974 from program when direct calls in this compilation unit are removed.
1976 As a special case COMDAT functions are
1977 cgraph_can_remove_if_no_direct_calls_p while the are not
1978 cgraph_only_called_directly_p (it is possible they are called from other
1981 This function behaves as cgraph_only_called_directly_p because eliminating
1982 all uses of COMDAT function does not make it necessarily disappear from
1983 the program unless we are compiling whole program or we do LTO. In this
1984 case we know we win since dynamic linking will not really discard the
1985 linkonce section. */
1988 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
1990 gcc_assert (!node->global.inlined_to);
1991 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
1993 if (!in_lto_p && !flag_whole_program)
1994 return cgraph_only_called_directly_p (node);
1997 if (DECL_EXTERNAL (node->symbol.decl))
1999 return cgraph_can_remove_if_no_direct_calls_p (node);
2004 /* Worker for cgraph_only_called_directly_p. */
2007 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2009 return !cgraph_only_called_directly_or_aliased_p (node);
2012 /* Return true when function NODE and all its aliases are only called
2014 i.e. it is not externally visible, address was not taken and
2015 it is not used in any other non-standard way. */
2018 cgraph_only_called_directly_p (struct cgraph_node *node)
2020 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2021 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2026 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2029 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2031 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
2032 struct cgraph_edge *cs;
2033 enum availability avail;
2034 cgraph_function_or_thunk_node (node, &avail);
2036 if (avail > AVAIL_OVERWRITABLE)
2037 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2038 if (!cs->indirect_inlining_edge)
2039 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
2043 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2044 (i.e. are not overwritable). */
2046 VEC (cgraph_edge_p, heap) *
2047 collect_callers_of_node (struct cgraph_node *node)
2049 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
2050 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2051 &redirect_callers, false);
2052 return redirect_callers;
2055 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2057 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2059 node = cgraph_function_or_thunk_node (node, NULL);
2060 node2 = cgraph_function_or_thunk_node (node2, NULL);
2061 while (node != node2 && node2)
2062 node2 = node2->clone_of;
2063 return node2 != NULL;
2066 /* Verify edge E count and frequency. */
2069 verify_edge_count_and_frequency (struct cgraph_edge *e)
2071 bool error_found = false;
2074 error ("caller edge count is negative");
2077 if (e->frequency < 0)
2079 error ("caller edge frequency is negative");
2082 if (e->frequency > CGRAPH_FREQ_MAX)
2084 error ("caller edge frequency is too large");
2087 if (gimple_has_body_p (e->caller->symbol.decl)
2088 && !e->caller->global.inlined_to
2089 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2090 Remove this once edges are actualy removed from the function at that time. */
2092 || (inline_edge_summary_vec
2093 && ((VEC_length(inline_edge_summary_t, inline_edge_summary_vec)
2094 <= (unsigned) e->uid)
2095 || !inline_edge_summary (e)->predicate)))
2097 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2098 gimple_bb (e->call_stmt))))
2100 error ("caller edge frequency %i does not match BB frequency %i",
2102 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2103 gimple_bb (e->call_stmt)));
2109 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2111 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2113 /* debug_gimple_stmt needs correct cfun */
2114 if (cfun != this_cfun)
2115 set_cfun (this_cfun);
2116 debug_gimple_stmt (stmt);
2119 /* Verify that call graph edge E corresponds to DECL from the associated
2120 statement. Return true if the verification should fail. */
2123 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2125 struct cgraph_node *node;
2127 if (!decl || e->callee->global.inlined_to)
2129 node = cgraph_get_node (decl);
2131 /* We do not know if a node from a different partition is an alias or what it
2132 aliases and therefore cannot do the former_clone_of check reliably. */
2133 if (!node || node->symbol.in_other_partition)
2135 node = cgraph_function_or_thunk_node (node, NULL);
2137 if ((e->callee->former_clone_of != node->symbol.decl
2138 && (!node->same_body_alias
2139 || e->callee->former_clone_of != node->thunk.alias))
2140 /* IPA-CP sometimes redirect edge to clone and then back to the former
2141 function. This ping-pong has to go, eventually. */
2142 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2143 && !clone_of_p (node, e->callee)
2144 /* If decl is a same body alias of some other decl, allow e->callee to be
2145 a clone of a clone of that other decl too. */
2146 && (!node->same_body_alias
2147 || !clone_of_p (cgraph_get_node (node->thunk.alias), e->callee)))
2153 /* Verify cgraph nodes of given cgraph node. */
2155 verify_cgraph_node (struct cgraph_node *node)
2157 struct cgraph_edge *e;
2158 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2159 basic_block this_block;
2160 gimple_stmt_iterator gsi;
2161 bool error_found = false;
2166 timevar_push (TV_CGRAPH_VERIFY);
2167 error_found |= verify_symtab_base ((symtab_node) node);
2168 for (e = node->callees; e; e = e->next_callee)
2171 error ("aux field set for edge %s->%s",
2172 identifier_to_locale (cgraph_node_name (e->caller)),
2173 identifier_to_locale (cgraph_node_name (e->callee)));
2176 if (node->count < 0)
2178 error ("execution count is negative");
2181 if (node->global.inlined_to && node->symbol.same_comdat_group)
2183 error ("inline clone in same comdat group list");
2186 if (node->global.inlined_to && node->symbol.externally_visible)
2188 error ("externally visible inline clone");
2191 if (node->global.inlined_to && node->symbol.address_taken)
2193 error ("inline clone with address taken");
2196 if (node->global.inlined_to && node->symbol.force_output)
2198 error ("inline clone is forced to output");
2201 for (e = node->indirect_calls; e; e = e->next_callee)
2205 error ("aux field set for indirect edge from %s",
2206 identifier_to_locale (cgraph_node_name (e->caller)));
2209 if (!e->indirect_unknown_callee
2210 || !e->indirect_info)
2212 error ("An indirect edge from %s is not marked as indirect or has "
2213 "associated indirect_info, the corresponding statement is: ",
2214 identifier_to_locale (cgraph_node_name (e->caller)));
2215 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2219 for (e = node->callers; e; e = e->next_caller)
2221 if (verify_edge_count_and_frequency (e))
2223 if (!e->inline_failed)
2225 if (node->global.inlined_to
2226 != (e->caller->global.inlined_to
2227 ? e->caller->global.inlined_to : e->caller))
2229 error ("inlined_to pointer is wrong");
2232 if (node->callers->next_caller)
2234 error ("multiple inline callers");
2239 if (node->global.inlined_to)
2241 error ("inlined_to pointer set for noninline callers");
2245 for (e = node->indirect_calls; e; e = e->next_callee)
2246 if (verify_edge_count_and_frequency (e))
2248 if (!node->callers && node->global.inlined_to)
2250 error ("inlined_to pointer is set but no predecessors found");
2253 if (node->global.inlined_to == node)
2255 error ("inlined_to pointer refers to itself");
2261 struct cgraph_node *n;
2262 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2267 error ("node has wrong clone_of");
2273 struct cgraph_node *n;
2274 for (n = node->clones; n; n = n->next_sibling_clone)
2275 if (n->clone_of != node)
2279 error ("node has wrong clone list");
2283 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2285 error ("node is in clone list but it is not clone");
2288 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2290 error ("node has wrong prev_clone pointer");
2293 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2295 error ("double linked list of clones corrupted");
2299 if (node->analyzed && node->alias)
2301 bool ref_found = false;
2303 struct ipa_ref *ref;
2307 error ("Alias has call edges");
2310 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2312 if (ref->use != IPA_REF_ALIAS)
2314 error ("Alias has non-alias reference");
2319 error ("Alias has more than one alias reference");
2326 error ("Analyzed alias has no reference");
2330 if (node->analyzed && node->thunk.thunk_p)
2334 error ("No edge out of thunk node");
2337 else if (node->callees->next_callee)
2339 error ("More than one edge out of thunk node");
2342 if (gimple_has_body_p (node->symbol.decl))
2344 error ("Thunk is not supposed to have body");
2348 else if (node->analyzed && gimple_has_body_p (node->symbol.decl)
2349 && !TREE_ASM_WRITTEN (node->symbol.decl)
2350 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2355 /* The nodes we're interested in are never shared, so walk
2356 the tree ignoring duplicates. */
2357 struct pointer_set_t *visited_nodes = pointer_set_create ();
2358 /* Reach the trees by walking over the CFG, and note the
2359 enclosing basic-blocks in the call edges. */
2360 FOR_EACH_BB_FN (this_block, this_cfun)
2361 for (gsi = gsi_start_bb (this_block);
2365 gimple stmt = gsi_stmt (gsi);
2366 if (is_gimple_call (stmt))
2368 struct cgraph_edge *e = cgraph_edge (node, stmt);
2369 tree decl = gimple_call_fndecl (stmt);
2374 error ("shared call_stmt:");
2375 cgraph_debug_gimple_stmt (this_cfun, stmt);
2378 if (!e->indirect_unknown_callee)
2380 if (verify_edge_corresponds_to_fndecl (e, decl))
2382 error ("edge points to wrong declaration:");
2383 debug_tree (e->callee->symbol.decl);
2384 fprintf (stderr," Instead of:");
2391 error ("an indirect edge with unknown callee "
2392 "corresponding to a call_stmt with "
2393 "a known declaration:");
2395 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2401 error ("missing callgraph edge for call stmt:");
2402 cgraph_debug_gimple_stmt (this_cfun, stmt);
2407 pointer_set_destroy (visited_nodes);
2410 /* No CFG available?! */
2413 for (e = node->callees; e; e = e->next_callee)
2417 error ("edge %s->%s has no corresponding call_stmt",
2418 identifier_to_locale (cgraph_node_name (e->caller)),
2419 identifier_to_locale (cgraph_node_name (e->callee)));
2420 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2425 for (e = node->indirect_calls; e; e = e->next_callee)
2429 error ("an indirect edge from %s has no corresponding call_stmt",
2430 identifier_to_locale (cgraph_node_name (e->caller)));
2431 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2439 dump_cgraph_node (stderr, node);
2440 internal_error ("verify_cgraph_node failed");
2442 timevar_pop (TV_CGRAPH_VERIFY);
2445 /* Verify whole cgraph structure. */
2447 verify_cgraph (void)
2449 struct cgraph_node *node;
2454 FOR_EACH_FUNCTION (node)
2455 verify_cgraph_node (node);
2457 #include "gt-cgraph.h"