1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 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"
103 const char * const ld_plugin_symbol_resolution_names[]=
108 "prevailing_def_ironly",
116 static void cgraph_node_remove_callers (struct cgraph_node *node);
117 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
118 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
120 /* Hash table used to convert declarations into nodes. */
121 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
122 /* Hash table used to convert assembler names into nodes. */
123 static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
125 /* The linked list of cgraph nodes. */
126 struct cgraph_node *cgraph_nodes;
128 /* Queue of cgraph nodes scheduled to be lowered. */
129 struct cgraph_node *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 /* The order index of the next cgraph node to be created. This is
161 used so that we can sort the cgraph nodes in order by when we saw
162 them, to support -fno-toplevel-reorder. */
165 /* List of hooks triggered on cgraph_edge events. */
166 struct cgraph_edge_hook_list {
167 cgraph_edge_hook hook;
169 struct cgraph_edge_hook_list *next;
172 /* List of hooks triggered on cgraph_node events. */
173 struct cgraph_node_hook_list {
174 cgraph_node_hook hook;
176 struct cgraph_node_hook_list *next;
179 /* List of hooks triggered on events involving two cgraph_edges. */
180 struct cgraph_2edge_hook_list {
181 cgraph_2edge_hook hook;
183 struct cgraph_2edge_hook_list *next;
186 /* List of hooks triggered on events involving two cgraph_nodes. */
187 struct cgraph_2node_hook_list {
188 cgraph_2node_hook hook;
190 struct cgraph_2node_hook_list *next;
193 /* List of hooks triggered when an edge is removed. */
194 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
195 /* List of hooks triggered when a node is removed. */
196 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
197 /* List of hooks triggered when an edge is duplicated. */
198 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
199 /* List of hooks triggered when a node is duplicated. */
200 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
201 /* List of hooks triggered when an function is inserted. */
202 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
204 /* Head of a linked list of unused (freed) call graph nodes.
205 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
206 static GTY(()) struct cgraph_node *free_nodes;
207 /* Head of a linked list of unused (freed) call graph edges.
208 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
209 static GTY(()) struct cgraph_edge *free_edges;
211 /* Macros to access the next item in the list of free cgraph nodes and
213 #define NEXT_FREE_NODE(NODE) (NODE)->next
214 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
216 /* Register HOOK to be called with DATA on each removed edge. */
217 struct cgraph_edge_hook_list *
218 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
220 struct cgraph_edge_hook_list *entry;
221 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
223 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
233 /* Remove ENTRY from the list of hooks called on removing edges. */
235 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
237 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
239 while (*ptr != entry)
245 /* Call all edge removal hooks. */
247 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
249 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
252 entry->hook (e, entry->data);
257 /* Register HOOK to be called with DATA on each removed node. */
258 struct cgraph_node_hook_list *
259 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
261 struct cgraph_node_hook_list *entry;
262 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
264 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
274 /* Remove ENTRY from the list of hooks called on removing nodes. */
276 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
278 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
280 while (*ptr != entry)
286 /* Call all node removal hooks. */
288 cgraph_call_node_removal_hooks (struct cgraph_node *node)
290 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
293 entry->hook (node, entry->data);
298 /* Register HOOK to be called with DATA on each inserted node. */
299 struct cgraph_node_hook_list *
300 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
302 struct cgraph_node_hook_list *entry;
303 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
305 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
315 /* Remove ENTRY from the list of hooks called on inserted nodes. */
317 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
319 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
321 while (*ptr != entry)
327 /* Call all node insertion hooks. */
329 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
331 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
334 entry->hook (node, entry->data);
339 /* Register HOOK to be called with DATA on each duplicated edge. */
340 struct cgraph_2edge_hook_list *
341 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
343 struct cgraph_2edge_hook_list *entry;
344 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
346 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
356 /* Remove ENTRY from the list of hooks called on duplicating edges. */
358 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
360 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
362 while (*ptr != entry)
368 /* Call all edge duplication hooks. */
370 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
371 struct cgraph_edge *cs2)
373 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
376 entry->hook (cs1, cs2, entry->data);
381 /* Register HOOK to be called with DATA on each duplicated node. */
382 struct cgraph_2node_hook_list *
383 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
385 struct cgraph_2node_hook_list *entry;
386 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
388 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
398 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
400 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
402 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
404 while (*ptr != entry)
410 /* Call all node duplication hooks. */
412 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
413 struct cgraph_node *node2)
415 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
418 entry->hook (node1, node2, entry->data);
423 /* Returns a hash code for P. */
426 hash_node (const void *p)
428 const struct cgraph_node *n = (const struct cgraph_node *) p;
429 return (hashval_t) DECL_UID (n->decl);
433 /* Returns nonzero if P1 and P2 are equal. */
436 eq_node (const void *p1, const void *p2)
438 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
439 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
440 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
443 /* Allocate new callgraph node. */
445 static inline struct cgraph_node *
446 cgraph_allocate_node (void)
448 struct cgraph_node *node;
453 free_nodes = NEXT_FREE_NODE (node);
457 node = ggc_alloc_cleared_cgraph_node ();
458 node->uid = cgraph_max_uid++;
464 /* Allocate new callgraph node and insert it into basic data structures. */
466 static struct cgraph_node *
467 cgraph_create_node_1 (void)
469 struct cgraph_node *node = cgraph_allocate_node ();
471 node->next = cgraph_nodes;
472 node->order = cgraph_order++;
474 cgraph_nodes->previous = node;
475 node->previous = NULL;
476 node->frequency = NODE_FREQUENCY_NORMAL;
477 node->count_materialization_scale = REG_BR_PROB_BASE;
478 ipa_empty_ref_list (&node->ref_list);
484 /* Return cgraph node assigned to DECL. Create new one when needed. */
487 cgraph_create_node (tree decl)
489 struct cgraph_node key, *node, **slot;
491 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
494 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
497 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
500 node = cgraph_create_node_1 ();
503 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
505 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
506 node->next_nested = node->origin->nested;
507 node->origin->nested = node;
509 if (assembler_name_hash)
512 tree name = DECL_ASSEMBLER_NAME (decl);
514 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
515 decl_assembler_name_hash (name),
517 /* We can have multiple declarations with same assembler name. For C++
518 it is __builtin_strlen and strlen, for instance. Do we need to
519 record them all? Original implementation marked just first one
520 so lets hope for the best. */
527 /* Try to find a call graph node for declaration DECL and if it does not exist,
531 cgraph_get_create_node (tree decl)
533 struct cgraph_node *node;
535 node = cgraph_get_node (decl);
539 return cgraph_create_node (decl);
542 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
543 the function body is associated with (not neccesarily cgraph_node (DECL). */
545 static struct cgraph_node *
546 cgraph_same_body_alias_1 (struct cgraph_node *decl_node, tree alias, tree decl)
548 struct cgraph_node key, *alias_node, **slot;
550 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
551 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
555 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
557 /* If the cgraph_node has been already created, fail. */
561 alias_node = cgraph_allocate_node ();
562 alias_node->decl = alias;
563 alias_node->same_body_alias = 1;
564 alias_node->same_body = decl_node;
565 alias_node->previous = NULL;
566 if (decl_node->same_body)
567 decl_node->same_body->previous = alias_node;
568 alias_node->next = decl_node->same_body;
569 alias_node->thunk.alias = decl;
570 decl_node->same_body = alias_node;
575 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
577 Same body aliases are output whenever the body of DECL is output,
578 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
581 cgraph_same_body_alias (struct cgraph_node *decl_node, tree alias, tree decl)
583 #ifndef ASM_OUTPUT_DEF
584 /* If aliases aren't supported by the assembler, fail. */
588 /*gcc_assert (!assembler_name_hash);*/
590 return cgraph_same_body_alias_1 (decl_node, alias, decl);
593 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
594 aliases DECL with an adjustments made into the first parameter.
595 See comments in thunk_adjust for detail on the parameters. */
598 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
599 tree alias, tree decl,
601 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
605 struct cgraph_node *node;
607 node = cgraph_get_node (alias);
610 gcc_assert (node->local.finalized);
611 gcc_assert (!node->alias);
612 gcc_assert (!node->thunk.thunk_p);
613 cgraph_remove_node (node);
616 node = cgraph_create_node (alias);
617 gcc_checking_assert (!virtual_offset
618 || double_int_equal_p
619 (tree_to_double_int (virtual_offset),
620 shwi_to_double_int (virtual_value)));
621 node->thunk.fixed_offset = fixed_offset;
622 node->thunk.this_adjusting = this_adjusting;
623 node->thunk.virtual_value = virtual_value;
624 node->thunk.virtual_offset_p = virtual_offset != NULL;
625 node->thunk.alias = real_alias;
626 node->thunk.thunk_p = true;
627 node->local.finalized = true;
629 if (cgraph_decide_is_function_needed (node, decl))
630 cgraph_mark_needed_node (node);
632 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
633 || (DECL_VIRTUAL_P (decl)
634 && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
635 cgraph_mark_reachable_node (node);
639 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
643 cgraph_get_node_or_alias (const_tree decl)
645 struct cgraph_node key, *node = NULL, **slot;
647 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
652 key.decl = CONST_CAST2 (tree, const_tree, decl);
654 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
662 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
666 cgraph_get_node (const_tree decl)
668 struct cgraph_node key, *node = NULL, **slot;
670 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
675 key.decl = CONST_CAST2 (tree, const_tree, decl);
677 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
683 if (node->same_body_alias)
684 node = node->same_body;
689 /* Insert already constructed node into hashtable. */
692 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
694 struct cgraph_node **slot;
696 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
702 /* Returns a hash code for P. */
705 hash_node_by_assembler_name (const void *p)
707 const struct cgraph_node *n = (const struct cgraph_node *) p;
708 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
711 /* Returns nonzero if P1 and P2 are equal. */
714 eq_assembler_name (const void *p1, const void *p2)
716 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
717 const_tree name = (const_tree)p2;
718 return (decl_assembler_name_equal (n1->decl, name));
721 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
722 Return NULL if there's no such node. */
725 cgraph_node_for_asm (tree asmname)
727 struct cgraph_node *node;
730 if (!assembler_name_hash)
732 assembler_name_hash =
733 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
735 for (node = cgraph_nodes; node; node = node->next)
736 if (!node->global.inlined_to)
738 tree name = DECL_ASSEMBLER_NAME (node->decl);
739 slot = htab_find_slot_with_hash (assembler_name_hash, name,
740 decl_assembler_name_hash (name),
742 /* We can have multiple declarations with same assembler name. For C++
743 it is __builtin_strlen and strlen, for instance. Do we need to
744 record them all? Original implementation marked just first one
745 so lets hope for the best. */
750 struct cgraph_node *alias;
752 for (alias = node->same_body; alias; alias = alias->next)
755 name = DECL_ASSEMBLER_NAME (alias->decl);
756 hash = decl_assembler_name_hash (name);
757 slot = htab_find_slot_with_hash (assembler_name_hash, name,
766 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
767 decl_assembler_name_hash (asmname),
772 node = (struct cgraph_node *) *slot;
773 if (node->same_body_alias)
774 node = node->same_body;
780 /* Returns a hash value for X (which really is a die_struct). */
783 edge_hash (const void *x)
785 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
788 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
791 edge_eq (const void *x, const void *y)
793 return ((const struct cgraph_edge *) x)->call_stmt == y;
796 /* Add call graph edge E to call site hash of its caller. */
799 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
802 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
804 htab_hash_pointer (e->call_stmt),
810 /* Return the callgraph edge representing the GIMPLE_CALL statement
814 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
816 struct cgraph_edge *e, *e2;
819 if (node->call_site_hash)
820 return (struct cgraph_edge *)
821 htab_find_with_hash (node->call_site_hash, call_stmt,
822 htab_hash_pointer (call_stmt));
824 /* This loop may turn out to be performance problem. In such case adding
825 hashtables into call nodes with very many edges is probably best
826 solution. It is not good idea to add pointer into CALL_EXPR itself
827 because we want to make possible having multiple cgraph nodes representing
828 different clones of the same body before the body is actually cloned. */
829 for (e = node->callees; e; e = e->next_callee)
831 if (e->call_stmt == call_stmt)
837 for (e = node->indirect_calls; e; e = e->next_callee)
839 if (e->call_stmt == call_stmt)
846 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
847 for (e2 = node->callees; e2; e2 = e2->next_callee)
848 cgraph_add_edge_to_call_site_hash (e2);
849 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
850 cgraph_add_edge_to_call_site_hash (e2);
857 /* Change field call_stmt of edge E to NEW_STMT. */
860 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
864 if (e->caller->call_site_hash)
866 htab_remove_elt_with_hash (e->caller->call_site_hash,
868 htab_hash_pointer (e->call_stmt));
871 e->call_stmt = new_stmt;
872 if (e->indirect_unknown_callee
873 && (decl = gimple_call_fndecl (new_stmt)))
875 /* Constant propagation (and possibly also inlining?) can turn an
876 indirect call into a direct one. */
877 struct cgraph_node *new_callee = cgraph_get_node (decl);
879 gcc_checking_assert (new_callee);
880 cgraph_make_edge_direct (e, new_callee, 0);
883 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
884 e->can_throw_external = stmt_can_throw_external (new_stmt);
886 if (e->caller->call_site_hash)
887 cgraph_add_edge_to_call_site_hash (e);
890 /* Like cgraph_set_call_stmt but walk the clone tree and update all
891 clones sharing the same function body. */
894 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
895 gimple old_stmt, gimple new_stmt)
897 struct cgraph_node *node;
898 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
901 cgraph_set_call_stmt (edge, new_stmt);
907 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
909 cgraph_set_call_stmt (edge, new_stmt);
912 else if (node->next_sibling_clone)
913 node = node->next_sibling_clone;
916 while (node != orig && !node->next_sibling_clone)
917 node = node->clone_of;
919 node = node->next_sibling_clone;
924 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
925 same function body. If clones already have edge for OLD_STMT; only
926 update the edge same way as cgraph_set_call_stmt_including_clones does.
928 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
929 frequencies of the clones. */
932 cgraph_create_edge_including_clones (struct cgraph_node *orig,
933 struct cgraph_node *callee,
935 gimple stmt, gcov_type count,
937 cgraph_inline_failed_t reason)
939 struct cgraph_node *node;
940 struct cgraph_edge *edge;
942 if (!cgraph_edge (orig, stmt))
944 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
945 edge->inline_failed = reason;
952 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
954 /* It is possible that clones already contain the edge while
955 master didn't. Either we promoted indirect call into direct
956 call in the clone or we are processing clones of unreachable
957 master where edges has been removed. */
959 cgraph_set_call_stmt (edge, stmt);
960 else if (!cgraph_edge (node, stmt))
962 edge = cgraph_create_edge (node, callee, stmt, count,
964 edge->inline_failed = reason;
969 else if (node->next_sibling_clone)
970 node = node->next_sibling_clone;
973 while (node != orig && !node->next_sibling_clone)
974 node = node->clone_of;
976 node = node->next_sibling_clone;
981 /* Allocate a cgraph_edge structure and fill it with data according to the
982 parameters of which only CALLEE can be NULL (when creating an indirect call
985 static struct cgraph_edge *
986 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
987 gimple call_stmt, gcov_type count, int freq)
989 struct cgraph_edge *edge;
991 /* LTO does not actually have access to the call_stmt since these
992 have not been loaded yet. */
995 /* This is a rather expensive check possibly triggering
996 construction of call stmt hashtable. */
997 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
999 gcc_assert (is_gimple_call (call_stmt));
1005 free_edges = NEXT_FREE_EDGE (edge);
1009 edge = ggc_alloc_cgraph_edge ();
1010 edge->uid = cgraph_edge_max_uid++;
1014 edge->caller = caller;
1015 edge->callee = callee;
1016 edge->prev_caller = NULL;
1017 edge->next_caller = NULL;
1018 edge->prev_callee = NULL;
1019 edge->next_callee = NULL;
1021 edge->count = count;
1022 gcc_assert (count >= 0);
1023 edge->frequency = freq;
1024 gcc_assert (freq >= 0);
1025 gcc_assert (freq <= CGRAPH_FREQ_MAX);
1027 edge->call_stmt = call_stmt;
1028 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
1029 edge->can_throw_external
1030 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
1032 edge->call_stmt_cannot_inline_p =
1033 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1034 if (call_stmt && caller->call_site_hash)
1035 cgraph_add_edge_to_call_site_hash (edge);
1037 edge->indirect_info = NULL;
1038 edge->indirect_inlining_edge = 0;
1043 /* Create edge from CALLER to CALLEE in the cgraph. */
1045 struct cgraph_edge *
1046 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1047 gimple call_stmt, gcov_type count, int freq)
1049 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1052 edge->indirect_unknown_callee = 0;
1053 initialize_inline_failed (edge);
1055 edge->next_caller = callee->callers;
1056 if (callee->callers)
1057 callee->callers->prev_caller = edge;
1058 edge->next_callee = caller->callees;
1059 if (caller->callees)
1060 caller->callees->prev_callee = edge;
1061 caller->callees = edge;
1062 callee->callers = edge;
1067 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1069 struct cgraph_indirect_call_info *
1070 cgraph_allocate_init_indirect_info (void)
1072 struct cgraph_indirect_call_info *ii;
1074 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1075 ii->param_index = -1;
1079 /* Create an indirect edge with a yet-undetermined callee where the call
1080 statement destination is a formal parameter of the caller with index
1083 struct cgraph_edge *
1084 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1086 gcov_type count, int freq)
1088 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1091 edge->indirect_unknown_callee = 1;
1092 initialize_inline_failed (edge);
1094 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1095 edge->indirect_info->ecf_flags = ecf_flags;
1097 edge->next_callee = caller->indirect_calls;
1098 if (caller->indirect_calls)
1099 caller->indirect_calls->prev_callee = edge;
1100 caller->indirect_calls = edge;
1105 /* Remove the edge E from the list of the callers of the callee. */
1108 cgraph_edge_remove_callee (struct cgraph_edge *e)
1110 gcc_assert (!e->indirect_unknown_callee);
1112 e->prev_caller->next_caller = e->next_caller;
1114 e->next_caller->prev_caller = e->prev_caller;
1115 if (!e->prev_caller)
1116 e->callee->callers = e->next_caller;
1119 /* Remove the edge E from the list of the callees of the caller. */
1122 cgraph_edge_remove_caller (struct cgraph_edge *e)
1125 e->prev_callee->next_callee = e->next_callee;
1127 e->next_callee->prev_callee = e->prev_callee;
1128 if (!e->prev_callee)
1130 if (e->indirect_unknown_callee)
1131 e->caller->indirect_calls = e->next_callee;
1133 e->caller->callees = e->next_callee;
1135 if (e->caller->call_site_hash)
1136 htab_remove_elt_with_hash (e->caller->call_site_hash,
1138 htab_hash_pointer (e->call_stmt));
1141 /* Put the edge onto the free list. */
1144 cgraph_free_edge (struct cgraph_edge *e)
1148 /* Clear out the edge so we do not dangle pointers. */
1149 memset (e, 0, sizeof (*e));
1151 NEXT_FREE_EDGE (e) = free_edges;
1155 /* Remove the edge E in the cgraph. */
1158 cgraph_remove_edge (struct cgraph_edge *e)
1160 /* Call all edge removal hooks. */
1161 cgraph_call_edge_removal_hooks (e);
1163 if (!e->indirect_unknown_callee)
1164 /* Remove from callers list of the callee. */
1165 cgraph_edge_remove_callee (e);
1167 /* Remove from callees list of the callers. */
1168 cgraph_edge_remove_caller (e);
1170 /* Put the edge onto the free list. */
1171 cgraph_free_edge (e);
1174 /* Set callee of call graph edge E and add it to the corresponding set of
1178 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1180 e->prev_caller = NULL;
1182 n->callers->prev_caller = e;
1183 e->next_caller = n->callers;
1188 /* Redirect callee of E to N. The function does not update underlying
1192 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1194 /* Remove from callers list of the current callee. */
1195 cgraph_edge_remove_callee (e);
1197 /* Insert to callers list of the new callee. */
1198 cgraph_set_edge_callee (e, n);
1201 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1202 CALLEE. DELTA is an integer constant that is to be added to the this
1203 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1206 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
1207 HOST_WIDE_INT delta)
1209 edge->indirect_unknown_callee = 0;
1210 edge->indirect_info->thunk_delta = delta;
1212 /* Get the edge out of the indirect edge list. */
1213 if (edge->prev_callee)
1214 edge->prev_callee->next_callee = edge->next_callee;
1215 if (edge->next_callee)
1216 edge->next_callee->prev_callee = edge->prev_callee;
1217 if (!edge->prev_callee)
1218 edge->caller->indirect_calls = edge->next_callee;
1220 /* Put it into the normal callee list */
1221 edge->prev_callee = NULL;
1222 edge->next_callee = edge->caller->callees;
1223 if (edge->caller->callees)
1224 edge->caller->callees->prev_callee = edge;
1225 edge->caller->callees = edge;
1227 /* Insert to callers list of the new callee. */
1228 cgraph_set_edge_callee (edge, callee);
1230 /* We need to re-determine the inlining status of the edge. */
1231 initialize_inline_failed (edge);
1235 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1236 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1237 of OLD_STMT if it was previously call statement.
1238 If NEW_STMT is NULL, the call has been dropped without any
1242 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1243 gimple old_stmt, tree old_call,
1246 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1247 ? gimple_call_fndecl (new_stmt) : 0;
1249 /* We are seeing indirect calls, then there is nothing to update. */
1250 if (!new_call && !old_call)
1252 /* See if we turned indirect call into direct call or folded call to one builtin
1253 into different builtin. */
1254 if (old_call != new_call)
1256 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1257 struct cgraph_edge *ne = NULL;
1263 /* See if the edge is already there and has the correct callee. It
1264 might be so because of indirect inlining has already updated
1265 it. We also might've cloned and redirected the edge. */
1266 if (new_call && e->callee)
1268 struct cgraph_node *callee = e->callee;
1271 if (callee->decl == new_call
1272 || callee->former_clone_of == new_call)
1274 callee = callee->clone_of;
1278 /* Otherwise remove edge and create new one; we can't simply redirect
1279 since function has changed, so inline plan and other information
1280 attached to edge is invalid. */
1282 frequency = e->frequency;
1283 cgraph_remove_edge (e);
1287 /* We are seeing new direct call; compute profile info based on BB. */
1288 basic_block bb = gimple_bb (new_stmt);
1290 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1296 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1297 new_stmt, count, frequency);
1298 gcc_assert (ne->inline_failed);
1301 /* We only updated the call stmt; update pointer in cgraph edge.. */
1302 else if (old_stmt != new_stmt)
1303 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1306 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1307 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1308 of OLD_STMT before it was updated (updating can happen inplace). */
1311 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1313 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1314 struct cgraph_node *node;
1316 gcc_checking_assert (orig);
1317 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1319 for (node = orig->clones; node != orig;)
1321 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1323 node = node->clones;
1324 else if (node->next_sibling_clone)
1325 node = node->next_sibling_clone;
1328 while (node != orig && !node->next_sibling_clone)
1329 node = node->clone_of;
1331 node = node->next_sibling_clone;
1337 /* Remove all callees from the node. */
1340 cgraph_node_remove_callees (struct cgraph_node *node)
1342 struct cgraph_edge *e, *f;
1344 /* It is sufficient to remove the edges from the lists of callers of
1345 the callees. The callee list of the node can be zapped with one
1347 for (e = node->callees; e; e = f)
1350 cgraph_call_edge_removal_hooks (e);
1351 if (!e->indirect_unknown_callee)
1352 cgraph_edge_remove_callee (e);
1353 cgraph_free_edge (e);
1355 for (e = node->indirect_calls; e; e = f)
1358 cgraph_call_edge_removal_hooks (e);
1359 if (!e->indirect_unknown_callee)
1360 cgraph_edge_remove_callee (e);
1361 cgraph_free_edge (e);
1363 node->indirect_calls = NULL;
1364 node->callees = NULL;
1365 if (node->call_site_hash)
1367 htab_delete (node->call_site_hash);
1368 node->call_site_hash = NULL;
1372 /* Remove all callers from the node. */
1375 cgraph_node_remove_callers (struct cgraph_node *node)
1377 struct cgraph_edge *e, *f;
1379 /* It is sufficient to remove the edges from the lists of callees of
1380 the callers. The caller list of the node can be zapped with one
1382 for (e = node->callers; e; e = f)
1385 cgraph_call_edge_removal_hooks (e);
1386 cgraph_edge_remove_caller (e);
1387 cgraph_free_edge (e);
1389 node->callers = NULL;
1392 /* Release memory used to represent body of function NODE. */
1395 cgraph_release_function_body (struct cgraph_node *node)
1397 if (DECL_STRUCT_FUNCTION (node->decl))
1399 tree old_decl = current_function_decl;
1400 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1401 if (cfun->gimple_df)
1403 current_function_decl = node->decl;
1405 delete_tree_cfg_annotations ();
1407 current_function_decl = old_decl;
1411 gcc_assert (dom_computed[0] == DOM_NONE);
1412 gcc_assert (dom_computed[1] == DOM_NONE);
1415 if (cfun->value_histograms)
1417 gcc_assert (!current_loops);
1419 gimple_set_body (node->decl, NULL);
1420 VEC_free (ipa_opt_pass, heap,
1421 node->ipa_transforms_to_apply);
1422 /* Struct function hangs a lot of data that would leak if we didn't
1423 removed all pointers to it. */
1424 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1425 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1427 DECL_SAVED_TREE (node->decl) = NULL;
1428 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1429 of its associated function function declaration because it's
1430 needed to emit debug info later. */
1431 if (!node->abstract_and_needed)
1432 DECL_INITIAL (node->decl) = error_mark_node;
1435 /* Remove same body alias node. */
1438 cgraph_remove_same_body_alias (struct cgraph_node *node)
1441 int uid = node->uid;
1443 gcc_assert (node->same_body_alias);
1445 node->previous->next = node->next;
1447 node->same_body->same_body = node->next;
1449 node->next->previous = node->previous;
1451 node->previous = NULL;
1452 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1454 htab_clear_slot (cgraph_hash, slot);
1455 if (assembler_name_hash)
1457 tree name = DECL_ASSEMBLER_NAME (node->decl);
1458 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1459 decl_assembler_name_hash (name),
1461 if (slot && *slot == node)
1462 htab_clear_slot (assembler_name_hash, slot);
1465 /* Clear out the node to NULL all pointers and add the node to the free
1467 memset (node, 0, sizeof(*node));
1469 NEXT_FREE_NODE (node) = free_nodes;
1473 /* Remove the node from cgraph. */
1476 cgraph_remove_node (struct cgraph_node *node)
1479 bool kill_body = false;
1480 struct cgraph_node *n;
1481 int uid = node->uid;
1483 cgraph_call_node_removal_hooks (node);
1484 cgraph_node_remove_callers (node);
1485 cgraph_node_remove_callees (node);
1486 ipa_remove_all_references (&node->ref_list);
1487 ipa_remove_all_refering (&node->ref_list);
1488 VEC_free (ipa_opt_pass, heap,
1489 node->ipa_transforms_to_apply);
1491 /* Incremental inlining access removed nodes stored in the postorder list.
1493 node->needed = node->reachable = false;
1494 for (n = node->nested; n; n = n->next_nested)
1496 node->nested = NULL;
1499 struct cgraph_node **node2 = &node->origin->nested;
1501 while (*node2 != node)
1502 node2 = &(*node2)->next_nested;
1503 *node2 = node->next_nested;
1506 node->previous->next = node->next;
1508 cgraph_nodes = node->next;
1510 node->next->previous = node->previous;
1512 node->previous = NULL;
1513 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1516 struct cgraph_node *next_inline_clone;
1518 for (next_inline_clone = node->clones;
1519 next_inline_clone && next_inline_clone->decl != node->decl;
1520 next_inline_clone = next_inline_clone->next_sibling_clone)
1523 /* If there is inline clone of the node being removed, we need
1524 to put it into the position of removed node and reorganize all
1525 other clones to be based on it. */
1526 if (next_inline_clone)
1528 struct cgraph_node *n;
1529 struct cgraph_node *new_clones;
1531 *slot = next_inline_clone;
1533 /* Unlink inline clone from the list of clones of removed node. */
1534 if (next_inline_clone->next_sibling_clone)
1535 next_inline_clone->next_sibling_clone->prev_sibling_clone
1536 = next_inline_clone->prev_sibling_clone;
1537 if (next_inline_clone->prev_sibling_clone)
1539 gcc_assert (node->clones != next_inline_clone);
1540 next_inline_clone->prev_sibling_clone->next_sibling_clone
1541 = next_inline_clone->next_sibling_clone;
1545 gcc_assert (node->clones == next_inline_clone);
1546 node->clones = next_inline_clone->next_sibling_clone;
1549 new_clones = node->clones;
1550 node->clones = NULL;
1552 /* Copy clone info. */
1553 next_inline_clone->clone = node->clone;
1555 /* Now place it into clone tree at same level at NODE. */
1556 next_inline_clone->clone_of = node->clone_of;
1557 next_inline_clone->prev_sibling_clone = NULL;
1558 next_inline_clone->next_sibling_clone = NULL;
1561 if (node->clone_of->clones)
1562 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1563 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1564 node->clone_of->clones = next_inline_clone;
1567 /* Merge the clone list. */
1570 if (!next_inline_clone->clones)
1571 next_inline_clone->clones = new_clones;
1574 n = next_inline_clone->clones;
1575 while (n->next_sibling_clone)
1576 n = n->next_sibling_clone;
1577 n->next_sibling_clone = new_clones;
1578 new_clones->prev_sibling_clone = n;
1582 /* Update clone_of pointers. */
1586 n->clone_of = next_inline_clone;
1587 n = n->next_sibling_clone;
1592 htab_clear_slot (cgraph_hash, slot);
1597 if (node->prev_sibling_clone)
1598 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1599 else if (node->clone_of)
1600 node->clone_of->clones = node->next_sibling_clone;
1601 if (node->next_sibling_clone)
1602 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1605 struct cgraph_node *n, *next;
1609 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1610 n->clone_of = node->clone_of;
1611 n->clone_of = node->clone_of;
1612 n->next_sibling_clone = node->clone_of->clones;
1613 if (node->clone_of->clones)
1614 node->clone_of->clones->prev_sibling_clone = n;
1615 node->clone_of->clones = node->clones;
1619 /* We are removing node with clones. this makes clones inconsistent,
1620 but assume they will be removed subsequently and just keep clone
1621 tree intact. This can happen in unreachable function removal since
1622 we remove unreachable functions in random order, not by bottom-up
1623 walk of clone trees. */
1624 for (n = node->clones; n; n = next)
1626 next = n->next_sibling_clone;
1627 n->next_sibling_clone = NULL;
1628 n->prev_sibling_clone = NULL;
1634 while (node->same_body)
1635 cgraph_remove_same_body_alias (node->same_body);
1637 if (node->same_comdat_group)
1639 struct cgraph_node *prev;
1640 for (prev = node->same_comdat_group;
1641 prev->same_comdat_group != node;
1642 prev = prev->same_comdat_group)
1644 if (node->same_comdat_group == prev)
1645 prev->same_comdat_group = NULL;
1647 prev->same_comdat_group = node->same_comdat_group;
1648 node->same_comdat_group = NULL;
1651 /* While all the clones are removed after being proceeded, the function
1652 itself is kept in the cgraph even after it is compiled. Check whether
1653 we are done with this body and reclaim it proactively if this is the case.
1655 if (!kill_body && *slot)
1657 struct cgraph_node *n = (struct cgraph_node *) *slot;
1658 if (!n->clones && !n->clone_of && !n->global.inlined_to
1659 && (cgraph_global_info_ready
1660 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1661 || n->in_other_partition)))
1664 if (assembler_name_hash)
1666 tree name = DECL_ASSEMBLER_NAME (node->decl);
1667 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1668 decl_assembler_name_hash (name),
1670 /* Inline clones are not hashed. */
1671 if (slot && *slot == node)
1672 htab_clear_slot (assembler_name_hash, slot);
1676 cgraph_release_function_body (node);
1678 if (node->call_site_hash)
1680 htab_delete (node->call_site_hash);
1681 node->call_site_hash = NULL;
1685 /* Clear out the node to NULL all pointers and add the node to the free
1687 memset (node, 0, sizeof(*node));
1689 NEXT_FREE_NODE (node) = free_nodes;
1693 /* Remove the node from cgraph. */
1696 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1698 struct cgraph_edge *e, *next;
1699 for (e = node->callees; e; e = next)
1701 next = e->next_callee;
1702 if (!e->inline_failed)
1703 cgraph_remove_node_and_inline_clones (e->callee);
1705 cgraph_remove_node (node);
1708 /* Notify finalize_compilation_unit that given node is reachable. */
1711 cgraph_mark_reachable_node (struct cgraph_node *node)
1713 if (!node->reachable && node->local.finalized)
1715 if (cgraph_global_info_ready)
1717 /* Verify that function does not appear to be needed out of blue
1718 during the optimization process. This can happen for extern
1719 inlines when bodies was removed after inlining. */
1720 gcc_assert ((node->analyzed || node->in_other_partition
1721 || DECL_EXTERNAL (node->decl)));
1724 notice_global_symbol (node->decl);
1725 node->reachable = 1;
1727 node->next_needed = cgraph_nodes_queue;
1728 cgraph_nodes_queue = node;
1732 /* Likewise indicate that a node is needed, i.e. reachable via some
1736 cgraph_mark_needed_node (struct cgraph_node *node)
1739 gcc_assert (!node->global.inlined_to);
1740 cgraph_mark_reachable_node (node);
1743 /* Likewise indicate that a node is having address taken. */
1746 cgraph_mark_address_taken_node (struct cgraph_node *node)
1748 gcc_assert (!node->global.inlined_to);
1749 cgraph_mark_reachable_node (node);
1750 node->address_taken = 1;
1753 /* Return local info for the compiled function. */
1755 struct cgraph_local_info *
1756 cgraph_local_info (tree decl)
1758 struct cgraph_node *node;
1760 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1761 node = cgraph_get_node (decl);
1764 return &node->local;
1767 /* Return local info for the compiled function. */
1769 struct cgraph_global_info *
1770 cgraph_global_info (tree decl)
1772 struct cgraph_node *node;
1774 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1775 node = cgraph_get_node (decl);
1778 return &node->global;
1781 /* Return local info for the compiled function. */
1783 struct cgraph_rtl_info *
1784 cgraph_rtl_info (tree decl)
1786 struct cgraph_node *node;
1788 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1789 node = cgraph_get_node (decl);
1791 || (decl != current_function_decl
1792 && !TREE_ASM_WRITTEN (node->decl)))
1797 /* Return a string describing the failure REASON. */
1800 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1803 #define DEFCIFCODE(code, string) string,
1805 static const char *cif_string_table[CIF_N_REASONS] = {
1806 #include "cif-code.def"
1809 /* Signedness of an enum type is implementation defined, so cast it
1810 to unsigned before testing. */
1811 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1812 return cif_string_table[reason];
1815 /* Return name of the node used in debug output. */
1817 cgraph_node_name (struct cgraph_node *node)
1819 return lang_hooks.decl_printable_name (node->decl, 2);
1822 /* Names used to print out the availability enum. */
1823 const char * const cgraph_availability_names[] =
1824 {"unset", "not_available", "overwritable", "available", "local"};
1827 /* Dump call graph node NODE to file F. */
1830 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1832 struct cgraph_edge *edge;
1833 int indirect_calls_count = 0;
1835 fprintf (f, "%s/%i", cgraph_node_name (node), node->uid);
1836 dump_addr (f, " @", (void *)node);
1837 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1838 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1839 if (node->global.inlined_to)
1840 fprintf (f, " (inline copy in %s/%i)",
1841 cgraph_node_name (node->global.inlined_to),
1842 node->global.inlined_to->uid);
1843 if (node->same_comdat_group)
1844 fprintf (f, " (same comdat group as %s/%i)",
1845 cgraph_node_name (node->same_comdat_group),
1846 node->same_comdat_group->uid);
1848 fprintf (f, " (clone of %s/%i)",
1849 cgraph_node_name (node->clone_of),
1850 node->clone_of->uid);
1851 if (cgraph_function_flags_ready)
1852 fprintf (f, " availability:%s",
1853 cgraph_availability_names [cgraph_function_body_availability (node)]);
1855 fprintf (f, " analyzed");
1856 if (node->in_other_partition)
1857 fprintf (f, " in_other_partition");
1859 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1860 (HOST_WIDEST_INT)node->count);
1862 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1864 fprintf (f, " needed");
1865 if (node->address_taken)
1866 fprintf (f, " address_taken");
1867 else if (node->reachable)
1868 fprintf (f, " reachable");
1869 else if (node->reachable_from_other_partition)
1870 fprintf (f, " reachable_from_other_partition");
1871 if (gimple_has_body_p (node->decl))
1872 fprintf (f, " body");
1874 fprintf (f, " process");
1875 if (node->local.local)
1876 fprintf (f, " local");
1877 if (node->local.externally_visible)
1878 fprintf (f, " externally_visible");
1879 if (node->resolution != LDPR_UNKNOWN)
1881 ld_plugin_symbol_resolution_names[(int)node->resolution]);
1882 if (node->local.finalized)
1883 fprintf (f, " finalized");
1884 if (node->local.redefined_extern_inline)
1885 fprintf (f, " redefined_extern_inline");
1886 if (TREE_ASM_WRITTEN (node->decl))
1887 fprintf (f, " asm_written");
1888 if (node->only_called_at_startup)
1889 fprintf (f, " only_called_at_startup");
1890 if (node->only_called_at_exit)
1891 fprintf (f, " only_called_at_exit");
1895 if (node->thunk.thunk_p)
1897 fprintf (f, " thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1898 "virtual offset %i)\n",
1899 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1900 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1901 (int)node->thunk.fixed_offset,
1902 (int)node->thunk.virtual_value,
1903 (int)node->thunk.virtual_offset_p);
1906 fprintf (f, " called by: ");
1908 for (edge = node->callers; edge; edge = edge->next_caller)
1910 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1913 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1914 (HOST_WIDEST_INT)edge->count);
1915 if (edge->frequency)
1916 fprintf (f, "(%.2f per call) ",
1917 edge->frequency / (double)CGRAPH_FREQ_BASE);
1918 if (!edge->inline_failed)
1919 fprintf(f, "(inlined) ");
1920 if (edge->indirect_inlining_edge)
1921 fprintf(f, "(indirect_inlining) ");
1922 if (edge->can_throw_external)
1923 fprintf(f, "(can throw external) ");
1926 fprintf (f, "\n calls: ");
1927 for (edge = node->callees; edge; edge = edge->next_callee)
1929 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1931 if (!edge->inline_failed)
1932 fprintf(f, "(inlined) ");
1933 if (edge->indirect_inlining_edge)
1934 fprintf(f, "(indirect_inlining) ");
1936 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1937 (HOST_WIDEST_INT)edge->count);
1938 if (edge->frequency)
1939 fprintf (f, "(%.2f per call) ",
1940 edge->frequency / (double)CGRAPH_FREQ_BASE);
1941 if (edge->can_throw_external)
1942 fprintf(f, "(can throw external) ");
1945 fprintf (f, " References: ");
1946 ipa_dump_references (f, &node->ref_list);
1947 fprintf (f, " Refering this function: ");
1948 ipa_dump_refering (f, &node->ref_list);
1950 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1951 indirect_calls_count++;
1952 if (indirect_calls_count)
1953 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1954 indirect_calls_count);
1956 if (node->same_body)
1958 struct cgraph_node *n;
1959 fprintf (f, " aliases:");
1960 for (n = node->same_body; n; n = n->next)
1962 fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1963 if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1964 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1971 /* Dump call graph node NODE to stderr. */
1974 debug_cgraph_node (struct cgraph_node *node)
1976 dump_cgraph_node (stderr, node);
1980 /* Dump the callgraph to file F. */
1983 dump_cgraph (FILE *f)
1985 struct cgraph_node *node;
1987 fprintf (f, "callgraph:\n\n");
1988 for (node = cgraph_nodes; node; node = node->next)
1989 dump_cgraph_node (f, node);
1993 /* Dump the call graph to stderr. */
1998 dump_cgraph (stderr);
2002 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
2005 change_decl_assembler_name (tree decl, tree name)
2007 struct cgraph_node *node;
2009 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
2010 SET_DECL_ASSEMBLER_NAME (decl, name);
2013 if (name == DECL_ASSEMBLER_NAME (decl))
2016 if (assembler_name_hash
2017 && TREE_CODE (decl) == FUNCTION_DECL
2018 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2020 tree old_name = DECL_ASSEMBLER_NAME (decl);
2021 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2022 decl_assembler_name_hash (old_name),
2024 /* Inline clones are not hashed. */
2025 if (slot && *slot == node)
2026 htab_clear_slot (assembler_name_hash, slot);
2028 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2029 && DECL_RTL_SET_P (decl))
2030 warning (0, "%D renamed after being referenced in assembly", decl);
2032 SET_DECL_ASSEMBLER_NAME (decl, name);
2034 if (assembler_name_hash
2035 && TREE_CODE (decl) == FUNCTION_DECL
2036 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2038 slot = htab_find_slot_with_hash (assembler_name_hash, name,
2039 decl_assembler_name_hash (name),
2041 gcc_assert (!*slot);
2046 /* Add a top-level asm statement to the list. */
2048 struct cgraph_asm_node *
2049 cgraph_add_asm_node (tree asm_str)
2051 struct cgraph_asm_node *node;
2053 node = ggc_alloc_cleared_cgraph_asm_node ();
2054 node->asm_str = asm_str;
2055 node->order = cgraph_order++;
2057 if (cgraph_asm_nodes == NULL)
2058 cgraph_asm_nodes = node;
2060 cgraph_asm_last_node->next = node;
2061 cgraph_asm_last_node = node;
2065 /* Return true when the DECL can possibly be inlined. */
2067 cgraph_function_possibly_inlined_p (tree decl)
2069 if (!cgraph_global_info_ready)
2070 return !DECL_UNINLINABLE (decl);
2071 return DECL_POSSIBLY_INLINED (decl);
2074 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2075 struct cgraph_edge *
2076 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2077 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2078 int freq_scale, bool update_original)
2080 struct cgraph_edge *new_edge;
2081 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2084 /* We do not want to ignore loop nest after frequency drops to 0. */
2087 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2088 if (freq > CGRAPH_FREQ_MAX)
2089 freq = CGRAPH_FREQ_MAX;
2091 if (e->indirect_unknown_callee)
2095 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2097 struct cgraph_node *callee = cgraph_get_node (decl);
2098 gcc_checking_assert (callee);
2099 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
2103 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2104 e->indirect_info->ecf_flags,
2106 *new_edge->indirect_info = *e->indirect_info;
2111 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
2112 if (e->indirect_info)
2114 new_edge->indirect_info
2115 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2116 *new_edge->indirect_info = *e->indirect_info;
2120 new_edge->inline_failed = e->inline_failed;
2121 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2122 new_edge->lto_stmt_uid = stmt_uid;
2123 /* Clone flags that depend on call_stmt availability manually. */
2124 new_edge->can_throw_external = e->can_throw_external;
2125 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2126 if (update_original)
2128 e->count -= new_edge->count;
2132 cgraph_call_edge_duplication_hooks (e, new_edge);
2137 /* Create node representing clone of N executed COUNT times. Decrease
2138 the execution counts from original node too.
2139 The new clone will have decl set to DECL that may or may not be the same
2142 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2143 function's profile to reflect the fact that part of execution is handled
2145 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
2146 the new clone. Otherwise the caller is responsible for doing so later. */
2148 struct cgraph_node *
2149 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2150 bool update_original,
2151 VEC(cgraph_edge_p,heap) *redirect_callers,
2152 bool call_duplication_hook)
2154 struct cgraph_node *new_node = cgraph_create_node_1 ();
2155 struct cgraph_edge *e;
2156 gcov_type count_scale;
2159 new_node->decl = decl;
2160 new_node->origin = n->origin;
2161 if (new_node->origin)
2163 new_node->next_nested = new_node->origin->nested;
2164 new_node->origin->nested = new_node;
2166 new_node->analyzed = n->analyzed;
2167 new_node->local = n->local;
2168 new_node->local.externally_visible = false;
2169 new_node->local.local = true;
2170 new_node->global = n->global;
2171 new_node->rtl = n->rtl;
2172 new_node->count = count;
2173 new_node->frequency = n->frequency;
2174 new_node->clone = n->clone;
2175 new_node->clone.tree_map = 0;
2178 if (new_node->count > n->count)
2179 count_scale = REG_BR_PROB_BASE;
2181 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2185 if (update_original)
2192 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2194 /* Redirect calls to the old version node to point to its new
2196 cgraph_redirect_edge_callee (e, new_node);
2200 for (e = n->callees;e; e=e->next_callee)
2201 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2202 count_scale, freq, update_original);
2204 for (e = n->indirect_calls; e; e = e->next_callee)
2205 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2206 count_scale, freq, update_original);
2207 ipa_clone_references (new_node, NULL, &n->ref_list);
2209 new_node->next_sibling_clone = n->clones;
2211 n->clones->prev_sibling_clone = new_node;
2212 n->clones = new_node;
2213 new_node->clone_of = n;
2215 if (n->decl != decl)
2217 struct cgraph_node **slot;
2218 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2219 gcc_assert (!*slot);
2221 if (assembler_name_hash)
2224 tree name = DECL_ASSEMBLER_NAME (decl);
2226 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2227 decl_assembler_name_hash (name),
2229 gcc_assert (!*aslot);
2234 if (call_duplication_hook)
2235 cgraph_call_node_duplication_hooks (n, new_node);
2239 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2241 static GTY(()) unsigned int clone_fn_id_num;
2244 clone_function_name (tree decl, const char *suffix)
2246 tree name = DECL_ASSEMBLER_NAME (decl);
2247 size_t len = IDENTIFIER_LENGTH (name);
2248 char *tmp_name, *prefix;
2250 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2251 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2252 strcpy (prefix + len + 1, suffix);
2253 #ifndef NO_DOT_IN_LABEL
2255 #elif !defined NO_DOLLAR_IN_LABEL
2260 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2261 return get_identifier (tmp_name);
2264 /* Create callgraph node clone with new declaration. The actual body will
2265 be copied later at compilation stage.
2267 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2270 struct cgraph_node *
2271 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2272 VEC(cgraph_edge_p,heap) *redirect_callers,
2273 VEC(ipa_replace_map_p,gc) *tree_map,
2274 bitmap args_to_skip,
2275 const char * suffix)
2277 tree old_decl = old_node->decl;
2278 struct cgraph_node *new_node = NULL;
2281 struct ipa_replace_map *map;
2284 gcc_checking_assert (tree_versionable_function_p (old_decl));
2286 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2288 /* Make a new FUNCTION_DECL tree node */
2290 new_decl = copy_node (old_decl);
2292 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2293 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2295 /* Generate a new name for the new version. */
2296 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2297 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2298 SET_DECL_RTL (new_decl, NULL);
2300 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2301 CGRAPH_FREQ_BASE, false,
2302 redirect_callers, false);
2303 /* Update the properties.
2304 Make clone visible only within this translation unit. Make sure
2305 that is not weak also.
2306 ??? We cannot use COMDAT linkage because there is no
2307 ABI support for this. */
2308 DECL_EXTERNAL (new_node->decl) = 0;
2309 if (DECL_ONE_ONLY (old_decl))
2310 DECL_SECTION_NAME (new_node->decl) = NULL;
2311 DECL_COMDAT_GROUP (new_node->decl) = 0;
2312 TREE_PUBLIC (new_node->decl) = 0;
2313 DECL_COMDAT (new_node->decl) = 0;
2314 DECL_WEAK (new_node->decl) = 0;
2315 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
2316 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
2317 new_node->clone.tree_map = tree_map;
2318 new_node->clone.args_to_skip = args_to_skip;
2319 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2321 tree var = map->new_tree;
2324 if (TREE_CODE (var) != ADDR_EXPR)
2326 var = get_base_var (var);
2330 /* Record references of the future statement initializing the constant
2332 if (TREE_CODE (var) == FUNCTION_DECL)
2334 struct cgraph_node *ref_node = cgraph_get_node (var);
2335 gcc_checking_assert (ref_node);
2336 ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2339 else if (TREE_CODE (var) == VAR_DECL)
2340 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2341 IPA_REF_ADDR, NULL);
2344 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2345 else if (old_node->clone.combined_args_to_skip)
2347 int newi = 0, oldi = 0;
2349 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2350 struct cgraph_node *orig_node;
2351 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2353 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2355 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2357 bitmap_set_bit (new_args_to_skip, oldi);
2360 if (bitmap_bit_p (args_to_skip, newi))
2361 bitmap_set_bit (new_args_to_skip, oldi);
2364 new_node->clone.combined_args_to_skip = new_args_to_skip;
2367 new_node->clone.combined_args_to_skip = args_to_skip;
2368 new_node->local.externally_visible = 0;
2369 new_node->local.local = 1;
2370 new_node->lowered = true;
2371 new_node->reachable = true;
2373 cgraph_call_node_duplication_hooks (old_node, new_node);
2379 /* NODE is no longer nested function; update cgraph accordingly. */
2381 cgraph_unnest_node (struct cgraph_node *node)
2383 struct cgraph_node **node2 = &node->origin->nested;
2384 gcc_assert (node->origin);
2386 while (*node2 != node)
2387 node2 = &(*node2)->next_nested;
2388 *node2 = node->next_nested;
2389 node->origin = NULL;
2392 /* Return function availability. See cgraph.h for description of individual
2395 cgraph_function_body_availability (struct cgraph_node *node)
2397 enum availability avail;
2398 gcc_assert (cgraph_function_flags_ready);
2399 if (!node->analyzed)
2400 avail = AVAIL_NOT_AVAILABLE;
2401 else if (node->local.local)
2402 avail = AVAIL_LOCAL;
2403 else if (!node->local.externally_visible)
2404 avail = AVAIL_AVAILABLE;
2405 /* Inline functions are safe to be analyzed even if their symbol can
2406 be overwritten at runtime. It is not meaningful to enforce any sane
2407 behaviour on replacing inline function by different body. */
2408 else if (DECL_DECLARED_INLINE_P (node->decl))
2409 avail = AVAIL_AVAILABLE;
2411 /* If the function can be overwritten, return OVERWRITABLE. Take
2412 care at least of two notable extensions - the COMDAT functions
2413 used to share template instantiations in C++ (this is symmetric
2414 to code cp_cannot_inline_tree_fn and probably shall be shared and
2415 the inlinability hooks completely eliminated).
2417 ??? Does the C++ one definition rule allow us to always return
2418 AVAIL_AVAILABLE here? That would be good reason to preserve this
2421 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2422 avail = AVAIL_OVERWRITABLE;
2423 else avail = AVAIL_AVAILABLE;
2428 /* Add the function FNDECL to the call graph.
2429 Unlike cgraph_finalize_function, this function is intended to be used
2430 by middle end and allows insertion of new function at arbitrary point
2431 of compilation. The function can be either in high, low or SSA form
2434 The function is assumed to be reachable and have address taken (so no
2435 API breaking optimizations are performed on it).
2437 Main work done by this function is to enqueue the function for later
2438 processing to avoid need the passes to be re-entrant. */
2441 cgraph_add_new_function (tree fndecl, bool lowered)
2443 struct cgraph_node *node;
2444 switch (cgraph_state)
2446 case CGRAPH_STATE_CONSTRUCTION:
2447 /* Just enqueue function to be processed at nearest occurrence. */
2448 node = cgraph_create_node (fndecl);
2449 node->next_needed = cgraph_new_nodes;
2451 node->lowered = true;
2452 cgraph_new_nodes = node;
2455 case CGRAPH_STATE_IPA:
2456 case CGRAPH_STATE_IPA_SSA:
2457 case CGRAPH_STATE_EXPANSION:
2458 /* Bring the function into finalized state and enqueue for later
2459 analyzing and compilation. */
2460 node = cgraph_get_create_node (fndecl);
2461 node->local.local = false;
2462 node->local.finalized = true;
2463 node->reachable = node->needed = true;
2464 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2466 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2467 current_function_decl = fndecl;
2468 gimple_register_cfg_hooks ();
2469 tree_lowering_passes (fndecl);
2470 bitmap_obstack_initialize (NULL);
2471 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2472 execute_pass_list (pass_early_local_passes.pass.sub);
2473 bitmap_obstack_release (NULL);
2475 current_function_decl = NULL;
2480 node->lowered = true;
2481 node->next_needed = cgraph_new_nodes;
2482 cgraph_new_nodes = node;
2485 case CGRAPH_STATE_FINISHED:
2486 /* At the very end of compilation we have to do all the work up
2488 node = cgraph_create_node (fndecl);
2490 node->lowered = true;
2491 cgraph_analyze_function (node);
2492 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2493 current_function_decl = fndecl;
2494 gimple_register_cfg_hooks ();
2495 bitmap_obstack_initialize (NULL);
2496 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2497 execute_pass_list (pass_early_local_passes.pass.sub);
2498 bitmap_obstack_release (NULL);
2499 tree_rest_of_compilation (fndecl);
2501 current_function_decl = NULL;
2505 /* Set a personality if required and we already passed EH lowering. */
2507 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2508 == eh_personality_lang))
2509 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2512 /* Worker for cgraph_node_can_be_local_p. */
2514 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2515 void *data ATTRIBUTE_UNUSED)
2517 return !(!node->needed
2518 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2519 || !node->local.externally_visible));
2522 /* Return true if NODE can be made local for API change.
2523 Extern inline functions and C++ COMDAT functions can be made local
2524 at the expense of possible code size growth if function is used in multiple
2525 compilation units. */
2527 cgraph_node_can_be_local_p (struct cgraph_node *node)
2529 return (!node->address_taken
2530 && !cgraph_for_node_and_aliases (node,
2531 cgraph_node_cannot_be_local_p_1,
2535 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2536 but other code such as notice_global_symbol generates rtl. */
2538 cgraph_make_decl_local (tree decl)
2542 if (TREE_CODE (decl) == VAR_DECL)
2543 DECL_COMMON (decl) = 0;
2544 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2546 if (DECL_COMDAT (decl))
2548 /* It is possible that we are linking against library defining same COMDAT
2549 function. To avoid conflict we need to rename our local name of the
2550 function just in the case WHOPR partitioning decide to make it hidden
2551 to avoid cross partition references. */
2554 const char *old_name;
2556 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2557 if (TREE_CODE (decl) == FUNCTION_DECL)
2559 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2560 change_decl_assembler_name (decl,
2561 clone_function_name (decl, "local"));
2562 if (node->local.lto_file_data)
2563 lto_record_renamed_decl (node->local.lto_file_data,
2566 (DECL_ASSEMBLER_NAME (decl)));
2568 else if (TREE_CODE (decl) == VAR_DECL)
2570 struct varpool_node *vnode = varpool_get_node (decl);
2571 /* change_decl_assembler_name will warn here on vtables because
2572 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2573 SET_DECL_ASSEMBLER_NAME (decl,
2574 clone_function_name (decl, "local"));
2575 if (vnode->lto_file_data)
2576 lto_record_renamed_decl (vnode->lto_file_data,
2579 (DECL_ASSEMBLER_NAME (decl)));
2582 DECL_SECTION_NAME (decl) = 0;
2583 DECL_COMDAT (decl) = 0;
2585 DECL_COMDAT_GROUP (decl) = 0;
2586 DECL_WEAK (decl) = 0;
2587 DECL_EXTERNAL (decl) = 0;
2588 TREE_PUBLIC (decl) = 0;
2589 if (!DECL_RTL_SET_P (decl))
2592 /* Update rtl flags. */
2593 make_decl_rtl (decl);
2595 rtl = DECL_RTL (decl);
2599 symbol = XEXP (rtl, 0);
2600 if (GET_CODE (symbol) != SYMBOL_REF)
2603 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2606 /* Call calback on NODE, thunks and aliases asociated to NODE.
2607 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2611 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2612 bool (*callback) (struct cgraph_node *, void *),
2614 bool include_overwritable)
2616 struct cgraph_edge *e;
2617 struct cgraph_node *alias;
2619 if (callback (node, data))
2621 for (alias = node->same_body; alias; alias = alias->next)
2622 if (callback (alias, data))
2624 for (e = node->callers; e; e = e->next_caller)
2625 if (e->caller->thunk.thunk_p
2626 && (include_overwritable
2627 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2628 cgraph_for_node_thunks_and_aliases (e->caller, callback, data, include_overwritable);
2632 /* Call calback on NODE and aliases asociated to NODE.
2633 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2637 cgraph_for_node_and_aliases (struct cgraph_node *node,
2638 bool (*callback) (struct cgraph_node *, void *),
2640 bool include_overwritable ATTRIBUTE_UNUSED)
2642 struct cgraph_node *alias;
2644 if (callback (node, data))
2646 for (alias = node->same_body; alias; alias = alias->next)
2647 if (callback (alias, data))
2652 /* Worker to bring NODE local. */
2655 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2657 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2658 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2660 cgraph_make_decl_local (node->decl);
2662 node->local.externally_visible = false;
2663 node->local.local = true;
2664 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2665 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2670 /* Bring NODE local. */
2673 cgraph_make_node_local (struct cgraph_node *node)
2675 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2679 /* Worker to set nothrow flag. */
2682 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2684 TREE_NOTHROW (node->decl) = data != NULL;
2688 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2689 if any to NOTHROW. */
2692 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2694 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2695 (void *)(size_t)nothrow, false);
2698 /* Worker to set const flag. */
2701 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2703 /* Static constructors and destructors without a side effect can be
2705 if (data && !((size_t)data & 2))
2707 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2708 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2709 if (DECL_STATIC_DESTRUCTOR (node->decl))
2710 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2712 TREE_READONLY (node->decl) = data != NULL;
2713 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2717 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2718 if any to READONLY. */
2721 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2723 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2724 (void *)(size_t)(readonly + (int)looping * 2),
2728 /* Worker to set pure flag. */
2731 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2733 /* Static pureructors and destructors without a side effect can be
2735 if (data && !((size_t)data & 2))
2737 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2738 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2739 if (DECL_STATIC_DESTRUCTOR (node->decl))
2740 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2742 DECL_PURE_P (node->decl) = data != NULL;
2743 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2747 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2751 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2753 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2754 (void *)(size_t)(pure + (int)looping * 2),
2758 /* Data used by cgraph_propagate_frequency. */
2760 struct cgraph_propagate_frequency_data
2762 bool maybe_unlikely_executed;
2763 bool maybe_executed_once;
2764 bool only_called_at_startup;
2765 bool only_called_at_exit;
2768 /* Worker for cgraph_propagate_frequency_1. */
2771 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
2773 struct cgraph_propagate_frequency_data *d;
2774 struct cgraph_edge *edge;
2776 d = (struct cgraph_propagate_frequency_data *)data;
2777 for (edge = node->callers;
2778 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
2779 || d->only_called_at_startup || d->only_called_at_exit);
2780 edge = edge->next_caller)
2782 if (edge->caller != node)
2784 d->only_called_at_startup &= edge->caller->only_called_at_startup;
2785 /* It makes sense to put main() together with the static constructors.
2786 It will be executed for sure, but rest of functions called from
2787 main are definitely not at startup only. */
2788 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2789 d->only_called_at_startup = 0;
2790 d->only_called_at_exit &= edge->caller->only_called_at_exit;
2792 if (!edge->frequency)
2794 switch (edge->caller->frequency)
2796 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2798 case NODE_FREQUENCY_EXECUTED_ONCE:
2799 if (dump_file && (dump_flags & TDF_DETAILS))
2800 fprintf (dump_file, " Called by %s that is executed once\n",
2801 cgraph_node_name (edge->caller));
2802 d->maybe_unlikely_executed = false;
2803 if (inline_edge_summary (edge)->loop_depth)
2805 d->maybe_executed_once = false;
2806 if (dump_file && (dump_flags & TDF_DETAILS))
2807 fprintf (dump_file, " Called in loop\n");
2810 case NODE_FREQUENCY_HOT:
2811 case NODE_FREQUENCY_NORMAL:
2812 if (dump_file && (dump_flags & TDF_DETAILS))
2813 fprintf (dump_file, " Called by %s that is normal or hot\n",
2814 cgraph_node_name (edge->caller));
2815 d->maybe_unlikely_executed = false;
2816 d->maybe_executed_once = false;
2820 return edge != NULL;
2823 /* See if the frequency of NODE can be updated based on frequencies of its
2826 cgraph_propagate_frequency (struct cgraph_node *node)
2828 struct cgraph_propagate_frequency_data d = {true, true, true, true};
2829 bool changed = false;
2831 if (!node->local.local)
2833 gcc_assert (node->analyzed);
2834 if (dump_file && (dump_flags & TDF_DETAILS))
2835 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2837 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
2839 if ((d.only_called_at_startup && !d.only_called_at_exit)
2840 && !node->only_called_at_startup)
2842 node->only_called_at_startup = true;
2844 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2845 cgraph_node_name (node));
2848 if ((d.only_called_at_exit && !d.only_called_at_startup)
2849 && !node->only_called_at_exit)
2851 node->only_called_at_exit = true;
2853 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2854 cgraph_node_name (node));
2857 /* These come either from profile or user hints; never update them. */
2858 if (node->frequency == NODE_FREQUENCY_HOT
2859 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2861 if (d.maybe_unlikely_executed)
2863 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2865 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2866 cgraph_node_name (node));
2869 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2871 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2873 fprintf (dump_file, "Node %s promoted to executed once.\n",
2874 cgraph_node_name (node));
2880 /* Return true when NODE can not return or throw and thus
2881 it is safe to ignore its side effects for IPA analysis. */
2884 cgraph_node_cannot_return (struct cgraph_node *node)
2886 int flags = flags_from_decl_or_type (node->decl);
2887 if (!flag_exceptions)
2888 return (flags & ECF_NORETURN) != 0;
2890 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2891 == (ECF_NORETURN | ECF_NOTHROW));
2894 /* Return true when call of E can not lead to return from caller
2895 and thus it is safe to ignore its side effects for IPA analysis
2896 when computing side effects of the caller.
2897 FIXME: We could actually mark all edges that have no reaching
2898 patch to EXIT_BLOCK_PTR or throw to get better results. */
2900 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2902 if (cgraph_node_cannot_return (e->caller))
2904 if (e->indirect_unknown_callee)
2906 int flags = e->indirect_info->ecf_flags;
2907 if (!flag_exceptions)
2908 return (flags & ECF_NORETURN) != 0;
2910 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2911 == (ECF_NORETURN | ECF_NOTHROW));
2914 return cgraph_node_cannot_return (e->callee);
2917 /* Return true when function NODE can be removed from callgraph
2918 if all direct calls are eliminated. */
2921 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2923 gcc_assert (!node->global.inlined_to);
2924 /* Extern inlines can always go, we will use the external definition. */
2925 if (DECL_EXTERNAL (node->decl))
2927 /* When function is needed, we can not remove it. */
2928 if (node->needed || node->reachable_from_other_partition)
2930 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2931 || DECL_STATIC_DESTRUCTOR (node->decl))
2933 /* Only COMDAT functions can be removed if externally visible. */
2934 if (node->local.externally_visible
2935 && (!DECL_COMDAT (node->decl)
2936 || cgraph_used_from_object_file_p (node)))
2941 /* Return true when function NODE can be expected to be removed
2942 from program when direct calls in this compilation unit are removed.
2944 As a special case COMDAT functions are
2945 cgraph_can_remove_if_no_direct_calls_p while the are not
2946 cgraph_only_called_directly_p (it is possible they are called from other
2949 This function behaves as cgraph_only_called_directly_p because eliminating
2950 all uses of COMDAT function does not make it necessarily disappear from
2951 the program unless we are compiling whole program or we do LTO. In this
2952 case we know we win since dynamic linking will not really discard the
2953 linkonce section. */
2956 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2958 gcc_assert (!node->global.inlined_to);
2959 if (cgraph_used_from_object_file_p (node))
2961 if (!in_lto_p && !flag_whole_program)
2962 return cgraph_only_called_directly_p (node);
2965 if (DECL_EXTERNAL (node->decl))
2967 return cgraph_can_remove_if_no_direct_calls_p (node);
2971 /* Return true when RESOLUTION indicate that linker will use
2972 the symbol from non-LTO object files. */
2975 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2977 return (resolution == LDPR_PREVAILING_DEF
2978 || resolution == LDPR_PREEMPTED_REG
2979 || resolution == LDPR_RESOLVED_EXEC
2980 || resolution == LDPR_RESOLVED_DYN);
2984 /* Return true when NODE is known to be used from other (non-LTO) object file.
2985 Known only when doing LTO via linker plugin. */
2988 cgraph_used_from_object_file_p (struct cgraph_node *node)
2990 gcc_assert (!node->global.inlined_to);
2991 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2993 if (resolution_used_from_other_file_p (node->resolution))
2998 /* Worker for cgraph_only_called_directly_p. */
3001 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3003 return !cgraph_only_called_directly_or_aliased_p (node);
3006 /* Return true when function NODE and all its aliases are only called
3008 i.e. it is not externally visible, address was not taken and
3009 it is not used in any other non-standard way. */
3012 cgraph_only_called_directly_p (struct cgraph_node *node)
3014 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
3015 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
3020 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3023 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
3025 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
3026 struct cgraph_edge *cs;
3027 enum availability avail;
3028 cgraph_function_or_thunk_node (node, &avail);
3030 if (avail > AVAIL_OVERWRITABLE)
3031 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3032 if (!cs->indirect_inlining_edge)
3033 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
3037 /* Collect all callers of NODE and its aliases that are known to lead to NODE
3038 (i.e. are not overwritable). */
3040 VEC (cgraph_edge_p, heap) *
3041 collect_callers_of_node (struct cgraph_node *node)
3043 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
3044 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
3045 &redirect_callers, false);
3046 return redirect_callers;
3049 #include "gt-cgraph.h"