PR c++/35315
[official-gcc.git] / gcc / cgraph.c
blob64d278f94c1cf5052be7d457fdcb7f3292c55583
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
11 version.
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
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains basic routines manipulating call graph
24 The callgraph:
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
28 sharing.
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
40 to the callgraph.
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
46 added there.
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.
60 Inlining plans:
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
69 caller.
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. */
75 #include "config.h"
76 #include "system.h"
77 #include "coretypes.h"
78 #include "tm.h"
79 #include "tree.h"
80 #include "tree-inline.h"
81 #include "langhooks.h"
82 #include "hashtab.h"
83 #include "toplev.h"
84 #include "flags.h"
85 #include "ggc.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "basic-block.h"
89 #include "cgraph.h"
90 #include "output.h"
91 #include "intl.h"
92 #include "gimple.h"
93 #include "tree-dump.h"
94 #include "tree-flow.h"
95 #include "value-prof.h"
96 #include "except.h"
97 #include "diagnostic-core.h"
98 #include "rtl.h"
99 #include "ipa-utils.h"
100 #include "lto-streamer.h"
102 const char * const ld_plugin_symbol_resolution_names[]=
105 "undef",
106 "prevailing_def",
107 "prevailing_def_ironly",
108 "preempted_reg",
109 "preempted_ir",
110 "resolved_ir",
111 "resolved_exec",
112 "resolved_dyn"
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. */
136 int cgraph_n_nodes;
138 /* Maximal uid used in cgraph nodes. */
139 int cgraph_max_uid;
141 /* Maximal uid used in cgraph edges. */
142 int cgraph_edge_max_uid;
144 /* Maximal pid used for profiling */
145 int cgraph_max_pid;
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. */
165 int cgraph_order;
167 /* List of hooks triggered on cgraph_edge events. */
168 struct cgraph_edge_hook_list {
169 cgraph_edge_hook hook;
170 void *data;
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;
177 void *data;
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;
184 void *data;
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;
191 void *data;
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
214 edges. */
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));
226 entry->hook = hook;
227 entry->data = data;
228 entry->next = NULL;
229 while (*ptr)
230 ptr = &(*ptr)->next;
231 *ptr = entry;
232 return entry;
235 /* Remove ENTRY from the list of hooks called on removing edges. */
236 void
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)
242 ptr = &(*ptr)->next;
243 *ptr = entry->next;
244 free (entry);
247 /* Call all edge removal hooks. */
248 static void
249 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
251 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
252 while (entry)
254 entry->hook (e, entry->data);
255 entry = entry->next;
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));
267 entry->hook = hook;
268 entry->data = data;
269 entry->next = NULL;
270 while (*ptr)
271 ptr = &(*ptr)->next;
272 *ptr = entry;
273 return entry;
276 /* Remove ENTRY from the list of hooks called on removing nodes. */
277 void
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)
283 ptr = &(*ptr)->next;
284 *ptr = entry->next;
285 free (entry);
288 /* Call all node removal hooks. */
289 static void
290 cgraph_call_node_removal_hooks (struct cgraph_node *node)
292 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
293 while (entry)
295 entry->hook (node, entry->data);
296 entry = entry->next;
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));
308 entry->hook = hook;
309 entry->data = data;
310 entry->next = NULL;
311 while (*ptr)
312 ptr = &(*ptr)->next;
313 *ptr = entry;
314 return entry;
317 /* Remove ENTRY from the list of hooks called on inserted nodes. */
318 void
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)
324 ptr = &(*ptr)->next;
325 *ptr = entry->next;
326 free (entry);
329 /* Call all node insertion hooks. */
330 void
331 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
333 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
334 while (entry)
336 entry->hook (node, entry->data);
337 entry = entry->next;
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));
349 entry->hook = hook;
350 entry->data = data;
351 entry->next = NULL;
352 while (*ptr)
353 ptr = &(*ptr)->next;
354 *ptr = entry;
355 return entry;
358 /* Remove ENTRY from the list of hooks called on duplicating edges. */
359 void
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)
365 ptr = &(*ptr)->next;
366 *ptr = entry->next;
367 free (entry);
370 /* Call all edge duplication hooks. */
371 static void
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;
376 while (entry)
378 entry->hook (cs1, cs2, entry->data);
379 entry = entry->next;
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));
391 entry->hook = hook;
392 entry->data = data;
393 entry->next = NULL;
394 while (*ptr)
395 ptr = &(*ptr)->next;
396 *ptr = entry;
397 return entry;
400 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
401 void
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)
407 ptr = &(*ptr)->next;
408 *ptr = entry->next;
409 free (entry);
412 /* Call all node duplication hooks. */
413 static void
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;
418 while (entry)
420 entry->hook (node1, node2, entry->data);
421 entry = entry->next;
425 /* Returns a hash code for P. */
427 static hashval_t
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. */
437 static int
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;
452 if (free_nodes)
454 node = free_nodes;
455 free_nodes = NEXT_FREE_NODE (node);
457 else
459 node = ggc_alloc_cleared_cgraph_node ();
460 node->uid = cgraph_max_uid++;
463 return node;
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;
474 node->pid = -1;
475 node->order = cgraph_order++;
476 if (cgraph_nodes)
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);
483 cgraph_nodes = node;
484 cgraph_n_nodes++;
485 return node;
488 /* Return cgraph node assigned to DECL. Create new one when needed. */
490 struct cgraph_node *
491 cgraph_node (tree decl)
493 struct cgraph_node key, *node, **slot;
495 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
497 if (!cgraph_hash)
498 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
500 key.decl = decl;
502 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
504 if (*slot)
506 node = *slot;
507 if (node->same_body_alias)
508 node = node->same_body;
509 return node;
512 node = cgraph_create_node ();
513 node->decl = decl;
514 *slot = 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)
523 void **aslot;
524 tree name = DECL_ASSEMBLER_NAME (decl);
526 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
527 decl_assembler_name_hash (name),
528 INSERT);
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. */
533 if (*aslot == NULL)
534 *aslot = node;
536 return node;
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);
550 key.decl = alias;
552 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
554 /* If the cgraph_node has been already created, fail. */
555 if (*slot)
556 return NULL;
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;
568 *slot = alias_node;
569 return alias_node;
572 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
573 and NULL otherwise.
574 Same body aliases are output whenever the body of DECL is output,
575 and cgraph_node (ALIAS) transparently returns cgraph_node (DECL). */
577 struct cgraph_node *
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. */
582 return NULL;
583 #endif
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. */
594 struct cgraph_node *
595 cgraph_add_thunk (struct cgraph_node *decl_node, tree alias, tree decl,
596 bool this_adjusting,
597 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
598 tree virtual_offset,
599 tree real_alias)
601 struct cgraph_node *node = cgraph_get_node (alias);
603 if (node)
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);
611 gcc_assert (node);
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;
621 return node;
624 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
625 is assigned. */
627 struct 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);
634 if (!cgraph_hash)
635 return NULL;
637 key.decl = CONST_CAST2 (tree, const_tree, decl);
639 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
640 NO_INSERT);
642 if (slot && *slot)
643 node = *slot;
644 return node;
647 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
648 is assigned. */
650 struct 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);
657 if (!cgraph_hash)
658 return NULL;
660 key.decl = CONST_CAST2 (tree, const_tree, decl);
662 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
663 NO_INSERT);
665 if (slot && *slot)
667 node = *slot;
668 if (node->same_body_alias)
669 node = node->same_body;
671 return node;
674 /* Insert already constructed node into hashtable. */
676 void
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);
683 gcc_assert (!*slot);
684 *slot = node;
687 /* Returns a hash code for P. */
689 static hashval_t
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. */
698 static int
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. */
709 struct cgraph_node *
710 cgraph_node_for_asm (tree asmname)
712 struct cgraph_node *node;
713 void **slot;
715 if (!assembler_name_hash)
717 assembler_name_hash =
718 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
719 NULL);
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),
726 INSERT);
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. */
731 if (!*slot)
732 *slot = node;
733 if (node->same_body)
735 struct cgraph_node *alias;
737 for (alias = node->same_body; alias; alias = alias->next)
739 hashval_t hash;
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,
743 hash, INSERT);
744 if (!*slot)
745 *slot = alias;
751 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
752 decl_assembler_name_hash (asmname),
753 NO_INSERT);
755 if (slot)
757 node = (struct cgraph_node *) *slot;
758 if (node->same_body_alias)
759 node = node->same_body;
760 return node;
762 return NULL;
765 /* Returns a hash value for X (which really is a die_struct). */
767 static hashval_t
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. */
775 static int
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. */
783 static inline void
784 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
786 void **slot;
787 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
788 e->call_stmt,
789 htab_hash_pointer (e->call_stmt),
790 INSERT);
791 gcc_assert (!*slot);
792 *slot = e;
795 /* Return the callgraph edge representing the GIMPLE_CALL statement
796 CALL_STMT. */
798 struct cgraph_edge *
799 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
801 struct cgraph_edge *e, *e2;
802 int n = 0;
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)
817 break;
818 n++;
821 if (!e)
822 for (e = node->indirect_calls; e; e = e->next_callee)
824 if (e->call_stmt == call_stmt)
825 break;
826 n++;
829 if (n > 100)
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);
838 return e;
842 /* Change field call_stmt of edge E to NEW_STMT. */
844 void
845 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
847 tree decl;
849 if (e->caller->call_site_hash)
851 htab_remove_elt_with_hash (e->caller->call_site_hash,
852 e->call_stmt,
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);
869 pop_cfun ();
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. */
877 void
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);
884 if (edge)
885 cgraph_set_call_stmt (edge, new_stmt);
887 node = orig->clones;
888 if (node)
889 while (node != orig)
891 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
892 if (edge)
893 cgraph_set_call_stmt (edge, new_stmt);
894 if (node->clones)
895 node = node->clones;
896 else if (node->next_sibling_clone)
897 node = node->next_sibling_clone;
898 else
900 while (node != orig && !node->next_sibling_clone)
901 node = node->clone_of;
902 if (node != orig)
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. */
915 void
916 cgraph_create_edge_including_clones (struct cgraph_node *orig,
917 struct cgraph_node *callee,
918 gimple old_stmt,
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;
932 node = orig->clones;
933 if (node)
934 while (node != orig)
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. */
942 if (edge)
943 cgraph_set_call_stmt (edge, stmt);
944 else if (!cgraph_edge (node, stmt))
946 edge = cgraph_create_edge (node, callee, stmt, count,
947 freq, loop_depth);
948 edge->inline_failed = reason;
951 if (node->clones)
952 node = node->clones;
953 else if (node->next_sibling_clone)
954 node = node->next_sibling_clone;
955 else
957 while (node != orig && !node->next_sibling_clone)
958 node = node->clone_of;
959 if (node != orig)
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. */
968 static void
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;
983 else
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
989 edge). */
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. */
999 if (call_stmt)
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));
1008 if (free_edges)
1010 edge = free_edges;
1011 free_edges = NEXT_FREE_EDGE (edge);
1013 else
1015 edge = ggc_alloc_cgraph_edge ();
1016 edge->uid = cgraph_edge_max_uid++;
1019 edge->aux = NULL;
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;
1038 pop_cfun ();
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;
1047 return edge;
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,
1057 count, freq, nest);
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;
1071 return 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;
1083 return ii;
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
1088 PARAM_INDEX. */
1090 struct cgraph_edge *
1091 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1092 int ecf_flags,
1093 gcov_type count, int freq, int nest)
1095 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1096 count, freq, nest);
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;
1109 return edge;
1112 /* Remove the edge E from the list of the callers of the callee. */
1114 static inline void
1115 cgraph_edge_remove_callee (struct cgraph_edge *e)
1117 gcc_assert (!e->indirect_unknown_callee);
1118 if (e->prev_caller)
1119 e->prev_caller->next_caller = e->next_caller;
1120 if (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. */
1128 static inline void
1129 cgraph_edge_remove_caller (struct cgraph_edge *e)
1131 if (e->prev_callee)
1132 e->prev_callee->next_callee = e->next_callee;
1133 if (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;
1139 else
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,
1144 e->call_stmt,
1145 htab_hash_pointer (e->call_stmt));
1148 /* Put the edge onto the free list. */
1150 static void
1151 cgraph_free_edge (struct cgraph_edge *e)
1153 int uid = e->uid;
1155 /* Clear out the edge so we do not dangle pointers. */
1156 memset (e, 0, sizeof (*e));
1157 e->uid = uid;
1158 NEXT_FREE_EDGE (e) = free_edges;
1159 free_edges = e;
1162 /* Remove the edge E in the cgraph. */
1164 void
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
1182 callers. */
1184 static void
1185 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1187 e->prev_caller = NULL;
1188 if (n->callers)
1189 n->callers->prev_caller = e;
1190 e->next_caller = n->callers;
1191 n->callers = e;
1192 e->callee = n;
1195 /* Redirect callee of E to N. The function does not update underlying
1196 call expression. */
1198 void
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. */
1212 void
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. */
1246 static void
1247 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1248 gimple old_stmt, tree old_call, gimple new_stmt)
1250 tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
1252 /* We are seeing indirect calls, then there is nothing to update. */
1253 if (!new_call && !old_call)
1254 return;
1255 /* See if we turned indirect call into direct call or folded call to one builtin
1256 into different builtin. */
1257 if (old_call != new_call)
1259 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1260 struct cgraph_edge *ne = NULL;
1261 gcov_type count;
1262 int frequency;
1263 int loop_nest;
1265 if (e)
1267 /* See if the edge is already there and has the correct callee. It
1268 might be so because of indirect inlining has already updated
1269 it. We also might've cloned and redirected the edge. */
1270 if (new_call && e->callee)
1272 struct cgraph_node *callee = e->callee;
1273 while (callee)
1275 if (callee->decl == new_call
1276 || callee->former_clone_of == new_call)
1277 return;
1278 callee = callee->clone_of;
1282 /* Otherwise remove edge and create new one; we can't simply redirect
1283 since function has changed, so inline plan and other information
1284 attached to edge is invalid. */
1285 count = e->count;
1286 frequency = e->frequency;
1287 loop_nest = e->loop_nest;
1288 cgraph_remove_edge (e);
1290 else
1292 /* We are seeing new direct call; compute profile info based on BB. */
1293 basic_block bb = gimple_bb (new_stmt);
1294 count = bb->count;
1295 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1296 bb);
1297 loop_nest = bb->loop_depth;
1300 if (new_call)
1302 ne = cgraph_create_edge (node, cgraph_node (new_call),
1303 new_stmt, count, frequency,
1304 loop_nest);
1305 gcc_assert (ne->inline_failed);
1308 /* We only updated the call stmt; update pointer in cgraph edge.. */
1309 else if (old_stmt != new_stmt)
1310 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1313 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1314 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1315 of OLD_STMT before it was updated (updating can happen inplace). */
1317 void
1318 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1320 struct cgraph_node *orig = cgraph_node (cfun->decl);
1321 struct cgraph_node *node;
1323 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1324 if (orig->clones)
1325 for (node = orig->clones; node != orig;)
1327 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1328 if (node->clones)
1329 node = node->clones;
1330 else if (node->next_sibling_clone)
1331 node = node->next_sibling_clone;
1332 else
1334 while (node != orig && !node->next_sibling_clone)
1335 node = node->clone_of;
1336 if (node != orig)
1337 node = node->next_sibling_clone;
1343 /* Remove all callees from the node. */
1345 void
1346 cgraph_node_remove_callees (struct cgraph_node *node)
1348 struct cgraph_edge *e, *f;
1350 /* It is sufficient to remove the edges from the lists of callers of
1351 the callees. The callee list of the node can be zapped with one
1352 assignment. */
1353 for (e = node->callees; e; e = f)
1355 f = e->next_callee;
1356 cgraph_call_edge_removal_hooks (e);
1357 if (!e->indirect_unknown_callee)
1358 cgraph_edge_remove_callee (e);
1359 cgraph_free_edge (e);
1361 for (e = node->indirect_calls; e; e = f)
1363 f = e->next_callee;
1364 cgraph_call_edge_removal_hooks (e);
1365 if (!e->indirect_unknown_callee)
1366 cgraph_edge_remove_callee (e);
1367 cgraph_free_edge (e);
1369 node->indirect_calls = NULL;
1370 node->callees = NULL;
1371 if (node->call_site_hash)
1373 htab_delete (node->call_site_hash);
1374 node->call_site_hash = NULL;
1378 /* Remove all callers from the node. */
1380 static void
1381 cgraph_node_remove_callers (struct cgraph_node *node)
1383 struct cgraph_edge *e, *f;
1385 /* It is sufficient to remove the edges from the lists of callees of
1386 the callers. The caller list of the node can be zapped with one
1387 assignment. */
1388 for (e = node->callers; e; e = f)
1390 f = e->next_caller;
1391 cgraph_call_edge_removal_hooks (e);
1392 cgraph_edge_remove_caller (e);
1393 cgraph_free_edge (e);
1395 node->callers = NULL;
1398 /* Release memory used to represent body of function NODE. */
1400 void
1401 cgraph_release_function_body (struct cgraph_node *node)
1403 if (DECL_STRUCT_FUNCTION (node->decl))
1405 tree old_decl = current_function_decl;
1406 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1407 if (cfun->gimple_df)
1409 current_function_decl = node->decl;
1410 delete_tree_ssa ();
1411 delete_tree_cfg_annotations ();
1412 cfun->eh = NULL;
1413 current_function_decl = old_decl;
1415 if (cfun->cfg)
1417 gcc_assert (dom_computed[0] == DOM_NONE);
1418 gcc_assert (dom_computed[1] == DOM_NONE);
1419 clear_edges ();
1421 if (cfun->value_histograms)
1422 free_histograms ();
1423 gcc_assert (!current_loops);
1424 pop_cfun();
1425 gimple_set_body (node->decl, NULL);
1426 VEC_free (ipa_opt_pass, heap,
1427 node->ipa_transforms_to_apply);
1428 /* Struct function hangs a lot of data that would leak if we didn't
1429 removed all pointers to it. */
1430 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1431 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1433 DECL_SAVED_TREE (node->decl) = NULL;
1434 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1435 of its associated function function declaration because it's
1436 needed to emit debug info later. */
1437 if (!node->abstract_and_needed)
1438 DECL_INITIAL (node->decl) = error_mark_node;
1441 /* Remove same body alias node. */
1443 void
1444 cgraph_remove_same_body_alias (struct cgraph_node *node)
1446 void **slot;
1447 int uid = node->uid;
1449 gcc_assert (node->same_body_alias);
1450 if (node->previous)
1451 node->previous->next = node->next;
1452 else
1453 node->same_body->same_body = node->next;
1454 if (node->next)
1455 node->next->previous = node->previous;
1456 node->next = NULL;
1457 node->previous = NULL;
1458 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1459 if (*slot == node)
1460 htab_clear_slot (cgraph_hash, slot);
1461 if (assembler_name_hash)
1463 tree name = DECL_ASSEMBLER_NAME (node->decl);
1464 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1465 decl_assembler_name_hash (name),
1466 NO_INSERT);
1467 if (slot && *slot == node)
1468 htab_clear_slot (assembler_name_hash, slot);
1471 /* Clear out the node to NULL all pointers and add the node to the free
1472 list. */
1473 memset (node, 0, sizeof(*node));
1474 node->uid = uid;
1475 NEXT_FREE_NODE (node) = free_nodes;
1476 free_nodes = node;
1479 /* Remove the node from cgraph. */
1481 void
1482 cgraph_remove_node (struct cgraph_node *node)
1484 void **slot;
1485 bool kill_body = false;
1486 struct cgraph_node *n;
1487 int uid = node->uid;
1489 cgraph_call_node_removal_hooks (node);
1490 cgraph_node_remove_callers (node);
1491 cgraph_node_remove_callees (node);
1492 ipa_remove_all_references (&node->ref_list);
1493 ipa_remove_all_refering (&node->ref_list);
1494 VEC_free (ipa_opt_pass, heap,
1495 node->ipa_transforms_to_apply);
1497 /* Incremental inlining access removed nodes stored in the postorder list.
1499 node->needed = node->reachable = false;
1500 for (n = node->nested; n; n = n->next_nested)
1501 n->origin = NULL;
1502 node->nested = NULL;
1503 if (node->origin)
1505 struct cgraph_node **node2 = &node->origin->nested;
1507 while (*node2 != node)
1508 node2 = &(*node2)->next_nested;
1509 *node2 = node->next_nested;
1511 if (node->previous)
1512 node->previous->next = node->next;
1513 else
1514 cgraph_nodes = node->next;
1515 if (node->next)
1516 node->next->previous = node->previous;
1517 node->next = NULL;
1518 node->previous = NULL;
1519 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1520 if (*slot == node)
1522 struct cgraph_node *next_inline_clone;
1524 for (next_inline_clone = node->clones;
1525 next_inline_clone && next_inline_clone->decl != node->decl;
1526 next_inline_clone = next_inline_clone->next_sibling_clone)
1529 /* If there is inline clone of the node being removed, we need
1530 to put it into the position of removed node and reorganize all
1531 other clones to be based on it. */
1532 if (next_inline_clone)
1534 struct cgraph_node *n;
1535 struct cgraph_node *new_clones;
1537 *slot = next_inline_clone;
1539 /* Unlink inline clone from the list of clones of removed node. */
1540 if (next_inline_clone->next_sibling_clone)
1541 next_inline_clone->next_sibling_clone->prev_sibling_clone
1542 = next_inline_clone->prev_sibling_clone;
1543 if (next_inline_clone->prev_sibling_clone)
1545 gcc_assert (node->clones != next_inline_clone);
1546 next_inline_clone->prev_sibling_clone->next_sibling_clone
1547 = next_inline_clone->next_sibling_clone;
1549 else
1551 gcc_assert (node->clones == next_inline_clone);
1552 node->clones = next_inline_clone->next_sibling_clone;
1555 new_clones = node->clones;
1556 node->clones = NULL;
1558 /* Copy clone info. */
1559 next_inline_clone->clone = node->clone;
1561 /* Now place it into clone tree at same level at NODE. */
1562 next_inline_clone->clone_of = node->clone_of;
1563 next_inline_clone->prev_sibling_clone = NULL;
1564 next_inline_clone->next_sibling_clone = NULL;
1565 if (node->clone_of)
1567 if (node->clone_of->clones)
1568 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1569 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1570 node->clone_of->clones = next_inline_clone;
1573 /* Merge the clone list. */
1574 if (new_clones)
1576 if (!next_inline_clone->clones)
1577 next_inline_clone->clones = new_clones;
1578 else
1580 n = next_inline_clone->clones;
1581 while (n->next_sibling_clone)
1582 n = n->next_sibling_clone;
1583 n->next_sibling_clone = new_clones;
1584 new_clones->prev_sibling_clone = n;
1588 /* Update clone_of pointers. */
1589 n = new_clones;
1590 while (n)
1592 n->clone_of = next_inline_clone;
1593 n = n->next_sibling_clone;
1596 else
1598 htab_clear_slot (cgraph_hash, slot);
1599 kill_body = true;
1603 if (node->prev_sibling_clone)
1604 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1605 else if (node->clone_of)
1606 node->clone_of->clones = node->next_sibling_clone;
1607 if (node->next_sibling_clone)
1608 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1609 if (node->clones)
1611 struct cgraph_node *n, *next;
1613 if (node->clone_of)
1615 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1616 n->clone_of = node->clone_of;
1617 n->clone_of = node->clone_of;
1618 n->next_sibling_clone = node->clone_of->clones;
1619 if (node->clone_of->clones)
1620 node->clone_of->clones->prev_sibling_clone = n;
1621 node->clone_of->clones = node->clones;
1623 else
1625 /* We are removing node with clones. this makes clones inconsistent,
1626 but assume they will be removed subsequently and just keep clone
1627 tree intact. This can happen in unreachable function removal since
1628 we remove unreachable functions in random order, not by bottom-up
1629 walk of clone trees. */
1630 for (n = node->clones; n; n = next)
1632 next = n->next_sibling_clone;
1633 n->next_sibling_clone = NULL;
1634 n->prev_sibling_clone = NULL;
1635 n->clone_of = NULL;
1640 while (node->same_body)
1641 cgraph_remove_same_body_alias (node->same_body);
1643 if (node->same_comdat_group)
1645 struct cgraph_node *prev;
1646 for (prev = node->same_comdat_group;
1647 prev->same_comdat_group != node;
1648 prev = prev->same_comdat_group)
1650 if (node->same_comdat_group == prev)
1651 prev->same_comdat_group = NULL;
1652 else
1653 prev->same_comdat_group = node->same_comdat_group;
1654 node->same_comdat_group = NULL;
1657 /* While all the clones are removed after being proceeded, the function
1658 itself is kept in the cgraph even after it is compiled. Check whether
1659 we are done with this body and reclaim it proactively if this is the case.
1661 if (!kill_body && *slot)
1663 struct cgraph_node *n = (struct cgraph_node *) *slot;
1664 if (!n->clones && !n->clone_of && !n->global.inlined_to
1665 && (cgraph_global_info_ready
1666 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1667 || n->in_other_partition)))
1668 kill_body = true;
1670 if (assembler_name_hash)
1672 tree name = DECL_ASSEMBLER_NAME (node->decl);
1673 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1674 decl_assembler_name_hash (name),
1675 NO_INSERT);
1676 /* Inline clones are not hashed. */
1677 if (slot && *slot == node)
1678 htab_clear_slot (assembler_name_hash, slot);
1681 if (kill_body)
1682 cgraph_release_function_body (node);
1683 node->decl = NULL;
1684 if (node->call_site_hash)
1686 htab_delete (node->call_site_hash);
1687 node->call_site_hash = NULL;
1689 cgraph_n_nodes--;
1691 /* Clear out the node to NULL all pointers and add the node to the free
1692 list. */
1693 memset (node, 0, sizeof(*node));
1694 node->uid = uid;
1695 NEXT_FREE_NODE (node) = free_nodes;
1696 free_nodes = node;
1699 /* Remove the node from cgraph. */
1701 void
1702 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1704 struct cgraph_edge *e, *next;
1705 for (e = node->callees; e; e = next)
1707 next = e->next_callee;
1708 if (!e->inline_failed)
1709 cgraph_remove_node_and_inline_clones (e->callee);
1711 cgraph_remove_node (node);
1714 /* Notify finalize_compilation_unit that given node is reachable. */
1716 void
1717 cgraph_mark_reachable_node (struct cgraph_node *node)
1719 if (!node->reachable && node->local.finalized)
1721 if (cgraph_global_info_ready)
1723 /* Verify that function does not appear to be needed out of blue
1724 during the optimization process. This can happen for extern
1725 inlines when bodies was removed after inlining. */
1726 gcc_assert ((node->analyzed || node->in_other_partition
1727 || DECL_EXTERNAL (node->decl)));
1729 else
1730 notice_global_symbol (node->decl);
1731 node->reachable = 1;
1733 node->next_needed = cgraph_nodes_queue;
1734 cgraph_nodes_queue = node;
1738 /* Likewise indicate that a node is needed, i.e. reachable via some
1739 external means. */
1741 void
1742 cgraph_mark_needed_node (struct cgraph_node *node)
1744 node->needed = 1;
1745 gcc_assert (!node->global.inlined_to);
1746 cgraph_mark_reachable_node (node);
1749 /* Likewise indicate that a node is having address taken. */
1751 void
1752 cgraph_mark_address_taken_node (struct cgraph_node *node)
1754 gcc_assert (!node->global.inlined_to);
1755 cgraph_mark_reachable_node (node);
1756 node->address_taken = 1;
1759 /* Return local info for the compiled function. */
1761 struct cgraph_local_info *
1762 cgraph_local_info (tree decl)
1764 struct cgraph_node *node;
1766 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1767 node = cgraph_node (decl);
1768 return &node->local;
1771 /* Return local info for the compiled function. */
1773 struct cgraph_global_info *
1774 cgraph_global_info (tree decl)
1776 struct cgraph_node *node;
1778 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1779 node = cgraph_node (decl);
1780 return &node->global;
1783 /* Return local info for the compiled function. */
1785 struct cgraph_rtl_info *
1786 cgraph_rtl_info (tree decl)
1788 struct cgraph_node *node;
1790 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1791 node = cgraph_node (decl);
1792 if (decl != current_function_decl
1793 && !TREE_ASM_WRITTEN (node->decl))
1794 return NULL;
1795 return &node->rtl;
1798 /* Return a string describing the failure REASON. */
1800 const char*
1801 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1803 #undef DEFCIFCODE
1804 #define DEFCIFCODE(code, string) string,
1806 static const char *cif_string_table[CIF_N_REASONS] = {
1807 #include "cif-code.def"
1810 /* Signedness of an enum type is implementation defined, so cast it
1811 to unsigned before testing. */
1812 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1813 return cif_string_table[reason];
1816 /* Return name of the node used in debug output. */
1817 const char *
1818 cgraph_node_name (struct cgraph_node *node)
1820 return lang_hooks.decl_printable_name (node->decl, 2);
1823 /* Names used to print out the availability enum. */
1824 const char * const cgraph_availability_names[] =
1825 {"unset", "not_available", "overwritable", "available", "local"};
1828 /* Dump call graph node NODE to file F. */
1830 void
1831 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1833 struct cgraph_edge *edge;
1834 int indirect_calls_count = 0;
1836 fprintf (f, "%s/%i(%i)", cgraph_node_name (node), node->uid,
1837 node->pid);
1838 dump_addr (f, " @", (void *)node);
1839 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1840 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1841 if (node->global.inlined_to)
1842 fprintf (f, " (inline copy in %s/%i)",
1843 cgraph_node_name (node->global.inlined_to),
1844 node->global.inlined_to->uid);
1845 if (node->same_comdat_group)
1846 fprintf (f, " (same comdat group as %s/%i)",
1847 cgraph_node_name (node->same_comdat_group),
1848 node->same_comdat_group->uid);
1849 if (node->clone_of)
1850 fprintf (f, " (clone of %s/%i)",
1851 cgraph_node_name (node->clone_of),
1852 node->clone_of->uid);
1853 if (cgraph_function_flags_ready)
1854 fprintf (f, " availability:%s",
1855 cgraph_availability_names [cgraph_function_body_availability (node)]);
1856 if (node->analyzed)
1857 fprintf (f, " analyzed");
1858 if (node->in_other_partition)
1859 fprintf (f, " in_other_partition");
1860 if (node->count)
1861 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1862 (HOST_WIDEST_INT)node->count);
1863 if (node->local.inline_summary.self_time)
1864 fprintf (f, " %i time, %i benefit", node->local.inline_summary.self_time,
1865 node->local.inline_summary.time_inlining_benefit);
1866 if (node->global.time && node->global.time
1867 != node->local.inline_summary.self_time)
1868 fprintf (f, " (%i after inlining)", node->global.time);
1869 if (node->local.inline_summary.self_size)
1870 fprintf (f, " %i size, %i benefit", node->local.inline_summary.self_size,
1871 node->local.inline_summary.size_inlining_benefit);
1872 if (node->global.size && node->global.size
1873 != node->local.inline_summary.self_size)
1874 fprintf (f, " (%i after inlining)", node->global.size);
1875 if (node->local.inline_summary.estimated_self_stack_size)
1876 fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1877 if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
1878 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
1879 if (node->origin)
1880 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1881 if (node->needed)
1882 fprintf (f, " needed");
1883 if (node->address_taken)
1884 fprintf (f, " address_taken");
1885 else if (node->reachable)
1886 fprintf (f, " reachable");
1887 else if (node->reachable_from_other_partition)
1888 fprintf (f, " reachable_from_other_partition");
1889 if (gimple_has_body_p (node->decl))
1890 fprintf (f, " body");
1891 if (node->process)
1892 fprintf (f, " process");
1893 if (node->local.local)
1894 fprintf (f, " local");
1895 if (node->local.externally_visible)
1896 fprintf (f, " externally_visible");
1897 if (node->resolution != LDPR_UNKNOWN)
1898 fprintf (f, " %s",
1899 ld_plugin_symbol_resolution_names[(int)node->resolution]);
1900 if (node->local.finalized)
1901 fprintf (f, " finalized");
1902 if (node->local.disregard_inline_limits)
1903 fprintf (f, " always_inline");
1904 else if (node->local.inlinable)
1905 fprintf (f, " inlinable");
1906 else if (node->local.versionable)
1907 fprintf (f, " versionable");
1908 if (node->local.redefined_extern_inline)
1909 fprintf (f, " redefined_extern_inline");
1910 if (TREE_ASM_WRITTEN (node->decl))
1911 fprintf (f, " asm_written");
1912 if (node->only_called_at_startup)
1913 fprintf (f, " only_called_at_startup");
1914 if (node->only_called_at_exit)
1915 fprintf (f, " only_called_at_exit");
1917 fprintf (f, "\n called by: ");
1918 for (edge = node->callers; edge; edge = edge->next_caller)
1920 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1921 edge->caller->uid);
1922 if (edge->count)
1923 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1924 (HOST_WIDEST_INT)edge->count);
1925 if (edge->frequency)
1926 fprintf (f, "(%.2f per call) ",
1927 edge->frequency / (double)CGRAPH_FREQ_BASE);
1928 if (!edge->inline_failed)
1929 fprintf(f, "(inlined) ");
1930 if (edge->indirect_inlining_edge)
1931 fprintf(f, "(indirect_inlining) ");
1932 if (edge->can_throw_external)
1933 fprintf(f, "(can throw external) ");
1936 fprintf (f, "\n calls: ");
1937 for (edge = node->callees; edge; edge = edge->next_callee)
1939 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1940 edge->callee->uid);
1941 if (!edge->inline_failed)
1942 fprintf(f, "(inlined) ");
1943 if (edge->indirect_inlining_edge)
1944 fprintf(f, "(indirect_inlining) ");
1945 if (edge->count)
1946 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1947 (HOST_WIDEST_INT)edge->count);
1948 if (edge->frequency)
1949 fprintf (f, "(%.2f per call) ",
1950 edge->frequency / (double)CGRAPH_FREQ_BASE);
1951 if (edge->loop_nest)
1952 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1953 if (edge->can_throw_external)
1954 fprintf(f, "(can throw external) ");
1956 fprintf (f, "\n");
1957 fprintf (f, " References: ");
1958 ipa_dump_references (f, &node->ref_list);
1959 fprintf (f, " Refering this function: ");
1960 ipa_dump_refering (f, &node->ref_list);
1962 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1963 indirect_calls_count++;
1964 if (indirect_calls_count)
1965 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1966 indirect_calls_count);
1968 if (node->same_body)
1970 struct cgraph_node *n;
1971 fprintf (f, " aliases & thunks:");
1972 for (n = node->same_body; n; n = n->next)
1974 fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1975 if (n->thunk.thunk_p)
1977 fprintf (f, " (thunk of %s fixed offset %i virtual value %i has "
1978 "virtual offset %i",
1979 lang_hooks.decl_printable_name (n->thunk.alias, 2),
1980 (int)n->thunk.fixed_offset,
1981 (int)n->thunk.virtual_value,
1982 (int)n->thunk.virtual_offset_p);
1983 fprintf (f, ")");
1985 if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1986 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1988 fprintf (f, "\n");
1993 /* Dump call graph node NODE to stderr. */
1995 DEBUG_FUNCTION void
1996 debug_cgraph_node (struct cgraph_node *node)
1998 dump_cgraph_node (stderr, node);
2002 /* Dump the callgraph to file F. */
2004 void
2005 dump_cgraph (FILE *f)
2007 struct cgraph_node *node;
2009 fprintf (f, "callgraph:\n\n");
2010 for (node = cgraph_nodes; node; node = node->next)
2011 dump_cgraph_node (f, node);
2015 /* Dump the call graph to stderr. */
2017 DEBUG_FUNCTION void
2018 debug_cgraph (void)
2020 dump_cgraph (stderr);
2024 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
2026 void
2027 change_decl_assembler_name (tree decl, tree name)
2029 struct cgraph_node *node;
2030 void **slot;
2031 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
2032 SET_DECL_ASSEMBLER_NAME (decl, name);
2033 else
2035 if (name == DECL_ASSEMBLER_NAME (decl))
2036 return;
2038 if (assembler_name_hash
2039 && TREE_CODE (decl) == FUNCTION_DECL
2040 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2042 tree old_name = DECL_ASSEMBLER_NAME (decl);
2043 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2044 decl_assembler_name_hash (old_name),
2045 NO_INSERT);
2046 /* Inline clones are not hashed. */
2047 if (slot && *slot == node)
2048 htab_clear_slot (assembler_name_hash, slot);
2050 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2051 && DECL_RTL_SET_P (decl))
2052 warning (0, "%D renamed after being referenced in assembly", decl);
2054 SET_DECL_ASSEMBLER_NAME (decl, name);
2056 if (assembler_name_hash
2057 && TREE_CODE (decl) == FUNCTION_DECL
2058 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2060 slot = htab_find_slot_with_hash (assembler_name_hash, name,
2061 decl_assembler_name_hash (name),
2062 INSERT);
2063 gcc_assert (!*slot);
2064 *slot = node;
2068 /* Add a top-level asm statement to the list. */
2070 struct cgraph_asm_node *
2071 cgraph_add_asm_node (tree asm_str)
2073 struct cgraph_asm_node *node;
2075 node = ggc_alloc_cleared_cgraph_asm_node ();
2076 node->asm_str = asm_str;
2077 node->order = cgraph_order++;
2078 node->next = NULL;
2079 if (cgraph_asm_nodes == NULL)
2080 cgraph_asm_nodes = node;
2081 else
2082 cgraph_asm_last_node->next = node;
2083 cgraph_asm_last_node = node;
2084 return node;
2087 /* Return true when the DECL can possibly be inlined. */
2088 bool
2089 cgraph_function_possibly_inlined_p (tree decl)
2091 if (!cgraph_global_info_ready)
2092 return !DECL_UNINLINABLE (decl);
2093 return DECL_POSSIBLY_INLINED (decl);
2096 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2097 struct cgraph_edge *
2098 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2099 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2100 int freq_scale, int loop_nest, bool update_original)
2102 struct cgraph_edge *new_edge;
2103 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2104 gcov_type freq;
2106 /* We do not want to ignore loop nest after frequency drops to 0. */
2107 if (!freq_scale)
2108 freq_scale = 1;
2109 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2110 if (freq > CGRAPH_FREQ_MAX)
2111 freq = CGRAPH_FREQ_MAX;
2113 if (e->indirect_unknown_callee)
2115 tree decl;
2117 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2119 struct cgraph_node *callee = cgraph_node (decl);
2120 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq,
2121 e->loop_nest + loop_nest);
2123 else
2125 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2126 e->indirect_info->ecf_flags,
2127 count, freq,
2128 e->loop_nest + loop_nest);
2129 *new_edge->indirect_info = *e->indirect_info;
2132 else
2134 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
2135 e->loop_nest + loop_nest);
2136 if (e->indirect_info)
2138 new_edge->indirect_info
2139 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2140 *new_edge->indirect_info = *e->indirect_info;
2144 new_edge->inline_failed = e->inline_failed;
2145 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2146 new_edge->lto_stmt_uid = stmt_uid;
2147 /* Clone flags that depend on call_stmt availability manually. */
2148 new_edge->can_throw_external = e->can_throw_external;
2149 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2150 if (update_original)
2152 e->count -= new_edge->count;
2153 if (e->count < 0)
2154 e->count = 0;
2156 cgraph_call_edge_duplication_hooks (e, new_edge);
2157 return new_edge;
2160 /* Create node representing clone of N executed COUNT times. Decrease
2161 the execution counts from original node too.
2162 The new clone will have decl set to DECL that may or may not be the same
2163 as decl of N.
2165 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2166 function's profile to reflect the fact that part of execution is handled
2167 by node. */
2168 struct cgraph_node *
2169 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2170 int loop_nest, bool update_original,
2171 VEC(cgraph_edge_p,heap) *redirect_callers)
2173 struct cgraph_node *new_node = cgraph_create_node ();
2174 struct cgraph_edge *e;
2175 gcov_type count_scale;
2176 unsigned i;
2178 new_node->decl = decl;
2179 new_node->origin = n->origin;
2180 if (new_node->origin)
2182 new_node->next_nested = new_node->origin->nested;
2183 new_node->origin->nested = new_node;
2185 new_node->analyzed = n->analyzed;
2186 new_node->local = n->local;
2187 new_node->local.externally_visible = false;
2188 new_node->local.local = true;
2189 new_node->local.vtable_method = false;
2190 new_node->global = n->global;
2191 new_node->rtl = n->rtl;
2192 new_node->count = count;
2193 new_node->frequency = n->frequency;
2194 new_node->clone = n->clone;
2195 new_node->clone.tree_map = 0;
2196 if (n->count)
2198 if (new_node->count > n->count)
2199 count_scale = REG_BR_PROB_BASE;
2200 else
2201 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2203 else
2204 count_scale = 0;
2205 if (update_original)
2207 n->count -= count;
2208 if (n->count < 0)
2209 n->count = 0;
2212 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2214 /* Redirect calls to the old version node to point to its new
2215 version. */
2216 cgraph_redirect_edge_callee (e, new_node);
2220 for (e = n->callees;e; e=e->next_callee)
2221 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2222 count_scale, freq, loop_nest, update_original);
2224 for (e = n->indirect_calls; e; e = e->next_callee)
2225 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2226 count_scale, freq, loop_nest, update_original);
2227 ipa_clone_references (new_node, NULL, &n->ref_list);
2229 new_node->next_sibling_clone = n->clones;
2230 if (n->clones)
2231 n->clones->prev_sibling_clone = new_node;
2232 n->clones = new_node;
2233 new_node->clone_of = n;
2235 cgraph_call_node_duplication_hooks (n, new_node);
2236 if (n->decl != decl)
2238 struct cgraph_node **slot;
2239 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2240 gcc_assert (!*slot);
2241 *slot = new_node;
2242 if (assembler_name_hash)
2244 void **aslot;
2245 tree name = DECL_ASSEMBLER_NAME (decl);
2247 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2248 decl_assembler_name_hash (name),
2249 INSERT);
2250 gcc_assert (!*aslot);
2251 *aslot = new_node;
2254 return new_node;
2257 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2259 static GTY(()) unsigned int clone_fn_id_num;
2261 tree
2262 clone_function_name (tree decl, const char *suffix)
2264 tree name = DECL_ASSEMBLER_NAME (decl);
2265 size_t len = IDENTIFIER_LENGTH (name);
2266 char *tmp_name, *prefix;
2268 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2269 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2270 strcpy (prefix + len + 1, suffix);
2271 #ifndef NO_DOT_IN_LABEL
2272 prefix[len] = '.';
2273 #elif !defined NO_DOLLAR_IN_LABEL
2274 prefix[len] = '$';
2275 #else
2276 prefix[len] = '_';
2277 #endif
2278 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2279 return get_identifier (tmp_name);
2282 /* Create callgraph node clone with new declaration. The actual body will
2283 be copied later at compilation stage.
2285 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2286 bitmap interface.
2288 struct cgraph_node *
2289 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2290 VEC(cgraph_edge_p,heap) *redirect_callers,
2291 VEC(ipa_replace_map_p,gc) *tree_map,
2292 bitmap args_to_skip,
2293 const char * suffix)
2295 tree old_decl = old_node->decl;
2296 struct cgraph_node *new_node = NULL;
2297 tree new_decl;
2298 size_t i;
2299 struct ipa_replace_map *map;
2301 if (!flag_wpa)
2302 gcc_checking_assert (tree_versionable_function_p (old_decl));
2304 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2306 /* Make a new FUNCTION_DECL tree node */
2307 if (!args_to_skip)
2308 new_decl = copy_node (old_decl);
2309 else
2310 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2311 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2313 /* Generate a new name for the new version. */
2314 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2315 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2316 SET_DECL_RTL (new_decl, NULL);
2318 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2319 CGRAPH_FREQ_BASE, 0, false,
2320 redirect_callers);
2321 /* Update the properties.
2322 Make clone visible only within this translation unit. Make sure
2323 that is not weak also.
2324 ??? We cannot use COMDAT linkage because there is no
2325 ABI support for this. */
2326 DECL_EXTERNAL (new_node->decl) = 0;
2327 if (DECL_ONE_ONLY (old_decl))
2328 DECL_SECTION_NAME (new_node->decl) = NULL;
2329 DECL_COMDAT_GROUP (new_node->decl) = 0;
2330 TREE_PUBLIC (new_node->decl) = 0;
2331 DECL_COMDAT (new_node->decl) = 0;
2332 DECL_WEAK (new_node->decl) = 0;
2333 new_node->clone.tree_map = tree_map;
2334 new_node->clone.args_to_skip = args_to_skip;
2335 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2337 tree var = map->new_tree;
2339 STRIP_NOPS (var);
2340 if (TREE_CODE (var) != ADDR_EXPR)
2341 continue;
2342 var = get_base_var (var);
2343 if (!var)
2344 continue;
2346 /* Record references of the future statement initializing the constant
2347 argument. */
2348 if (TREE_CODE (var) == FUNCTION_DECL)
2349 ipa_record_reference (new_node, NULL, cgraph_node (var),
2350 NULL, IPA_REF_ADDR, NULL);
2351 else if (TREE_CODE (var) == VAR_DECL)
2352 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2353 IPA_REF_ADDR, NULL);
2355 if (!args_to_skip)
2356 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2357 else if (old_node->clone.combined_args_to_skip)
2359 int newi = 0, oldi = 0;
2360 tree arg;
2361 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2362 struct cgraph_node *orig_node;
2363 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2365 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2367 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2369 bitmap_set_bit (new_args_to_skip, oldi);
2370 continue;
2372 if (bitmap_bit_p (args_to_skip, newi))
2373 bitmap_set_bit (new_args_to_skip, oldi);
2374 newi++;
2376 new_node->clone.combined_args_to_skip = new_args_to_skip;
2378 else
2379 new_node->clone.combined_args_to_skip = args_to_skip;
2380 new_node->local.externally_visible = 0;
2381 new_node->local.local = 1;
2382 new_node->lowered = true;
2383 new_node->reachable = true;
2386 return new_node;
2389 /* NODE is no longer nested function; update cgraph accordingly. */
2390 void
2391 cgraph_unnest_node (struct cgraph_node *node)
2393 struct cgraph_node **node2 = &node->origin->nested;
2394 gcc_assert (node->origin);
2396 while (*node2 != node)
2397 node2 = &(*node2)->next_nested;
2398 *node2 = node->next_nested;
2399 node->origin = NULL;
2402 /* Return function availability. See cgraph.h for description of individual
2403 return values. */
2404 enum availability
2405 cgraph_function_body_availability (struct cgraph_node *node)
2407 enum availability avail;
2408 gcc_assert (cgraph_function_flags_ready);
2409 if (!node->analyzed)
2410 avail = AVAIL_NOT_AVAILABLE;
2411 else if (node->local.local)
2412 avail = AVAIL_LOCAL;
2413 else if (!node->local.externally_visible)
2414 avail = AVAIL_AVAILABLE;
2415 /* Inline functions are safe to be analyzed even if their symbol can
2416 be overwritten at runtime. It is not meaningful to enforce any sane
2417 behaviour on replacing inline function by different body. */
2418 else if (DECL_DECLARED_INLINE_P (node->decl))
2419 avail = AVAIL_AVAILABLE;
2421 /* If the function can be overwritten, return OVERWRITABLE. Take
2422 care at least of two notable extensions - the COMDAT functions
2423 used to share template instantiations in C++ (this is symmetric
2424 to code cp_cannot_inline_tree_fn and probably shall be shared and
2425 the inlinability hooks completely eliminated).
2427 ??? Does the C++ one definition rule allow us to always return
2428 AVAIL_AVAILABLE here? That would be good reason to preserve this
2429 bit. */
2431 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2432 avail = AVAIL_OVERWRITABLE;
2433 else avail = AVAIL_AVAILABLE;
2435 return avail;
2438 /* Add the function FNDECL to the call graph.
2439 Unlike cgraph_finalize_function, this function is intended to be used
2440 by middle end and allows insertion of new function at arbitrary point
2441 of compilation. The function can be either in high, low or SSA form
2442 GIMPLE.
2444 The function is assumed to be reachable and have address taken (so no
2445 API breaking optimizations are performed on it).
2447 Main work done by this function is to enqueue the function for later
2448 processing to avoid need the passes to be re-entrant. */
2450 void
2451 cgraph_add_new_function (tree fndecl, bool lowered)
2453 struct cgraph_node *node;
2454 switch (cgraph_state)
2456 case CGRAPH_STATE_CONSTRUCTION:
2457 /* Just enqueue function to be processed at nearest occurrence. */
2458 node = cgraph_node (fndecl);
2459 node->next_needed = cgraph_new_nodes;
2460 if (lowered)
2461 node->lowered = true;
2462 cgraph_new_nodes = node;
2463 break;
2465 case CGRAPH_STATE_IPA:
2466 case CGRAPH_STATE_IPA_SSA:
2467 case CGRAPH_STATE_EXPANSION:
2468 /* Bring the function into finalized state and enqueue for later
2469 analyzing and compilation. */
2470 node = cgraph_node (fndecl);
2471 node->local.local = false;
2472 node->local.finalized = true;
2473 node->reachable = node->needed = true;
2474 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2476 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2477 current_function_decl = fndecl;
2478 gimple_register_cfg_hooks ();
2479 tree_lowering_passes (fndecl);
2480 bitmap_obstack_initialize (NULL);
2481 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2482 execute_pass_list (pass_early_local_passes.pass.sub);
2483 bitmap_obstack_release (NULL);
2484 pop_cfun ();
2485 current_function_decl = NULL;
2487 lowered = true;
2489 if (lowered)
2490 node->lowered = true;
2491 node->next_needed = cgraph_new_nodes;
2492 cgraph_new_nodes = node;
2493 break;
2495 case CGRAPH_STATE_FINISHED:
2496 /* At the very end of compilation we have to do all the work up
2497 to expansion. */
2498 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2499 current_function_decl = fndecl;
2500 gimple_register_cfg_hooks ();
2501 if (!lowered)
2502 tree_lowering_passes (fndecl);
2503 bitmap_obstack_initialize (NULL);
2504 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2505 execute_pass_list (pass_early_local_passes.pass.sub);
2506 bitmap_obstack_release (NULL);
2507 tree_rest_of_compilation (fndecl);
2508 pop_cfun ();
2509 current_function_decl = NULL;
2510 break;
2513 /* Set a personality if required and we already passed EH lowering. */
2514 if (lowered
2515 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2516 == eh_personality_lang))
2517 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2520 /* Return true if NODE can be made local for API change.
2521 Extern inline functions and C++ COMDAT functions can be made local
2522 at the expense of possible code size growth if function is used in multiple
2523 compilation units. */
2524 bool
2525 cgraph_node_can_be_local_p (struct cgraph_node *node)
2527 return (!node->needed && !node->address_taken
2528 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2529 || !node->local.externally_visible));
2532 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2533 but other code such as notice_global_symbol generates rtl. */
2534 void
2535 cgraph_make_decl_local (tree decl)
2537 rtx rtl, symbol;
2539 if (TREE_CODE (decl) == VAR_DECL)
2540 DECL_COMMON (decl) = 0;
2541 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2543 if (DECL_COMDAT (decl))
2545 /* It is possible that we are linking against library defining same COMDAT
2546 function. To avoid conflict we need to rename our local name of the
2547 function just in the case WHOPR partitioning decide to make it hidden
2548 to avoid cross partition references. */
2549 if (flag_wpa)
2551 const char *old_name;
2553 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2554 if (TREE_CODE (decl) == FUNCTION_DECL)
2556 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2557 change_decl_assembler_name (decl,
2558 clone_function_name (decl, "local"));
2559 if (node->local.lto_file_data)
2560 lto_record_renamed_decl (node->local.lto_file_data,
2561 old_name,
2562 IDENTIFIER_POINTER
2563 (DECL_ASSEMBLER_NAME (decl)));
2565 else if (TREE_CODE (decl) == VAR_DECL)
2567 struct varpool_node *vnode = varpool_get_node (decl);
2568 /* change_decl_assembler_name will warn here on vtables because
2569 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2570 SET_DECL_ASSEMBLER_NAME (decl,
2571 clone_function_name (decl, "local"));
2572 if (vnode->lto_file_data)
2573 lto_record_renamed_decl (vnode->lto_file_data,
2574 old_name,
2575 IDENTIFIER_POINTER
2576 (DECL_ASSEMBLER_NAME (decl)));
2579 DECL_SECTION_NAME (decl) = 0;
2580 DECL_COMDAT (decl) = 0;
2582 DECL_COMDAT_GROUP (decl) = 0;
2583 DECL_WEAK (decl) = 0;
2584 DECL_EXTERNAL (decl) = 0;
2585 TREE_PUBLIC (decl) = 0;
2586 if (!DECL_RTL_SET_P (decl))
2587 return;
2589 /* Update rtl flags. */
2590 make_decl_rtl (decl);
2592 rtl = DECL_RTL (decl);
2593 if (!MEM_P (rtl))
2594 return;
2596 symbol = XEXP (rtl, 0);
2597 if (GET_CODE (symbol) != SYMBOL_REF)
2598 return;
2600 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2603 /* Bring NODE local. */
2604 void
2605 cgraph_make_node_local (struct cgraph_node *node)
2607 gcc_assert (cgraph_node_can_be_local_p (node));
2608 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2610 struct cgraph_node *alias;
2611 cgraph_make_decl_local (node->decl);
2613 for (alias = node->same_body; alias; alias = alias->next)
2614 cgraph_make_decl_local (alias->decl);
2616 node->local.externally_visible = false;
2617 node->local.local = true;
2618 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2619 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2623 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2624 if any to NOTHROW. */
2626 void
2627 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2629 struct cgraph_node *alias;
2630 TREE_NOTHROW (node->decl) = nothrow;
2631 for (alias = node->same_body; alias; alias = alias->next)
2632 TREE_NOTHROW (alias->decl) = nothrow;
2635 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2636 if any to READONLY. */
2638 void
2639 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2641 struct cgraph_node *alias;
2642 /* Static constructors and destructors without a side effect can be
2643 optimized out. */
2644 if (!looping && readonly)
2646 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2647 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2648 if (DECL_STATIC_DESTRUCTOR (node->decl))
2649 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2651 TREE_READONLY (node->decl) = readonly;
2652 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2653 for (alias = node->same_body; alias; alias = alias->next)
2655 TREE_READONLY (alias->decl) = readonly;
2656 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2660 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2661 if any to PURE. */
2663 void
2664 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2666 struct cgraph_node *alias;
2667 /* Static constructors and destructors without a side effect can be
2668 optimized out. */
2669 if (!looping && pure)
2671 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2672 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2673 if (DECL_STATIC_DESTRUCTOR (node->decl))
2674 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2676 DECL_PURE_P (node->decl) = pure;
2677 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2678 for (alias = node->same_body; alias; alias = alias->next)
2680 DECL_PURE_P (alias->decl) = pure;
2681 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2685 /* See if the frequency of NODE can be updated based on frequencies of its
2686 callers. */
2687 bool
2688 cgraph_propagate_frequency (struct cgraph_node *node)
2690 bool maybe_unlikely_executed = true, maybe_executed_once = true;
2691 bool only_called_at_startup = true;
2692 bool only_called_at_exit = true;
2693 bool changed = false;
2694 struct cgraph_edge *edge;
2696 if (!node->local.local)
2697 return false;
2698 gcc_assert (node->analyzed);
2699 if (dump_file && (dump_flags & TDF_DETAILS))
2700 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2702 for (edge = node->callers;
2703 edge && (maybe_unlikely_executed || maybe_executed_once
2704 || only_called_at_startup || only_called_at_exit);
2705 edge = edge->next_caller)
2707 if (edge->caller != node)
2709 only_called_at_startup &= edge->caller->only_called_at_startup;
2710 /* It makes sense to put main() together with the static constructors.
2711 It will be executed for sure, but rest of functions called from
2712 main are definitely not at startup only. */
2713 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2714 only_called_at_startup = 0;
2715 only_called_at_exit &= edge->caller->only_called_at_exit;
2717 if (!edge->frequency)
2718 continue;
2719 switch (edge->caller->frequency)
2721 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2722 break;
2723 case NODE_FREQUENCY_EXECUTED_ONCE:
2724 if (dump_file && (dump_flags & TDF_DETAILS))
2725 fprintf (dump_file, " Called by %s that is executed once\n",
2726 cgraph_node_name (edge->caller));
2727 maybe_unlikely_executed = false;
2728 if (edge->loop_nest)
2730 maybe_executed_once = false;
2731 if (dump_file && (dump_flags & TDF_DETAILS))
2732 fprintf (dump_file, " Called in loop\n");
2734 break;
2735 case NODE_FREQUENCY_HOT:
2736 case NODE_FREQUENCY_NORMAL:
2737 if (dump_file && (dump_flags & TDF_DETAILS))
2738 fprintf (dump_file, " Called by %s that is normal or hot\n",
2739 cgraph_node_name (edge->caller));
2740 maybe_unlikely_executed = false;
2741 maybe_executed_once = false;
2742 break;
2745 if ((only_called_at_startup && !only_called_at_exit)
2746 && !node->only_called_at_startup)
2748 node->only_called_at_startup = true;
2749 if (dump_file)
2750 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2751 cgraph_node_name (node));
2752 changed = true;
2754 if ((only_called_at_exit && !only_called_at_startup)
2755 && !node->only_called_at_exit)
2757 node->only_called_at_exit = true;
2758 if (dump_file)
2759 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2760 cgraph_node_name (node));
2761 changed = true;
2763 /* These come either from profile or user hints; never update them. */
2764 if (node->frequency == NODE_FREQUENCY_HOT
2765 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2766 return changed;
2767 if (maybe_unlikely_executed)
2769 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2770 if (dump_file)
2771 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2772 cgraph_node_name (node));
2773 changed = true;
2775 else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2777 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2778 if (dump_file)
2779 fprintf (dump_file, "Node %s promoted to executed once.\n",
2780 cgraph_node_name (node));
2781 changed = true;
2783 return changed;
2786 /* Return true when NODE can not return or throw and thus
2787 it is safe to ignore its side effects for IPA analysis. */
2789 bool
2790 cgraph_node_cannot_return (struct cgraph_node *node)
2792 int flags = flags_from_decl_or_type (node->decl);
2793 if (!flag_exceptions)
2794 return (flags & ECF_NORETURN) != 0;
2795 else
2796 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2797 == (ECF_NORETURN | ECF_NOTHROW));
2800 /* Return true when call of E can not lead to return from caller
2801 and thus it is safe to ignore its side effects for IPA analysis
2802 when computing side effects of the caller.
2803 FIXME: We could actually mark all edges that have no reaching
2804 patch to EXIT_BLOCK_PTR or throw to get better results. */
2805 bool
2806 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2808 if (cgraph_node_cannot_return (e->caller))
2809 return true;
2810 if (e->indirect_unknown_callee)
2812 int flags = e->indirect_info->ecf_flags;
2813 if (!flag_exceptions)
2814 return (flags & ECF_NORETURN) != 0;
2815 else
2816 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2817 == (ECF_NORETURN | ECF_NOTHROW));
2819 else
2820 return cgraph_node_cannot_return (e->callee);
2823 /* Return true when function NODE can be removed from callgraph
2824 if all direct calls are eliminated. */
2826 bool
2827 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2829 gcc_assert (!node->global.inlined_to);
2830 /* Extern inlines can always go, we will use the external definition. */
2831 if (DECL_EXTERNAL (node->decl))
2832 return true;
2833 /* When function is needed, we can not remove it. */
2834 if (node->needed || node->reachable_from_other_partition)
2835 return false;
2836 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2837 || DECL_STATIC_DESTRUCTOR (node->decl))
2838 return false;
2839 /* Only COMDAT functions can be removed if externally visible. */
2840 if (node->local.externally_visible
2841 && (!DECL_COMDAT (node->decl)
2842 || cgraph_used_from_object_file_p (node)))
2843 return false;
2844 return true;
2847 /* Return true when function NODE can be expected to be removed
2848 from program when direct calls in this compilation unit are removed.
2850 As a special case COMDAT functions are
2851 cgraph_can_remove_if_no_direct_calls_p while the are not
2852 cgraph_only_called_directly_p (it is possible they are called from other
2853 unit)
2855 This function behaves as cgraph_only_called_directly_p because eliminating
2856 all uses of COMDAT function does not make it necessarily disappear from
2857 the program unless we are compiling whole program or we do LTO. In this
2858 case we know we win since dynamic linking will not really discard the
2859 linkonce section. */
2861 bool
2862 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2864 gcc_assert (!node->global.inlined_to);
2865 if (cgraph_used_from_object_file_p (node))
2866 return false;
2867 if (!in_lto_p && !flag_whole_program)
2868 return cgraph_only_called_directly_p (node);
2869 else
2871 if (DECL_EXTERNAL (node->decl))
2872 return true;
2873 return cgraph_can_remove_if_no_direct_calls_p (node);
2877 /* Return true when RESOLUTION indicate that linker will use
2878 the symbol from non-LTO object files. */
2880 bool
2881 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2883 return (resolution == LDPR_PREVAILING_DEF
2884 || resolution == LDPR_PREEMPTED_REG
2885 || resolution == LDPR_RESOLVED_EXEC
2886 || resolution == LDPR_RESOLVED_DYN);
2889 /* Return true when NODE is known to be used from other (non-LTO) object file.
2890 Known only when doing LTO via linker plugin. */
2892 bool
2893 cgraph_used_from_object_file_p (struct cgraph_node *node)
2895 struct cgraph_node *alias;
2897 gcc_assert (!node->global.inlined_to);
2898 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2899 return false;
2900 if (resolution_used_from_other_file_p (node->resolution))
2901 return true;
2902 for (alias = node->same_body; alias; alias = alias->next)
2903 if (TREE_PUBLIC (alias->decl)
2904 && resolution_used_from_other_file_p (alias->resolution))
2905 return true;
2906 return false;
2909 #include "gt-cgraph.h"