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"
102 const char * const ld_plugin_symbol_resolution_names[]=
107 "prevailing_def_ironly",
115 static void cgraph_node_remove_callers (struct cgraph_node *node);
116 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
117 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
119 /* Hash table used to convert declarations into nodes. */
120 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
121 /* Hash table used to convert assembler names into nodes. */
122 static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
124 /* The linked list of cgraph nodes. */
125 struct cgraph_node *cgraph_nodes;
127 /* Queue of cgraph nodes scheduled to be lowered. */
128 struct cgraph_node *cgraph_nodes_queue;
130 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
131 secondary queue used during optimization to accommodate passes that
132 may generate new functions that need to be optimized and expanded. */
133 struct cgraph_node *cgraph_new_nodes;
135 /* Number of nodes in existence. */
138 /* Maximal uid used in cgraph nodes. */
141 /* Maximal uid used in cgraph edges. */
142 int cgraph_edge_max_uid;
144 /* Maximal pid used for profiling */
147 /* Set when whole unit has been analyzed so we can access global info. */
148 bool cgraph_global_info_ready = false;
150 /* What state callgraph is in right now. */
151 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
153 /* Set when the cgraph is fully build and the basic flags are computed. */
154 bool cgraph_function_flags_ready = false;
156 /* Linked list of cgraph asm nodes. */
157 struct cgraph_asm_node *cgraph_asm_nodes;
159 /* Last node in cgraph_asm_nodes. */
160 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
162 /* The order index of the next cgraph node to be created. This is
163 used so that we can sort the cgraph nodes in order by when we saw
164 them, to support -fno-toplevel-reorder. */
167 /* List of hooks triggered on cgraph_edge events. */
168 struct cgraph_edge_hook_list {
169 cgraph_edge_hook hook;
171 struct cgraph_edge_hook_list *next;
174 /* List of hooks triggered on cgraph_node events. */
175 struct cgraph_node_hook_list {
176 cgraph_node_hook hook;
178 struct cgraph_node_hook_list *next;
181 /* List of hooks triggered on events involving two cgraph_edges. */
182 struct cgraph_2edge_hook_list {
183 cgraph_2edge_hook hook;
185 struct cgraph_2edge_hook_list *next;
188 /* List of hooks triggered on events involving two cgraph_nodes. */
189 struct cgraph_2node_hook_list {
190 cgraph_2node_hook hook;
192 struct cgraph_2node_hook_list *next;
195 /* List of hooks triggered when an edge is removed. */
196 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
197 /* List of hooks triggered when a node is removed. */
198 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
199 /* List of hooks triggered when an edge is duplicated. */
200 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
201 /* List of hooks triggered when a node is duplicated. */
202 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
203 /* List of hooks triggered when an function is inserted. */
204 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
206 /* Head of a linked list of unused (freed) call graph nodes.
207 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
208 static GTY(()) struct cgraph_node *free_nodes;
209 /* Head of a linked list of unused (freed) call graph edges.
210 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
211 static GTY(()) struct cgraph_edge *free_edges;
213 /* Macros to access the next item in the list of free cgraph nodes and
215 #define NEXT_FREE_NODE(NODE) (NODE)->next
216 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
218 /* Register HOOK to be called with DATA on each removed edge. */
219 struct cgraph_edge_hook_list *
220 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
222 struct cgraph_edge_hook_list *entry;
223 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
225 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
235 /* Remove ENTRY from the list of hooks called on removing edges. */
237 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
239 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
241 while (*ptr != entry)
247 /* Call all edge removal hooks. */
249 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
251 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
254 entry->hook (e, entry->data);
259 /* Register HOOK to be called with DATA on each removed node. */
260 struct cgraph_node_hook_list *
261 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
263 struct cgraph_node_hook_list *entry;
264 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
266 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
276 /* Remove ENTRY from the list of hooks called on removing nodes. */
278 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
280 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
282 while (*ptr != entry)
288 /* Call all node removal hooks. */
290 cgraph_call_node_removal_hooks (struct cgraph_node *node)
292 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
295 entry->hook (node, entry->data);
300 /* Register HOOK to be called with DATA on each inserted node. */
301 struct cgraph_node_hook_list *
302 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
304 struct cgraph_node_hook_list *entry;
305 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
307 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
317 /* Remove ENTRY from the list of hooks called on inserted nodes. */
319 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
321 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
323 while (*ptr != entry)
329 /* Call all node insertion hooks. */
331 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
333 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
336 entry->hook (node, entry->data);
341 /* Register HOOK to be called with DATA on each duplicated edge. */
342 struct cgraph_2edge_hook_list *
343 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
345 struct cgraph_2edge_hook_list *entry;
346 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
348 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
358 /* Remove ENTRY from the list of hooks called on duplicating edges. */
360 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
362 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
364 while (*ptr != entry)
370 /* Call all edge duplication hooks. */
372 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
373 struct cgraph_edge *cs2)
375 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
378 entry->hook (cs1, cs2, entry->data);
383 /* Register HOOK to be called with DATA on each duplicated node. */
384 struct cgraph_2node_hook_list *
385 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
387 struct cgraph_2node_hook_list *entry;
388 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
390 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
400 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
402 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
404 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
406 while (*ptr != entry)
412 /* Call all node duplication hooks. */
414 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
415 struct cgraph_node *node2)
417 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
420 entry->hook (node1, node2, entry->data);
425 /* Returns a hash code for P. */
428 hash_node (const void *p)
430 const struct cgraph_node *n = (const struct cgraph_node *) p;
431 return (hashval_t) DECL_UID (n->decl);
435 /* Returns nonzero if P1 and P2 are equal. */
438 eq_node (const void *p1, const void *p2)
440 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
441 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
442 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
445 /* Allocate new callgraph node. */
447 static inline struct cgraph_node *
448 cgraph_allocate_node (void)
450 struct cgraph_node *node;
455 free_nodes = NEXT_FREE_NODE (node);
459 node = ggc_alloc_cleared_cgraph_node ();
460 node->uid = cgraph_max_uid++;
466 /* Allocate new callgraph node and insert it into basic data structures. */
468 static struct cgraph_node *
469 cgraph_create_node (void)
471 struct cgraph_node *node = cgraph_allocate_node ();
473 node->next = cgraph_nodes;
475 node->order = cgraph_order++;
477 cgraph_nodes->previous = node;
478 node->previous = NULL;
479 node->global.estimated_growth = INT_MIN;
480 node->frequency = NODE_FREQUENCY_NORMAL;
481 node->count_materialization_scale = REG_BR_PROB_BASE;
482 ipa_empty_ref_list (&node->ref_list);
488 /* Return cgraph node assigned to DECL. Create new one when needed. */
491 cgraph_node (tree decl)
493 struct cgraph_node key, *node, **slot;
495 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
498 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
502 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
507 if (node->same_body_alias)
508 node = node->same_body;
512 node = cgraph_create_node ();
515 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
517 node->origin = cgraph_node (DECL_CONTEXT (decl));
518 node->next_nested = node->origin->nested;
519 node->origin->nested = node;
521 if (assembler_name_hash)
524 tree name = DECL_ASSEMBLER_NAME (decl);
526 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
527 decl_assembler_name_hash (name),
529 /* We can have multiple declarations with same assembler name. For C++
530 it is __builtin_strlen and strlen, for instance. Do we need to
531 record them all? Original implementation marked just first one
532 so lets hope for the best. */
539 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
540 the function body is associated with (not neccesarily cgraph_node (DECL). */
542 static struct cgraph_node *
543 cgraph_same_body_alias_1 (struct cgraph_node *decl_node, tree alias, tree decl)
545 struct cgraph_node key, *alias_node, **slot;
547 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
548 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
552 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
554 /* If the cgraph_node has been already created, fail. */
558 alias_node = cgraph_allocate_node ();
559 alias_node->decl = alias;
560 alias_node->same_body_alias = 1;
561 alias_node->same_body = decl_node;
562 alias_node->previous = NULL;
563 if (decl_node->same_body)
564 decl_node->same_body->previous = alias_node;
565 alias_node->next = decl_node->same_body;
566 alias_node->thunk.alias = decl;
567 decl_node->same_body = alias_node;
572 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
574 Same body aliases are output whenever the body of DECL is output,
575 and cgraph_node (ALIAS) transparently returns cgraph_node (DECL). */
578 cgraph_same_body_alias (struct cgraph_node *decl_node, tree alias, tree decl)
580 #ifndef ASM_OUTPUT_DEF
581 /* If aliases aren't supported by the assembler, fail. */
585 /*gcc_assert (!assembler_name_hash);*/
587 return cgraph_same_body_alias_1 (decl_node, alias, decl);
590 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
591 aliases DECL with an adjustments made into the first parameter.
592 See comments in thunk_adjust for detail on the parameters. */
595 cgraph_add_thunk (struct cgraph_node *decl_node, tree alias, tree decl,
597 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
601 struct cgraph_node *node = cgraph_get_node (alias);
605 gcc_assert (node->local.finalized);
606 gcc_assert (!node->same_body);
607 cgraph_remove_node (node);
610 node = cgraph_same_body_alias_1 (decl_node, alias, decl);
612 gcc_checking_assert (!virtual_offset
613 || tree_int_cst_equal (virtual_offset,
614 size_int (virtual_value)));
615 node->thunk.fixed_offset = fixed_offset;
616 node->thunk.this_adjusting = this_adjusting;
617 node->thunk.virtual_value = virtual_value;
618 node->thunk.virtual_offset_p = virtual_offset != NULL;
619 node->thunk.alias = real_alias;
620 node->thunk.thunk_p = true;
624 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
628 cgraph_get_node_or_alias (const_tree decl)
630 struct cgraph_node key, *node = NULL, **slot;
632 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
637 key.decl = CONST_CAST2 (tree, const_tree, decl);
639 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
647 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
651 cgraph_get_node (const_tree decl)
653 struct cgraph_node key, *node = NULL, **slot;
655 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
660 key.decl = CONST_CAST2 (tree, const_tree, decl);
662 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
668 if (node->same_body_alias)
669 node = node->same_body;
674 /* Insert already constructed node into hashtable. */
677 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
679 struct cgraph_node **slot;
681 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
687 /* Returns a hash code for P. */
690 hash_node_by_assembler_name (const void *p)
692 const struct cgraph_node *n = (const struct cgraph_node *) p;
693 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
696 /* Returns nonzero if P1 and P2 are equal. */
699 eq_assembler_name (const void *p1, const void *p2)
701 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
702 const_tree name = (const_tree)p2;
703 return (decl_assembler_name_equal (n1->decl, name));
706 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
707 Return NULL if there's no such node. */
710 cgraph_node_for_asm (tree asmname)
712 struct cgraph_node *node;
715 if (!assembler_name_hash)
717 assembler_name_hash =
718 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
720 for (node = cgraph_nodes; node; node = node->next)
721 if (!node->global.inlined_to)
723 tree name = DECL_ASSEMBLER_NAME (node->decl);
724 slot = htab_find_slot_with_hash (assembler_name_hash, name,
725 decl_assembler_name_hash (name),
727 /* We can have multiple declarations with same assembler name. For C++
728 it is __builtin_strlen and strlen, for instance. Do we need to
729 record them all? Original implementation marked just first one
730 so lets hope for the best. */
735 struct cgraph_node *alias;
737 for (alias = node->same_body; alias; alias = alias->next)
740 name = DECL_ASSEMBLER_NAME (alias->decl);
741 hash = decl_assembler_name_hash (name);
742 slot = htab_find_slot_with_hash (assembler_name_hash, name,
751 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
752 decl_assembler_name_hash (asmname),
757 node = (struct cgraph_node *) *slot;
758 if (node->same_body_alias)
759 node = node->same_body;
765 /* Returns a hash value for X (which really is a die_struct). */
768 edge_hash (const void *x)
770 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
773 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
776 edge_eq (const void *x, const void *y)
778 return ((const struct cgraph_edge *) x)->call_stmt == y;
781 /* Add call graph edge E to call site hash of its caller. */
784 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
787 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
789 htab_hash_pointer (e->call_stmt),
795 /* Return the callgraph edge representing the GIMPLE_CALL statement
799 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
801 struct cgraph_edge *e, *e2;
804 if (node->call_site_hash)
805 return (struct cgraph_edge *)
806 htab_find_with_hash (node->call_site_hash, call_stmt,
807 htab_hash_pointer (call_stmt));
809 /* This loop may turn out to be performance problem. In such case adding
810 hashtables into call nodes with very many edges is probably best
811 solution. It is not good idea to add pointer into CALL_EXPR itself
812 because we want to make possible having multiple cgraph nodes representing
813 different clones of the same body before the body is actually cloned. */
814 for (e = node->callees; e; e = e->next_callee)
816 if (e->call_stmt == call_stmt)
822 for (e = node->indirect_calls; e; e = e->next_callee)
824 if (e->call_stmt == call_stmt)
831 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
832 for (e2 = node->callees; e2; e2 = e2->next_callee)
833 cgraph_add_edge_to_call_site_hash (e2);
834 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
835 cgraph_add_edge_to_call_site_hash (e2);
842 /* Change field call_stmt of edge E to NEW_STMT. */
845 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
849 if (e->caller->call_site_hash)
851 htab_remove_elt_with_hash (e->caller->call_site_hash,
853 htab_hash_pointer (e->call_stmt));
856 e->call_stmt = new_stmt;
857 if (e->indirect_unknown_callee
858 && (decl = gimple_call_fndecl (new_stmt)))
860 /* Constant propagation (and possibly also inlining?) can turn an
861 indirect call into a direct one. */
862 struct cgraph_node *new_callee = cgraph_node (decl);
864 cgraph_make_edge_direct (e, new_callee, 0);
867 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
868 e->can_throw_external = stmt_can_throw_external (new_stmt);
870 if (e->caller->call_site_hash)
871 cgraph_add_edge_to_call_site_hash (e);
874 /* Like cgraph_set_call_stmt but walk the clone tree and update all
875 clones sharing the same function body. */
878 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
879 gimple old_stmt, gimple new_stmt)
881 struct cgraph_node *node;
882 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
885 cgraph_set_call_stmt (edge, new_stmt);
891 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
893 cgraph_set_call_stmt (edge, new_stmt);
896 else if (node->next_sibling_clone)
897 node = node->next_sibling_clone;
900 while (node != orig && !node->next_sibling_clone)
901 node = node->clone_of;
903 node = node->next_sibling_clone;
908 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
909 same function body. If clones already have edge for OLD_STMT; only
910 update the edge same way as cgraph_set_call_stmt_including_clones does.
912 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
913 frequencies of the clones. */
916 cgraph_create_edge_including_clones (struct cgraph_node *orig,
917 struct cgraph_node *callee,
919 gimple stmt, gcov_type count,
920 int freq, int loop_depth,
921 cgraph_inline_failed_t reason)
923 struct cgraph_node *node;
924 struct cgraph_edge *edge;
926 if (!cgraph_edge (orig, stmt))
928 edge = cgraph_create_edge (orig, callee, stmt, count, freq, loop_depth);
929 edge->inline_failed = reason;
936 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
938 /* It is possible that clones already contain the edge while
939 master didn't. Either we promoted indirect call into direct
940 call in the clone or we are processing clones of unreachable
941 master where edges has been removed. */
943 cgraph_set_call_stmt (edge, stmt);
944 else if (!cgraph_edge (node, stmt))
946 edge = cgraph_create_edge (node, callee, stmt, count,
948 edge->inline_failed = reason;
953 else if (node->next_sibling_clone)
954 node = node->next_sibling_clone;
957 while (node != orig && !node->next_sibling_clone)
958 node = node->clone_of;
960 node = node->next_sibling_clone;
965 /* Give initial reasons why inlining would fail on EDGE. This gets either
966 nullified or usually overwritten by more precise reasons later. */
969 initialize_inline_failed (struct cgraph_edge *e)
971 struct cgraph_node *callee = e->callee;
973 if (e->indirect_unknown_callee)
974 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
975 else if (!callee->analyzed)
976 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
977 else if (callee->local.redefined_extern_inline)
978 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
979 else if (!callee->local.inlinable)
980 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
981 else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
982 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
984 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
987 /* Allocate a cgraph_edge structure and fill it with data according to the
988 parameters of which only CALLEE can be NULL (when creating an indirect call
991 static struct cgraph_edge *
992 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
993 gimple call_stmt, gcov_type count, int freq, int nest)
995 struct cgraph_edge *edge;
997 /* LTO does not actually have access to the call_stmt since these
998 have not been loaded yet. */
1001 /* This is a rather expensive check possibly triggering
1002 construction of call stmt hashtable. */
1003 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
1005 gcc_assert (is_gimple_call (call_stmt));
1011 free_edges = NEXT_FREE_EDGE (edge);
1015 edge = ggc_alloc_cgraph_edge ();
1016 edge->uid = cgraph_edge_max_uid++;
1020 edge->caller = caller;
1021 edge->callee = callee;
1022 edge->prev_caller = NULL;
1023 edge->next_caller = NULL;
1024 edge->prev_callee = NULL;
1025 edge->next_callee = NULL;
1027 edge->count = count;
1028 gcc_assert (count >= 0);
1029 edge->frequency = freq;
1030 gcc_assert (freq >= 0);
1031 gcc_assert (freq <= CGRAPH_FREQ_MAX);
1032 edge->loop_nest = nest;
1034 edge->call_stmt = call_stmt;
1035 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
1036 edge->can_throw_external
1037 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
1039 edge->call_stmt_cannot_inline_p =
1040 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1041 if (call_stmt && caller->call_site_hash)
1042 cgraph_add_edge_to_call_site_hash (edge);
1044 edge->indirect_info = NULL;
1045 edge->indirect_inlining_edge = 0;
1050 /* Create edge from CALLER to CALLEE in the cgraph. */
1052 struct cgraph_edge *
1053 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1054 gimple call_stmt, gcov_type count, int freq, int nest)
1056 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1059 edge->indirect_unknown_callee = 0;
1060 initialize_inline_failed (edge);
1062 edge->next_caller = callee->callers;
1063 if (callee->callers)
1064 callee->callers->prev_caller = edge;
1065 edge->next_callee = caller->callees;
1066 if (caller->callees)
1067 caller->callees->prev_callee = edge;
1068 caller->callees = edge;
1069 callee->callers = edge;
1074 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1076 struct cgraph_indirect_call_info *
1077 cgraph_allocate_init_indirect_info (void)
1079 struct cgraph_indirect_call_info *ii;
1081 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1082 ii->param_index = -1;
1086 /* Create an indirect edge with a yet-undetermined callee where the call
1087 statement destination is a formal parameter of the caller with index
1090 struct cgraph_edge *
1091 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1093 gcov_type count, int freq, int nest)
1095 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1098 edge->indirect_unknown_callee = 1;
1099 initialize_inline_failed (edge);
1101 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1102 edge->indirect_info->ecf_flags = ecf_flags;
1104 edge->next_callee = caller->indirect_calls;
1105 if (caller->indirect_calls)
1106 caller->indirect_calls->prev_callee = edge;
1107 caller->indirect_calls = edge;
1112 /* Remove the edge E from the list of the callers of the callee. */
1115 cgraph_edge_remove_callee (struct cgraph_edge *e)
1117 gcc_assert (!e->indirect_unknown_callee);
1119 e->prev_caller->next_caller = e->next_caller;
1121 e->next_caller->prev_caller = e->prev_caller;
1122 if (!e->prev_caller)
1123 e->callee->callers = e->next_caller;
1126 /* Remove the edge E from the list of the callees of the caller. */
1129 cgraph_edge_remove_caller (struct cgraph_edge *e)
1132 e->prev_callee->next_callee = e->next_callee;
1134 e->next_callee->prev_callee = e->prev_callee;
1135 if (!e->prev_callee)
1137 if (e->indirect_unknown_callee)
1138 e->caller->indirect_calls = e->next_callee;
1140 e->caller->callees = e->next_callee;
1142 if (e->caller->call_site_hash)
1143 htab_remove_elt_with_hash (e->caller->call_site_hash,
1145 htab_hash_pointer (e->call_stmt));
1148 /* Put the edge onto the free list. */
1151 cgraph_free_edge (struct cgraph_edge *e)
1155 /* Clear out the edge so we do not dangle pointers. */
1156 memset (e, 0, sizeof (*e));
1158 NEXT_FREE_EDGE (e) = free_edges;
1162 /* Remove the edge E in the cgraph. */
1165 cgraph_remove_edge (struct cgraph_edge *e)
1167 /* Call all edge removal hooks. */
1168 cgraph_call_edge_removal_hooks (e);
1170 if (!e->indirect_unknown_callee)
1171 /* Remove from callers list of the callee. */
1172 cgraph_edge_remove_callee (e);
1174 /* Remove from callees list of the callers. */
1175 cgraph_edge_remove_caller (e);
1177 /* Put the edge onto the free list. */
1178 cgraph_free_edge (e);
1181 /* Set callee of call graph edge E and add it to the corresponding set of
1185 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1187 e->prev_caller = NULL;
1189 n->callers->prev_caller = e;
1190 e->next_caller = n->callers;
1195 /* Redirect callee of E to N. The function does not update underlying
1199 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1201 /* Remove from callers list of the current callee. */
1202 cgraph_edge_remove_callee (e);
1204 /* Insert to callers list of the new callee. */
1205 cgraph_set_edge_callee (e, n);
1208 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1209 CALLEE. DELTA is an integer constant that is to be added to the this
1210 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1213 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
1214 HOST_WIDE_INT delta)
1216 edge->indirect_unknown_callee = 0;
1217 edge->indirect_info->thunk_delta = delta;
1219 /* Get the edge out of the indirect edge list. */
1220 if (edge->prev_callee)
1221 edge->prev_callee->next_callee = edge->next_callee;
1222 if (edge->next_callee)
1223 edge->next_callee->prev_callee = edge->prev_callee;
1224 if (!edge->prev_callee)
1225 edge->caller->indirect_calls = edge->next_callee;
1227 /* Put it into the normal callee list */
1228 edge->prev_callee = NULL;
1229 edge->next_callee = edge->caller->callees;
1230 if (edge->caller->callees)
1231 edge->caller->callees->prev_callee = edge;
1232 edge->caller->callees = edge;
1234 /* Insert to callers list of the new callee. */
1235 cgraph_set_edge_callee (edge, callee);
1237 /* We need to re-determine the inlining status of the edge. */
1238 initialize_inline_failed (edge);
1242 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1243 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1244 of OLD_STMT if it was previously call statement.
1245 If NEW_STMT is NULL, the call has been dropped without any
1249 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1250 gimple old_stmt, tree old_call,
1253 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1254 ? gimple_call_fndecl (new_stmt) : 0;
1256 /* We are seeing indirect calls, then there is nothing to update. */
1257 if (!new_call && !old_call)
1259 /* See if we turned indirect call into direct call or folded call to one builtin
1260 into different builtin. */
1261 if (old_call != new_call)
1263 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1264 struct cgraph_edge *ne = NULL;
1271 /* See if the edge is already there and has the correct callee. It
1272 might be so because of indirect inlining has already updated
1273 it. We also might've cloned and redirected the edge. */
1274 if (new_call && e->callee)
1276 struct cgraph_node *callee = e->callee;
1279 if (callee->decl == new_call
1280 || callee->former_clone_of == new_call)
1282 callee = callee->clone_of;
1286 /* Otherwise remove edge and create new one; we can't simply redirect
1287 since function has changed, so inline plan and other information
1288 attached to edge is invalid. */
1290 frequency = e->frequency;
1291 loop_nest = e->loop_nest;
1292 cgraph_remove_edge (e);
1296 /* We are seeing new direct call; compute profile info based on BB. */
1297 basic_block bb = gimple_bb (new_stmt);
1299 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1301 loop_nest = bb->loop_depth;
1306 ne = cgraph_create_edge (node, cgraph_node (new_call),
1307 new_stmt, count, frequency,
1309 gcc_assert (ne->inline_failed);
1312 /* We only updated the call stmt; update pointer in cgraph edge.. */
1313 else if (old_stmt != new_stmt)
1314 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1317 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1318 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1319 of OLD_STMT before it was updated (updating can happen inplace). */
1322 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1324 struct cgraph_node *orig = cgraph_node (cfun->decl);
1325 struct cgraph_node *node;
1327 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1329 for (node = orig->clones; node != orig;)
1331 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1333 node = node->clones;
1334 else if (node->next_sibling_clone)
1335 node = node->next_sibling_clone;
1338 while (node != orig && !node->next_sibling_clone)
1339 node = node->clone_of;
1341 node = node->next_sibling_clone;
1347 /* Remove all callees from the node. */
1350 cgraph_node_remove_callees (struct cgraph_node *node)
1352 struct cgraph_edge *e, *f;
1354 /* It is sufficient to remove the edges from the lists of callers of
1355 the callees. The callee list of the node can be zapped with one
1357 for (e = node->callees; e; e = f)
1360 cgraph_call_edge_removal_hooks (e);
1361 if (!e->indirect_unknown_callee)
1362 cgraph_edge_remove_callee (e);
1363 cgraph_free_edge (e);
1365 for (e = node->indirect_calls; e; e = f)
1368 cgraph_call_edge_removal_hooks (e);
1369 if (!e->indirect_unknown_callee)
1370 cgraph_edge_remove_callee (e);
1371 cgraph_free_edge (e);
1373 node->indirect_calls = NULL;
1374 node->callees = NULL;
1375 if (node->call_site_hash)
1377 htab_delete (node->call_site_hash);
1378 node->call_site_hash = NULL;
1382 /* Remove all callers from the node. */
1385 cgraph_node_remove_callers (struct cgraph_node *node)
1387 struct cgraph_edge *e, *f;
1389 /* It is sufficient to remove the edges from the lists of callees of
1390 the callers. The caller list of the node can be zapped with one
1392 for (e = node->callers; e; e = f)
1395 cgraph_call_edge_removal_hooks (e);
1396 cgraph_edge_remove_caller (e);
1397 cgraph_free_edge (e);
1399 node->callers = NULL;
1402 /* Release memory used to represent body of function NODE. */
1405 cgraph_release_function_body (struct cgraph_node *node)
1407 if (DECL_STRUCT_FUNCTION (node->decl))
1409 tree old_decl = current_function_decl;
1410 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1411 if (cfun->gimple_df)
1413 current_function_decl = node->decl;
1415 delete_tree_cfg_annotations ();
1417 current_function_decl = old_decl;
1421 gcc_assert (dom_computed[0] == DOM_NONE);
1422 gcc_assert (dom_computed[1] == DOM_NONE);
1425 if (cfun->value_histograms)
1427 gcc_assert (!current_loops);
1429 gimple_set_body (node->decl, NULL);
1430 VEC_free (ipa_opt_pass, heap,
1431 node->ipa_transforms_to_apply);
1432 /* Struct function hangs a lot of data that would leak if we didn't
1433 removed all pointers to it. */
1434 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1435 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1437 DECL_SAVED_TREE (node->decl) = NULL;
1438 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1439 of its associated function function declaration because it's
1440 needed to emit debug info later. */
1441 if (!node->abstract_and_needed)
1442 DECL_INITIAL (node->decl) = error_mark_node;
1445 /* Remove same body alias node. */
1448 cgraph_remove_same_body_alias (struct cgraph_node *node)
1451 int uid = node->uid;
1453 gcc_assert (node->same_body_alias);
1455 node->previous->next = node->next;
1457 node->same_body->same_body = node->next;
1459 node->next->previous = node->previous;
1461 node->previous = NULL;
1462 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1464 htab_clear_slot (cgraph_hash, slot);
1465 if (assembler_name_hash)
1467 tree name = DECL_ASSEMBLER_NAME (node->decl);
1468 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1469 decl_assembler_name_hash (name),
1471 if (slot && *slot == node)
1472 htab_clear_slot (assembler_name_hash, slot);
1475 /* Clear out the node to NULL all pointers and add the node to the free
1477 memset (node, 0, sizeof(*node));
1479 NEXT_FREE_NODE (node) = free_nodes;
1483 /* Remove the node from cgraph. */
1486 cgraph_remove_node (struct cgraph_node *node)
1489 bool kill_body = false;
1490 struct cgraph_node *n;
1491 int uid = node->uid;
1493 cgraph_call_node_removal_hooks (node);
1494 cgraph_node_remove_callers (node);
1495 cgraph_node_remove_callees (node);
1496 ipa_remove_all_references (&node->ref_list);
1497 ipa_remove_all_refering (&node->ref_list);
1498 VEC_free (ipa_opt_pass, heap,
1499 node->ipa_transforms_to_apply);
1501 /* Incremental inlining access removed nodes stored in the postorder list.
1503 node->needed = node->reachable = false;
1504 for (n = node->nested; n; n = n->next_nested)
1506 node->nested = NULL;
1509 struct cgraph_node **node2 = &node->origin->nested;
1511 while (*node2 != node)
1512 node2 = &(*node2)->next_nested;
1513 *node2 = node->next_nested;
1516 node->previous->next = node->next;
1518 cgraph_nodes = node->next;
1520 node->next->previous = node->previous;
1522 node->previous = NULL;
1523 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1526 struct cgraph_node *next_inline_clone;
1528 for (next_inline_clone = node->clones;
1529 next_inline_clone && next_inline_clone->decl != node->decl;
1530 next_inline_clone = next_inline_clone->next_sibling_clone)
1533 /* If there is inline clone of the node being removed, we need
1534 to put it into the position of removed node and reorganize all
1535 other clones to be based on it. */
1536 if (next_inline_clone)
1538 struct cgraph_node *n;
1539 struct cgraph_node *new_clones;
1541 *slot = next_inline_clone;
1543 /* Unlink inline clone from the list of clones of removed node. */
1544 if (next_inline_clone->next_sibling_clone)
1545 next_inline_clone->next_sibling_clone->prev_sibling_clone
1546 = next_inline_clone->prev_sibling_clone;
1547 if (next_inline_clone->prev_sibling_clone)
1549 gcc_assert (node->clones != next_inline_clone);
1550 next_inline_clone->prev_sibling_clone->next_sibling_clone
1551 = next_inline_clone->next_sibling_clone;
1555 gcc_assert (node->clones == next_inline_clone);
1556 node->clones = next_inline_clone->next_sibling_clone;
1559 new_clones = node->clones;
1560 node->clones = NULL;
1562 /* Copy clone info. */
1563 next_inline_clone->clone = node->clone;
1565 /* Now place it into clone tree at same level at NODE. */
1566 next_inline_clone->clone_of = node->clone_of;
1567 next_inline_clone->prev_sibling_clone = NULL;
1568 next_inline_clone->next_sibling_clone = NULL;
1571 if (node->clone_of->clones)
1572 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1573 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1574 node->clone_of->clones = next_inline_clone;
1577 /* Merge the clone list. */
1580 if (!next_inline_clone->clones)
1581 next_inline_clone->clones = new_clones;
1584 n = next_inline_clone->clones;
1585 while (n->next_sibling_clone)
1586 n = n->next_sibling_clone;
1587 n->next_sibling_clone = new_clones;
1588 new_clones->prev_sibling_clone = n;
1592 /* Update clone_of pointers. */
1596 n->clone_of = next_inline_clone;
1597 n = n->next_sibling_clone;
1602 htab_clear_slot (cgraph_hash, slot);
1607 if (node->prev_sibling_clone)
1608 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1609 else if (node->clone_of)
1610 node->clone_of->clones = node->next_sibling_clone;
1611 if (node->next_sibling_clone)
1612 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1615 struct cgraph_node *n, *next;
1619 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1620 n->clone_of = node->clone_of;
1621 n->clone_of = node->clone_of;
1622 n->next_sibling_clone = node->clone_of->clones;
1623 if (node->clone_of->clones)
1624 node->clone_of->clones->prev_sibling_clone = n;
1625 node->clone_of->clones = node->clones;
1629 /* We are removing node with clones. this makes clones inconsistent,
1630 but assume they will be removed subsequently and just keep clone
1631 tree intact. This can happen in unreachable function removal since
1632 we remove unreachable functions in random order, not by bottom-up
1633 walk of clone trees. */
1634 for (n = node->clones; n; n = next)
1636 next = n->next_sibling_clone;
1637 n->next_sibling_clone = NULL;
1638 n->prev_sibling_clone = NULL;
1644 while (node->same_body)
1645 cgraph_remove_same_body_alias (node->same_body);
1647 if (node->same_comdat_group)
1649 struct cgraph_node *prev;
1650 for (prev = node->same_comdat_group;
1651 prev->same_comdat_group != node;
1652 prev = prev->same_comdat_group)
1654 if (node->same_comdat_group == prev)
1655 prev->same_comdat_group = NULL;
1657 prev->same_comdat_group = node->same_comdat_group;
1658 node->same_comdat_group = NULL;
1661 /* While all the clones are removed after being proceeded, the function
1662 itself is kept in the cgraph even after it is compiled. Check whether
1663 we are done with this body and reclaim it proactively if this is the case.
1665 if (!kill_body && *slot)
1667 struct cgraph_node *n = (struct cgraph_node *) *slot;
1668 if (!n->clones && !n->clone_of && !n->global.inlined_to
1669 && (cgraph_global_info_ready
1670 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1671 || n->in_other_partition)))
1674 if (assembler_name_hash)
1676 tree name = DECL_ASSEMBLER_NAME (node->decl);
1677 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1678 decl_assembler_name_hash (name),
1680 /* Inline clones are not hashed. */
1681 if (slot && *slot == node)
1682 htab_clear_slot (assembler_name_hash, slot);
1686 cgraph_release_function_body (node);
1688 if (node->call_site_hash)
1690 htab_delete (node->call_site_hash);
1691 node->call_site_hash = NULL;
1695 /* Clear out the node to NULL all pointers and add the node to the free
1697 memset (node, 0, sizeof(*node));
1699 NEXT_FREE_NODE (node) = free_nodes;
1703 /* Remove the node from cgraph and all inline clones inlined into it.
1704 Skip however removal of FORBIDDEN_NODE and return true if it needs to be
1705 removed. This allows to call the function from outer loop walking clone
1709 cgraph_remove_node_and_inline_clones (struct cgraph_node *node, struct cgraph_node *forbidden_node)
1711 struct cgraph_edge *e, *next;
1714 if (node == forbidden_node)
1716 for (e = node->callees; e; e = next)
1718 next = e->next_callee;
1719 if (!e->inline_failed)
1720 found |= cgraph_remove_node_and_inline_clones (e->callee, forbidden_node);
1722 cgraph_remove_node (node);
1726 /* Notify finalize_compilation_unit that given node is reachable. */
1729 cgraph_mark_reachable_node (struct cgraph_node *node)
1731 if (!node->reachable && node->local.finalized)
1733 if (cgraph_global_info_ready)
1735 /* Verify that function does not appear to be needed out of blue
1736 during the optimization process. This can happen for extern
1737 inlines when bodies was removed after inlining. */
1738 gcc_assert ((node->analyzed || node->in_other_partition
1739 || DECL_EXTERNAL (node->decl)));
1742 notice_global_symbol (node->decl);
1743 node->reachable = 1;
1745 node->next_needed = cgraph_nodes_queue;
1746 cgraph_nodes_queue = node;
1750 /* Likewise indicate that a node is needed, i.e. reachable via some
1754 cgraph_mark_needed_node (struct cgraph_node *node)
1757 gcc_assert (!node->global.inlined_to);
1758 cgraph_mark_reachable_node (node);
1761 /* Likewise indicate that a node is having address taken. */
1764 cgraph_mark_address_taken_node (struct cgraph_node *node)
1766 gcc_assert (!node->global.inlined_to);
1767 cgraph_mark_reachable_node (node);
1768 node->address_taken = 1;
1771 /* Return local info for the compiled function. */
1773 struct cgraph_local_info *
1774 cgraph_local_info (tree decl)
1776 struct cgraph_node *node;
1778 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1779 node = cgraph_node (decl);
1780 return &node->local;
1783 /* Return local info for the compiled function. */
1785 struct cgraph_global_info *
1786 cgraph_global_info (tree decl)
1788 struct cgraph_node *node;
1790 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1791 node = cgraph_node (decl);
1792 return &node->global;
1795 /* Return local info for the compiled function. */
1797 struct cgraph_rtl_info *
1798 cgraph_rtl_info (tree decl)
1800 struct cgraph_node *node;
1802 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1803 node = cgraph_node (decl);
1804 if (decl != current_function_decl
1805 && !TREE_ASM_WRITTEN (node->decl))
1810 /* Return a string describing the failure REASON. */
1813 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1816 #define DEFCIFCODE(code, string) string,
1818 static const char *cif_string_table[CIF_N_REASONS] = {
1819 #include "cif-code.def"
1822 /* Signedness of an enum type is implementation defined, so cast it
1823 to unsigned before testing. */
1824 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1825 return cif_string_table[reason];
1828 /* Return name of the node used in debug output. */
1830 cgraph_node_name (struct cgraph_node *node)
1832 return lang_hooks.decl_printable_name (node->decl, 2);
1835 /* Names used to print out the availability enum. */
1836 const char * const cgraph_availability_names[] =
1837 {"unset", "not_available", "overwritable", "available", "local"};
1840 /* Dump call graph node NODE to file F. */
1843 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1845 struct cgraph_edge *edge;
1846 int indirect_calls_count = 0;
1848 fprintf (f, "%s/%i(%i)", cgraph_node_name (node), node->uid,
1850 dump_addr (f, " @", (void *)node);
1851 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1852 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1853 if (node->global.inlined_to)
1854 fprintf (f, " (inline copy in %s/%i)",
1855 cgraph_node_name (node->global.inlined_to),
1856 node->global.inlined_to->uid);
1857 if (node->same_comdat_group)
1858 fprintf (f, " (same comdat group as %s/%i)",
1859 cgraph_node_name (node->same_comdat_group),
1860 node->same_comdat_group->uid);
1862 fprintf (f, " (clone of %s/%i)",
1863 cgraph_node_name (node->clone_of),
1864 node->clone_of->uid);
1865 if (cgraph_function_flags_ready)
1866 fprintf (f, " availability:%s",
1867 cgraph_availability_names [cgraph_function_body_availability (node)]);
1869 fprintf (f, " analyzed");
1870 if (node->in_other_partition)
1871 fprintf (f, " in_other_partition");
1873 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1874 (HOST_WIDEST_INT)node->count);
1875 if (node->local.inline_summary.self_time)
1876 fprintf (f, " %i time, %i benefit", node->local.inline_summary.self_time,
1877 node->local.inline_summary.time_inlining_benefit);
1878 if (node->global.time && node->global.time
1879 != node->local.inline_summary.self_time)
1880 fprintf (f, " (%i after inlining)", node->global.time);
1881 if (node->local.inline_summary.self_size)
1882 fprintf (f, " %i size, %i benefit", node->local.inline_summary.self_size,
1883 node->local.inline_summary.size_inlining_benefit);
1884 if (node->global.size && node->global.size
1885 != node->local.inline_summary.self_size)
1886 fprintf (f, " (%i after inlining)", node->global.size);
1887 if (node->local.inline_summary.estimated_self_stack_size)
1888 fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1889 if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
1890 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
1892 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1894 fprintf (f, " needed");
1895 if (node->address_taken)
1896 fprintf (f, " address_taken");
1897 else if (node->reachable)
1898 fprintf (f, " reachable");
1899 else if (node->reachable_from_other_partition)
1900 fprintf (f, " reachable_from_other_partition");
1901 if (gimple_has_body_p (node->decl))
1902 fprintf (f, " body");
1904 fprintf (f, " process");
1905 if (node->local.local)
1906 fprintf (f, " local");
1907 if (node->local.externally_visible)
1908 fprintf (f, " externally_visible");
1909 if (node->resolution != LDPR_UNKNOWN)
1911 ld_plugin_symbol_resolution_names[(int)node->resolution]);
1912 if (node->local.finalized)
1913 fprintf (f, " finalized");
1914 if (node->local.disregard_inline_limits)
1915 fprintf (f, " always_inline");
1916 else if (node->local.inlinable)
1917 fprintf (f, " inlinable");
1918 else if (node->local.versionable)
1919 fprintf (f, " versionable");
1920 if (node->local.redefined_extern_inline)
1921 fprintf (f, " redefined_extern_inline");
1922 if (TREE_ASM_WRITTEN (node->decl))
1923 fprintf (f, " asm_written");
1924 if (node->only_called_at_startup)
1925 fprintf (f, " only_called_at_startup");
1926 if (node->only_called_at_exit)
1927 fprintf (f, " only_called_at_exit");
1929 fprintf (f, "\n called by: ");
1930 for (edge = node->callers; edge; edge = edge->next_caller)
1932 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1935 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1936 (HOST_WIDEST_INT)edge->count);
1937 if (edge->frequency)
1938 fprintf (f, "(%.2f per call) ",
1939 edge->frequency / (double)CGRAPH_FREQ_BASE);
1940 if (!edge->inline_failed)
1941 fprintf(f, "(inlined) ");
1942 if (edge->indirect_inlining_edge)
1943 fprintf(f, "(indirect_inlining) ");
1944 if (edge->can_throw_external)
1945 fprintf(f, "(can throw external) ");
1948 fprintf (f, "\n calls: ");
1949 for (edge = node->callees; edge; edge = edge->next_callee)
1951 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1953 if (!edge->inline_failed)
1954 fprintf(f, "(inlined) ");
1955 if (edge->indirect_inlining_edge)
1956 fprintf(f, "(indirect_inlining) ");
1958 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1959 (HOST_WIDEST_INT)edge->count);
1960 if (edge->frequency)
1961 fprintf (f, "(%.2f per call) ",
1962 edge->frequency / (double)CGRAPH_FREQ_BASE);
1963 if (edge->loop_nest)
1964 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1965 if (edge->can_throw_external)
1966 fprintf(f, "(can throw external) ");
1969 fprintf (f, " References: ");
1970 ipa_dump_references (f, &node->ref_list);
1971 fprintf (f, " Refering this function: ");
1972 ipa_dump_refering (f, &node->ref_list);
1974 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1975 indirect_calls_count++;
1976 if (indirect_calls_count)
1977 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1978 indirect_calls_count);
1980 if (node->same_body)
1982 struct cgraph_node *n;
1983 fprintf (f, " aliases & thunks:");
1984 for (n = node->same_body; n; n = n->next)
1986 fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1987 if (n->thunk.thunk_p)
1989 fprintf (f, " (thunk of %s fixed offset %i virtual value %i has "
1990 "virtual offset %i",
1991 lang_hooks.decl_printable_name (n->thunk.alias, 2),
1992 (int)n->thunk.fixed_offset,
1993 (int)n->thunk.virtual_value,
1994 (int)n->thunk.virtual_offset_p);
1997 if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1998 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
2005 /* Dump call graph node NODE to stderr. */
2008 debug_cgraph_node (struct cgraph_node *node)
2010 dump_cgraph_node (stderr, node);
2014 /* Dump the callgraph to file F. */
2017 dump_cgraph (FILE *f)
2019 struct cgraph_node *node;
2021 fprintf (f, "callgraph:\n\n");
2022 for (node = cgraph_nodes; node; node = node->next)
2023 dump_cgraph_node (f, node);
2027 /* Dump the call graph to stderr. */
2032 dump_cgraph (stderr);
2036 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
2039 change_decl_assembler_name (tree decl, tree name)
2041 struct cgraph_node *node;
2043 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
2044 SET_DECL_ASSEMBLER_NAME (decl, name);
2047 if (name == DECL_ASSEMBLER_NAME (decl))
2050 if (assembler_name_hash
2051 && TREE_CODE (decl) == FUNCTION_DECL
2052 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2054 tree old_name = DECL_ASSEMBLER_NAME (decl);
2055 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2056 decl_assembler_name_hash (old_name),
2058 /* Inline clones are not hashed. */
2059 if (slot && *slot == node)
2060 htab_clear_slot (assembler_name_hash, slot);
2062 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2063 && DECL_RTL_SET_P (decl))
2064 warning (0, "%D renamed after being referenced in assembly", decl);
2066 SET_DECL_ASSEMBLER_NAME (decl, name);
2068 if (assembler_name_hash
2069 && TREE_CODE (decl) == FUNCTION_DECL
2070 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2072 slot = htab_find_slot_with_hash (assembler_name_hash, name,
2073 decl_assembler_name_hash (name),
2075 gcc_assert (!*slot);
2080 /* Add a top-level asm statement to the list. */
2082 struct cgraph_asm_node *
2083 cgraph_add_asm_node (tree asm_str)
2085 struct cgraph_asm_node *node;
2087 node = ggc_alloc_cleared_cgraph_asm_node ();
2088 node->asm_str = asm_str;
2089 node->order = cgraph_order++;
2091 if (cgraph_asm_nodes == NULL)
2092 cgraph_asm_nodes = node;
2094 cgraph_asm_last_node->next = node;
2095 cgraph_asm_last_node = node;
2099 /* Return true when the DECL can possibly be inlined. */
2101 cgraph_function_possibly_inlined_p (tree decl)
2103 if (!cgraph_global_info_ready)
2104 return !DECL_UNINLINABLE (decl);
2105 return DECL_POSSIBLY_INLINED (decl);
2108 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2109 struct cgraph_edge *
2110 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2111 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2112 int freq_scale, int loop_nest, bool update_original)
2114 struct cgraph_edge *new_edge;
2115 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2118 /* We do not want to ignore loop nest after frequency drops to 0. */
2121 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2122 if (freq > CGRAPH_FREQ_MAX)
2123 freq = CGRAPH_FREQ_MAX;
2125 if (e->indirect_unknown_callee)
2129 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2131 struct cgraph_node *callee = cgraph_node (decl);
2132 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq,
2133 e->loop_nest + loop_nest);
2137 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2138 e->indirect_info->ecf_flags,
2140 e->loop_nest + loop_nest);
2141 *new_edge->indirect_info = *e->indirect_info;
2146 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
2147 e->loop_nest + loop_nest);
2148 if (e->indirect_info)
2150 new_edge->indirect_info
2151 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2152 *new_edge->indirect_info = *e->indirect_info;
2156 new_edge->inline_failed = e->inline_failed;
2157 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2158 new_edge->lto_stmt_uid = stmt_uid;
2159 /* Clone flags that depend on call_stmt availability manually. */
2160 new_edge->can_throw_external = e->can_throw_external;
2161 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2162 if (update_original)
2164 e->count -= new_edge->count;
2168 cgraph_call_edge_duplication_hooks (e, new_edge);
2172 /* Create node representing clone of N executed COUNT times. Decrease
2173 the execution counts from original node too.
2174 The new clone will have decl set to DECL that may or may not be the same
2177 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2178 function's profile to reflect the fact that part of execution is handled
2180 struct cgraph_node *
2181 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2182 int loop_nest, bool update_original,
2183 VEC(cgraph_edge_p,heap) *redirect_callers)
2185 struct cgraph_node *new_node = cgraph_create_node ();
2186 struct cgraph_edge *e;
2187 gcov_type count_scale;
2190 new_node->decl = decl;
2191 new_node->origin = n->origin;
2192 if (new_node->origin)
2194 new_node->next_nested = new_node->origin->nested;
2195 new_node->origin->nested = new_node;
2197 new_node->analyzed = n->analyzed;
2198 new_node->local = n->local;
2199 new_node->local.externally_visible = false;
2200 new_node->local.local = true;
2201 new_node->local.vtable_method = false;
2202 new_node->global = n->global;
2203 new_node->rtl = n->rtl;
2204 new_node->count = count;
2205 new_node->frequency = n->frequency;
2206 new_node->clone = n->clone;
2207 new_node->clone.tree_map = 0;
2210 if (new_node->count > n->count)
2211 count_scale = REG_BR_PROB_BASE;
2213 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2217 if (update_original)
2224 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2226 /* Redirect calls to the old version node to point to its new
2228 cgraph_redirect_edge_callee (e, new_node);
2232 for (e = n->callees;e; e=e->next_callee)
2233 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2234 count_scale, freq, loop_nest, update_original);
2236 for (e = n->indirect_calls; e; e = e->next_callee)
2237 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2238 count_scale, freq, loop_nest, update_original);
2239 ipa_clone_references (new_node, NULL, &n->ref_list);
2241 new_node->next_sibling_clone = n->clones;
2243 n->clones->prev_sibling_clone = new_node;
2244 n->clones = new_node;
2245 new_node->clone_of = n;
2247 cgraph_call_node_duplication_hooks (n, new_node);
2248 if (n->decl != decl)
2250 struct cgraph_node **slot;
2251 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2252 gcc_assert (!*slot);
2254 if (assembler_name_hash)
2257 tree name = DECL_ASSEMBLER_NAME (decl);
2259 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2260 decl_assembler_name_hash (name),
2262 gcc_assert (!*aslot);
2269 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2271 static GTY(()) unsigned int clone_fn_id_num;
2274 clone_function_name (tree decl, const char *suffix)
2276 tree name = DECL_ASSEMBLER_NAME (decl);
2277 size_t len = IDENTIFIER_LENGTH (name);
2278 char *tmp_name, *prefix;
2280 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2281 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2282 strcpy (prefix + len + 1, suffix);
2283 #ifndef NO_DOT_IN_LABEL
2285 #elif !defined NO_DOLLAR_IN_LABEL
2290 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2291 return get_identifier (tmp_name);
2294 /* Create callgraph node clone with new declaration. The actual body will
2295 be copied later at compilation stage.
2297 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2300 struct cgraph_node *
2301 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2302 VEC(cgraph_edge_p,heap) *redirect_callers,
2303 VEC(ipa_replace_map_p,gc) *tree_map,
2304 bitmap args_to_skip,
2305 const char * suffix)
2307 tree old_decl = old_node->decl;
2308 struct cgraph_node *new_node = NULL;
2311 struct ipa_replace_map *map;
2314 gcc_checking_assert (tree_versionable_function_p (old_decl));
2316 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2318 /* Make a new FUNCTION_DECL tree node */
2320 new_decl = copy_node (old_decl);
2322 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2323 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2325 /* Generate a new name for the new version. */
2326 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2327 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2328 SET_DECL_RTL (new_decl, NULL);
2330 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2331 CGRAPH_FREQ_BASE, 0, false,
2333 /* Update the properties.
2334 Make clone visible only within this translation unit. Make sure
2335 that is not weak also.
2336 ??? We cannot use COMDAT linkage because there is no
2337 ABI support for this. */
2338 DECL_EXTERNAL (new_node->decl) = 0;
2339 if (DECL_ONE_ONLY (old_decl))
2340 DECL_SECTION_NAME (new_node->decl) = NULL;
2341 DECL_COMDAT_GROUP (new_node->decl) = 0;
2342 TREE_PUBLIC (new_node->decl) = 0;
2343 DECL_COMDAT (new_node->decl) = 0;
2344 DECL_WEAK (new_node->decl) = 0;
2345 DECL_VIRTUAL_P (new_node->decl) = 0;
2346 new_node->clone.tree_map = tree_map;
2347 new_node->clone.args_to_skip = args_to_skip;
2348 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2350 tree var = map->new_tree;
2353 if (TREE_CODE (var) != ADDR_EXPR)
2355 var = get_base_var (var);
2359 /* Record references of the future statement initializing the constant
2361 if (TREE_CODE (var) == FUNCTION_DECL)
2362 ipa_record_reference (new_node, NULL, cgraph_node (var),
2363 NULL, IPA_REF_ADDR, NULL);
2364 else if (TREE_CODE (var) == VAR_DECL)
2365 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2366 IPA_REF_ADDR, NULL);
2369 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2370 else if (old_node->clone.combined_args_to_skip)
2372 int newi = 0, oldi = 0;
2374 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2375 struct cgraph_node *orig_node;
2376 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2378 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2380 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2382 bitmap_set_bit (new_args_to_skip, oldi);
2385 if (bitmap_bit_p (args_to_skip, newi))
2386 bitmap_set_bit (new_args_to_skip, oldi);
2389 new_node->clone.combined_args_to_skip = new_args_to_skip;
2392 new_node->clone.combined_args_to_skip = args_to_skip;
2393 new_node->local.externally_visible = 0;
2394 new_node->local.local = 1;
2395 new_node->lowered = true;
2396 new_node->reachable = true;
2402 /* NODE is no longer nested function; update cgraph accordingly. */
2404 cgraph_unnest_node (struct cgraph_node *node)
2406 struct cgraph_node **node2 = &node->origin->nested;
2407 gcc_assert (node->origin);
2409 while (*node2 != node)
2410 node2 = &(*node2)->next_nested;
2411 *node2 = node->next_nested;
2412 node->origin = NULL;
2415 /* Return function availability. See cgraph.h for description of individual
2418 cgraph_function_body_availability (struct cgraph_node *node)
2420 enum availability avail;
2421 gcc_assert (cgraph_function_flags_ready);
2422 if (!node->analyzed)
2423 avail = AVAIL_NOT_AVAILABLE;
2424 else if (node->local.local)
2425 avail = AVAIL_LOCAL;
2426 else if (!node->local.externally_visible)
2427 avail = AVAIL_AVAILABLE;
2428 /* Inline functions are safe to be analyzed even if their symbol can
2429 be overwritten at runtime. It is not meaningful to enforce any sane
2430 behaviour on replacing inline function by different body. */
2431 else if (DECL_DECLARED_INLINE_P (node->decl))
2432 avail = AVAIL_AVAILABLE;
2434 /* If the function can be overwritten, return OVERWRITABLE. Take
2435 care at least of two notable extensions - the COMDAT functions
2436 used to share template instantiations in C++ (this is symmetric
2437 to code cp_cannot_inline_tree_fn and probably shall be shared and
2438 the inlinability hooks completely eliminated).
2440 ??? Does the C++ one definition rule allow us to always return
2441 AVAIL_AVAILABLE here? That would be good reason to preserve this
2444 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2445 avail = AVAIL_OVERWRITABLE;
2446 else avail = AVAIL_AVAILABLE;
2451 /* Add the function FNDECL to the call graph.
2452 Unlike cgraph_finalize_function, this function is intended to be used
2453 by middle end and allows insertion of new function at arbitrary point
2454 of compilation. The function can be either in high, low or SSA form
2457 The function is assumed to be reachable and have address taken (so no
2458 API breaking optimizations are performed on it).
2460 Main work done by this function is to enqueue the function for later
2461 processing to avoid need the passes to be re-entrant. */
2464 cgraph_add_new_function (tree fndecl, bool lowered)
2466 struct cgraph_node *node;
2467 switch (cgraph_state)
2469 case CGRAPH_STATE_CONSTRUCTION:
2470 /* Just enqueue function to be processed at nearest occurrence. */
2471 node = cgraph_node (fndecl);
2472 node->next_needed = cgraph_new_nodes;
2474 node->lowered = true;
2475 cgraph_new_nodes = node;
2478 case CGRAPH_STATE_IPA:
2479 case CGRAPH_STATE_IPA_SSA:
2480 case CGRAPH_STATE_EXPANSION:
2481 /* Bring the function into finalized state and enqueue for later
2482 analyzing and compilation. */
2483 node = cgraph_node (fndecl);
2484 node->local.local = false;
2485 node->local.finalized = true;
2486 node->reachable = node->needed = true;
2487 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2489 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2490 current_function_decl = fndecl;
2491 gimple_register_cfg_hooks ();
2492 tree_lowering_passes (fndecl);
2493 bitmap_obstack_initialize (NULL);
2494 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2495 execute_pass_list (pass_early_local_passes.pass.sub);
2496 bitmap_obstack_release (NULL);
2498 current_function_decl = NULL;
2503 node->lowered = true;
2504 node->next_needed = cgraph_new_nodes;
2505 cgraph_new_nodes = node;
2508 case CGRAPH_STATE_FINISHED:
2509 /* At the very end of compilation we have to do all the work up
2511 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2512 current_function_decl = fndecl;
2513 gimple_register_cfg_hooks ();
2515 tree_lowering_passes (fndecl);
2516 bitmap_obstack_initialize (NULL);
2517 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2518 execute_pass_list (pass_early_local_passes.pass.sub);
2519 bitmap_obstack_release (NULL);
2520 tree_rest_of_compilation (fndecl);
2522 current_function_decl = NULL;
2526 /* Set a personality if required and we already passed EH lowering. */
2528 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2529 == eh_personality_lang))
2530 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2533 /* Return true if NODE can be made local for API change.
2534 Extern inline functions and C++ COMDAT functions can be made local
2535 at the expense of possible code size growth if function is used in multiple
2536 compilation units. */
2538 cgraph_node_can_be_local_p (struct cgraph_node *node)
2540 return (!node->needed && !node->address_taken
2541 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2542 || !node->local.externally_visible));
2545 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2546 but other code such as notice_global_symbol generates rtl. */
2548 cgraph_make_decl_local (tree decl)
2552 if (TREE_CODE (decl) == VAR_DECL)
2553 DECL_COMMON (decl) = 0;
2554 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2556 if (DECL_COMDAT (decl))
2558 /* It is possible that we are linking against library defining same COMDAT
2559 function. To avoid conflict we need to rename our local name of the
2560 function just in the case WHOPR partitioning decide to make it hidden
2561 to avoid cross partition references. */
2564 const char *old_name;
2566 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2567 if (TREE_CODE (decl) == FUNCTION_DECL)
2569 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2570 change_decl_assembler_name (decl,
2571 clone_function_name (decl, "local"));
2572 if (node->local.lto_file_data)
2573 lto_record_renamed_decl (node->local.lto_file_data,
2576 (DECL_ASSEMBLER_NAME (decl)));
2578 else if (TREE_CODE (decl) == VAR_DECL)
2580 struct varpool_node *vnode = varpool_get_node (decl);
2581 /* change_decl_assembler_name will warn here on vtables because
2582 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2583 SET_DECL_ASSEMBLER_NAME (decl,
2584 clone_function_name (decl, "local"));
2585 if (vnode->lto_file_data)
2586 lto_record_renamed_decl (vnode->lto_file_data,
2589 (DECL_ASSEMBLER_NAME (decl)));
2592 DECL_SECTION_NAME (decl) = 0;
2593 DECL_COMDAT (decl) = 0;
2595 DECL_COMDAT_GROUP (decl) = 0;
2596 DECL_WEAK (decl) = 0;
2597 DECL_EXTERNAL (decl) = 0;
2598 TREE_PUBLIC (decl) = 0;
2599 if (!DECL_RTL_SET_P (decl))
2602 /* Update rtl flags. */
2603 make_decl_rtl (decl);
2605 rtl = DECL_RTL (decl);
2609 symbol = XEXP (rtl, 0);
2610 if (GET_CODE (symbol) != SYMBOL_REF)
2613 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2616 /* Bring NODE local. */
2618 cgraph_make_node_local (struct cgraph_node *node)
2620 gcc_assert (cgraph_node_can_be_local_p (node));
2621 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2623 struct cgraph_node *alias;
2624 cgraph_make_decl_local (node->decl);
2626 for (alias = node->same_body; alias; alias = alias->next)
2627 cgraph_make_decl_local (alias->decl);
2629 node->local.externally_visible = false;
2630 node->local.local = true;
2631 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2632 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2636 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2637 if any to NOTHROW. */
2640 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2642 struct cgraph_node *alias;
2643 TREE_NOTHROW (node->decl) = nothrow;
2644 for (alias = node->same_body; alias; alias = alias->next)
2645 TREE_NOTHROW (alias->decl) = nothrow;
2648 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2649 if any to READONLY. */
2652 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2654 struct cgraph_node *alias;
2655 /* Static constructors and destructors without a side effect can be
2657 if (!looping && readonly)
2659 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2660 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2661 if (DECL_STATIC_DESTRUCTOR (node->decl))
2662 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2664 TREE_READONLY (node->decl) = readonly;
2665 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2666 for (alias = node->same_body; alias; alias = alias->next)
2668 TREE_READONLY (alias->decl) = readonly;
2669 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2673 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2677 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2679 struct cgraph_node *alias;
2680 /* Static constructors and destructors without a side effect can be
2682 if (!looping && pure)
2684 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2685 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2686 if (DECL_STATIC_DESTRUCTOR (node->decl))
2687 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2689 DECL_PURE_P (node->decl) = pure;
2690 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2691 for (alias = node->same_body; alias; alias = alias->next)
2693 DECL_PURE_P (alias->decl) = pure;
2694 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2698 /* See if the frequency of NODE can be updated based on frequencies of its
2701 cgraph_propagate_frequency (struct cgraph_node *node)
2703 bool maybe_unlikely_executed = true, maybe_executed_once = true;
2704 bool only_called_at_startup = true;
2705 bool only_called_at_exit = true;
2706 bool changed = false;
2707 struct cgraph_edge *edge;
2709 if (!node->local.local)
2711 gcc_assert (node->analyzed);
2712 if (dump_file && (dump_flags & TDF_DETAILS))
2713 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2715 for (edge = node->callers;
2716 edge && (maybe_unlikely_executed || maybe_executed_once
2717 || only_called_at_startup || only_called_at_exit);
2718 edge = edge->next_caller)
2720 if (edge->caller != node)
2722 only_called_at_startup &= edge->caller->only_called_at_startup;
2723 /* It makes sense to put main() together with the static constructors.
2724 It will be executed for sure, but rest of functions called from
2725 main are definitely not at startup only. */
2726 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2727 only_called_at_startup = 0;
2728 only_called_at_exit &= edge->caller->only_called_at_exit;
2730 if (!edge->frequency)
2732 switch (edge->caller->frequency)
2734 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2736 case NODE_FREQUENCY_EXECUTED_ONCE:
2737 if (dump_file && (dump_flags & TDF_DETAILS))
2738 fprintf (dump_file, " Called by %s that is executed once\n",
2739 cgraph_node_name (edge->caller));
2740 maybe_unlikely_executed = false;
2741 if (edge->loop_nest)
2743 maybe_executed_once = false;
2744 if (dump_file && (dump_flags & TDF_DETAILS))
2745 fprintf (dump_file, " Called in loop\n");
2748 case NODE_FREQUENCY_HOT:
2749 case NODE_FREQUENCY_NORMAL:
2750 if (dump_file && (dump_flags & TDF_DETAILS))
2751 fprintf (dump_file, " Called by %s that is normal or hot\n",
2752 cgraph_node_name (edge->caller));
2753 maybe_unlikely_executed = false;
2754 maybe_executed_once = false;
2758 if ((only_called_at_startup && !only_called_at_exit)
2759 && !node->only_called_at_startup)
2761 node->only_called_at_startup = true;
2763 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2764 cgraph_node_name (node));
2767 if ((only_called_at_exit && !only_called_at_startup)
2768 && !node->only_called_at_exit)
2770 node->only_called_at_exit = true;
2772 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2773 cgraph_node_name (node));
2776 /* These come either from profile or user hints; never update them. */
2777 if (node->frequency == NODE_FREQUENCY_HOT
2778 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2780 if (maybe_unlikely_executed)
2782 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2784 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2785 cgraph_node_name (node));
2788 else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2790 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2792 fprintf (dump_file, "Node %s promoted to executed once.\n",
2793 cgraph_node_name (node));
2799 /* Return true when NODE can not return or throw and thus
2800 it is safe to ignore its side effects for IPA analysis. */
2803 cgraph_node_cannot_return (struct cgraph_node *node)
2805 int flags = flags_from_decl_or_type (node->decl);
2806 if (!flag_exceptions)
2807 return (flags & ECF_NORETURN) != 0;
2809 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2810 == (ECF_NORETURN | ECF_NOTHROW));
2813 /* Return true when call of E can not lead to return from caller
2814 and thus it is safe to ignore its side effects for IPA analysis
2815 when computing side effects of the caller.
2816 FIXME: We could actually mark all edges that have no reaching
2817 patch to EXIT_BLOCK_PTR or throw to get better results. */
2819 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2821 if (cgraph_node_cannot_return (e->caller))
2823 if (e->indirect_unknown_callee)
2825 int flags = e->indirect_info->ecf_flags;
2826 if (!flag_exceptions)
2827 return (flags & ECF_NORETURN) != 0;
2829 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2830 == (ECF_NORETURN | ECF_NOTHROW));
2833 return cgraph_node_cannot_return (e->callee);
2836 /* Return true when function NODE can be removed from callgraph
2837 if all direct calls are eliminated. */
2840 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2842 gcc_assert (!node->global.inlined_to);
2843 /* Extern inlines can always go, we will use the external definition. */
2844 if (DECL_EXTERNAL (node->decl))
2846 /* When function is needed, we can not remove it. */
2847 if (node->needed || node->reachable_from_other_partition)
2849 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2850 || DECL_STATIC_DESTRUCTOR (node->decl))
2852 /* Only COMDAT functions can be removed if externally visible. */
2853 if (node->local.externally_visible
2854 && (!DECL_COMDAT (node->decl)
2855 || cgraph_used_from_object_file_p (node)))
2860 /* Return true when function NODE can be expected to be removed
2861 from program when direct calls in this compilation unit are removed.
2863 As a special case COMDAT functions are
2864 cgraph_can_remove_if_no_direct_calls_p while the are not
2865 cgraph_only_called_directly_p (it is possible they are called from other
2868 This function behaves as cgraph_only_called_directly_p because eliminating
2869 all uses of COMDAT function does not make it necessarily disappear from
2870 the program unless we are compiling whole program or we do LTO. In this
2871 case we know we win since dynamic linking will not really discard the
2872 linkonce section. */
2875 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2877 gcc_assert (!node->global.inlined_to);
2878 if (cgraph_used_from_object_file_p (node))
2880 if (!in_lto_p && !flag_whole_program)
2881 return cgraph_only_called_directly_p (node);
2884 if (DECL_EXTERNAL (node->decl))
2886 return cgraph_can_remove_if_no_direct_calls_p (node);
2890 /* Return true when RESOLUTION indicate that linker will use
2891 the symbol from non-LTO object files. */
2894 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2896 return (resolution == LDPR_PREVAILING_DEF
2897 || resolution == LDPR_PREEMPTED_REG
2898 || resolution == LDPR_RESOLVED_EXEC
2899 || resolution == LDPR_RESOLVED_DYN);
2902 /* Return true when NODE is known to be used from other (non-LTO) object file.
2903 Known only when doing LTO via linker plugin. */
2906 cgraph_used_from_object_file_p (struct cgraph_node *node)
2908 struct cgraph_node *alias;
2910 gcc_assert (!node->global.inlined_to);
2911 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2913 if (resolution_used_from_other_file_p (node->resolution))
2915 for (alias = node->same_body; alias; alias = alias->next)
2916 if (TREE_PUBLIC (alias->decl)
2917 && resolution_used_from_other_file_p (alias->resolution))
2922 #include "gt-cgraph.h"