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
26 The call-graph is data structure designed for intra-procedural optimization
27 but it is also used in non-unit-at-a-time compilation to allow easier code
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node.
33 The mapping from declarations to call-graph nodes is done using hash table
34 based on DECL_UID. The call-graph nodes are created lazily using
35 cgraph_node function when called for unknown declaration.
37 The callgraph at the moment does not represent all indirect calls or calls
38 from other compilation units. Flag NEEDED is set for each node that may be
39 accessed in such an invisible way and it shall be considered an entry point
42 On the other hand, the callgraph currently does contain some edges for
43 indirect calls with unknown callees which can be accessed through
44 indirect_calls field of a node. It should be noted however that at the
45 moment only calls which are potential candidates for indirect inlining are
48 Interprocedural information:
50 Callgraph is place to store data needed for interprocedural optimization.
51 All data structures are divided into three components: local_info that
52 is produced while analyzing the function, global_info that is result
53 of global walking of the callgraph on the end of compilation and
54 rtl_info used by RTL backend to propagate data from already compiled
55 functions to their callers.
57 Moreover, each node has a uid which can be used to keep information in
58 on-the-side arrays. UIDs are reused and therefore reasonably dense.
62 The function inlining information is decided in advance and maintained
63 in the callgraph as so called inline plan.
64 For each inlined call, the callee's node is cloned to represent the
65 new function copy produced by inliner.
66 Each inlined call gets a unique corresponding clone node of the callee
67 and the data structure is updated while inlining is performed, so
68 the clones are eliminated and their callee edges redirected to the
71 Each edge has "inline_failed" field. When the field is set to NULL,
72 the call will be inlined. When it is non-NULL it contains a reason
73 why inlining wasn't performed. */
77 #include "coretypes.h"
80 #include "tree-inline.h"
81 #include "langhooks.h"
88 #include "basic-block.h"
93 #include "tree-dump.h"
94 #include "tree-flow.h"
95 #include "value-prof.h"
97 #include "diagnostic-core.h"
99 #include "ipa-utils.h"
100 #include "lto-streamer.h"
101 #include "ipa-inline.h"
104 const char * const ld_plugin_symbol_resolution_names[]=
109 "prevailing_def_ironly",
115 "prevailing_def_ironly_exp"
118 static void cgraph_node_remove_callers (struct cgraph_node *node);
119 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
120 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
122 /* Hash table used to convert declarations into nodes. */
123 static GTY((param_is (union symtab_node_def))) htab_t cgraph_hash;
124 /* Hash table used to convert assembler names into nodes. */
125 static GTY((param_is (union symtab_node_def))) htab_t assembler_name_hash;
127 /* Queue of cgraph nodes scheduled to be lowered. */
128 symtab_node x_cgraph_nodes_queue;
129 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
131 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
132 secondary queue used during optimization to accommodate passes that
133 may generate new functions that need to be optimized and expanded. */
134 struct cgraph_node *cgraph_new_nodes;
136 /* Number of nodes in existence. */
139 /* Maximal uid used in cgraph nodes. */
142 /* Maximal uid used in cgraph edges. */
143 int cgraph_edge_max_uid;
145 /* Set when whole unit has been analyzed so we can access global info. */
146 bool cgraph_global_info_ready = false;
148 /* What state callgraph is in right now. */
149 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
151 /* Set when the cgraph is fully build and the basic flags are computed. */
152 bool cgraph_function_flags_ready = false;
154 /* Linked list of cgraph asm nodes. */
155 struct cgraph_asm_node *cgraph_asm_nodes;
157 /* Last node in cgraph_asm_nodes. */
158 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
160 /* List of hooks triggered on cgraph_edge events. */
161 struct cgraph_edge_hook_list {
162 cgraph_edge_hook hook;
164 struct cgraph_edge_hook_list *next;
167 /* List of hooks triggered on cgraph_node events. */
168 struct cgraph_node_hook_list {
169 cgraph_node_hook hook;
171 struct cgraph_node_hook_list *next;
174 /* List of hooks triggered on events involving two cgraph_edges. */
175 struct cgraph_2edge_hook_list {
176 cgraph_2edge_hook hook;
178 struct cgraph_2edge_hook_list *next;
181 /* List of hooks triggered on events involving two cgraph_nodes. */
182 struct cgraph_2node_hook_list {
183 cgraph_2node_hook hook;
185 struct cgraph_2node_hook_list *next;
188 /* List of hooks triggered when an edge is removed. */
189 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
190 /* List of hooks triggered when a node is removed. */
191 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
192 /* List of hooks triggered when an edge is duplicated. */
193 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
194 /* List of hooks triggered when a node is duplicated. */
195 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
196 /* List of hooks triggered when an function is inserted. */
197 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
199 /* Head of a linked list of unused (freed) call graph nodes.
200 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
201 static GTY(()) struct cgraph_node *free_nodes;
202 /* Head of a linked list of unused (freed) call graph edges.
203 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
204 static GTY(()) struct cgraph_edge *free_edges;
206 /* Did procss_same_body_aliases run? */
207 bool same_body_aliases_done;
209 /* Macros to access the next item in the list of free cgraph nodes and
211 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
212 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
213 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
215 /* Register HOOK to be called with DATA on each removed edge. */
216 struct cgraph_edge_hook_list *
217 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
219 struct cgraph_edge_hook_list *entry;
220 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
222 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
232 /* Remove ENTRY from the list of hooks called on removing edges. */
234 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
236 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
238 while (*ptr != entry)
244 /* Call all edge removal hooks. */
246 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
248 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
251 entry->hook (e, entry->data);
256 /* Register HOOK to be called with DATA on each removed node. */
257 struct cgraph_node_hook_list *
258 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
260 struct cgraph_node_hook_list *entry;
261 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
263 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
273 /* Remove ENTRY from the list of hooks called on removing nodes. */
275 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
277 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
279 while (*ptr != entry)
285 /* Call all node removal hooks. */
287 cgraph_call_node_removal_hooks (struct cgraph_node *node)
289 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
292 entry->hook (node, entry->data);
297 /* Register HOOK to be called with DATA on each inserted node. */
298 struct cgraph_node_hook_list *
299 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
301 struct cgraph_node_hook_list *entry;
302 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
304 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
314 /* Remove ENTRY from the list of hooks called on inserted nodes. */
316 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
318 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
320 while (*ptr != entry)
326 /* Call all node insertion hooks. */
328 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
330 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
333 entry->hook (node, entry->data);
338 /* Register HOOK to be called with DATA on each duplicated edge. */
339 struct cgraph_2edge_hook_list *
340 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
342 struct cgraph_2edge_hook_list *entry;
343 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
345 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
355 /* Remove ENTRY from the list of hooks called on duplicating edges. */
357 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
359 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
361 while (*ptr != entry)
367 /* Call all edge duplication hooks. */
369 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
370 struct cgraph_edge *cs2)
372 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
375 entry->hook (cs1, cs2, entry->data);
380 /* Register HOOK to be called with DATA on each duplicated node. */
381 struct cgraph_2node_hook_list *
382 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
384 struct cgraph_2node_hook_list *entry;
385 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
387 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
397 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
399 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
401 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
403 while (*ptr != entry)
409 /* Call all node duplication hooks. */
411 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
412 struct cgraph_node *node2)
414 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
417 entry->hook (node1, node2, entry->data);
422 /* Returns a hash code for P. */
425 hash_node (const void *p)
427 const struct cgraph_node *n = (const struct cgraph_node *) p;
428 return (hashval_t) DECL_UID (n->symbol.decl);
432 /* Returns nonzero if P1 and P2 are equal. */
435 eq_node (const void *p1, const void *p2)
437 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
438 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
439 return DECL_UID (n1->symbol.decl) == DECL_UID (n2->symbol.decl);
442 /* Allocate new callgraph node. */
444 static inline struct cgraph_node *
445 cgraph_allocate_node (void)
447 struct cgraph_node *node;
452 free_nodes = NEXT_FREE_NODE (node);
456 node = ggc_alloc_cleared_cgraph_node ();
457 node->uid = cgraph_max_uid++;
463 /* Allocate new callgraph node and insert it into basic data structures. */
465 static struct cgraph_node *
466 cgraph_create_node_1 (void)
468 struct cgraph_node *node = cgraph_allocate_node ();
470 node->symbol.type = SYMTAB_FUNCTION;
471 node->frequency = NODE_FREQUENCY_NORMAL;
472 node->count_materialization_scale = REG_BR_PROB_BASE;
477 /* Return cgraph node assigned to DECL. Create new one when needed. */
480 cgraph_create_node (tree decl)
482 struct cgraph_node key, *node, **slot;
484 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
487 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
489 key.symbol.decl = decl;
490 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
493 node = cgraph_create_node_1 ();
494 node->symbol.decl = decl;
495 symtab_register_node ((symtab_node)node);
497 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
499 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
500 node->next_nested = node->origin->nested;
501 node->origin->nested = node;
503 if (assembler_name_hash)
506 tree name = DECL_ASSEMBLER_NAME (decl);
508 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
509 decl_assembler_name_hash (name),
511 /* We can have multiple declarations with same assembler name. For C++
512 it is __builtin_strlen and strlen, for instance. Do we need to
513 record them all? Original implementation marked just first one
514 so lets hope for the best. */
521 /* Try to find a call graph node for declaration DECL and if it does not exist,
525 cgraph_get_create_node (tree decl)
527 struct cgraph_node *node;
529 node = cgraph_get_node (decl);
533 return cgraph_create_node (decl);
536 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
537 the function body is associated with (not neccesarily cgraph_node (DECL). */
540 cgraph_create_function_alias (tree alias, tree decl)
542 struct cgraph_node *alias_node;
544 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
545 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
546 alias_node = cgraph_get_create_node (alias);
547 gcc_assert (!alias_node->local.finalized);
548 alias_node->thunk.alias = decl;
549 alias_node->local.finalized = true;
550 alias_node->alias = 1;
552 if ((TREE_PUBLIC (alias) && !DECL_COMDAT (alias) && !DECL_EXTERNAL (alias))
553 || (DECL_VIRTUAL_P (alias)
554 && (DECL_COMDAT (alias) || DECL_EXTERNAL (alias))))
555 cgraph_mark_reachable_node (alias_node);
559 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
561 Same body aliases are output whenever the body of DECL is output,
562 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
565 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
567 struct cgraph_node *n;
568 #ifndef ASM_OUTPUT_DEF
569 /* If aliases aren't supported by the assembler, fail. */
572 /* Langhooks can create same body aliases of symbols not defined.
573 Those are useless. Drop them on the floor. */
574 if (cgraph_global_info_ready)
577 n = cgraph_create_function_alias (alias, decl);
578 n->same_body_alias = true;
579 if (same_body_aliases_done)
580 ipa_record_reference (n, NULL, cgraph_get_node (decl), NULL, IPA_REF_ALIAS,
585 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
586 aliases DECL with an adjustments made into the first parameter.
587 See comments in thunk_adjust for detail on the parameters. */
590 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
591 tree alias, tree decl,
593 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
597 struct cgraph_node *node;
599 node = cgraph_get_node (alias);
602 gcc_assert (node->local.finalized);
603 gcc_assert (!node->alias);
604 gcc_assert (!node->thunk.thunk_p);
605 cgraph_remove_node (node);
608 node = cgraph_create_node (alias);
609 gcc_checking_assert (!virtual_offset
610 || double_int_equal_p
611 (tree_to_double_int (virtual_offset),
612 shwi_to_double_int (virtual_value)));
613 node->thunk.fixed_offset = fixed_offset;
614 node->thunk.this_adjusting = this_adjusting;
615 node->thunk.virtual_value = virtual_value;
616 node->thunk.virtual_offset_p = virtual_offset != NULL;
617 node->thunk.alias = real_alias;
618 node->thunk.thunk_p = true;
619 node->local.finalized = true;
621 if (cgraph_decide_is_function_needed (node, decl))
622 cgraph_mark_needed_node (node);
624 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
625 || (DECL_VIRTUAL_P (decl)
626 && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
627 cgraph_mark_reachable_node (node);
632 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
636 cgraph_get_node (const_tree decl)
638 struct cgraph_node key, *node = NULL, **slot;
640 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
645 key.symbol.decl = CONST_CAST2 (tree, const_tree, decl);
647 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
655 /* Insert already constructed node into hashtable. */
658 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
660 struct cgraph_node **slot;
662 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
668 /* Returns a hash code for P. */
671 hash_node_by_assembler_name (const void *p)
673 const struct cgraph_node *n = (const struct cgraph_node *) p;
674 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->symbol.decl));
677 /* Returns nonzero if P1 and P2 are equal. */
680 eq_assembler_name (const void *p1, const void *p2)
682 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
683 const_tree name = (const_tree)p2;
684 return (decl_assembler_name_equal (n1->symbol.decl, name));
687 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
688 Return NULL if there's no such node. */
691 cgraph_node_for_asm (tree asmname)
693 struct cgraph_node *node;
696 if (!assembler_name_hash)
698 assembler_name_hash =
699 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
701 FOR_EACH_FUNCTION (node)
702 if (!node->global.inlined_to)
704 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
705 slot = htab_find_slot_with_hash (assembler_name_hash, name,
706 decl_assembler_name_hash (name),
708 /* We can have multiple declarations with same assembler name. For C++
709 it is __builtin_strlen and strlen, for instance. Do we need to
710 record them all? Original implementation marked just first one
711 so lets hope for the best. */
717 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
718 decl_assembler_name_hash (asmname),
723 node = (struct cgraph_node *) *slot;
729 /* Returns a hash value for X (which really is a die_struct). */
732 edge_hash (const void *x)
734 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
737 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
740 edge_eq (const void *x, const void *y)
742 return ((const struct cgraph_edge *) x)->call_stmt == y;
745 /* Add call graph edge E to call site hash of its caller. */
748 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
751 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
753 htab_hash_pointer (e->call_stmt),
759 /* Return the callgraph edge representing the GIMPLE_CALL statement
763 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
765 struct cgraph_edge *e, *e2;
768 if (node->call_site_hash)
769 return (struct cgraph_edge *)
770 htab_find_with_hash (node->call_site_hash, call_stmt,
771 htab_hash_pointer (call_stmt));
773 /* This loop may turn out to be performance problem. In such case adding
774 hashtables into call nodes with very many edges is probably best
775 solution. It is not good idea to add pointer into CALL_EXPR itself
776 because we want to make possible having multiple cgraph nodes representing
777 different clones of the same body before the body is actually cloned. */
778 for (e = node->callees; e; e = e->next_callee)
780 if (e->call_stmt == call_stmt)
786 for (e = node->indirect_calls; e; e = e->next_callee)
788 if (e->call_stmt == call_stmt)
795 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
796 for (e2 = node->callees; e2; e2 = e2->next_callee)
797 cgraph_add_edge_to_call_site_hash (e2);
798 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
799 cgraph_add_edge_to_call_site_hash (e2);
806 /* Change field call_stmt of edge E to NEW_STMT. */
809 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
813 if (e->caller->call_site_hash)
815 htab_remove_elt_with_hash (e->caller->call_site_hash,
817 htab_hash_pointer (e->call_stmt));
820 e->call_stmt = new_stmt;
821 if (e->indirect_unknown_callee
822 && (decl = gimple_call_fndecl (new_stmt)))
824 /* Constant propagation (and possibly also inlining?) can turn an
825 indirect call into a direct one. */
826 struct cgraph_node *new_callee = cgraph_get_node (decl);
828 gcc_checking_assert (new_callee);
829 cgraph_make_edge_direct (e, new_callee);
832 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
833 e->can_throw_external = stmt_can_throw_external (new_stmt);
835 if (e->caller->call_site_hash)
836 cgraph_add_edge_to_call_site_hash (e);
839 /* Like cgraph_set_call_stmt but walk the clone tree and update all
840 clones sharing the same function body. */
843 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
844 gimple old_stmt, gimple new_stmt)
846 struct cgraph_node *node;
847 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
850 cgraph_set_call_stmt (edge, new_stmt);
856 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
858 cgraph_set_call_stmt (edge, new_stmt);
861 else if (node->next_sibling_clone)
862 node = node->next_sibling_clone;
865 while (node != orig && !node->next_sibling_clone)
866 node = node->clone_of;
868 node = node->next_sibling_clone;
873 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
874 same function body. If clones already have edge for OLD_STMT; only
875 update the edge same way as cgraph_set_call_stmt_including_clones does.
877 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
878 frequencies of the clones. */
881 cgraph_create_edge_including_clones (struct cgraph_node *orig,
882 struct cgraph_node *callee,
884 gimple stmt, gcov_type count,
886 cgraph_inline_failed_t reason)
888 struct cgraph_node *node;
889 struct cgraph_edge *edge;
891 if (!cgraph_edge (orig, stmt))
893 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
894 edge->inline_failed = reason;
901 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
903 /* It is possible that clones already contain the edge while
904 master didn't. Either we promoted indirect call into direct
905 call in the clone or we are processing clones of unreachable
906 master where edges has been removed. */
908 cgraph_set_call_stmt (edge, stmt);
909 else if (!cgraph_edge (node, stmt))
911 edge = cgraph_create_edge (node, callee, stmt, count,
913 edge->inline_failed = reason;
918 else if (node->next_sibling_clone)
919 node = node->next_sibling_clone;
922 while (node != orig && !node->next_sibling_clone)
923 node = node->clone_of;
925 node = node->next_sibling_clone;
930 /* Allocate a cgraph_edge structure and fill it with data according to the
931 parameters of which only CALLEE can be NULL (when creating an indirect call
934 static struct cgraph_edge *
935 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
936 gimple call_stmt, gcov_type count, int freq)
938 struct cgraph_edge *edge;
940 /* LTO does not actually have access to the call_stmt since these
941 have not been loaded yet. */
944 /* This is a rather expensive check possibly triggering
945 construction of call stmt hashtable. */
946 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
948 gcc_assert (is_gimple_call (call_stmt));
954 free_edges = NEXT_FREE_EDGE (edge);
958 edge = ggc_alloc_cgraph_edge ();
959 edge->uid = cgraph_edge_max_uid++;
963 edge->caller = caller;
964 edge->callee = callee;
965 edge->prev_caller = NULL;
966 edge->next_caller = NULL;
967 edge->prev_callee = NULL;
968 edge->next_callee = NULL;
971 gcc_assert (count >= 0);
972 edge->frequency = freq;
973 gcc_assert (freq >= 0);
974 gcc_assert (freq <= CGRAPH_FREQ_MAX);
976 edge->call_stmt = call_stmt;
977 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
978 edge->can_throw_external
979 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
982 && callee && callee->symbol.decl
983 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl))
984 edge->call_stmt_cannot_inline_p = true;
986 edge->call_stmt_cannot_inline_p = false;
987 if (call_stmt && caller->call_site_hash)
988 cgraph_add_edge_to_call_site_hash (edge);
990 edge->indirect_info = NULL;
991 edge->indirect_inlining_edge = 0;
996 /* Create edge from CALLER to CALLEE in the cgraph. */
999 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1000 gimple call_stmt, gcov_type count, int freq)
1002 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1005 edge->indirect_unknown_callee = 0;
1006 initialize_inline_failed (edge);
1008 edge->next_caller = callee->callers;
1009 if (callee->callers)
1010 callee->callers->prev_caller = edge;
1011 edge->next_callee = caller->callees;
1012 if (caller->callees)
1013 caller->callees->prev_callee = edge;
1014 caller->callees = edge;
1015 callee->callers = edge;
1020 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1022 struct cgraph_indirect_call_info *
1023 cgraph_allocate_init_indirect_info (void)
1025 struct cgraph_indirect_call_info *ii;
1027 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1028 ii->param_index = -1;
1032 /* Create an indirect edge with a yet-undetermined callee where the call
1033 statement destination is a formal parameter of the caller with index
1036 struct cgraph_edge *
1037 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1039 gcov_type count, int freq)
1041 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1044 edge->indirect_unknown_callee = 1;
1045 initialize_inline_failed (edge);
1047 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1048 edge->indirect_info->ecf_flags = ecf_flags;
1050 edge->next_callee = caller->indirect_calls;
1051 if (caller->indirect_calls)
1052 caller->indirect_calls->prev_callee = edge;
1053 caller->indirect_calls = edge;
1058 /* Remove the edge E from the list of the callers of the callee. */
1061 cgraph_edge_remove_callee (struct cgraph_edge *e)
1063 gcc_assert (!e->indirect_unknown_callee);
1065 e->prev_caller->next_caller = e->next_caller;
1067 e->next_caller->prev_caller = e->prev_caller;
1068 if (!e->prev_caller)
1069 e->callee->callers = e->next_caller;
1072 /* Remove the edge E from the list of the callees of the caller. */
1075 cgraph_edge_remove_caller (struct cgraph_edge *e)
1078 e->prev_callee->next_callee = e->next_callee;
1080 e->next_callee->prev_callee = e->prev_callee;
1081 if (!e->prev_callee)
1083 if (e->indirect_unknown_callee)
1084 e->caller->indirect_calls = e->next_callee;
1086 e->caller->callees = e->next_callee;
1088 if (e->caller->call_site_hash)
1089 htab_remove_elt_with_hash (e->caller->call_site_hash,
1091 htab_hash_pointer (e->call_stmt));
1094 /* Put the edge onto the free list. */
1097 cgraph_free_edge (struct cgraph_edge *e)
1101 /* Clear out the edge so we do not dangle pointers. */
1102 memset (e, 0, sizeof (*e));
1104 NEXT_FREE_EDGE (e) = free_edges;
1108 /* Remove the edge E in the cgraph. */
1111 cgraph_remove_edge (struct cgraph_edge *e)
1113 /* Call all edge removal hooks. */
1114 cgraph_call_edge_removal_hooks (e);
1116 if (!e->indirect_unknown_callee)
1117 /* Remove from callers list of the callee. */
1118 cgraph_edge_remove_callee (e);
1120 /* Remove from callees list of the callers. */
1121 cgraph_edge_remove_caller (e);
1123 /* Put the edge onto the free list. */
1124 cgraph_free_edge (e);
1127 /* Set callee of call graph edge E and add it to the corresponding set of
1131 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1133 e->prev_caller = NULL;
1135 n->callers->prev_caller = e;
1136 e->next_caller = n->callers;
1141 /* Redirect callee of E to N. The function does not update underlying
1145 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1147 /* Remove from callers list of the current callee. */
1148 cgraph_edge_remove_callee (e);
1150 /* Insert to callers list of the new callee. */
1151 cgraph_set_edge_callee (e, n);
1154 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1155 CALLEE. DELTA is an integer constant that is to be added to the this
1156 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1159 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1161 edge->indirect_unknown_callee = 0;
1163 /* Get the edge out of the indirect edge list. */
1164 if (edge->prev_callee)
1165 edge->prev_callee->next_callee = edge->next_callee;
1166 if (edge->next_callee)
1167 edge->next_callee->prev_callee = edge->prev_callee;
1168 if (!edge->prev_callee)
1169 edge->caller->indirect_calls = edge->next_callee;
1171 /* Put it into the normal callee list */
1172 edge->prev_callee = NULL;
1173 edge->next_callee = edge->caller->callees;
1174 if (edge->caller->callees)
1175 edge->caller->callees->prev_callee = edge;
1176 edge->caller->callees = edge;
1178 /* Insert to callers list of the new callee. */
1179 cgraph_set_edge_callee (edge, callee);
1181 if (edge->call_stmt)
1182 edge->call_stmt_cannot_inline_p
1183 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl);
1185 /* We need to re-determine the inlining status of the edge. */
1186 initialize_inline_failed (edge);
1190 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1191 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1192 of OLD_STMT if it was previously call statement.
1193 If NEW_STMT is NULL, the call has been dropped without any
1197 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1198 gimple old_stmt, tree old_call,
1201 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1202 ? gimple_call_fndecl (new_stmt) : 0;
1204 /* We are seeing indirect calls, then there is nothing to update. */
1205 if (!new_call && !old_call)
1207 /* See if we turned indirect call into direct call or folded call to one builtin
1208 into different builtin. */
1209 if (old_call != new_call)
1211 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1212 struct cgraph_edge *ne = NULL;
1218 /* See if the edge is already there and has the correct callee. It
1219 might be so because of indirect inlining has already updated
1220 it. We also might've cloned and redirected the edge. */
1221 if (new_call && e->callee)
1223 struct cgraph_node *callee = e->callee;
1226 if (callee->symbol.decl == new_call
1227 || callee->former_clone_of == new_call)
1229 callee = callee->clone_of;
1233 /* Otherwise remove edge and create new one; we can't simply redirect
1234 since function has changed, so inline plan and other information
1235 attached to edge is invalid. */
1237 frequency = e->frequency;
1238 cgraph_remove_edge (e);
1242 /* We are seeing new direct call; compute profile info based on BB. */
1243 basic_block bb = gimple_bb (new_stmt);
1245 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1251 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1252 new_stmt, count, frequency);
1253 gcc_assert (ne->inline_failed);
1256 /* We only updated the call stmt; update pointer in cgraph edge.. */
1257 else if (old_stmt != new_stmt)
1258 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1261 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1262 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1263 of OLD_STMT before it was updated (updating can happen inplace). */
1266 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1268 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1269 struct cgraph_node *node;
1271 gcc_checking_assert (orig);
1272 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1274 for (node = orig->clones; node != orig;)
1276 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1278 node = node->clones;
1279 else if (node->next_sibling_clone)
1280 node = node->next_sibling_clone;
1283 while (node != orig && !node->next_sibling_clone)
1284 node = node->clone_of;
1286 node = node->next_sibling_clone;
1292 /* Remove all callees from the node. */
1295 cgraph_node_remove_callees (struct cgraph_node *node)
1297 struct cgraph_edge *e, *f;
1299 /* It is sufficient to remove the edges from the lists of callers of
1300 the callees. The callee list of the node can be zapped with one
1302 for (e = node->callees; e; e = f)
1305 cgraph_call_edge_removal_hooks (e);
1306 if (!e->indirect_unknown_callee)
1307 cgraph_edge_remove_callee (e);
1308 cgraph_free_edge (e);
1310 for (e = node->indirect_calls; e; e = f)
1313 cgraph_call_edge_removal_hooks (e);
1314 if (!e->indirect_unknown_callee)
1315 cgraph_edge_remove_callee (e);
1316 cgraph_free_edge (e);
1318 node->indirect_calls = NULL;
1319 node->callees = NULL;
1320 if (node->call_site_hash)
1322 htab_delete (node->call_site_hash);
1323 node->call_site_hash = NULL;
1327 /* Remove all callers from the node. */
1330 cgraph_node_remove_callers (struct cgraph_node *node)
1332 struct cgraph_edge *e, *f;
1334 /* It is sufficient to remove the edges from the lists of callees of
1335 the callers. The caller list of the node can be zapped with one
1337 for (e = node->callers; e; e = f)
1340 cgraph_call_edge_removal_hooks (e);
1341 cgraph_edge_remove_caller (e);
1342 cgraph_free_edge (e);
1344 node->callers = NULL;
1347 /* Release memory used to represent body of function NODE. */
1350 cgraph_release_function_body (struct cgraph_node *node)
1352 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1354 tree old_decl = current_function_decl;
1355 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1359 cfun->curr_properties &= ~PROP_loops;
1360 loop_optimizer_finalize ();
1362 if (cfun->gimple_df)
1364 current_function_decl = node->symbol.decl;
1366 delete_tree_cfg_annotations ();
1368 current_function_decl = old_decl;
1372 gcc_assert (dom_computed[0] == DOM_NONE);
1373 gcc_assert (dom_computed[1] == DOM_NONE);
1376 if (cfun->value_histograms)
1379 gimple_set_body (node->symbol.decl, NULL);
1380 VEC_free (ipa_opt_pass, heap,
1381 node->ipa_transforms_to_apply);
1382 /* Struct function hangs a lot of data that would leak if we didn't
1383 removed all pointers to it. */
1384 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1385 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1387 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1388 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1389 of its associated function function declaration because it's
1390 needed to emit debug info later. */
1391 if (!node->abstract_and_needed)
1392 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1395 /* Remove the node from cgraph. */
1398 cgraph_remove_node (struct cgraph_node *node)
1401 bool kill_body = false;
1402 struct cgraph_node *n;
1403 int uid = node->uid;
1405 cgraph_call_node_removal_hooks (node);
1406 cgraph_node_remove_callers (node);
1407 cgraph_node_remove_callees (node);
1408 VEC_free (ipa_opt_pass, heap,
1409 node->ipa_transforms_to_apply);
1411 /* Incremental inlining access removed nodes stored in the postorder list.
1413 node->needed = node->reachable = false;
1414 for (n = node->nested; n; n = n->next_nested)
1416 node->nested = NULL;
1419 struct cgraph_node **node2 = &node->origin->nested;
1421 while (*node2 != node)
1422 node2 = &(*node2)->next_nested;
1423 *node2 = node->next_nested;
1425 symtab_unregister_node ((symtab_node)node);
1426 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1429 struct cgraph_node *next_inline_clone;
1431 for (next_inline_clone = node->clones;
1433 && next_inline_clone->symbol.decl != node->symbol.decl;
1434 next_inline_clone = next_inline_clone->next_sibling_clone)
1437 /* If there is inline clone of the node being removed, we need
1438 to put it into the position of removed node and reorganize all
1439 other clones to be based on it. */
1440 if (next_inline_clone)
1442 struct cgraph_node *n;
1443 struct cgraph_node *new_clones;
1445 *slot = next_inline_clone;
1447 /* Unlink inline clone from the list of clones of removed node. */
1448 if (next_inline_clone->next_sibling_clone)
1449 next_inline_clone->next_sibling_clone->prev_sibling_clone
1450 = next_inline_clone->prev_sibling_clone;
1451 if (next_inline_clone->prev_sibling_clone)
1453 gcc_assert (node->clones != next_inline_clone);
1454 next_inline_clone->prev_sibling_clone->next_sibling_clone
1455 = next_inline_clone->next_sibling_clone;
1459 gcc_assert (node->clones == next_inline_clone);
1460 node->clones = next_inline_clone->next_sibling_clone;
1463 new_clones = node->clones;
1464 node->clones = NULL;
1466 /* Copy clone info. */
1467 next_inline_clone->clone = node->clone;
1469 /* Now place it into clone tree at same level at NODE. */
1470 next_inline_clone->clone_of = node->clone_of;
1471 next_inline_clone->prev_sibling_clone = NULL;
1472 next_inline_clone->next_sibling_clone = NULL;
1475 if (node->clone_of->clones)
1476 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1477 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1478 node->clone_of->clones = next_inline_clone;
1481 /* Merge the clone list. */
1484 if (!next_inline_clone->clones)
1485 next_inline_clone->clones = new_clones;
1488 n = next_inline_clone->clones;
1489 while (n->next_sibling_clone)
1490 n = n->next_sibling_clone;
1491 n->next_sibling_clone = new_clones;
1492 new_clones->prev_sibling_clone = n;
1496 /* Update clone_of pointers. */
1500 n->clone_of = next_inline_clone;
1501 n = n->next_sibling_clone;
1506 htab_clear_slot (cgraph_hash, slot);
1511 if (node->prev_sibling_clone)
1512 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1513 else if (node->clone_of)
1514 node->clone_of->clones = node->next_sibling_clone;
1515 if (node->next_sibling_clone)
1516 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1519 struct cgraph_node *n, *next;
1523 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1524 n->clone_of = node->clone_of;
1525 n->clone_of = node->clone_of;
1526 n->next_sibling_clone = node->clone_of->clones;
1527 if (node->clone_of->clones)
1528 node->clone_of->clones->prev_sibling_clone = n;
1529 node->clone_of->clones = node->clones;
1533 /* We are removing node with clones. this makes clones inconsistent,
1534 but assume they will be removed subsequently and just keep clone
1535 tree intact. This can happen in unreachable function removal since
1536 we remove unreachable functions in random order, not by bottom-up
1537 walk of clone trees. */
1538 for (n = node->clones; n; n = next)
1540 next = n->next_sibling_clone;
1541 n->next_sibling_clone = NULL;
1542 n->prev_sibling_clone = NULL;
1548 /* While all the clones are removed after being proceeded, the function
1549 itself is kept in the cgraph even after it is compiled. Check whether
1550 we are done with this body and reclaim it proactively if this is the case.
1552 if (!kill_body && *slot)
1554 struct cgraph_node *n = (struct cgraph_node *) *slot;
1555 if (!n->clones && !n->clone_of && !n->global.inlined_to
1556 && (cgraph_global_info_ready
1557 && (TREE_ASM_WRITTEN (n->symbol.decl)
1558 || DECL_EXTERNAL (n->symbol.decl)
1559 || n->symbol.in_other_partition)))
1562 if (assembler_name_hash)
1564 tree name = DECL_ASSEMBLER_NAME (node->symbol.decl);
1565 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1566 decl_assembler_name_hash (name),
1568 /* Inline clones are not hashed. */
1569 if (slot && *slot == node)
1570 htab_clear_slot (assembler_name_hash, slot);
1574 cgraph_release_function_body (node);
1575 node->symbol.decl = NULL;
1576 if (node->call_site_hash)
1578 htab_delete (node->call_site_hash);
1579 node->call_site_hash = NULL;
1583 /* Clear out the node to NULL all pointers and add the node to the free
1585 memset (node, 0, sizeof(*node));
1586 node->symbol.type = SYMTAB_FUNCTION;
1588 SET_NEXT_FREE_NODE (node, free_nodes);
1592 /* Add NEW_ to the same comdat group that OLD is in. */
1595 cgraph_add_to_same_comdat_group (struct cgraph_node *new_node,
1596 struct cgraph_node *old_node)
1598 gcc_assert (DECL_ONE_ONLY (old_node->symbol.decl));
1599 gcc_assert (!new_node->symbol.same_comdat_group);
1600 gcc_assert (new_node != old_node);
1602 DECL_COMDAT_GROUP (new_node->symbol.decl) = DECL_COMDAT_GROUP (old_node->symbol.decl);
1603 new_node->symbol.same_comdat_group = (symtab_node)old_node;
1604 if (!old_node->symbol.same_comdat_group)
1605 old_node->symbol.same_comdat_group = (symtab_node)new_node;
1609 for (n = old_node->symbol.same_comdat_group;
1610 n->symbol.same_comdat_group != (symtab_node)old_node;
1611 n = n->symbol.same_comdat_group)
1613 n->symbol.same_comdat_group = (symtab_node)new_node;
1617 /* Remove the node from cgraph and all inline clones inlined into it.
1618 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
1619 removed. This allows to call the function from outer loop walking clone
1623 cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
1625 struct cgraph_edge *e, *next;
1628 if (node == forbidden_node)
1630 for (e = node->callees; e; e = next)
1632 next = e->next_callee;
1633 if (!e->inline_failed)
1634 found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
1636 cgraph_remove_node (node);
1640 /* Notify finalize_compilation_unit that given node is reachable. */
1643 cgraph_mark_reachable_node (struct cgraph_node *node)
1645 if (!node->reachable && node->local.finalized)
1647 if (cgraph_global_info_ready)
1649 /* Verify that function does not appear to be needed out of blue
1650 during the optimization process. This can happen for extern
1651 inlines when bodies was removed after inlining. */
1652 gcc_assert ((node->analyzed || node->symbol.in_other_partition
1653 || DECL_EXTERNAL (node->symbol.decl)));
1656 notice_global_symbol (node->symbol.decl);
1657 node->reachable = 1;
1659 node->next_needed = cgraph_nodes_queue;
1660 x_cgraph_nodes_queue = (symtab_node)node;
1664 /* Likewise indicate that a node is needed, i.e. reachable via some
1668 cgraph_mark_needed_node (struct cgraph_node *node)
1671 gcc_assert (!node->global.inlined_to);
1672 cgraph_mark_reachable_node (node);
1675 /* Likewise indicate that a node is having address taken. */
1678 cgraph_mark_address_taken_node (struct cgraph_node *node)
1680 gcc_assert (!node->global.inlined_to);
1681 cgraph_mark_reachable_node (node);
1682 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1683 IPA_REF_ADDR reference exists (and thus it should be set on node
1684 representing alias we take address of) and as a test whether address
1685 of the object was taken (and thus it should be set on node alias is
1686 referring to). We should remove the first use and the remove the
1688 node->symbol.address_taken = 1;
1689 node = cgraph_function_or_thunk_node (node, NULL);
1690 node->symbol.address_taken = 1;
1693 /* Return local info for the compiled function. */
1695 struct cgraph_local_info *
1696 cgraph_local_info (tree decl)
1698 struct cgraph_node *node;
1700 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1701 node = cgraph_get_node (decl);
1704 return &node->local;
1707 /* Return local info for the compiled function. */
1709 struct cgraph_global_info *
1710 cgraph_global_info (tree decl)
1712 struct cgraph_node *node;
1714 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1715 node = cgraph_get_node (decl);
1718 return &node->global;
1721 /* Return local info for the compiled function. */
1723 struct cgraph_rtl_info *
1724 cgraph_rtl_info (tree decl)
1726 struct cgraph_node *node;
1728 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1729 node = cgraph_get_node (decl);
1731 || (decl != current_function_decl
1732 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1737 /* Return a string describing the failure REASON. */
1740 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1743 #define DEFCIFCODE(code, string) string,
1745 static const char *cif_string_table[CIF_N_REASONS] = {
1746 #include "cif-code.def"
1749 /* Signedness of an enum type is implementation defined, so cast it
1750 to unsigned before testing. */
1751 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1752 return cif_string_table[reason];
1755 /* Return name of the node used in debug output. */
1757 cgraph_node_name (struct cgraph_node *node)
1759 return lang_hooks.decl_printable_name (node->symbol.decl, 2);
1762 /* Names used to print out the availability enum. */
1763 const char * const cgraph_availability_names[] =
1764 {"unset", "not_available", "overwritable", "available", "local"};
1767 /* Dump call graph node NODE to file F. */
1770 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1772 struct cgraph_edge *edge;
1773 int indirect_calls_count = 0;
1775 fprintf (f, "%s/%i", cgraph_node_name (node), node->uid);
1776 dump_addr (f, " @", (void *)node);
1777 if (DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
1778 fprintf (f, " (asm: %s)",
1779 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl)));
1780 if (node->global.inlined_to)
1781 fprintf (f, " (inline copy in %s/%i)",
1782 cgraph_node_name (node->global.inlined_to),
1783 node->global.inlined_to->uid);
1784 if (node->symbol.same_comdat_group)
1785 fprintf (f, " (same comdat group as %s/%i)",
1786 cgraph_node_name (cgraph (node->symbol.same_comdat_group)),
1787 cgraph (node->symbol.same_comdat_group)->uid);
1789 fprintf (f, " (clone of %s/%i)",
1790 cgraph_node_name (node->clone_of),
1791 node->clone_of->uid);
1792 if (cgraph_function_flags_ready)
1793 fprintf (f, " availability:%s",
1794 cgraph_availability_names [cgraph_function_body_availability (node)]);
1796 fprintf (f, " analyzed");
1797 if (node->symbol.in_other_partition)
1798 fprintf (f, " in_other_partition");
1800 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1801 (HOST_WIDEST_INT)node->count);
1803 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1805 fprintf (f, " needed");
1806 if (node->symbol.address_taken)
1807 fprintf (f, " address_taken");
1808 else if (node->reachable)
1809 fprintf (f, " reachable");
1810 else if (node->symbol.used_from_other_partition)
1811 fprintf (f, " used_from_other_partition");
1812 if (gimple_has_body_p (node->symbol.decl))
1813 fprintf (f, " body");
1815 fprintf (f, " process");
1816 if (node->local.local)
1817 fprintf (f, " local");
1818 if (node->symbol.externally_visible)
1819 fprintf (f, " externally_visible");
1820 if (node->symbol.resolution != LDPR_UNKNOWN)
1822 ld_plugin_symbol_resolution_names[(int)node->symbol.resolution]);
1823 if (node->local.finalized)
1824 fprintf (f, " finalized");
1825 if (node->local.redefined_extern_inline)
1826 fprintf (f, " redefined_extern_inline");
1827 if (TREE_ASM_WRITTEN (node->symbol.decl))
1828 fprintf (f, " asm_written");
1829 if (node->only_called_at_startup)
1830 fprintf (f, " only_called_at_startup");
1831 if (node->only_called_at_exit)
1832 fprintf (f, " only_called_at_exit");
1833 else if (node->alias)
1834 fprintf (f, " alias");
1836 fprintf (f, " tm_clone");
1840 if (node->thunk.thunk_p)
1842 fprintf (f, " thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1843 "virtual offset %i)\n",
1844 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1845 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1846 (int)node->thunk.fixed_offset,
1847 (int)node->thunk.virtual_value,
1848 (int)node->thunk.virtual_offset_p);
1850 if (node->alias && node->thunk.alias)
1852 fprintf (f, " alias of %s",
1853 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1854 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1855 fprintf (f, " (asm: %s)",
1856 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1860 fprintf (f, " called by: ");
1862 for (edge = node->callers; edge; edge = edge->next_caller)
1864 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1867 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1868 (HOST_WIDEST_INT)edge->count);
1869 if (edge->frequency)
1870 fprintf (f, "(%.2f per call) ",
1871 edge->frequency / (double)CGRAPH_FREQ_BASE);
1872 if (!edge->inline_failed)
1873 fprintf(f, "(inlined) ");
1874 if (edge->indirect_inlining_edge)
1875 fprintf(f, "(indirect_inlining) ");
1876 if (edge->can_throw_external)
1877 fprintf(f, "(can throw external) ");
1880 fprintf (f, "\n calls: ");
1881 for (edge = node->callees; edge; edge = edge->next_callee)
1883 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1885 if (!edge->inline_failed)
1886 fprintf(f, "(inlined) ");
1887 if (edge->indirect_inlining_edge)
1888 fprintf(f, "(indirect_inlining) ");
1890 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1891 (HOST_WIDEST_INT)edge->count);
1892 if (edge->frequency)
1893 fprintf (f, "(%.2f per call) ",
1894 edge->frequency / (double)CGRAPH_FREQ_BASE);
1895 if (edge->can_throw_external)
1896 fprintf(f, "(can throw external) ");
1899 fprintf (f, " References: ");
1900 ipa_dump_references (f, &node->symbol.ref_list);
1901 fprintf (f, " Refering this function: ");
1902 ipa_dump_refering (f, &node->symbol.ref_list);
1904 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1905 indirect_calls_count++;
1906 if (indirect_calls_count)
1907 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1908 indirect_calls_count);
1912 /* Dump call graph node NODE to stderr. */
1915 debug_cgraph_node (struct cgraph_node *node)
1917 dump_cgraph_node (stderr, node);
1921 /* Dump the callgraph to file F. */
1924 dump_cgraph (FILE *f)
1926 struct cgraph_node *node;
1928 fprintf (f, "callgraph:\n\n");
1929 FOR_EACH_FUNCTION (node)
1930 dump_cgraph_node (f, node);
1934 /* Dump the call graph to stderr. */
1939 dump_cgraph (stderr);
1943 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
1946 change_decl_assembler_name (tree decl, tree name)
1948 struct cgraph_node *node;
1950 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1951 SET_DECL_ASSEMBLER_NAME (decl, name);
1954 if (name == DECL_ASSEMBLER_NAME (decl))
1957 if (assembler_name_hash
1958 && TREE_CODE (decl) == FUNCTION_DECL
1959 && (node = cgraph_get_node (decl)) != NULL)
1961 tree old_name = DECL_ASSEMBLER_NAME (decl);
1962 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
1963 decl_assembler_name_hash (old_name),
1965 /* Inline clones are not hashed. */
1966 if (slot && *slot == node)
1967 htab_clear_slot (assembler_name_hash, slot);
1969 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1970 && DECL_RTL_SET_P (decl))
1971 warning (0, "%D renamed after being referenced in assembly", decl);
1973 SET_DECL_ASSEMBLER_NAME (decl, name);
1975 if (assembler_name_hash
1976 && TREE_CODE (decl) == FUNCTION_DECL
1977 && (node = cgraph_get_node (decl)) != NULL)
1979 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1980 decl_assembler_name_hash (name),
1982 gcc_assert (!*slot);
1987 /* Add a top-level asm statement to the list. */
1989 struct cgraph_asm_node *
1990 cgraph_add_asm_node (tree asm_str)
1992 struct cgraph_asm_node *node;
1994 node = ggc_alloc_cleared_cgraph_asm_node ();
1995 node->asm_str = asm_str;
1996 node->order = symtab_order++;
1998 if (cgraph_asm_nodes == NULL)
1999 cgraph_asm_nodes = node;
2001 cgraph_asm_last_node->next = node;
2002 cgraph_asm_last_node = node;
2006 /* Return true when the DECL can possibly be inlined. */
2008 cgraph_function_possibly_inlined_p (tree decl)
2010 if (!cgraph_global_info_ready)
2011 return !DECL_UNINLINABLE (decl);
2012 return DECL_POSSIBLY_INLINED (decl);
2015 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2016 struct cgraph_edge *
2017 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2018 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2019 int freq_scale, bool update_original)
2021 struct cgraph_edge *new_edge;
2022 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2025 /* We do not want to ignore loop nest after frequency drops to 0. */
2028 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2029 if (freq > CGRAPH_FREQ_MAX)
2030 freq = CGRAPH_FREQ_MAX;
2032 if (e->indirect_unknown_callee)
2036 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2038 struct cgraph_node *callee = cgraph_get_node (decl);
2039 gcc_checking_assert (callee);
2040 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
2044 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2045 e->indirect_info->ecf_flags,
2047 *new_edge->indirect_info = *e->indirect_info;
2052 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
2053 if (e->indirect_info)
2055 new_edge->indirect_info
2056 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2057 *new_edge->indirect_info = *e->indirect_info;
2061 new_edge->inline_failed = e->inline_failed;
2062 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2063 new_edge->lto_stmt_uid = stmt_uid;
2064 /* Clone flags that depend on call_stmt availability manually. */
2065 new_edge->can_throw_external = e->can_throw_external;
2066 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2067 if (update_original)
2069 e->count -= new_edge->count;
2073 cgraph_call_edge_duplication_hooks (e, new_edge);
2078 /* Create node representing clone of N executed COUNT times. Decrease
2079 the execution counts from original node too.
2080 The new clone will have decl set to DECL that may or may not be the same
2083 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2084 function's profile to reflect the fact that part of execution is handled
2086 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
2087 the new clone. Otherwise the caller is responsible for doing so later. */
2089 struct cgraph_node *
2090 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2091 bool update_original,
2092 VEC(cgraph_edge_p,heap) *redirect_callers,
2093 bool call_duplication_hook)
2095 struct cgraph_node *new_node = cgraph_create_node_1 ();
2096 struct cgraph_edge *e;
2097 gcov_type count_scale;
2100 new_node->symbol.decl = decl;
2101 symtab_register_node ((symtab_node)new_node);
2102 new_node->origin = n->origin;
2103 if (new_node->origin)
2105 new_node->next_nested = new_node->origin->nested;
2106 new_node->origin->nested = new_node;
2108 new_node->analyzed = n->analyzed;
2109 new_node->local = n->local;
2110 new_node->symbol.externally_visible = false;
2111 new_node->local.local = true;
2112 new_node->global = n->global;
2113 new_node->rtl = n->rtl;
2114 new_node->count = count;
2115 new_node->frequency = n->frequency;
2116 new_node->clone = n->clone;
2117 new_node->clone.tree_map = 0;
2120 if (new_node->count > n->count)
2121 count_scale = REG_BR_PROB_BASE;
2123 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2127 if (update_original)
2134 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2136 /* Redirect calls to the old version node to point to its new
2138 cgraph_redirect_edge_callee (e, new_node);
2142 for (e = n->callees;e; e=e->next_callee)
2143 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2144 count_scale, freq, update_original);
2146 for (e = n->indirect_calls; e; e = e->next_callee)
2147 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2148 count_scale, freq, update_original);
2149 ipa_clone_references (new_node, NULL, &n->symbol.ref_list);
2151 new_node->next_sibling_clone = n->clones;
2153 n->clones->prev_sibling_clone = new_node;
2154 n->clones = new_node;
2155 new_node->clone_of = n;
2157 if (n->symbol.decl != decl)
2159 struct cgraph_node **slot;
2160 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2161 gcc_assert (!*slot);
2163 if (assembler_name_hash)
2166 tree name = DECL_ASSEMBLER_NAME (decl);
2168 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2169 decl_assembler_name_hash (name),
2171 gcc_assert (!*aslot);
2176 if (call_duplication_hook)
2177 cgraph_call_node_duplication_hooks (n, new_node);
2181 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2183 static GTY(()) unsigned int clone_fn_id_num;
2186 clone_function_name (tree decl, const char *suffix)
2188 tree name = DECL_ASSEMBLER_NAME (decl);
2189 size_t len = IDENTIFIER_LENGTH (name);
2190 char *tmp_name, *prefix;
2192 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2193 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2194 strcpy (prefix + len + 1, suffix);
2195 #ifndef NO_DOT_IN_LABEL
2197 #elif !defined NO_DOLLAR_IN_LABEL
2202 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2203 return get_identifier (tmp_name);
2206 /* Create callgraph node clone with new declaration. The actual body will
2207 be copied later at compilation stage.
2209 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2212 struct cgraph_node *
2213 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2214 VEC(cgraph_edge_p,heap) *redirect_callers,
2215 VEC(ipa_replace_map_p,gc) *tree_map,
2216 bitmap args_to_skip,
2217 const char * suffix)
2219 tree old_decl = old_node->symbol.decl;
2220 struct cgraph_node *new_node = NULL;
2223 struct ipa_replace_map *map;
2226 gcc_checking_assert (tree_versionable_function_p (old_decl));
2228 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2230 /* Make a new FUNCTION_DECL tree node */
2232 new_decl = copy_node (old_decl);
2234 new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
2235 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2237 /* Generate a new name for the new version. */
2238 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2239 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2240 SET_DECL_RTL (new_decl, NULL);
2242 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2243 CGRAPH_FREQ_BASE, false,
2244 redirect_callers, false);
2245 /* Update the properties.
2246 Make clone visible only within this translation unit. Make sure
2247 that is not weak also.
2248 ??? We cannot use COMDAT linkage because there is no
2249 ABI support for this. */
2250 DECL_EXTERNAL (new_node->symbol.decl) = 0;
2251 if (DECL_ONE_ONLY (old_decl))
2252 DECL_SECTION_NAME (new_node->symbol.decl) = NULL;
2253 DECL_COMDAT_GROUP (new_node->symbol.decl) = 0;
2254 TREE_PUBLIC (new_node->symbol.decl) = 0;
2255 DECL_COMDAT (new_node->symbol.decl) = 0;
2256 DECL_WEAK (new_node->symbol.decl) = 0;
2257 DECL_STATIC_CONSTRUCTOR (new_node->symbol.decl) = 0;
2258 DECL_STATIC_DESTRUCTOR (new_node->symbol.decl) = 0;
2259 new_node->clone.tree_map = tree_map;
2260 new_node->clone.args_to_skip = args_to_skip;
2261 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2263 tree var = map->new_tree;
2266 if (TREE_CODE (var) != ADDR_EXPR)
2268 var = get_base_var (var);
2272 /* Record references of the future statement initializing the constant
2274 if (TREE_CODE (var) == FUNCTION_DECL)
2276 struct cgraph_node *ref_node = cgraph_get_node (var);
2277 gcc_checking_assert (ref_node);
2278 ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2281 else if (TREE_CODE (var) == VAR_DECL)
2282 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2283 IPA_REF_ADDR, NULL);
2286 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2287 else if (old_node->clone.combined_args_to_skip)
2289 int newi = 0, oldi = 0;
2291 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2292 struct cgraph_node *orig_node;
2293 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2295 for (arg = DECL_ARGUMENTS (orig_node->symbol.decl);
2296 arg; arg = DECL_CHAIN (arg), oldi++)
2298 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2300 bitmap_set_bit (new_args_to_skip, oldi);
2303 if (bitmap_bit_p (args_to_skip, newi))
2304 bitmap_set_bit (new_args_to_skip, oldi);
2307 new_node->clone.combined_args_to_skip = new_args_to_skip;
2310 new_node->clone.combined_args_to_skip = args_to_skip;
2311 new_node->symbol.externally_visible = 0;
2312 new_node->local.local = 1;
2313 new_node->lowered = true;
2314 new_node->reachable = true;
2316 cgraph_call_node_duplication_hooks (old_node, new_node);
2322 /* NODE is no longer nested function; update cgraph accordingly. */
2324 cgraph_unnest_node (struct cgraph_node *node)
2326 struct cgraph_node **node2 = &node->origin->nested;
2327 gcc_assert (node->origin);
2329 while (*node2 != node)
2330 node2 = &(*node2)->next_nested;
2331 *node2 = node->next_nested;
2332 node->origin = NULL;
2335 /* Return function availability. See cgraph.h for description of individual
2338 cgraph_function_body_availability (struct cgraph_node *node)
2340 enum availability avail;
2341 gcc_assert (cgraph_function_flags_ready);
2342 if (!node->analyzed)
2343 avail = AVAIL_NOT_AVAILABLE;
2344 else if (node->local.local)
2345 avail = AVAIL_LOCAL;
2346 else if (!node->symbol.externally_visible)
2347 avail = AVAIL_AVAILABLE;
2348 /* Inline functions are safe to be analyzed even if their symbol can
2349 be overwritten at runtime. It is not meaningful to enforce any sane
2350 behaviour on replacing inline function by different body. */
2351 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
2352 avail = AVAIL_AVAILABLE;
2354 /* If the function can be overwritten, return OVERWRITABLE. Take
2355 care at least of two notable extensions - the COMDAT functions
2356 used to share template instantiations in C++ (this is symmetric
2357 to code cp_cannot_inline_tree_fn and probably shall be shared and
2358 the inlinability hooks completely eliminated).
2360 ??? Does the C++ one definition rule allow us to always return
2361 AVAIL_AVAILABLE here? That would be good reason to preserve this
2364 else if (decl_replaceable_p (node->symbol.decl)
2365 && !DECL_EXTERNAL (node->symbol.decl))
2366 avail = AVAIL_OVERWRITABLE;
2367 else avail = AVAIL_AVAILABLE;
2372 /* Worker for cgraph_node_can_be_local_p. */
2374 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2375 void *data ATTRIBUTE_UNUSED)
2377 return !(!node->needed
2378 && ((DECL_COMDAT (node->symbol.decl)
2379 && !node->symbol.same_comdat_group)
2380 || !node->symbol.externally_visible));
2383 /* Return true if NODE can be made local for API change.
2384 Extern inline functions and C++ COMDAT functions can be made local
2385 at the expense of possible code size growth if function is used in multiple
2386 compilation units. */
2388 cgraph_node_can_be_local_p (struct cgraph_node *node)
2390 return (!node->symbol.address_taken
2391 && !cgraph_for_node_and_aliases (node,
2392 cgraph_node_cannot_be_local_p_1,
2396 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2397 but other code such as notice_global_symbol generates rtl. */
2399 cgraph_make_decl_local (tree decl)
2403 if (TREE_CODE (decl) == VAR_DECL)
2404 DECL_COMMON (decl) = 0;
2405 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2407 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
2409 /* It is possible that we are linking against library defining same COMDAT
2410 function. To avoid conflict we need to rename our local name of the
2411 function just in the case WHOPR partitioning decide to make it hidden
2412 to avoid cross partition references. */
2415 const char *old_name;
2417 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2418 if (TREE_CODE (decl) == FUNCTION_DECL)
2420 struct cgraph_node *node = cgraph_get_node (decl);
2421 change_decl_assembler_name (decl,
2422 clone_function_name (decl, "local"));
2423 if (node->symbol.lto_file_data)
2424 lto_record_renamed_decl (node->symbol.lto_file_data,
2427 (DECL_ASSEMBLER_NAME (decl)));
2429 else if (TREE_CODE (decl) == VAR_DECL)
2431 struct varpool_node *vnode = varpool_get_node (decl);
2432 /* change_decl_assembler_name will warn here on vtables because
2433 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2434 SET_DECL_ASSEMBLER_NAME (decl,
2435 clone_function_name (decl, "local"));
2436 if (vnode->symbol.lto_file_data)
2437 lto_record_renamed_decl (vnode->symbol.lto_file_data,
2440 (DECL_ASSEMBLER_NAME (decl)));
2443 DECL_SECTION_NAME (decl) = 0;
2444 DECL_COMDAT (decl) = 0;
2446 DECL_COMDAT_GROUP (decl) = 0;
2447 DECL_WEAK (decl) = 0;
2448 DECL_EXTERNAL (decl) = 0;
2449 TREE_PUBLIC (decl) = 0;
2450 if (!DECL_RTL_SET_P (decl))
2453 /* Update rtl flags. */
2454 make_decl_rtl (decl);
2456 rtl = DECL_RTL (decl);
2460 symbol = XEXP (rtl, 0);
2461 if (GET_CODE (symbol) != SYMBOL_REF)
2464 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2467 /* Call calback on NODE, thunks and aliases asociated to NODE.
2468 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2472 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2473 bool (*callback) (struct cgraph_node *, void *),
2475 bool include_overwritable)
2477 struct cgraph_edge *e;
2479 struct ipa_ref *ref;
2481 if (callback (node, data))
2483 for (e = node->callers; e; e = e->next_caller)
2484 if (e->caller->thunk.thunk_p
2485 && (include_overwritable
2486 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2487 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2488 include_overwritable))
2490 for (i = 0; ipa_ref_list_refering_iterate (&node->symbol.ref_list, i, ref); i++)
2491 if (ref->use == IPA_REF_ALIAS)
2493 struct cgraph_node *alias = ipa_ref_refering_node (ref);
2494 if (include_overwritable
2495 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2496 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2497 include_overwritable))
2503 /* Call calback on NODE and aliases asociated to NODE.
2504 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2508 cgraph_for_node_and_aliases (struct cgraph_node *node,
2509 bool (*callback) (struct cgraph_node *, void *),
2511 bool include_overwritable)
2514 struct ipa_ref *ref;
2516 if (callback (node, data))
2518 for (i = 0; ipa_ref_list_refering_iterate (&node->symbol.ref_list, i, ref); i++)
2519 if (ref->use == IPA_REF_ALIAS)
2521 struct cgraph_node *alias = ipa_ref_refering_node (ref);
2522 if (include_overwritable
2523 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2524 if (cgraph_for_node_and_aliases (alias, callback, data,
2525 include_overwritable))
2531 /* Worker to bring NODE local. */
2534 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2536 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2537 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
2539 cgraph_make_decl_local (node->symbol.decl);
2541 node->symbol.externally_visible = false;
2542 node->local.local = true;
2543 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
2544 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2549 /* Bring NODE local. */
2552 cgraph_make_node_local (struct cgraph_node *node)
2554 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2558 /* Worker to set nothrow flag. */
2561 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2563 struct cgraph_edge *e;
2565 TREE_NOTHROW (node->symbol.decl) = data != NULL;
2568 for (e = node->callers; e; e = e->next_caller)
2569 e->can_throw_external = false;
2573 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2574 if any to NOTHROW. */
2577 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2579 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2580 (void *)(size_t)nothrow, false);
2583 /* Worker to set const flag. */
2586 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2588 /* Static constructors and destructors without a side effect can be
2590 if (data && !((size_t)data & 2))
2592 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
2593 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
2594 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2595 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
2597 TREE_READONLY (node->symbol.decl) = data != NULL;
2598 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
2602 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2603 if any to READONLY. */
2606 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2608 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2609 (void *)(size_t)(readonly + (int)looping * 2),
2613 /* Worker to set pure flag. */
2616 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2618 /* Static pureructors and destructors without a side effect can be
2620 if (data && !((size_t)data & 2))
2622 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
2623 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
2624 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2625 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
2627 DECL_PURE_P (node->symbol.decl) = data != NULL;
2628 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
2632 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2636 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2638 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2639 (void *)(size_t)(pure + (int)looping * 2),
2643 /* Data used by cgraph_propagate_frequency. */
2645 struct cgraph_propagate_frequency_data
2647 bool maybe_unlikely_executed;
2648 bool maybe_executed_once;
2649 bool only_called_at_startup;
2650 bool only_called_at_exit;
2653 /* Worker for cgraph_propagate_frequency_1. */
2656 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
2658 struct cgraph_propagate_frequency_data *d;
2659 struct cgraph_edge *edge;
2661 d = (struct cgraph_propagate_frequency_data *)data;
2662 for (edge = node->callers;
2663 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
2664 || d->only_called_at_startup || d->only_called_at_exit);
2665 edge = edge->next_caller)
2667 if (edge->caller != node)
2669 d->only_called_at_startup &= edge->caller->only_called_at_startup;
2670 /* It makes sense to put main() together with the static constructors.
2671 It will be executed for sure, but rest of functions called from
2672 main are definitely not at startup only. */
2673 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
2674 d->only_called_at_startup = 0;
2675 d->only_called_at_exit &= edge->caller->only_called_at_exit;
2677 if (!edge->frequency)
2679 switch (edge->caller->frequency)
2681 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2683 case NODE_FREQUENCY_EXECUTED_ONCE:
2684 if (dump_file && (dump_flags & TDF_DETAILS))
2685 fprintf (dump_file, " Called by %s that is executed once\n",
2686 cgraph_node_name (edge->caller));
2687 d->maybe_unlikely_executed = false;
2688 if (inline_edge_summary (edge)->loop_depth)
2690 d->maybe_executed_once = false;
2691 if (dump_file && (dump_flags & TDF_DETAILS))
2692 fprintf (dump_file, " Called in loop\n");
2695 case NODE_FREQUENCY_HOT:
2696 case NODE_FREQUENCY_NORMAL:
2697 if (dump_file && (dump_flags & TDF_DETAILS))
2698 fprintf (dump_file, " Called by %s that is normal or hot\n",
2699 cgraph_node_name (edge->caller));
2700 d->maybe_unlikely_executed = false;
2701 d->maybe_executed_once = false;
2705 return edge != NULL;
2708 /* See if the frequency of NODE can be updated based on frequencies of its
2711 cgraph_propagate_frequency (struct cgraph_node *node)
2713 struct cgraph_propagate_frequency_data d = {true, true, true, true};
2714 bool changed = false;
2716 if (!node->local.local)
2718 gcc_assert (node->analyzed);
2719 if (dump_file && (dump_flags & TDF_DETAILS))
2720 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2722 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
2724 if ((d.only_called_at_startup && !d.only_called_at_exit)
2725 && !node->only_called_at_startup)
2727 node->only_called_at_startup = true;
2729 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2730 cgraph_node_name (node));
2733 if ((d.only_called_at_exit && !d.only_called_at_startup)
2734 && !node->only_called_at_exit)
2736 node->only_called_at_exit = true;
2738 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2739 cgraph_node_name (node));
2742 /* These come either from profile or user hints; never update them. */
2743 if (node->frequency == NODE_FREQUENCY_HOT
2744 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2746 if (d.maybe_unlikely_executed)
2748 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2750 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2751 cgraph_node_name (node));
2754 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2756 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2758 fprintf (dump_file, "Node %s promoted to executed once.\n",
2759 cgraph_node_name (node));
2765 /* Return true when NODE can not return or throw and thus
2766 it is safe to ignore its side effects for IPA analysis. */
2769 cgraph_node_cannot_return (struct cgraph_node *node)
2771 int flags = flags_from_decl_or_type (node->symbol.decl);
2772 if (!flag_exceptions)
2773 return (flags & ECF_NORETURN) != 0;
2775 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2776 == (ECF_NORETURN | ECF_NOTHROW));
2779 /* Return true when call of E can not lead to return from caller
2780 and thus it is safe to ignore its side effects for IPA analysis
2781 when computing side effects of the caller.
2782 FIXME: We could actually mark all edges that have no reaching
2783 patch to EXIT_BLOCK_PTR or throw to get better results. */
2785 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2787 if (cgraph_node_cannot_return (e->caller))
2789 if (e->indirect_unknown_callee)
2791 int flags = e->indirect_info->ecf_flags;
2792 if (!flag_exceptions)
2793 return (flags & ECF_NORETURN) != 0;
2795 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2796 == (ECF_NORETURN | ECF_NOTHROW));
2799 return cgraph_node_cannot_return (e->callee);
2802 /* Return true when function NODE can be removed from callgraph
2803 if all direct calls are eliminated. */
2806 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2808 gcc_assert (!node->global.inlined_to);
2809 /* Extern inlines can always go, we will use the external definition. */
2810 if (DECL_EXTERNAL (node->symbol.decl))
2812 /* When function is needed, we can not remove it. */
2813 if (node->needed || node->symbol.used_from_other_partition)
2815 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
2816 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2818 /* Only COMDAT functions can be removed if externally visible. */
2819 if (node->symbol.externally_visible
2820 && (!DECL_COMDAT (node->symbol.decl)
2821 || cgraph_used_from_object_file_p (node)))
2826 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2829 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2831 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2834 /* Return true when function NODE and its aliases can be removed from callgraph
2835 if all direct calls are eliminated. */
2838 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2840 /* Extern inlines can always go, we will use the external definition. */
2841 if (DECL_EXTERNAL (node->symbol.decl))
2843 if (node->symbol.address_taken)
2845 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2848 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2851 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2853 return cgraph_used_from_object_file_p (node);
2856 /* Return true when function NODE can be expected to be removed
2857 from program when direct calls in this compilation unit are removed.
2859 As a special case COMDAT functions are
2860 cgraph_can_remove_if_no_direct_calls_p while the are not
2861 cgraph_only_called_directly_p (it is possible they are called from other
2864 This function behaves as cgraph_only_called_directly_p because eliminating
2865 all uses of COMDAT function does not make it necessarily disappear from
2866 the program unless we are compiling whole program or we do LTO. In this
2867 case we know we win since dynamic linking will not really discard the
2868 linkonce section. */
2871 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2873 gcc_assert (!node->global.inlined_to);
2874 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2876 if (!in_lto_p && !flag_whole_program)
2877 return cgraph_only_called_directly_p (node);
2880 if (DECL_EXTERNAL (node->symbol.decl))
2882 return cgraph_can_remove_if_no_direct_calls_p (node);
2886 /* Return true when RESOLUTION indicate that linker will use
2887 the symbol from non-LTO object files. */
2890 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2892 return (resolution == LDPR_PREVAILING_DEF
2893 || resolution == LDPR_PREEMPTED_REG
2894 || resolution == LDPR_RESOLVED_EXEC
2895 || resolution == LDPR_RESOLVED_DYN);
2899 /* Return true when NODE is known to be used from other (non-LTO) object file.
2900 Known only when doing LTO via linker plugin. */
2903 cgraph_used_from_object_file_p (struct cgraph_node *node)
2905 gcc_assert (!node->global.inlined_to);
2906 if (!TREE_PUBLIC (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
2908 if (resolution_used_from_other_file_p (node->symbol.resolution))
2913 /* Worker for cgraph_only_called_directly_p. */
2916 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2918 return !cgraph_only_called_directly_or_aliased_p (node);
2921 /* Return true when function NODE and all its aliases are only called
2923 i.e. it is not externally visible, address was not taken and
2924 it is not used in any other non-standard way. */
2927 cgraph_only_called_directly_p (struct cgraph_node *node)
2929 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2930 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2935 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2938 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2940 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
2941 struct cgraph_edge *cs;
2942 enum availability avail;
2943 cgraph_function_or_thunk_node (node, &avail);
2945 if (avail > AVAIL_OVERWRITABLE)
2946 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2947 if (!cs->indirect_inlining_edge)
2948 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
2952 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2953 (i.e. are not overwritable). */
2955 VEC (cgraph_edge_p, heap) *
2956 collect_callers_of_node (struct cgraph_node *node)
2958 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
2959 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2960 &redirect_callers, false);
2961 return redirect_callers;
2964 #include "gt-cgraph.h"